1c2c66affSColin Finck /*
2c2c66affSColin Finck * wrppm.c
3c2c66affSColin Finck *
4c2c66affSColin Finck * Copyright (C) 1991-1996, Thomas G. Lane.
5*1d574191SThomas Faber * Modified 2009-2019 by Guido Vollbeding.
6c2c66affSColin Finck * This file is part of the Independent JPEG Group's software.
7c2c66affSColin Finck * For conditions of distribution and use, see the accompanying README file.
8c2c66affSColin Finck *
9c2c66affSColin Finck * This file contains routines to write output images in PPM/PGM format.
10c2c66affSColin Finck * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
11c2c66affSColin Finck * The PBMPLUS library is NOT required to compile this software
12c2c66affSColin Finck * (but it is highly useful as a set of PPM image manipulation programs).
13c2c66affSColin Finck *
14c2c66affSColin Finck * These routines may need modification for non-Unix environments or
15c2c66affSColin Finck * specialized applications. As they stand, they assume output to
16c2c66affSColin Finck * an ordinary stdio stream.
17c2c66affSColin Finck */
18c2c66affSColin Finck
19c2c66affSColin Finck #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
20c2c66affSColin Finck
21c2c66affSColin Finck #ifdef PPM_SUPPORTED
22c2c66affSColin Finck
23c2c66affSColin Finck
24c2c66affSColin Finck /*
25c2c66affSColin Finck * For 12-bit JPEG data, we either downscale the values to 8 bits
26c2c66affSColin Finck * (to write standard byte-per-sample PPM/PGM files), or output
27c2c66affSColin Finck * nonstandard word-per-sample PPM/PGM files. Downscaling is done
28c2c66affSColin Finck * if PPM_NORAWWORD is defined (this can be done in the Makefile
29c2c66affSColin Finck * or in jconfig.h).
30c2c66affSColin Finck * (When the core library supports data precision reduction, a cleaner
31c2c66affSColin Finck * implementation will be to ask for that instead.)
32c2c66affSColin Finck */
33c2c66affSColin Finck
34c2c66affSColin Finck #if BITS_IN_JSAMPLE == 8
35c2c66affSColin Finck #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
36c2c66affSColin Finck #define BYTESPERSAMPLE 1
37c2c66affSColin Finck #define PPM_MAXVAL 255
38c2c66affSColin Finck #else
39c2c66affSColin Finck #ifdef PPM_NORAWWORD
40c2c66affSColin Finck #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
41c2c66affSColin Finck #define BYTESPERSAMPLE 1
42c2c66affSColin Finck #define PPM_MAXVAL 255
43c2c66affSColin Finck #else
44c2c66affSColin Finck /* The word-per-sample format always puts the MSB first. */
45c2c66affSColin Finck #define PUTPPMSAMPLE(ptr,v) \
46c2c66affSColin Finck { register int val_ = v; \
47c2c66affSColin Finck *ptr++ = (char) ((val_ >> 8) & 0xFF); \
48c2c66affSColin Finck *ptr++ = (char) (val_ & 0xFF); \
49c2c66affSColin Finck }
50c2c66affSColin Finck #define BYTESPERSAMPLE 2
51c2c66affSColin Finck #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
52c2c66affSColin Finck #endif
53c2c66affSColin Finck #endif
54c2c66affSColin Finck
55c2c66affSColin Finck
56c2c66affSColin Finck /*
57c2c66affSColin Finck * When JSAMPLE is the same size as char, we can just fwrite() the
58c2c66affSColin Finck * decompressed data to the PPM or PGM file. On PCs, in order to make this
59c2c66affSColin Finck * work the output buffer must be allocated in near data space, because we are
60c2c66affSColin Finck * assuming small-data memory model wherein fwrite() can't reach far memory.
61c2c66affSColin Finck * If you need to process very wide images on a PC, you might have to compile
62c2c66affSColin Finck * in large-memory model, or else replace fwrite() with a putc() loop ---
63c2c66affSColin Finck * which will be much slower.
64c2c66affSColin Finck */
65c2c66affSColin Finck
66c2c66affSColin Finck
67c2c66affSColin Finck /* Private version of data destination object */
68c2c66affSColin Finck
69c2c66affSColin Finck typedef struct {
70c2c66affSColin Finck struct djpeg_dest_struct pub; /* public fields */
71c2c66affSColin Finck
72c2c66affSColin Finck /* Usually these two pointers point to the same place: */
73c2c66affSColin Finck char *iobuffer; /* fwrite's I/O buffer */
74c2c66affSColin Finck JSAMPROW pixrow; /* decompressor output buffer */
75c2c66affSColin Finck size_t buffer_width; /* width of I/O buffer */
76c2c66affSColin Finck JDIMENSION samples_per_row; /* JSAMPLEs per output row */
77c2c66affSColin Finck } ppm_dest_struct;
78c2c66affSColin Finck
79c2c66affSColin Finck typedef ppm_dest_struct * ppm_dest_ptr;
80c2c66affSColin Finck
81c2c66affSColin Finck
82c2c66affSColin Finck /*
83c2c66affSColin Finck * Write some pixel data.
84c2c66affSColin Finck * In this module rows_supplied will always be 1.
85c2c66affSColin Finck *
86c2c66affSColin Finck * put_pixel_rows handles the "normal" 8-bit case where the decompressor
87c2c66affSColin Finck * output buffer is physically the same as the fwrite buffer.
88c2c66affSColin Finck */
89c2c66affSColin Finck
90c2c66affSColin Finck METHODDEF(void)
put_pixel_rows(j_decompress_ptr cinfo,djpeg_dest_ptr dinfo,JDIMENSION rows_supplied)91c2c66affSColin Finck put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
92c2c66affSColin Finck JDIMENSION rows_supplied)
93c2c66affSColin Finck {
94c2c66affSColin Finck ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
95c2c66affSColin Finck
96c2c66affSColin Finck (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
97c2c66affSColin Finck }
98c2c66affSColin Finck
99c2c66affSColin Finck
100c2c66affSColin Finck /*
101c2c66affSColin Finck * This code is used when we have to copy the data and apply a pixel
102c2c66affSColin Finck * format translation. Typically this only happens in 12-bit mode.
103c2c66affSColin Finck */
104c2c66affSColin Finck
105c2c66affSColin Finck METHODDEF(void)
copy_pixel_rows(j_decompress_ptr cinfo,djpeg_dest_ptr dinfo,JDIMENSION rows_supplied)106c2c66affSColin Finck copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
107c2c66affSColin Finck JDIMENSION rows_supplied)
108c2c66affSColin Finck {
109c2c66affSColin Finck ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
110c2c66affSColin Finck register char * bufferptr;
111c2c66affSColin Finck register JSAMPROW ptr;
112c2c66affSColin Finck register JDIMENSION col;
113c2c66affSColin Finck
114c2c66affSColin Finck ptr = dest->pub.buffer[0];
115c2c66affSColin Finck bufferptr = dest->iobuffer;
116c2c66affSColin Finck for (col = dest->samples_per_row; col > 0; col--) {
117c2c66affSColin Finck PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
118c2c66affSColin Finck }
119c2c66affSColin Finck (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
120c2c66affSColin Finck }
121c2c66affSColin Finck
122c2c66affSColin Finck
123c2c66affSColin Finck /*
124c2c66affSColin Finck * Write some pixel data when color quantization is in effect.
125c2c66affSColin Finck * We have to demap the color index values to straight data.
126c2c66affSColin Finck */
127c2c66affSColin Finck
128c2c66affSColin Finck METHODDEF(void)
put_demapped_rgb(j_decompress_ptr cinfo,djpeg_dest_ptr dinfo,JDIMENSION rows_supplied)129c2c66affSColin Finck put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
130c2c66affSColin Finck JDIMENSION rows_supplied)
131c2c66affSColin Finck {
132c2c66affSColin Finck ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
133c2c66affSColin Finck register char * bufferptr;
134c2c66affSColin Finck register int pixval;
135c2c66affSColin Finck register JSAMPROW ptr;
136c2c66affSColin Finck register JSAMPROW color_map0 = cinfo->colormap[0];
137c2c66affSColin Finck register JSAMPROW color_map1 = cinfo->colormap[1];
138c2c66affSColin Finck register JSAMPROW color_map2 = cinfo->colormap[2];
139c2c66affSColin Finck register JDIMENSION col;
140c2c66affSColin Finck
141c2c66affSColin Finck ptr = dest->pub.buffer[0];
142c2c66affSColin Finck bufferptr = dest->iobuffer;
143c2c66affSColin Finck for (col = cinfo->output_width; col > 0; col--) {
144c2c66affSColin Finck pixval = GETJSAMPLE(*ptr++);
145c2c66affSColin Finck PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
146c2c66affSColin Finck PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
147c2c66affSColin Finck PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
148c2c66affSColin Finck }
149c2c66affSColin Finck (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
150c2c66affSColin Finck }
151c2c66affSColin Finck
152c2c66affSColin Finck METHODDEF(void)
put_demapped_gray(j_decompress_ptr cinfo,djpeg_dest_ptr dinfo,JDIMENSION rows_supplied)153c2c66affSColin Finck put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
154c2c66affSColin Finck JDIMENSION rows_supplied)
155c2c66affSColin Finck {
156c2c66affSColin Finck ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
157c2c66affSColin Finck register char * bufferptr;
158c2c66affSColin Finck register JSAMPROW ptr;
159*1d574191SThomas Faber register JSAMPROW color_map0 = cinfo->colormap[0];
160c2c66affSColin Finck register JDIMENSION col;
161c2c66affSColin Finck
162c2c66affSColin Finck ptr = dest->pub.buffer[0];
163c2c66affSColin Finck bufferptr = dest->iobuffer;
164c2c66affSColin Finck for (col = cinfo->output_width; col > 0; col--) {
165*1d574191SThomas Faber PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[GETJSAMPLE(*ptr++)]));
166c2c66affSColin Finck }
167c2c66affSColin Finck (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
168c2c66affSColin Finck }
169c2c66affSColin Finck
170c2c66affSColin Finck
171c2c66affSColin Finck /*
172c2c66affSColin Finck * Startup: write the file header.
173c2c66affSColin Finck */
174c2c66affSColin Finck
175c2c66affSColin Finck METHODDEF(void)
start_output_ppm(j_decompress_ptr cinfo,djpeg_dest_ptr dinfo)176c2c66affSColin Finck start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
177c2c66affSColin Finck {
178c2c66affSColin Finck /* Emit file header */
179c2c66affSColin Finck switch (cinfo->out_color_space) {
180c2c66affSColin Finck case JCS_GRAYSCALE:
181c2c66affSColin Finck /* emit header for raw PGM format */
182*1d574191SThomas Faber fprintf(dinfo->output_file, "P5\n%ld %ld\n%d\n",
183c2c66affSColin Finck (long) cinfo->output_width, (long) cinfo->output_height,
184c2c66affSColin Finck PPM_MAXVAL);
185c2c66affSColin Finck break;
186c2c66affSColin Finck case JCS_RGB:
187c2c66affSColin Finck /* emit header for raw PPM format */
188*1d574191SThomas Faber fprintf(dinfo->output_file, "P6\n%ld %ld\n%d\n",
189c2c66affSColin Finck (long) cinfo->output_width, (long) cinfo->output_height,
190c2c66affSColin Finck PPM_MAXVAL);
191c2c66affSColin Finck break;
192c2c66affSColin Finck default:
193c2c66affSColin Finck ERREXIT(cinfo, JERR_PPM_COLORSPACE);
194c2c66affSColin Finck }
195c2c66affSColin Finck }
196c2c66affSColin Finck
197c2c66affSColin Finck
198c2c66affSColin Finck /*
199c2c66affSColin Finck * Finish up at the end of the file.
200c2c66affSColin Finck */
201c2c66affSColin Finck
202c2c66affSColin Finck METHODDEF(void)
finish_output_ppm(j_decompress_ptr cinfo,djpeg_dest_ptr dinfo)203c2c66affSColin Finck finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
204c2c66affSColin Finck {
205c2c66affSColin Finck /* Make sure we wrote the output file OK */
206ef4f5757SThomas Faber JFFLUSH(dinfo->output_file);
207ef4f5757SThomas Faber if (JFERROR(dinfo->output_file))
208c2c66affSColin Finck ERREXIT(cinfo, JERR_FILE_WRITE);
209c2c66affSColin Finck }
210c2c66affSColin Finck
211c2c66affSColin Finck
212c2c66affSColin Finck /*
213c2c66affSColin Finck * The module selection routine for PPM format output.
214c2c66affSColin Finck */
215c2c66affSColin Finck
216c2c66affSColin Finck GLOBAL(djpeg_dest_ptr)
jinit_write_ppm(j_decompress_ptr cinfo)217c2c66affSColin Finck jinit_write_ppm (j_decompress_ptr cinfo)
218c2c66affSColin Finck {
219c2c66affSColin Finck ppm_dest_ptr dest;
220c2c66affSColin Finck
221c2c66affSColin Finck /* Create module interface object, fill in method pointers */
222*1d574191SThomas Faber dest = (ppm_dest_ptr) (*cinfo->mem->alloc_small)
223*1d574191SThomas Faber ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(ppm_dest_struct));
224c2c66affSColin Finck dest->pub.start_output = start_output_ppm;
225c2c66affSColin Finck dest->pub.finish_output = finish_output_ppm;
226c2c66affSColin Finck
227c2c66affSColin Finck /* Calculate output image dimensions so we can allocate space */
228c2c66affSColin Finck jpeg_calc_output_dimensions(cinfo);
229c2c66affSColin Finck
230c2c66affSColin Finck /* Create physical I/O buffer. Note we make this near on a PC. */
231c2c66affSColin Finck dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
232c2c66affSColin Finck dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
233c2c66affSColin Finck dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
234c2c66affSColin Finck ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
235c2c66affSColin Finck
236c2c66affSColin Finck if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
237c2c66affSColin Finck SIZEOF(JSAMPLE) != SIZEOF(char)) {
238c2c66affSColin Finck /* When quantizing, we need an output buffer for colormap indexes
239c2c66affSColin Finck * that's separate from the physical I/O buffer. We also need a
240c2c66affSColin Finck * separate buffer if pixel format translation must take place.
241c2c66affSColin Finck */
242c2c66affSColin Finck dest->pub.buffer = (*cinfo->mem->alloc_sarray)
243c2c66affSColin Finck ((j_common_ptr) cinfo, JPOOL_IMAGE,
244c2c66affSColin Finck cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
245c2c66affSColin Finck dest->pub.buffer_height = 1;
246c2c66affSColin Finck if (! cinfo->quantize_colors)
247c2c66affSColin Finck dest->pub.put_pixel_rows = copy_pixel_rows;
248c2c66affSColin Finck else if (cinfo->out_color_space == JCS_GRAYSCALE)
249c2c66affSColin Finck dest->pub.put_pixel_rows = put_demapped_gray;
250c2c66affSColin Finck else
251c2c66affSColin Finck dest->pub.put_pixel_rows = put_demapped_rgb;
252c2c66affSColin Finck } else {
253c2c66affSColin Finck /* We will fwrite() directly from decompressor output buffer. */
254c2c66affSColin Finck /* Synthesize a JSAMPARRAY pointer structure */
255c2c66affSColin Finck /* Cast here implies near->far pointer conversion on PCs */
256c2c66affSColin Finck dest->pixrow = (JSAMPROW) dest->iobuffer;
257c2c66affSColin Finck dest->pub.buffer = & dest->pixrow;
258c2c66affSColin Finck dest->pub.buffer_height = 1;
259c2c66affSColin Finck dest->pub.put_pixel_rows = put_pixel_rows;
260c2c66affSColin Finck }
261c2c66affSColin Finck
262ef4f5757SThomas Faber return &dest->pub;
263c2c66affSColin Finck }
264c2c66affSColin Finck
265c2c66affSColin Finck #endif /* PPM_SUPPORTED */
266