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
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).
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.
23 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
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
34 * (When the core library supports data precision reduction, a cleaner
35 * implementation will be to ask for that instead.)
38 #if BITS_IN_JSAMPLE == 8
39 #define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)(v)
40 #define BYTESPERSAMPLE 1
41 #define PPM_MAXVAL 255
44 #define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)((v) >> (BITS_IN_JSAMPLE - 8))
45 #define BYTESPERSAMPLE 1
46 #define PPM_MAXVAL 255
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); \
54 #define BYTESPERSAMPLE 2
55 #define PPM_MAXVAL ((1 << BITS_IN_JSAMPLE) - 1)
61 * When JSAMPLE is the same size as char, we can just fwrite() the
62 * decompressed data to the PPM or PGM file.
66 /* Private version of data destination object */
69 struct djpeg_dest_struct pub; /* public fields */
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 */
78 typedef ppm_dest_struct *ppm_dest_ptr;
82 * Write some pixel data.
83 * In this module rows_supplied will always be 1.
85 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
86 * output buffer is physically the same as the fwrite buffer.
90 put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
91 JDIMENSION rows_supplied)
93 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
95 fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
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.
105 copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
106 JDIMENSION rows_supplied)
108 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
109 register char *bufferptr;
110 register JSAMPROW ptr;
111 #if BITS_IN_JSAMPLE != 8
112 register JDIMENSION col;
115 ptr = dest->pub.buffer[0];
116 bufferptr = dest->iobuffer;
117 #if BITS_IN_JSAMPLE == 8
118 memcpy(bufferptr, ptr, dest->samples_per_row);
120 for (col = dest->samples_per_row; col > 0; col--) {
121 PUTPPMSAMPLE(bufferptr, *ptr++);
124 fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
129 * Convert extended RGB to RGB.
133 put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
135 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
136 register char *bufferptr;
137 register JSAMPROW ptr;
138 register JDIMENSION col;
139 register int rindex = rgb_red[cinfo->out_color_space];
140 register int gindex = rgb_green[cinfo->out_color_space];
141 register int bindex = rgb_blue[cinfo->out_color_space];
142 register int ps = rgb_pixelsize[cinfo->out_color_space];
144 ptr = dest->pub.buffer[0];
145 bufferptr = dest->iobuffer;
146 for (col = cinfo->output_width; col > 0; col--) {
147 PUTPPMSAMPLE(bufferptr, ptr[rindex]);
148 PUTPPMSAMPLE(bufferptr, ptr[gindex]);
149 PUTPPMSAMPLE(bufferptr, ptr[bindex]);
152 fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
157 * Convert CMYK to RGB.
161 put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
162 JDIMENSION rows_supplied)
164 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
165 register char *bufferptr;
166 register JSAMPROW ptr;
167 register JDIMENSION col;
169 ptr = dest->pub.buffer[0];
170 bufferptr = dest->iobuffer;
171 for (col = cinfo->output_width; col > 0; col--) {
172 JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
173 cmyk_to_rgb(c, m, y, k, &r, &g, &b);
174 PUTPPMSAMPLE(bufferptr, r);
175 PUTPPMSAMPLE(bufferptr, g);
176 PUTPPMSAMPLE(bufferptr, b);
178 fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
183 * Write some pixel data when color quantization is in effect.
184 * We have to demap the color index values to straight data.
188 put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
189 JDIMENSION rows_supplied)
191 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
192 register char *bufferptr;
194 register JSAMPROW ptr;
195 register JSAMPROW color_map0 = cinfo->colormap[0];
196 register JSAMPROW color_map1 = cinfo->colormap[1];
197 register JSAMPROW color_map2 = cinfo->colormap[2];
198 register JDIMENSION col;
200 ptr = dest->pub.buffer[0];
201 bufferptr = dest->iobuffer;
202 for (col = cinfo->output_width; col > 0; col--) {
204 PUTPPMSAMPLE(bufferptr, color_map0[pixval]);
205 PUTPPMSAMPLE(bufferptr, color_map1[pixval]);
206 PUTPPMSAMPLE(bufferptr, color_map2[pixval]);
208 fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
213 put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
214 JDIMENSION rows_supplied)
216 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
217 register char *bufferptr;
218 register JSAMPROW ptr;
219 register JSAMPROW color_map = cinfo->colormap[0];
220 register JDIMENSION col;
222 ptr = dest->pub.buffer[0];
223 bufferptr = dest->iobuffer;
224 for (col = cinfo->output_width; col > 0; col--) {
225 PUTPPMSAMPLE(bufferptr, color_map[*ptr++]);
227 fwrite(dest->iobuffer, 1, dest->buffer_width, dest->pub.output_file);
232 * Startup: write the file header.
236 start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
238 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
240 /* Emit file header */
241 switch (cinfo->out_color_space) {
243 /* emit header for raw PGM format */
244 fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
245 (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
259 if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)
260 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
261 /* emit header for raw PPM format */
262 fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
263 (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
266 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
272 * Finish up at the end of the file.
276 finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
278 /* Make sure we wrote the output file OK */
279 fflush(dinfo->output_file);
280 if (ferror(dinfo->output_file))
281 ERREXIT(cinfo, JERR_FILE_WRITE);
286 * Re-calculate buffer dimensions based on output dimensions.
290 calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
292 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
294 if (cinfo->out_color_space == JCS_GRAYSCALE)
295 dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
297 dest->samples_per_row = cinfo->output_width * 3;
298 dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
303 * The module selection routine for PPM format output.
306 GLOBAL(djpeg_dest_ptr)
307 jinit_write_ppm(j_decompress_ptr cinfo)
311 /* Create module interface object, fill in method pointers */
312 dest = (ppm_dest_ptr)
313 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
314 sizeof(ppm_dest_struct));
315 dest->pub.start_output = start_output_ppm;
316 dest->pub.finish_output = finish_output_ppm;
317 dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
319 /* Calculate output image dimensions so we can allocate space */
320 jpeg_calc_output_dimensions(cinfo);
322 /* Create physical I/O buffer */
323 dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
324 dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
325 ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
327 if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
328 sizeof(JSAMPLE) != sizeof(char) ||
329 #if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
330 (cinfo->out_color_space != JCS_EXT_RGB &&
331 cinfo->out_color_space != JCS_RGB)) {
333 cinfo->out_color_space != JCS_EXT_RGB) {
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.
339 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
340 ((j_common_ptr)cinfo, JPOOL_IMAGE,
341 cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
342 dest->pub.buffer_height = 1;
343 if (!cinfo->quantize_colors) {
344 if (IsExtRGB(cinfo->out_color_space))
345 dest->pub.put_pixel_rows = put_rgb;
346 else if (cinfo->out_color_space == JCS_CMYK)
347 dest->pub.put_pixel_rows = put_cmyk;
349 dest->pub.put_pixel_rows = copy_pixel_rows;
350 } else if (cinfo->out_color_space == JCS_GRAYSCALE)
351 dest->pub.put_pixel_rows = put_demapped_gray;
353 dest->pub.put_pixel_rows = put_demapped_rgb;
355 /* We will fwrite() directly from decompressor output buffer. */
356 /* Synthesize a JSAMPARRAY pointer structure */
357 dest->pixrow = (JSAMPROW)dest->iobuffer;
358 dest->pub.buffer = &dest->pixrow;
359 dest->pub.buffer_height = 1;
360 dest->pub.put_pixel_rows = put_pixel_rows;
363 return (djpeg_dest_ptr)dest;
366 #endif /* PPM_SUPPORTED */