blob: 9699ca5ee850c682f6be59da1f83da68bd29b1bf [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:
Jonathan Wright24e31052021-04-26 12:10:48 +01008 * Copyright (C) 2015-2017, 2020-2021, D. R. Commander.
Tom Hudson0d47d2d2016-05-04 13:22:56 -04009 * 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
Chris Blumecca8c4d2019-03-01 01:09:50 -080025#include "cmyk.h"
Tom Hudson0d47d2d2016-05-04 13:22:56 -040026#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000027
28#ifdef PPM_SUPPORTED
29
30
31/* Portions of this code are based on the PBMPLUS library, which is:
32**
33** Copyright (C) 1988 by Jef Poskanzer.
34**
35** Permission to use, copy, modify, and distribute this software and its
36** documentation for any purpose and without fee is hereby granted, provided
37** that the above copyright notice appear in all copies and that both that
38** copyright notice and this permission notice appear in supporting
39** documentation. This software is provided "as is" without express or
40** implied warranty.
41*/
42
43
44/* Macros to deal with unsigned chars as efficiently as compiler allows */
45
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000046typedef unsigned char U_CHAR;
Chris Blumecca8c4d2019-03-01 01:09:50 -080047#define UCH(x) ((int)(x))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000048
49
Chris Blumecca8c4d2019-03-01 01:09:50 -080050#define ReadOK(file, buffer, len) \
51 (JFREAD(file, buffer, len) == ((size_t)(len)))
52
53static int alpha_index[JPEG_NUMCS] = {
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
55};
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000056
57
58/* Private version of data source object */
59
60typedef struct {
61 struct cjpeg_source_struct pub; /* public fields */
62
Tom Hudson0d47d2d2016-05-04 13:22:56 -040063 /* Usually these two pointers point to the same place: */
64 U_CHAR *iobuffer; /* fread's I/O buffer */
65 JSAMPROW pixrow; /* compressor input buffer */
66 size_t buffer_width; /* width of I/O buffer */
67 JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
Chris Blumecca8c4d2019-03-01 01:09:50 -080068 unsigned int maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000069} ppm_source_struct;
70
Tom Hudson0d47d2d2016-05-04 13:22:56 -040071typedef ppm_source_struct *ppm_source_ptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000072
73
74LOCAL(int)
Chris Blumecca8c4d2019-03-01 01:09:50 -080075pbm_getc(FILE *infile)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000076/* Read next char, skipping over any comments */
77/* A comment/newline sequence is returned as a newline */
78{
79 register int ch;
80
81 ch = getc(infile);
82 if (ch == '#') {
83 do {
84 ch = getc(infile);
85 } while (ch != '\n' && ch != EOF);
86 }
87 return ch;
88}
89
90
91LOCAL(unsigned int)
Chris Blumecca8c4d2019-03-01 01:09:50 -080092read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +000093/* Read an unsigned decimal integer from the PPM file */
94/* Swallows one trailing character after the integer */
95/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
96/* This should not be a problem in practice. */
97{
98 register int ch;
99 register unsigned int val;
100
101 /* Skip any leading whitespace */
102 do {
103 ch = pbm_getc(infile);
104 if (ch == EOF)
105 ERREXIT(cinfo, JERR_INPUT_EOF);
106 } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
107
108 if (ch < '0' || ch > '9')
109 ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
110
111 val = ch - '0';
112 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
113 val *= 10;
114 val += ch - '0';
Jonathan Wright24e31052021-04-26 12:10:48 +0100115 if (val > maxval)
116 ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000117 }
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400118
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000119 return val;
120}
121
122
123/*
124 * Read one row of pixels.
125 *
126 * We provide several different versions depending on input file format.
127 * In all cases, input is scaled to the size of JSAMPLE.
128 *
129 * A really fast path is provided for reading byte/sample raw files with
130 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
131 */
132
133
134METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800135get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000136/* This version is for reading text-format PGM files with any maxval */
137{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800138 ppm_source_ptr source = (ppm_source_ptr)sinfo;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400139 FILE *infile = source->pub.input_file;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000140 register JSAMPROW ptr;
141 register JSAMPLE *rescale = source->rescale;
142 JDIMENSION col;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400143 unsigned int maxval = source->maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000144
145 ptr = source->pub.buffer[0];
146 for (col = cinfo->image_width; col > 0; col--) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400147 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000148 }
149 return 1;
150}
151
152
Chris Blumecca8c4d2019-03-01 01:09:50 -0800153#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
154 for (col = cinfo->image_width; col > 0; col--) { \
155 ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
156 alpha_set_op \
157 ptr += ps; \
158 } \
159}
160
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000161METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800162get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
163/* This version is for reading text-format PGM files with any maxval and
164 converting to extended RGB */
165{
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;
171 unsigned int maxval = source->maxval;
172 register int rindex = rgb_red[cinfo->in_color_space];
173 register int gindex = rgb_green[cinfo->in_color_space];
174 register int bindex = rgb_blue[cinfo->in_color_space];
175 register int aindex = alpha_index[cinfo->in_color_space];
176 register int ps = rgb_pixelsize[cinfo->in_color_space];
177
178 ptr = source->pub.buffer[0];
179 if (maxval == MAXJSAMPLE) {
180 if (aindex >= 0)
181 GRAY_RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),
182 ptr[aindex] = 0xFF;)
183 else
184 GRAY_RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),)
185 } else {
186 if (aindex >= 0)
187 GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
188 ptr[aindex] = 0xFF;)
189 else
190 GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],)
191 }
192 return 1;
193}
194
195
196METHODDEF(JDIMENSION)
197get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
198/* This version is for reading text-format PGM files with any maxval and
199 converting to CMYK */
200{
201 ppm_source_ptr source = (ppm_source_ptr)sinfo;
202 FILE *infile = source->pub.input_file;
203 register JSAMPROW ptr;
204 register JSAMPLE *rescale = source->rescale;
205 JDIMENSION col;
206 unsigned int maxval = source->maxval;
207
208 ptr = source->pub.buffer[0];
209 if (maxval == MAXJSAMPLE) {
210 for (col = cinfo->image_width; col > 0; col--) {
211 JSAMPLE gray = read_pbm_integer(cinfo, infile, maxval);
212 rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
213 ptr += 4;
214 }
215 } else {
216 for (col = cinfo->image_width; col > 0; col--) {
217 JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
218 rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
219 ptr += 4;
220 }
221 }
222 return 1;
223}
224
225
226#define RGB_READ_LOOP(read_op, alpha_set_op) { \
227 for (col = cinfo->image_width; col > 0; col--) { \
228 ptr[rindex] = read_op; \
229 ptr[gindex] = read_op; \
230 ptr[bindex] = read_op; \
231 alpha_set_op \
232 ptr += ps; \
233 } \
234}
235
236METHODDEF(JDIMENSION)
237get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000238/* This version is for reading text-format PPM files with any maxval */
239{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800240 ppm_source_ptr source = (ppm_source_ptr)sinfo;
241 FILE *infile = source->pub.input_file;
242 register JSAMPROW ptr;
243 register JSAMPLE *rescale = source->rescale;
244 JDIMENSION col;
245 unsigned int maxval = source->maxval;
246 register int rindex = rgb_red[cinfo->in_color_space];
247 register int gindex = rgb_green[cinfo->in_color_space];
248 register int bindex = rgb_blue[cinfo->in_color_space];
249 register int aindex = alpha_index[cinfo->in_color_space];
250 register int ps = rgb_pixelsize[cinfo->in_color_space];
251
252 ptr = source->pub.buffer[0];
253 if (maxval == MAXJSAMPLE) {
254 if (aindex >= 0)
255 RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),
256 ptr[aindex] = 0xFF;)
257 else
258 RGB_READ_LOOP(read_pbm_integer(cinfo, infile, maxval),)
259 } else {
260 if (aindex >= 0)
261 RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
262 ptr[aindex] = 0xFF;)
263 else
264 RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],)
265 }
266 return 1;
267}
268
269
270METHODDEF(JDIMENSION)
271get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
272/* This version is for reading text-format PPM files with any maxval and
273 converting to CMYK */
274{
275 ppm_source_ptr source = (ppm_source_ptr)sinfo;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400276 FILE *infile = source->pub.input_file;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000277 register JSAMPROW ptr;
278 register JSAMPLE *rescale = source->rescale;
279 JDIMENSION col;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400280 unsigned int maxval = source->maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000281
282 ptr = source->pub.buffer[0];
Chris Blumecca8c4d2019-03-01 01:09:50 -0800283 if (maxval == MAXJSAMPLE) {
284 for (col = cinfo->image_width; col > 0; col--) {
285 JSAMPLE r = read_pbm_integer(cinfo, infile, maxval);
286 JSAMPLE g = read_pbm_integer(cinfo, infile, maxval);
287 JSAMPLE b = read_pbm_integer(cinfo, infile, maxval);
288 rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
289 ptr += 4;
290 }
291 } else {
292 for (col = cinfo->image_width; col > 0; col--) {
293 JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
294 JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
295 JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
296 rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
297 ptr += 4;
298 }
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000299 }
300 return 1;
301}
302
303
304METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800305get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000306/* This version is for reading raw-byte-format PGM files with any maxval */
307{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800308 ppm_source_ptr source = (ppm_source_ptr)sinfo;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000309 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400310 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000311 register JSAMPLE *rescale = source->rescale;
312 JDIMENSION col;
313
Chris Blumecca8c4d2019-03-01 01:09:50 -0800314 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000315 ERREXIT(cinfo, JERR_INPUT_EOF);
316 ptr = source->pub.buffer[0];
317 bufferptr = source->iobuffer;
318 for (col = cinfo->image_width; col > 0; col--) {
319 *ptr++ = rescale[UCH(*bufferptr++)];
320 }
321 return 1;
322}
323
324
325METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800326get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
327/* This version is for reading raw-byte-format PGM files with any maxval
328 and converting to extended RGB */
329{
330 ppm_source_ptr source = (ppm_source_ptr)sinfo;
331 register JSAMPROW ptr;
332 register U_CHAR *bufferptr;
333 register JSAMPLE *rescale = source->rescale;
334 JDIMENSION col;
335 unsigned int maxval = source->maxval;
336 register int rindex = rgb_red[cinfo->in_color_space];
337 register int gindex = rgb_green[cinfo->in_color_space];
338 register int bindex = rgb_blue[cinfo->in_color_space];
339 register int aindex = alpha_index[cinfo->in_color_space];
340 register int ps = rgb_pixelsize[cinfo->in_color_space];
341
342 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
343 ERREXIT(cinfo, JERR_INPUT_EOF);
344 ptr = source->pub.buffer[0];
345 bufferptr = source->iobuffer;
346 if (maxval == MAXJSAMPLE) {
347 if (aindex >= 0)
348 GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = 0xFF;)
349 else
350 GRAY_RGB_READ_LOOP(*bufferptr++,)
351 } else {
352 if (aindex >= 0)
353 GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = 0xFF;)
354 else
355 GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],)
356 }
357 return 1;
358}
359
360
361METHODDEF(JDIMENSION)
362get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
363/* This version is for reading raw-byte-format PGM files with any maxval
364 and converting to CMYK */
365{
366 ppm_source_ptr source = (ppm_source_ptr)sinfo;
367 register JSAMPROW ptr;
368 register U_CHAR *bufferptr;
369 register JSAMPLE *rescale = source->rescale;
370 JDIMENSION col;
371 unsigned int maxval = source->maxval;
372
373 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
374 ERREXIT(cinfo, JERR_INPUT_EOF);
375 ptr = source->pub.buffer[0];
376 bufferptr = source->iobuffer;
377 if (maxval == MAXJSAMPLE) {
378 for (col = cinfo->image_width; col > 0; col--) {
379 JSAMPLE gray = *bufferptr++;
380 rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
381 ptr += 4;
382 }
383 } else {
384 for (col = cinfo->image_width; col > 0; col--) {
385 JSAMPLE gray = rescale[UCH(*bufferptr++)];
386 rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
387 ptr += 4;
388 }
389 }
390 return 1;
391}
392
393
394METHODDEF(JDIMENSION)
395get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000396/* This version is for reading raw-byte-format PPM files with any maxval */
397{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800398 ppm_source_ptr source = (ppm_source_ptr)sinfo;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000399 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400400 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000401 register JSAMPLE *rescale = source->rescale;
402 JDIMENSION col;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800403 unsigned int maxval = source->maxval;
404 register int rindex = rgb_red[cinfo->in_color_space];
405 register int gindex = rgb_green[cinfo->in_color_space];
406 register int bindex = rgb_blue[cinfo->in_color_space];
407 register int aindex = alpha_index[cinfo->in_color_space];
408 register int ps = rgb_pixelsize[cinfo->in_color_space];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000409
Chris Blumecca8c4d2019-03-01 01:09:50 -0800410 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000411 ERREXIT(cinfo, JERR_INPUT_EOF);
412 ptr = source->pub.buffer[0];
413 bufferptr = source->iobuffer;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800414 if (maxval == MAXJSAMPLE) {
415 if (aindex >= 0)
416 RGB_READ_LOOP(*bufferptr++, ptr[aindex] = 0xFF;)
417 else
418 RGB_READ_LOOP(*bufferptr++,)
419 } else {
420 if (aindex >= 0)
421 RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = 0xFF;)
422 else
423 RGB_READ_LOOP(rescale[UCH(*bufferptr++)],)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000424 }
425 return 1;
426}
427
428
429METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800430get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
431/* This version is for reading raw-byte-format PPM files with any maxval and
432 converting to CMYK */
433{
434 ppm_source_ptr source = (ppm_source_ptr)sinfo;
435 register JSAMPROW ptr;
436 register U_CHAR *bufferptr;
437 register JSAMPLE *rescale = source->rescale;
438 JDIMENSION col;
439 unsigned int maxval = source->maxval;
440
441 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
442 ERREXIT(cinfo, JERR_INPUT_EOF);
443 ptr = source->pub.buffer[0];
444 bufferptr = source->iobuffer;
445 if (maxval == MAXJSAMPLE) {
446 for (col = cinfo->image_width; col > 0; col--) {
447 JSAMPLE r = *bufferptr++;
448 JSAMPLE g = *bufferptr++;
449 JSAMPLE b = *bufferptr++;
450 rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
451 ptr += 4;
452 }
453 } else {
454 for (col = cinfo->image_width; col > 0; col--) {
455 JSAMPLE r = rescale[UCH(*bufferptr++)];
456 JSAMPLE g = rescale[UCH(*bufferptr++)];
457 JSAMPLE b = rescale[UCH(*bufferptr++)];
458 rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
459 ptr += 4;
460 }
461 }
462 return 1;
463}
464
465
466METHODDEF(JDIMENSION)
467get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000468/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
469 * In this case we just read right into the JSAMPLE buffer!
470 * Note that same code works for PPM and PGM files.
471 */
472{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800473 ppm_source_ptr source = (ppm_source_ptr)sinfo;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000474
Chris Blumecca8c4d2019-03-01 01:09:50 -0800475 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000476 ERREXIT(cinfo, JERR_INPUT_EOF);
477 return 1;
478}
479
480
481METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800482get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000483/* This version is for reading raw-word-format PGM files with any maxval */
484{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800485 ppm_source_ptr source = (ppm_source_ptr)sinfo;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000486 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400487 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000488 register JSAMPLE *rescale = source->rescale;
489 JDIMENSION col;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800490 unsigned int maxval = source->maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000491
Chris Blumecca8c4d2019-03-01 01:09:50 -0800492 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000493 ERREXIT(cinfo, JERR_INPUT_EOF);
494 ptr = source->pub.buffer[0];
495 bufferptr = source->iobuffer;
496 for (col = cinfo->image_width; col > 0; col--) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800497 register unsigned int temp;
hbono@chromium.org98626972011-08-03 03:13:08 +0000498 temp = UCH(*bufferptr++) << 8;
499 temp |= UCH(*bufferptr++);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800500 if (temp > maxval)
501 ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000502 *ptr++ = rescale[temp];
503 }
504 return 1;
505}
506
507
508METHODDEF(JDIMENSION)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800509get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000510/* This version is for reading raw-word-format PPM files with any maxval */
511{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800512 ppm_source_ptr source = (ppm_source_ptr)sinfo;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000513 register JSAMPROW ptr;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400514 register U_CHAR *bufferptr;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000515 register JSAMPLE *rescale = source->rescale;
516 JDIMENSION col;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800517 unsigned int maxval = source->maxval;
Jonathan Wright24e31052021-04-26 12:10:48 +0100518 register int rindex = rgb_red[cinfo->in_color_space];
519 register int gindex = rgb_green[cinfo->in_color_space];
520 register int bindex = rgb_blue[cinfo->in_color_space];
521 register int aindex = alpha_index[cinfo->in_color_space];
522 register int ps = rgb_pixelsize[cinfo->in_color_space];
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000523
Chris Blumecca8c4d2019-03-01 01:09:50 -0800524 if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000525 ERREXIT(cinfo, JERR_INPUT_EOF);
526 ptr = source->pub.buffer[0];
527 bufferptr = source->iobuffer;
528 for (col = cinfo->image_width; col > 0; col--) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800529 register unsigned int temp;
hbono@chromium.org98626972011-08-03 03:13:08 +0000530 temp = UCH(*bufferptr++) << 8;
531 temp |= UCH(*bufferptr++);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800532 if (temp > maxval)
533 ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
Jonathan Wright24e31052021-04-26 12:10:48 +0100534 ptr[rindex] = rescale[temp];
hbono@chromium.org98626972011-08-03 03:13:08 +0000535 temp = UCH(*bufferptr++) << 8;
536 temp |= UCH(*bufferptr++);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800537 if (temp > maxval)
538 ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
Jonathan Wright24e31052021-04-26 12:10:48 +0100539 ptr[gindex] = rescale[temp];
hbono@chromium.org98626972011-08-03 03:13:08 +0000540 temp = UCH(*bufferptr++) << 8;
541 temp |= UCH(*bufferptr++);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800542 if (temp > maxval)
543 ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
Jonathan Wright24e31052021-04-26 12:10:48 +0100544 ptr[bindex] = rescale[temp];
545 if (aindex >= 0)
546 ptr[aindex] = 0xFF;
547 ptr += ps;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000548 }
549 return 1;
550}
551
552
553/*
554 * Read the file header; return image size and component count.
555 */
556
557METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800558start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000559{
Chris Blumecca8c4d2019-03-01 01:09:50 -0800560 ppm_source_ptr source = (ppm_source_ptr)sinfo;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000561 int c;
562 unsigned int w, h, maxval;
563 boolean need_iobuffer, use_raw_buffer, need_rescale;
564
565 if (getc(source->pub.input_file) != 'P')
566 ERREXIT(cinfo, JERR_PPM_NOT);
567
568 c = getc(source->pub.input_file); /* subformat discriminator character */
569
570 /* detect unsupported variants (ie, PBM) before trying to read header */
571 switch (c) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400572 case '2': /* it's a text-format PGM file */
573 case '3': /* it's a text-format PPM file */
574 case '5': /* it's a raw-format PGM file */
575 case '6': /* it's a raw-format PPM file */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000576 break;
577 default:
578 ERREXIT(cinfo, JERR_PPM_NOT);
579 break;
580 }
581
582 /* fetch the remaining header info */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400583 w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
584 h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
585 maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000586
587 if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
588 ERREXIT(cinfo, JERR_PPM_NOT);
Jonathan Wright24e31052021-04-26 12:10:48 +0100589#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
590 if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
591 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
592#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000593
594 cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800595 cinfo->image_width = (JDIMENSION)w;
596 cinfo->image_height = (JDIMENSION)h;
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400597 source->maxval = maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000598
599 /* initialize flags to most common settings */
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400600 need_iobuffer = TRUE; /* do we need an I/O buffer? */
601 use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */
602 need_rescale = TRUE; /* do we need a rescale array? */
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000603
604 switch (c) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400605 case '2': /* it's a text-format PGM file */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800606 if (cinfo->in_color_space == JCS_UNKNOWN)
607 cinfo->in_color_space = JCS_GRAYSCALE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000608 TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800609 if (cinfo->in_color_space == JCS_GRAYSCALE)
610 source->pub.get_pixel_rows = get_text_gray_row;
611 else if (IsExtRGB(cinfo->in_color_space))
612 source->pub.get_pixel_rows = get_text_gray_rgb_row;
613 else if (cinfo->in_color_space == JCS_CMYK)
614 source->pub.get_pixel_rows = get_text_gray_cmyk_row;
615 else
616 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000617 need_iobuffer = FALSE;
618 break;
619
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400620 case '3': /* it's a text-format PPM file */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800621 if (cinfo->in_color_space == JCS_UNKNOWN)
622 cinfo->in_color_space = JCS_EXT_RGB;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000623 TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800624 if (IsExtRGB(cinfo->in_color_space))
625 source->pub.get_pixel_rows = get_text_rgb_row;
626 else if (cinfo->in_color_space == JCS_CMYK)
627 source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
628 else
629 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000630 need_iobuffer = FALSE;
631 break;
632
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400633 case '5': /* it's a raw-format PGM file */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800634 if (cinfo->in_color_space == JCS_UNKNOWN)
635 cinfo->in_color_space = JCS_GRAYSCALE;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000636 TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
637 if (maxval > 255) {
Jonathan Wright24e31052021-04-26 12:10:48 +0100638 if (cinfo->in_color_space == JCS_GRAYSCALE)
639 source->pub.get_pixel_rows = get_word_gray_row;
640 else
641 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800642 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
643 cinfo->in_color_space == JCS_GRAYSCALE) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000644 source->pub.get_pixel_rows = get_raw_row;
645 use_raw_buffer = TRUE;
646 need_rescale = FALSE;
647 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800648 if (cinfo->in_color_space == JCS_GRAYSCALE)
649 source->pub.get_pixel_rows = get_scaled_gray_row;
650 else if (IsExtRGB(cinfo->in_color_space))
651 source->pub.get_pixel_rows = get_gray_rgb_row;
652 else if (cinfo->in_color_space == JCS_CMYK)
653 source->pub.get_pixel_rows = get_gray_cmyk_row;
654 else
655 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000656 }
657 break;
658
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400659 case '6': /* it's a raw-format PPM file */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800660 if (cinfo->in_color_space == JCS_UNKNOWN)
661 cinfo->in_color_space = JCS_EXT_RGB;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000662 TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
663 if (maxval > 255) {
Jonathan Wright24e31052021-04-26 12:10:48 +0100664 if (IsExtRGB(cinfo->in_color_space))
665 source->pub.get_pixel_rows = get_word_rgb_row;
666 else
667 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
Chris Blumecca8c4d2019-03-01 01:09:50 -0800668 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
Chris Blumecca8c4d2019-03-01 01:09:50 -0800669#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
Jonathan Wrightbbb82822020-11-25 13:36:43 +0000670 (cinfo->in_color_space == JCS_EXT_RGB ||
671 cinfo->in_color_space == JCS_RGB)) {
672#else
673 cinfo->in_color_space == JCS_EXT_RGB) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800674#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000675 source->pub.get_pixel_rows = get_raw_row;
676 use_raw_buffer = TRUE;
677 need_rescale = FALSE;
678 } else {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800679 if (IsExtRGB(cinfo->in_color_space))
680 source->pub.get_pixel_rows = get_rgb_row;
681 else if (cinfo->in_color_space == JCS_CMYK)
682 source->pub.get_pixel_rows = get_rgb_cmyk_row;
683 else
684 ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000685 }
686 break;
687 }
688
Chris Blumecca8c4d2019-03-01 01:09:50 -0800689 if (IsExtRGB(cinfo->in_color_space))
690 cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
691 else if (cinfo->in_color_space == JCS_GRAYSCALE)
692 cinfo->input_components = 1;
693 else if (cinfo->in_color_space == JCS_CMYK)
694 cinfo->input_components = 4;
695
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000696 /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
697 if (need_iobuffer) {
Chris Blumecca8c4d2019-03-01 01:09:50 -0800698 if (c == '6')
699 source->buffer_width = (size_t)w * 3 *
700 ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
701 else
702 source->buffer_width = (size_t)w *
703 ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000704 source->iobuffer = (U_CHAR *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800705 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400706 source->buffer_width);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000707 }
708
709 /* Create compressor input buffer. */
710 if (use_raw_buffer) {
711 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
712 /* Synthesize a JSAMPARRAY pointer structure */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800713 source->pixrow = (JSAMPROW)source->iobuffer;
714 source->pub.buffer = &source->pixrow;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000715 source->pub.buffer_height = 1;
716 } else {
717 /* Need to translate anyway, so make a separate sample buffer. */
718 source->pub.buffer = (*cinfo->mem->alloc_sarray)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800719 ((j_common_ptr)cinfo, JPOOL_IMAGE,
720 (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000721 source->pub.buffer_height = 1;
722 }
723
724 /* Compute the rescaling array if required. */
725 if (need_rescale) {
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400726 long val, half_maxval;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000727
728 /* On 16-bit-int machines we have to be careful of maxval = 65535 */
729 source->rescale = (JSAMPLE *)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800730 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
Jonathan Wrightdb870df2020-08-05 11:42:22 +0100731 (size_t)(((long)MAX(maxval, 255) + 1L) *
Chris Blumecca8c4d2019-03-01 01:09:50 -0800732 sizeof(JSAMPLE)));
Jonathan Wright24e31052021-04-26 12:10:48 +0100733 MEMZERO(source->rescale, (size_t)(((long)MAX(maxval, 255) + 1L) *
734 sizeof(JSAMPLE)));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000735 half_maxval = maxval / 2;
Chris Blumecca8c4d2019-03-01 01:09:50 -0800736 for (val = 0; val <= (long)maxval; val++) {
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000737 /* The multiplication here must be done in 32 bits to avoid overflow */
Chris Blumecca8c4d2019-03-01 01:09:50 -0800738 source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) /
Tom Hudson0d47d2d2016-05-04 13:22:56 -0400739 maxval);
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000740 }
741 }
742}
743
744
745/*
746 * Finish up at the end of the file.
747 */
748
749METHODDEF(void)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800750finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000751{
752 /* no work */
753}
754
755
756/*
757 * The module selection routine for PPM format input.
758 */
759
760GLOBAL(cjpeg_source_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800761jinit_read_ppm(j_compress_ptr cinfo)
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000762{
763 ppm_source_ptr source;
764
765 /* Create module interface object */
766 source = (ppm_source_ptr)
Chris Blumecca8c4d2019-03-01 01:09:50 -0800767 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
768 sizeof(ppm_source_struct));
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000769 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
770 source->pub.start_input = start_input_ppm;
771 source->pub.finish_input = finish_input_ppm;
Jonathan Wright24e31052021-04-26 12:10:48 +0100772#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
773 source->pub.max_pixels = 0;
774#endif
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000775
Chris Blumecca8c4d2019-03-01 01:09:50 -0800776 return (cjpeg_source_ptr)source;
hbono@chromium.orgf0c4f332010-11-01 05:14:55 +0000777}
778
779#endif /* PPM_SUPPORTED */