hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rdppm.c |
| 3 | * |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 4 | * This file was part of the Independent JPEG Group's software: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1997, Thomas G. Lane. |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 6 | * Modified 2009 by Bill Allombert, Guido Vollbeding. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 7 | * libjpeg-turbo Modifications: |
| 8 | * Copyright (C) 2015, D. R. Commander. |
| 9 | * For conditions of distribution and use, see the accompanying README.ijg |
| 10 | * file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 11 | * |
| 12 | * This file contains routines to read input images in PPM/PGM format. |
| 13 | * The extended 2-byte-per-sample raw PPM/PGM formats are supported. |
| 14 | * 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 input from |
| 19 | * an ordinary stdio stream. They further assume that reading begins |
| 20 | * at the start of the file; start_input may need work if the |
| 21 | * user interface has already read some data (e.g., to determine that |
| 22 | * the file is indeed PPM format). |
| 23 | */ |
| 24 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 25 | #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 26 | |
| 27 | #ifdef PPM_SUPPORTED |
| 28 | |
| 29 | |
| 30 | /* Portions of this code are based on the PBMPLUS library, which is: |
| 31 | ** |
| 32 | ** Copyright (C) 1988 by Jef Poskanzer. |
| 33 | ** |
| 34 | ** Permission to use, copy, modify, and distribute this software and its |
| 35 | ** documentation for any purpose and without fee is hereby granted, provided |
| 36 | ** that the above copyright notice appear in all copies and that both that |
| 37 | ** copyright notice and this permission notice appear in supporting |
| 38 | ** documentation. This software is provided "as is" without express or |
| 39 | ** implied warranty. |
| 40 | */ |
| 41 | |
| 42 | |
| 43 | /* Macros to deal with unsigned chars as efficiently as compiler allows */ |
| 44 | |
| 45 | #ifdef HAVE_UNSIGNED_CHAR |
| 46 | typedef unsigned char U_CHAR; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 47 | #define UCH(x) ((int) (x)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 48 | #else /* !HAVE_UNSIGNED_CHAR */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 49 | #ifdef __CHAR_UNSIGNED__ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 50 | typedef char U_CHAR; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 51 | #define UCH(x) ((int) (x)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 52 | #else |
| 53 | typedef char U_CHAR; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 54 | #define UCH(x) ((int) (x) & 0xFF) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 55 | #endif |
| 56 | #endif /* HAVE_UNSIGNED_CHAR */ |
| 57 | |
| 58 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 59 | #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | /* Private version of data source object */ |
| 63 | |
| 64 | typedef struct { |
| 65 | struct cjpeg_source_struct pub; /* public fields */ |
| 66 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 67 | /* Usually these two pointers point to the same place: */ |
| 68 | U_CHAR *iobuffer; /* fread's I/O buffer */ |
| 69 | JSAMPROW pixrow; /* compressor input buffer */ |
| 70 | size_t buffer_width; /* width of I/O buffer */ |
| 71 | JSAMPLE *rescale; /* => maxval-remapping array, or NULL */ |
| 72 | int maxval; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 73 | } ppm_source_struct; |
| 74 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 75 | typedef ppm_source_struct *ppm_source_ptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | LOCAL(int) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 79 | pbm_getc (FILE *infile) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 80 | /* Read next char, skipping over any comments */ |
| 81 | /* A comment/newline sequence is returned as a newline */ |
| 82 | { |
| 83 | register int ch; |
| 84 | |
| 85 | ch = getc(infile); |
| 86 | if (ch == '#') { |
| 87 | do { |
| 88 | ch = getc(infile); |
| 89 | } while (ch != '\n' && ch != EOF); |
| 90 | } |
| 91 | return ch; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | LOCAL(unsigned int) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 96 | read_pbm_integer (j_compress_ptr cinfo, FILE *infile, unsigned int maxval) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 97 | /* Read an unsigned decimal integer from the PPM file */ |
| 98 | /* Swallows one trailing character after the integer */ |
| 99 | /* Note that on a 16-bit-int machine, only values up to 64k can be read. */ |
| 100 | /* This should not be a problem in practice. */ |
| 101 | { |
| 102 | register int ch; |
| 103 | register unsigned int val; |
| 104 | |
| 105 | /* Skip any leading whitespace */ |
| 106 | do { |
| 107 | ch = pbm_getc(infile); |
| 108 | if (ch == EOF) |
| 109 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 110 | } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); |
| 111 | |
| 112 | if (ch < '0' || ch > '9') |
| 113 | ERREXIT(cinfo, JERR_PPM_NONNUMERIC); |
| 114 | |
| 115 | val = ch - '0'; |
| 116 | while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') { |
| 117 | val *= 10; |
| 118 | val += ch - '0'; |
| 119 | } |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 120 | |
| 121 | if (val > maxval) |
| 122 | ERREXIT(cinfo, JERR_PPM_TOOLARGE); |
| 123 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 124 | return val; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | * Read one row of pixels. |
| 130 | * |
| 131 | * We provide several different versions depending on input file format. |
| 132 | * In all cases, input is scaled to the size of JSAMPLE. |
| 133 | * |
| 134 | * A really fast path is provided for reading byte/sample raw files with |
| 135 | * maxval = MAXJSAMPLE, which is the normal case for 8-bit data. |
| 136 | */ |
| 137 | |
| 138 | |
| 139 | METHODDEF(JDIMENSION) |
| 140 | get_text_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 141 | /* This version is for reading text-format PGM files with any maxval */ |
| 142 | { |
| 143 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 144 | FILE *infile = source->pub.input_file; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 145 | register JSAMPROW ptr; |
| 146 | register JSAMPLE *rescale = source->rescale; |
| 147 | JDIMENSION col; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 148 | unsigned int maxval = source->maxval; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 149 | |
| 150 | ptr = source->pub.buffer[0]; |
| 151 | for (col = cinfo->image_width; col > 0; col--) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 152 | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 153 | } |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | METHODDEF(JDIMENSION) |
| 159 | get_text_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 160 | /* This version is for reading text-format PPM files with any maxval */ |
| 161 | { |
| 162 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 163 | FILE *infile = source->pub.input_file; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 164 | register JSAMPROW ptr; |
| 165 | register JSAMPLE *rescale = source->rescale; |
| 166 | JDIMENSION col; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 167 | unsigned int maxval = source->maxval; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 168 | |
| 169 | ptr = source->pub.buffer[0]; |
| 170 | for (col = cinfo->image_width; col > 0; col--) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 171 | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
| 172 | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
| 173 | *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 174 | } |
| 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | METHODDEF(JDIMENSION) |
| 180 | get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 181 | /* This version is for reading raw-byte-format PGM files with any maxval */ |
| 182 | { |
| 183 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 184 | register JSAMPROW ptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 185 | register U_CHAR *bufferptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 186 | register JSAMPLE *rescale = source->rescale; |
| 187 | JDIMENSION col; |
| 188 | |
| 189 | if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 190 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 191 | ptr = source->pub.buffer[0]; |
| 192 | bufferptr = source->iobuffer; |
| 193 | for (col = cinfo->image_width; col > 0; col--) { |
| 194 | *ptr++ = rescale[UCH(*bufferptr++)]; |
| 195 | } |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | METHODDEF(JDIMENSION) |
| 201 | get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 202 | /* This version is for reading raw-byte-format PPM files with any maxval */ |
| 203 | { |
| 204 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 205 | register JSAMPROW ptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 206 | register U_CHAR *bufferptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 207 | register JSAMPLE *rescale = source->rescale; |
| 208 | JDIMENSION col; |
| 209 | |
| 210 | if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 211 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 212 | ptr = source->pub.buffer[0]; |
| 213 | bufferptr = source->iobuffer; |
| 214 | for (col = cinfo->image_width; col > 0; col--) { |
| 215 | *ptr++ = rescale[UCH(*bufferptr++)]; |
| 216 | *ptr++ = rescale[UCH(*bufferptr++)]; |
| 217 | *ptr++ = rescale[UCH(*bufferptr++)]; |
| 218 | } |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | METHODDEF(JDIMENSION) |
| 224 | get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 225 | /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE. |
| 226 | * In this case we just read right into the JSAMPLE buffer! |
| 227 | * Note that same code works for PPM and PGM files. |
| 228 | */ |
| 229 | { |
| 230 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 231 | |
| 232 | if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 233 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | |
| 238 | METHODDEF(JDIMENSION) |
| 239 | get_word_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 240 | /* This version is for reading raw-word-format PGM files with any maxval */ |
| 241 | { |
| 242 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 243 | register JSAMPROW ptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 244 | register U_CHAR *bufferptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 245 | register JSAMPLE *rescale = source->rescale; |
| 246 | JDIMENSION col; |
| 247 | |
| 248 | if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 249 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 250 | ptr = source->pub.buffer[0]; |
| 251 | bufferptr = source->iobuffer; |
| 252 | for (col = cinfo->image_width; col > 0; col--) { |
| 253 | register int temp; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 254 | temp = UCH(*bufferptr++) << 8; |
| 255 | temp |= UCH(*bufferptr++); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 256 | *ptr++ = rescale[temp]; |
| 257 | } |
| 258 | return 1; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | METHODDEF(JDIMENSION) |
| 263 | get_word_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 264 | /* This version is for reading raw-word-format PPM files with any maxval */ |
| 265 | { |
| 266 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 267 | register JSAMPROW ptr; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 268 | register U_CHAR *bufferptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 269 | register JSAMPLE *rescale = source->rescale; |
| 270 | JDIMENSION col; |
| 271 | |
| 272 | if (! ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width)) |
| 273 | ERREXIT(cinfo, JERR_INPUT_EOF); |
| 274 | ptr = source->pub.buffer[0]; |
| 275 | bufferptr = source->iobuffer; |
| 276 | for (col = cinfo->image_width; col > 0; col--) { |
| 277 | register int temp; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 278 | temp = UCH(*bufferptr++) << 8; |
| 279 | temp |= UCH(*bufferptr++); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 280 | *ptr++ = rescale[temp]; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 281 | temp = UCH(*bufferptr++) << 8; |
| 282 | temp |= UCH(*bufferptr++); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 283 | *ptr++ = rescale[temp]; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 284 | temp = UCH(*bufferptr++) << 8; |
| 285 | temp |= UCH(*bufferptr++); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 286 | *ptr++ = rescale[temp]; |
| 287 | } |
| 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /* |
| 293 | * Read the file header; return image size and component count. |
| 294 | */ |
| 295 | |
| 296 | METHODDEF(void) |
| 297 | start_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 298 | { |
| 299 | ppm_source_ptr source = (ppm_source_ptr) sinfo; |
| 300 | int c; |
| 301 | unsigned int w, h, maxval; |
| 302 | boolean need_iobuffer, use_raw_buffer, need_rescale; |
| 303 | |
| 304 | if (getc(source->pub.input_file) != 'P') |
| 305 | ERREXIT(cinfo, JERR_PPM_NOT); |
| 306 | |
| 307 | c = getc(source->pub.input_file); /* subformat discriminator character */ |
| 308 | |
| 309 | /* detect unsupported variants (ie, PBM) before trying to read header */ |
| 310 | switch (c) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 311 | case '2': /* it's a text-format PGM file */ |
| 312 | case '3': /* it's a text-format PPM file */ |
| 313 | case '5': /* it's a raw-format PGM file */ |
| 314 | case '6': /* it's a raw-format PPM file */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 315 | break; |
| 316 | default: |
| 317 | ERREXIT(cinfo, JERR_PPM_NOT); |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | /* fetch the remaining header info */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 322 | w = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
| 323 | h = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
| 324 | maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 325 | |
| 326 | if (w <= 0 || h <= 0 || maxval <= 0) /* error check */ |
| 327 | ERREXIT(cinfo, JERR_PPM_NOT); |
| 328 | |
| 329 | cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */ |
| 330 | cinfo->image_width = (JDIMENSION) w; |
| 331 | cinfo->image_height = (JDIMENSION) h; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 332 | source->maxval = maxval; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 333 | |
| 334 | /* initialize flags to most common settings */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 335 | need_iobuffer = TRUE; /* do we need an I/O buffer? */ |
| 336 | use_raw_buffer = FALSE; /* do we map input buffer onto I/O buffer? */ |
| 337 | need_rescale = TRUE; /* do we need a rescale array? */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 338 | |
| 339 | switch (c) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 340 | case '2': /* it's a text-format PGM file */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 341 | cinfo->input_components = 1; |
| 342 | cinfo->in_color_space = JCS_GRAYSCALE; |
| 343 | TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h); |
| 344 | source->pub.get_pixel_rows = get_text_gray_row; |
| 345 | need_iobuffer = FALSE; |
| 346 | break; |
| 347 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 348 | case '3': /* it's a text-format PPM file */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 349 | cinfo->input_components = 3; |
| 350 | cinfo->in_color_space = JCS_RGB; |
| 351 | TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h); |
| 352 | source->pub.get_pixel_rows = get_text_rgb_row; |
| 353 | need_iobuffer = FALSE; |
| 354 | break; |
| 355 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 356 | case '5': /* it's a raw-format PGM file */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 357 | cinfo->input_components = 1; |
| 358 | cinfo->in_color_space = JCS_GRAYSCALE; |
| 359 | TRACEMS2(cinfo, 1, JTRC_PGM, w, h); |
| 360 | if (maxval > 255) { |
| 361 | source->pub.get_pixel_rows = get_word_gray_row; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 362 | } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 363 | source->pub.get_pixel_rows = get_raw_row; |
| 364 | use_raw_buffer = TRUE; |
| 365 | need_rescale = FALSE; |
| 366 | } else { |
| 367 | source->pub.get_pixel_rows = get_scaled_gray_row; |
| 368 | } |
| 369 | break; |
| 370 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 371 | case '6': /* it's a raw-format PPM file */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 372 | cinfo->input_components = 3; |
| 373 | cinfo->in_color_space = JCS_RGB; |
| 374 | TRACEMS2(cinfo, 1, JTRC_PPM, w, h); |
| 375 | if (maxval > 255) { |
| 376 | source->pub.get_pixel_rows = get_word_rgb_row; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 377 | } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 378 | source->pub.get_pixel_rows = get_raw_row; |
| 379 | use_raw_buffer = TRUE; |
| 380 | need_rescale = FALSE; |
| 381 | } else { |
| 382 | source->pub.get_pixel_rows = get_scaled_rgb_row; |
| 383 | } |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */ |
| 388 | if (need_iobuffer) { |
| 389 | source->buffer_width = (size_t) w * cinfo->input_components * |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 390 | ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR))); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 391 | source->iobuffer = (U_CHAR *) |
| 392 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 393 | source->buffer_width); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* Create compressor input buffer. */ |
| 397 | if (use_raw_buffer) { |
| 398 | /* For unscaled raw-input case, we can just map it onto the I/O buffer. */ |
| 399 | /* Synthesize a JSAMPARRAY pointer structure */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 400 | source->pixrow = (JSAMPROW) source->iobuffer; |
| 401 | source->pub.buffer = & source->pixrow; |
| 402 | source->pub.buffer_height = 1; |
| 403 | } else { |
| 404 | /* Need to translate anyway, so make a separate sample buffer. */ |
| 405 | source->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 406 | ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 407 | (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1); |
| 408 | source->pub.buffer_height = 1; |
| 409 | } |
| 410 | |
| 411 | /* Compute the rescaling array if required. */ |
| 412 | if (need_rescale) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 413 | long val, half_maxval; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 414 | |
| 415 | /* On 16-bit-int machines we have to be careful of maxval = 65535 */ |
| 416 | source->rescale = (JSAMPLE *) |
| 417 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 418 | (size_t) (((long) maxval + 1L) * |
| 419 | sizeof(JSAMPLE))); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 420 | half_maxval = maxval / 2; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 421 | for (val = 0; val <= (long) maxval; val++) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 422 | /* The multiplication here must be done in 32 bits to avoid overflow */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 423 | source->rescale[val] = (JSAMPLE) ((val * MAXJSAMPLE + half_maxval) / |
| 424 | maxval); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /* |
| 431 | * Finish up at the end of the file. |
| 432 | */ |
| 433 | |
| 434 | METHODDEF(void) |
| 435 | finish_input_ppm (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 436 | { |
| 437 | /* no work */ |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* |
| 442 | * The module selection routine for PPM format input. |
| 443 | */ |
| 444 | |
| 445 | GLOBAL(cjpeg_source_ptr) |
| 446 | jinit_read_ppm (j_compress_ptr cinfo) |
| 447 | { |
| 448 | ppm_source_ptr source; |
| 449 | |
| 450 | /* Create module interface object */ |
| 451 | source = (ppm_source_ptr) |
| 452 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame^] | 453 | sizeof(ppm_source_struct)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 454 | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
| 455 | source->pub.start_input = start_input_ppm; |
| 456 | source->pub.finish_input = finish_input_ppm; |
| 457 | |
| 458 | return (cjpeg_source_ptr) source; |
| 459 | } |
| 460 | |
| 461 | #endif /* PPM_SUPPORTED */ |