comparison mupdf-source/thirdparty/libjpeg/rdppm.c @ 2:b50eed0cc0ef upstream

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