/src/libjpeg-turbo.main/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 | | #if defined(PPM_SUPPORTED) && \ |
29 | | (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) |
30 | | |
31 | | |
32 | | /* Portions of this code are based on the PBMPLUS library, which is: |
33 | | ** |
34 | | ** Copyright (C) 1988 by Jef Poskanzer. |
35 | | ** |
36 | | ** Permission to use, copy, modify, and distribute this software and its |
37 | | ** documentation for any purpose and without fee is hereby granted, provided |
38 | | ** that the above copyright notice appear in all copies and that both that |
39 | | ** copyright notice and this permission notice appear in supporting |
40 | | ** documentation. This software is provided "as is" without express or |
41 | | ** implied warranty. |
42 | | */ |
43 | | |
44 | | |
45 | | /* Macros to deal with unsigned chars as efficiently as compiler allows */ |
46 | | |
47 | | typedef unsigned char U_CHAR; |
48 | 13.3M | #define UCH(x) ((int)(x)) |
49 | | |
50 | | |
51 | | #define ReadOK(file, buffer, len) \ |
52 | 13.1M | (fread(buffer, 1, len, file) == ((size_t)(len))) |
53 | | |
54 | | static int alpha_index[JPEG_NUMCS] = { |
55 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1 |
56 | | }; |
57 | | |
58 | | |
59 | | /* Private version of data source object */ |
60 | | |
61 | | typedef struct { |
62 | | struct cjpeg_source_struct pub; /* public fields */ |
63 | | |
64 | | /* Usually these two pointers point to the same place: */ |
65 | | U_CHAR *iobuffer; /* fread's I/O buffer */ |
66 | | _JSAMPROW pixrow; /* compressor input buffer */ |
67 | | size_t buffer_width; /* width of I/O buffer */ |
68 | | _JSAMPLE *rescale; /* => maxval-remapping array, or NULL */ |
69 | | unsigned int maxval; |
70 | | } ppm_source_struct; |
71 | | |
72 | | typedef ppm_source_struct *ppm_source_ptr; |
73 | | |
74 | | |
75 | | LOCAL(int) |
76 | | pbm_getc(FILE *infile) |
77 | | /* Read next char, skipping over any comments */ |
78 | | /* A comment/newline sequence is returned as a newline */ |
79 | 235k | { |
80 | 235k | register int ch; |
81 | | |
82 | 235k | ch = getc(infile); |
83 | 235k | if (ch == '#') { |
84 | 125k | do { |
85 | 125k | ch = getc(infile); |
86 | 125k | } while (ch != '\n' && ch != EOF); |
87 | 3.08k | } |
88 | 235k | return ch; |
89 | 235k | } |
90 | | |
91 | | |
92 | | LOCAL(unsigned int) |
93 | | read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval) |
94 | | /* Read an unsigned decimal integer from the PPM file */ |
95 | | /* Swallows one trailing character after the integer */ |
96 | | /* Note that on a 16-bit-int machine, only values up to 64k can be read. */ |
97 | | /* This should not be a problem in practice. */ |
98 | 100k | { |
99 | 100k | register int ch; |
100 | 100k | register unsigned int val; |
101 | | |
102 | | /* Skip any leading whitespace */ |
103 | 107k | do { |
104 | 107k | ch = pbm_getc(infile); |
105 | 107k | if (ch == EOF) |
106 | 2.56k | ERREXIT(cinfo, JERR_INPUT_EOF); |
107 | 107k | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
108 | | |
109 | 100k | if (ch < '0' || ch > '9') |
110 | 242 | ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
111 | | |
112 | 100k | val = ch - '0'; |
113 | 130k | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
114 | 29.5k | val *= 10; |
115 | 29.5k | val += ch - '0'; |
116 | 29.5k | if (val > maxval) |
117 | 152 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
118 | 29.5k | } |
119 | | |
120 | 100k | return val; |
121 | 100k | } |
122 | | |
123 | | |
124 | | /* |
125 | | * Read one row of pixels. |
126 | | * |
127 | | * We provide several different versions depending on input file format. |
128 | | * In all cases, input is scaled to the size of _JSAMPLE. |
129 | | * |
130 | | * A really fast path is provided for reading byte/sample raw files with |
131 | | * maxval = _MAXJSAMPLE, which is the normal case for 8-bit data. |
132 | | */ |
133 | | |
134 | | |
135 | | METHODDEF(JDIMENSION) |
136 | | get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
137 | | /* This version is for reading text-format PGM files with any maxval */ |
138 | 1.32k | { |
139 | 1.32k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
140 | 1.32k | FILE *infile = source->pub.input_file; |
141 | 1.32k | register _JSAMPROW ptr; |
142 | 1.32k | register _JSAMPLE *rescale = source->rescale; |
143 | 1.32k | JDIMENSION col; |
144 | 1.32k | unsigned int maxval = source->maxval; |
145 | | |
146 | 1.32k | ptr = source->pub._buffer[0]; |
147 | 3.78k | for (col = cinfo->image_width; col > 0; col--) { |
148 | 2.45k | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
149 | 2.45k | } |
150 | 1.32k | return 1; |
151 | 1.32k | } |
152 | | |
153 | | |
154 | 9.25M | #define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \ |
155 | 46.8M | for (col = cinfo->image_width; col > 0; col--) { \ |
156 | 37.6M | ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \ |
157 | 37.6M | alpha_set_op \ |
158 | 37.6M | ptr += ps; \ |
159 | 37.6M | } \ |
160 | 9.25M | } |
161 | | |
162 | | METHODDEF(JDIMENSION) |
163 | | get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
164 | | /* This version is for reading text-format PGM files with any maxval and |
165 | | converting to extended RGB */ |
166 | 6.62k | { |
167 | 6.62k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
168 | 6.62k | FILE *infile = source->pub.input_file; |
169 | 6.62k | register _JSAMPROW ptr; |
170 | 6.62k | register _JSAMPLE *rescale = source->rescale; |
171 | 6.62k | JDIMENSION col; |
172 | 6.62k | unsigned int maxval = source->maxval; |
173 | 6.62k | register int rindex = rgb_red[cinfo->in_color_space]; |
174 | 6.62k | register int gindex = rgb_green[cinfo->in_color_space]; |
175 | 6.62k | register int bindex = rgb_blue[cinfo->in_color_space]; |
176 | 6.62k | register int aindex = alpha_index[cinfo->in_color_space]; |
177 | 6.62k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
178 | | |
179 | 6.62k | ptr = source->pub._buffer[0]; |
180 | 6.62k | if (maxval == _MAXJSAMPLE) { |
181 | 1.41k | if (aindex >= 0) |
182 | 283 | GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
183 | 1.41k | ptr[aindex] = _MAXJSAMPLE;) |
184 | 1.13k | else |
185 | 1.13k | GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
186 | 5.21k | } else { |
187 | 5.21k | if (aindex >= 0) |
188 | 1.04k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
189 | 5.21k | ptr[aindex] = _MAXJSAMPLE;) |
190 | 4.16k | else |
191 | 4.16k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
192 | 5.21k | } |
193 | 6.62k | return 1; |
194 | 6.62k | } |
195 | | |
196 | | |
197 | | METHODDEF(JDIMENSION) |
198 | | get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
199 | | /* This version is for reading text-format PGM files with any maxval and |
200 | | converting to CMYK */ |
201 | 1.32k | { |
202 | 1.32k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
203 | 1.32k | FILE *infile = source->pub.input_file; |
204 | 1.32k | register _JSAMPROW ptr; |
205 | 1.32k | register _JSAMPLE *rescale = source->rescale; |
206 | 1.32k | JDIMENSION col; |
207 | 1.32k | unsigned int maxval = source->maxval; |
208 | | |
209 | 1.32k | ptr = source->pub._buffer[0]; |
210 | 1.32k | if (maxval == _MAXJSAMPLE) { |
211 | 1.01k | for (col = cinfo->image_width; col > 0; col--) { |
212 | 727 | _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
213 | 727 | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
214 | 727 | ptr += 4; |
215 | 727 | } |
216 | 1.04k | } else { |
217 | 2.77k | for (col = cinfo->image_width; col > 0; col--) { |
218 | 1.73k | _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
219 | 1.73k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
220 | 1.73k | ptr += 4; |
221 | 1.73k | } |
222 | 1.04k | } |
223 | 1.32k | return 1; |
224 | 1.32k | } |
225 | | |
226 | | |
227 | 51.7k | #define RGB_READ_LOOP(read_op, alpha_set_op) { \ |
228 | 3.14M | for (col = cinfo->image_width; col > 0; col--) { \ |
229 | 3.09M | ptr[rindex] = read_op; \ |
230 | 3.09M | ptr[gindex] = read_op; \ |
231 | 3.09M | ptr[bindex] = read_op; \ |
232 | 3.09M | alpha_set_op \ |
233 | 3.09M | ptr += ps; \ |
234 | 3.09M | } \ |
235 | 51.7k | } |
236 | | |
237 | | METHODDEF(JDIMENSION) |
238 | | get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
239 | | /* This version is for reading text-format PPM files with any maxval */ |
240 | 7.44k | { |
241 | 7.44k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
242 | 7.44k | FILE *infile = source->pub.input_file; |
243 | 7.44k | register _JSAMPROW ptr; |
244 | 7.44k | register _JSAMPLE *rescale = source->rescale; |
245 | 7.44k | JDIMENSION col; |
246 | 7.44k | unsigned int maxval = source->maxval; |
247 | 7.44k | register int rindex = rgb_red[cinfo->in_color_space]; |
248 | 7.44k | register int gindex = rgb_green[cinfo->in_color_space]; |
249 | 7.44k | register int bindex = rgb_blue[cinfo->in_color_space]; |
250 | 7.44k | register int aindex = alpha_index[cinfo->in_color_space]; |
251 | 7.44k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
252 | | |
253 | 7.44k | ptr = source->pub._buffer[0]; |
254 | 7.44k | if (maxval == _MAXJSAMPLE) { |
255 | 2.91k | if (aindex >= 0) |
256 | 583 | RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
257 | 2.91k | ptr[aindex] = _MAXJSAMPLE;) |
258 | 2.33k | else |
259 | 2.33k | RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
260 | 4.53k | } else { |
261 | 4.53k | if (aindex >= 0) |
262 | 906 | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
263 | 4.53k | ptr[aindex] = _MAXJSAMPLE;) |
264 | 3.62k | else |
265 | 3.62k | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
266 | 4.53k | } |
267 | 7.44k | return 1; |
268 | 7.44k | } |
269 | | |
270 | | |
271 | | METHODDEF(JDIMENSION) |
272 | | get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
273 | | /* This version is for reading text-format PPM files with any maxval and |
274 | | converting to CMYK */ |
275 | 1.48k | { |
276 | 1.48k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
277 | 1.48k | FILE *infile = source->pub.input_file; |
278 | 1.48k | register _JSAMPROW ptr; |
279 | 1.48k | register _JSAMPLE *rescale = source->rescale; |
280 | 1.48k | JDIMENSION col; |
281 | 1.48k | unsigned int maxval = source->maxval; |
282 | | |
283 | 1.48k | ptr = source->pub._buffer[0]; |
284 | 1.48k | if (maxval == _MAXJSAMPLE) { |
285 | 1.88k | for (col = cinfo->image_width; col > 0; col--) { |
286 | 1.29k | _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
287 | 1.29k | _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
288 | 1.29k | _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
289 | 1.29k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
290 | 1.29k | ptr += 4; |
291 | 1.29k | } |
292 | 906 | } else { |
293 | 2.56k | for (col = cinfo->image_width; col > 0; col--) { |
294 | 1.65k | _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
295 | 1.65k | _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
296 | 1.65k | _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
297 | 1.65k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
298 | 1.65k | ptr += 4; |
299 | 1.65k | } |
300 | 906 | } |
301 | 1.48k | return 1; |
302 | 1.48k | } |
303 | | |
304 | | |
305 | | METHODDEF(JDIMENSION) |
306 | | get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
307 | | /* This version is for reading raw-byte-format PGM files with any maxval */ |
308 | 1.61M | { |
309 | 1.61M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
310 | 1.61M | register _JSAMPROW ptr; |
311 | 1.61M | register U_CHAR *bufferptr; |
312 | 1.61M | register _JSAMPLE *rescale = source->rescale; |
313 | 1.61M | JDIMENSION col; |
314 | | |
315 | 1.61M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
316 | 68 | ERREXIT(cinfo, JERR_INPUT_EOF); |
317 | 1.61M | ptr = source->pub._buffer[0]; |
318 | 1.61M | bufferptr = source->iobuffer; |
319 | 6.60M | for (col = cinfo->image_width; col > 0; col--) { |
320 | 4.99M | *ptr++ = rescale[UCH(*bufferptr++)]; |
321 | 4.99M | } |
322 | 1.61M | return 1; |
323 | 1.61M | } |
324 | | |
325 | | |
326 | | METHODDEF(JDIMENSION) |
327 | | get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
328 | | /* This version is for reading raw-byte-format PGM files with any maxval |
329 | | and converting to extended RGB */ |
330 | 9.25M | { |
331 | 9.25M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
332 | 9.25M | register _JSAMPROW ptr; |
333 | 9.25M | register U_CHAR *bufferptr; |
334 | 9.25M | register _JSAMPLE *rescale = source->rescale; |
335 | 9.25M | JDIMENSION col; |
336 | 9.25M | unsigned int maxval = source->maxval; |
337 | 9.25M | register int rindex = rgb_red[cinfo->in_color_space]; |
338 | 9.25M | register int gindex = rgb_green[cinfo->in_color_space]; |
339 | 9.25M | register int bindex = rgb_blue[cinfo->in_color_space]; |
340 | 9.25M | register int aindex = alpha_index[cinfo->in_color_space]; |
341 | 9.25M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
342 | | |
343 | 9.25M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
344 | 535 | ERREXIT(cinfo, JERR_INPUT_EOF); |
345 | 9.25M | ptr = source->pub._buffer[0]; |
346 | 9.25M | bufferptr = source->iobuffer; |
347 | 9.25M | if (maxval == _MAXJSAMPLE) { |
348 | 1.19M | if (aindex >= 0) |
349 | 239k | GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;) |
350 | 957k | else |
351 | 957k | GRAY_RGB_READ_LOOP(*bufferptr++, {}) |
352 | 8.05M | } else { |
353 | 8.05M | if (aindex >= 0) |
354 | 1.61M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], |
355 | 8.05M | ptr[aindex] = _MAXJSAMPLE;) |
356 | 6.44M | else |
357 | 6.44M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
358 | 8.05M | } |
359 | 9.25M | return 1; |
360 | 9.25M | } |
361 | | |
362 | | |
363 | | METHODDEF(JDIMENSION) |
364 | | get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
365 | | /* This version is for reading raw-byte-format PGM files with any maxval |
366 | | and converting to CMYK */ |
367 | 1.85M | { |
368 | 1.85M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
369 | 1.85M | register _JSAMPROW ptr; |
370 | 1.85M | register U_CHAR *bufferptr; |
371 | 1.85M | register _JSAMPLE *rescale = source->rescale; |
372 | 1.85M | JDIMENSION col; |
373 | 1.85M | unsigned int maxval = source->maxval; |
374 | | |
375 | 1.85M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
376 | 107 | ERREXIT(cinfo, JERR_INPUT_EOF); |
377 | 1.85M | ptr = source->pub._buffer[0]; |
378 | 1.85M | bufferptr = source->iobuffer; |
379 | 1.85M | if (maxval == _MAXJSAMPLE) { |
380 | 2.76M | for (col = cinfo->image_width; col > 0; col--) { |
381 | 2.53M | _JSAMPLE gray = *bufferptr++; |
382 | 2.53M | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
383 | 2.53M | ptr += 4; |
384 | 2.53M | } |
385 | 1.61M | } else { |
386 | 6.60M | for (col = cinfo->image_width; col > 0; col--) { |
387 | 4.99M | _JSAMPLE gray = rescale[UCH(*bufferptr++)]; |
388 | 4.99M | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
389 | 4.99M | ptr += 4; |
390 | 4.99M | } |
391 | 1.61M | } |
392 | 1.85M | return 1; |
393 | 1.85M | } |
394 | | |
395 | | |
396 | | METHODDEF(JDIMENSION) |
397 | | get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
398 | | /* This version is for reading raw-byte-format PPM files with any maxval */ |
399 | 44.2k | { |
400 | 44.2k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
401 | 44.2k | register _JSAMPROW ptr; |
402 | 44.2k | register U_CHAR *bufferptr; |
403 | 44.2k | register _JSAMPLE *rescale = source->rescale; |
404 | 44.2k | JDIMENSION col; |
405 | 44.2k | unsigned int maxval = source->maxval; |
406 | 44.2k | register int rindex = rgb_red[cinfo->in_color_space]; |
407 | 44.2k | register int gindex = rgb_green[cinfo->in_color_space]; |
408 | 44.2k | register int bindex = rgb_blue[cinfo->in_color_space]; |
409 | 44.2k | register int aindex = alpha_index[cinfo->in_color_space]; |
410 | 44.2k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
411 | | |
412 | 44.2k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
413 | 819 | ERREXIT(cinfo, JERR_INPUT_EOF); |
414 | 44.2k | ptr = source->pub._buffer[0]; |
415 | 44.2k | bufferptr = source->iobuffer; |
416 | 44.2k | if (maxval == _MAXJSAMPLE) { |
417 | 4.06k | if (aindex >= 0) |
418 | 1.01k | RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;) |
419 | 3.05k | else |
420 | 3.05k | RGB_READ_LOOP(*bufferptr++, {}) |
421 | 40.2k | } else { |
422 | 40.2k | if (aindex >= 0) |
423 | 7.87k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = _MAXJSAMPLE;) |
424 | 32.3k | else |
425 | 32.3k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
426 | 40.2k | } |
427 | 44.2k | return 1; |
428 | 44.2k | } |
429 | | |
430 | | |
431 | | METHODDEF(JDIMENSION) |
432 | | get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
433 | | /* This version is for reading raw-byte-format PPM files with any maxval and |
434 | | converting to CMYK */ |
435 | 9.07k | { |
436 | 9.07k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
437 | 9.07k | register _JSAMPROW ptr; |
438 | 9.07k | register U_CHAR *bufferptr; |
439 | 9.07k | register _JSAMPLE *rescale = source->rescale; |
440 | 9.07k | JDIMENSION col; |
441 | 9.07k | unsigned int maxval = source->maxval; |
442 | | |
443 | 9.07k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
444 | 177 | ERREXIT(cinfo, JERR_INPUT_EOF); |
445 | 9.07k | ptr = source->pub._buffer[0]; |
446 | 9.07k | bufferptr = source->iobuffer; |
447 | 9.07k | if (maxval == _MAXJSAMPLE) { |
448 | 266k | for (col = cinfo->image_width; col > 0; col--) { |
449 | 265k | _JSAMPLE r = *bufferptr++; |
450 | 265k | _JSAMPLE g = *bufferptr++; |
451 | 265k | _JSAMPLE b = *bufferptr++; |
452 | 265k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
453 | 265k | ptr += 4; |
454 | 265k | } |
455 | 8.05k | } else { |
456 | 412k | for (col = cinfo->image_width; col > 0; col--) { |
457 | 403k | _JSAMPLE r = rescale[UCH(*bufferptr++)]; |
458 | 403k | _JSAMPLE g = rescale[UCH(*bufferptr++)]; |
459 | 403k | _JSAMPLE b = rescale[UCH(*bufferptr++)]; |
460 | 403k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
461 | 403k | ptr += 4; |
462 | 403k | } |
463 | 8.05k | } |
464 | 9.07k | return 1; |
465 | 9.07k | } |
466 | | |
467 | | |
468 | | METHODDEF(JDIMENSION) |
469 | | get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
470 | | /* This version is for reading raw-byte-format files with maxval = _MAXJSAMPLE. |
471 | | * In this case we just read right into the _JSAMPLE buffer! |
472 | | * Note that same code works for PPM and PGM files. |
473 | | */ |
474 | 240k | { |
475 | 240k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
476 | | |
477 | 240k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
478 | 105 | ERREXIT(cinfo, JERR_INPUT_EOF); |
479 | 240k | return 1; |
480 | 240k | } |
481 | | |
482 | | |
483 | | METHODDEF(JDIMENSION) |
484 | | get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
485 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
486 | 20.1k | { |
487 | 20.1k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
488 | 20.1k | register _JSAMPROW ptr; |
489 | 20.1k | register U_CHAR *bufferptr; |
490 | 20.1k | register _JSAMPLE *rescale = source->rescale; |
491 | 20.1k | JDIMENSION col; |
492 | 20.1k | unsigned int maxval = source->maxval; |
493 | | |
494 | 20.1k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
495 | 68 | ERREXIT(cinfo, JERR_INPUT_EOF); |
496 | 20.1k | ptr = source->pub._buffer[0]; |
497 | 20.1k | bufferptr = source->iobuffer; |
498 | 166k | for (col = cinfo->image_width; col > 0; col--) { |
499 | 146k | register unsigned int temp; |
500 | 146k | temp = UCH(*bufferptr++) << 8; |
501 | 146k | temp |= UCH(*bufferptr++); |
502 | 146k | if (temp > maxval) |
503 | 36 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
504 | 146k | *ptr++ = rescale[temp]; |
505 | 146k | } |
506 | 20.1k | return 1; |
507 | 20.1k | } |
508 | | |
509 | | |
510 | | METHODDEF(JDIMENSION) |
511 | | get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
512 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
513 | 100k | { |
514 | 100k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
515 | 100k | register _JSAMPROW ptr; |
516 | 100k | register U_CHAR *bufferptr; |
517 | 100k | register _JSAMPLE *rescale = source->rescale; |
518 | 100k | JDIMENSION col; |
519 | 100k | unsigned int maxval = source->maxval; |
520 | 100k | register int rindex = rgb_red[cinfo->in_color_space]; |
521 | 100k | register int gindex = rgb_green[cinfo->in_color_space]; |
522 | 100k | register int bindex = rgb_blue[cinfo->in_color_space]; |
523 | 100k | register int aindex = alpha_index[cinfo->in_color_space]; |
524 | 100k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
525 | | |
526 | 100k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
527 | 340 | ERREXIT(cinfo, JERR_INPUT_EOF); |
528 | 100k | ptr = source->pub._buffer[0]; |
529 | 100k | bufferptr = source->iobuffer; |
530 | 831k | for (col = cinfo->image_width; col > 0; col--) { |
531 | 730k | register unsigned int temp; |
532 | 730k | temp = UCH(*bufferptr++) << 8; |
533 | 730k | temp |= UCH(*bufferptr++); |
534 | 730k | if (temp > maxval) |
535 | 180 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
536 | 730k | ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp]; |
537 | 730k | if (aindex >= 0) |
538 | 146k | ptr[aindex] = _MAXJSAMPLE; |
539 | 730k | ptr += ps; |
540 | 730k | } |
541 | 100k | return 1; |
542 | 100k | } |
543 | | |
544 | | |
545 | | METHODDEF(JDIMENSION) |
546 | | get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
547 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
548 | 20.1k | { |
549 | 20.1k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
550 | 20.1k | register _JSAMPROW ptr; |
551 | 20.1k | register U_CHAR *bufferptr; |
552 | 20.1k | register _JSAMPLE *rescale = source->rescale; |
553 | 20.1k | JDIMENSION col; |
554 | 20.1k | unsigned int maxval = source->maxval; |
555 | | |
556 | 20.1k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
557 | 68 | ERREXIT(cinfo, JERR_INPUT_EOF); |
558 | 20.1k | ptr = source->pub._buffer[0]; |
559 | 20.1k | bufferptr = source->iobuffer; |
560 | 166k | for (col = cinfo->image_width; col > 0; col--) { |
561 | 146k | register unsigned int gray; |
562 | 146k | gray = UCH(*bufferptr++) << 8; |
563 | 146k | gray |= UCH(*bufferptr++); |
564 | 146k | if (gray > maxval) |
565 | 36 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
566 | 146k | rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1, |
567 | 146k | ptr + 2, ptr + 3); |
568 | 146k | ptr += 4; |
569 | 146k | } |
570 | 20.1k | return 1; |
571 | 20.1k | } |
572 | | |
573 | | |
574 | | METHODDEF(JDIMENSION) |
575 | | get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
576 | | /* This version is for reading raw-word-format PPM files with any maxval */ |
577 | 10.0k | { |
578 | 10.0k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
579 | 10.0k | register _JSAMPROW ptr; |
580 | 10.0k | register U_CHAR *bufferptr; |
581 | 10.0k | register _JSAMPLE *rescale = source->rescale; |
582 | 10.0k | JDIMENSION col; |
583 | 10.0k | unsigned int maxval = source->maxval; |
584 | 10.0k | register int rindex = rgb_red[cinfo->in_color_space]; |
585 | 10.0k | register int gindex = rgb_green[cinfo->in_color_space]; |
586 | 10.0k | register int bindex = rgb_blue[cinfo->in_color_space]; |
587 | 10.0k | register int aindex = alpha_index[cinfo->in_color_space]; |
588 | 10.0k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
589 | | |
590 | 10.0k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
591 | 415 | ERREXIT(cinfo, JERR_INPUT_EOF); |
592 | 10.0k | ptr = source->pub._buffer[0]; |
593 | 10.0k | bufferptr = source->iobuffer; |
594 | 26.7k | for (col = cinfo->image_width; col > 0; col--) { |
595 | 16.7k | register unsigned int temp; |
596 | 16.7k | temp = UCH(*bufferptr++) << 8; |
597 | 16.7k | temp |= UCH(*bufferptr++); |
598 | 16.7k | if (temp > maxval) |
599 | 145 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
600 | 16.7k | ptr[rindex] = rescale[temp]; |
601 | 16.7k | temp = UCH(*bufferptr++) << 8; |
602 | 16.7k | temp |= UCH(*bufferptr++); |
603 | 16.7k | if (temp > maxval) |
604 | 110 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
605 | 16.7k | ptr[gindex] = rescale[temp]; |
606 | 16.7k | temp = UCH(*bufferptr++) << 8; |
607 | 16.7k | temp |= UCH(*bufferptr++); |
608 | 16.7k | if (temp > maxval) |
609 | 115 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
610 | 16.7k | ptr[bindex] = rescale[temp]; |
611 | 16.7k | if (aindex >= 0) |
612 | 3.26k | ptr[aindex] = _MAXJSAMPLE; |
613 | 16.7k | ptr += ps; |
614 | 16.7k | } |
615 | 10.0k | return 1; |
616 | 10.0k | } |
617 | | |
618 | | |
619 | | METHODDEF(JDIMENSION) |
620 | | get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
621 | | /* This version is for reading raw-word-format PPM files with any maxval */ |
622 | 2.01k | { |
623 | 2.01k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
624 | 2.01k | register _JSAMPROW ptr; |
625 | 2.01k | register U_CHAR *bufferptr; |
626 | 2.01k | register _JSAMPLE *rescale = source->rescale; |
627 | 2.01k | JDIMENSION col; |
628 | 2.01k | unsigned int maxval = source->maxval; |
629 | | |
630 | 2.01k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
631 | 83 | ERREXIT(cinfo, JERR_INPUT_EOF); |
632 | 2.01k | ptr = source->pub._buffer[0]; |
633 | 2.01k | bufferptr = source->iobuffer; |
634 | 5.35k | for (col = cinfo->image_width; col > 0; col--) { |
635 | 3.34k | register unsigned int r, g, b; |
636 | 3.34k | r = UCH(*bufferptr++) << 8; |
637 | 3.34k | r |= UCH(*bufferptr++); |
638 | 3.34k | if (r > maxval) |
639 | 29 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
640 | 3.34k | g = UCH(*bufferptr++) << 8; |
641 | 3.34k | g |= UCH(*bufferptr++); |
642 | 3.34k | if (g > maxval) |
643 | 22 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
644 | 3.34k | b = UCH(*bufferptr++) << 8; |
645 | 3.34k | b |= UCH(*bufferptr++); |
646 | 3.34k | if (b > maxval) |
647 | 23 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
648 | 3.34k | rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2, |
649 | 3.34k | ptr + 3); |
650 | 3.34k | ptr += 4; |
651 | 3.34k | } |
652 | 2.01k | return 1; |
653 | 2.01k | } |
654 | | |
655 | | |
656 | | /* |
657 | | * Read the file header; return image size and component count. |
658 | | */ |
659 | | |
660 | | METHODDEF(void) |
661 | | start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
662 | 11.4k | { |
663 | 11.4k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
664 | 11.4k | int c; |
665 | 11.4k | unsigned int w, h, maxval; |
666 | 11.4k | boolean need_iobuffer, use_raw_buffer, need_rescale; |
667 | | |
668 | 11.4k | if (getc(source->pub.input_file) != 'P') |
669 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
670 | | |
671 | 11.4k | c = getc(source->pub.input_file); /* subformat discriminator character */ |
672 | | |
673 | | /* detect unsupported variants (ie, PBM) before trying to read header */ |
674 | 11.4k | switch (c) { |
675 | 1.19k | case '2': /* it's a text-format PGM file */ |
676 | 2.65k | case '3': /* it's a text-format PPM file */ |
677 | 8.31k | case '5': /* it's a raw-format PGM file */ |
678 | 11.4k | case '6': /* it's a raw-format PPM file */ |
679 | 11.4k | break; |
680 | 14 | default: |
681 | 14 | ERREXIT(cinfo, JERR_PPM_NOT); |
682 | 14 | break; |
683 | 11.4k | } |
684 | | |
685 | | /* fetch the remaining header info */ |
686 | 11.4k | w = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
687 | 11.4k | h = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
688 | 11.4k | maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
689 | | |
690 | 11.4k | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
691 | 28 | ERREXIT(cinfo, JERR_PPM_NOT); |
692 | 11.4k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
693 | 11.4k | if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels) |
694 | 238 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
695 | 11.4k | #endif |
696 | | |
697 | 11.4k | cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ |
698 | 11.4k | cinfo->image_width = (JDIMENSION)w; |
699 | 11.4k | cinfo->image_height = (JDIMENSION)h; |
700 | 11.4k | source->maxval = maxval; |
701 | | |
702 | | /* initialize flags to most common settings */ |
703 | 11.4k | need_iobuffer = TRUE; /* do we need an I/O buffer? */ |
704 | 11.4k | use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ |
705 | 11.4k | need_rescale = TRUE; /* do we need a rescale array? */ |
706 | | |
707 | 11.4k | switch (c) { |
708 | 861 | case '2': /* it's a text-format PGM file */ |
709 | 861 | if (cinfo->in_color_space == JCS_UNKNOWN || |
710 | 861 | cinfo->in_color_space == JCS_RGB) |
711 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
712 | 861 | TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h); |
713 | 861 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
714 | 123 | source->pub.get_pixel_rows = get_text_gray_row; |
715 | 738 | else if (IsExtRGB(cinfo->in_color_space)) |
716 | 615 | source->pub.get_pixel_rows = get_text_gray_rgb_row; |
717 | 123 | else if (cinfo->in_color_space == JCS_CMYK) |
718 | 123 | source->pub.get_pixel_rows = get_text_gray_cmyk_row; |
719 | 0 | else |
720 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
721 | 861 | need_iobuffer = FALSE; |
722 | 861 | break; |
723 | | |
724 | 1.14k | case '3': /* it's a text-format PPM file */ |
725 | 1.14k | if (cinfo->in_color_space == JCS_UNKNOWN) |
726 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
727 | 1.14k | TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h); |
728 | 1.14k | if (IsExtRGB(cinfo->in_color_space)) |
729 | 820 | source->pub.get_pixel_rows = get_text_rgb_row; |
730 | 328 | else if (cinfo->in_color_space == JCS_CMYK) |
731 | 164 | source->pub.get_pixel_rows = get_text_rgb_cmyk_row; |
732 | 164 | else |
733 | 164 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
734 | 1.14k | need_iobuffer = FALSE; |
735 | 1.14k | break; |
736 | | |
737 | 5.19k | case '5': /* it's a raw-format PGM file */ |
738 | 5.19k | if (cinfo->in_color_space == JCS_UNKNOWN || |
739 | 5.19k | cinfo->in_color_space == JCS_RGB) |
740 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
741 | 5.19k | TRACEMS2(cinfo, 1, JTRC_PGM, w, h); |
742 | 5.19k | if (maxval > 255) { |
743 | 882 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
744 | 126 | source->pub.get_pixel_rows = get_word_gray_row; |
745 | 756 | else if (IsExtRGB(cinfo->in_color_space)) |
746 | 630 | source->pub.get_pixel_rows = get_word_gray_rgb_row; |
747 | 126 | else if (cinfo->in_color_space == JCS_CMYK) |
748 | 126 | source->pub.get_pixel_rows = get_word_gray_cmyk_row; |
749 | 0 | else |
750 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
751 | 4.31k | } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) && |
752 | 4.31k | cinfo->in_color_space == JCS_GRAYSCALE) { |
753 | 120 | source->pub.get_pixel_rows = get_raw_row; |
754 | 120 | use_raw_buffer = TRUE; |
755 | 120 | need_rescale = FALSE; |
756 | 4.19k | } else { |
757 | 4.19k | if (cinfo->in_color_space == JCS_GRAYSCALE) |
758 | 496 | source->pub.get_pixel_rows = get_scaled_gray_row; |
759 | 3.69k | else if (IsExtRGB(cinfo->in_color_space)) |
760 | 3.08k | source->pub.get_pixel_rows = get_gray_rgb_row; |
761 | 616 | else if (cinfo->in_color_space == JCS_CMYK) |
762 | 616 | source->pub.get_pixel_rows = get_gray_cmyk_row; |
763 | 0 | else |
764 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
765 | 4.19k | } |
766 | 5.19k | break; |
767 | | |
768 | 2.77k | case '6': /* it's a raw-format PPM file */ |
769 | 2.77k | if (cinfo->in_color_space == JCS_UNKNOWN) |
770 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
771 | 2.77k | TRACEMS2(cinfo, 1, JTRC_PPM, w, h); |
772 | 2.77k | if (maxval > 255) { |
773 | 1.12k | if (IsExtRGB(cinfo->in_color_space)) |
774 | 805 | source->pub.get_pixel_rows = get_word_rgb_row; |
775 | 322 | else if (cinfo->in_color_space == JCS_CMYK) |
776 | 161 | source->pub.get_pixel_rows = get_word_rgb_cmyk_row; |
777 | 161 | else |
778 | 161 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
779 | 1.64k | } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) && |
780 | 1.64k | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
781 | 1.64k | (cinfo->in_color_space == JCS_EXT_RGB || |
782 | 497 | cinfo->in_color_space == JCS_RGB)) { |
783 | | #else |
784 | | cinfo->in_color_space == JCS_EXT_RGB) { |
785 | | #endif |
786 | 71 | source->pub.get_pixel_rows = get_raw_row; |
787 | 71 | use_raw_buffer = TRUE; |
788 | 71 | need_rescale = FALSE; |
789 | 1.57k | } else { |
790 | 1.57k | if (IsExtRGB(cinfo->in_color_space)) |
791 | 1.10k | source->pub.get_pixel_rows = get_rgb_row; |
792 | 470 | else if (cinfo->in_color_space == JCS_CMYK) |
793 | 235 | source->pub.get_pixel_rows = get_rgb_cmyk_row; |
794 | 235 | else |
795 | 235 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
796 | 1.57k | } |
797 | 2.77k | break; |
798 | 11.4k | } |
799 | | |
800 | 9.41k | if (IsExtRGB(cinfo->in_color_space)) |
801 | 7.12k | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
802 | 2.29k | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
803 | 865 | cinfo->input_components = 1; |
804 | 1.42k | else if (cinfo->in_color_space == JCS_CMYK) |
805 | 1.42k | cinfo->input_components = 4; |
806 | | |
807 | | /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ |
808 | 9.41k | if (need_iobuffer) { |
809 | 7.57k | if (c == '6') |
810 | 2.37k | source->buffer_width = (size_t)w * 3 * |
811 | 2.37k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
812 | 5.19k | else |
813 | 5.19k | source->buffer_width = (size_t)w * |
814 | 5.19k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
815 | 7.57k | source->iobuffer = (U_CHAR *) |
816 | 7.57k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
817 | 7.57k | source->buffer_width); |
818 | 7.57k | } |
819 | | |
820 | | /* Create compressor input buffer. */ |
821 | 9.41k | if (use_raw_buffer) { |
822 | | /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ |
823 | | /* Synthesize a _JSAMPARRAY pointer structure */ |
824 | 191 | source->pixrow = (_JSAMPROW)source->iobuffer; |
825 | 191 | source->pub._buffer = &source->pixrow; |
826 | 191 | source->pub.buffer_height = 1; |
827 | 9.22k | } else { |
828 | | /* Need to translate anyway, so make a separate sample buffer. */ |
829 | 9.22k | source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray) |
830 | 9.22k | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
831 | 9.22k | (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1); |
832 | 9.22k | source->pub.buffer_height = 1; |
833 | 9.22k | } |
834 | | |
835 | | /* Compute the rescaling array if required. */ |
836 | 9.41k | if (need_rescale) { |
837 | 9.22k | long val, half_maxval; |
838 | | |
839 | | /* On 16-bit-int machines we have to be careful of maxval = 65535 */ |
840 | 9.22k | source->rescale = (_JSAMPLE *) |
841 | 9.22k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
842 | 9.22k | (size_t)(((long)MAX(maxval, 255) + 1L) * |
843 | 9.22k | sizeof(_JSAMPLE))); |
844 | 9.22k | memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) * |
845 | 9.22k | sizeof(_JSAMPLE))); |
846 | 9.22k | half_maxval = maxval / 2; |
847 | 34.5M | for (val = 0; val <= (long)maxval; val++) { |
848 | | /* The multiplication here must be done in 32 bits to avoid overflow */ |
849 | 34.5M | source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) / |
850 | 34.5M | maxval); |
851 | 34.5M | } |
852 | 9.22k | } |
853 | 9.41k | } |
854 | | |
855 | | |
856 | | /* |
857 | | * Finish up at the end of the file. |
858 | | */ |
859 | | |
860 | | METHODDEF(void) |
861 | | finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
862 | 4.17k | { |
863 | | /* no work */ |
864 | 4.17k | } |
865 | | |
866 | | |
867 | | /* |
868 | | * The module selection routine for PPM format input. |
869 | | */ |
870 | | |
871 | | GLOBAL(cjpeg_source_ptr) |
872 | | _jinit_read_ppm(j_compress_ptr cinfo) |
873 | 11.4k | { |
874 | 11.4k | ppm_source_ptr source; |
875 | | |
876 | 11.4k | if (cinfo->data_precision != BITS_IN_JSAMPLE) |
877 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
878 | | |
879 | | /* Create module interface object */ |
880 | 11.4k | source = (ppm_source_ptr) |
881 | 11.4k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
882 | 11.4k | sizeof(ppm_source_struct)); |
883 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
884 | 11.4k | source->pub.start_input = start_input_ppm; |
885 | 11.4k | source->pub.finish_input = finish_input_ppm; |
886 | 11.4k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
887 | 11.4k | source->pub.max_pixels = 0; |
888 | 11.4k | #endif |
889 | | |
890 | 11.4k | return (cjpeg_source_ptr)source; |
891 | 11.4k | } Line | Count | Source | 873 | 11.4k | { | 874 | 11.4k | ppm_source_ptr source; | 875 | | | 876 | 11.4k | if (cinfo->data_precision != BITS_IN_JSAMPLE) | 877 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | 878 | | | 879 | | /* Create module interface object */ | 880 | 11.4k | source = (ppm_source_ptr) | 881 | 11.4k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, | 882 | 11.4k | sizeof(ppm_source_struct)); | 883 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ | 884 | 11.4k | source->pub.start_input = start_input_ppm; | 885 | 11.4k | source->pub.finish_input = finish_input_ppm; | 886 | 11.4k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION | 887 | 11.4k | source->pub.max_pixels = 0; | 888 | 11.4k | #endif | 889 | | | 890 | 11.4k | return (cjpeg_source_ptr)source; | 891 | 11.4k | } |
Unexecuted instantiation: j12init_read_ppm Unexecuted instantiation: j16init_read_ppm |
892 | | |
893 | | #endif /* defined(PPM_SUPPORTED) && |
894 | | (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */ |