/src/libjpeg-turbo.2.1.x/wrppm.c
Line  | Count  | Source (jump to first uncovered line)  | 
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, 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  |  | #ifdef PPM_SUPPORTED  | 
26  |  |  | 
27  |  |  | 
28  |  | /*  | 
29  |  |  * For 12-bit JPEG data, we either downscale the values to 8 bits  | 
30  |  |  * (to write standard byte-per-sample PPM/PGM files), or output  | 
31  |  |  * nonstandard word-per-sample PPM/PGM files.  Downscaling is done  | 
32  |  |  * if PPM_NORAWWORD is defined (this can be done in the Makefile  | 
33  |  |  * or in jconfig.h).  | 
34  |  |  * (When the core library supports data precision reduction, a cleaner  | 
35  |  |  * implementation will be to ask for that instead.)  | 
36  |  |  */  | 
37  |  |  | 
38  |  | #if BITS_IN_JSAMPLE == 8  | 
39  | 0  | #define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)(v)  | 
40  | 0  | #define BYTESPERSAMPLE  1  | 
41  | 0  | #define PPM_MAXVAL  255  | 
42  |  | #else  | 
43  |  | #ifdef PPM_NORAWWORD  | 
44  |  | #define PUTPPMSAMPLE(ptr, v)  *ptr++ = (char)((v) >> (BITS_IN_JSAMPLE - 8))  | 
45  |  | #define BYTESPERSAMPLE  1  | 
46  |  | #define PPM_MAXVAL  255  | 
47  |  | #else  | 
48  |  | /* The word-per-sample format always puts the MSB first. */  | 
49  |  | #define PUTPPMSAMPLE(ptr, v) { \ | 
50  |  |   register int val_ = v; \  | 
51  |  |   *ptr++ = (char)((val_ >> 8) & 0xFF); \  | 
52  |  |   *ptr++ = (char)(val_ & 0xFF); \  | 
53  |  | }  | 
54  |  | #define BYTESPERSAMPLE  2  | 
55  |  | #define PPM_MAXVAL  ((1 << BITS_IN_JSAMPLE) - 1)  | 
56  |  | #endif  | 
57  |  | #endif  | 
58  |  |  | 
59  |  |  | 
60  |  | /*  | 
61  |  |  * When JSAMPLE is the same size as char, we can just fwrite() the  | 
62  |  |  * decompressed data to the PPM or PGM file.  | 
63  |  |  */  | 
64  |  |  | 
65  |  |  | 
66  |  | /* Private version of data destination object */  | 
67  |  |  | 
68  |  | typedef struct { | 
69  |  |   struct djpeg_dest_struct pub; /* public fields */  | 
70  |  |  | 
71  |  |   /* Usually these two pointers point to the same place: */  | 
72  |  |   char *iobuffer;               /* fwrite's I/O buffer */  | 
73  |  |   JSAMPROW pixrow;              /* decompressor output buffer */  | 
74  |  |   size_t buffer_width;          /* width of I/O buffer */  | 
75  |  |   JDIMENSION samples_per_row;   /* JSAMPLEs per output row */  | 
76  |  | } ppm_dest_struct;  | 
77  |  |  | 
78  |  | typedef ppm_dest_struct *ppm_dest_ptr;  | 
79  |  |  | 
80  |  |  | 
81  |  | /*  | 
82  |  |  * Write some pixel data.  | 
83  |  |  * In this module rows_supplied will always be 1.  | 
84  |  |  *  | 
85  |  |  * put_pixel_rows handles the "normal" 8-bit case where the decompressor  | 
86  |  |  * output buffer is physically the same as the fwrite buffer.  | 
87  |  |  */  | 
88  |  |  | 
89  |  | METHODDEF(void)  | 
90  |  | put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,  | 
91  |  |                JDIMENSION rows_supplied)  | 
92  | 0  | { | 
93  | 0  |   ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;  | 
94  |  | 
  | 
95  | 0  |   fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);  | 
96  | 0  | }  | 
97  |  |  | 
98  |  |  | 
99  |  | /*  | 
100  |  |  * This code is used when we have to copy the data and apply a pixel  | 
101  |  |  * format translation.  Typically this only happens in 12-bit mode.  | 
102  |  |  */  | 
103  |  |  | 
104  |  | METHODDEF(void)  | 
105  |  | copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,  | 
106  |  |                 JDIMENSION rows_supplied)  | 
107  | 0  | { | 
108  | 0  |   ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;  | 
109  | 0  |   register char *bufferptr;  | 
110  | 0  |   register JSAMPROW ptr;  | 
111  |  | #if BITS_IN_JSAMPLE != 8  | 
112  |  |   register JDIMENSION col;  | 
113  |  | #endif  | 
114  |  | 
  | 
115  | 0  |   ptr = dest->pub.buffer[0];  | 
116  | 0  |   bufferptr = dest->iobuffer;  | 
117  | 0  | #if BITS_IN_JSAMPLE == 8  | 
118  | 0  |   memcpy(bufferptr, ptr, dest->samples_per_row);  | 
119  |  | #else  | 
120  |  |   for (col = dest->samples_per_row; col > 0; col--) { | 
121  |  |     PUTPPMSAMPLE(bufferptr, *ptr++);  | 
122  |  |   }  | 
123  |  | #endif  | 
124  | 0  |   fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);  | 
125  | 0  | }  | 
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  | }  | 
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(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  | }  | 
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 = cinfo->colormap[0];  | 
196  | 0  |   register JSAMPROW color_map1 = cinfo->colormap[1];  | 
197  | 0  |   register JSAMPROW color_map2 = cinfo->colormap[2];  | 
198  | 0  |   register JDIMENSION col;  | 
199  |  | 
  | 
200  | 0  |   ptr = dest->pub.buffer[0];  | 
201  | 0  |   bufferptr = dest->iobuffer;  | 
202  | 0  |   for (col = cinfo->output_width; col > 0; col--) { | 
203  | 0  |     pixval = *ptr++;  | 
204  | 0  |     PUTPPMSAMPLE(bufferptr, color_map0[pixval]);  | 
205  | 0  |     PUTPPMSAMPLE(bufferptr, color_map1[pixval]);  | 
206  | 0  |     PUTPPMSAMPLE(bufferptr, color_map2[pixval]);  | 
207  | 0  |   }  | 
208  | 0  |   fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);  | 
209  | 0  | }  | 
210  |  |  | 
211  |  |  | 
212  |  | METHODDEF(void)  | 
213  |  | put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,  | 
214  |  |                   JDIMENSION rows_supplied)  | 
215  | 0  | { | 
216  | 0  |   ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;  | 
217  | 0  |   register char *bufferptr;  | 
218  | 0  |   register JSAMPROW ptr;  | 
219  | 0  |   register JSAMPROW color_map = cinfo->colormap[0];  | 
220  | 0  |   register JDIMENSION col;  | 
221  |  | 
  | 
222  | 0  |   ptr = dest->pub.buffer[0];  | 
223  | 0  |   bufferptr = dest->iobuffer;  | 
224  | 0  |   for (col = cinfo->output_width; col > 0; col--) { | 
225  | 0  |     PUTPPMSAMPLE(bufferptr, color_map[*ptr++]);  | 
226  | 0  |   }  | 
227  | 0  |   fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);  | 
228  | 0  | }  | 
229  |  |  | 
230  |  |  | 
231  |  | /*  | 
232  |  |  * Startup: write the file header.  | 
233  |  |  */  | 
234  |  |  | 
235  |  | METHODDEF(void)  | 
236  |  | start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)  | 
237  | 0  | { | 
238  | 0  |   ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;  | 
239  |  |  | 
240  |  |   /* Emit file header */  | 
241  | 0  |   switch (cinfo->out_color_space) { | 
242  | 0  |   case JCS_GRAYSCALE:  | 
243  |  |     /* emit header for raw PGM format */  | 
244  | 0  |     fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",  | 
245  | 0  |             (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);  | 
246  | 0  |     break;  | 
247  | 0  |   case JCS_RGB:  | 
248  | 0  |   case JCS_EXT_RGB:  | 
249  | 0  |   case JCS_EXT_RGBX:  | 
250  | 0  |   case JCS_EXT_BGR:  | 
251  | 0  |   case JCS_EXT_BGRX:  | 
252  | 0  |   case JCS_EXT_XBGR:  | 
253  | 0  |   case JCS_EXT_XRGB:  | 
254  | 0  |   case JCS_EXT_RGBA:  | 
255  | 0  |   case JCS_EXT_BGRA:  | 
256  | 0  |   case JCS_EXT_ABGR:  | 
257  | 0  |   case JCS_EXT_ARGB:  | 
258  | 0  |   case JCS_CMYK:  | 
259  | 0  |     if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)  | 
260  | 0  |       ERREXIT(cinfo, JERR_PPM_COLORSPACE);  | 
261  |  |     /* emit header for raw PPM format */  | 
262  | 0  |     fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",  | 
263  | 0  |             (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);  | 
264  | 0  |     break;  | 
265  | 0  |   default:  | 
266  | 0  |     ERREXIT(cinfo, JERR_PPM_COLORSPACE);  | 
267  | 0  |   }  | 
268  | 0  | }  | 
269  |  |  | 
270  |  |  | 
271  |  | /*  | 
272  |  |  * Finish up at the end of the file.  | 
273  |  |  */  | 
274  |  |  | 
275  |  | METHODDEF(void)  | 
276  |  | finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)  | 
277  | 0  | { | 
278  |  |   /* Make sure we wrote the output file OK */  | 
279  | 0  |   fflush(dinfo->output_file);  | 
280  | 0  |   if (ferror(dinfo->output_file))  | 
281  | 0  |     ERREXIT(cinfo, JERR_FILE_WRITE);  | 
282  | 0  | }  | 
283  |  |  | 
284  |  |  | 
285  |  | /*  | 
286  |  |  * Re-calculate buffer dimensions based on output dimensions.  | 
287  |  |  */  | 
288  |  |  | 
289  |  | METHODDEF(void)  | 
290  |  | calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)  | 
291  | 0  | { | 
292  | 0  |   ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;  | 
293  |  | 
  | 
294  | 0  |   if (cinfo->out_color_space == JCS_GRAYSCALE)  | 
295  | 0  |     dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;  | 
296  | 0  |   else  | 
297  | 0  |     dest->samples_per_row = cinfo->output_width * 3;  | 
298  | 0  |   dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));  | 
299  | 0  | }  | 
300  |  |  | 
301  |  |  | 
302  |  | /*  | 
303  |  |  * The module selection routine for PPM format output.  | 
304  |  |  */  | 
305  |  |  | 
306  |  | GLOBAL(djpeg_dest_ptr)  | 
307  |  | jinit_write_ppm(j_decompress_ptr cinfo)  | 
308  | 0  | { | 
309  | 0  |   ppm_dest_ptr dest;  | 
310  |  |  | 
311  |  |   /* Create module interface object, fill in method pointers */  | 
312  | 0  |   dest = (ppm_dest_ptr)  | 
313  | 0  |       (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,  | 
314  | 0  |                                   sizeof(ppm_dest_struct));  | 
315  | 0  |   dest->pub.start_output = start_output_ppm;  | 
316  | 0  |   dest->pub.finish_output = finish_output_ppm;  | 
317  | 0  |   dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;  | 
318  |  |  | 
319  |  |   /* Calculate output image dimensions so we can allocate space */  | 
320  | 0  |   jpeg_calc_output_dimensions(cinfo);  | 
321  |  |  | 
322  |  |   /* Create physical I/O buffer */  | 
323  | 0  |   dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);  | 
324  | 0  |   dest->iobuffer = (char *)(*cinfo->mem->alloc_small)  | 
325  | 0  |     ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);  | 
326  |  | 
  | 
