blob: f56d5cf324971aecb831000e3180b22ded604fe6 [file] [log] [blame]
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001/*
2 * rdppm.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding5996a252009-06-27 00:00:00 +00006 * Modified 2009 by Bill Allombert, Guido Vollbeding.
DRC5033f3e2014-05-18 18:33:44 +00007 * It was modified by The libjpeg-turbo Project to include only code and
8 * information relevant to libjpeg-turbo.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00009 * 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. Lane9ba2f5e1994-12-07 00:00:00 +000012 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000013 * 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
DRCe5eaf372014-05-09 18:00:32 +000024#include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025
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
45typedef unsigned char U_CHAR;
DRCe5eaf372014-05-09 18:00:32 +000046#define UCH(x) ((int) (x))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047#else /* !HAVE_UNSIGNED_CHAR */
DRC61976bd2014-04-20 19:13:10 +000048#ifdef __CHAR_UNSIGNED__
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000049typedef char U_CHAR;
DRCe5eaf372014-05-09 18:00:32 +000050#define UCH(x) ((int) (x))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000051#else
52typedef char U_CHAR;
DRCe5eaf372014-05-09 18:00:32 +000053#define UCH(x) ((int) (x) & 0xFF)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000054#endif
55#endif /* HAVE_UNSIGNED_CHAR */
56
57
DRCe5eaf372014-05-09 18:00:32 +000058#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000059
60
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061/* Private version of data source object */
62
63typedef struct {
64 struct cjpeg_source_struct pub; /* public fields */
65
DRC5033f3e2014-05-18 18:33:44 +000066 /* Usually these two pointers point to the same place: */
67 U_CHAR *iobuffer; /* fread's I/O buffer */
68 JSAMPROW pixrow; /* compressor input buffer */
DRCe5eaf372014-05-09 18:00:32 +000069 size_t buffer_width; /* width of I/O buffer */
70 JSAMPLE *rescale; /* => maxval-remapping array, or NULL */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071} ppm_source_struct;
72
73typedef ppm_source_struct * ppm_source_ptr;
74
75
Thomas G. Lane489583f1996-02-07 00:00:00 +000076LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000077pbm_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. Lane9ba2f5e1994-12-07 00:00:00 +000082
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 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. Lane489583f1996-02-07 00:00:00 +000093LOCAL(unsigned int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094read_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. Lane9ba2f5e1994-12-07 00:00:00 +0000102
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000103 /* 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. Lane9ba2f5e1994-12-07 00:00:00 +0000109
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110 if (ch < '0' || ch > '9')
111 ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000112
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113 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. Lane9ba2f5e1994-12-07 00:00:00 +0000128 * 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. Lane36a4ccc1994-09-24 00:00:00 +0000130 */
131
132
Thomas G. Lane489583f1996-02-07 00:00:00 +0000133METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000134get_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. Lane9ba2f5e1994-12-07 00:00:00 +0000142
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000143 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. Lane489583f1996-02-07 00:00:00 +0000151METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152get_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. Lane9ba2f5e1994-12-07 00:00:00 +0000160
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000161 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. Lane489583f1996-02-07 00:00:00 +0000171METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000173/* This version is for reading raw-byte-format PGM files with any maxval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000174{
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. Lane489583f1996-02-07 00:00:00 +0000192METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000193get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000194/* This version is for reading raw-byte-format PPM files with any maxval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000195{
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. Lane489583f1996-02-07 00:00:00 +0000215METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000217/* 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. Lane36a4ccc1994-09-24 00:00:00 +0000221{
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. Lane489583f1996-02-07 00:00:00 +0000230METHODDEF(JDIMENSION)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000231get_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 Vollbeding5996a252009-06-27 00:00:00 +0000246 temp = UCH(*bufferptr++) << 8;
247 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000248 *ptr++ = rescale[temp];
249 }
250 return 1;
251}
252
253
Thomas G. Lane489583f1996-02-07 00:00:00 +0000254METHODDEF(JDIMENSION)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000255get_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 Vollbeding5996a252009-06-27 00:00:00 +0000270 temp = UCH(*bufferptr++) << 8;
271 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000272 *ptr++ = rescale[temp];
Guido Vollbeding5996a252009-06-27 00:00:00 +0000273 temp = UCH(*bufferptr++) << 8;
274 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000275 *ptr++ = rescale[temp];
Guido Vollbeding5996a252009-06-27 00:00:00 +0000276 temp = UCH(*bufferptr++) << 8;
277 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000278 *ptr++ = rescale[temp];
279 }
280 return 1;
281}
282
283
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000284/*
285 * Read the file header; return image size and component count.
286 */
287
Thomas G. Lane489583f1996-02-07 00:00:00 +0000288METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000289start_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. Lane9ba2f5e1994-12-07 00:00:00 +0000294 boolean need_iobuffer, use_raw_buffer, need_rescale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295
296 if (getc(source->pub.input_file) != 'P')
297 ERREXIT(cinfo, JERR_PPM_NOT);
298
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000299 c = getc(source->pub.input_file); /* subformat discriminator character */
300
301 /* detect unsupported variants (ie, PBM) before trying to read header */
302 switch (c) {
DRCe5eaf372014-05-09 18:00:32 +0000303 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. Lane5ead57a1998-03-27 00:00:00 +0000307 break;
308 default:
309 ERREXIT(cinfo, JERR_PPM_NOT);
310 break;
311 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000312
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000313 /* fetch the remaining header info */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000314 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. Lane9ba2f5e1994-12-07 00:00:00 +0000325 /* initialize flags to most common settings */
DRCe5eaf372014-05-09 18:00:32 +0000326 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. Lane36a4ccc1994-09-24 00:00:00 +0000329
330 switch (c) {
DRCe5eaf372014-05-09 18:00:32 +0000331 case '2': /* it's a text-format PGM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332 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. Lane9ba2f5e1994-12-07 00:00:00 +0000336 need_iobuffer = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000337 break;
338
DRCe5eaf372014-05-09 18:00:32 +0000339 case '3': /* it's a text-format PPM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000340 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. Lane9ba2f5e1994-12-07 00:00:00 +0000344 need_iobuffer = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000345 break;
346
DRCe5eaf372014-05-09 18:00:32 +0000347 case '5': /* it's a raw-format PGM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348 cinfo->input_components = 1;
349 cinfo->in_color_space = JCS_GRAYSCALE;
350 TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000351 if (maxval > 255) {
352 source->pub.get_pixel_rows = get_word_gray_row;
DRC5de454b2014-05-18 19:04:03 +0000353 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354 source->pub.get_pixel_rows = get_raw_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000355 use_raw_buffer = TRUE;
356 need_rescale = FALSE;
357 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000358 source->pub.get_pixel_rows = get_scaled_gray_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000359 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000360 break;
361
DRCe5eaf372014-05-09 18:00:32 +0000362 case '6': /* it's a raw-format PPM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363 cinfo->input_components = 3;
364 cinfo->in_color_space = JCS_RGB;
365 TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000366 if (maxval > 255) {
367 source->pub.get_pixel_rows = get_word_rgb_row;
DRC5de454b2014-05-18 19:04:03 +0000368 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000369 source->pub.get_pixel_rows = get_raw_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000370 use_raw_buffer = TRUE;
371 need_rescale = FALSE;
372 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000373 source->pub.get_pixel_rows = get_scaled_rgb_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000374 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000375 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376 }
377
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000378 /* 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 *
DRC5de454b2014-05-18 19:04:03 +0000381 ((maxval<=255) ? sizeof(U_CHAR) : (2*sizeof(U_CHAR)));
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000382 source->iobuffer = (U_CHAR *)
383 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000384 source->buffer_width);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000385 }
386
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387 /* Create compressor input buffer. */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000388 if (use_raw_buffer) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000389 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
390 /* Synthesize a JSAMPARRAY pointer structure */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391 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. Lane9ba2f5e1994-12-07 00:00:00 +0000398 (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 source->pub.buffer_height = 1;
400 }
401
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000402 /* Compute the rescaling array if required. */
403 if (need_rescale) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000404 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,
DRC5de454b2014-05-18 19:04:03 +0000409 (size_t) (((long) maxval + 1L) * sizeof(JSAMPLE)));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000410 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. Lane489583f1996-02-07 00:00:00 +0000423METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000424finish_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. Lane489583f1996-02-07 00:00:00 +0000434GLOBAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435jinit_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,
DRC5de454b2014-05-18 19:04:03 +0000442 sizeof(ppm_source_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443 /* 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 */