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