1 /*
2  * rdppm.c
3  *
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.
10  *
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).
15  *
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).
22  */
23 
24 #include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
25 
26 #ifdef PPM_SUPPORTED
27 
28 
29 /* Portions of this code are based on the PBMPLUS library, which is:
30 **
31 ** Copyright (C) 1988 by Jef Poskanzer.
32 **
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
38 ** implied warranty.
39 */
40 
41 
42 /* Macros to deal with unsigned chars as efficiently as compiler allows */
43 
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__
49 typedef char U_CHAR;
50 #define UCH(x)  ((int) (x))
51 #else
52 typedef char U_CHAR;
53 #define UCH(x)  ((int) (x) & 0xFF)
54 #endif
55 #endif /* HAVE_UNSIGNED_CHAR */
56 
57 
58 #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
59 
60 
61 /* Private version of data source object */
62 
63 typedef struct {
64   struct cjpeg_source_struct pub; /* public fields */
65 
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 */
71   int maxval;
72 } ppm_source_struct;
73 
74 typedef ppm_source_struct * ppm_source_ptr;
75 
76 
77 LOCAL(int)
pbm_getc(FILE * infile)78 pbm_getc (FILE * infile)
79 /* Read next char, skipping over any comments */
80 /* A comment/newline sequence is returned as a newline */
81 {
82   register int ch;
83 
84   ch = getc(infile);
85   if (ch == '#') {
86     do {
87       ch = getc(infile);
88     } while (ch != '\n' && ch != EOF);
89   }
90   return ch;
91 }
92 
93 
94 LOCAL(unsigned int)
read_pbm_integer(j_compress_ptr cinfo,FILE * infile,int maxval)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. */
100 {
101   register int ch;
102   register unsigned int val;
103 
104   /* Skip any leading whitespace */
105   do {
106     ch = pbm_getc(infile);
107     if (ch == EOF)
108       ERREXIT(cinfo, JERR_INPUT_EOF);
109   } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
110 
111   if (ch < '0' || ch > '9')
112     ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
113 
114   val = ch - '0';
115   while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
116     val *= 10;
117     val += ch - '0';
118   }
119 
120   if (val > maxval)
121     ERREXIT(cinfo, JERR_PPM_TOOLARGE);
122 
123   return val;
124 }
125 
126 
127 /*
128  * Read one row of pixels.
129  *
130  * We provide several different versions depending on input file format.
131  * In all cases, input is scaled to the size of JSAMPLE.
132  *
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.
135  */
136 
137 
138 METHODDEF(JDIMENSION)
get_text_gray_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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 */
141 {
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;
146   JDIMENSION col;
147   int maxval = source->maxval;
148 
149   ptr = source->pub.buffer[0];
150   for (col = cinfo->image_width; col > 0; col--) {
151     *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
152   }
153   return 1;
154 }
155 
156 
157 METHODDEF(JDIMENSION)
get_text_rgb_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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 */
160 {
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;
165   JDIMENSION col;
166   int maxval = source->maxval;
167 
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)];
173   }
174   return 1;
175 }
176 
177 
178 METHODDEF(JDIMENSION)
get_scaled_gray_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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 */
181 {
182   ppm_source_ptr source = (ppm_source_ptr) sinfo;
183   register JSAMPROW ptr;
184   register U_CHAR * bufferptr;
185   register JSAMPLE *rescale = source->rescale;
186   JDIMENSION col;
187 
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++)];
194   }
195   return 1;
196 }
197 
198 
199 METHODDEF(JDIMENSION)
get_scaled_rgb_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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 */
202 {
203   ppm_source_ptr source = (ppm_source_ptr) sinfo;
204   register JSAMPROW ptr;
205   register U_CHAR * bufferptr;
206   register JSAMPLE *rescale = source->rescale;
207   JDIMENSION col;
208 
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++)];
217   }
218   return 1;
219 }
220 
221 
222 METHODDEF(JDIMENSION)
get_raw_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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.
227  */
228 {
229   ppm_source_ptr source = (ppm_source_ptr) sinfo;
230 
231   if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
232     ERREXIT(cinfo, JERR_INPUT_EOF);
233   return 1;
234 }
235 
236 
237 METHODDEF(JDIMENSION)
get_word_gray_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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 */
240 {
241   ppm_source_ptr source = (ppm_source_ptr) sinfo;
242   register JSAMPROW ptr;
243   register U_CHAR * bufferptr;
244   register JSAMPLE *rescale = source->rescale;
245   JDIMENSION col;
246 
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--) {
252     register int temp;
253     temp  = UCH(*bufferptr++) << 8;
254     temp |= UCH(*bufferptr++);
255     *ptr++ = rescale[temp];
256   }
257   return 1;
258 }
259 
260 
261 METHODDEF(JDIMENSION)
get_word_rgb_row(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)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 */
264 {
265   ppm_source_ptr source = (ppm_source_ptr) sinfo;
266   register JSAMPROW ptr;
267   register U_CHAR * bufferptr;
268   register JSAMPLE *rescale = source->rescale;
269   JDIMENSION col;
270 
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--) {
276     register int temp;
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];
286   }
287   return 1;
288 }
289 
290 
291 /*
292  * Read the file header; return image size and component count.
293  */
294 
295 METHODDEF(void)
start_input_ppm(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)296 start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
297 {
298   ppm_source_ptr source = (ppm_source_ptr) sinfo;
299   int c;
300   unsigned int w, h, maxval;
301   boolean need_iobuffer, use_raw_buffer, need_rescale;
302 
303   if (getc(source->pub.input_file) != 'P')
304     ERREXIT(cinfo, JERR_PPM_NOT);
305 
306   c = getc(source->pub.input_file); /* subformat discriminator character */
307 
308   /* detect unsupported variants (ie, PBM) before trying to read header */
309   switch (c) {
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 */
314     break;
315   default:
316     ERREXIT(cinfo, JERR_PPM_NOT);
317     break;
318   }
319 
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);
324 
325   if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
326     ERREXIT(cinfo, JERR_PPM_NOT);
327 
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;
332 
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? */
337 
338   switch (c) {
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;
345     break;
346 
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;
353     break;
354 
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);
359     if (maxval > 255) {
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;
365     } else {
366       source->pub.get_pixel_rows = get_scaled_gray_row;
367     }
368     break;
369 
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);
374     if (maxval > 255) {
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;
380     } else {
381       source->pub.get_pixel_rows = get_scaled_rgb_row;
382     }
383     break;
384   }
385 
386   /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
387   if (need_iobuffer) {
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);
393   }
394 
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;
402   } else {
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;
408   }
409 
410   /* Compute the rescaling array if required. */
411   if (need_rescale) {
412     INT32 val, half_maxval;
413 
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;
419     for (val = 0; val <= (INT32) maxval; val++) {
420       /* The multiplication here must be done in 32 bits to avoid overflow */
421       source->rescale[val] = (JSAMPLE) ((val*MAXJSAMPLE + half_maxval)/maxval);
422     }
423   }
424 }
425 
426 
427 /*
428  * Finish up at the end of the file.
429  */
430 
431 METHODDEF(void)
finish_input_ppm(j_compress_ptr cinfo,cjpeg_source_ptr sinfo)432 finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
433 {
434   /* no work */
435 }
436 
437 
438 /*
439  * The module selection routine for PPM format input.
440  */
441 
442 GLOBAL(cjpeg_source_ptr)
jinit_read_ppm(j_compress_ptr cinfo)443 jinit_read_ppm (j_compress_ptr cinfo)
444 {
445   ppm_source_ptr source;
446 
447   /* Create module interface object */
448   source = (ppm_source_ptr)
449       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
450                                   sizeof(ppm_source_struct));
451   /* Fill in method ptrs, except get_pixel_rows which start_input sets */
452   source->pub.start_input = start_input_ppm;
453   source->pub.finish_input = finish_input_ppm;
454 
455   return (cjpeg_source_ptr) source;
456 }
457 
458 #endif /* PPM_SUPPORTED */
459