Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / test / rdppm.c
blob4564a0a98d05bcb216c6417858615b470ea0c5cc
1 /*
2 $Id$
3 */
5 /*
6 * rdppm.c
8 * Copyright (C) 1991-1997, 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 PPM/PGM format.
13 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
14 * The PBMPLUS library is NOT required to compile this software
15 * (but it is highly useful as a set of PPM image manipulation programs).
17 * These routines may need modification for non-Unix environments or
18 * specialized applications. As they stand, they assume input from
19 * an ordinary stdio stream. They further assume that reading begins
20 * at the start of the file; start_input may need work if the
21 * user interface has already read some data (e.g., to determine that
22 * the file is indeed PPM format).
25 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
27 #ifdef PPM_SUPPORTED
30 /* Portions of this code are based on the PBMPLUS library, which is:
32 ** Copyright (C) 1988 by Jef Poskanzer.
34 ** Permission to use, copy, modify, and distribute this software and its
35 ** documentation for any purpose and without fee is hereby granted, provided
36 ** that the above copyright notice appear in all copies and that both that
37 ** copyright notice and this permission notice appear in supporting
38 ** documentation. This software is provided "as is" without express or
39 ** implied warranty.
43 /* Macros to deal with unsigned chars as efficiently as compiler allows */
45 #ifdef HAVE_UNSIGNED_CHAR
46 typedef unsigned char U_CHAR;
47 #define UCH(x) ((int) (x))
48 #else /* !HAVE_UNSIGNED_CHAR */
49 #ifdef CHAR_IS_UNSIGNED
50 typedef char U_CHAR;
51 #define UCH(x) ((int) (x))
52 #else
53 typedef char U_CHAR;
54 #define UCH(x) ((int) (x) & 0xFF)
55 #endif
56 #endif /* HAVE_UNSIGNED_CHAR */
59 #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
63 * On most systems, reading individual bytes with getc() is drastically less
64 * efficient than buffering a row at a time with fread(). On PCs, we must
65 * allocate the buffer in near data space, because we are assuming small-data
66 * memory model, wherein fread() can't reach far memory. If you need to
67 * process very wide images on a PC, you might have to compile in large-memory
68 * model, or else replace fread() with a getc() loop --- which will be much
69 * slower.
73 /* Private version of data source object */
75 typedef struct {
76 struct cjpeg_source_struct pub; /* public fields */
78 U_CHAR *iobuffer; /* non-FAR pointer to I/O buffer */
79 JSAMPROW pixrow; /* FAR pointer to same */
80 size_t buffer_width; /* width of I/O buffer */
81 JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
82 } ppm_source_struct;
84 typedef ppm_source_struct * ppm_source_ptr;
87 LOCAL(int)
88 pbm_getc (FILE * infile)
89 /* Read next char, skipping over any comments */
90 /* A comment/newline sequence is returned as a newline */
92 register int ch;
94 ch = getc(infile);
95 if (ch == '#') {
96 do {
97 ch = getc(infile);
98 } while (ch != '\n' && ch != EOF);
100 return ch;
104 LOCAL(unsigned int)
105 read_pbm_integer (j_compress_ptr cinfo, FILE * infile)
106 /* Read an unsigned decimal integer from the PPM file */
107 /* Swallows one trailing character after the integer */
108 /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
109 /* This should not be a problem in practice. */
111 register int ch;
112 register unsigned int val;
114 /* Skip any leading whitespace */
115 do {
116 ch = pbm_getc(infile);
117 if (ch == EOF)
118 ERREXIT(cinfo, JERR_INPUT_EOF);
119 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
121 if (ch < '0' || ch > '9')
122 ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
124 val = ch - '0';
125 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
126 val *= 10;
127 val += ch - '0';
129 return val;
134 * Read one row of pixels.
136 * We provide several different versions depending on input file format.
137 * In all cases, input is scaled to the size of JSAMPLE.
139 * A really fast path is provided for reading byte/sample raw files with
140 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
144 METHODDEF(JDIMENSION)
145 get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
146 /* This version is for reading text-format PGM files with any maxval */
148 ppm_source_ptr source = (ppm_source_ptr) sinfo;
149 FILE * infile = source->pub.input_file;
150 register JSAMPROW ptr;
151 register JSAMPLE *rescale = source->rescale;
152 JDIMENSION col;
154 ptr = source->pub.buffer[0];
155 for (col = cinfo->image_width; col > 0; col--) {
156 *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
158 return 1;
162 METHODDEF(JDIMENSION)
163 get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
164 /* This version is for reading text-format PPM files with any maxval */
166 ppm_source_ptr source = (ppm_source_ptr) sinfo;
167 FILE * infile = source->pub.input_file;
168 register JSAMPROW ptr;
169 register JSAMPLE *rescale = source->rescale;
170 JDIMENSION col;
172 ptr = source->pub.buffer[0];
173 for (col = cinfo->image_width; col > 0; col--) {
174 *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
175 *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
176 *ptr++ = rescale[read_pbm_integer(cinfo, infile)];
178 return 1;
182 METHODDEF(JDIMENSION)
183 get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
184 /* This version is for reading raw-byte-format PGM files with any maxval */
186 ppm_source_ptr source = (ppm_source_ptr) sinfo;
187 register JSAMPROW ptr;
188 register U_CHAR * bufferptr;
189 register JSAMPLE *rescale = source->rescale;
190 JDIMENSION col;
192 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
193 ERREXIT(cinfo, JERR_INPUT_EOF);
194 ptr = source->pub.buffer[0];
195 bufferptr = source->iobuffer;
196 for (col = cinfo->image_width; col > 0; col--) {
197 *ptr++ = rescale[UCH(*bufferptr++)];
199 return 1;
203 METHODDEF(JDIMENSION)
204 get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
205 /* This version is for reading raw-byte-format PPM files with any maxval */
207 ppm_source_ptr source = (ppm_source_ptr) sinfo;
208 register JSAMPROW ptr;
209 register U_CHAR * bufferptr;
210 register JSAMPLE *rescale = source->rescale;
211 JDIMENSION col;
213 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
214 ERREXIT(cinfo, JERR_INPUT_EOF);
215 ptr = source->pub.buffer[0];
216 bufferptr = source->iobuffer;
217 for (col = cinfo->image_width; col > 0; col--) {
218 *ptr++ = rescale[UCH(*bufferptr++)];
219 *ptr++ = rescale[UCH(*bufferptr++)];
220 *ptr++ = rescale[UCH(*bufferptr++)];
222 return 1;
226 METHODDEF(JDIMENSION)
227 get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
228 /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
229 * In this case we just read right into the JSAMPLE buffer!
230 * Note that same code works for PPM and PGM files.
233 ppm_source_ptr source = (ppm_source_ptr) sinfo;
235 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
236 ERREXIT(cinfo, JERR_INPUT_EOF);
237 return 1;
241 METHODDEF(JDIMENSION)
242 get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
243 /* This version is for reading raw-word-format PGM files with any maxval */
245 ppm_source_ptr source = (ppm_source_ptr) sinfo;
246 register JSAMPROW ptr;
247 register U_CHAR * bufferptr;
248 register JSAMPLE *rescale = source->rescale;
249 JDIMENSION col;
251 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
252 ERREXIT(cinfo, JERR_INPUT_EOF);
253 ptr = source->pub.buffer[0];
254 bufferptr = source->iobuffer;
255 for (col = cinfo->image_width; col > 0; col--) {
256 register int temp;
257 temp = UCH(*bufferptr++);
258 temp |= UCH(*bufferptr++) << 8;
259 *ptr++ = rescale[temp];
261 return 1;
265 METHODDEF(JDIMENSION)
266 get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
267 /* This version is for reading raw-word-format PPM files with any maxval */
269 ppm_source_ptr source = (ppm_source_ptr) sinfo;
270 register JSAMPROW ptr;
271 register U_CHAR * bufferptr;
272 register JSAMPLE *rescale = source->rescale;
273 JDIMENSION col;
275 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
276 ERREXIT(cinfo, JERR_INPUT_EOF);
277 ptr = source->pub.buffer[0];
278 bufferptr = source->iobuffer;
279 for (col = cinfo->image_width; col > 0; col--) {
280 register int temp;
281 temp = UCH(*bufferptr++);
282 temp |= UCH(*bufferptr++) << 8;
283 *ptr++ = rescale[temp];
284 temp = UCH(*bufferptr++);
285 temp |= UCH(*bufferptr++) << 8;
286 *ptr++ = rescale[temp];
287 temp = UCH(*bufferptr++);
288 temp |= UCH(*bufferptr++) << 8;
289 *ptr++ = rescale[temp];
291 return 1;
296 * Read the file header; return image size and component count.
299 METHODDEF(void)
300 start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
302 ppm_source_ptr source = (ppm_source_ptr) sinfo;
303 int c;
304 unsigned int w, h, maxval;
305 boolean need_iobuffer, use_raw_buffer, need_rescale;
307 if (getc(source->pub.input_file) != 'P')
308 ERREXIT(cinfo, JERR_PPM_NOT);
310 c = getc(source->pub.input_file); /* subformat discriminator character */
312 /* detect unsupported variants (ie, PBM) before trying to read header */
313 switch (c) {
314 case '2': /* it's a text-format PGM file */
315 case '3': /* it's a text-format PPM file */
316 case '5': /* it's a raw-format PGM file */
317 case '6': /* it's a raw-format PPM file */
318 break;
319 default:
320 ERREXIT(cinfo, JERR_PPM_NOT);
321 break;
324 /* fetch the remaining header info */
325 w = read_pbm_integer(cinfo, source->pub.input_file);
326 h = read_pbm_integer(cinfo, source->pub.input_file);
327 maxval = read_pbm_integer(cinfo, source->pub.input_file);
329 if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
330 ERREXIT(cinfo, JERR_PPM_NOT);
332 cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
333 cinfo->image_width = (JDIMENSION) w;
334 cinfo->image_height = (JDIMENSION) h;
336 /* initialize flags to most common settings */
337 need_iobuffer = TRUE; /* do we need an I/O buffer? */
338 use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
339 need_rescale = TRUE; /* do we need a rescale array? */
341 switch (c) {
342 case '2': /* it's a text-format PGM file */
343 cinfo->input_components = 1;
344 cinfo->in_color_space = JCS_GRAYSCALE;
345 TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
346 source->pub.get_pixel_rows = get_text_gray_row;
347 need_iobuffer = FALSE;
348 break;
350 case '3': /* it's a text-format PPM file */
351 cinfo->input_components = 3;
352 cinfo->in_color_space = JCS_RGB;
353 TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
354 source->pub.get_pixel_rows = get_text_rgb_row;
355 need_iobuffer = FALSE;
356 break;
358 case '5': /* it's a raw-format PGM file */
359 cinfo->input_components = 1;
360 cinfo->in_color_space = JCS_GRAYSCALE;
361 TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
362 if (maxval > 255) {
363 source->pub.get_pixel_rows = get_word_gray_row;
364 } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
365 source->pub.get_pixel_rows = get_raw_row;
366 use_raw_buffer = TRUE;
367 need_rescale = FALSE;
368 } else {
369 source->pub.get_pixel_rows = get_scaled_gray_row;
371 break;
373 case '6': /* it's a raw-format PPM file */
374 cinfo->input_components = 3;
375 cinfo->in_color_space = JCS_RGB;
376 TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
377 if (maxval > 255) {
378 source->pub.get_pixel_rows = get_word_rgb_row;
379 } else if (maxval == MAXJSAMPLE && SIZEOF(JSAMPLE) == SIZEOF(U_CHAR)) {
380 source->pub.get_pixel_rows = get_raw_row;
381 use_raw_buffer = TRUE;
382 need_rescale = FALSE;
383 } else {
384 source->pub.get_pixel_rows = get_scaled_rgb_row;
386 break;
389 /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
390 if (need_iobuffer) {
391 source->buffer_width = (size_t) w * cinfo->input_components *
392 ((maxval<=255) ? SIZEOF(U_CHAR) : (2*SIZEOF(U_CHAR)));
393 source->iobuffer = (U_CHAR *)
394 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
395 source->buffer_width);
398 /* Create compressor input buffer. */
399 if (use_raw_buffer) {
400 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
401 /* Synthesize a JSAMPARRAY pointer structure */
402 /* Cast here implies near->far pointer conversion on PCs */
403 source->pixrow = (JSAMPROW) source->iobuffer;
404 source->pub.buffer = & source->pixrow;
405 source->pub.buffer_height = 1;
406 } else {
407 /* Need to translate anyway, so make a separate sample buffer. */
408 source->pub.buffer = (*cinfo->mem->alloc_sarray)
409 ((j_common_ptr) cinfo, JPOOL_IMAGE,
410 (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
411 source->pub.buffer_height = 1;
414 /* Compute the rescaling array if required. */
415 if (need_rescale) {
416 INT32 val, half_maxval;
418 /* On 16-bit-int machines we have to be careful of maxval = 65535 */
419 source->rescale = (JSAMPLE *)
420 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
421 (size_t) (((long) maxval + 1L) * SIZEOF(JSAMPLE)));
422 half_maxval = maxval / 2;
423 for (val = 0; val <= (INT32) maxval; val++) {
424 /* The multiplication here must be done in 32 bits to avoid overflow */
425 source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
432 * Finish up at the end of the file.
435 METHODDEF(void)
436 finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
438 /* no work */
443 * The module selection routine for PPM format input.
446 JGLOBAL(cjpeg_source_ptr)
447 jinit_read_ppm (j_compress_ptr cinfo)
449 ppm_source_ptr source;
451 /* Create module interface object */
452 source = (ppm_source_ptr)
453 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
454 SIZEOF(ppm_source_struct));
455 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
456 source->pub.start_input = start_input_ppm;
457 source->pub.finish_input = finish_input_ppm;
459 return (cjpeg_source_ptr) source;
462 #endif /* PPM_SUPPORTED */