Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /*
shoaib_ahmed 0:791a779d6220 2 * rdtarga.c
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Copyright (C) 1991-1996, Thomas G. Lane.
shoaib_ahmed 0:791a779d6220 5 * This file is part of the Independent JPEG Group's software.
shoaib_ahmed 0:791a779d6220 6 * For conditions of distribution and use, see the accompanying README file.
shoaib_ahmed 0:791a779d6220 7 *
shoaib_ahmed 0:791a779d6220 8 * This file contains routines to read input images in Targa format.
shoaib_ahmed 0:791a779d6220 9 *
shoaib_ahmed 0:791a779d6220 10 * These routines may need modification for non-Unix environments or
shoaib_ahmed 0:791a779d6220 11 * specialized applications. As they stand, they assume input from
shoaib_ahmed 0:791a779d6220 12 * an ordinary stdio stream. They further assume that reading begins
shoaib_ahmed 0:791a779d6220 13 * at the start of the file; start_input may need work if the
shoaib_ahmed 0:791a779d6220 14 * user interface has already read some data (e.g., to determine that
shoaib_ahmed 0:791a779d6220 15 * the file is indeed Targa format).
shoaib_ahmed 0:791a779d6220 16 *
shoaib_ahmed 0:791a779d6220 17 * Based on code contributed by Lee Daniel Crocker.
shoaib_ahmed 0:791a779d6220 18 */
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
shoaib_ahmed 0:791a779d6220 21
shoaib_ahmed 0:791a779d6220 22 #ifdef TARGA_SUPPORTED
shoaib_ahmed 0:791a779d6220 23
shoaib_ahmed 0:791a779d6220 24
shoaib_ahmed 0:791a779d6220 25 /* Macros to deal with unsigned chars as efficiently as compiler allows */
shoaib_ahmed 0:791a779d6220 26
shoaib_ahmed 0:791a779d6220 27 #ifdef HAVE_UNSIGNED_CHAR
shoaib_ahmed 0:791a779d6220 28 typedef unsigned char U_CHAR;
shoaib_ahmed 0:791a779d6220 29 #define UCH(x) ((int) (x))
shoaib_ahmed 0:791a779d6220 30 #else /* !HAVE_UNSIGNED_CHAR */
shoaib_ahmed 0:791a779d6220 31 #ifdef CHAR_IS_UNSIGNED
shoaib_ahmed 0:791a779d6220 32 typedef char U_CHAR;
shoaib_ahmed 0:791a779d6220 33 #define UCH(x) ((int) (x))
shoaib_ahmed 0:791a779d6220 34 #else
shoaib_ahmed 0:791a779d6220 35 typedef char U_CHAR;
shoaib_ahmed 0:791a779d6220 36 #define UCH(x) ((int) (x) & 0xFF)
shoaib_ahmed 0:791a779d6220 37 #endif
shoaib_ahmed 0:791a779d6220 38 #endif /* HAVE_UNSIGNED_CHAR */
shoaib_ahmed 0:791a779d6220 39
shoaib_ahmed 0:791a779d6220 40
shoaib_ahmed 0:791a779d6220 41 #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
shoaib_ahmed 0:791a779d6220 42
shoaib_ahmed 0:791a779d6220 43
shoaib_ahmed 0:791a779d6220 44 /* Private version of data source object */
shoaib_ahmed 0:791a779d6220 45
shoaib_ahmed 0:791a779d6220 46 typedef struct _tga_source_struct * tga_source_ptr;
shoaib_ahmed 0:791a779d6220 47
shoaib_ahmed 0:791a779d6220 48 typedef struct _tga_source_struct {
shoaib_ahmed 0:791a779d6220 49 struct cjpeg_source_struct pub; /* public fields */
shoaib_ahmed 0:791a779d6220 50
shoaib_ahmed 0:791a779d6220 51 j_compress_ptr cinfo; /* back link saves passing separate parm */
shoaib_ahmed 0:791a779d6220 52
shoaib_ahmed 0:791a779d6220 53 JSAMPARRAY colormap; /* Targa colormap (converted to my format) */
shoaib_ahmed 0:791a779d6220 54
shoaib_ahmed 0:791a779d6220 55 jvirt_sarray_ptr whole_image; /* Needed if funny input row order */
shoaib_ahmed 0:791a779d6220 56 JDIMENSION current_row; /* Current logical row number to read */
shoaib_ahmed 0:791a779d6220 57
shoaib_ahmed 0:791a779d6220 58 /* Pointer to routine to extract next Targa pixel from input file */
shoaib_ahmed 0:791a779d6220 59 JMETHOD(void, read_pixel, (tga_source_ptr sinfo));
shoaib_ahmed 0:791a779d6220 60
shoaib_ahmed 0:791a779d6220 61 /* Result of read_pixel is delivered here: */
shoaib_ahmed 0:791a779d6220 62 U_CHAR tga_pixel[4];
shoaib_ahmed 0:791a779d6220 63
shoaib_ahmed 0:791a779d6220 64 int pixel_size; /* Bytes per Targa pixel (1 to 4) */
shoaib_ahmed 0:791a779d6220 65
shoaib_ahmed 0:791a779d6220 66 /* State info for reading RLE-coded pixels; both counts must be init to 0 */
shoaib_ahmed 0:791a779d6220 67 int block_count; /* # of pixels remaining in RLE block */
shoaib_ahmed 0:791a779d6220 68 int dup_pixel_count; /* # of times to duplicate previous pixel */
shoaib_ahmed 0:791a779d6220 69
shoaib_ahmed 0:791a779d6220 70 /* This saves the correct pixel-row-expansion method for preload_image */
shoaib_ahmed 0:791a779d6220 71 JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo,
shoaib_ahmed 0:791a779d6220 72 cjpeg_source_ptr sinfo));
shoaib_ahmed 0:791a779d6220 73 } tga_source_struct;
shoaib_ahmed 0:791a779d6220 74
shoaib_ahmed 0:791a779d6220 75
shoaib_ahmed 0:791a779d6220 76 /* For expanding 5-bit pixel values to 8-bit with best rounding */
shoaib_ahmed 0:791a779d6220 77
shoaib_ahmed 0:791a779d6220 78 static const UINT8 c5to8bits[32] = {
shoaib_ahmed 0:791a779d6220 79 0, 8, 16, 25, 33, 41, 49, 58,
shoaib_ahmed 0:791a779d6220 80 66, 74, 82, 90, 99, 107, 115, 123,
shoaib_ahmed 0:791a779d6220 81 132, 140, 148, 156, 165, 173, 181, 189,
shoaib_ahmed 0:791a779d6220 82 197, 206, 214, 222, 230, 239, 247, 255
shoaib_ahmed 0:791a779d6220 83 };
shoaib_ahmed 0:791a779d6220 84
shoaib_ahmed 0:791a779d6220 85
shoaib_ahmed 0:791a779d6220 86
shoaib_ahmed 0:791a779d6220 87 LOCAL(int)
shoaib_ahmed 0:791a779d6220 88 read_byte (tga_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 89 /* Read next byte from Targa file */
shoaib_ahmed 0:791a779d6220 90 {
shoaib_ahmed 0:791a779d6220 91 register FILE *infile = sinfo->pub.input_file;
shoaib_ahmed 0:791a779d6220 92 register int c;
shoaib_ahmed 0:791a779d6220 93
shoaib_ahmed 0:791a779d6220 94 if ((c = getc(infile)) == EOF)
shoaib_ahmed 0:791a779d6220 95 ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
shoaib_ahmed 0:791a779d6220 96 return c;
shoaib_ahmed 0:791a779d6220 97 }
shoaib_ahmed 0:791a779d6220 98
shoaib_ahmed 0:791a779d6220 99
shoaib_ahmed 0:791a779d6220 100 LOCAL(void)
shoaib_ahmed 0:791a779d6220 101 read_colormap (tga_source_ptr sinfo, int cmaplen, int mapentrysize)
shoaib_ahmed 0:791a779d6220 102 /* Read the colormap from a Targa file */
shoaib_ahmed 0:791a779d6220 103 {
shoaib_ahmed 0:791a779d6220 104 int i;
shoaib_ahmed 0:791a779d6220 105
shoaib_ahmed 0:791a779d6220 106 /* Presently only handles 24-bit BGR format */
shoaib_ahmed 0:791a779d6220 107 if (mapentrysize != 24)
shoaib_ahmed 0:791a779d6220 108 ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP);
shoaib_ahmed 0:791a779d6220 109
shoaib_ahmed 0:791a779d6220 110 for (i = 0; i < cmaplen; i++) {
shoaib_ahmed 0:791a779d6220 111 sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo);
shoaib_ahmed 0:791a779d6220 112 sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo);
shoaib_ahmed 0:791a779d6220 113 sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo);
shoaib_ahmed 0:791a779d6220 114 }
shoaib_ahmed 0:791a779d6220 115 }
shoaib_ahmed 0:791a779d6220 116
shoaib_ahmed 0:791a779d6220 117
shoaib_ahmed 0:791a779d6220 118 /*
shoaib_ahmed 0:791a779d6220 119 * read_pixel methods: get a single pixel from Targa file into tga_pixel[]
shoaib_ahmed 0:791a779d6220 120 */
shoaib_ahmed 0:791a779d6220 121
shoaib_ahmed 0:791a779d6220 122 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 123 read_non_rle_pixel (tga_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 124 /* Read one Targa pixel from the input file; no RLE expansion */
shoaib_ahmed 0:791a779d6220 125 {
shoaib_ahmed 0:791a779d6220 126 register FILE *infile = sinfo->pub.input_file;
shoaib_ahmed 0:791a779d6220 127 register int i;
shoaib_ahmed 0:791a779d6220 128
shoaib_ahmed 0:791a779d6220 129 for (i = 0; i < sinfo->pixel_size; i++) {
shoaib_ahmed 0:791a779d6220 130 sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
shoaib_ahmed 0:791a779d6220 131 }
shoaib_ahmed 0:791a779d6220 132 }
shoaib_ahmed 0:791a779d6220 133
shoaib_ahmed 0:791a779d6220 134
shoaib_ahmed 0:791a779d6220 135 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 136 read_rle_pixel (tga_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 137 /* Read one Targa pixel from the input file, expanding RLE data as needed */
shoaib_ahmed 0:791a779d6220 138 {
shoaib_ahmed 0:791a779d6220 139 register FILE *infile = sinfo->pub.input_file;
shoaib_ahmed 0:791a779d6220 140 register int i;
shoaib_ahmed 0:791a779d6220 141
shoaib_ahmed 0:791a779d6220 142 /* Duplicate previously read pixel? */
shoaib_ahmed 0:791a779d6220 143 if (sinfo->dup_pixel_count > 0) {
shoaib_ahmed 0:791a779d6220 144 sinfo->dup_pixel_count--;
shoaib_ahmed 0:791a779d6220 145 return;
shoaib_ahmed 0:791a779d6220 146 }
shoaib_ahmed 0:791a779d6220 147
shoaib_ahmed 0:791a779d6220 148 /* Time to read RLE block header? */
shoaib_ahmed 0:791a779d6220 149 if (--sinfo->block_count < 0) { /* decrement pixels remaining in block */
shoaib_ahmed 0:791a779d6220 150 i = read_byte(sinfo);
shoaib_ahmed 0:791a779d6220 151 if (i & 0x80) { /* Start of duplicate-pixel block? */
shoaib_ahmed 0:791a779d6220 152 sinfo->dup_pixel_count = i & 0x7F; /* number of dups after this one */
shoaib_ahmed 0:791a779d6220 153 sinfo->block_count = 0; /* then read new block header */
shoaib_ahmed 0:791a779d6220 154 } else {
shoaib_ahmed 0:791a779d6220 155 sinfo->block_count = i & 0x7F; /* number of pixels after this one */
shoaib_ahmed 0:791a779d6220 156 }
shoaib_ahmed 0:791a779d6220 157 }
shoaib_ahmed 0:791a779d6220 158
shoaib_ahmed 0:791a779d6220 159 /* Read next pixel */
shoaib_ahmed 0:791a779d6220 160 for (i = 0; i < sinfo->pixel_size; i++) {
shoaib_ahmed 0:791a779d6220 161 sinfo->tga_pixel[i] = (U_CHAR) getc(infile);
shoaib_ahmed 0:791a779d6220 162 }
shoaib_ahmed 0:791a779d6220 163 }
shoaib_ahmed 0:791a779d6220 164
shoaib_ahmed 0:791a779d6220 165
shoaib_ahmed 0:791a779d6220 166 /*
shoaib_ahmed 0:791a779d6220 167 * Read one row of pixels.
shoaib_ahmed 0:791a779d6220 168 *
shoaib_ahmed 0:791a779d6220 169 * We provide several different versions depending on input file format.
shoaib_ahmed 0:791a779d6220 170 */
shoaib_ahmed 0:791a779d6220 171
shoaib_ahmed 0:791a779d6220 172
shoaib_ahmed 0:791a779d6220 173 METHODDEF(JDIMENSION)
shoaib_ahmed 0:791a779d6220 174 get_8bit_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 175 /* This version is for reading 8-bit grayscale pixels */
shoaib_ahmed 0:791a779d6220 176 {
shoaib_ahmed 0:791a779d6220 177 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 178 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 179 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 180
shoaib_ahmed 0:791a779d6220 181 ptr = source->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 182 for (col = cinfo->image_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 183 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
shoaib_ahmed 0:791a779d6220 184 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
shoaib_ahmed 0:791a779d6220 185 }
shoaib_ahmed 0:791a779d6220 186 return 1;
shoaib_ahmed 0:791a779d6220 187 }
shoaib_ahmed 0:791a779d6220 188
shoaib_ahmed 0:791a779d6220 189 METHODDEF(JDIMENSION)
shoaib_ahmed 0:791a779d6220 190 get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 191 /* This version is for reading 8-bit colormap indexes */
shoaib_ahmed 0:791a779d6220 192 {
shoaib_ahmed 0:791a779d6220 193 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 194 register int t;
shoaib_ahmed 0:791a779d6220 195 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 196 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 197 register JSAMPARRAY colormap = source->colormap;
shoaib_ahmed 0:791a779d6220 198
shoaib_ahmed 0:791a779d6220 199 ptr = source->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 200 for (col = cinfo->image_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 201 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
shoaib_ahmed 0:791a779d6220 202 t = UCH(source->tga_pixel[0]);
shoaib_ahmed 0:791a779d6220 203 *ptr++ = colormap[0][t];
shoaib_ahmed 0:791a779d6220 204 *ptr++ = colormap[1][t];
shoaib_ahmed 0:791a779d6220 205 *ptr++ = colormap[2][t];
shoaib_ahmed 0:791a779d6220 206 }
shoaib_ahmed 0:791a779d6220 207 return 1;
shoaib_ahmed 0:791a779d6220 208 }
shoaib_ahmed 0:791a779d6220 209
shoaib_ahmed 0:791a779d6220 210 METHODDEF(JDIMENSION)
shoaib_ahmed 0:791a779d6220 211 get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 212 /* This version is for reading 16-bit pixels */
shoaib_ahmed 0:791a779d6220 213 {
shoaib_ahmed 0:791a779d6220 214 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 215 register int t;
shoaib_ahmed 0:791a779d6220 216 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 217 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 218
shoaib_ahmed 0:791a779d6220 219 ptr = source->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 220 for (col = cinfo->image_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 221 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
shoaib_ahmed 0:791a779d6220 222 t = UCH(source->tga_pixel[0]);
shoaib_ahmed 0:791a779d6220 223 t += UCH(source->tga_pixel[1]) << 8;
shoaib_ahmed 0:791a779d6220 224 /* We expand 5 bit data to 8 bit sample width.
shoaib_ahmed 0:791a779d6220 225 * The format of the 16-bit (LSB first) input word is
shoaib_ahmed 0:791a779d6220 226 * xRRRRRGGGGGBBBBB
shoaib_ahmed 0:791a779d6220 227 */
shoaib_ahmed 0:791a779d6220 228 ptr[2] = (JSAMPLE) c5to8bits[t & 0x1F];
shoaib_ahmed 0:791a779d6220 229 t >>= 5;
shoaib_ahmed 0:791a779d6220 230 ptr[1] = (JSAMPLE) c5to8bits[t & 0x1F];
shoaib_ahmed 0:791a779d6220 231 t >>= 5;
shoaib_ahmed 0:791a779d6220 232 ptr[0] = (JSAMPLE) c5to8bits[t & 0x1F];
shoaib_ahmed 0:791a779d6220 233 ptr += 3;
shoaib_ahmed 0:791a779d6220 234 }
shoaib_ahmed 0:791a779d6220 235 return 1;
shoaib_ahmed 0:791a779d6220 236 }
shoaib_ahmed 0:791a779d6220 237
shoaib_ahmed 0:791a779d6220 238 METHODDEF(JDIMENSION)
shoaib_ahmed 0:791a779d6220 239 get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 240 /* This version is for reading 24-bit pixels */
shoaib_ahmed 0:791a779d6220 241 {
shoaib_ahmed 0:791a779d6220 242 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 243 register JSAMPROW ptr;
shoaib_ahmed 0:791a779d6220 244 register JDIMENSION col;
shoaib_ahmed 0:791a779d6220 245
shoaib_ahmed 0:791a779d6220 246 ptr = source->pub.buffer[0];
shoaib_ahmed 0:791a779d6220 247 for (col = cinfo->image_width; col > 0; col--) {
shoaib_ahmed 0:791a779d6220 248 (*source->read_pixel) (source); /* Load next pixel into tga_pixel */
shoaib_ahmed 0:791a779d6220 249 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[2]); /* change BGR to RGB order */
shoaib_ahmed 0:791a779d6220 250 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[1]);
shoaib_ahmed 0:791a779d6220 251 *ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]);
shoaib_ahmed 0:791a779d6220 252 }
shoaib_ahmed 0:791a779d6220 253 return 1;
shoaib_ahmed 0:791a779d6220 254 }
shoaib_ahmed 0:791a779d6220 255
shoaib_ahmed 0:791a779d6220 256 /*
shoaib_ahmed 0:791a779d6220 257 * Targa also defines a 32-bit pixel format with order B,G,R,A.
shoaib_ahmed 0:791a779d6220 258 * We presently ignore the attribute byte, so the code for reading
shoaib_ahmed 0:791a779d6220 259 * these pixels is identical to the 24-bit routine above.
shoaib_ahmed 0:791a779d6220 260 * This works because the actual pixel length is only known to read_pixel.
shoaib_ahmed 0:791a779d6220 261 */
shoaib_ahmed 0:791a779d6220 262
shoaib_ahmed 0:791a779d6220 263 #define get_32bit_row get_24bit_row
shoaib_ahmed 0:791a779d6220 264
shoaib_ahmed 0:791a779d6220 265
shoaib_ahmed 0:791a779d6220 266 /*
shoaib_ahmed 0:791a779d6220 267 * This method is for re-reading the input data in standard top-down
shoaib_ahmed 0:791a779d6220 268 * row order. The entire image has already been read into whole_image
shoaib_ahmed 0:791a779d6220 269 * with proper conversion of pixel format, but it's in a funny row order.
shoaib_ahmed 0:791a779d6220 270 */
shoaib_ahmed 0:791a779d6220 271
shoaib_ahmed 0:791a779d6220 272 METHODDEF(JDIMENSION)
shoaib_ahmed 0:791a779d6220 273 get_memory_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 274 {
shoaib_ahmed 0:791a779d6220 275 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 276 JDIMENSION source_row;
shoaib_ahmed 0:791a779d6220 277
shoaib_ahmed 0:791a779d6220 278 /* Compute row of source that maps to current_row of normal order */
shoaib_ahmed 0:791a779d6220 279 /* For now, assume image is bottom-up and not interlaced. */
shoaib_ahmed 0:791a779d6220 280 /* NEEDS WORK to support interlaced images! */
shoaib_ahmed 0:791a779d6220 281 source_row = cinfo->image_height - source->current_row - 1;
shoaib_ahmed 0:791a779d6220 282
shoaib_ahmed 0:791a779d6220 283 /* Fetch that row from virtual array */
shoaib_ahmed 0:791a779d6220 284 source->pub.buffer = (*cinfo->mem->access_virt_sarray)
shoaib_ahmed 0:791a779d6220 285 ((j_common_ptr) cinfo, source->whole_image,
shoaib_ahmed 0:791a779d6220 286 source_row, (JDIMENSION) 1, FALSE);
shoaib_ahmed 0:791a779d6220 287
shoaib_ahmed 0:791a779d6220 288 source->current_row++;
shoaib_ahmed 0:791a779d6220 289 return 1;
shoaib_ahmed 0:791a779d6220 290 }
shoaib_ahmed 0:791a779d6220 291
shoaib_ahmed 0:791a779d6220 292
shoaib_ahmed 0:791a779d6220 293 /*
shoaib_ahmed 0:791a779d6220 294 * This method loads the image into whole_image during the first call on
shoaib_ahmed 0:791a779d6220 295 * get_pixel_rows. The get_pixel_rows pointer is then adjusted to call
shoaib_ahmed 0:791a779d6220 296 * get_memory_row on subsequent calls.
shoaib_ahmed 0:791a779d6220 297 */
shoaib_ahmed 0:791a779d6220 298
shoaib_ahmed 0:791a779d6220 299 METHODDEF(JDIMENSION)
shoaib_ahmed 0:791a779d6220 300 preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 301 {
shoaib_ahmed 0:791a779d6220 302 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 303 JDIMENSION row;
shoaib_ahmed 0:791a779d6220 304 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
shoaib_ahmed 0:791a779d6220 305
shoaib_ahmed 0:791a779d6220 306 /* Read the data into a virtual array in input-file row order. */
shoaib_ahmed 0:791a779d6220 307 for (row = 0; row < cinfo->image_height; row++) {
shoaib_ahmed 0:791a779d6220 308 if (progress != NULL) {
shoaib_ahmed 0:791a779d6220 309 progress->pub.pass_counter = (long) row;
shoaib_ahmed 0:791a779d6220 310 progress->pub.pass_limit = (long) cinfo->image_height;
shoaib_ahmed 0:791a779d6220 311 (*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
shoaib_ahmed 0:791a779d6220 312 }
shoaib_ahmed 0:791a779d6220 313 source->pub.buffer = (*cinfo->mem->access_virt_sarray)
shoaib_ahmed 0:791a779d6220 314 ((j_common_ptr) cinfo, source->whole_image, row, (JDIMENSION) 1, TRUE);
shoaib_ahmed 0:791a779d6220 315 (*source->get_pixel_rows) (cinfo, sinfo);
shoaib_ahmed 0:791a779d6220 316 }
shoaib_ahmed 0:791a779d6220 317 if (progress != NULL)
shoaib_ahmed 0:791a779d6220 318 progress->completed_extra_passes++;
shoaib_ahmed 0:791a779d6220 319
shoaib_ahmed 0:791a779d6220 320 /* Set up to read from the virtual array in unscrambled order */
shoaib_ahmed 0:791a779d6220 321 source->pub.get_pixel_rows = get_memory_row;
shoaib_ahmed 0:791a779d6220 322 source->current_row = 0;
shoaib_ahmed 0:791a779d6220 323 /* And read the first row */
shoaib_ahmed 0:791a779d6220 324 return get_memory_row(cinfo, sinfo);
shoaib_ahmed 0:791a779d6220 325 }
shoaib_ahmed 0:791a779d6220 326
shoaib_ahmed 0:791a779d6220 327
shoaib_ahmed 0:791a779d6220 328 /*
shoaib_ahmed 0:791a779d6220 329 * Read the file header; return image size and component count.
shoaib_ahmed 0:791a779d6220 330 */
shoaib_ahmed 0:791a779d6220 331
shoaib_ahmed 0:791a779d6220 332 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 333 start_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 334 {
shoaib_ahmed 0:791a779d6220 335 tga_source_ptr source = (tga_source_ptr) sinfo;
shoaib_ahmed 0:791a779d6220 336 U_CHAR targaheader[18];
shoaib_ahmed 0:791a779d6220 337 int idlen, cmaptype, subtype, flags, interlace_type, components;
shoaib_ahmed 0:791a779d6220 338 unsigned int width, height, maplen;
shoaib_ahmed 0:791a779d6220 339 boolean is_bottom_up;
shoaib_ahmed 0:791a779d6220 340
shoaib_ahmed 0:791a779d6220 341 #define GET_2B(offset) ((unsigned int) UCH(targaheader[offset]) + \
shoaib_ahmed 0:791a779d6220 342 (((unsigned int) UCH(targaheader[offset+1])) << 8))
shoaib_ahmed 0:791a779d6220 343
shoaib_ahmed 0:791a779d6220 344 if (! ReadOK(source->pub.input_file, targaheader, 18))
shoaib_ahmed 0:791a779d6220 345 ERREXIT(cinfo, JERR_INPUT_EOF);
shoaib_ahmed 0:791a779d6220 346
shoaib_ahmed 0:791a779d6220 347 /* Pretend "15-bit" pixels are 16-bit --- we ignore attribute bit anyway */
shoaib_ahmed 0:791a779d6220 348 if (targaheader[16] == 15)
shoaib_ahmed 0:791a779d6220 349 targaheader[16] = 16;
shoaib_ahmed 0:791a779d6220 350
shoaib_ahmed 0:791a779d6220 351 idlen = UCH(targaheader[0]);
shoaib_ahmed 0:791a779d6220 352 cmaptype = UCH(targaheader[1]);
shoaib_ahmed 0:791a779d6220 353 subtype = UCH(targaheader[2]);
shoaib_ahmed 0:791a779d6220 354 maplen = GET_2B(5);
shoaib_ahmed 0:791a779d6220 355 width = GET_2B(12);
shoaib_ahmed 0:791a779d6220 356 height = GET_2B(14);
shoaib_ahmed 0:791a779d6220 357 source->pixel_size = UCH(targaheader[16]) >> 3;
shoaib_ahmed 0:791a779d6220 358 flags = UCH(targaheader[17]); /* Image Descriptor byte */
shoaib_ahmed 0:791a779d6220 359
shoaib_ahmed 0:791a779d6220 360 is_bottom_up = ((flags & 0x20) == 0); /* bit 5 set => top-down */
shoaib_ahmed 0:791a779d6220 361 interlace_type = flags >> 6; /* bits 6/7 are interlace code */
shoaib_ahmed 0:791a779d6220 362
shoaib_ahmed 0:791a779d6220 363 if (cmaptype > 1 || /* cmaptype must be 0 or 1 */
shoaib_ahmed 0:791a779d6220 364 source->pixel_size < 1 || source->pixel_size > 4 ||
shoaib_ahmed 0:791a779d6220 365 (UCH(targaheader[16]) & 7) != 0 || /* bits/pixel must be multiple of 8 */
shoaib_ahmed 0:791a779d6220 366 interlace_type != 0) /* currently don't allow interlaced image */
shoaib_ahmed 0:791a779d6220 367 ERREXIT(cinfo, JERR_TGA_BADPARMS);
shoaib_ahmed 0:791a779d6220 368
shoaib_ahmed 0:791a779d6220 369 if (subtype > 8) {
shoaib_ahmed 0:791a779d6220 370 /* It's an RLE-coded file */
shoaib_ahmed 0:791a779d6220 371 source->read_pixel = read_rle_pixel;
shoaib_ahmed 0:791a779d6220 372 source->block_count = source->dup_pixel_count = 0;
shoaib_ahmed 0:791a779d6220 373 subtype -= 8;
shoaib_ahmed 0:791a779d6220 374 } else {
shoaib_ahmed 0:791a779d6220 375 /* Non-RLE file */
shoaib_ahmed 0:791a779d6220 376 source->read_pixel = read_non_rle_pixel;
shoaib_ahmed 0:791a779d6220 377 }
shoaib_ahmed 0:791a779d6220 378
shoaib_ahmed 0:791a779d6220 379 /* Now should have subtype 1, 2, or 3 */
shoaib_ahmed 0:791a779d6220 380 components = 3; /* until proven different */
shoaib_ahmed 0:791a779d6220 381 cinfo->in_color_space = JCS_RGB;
shoaib_ahmed 0:791a779d6220 382
shoaib_ahmed 0:791a779d6220 383 switch (subtype) {
shoaib_ahmed 0:791a779d6220 384 case 1: /* Colormapped image */
shoaib_ahmed 0:791a779d6220 385 if (source->pixel_size == 1 && cmaptype == 1)
shoaib_ahmed 0:791a779d6220 386 source->get_pixel_rows = get_8bit_row;
shoaib_ahmed 0:791a779d6220 387 else
shoaib_ahmed 0:791a779d6220 388 ERREXIT(cinfo, JERR_TGA_BADPARMS);
shoaib_ahmed 0:791a779d6220 389 TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height);
shoaib_ahmed 0:791a779d6220 390 break;
shoaib_ahmed 0:791a779d6220 391 case 2: /* RGB image */
shoaib_ahmed 0:791a779d6220 392 switch (source->pixel_size) {
shoaib_ahmed 0:791a779d6220 393 case 2:
shoaib_ahmed 0:791a779d6220 394 source->get_pixel_rows = get_16bit_row;
shoaib_ahmed 0:791a779d6220 395 break;
shoaib_ahmed 0:791a779d6220 396 case 3:
shoaib_ahmed 0:791a779d6220 397 source->get_pixel_rows = get_24bit_row;
shoaib_ahmed 0:791a779d6220 398 break;
shoaib_ahmed 0:791a779d6220 399 case 4:
shoaib_ahmed 0:791a779d6220 400 source->get_pixel_rows = get_32bit_row;
shoaib_ahmed 0:791a779d6220 401 break;
shoaib_ahmed 0:791a779d6220 402 default:
shoaib_ahmed 0:791a779d6220 403 ERREXIT(cinfo, JERR_TGA_BADPARMS);
shoaib_ahmed 0:791a779d6220 404 break;
shoaib_ahmed 0:791a779d6220 405 }
shoaib_ahmed 0:791a779d6220 406 TRACEMS2(cinfo, 1, JTRC_TGA, width, height);
shoaib_ahmed 0:791a779d6220 407 break;
shoaib_ahmed 0:791a779d6220 408 case 3: /* Grayscale image */
shoaib_ahmed 0:791a779d6220 409 components = 1;
shoaib_ahmed 0:791a779d6220 410 cinfo->in_color_space = JCS_GRAYSCALE;
shoaib_ahmed 0:791a779d6220 411 if (source->pixel_size == 1)
shoaib_ahmed 0:791a779d6220 412 source->get_pixel_rows = get_8bit_gray_row;
shoaib_ahmed 0:791a779d6220 413 else
shoaib_ahmed 0:791a779d6220 414 ERREXIT(cinfo, JERR_TGA_BADPARMS);
shoaib_ahmed 0:791a779d6220 415 TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height);
shoaib_ahmed 0:791a779d6220 416 break;
shoaib_ahmed 0:791a779d6220 417 default:
shoaib_ahmed 0:791a779d6220 418 ERREXIT(cinfo, JERR_TGA_BADPARMS);
shoaib_ahmed 0:791a779d6220 419 break;
shoaib_ahmed 0:791a779d6220 420 }
shoaib_ahmed 0:791a779d6220 421
shoaib_ahmed 0:791a779d6220 422 if (is_bottom_up) {
shoaib_ahmed 0:791a779d6220 423 /* Create a virtual array to buffer the upside-down image. */
shoaib_ahmed 0:791a779d6220 424 source->whole_image = (*cinfo->mem->request_virt_sarray)
shoaib_ahmed 0:791a779d6220 425 ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
shoaib_ahmed 0:791a779d6220 426 (JDIMENSION) width * components, (JDIMENSION) height, (JDIMENSION) 1);
shoaib_ahmed 0:791a779d6220 427 if (cinfo->progress != NULL) {
shoaib_ahmed 0:791a779d6220 428 cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
shoaib_ahmed 0:791a779d6220 429 progress->total_extra_passes++; /* count file input as separate pass */
shoaib_ahmed 0:791a779d6220 430 }
shoaib_ahmed 0:791a779d6220 431 /* source->pub.buffer will point to the virtual array. */
shoaib_ahmed 0:791a779d6220 432 source->pub.buffer_height = 1; /* in case anyone looks at it */
shoaib_ahmed 0:791a779d6220 433 source->pub.get_pixel_rows = preload_image;
shoaib_ahmed 0:791a779d6220 434 } else {
shoaib_ahmed 0:791a779d6220 435 /* Don't need a virtual array, but do need a one-row input buffer. */
shoaib_ahmed 0:791a779d6220 436 source->whole_image = NULL;
shoaib_ahmed 0:791a779d6220 437 source->pub.buffer = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 438 ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 439 (JDIMENSION) width * components, (JDIMENSION) 1);
shoaib_ahmed 0:791a779d6220 440 source->pub.buffer_height = 1;
shoaib_ahmed 0:791a779d6220 441 source->pub.get_pixel_rows = source->get_pixel_rows;
shoaib_ahmed 0:791a779d6220 442 }
shoaib_ahmed 0:791a779d6220 443
shoaib_ahmed 0:791a779d6220 444 while (idlen--) /* Throw away ID field */
shoaib_ahmed 0:791a779d6220 445 (void) read_byte(source);
shoaib_ahmed 0:791a779d6220 446
shoaib_ahmed 0:791a779d6220 447 if (maplen > 0) {
shoaib_ahmed 0:791a779d6220 448 if (maplen > 256 || GET_2B(3) != 0)
shoaib_ahmed 0:791a779d6220 449 ERREXIT(cinfo, JERR_TGA_BADCMAP);
shoaib_ahmed 0:791a779d6220 450 /* Allocate space to store the colormap */
shoaib_ahmed 0:791a779d6220 451 source->colormap = (*cinfo->mem->alloc_sarray)
shoaib_ahmed 0:791a779d6220 452 ((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) maplen, (JDIMENSION) 3);
shoaib_ahmed 0:791a779d6220 453 /* and read it from the file */
shoaib_ahmed 0:791a779d6220 454 read_colormap(source, (int) maplen, UCH(targaheader[7]));
shoaib_ahmed 0:791a779d6220 455 } else {
shoaib_ahmed 0:791a779d6220 456 if (cmaptype) /* but you promised a cmap! */
shoaib_ahmed 0:791a779d6220 457 ERREXIT(cinfo, JERR_TGA_BADPARMS);
shoaib_ahmed 0:791a779d6220 458 source->colormap = NULL;
shoaib_ahmed 0:791a779d6220 459 }
shoaib_ahmed 0:791a779d6220 460
shoaib_ahmed 0:791a779d6220 461 cinfo->input_components = components;
shoaib_ahmed 0:791a779d6220 462 cinfo->data_precision = 8;
shoaib_ahmed 0:791a779d6220 463 cinfo->image_width = width;
shoaib_ahmed 0:791a779d6220 464 cinfo->image_height = height;
shoaib_ahmed 0:791a779d6220 465 }
shoaib_ahmed 0:791a779d6220 466
shoaib_ahmed 0:791a779d6220 467
shoaib_ahmed 0:791a779d6220 468 /*
shoaib_ahmed 0:791a779d6220 469 * Finish up at the end of the file.
shoaib_ahmed 0:791a779d6220 470 */
shoaib_ahmed 0:791a779d6220 471
shoaib_ahmed 0:791a779d6220 472 METHODDEF(void)
shoaib_ahmed 0:791a779d6220 473 finish_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
shoaib_ahmed 0:791a779d6220 474 {
shoaib_ahmed 0:791a779d6220 475 /* no work */
shoaib_ahmed 0:791a779d6220 476 }
shoaib_ahmed 0:791a779d6220 477
shoaib_ahmed 0:791a779d6220 478
shoaib_ahmed 0:791a779d6220 479 /*
shoaib_ahmed 0:791a779d6220 480 * The module selection routine for Targa format input.
shoaib_ahmed 0:791a779d6220 481 */
shoaib_ahmed 0:791a779d6220 482
shoaib_ahmed 0:791a779d6220 483 GLOBAL(cjpeg_source_ptr)
shoaib_ahmed 0:791a779d6220 484 jinit_read_targa (j_compress_ptr cinfo)
shoaib_ahmed 0:791a779d6220 485 {
shoaib_ahmed 0:791a779d6220 486 tga_source_ptr source;
shoaib_ahmed 0:791a779d6220 487
shoaib_ahmed 0:791a779d6220 488 /* Create module interface object */
shoaib_ahmed 0:791a779d6220 489 source = (tga_source_ptr)
shoaib_ahmed 0:791a779d6220 490 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
shoaib_ahmed 0:791a779d6220 491 SIZEOF(tga_source_struct));
shoaib_ahmed 0:791a779d6220 492 source->cinfo = cinfo; /* make back link for subroutines */
shoaib_ahmed 0:791a779d6220 493 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
shoaib_ahmed 0:791a779d6220 494 source->pub.start_input = start_input_tga;
shoaib_ahmed 0:791a779d6220 495 source->pub.finish_input = finish_input_tga;
shoaib_ahmed 0:791a779d6220 496
shoaib_ahmed 0:791a779d6220 497 return (cjpeg_source_ptr) source;
shoaib_ahmed 0:791a779d6220 498 }
shoaib_ahmed 0:791a779d6220 499
shoaib_ahmed 0:791a779d6220 500 #endif /* TARGA_SUPPORTED */