327  | 0  |   if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||  | 
328  | 0  |       sizeof(JSAMPLE) != sizeof(char) ||  | 
329  | 0  | #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3  | 
330  | 0  |       (cinfo->out_color_space != JCS_EXT_RGB &&  | 
331  | 0  |        cinfo->out_color_space != JCS_RGB)) { | 
332  |  | #else  | 
333  |  |       cinfo->out_color_space != JCS_EXT_RGB) { | 
334  |  | #endif  | 
335  |  |     /* When quantizing, we need an output buffer for colormap indexes  | 
336  |  |      * that's separate from the physical I/O buffer.  We also need a  | 
337  |  |      * separate buffer if pixel format translation must take place.  | 
338  |  |      */  | 
339  | 0  |     dest->pub.buffer = (*cinfo->mem->alloc_sarray)  | 
340  | 0  |       ((j_common_ptr)cinfo, JPOOL_IMAGE,  | 
341  | 0  |        cinfo->output_width * cinfo->output_components, (JDIMENSION)1);  | 
342  | 0  |     dest->pub.buffer_height = 1;  | 
343  | 0  |     if (!cinfo->quantize_colors) { | 
344  | 0  |       if (IsExtRGB(cinfo->out_color_space))  | 
345  | 0  |         dest->pub.put_pixel_rows = put_rgb;  | 
346  | 0  |       else if (cinfo->out_color_space == JCS_CMYK)  | 
347  | 0  |         dest->pub.put_pixel_rows = put_cmyk;  | 
348  | 0  |       else  | 
349  | 0  |         dest->pub.put_pixel_rows = copy_pixel_rows;  | 
350  | 0  |     } else if (cinfo->out_color_space == JCS_GRAYSCALE)  | 
351  | 0  |       dest->pub.put_pixel_rows = put_demapped_gray;  | 
352  | 0  |     else  | 
353  | 0  |       dest->pub.put_pixel_rows = put_demapped_rgb;  | 
354  | 0  |   } else { | 
355  |  |     /* We will fwrite() directly from decompressor output buffer. */  | 
356  |  |     /* Synthesize a JSAMPARRAY pointer structure */  | 
357  | 0  |     dest->pixrow = (JSAMPROW)dest->iobuffer;  | 
358  | 0  |     dest->pub.buffer = &dest->pixrow;  | 
359  | 0  |     dest->pub.buffer_height = 1;  | 
360  | 0  |     dest->pub.put_pixel_rows = put_pixel_rows;  | 
361  | 0  |   }  | 
362  |  | 
  | 
363  | 0  |   return (djpeg_dest_ptr)dest;  | 
364  | 0  | }  | 
365  |  |  | 
366  |  | #endif /* PPM_SUPPORTED */  |