4 * This file was part of the Independent JPEG Group's software:
5 * Copyright (C) 1991-1997, Thomas G. Lane.
6 * Modified 2009 by Bill Allombert, Guido Vollbeding.
7 * It was modified by The libjpeg-turbo Project to include only code and
8 * information relevant to libjpeg-turbo.
9 * For conditions of distribution and use, see the accompanying README file.
11 * This file contains routines to read input images in PPM/PGM format.
12 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
13 * The PBMPLUS library is NOT required to compile this software
14 * (but it is highly useful as a set of PPM image manipulation programs).
16 * These routines may need modification for non-Unix environments or
17 * specialized applications. As they stand, they assume input from
18 * an ordinary stdio stream. They further assume that reading begins
19 * at the start of the file; start_input may need work if the
20 * user interface has already read some data (e.g., to determine that
21 * the file is indeed PPM format).
24 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
29 /* Portions of this code are based on the PBMPLUS library, which is:
31 ** Copyright (C) 1988 by Jef Poskanzer.
33 ** Permission to use, copy, modify, and distribute this software and its
34 ** documentation for any purpose and without fee is hereby granted, provided
35 ** that the above copyright notice appear in all copies and that both that
36 ** copyright notice and this permission notice appear in supporting
37 ** documentation. This software is provided "as is" without express or
42 /* Macros to deal with unsigned chars as efficiently as compiler allows */
44 #ifdef HAVE_UNSIGNED_CHAR
45 typedef unsigned char U_CHAR;
46 #define UCH(x) ((int) (x))
47 #else /* !HAVE_UNSIGNED_CHAR */
48 #ifdef __CHAR_UNSIGNED__
50 #define UCH(x) ((int) (x))
53 #define UCH(x) ((int) (x) & 0xFF)
55 #endif /* HAVE_UNSIGNED_CHAR */
58 #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
61 /* Private version of data source object */
64 struct cjpeg_source_struct pub; /* public fields */
66 /* Usually these two pointers point to the same place: */
67 U_CHAR *iobuffer; /* fread's I/O buffer */
68 JSAMPROW pixrow; /* compressor input buffer */
69 size_t buffer_width; /* width of I/O buffer */
70 JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
74 typedef ppm_source_struct * ppm_source_ptr;
78 pbm_getc (FILE * infile)
79 /* Read next char, skipping over any comments */
80 /* A comment/newline sequence is returned as a newline */
88 } while (ch != '\n' && ch != EOF);
95 read_pbm_integer (j_compress_ptr cinfo, FILE * infile, int maxval)
96 /* Read an unsigned decimal integer from the PPM file */
97 /* Swallows one trailing character after the integer */
98 /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
99 /* This should not be a problem in practice. */
102 register unsigned int val;
104 /* Skip any leading whitespace */
106 ch = pbm_getc(infile);
108 ERREXIT(cinfo, JERR_INPUT_EOF);
109 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
111 if (ch < '0' || ch > '9')
112 ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
115 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
121 ERREXIT(cinfo, JERR_PPM_TOOLARGE);
128 * Read one row of pixels.
130 * We provide several different versions depending on input file format.
131 * In all cases, input is scaled to the size of JSAMPLE.
133 * A really fast path is provided for reading byte/sample raw files with
134 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
138 METHODDEF(JDIMENSION)
139 get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
140 /* This version is for reading text-format PGM files with any maxval */
142 ppm_source_ptr source = (ppm_source_ptr) sinfo;
143 FILE * infile = source->pub.input_file;
144 register JSAMPROW ptr;
145 register JSAMPLE *rescale = source->rescale;
147 int maxval = source->maxval;
149 ptr = source->pub.buffer[0];
150 for (col = cinfo->image_width; col > 0; col--) {
151 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
157 METHODDEF(JDIMENSION)
158 get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
159 /* This version is for reading text-format PPM files with any maxval */
161 ppm_source_ptr source = (ppm_source_ptr) sinfo;
162 FILE * infile = source->pub.input_file;
163 register JSAMPROW ptr;
164 register JSAMPLE *rescale = source->rescale;
166 int maxval = source->maxval;
168 ptr = source->pub.buffer[0];
169 for (col = cinfo->image_width; col > 0; col--) {
170 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
171 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
172 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
178 METHODDEF(JDIMENSION)
179 get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
180 /* This version is for reading raw-byte-format PGM files with any maxval */
182 ppm_source_ptr source = (ppm_source_ptr) sinfo;
183 register JSAMPROW ptr;
184 register U_CHAR * bufferptr;
185 register JSAMPLE *rescale = source->rescale;
188 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
189 ERREXIT(cinfo, JERR_INPUT_EOF);
190 ptr = source->pub.buffer[0];
191 bufferptr = source->iobuffer;
192 for (col = cinfo->image_width; col > 0; col--) {
193 *ptr++ = rescale[UCH(*bufferptr++)];
199 METHODDEF(JDIMENSION)
200 get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
201 /* This version is for reading raw-byte-format PPM files with any maxval */
203 ppm_source_ptr source = (ppm_source_ptr) sinfo;
204 register JSAMPROW ptr;
205 register U_CHAR * bufferptr;
206 register JSAMPLE *rescale = source->rescale;
209 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
210 ERREXIT(cinfo, JERR_INPUT_EOF);
211 ptr = source->pub.buffer[0];
212 bufferptr = source->iobuffer;
213 for (col = cinfo->image_width; col > 0; col--) {
214 *ptr++ = rescale[UCH(*bufferptr++)];
215 *ptr++ = rescale[UCH(*bufferptr++)];
216 *ptr++ = rescale[UCH(*bufferptr++)];
222 METHODDEF(JDIMENSION)
223 get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
224 /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
225 * In this case we just read right into the JSAMPLE buffer!
226 * Note that same code works for PPM and PGM files.
229 ppm_source_ptr source = (ppm_source_ptr) sinfo;
231 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
232 ERREXIT(cinfo, JERR_INPUT_EOF);
237 METHODDEF(JDIMENSION)
238 get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
239 /* This version is for reading raw-word-format PGM files with any maxval */
241 ppm_source_ptr source = (ppm_source_ptr) sinfo;
242 register JSAMPROW ptr;
243 register U_CHAR * bufferptr;
244 register JSAMPLE *rescale = source->rescale;
247 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
248 ERREXIT(cinfo, JERR_INPUT_EOF);
249 ptr = source->pub.buffer[0];
250 bufferptr = source->iobuffer;
251 for (col = cinfo->image_width; col > 0; col--) {
253 temp = UCH(*bufferptr++) << 8;
254 temp |= UCH(*bufferptr++);
255 *ptr++ = rescale[temp];
261 METHODDEF(JDIMENSION)
262 get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
263 /* This version is for reading raw-word-format PPM files with any maxval */
265 ppm_source_ptr source = (ppm_source_ptr) sinfo;
266 register JSAMPROW ptr;
267 register U_CHAR * bufferptr;
268 register JSAMPLE *rescale = source->rescale;
271 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
272 ERREXIT(cinfo, JERR_INPUT_EOF);
273 ptr = source->pub.buffer[0];
274 bufferptr = source->iobuffer;
275 for (col = cinfo->image_width; col > 0; col--) {
277 temp = UCH(*bufferptr++) << 8;
278 temp |= UCH(*bufferptr++);
279 *ptr++ = rescale[temp];
280 temp = UCH(*bufferptr++) << 8;
281 temp |= UCH(*bufferptr++);
282 *ptr++ = rescale[temp];
283 temp = UCH(*bufferptr++) << 8;
284 temp |= UCH(*bufferptr++);
285 *ptr++ = rescale[temp];
292 * Read the file header; return image size and component count.
296 start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
298 ppm_source_ptr source = (ppm_source_ptr) sinfo;
300 unsigned int w, h, maxval;
301 boolean need_iobuffer, use_raw_buffer, need_rescale;
303 if (getc(source->pub.input_file) != 'P')
304 ERREXIT(cinfo, JERR_PPM_NOT);
306 c = getc(source->pub.input_file); /* subformat discriminator character */
308 /* detect unsupported variants (ie, PBM) before trying to read header */
310 case '2': /* it's a text-format PGM file */
311 case '3': /* it's a text-format PPM file */
312 case '5': /* it's a raw-format PGM file */
313 case '6': /* it's a raw-format PPM file */
316 ERREXIT(cinfo, JERR_PPM_NOT);
320 /* fetch the remaining header info */
321 w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
322 h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
323 maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
325 if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
326 ERREXIT(cinfo, JERR_PPM_NOT);
328 cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
329 cinfo->image_width = (JDIMENSION) w;
330 cinfo->image_height = (JDIMENSION) h;
331 source->maxval = maxval;
333 /* initialize flags to most common settings */
334 need_iobuffer = TRUE; /* do we need an I/O buffer? */
335 use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
336 need_rescale = TRUE; /* do we need a rescale array? */
339 case '2': /* it's a text-format PGM file */
340 cinfo->input_components = 1;
341 cinfo->in_color_space = JCS_GRAYSCALE;
342 TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
343 source->pub.get_pixel_rows = get_text_gray_row;
344 need_iobuffer = FALSE;
347 case '3': /* it's a text-format PPM file */
348 cinfo->input_components = 3;
349 cinfo->in_color_space = JCS_RGB;
350 TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
351 source->pub.get_pixel_rows = get_text_rgb_row;
352 need_iobuffer = FALSE;
355 case '5': /* it's a raw-format PGM file */
356 cinfo->input_components = 1;
357 cinfo->in_color_space = JCS_GRAYSCALE;
358 TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
360 source->pub.get_pixel_rows = get_word_gray_row;
361 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
362 source->pub.get_pixel_rows = get_raw_row;
363 use_raw_buffer = TRUE;
364 need_rescale = FALSE;
366 source->pub.get_pixel_rows = get_scaled_gray_row;
370 case '6': /* it's a raw-format PPM file */
371 cinfo->input_components = 3;
372 cinfo->in_color_space = JCS_RGB;
373 TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
375 source->pub.get_pixel_rows = get_word_rgb_row;
376 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
377 source->pub.get_pixel_rows = get_raw_row;
378 use_raw_buffer = TRUE;
379 need_rescale = FALSE;
381 source->pub.get_pixel_rows = get_scaled_rgb_row;
386 /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
388 source->buffer_width = (size_t) w * cinfo->input_components *
389 ((maxval<=255) ? sizeof(U_CHAR) : (2*sizeof(U_CHAR)));
390 source->iobuffer = (U_CHAR *)
391 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
392 source->buffer_width);
395 /* Create compressor input buffer. */
396 if (use_raw_buffer) {
397 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
398 /* Synthesize a JSAMPARRAY pointer structure */
399 source->pixrow = (JSAMPROW) source->iobuffer;
400 source->pub.buffer = & source->pixrow;
401 source->pub.buffer_height = 1;
403 /* Need to translate anyway, so make a separate sample buffer. */
404 source->pub.buffer = (*cinfo->mem->alloc_sarray)
405 ((j_common_ptr) cinfo, JPOOL_IMAGE,
406 (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
407 source->pub.buffer_height = 1;
410 /* Compute the rescaling array if required. */
412 INT32 val, half_maxval;
414 /* On 16-bit-int machines we have to be careful of maxval = 65535 */
415 source->rescale = (JSAMPLE *)
416 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
417 (size_t) (((long) maxval + 1L) * sizeof(JSAMPLE)));
418 half_maxval = maxval / 2;
421 for (val = 0; val <= (INT32) maxval; val++) {
422 /* The multiplication here must be done in 32 bits to avoid overflow */
423 source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
427 for (val = 0; val <= (INT32) maxval; val++) {
428 /* The multiplication here must be done in 32 bits to avoid overflow */
429 source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
437 * Finish up at the end of the file.
441 finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
448 * The module selection routine for PPM format input.
451 GLOBAL(cjpeg_source_ptr)
452 jinit_read_ppm (j_compress_ptr cinfo)
454 ppm_source_ptr source;
456 /* Create module interface object */
457 source = (ppm_source_ptr)
458 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
459 sizeof(ppm_source_struct));
460 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
461 source->pub.start_input = start_input_ppm;
462 source->pub.finish_input = finish_input_ppm;
464 return (cjpeg_source_ptr) source;
467 #endif /* PPM_SUPPORTED */