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