/src/libjpeg-turbo.3.1.x/src/wrppm.c
Line | Count | Source |
1 | | /* |
2 | | * wrppm.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1991-1996, Thomas G. Lane. |
6 | | * Modified 2009 by Guido Vollbeding. |
7 | | * libjpeg-turbo Modifications: |
8 | | * Copyright (C) 2017, 2019-2020, 2022, 2024, 2026, D. R. Commander. |
9 | | * For conditions of distribution and use, see the accompanying README.ijg |
10 | | * file. |
11 | | * |
12 | | * This file contains routines to write output 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 output to |
19 | | * an ordinary stdio stream. |
20 | | */ |
21 | | |
22 | | #include "cmyk.h" |
23 | | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
24 | | |
25 | | #if defined(PPM_SUPPORTED) && \ |
26 | | (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)) |
27 | | |
28 | | |
29 | | /* |
30 | | * For 12-bit JPEG data, we either downscale the values to 8 bits |
31 | | * (to write standard byte-per-sample PPM/PGM files), or output |
32 | | * nonstandard word-per-sample PPM/PGM files. Downscaling is done |
33 | | * if PPM_NORAWWORD is defined (this can be done in the Makefile |
34 | | * or in jconfig.h). |
35 | | * (When the core library supports data precision reduction, a cleaner |
36 | | * implementation will be to ask for that instead.) |
37 | | */ |
38 | | |
39 | | #if BITS_IN_JSAMPLE == 8 |
40 | 0 | #define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)(v) |
41 | 0 | #define BYTESPERSAMPLE 1 |
42 | 0 | #define PPM_MAXVAL ((1 << cinfo->data_precision) - 1) |
43 | | #else |
44 | | #ifdef PPM_NORAWWORD |
45 | | #define PUTPPMSAMPLE(ptr, v) \ |
46 | | *ptr++ = (char)((v) >> (cinfo->data_precision - 8)) |
47 | | #define BYTESPERSAMPLE 1 |
48 | | #define PPM_MAXVAL 255 |
49 | | #else |
50 | | /* The word-per-sample format always puts the MSB first. */ |
51 | 0 | #define PUTPPMSAMPLE(ptr, v) { \ |
52 | 0 | register int val_ = v; \ |
53 | 0 | *ptr++ = (char)((val_ >> 8) & 0xFF); \ |
54 | 0 | *ptr++ = (char)(val_ & 0xFF); \ |
55 | 0 | } |
56 | 0 | #define BYTESPERSAMPLE 2 |
57 | 0 | #define PPM_MAXVAL ((1 << cinfo->data_precision) - 1) |
58 | | #endif |
59 | | #endif |
60 | | |
61 | | |
62 | | /* |
63 | | * When _JSAMPLE is the same size as char, we can just fwrite() the |
64 | | * decompressed data to the PPM or PGM file. |
65 | | */ |
66 | | |
67 | | |
68 | | /* Private version of data destination object */ |
69 | | |
70 | | typedef struct { |
71 | | struct djpeg_dest_struct pub; /* public fields */ |
72 | | |
73 | | /* Usually these two pointers point to the same place: */ |
74 | | char *iobuffer; /* fwrite's I/O buffer */ |
75 | | _JSAMPROW pixrow; /* decompressor output buffer */ |
76 | | size_t buffer_width; /* width of I/O buffer */ |
77 | | JDIMENSION samples_per_row; /* _JSAMPLEs per output row */ |
78 | | } ppm_dest_struct; |
79 | | |
80 | | typedef ppm_dest_struct *ppm_dest_ptr; |
81 | | |
82 | | |
83 | | #if BITS_IN_JSAMPLE == 8 |
84 | | |
85 | | /* |
86 | | * Write some pixel data. |
87 | | * In this module rows_supplied will always be 1. |
88 | | * |
89 | | * put_pixel_rows handles the "normal" 8-bit case where the decompressor |
90 | | * output buffer is physically the same as the fwrite buffer. |
91 | | */ |
92 | | |
93 | | METHODDEF(void) |
94 | | put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
95 | | JDIMENSION rows_supplied) |
96 | 0 | { |
97 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
98 | |
|
99 | 0 | fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file); |
100 | 0 | } |
101 | | |
102 | | #endif |
103 | | |
104 | | |
105 | | /* |
106 | | * This code is used when we have to copy the data and apply a pixel |
107 | | * format translation. Typically this only happens in 12-bit mode. |
108 | | */ |
109 | | |
110 | | METHODDEF(void) |
111 | | copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
112 | | JDIMENSION rows_supplied) |
113 | 0 | { |
114 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
115 | 0 | register char *bufferptr; |
116 | 0 | register _JSAMPROW ptr; |
117 | 0 | register JDIMENSION col; |
118 | |
|
119 | 0 | ptr = dest->pub._buffer[0]; |
120 | 0 | bufferptr = dest->iobuffer; |
121 | 0 | for (col = dest->samples_per_row; col > 0; col--) { |
122 | 0 | PUTPPMSAMPLE(bufferptr, *ptr++); |
123 | 0 | } |
124 | 0 | fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file); |
125 | 0 | } Unexecuted instantiation: wrppm-8.c:copy_pixel_rows Unexecuted instantiation: wrppm-12.c:copy_pixel_rows Unexecuted instantiation: wrppm-16.c:copy_pixel_rows |
126 | | |
127 | | |
128 | | /* |
129 | | * Convert extended RGB to RGB. |
130 | | */ |
131 | | |
132 | | METHODDEF(void) |
133 | | put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied) |
134 | 0 | { |
135 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
136 | 0 | register char *bufferptr; |
137 | 0 | register _JSAMPROW ptr; |
138 | 0 | register JDIMENSION col; |
139 | 0 | register int rindex = rgb_red[cinfo->out_color_space]; |
140 | 0 | register int gindex = rgb_green[cinfo->out_color_space]; |
141 | 0 | register int bindex = rgb_blue[cinfo->out_color_space]; |
142 | 0 | register int ps = rgb_pixelsize[cinfo->out_color_space]; |
143 | |
|
144 | 0 | ptr = dest->pub._buffer[0]; |
145 | 0 | bufferptr = dest->iobuffer; |
146 | 0 | for (col = cinfo->output_width; col > 0; col--) { |
147 | 0 | PUTPPMSAMPLE(bufferptr, ptr[rindex]); |
148 | 0 | PUTPPMSAMPLE(bufferptr, ptr[gindex]); |
149 | 0 | PUTPPMSAMPLE(bufferptr, ptr[bindex]); |
150 | 0 | ptr += ps; |
151 | 0 | } |
152 | 0 | fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file); |
153 | 0 | } Unexecuted instantiation: wrppm-8.c:put_rgb Unexecuted instantiation: wrppm-12.c:put_rgb Unexecuted instantiation: wrppm-16.c:put_rgb |
154 | | |
155 | | |
156 | | /* |
157 | | * Convert CMYK to RGB. |
158 | | */ |
159 | | |
160 | | METHODDEF(void) |
161 | | put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
162 | | JDIMENSION rows_supplied) |
163 | 0 | { |
164 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
165 | 0 | register char *bufferptr; |
166 | 0 | register _JSAMPROW ptr; |
167 | 0 | register JDIMENSION col; |
168 | |
|
169 | 0 | ptr = dest->pub._buffer[0]; |
170 | 0 | bufferptr = dest->iobuffer; |
171 | 0 | for (col = cinfo->output_width; col > 0; col--) { |
172 | 0 | _JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++; |
173 | 0 | cmyk_to_rgb(PPM_MAXVAL, c, m, y, k, &r, &g, &b); |
174 | 0 | PUTPPMSAMPLE(bufferptr, r); |
175 | 0 | PUTPPMSAMPLE(bufferptr, g); |
176 | 0 | PUTPPMSAMPLE(bufferptr, b); |
177 | 0 | } |
178 | 0 | fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file); |
179 | 0 | } Unexecuted instantiation: wrppm-8.c:put_cmyk Unexecuted instantiation: wrppm-12.c:put_cmyk Unexecuted instantiation: wrppm-16.c:put_cmyk |
180 | | |
181 | | |
182 | | /* |
183 | | * Write some pixel data when color quantization is in effect. |
184 | | * We have to demap the color index values to straight data. |
185 | | */ |
186 | | |
187 | | METHODDEF(void) |
188 | | put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
189 | | JDIMENSION rows_supplied) |
190 | 0 | { |
191 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
192 | 0 | register char *bufferptr; |
193 | 0 | register int pixval; |
194 | 0 | register _JSAMPROW ptr; |
195 | 0 | register _JSAMPROW color_map0 = |
196 | 0 | ((_JSAMPARRAY)cinfo->colormap)[rgb_red[cinfo->out_color_space]]; |
197 | 0 | register _JSAMPROW color_map1 = |
198 | 0 | ((_JSAMPARRAY)cinfo->colormap)[rgb_green[cinfo->out_color_space]]; |
199 | 0 | register _JSAMPROW color_map2 = |
200 | 0 | ((_JSAMPARRAY)cinfo->colormap)[rgb_blue[cinfo->out_color_space]]; |
201 | 0 | register JDIMENSION col; |
202 | |
|
203 | 0 | ptr = dest->pub._buffer[0]; |
204 | 0 | bufferptr = dest->iobuffer; |
205 | 0 | for (col = cinfo->output_width; col > 0; col--) { |
206 | 0 | pixval = *ptr++; |
207 | 0 | PUTPPMSAMPLE(bufferptr, color_map0[pixval]); |
208 | 0 | PUTPPMSAMPLE(bufferptr, color_map1[pixval]); |
209 | 0 | PUTPPMSAMPLE(bufferptr, color_map2[pixval]); |
210 | 0 | } |
211 | 0 | fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file); |
212 | 0 | } Unexecuted instantiation: wrppm-8.c:put_demapped_rgb Unexecuted instantiation: wrppm-12.c:put_demapped_rgb Unexecuted instantiation: wrppm-16.c:put_demapped_rgb |
213 | | |
214 | | |
215 | | METHODDEF(void) |
216 | | put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
217 | | JDIMENSION rows_supplied) |
218 | 0 | { |
219 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
220 | 0 | register char *bufferptr; |
221 | 0 | register _JSAMPROW ptr; |
222 | 0 | register _JSAMPROW color_map = ((_JSAMPARRAY)cinfo->colormap)[0]; |
223 | 0 | register JDIMENSION col; |
224 | |
|
225 | 0 | ptr = dest->pub._buffer[0]; |
226 | 0 | bufferptr = dest->iobuffer; |
227 | 0 | for (col = cinfo->output_width; col > 0; col--) { |
228 | 0 | PUTPPMSAMPLE(bufferptr, color_map[*ptr++]); |
229 | 0 | } |
230 | 0 | fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file); |
231 | 0 | } Unexecuted instantiation: wrppm-8.c:put_demapped_gray Unexecuted instantiation: wrppm-12.c:put_demapped_gray Unexecuted instantiation: wrppm-16.c:put_demapped_gray |
232 | | |
233 | | |
234 | | /* |
235 | | * Startup: write the file header. |
236 | | */ |
237 | | |
238 | | METHODDEF(void) |
239 | | start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
240 | 0 | { |
241 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
242 | | |
243 | | /* Emit file header */ |
244 | 0 | switch (cinfo->out_color_space) { |
245 | 0 | case JCS_GRAYSCALE: |
246 | | /* emit header for raw PGM format */ |
247 | 0 | fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n", |
248 | 0 | (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL); |
249 | 0 | break; |
250 | 0 | case JCS_RGB: |
251 | 0 | case JCS_EXT_RGB: |
252 | 0 | case JCS_EXT_RGBX: |
253 | 0 | case JCS_EXT_BGR: |
254 | 0 | case JCS_EXT_BGRX: |
255 | 0 | case JCS_EXT_XBGR: |
256 | 0 | case JCS_EXT_XRGB: |
257 | 0 | case JCS_EXT_RGBA: |
258 | 0 | case JCS_EXT_BGRA: |
259 | 0 | case JCS_EXT_ABGR: |
260 | 0 | case JCS_EXT_ARGB: |
261 | 0 | case JCS_CMYK: |
262 | 0 | if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors) |
263 | 0 | ERREXIT(cinfo, JERR_PPM_COLORSPACE); |
264 | | /* emit header for raw PPM format */ |
265 | 0 | fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n", |
266 | 0 | (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL); |
267 | 0 | break; |
268 | 0 | default: |
269 | 0 | ERREXIT(cinfo, JERR_PPM_COLORSPACE); |
270 | 0 | } |
271 | 0 | } Unexecuted instantiation: wrppm-8.c:start_output_ppm Unexecuted instantiation: wrppm-12.c:start_output_ppm Unexecuted instantiation: wrppm-16.c:start_output_ppm |
272 | | |
273 | | |
274 | | /* |
275 | | * Finish up at the end of the file. |
276 | | */ |
277 | | |
278 | | METHODDEF(void) |
279 | | finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
280 | 0 | { |
281 | | /* Make sure we wrote the output file OK */ |
282 | 0 | fflush(dinfo->output_file); |
283 | 0 | if (ferror(dinfo->output_file)) |
284 | 0 | ERREXIT(cinfo, JERR_FILE_WRITE); |
285 | 0 | } Unexecuted instantiation: wrppm-8.c:finish_output_ppm Unexecuted instantiation: wrppm-12.c:finish_output_ppm Unexecuted instantiation: wrppm-16.c:finish_output_ppm |
286 | | |
287 | | |
288 | | /* |
289 | | * Re-calculate buffer dimensions based on output dimensions. |
290 | | */ |
291 | | |
292 | | METHODDEF(void) |
293 | | calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
294 | 0 | { |
295 | 0 | ppm_dest_ptr dest = (ppm_dest_ptr)dinfo; |
296 | |
|
297 | 0 | if (cinfo->out_color_space == JCS_GRAYSCALE) |
298 | 0 | dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; |
299 | 0 | else |
300 | 0 | dest->samples_per_row = cinfo->output_width * 3; |
301 | 0 | dest->buffer_width = dest->samples_per_row * BYTESPERSAMPLE; |
302 | 0 | } Unexecuted instantiation: wrppm-8.c:calc_buffer_dimensions_ppm Unexecuted instantiation: wrppm-12.c:calc_buffer_dimensions_ppm Unexecuted instantiation: wrppm-16.c:calc_buffer_dimensions_ppm |
303 | | |
304 | | |
305 | | /* |
306 | | * The module selection routine for PPM format output. |
307 | | */ |
308 | | |
309 | | GLOBAL(djpeg_dest_ptr) |
310 | | _jinit_write_ppm(j_decompress_ptr cinfo) |
311 | 0 | { |
312 | 0 | ppm_dest_ptr dest; |
313 | |
|
314 | | #if BITS_IN_JSAMPLE == 8 |
315 | 0 | if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2) |
316 | | #else |
317 | 0 | if (cinfo->data_precision > BITS_IN_JSAMPLE || |
318 | 0 | cinfo->data_precision < BITS_IN_JSAMPLE - 3) |
319 | 0 | #endif |
320 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
321 | | |
322 | | /* Create module interface object, fill in method pointers */ |
323 | 0 | dest = (ppm_dest_ptr) |
324 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
325 | 0 | sizeof(ppm_dest_struct)); |
326 | 0 | dest->pub.start_output = start_output_ppm; |
327 | 0 | dest->pub.finish_output = finish_output_ppm; |
328 | 0 | dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm; |
329 | | |
330 | | /* Calculate output image dimensions so we can allocate space */ |
331 | 0 | if (cinfo->image_width > JPEG_MAX_DIMENSION || |
332 | 0 | cinfo->image_height > JPEG_MAX_DIMENSION) |
333 | 0 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION); |
334 | 0 | jpeg_calc_output_dimensions(cinfo); |
335 | | |
336 | | /* Create physical I/O buffer */ |
337 | 0 | dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest); |
338 | 0 | dest->iobuffer = (char *)(*cinfo->mem->alloc_small) |
339 | 0 | ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width); |
340 | |
|
341 | | #if BITS_IN_JSAMPLE == 8 |
342 | 0 | if (cinfo->quantize_colors || |
343 | 0 | (cinfo->out_color_space != JCS_EXT_RGB && |
344 | 0 | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3 |
345 | 0 | cinfo->out_color_space != JCS_RGB && |
346 | 0 | #endif |
347 | 0 | cinfo->out_color_space != JCS_GRAYSCALE)) |
348 | 0 | #endif |
349 | 0 | { |
350 | | /* When quantizing, we need an output buffer for colormap indexes |
351 | | * that's separate from the physical I/O buffer. We also need a |
352 | | * separate buffer if pixel format translation must take place. |
353 | | */ |
354 | 0 | dest->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray) |
355 | 0 | ((j_common_ptr)cinfo, JPOOL_IMAGE, |
356 | 0 | cinfo->output_width * cinfo->output_components, (JDIMENSION)1); |
357 | 0 | dest->pub.buffer_height = 1; |
358 | 0 | if (!cinfo->quantize_colors) { |
359 | 0 | if (IsExtRGB(cinfo->out_color_space)) |
360 | 0 | dest->pub.put_pixel_rows = put_rgb; |
361 | 0 | else if (cinfo->out_color_space == JCS_CMYK) |
362 | 0 | dest->pub.put_pixel_rows = put_cmyk; |
363 | 0 | else |
364 | 0 | dest->pub.put_pixel_rows = copy_pixel_rows; |
365 | 0 | } else if (cinfo->out_color_space == JCS_GRAYSCALE) |
366 | 0 | dest->pub.put_pixel_rows = put_demapped_gray; |
367 | 0 | else |
368 | 0 | dest->pub.put_pixel_rows = put_demapped_rgb; |
369 | 0 | } |
370 | | #if BITS_IN_JSAMPLE == 8 |
371 | 0 | else { |
372 | | /* We will fwrite() directly from decompressor output buffer. */ |
373 | | /* Synthesize a _JSAMPARRAY pointer structure */ |
374 | 0 | dest->pixrow = (_JSAMPROW)dest->iobuffer; |
375 | 0 | dest->pub._buffer = &dest->pixrow; |
376 | 0 | dest->pub.buffer_height = 1; |
377 | 0 | dest->pub.put_pixel_rows = put_pixel_rows; |
378 | 0 | } |
379 | | #endif |
380 | |
|
381 | 0 | return (djpeg_dest_ptr)dest; |
382 | 0 | } Unexecuted instantiation: jinit_write_ppm Unexecuted instantiation: j12init_write_ppm Unexecuted instantiation: j16init_write_ppm |
383 | | |
384 | | #endif /* defined(PPM_SUPPORTED) && |
385 | | (BITS_IN_JSAMPLE != 16 || defined(D_LOSSLESS_SUPPORTED)) */ |