blob: aef49239384f48e2d9499dbf6e49f5c39a653681 [file] [log] [blame]
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00001/*
2 * rdppm.c
3 *
Tom Hudson0d47d2d2016-05-04 13:22:56 -04004 * This file was part of the Independent JPEG Group's software:
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
hbono@chromium.org98626972011-08-03 03:13:08 +00006 * Modified 2009 by Bill Allombert, Guido Vollbeding.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04007 * libjpeg-turbo Modifications:
8 * Copyright (C) 2015, D. R. Commander.
9 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000011 *
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).
16 *
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).
23 */
24
Tom Hudson0d47d2d2016-05-04 13:22:56 -040025#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000026
27#ifdef PPM_SUPPORTED
28
29
30/* Portions of this code are based on the PBMPLUS library, which is:
31**
32** Copyright (C) 1988 by Jef Poskanzer.
33**
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.
40*/
41
42
43/* Macros to deal with unsigned chars as efficiently as compiler allows */
44
45#ifdef HAVE_UNSIGNED_CHAR
46typedef unsigned char U_CHAR;
Tom Hudson0d47d2d2016-05-04 13:22:56 -040047#define UCH(x) ((int) (x))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000048#else /* !HAVE_UNSIGNED_CHAR */
Tom Hudson0d47d2d2016-05-04 13:22:56 -040049#ifdef __CHAR_UNSIGNED__
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000050typedef char U_CHAR;
Tom Hudson0d47d2d2016-05-04 13:22:56 -040051#define UCH(x) ((int) (x))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000052#else
53typedef char U_CHAR;
Tom Hudson0d47d2d2016-05-04 13:22:56 -040054#define UCH(x) ((int) (x) & 0xFF)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000055#endif
56#endif /* HAVE_UNSIGNED_CHAR */
57
58
Tom Hudson0d47d2d2016-05-04 13:22:56 -040059#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000060
61
62/* Private version of data source object */
63
64typedef struct {
65 struct cjpeg_source_struct pub; /* public fields */
66
Tom Hudson0d47d2d2016-05-04 13:22:56 -040067 /* Usually these two pointers point to the same place: */
68 U_CHAR *iobuffer; /* fread's I/O buffer */
69 JSAMPROW pixrow; /* compressor input buffer */
70 size_t buffer_width; /* width of I/O buffer */
71 JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
72 int maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000073} ppm_source_struct;
74
Tom Hudson0d47d2d2016-05-04 13:22:56 -040075typedef ppm_source_struct *ppm_source_ptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000076
77
78LOCAL(int)
Tom Hudson0d47d2d2016-05-04 13:22:56 -040079pbm_getc (FILE *infile)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000080/* Read next char, skipping over any comments */
81/* A comment/newline sequence is returned as a newline */
82{
83 register int ch;
84
85 ch = getc(infile);
86 if (ch == '#') {
87 do {
88 ch = getc(infile);
89 } while (ch != '\n' && ch != EOF);
90 }
91 return ch;
92}
93
94
95LOCAL(unsigned int)
Tom Hudson0d47d2d2016-05-04 13:22:56 -040096read_pbm_integer (j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000097/* Read an unsigned decimal integer from the PPM file */
98/* Swallows one trailing character after the integer */
99/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
100/* This should not be a problem in practice. */
101{
102 register int ch;
103 register unsigned int val;
104
105 /* Skip any leading whitespace */
106 do {
107 ch = pbm_getc(infile);
108 if (ch == EOF)
109 ERREXIT(cinfo, JERR_INPUT_EOF);
110 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
111
112 if (ch < '0' || ch > '9')
113 ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
114
115 val = ch - '0';
116 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
117 val *= 10;
118 val += ch - '0';
119 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400120
121 if (val > maxval)
122 ERREXIT(cinfo, JERR_PPM_TOOLARGE);
123
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000124 return val;
125}
126
127
128/*
129 * Read one row of pixels.
130 *
131 * We provide several different versions depending on input file format.
132 * In all cases, input is scaled to the size of JSAMPLE.
133 *
134 * A really fast path is provided for reading byte/sample raw files with
135 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
136 */
137
138
139METHODDEF(JDIMENSION)
140get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
141/* This version is for reading text-format PGM files with any maxval */
142{
143 ppm_source_ptr source = (ppm_source_ptr) sinfo;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400144 FILE *infile = source->pub.input_file;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000145 register JSAMPROW ptr;
146 register JSAMPLE *rescale = source->rescale;
147 JDIMENSION col;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400148 unsigned int maxval = source->maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000149
150 ptr = source->pub.buffer[0];
151 for (col = cinfo->image_width; col > 0; col--) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400152 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000153 }
154 return 1;
155}
156
157
158METHODDEF(JDIMENSION)
159get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
160/* This version is for reading text-format PPM files with any maxval */
161{
162 ppm_source_ptr source = (ppm_source_ptr) sinfo;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400163 FILE *infile = source->pub.input_file;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000164 register JSAMPROW ptr;
165 register JSAMPLE *rescale = source->rescale;
166 JDIMENSION col;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400167 unsigned int maxval = source->maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000168
169 ptr = source->pub.buffer[0];
170 for (col = cinfo->image_width; col > 0; col--) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400171 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
172 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
173 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000174 }
175 return 1;
176}
177
178
179METHODDEF(JDIMENSION)
180get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
181/* This version is for reading raw-byte-format PGM files with any maxval */
182{
183 ppm_source_ptr source = (ppm_source_ptr) sinfo;
184 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400185 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000186 register JSAMPLE *rescale = source->rescale;
187 JDIMENSION col;
188
189 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
190 ERREXIT(cinfo, JERR_INPUT_EOF);
191 ptr = source->pub.buffer[0];
192 bufferptr = source->iobuffer;
193 for (col = cinfo->image_width; col > 0; col--) {
194 *ptr++ = rescale[UCH(*bufferptr++)];
195 }
196 return 1;
197}
198
199
200METHODDEF(JDIMENSION)
201get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
202/* This version is for reading raw-byte-format PPM files with any maxval */
203{
204 ppm_source_ptr source = (ppm_source_ptr) sinfo;
205 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400206 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000207 register JSAMPLE *rescale = source->rescale;
208 JDIMENSION col;
209
210 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
211 ERREXIT(cinfo, JERR_INPUT_EOF);
212 ptr = source->pub.buffer[0];
213 bufferptr = source->iobuffer;
214 for (col = cinfo->image_width; col > 0; col--) {
215 *ptr++ = rescale[UCH(*bufferptr++)];
216 *ptr++ = rescale[UCH(*bufferptr++)];
217 *ptr++ = rescale[UCH(*bufferptr++)];
218 }
219 return 1;
220}
221
222
223METHODDEF(JDIMENSION)
224get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
225/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
226 * In this case we just read right into the JSAMPLE buffer!
227 * Note that same code works for PPM and PGM files.
228 */
229{
230 ppm_source_ptr source = (ppm_source_ptr) sinfo;
231
232 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
233 ERREXIT(cinfo, JERR_INPUT_EOF);
234 return 1;
235}
236
237
238METHODDEF(JDIMENSION)
239get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
240/* This version is for reading raw-word-format PGM files with any maxval */
241{
242 ppm_source_ptr source = (ppm_source_ptr) sinfo;
243 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400244 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000245 register JSAMPLE *rescale = source->rescale;
246 JDIMENSION col;
247
248 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
249 ERREXIT(cinfo, JERR_INPUT_EOF);
250 ptr = source->pub.buffer[0];
251 bufferptr = source->iobuffer;
252 for (col = cinfo->image_width; col > 0; col--) {
253 register int temp;
hbono@chromium.org98626972011-08-03 03:13:08 +0000254 temp = UCH(*bufferptr++) << 8;
255 temp |= UCH(*bufferptr++);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000256 *ptr++ = rescale[temp];
257 }
258 return 1;
259}
260
261
262METHODDEF(JDIMENSION)
263get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
264/* This version is for reading raw-word-format PPM files with any maxval */
265{
266 ppm_source_ptr source = (ppm_source_ptr) sinfo;
267 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400268 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000269 register JSAMPLE *rescale = source->rescale;
270 JDIMENSION col;
271
272 if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
273 ERREXIT(cinfo, JERR_INPUT_EOF);
274 ptr = source->pub.buffer[0];
275 bufferptr = source->iobuffer;
276 for (col = cinfo->image_width; col > 0; col--) {
277 register int temp;
hbono@chromium.org98626972011-08-03 03:13:08 +0000278 temp = UCH(*bufferptr++) << 8;
279 temp |= UCH(*bufferptr++);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000280 *ptr++ = rescale[temp];
hbono@chromium.org98626972011-08-03 03:13:08 +0000281 temp = UCH(*bufferptr++) << 8;
282 temp |= UCH(*bufferptr++);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000283 *ptr++ = rescale[temp];
hbono@chromium.org98626972011-08-03 03:13:08 +0000284 temp = UCH(*bufferptr++) << 8;
285 temp |= UCH(*bufferptr++);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000286 *ptr++ = rescale[temp];
287 }
288 return 1;
289}
290
291
292/*
293 * Read the file header; return image size and component count.
294 */
295
296METHODDEF(void)
297start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
298{
299 ppm_source_ptr source = (ppm_source_ptr) sinfo;
300 int c;
301 unsigned int w, h, maxval;
302 boolean need_iobuffer, use_raw_buffer, need_rescale;
303
304 if (getc(source->pub.input_file) != 'P')
305 ERREXIT(cinfo, JERR_PPM_NOT);
306
307 c = getc(source->pub.input_file); /* subformat discriminator character */
308
309 /* detect unsupported variants (ie, PBM) before trying to read header */
310 switch (c) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400311 case '2': /* it's a text-format PGM file */
312 case '3': /* it's a text-format PPM file */
313 case '5': /* it's a raw-format PGM file */
314 case '6': /* it's a raw-format PPM file */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000315 break;
316 default:
317 ERREXIT(cinfo, JERR_PPM_NOT);
318 break;
319 }
320
321 /* fetch the remaining header info */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400322 w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
323 h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
324 maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000325
326 if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
327 ERREXIT(cinfo, JERR_PPM_NOT);
328
329 cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
330 cinfo->image_width = (JDIMENSION) w;
331 cinfo->image_height = (JDIMENSION) h;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400332 source->maxval = maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000333
334 /* initialize flags to most common settings */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400335 need_iobuffer = TRUE; /* do we need an I/O buffer? */
336 use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
337 need_rescale = TRUE; /* do we need a rescale array? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000338
339 switch (c) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400340 case '2': /* it's a text-format PGM file */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000341 cinfo->input_components = 1;
342 cinfo->in_color_space = JCS_GRAYSCALE;
343 TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
344 source->pub.get_pixel_rows = get_text_gray_row;
345 need_iobuffer = FALSE;
346 break;
347
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400348 case '3': /* it's a text-format PPM file */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000349 cinfo->input_components = 3;
350 cinfo->in_color_space = JCS_RGB;
351 TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
352 source->pub.get_pixel_rows = get_text_rgb_row;
353 need_iobuffer = FALSE;
354 break;
355
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400356 case '5': /* it's a raw-format PGM file */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000357 cinfo->input_components = 1;
358 cinfo->in_color_space = JCS_GRAYSCALE;
359 TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
360 if (maxval > 255) {
361 source->pub.get_pixel_rows = get_word_gray_row;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400362 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000363 source->pub.get_pixel_rows = get_raw_row;
364 use_raw_buffer = TRUE;
365 need_rescale = FALSE;
366 } else {
367 source->pub.get_pixel_rows = get_scaled_gray_row;
368 }
369 break;
370
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400371 case '6': /* it's a raw-format PPM file */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000372 cinfo->input_components = 3;
373 cinfo->in_color_space = JCS_RGB;
374 TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
375 if (maxval > 255) {
376 source->pub.get_pixel_rows = get_word_rgb_row;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400377 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000378 source->pub.get_pixel_rows = get_raw_row;
379 use_raw_buffer = TRUE;
380 need_rescale = FALSE;
381 } else {
382 source->pub.get_pixel_rows = get_scaled_rgb_row;
383 }
384 break;
385 }
386
387 /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
388 if (need_iobuffer) {
389 source->buffer_width = (size_t) w * cinfo->input_components *
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400390 ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000391 source->iobuffer = (U_CHAR *)
392 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400393 source->buffer_width);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000394 }
395
396 /* Create compressor input buffer. */
397 if (use_raw_buffer) {
398 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
399 /* Synthesize a JSAMPARRAY pointer structure */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000400 source->pixrow = (JSAMPROW) source->iobuffer;
401 source->pub.buffer = & source->pixrow;
402 source->pub.buffer_height = 1;
403 } else {
404 /* Need to translate anyway, so make a separate sample buffer. */
405 source->pub.buffer = (*cinfo->mem->alloc_sarray)
406 ((j_common_ptr) cinfo, JPOOL_IMAGE,
407 (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
408 source->pub.buffer_height = 1;
409 }
410
411 /* Compute the rescaling array if required. */
412 if (need_rescale) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400413 long val, half_maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000414
415 /* On 16-bit-int machines we have to be careful of maxval = 65535 */
416 source->rescale = (JSAMPLE *)
417 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400418 (size_t) (((long) maxval + 1L) *
419 sizeof(JSAMPLE)));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000420 half_maxval = maxval / 2;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400421 for (val = 0; val <= (long) maxval; val++) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000422 /* The multiplication here must be done in 32 bits to avoid overflow */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400423 source->rescale[val] = (JSAMPLE) ((val * MAXJSAMPLE + half_maxval) /
424 maxval);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000425 }
426 }
427}
428
429
430/*
431 * Finish up at the end of the file.
432 */
433
434METHODDEF(void)
435finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
436{
437 /* no work */
438}
439
440
441/*
442 * The module selection routine for PPM format input.
443 */
444
445GLOBAL(cjpeg_source_ptr)
446jinit_read_ppm (j_compress_ptr cinfo)
447{
448 ppm_source_ptr source;
449
450 /* Create module interface object */
451 source = (ppm_source_ptr)
452 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400453 sizeof(ppm_source_struct));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000454 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
455 source->pub.start_input = start_input_ppm;
456 source->pub.finish_input = finish_input_ppm;
457
458 return (cjpeg_source_ptr) source;
459}
460
461#endif /* PPM_SUPPORTED */