The Independent JPEG Group's JPEG software v5a
diff --git a/wrppm.c b/wrppm.c
index 4f7a2cb..742ecfc 100644
--- a/wrppm.c
+++ b/wrppm.c
@@ -6,6 +6,7 @@
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains routines to write output images in PPM/PGM format.
+ * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
* The PBMPLUS library is NOT required to compile this software
* (but it is highly useful as a set of PPM image manipulation programs).
*
@@ -20,18 +21,34 @@
/*
- * Currently, this code only knows how to write raw PPM or PGM format,
- * which can be no more than 8 bits/sample. As an expedient for testing
- * 12-bit JPEG mode, we support writing 12-bit data to an 8-bit file by
- * downscaling the values. Of course this implies loss of precision.
+ * For 12-bit JPEG data, we either downscale the values to 8 bits
+ * (to write standard byte-per-sample PPM/PGM files), or output
+ * nonstandard word-per-sample PPM/PGM files. Downscaling is done
+ * if PPM_NORAWWORD is defined (this can be done in the Makefile
+ * or in jconfig.h).
* (When the core library supports data precision reduction, a cleaner
* implementation will be to ask for that instead.)
*/
#if BITS_IN_JSAMPLE == 8
-#define DOWNSCALE(x) (x)
+#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v)
+#define BYTESPERSAMPLE 1
+#define PPM_MAXVAL 255
#else
-#define DOWNSCALE(x) ((x) >> (BITS_IN_JSAMPLE-8))
+#ifdef PPM_NORAWWORD
+#define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) ((v) >> (BITS_IN_JSAMPLE-8))
+#define BYTESPERSAMPLE 1
+#define PPM_MAXVAL 255
+#else
+/* The word-per-sample format always puts the LSB first. */
+#define PUTPPMSAMPLE(ptr,v) \
+ { register int val_ = v; \
+ *ptr++ = (char) (val_ & 0xFF); \
+ *ptr++ = (char) ((val_ >> 8) & 0xFF); \
+ }
+#define BYTESPERSAMPLE 2
+#define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1)
+#endif
#endif
@@ -54,8 +71,8 @@
/* Usually these two pointers point to the same place: */
char *iobuffer; /* fwrite's I/O buffer */
JSAMPROW pixrow; /* decompressor output buffer */
-
- JDIMENSION buffer_width; /* width of one row */
+ size_t buffer_width; /* width of I/O buffer */
+ JDIMENSION samples_per_row; /* JSAMPLEs per output row */
} ppm_dest_struct;
typedef ppm_dest_struct * ppm_dest_ptr;
@@ -80,8 +97,8 @@
/*
- * This code is used when we have to copy the data because JSAMPLE is not
- * the same size as char. Typically this only happens in 12-bit mode.
+ * This code is used when we have to copy the data and apply a pixel
+ * format translation. Typically this only happens in 12-bit mode.
*/
METHODDEF void
@@ -95,8 +112,8 @@
ptr = dest->pub.buffer[0];
bufferptr = dest->iobuffer;
- for (col = dest->buffer_width; col > 0; col--) {
- *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(*ptr++));
+ for (col = dest->samples_per_row; col > 0; col--) {
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++));
}
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
}
@@ -124,9 +141,9 @@
bufferptr = dest->iobuffer;
for (col = cinfo->output_width; col > 0; col--) {
pixval = GETJSAMPLE(*ptr++);
- *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map0[pixval]));
- *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map1[pixval]));
- *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map2[pixval]));
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval]));
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval]));
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval]));
}
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
}
@@ -145,7 +162,7 @@
ptr = dest->pub.buffer[0];
bufferptr = dest->iobuffer;
for (col = cinfo->output_width; col > 0; col--) {
- *bufferptr++ = (char) DOWNSCALE(GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
+ PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)]));
}
(void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width);
}
@@ -165,12 +182,14 @@
case JCS_GRAYSCALE:
/* emit header for raw PGM format */
fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n",
- (long) cinfo->output_width, (long) cinfo->output_height, 255);
+ (long) cinfo->output_width, (long) cinfo->output_height,
+ PPM_MAXVAL);
break;
case JCS_RGB:
/* emit header for raw PPM format */
fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n",
- (long) cinfo->output_width, (long) cinfo->output_height, 255);
+ (long) cinfo->output_width, (long) cinfo->output_height,
+ PPM_MAXVAL);
break;
default:
ERREXIT(cinfo, JERR_PPM_COLORSPACE);
@@ -212,15 +231,16 @@
jpeg_calc_output_dimensions(cinfo);
/* Create physical I/O buffer. Note we make this near on a PC. */
- dest->buffer_width = cinfo->output_width * cinfo->out_color_components;
- dest->iobuffer = (char *)
- (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
- (size_t) (dest->buffer_width * SIZEOF(char)));
+ dest->samples_per_row = cinfo->output_width * cinfo->out_color_components;
+ dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * SIZEOF(char));
+ dest->iobuffer = (char *) (*cinfo->mem->alloc_small)
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width);
- if (cinfo->quantize_colors || SIZEOF(JSAMPLE) != SIZEOF(char)) {
+ if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 ||
+ SIZEOF(JSAMPLE) != SIZEOF(char)) {
/* When quantizing, we need an output buffer for colormap indexes
* that's separate from the physical I/O buffer. We also need a
- * separate buffer if JSAMPLE and char are not the same size.
+ * separate buffer if pixel format translation must take place.
*/
dest->pub.buffer = (*cinfo->mem->alloc_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE,