blob: d3a613c844a04dda62f98e0c53d7a86d1588302b [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * wrppm.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane489583f1996-02-07 00:00:00 +00005 * Copyright (C) 1991-1996, Thomas G. Lane.
Guido Vollbeding5996a252009-06-27 00:00:00 +00006 * Modified 2009 by Guido Vollbeding.
DRC5de454b2014-05-18 19:04:03 +00007 * It was modified by The libjpeg-turbo Project to include only code and
8 * information relevant to libjpeg-turbo.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * 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.
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000012 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000013 * 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
DRCe5eaf372014-05-09 18:00:32 +000021#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022
23#ifdef PPM_SUPPORTED
24
25
26/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000027 * 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).
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000032 * (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
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000037#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
38#define BYTESPERSAMPLE 1
39#define PPM_MAXVAL 255
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040#else
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000041#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
Guido Vollbeding5996a252009-06-27 00:00:00 +000046/* The word-per-sample format always puts the MSB first. */
DRCe5eaf372014-05-09 18:00:32 +000047#define PUTPPMSAMPLE(ptr,v) \
48 { register int val_ = v; \
49 *ptr++ = (char) ((val_ >> 8) & 0xFF); \
50 *ptr++ = (char) (val_ & 0xFF); \
51 }
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000052#define BYTESPERSAMPLE 2
53#define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
54#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000055#endif
56
57
58/*
59 * When JSAMPLE is the same size as char, we can just fwrite() the
DRC5033f3e2014-05-18 18:33:44 +000060 * decompressed data to the PPM or PGM file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061 */
62
63
64/* Private version of data destination object */
65
66typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000067 struct djpeg_dest_struct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000068
69 /* Usually these two pointers point to the same place: */
DRCe5eaf372014-05-09 18:00:32 +000070 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 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074} ppm_dest_struct;
75
76typedef 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
Thomas G. Lane489583f1996-02-07 00:00:00 +000087METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000088put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +000089 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090{
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/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000098 * 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.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000100 */
101
Thomas G. Lane489583f1996-02-07 00:00:00 +0000102METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000103copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000104 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105{
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;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000113 for (col = dest->samples_per_row; col > 0; col--) {
114 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000115 }
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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000125METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000126put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000127 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128{
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++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000142 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
143 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
144 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145 }
146 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
147}
148
149
Thomas G. Lane489583f1996-02-07 00:00:00 +0000150METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000151put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000152 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153{
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--) {
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000163 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164 }
165 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
166}
167
168
169/*
170 * Startup: write the file header.
171 */
172
Thomas G. Lane489583f1996-02-07 00:00:00 +0000173METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174start_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",
DRCe5eaf372014-05-09 18:00:32 +0000183 (long) cinfo->output_width, (long) cinfo->output_height,
184 PPM_MAXVAL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000185 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",
DRCe5eaf372014-05-09 18:00:32 +0000189 (long) cinfo->output_width, (long) cinfo->output_height,
190 PPM_MAXVAL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000191 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000202METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203finish_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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000216GLOBAL(djpeg_dest_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217jinit_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,
DRC5de454b2014-05-18 19:04:03 +0000224 sizeof(ppm_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225 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
DRC5033f3e2014-05-18 18:33:44 +0000231 /* Create physical I/O buffer */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000232 dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
DRC5de454b2014-05-18 19:04:03 +0000233 dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000234 dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
235 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000236
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000237 if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
DRC5de454b2014-05-18 19:04:03 +0000238 sizeof(JSAMPLE) != sizeof(char)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239 /* When quantizing, we need an output buffer for colormap indexes
240 * that's separate from the physical I/O buffer. We also need a
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000241 * separate buffer if pixel format translation must take place.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000242 */
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 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000256 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 */