/src/libjpeg-turbo.3.0.x/rdppm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * rdppm.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1997, Thomas G. Lane. |
6 | | * Modified 2009 by Bill Allombert, Guido Vollbeding. |
7 | | * libjpeg-turbo Modifications: |
8 | | * Copyright (C) 2015-2017, 2020-2024, 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 | 157M | #define UCH(x) ((int)(x)) |
49 | | |
50 | | |
51 | | #define ReadOK(file, buffer, len) \ |
52 | 202M | (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 | 2.05M | { |
80 | 2.05M | register int ch; |
81 | | |
82 | 2.05M | ch = getc(infile); |
83 | 2.05M | if (ch == '#') { |
84 | 377k | do { |
85 | 377k | ch = getc(infile); |
86 | 377k | } while (ch != '\n' && ch != EOF); |
87 | 22.5k | } |
88 | 2.05M | return ch; |
89 | 2.05M | } |
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 | 821k | { |
99 | 821k | register int ch; |
100 | 821k | register unsigned int val; |
101 | | |
102 | | /* Skip any leading whitespace */ |
103 | 880k | do { |
104 | 880k | ch = pbm_getc(infile); |
105 | 880k | if (ch == EOF) |
106 | 14.8k | ERREXIT(cinfo, JERR_INPUT_EOF); |
107 | 880k | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
108 | | |
109 | 821k | if (ch < '0' || ch > '9') |
110 | 1.34k | ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
111 | | |
112 | 821k | val = ch - '0'; |
113 | 1.19M | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
114 | 370k | val *= 10; |
115 | 370k | val += ch - '0'; |
116 | 370k | if (val > maxval) |
117 | 759 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
118 | 370k | } |
119 | | |
120 | 821k | return val; |
121 | 821k | } |
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 | 10.3k | { |
139 | 10.3k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
140 | 10.3k | FILE *infile = source->pub.input_file; |
141 | 10.3k | register _JSAMPROW ptr; |
142 | 10.3k | register _JSAMPLE *rescale = source->rescale; |
143 | 10.3k | JDIMENSION col; |
144 | 10.3k | unsigned int maxval = source->maxval; |
145 | | |
146 | 10.3k | ptr = source->pub._buffer[0]; |
147 | 26.9k | for (col = cinfo->image_width; col > 0; col--) { |
148 | 16.5k | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
149 | 16.5k | } |
150 | 10.3k | return 1; |
151 | 10.3k | } |
152 | | |
153 | | |
154 | 145M | #define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \ |
155 | 521M | for (col = cinfo->image_width; col > 0; col--) { \ |
156 | 376M | ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \ |
157 | 376M | alpha_set_op \ |
158 | 376M | ptr += ps; \ |
159 | 376M | } \ |
160 | 145M | } |
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 | 51.6k | { |
167 | 51.6k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
168 | 51.6k | FILE *infile = source->pub.input_file; |
169 | 51.6k | register _JSAMPROW ptr; |
170 | 51.6k | register _JSAMPLE *rescale = source->rescale; |
171 | 51.6k | JDIMENSION col; |
172 | 51.6k | unsigned int maxval = source->maxval; |
173 | 51.6k | register int rindex = rgb_red[cinfo->in_color_space]; |
174 | 51.6k | register int gindex = rgb_green[cinfo->in_color_space]; |
175 | 51.6k | register int bindex = rgb_blue[cinfo->in_color_space]; |
176 | 51.6k | register int aindex = alpha_index[cinfo->in_color_space]; |
177 | 51.6k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
178 | | |
179 | 51.6k | ptr = source->pub._buffer[0]; |
180 | 51.6k | if (maxval == _MAXJSAMPLE) { |
181 | 12.3k | if (aindex >= 0) |
182 | 1.79k | GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
183 | 12.3k | ptr[aindex] = _MAXJSAMPLE;) |
184 | 10.6k | else |
185 | 10.6k | GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
186 | 39.2k | } else { |
187 | 39.2k | if (aindex >= 0) |
188 | 6.55k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
189 | 39.2k | ptr[aindex] = _MAXJSAMPLE;) |
190 | 32.7k | else |
191 | 32.7k | GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
192 | 39.2k | } |
193 | 51.6k | return 1; |
194 | 51.6k | } |
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 | 8.34k | { |
202 | 8.34k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
203 | 8.34k | FILE *infile = source->pub.input_file; |
204 | 8.34k | register _JSAMPROW ptr; |
205 | 8.34k | register _JSAMPLE *rescale = source->rescale; |
206 | 8.34k | JDIMENSION col; |
207 | 8.34k | unsigned int maxval = source->maxval; |
208 | | |
209 | 8.34k | ptr = source->pub._buffer[0]; |
210 | 8.34k | if (maxval == _MAXJSAMPLE) { |
211 | 5.20k | for (col = cinfo->image_width; col > 0; col--) { |
212 | 3.41k | _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
213 | 3.41k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
214 | 3.41k | ptr += 4; |
215 | 3.41k | } |
216 | 6.55k | } else { |
217 | 16.3k | for (col = cinfo->image_width; col > 0; col--) { |
218 | 9.81k | _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
219 | 9.81k | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
220 | 9.81k | ptr += 4; |
221 | 9.81k | } |
222 | 6.55k | } |
223 | 8.34k | return 1; |
224 | 8.34k | } |
225 | | |
226 | | |
227 | 1.91M | #define RGB_READ_LOOP(read_op, alpha_set_op) { \ |
228 | 26.0M | for (col = cinfo->image_width; col > 0; col--) { \ |
229 | 24.0M | ptr[rindex] = read_op; \ |
230 | 24.0M | ptr[gindex] = read_op; \ |
231 | 24.0M | ptr[bindex] = read_op; \ |
232 | 24.0M | alpha_set_op \ |
233 | 24.0M | ptr += ps; \ |
234 | 24.0M | } \ |
235 | 1.91M | } |
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 | 51.3k | { |
241 | 51.3k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
242 | 51.3k | FILE *infile = source->pub.input_file; |
243 | 51.3k | register _JSAMPROW ptr; |
244 | 51.3k | register _JSAMPLE *rescale = source->rescale; |
245 | 51.3k | JDIMENSION col; |
246 | 51.3k | unsigned int maxval = source->maxval; |
247 | 51.3k | register int rindex = rgb_red[cinfo->in_color_space]; |
248 | 51.3k | register int gindex = rgb_green[cinfo->in_color_space]; |
249 | 51.3k | register int bindex = rgb_blue[cinfo->in_color_space]; |
250 | 51.3k | register int aindex = alpha_index[cinfo->in_color_space]; |
251 | 51.3k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
252 | | |
253 | 51.3k | ptr = source->pub._buffer[0]; |
254 | 51.3k | if (maxval == _MAXJSAMPLE) { |
255 | 13.5k | if (aindex >= 0) |
256 | 2.14k | RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), |
257 | 13.5k | ptr[aindex] = _MAXJSAMPLE;) |
258 | 11.3k | else |
259 | 11.3k | RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {}) |
260 | 37.7k | } else { |
261 | 37.7k | if (aindex >= 0) |
262 | 6.28k | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], |
263 | 37.7k | ptr[aindex] = _MAXJSAMPLE;) |
264 | 31.5k | else |
265 | 31.5k | RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {}) |
266 | 37.7k | } |
267 | 51.3k | return 1; |
268 | 51.3k | } |
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 | 8.42k | { |
276 | 8.42k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
277 | 8.42k | FILE *infile = source->pub.input_file; |
278 | 8.42k | register _JSAMPROW ptr; |
279 | 8.42k | register _JSAMPLE *rescale = source->rescale; |
280 | 8.42k | JDIMENSION col; |
281 | 8.42k | unsigned int maxval = source->maxval; |
282 | | |
283 | 8.42k | ptr = source->pub._buffer[0]; |
284 | 8.42k | if (maxval == _MAXJSAMPLE) { |
285 | 13.5k | for (col = cinfo->image_width; col > 0; col--) { |
286 | 11.4k | _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
287 | 11.4k | _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
288 | 11.4k | _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval); |
289 | 11.4k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
290 | 11.4k | ptr += 4; |
291 | 11.4k | } |
292 | 6.28k | } else { |
293 | 16.5k | for (col = cinfo->image_width; col > 0; col--) { |
294 | 10.2k | _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
295 | 10.2k | _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
296 | 10.2k | _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
297 | 10.2k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
298 | 10.2k | ptr += 4; |
299 | 10.2k | } |
300 | 6.28k | } |
301 | 8.42k | return 1; |
302 | 8.42k | } |
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 | 25.6M | { |
309 | 25.6M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
310 | 25.6M | register _JSAMPROW ptr; |
311 | 25.6M | register U_CHAR *bufferptr; |
312 | 25.6M | register _JSAMPLE *rescale = source->rescale; |
313 | 25.6M | JDIMENSION col; |
314 | | |
315 | 25.6M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
316 | 382 | ERREXIT(cinfo, JERR_INPUT_EOF); |
317 | 25.6M | ptr = source->pub._buffer[0]; |
318 | 25.6M | bufferptr = source->iobuffer; |
319 | 96.6M | for (col = cinfo->image_width; col > 0; col--) { |
320 | 70.9M | *ptr++ = rescale[UCH(*bufferptr++)]; |
321 | 70.9M | } |
322 | 25.6M | return 1; |
323 | 25.6M | } |
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 | 145M | { |
331 | 145M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
332 | 145M | register _JSAMPROW ptr; |
333 | 145M | register U_CHAR *bufferptr; |
334 | 145M | register _JSAMPLE *rescale = source->rescale; |
335 | 145M | JDIMENSION col; |
336 | 145M | unsigned int maxval = source->maxval; |
337 | 145M | register int rindex = rgb_red[cinfo->in_color_space]; |
338 | 145M | register int gindex = rgb_green[cinfo->in_color_space]; |
339 | 145M | register int bindex = rgb_blue[cinfo->in_color_space]; |
340 | 145M | register int aindex = alpha_index[cinfo->in_color_space]; |
341 | 145M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
342 | | |
343 | 145M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
344 | 2.33k | ERREXIT(cinfo, JERR_INPUT_EOF); |
345 | 145M | ptr = source->pub._buffer[0]; |
346 | 145M | bufferptr = source->iobuffer; |
347 | 145M | if (maxval == _MAXJSAMPLE) { |
348 | 17.2M | if (aindex >= 0) |
349 | 1.72M | GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;) |
350 | 15.4M | else |
351 | 15.4M | GRAY_RGB_READ_LOOP(*bufferptr++, {}) |
352 | 128M | } else { |
353 | 128M | if (aindex >= 0) |
354 | 21.8M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], |
355 | 128M | ptr[aindex] = _MAXJSAMPLE;) |
356 | 106M | else |
357 | 106M | GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
358 | 128M | } |
359 | 145M | return 1; |
360 | 145M | } |
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 | 23.5M | { |
368 | 23.5M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
369 | 23.5M | register _JSAMPROW ptr; |
370 | 23.5M | register U_CHAR *bufferptr; |
371 | 23.5M | register _JSAMPLE *rescale = source->rescale; |
372 | 23.5M | JDIMENSION col; |
373 | 23.5M | unsigned int maxval = source->maxval; |
374 | | |
375 | 23.5M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
376 | 379 | ERREXIT(cinfo, JERR_INPUT_EOF); |
377 | 23.5M | ptr = source->pub._buffer[0]; |
378 | 23.5M | bufferptr = source->iobuffer; |
379 | 23.5M | if (maxval == _MAXJSAMPLE) { |
380 | 3.86M | for (col = cinfo->image_width; col > 0; col--) { |
381 | 2.14M | _JSAMPLE gray = *bufferptr++; |
382 | 2.14M | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
383 | 2.14M | ptr += 4; |
384 | 2.14M | } |
385 | 21.8M | } else { |
386 | 80.5M | for (col = cinfo->image_width; col > 0; col--) { |
387 | 58.7M | _JSAMPLE gray = rescale[UCH(*bufferptr++)]; |
388 | 58.7M | rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3); |
389 | 58.7M | ptr += 4; |
390 | 58.7M | } |
391 | 21.8M | } |
392 | 23.5M | return 1; |
393 | 23.5M | } |
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 | 1.86M | { |
400 | 1.86M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
401 | 1.86M | register _JSAMPROW ptr; |
402 | 1.86M | register U_CHAR *bufferptr; |
403 | 1.86M | register _JSAMPLE *rescale = source->rescale; |
404 | 1.86M | JDIMENSION col; |
405 | 1.86M | unsigned int maxval = source->maxval; |
406 | 1.86M | register int rindex = rgb_red[cinfo->in_color_space]; |
407 | 1.86M | register int gindex = rgb_green[cinfo->in_color_space]; |
408 | 1.86M | register int bindex = rgb_blue[cinfo->in_color_space]; |
409 | 1.86M | register int aindex = alpha_index[cinfo->in_color_space]; |
410 | 1.86M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
411 | | |
412 | 1.86M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
413 | 3.32k | ERREXIT(cinfo, JERR_INPUT_EOF); |
414 | 1.86M | ptr = source->pub._buffer[0]; |
415 | 1.86M | bufferptr = source->iobuffer; |
416 | 1.86M | if (maxval == _MAXJSAMPLE) { |
417 | 147k | if (aindex >= 0) |
418 | 35.2k | RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;) |
419 | 111k | else |
420 | 111k | RGB_READ_LOOP(*bufferptr++, {}) |
421 | 1.71M | } else { |
422 | 1.71M | if (aindex >= 0) |
423 | 328k | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = _MAXJSAMPLE;) |
424 | 1.38M | else |
425 | 1.38M | RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {}) |
426 | 1.71M | } |
427 | 1.86M | return 1; |
428 | 1.86M | } |
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 | 364k | { |
436 | 364k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
437 | 364k | register _JSAMPROW ptr; |
438 | 364k | register U_CHAR *bufferptr; |
439 | 364k | register _JSAMPLE *rescale = source->rescale; |
440 | 364k | JDIMENSION col; |
441 | 364k | unsigned int maxval = source->maxval; |
442 | | |
443 | 364k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
444 | 592 | ERREXIT(cinfo, JERR_INPUT_EOF); |
445 | 364k | ptr = source->pub._buffer[0]; |
446 | 364k | bufferptr = source->iobuffer; |
447 | 364k | if (maxval == _MAXJSAMPLE) { |
448 | 390k | for (col = cinfo->image_width; col > 0; col--) { |
449 | 355k | _JSAMPLE r = *bufferptr++; |
450 | 355k | _JSAMPLE g = *bufferptr++; |
451 | 355k | _JSAMPLE b = *bufferptr++; |
452 | 355k | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
453 | 355k | ptr += 4; |
454 | 355k | } |
455 | 328k | } else { |
456 | 4.38M | for (col = cinfo->image_width; col > 0; col--) { |
457 | 4.05M | _JSAMPLE r = rescale[UCH(*bufferptr++)]; |
458 | 4.05M | _JSAMPLE g = rescale[UCH(*bufferptr++)]; |
459 | 4.05M | _JSAMPLE b = rescale[UCH(*bufferptr++)]; |
460 | 4.05M | rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3); |
461 | 4.05M | ptr += 4; |
462 | 4.05M | } |
463 | 328k | } |
464 | 364k | return 1; |
465 | 364k | } |
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 | 3.48M | { |
475 | 3.48M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
476 | | |
477 | 3.48M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
478 | 228 | ERREXIT(cinfo, JERR_INPUT_EOF); |
479 | 3.48M | return 1; |
480 | 3.48M | } |
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 | 313k | { |
487 | 313k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
488 | 313k | register _JSAMPROW ptr; |
489 | 313k | register U_CHAR *bufferptr; |
490 | 313k | register _JSAMPLE *rescale = source->rescale; |
491 | 313k | JDIMENSION col; |
492 | 313k | unsigned int maxval = source->maxval; |
493 | | |
494 | 313k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
495 | 350 | ERREXIT(cinfo, JERR_INPUT_EOF); |
496 | 313k | ptr = source->pub._buffer[0]; |
497 | 313k | bufferptr = source->iobuffer; |
498 | 1.23M | for (col = cinfo->image_width; col > 0; col--) { |
499 | 926k | register unsigned int temp; |
500 | 926k | temp = UCH(*bufferptr++) << 8; |
501 | 926k | temp |= UCH(*bufferptr++); |
502 | 926k | if (temp > maxval) |
503 | 222 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
504 | 926k | *ptr++ = rescale[temp]; |
505 | 926k | } |
506 | 313k | return 1; |
507 | 313k | } |
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 | 1.56M | { |
514 | 1.56M | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
515 | 1.56M | register _JSAMPROW ptr; |
516 | 1.56M | register U_CHAR *bufferptr; |
517 | 1.56M | register _JSAMPLE *rescale = source->rescale; |
518 | 1.56M | JDIMENSION col; |
519 | 1.56M | unsigned int maxval = source->maxval; |
520 | 1.56M | register int rindex = rgb_red[cinfo->in_color_space]; |
521 | 1.56M | register int gindex = rgb_green[cinfo->in_color_space]; |
522 | 1.56M | register int bindex = rgb_blue[cinfo->in_color_space]; |
523 | 1.56M | register int aindex = alpha_index[cinfo->in_color_space]; |
524 | 1.56M | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
525 | | |
526 | 1.56M | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
527 | 1.75k | ERREXIT(cinfo, JERR_INPUT_EOF); |
528 | 1.56M | ptr = source->pub._buffer[0]; |
529 | 1.56M | bufferptr = source->iobuffer; |
530 | 6.19M | for (col = cinfo->image_width; col > 0; col--) { |
531 | 4.63M | register unsigned int temp; |
532 | 4.63M | temp = UCH(*bufferptr++) << 8; |
533 | 4.63M | temp |= UCH(*bufferptr++); |
534 | 4.63M | if (temp > maxval) |
535 | 1.11k | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
536 | 4.63M | ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp]; |
537 | 4.63M | if (aindex >= 0) |
538 | 820k | ptr[aindex] = _MAXJSAMPLE; |
539 | 4.63M | ptr += ps; |
540 | 4.63M | } |
541 | 1.56M | return 1; |
542 | 1.56M | } |
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 | 241k | { |
549 | 241k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
550 | 241k | register _JSAMPROW ptr; |
551 | 241k | register U_CHAR *bufferptr; |
552 | 241k | register _JSAMPLE *rescale = source->rescale; |
553 | 241k | JDIMENSION col; |
554 | 241k | unsigned int maxval = source->maxval; |
555 | | |
556 | 241k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
557 | 298 | ERREXIT(cinfo, JERR_INPUT_EOF); |
558 | 241k | ptr = source->pub._buffer[0]; |
559 | 241k | bufferptr = source->iobuffer; |
560 | 1.06M | for (col = cinfo->image_width; col > 0; col--) { |
561 | 820k | register unsigned int gray; |
562 | 820k | gray = UCH(*bufferptr++) << 8; |
563 | 820k | gray |= UCH(*bufferptr++); |
564 | 820k | if (gray > maxval) |
565 | 182 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
566 | 820k | rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1, |
567 | 820k | ptr + 2, ptr + 3); |
568 | 820k | ptr += 4; |
569 | 820k | } |
570 | 241k | return 1; |
571 | 241k | } |
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 | 105k | { |
578 | 105k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
579 | 105k | register _JSAMPROW ptr; |
580 | 105k | register U_CHAR *bufferptr; |
581 | 105k | register _JSAMPLE *rescale = source->rescale; |
582 | 105k | JDIMENSION col; |
583 | 105k | unsigned int maxval = source->maxval; |
584 | 105k | register int rindex = rgb_red[cinfo->in_color_space]; |
585 | 105k | register int gindex = rgb_green[cinfo->in_color_space]; |
586 | 105k | register int bindex = rgb_blue[cinfo->in_color_space]; |
587 | 105k | register int aindex = alpha_index[cinfo->in_color_space]; |
588 | 105k | register int ps = rgb_pixelsize[cinfo->in_color_space]; |
589 | | |
590 | 105k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
591 | 2.23k | ERREXIT(cinfo, JERR_INPUT_EOF); |
592 | 105k | ptr = source->pub._buffer[0]; |
593 | 105k | bufferptr = source->iobuffer; |
594 | 544k | for (col = cinfo->image_width; col > 0; col--) { |
595 | 439k | register unsigned int temp; |
596 | 439k | temp = UCH(*bufferptr++) << 8; |
597 | 439k | temp |= UCH(*bufferptr++); |
598 | 439k | if (temp > maxval) |
599 | 885 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
600 | 439k | ptr[rindex] = rescale[temp]; |
601 | 439k | temp = UCH(*bufferptr++) << 8; |
602 | 439k | temp |= UCH(*bufferptr++); |
603 | 439k | if (temp > maxval) |
604 | 655 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
605 | 439k | ptr[gindex] = rescale[temp]; |
606 | 439k | temp = UCH(*bufferptr++) << 8; |
607 | 439k | temp |= UCH(*bufferptr++); |
608 | 439k | if (temp > maxval) |
609 | 665 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
610 | 439k | ptr[bindex] = rescale[temp]; |
611 | 439k | if (aindex >= 0) |
612 | 85.7k | ptr[aindex] = _MAXJSAMPLE; |
613 | 439k | ptr += ps; |
614 | 439k | } |
615 | 105k | return 1; |
616 | 105k | } |
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 | 19.9k | { |
623 | 19.9k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
624 | 19.9k | register _JSAMPROW ptr; |
625 | 19.9k | register U_CHAR *bufferptr; |
626 | 19.9k | register _JSAMPLE *rescale = source->rescale; |
627 | 19.9k | JDIMENSION col; |
628 | 19.9k | unsigned int maxval = source->maxval; |
629 | | |
630 | 19.9k | if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
631 | 394 | ERREXIT(cinfo, JERR_INPUT_EOF); |
632 | 19.9k | ptr = source->pub._buffer[0]; |
633 | 19.9k | bufferptr = source->iobuffer; |
634 | 105k | for (col = cinfo->image_width; col > 0; col--) { |
635 | 86.0k | register unsigned int r, g, b; |
636 | 86.0k | r = UCH(*bufferptr++) << 8; |
637 | 86.0k | r |= UCH(*bufferptr++); |
638 | 86.0k | if (r > maxval) |
639 | 152 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
640 | 86.0k | g = UCH(*bufferptr++) << 8; |
641 | 86.0k | g |= UCH(*bufferptr++); |
642 | 86.0k | if (g > maxval) |
643 | 107 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
644 | 86.0k | b = UCH(*bufferptr++) << 8; |
645 | 86.0k | b |= UCH(*bufferptr++); |
646 | 86.0k | if (b > maxval) |
647 | 110 | ERREXIT(cinfo, JERR_PPM_OUTOFRANGE); |
648 | 86.0k | rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2, |
649 | 86.0k | ptr + 3); |
650 | 86.0k | ptr += 4; |
651 | 86.0k | } |
652 | 19.9k | return 1; |
653 | 19.9k | } |
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 | 97.8k | { |
663 | 97.8k | ppm_source_ptr source = (ppm_source_ptr)sinfo; |
664 | 97.8k | int c; |
665 | 97.8k | unsigned int w, h, maxval; |
666 | 97.8k | boolean need_iobuffer, use_raw_buffer, need_rescale; |
667 | | |
668 | 97.8k | if (getc(source->pub.input_file) != 'P') |
669 | 0 | ERREXIT(cinfo, JERR_PPM_NOT); |
670 | | |
671 | 97.8k | c = getc(source->pub.input_file); /* subformat discriminator character */ |
672 | | |
673 | | /* detect unsupported variants (ie, PBM) before trying to read header */ |
674 | 97.8k | switch (c) { |
675 | 8.19k | case '2': /* it's a text-format PGM file */ |
676 | 16.6k | case '3': /* it's a text-format PPM file */ |
677 | 76.3k | case '5': /* it's a raw-format PGM file */ |
678 | 97.7k | case '6': /* it's a raw-format PPM file */ |
679 | 97.7k | break; |
680 | 55 | default: |
681 | 55 | ERREXIT(cinfo, JERR_PPM_NOT); |
682 | 55 | break; |
683 | 97.8k | } |
684 | | |
685 | | /* fetch the remaining header info */ |
686 | 97.7k | w = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
687 | 97.7k | h = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
688 | 97.7k | maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
689 | | |
690 | 97.7k | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
691 | 172 | ERREXIT(cinfo, JERR_PPM_NOT); |
692 | 97.7k | if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels) |
693 | 1.48k | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels); |
694 | | |
695 | 97.7k | cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ |
696 | 97.7k | cinfo->image_width = (JDIMENSION)w; |
697 | 97.7k | cinfo->image_height = (JDIMENSION)h; |
698 | 97.7k | source->maxval = maxval; |
699 | | |
700 | | /* initialize flags to most common settings */ |
701 | 97.7k | need_iobuffer = TRUE; /* do we need an I/O buffer? */ |
702 | 97.7k | use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ |
703 | 97.7k | need_rescale = TRUE; /* do we need a rescale array? */ |
704 | | |
705 | 97.7k | switch (c) { |
706 | 5.79k | case '2': /* it's a text-format PGM file */ |
707 | 5.79k | if (cinfo->in_color_space == JCS_UNKNOWN || |
708 | 5.79k | cinfo->in_color_space == JCS_RGB) |
709 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
710 | 5.79k | TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval); |
711 | 5.79k | if (cinfo->in_color_space == JCS_GRAYSCALE) |
712 | 846 | source->pub.get_pixel_rows = get_text_gray_row; |
713 | 4.95k | else if (IsExtRGB(cinfo->in_color_space)) |
714 | 4.23k | source->pub.get_pixel_rows = get_text_gray_rgb_row; |
715 | 723 | else if (cinfo->in_color_space == JCS_CMYK) |
716 | 723 | source->pub.get_pixel_rows = get_text_gray_cmyk_row; |
717 | 0 | else |
718 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
719 | 5.79k | need_iobuffer = FALSE; |
720 | 5.79k | break; |
721 | | |
722 | 6.65k | case '3': /* it's a text-format PPM file */ |
723 | 6.65k | if (cinfo->in_color_space == JCS_UNKNOWN) |
724 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
725 | 6.65k | TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval); |
726 | 6.65k | if (IsExtRGB(cinfo->in_color_space)) |
727 | 4.83k | source->pub.get_pixel_rows = get_text_rgb_row; |
728 | 1.81k | else if (cinfo->in_color_space == JCS_CMYK) |
729 | 848 | source->pub.get_pixel_rows = get_text_rgb_cmyk_row; |
730 | 967 | else |
731 | 967 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
732 | 6.65k | need_iobuffer = FALSE; |
733 | 6.65k | break; |
734 | | |
735 | 57.5k | case '5': /* it's a raw-format PGM file */ |
736 | 57.5k | if (cinfo->in_color_space == JCS_UNKNOWN || |
737 | 57.5k | cinfo->in_color_space == JCS_RGB) |
738 | 0 | cinfo->in_color_space = JCS_GRAYSCALE; |
739 | 57.5k | TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval); |
740 | 57.5k | if (maxval > 255) { |
741 | 4.47k | if (cinfo->in_color_space == JCS_GRAYSCALE) |
742 | 653 | source->pub.get_pixel_rows = get_word_gray_row; |
743 | 3.82k | else if (IsExtRGB(cinfo->in_color_space)) |
744 | 3.26k | source->pub.get_pixel_rows = get_word_gray_rgb_row; |
745 | 555 | else if (cinfo->in_color_space == JCS_CMYK) |
746 | 555 | source->pub.get_pixel_rows = get_word_gray_cmyk_row; |
747 | 0 | else |
748 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
749 | 53.0k | } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) && |
750 | 53.0k | cinfo->in_color_space == JCS_GRAYSCALE) { |
751 | 506 | source->pub.get_pixel_rows = get_raw_row; |
752 | 506 | use_raw_buffer = TRUE; |
753 | 506 | need_rescale = FALSE; |
754 | 52.5k | } else { |
755 | 52.5k | if (cinfo->in_color_space == JCS_GRAYSCALE) |
756 | 7.31k | source->pub.get_pixel_rows = get_scaled_gray_row; |
757 | 45.2k | else if (IsExtRGB(cinfo->in_color_space)) |
758 | 39.0k | source->pub.get_pixel_rows = get_gray_rgb_row; |
759 | 6.13k | else if (cinfo->in_color_space == JCS_CMYK) |
760 | 6.13k | source->pub.get_pixel_rows = get_gray_cmyk_row; |
761 | 0 | else |
762 | 0 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
763 | 52.5k | } |
764 | 57.5k | break; |
765 | | |
766 | 19.6k | case '6': /* it's a raw-format PPM file */ |
767 | 19.6k | if (cinfo->in_color_space == JCS_UNKNOWN) |
768 | 0 | cinfo->in_color_space = JCS_EXT_RGB; |
769 | 19.6k | TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval); |
770 | 19.6k | if (maxval > 255) { |
771 | 6.40k | if (IsExtRGB(cinfo->in_color_space)) |
772 | 4.66k | source->pub.get_pixel_rows = get_word_rgb_row; |
773 | 1.74k | else if (cinfo->in_color_space == JCS_CMYK) |
774 | 807 | source->pub.get_pixel_rows = get_word_rgb_cmyk_row; |
775 | 933 | else |
776 | 933 | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
777 | 13.2k | } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) && |
778 | 13.2k | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
779 | 13.2k | (cinfo->in_color_space == JCS_EXT_RGB || |
780 | 1.37k | cinfo->in_color_space == JCS_RGB)) { |
781 | | #else |
782 | | cinfo->in_color_space == JCS_EXT_RGB) { |
783 | | #endif |
784 | 203 | source->pub.get_pixel_rows = get_raw_row; |
785 | 203 | use_raw_buffer = TRUE; |
786 | 203 | need_rescale = FALSE; |
787 | 13.0k | } else { |
788 | 13.0k | if (IsExtRGB(cinfo->in_color_space)) |
789 | 9.46k | source->pub.get_pixel_rows = get_rgb_row; |
790 | 3.55k | else if (cinfo->in_color_space == JCS_CMYK) |
791 | 1.62k | source->pub.get_pixel_rows = get_rgb_cmyk_row; |
792 | 1.93k | else |
793 | 1.93k | ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
794 | 13.0k | } |
795 | 19.6k | break; |
796 | 97.7k | } |
797 | | |
798 | 85.7k | if (IsExtRGB(cinfo->in_color_space)) |
799 | 65.7k | cinfo->input_components = rgb_pixelsize[cinfo->in_color_space]; |
800 | 20.0k | else if (cinfo->in_color_space == JCS_GRAYSCALE) |
801 | 9.31k | cinfo->input_components = 1; |
802 | 10.6k | else if (cinfo->in_color_space == JCS_CMYK) |
803 | 10.6k | cinfo->input_components = 4; |
804 | | |
805 | | /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ |
806 | 85.7k | if (need_iobuffer) { |
807 | 74.2k | if (c == '6') |
808 | 16.7k | source->buffer_width = (size_t)w * 3 * |
809 | 16.7k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
810 | 57.5k | else |
811 | 57.5k | source->buffer_width = (size_t)w * |
812 | 57.5k | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
813 | 74.2k | source->iobuffer = (U_CHAR *) |
814 | 74.2k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
815 | 74.2k | source->buffer_width); |
816 | 74.2k | } |
817 | | |
818 | | /* Create compressor input buffer. */ |
819 | 85.7k | if (use_raw_buffer) { |
820 | | /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ |
821 | | /* Synthesize a _JSAMPARRAY pointer structure */ |
822 | 709 | source->pixrow = (_JSAMPROW)source->iobuffer; |
823 | 709 | source->pub._buffer = &source->pixrow; |
824 | 709 | source->pub.buffer_height = 1; |
825 | 85.0k | } else { |
826 | | /* Need to translate anyway, so make a separate sample buffer. */ |
827 | 85.0k | source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray) |
828 | 85.0k | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
829 | 85.0k | (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1); |
830 | 85.0k | source->pub.buffer_height = 1; |
831 | 85.0k | } |
832 | | |
833 | | /* Compute the rescaling array if required. */ |
834 | 85.7k | if (need_rescale) { |
835 | 85.0k | long val, half_maxval; |
836 | | |
837 | | /* On 16-bit-int machines we have to be careful of maxval = 65535 */ |
838 | 85.0k | source->rescale = (_JSAMPLE *) |
839 | 85.0k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
840 | 85.0k | (size_t)(((long)MAX(maxval, 255) + 1L) * |
841 | 85.0k | sizeof(_JSAMPLE))); |
842 | 85.0k | memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) * |
843 | 85.0k | sizeof(_JSAMPLE))); |
844 | 85.0k | half_maxval = maxval / 2; |
845 | 226M | for (val = 0; val <= (long)maxval; val++) { |
846 | | /* The multiplication here must be done in 32 bits to avoid overflow */ |
847 | 226M | source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) / |
848 | 226M | maxval); |
849 | 226M | } |
850 | 85.0k | } |
851 | 85.7k | } |
852 | | |
853 | | |
854 | | /* |
855 | | * Finish up at the end of the file. |
856 | | */ |
857 | | |
858 | | METHODDEF(void) |
859 | | finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
860 | 58.9k | { |
861 | | /* no work */ |
862 | 58.9k | } |
863 | | |
864 | | |
865 | | /* |
866 | | * The module selection routine for PPM format input. |
867 | | */ |
868 | | |
869 | | GLOBAL(cjpeg_source_ptr) |
870 | | _jinit_read_ppm(j_compress_ptr cinfo) |
871 | 97.8k | { |
872 | 97.8k | ppm_source_ptr source; |
873 | | |
874 | 97.8k | if (cinfo->data_precision != BITS_IN_JSAMPLE) |
875 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
876 | | |
877 | | /* Create module interface object */ |
878 | 97.8k | source = (ppm_source_ptr) |
879 | 97.8k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
880 | 97.8k | sizeof(ppm_source_struct)); |
881 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
882 | 97.8k | source->pub.start_input = start_input_ppm; |
883 | 97.8k | source->pub.finish_input = finish_input_ppm; |
884 | 97.8k | source->pub.max_pixels = 0; |
885 | | |
886 | 97.8k | return (cjpeg_source_ptr)source; |
887 | 97.8k | } Line | Count | Source | 871 | 47.3k | { | 872 | 47.3k | ppm_source_ptr source; | 873 | | | 874 | 47.3k | if (cinfo->data_precision != BITS_IN_JSAMPLE) | 875 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | 876 | | | 877 | | /* Create module interface object */ | 878 | 47.3k | source = (ppm_source_ptr) | 879 | 47.3k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, | 880 | 47.3k | sizeof(ppm_source_struct)); | 881 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ | 882 | 47.3k | source->pub.start_input = start_input_ppm; | 883 | 47.3k | source->pub.finish_input = finish_input_ppm; | 884 | 47.3k | source->pub.max_pixels = 0; | 885 | | | 886 | 47.3k | return (cjpeg_source_ptr)source; | 887 | 47.3k | } |
Line | Count | Source | 871 | 39.5k | { | 872 | 39.5k | ppm_source_ptr source; | 873 | | | 874 | 39.5k | if (cinfo->data_precision != BITS_IN_JSAMPLE) | 875 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | 876 | | | 877 | | /* Create module interface object */ | 878 | 39.5k | source = (ppm_source_ptr) | 879 | 39.5k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, | 880 | 39.5k | sizeof(ppm_source_struct)); | 881 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ | 882 | 39.5k | source->pub.start_input = start_input_ppm; | 883 | 39.5k | source->pub.finish_input = finish_input_ppm; | 884 | 39.5k | source->pub.max_pixels = 0; | 885 | | | 886 | 39.5k | return (cjpeg_source_ptr)source; | 887 | 39.5k | } |
Line | Count | Source | 871 | 10.9k | { | 872 | 10.9k | ppm_source_ptr source; | 873 | | | 874 | 10.9k | if (cinfo->data_precision != BITS_IN_JSAMPLE) | 875 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | 876 | | | 877 | | /* Create module interface object */ | 878 | 10.9k | source = (ppm_source_ptr) | 879 | 10.9k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, | 880 | 10.9k | sizeof(ppm_source_struct)); | 881 | | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ | 882 | 10.9k | source->pub.start_input = start_input_ppm; | 883 | 10.9k | source->pub.finish_input = finish_input_ppm; | 884 | 10.9k | source->pub.max_pixels = 0; | 885 | | | 886 | 10.9k | return (cjpeg_source_ptr)source; | 887 | 10.9k | } |
|
888 | | |
889 | | #endif /* defined(PPM_SUPPORTED) && |
890 | | (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */ |