8 * Copyright (C) 1991-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains routines to read input images in PPM/PGM format.
13 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
14 * The PBMPLUS library is NOT required to compile this software
15 * (but it is highly useful as a set of PPM image manipulation programs).
17 * These routines may need modification for non-Unix environments or
18 * specialized applications. As they stand, they assume input from
19 * an ordinary stdio stream. They further assume that reading begins
20 * at the start of the file; start_input may need work if the
21 * user interface has already read some data (e.g., to determine that
22 * the file is indeed PPM format).
25 #include "cdjpeg.h" /* Common decls for cjpeg/djpeg applications */
30 /* Portions of this code are based on the PBMPLUS library, which is:
32 ** Copyright (C) 1988 by Jef Poskanzer.
34 ** Permission to use, copy, modify, and distribute this software and its
35 ** documentation for any purpose and without fee is hereby granted, provided
36 ** that the above copyright notice appear in all copies and that both that
37 ** copyright notice and this permission notice appear in supporting
38 ** documentation. This software is provided "as is" without express or
43 /* Macros to deal with unsigned chars as efficiently as compiler allows */
45 #ifdef HAVE_UNSIGNED_CHAR
46 typedef unsigned char U_CHAR
;
47 #define UCH(x) ((int) (x))
48 #else /* !HAVE_UNSIGNED_CHAR */
49 #ifdef CHAR_IS_UNSIGNED
51 #define UCH(x) ((int) (x))
54 #define UCH(x) ((int) (x) & 0xFF)
56 #endif /* HAVE_UNSIGNED_CHAR */
59 #define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len)))
63 * On most systems, reading individual bytes with getc() is drastically less
64 * efficient than buffering a row at a time with fread(). On PCs, we must
65 * allocate the buffer in near data space, because we are assuming small-data
66 * memory model, wherein fread() can't reach far memory. If you need to
67 * process very wide images on a PC, you might have to compile in large-memory
68 * model, or else replace fread() with a getc() loop --- which will be much
73 /* Private version of data source object */
76 struct cjpeg_source_struct pub
; /* public fields */
78 U_CHAR
*iobuffer
; /* non-FAR pointer to I/O buffer */
79 JSAMPROW pixrow
; /* FAR pointer to same */
80 size_t buffer_width
; /* width of I/O buffer */
81 JSAMPLE
*rescale
; /* => maxval-remapping array, or NULL */
84 typedef ppm_source_struct
* ppm_source_ptr
;
88 pbm_getc (FILE * infile
)
89 /* Read next char, skipping over any comments */
90 /* A comment/newline sequence is returned as a newline */
98 } while (ch
!= '\n' && ch
!= EOF
);
105 read_pbm_integer (j_compress_ptr cinfo
, FILE * infile
)
106 /* Read an unsigned decimal integer from the PPM file */
107 /* Swallows one trailing character after the integer */
108 /* Note that on a 16-bit-int machine, only values up to 64k can be read. */
109 /* This should not be a problem in practice. */
112 register unsigned int val
;
114 /* Skip any leading whitespace */
116 ch
= pbm_getc(infile
);
118 ERREXIT(cinfo
, JERR_INPUT_EOF
);
119 } while (ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r');
121 if (ch
< '0' || ch
> '9')
122 ERREXIT(cinfo
, JERR_PPM_NONNUMERIC
);
125 while ((ch
= pbm_getc(infile
)) >= '0' && ch
<= '9') {
134 * Read one row of pixels.
136 * We provide several different versions depending on input file format.
137 * In all cases, input is scaled to the size of JSAMPLE.
139 * A really fast path is provided for reading byte/sample raw files with
140 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
144 METHODDEF(JDIMENSION
)
145 get_text_gray_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
146 /* This version is for reading text-format PGM files with any maxval */
148 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
149 FILE * infile
= source
->pub
.input_file
;
150 register JSAMPROW ptr
;
151 register JSAMPLE
*rescale
= source
->rescale
;
154 ptr
= source
->pub
.buffer
[0];
155 for (col
= cinfo
->image_width
; col
> 0; col
--) {
156 *ptr
++ = rescale
[read_pbm_integer(cinfo
, infile
)];
162 METHODDEF(JDIMENSION
)
163 get_text_rgb_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
164 /* This version is for reading text-format PPM files with any maxval */
166 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
167 FILE * infile
= source
->pub
.input_file
;
168 register JSAMPROW ptr
;
169 register JSAMPLE
*rescale
= source
->rescale
;
172 ptr
= source
->pub
.buffer
[0];
173 for (col
= cinfo
->image_width
; col
> 0; col
--) {
174 *ptr
++ = rescale
[read_pbm_integer(cinfo
, infile
)];
175 *ptr
++ = rescale
[read_pbm_integer(cinfo
, infile
)];
176 *ptr
++ = rescale
[read_pbm_integer(cinfo
, infile
)];
182 METHODDEF(JDIMENSION
)
183 get_scaled_gray_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
184 /* This version is for reading raw-byte-format PGM files with any maxval */
186 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
187 register JSAMPROW ptr
;
188 register U_CHAR
* bufferptr
;
189 register JSAMPLE
*rescale
= source
->rescale
;
192 if (! ReadOK(source
->pub
.input_file
, source
->iobuffer
, source
->buffer_width
))
193 ERREXIT(cinfo
, JERR_INPUT_EOF
);
194 ptr
= source
->pub
.buffer
[0];
195 bufferptr
= source
->iobuffer
;
196 for (col
= cinfo
->image_width
; col
> 0; col
--) {
197 *ptr
++ = rescale
[UCH(*bufferptr
++)];
203 METHODDEF(JDIMENSION
)
204 get_scaled_rgb_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
205 /* This version is for reading raw-byte-format PPM files with any maxval */
207 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
208 register JSAMPROW ptr
;
209 register U_CHAR
* bufferptr
;
210 register JSAMPLE
*rescale
= source
->rescale
;
213 if (! ReadOK(source
->pub
.input_file
, source
->iobuffer
, source
->buffer_width
))
214 ERREXIT(cinfo
, JERR_INPUT_EOF
);
215 ptr
= source
->pub
.buffer
[0];
216 bufferptr
= source
->iobuffer
;
217 for (col
= cinfo
->image_width
; col
> 0; col
--) {
218 *ptr
++ = rescale
[UCH(*bufferptr
++)];
219 *ptr
++ = rescale
[UCH(*bufferptr
++)];
220 *ptr
++ = rescale
[UCH(*bufferptr
++)];
226 METHODDEF(JDIMENSION
)
227 get_raw_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
228 /* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
229 * In this case we just read right into the JSAMPLE buffer!
230 * Note that same code works for PPM and PGM files.
233 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
235 if (! ReadOK(source
->pub
.input_file
, source
->iobuffer
, source
->buffer_width
))
236 ERREXIT(cinfo
, JERR_INPUT_EOF
);
241 METHODDEF(JDIMENSION
)
242 get_word_gray_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
243 /* This version is for reading raw-word-format PGM files with any maxval */
245 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
246 register JSAMPROW ptr
;
247 register U_CHAR
* bufferptr
;
248 register JSAMPLE
*rescale
= source
->rescale
;
251 if (! ReadOK(source
->pub
.input_file
, source
->iobuffer
, source
->buffer_width
))
252 ERREXIT(cinfo
, JERR_INPUT_EOF
);
253 ptr
= source
->pub
.buffer
[0];
254 bufferptr
= source
->iobuffer
;
255 for (col
= cinfo
->image_width
; col
> 0; col
--) {
257 temp
= UCH(*bufferptr
++);
258 temp
|= UCH(*bufferptr
++) << 8;
259 *ptr
++ = rescale
[temp
];
265 METHODDEF(JDIMENSION
)
266 get_word_rgb_row (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
267 /* This version is for reading raw-word-format PPM files with any maxval */
269 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
270 register JSAMPROW ptr
;
271 register U_CHAR
* bufferptr
;
272 register JSAMPLE
*rescale
= source
->rescale
;
275 if (! ReadOK(source
->pub
.input_file
, source
->iobuffer
, source
->buffer_width
))
276 ERREXIT(cinfo
, JERR_INPUT_EOF
);
277 ptr
= source
->pub
.buffer
[0];
278 bufferptr
= source
->iobuffer
;
279 for (col
= cinfo
->image_width
; col
> 0; col
--) {
281 temp
= UCH(*bufferptr
++);
282 temp
|= UCH(*bufferptr
++) << 8;
283 *ptr
++ = rescale
[temp
];
284 temp
= UCH(*bufferptr
++);
285 temp
|= UCH(*bufferptr
++) << 8;
286 *ptr
++ = rescale
[temp
];
287 temp
= UCH(*bufferptr
++);
288 temp
|= UCH(*bufferptr
++) << 8;
289 *ptr
++ = rescale
[temp
];
296 * Read the file header; return image size and component count.
300 start_input_ppm (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
302 ppm_source_ptr source
= (ppm_source_ptr
) sinfo
;
304 unsigned int w
, h
, maxval
;
305 boolean need_iobuffer
, use_raw_buffer
, need_rescale
;
307 if (getc(source
->pub
.input_file
) != 'P')
308 ERREXIT(cinfo
, JERR_PPM_NOT
);
310 c
= getc(source
->pub
.input_file
); /* subformat discriminator character */
312 /* detect unsupported variants (ie, PBM) before trying to read header */
314 case '2': /* it's a text-format PGM file */
315 case '3': /* it's a text-format PPM file */
316 case '5': /* it's a raw-format PGM file */
317 case '6': /* it's a raw-format PPM file */
320 ERREXIT(cinfo
, JERR_PPM_NOT
);
324 /* fetch the remaining header info */
325 w
= read_pbm_integer(cinfo
, source
->pub
.input_file
);
326 h
= read_pbm_integer(cinfo
, source
->pub
.input_file
);
327 maxval
= read_pbm_integer(cinfo
, source
->pub
.input_file
);
329 if (w
<= 0 || h
<= 0 || maxval
<= 0) /* error check */
330 ERREXIT(cinfo
, JERR_PPM_NOT
);
332 cinfo
->data_precision
= BITS_IN_JSAMPLE
; /* we always rescale data to this */
333 cinfo
->image_width
= (JDIMENSION
) w
;
334 cinfo
->image_height
= (JDIMENSION
) h
;
336 /* initialize flags to most common settings */
337 need_iobuffer
= TRUE
; /* do we need an I/O buffer? */
338 use_raw_buffer
= FALSE
; /* do we map input buffer onto I/O buffer? */
339 need_rescale
= TRUE
; /* do we need a rescale array? */
342 case '2': /* it's a text-format PGM file */
343 cinfo
->input_components
= 1;
344 cinfo
->in_color_space
= JCS_GRAYSCALE
;
345 TRACEMS2(cinfo
, 1, JTRC_PGM_TEXT
, w
, h
);
346 source
->pub
.get_pixel_rows
= get_text_gray_row
;
347 need_iobuffer
= FALSE
;
350 case '3': /* it's a text-format PPM file */
351 cinfo
->input_components
= 3;
352 cinfo
->in_color_space
= JCS_RGB
;
353 TRACEMS2(cinfo
, 1, JTRC_PPM_TEXT
, w
, h
);
354 source
->pub
.get_pixel_rows
= get_text_rgb_row
;
355 need_iobuffer
= FALSE
;
358 case '5': /* it's a raw-format PGM file */
359 cinfo
->input_components
= 1;
360 cinfo
->in_color_space
= JCS_GRAYSCALE
;
361 TRACEMS2(cinfo
, 1, JTRC_PGM
, w
, h
);
363 source
->pub
.get_pixel_rows
= get_word_gray_row
;
364 } else if (maxval
== MAXJSAMPLE
&& SIZEOF(JSAMPLE
) == SIZEOF(U_CHAR
)) {
365 source
->pub
.get_pixel_rows
= get_raw_row
;
366 use_raw_buffer
= TRUE
;
367 need_rescale
= FALSE
;
369 source
->pub
.get_pixel_rows
= get_scaled_gray_row
;
373 case '6': /* it's a raw-format PPM file */
374 cinfo
->input_components
= 3;
375 cinfo
->in_color_space
= JCS_RGB
;
376 TRACEMS2(cinfo
, 1, JTRC_PPM
, w
, h
);
378 source
->pub
.get_pixel_rows
= get_word_rgb_row
;
379 } else if (maxval
== MAXJSAMPLE
&& SIZEOF(JSAMPLE
) == SIZEOF(U_CHAR
)) {
380 source
->pub
.get_pixel_rows
= get_raw_row
;
381 use_raw_buffer
= TRUE
;
382 need_rescale
= FALSE
;
384 source
->pub
.get_pixel_rows
= get_scaled_rgb_row
;
389 /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
391 source
->buffer_width
= (size_t) w
* cinfo
->input_components
*
392 ((maxval
<=255) ? SIZEOF(U_CHAR
) : (2*SIZEOF(U_CHAR
)));
393 source
->iobuffer
= (U_CHAR
*)
394 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
395 source
->buffer_width
);
398 /* Create compressor input buffer. */
399 if (use_raw_buffer
) {
400 /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
401 /* Synthesize a JSAMPARRAY pointer structure */
402 /* Cast here implies near->far pointer conversion on PCs */
403 source
->pixrow
= (JSAMPROW
) source
->iobuffer
;
404 source
->pub
.buffer
= & source
->pixrow
;
405 source
->pub
.buffer_height
= 1;
407 /* Need to translate anyway, so make a separate sample buffer. */
408 source
->pub
.buffer
= (*cinfo
->mem
->alloc_sarray
)
409 ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
410 (JDIMENSION
) w
* cinfo
->input_components
, (JDIMENSION
) 1);
411 source
->pub
.buffer_height
= 1;
414 /* Compute the rescaling array if required. */
416 INT32 val
, half_maxval
;
418 /* On 16-bit-int machines we have to be careful of maxval = 65535 */
419 source
->rescale
= (JSAMPLE
*)
420 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
421 (size_t) (((long) maxval
+ 1L) * SIZEOF(JSAMPLE
)));
422 half_maxval
= maxval
/ 2;
423 for (val
= 0; val
<= (INT32
) maxval
; val
++) {
424 /* The multiplication here must be done in 32 bits to avoid overflow */
425 source
->rescale
[val
] = (JSAMPLE
) ((val
*MAXJSAMPLE
+ half_maxval
)/maxval
);
432 * Finish up at the end of the file.
436 finish_input_ppm (j_compress_ptr cinfo
, cjpeg_source_ptr sinfo
)
443 * The module selection routine for PPM format input.
446 JGLOBAL(cjpeg_source_ptr
)
447 jinit_read_ppm (j_compress_ptr cinfo
)
449 ppm_source_ptr source
;
451 /* Create module interface object */
452 source
= (ppm_source_ptr
)
453 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_IMAGE
,
454 SIZEOF(ppm_source_struct
));
455 /* Fill in method ptrs, except get_pixel_rows which start_input sets */
456 source
->pub
.start_input
= start_input_ppm
;
457 source
->pub
.finish_input
= finish_input_ppm
;
459 return (cjpeg_source_ptr
) source
;
462 #endif /* PPM_SUPPORTED */