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 */ |
DRC | 0ef076f | 2016-02-19 18:32:10 -0600 | [diff] [blame] | 22 | #include "wrppm.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 23 | |
| 24 | #ifdef PPM_SUPPORTED |
| 25 | |
| 26 | |
| 27 | /* |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 28 | * 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. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 33 | * (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. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 38 | #define PUTPPMSAMPLE(ptr,v) *ptr++ = (char) (v) |
| 39 | #define BYTESPERSAMPLE 1 |
| 40 | #define PPM_MAXVAL 255 |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 41 | #else |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 42 | #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 Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 47 | /* The word-per-sample format always puts the MSB first. */ |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 48 | #define PUTPPMSAMPLE(ptr,v) \ |
| 49 | { register int val_ = v; \ |
| 50 | *ptr++ = (char) ((val_ >> 8) & 0xFF); \ |
| 51 | *ptr++ = (char) (val_ & 0xFF); \ |
| 52 | } |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 53 | #define BYTESPERSAMPLE 2 |
| 54 | #define PPM_MAXVAL ((1<<BITS_IN_JSAMPLE)-1) |
| 55 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * When JSAMPLE is the same size as char, we can just fwrite() the |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 61 | * decompressed data to the PPM or PGM file. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 62 | */ |
| 63 | |
| 64 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 65 | /* |
| 66 | * Write some pixel data. |
| 67 | * In this module rows_supplied will always be 1. |
| 68 | * |
| 69 | * put_pixel_rows handles the "normal" 8-bit case where the decompressor |
| 70 | * output buffer is physically the same as the fwrite buffer. |
| 71 | */ |
| 72 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 73 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 74 | put_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 75 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 76 | { |
| 77 | ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 78 | |
| 79 | (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /* |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 84 | * This code is used when we have to copy the data and apply a pixel |
| 85 | * format translation. Typically this only happens in 12-bit mode. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 86 | */ |
| 87 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 88 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 89 | copy_pixel_rows (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 90 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 91 | { |
| 92 | ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 93 | register char * bufferptr; |
| 94 | register JSAMPROW ptr; |
| 95 | register JDIMENSION col; |
| 96 | |
| 97 | ptr = dest->pub.buffer[0]; |
| 98 | bufferptr = dest->iobuffer; |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 99 | for (col = dest->samples_per_row; col > 0; col--) { |
| 100 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(*ptr++)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 101 | } |
| 102 | (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * Write some pixel data when color quantization is in effect. |
| 108 | * We have to demap the color index values to straight data. |
| 109 | */ |
| 110 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 111 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 112 | put_demapped_rgb (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 113 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 114 | { |
| 115 | ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 116 | register char * bufferptr; |
| 117 | register int pixval; |
| 118 | register JSAMPROW ptr; |
| 119 | register JSAMPROW color_map0 = cinfo->colormap[0]; |
| 120 | register JSAMPROW color_map1 = cinfo->colormap[1]; |
| 121 | register JSAMPROW color_map2 = cinfo->colormap[2]; |
| 122 | register JDIMENSION col; |
| 123 | |
| 124 | ptr = dest->pub.buffer[0]; |
| 125 | bufferptr = dest->iobuffer; |
| 126 | for (col = cinfo->output_width; col > 0; col--) { |
| 127 | pixval = GETJSAMPLE(*ptr++); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 128 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map0[pixval])); |
| 129 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map1[pixval])); |
| 130 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map2[pixval])); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 131 | } |
| 132 | (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 133 | } |
| 134 | |
| 135 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 136 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 137 | put_demapped_gray (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo, |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 138 | JDIMENSION rows_supplied) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 139 | { |
| 140 | ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 141 | register char * bufferptr; |
| 142 | register JSAMPROW ptr; |
| 143 | register JSAMPROW color_map = cinfo->colormap[0]; |
| 144 | register JDIMENSION col; |
| 145 | |
| 146 | ptr = dest->pub.buffer[0]; |
| 147 | bufferptr = dest->iobuffer; |
| 148 | for (col = cinfo->output_width; col > 0; col--) { |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 149 | PUTPPMSAMPLE(bufferptr, GETJSAMPLE(color_map[GETJSAMPLE(*ptr++)])); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 150 | } |
| 151 | (void) JFWRITE(dest->pub.output_file, dest->iobuffer, dest->buffer_width); |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | * Startup: write the file header. |
| 157 | */ |
| 158 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 159 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 160 | start_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 161 | { |
| 162 | ppm_dest_ptr dest = (ppm_dest_ptr) dinfo; |
| 163 | |
| 164 | /* Emit file header */ |
| 165 | switch (cinfo->out_color_space) { |
| 166 | case JCS_GRAYSCALE: |
| 167 | /* emit header for raw PGM format */ |
| 168 | fprintf(dest->pub.output_file, "P5\n%ld %ld\n%d\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 169 | (long) cinfo->output_width, (long) cinfo->output_height, |
| 170 | PPM_MAXVAL); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 171 | break; |
| 172 | case JCS_RGB: |
| 173 | /* emit header for raw PPM format */ |
| 174 | fprintf(dest->pub.output_file, "P6\n%ld %ld\n%d\n", |
DRC | e5eaf37 | 2014-05-09 18:00:32 +0000 | [diff] [blame] | 175 | (long) cinfo->output_width, (long) cinfo->output_height, |
| 176 | PPM_MAXVAL); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | ERREXIT(cinfo, JERR_PPM_COLORSPACE); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /* |
| 185 | * Finish up at the end of the file. |
| 186 | */ |
| 187 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 188 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 189 | finish_output_ppm (j_decompress_ptr cinfo, djpeg_dest_ptr dinfo) |
| 190 | { |
| 191 | /* Make sure we wrote the output file OK */ |
| 192 | fflush(dinfo->output_file); |
| 193 | if (ferror(dinfo->output_file)) |
| 194 | ERREXIT(cinfo, JERR_FILE_WRITE); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /* |
| 199 | * The module selection routine for PPM format output. |
| 200 | */ |
| 201 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 202 | GLOBAL(djpeg_dest_ptr) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 203 | jinit_write_ppm (j_decompress_ptr cinfo) |
| 204 | { |
| 205 | ppm_dest_ptr dest; |
| 206 | |
| 207 | /* Create module interface object, fill in method pointers */ |
| 208 | dest = (ppm_dest_ptr) |
| 209 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 210 | sizeof(ppm_dest_struct)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 211 | dest->pub.start_output = start_output_ppm; |
| 212 | dest->pub.finish_output = finish_output_ppm; |
| 213 | |
| 214 | /* Calculate output image dimensions so we can allocate space */ |
| 215 | jpeg_calc_output_dimensions(cinfo); |
| 216 | |
DRC | 5033f3e | 2014-05-18 18:33:44 +0000 | [diff] [blame] | 217 | /* Create physical I/O buffer */ |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 218 | dest->samples_per_row = cinfo->output_width * cinfo->out_color_components; |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 219 | dest->buffer_width = dest->samples_per_row * (BYTESPERSAMPLE * sizeof(char)); |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 220 | dest->iobuffer = (char *) (*cinfo->mem->alloc_small) |
| 221 | ((j_common_ptr) cinfo, JPOOL_IMAGE, dest->buffer_width); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 222 | |
Thomas G. Lane | 9ba2f5e | 1994-12-07 00:00:00 +0000 | [diff] [blame] | 223 | if (cinfo->quantize_colors || BITS_IN_JSAMPLE != 8 || |
DRC | 5de454b | 2014-05-18 19:04:03 +0000 | [diff] [blame] | 224 | sizeof(JSAMPLE) != sizeof(char)) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 225 | /* When quantizing, we need an output buffer for colormap indexes |
| 226 | * 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] | 227 | * separate buffer if pixel format translation must take place. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 228 | */ |
| 229 | dest->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 230 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 231 | cinfo->output_width * cinfo->output_components, (JDIMENSION) 1); |
| 232 | dest->pub.buffer_height = 1; |
| 233 | if (! cinfo->quantize_colors) |
| 234 | dest->pub.put_pixel_rows = copy_pixel_rows; |
| 235 | else if (cinfo->out_color_space == JCS_GRAYSCALE) |
| 236 | dest->pub.put_pixel_rows = put_demapped_gray; |
| 237 | else |
| 238 | dest->pub.put_pixel_rows = put_demapped_rgb; |
| 239 | } else { |
| 240 | /* We will fwrite() directly from decompressor output buffer. */ |
| 241 | /* Synthesize a JSAMPARRAY pointer structure */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 242 | dest->pixrow = (JSAMPROW) dest->iobuffer; |
| 243 | dest->pub.buffer = & dest->pixrow; |
| 244 | dest->pub.buffer_height = 1; |
| 245 | dest->pub.put_pixel_rows = put_pixel_rows; |
| 246 | } |
| 247 | |
| 248 | return (djpeg_dest_ptr) dest; |
| 249 | } |
| 250 | |
| 251 | #endif /* PPM_SUPPORTED */ |