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