Imported Upstream version 1.4.2
[platform/upstream/libjpeg-turbo.git] / wrppm.c
1 /*
2  * wrppm.c
3  *
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.
10  *
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).
15  *
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.
19  */
20
21 #include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
22
23 #ifdef PPM_SUPPORTED
24
25
26 /*
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
31  * or in jconfig.h).
32  * (When the core library supports data precision reduction, a cleaner
33  * implementation will be to ask for that instead.)
34  */
35
36 #if BITS_IN_JSAMPLE == 8
37 #define PUTPPMSAMPLE(ptr,v)  *ptr++ = (char) (v)
38 #define BYTESPERSAMPLE 1
39 #define PPM_MAXVAL 255
40 #else
41 #ifdef PPM_NORAWWORD
42 #define PUTPPMSAMPLE(ptr,v)  *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
43 #define BYTESPERSAMPLE 1
44 #define PPM_MAXVAL 255
45 #else
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);        \
51         }
52 #define BYTESPERSAMPLE 2
53 #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
54 #endif
55 #endif
56
57
58 /*
59  * When JSAMPLE is the same size as char, we can just fwrite() the
60  * decompressed data to the PPM or PGM file.
61  */
62
63
64 /* Private version of data destination object */
65
66 typedef struct {
67   struct djpeg_dest_struct pub; /* public fields */
68
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 */
74 } ppm_dest_struct;
75
76 typedef ppm_dest_struct * ppm_dest_ptr;
77
78
79 /*
80  * Write some pixel data.
81  * In this module rows_supplied will always be 1.
82  *
83  * put_pixel_rows handles the "normal" 8-bit case where the decompressor
84  * output buffer is physically the same as the fwrite buffer.
85  */
86
87 METHODDEF(void)
88 put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
89                 JDIMENSION rows_supplied)
90 {
91   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
92
93   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
94 }
95
96
97 /*
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.
100  */
101
102 METHODDEF(void)
103 copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
104                  JDIMENSION rows_supplied)
105 {
106   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
107   register char * bufferptr;
108   register JSAMPROW ptr;
109   register JDIMENSION col;
110
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++));
115   }
116   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
117 }
118
119
120 /*
121  * Write some pixel data when color quantization is in effect.
122  * We have to demap the color index values to straight data.
123  */
124
125 METHODDEF(void)
126 put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
127                   JDIMENSION rows_supplied)
128 {
129   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
130   register char * bufferptr;
131   register int pixval;
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;
137
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]));
145   }
146   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
147 }
148
149
150 METHODDEF(void)
151 put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
152                    JDIMENSION rows_supplied)
153 {
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;
159
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++)]));
164   }
165   (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
166 }
167
168
169 /*
170  * Startup: write the file header.
171  */
172
173 METHODDEF(void)
174 start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
175 {
176   ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
177
178   /* Emit file header */
179   switch (cinfo->out_color_space) {
180   case JCS_GRAYSCALE:
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,
184             PPM_MAXVAL);
185     break;
186   case JCS_RGB:
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,
190             PPM_MAXVAL);
191     break;
192   default:
193     ERREXIT(cinfo, JERR_PPM_COLORSPACE);
194   }
195 }
196
197
198 /*
199  * Finish up at the end of the file.
200  */
201
202 METHODDEF(void)
203 finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
204 {
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);
209 }
210
211
212 /*
213  * The module selection routine for PPM format output.
214  */
215
216 GLOBAL(djpeg_dest_ptr)
217 jinit_write_ppm (j_decompress_ptr cinfo)
218 {
219   ppm_dest_ptr dest;
220
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;
227
228   /* Calculate output image dimensions so we can allocate space */
229   jpeg_calc_output_dimensions(cinfo);
230
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);
236
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.
242      */
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;
251     else
252       dest->pub.put_pixel_rows = put_demapped_rgb;
253   } else {
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;
260   }
261
262   return (djpeg_dest_ptr) dest;
263 }
264
265 #endif /* PPM_SUPPORTED */