blob: 69f91e816aecfec98d9a97cec60d7b9061fbd3d0 [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.
DRC5bc43c72017-11-13 21:01:53 -06007 * libjpeg-turbo Modifications:
DRC1ee87a92019-01-21 16:25:02 -06008 * Copyright (C) 2017, 2019, D. R. Commander.
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
DRCaa745902017-11-16 18:09:07 -060022#include "cmyk.h"
DRC19c791c2018-03-08 10:55:20 -060023#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000024
25#ifdef PPM_SUPPORTED
26
27
28/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000029 * For 12-bit JPEG data, we either downscale the values to 8 bits
30 * (to write standard byte-per-sample PPM/PGM files), or output
31 * nonstandard word-per-sample PPM/PGM files. Downscaling is done
32 * if PPM_NORAWWORD is defined (this can be done in the Makefile
33 * or in jconfig.h).
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034 * (When the core library supports data precision reduction, a cleaner
35 * implementation will be to ask for that instead.)
36 */
37
38#if BITS_IN_JSAMPLE == 8
DRC19c791c2018-03-08 10:55:20 -060039#define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)(v)
DRC293263c2018-03-17 15:14:35 -050040#define BYTESPERSAMPLE 1
41#define PPM_MAXVAL 255
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000042#else
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000043#ifdef PPM_NORAWWORD
DRC19c791c2018-03-08 10:55:20 -060044#define PUTPPMSAMPLE(ptr, v) *ptr++ = (char)((v) >> (BITS_IN_JSAMPLE - 8))
DRC293263c2018-03-17 15:14:35 -050045#define BYTESPERSAMPLE 1
46#define PPM_MAXVAL 255
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000047#else
Guido Vollbeding5996a252009-06-27 00:00:00 +000048/* The word-per-sample format always puts the MSB first. */
DRC19c791c2018-03-08 10:55:20 -060049#define PUTPPMSAMPLE(ptr, v) { \
50 register int val_ = v; \
51 *ptr++ = (char)((val_ >> 8) & 0xFF); \
52 *ptr++ = (char)(val_ & 0xFF); \
53}
DRC293263c2018-03-17 15:14:35 -050054#define BYTESPERSAMPLE 2
55#define PPM_MAXVAL ((1 << BITS_IN_JSAMPLE) - 1)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +000056#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000057#endif
58
59
60/*
61 * When JSAMPLE is the same size as char, we can just fwrite() the
DRC5033f3e2014-05-18 18:33:44 +000062 * decompressed data to the PPM or PGM file.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063 */
64
65
DRC5bc43c72017-11-13 21:01:53 -060066/* Private version of data destination object */
67
68typedef struct {
69 struct djpeg_dest_struct pub; /* public fields */
70
71 /* Usually these two pointers point to the same place: */
72 char *iobuffer; /* fwrite's I/O buffer */
73 JSAMPROW pixrow; /* decompressor output buffer */
74 size_t buffer_width; /* width of I/O buffer */
75 JDIMENSION samples_per_row; /* JSAMPLEs per output row */
76} ppm_dest_struct;
77
78typedef ppm_dest_struct *ppm_dest_ptr;
79
80
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081/*
82 * Write some pixel data.
83 * In this module rows_supplied will always be 1.
84 *
85 * put_pixel_rows handles the "normal" 8-bit case where the decompressor
86 * output buffer is physically the same as the fwrite buffer.
87 */
88
Thomas G. Lane489583f1996-02-07 00:00:00 +000089METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -060090put_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
91 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092{
DRC19c791c2018-03-08 10:55:20 -060093 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094
DRC19c791c2018-03-08 10:55:20 -060095 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096}
97
98
99/*
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000100 * This code is used when we have to copy the data and apply a pixel
101 * format translation. Typically this only happens in 12-bit mode.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102 */
103
Thomas G. Lane489583f1996-02-07 00:00:00 +0000104METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600105copy_pixel_rows(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
106 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000107{
DRC19c791c2018-03-08 10:55:20 -0600108 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
DRCbd498032016-02-19 08:53:33 -0600109 register char *bufferptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110 register JSAMPROW ptr;
DRCaa745902017-11-16 18:09:07 -0600111#if BITS_IN_JSAMPLE != 8 || (!defined(HAVE_UNSIGNED_CHAR) && !defined(__CHAR_UNSIGNED__))
112 register JDIMENSION col;
113#endif
114
115 ptr = dest->pub.buffer[0];
116 bufferptr = dest->iobuffer;
117#if BITS_IN_JSAMPLE == 8 && (defined(HAVE_UNSIGNED_CHAR) || defined(__CHAR_UNSIGNED__))
118 MEMCOPY(bufferptr, ptr, dest->samples_per_row);
119#else
120 for (col = dest->samples_per_row; col > 0; col--) {
121 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
122 }
123#endif
DRC19c791c2018-03-08 10:55:20 -0600124 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
DRCaa745902017-11-16 18:09:07 -0600125}
126
127
128/*
129 * Convert extended RGB to RGB.
130 */
131
132METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600133put_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, JDIMENSION rows_supplied)
DRCaa745902017-11-16 18:09:07 -0600134{
DRC19c791c2018-03-08 10:55:20 -0600135 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
DRCaa745902017-11-16 18:09:07 -0600136 register char *bufferptr;
137 register JSAMPROW ptr;
138 register JDIMENSION col;
139 register int rindex = rgb_red[cinfo->out_color_space];
140 register int gindex = rgb_green[cinfo->out_color_space];
141 register int bindex = rgb_blue[cinfo->out_color_space];
142 register int ps = rgb_pixelsize[cinfo->out_color_space];
143
144 ptr = dest->pub.buffer[0];
145 bufferptr = dest->iobuffer;
146 for (col = cinfo->output_width; col > 0; col--) {
147 PUTPPMSAMPLE(bufferptr, ptr[rindex]);
148 PUTPPMSAMPLE(bufferptr, ptr[gindex]);
149 PUTPPMSAMPLE(bufferptr, ptr[bindex]);
150 ptr += ps;
151 }
DRC19c791c2018-03-08 10:55:20 -0600152 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
DRCaa745902017-11-16 18:09:07 -0600153}
154
155
156/*
157 * Convert CMYK to RGB.
158 */
159
160METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600161put_cmyk(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
162 JDIMENSION rows_supplied)
DRCaa745902017-11-16 18:09:07 -0600163{
DRC19c791c2018-03-08 10:55:20 -0600164 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
DRCaa745902017-11-16 18:09:07 -0600165 register char *bufferptr;
166 register JSAMPROW ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000167 register JDIMENSION col;
168
169 ptr = dest->pub.buffer[0];
170 bufferptr = dest->iobuffer;
DRCaa745902017-11-16 18:09:07 -0600171 for (col = cinfo->output_width; col > 0; col--) {
172 JSAMPLE r, g, b, c = *ptr++, m = *ptr++, y = *ptr++, k = *ptr++;
173 cmyk_to_rgb(c, m, y, k, &r, &g, &b);
174 PUTPPMSAMPLE(bufferptr, r);
175 PUTPPMSAMPLE(bufferptr, g);
176 PUTPPMSAMPLE(bufferptr, b);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000177 }
DRC19c791c2018-03-08 10:55:20 -0600178 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179}
180
181
182/*
183 * Write some pixel data when color quantization is in effect.
184 * We have to demap the color index values to straight data.
185 */
186
Thomas G. Lane489583f1996-02-07 00:00:00 +0000187METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600188put_demapped_rgb(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
189 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000190{
DRC19c791c2018-03-08 10:55:20 -0600191 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
DRCbd498032016-02-19 08:53:33 -0600192 register char *bufferptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193 register int pixval;
194 register JSAMPROW ptr;
195 register JSAMPROW color_map0 = cinfo->colormap[0];
196 register JSAMPROW color_map1 = cinfo->colormap[1];
197 register JSAMPROW color_map2 = cinfo->colormap[2];
198 register JDIMENSION col;
199
200 ptr = dest->pub.buffer[0];
201 bufferptr = dest->iobuffer;
202 for (col = cinfo->output_width; col > 0; col--) {
203 pixval = GETJSAMPLE(*ptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000204 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
205 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
206 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000207 }
DRC19c791c2018-03-08 10:55:20 -0600208 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209}
210
211
Thomas G. Lane489583f1996-02-07 00:00:00 +0000212METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600213put_demapped_gray(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo,
214 JDIMENSION rows_supplied)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215{
DRC19c791c2018-03-08 10:55:20 -0600216 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
DRCbd498032016-02-19 08:53:33 -0600217 register char *bufferptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 register JSAMPROW ptr;
219 register JSAMPROW color_map = cinfo->colormap[0];
220 register JDIMENSION col;
221
222 ptr = dest->pub.buffer[0];
223 bufferptr = dest->iobuffer;
224 for (col = cinfo->output_width; col > 0; col--) {
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000225 PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000226 }
DRC19c791c2018-03-08 10:55:20 -0600227 (void)JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000228}
229
230
231/*
232 * Startup: write the file header.
233 */
234
Thomas G. Lane489583f1996-02-07 00:00:00 +0000235METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600236start_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237{
DRC19c791c2018-03-08 10:55:20 -0600238 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000239
240 /* Emit file header */
241 switch (cinfo->out_color_space) {
242 case JCS_GRAYSCALE:
243 /* emit header for raw PGM format */
244 fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
DRC19c791c2018-03-08 10:55:20 -0600245 (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000246 break;
247 case JCS_RGB:
DRCaa745902017-11-16 18:09:07 -0600248 case JCS_EXT_RGB:
249 case JCS_EXT_RGBX:
250 case JCS_EXT_BGR:
251 case JCS_EXT_BGRX:
252 case JCS_EXT_XBGR:
253 case JCS_EXT_XRGB:
254 case JCS_EXT_RGBA:
255 case JCS_EXT_BGRA:
256 case JCS_EXT_ABGR:
257 case JCS_EXT_ARGB:
258 case JCS_CMYK:
DRC1ee87a92019-01-21 16:25:02 -0600259 if (!IsExtRGB(cinfo->out_color_space) && cinfo->quantize_colors)
260 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000261 /* emit header for raw PPM format */
262 fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
DRC19c791c2018-03-08 10:55:20 -0600263 (long)cinfo->output_width, (long)cinfo->output_height, PPM_MAXVAL);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000264 break;
265 default:
266 ERREXIT(cinfo, JERR_PPM_COLORSPACE);
267 }
268}
269
270
271/*
272 * Finish up at the end of the file.
273 */
274
Thomas G. Lane489583f1996-02-07 00:00:00 +0000275METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600276finish_output_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000277{
278 /* Make sure we wrote the output file OK */
279 fflush(dinfo->output_file);
280 if (ferror(dinfo->output_file))
281 ERREXIT(cinfo, JERR_FILE_WRITE);
282}
283
284
285/*
DRC5bc43c72017-11-13 21:01:53 -0600286 * Re-calculate buffer dimensions based on output dimensions.
287 */
288
289METHODDEF(void)
DRC19c791c2018-03-08 10:55:20 -0600290calc_buffer_dimensions_ppm(j_decompress_ptr cinfo, djpeg_dest_ptr dinfo)
DRC5bc43c72017-11-13 21:01:53 -0600291{
DRC19c791c2018-03-08 10:55:20 -0600292 ppm_dest_ptr dest = (ppm_dest_ptr)dinfo;
DRC5bc43c72017-11-13 21:01:53 -0600293
DRCaa745902017-11-16 18:09:07 -0600294 if (cinfo->out_color_space == JCS_GRAYSCALE)
295 dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
296 else
297 dest->samples_per_row = cinfo->output_width * 3;
DRC5bc43c72017-11-13 21:01:53 -0600298 dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char));
299}
300
301
302/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 * The module selection routine for PPM format output.
304 */
305
Thomas G. Lane489583f1996-02-07 00:00:00 +0000306GLOBAL(djpeg_dest_ptr)
DRC19c791c2018-03-08 10:55:20 -0600307jinit_write_ppm(j_decompress_ptr cinfo)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000308{
309 ppm_dest_ptr dest;
310
311 /* Create module interface object, fill in method pointers */
312 dest = (ppm_dest_ptr)
DRC19c791c2018-03-08 10:55:20 -0600313 (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000314 sizeof(ppm_dest_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 dest->pub.start_output = start_output_ppm;
316 dest->pub.finish_output = finish_output_ppm;
DRC5bc43c72017-11-13 21:01:53 -0600317 dest->pub.calc_buffer_dimensions = calc_buffer_dimensions_ppm;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000318
319 /* Calculate output image dimensions so we can allocate space */
320 jpeg_calc_output_dimensions(cinfo);
321
DRC5033f3e2014-05-18 18:33:44 +0000322 /* Create physical I/O buffer */
DRC19c791c2018-03-08 10:55:20 -0600323 dest->pub.calc_buffer_dimensions(cinfo, (djpeg_dest_ptr)dest);
324 dest->iobuffer = (char *)(*cinfo->mem->alloc_small)
325 ((j_common_ptr)cinfo, JPOOL_IMAGE, dest->buffer_width);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000327 if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
DRCaa745902017-11-16 18:09:07 -0600328 sizeof(JSAMPLE) != sizeof(char) ||
329 (cinfo->out_color_space != JCS_EXT_RGB
330#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
331 && cinfo->out_color_space != JCS_RGB
332#endif
333 )) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000334 /* When quantizing, we need an output buffer for colormap indexes
335 * that's separate from the physical I/O buffer. We also need a
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000336 * separate buffer if pixel format translation must take place.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337 */
338 dest->pub.buffer = (*cinfo->mem->alloc_sarray)
DRC19c791c2018-03-08 10:55:20 -0600339 ((j_common_ptr)cinfo, JPOOL_IMAGE,
340 cinfo->output_width * cinfo->output_components, (JDIMENSION)1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000341 dest->pub.buffer_height = 1;
DRC1ee87a92019-01-21 16:25:02 -0600342 if (!cinfo->quantize_colors) {
343 if (IsExtRGB(cinfo->out_color_space))
344 dest->pub.put_pixel_rows = put_rgb;
345 else if (cinfo->out_color_space == JCS_CMYK)
346 dest->pub.put_pixel_rows = put_cmyk;
347 else
348 dest->pub.put_pixel_rows = copy_pixel_rows;
349 } else if (cinfo->out_color_space == JCS_GRAYSCALE)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350 dest->pub.put_pixel_rows = put_demapped_gray;
351 else
352 dest->pub.put_pixel_rows = put_demapped_rgb;
353 } else {
354 /* We will fwrite() directly from decompressor output buffer. */
355 /* Synthesize a JSAMPARRAY pointer structure */
DRC19c791c2018-03-08 10:55:20 -0600356 dest->pixrow = (JSAMPROW)dest->iobuffer;
357 dest->pub.buffer = &dest->pixrow;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000358 dest->pub.buffer_height = 1;
359 dest->pub.put_pixel_rows = put_pixel_rows;
360 }
361
DRC19c791c2018-03-08 10:55:20 -0600362 return (djpeg_dest_ptr)dest;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363}
364
365#endif /* PPM_SUPPORTED */