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