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 * It was modified by The libjpeg-turbo Project to include only code and
8 * information relevant to libjpeg-turbo.
9 * For conditions of distribution and use, see the accompanying README file.
11 * This file contains routines to write output images in PPM/PGM format.
12 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
13 * The PBMPLUS library is NOT required to compile this software
14 * (but it is highly useful as a set of PPM image manipulation programs).
16 * These routines may need modification for non-Unix environments or
17 * specialized applications. As they stand, they assume output to
18 * an ordinary stdio stream.
21 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
27 * For 12-bit JPEG data, we either downscale the values to 8 bits
28 * (to write standard byte-per-sample PPM/PGM files), or output
29 * nonstandard word-per-sample PPM/PGM files. Downscaling is done
30 * if PPM_NORAWWORD is defined (this can be done in the Makefile
32 * (When the core library supports data precision reduction, a cleaner
33 * implementation will be to ask for that instead.)
36 #if BITS_IN_JSAMPLE == 8
37 #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
38 #define BYTESPERSAMPLE 1
39 #define PPM_MAXVAL 255
42 #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
43 #define BYTESPERSAMPLE 1
44 #define PPM_MAXVAL 255
46 /* The word-per-sample format always puts the MSB first. */
47 #define PUTPPMSAMPLE(ptr,v) \
48 { register int val_ = v; \
49 *ptr++ = (char) ((val_ >> 8) & 0xFF); \
50 *ptr++ = (char) (val_ & 0xFF); \
52 #define BYTESPERSAMPLE 2
53 #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
59 * When JSAMPLE is the same size as char, we can just fwrite() the
60 * decompressed data to the PPM or PGM file.
64 /* Private version of data destination object */
67 struct djpeg_dest_struct pub; /* public fields */
69 /* Usually these two pointers point to the same place: */
70 char *iobuffer; /* fwrite's I/O buffer */
71 JSAMPROW pixrow; /* decompressor output buffer */
72 size_t buffer_width; /* width of I/O buffer */
73 JDIMENSION samples_per_row; /* JSAMPLEs per output row */
76 typedef ppm_dest_struct * ppm_dest_ptr;
80 * Write some pixel data.
81 * In this module rows_supplied will always be 1.
83 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
84 * output buffer is physically the same as the fwrite buffer.
88 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
89 JDIMENSION rows_supplied)
91 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
93 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
98 * This code is used when we have to copy the data and apply a pixel
99 * format translation. Typically this only happens in 12-bit mode.
103 copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
104 JDIMENSION rows_supplied)
106 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
107 register char * bufferptr;
108 register JSAMPROW ptr;
109 register JDIMENSION col;
111 ptr = dest->pub.buffer[0];
112 bufferptr = dest->iobuffer;
113 for (col = dest->samples_per_row; col > 0; col--) {
114 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
116 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
121 * Write some pixel data when color quantization is in effect.
122 * We have to demap the color index values to straight data.
126 put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
127 JDIMENSION rows_supplied)
129 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
130 register char * bufferptr;
132 register JSAMPROW ptr;
133 register JSAMPROW color_map0 = cinfo->colormap[0];
134 register JSAMPROW color_map1 = cinfo->colormap[1];
135 register JSAMPROW color_map2 = cinfo->colormap[2];
136 register JDIMENSION col;
138 ptr = dest->pub.buffer[0];
139 bufferptr = dest->iobuffer;
140 for (col = cinfo->output_width; col > 0; col--) {
141 pixval = GETJSAMPLE(*ptr++);
142 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
143 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
144 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
146 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
151 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
152 JDIMENSION rows_supplied)
154 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
155 register char * bufferptr;
156 register JSAMPROW ptr;
157 register JSAMPROW color_map = cinfo->colormap[0];
158 register JDIMENSION col;
160 ptr = dest->pub.buffer[0];
161 bufferptr = dest->iobuffer;
162 for (col = cinfo->output_width; col > 0; col--) {
163 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
165 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
170 * Startup: write the file header.
174 start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
176 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
178 /* Emit file header */
179 switch (cinfo->out_color_space) {
181 /* emit header for raw PGM format */
182 fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
183 (long) cinfo->output_width, (long) cinfo->output_height,
187 /* emit header for raw PPM format */
188 fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
189 (long) cinfo->output_width, (long) cinfo->output_height,
193 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
199 * Finish up at the end of the file.
203 finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
205 /* Make sure we wrote the output file OK */
206 fflush(dinfo->output_file);
207 if (ferror(dinfo->output_file))
208 ERREXIT(cinfo, JERR_FILE_WRITE);
213 * The module selection routine for PPM format output.
216 GLOBAL(djpeg_dest_ptr)
217 jinit_write_ppm (j_decompress_ptr cinfo)
221 /* Create module interface object, fill in method pointers */
222 dest = (ppm_dest_ptr)
223 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
224 sizeof(ppm_dest_struct));
225 dest->pub.start_output = start_output_ppm;
226 dest->pub.finish_output = finish_output_ppm;
228 /* Calculate output image dimensions so we can allocate space */
229 jpeg_calc_output_dimensions(cinfo);
231 /* Create physical I/O buffer */
232 dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
233 dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
234 dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
235 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
237 if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
238 sizeof(JSAMPLE) != sizeof(char)) {
239 /* When quantizing, we need an output buffer for colormap indexes
240 * that's separate from the physical I/O buffer. We also need a
241 * separate buffer if pixel format translation must take place.
243 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
244 ((j_common_ptr) cinfo, JPOOL_IMAGE,
245 cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
246 dest->pub.buffer_height = 1;
247 if (! cinfo->quantize_colors)
248 dest->pub.put_pixel_rows = copy_pixel_rows;
249 else if (cinfo->out_color_space == JCS_GRAYSCALE)
250 dest->pub.put_pixel_rows = put_demapped_gray;
252 dest->pub.put_pixel_rows = put_demapped_rgb;
254 /* We will fwrite() directly from decompressor output buffer. */
255 /* Synthesize a JSAMPARRAY pointer structure */
256 dest->pixrow = (JSAMPROW) dest->iobuffer;
257 dest->pub.buffer = & dest->pixrow;
258 dest->pub.buffer_height = 1;
259 dest->pub.put_pixel_rows = put_pixel_rows;
262 return (djpeg_dest_ptr) dest;
265 #endif /* PPM_SUPPORTED */