/src/libjpeg-turbo.dev/src/rdppm.c
Line | Count | Source |
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-2025, 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 | 0 | #define UCH(x) ((int)(x)) |
49 | | |
50 | | |
51 | | #define ReadOK(file, buffer, len) \ |
52 | 0 | (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 | 0 | { |
80 | 0 | register int ch; |
81 | |
|
82 | 0 | ch = getc(infile); |
83 | 0 | if (ch == '#') { |
84 | 0 | do { |
85 | 0 | ch = getc(infile); |
86 | 0 | } while (ch != '\n' && ch != EOF); |
87 | 0 | } |
88 | 0 | return ch; |
89 | 0 | } Unexecuted instantiation: rdppm-8.c:pbm_getc Unexecuted instantiation: rdppm-12.c:pbm_getc Unexecuted instantiation: rdppm-16.c:pbm_getc |
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 | 0 | { |
99 | 0 | register int ch; |
100 | 0 | register unsigned int val; |
101 | | |
102 | | /* Skip any leading whitespace */ |
103 | 0 | do { |
104 | 0 | ch = pbm_getc(infile); |
105 | 0 | if (ch == EOF) |
106 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
107 | 0 | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
108 | |
|
109 | 0 | if (ch < '0' || ch > '9') |
110 | 0 | ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
111 | |
|
112 | 0 | val = ch - '0'; |
113 | 0 | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
114 | 0 | val *= 10; |
115 | 0 | val += ch - '0'; |
116 | 0 | if (val > maxval) |
117 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
118 | 0 | } |
119 | |
|
120 | 0 | return val; |
121 | 0 | } Unexecuted instantiation: rdppm-8.c:read_pbm_integer Unexecuted instantiation: rdppm-12.c:read_pbm_integer Unexecuted instantiation: rdppm-16.c:read_pbm_integer |
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 cinfo->data_precision. |
129 | | * |
130 | | * A really fast path is provided for reading byte/sample raw files with |
131 | | * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U, |
132 | | * which is the normal case for 8-bit data. |
133 | | */ |
134 | | |
135 | | |
136 | | METHODDEF(JDIMENSION) |
137 | | get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
138 | | /* This version is for reading text-format PGM files with any maxval */ |
139 | 0 | { |
140 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
141 | 0 | FILE *infile = source->pub.input_file; |
142 | 0 | register _JSAMPROW ptr; |
143 | 0 | register _JSAMPLE *rescale = source->rescale; |
144 | 0 | JDIMENSION col; |
145 | 0 | unsigned int maxval = source->maxval; |
146 | |
|
147 | 0 | ptr = source->pub._buffer[0]; |
148 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
149 | 0 | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
150 | 0 | } |
151 | 0 | return 1; |
152 | 0 | } Unexecuted instantiation: rdppm-8.c:get_text_gray_row Unexecuted instantiation: rdppm-12.c:get_text_gray_row Unexecuted instantiation: rdppm-16.c:get_text_gray_row |
153 | | |
154 | | |
155 | 0 | #define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \ |
156 | 0 | for (col = cinfo->image_width; col > 0; col--) { \ |
157 | 0 | ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \ |
158 | 0 | alpha_set_op \ |
159 | 0 | ptr += ps; \ |
160 | 0 | } \ |
161 | 0 | } |
162 | | |
163 | | METHODDEF(JDIMENSION) |
164 | | get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
165 | | /* This version is for reading text-format PGM files with any maxval and |
166 | | converting to extended RGB */ |
167 | 0 | { |
168 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
169 | 0 | FILE *infile = source->pub.input_file; |
170 | 0 | register _JSAMPROW ptr; |
171 | 0 | register _JSAMPLE *rescale = source->rescale; |
172 | 0 | JDIMENSION col; |
173 | 0 | unsigned int maxval = source->maxval; |
174 | 0 | register int rindex = rgb_red[cinfo->in_color_space]; |
175 | 0 | register int gindex = rgb_green[cinfo->in_color_space]; |
176 | 0 | register int bindex = rgb_blue[cinfo->in_color_space]; |
177 | 0 | register int aindex = alpha_index[cinfo->in_color_space]; |
178 | 0 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
179 | |
|
180 | 0 | ptr = source->pub._buffer[0]; |
181 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
182 | 0 | if (aindex >= 0) |
183 | 0 | GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
184 | 0 | ptr[aindex] = (_JSAMPLE)maxval;) |
185 | 0 | else |
186 | 0 | GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
187 | 0 | } else { |
188 | 0 | if (aindex >= 0) |
189 | 0 | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
190 | 0 | ptr[aindex] = (1 << cinfo->data_precision) - 1;) |
191 | 0 | else |
192 | 0 | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
193 | 0 | } |
194 | 0 | return 1; |
195 | 0 | } Unexecuted instantiation: rdppm-8.c:get_text_gray_rgb_row Unexecuted instantiation: rdppm-12.c:get_text_gray_rgb_row Unexecuted instantiation: rdppm-16.c:get_text_gray_rgb_row |
196 | | |
197 | | |
198 | | METHODDEF(JDIMENSION) |
199 | | get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
200 | | /* This version is for reading text-format PGM files with any maxval and |
201 | | converting to CMYK */ |
202 | 0 | { |
203 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
204 | 0 | FILE *infile = source->pub.input_file; |
205 | 0 | register _JSAMPROW ptr; |
206 | 0 | register _JSAMPLE *rescale = source->rescale; |
207 | 0 | JDIMENSION col; |
208 | 0 | unsigned int maxval = source->maxval; |
209 | |
|
210 | 0 | ptr = source->pub._buffer[0]; |
211 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
212 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
213 | 0 | _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
214 | 0 | rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
215 | 0 | ptr += 4; |
216 | 0 | } |
217 | 0 | } else { |
218 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
219 | 0 | _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
220 | 0 | rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr, |
221 | 0 | ptr + 1, ptr + 2, ptr + 3); |
222 | 0 | ptr += 4; |
223 | 0 | } |
224 | 0 | } |
225 | 0 | return 1; |
226 | 0 | } Unexecuted instantiation: rdppm-8.c:get_text_gray_cmyk_row Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row |
227 | | |
228 | | |
229 | 0 | #define RGB_READ_LOOP(read_op, alpha_set_op) { \ |
230 | 0 | for (col = cinfo->image_width; col > 0; col--) { \ |
231 | 0 | ptr[rindex] = read_op; \ |
232 | 0 | ptr[gindex] = read_op; \ |
233 | 0 | ptr[bindex] = read_op; \ |
234 | 0 | alpha_set_op \ |
235 | 0 | ptr += ps; \ |
236 | 0 | } \ |
237 | 0 | } |
238 | | |
239 | | METHODDEF(JDIMENSION) |
240 | | get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
241 | | /* This version is for reading text-format PPM files with any maxval */ |
242 | 0 | { |
243 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
244 | 0 | FILE *infile = source->pub.input_file; |
245 | 0 | register _JSAMPROW ptr; |
246 | 0 | register _JSAMPLE *rescale = source->rescale; |
247 | 0 | JDIMENSION col; |
248 | 0 | unsigned int maxval = source->maxval; |
249 | 0 | register int rindex = rgb_red[cinfo->in_color_space]; |
250 | 0 | register int gindex = rgb_green[cinfo->in_color_space]; |
251 | 0 | register int bindex = rgb_blue[cinfo->in_color_space]; |
252 | 0 | register int aindex = alpha_index[cinfo->in_color_space]; |
253 | 0 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
254 | |
|
255 | 0 | ptr = source->pub._buffer[0]; |
256 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
257 | 0 | if (aindex >= 0) |
258 | 0 | RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
259 | 0 | ptr[aindex] = (_JSAMPLE)maxval;) |
260 | 0 | else |
261 | 0 | RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
262 | 0 | } else { |
263 | 0 | if (aindex >= 0) |
264 | 0 | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
265 | 0 | ptr[aindex] = (1 << cinfo->data_precision) - 1;) |
266 | 0 | else |
267 | 0 | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
268 | 0 | } |
269 | 0 | return 1; |
270 | 0 | } Unexecuted instantiation: rdppm-8.c:get_text_rgb_row Unexecuted instantiation: rdppm-12.c:get_text_rgb_row Unexecuted instantiation: rdppm-16.c:get_text_rgb_row |
271 | | |
272 | | |
273 | | METHODDEF(JDIMENSION) |
274 | | get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
275 | | /* This version is for reading text-format PPM files with any maxval and |
276 | | converting to CMYK */ |
277 | 0 | { |
278 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
279 | 0 | FILE *infile = source->pub.input_file; |
280 | 0 | register _JSAMPROW ptr; |
281 | 0 | register _JSAMPLE *rescale = source->rescale; |
282 | 0 | JDIMENSION col; |
283 | 0 | unsigned int maxval = source->maxval; |
284 | |
|
285 | 0 | ptr = source->pub._buffer[0]; |
286 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
287 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
288 | 0 | _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
289 | 0 | _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
290 | 0 | _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
291 | 0 | rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
292 | 0 | ptr += 4; |
293 | 0 | } |
294 | 0 | } else { |
295 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
296 | 0 | _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
297 | 0 | _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
298 | 0 | _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
299 | 0 | rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1, |
300 | 0 | ptr + 2, ptr + 3); |
301 | 0 | ptr += 4; |
302 | 0 | } |
303 | 0 | } |
304 | 0 | return 1; |
305 | 0 | } Unexecuted instantiation: rdppm-8.c:get_text_rgb_cmyk_row Unexecuted instantiation: rdppm-12.c:get_text_rgb_cmyk_row Unexecuted instantiation: rdppm-16.c:get_text_rgb_cmyk_row |
306 | | |
307 | | |
308 | | METHODDEF(JDIMENSION) |
309 | | get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
310 | | /* This version is for reading raw-byte-format PGM files with any maxval */ |
311 | 0 | { |
312 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
313 | 0 | register _JSAMPROW ptr; |
314 | 0 | register U_CHAR *bufferptr; |
315 | 0 | register _JSAMPLE *rescale = source->rescale; |
316 | 0 | JDIMENSION col; |
317 | |
|
318 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
319 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
320 | 0 | ptr = source->pub._buffer[0]; |
321 | 0 | bufferptr = source->iobuffer; |
322 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
323 | 0 | *ptr++ = rescale[UCH(*bufferptr++)]; |
324 | 0 | } |
325 | 0 | return 1; |
326 | 0 | } Unexecuted instantiation: rdppm-8.c:get_scaled_gray_row Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row Unexecuted instantiation: rdppm-16.c:get_scaled_gray_row |
327 | | |
328 | | |
329 | | METHODDEF(JDIMENSION) |
330 | | get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
331 | | /* This version is for reading raw-byte-format PGM files with any maxval |
332 | | and converting to extended RGB */ |
333 | 0 | { |
334 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
335 | 0 | register _JSAMPROW ptr; |
336 | 0 | register U_CHAR *bufferptr; |
337 | 0 | register _JSAMPLE *rescale = source->rescale; |
338 | 0 | JDIMENSION col; |
339 | 0 | unsigned int maxval = source->maxval; |
340 | 0 | register int rindex = rgb_red[cinfo->in_color_space]; |
341 | 0 | register int gindex = rgb_green[cinfo->in_color_space]; |
342 | 0 | register int bindex = rgb_blue[cinfo->in_color_space]; |
343 | 0 | register int aindex = alpha_index[cinfo->in_color_space]; |
344 | 0 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
345 | |
|
346 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
347 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
348 | 0 | ptr = source->pub._buffer[0]; |
349 | 0 | bufferptr = source->iobuffer; |
350 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
351 | 0 | if (aindex >= 0) |
352 | 0 | GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;) |
353 | 0 | else |
354 | 0 | GRAY_RGB_READ_LOOP(*bufferptr++, {}) |
355 | 0 | } else { |
356 | 0 | if (aindex >= 0) |
357 | 0 | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], |
358 | 0 | ptr[aindex] = (1 << cinfo->data_precision) - 1;) |
359 | 0 | else |
360 | 0 | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
361 | 0 | } |
362 | 0 | return 1; |
363 | 0 | } Unexecuted instantiation: rdppm-8.c:get_gray_rgb_row Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row Unexecuted instantiation: rdppm-16.c:get_gray_rgb_row |
364 | | |
365 | | |
366 | | METHODDEF(JDIMENSION) |
367 | | get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
368 | | /* This version is for reading raw-byte-format PGM files with any maxval |
369 | | and converting to CMYK */ |
370 | 0 | { |
371 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
372 | 0 | register _JSAMPROW ptr; |
373 | 0 | register U_CHAR *bufferptr; |
374 | 0 | register _JSAMPLE *rescale = source->rescale; |
375 | 0 | JDIMENSION col; |
376 | 0 | unsigned int maxval = source->maxval; |
377 | |
|
378 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
379 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
380 | 0 | ptr = source->pub._buffer[0]; |
381 | 0 | bufferptr = source->iobuffer; |
382 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
383 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
384 | 0 | _JSAMPLE gray = *bufferptr++; |
385 | 0 | rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
386 | 0 | ptr += 4; |
387 | 0 | } |
388 | 0 | } else { |
389 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
390 | 0 | _JSAMPLE gray = rescale[UCH(*bufferptr++)]; |
391 | 0 | rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr, |
392 | 0 | ptr + 1, ptr + 2, ptr + 3); |
393 | 0 | ptr += 4; |
394 | 0 | } |
395 | 0 | } |
396 | 0 | return 1; |
397 | 0 | } Unexecuted instantiation: rdppm-8.c:get_gray_cmyk_row Unexecuted instantiation: rdppm-12.c:get_gray_cmyk_row Unexecuted instantiation: rdppm-16.c:get_gray_cmyk_row |
398 | | |
399 | | |
400 | | METHODDEF(JDIMENSION) |
401 | | get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
402 | | /* This version is for reading raw-byte-format PPM files with any maxval */ |
403 | 0 | { |
404 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
405 | 0 | register _JSAMPROW ptr; |
406 | 0 | register U_CHAR *bufferptr; |
407 | 0 | register _JSAMPLE *rescale = source->rescale; |
408 | 0 | JDIMENSION col; |
409 | 0 | unsigned int maxval = source->maxval; |
410 | 0 | register int rindex = rgb_red[cinfo->in_color_space]; |
411 | 0 | register int gindex = rgb_green[cinfo->in_color_space]; |
412 | 0 | register int bindex = rgb_blue[cinfo->in_color_space]; |
413 | 0 | register int aindex = alpha_index[cinfo->in_color_space]; |
414 | 0 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
415 | |
|
416 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
417 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
418 | 0 | ptr = source->pub._buffer[0]; |
419 | 0 | bufferptr = source->iobuffer; |
420 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
421 | 0 | if (aindex >= 0) |
422 | 0 | RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;) |
423 | 0 | else |
424 | 0 | RGB_READ_LOOP(*bufferptr++, {}) |
425 | 0 | } else { |
426 | 0 | if (aindex >= 0) |
427 | 0 | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], |
428 | 0 | ptr[aindex] = (1 << cinfo->data_precision) - 1;) |
429 | 0 | else |
430 | 0 | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
431 | 0 | } |
432 | 0 | return 1; |
433 | 0 | } Unexecuted instantiation: rdppm-8.c:get_rgb_row Unexecuted instantiation: rdppm-12.c:get_rgb_row Unexecuted instantiation: rdppm-16.c:get_rgb_row |
434 | | |
435 | | |
436 | | METHODDEF(JDIMENSION) |
437 | | get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
438 | | /* This version is for reading raw-byte-format PPM files with any maxval and |
439 | | converting to CMYK */ |
440 | 0 | { |
441 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
442 | 0 | register _JSAMPROW ptr; |
443 | 0 | register U_CHAR *bufferptr; |
444 | 0 | register _JSAMPLE *rescale = source->rescale; |
445 | 0 | JDIMENSION col; |
446 | 0 | unsigned int maxval = source->maxval; |
447 | |
|
448 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
449 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
450 | 0 | ptr = source->pub._buffer[0]; |
451 | 0 | bufferptr = source->iobuffer; |
452 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
453 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
454 | 0 | _JSAMPLE r = *bufferptr++; |
455 | 0 | _JSAMPLE g = *bufferptr++; |
456 | 0 | _JSAMPLE b = *bufferptr++; |
457 | 0 | rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
458 | 0 | ptr += 4; |
459 | 0 | } |
460 | 0 | } else { |
461 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
462 | 0 | _JSAMPLE r = rescale[UCH(*bufferptr++)]; |
463 | 0 | _JSAMPLE g = rescale[UCH(*bufferptr++)]; |
464 | 0 | _JSAMPLE b = rescale[UCH(*bufferptr++)]; |
465 | 0 | rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1, |
466 | 0 | ptr + 2, ptr + 3); |
467 | 0 | ptr += 4; |
468 | 0 | } |
469 | 0 | } |
470 | 0 | return 1; |
471 | 0 | } Unexecuted instantiation: rdppm-8.c:get_rgb_cmyk_row Unexecuted instantiation: rdppm-12.c:get_rgb_cmyk_row Unexecuted instantiation: rdppm-16.c:get_rgb_cmyk_row |
472 | | |
473 | | |
474 | | METHODDEF(JDIMENSION) |
475 | | get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
476 | | /* This version is for reading raw-byte-format files with |
477 | | * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U. |
478 | | * In this case we just read right into the _JSAMPLE buffer! |
479 | | * Note that same code works for PPM and PGM files. |
480 | | */ |
481 | 0 | { |
482 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
483 | |
|
484 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
485 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
486 | 0 | return 1; |
487 | 0 | } Unexecuted instantiation: rdppm-8.c:get_raw_row Unexecuted instantiation: rdppm-12.c:get_raw_row Unexecuted instantiation: rdppm-16.c:get_raw_row |
488 | | |
489 | | |
490 | | METHODDEF(JDIMENSION) |
491 | | get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
492 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
493 | 0 | { |
494 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
495 | 0 | register _JSAMPROW ptr; |
496 | 0 | register U_CHAR *bufferptr; |
497 | 0 | register _JSAMPLE *rescale = source->rescale; |
498 | 0 | JDIMENSION col; |
499 | 0 | unsigned int maxval = source->maxval; |
500 | |
|
501 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
502 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
503 | 0 | ptr = source->pub._buffer[0]; |
504 | 0 | bufferptr = source->iobuffer; |
505 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
506 | 0 | register unsigned int temp; |
507 | 0 | temp = UCH(*bufferptr++) << 8; |
508 | 0 | temp |= UCH(*bufferptr++); |
509 | 0 | if (temp > maxval) |
510 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
511 | 0 | *ptr++ = rescale[temp]; |
512 | 0 | } |
513 | 0 | return 1; |
514 | 0 | } Unexecuted instantiation: rdppm-8.c:get_word_gray_row Unexecuted instantiation: rdppm-12.c:get_word_gray_row Unexecuted instantiation: rdppm-16.c:get_word_gray_row |
515 | | |
516 | | |
517 | | METHODDEF(JDIMENSION) |
518 | | get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
519 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
520 | 0 | { |
521 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
522 | 0 | register _JSAMPROW ptr; |
523 | 0 | register U_CHAR *bufferptr; |
524 | 0 | register _JSAMPLE *rescale = source->rescale; |
525 | 0 | JDIMENSION col; |
526 | 0 | unsigned int maxval = source->maxval; |
527 | 0 | register int rindex = rgb_red[cinfo->in_color_space]; |
528 | 0 | register int gindex = rgb_green[cinfo->in_color_space]; |
529 | 0 | register int bindex = rgb_blue[cinfo->in_color_space]; |
530 | 0 | register int aindex = alpha_index[cinfo->in_color_space]; |
531 | 0 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
532 | |
|
533 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
534 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
535 | 0 | ptr = source->pub._buffer[0]; |
536 | 0 | bufferptr = source->iobuffer; |
537 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
538 | 0 | register unsigned int temp; |
539 | 0 | temp = UCH(*bufferptr++) << 8; |
540 | 0 | temp |= UCH(*bufferptr++); |
541 | 0 | if (temp > maxval) |
542 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
543 | 0 | ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp]; |
544 | 0 | if (aindex >= 0) |
545 | 0 | ptr[aindex] = (1 << cinfo->data_precision) - 1; |
546 | 0 | ptr += ps; |
547 | 0 | } |
548 | 0 | return 1; |
549 | 0 | } Unexecuted instantiation: rdppm-8.c:get_word_gray_rgb_row Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row Unexecuted instantiation: rdppm-16.c:get_word_gray_rgb_row |
550 | | |
551 | | |
552 | | METHODDEF(JDIMENSION) |
553 | | get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
554 | | /* This version is for reading raw-word-format PGM files with any maxval */ |
555 | 0 | { |
556 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
557 | 0 | register _JSAMPROW ptr; |
558 | 0 | register U_CHAR *bufferptr; |
559 | 0 | register _JSAMPLE *rescale = source->rescale; |
560 | 0 | JDIMENSION col; |
561 | 0 | unsigned int maxval = source->maxval; |
562 | |
|
563 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
564 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
565 | 0 | ptr = source->pub._buffer[0]; |
566 | 0 | bufferptr = source->iobuffer; |
567 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
568 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
569 | 0 | register unsigned int gray; |
570 | 0 | gray = UCH(*bufferptr++) << 8; |
571 | 0 | gray |= UCH(*bufferptr++); |
572 | 0 | if (gray > maxval) |
573 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
574 | 0 | rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr, |
575 | 0 | ptr + 1, ptr + 2, ptr + 3); |
576 | 0 | ptr += 4; |
577 | 0 | } |
578 | 0 | } else { |
579 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
580 | 0 | register unsigned int temp; |
581 | 0 | _JSAMPLE gray; |
582 | 0 | temp = UCH(*bufferptr++) << 8; |
583 | 0 | temp |= UCH(*bufferptr++); |
584 | 0 | if (temp > maxval) |
585 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
586 | 0 | gray = rescale[temp]; |
587 | 0 | rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr, |
588 | 0 | ptr + 1, ptr + 2, ptr + 3); |
589 | 0 | ptr += 4; |
590 | 0 | } |
591 | 0 | } |
592 | 0 | return 1; |
593 | 0 | } Unexecuted instantiation: rdppm-8.c:get_word_gray_cmyk_row Unexecuted instantiation: rdppm-12.c:get_word_gray_cmyk_row Unexecuted instantiation: rdppm-16.c:get_word_gray_cmyk_row |
594 | | |
595 | | |
596 | | METHODDEF(JDIMENSION) |
597 | | get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
598 | | /* This version is for reading raw-word-format PPM files with any maxval */ |
599 | 0 | { |
600 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
601 | 0 | register _JSAMPROW ptr; |
602 | 0 | register U_CHAR *bufferptr; |
603 | 0 | register _JSAMPLE *rescale = source->rescale; |
604 | 0 | JDIMENSION col; |
605 | 0 | unsigned int maxval = source->maxval; |
606 | 0 | register int rindex = rgb_red[cinfo->in_color_space]; |
607 | 0 | register int gindex = rgb_green[cinfo->in_color_space]; |
608 | 0 | register int bindex = rgb_blue[cinfo->in_color_space]; |
609 | 0 | register int aindex = alpha_index[cinfo->in_color_space]; |
610 | 0 | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
611 | |
|
612 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
613 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
614 | 0 | ptr = source->pub._buffer[0]; |
615 | 0 | bufferptr = source->iobuffer; |
616 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
617 | 0 | register unsigned int temp; |
618 | 0 | temp = UCH(*bufferptr++) << 8; |
619 | 0 | temp |= UCH(*bufferptr++); |
620 | 0 | if (temp > maxval) |
621 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
622 | 0 | ptr[rindex] = rescale[temp]; |
623 | 0 | temp = UCH(*bufferptr++) << 8; |
624 | 0 | temp |= UCH(*bufferptr++); |
625 | 0 | if (temp > maxval) |
626 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
627 | 0 | ptr[gindex] = rescale[temp]; |
628 | 0 | temp = UCH(*bufferptr++) << 8; |
629 | 0 | temp |= UCH(*bufferptr++); |
630 | 0 | if (temp > maxval) |
631 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
632 | 0 | ptr[bindex] = rescale[temp]; |
633 | 0 | if (aindex >= 0) |
634 | 0 | ptr[aindex] = (1 << cinfo->data_precision) - 1; |
635 | 0 | ptr += ps; |
636 | 0 | } |
637 | 0 | return 1; |
638 | 0 | } Unexecuted instantiation: rdppm-8.c:get_word_rgb_row Unexecuted instantiation: rdppm-12.c:get_word_rgb_row Unexecuted instantiation: rdppm-16.c:get_word_rgb_row |
639 | | |
640 | | |
641 | | METHODDEF(JDIMENSION) |
642 | | get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
643 | | /* This version is for reading raw-word-format PPM files with any maxval */ |
644 | 0 | { |
645 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
646 | 0 | register _JSAMPROW ptr; |
647 | 0 | register U_CHAR *bufferptr; |
648 | 0 | register _JSAMPLE *rescale = source->rescale; |
649 | 0 | JDIMENSION col; |
650 | 0 | unsigned int maxval = source->maxval; |
651 | |
|
652 | 0 | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
653 | 0 | ERREXIT(cinfo, JERR_INPUT_EOF); |
654 | 0 | ptr = source->pub._buffer[0]; |
655 | 0 | bufferptr = source->iobuffer; |
656 | 0 | if (maxval == (1U << cinfo->data_precision) - 1U) { |
657 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
658 | 0 | register unsigned int r, g, b; |
659 | 0 | r = UCH(*bufferptr++) << 8; |
660 | 0 | r |= UCH(*bufferptr++); |
661 | 0 | if (r > maxval) |
662 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
663 | 0 | g = UCH(*bufferptr++) << 8; |
664 | 0 | g |= UCH(*bufferptr++); |
665 | 0 | if (g > maxval) |
666 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
667 | 0 | b = UCH(*bufferptr++) << 8; |
668 | 0 | b |= UCH(*bufferptr++); |
669 | 0 | if (b > maxval) |
670 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
671 | 0 | rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1, |
672 | 0 | ptr + 2, ptr + 3); |
673 | 0 | ptr += 4; |
674 | 0 | } |
675 | 0 | } else { |
676 | 0 | for (col = cinfo->image_width; col > 0; col--) { |
677 | 0 | register unsigned int r, g, b; |
678 | 0 | r = UCH(*bufferptr++) << 8; |
679 | 0 | r |= UCH(*bufferptr++); |
680 | 0 | if (r > maxval) |
681 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
682 | 0 | g = UCH(*bufferptr++) << 8; |
683 | 0 | g |= UCH(*bufferptr++); |
684 | 0 | if (g > maxval) |
685 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
686 | 0 | b = UCH(*bufferptr++) << 8; |
687 | 0 | b |= UCH(*bufferptr++); |
688 | 0 | if (b > maxval) |
689 | 0 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
690 | 0 | rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g], |
691 | 0 | rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3); |
692 | 0 | ptr += 4; |
693 | 0 | } |
694 | 0 | } |
695 | 0 | return 1; |
696 | 0 | } Unexecuted instantiation: rdppm-8.c:get_word_rgb_cmyk_row Unexecuted instantiation: rdppm-12.c:get_word_rgb_cmyk_row Unexecuted instantiation: rdppm-16.c:get_word_rgb_cmyk_row |
697 | | |
698 | | |
699 | | /* |
700 | | * Read the file header; return image size and component count. |
701 | | */ |
702 | | |
703 | | METHODDEF(void) |
704 | | start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
705 | 0 | { |
706 | 0 | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
707 | 0 | int c; |
708 | 0 | unsigned int w, h, maxval; |
709 | 0 | boolean need_iobuffer, use_raw_buffer, need_rescale; |
710 | |
|
711 | 0 | if (getc(source->pub.input_file) != 'P') |
712 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
713 | |
|
714 | 0 | c = getc(source->pub.input_file); /* subformat discriminator character */ |
715 | | |
716 | | /* detect unsupported variants (ie, PBM) before trying to read header */ |
717 | 0 | switch (c) { |
718 | 0 | case '2': /* it's a text-format PGM file */ |
719 | 0 | case '3': /* it's a text-format PPM file */ |
720 | 0 | case '5': /* it's a raw-format PGM file */ |
721 | 0 | case '6': /* it's a raw-format PPM file */ |
722 | 0 | break; |
723 | 0 | default: |
724 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
725 | 0 | break; |
726 | 0 | } |
727 | | |
728 | | /* fetch the remaining header info */ |
729 | 0 | w = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
730 | 0 | h = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
731 | 0 | maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
732 | |
|
733 | 0 | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
734 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
735 | 0 | if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels) |
736 | 0 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels); |
737 | |
|
738 | 0 | cinfo->image_width = (JDIMENSION)w; |
739 | 0 | cinfo->image_height = (JDIMENSION)h; |
740 | 0 | source->maxval = maxval; |
741 | | |
742 | | /* initialize flags to most common settings */ |
743 | 0 | need_iobuffer = TRUE; /* do we need an I/O buffer? */ |
744 | 0 | use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ |
745 | 0 | need_rescale = TRUE; /* do we need a rescale array? */ |
746 | |
|
747 | 0 | switch (c) { |
748 | 0 | case '2': /* it's a text-format PGM file */ |
749 | 0 | if (cinfo->in_color_space == JCS_UNKNOWN || |
750 | 0 | cinfo->in_color_space == JCS_RGB) |
751 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
752 | 0 | TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval); |
753 | 0 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
754 | 0 | source->pub.get_pixel_rows = get_text_gray_row; |
755 | 0 | else if (IsExtRGB(cinfo->in_color_space)) |
756 | 0 | source->pub.get_pixel_rows = get_text_gray_rgb_row; |
757 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
758 | 0 | source->pub.get_pixel_rows = get_text_gray_cmyk_row; |
759 | 0 | else |
760 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
761 | 0 | need_iobuffer = FALSE; |
762 | 0 | break; |
763 | | |
764 | 0 | case '3': /* it's a text-format PPM file */ |
765 | 0 | if (cinfo->in_color_space == JCS_UNKNOWN) |
766 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
767 | 0 | TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval); |
768 | 0 | if (IsExtRGB(cinfo->in_color_space)) |
769 | 0 | source->pub.get_pixel_rows = get_text_rgb_row; |
770 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
771 | 0 | source->pub.get_pixel_rows = get_text_rgb_cmyk_row; |
772 | 0 | else |
773 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
774 | 0 | need_iobuffer = FALSE; |
775 | 0 | break; |
776 | | |
777 | 0 | case '5': /* it's a raw-format PGM file */ |
778 | 0 | if (cinfo->in_color_space == JCS_UNKNOWN || |
779 | 0 | cinfo->in_color_space == JCS_RGB) |
780 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
781 | 0 | TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval); |
782 | 0 | if (maxval > 255) { |
783 | 0 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
784 | 0 | source->pub.get_pixel_rows = get_word_gray_row; |
785 | 0 | else if (IsExtRGB(cinfo->in_color_space)) |
786 | 0 | source->pub.get_pixel_rows = get_word_gray_rgb_row; |
787 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
788 | 0 | source->pub.get_pixel_rows = get_word_gray_cmyk_row; |
789 | 0 | else |
790 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
791 | 0 | } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) && |
792 | 0 | maxval == ((1U << cinfo->data_precision) - 1U) && |
793 | 0 | cinfo->in_color_space == JCS_GRAYSCALE) { |
794 | 0 | source->pub.get_pixel_rows = get_raw_row; |
795 | 0 | use_raw_buffer = TRUE; |
796 | 0 | need_rescale = FALSE; |
797 | 0 | } else { |
798 | 0 | if (cinfo->in_color_space == JCS_GRAYSCALE) |
799 | 0 | source->pub.get_pixel_rows = get_scaled_gray_row; |
800 | 0 | else if (IsExtRGB(cinfo->in_color_space)) |
801 | 0 | source->pub.get_pixel_rows = get_gray_rgb_row; |
802 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
803 | 0 | source->pub.get_pixel_rows = get_gray_cmyk_row; |
804 | 0 | else |
805 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
806 | 0 | } |
807 | 0 | break; |
808 | | |
809 | 0 | case '6': /* it's a raw-format PPM file */ |
810 | 0 | if (cinfo->in_color_space == JCS_UNKNOWN) |
811 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
812 | 0 | TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval); |
813 | 0 | if (maxval > 255) { |
814 | 0 | if (IsExtRGB(cinfo->in_color_space)) |
815 | 0 | source->pub.get_pixel_rows = get_word_rgb_row; |
816 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
817 | 0 | source->pub.get_pixel_rows = get_word_rgb_cmyk_row; |
818 | 0 | else |
819 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
820 | 0 | } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) && |
821 | 0 | maxval == ((1U << cinfo->data_precision) - 1U) && |
822 | 0 | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
823 | 0 | (cinfo->in_color_space == JCS_EXT_RGB || |
824 | 0 | cinfo->in_color_space == JCS_RGB)) { |
825 | | #else |
826 | | cinfo->in_color_space == JCS_EXT_RGB) { |
827 | | #endif |
828 | 0 | source->pub.get_pixel_rows = get_raw_row; |
829 | 0 | use_raw_buffer = TRUE; |
830 | 0 | need_rescale = FALSE; |
831 | 0 | } else { |
832 | 0 | if (IsExtRGB(cinfo->in_color_space)) |
833 | 0 | source->pub.get_pixel_rows = get_rgb_row; |
834 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
835 | 0 | source->pub.get_pixel_rows = get_rgb_cmyk_row; |
836 | 0 | else |
837 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
838 | 0 | } |
839 | 0 | break; |
840 | 0 | } |
841 | | |
842 | 0 | if (IsExtRGB(cinfo->in_color_space)) |
843 | 0 | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
844 | 0 | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
845 | 0 | cinfo->input_components = 1; |
846 | 0 | else if (cinfo->in_color_space == JCS_CMYK) |
847 | 0 | cinfo->input_components = 4; |
848 | | |
849 | | /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ |
850 | 0 | if (need_iobuffer) { |
851 | 0 | if (c == '6') |
852 | 0 | source->buffer_width = (size_t)w * 3 * |
853 | 0 | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
854 | 0 | else |
855 | 0 | source->buffer_width = (size_t)w * |
856 | 0 | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
857 | 0 | source->iobuffer = (U_CHAR *) |
858 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
859 | 0 | source->buffer_width); |
860 | 0 | } |
861 | | |
862 | | /* Create compressor input buffer. */ |
863 | 0 | if (use_raw_buffer) { |
864 | | /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ |
865 | | /* Synthesize a _JSAMPARRAY pointer structure */ |
866 | 0 | source->pixrow = (_JSAMPROW)source->iobuffer; |
867 | 0 | source->pub._buffer = &source->pixrow; |
868 | 0 | source->pub.buffer_height = 1; |
869 | 0 | } else { |
870 | | /* Need to translate anyway, so make a separate sample buffer. */ |
871 | 0 | source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray) |
872 | 0 | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
873 | 0 | (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1); |
874 | 0 | source->pub.buffer_height = 1; |
875 | 0 | } |
876 | | |
877 | | /* Compute the rescaling array if required. */ |
878 | 0 | if (need_rescale) { |
879 | 0 | long val, half_maxval; |
880 | | |
881 | | /* On 16-bit-int machines we have to be careful of maxval = 65535 */ |
882 | 0 | source->rescale = (_JSAMPLE *) |
883 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
884 | 0 | (size_t)(((long)MAX(maxval, 255) + 1L) * |
885 | 0 | sizeof(_JSAMPLE))); |
886 | 0 | memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) * |
887 | 0 | sizeof(_JSAMPLE))); |
888 | 0 | half_maxval = maxval / 2; |
889 | 0 | for (val = 0; val <= (long)maxval; val++) { |
890 | | /* The multiplication here must be done in 32 bits to avoid overflow */ |
891 | 0 | source->rescale[val] = |
892 | 0 | (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) / |
893 | 0 | maxval); |
894 | 0 | } |
895 | 0 | } |
896 | 0 | } Unexecuted instantiation: rdppm-8.c:start_input_ppm Unexecuted instantiation: rdppm-12.c:start_input_ppm Unexecuted instantiation: rdppm-16.c:start_input_ppm |
897 | | |
898 | | |
899 | | /* |
900 | | * Finish up at the end of the file. |
901 | | */ |
902 | | |
903 | | METHODDEF(void) |
904 | | finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
905 | 0 | { |
906 | | /* no work */ |
907 | 0 | } Unexecuted instantiation: rdppm-8.c:finish_input_ppm Unexecuted instantiation: rdppm-12.c:finish_input_ppm Unexecuted instantiation: rdppm-16.c:finish_input_ppm |
908 | | |
909 | | |
910 | | /* |
911 | | * The module selection routine for PPM format input. |
912 | | */ |
913 | | |
914 | | GLOBAL(cjpeg_source_ptr) |
915 | | _jinit_read_ppm(j_compress_ptr cinfo) |
916 | 0 | { |
917 | 0 | ppm_source_ptr source; |
918 | |
|
919 | | #if BITS_IN_JSAMPLE == 8 |
920 | 0 | if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2) |
921 | | #else |
922 | 0 | if (cinfo->data_precision > BITS_IN_JSAMPLE || |
923 | 0 | cinfo->data_precision < BITS_IN_JSAMPLE - 3) |
924 | 0 | #endif |
925 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
926 | | |
927 | | /* Create module interface object */ |
928 | 0 | source = (ppm_source_ptr) |
929 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
930 | 0 | sizeof(ppm_source_struct)); |
931 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
932 | 0 | source->pub.start_input = start_input_ppm; |
933 | 0 | source->pub.finish_input = finish_input_ppm; |
934 | 0 | source->pub.max_pixels = 0; |
935 | |
|
936 | 0 | return (cjpeg_source_ptr)source; |
937 | 0 | } Unexecuted instantiation: jinit_read_ppm Unexecuted instantiation: j12init_read_ppm Unexecuted instantiation: j16init_read_ppm |
938 | | |
939 | | #endif /* defined(PPM_SUPPORTED) && |
940 | | (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */ |