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