Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wrppm.c |
| 3 | * |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1996, Thomas G. Lane. |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 6 | * Modified 2009 by Guido Vollbeding. |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 7 | * It was modified by The libjpeg-turbo Project to include only code and |
| 8 | * information relevant to libjpeg-turbo. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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. |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 12 | * The extended 2-byte-per-sample raw PPM/PGM formats are supported. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 21 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef PPM_SUPPORTED |
| 24 | |
| 25 | |
| 26 | /* |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 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). |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 37 | #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v) |
| 38 | #define BYTESPERSAMPLE 1 |
| 39 | #define PPM_MAXVAL 255 |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 40 | #else |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 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 |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 46 | /* The word-per-sample format always puts the MSB first. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 47 | #define PUTPPMSAMPLE(ptr,v) \ |
| 48 | { register int val_ = v; \ |
| 49 | *ptr++ = (char) ((val_ >> 8) & 0xFF); \ |
| 50 | *ptr++ = (char) (val_ & 0xFF); \ |
| 51 | } |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 52 | #define BYTESPERSAMPLE 2 |
| 53 | #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1) |
| 54 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | |
| 58 | /* |
| 59 | * When JSAMPLE is the same size as char, we can just fwrite() the |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 60 | * decompressed data to the PPM or PGM file. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | |
| 64 | /* Private version of data destination object */ |
| 65 | |
| 66 | typedef struct { |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 67 | struct djpeg_dest_struct pub; /* public fields */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 68 | |
| 69 | /* Usually these two pointers point to the same place: */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 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 */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 87 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 88 | put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 89 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | /* |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 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. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 100 | */ |
| 101 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 102 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 103 | copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 104 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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; |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 113 | for (col = dest->samples_per_row; col > 0; col--) { |
| 114 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 125 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 126 | put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 127 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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++); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 142 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval])); |
| 143 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval])); |
| 144 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval])); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 145 | } |
| 146 | (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 147 | } |
| 148 | |
| 149 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 150 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 151 | put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 152 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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--) { |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 163 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)])); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 173 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 183 | (long) cinfo->output_width, (long) cinfo->output_height, |
| 184 | PPM_MAXVAL); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 189 | (long) cinfo->output_width, (long) cinfo->output_height, |
| 190 | PPM_MAXVAL); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 202 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 216 | GLOBAL(djpeg_dest_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 224 | sizeof(ppm_dest_struct)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 | |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 231 | /* Create physical I/O buffer */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 232 | dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 233 | dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char)); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 234 | dest->iobuffer = (char *) (*cinfo->mem->alloc_small) |
| 235 | ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 236 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 237 | if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 || |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 238 | sizeof(JSAMPLE) != sizeof(char)) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 241 | * separate buffer if pixel format translation must take place. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 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 */ |