blob: 7d13e2db7e8f4fbbeaed5bf960932c74602f3868 [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.
DRC7e3acc02015-10-10 10:25:46 -05009 * For conditions of distribution and use, see the accompanying README.ijg
10 * file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 *
12 * This file contains routines to write output images in PPM/PGM format.
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000013 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014 * The PBMPLUS library is NOT required to compile this software
15 * (but it is highly useful as a set of PPM image manipulation programs).
16 *
17 * These routines may need modification for non-Unix environments or
18 * specialized applications. As they stand, they assume output to
19 * an ordinary stdio stream.
20 */
21
DRCe5eaf372014-05-09 18:00:32 +000022#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023
24#ifdef PPM_SUPPORTED
25
26
27/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000028 * For 12-bit JPEG data, we either downscale the values to 8 bits
29 * (to write standard byte-per-sample PPM/PGM files), or output
30 * nonstandard word-per-sample PPM/PGM files. Downscaling is done
31 * if PPM_NORAWWORD is defined (this can be done in the Makefile
32 * or in jconfig.h).
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033 * (When the core library supports data precision reduction, a cleaner
34 * implementation will be to ask for that instead.)
35 */
36
37#if BITS_IN_JSAMPLE == 8
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000038#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
39#define BYTESPERSAMPLE 1
40#define PPM_MAXVAL 255
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000041#else
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000042#ifdef PPM_NORAWWORD
43#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
44#define BYTESPERSAMPLE 1
45#define PPM_MAXVAL 255
46#else
Guido Vollbeding5996a252009-06-27 00:00:00 +000047/* The word-per-sample format always puts the MSB first. */
DRCe5eaf372014-05-09 18:00:32 +000048#define PUTPPMSAMPLE(ptr,v) \
49 { register int val_ = v; \
50 *ptr++ = (char) ((val_ >> 8) & 0xFF); \
51 *ptr++ = (char) (val_ & 0xFF); \
52 }
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000053#define BYTESPERSAMPLE 2
54#define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
55#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000056#endif
57
58
59/*
60 * When JSAMPLE is the same size as char, we can just fwrite() the
DRC5033f3e2014-05-18 18:33:44 +000061 * decompressed data to the PPM or PGM file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062 */
63
64
65/* Private version of data destination object */
66
67typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000068 struct djpeg_dest_struct pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069
70 /* Usually these two pointers point to the same place: */
DRCe5eaf372014-05-09 18:00:32 +000071 char *iobuffer; /* fwrite's I/O buffer */
72 JSAMPROW pixrow; /* decompressor output buffer */
73 size_t buffer_width; /* width of I/O buffer */
74 JDIMENSION samples_per_row; /* JSAMPLEs per output row */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075} ppm_dest_struct;
76
DRCbd498032016-02-19 08:53:33 -060077typedef ppm_dest_struct *ppm_dest_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078
79
80/*
81 * Write some pixel data.
82 * In this module rows_supplied will always be 1.
83 *
84 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
85 * output buffer is physically the same as the fwrite buffer.
86 */
87
Thomas G. Lane489583f1996-02-07 00:00:00 +000088METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000089put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +000090 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000091{
92 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
93
94 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
95}
96
97
98/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000099 * This code is used when we have to copy the data and apply a pixel
100 * format translation. Typically this only happens in 12-bit mode.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000101 */
102
Thomas G. Lane489583f1996-02-07 00:00:00 +0000103METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000104copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000105 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000106{
107 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
DRCbd498032016-02-19 08:53:33 -0600108 register char *bufferptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000109 register JSAMPROW ptr;
110 register JDIMENSION col;
111
112 ptr = dest->pub.buffer[0];
113 bufferptr = dest->iobuffer;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000114 for (col = dest->samples_per_row; col > 0; col--) {
115 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116 }
117 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
118}
119
120
121/*
122 * Write some pixel data when color quantization is in effect.
123 * We have to demap the color index values to straight data.
124 */
125
Thomas G. Lane489583f1996-02-07 00:00:00 +0000126METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000127put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000128 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129{
130 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
DRCbd498032016-02-19 08:53:33 -0600131 register char *bufferptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132 register int pixval;
133 register JSAMPROW ptr;
134 register JSAMPROW color_map0 = cinfo->colormap[0];
135 register JSAMPROW color_map1 = cinfo->colormap[1];
136 register JSAMPROW color_map2 = cinfo->colormap[2];
137 register JDIMENSION col;
138
139 ptr = dest->pub.buffer[0];
140 bufferptr = dest->iobuffer;
141 for (col = cinfo->output_width; col > 0; col--) {
142 pixval = GETJSAMPLE(*ptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000143 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
144 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
145 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146 }
147 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
148}
149
150
Thomas G. Lane489583f1996-02-07 00:00:00 +0000151METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
DRCe5eaf372014-05-09 18:00:32 +0000153 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000154{
155 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
DRCbd498032016-02-19 08:53:33 -0600156 register char *bufferptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000157 register JSAMPROW ptr;
158 register JSAMPROW color_map = cinfo->colormap[0];
159 register JDIMENSION col;
160
161 ptr = dest->pub.buffer[0];
162 bufferptr = dest->iobuffer;
163 for (col = cinfo->output_width; col > 0; col--) {
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000164 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000165 }
166 (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
167}
168
169
170/*
171 * Startup: write the file header.
172 */
173
Thomas G. Lane489583f1996-02-07 00:00:00 +0000174METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000175start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
176{
177 ppm_dest_ptr dest = (ppm_dest_ptr) dinfo;
178
179 /* Emit file header */
180 switch (cinfo->out_color_space) {
181 case JCS_GRAYSCALE:
182 /* emit header for raw PGM format */
183 fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
DRCe5eaf372014-05-09 18:00:32 +0000184 (long) cinfo->output_width, (long) cinfo->output_height,
185 PPM_MAXVAL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186 break;
187 case JCS_RGB:
188 /* emit header for raw PPM format */
189 fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
DRCe5eaf372014-05-09 18:00:32 +0000190 (long) cinfo->output_width, (long) cinfo->output_height,
191 PPM_MAXVAL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192 break;
193 default:
194 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
195 }
196}
197
198
199/*
200 * Finish up at the end of the file.
201 */
202
Thomas G. Lane489583f1996-02-07 00:00:00 +0000203METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
205{
206 /* Make sure we wrote the output file OK */
207 fflush(dinfo->output_file);
208 if (ferror(dinfo->output_file))
209 ERREXIT(cinfo, JERR_FILE_WRITE);
210}
211
212
213/*
214 * The module selection routine for PPM format output.
215 */
216
Thomas G. Lane489583f1996-02-07 00:00:00 +0000217GLOBAL(djpeg_dest_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218jinit_write_ppm (j_decompress_ptr cinfo)
219{
220 ppm_dest_ptr dest;
221
222 /* Create module interface object, fill in method pointers */
223 dest = (ppm_dest_ptr)
224 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000225 sizeof(ppm_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 dest->pub.start_output = start_output_ppm;
227 dest->pub.finish_output = finish_output_ppm;
228
229 /* Calculate output image dimensions so we can allocate space */
230 jpeg_calc_output_dimensions(cinfo);
231
DRC5033f3e2014-05-18 18:33:44 +0000232 /* Create physical I/O buffer */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000233 dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
DRC5de454b2014-05-18 19:04:03 +0000234 dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000235 dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
236 ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000238 if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
DRC5de454b2014-05-18 19:04:03 +0000239 sizeof(JSAMPLE) != sizeof(char)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000240 /* When quantizing, we need an output buffer for colormap indexes
241 * that's separate from the physical I/O buffer. We also need a
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000242 * separate buffer if pixel format translation must take place.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000243 */
244 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
245 ((j_common_ptr) cinfo, JPOOL_IMAGE,
246 cinfo->output_width * cinfo->output_components, (JDIMENSION) 1);
247 dest->pub.buffer_height = 1;
248 if (! cinfo->quantize_colors)
249 dest->pub.put_pixel_rows = copy_pixel_rows;
250 else if (cinfo->out_color_space == JCS_GRAYSCALE)
251 dest->pub.put_pixel_rows = put_demapped_gray;
252 else
253 dest->pub.put_pixel_rows = put_demapped_rgb;
254 } else {
255 /* We will fwrite() directly from decompressor output buffer. */
256 /* Synthesize a JSAMPARRAY pointer structure */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 dest->pixrow = (JSAMPROW) dest->iobuffer;
258 dest->pub.buffer = & dest->pixrow;
259 dest->pub.buffer_height = 1;
260 dest->pub.put_pixel_rows = put_pixel_rows;
261 }
262
263 return (djpeg_dest_ptr) dest;
264}
265
266#endif /* PPM_SUPPORTED */