/src/libjpeg-turbo.2.1.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 | | typedef unsigned char U_CHAR; |
47 | 12.5M | #define UCH(x) ((int)(x)) |
48 | | |
49 | | |
50 | | #define ReadOK(file, buffer, len) \ |
51 | 20.3M | (fread(buffer, 1, len, file) == ((size_t)(len))) |
52 | | |
53 | | static int alpha_index[JPEG_NUMCS] = { |
54 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1 |
55 | | }; |
56 | | |
57 | | |
58 | | /* Private version of data source object */ |
59 | | |
60 | | typedef struct { |
61 | | struct cjpeg_source_struct pub; /* public fields */ |
62 | | |
63 | | /* 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 */ |
68 | | unsigned int maxval; |
69 | | } ppm_source_struct; |
70 | | |
71 | | typedef ppm_source_struct *ppm_source_ptr; |
72 | | |
73 | | |
74 | | LOCAL(int) |
75 | | pbm_getc(FILE *infile) |
76 | | /* Read next char, skipping over any comments */ |
77 | | /* A comment/newline sequence is returned as a newline */ |
78 | 290k | { |
79 | 290k | register int ch; |
80 | | |
81 | 290k | ch = getc(infile); |
82 | 290k | if (ch == '#') { |
83 | 23.8k | do { |
84 | 23.8k | ch = getc(infile); |
85 | 23.8k | } while (ch != '\n' && ch != EOF); |
86 | 4.22k | } |
87 | 290k | return ch; |
88 | 290k | } |
89 | | |
90 | | |
91 | | LOCAL(unsigned int) |
92 | | read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval) |
93 | | /* 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 | 122k | { |
98 | 122k | register int ch; |
99 | 122k | register unsigned int val; |
100 | | |
101 | | /* Skip any leading whitespace */ |
102 | 135k | do { |
103 | 135k | ch = pbm_getc(infile); |
104 | 135k | if (ch == EOF) |
105 | 2.66k | ERREXIT(cinfo, JERR_INPUT_EOF); |
106 | 135k | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
107 | | |
108 | 122k | if (ch < '0' || ch > '9') |
109 | 224 | ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
110 | | |
111 | 122k | val = ch - '0'; |
112 | 158k | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
113 | 35.2k | val *= 10; |
114 | 35.2k | val += ch - '0'; |
115 | 35.2k | if (val > maxval) |
116 | 153 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
117 | 35.2k | } |
118 | | |
119 | 122k | return val; |
120 | 122k | } |
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 | | |
134 | | METHODDEF(JDIMENSION) |
135 | | get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
136 | | /* This version is for reading text-format PGM files with any maxval */ |
137 | 1.20k | { |
138 | 1.20k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
139 | 1.20k | FILE *infile = source->pub.input_file; |
140 | 1.20k | register JSAMPROW ptr; |
141 | 1.20k | register JSAMPLE *rescale = source->rescale; |
142 | 1.20k | JDIMENSION col; |
143 | 1.20k | unsigned int maxval = source->maxval; |
144 | | |
145 | 1.20k | ptr = source->pub.buffer[0]; |
146 | 3.68k | for (col = cinfo->image_width; col > 0; col--) { |
147 | 2.48k | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
148 | 2.48k | } |
149 | 1.20k | return 1; |
150 | 1.20k | } |
151 | | |
152 | | |
153 | 14.3M | #define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \ |
154 | 46.0M | for (col = cinfo->image_width; col > 0; col--) { \ |
155 | 31.6M | ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \ |
156 | 31.6M | alpha_set_op \ |
157 | 31.6M | ptr += ps; \ |
158 | 31.6M | } \ |
159 | 14.3M | } |
160 | | |
161 | | METHODDEF(JDIMENSION) |
162 | | get_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 | 6.00k | { |
166 | 6.00k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
167 | 6.00k | FILE *infile = source->pub.input_file; |
168 | 6.00k | register JSAMPROW ptr; |
169 | 6.00k | register JSAMPLE *rescale = source->rescale; |
170 | 6.00k | JDIMENSION col; |
171 | 6.00k | unsigned int maxval = source->maxval; |
172 | 6.00k | register int rindex = rgb_red[cinfo->in_color_space]; |
173 | 6.00k | register int gindex = rgb_green[cinfo->in_color_space]; |
174 | 6.00k | register int bindex = rgb_blue[cinfo->in_color_space]; |
175 | 6.00k | register int aindex = alpha_index[cinfo->in_color_space]; |
176 | 6.00k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
177 | | |
178 | 6.00k | ptr = source->pub.buffer[0]; |
179 | 6.00k | if (maxval == MAXJSAMPLE) { |
180 | 2.96k | if (aindex >= 0) |
181 | 592 | GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
182 | 2.96k | ptr[aindex] = MAXJSAMPLE;) |
183 | 2.36k | else |
184 | 2.36k | GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
185 | 3.04k | } else { |
186 | 3.04k | if (aindex >= 0) |
187 | 609 | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
188 | 3.04k | ptr[aindex] = MAXJSAMPLE;) |
189 | 2.43k | else |
190 | 2.43k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
191 | 3.04k | } |
192 | 6.00k | return 1; |
193 | 6.00k | } |
194 | | |
195 | | |
196 | | METHODDEF(JDIMENSION) |
197 | | get_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 | 1.20k | { |
201 | 1.20k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
202 | 1.20k | FILE *infile = source->pub.input_file; |
203 | 1.20k | register JSAMPROW ptr; |
204 | 1.20k | register JSAMPLE *rescale = source->rescale; |
205 | 1.20k | JDIMENSION col; |
206 | 1.20k | unsigned int maxval = source->maxval; |
207 | | |
208 | 1.20k | ptr = source->pub.buffer[0]; |
209 | 1.20k | if (maxval == MAXJSAMPLE) { |
210 | 1.66k | for (col = cinfo->image_width; col > 0; col--) { |
211 | 1.07k | JSAMPLE gray = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
212 | 1.07k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
213 | 1.07k | ptr += 4; |
214 | 1.07k | } |
215 | 609 | } else { |
216 | 2.02k | for (col = cinfo->image_width; col > 0; col--) { |
217 | 1.41k | JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
218 | 1.41k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
219 | 1.41k | ptr += 4; |
220 | 1.41k | } |
221 | 609 | } |
222 | 1.20k | return 1; |
223 | 1.20k | } |
224 | | |
225 | | |
226 | 135k | #define RGB_READ_LOOP(read_op, alpha_set_op) { \ |
227 | 1.94M | for (col = cinfo->image_width; col > 0; col--) { \ |
228 | 1.81M | ptr[rindex] = read_op; \ |
229 | 1.81M | ptr[gindex] = read_op; \ |
230 | 1.81M | ptr[bindex] = read_op; \ |
231 | 1.81M | alpha_set_op \ |
232 | 1.81M | ptr += ps; \ |
233 | 1.81M | } \ |
234 | 135k | } |
235 | | |
236 | | METHODDEF(JDIMENSION) |
237 | | get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
238 | | /* This version is for reading text-format PPM files with any maxval */ |
239 | 10.6k | { |
240 | 10.6k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
241 | 10.6k | FILE *infile = source->pub.input_file; |
242 | 10.6k | register JSAMPROW ptr; |
243 | 10.6k | register JSAMPLE *rescale = source->rescale; |
244 | 10.6k | JDIMENSION col; |
245 | 10.6k | unsigned int maxval = source->maxval; |
246 | 10.6k | register int rindex = rgb_red[cinfo->in_color_space]; |
247 | 10.6k | register int gindex = rgb_green[cinfo->in_color_space]; |
248 | 10.6k | register int bindex = rgb_blue[cinfo->in_color_space]; |
249 | 10.6k | register int aindex = alpha_index[cinfo->in_color_space]; |
250 | 10.6k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
251 | | |
252 | 10.6k | ptr = source->pub.buffer[0]; |
253 | 10.6k | if (maxval == MAXJSAMPLE) { |
254 | 3.63k | if (aindex >= 0) |
255 | 726 | RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
256 | 3.63k | ptr[aindex] = MAXJSAMPLE;) |
257 | 2.90k | else |
258 | 2.90k | RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
259 | 7.02k | } else { |
260 | 7.02k | if (aindex >= 0) |
261 | 1.40k | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
262 | 7.02k | ptr[aindex] = MAXJSAMPLE;) |
263 | 5.61k | else |
264 | 5.61k | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
265 | 7.02k | } |
266 | 10.6k | return 1; |
267 | 10.6k | } |
268 | | |
269 | | |
270 | | METHODDEF(JDIMENSION) |
271 | | get_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 | 2.13k | { |
275 | 2.13k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
276 | 2.13k | FILE *infile = source->pub.input_file; |
277 | 2.13k | register JSAMPROW ptr; |
278 | 2.13k | register JSAMPLE *rescale = source->rescale; |
279 | 2.13k | JDIMENSION col; |
280 | 2.13k | unsigned int maxval = source->maxval; |
281 | | |
282 | 2.13k | ptr = source->pub.buffer[0]; |
283 | 2.13k | if (maxval == MAXJSAMPLE) { |
284 | 2.52k | for (col = cinfo->image_width; col > 0; col--) { |
285 | 1.79k | JSAMPLE r = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
286 | 1.79k | JSAMPLE g = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
287 | 1.79k | JSAMPLE b = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
288 | 1.79k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
289 | 1.79k | ptr += 4; |
290 | 1.79k | } |
291 | 1.40k | } else { |
292 | 3.49k | for (col = cinfo->image_width; col > 0; col--) { |
293 | 2.09k | JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
294 | 2.09k | JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
295 | 2.09k | JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
296 | 2.09k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
297 | 2.09k | ptr += 4; |
298 | 2.09k | } |
299 | 1.40k | } |
300 | 2.13k | return 1; |
301 | 2.13k | } |
302 | | |
303 | | |
304 | | METHODDEF(JDIMENSION) |
305 | | get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
306 | | /* This version is for reading raw-byte-format PGM files with any maxval */ |
307 | 2.80M | { |
308 | 2.80M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
309 | 2.80M | register JSAMPROW ptr; |
310 | 2.80M | register U_CHAR *bufferptr; |
311 | 2.80M | register JSAMPLE *rescale = source->rescale; |
312 | 2.80M | JDIMENSION col; |
313 | | |
314 | 2.80M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
315 | 61 | ERREXIT(cinfo, JERR_INPUT_EOF); |
316 | 2.80M | ptr = source->pub.buffer[0]; |
317 | 2.80M | bufferptr = source->iobuffer; |
318 | 8.42M | for (col = cinfo->image_width; col > 0; col--) { |
319 | 5.62M | *ptr++ = rescale[UCH(*bufferptr++)]; |
320 | 5.62M | } |
321 | 2.80M | return 1; |
322 | 2.80M | } |
323 | | |
324 | | |
325 | | METHODDEF(JDIMENSION) |
326 | | get_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 | 14.3M | { |
330 | 14.3M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
331 | 14.3M | register JSAMPROW ptr; |
332 | 14.3M | register U_CHAR *bufferptr; |
333 | 14.3M | register JSAMPLE *rescale = source->rescale; |
334 | 14.3M | JDIMENSION col; |
335 | 14.3M | unsigned int maxval = source->maxval; |
336 | 14.3M | register int rindex = rgb_red[cinfo->in_color_space]; |
337 | 14.3M | register int gindex = rgb_green[cinfo->in_color_space]; |
338 | 14.3M | register int bindex = rgb_blue[cinfo->in_color_space]; |
339 | 14.3M | register int aindex = alpha_index[cinfo->in_color_space]; |
340 | 14.3M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
341 | | |
342 | 14.3M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
343 | 455 | ERREXIT(cinfo, JERR_INPUT_EOF); |
344 | 14.3M | ptr = source->pub.buffer[0]; |
345 | 14.3M | bufferptr = source->iobuffer; |
346 | 14.3M | if (maxval == MAXJSAMPLE) { |
347 | 343k | if (aindex >= 0) |
348 | 68.7k | GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;) |
349 | 275k | else |
350 | 275k | GRAY_RGB_READ_LOOP(*bufferptr++, {}) |
351 | 14.0M | } else { |
352 | 14.0M | if (aindex >= 0) |
353 | 2.80M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;) |
354 | 11.2M | else |
355 | 11.2M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
356 | 14.0M | } |
357 | 14.3M | return 1; |
358 | 14.3M | } |
359 | | |
360 | | |
361 | | METHODDEF(JDIMENSION) |
362 | | get_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 | 2.87M | { |
366 | 2.87M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
367 | 2.87M | register JSAMPROW ptr; |
368 | 2.87M | register U_CHAR *bufferptr; |
369 | 2.87M | register JSAMPLE *rescale = source->rescale; |
370 | 2.87M | JDIMENSION col; |
371 | 2.87M | unsigned int maxval = source->maxval; |
372 | | |
373 | 2.87M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
374 | 91 | ERREXIT(cinfo, JERR_INPUT_EOF); |
375 | 2.87M | ptr = source->pub.buffer[0]; |
376 | 2.87M | bufferptr = source->iobuffer; |
377 | 2.87M | if (maxval == MAXJSAMPLE) { |
378 | 780k | for (col = cinfo->image_width; col > 0; col--) { |
379 | 711k | JSAMPLE gray = *bufferptr++; |
380 | 711k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
381 | 711k | ptr += 4; |
382 | 711k | } |
383 | 2.80M | } else { |
384 | 8.42M | for (col = cinfo->image_width; col > 0; col--) { |
385 | 5.62M | JSAMPLE gray = rescale[UCH(*bufferptr++)]; |
386 | 5.62M | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
387 | 5.62M | ptr += 4; |
388 | 5.62M | } |
389 | 2.80M | } |
390 | 2.87M | return 1; |
391 | 2.87M | } |
392 | | |
393 | | |
394 | | METHODDEF(JDIMENSION) |
395 | | get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
396 | | /* This version is for reading raw-byte-format PPM files with any maxval */ |
397 | 124k | { |
398 | 124k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
399 | 124k | register JSAMPROW ptr; |
400 | 124k | register U_CHAR *bufferptr; |
401 | 124k | register JSAMPLE *rescale = source->rescale; |
402 | 124k | JDIMENSION col; |
403 | 124k | unsigned int maxval = source->maxval; |
404 | 124k | register int rindex = rgb_red[cinfo->in_color_space]; |
405 | 124k | register int gindex = rgb_green[cinfo->in_color_space]; |
406 | 124k | register int bindex = rgb_blue[cinfo->in_color_space]; |
407 | 124k | register int aindex = alpha_index[cinfo->in_color_space]; |
408 | 124k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
409 | | |
410 | 124k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
411 | 808 | ERREXIT(cinfo, JERR_INPUT_EOF); |
412 | 124k | ptr = source->pub.buffer[0]; |
413 | 124k | bufferptr = source->iobuffer; |
414 | 124k | if (maxval == MAXJSAMPLE) { |
415 | 40.9k | if (aindex >= 0) |
416 | 10.2k | RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;) |
417 | 30.6k | else |
418 | 30.6k | RGB_READ_LOOP(*bufferptr++, {}) |
419 | 83.7k | } else { |
420 | 83.7k | if (aindex >= 0) |
421 | 16.5k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;) |
422 | 67.1k | else |
423 | 67.1k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
424 | 83.7k | } |
425 | 124k | return 1; |
426 | 124k | } |
427 | | |
428 | | |
429 | | METHODDEF(JDIMENSION) |
430 | | get_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 | 26.9k | { |
434 | 26.9k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
435 | 26.9k | register JSAMPROW ptr; |
436 | 26.9k | register U_CHAR *bufferptr; |
437 | 26.9k | register JSAMPLE *rescale = source->rescale; |
438 | 26.9k | JDIMENSION col; |
439 | 26.9k | unsigned int maxval = source->maxval; |
440 | | |
441 | 26.9k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
442 | 174 | ERREXIT(cinfo, JERR_INPUT_EOF); |
443 | 26.9k | ptr = source->pub.buffer[0]; |
444 | 26.9k | bufferptr = source->iobuffer; |
445 | 26.9k | if (maxval == MAXJSAMPLE) { |
446 | 119k | for (col = cinfo->image_width; col > 0; col--) { |
447 | 109k | JSAMPLE r = *bufferptr++; |
448 | 109k | JSAMPLE g = *bufferptr++; |
449 | 109k | JSAMPLE b = *bufferptr++; |
450 | 109k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
451 | 109k | ptr += 4; |
452 | 109k | } |
453 | 16.7k | } else { |
454 | 287k | for (col = cinfo->image_width; col > 0; col--) { |
455 | 270k | JSAMPLE r = rescale[UCH(*bufferptr++)]; |
456 | 270k | JSAMPLE g = rescale[UCH(*bufferptr++)]; |
457 | 270k | JSAMPLE b = rescale[UCH(*bufferptr++)]; |
458 | 270k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
459 | 270k | ptr += 4; |
460 | 270k | } |
461 | 16.7k | } |
462 | 26.9k | return 1; |
463 | 26.9k | } |
464 | | |
465 | | |
466 | | METHODDEF(JDIMENSION) |
467 | | get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
468 | | /* 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 | 79.0k | { |
473 | 79.0k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
474 | | |
475 | 79.0k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
476 | 92 | ERREXIT(cinfo, JERR_INPUT_EOF); |
477 | 79.0k | return 1; |
478 | 79.0k | } |
479 | | |
480 | | |
481 | | METHODDEF(JDIMENSION) |
482 | | get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
483 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
484 | 66.0k | { |
485 | 66.0k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
486 | 66.0k | register JSAMPROW ptr; |
487 | 66.0k | register U_CHAR *bufferptr; |
488 | 66.0k | register JSAMPLE *rescale = source->rescale; |
489 | 66.0k | JDIMENSION col; |
490 | 66.0k | unsigned int maxval = source->maxval; |
491 | | |
492 | 66.0k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
493 | 37 | ERREXIT(cinfo, JERR_INPUT_EOF); |
494 | 66.0k | ptr = source->pub.buffer[0]; |
495 | 66.0k | bufferptr = source->iobuffer; |
496 | 132k | for (col = cinfo->image_width; col > 0; col--) { |
497 | 66.2k | register unsigned int temp; |
498 | 66.2k | temp = UCH(*bufferptr++) << 8; |
499 | 66.2k | temp |= UCH(*bufferptr++); |
500 | 66.2k | if (temp > maxval) |
501 | 14 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
502 | 66.2k | *ptr++ = rescale[temp]; |
503 | 66.2k | } |
504 | 66.0k | return 1; |
505 | 66.0k | } |
506 | | |
507 | | |
508 | | METHODDEF(JDIMENSION) |
509 | | get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
510 | | /* This version is for reading raw-word-format PPM files with any maxval */ |
511 | 6.96k | { |
512 | 6.96k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
513 | 6.96k | register JSAMPROW ptr; |
514 | 6.96k | register U_CHAR *bufferptr; |
515 | 6.96k | register JSAMPLE *rescale = source->rescale; |
516 | 6.96k | JDIMENSION col; |
517 | 6.96k | unsigned int maxval = source->maxval; |
518 | 6.96k | register int rindex = rgb_red[cinfo->in_color_space]; |
519 | 6.96k | register int gindex = rgb_green[cinfo->in_color_space]; |
520 | 6.96k | register int bindex = rgb_blue[cinfo->in_color_space]; |
521 | 6.96k | register int aindex = alpha_index[cinfo->in_color_space]; |
522 | 6.96k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
523 | | |
524 | 6.96k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
525 | 270 | ERREXIT(cinfo, JERR_INPUT_EOF); |
526 | 6.96k | ptr = source->pub.buffer[0]; |
527 | 6.96k | bufferptr = source->iobuffer; |
528 | 61.4k | for (col = cinfo->image_width; col > 0; col--) { |
529 | 54.5k | register unsigned int temp; |
530 | 54.5k | temp = UCH(*bufferptr++) << 8; |
531 | 54.5k | temp |= UCH(*bufferptr++); |
532 | 54.5k | if (temp > maxval) |
533 | 105 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
534 | 54.5k | ptr[rindex] = rescale[temp]; |
535 | 54.5k | temp = UCH(*bufferptr++) << 8; |
536 | 54.5k | temp |= UCH(*bufferptr++); |
537 | 54.5k | if (temp > maxval) |
538 | 105 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
539 | 54.5k | ptr[gindex] = rescale[temp]; |
540 | 54.5k | temp = UCH(*bufferptr++) << 8; |
541 | 54.5k | temp |= UCH(*bufferptr++); |
542 | 54.5k | if (temp > maxval) |
543 | 85 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
544 | 54.5k | ptr[bindex] = rescale[temp]; |
545 | 54.5k | if (aindex >= 0) |
546 | 10.8k | ptr[aindex] = MAXJSAMPLE; |
547 | 54.5k | ptr += ps; |
548 | 54.5k | } |
549 | 6.96k | return 1; |
550 | 6.96k | } |
551 | | |
552 | | |
553 | | /* |
554 | | * Read the file header; return image size and component count. |
555 | | */ |
556 | | |
557 | | METHODDEF(void) |
558 | | start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
559 | 13.1k | { |
560 | 13.1k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
561 | 13.1k | int c; |
562 | 13.1k | unsigned int w, h, maxval; |
563 | 13.1k | boolean need_iobuffer, use_raw_buffer, need_rescale; |
564 | | |
565 | 13.1k | if (getc(source->pub.input_file) != 'P') |
566 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
567 | | |
568 | 13.1k | c = getc(source->pub.input_file); /* subformat discriminator character */ |
569 | | |
570 | | /* detect unsupported variants (ie, PBM) before trying to read header */ |
571 | 13.1k | switch (c) { |
572 | 1.26k | case '2': /* it's a text-format PGM file */ |
573 | 2.84k | case '3': /* it's a text-format PPM file */ |
574 | 9.74k | case '5': /* it's a raw-format PGM file */ |
575 | 13.1k | case '6': /* it's a raw-format PPM file */ |
576 | 13.1k | break; |
577 | 7 | default: |
578 | 7 | ERREXIT(cinfo, JERR_PPM_NOT); |
579 | 7 | break; |
580 | 13.1k | } |
581 | | |
582 | | /* fetch the remaining header info */ |
583 | 13.1k | w = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
584 | 13.1k | h = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
585 | 13.1k | maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
586 | | |
587 | 13.1k | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
588 | 21 | ERREXIT(cinfo, JERR_PPM_NOT); |
589 | 13.1k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
590 | 13.1k | if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels) |
591 | 238 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
592 | 13.1k | #endif |
593 | | |
594 | 13.1k | cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ |
595 | 13.1k | cinfo->image_width = (JDIMENSION)w; |
596 | 13.1k | cinfo->image_height = (JDIMENSION)h; |
597 | 13.1k | source->maxval = maxval; |
598 | | |
599 | | /* initialize flags to most common settings */ |
600 | 13.1k | need_iobuffer = TRUE; /* do we need an I/O buffer? */ |
601 | 13.1k | use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ |
602 | 13.1k | need_rescale = TRUE; /* do we need a rescale array? */ |
603 | | |
604 | 13.1k | switch (c) { |
605 | 868 | case '2': /* it's a text-format PGM file */ |
606 | 868 | if (cinfo->in_color_space == JCS_UNKNOWN || |
607 | 868 | cinfo->in_color_space == JCS_RGB) |
608 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
609 | 868 | TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h); |
610 | 868 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
611 | 124 | source->pub.get_pixel_rows = get_text_gray_row; |
612 | 744 | else if (IsExtRGB(cinfo->in_color_space)) |
613 | 620 | source->pub.get_pixel_rows = get_text_gray_rgb_row; |
614 | 124 | else if (cinfo->in_color_space == JCS_CMYK) |
615 | 124 | source->pub.get_pixel_rows = get_text_gray_cmyk_row; |
616 | 0 | else |
617 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
618 | 868 | need_iobuffer = FALSE; |
619 | 868 | break; |
620 | | |
621 | 1.19k | case '3': /* it's a text-format PPM file */ |
622 | 1.19k | if (cinfo->in_color_space == JCS_UNKNOWN) |
623 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
624 | 1.19k | TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h); |
625 | 1.19k | if (IsExtRGB(cinfo->in_color_space)) |
626 | 855 | source->pub.get_pixel_rows = get_text_rgb_row; |
627 | 342 | else if (cinfo->in_color_space == JCS_CMYK) |
628 | 171 | source->pub.get_pixel_rows = get_text_rgb_cmyk_row; |
629 | 171 | else |
630 | 171 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
631 | 1.19k | need_iobuffer = FALSE; |
632 | 1.19k | break; |
633 | | |
634 | 6.57k | case '5': /* it's a raw-format PGM file */ |
635 | 6.57k | if (cinfo->in_color_space == JCS_UNKNOWN || |
636 | 6.57k | cinfo->in_color_space == JCS_RGB) |
637 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
638 | 6.57k | TRACEMS2(cinfo, 1, JTRC_PGM, w, h); |
639 | 6.57k | if (maxval > 255) { |
640 | 532 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
641 | 76 | source->pub.get_pixel_rows = get_word_gray_row; |
642 | 456 | else |
643 | 456 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
644 | 6.04k | } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) && |
645 | 6.04k | cinfo->in_color_space == JCS_GRAYSCALE) { |
646 | 87 | source->pub.get_pixel_rows = get_raw_row; |
647 | 87 | use_raw_buffer = TRUE; |
648 | 87 | need_rescale = FALSE; |
649 | 5.95k | } else { |
650 | 5.95k | if (cinfo->in_color_space == JCS_GRAYSCALE) |
651 | 776 | source->pub.get_pixel_rows = get_scaled_gray_row; |
652 | 5.17k | else if (IsExtRGB(cinfo->in_color_space)) |
653 | 4.31k | source->pub.get_pixel_rows = get_gray_rgb_row; |
654 | 863 | else if (cinfo->in_color_space == JCS_CMYK) |
655 | 863 | source->pub.get_pixel_rows = get_gray_cmyk_row; |
656 | 0 | else |
657 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
658 | 5.95k | } |
659 | 6.57k | break; |
660 | | |
661 | 3.03k | case '6': /* it's a raw-format PPM file */ |
662 | 3.03k | if (cinfo->in_color_space == JCS_UNKNOWN) |
663 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
664 | 3.03k | TRACEMS2(cinfo, 1, JTRC_PPM, w, h); |
665 | 3.03k | if (maxval > 255) { |
666 | 854 | if (IsExtRGB(cinfo->in_color_space)) |
667 | 610 | source->pub.get_pixel_rows = get_word_rgb_row; |
668 | 244 | else |
669 | 244 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
670 | 2.18k | } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) && |
671 | 2.18k | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
672 | 2.18k | (cinfo->in_color_space == JCS_EXT_RGB || |
673 | 574 | cinfo->in_color_space == JCS_RGB)) { |
674 | | #else |
675 | | cinfo->in_color_space == JCS_EXT_RGB) { |
676 | | #endif |
677 | 82 | source->pub.get_pixel_rows = get_raw_row; |
678 | 82 | use_raw_buffer = TRUE; |
679 | 82 | need_rescale = FALSE; |
680 | 2.10k | } else { |
681 | 2.10k | if (IsExtRGB(cinfo->in_color_space)) |
682 | 1.47k | source->pub.get_pixel_rows = get_rgb_row; |
683 | 624 | else if (cinfo->in_color_space == JCS_CMYK) |
684 | 312 | source->pub.get_pixel_rows = get_rgb_cmyk_row; |
685 | 312 | else |
686 | 312 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
687 | 2.10k | } |
688 | 3.03k | break; |
689 | 13.1k | } |
690 | | |
691 | 10.4k | if (IsExtRGB(cinfo->in_color_space)) |
692 | 7.96k | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
693 | 2.53k | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
694 | 1.06k | cinfo->input_components = 1; |
695 | 1.47k | else if (cinfo->in_color_space == JCS_CMYK) |
696 | 1.47k | cinfo->input_components = 4; |
697 | | |
698 | | /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ |
699 | 10.4k | if (need_iobuffer) { |
700 | 8.59k | if (c == '6') |
701 | 2.48k | source->buffer_width = (size_t)w * 3 * |
702 | 2.48k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
703 | 6.11k | else |
704 | 6.11k | source->buffer_width = (size_t)w * |
705 | 6.11k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
706 | 8.59k | source->iobuffer = (U_CHAR *) |
707 | 8.59k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
708 | 8.59k | source->buffer_width); |
709 | 8.59k | } |
710 | | |
711 | | /* Create compressor input buffer. */ |
712 | 10.4k | if (use_raw_buffer) { |
713 | | /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ |
714 | | /* Synthesize a JSAMPARRAY pointer structure */ |
715 | 169 | source->pixrow = (JSAMPROW)source->iobuffer; |
716 | 169 | source->pub.buffer = &source->pixrow; |
717 | 169 | source->pub.buffer_height = 1; |
718 | 10.3k | } else { |
719 | | /* Need to translate anyway, so make a separate sample buffer. */ |
720 | 10.3k | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
721 | 10.3k | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
722 | 10.3k | (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1); |
723 | 10.3k | source->pub.buffer_height = 1; |
724 | 10.3k | } |
725 | | |
726 | | /* Compute the rescaling array if required. */ |
727 | 10.4k | if (need_rescale) { |
728 | 10.3k | long val, half_maxval; |
729 | | |
730 | | /* On 16-bit-int machines we have to be careful of maxval = 65535 */ |
731 | 10.3k | source->rescale = (JSAMPLE *) |
732 | 10.3k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
733 | 10.3k | (size_t)(((long)MAX(maxval, 255) + 1L) * |
734 | 10.3k | sizeof(JSAMPLE))); |
735 | 10.3k | memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) * |
736 | 10.3k | sizeof(JSAMPLE))); |
737 | 10.3k | half_maxval = maxval / 2; |
738 | 11.8M | for (val = 0; val <= (long)maxval; val++) { |
739 | | /* The multiplication here must be done in 32 bits to avoid overflow */ |
740 | 11.8M | source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) / |
741 | 11.8M | maxval); |
742 | 11.8M | } |
743 | 10.3k | } |
744 | 10.4k | } |
745 | | |
746 | | |
747 | | /* |
748 | | * Finish up at the end of the file. |
749 | | */ |
750 | | |
751 | | METHODDEF(void) |
752 | | finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
753 | 6.34k | { |
754 | | /* no work */ |
755 | 6.34k | } |
756 | | |
757 | | |
758 | | /* |
759 | | * The module selection routine for PPM format input. |
760 | | */ |
761 | | |
762 | | GLOBAL(cjpeg_source_ptr) |
763 | | jinit_read_ppm(j_compress_ptr cinfo) |
764 | 13.1k | { |
765 | 13.1k | ppm_source_ptr source; |
766 | | |
767 | | /* Create module interface object */ |
768 | 13.1k | source = (ppm_source_ptr) |
769 | 13.1k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
770 | 13.1k | sizeof(ppm_source_struct)); |
771 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
772 | 13.1k | source->pub.start_input = start_input_ppm; |
773 | 13.1k | source->pub.finish_input = finish_input_ppm; |
774 | 13.1k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
775 | 13.1k | source->pub.max_pixels = 0; |
776 | 13.1k | #endif |
777 | | |
778 | 13.1k | return (cjpeg_source_ptr)source; |
779 | 13.1k | } |
780 | | |
781 | | #endif /* PPM_SUPPORTED */ |