blob: 2e9b54dd0457d80e79f8b1ff65c89294a2b3492a [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 */
Frank Bossen6709e4a2014-12-29 19:42:20 +010071 int maxval;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072} ppm_source_struct;
73
74typedef ppm_source_struct * ppm_source_ptr;
75
76
Thomas G. Lane489583f1996-02-07 00:00:00 +000077LOCAL(int)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078pbm_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. Lane9ba2f5e1994-12-07 00:00:00 +000083
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084 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. Lane489583f1996-02-07 00:00:00 +000094LOCAL(unsigned int)
Frank Bossen6709e4a2014-12-29 19:42:20 +010095read_pbm_integer (j_compress_ptr cinfo, FILE * infile, int maxval)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096/* 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. Lane9ba2f5e1994-12-07 00:00:00 +0000103
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000104 /* 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. Lane9ba2f5e1994-12-07 00:00:00 +0000110
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111 if (ch < '0' || ch > '9')
112 ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000113
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000114 val = ch - '0';
115 while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
116 val *= 10;
117 val += ch - '0';
118 }
Frank Bossen6709e4a2014-12-29 19:42:20 +0100119
120 if (val > maxval)
121 ERREXIT(cinfo, JERR_PPM_TOOLARGE);
122
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000123 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. Lane9ba2f5e1994-12-07 00:00:00 +0000133 * 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. Lane36a4ccc1994-09-24 00:00:00 +0000135 */
136
137
Thomas G. Lane489583f1996-02-07 00:00:00 +0000138METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000139get_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 Bossen6709e4a2014-12-29 19:42:20 +0100147 int maxval = source->maxval;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000148
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149 ptr = source->pub.buffer[0];
150 for (col = cinfo->image_width; col > 0; col--) {
Frank Bossen6709e4a2014-12-29 19:42:20 +0100151 *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000152 }
153 return 1;
154}
155
156
Thomas G. Lane489583f1996-02-07 00:00:00 +0000157METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158get_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 Bossen6709e4a2014-12-29 19:42:20 +0100166 int maxval = source->maxval;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000167
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000168 ptr = source->pub.buffer[0];
169 for (col = cinfo->image_width; col > 0; col--) {
Frank Bossen6709e4a2014-12-29 19:42:20 +0100170 *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. Lane36a4ccc1994-09-24 00:00:00 +0000173 }
174 return 1;
175}
176
177
Thomas G. Lane489583f1996-02-07 00:00:00 +0000178METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179get_scaled_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000180/* This version is for reading raw-byte-format PGM files with any maxval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181{
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. Lane489583f1996-02-07 00:00:00 +0000199METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200get_scaled_rgb_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000201/* This version is for reading raw-byte-format PPM files with any maxval */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202{
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. Lane489583f1996-02-07 00:00:00 +0000222METHODDEF(JDIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223get_raw_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000224/* 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. Lane36a4ccc1994-09-24 00:00:00 +0000228{
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. Lane489583f1996-02-07 00:00:00 +0000237METHODDEF(JDIMENSION)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000238get_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 Vollbeding5996a252009-06-27 00:00:00 +0000253 temp = UCH(*bufferptr++) << 8;
254 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000255 *ptr++ = rescale[temp];
256 }
257 return 1;
258}
259
260
Thomas G. Lane489583f1996-02-07 00:00:00 +0000261METHODDEF(JDIMENSION)
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000262get_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 Vollbeding5996a252009-06-27 00:00:00 +0000277 temp = UCH(*bufferptr++) << 8;
278 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000279 *ptr++ = rescale[temp];
Guido Vollbeding5996a252009-06-27 00:00:00 +0000280 temp = UCH(*bufferptr++) << 8;
281 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000282 *ptr++ = rescale[temp];
Guido Vollbeding5996a252009-06-27 00:00:00 +0000283 temp = UCH(*bufferptr++) << 8;
284 temp |= UCH(*bufferptr++);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000285 *ptr++ = rescale[temp];
286 }
287 return 1;
288}
289
290
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000291/*
292 * Read the file header; return image size and component count.
293 */
294
Thomas G. Lane489583f1996-02-07 00:00:00 +0000295METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296start_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. Lane9ba2f5e1994-12-07 00:00:00 +0000301 boolean need_iobuffer, use_raw_buffer, need_rescale;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000302
303 if (getc(source->pub.input_file) != 'P')
304 ERREXIT(cinfo, JERR_PPM_NOT);
305
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000306 c = getc(source->pub.input_file); /* subformat discriminator character */
307
308 /* detect unsupported variants (ie, PBM) before trying to read header */
309 switch (c) {
DRCe5eaf372014-05-09 18:00:32 +0000310 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. Lane5ead57a1998-03-27 00:00:00 +0000314 break;
315 default:
316 ERREXIT(cinfo, JERR_PPM_NOT);
317 break;
318 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000319
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000320 /* fetch the remaining header info */
Frank Bossen6709e4a2014-12-29 19:42:20 +0100321 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. Lane36a4ccc1994-09-24 00:00:00 +0000324
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 Bossen6709e4a2014-12-29 19:42:20 +0100331 source->maxval = maxval;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000332
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000333 /* initialize flags to most common settings */
DRCe5eaf372014-05-09 18:00:32 +0000334 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. Lane36a4ccc1994-09-24 00:00:00 +0000337
338 switch (c) {
DRCe5eaf372014-05-09 18:00:32 +0000339 case '2': /* it's a text-format PGM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000340 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. 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 '3': /* it's a text-format PPM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348 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. Lane9ba2f5e1994-12-07 00:00:00 +0000352 need_iobuffer = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000353 break;
354
DRCe5eaf372014-05-09 18:00:32 +0000355 case '5': /* it's a raw-format PGM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356 cinfo->input_components = 1;
357 cinfo->in_color_space = JCS_GRAYSCALE;
358 TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000359 if (maxval > 255) {
360 source->pub.get_pixel_rows = get_word_gray_row;
DRC5de454b2014-05-18 19:04:03 +0000361 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000362 source->pub.get_pixel_rows = get_raw_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000363 use_raw_buffer = TRUE;
364 need_rescale = FALSE;
365 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000366 source->pub.get_pixel_rows = get_scaled_gray_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000367 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368 break;
369
DRCe5eaf372014-05-09 18:00:32 +0000370 case '6': /* it's a raw-format PPM file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371 cinfo->input_components = 3;
372 cinfo->in_color_space = JCS_RGB;
373 TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000374 if (maxval > 255) {
375 source->pub.get_pixel_rows = get_word_rgb_row;
DRC5de454b2014-05-18 19:04:03 +0000376 } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR)) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000377 source->pub.get_pixel_rows = get_raw_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000378 use_raw_buffer = TRUE;
379 need_rescale = FALSE;
380 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381 source->pub.get_pixel_rows = get_scaled_rgb_row;
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000382 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000383 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 }
385
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000386 /* 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 *
DRC5de454b2014-05-18 19:04:03 +0000389 ((maxval<=255) ? sizeof(U_CHAR) : (2*sizeof(U_CHAR)));
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000390 source->iobuffer = (U_CHAR *)
391 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000392 source->buffer_width);
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000393 }
394
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395 /* Create compressor input buffer. */
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000396 if (use_raw_buffer) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000397 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
398 /* Synthesize a JSAMPARRAY pointer structure */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 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. Lane9ba2f5e1994-12-07 00:00:00 +0000406 (JDIMENSION) w * cinfo->input_components, (JDIMENSION) 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000407 source->pub.buffer_height = 1;
408 }
409
Thomas G. Lane9ba2f5e1994-12-07 00:00:00 +0000410 /* Compute the rescaling array if required. */
411 if (need_rescale) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000412 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,
DRC5de454b2014-05-18 19:04:03 +0000417 (size_t) (((long) maxval + 1L) * sizeof(JSAMPLE)));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418 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. Lane489583f1996-02-07 00:00:00 +0000431METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000432finish_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. Lane489583f1996-02-07 00:00:00 +0000442GLOBAL(cjpeg_source_ptr)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443jinit_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,
DRC5de454b2014-05-18 19:04:03 +0000450 sizeof(ppm_source_struct));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000451 /* 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 */