/src/libjpeg-turbo.2.0.x/rdppm.c
Line | Count | Source (jump to first uncovered line) |
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 | | * libjpeg-turbo Modifications: |
8 | | * Copyright (C) 2015-2017, 2020-2023, D. R. Commander. |
9 | | * For conditions of distribution and use, see the accompanying README.ijg |
10 | | * file. |
11 | | * |
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 | | |
25 | | #include "cmyk.h" |
26 | | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
27 | | |
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 | | |
46 | | #ifdef HAVE_UNSIGNED_CHAR |
47 | | typedef unsigned char U_CHAR; |
48 | 11.4M | #define UCH(x) ((int)(x)) |
49 | | #else /* !HAVE_UNSIGNED_CHAR */ |
50 | | #ifdef __CHAR_UNSIGNED__ |
51 | | typedef char U_CHAR; |
52 | | #define UCH(x) ((int)(x)) |
53 | | #else |
54 | | typedef char U_CHAR; |
55 | | #define UCH(x) ((int)(x) & 0xFF) |
56 | | #endif |
57 | | #endif /* HAVE_UNSIGNED_CHAR */ |
58 | | |
59 | | |
60 | | #define ReadOK(file, buffer, len) \ |
61 | 22.5M | (JFREAD(file, buffer, len) == ((size_t)(len))) |
62 | | |
63 | | static int alpha_index[JPEG_NUMCS] = { |
64 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1 |
65 | | }; |
66 | | |
67 | | |
68 | | /* Private version of data source object */ |
69 | | |
70 | | typedef struct { |
71 | | struct cjpeg_source_struct pub; /* public fields */ |
72 | | |
73 | | /* Usually these two pointers point to the same place: */ |
74 | | U_CHAR *iobuffer; /* fread's I/O buffer */ |
75 | | JSAMPROW pixrow; /* compressor input buffer */ |
76 | | size_t buffer_width; /* width of I/O buffer */ |
77 | | JSAMPLE *rescale; /* => maxval-remapping array, or NULL */ |
78 | | unsigned int maxval; |
79 | | } ppm_source_struct; |
80 | | |
81 | | typedef ppm_source_struct *ppm_source_ptr; |
82 | | |
83 | | |
84 | | LOCAL(int) |
85 | | pbm_getc(FILE *infile) |
86 | | /* Read next char, skipping over any comments */ |
87 | | /* A comment/newline sequence is returned as a newline */ |
88 | 271k | { |
89 | 271k | register int ch; |
90 | | |
91 | 271k | ch = getc(infile); |
92 | 271k | if (ch == '#') { |
93 | 117k | do { |
94 | 117k | ch = getc(infile); |
95 | 117k | } while (ch != '\n' && ch != EOF); |
96 | 5.70k | } |
97 | 271k | return ch; |
98 | 271k | } |
99 | | |
100 | | |
101 | | LOCAL(unsigned int) |
102 | | read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval) |
103 | | /* Read an unsigned decimal integer from the PPM file */ |
104 | | /* Swallows one trailing character after the integer */ |
105 | | /* Note that on a 16-bit-int machine, only values up to 64k can be read. */ |
106 | | /* This should not be a problem in practice. */ |
107 | 112k | { |
108 | 112k | register int ch; |
109 | 112k | register unsigned int val; |
110 | | |
111 | | /* Skip any leading whitespace */ |
112 | 125k | do { |
113 | 125k | ch = pbm_getc(infile); |
114 | 125k | if (ch == EOF) |
115 | 2.64k | ERREXIT(cinfo, JERR_INPUT_EOF); |
116 | 125k | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
117 | | |
118 | 112k | if (ch < '0' || ch > '9') |
119 | 242 | ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
120 | | |
121 | 112k | val = ch - '0'; |
122 | 149k | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
123 | 36.5k | val *= 10; |
124 | 36.5k | val += ch - '0'; |
125 | 36.5k | if (val > maxval) |
126 | 172 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
127 | 36.5k | } |
128 | | |
129 | 112k | return val; |
130 | 112k | } |
131 | | |
132 | | |
133 | | /* |
134 | | * Read one row of pixels. |
135 | | * |
136 | | * We provide several different versions depending on input file format. |
137 | | * In all cases, input is scaled to the size of JSAMPLE. |
138 | | * |
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. |
141 | | */ |
142 | | |
143 | | |
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 */ |
147 | 1.43k | { |
148 | 1.43k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
149 | 1.43k | FILE *infile = source->pub.input_file; |
150 | 1.43k | register JSAMPROW ptr; |
151 | 1.43k | register JSAMPLE *rescale = source->rescale; |
152 | 1.43k | JDIMENSION col; |
153 | 1.43k | unsigned int maxval = source->maxval; |
154 | | |
155 | 1.43k | ptr = source->pub.buffer[0]; |
156 | 3.90k | for (col = cinfo->image_width; col > 0; col--) { |
157 | 2.47k | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
158 | 2.47k | } |
159 | 1.43k | return 1; |
160 | 1.43k | } |
161 | | |
162 | | |
163 | 16.0M | #define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \ |
164 | 44.5M | for (col = cinfo->image_width; col > 0; col--) { \ |
165 | 28.4M | ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \ |
166 | 28.4M | alpha_set_op \ |
167 | 28.4M | ptr += ps; \ |
168 | 28.4M | } \ |
169 | 16.0M | } |
170 | | |
171 | | METHODDEF(JDIMENSION) |
172 | | get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
173 | | /* This version is for reading text-format PGM files with any maxval and |
174 | | converting to extended RGB */ |
175 | 7.18k | { |
176 | 7.18k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
177 | 7.18k | FILE *infile = source->pub.input_file; |
178 | 7.18k | register JSAMPROW ptr; |
179 | 7.18k | register JSAMPLE *rescale = source->rescale; |
180 | 7.18k | JDIMENSION col; |
181 | 7.18k | unsigned int maxval = source->maxval; |
182 | 7.18k | register int rindex = rgb_red[cinfo->in_color_space]; |
183 | 7.18k | register int gindex = rgb_green[cinfo->in_color_space]; |
184 | 7.18k | register int bindex = rgb_blue[cinfo->in_color_space]; |
185 | 7.18k | register int aindex = alpha_index[cinfo->in_color_space]; |
186 | 7.18k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
187 | | |
188 | 7.18k | ptr = source->pub.buffer[0]; |
189 | 7.18k | if (maxval == MAXJSAMPLE) { |
190 | 1.37k | if (aindex >= 0) |
191 | 275 | GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
192 | 1.37k | ptr[aindex] = MAXJSAMPLE;) |
193 | 1.10k | else |
194 | 1.10k | GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
195 | 5.81k | } else { |
196 | 5.81k | if (aindex >= 0) |
197 | 1.16k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
198 | 5.81k | ptr[aindex] = MAXJSAMPLE;) |
199 | 4.64k | else |
200 | 4.64k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
201 | 5.81k | } |
202 | 7.18k | return 1; |
203 | 7.18k | } |
204 | | |
205 | | |
206 | | METHODDEF(JDIMENSION) |
207 | | get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
208 | | /* This version is for reading text-format PGM files with any maxval and |
209 | | converting to CMYK */ |
210 | 1.43k | { |
211 | 1.43k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
212 | 1.43k | FILE *infile = source->pub.input_file; |
213 | 1.43k | register JSAMPROW ptr; |
214 | 1.43k | register JSAMPLE *rescale = source->rescale; |
215 | 1.43k | JDIMENSION col; |
216 | 1.43k | unsigned int maxval = source->maxval; |
217 | | |
218 | 1.43k | ptr = source->pub.buffer[0]; |
219 | 1.43k | if (maxval == MAXJSAMPLE) { |
220 | 836 | for (col = cinfo->image_width; col > 0; col--) { |
221 | 561 | JSAMPLE gray = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
222 | 561 | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
223 | 561 | ptr += 4; |
224 | 561 | } |
225 | 1.16k | } else { |
226 | 3.07k | for (col = cinfo->image_width; col > 0; col--) { |
227 | 1.91k | JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
228 | 1.91k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
229 | 1.91k | ptr += 4; |
230 | 1.91k | } |
231 | 1.16k | } |
232 | 1.43k | return 1; |
233 | 1.43k | } |
234 | | |
235 | | |
236 | 52.1k | #define RGB_READ_LOOP(read_op, alpha_set_op) { \ |
237 | 1.72M | for (col = cinfo->image_width; col > 0; col--) { \ |
238 | 1.67M | ptr[rindex] = read_op; \ |
239 | 1.67M | ptr[gindex] = read_op; \ |
240 | 1.67M | ptr[bindex] = read_op; \ |
241 | 1.67M | alpha_set_op \ |
242 | 1.67M | ptr += ps; \ |
243 | 1.67M | } \ |
244 | 52.1k | } |
245 | | |
246 | | METHODDEF(JDIMENSION) |
247 | | get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
248 | | /* This version is for reading text-format PPM files with any maxval */ |
249 | 8.29k | { |
250 | 8.29k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
251 | 8.29k | FILE *infile = source->pub.input_file; |
252 | 8.29k | register JSAMPROW ptr; |
253 | 8.29k | register JSAMPLE *rescale = source->rescale; |
254 | 8.29k | JDIMENSION col; |
255 | 8.29k | unsigned int maxval = source->maxval; |
256 | 8.29k | register int rindex = rgb_red[cinfo->in_color_space]; |
257 | 8.29k | register int gindex = rgb_green[cinfo->in_color_space]; |
258 | 8.29k | register int bindex = rgb_blue[cinfo->in_color_space]; |
259 | 8.29k | register int aindex = alpha_index[cinfo->in_color_space]; |
260 | 8.29k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
261 | | |
262 | 8.29k | ptr = source->pub.buffer[0]; |
263 | 8.29k | if (maxval == MAXJSAMPLE) { |
264 | 4.40k | if (aindex >= 0) |
265 | 881 | RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
266 | 4.40k | ptr[aindex] = MAXJSAMPLE;) |
267 | 3.52k | else |
268 | 3.52k | RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
269 | 4.40k | } else { |
270 | 3.89k | if (aindex >= 0) |
271 | 778 | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
272 | 3.89k | ptr[aindex] = MAXJSAMPLE;) |
273 | 3.11k | else |
274 | 3.11k | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
275 | 3.89k | } |
276 | 8.29k | return 1; |
277 | 8.29k | } |
278 | | |
279 | | |
280 | | METHODDEF(JDIMENSION) |
281 | | get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
282 | | /* This version is for reading text-format PPM files with any maxval and |
283 | | converting to CMYK */ |
284 | 1.65k | { |
285 | 1.65k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
286 | 1.65k | FILE *infile = source->pub.input_file; |
287 | 1.65k | register JSAMPROW ptr; |
288 | 1.65k | register JSAMPLE *rescale = source->rescale; |
289 | 1.65k | JDIMENSION col; |
290 | 1.65k | unsigned int maxval = source->maxval; |
291 | | |
292 | 1.65k | ptr = source->pub.buffer[0]; |
293 | 1.65k | if (maxval == MAXJSAMPLE) { |
294 | 2.53k | for (col = cinfo->image_width; col > 0; col--) { |
295 | 1.64k | JSAMPLE r = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
296 | 1.64k | JSAMPLE g = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
297 | 1.64k | JSAMPLE b = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
298 | 1.64k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
299 | 1.64k | ptr += 4; |
300 | 1.64k | } |
301 | 881 | } else { |
302 | 2.39k | for (col = cinfo->image_width; col > 0; col--) { |
303 | 1.61k | JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
304 | 1.61k | JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
305 | 1.61k | JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
306 | 1.61k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
307 | 1.61k | ptr += 4; |
308 | 1.61k | } |
309 | 778 | } |
310 | 1.65k | return 1; |
311 | 1.65k | } |
312 | | |
313 | | |
314 | | METHODDEF(JDIMENSION) |
315 | | get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
316 | | /* This version is for reading raw-byte-format PGM files with any maxval */ |
317 | 3.14M | { |
318 | 3.14M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
319 | 3.14M | register JSAMPROW ptr; |
320 | 3.14M | register U_CHAR *bufferptr; |
321 | 3.14M | register JSAMPLE *rescale = source->rescale; |
322 | 3.14M | JDIMENSION col; |
323 | | |
324 | 3.14M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
325 | 69 | ERREXIT(cinfo, JERR_INPUT_EOF); |
326 | 3.14M | ptr = source->pub.buffer[0]; |
327 | 3.14M | bufferptr = source->iobuffer; |
328 | 8.36M | for (col = cinfo->image_width; col > 0; col--) { |
329 | 5.21M | *ptr++ = rescale[UCH(*bufferptr++)]; |
330 | 5.21M | } |
331 | 3.14M | return 1; |
332 | 3.14M | } |
333 | | |
334 | | |
335 | | METHODDEF(JDIMENSION) |
336 | | get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
337 | | /* This version is for reading raw-byte-format PGM files with any maxval |
338 | | and converting to extended RGB */ |
339 | 16.0M | { |
340 | 16.0M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
341 | 16.0M | register JSAMPROW ptr; |
342 | 16.0M | register U_CHAR *bufferptr; |
343 | 16.0M | register JSAMPLE *rescale = source->rescale; |
344 | 16.0M | JDIMENSION col; |
345 | 16.0M | unsigned int maxval = source->maxval; |
346 | 16.0M | register int rindex = rgb_red[cinfo->in_color_space]; |
347 | 16.0M | register int gindex = rgb_green[cinfo->in_color_space]; |
348 | 16.0M | register int bindex = rgb_blue[cinfo->in_color_space]; |
349 | 16.0M | register int aindex = alpha_index[cinfo->in_color_space]; |
350 | 16.0M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
351 | | |
352 | 16.0M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
353 | 545 | ERREXIT(cinfo, JERR_INPUT_EOF); |
354 | 16.0M | ptr = source->pub.buffer[0]; |
355 | 16.0M | bufferptr = source->iobuffer; |
356 | 16.0M | if (maxval == MAXJSAMPLE) { |
357 | 318k | if (aindex >= 0) |
358 | 63.7k | GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;) |
359 | 254k | else |
360 | 254k | GRAY_RGB_READ_LOOP(*bufferptr++, {}) |
361 | 15.7M | } else { |
362 | 15.7M | if (aindex >= 0) |
363 | 3.14M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;) |
364 | 12.5M | else |
365 | 12.5M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
366 | 15.7M | } |
367 | 16.0M | return 1; |
368 | 16.0M | } |
369 | | |
370 | | |
371 | | METHODDEF(JDIMENSION) |
372 | | get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
373 | | /* This version is for reading raw-byte-format PGM files with any maxval |
374 | | and converting to CMYK */ |
375 | 3.20M | { |
376 | 3.20M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
377 | 3.20M | register JSAMPROW ptr; |
378 | 3.20M | register U_CHAR *bufferptr; |
379 | 3.20M | register JSAMPLE *rescale = source->rescale; |
380 | 3.20M | JDIMENSION col; |
381 | 3.20M | unsigned int maxval = source->maxval; |
382 | | |
383 | 3.20M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
384 | 109 | ERREXIT(cinfo, JERR_INPUT_EOF); |
385 | 3.20M | ptr = source->pub.buffer[0]; |
386 | 3.20M | bufferptr = source->iobuffer; |
387 | 3.20M | if (maxval == MAXJSAMPLE) { |
388 | 535k | for (col = cinfo->image_width; col > 0; col--) { |
389 | 471k | JSAMPLE gray = *bufferptr++; |
390 | 471k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
391 | 471k | ptr += 4; |
392 | 471k | } |
393 | 3.14M | } else { |
394 | 8.36M | for (col = cinfo->image_width; col > 0; col--) { |
395 | 5.21M | JSAMPLE gray = rescale[UCH(*bufferptr++)]; |
396 | 5.21M | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
397 | 5.21M | ptr += 4; |
398 | 5.21M | } |
399 | 3.14M | } |
400 | 3.20M | return 1; |
401 | 3.20M | } |
402 | | |
403 | | |
404 | | METHODDEF(JDIMENSION) |
405 | | get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
406 | | /* This version is for reading raw-byte-format PPM files with any maxval */ |
407 | 43.8k | { |
408 | 43.8k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
409 | 43.8k | register JSAMPROW ptr; |
410 | 43.8k | register U_CHAR *bufferptr; |
411 | 43.8k | register JSAMPLE *rescale = source->rescale; |
412 | 43.8k | JDIMENSION col; |
413 | 43.8k | unsigned int maxval = source->maxval; |
414 | 43.8k | register int rindex = rgb_red[cinfo->in_color_space]; |
415 | 43.8k | register int gindex = rgb_green[cinfo->in_color_space]; |
416 | 43.8k | register int bindex = rgb_blue[cinfo->in_color_space]; |
417 | 43.8k | register int aindex = alpha_index[cinfo->in_color_space]; |
418 | 43.8k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
419 | | |
420 | 43.8k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
421 | 756 | ERREXIT(cinfo, JERR_INPUT_EOF); |
422 | 43.8k | ptr = source->pub.buffer[0]; |
423 | 43.8k | bufferptr = source->iobuffer; |
424 | 43.8k | if (maxval == MAXJSAMPLE) { |
425 | 23.9k | if (aindex >= 0) |
426 | 5.99k | RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;) |
427 | 17.9k | else |
428 | 17.9k | RGB_READ_LOOP(*bufferptr++, {}) |
429 | 23.9k | } else { |
430 | 19.9k | if (aindex >= 0) |
431 | 3.83k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;) |
432 | 16.0k | else |
433 | 16.0k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
434 | 19.9k | } |
435 | 43.8k | return 1; |
436 | 43.8k | } |
437 | | |
438 | | |
439 | | METHODDEF(JDIMENSION) |
440 | | get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
441 | | /* This version is for reading raw-byte-format PPM files with any maxval and |
442 | | converting to CMYK */ |
443 | 9.98k | { |
444 | 9.98k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
445 | 9.98k | register JSAMPROW ptr; |
446 | 9.98k | register U_CHAR *bufferptr; |
447 | 9.98k | register JSAMPLE *rescale = source->rescale; |
448 | 9.98k | JDIMENSION col; |
449 | 9.98k | unsigned int maxval = source->maxval; |
450 | | |
451 | 9.98k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
452 | 162 | ERREXIT(cinfo, JERR_INPUT_EOF); |
453 | 9.98k | ptr = source->pub.buffer[0]; |
454 | 9.98k | bufferptr = source->iobuffer; |
455 | 9.98k | if (maxval == MAXJSAMPLE) { |
456 | 193k | for (col = cinfo->image_width; col > 0; col--) { |
457 | 187k | JSAMPLE r = *bufferptr++; |
458 | 187k | JSAMPLE g = *bufferptr++; |
459 | 187k | JSAMPLE b = *bufferptr++; |
460 | 187k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
461 | 187k | ptr += 4; |
462 | 187k | } |
463 | 5.99k | } else { |
464 | 184k | for (col = cinfo->image_width; col > 0; col--) { |
465 | 180k | JSAMPLE r = rescale[UCH(*bufferptr++)]; |
466 | 180k | JSAMPLE g = rescale[UCH(*bufferptr++)]; |
467 | 180k | JSAMPLE b = rescale[UCH(*bufferptr++)]; |
468 | 180k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
469 | 180k | ptr += 4; |
470 | 180k | } |
471 | 3.99k | } |
472 | 9.98k | return 1; |
473 | 9.98k | } |
474 | | |
475 | | |
476 | | METHODDEF(JDIMENSION) |
477 | | get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
478 | | /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE. |
479 | | * In this case we just read right into the JSAMPLE buffer! |
480 | | * Note that same code works for PPM and PGM files. |
481 | | */ |
482 | 69.8k | { |
483 | 69.8k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
484 | | |
485 | 69.8k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
486 | 94 | ERREXIT(cinfo, JERR_INPUT_EOF); |
487 | 69.8k | return 1; |
488 | 69.8k | } |
489 | | |
490 | | |
491 | | METHODDEF(JDIMENSION) |
492 | | get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
493 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
494 | 566 | { |
495 | 566 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
496 | 566 | register JSAMPROW ptr; |
497 | 566 | register U_CHAR *bufferptr; |
498 | 566 | register JSAMPLE *rescale = source->rescale; |
499 | 566 | JDIMENSION col; |
500 | 566 | unsigned int maxval = source->maxval; |
501 | | |
502 | 566 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
503 | 36 | ERREXIT(cinfo, JERR_INPUT_EOF); |
504 | 566 | ptr = source->pub.buffer[0]; |
505 | 566 | bufferptr = source->iobuffer; |
506 | 66.9k | for (col = cinfo->image_width; col > 0; col--) { |
507 | 66.4k | register unsigned int temp; |
508 | 66.4k | temp = UCH(*bufferptr++) << 8; |
509 | 66.4k | temp |= UCH(*bufferptr++); |
510 | 66.4k | if (temp > maxval) |
511 | 15 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
512 | 66.4k | *ptr++ = rescale[temp]; |
513 | 66.4k | } |
514 | 566 | return 1; |
515 | 566 | } |
516 | | |
517 | | |
518 | | METHODDEF(JDIMENSION) |
519 | | get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
520 | | /* This version is for reading raw-word-format PPM files with any maxval */ |
521 | 7.11k | { |
522 | 7.11k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
523 | 7.11k | register JSAMPROW ptr; |
524 | 7.11k | register U_CHAR *bufferptr; |
525 | 7.11k | register JSAMPLE *rescale = source->rescale; |
526 | 7.11k | JDIMENSION col; |
527 | 7.11k | unsigned int maxval = source->maxval; |
528 | 7.11k | register int rindex = rgb_red[cinfo->in_color_space]; |
529 | 7.11k | register int gindex = rgb_green[cinfo->in_color_space]; |
530 | 7.11k | register int bindex = rgb_blue[cinfo->in_color_space]; |
531 | 7.11k | register int aindex = alpha_index[cinfo->in_color_space]; |
532 | 7.11k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
533 | | |
534 | 7.11k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
535 | 250 | ERREXIT(cinfo, JERR_INPUT_EOF); |
536 | 7.11k | ptr = source->pub.buffer[0]; |
537 | 7.11k | bufferptr = source->iobuffer; |
538 | 63.3k | for (col = cinfo->image_width; col > 0; col--) { |
539 | 56.2k | register unsigned int temp; |
540 | 56.2k | temp = UCH(*bufferptr++) << 8; |
541 | 56.2k | temp |= UCH(*bufferptr++); |
542 | 56.2k | if (temp > maxval) |
543 | 115 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
544 | 56.2k | ptr[rindex] = rescale[temp]; |
545 | 56.2k | temp = UCH(*bufferptr++) << 8; |
546 | 56.2k | temp |= UCH(*bufferptr++); |
547 | 56.2k | if (temp > maxval) |
548 | 105 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
549 | 56.2k | ptr[gindex] = rescale[temp]; |
550 | 56.2k | temp = UCH(*bufferptr++) << 8; |
551 | 56.2k | temp |= UCH(*bufferptr++); |
552 | 56.2k | if (temp > maxval) |
553 | 100 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
554 | 56.2k | ptr[bindex] = rescale[temp]; |
555 | 56.2k | if (aindex >= 0) |
556 | 11.1k | ptr[aindex] = MAXJSAMPLE; |
557 | 56.2k | ptr += ps; |
558 | 56.2k | } |
559 | 7.11k | return 1; |
560 | 7.11k | } |
561 | | |
562 | | |
563 | | /* |
564 | | * Read the file header; return image size and component count. |
565 | | */ |
566 | | |
567 | | METHODDEF(void) |
568 | | start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
569 | 13.5k | { |
570 | 13.5k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
571 | 13.5k | int c; |
572 | 13.5k | unsigned int w, h, maxval; |
573 | 13.5k | boolean need_iobuffer, use_raw_buffer, need_rescale; |
574 | | |
575 | 13.5k | if (getc(source->pub.input_file) != 'P') |
576 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
577 | | |
578 | 13.5k | c = getc(source->pub.input_file); /* subformat discriminator character */ |
579 | | |
580 | | /* detect unsupported variants (ie, PBM) before trying to read header */ |
581 | 13.5k | switch (c) { |
582 | 1.19k | case '2': /* it's a text-format PGM file */ |
583 | 2.73k | case '3': /* it's a text-format PPM file */ |
584 | 9.98k | case '5': /* it's a raw-format PGM file */ |
585 | 13.5k | case '6': /* it's a raw-format PPM file */ |
586 | 13.5k | break; |
587 | 14 | default: |
588 | 14 | ERREXIT(cinfo, JERR_PPM_NOT); |
589 | 14 | break; |
590 | 13.5k | } |
591 | | |
592 | | /* fetch the remaining header info */ |
593 | 13.5k | w = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
594 | 13.5k | h = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
595 | 13.5k | maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
596 | | |
597 | 13.5k | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
598 | 28 | ERREXIT(cinfo, JERR_PPM_NOT); |
599 | 13.5k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
600 | 13.5k | if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels) |
601 | 210 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
602 | 13.5k | #endif |
603 | | |
604 | 13.5k | cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ |
605 | 13.5k | cinfo->image_width = (JDIMENSION)w; |
606 | 13.5k | cinfo->image_height = (JDIMENSION)h; |
607 | 13.5k | source->maxval = maxval; |
608 | | |
609 | | /* initialize flags to most common settings */ |
610 | 13.5k | need_iobuffer = TRUE; /* do we need an I/O buffer? */ |
611 | 13.5k | use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ |
612 | 13.5k | need_rescale = TRUE; /* do we need a rescale array? */ |
613 | | |
614 | 13.5k | switch (c) { |
615 | 847 | case '2': /* it's a text-format PGM file */ |
616 | 847 | if (cinfo->in_color_space == JCS_UNKNOWN || |
617 | 847 | cinfo->in_color_space == JCS_RGB) |
618 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
619 | 847 | TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h); |
620 | 847 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
621 | 121 | source->pub.get_pixel_rows = get_text_gray_row; |
622 | 726 | else if (IsExtRGB(cinfo->in_color_space)) |
623 | 605 | source->pub.get_pixel_rows = get_text_gray_rgb_row; |
624 | 121 | else if (cinfo->in_color_space == JCS_CMYK) |
625 | 121 | source->pub.get_pixel_rows = get_text_gray_cmyk_row; |
626 | 0 | else |
627 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
628 | 847 | need_iobuffer = FALSE; |
629 | 847 | break; |
630 | | |
631 | 1.19k | case '3': /* it's a text-format PPM file */ |
632 | 1.19k | if (cinfo->in_color_space == JCS_UNKNOWN) |
633 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
634 | 1.19k | TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h); |
635 | 1.19k | if (IsExtRGB(cinfo->in_color_space)) |
636 | 850 | source->pub.get_pixel_rows = get_text_rgb_row; |
637 | 340 | else if (cinfo->in_color_space == JCS_CMYK) |
638 | 170 | source->pub.get_pixel_rows = get_text_rgb_cmyk_row; |
639 | 170 | else |
640 | 170 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
641 | 1.19k | need_iobuffer = FALSE; |
642 | 1.19k | break; |
643 | | |
644 | 6.88k | case '5': /* it's a raw-format PGM file */ |
645 | 6.88k | if (cinfo->in_color_space == JCS_UNKNOWN || |
646 | 6.88k | cinfo->in_color_space == JCS_RGB) |
647 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
648 | 6.88k | TRACEMS2(cinfo, 1, JTRC_PGM, w, h); |
649 | 6.88k | if (maxval > 255) { |
650 | 553 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
651 | 79 | source->pub.get_pixel_rows = get_word_gray_row; |
652 | 474 | else |
653 | 474 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
654 | 6.33k | } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) && |
655 | 6.33k | cinfo->in_color_space == JCS_GRAYSCALE) { |
656 | 96 | source->pub.get_pixel_rows = get_raw_row; |
657 | 96 | use_raw_buffer = TRUE; |
658 | 96 | need_rescale = FALSE; |
659 | 6.23k | } else { |
660 | 6.23k | if (cinfo->in_color_space == JCS_GRAYSCALE) |
661 | 809 | source->pub.get_pixel_rows = get_scaled_gray_row; |
662 | 5.43k | else if (IsExtRGB(cinfo->in_color_space)) |
663 | 4.52k | source->pub.get_pixel_rows = get_gray_rgb_row; |
664 | 905 | else if (cinfo->in_color_space == JCS_CMYK) |
665 | 905 | source->pub.get_pixel_rows = get_gray_cmyk_row; |
666 | 0 | else |
667 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
668 | 6.23k | } |
669 | 6.88k | break; |
670 | | |
671 | 3.11k | case '6': /* it's a raw-format PPM file */ |
672 | 3.11k | if (cinfo->in_color_space == JCS_UNKNOWN) |
673 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
674 | 3.11k | TRACEMS2(cinfo, 1, JTRC_PPM, w, h); |
675 | 3.11k | if (maxval > 255) { |
676 | 861 | if (IsExtRGB(cinfo->in_color_space)) |
677 | 615 | source->pub.get_pixel_rows = get_word_rgb_row; |
678 | 246 | else |
679 | 246 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
680 | 2.25k | } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) && |
681 | 2.25k | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
682 | 2.25k | (cinfo->in_color_space == JCS_EXT_RGB || |
683 | 679 | cinfo->in_color_space == JCS_RGB)) { |
684 | | #else |
685 | | cinfo->in_color_space == JCS_EXT_RGB) { |
686 | | #endif |
687 | 97 | source->pub.get_pixel_rows = get_raw_row; |
688 | 97 | use_raw_buffer = TRUE; |
689 | 97 | need_rescale = FALSE; |
690 | 2.15k | } else { |
691 | 2.15k | if (IsExtRGB(cinfo->in_color_space)) |
692 | 1.51k | source->pub.get_pixel_rows = get_rgb_row; |
693 | 644 | else if (cinfo->in_color_space == JCS_CMYK) |
694 | 322 | source->pub.get_pixel_rows = get_rgb_cmyk_row; |
695 | 322 | else |
696 | 322 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
697 | 2.15k | } |
698 | 3.11k | break; |
699 | 13.5k | } |
700 | | |
701 | 10.8k | if (IsExtRGB(cinfo->in_color_space)) |
702 | 8.20k | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
703 | 2.62k | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
704 | 1.10k | cinfo->input_components = 1; |
705 | 1.51k | else if (cinfo->in_color_space == JCS_CMYK) |
706 | 1.51k | cinfo->input_components = 4; |
707 | | |
708 | | /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ |
709 | 10.8k | if (need_iobuffer) { |
710 | 8.96k | if (c == '6') |
711 | 2.54k | source->buffer_width = (size_t)w * 3 * |
712 | 2.54k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
713 | 6.41k | else |
714 | 6.41k | source->buffer_width = (size_t)w * |
715 | 6.41k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
716 | 8.96k | source->iobuffer = (U_CHAR *) |
717 | 8.96k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
718 | 8.96k | source->buffer_width); |
719 | 8.96k | } |
720 | | |
721 | | /* Create compressor input buffer. */ |
722 | 10.8k | if (use_raw_buffer) { |
723 | | /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ |
724 | | /* Synthesize a JSAMPARRAY pointer structure */ |
725 | 193 | source->pixrow = (JSAMPROW)source->iobuffer; |
726 | 193 | source->pub.buffer = &source->pixrow; |
727 | 193 | source->pub.buffer_height = 1; |
728 | 10.6k | } else { |
729 | | /* Need to translate anyway, so make a separate sample buffer. */ |
730 | 10.6k | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
731 | 10.6k | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
732 | 10.6k | (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1); |
733 | 10.6k | source->pub.buffer_height = 1; |
734 | 10.6k | } |
735 | | |
736 | | /* Compute the rescaling array if required. */ |
737 | 10.8k | if (need_rescale) { |
738 | 10.6k | long val, half_maxval; |
739 | | |
740 | | /* On 16-bit-int machines we have to be careful of maxval = 65535 */ |
741 | 10.6k | source->rescale = (JSAMPLE *) |
742 | 10.6k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
743 | 10.6k | (size_t)(((long)MAX(maxval, 255) + 1L) * |
744 | 10.6k | sizeof(JSAMPLE))); |
745 | 10.6k | MEMZERO(source->rescale, (size_t)(((long)MAX(maxval, 255) + 1L) * |
746 | 10.6k | sizeof(JSAMPLE))); |
747 | 10.6k | half_maxval = maxval / 2; |
748 | 13.5M | for (val = 0; val <= (long)maxval; val++) { |
749 | | /* The multiplication here must be done in 32 bits to avoid overflow */ |
750 | 13.5M | source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) / |
751 | 13.5M | maxval); |
752 | 13.5M | } |
753 | 10.6k | } |
754 | 10.8k | } |
755 | | |
756 | | |
757 | | /* |
758 | | * Finish up at the end of the file. |
759 | | */ |
760 | | |
761 | | METHODDEF(void) |
762 | | finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
763 | 6.67k | { |
764 | | /* no work */ |
765 | 6.67k | } |
766 | | |
767 | | |
768 | | /* |
769 | | * The module selection routine for PPM format input. |
770 | | */ |
771 | | |
772 | | GLOBAL(cjpeg_source_ptr) |
773 | | jinit_read_ppm(j_compress_ptr cinfo) |
774 | 13.5k | { |
775 | 13.5k | ppm_source_ptr source; |
776 | | |
777 | | /* Create module interface object */ |
778 | 13.5k | source = (ppm_source_ptr) |
779 | 13.5k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
780 | 13.5k | sizeof(ppm_source_struct)); |
781 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
782 | 13.5k | source->pub.start_input = start_input_ppm; |
783 | 13.5k | source->pub.finish_input = finish_input_ppm; |
784 | 13.5k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
785 | 13.5k | source->pub.max_pixels = 0; |
786 | 13.5k | #endif |
787 | | |
788 | 13.5k | return (cjpeg_source_ptr)source; |
789 | 13.5k | } |
790 | | |
791 | | #endif /* PPM_SUPPORTED */ |