Coverage Report

Created: 2026-04-12 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.3.0.x/rdppm.c
Line
Count
Source
1
/*
2
 * rdppm.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * Modified 2009 by Bill Allombert, Guido Vollbeding.
7
 * libjpeg-turbo Modifications:
8
 * Copyright (C) 2015-2017, 2020-2024, 2026, D. R. Commander.
9
 * For conditions of distribution and use, see the accompanying README.ijg
10
 * file.
11
 *
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).
16
 *
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).
23
 */
24
25
#include "cmyk.h"
26
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
27
28
#if defined(PPM_SUPPORTED) && \
29
    (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED))
30
31
32
/* Portions of this code are based on the PBMPLUS library, which is:
33
**
34
** Copyright (C) 1988 by Jef Poskanzer.
35
**
36
** Permission to use, copy, modify, and distribute this software and its
37
** documentation for any purpose and without fee is hereby granted, provided
38
** that the above copyright notice appear in all copies and that both that
39
** copyright notice and this permission notice appear in supporting
40
** documentation.  This software is provided "as is" without express or
41
** implied warranty.
42
*/
43
44
45
#define ReadOK(file, buffer, len) \
46
43.1M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
47
48
static int alpha_index[JPEG_NUMCS] = {
49
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
50
};
51
52
53
/* Private version of data source object */
54
55
typedef struct {
56
  struct cjpeg_source_struct pub; /* public fields */
57
58
  /* Usually these two pointers point to the same place: */
59
  unsigned char *iobuffer;      /* fread's I/O buffer */
60
  _JSAMPROW pixrow;             /* compressor input buffer */
61
  size_t buffer_width;          /* width of I/O buffer */
62
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
63
  unsigned int maxval;
64
} ppm_source_struct;
65
66
typedef ppm_source_struct *ppm_source_ptr;
67
68
69
LOCAL(int)
70
pbm_getc(FILE *infile)
71
/* Read next char, skipping over any comments */
72
/* A comment/newline sequence is returned as a newline */
73
336k
{
74
336k
  register int ch;
75
76
336k
  ch = getc(infile);
77
336k
  if (ch == '#') {
78
81.2k
    do {
79
81.2k
      ch = getc(infile);
80
81.2k
    } while (ch != '\n' && ch != EOF);
81
4.08k
  }
82
336k
  return ch;
83
336k
}
84
85
86
LOCAL(unsigned int)
87
read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
88
/* Read an unsigned decimal integer from the PPM file */
89
/* Swallows one trailing character after the integer */
90
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
91
/* This should not be a problem in practice. */
92
135k
{
93
135k
  register int ch;
94
135k
  register unsigned int val;
95
96
  /* Skip any leading whitespace */
97
144k
  do {
98
144k
    ch = pbm_getc(infile);
99
144k
    if (ch == EOF)
100
2.54k
      ERREXIT(cinfo, JERR_INPUT_EOF);
101
144k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
102
103
135k
  if (ch < '0' || ch > '9')
104
212
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
105
106
135k
  val = ch - '0';
107
195k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
108
59.3k
    val *= 10;
109
59.3k
    val += ch - '0';
110
59.3k
    if (val > maxval)
111
149
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
112
59.3k
  }
113
114
135k
  return val;
115
135k
}
116
117
118
/*
119
 * Read one row of pixels.
120
 *
121
 * We provide several different versions depending on input file format.
122
 * In all cases, input is scaled to the size of _JSAMPLE.
123
 *
124
 * A really fast path is provided for reading byte/sample raw files with
125
 * maxval = _MAXJSAMPLE, which is the normal case for 8-bit data.
126
 */
127
128
129
METHODDEF(JDIMENSION)
130
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
131
/* This version is for reading text-format PGM files with any maxval */
132
1.43k
{
133
1.43k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
134
1.43k
  FILE *infile = source->pub.input_file;
135
1.43k
  register _JSAMPROW ptr;
136
1.43k
  register _JSAMPLE *rescale = source->rescale;
137
1.43k
  JDIMENSION col;
138
1.43k
  unsigned int maxval = source->maxval;
139
140
1.43k
  ptr = source->pub._buffer[0];
141
3.65k
  for (col = cinfo->image_width; col > 0; col--) {
142
2.21k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
143
2.21k
  }
144
1.43k
  return 1;
145
1.43k
}
146
147
148
30.7M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
149
98.6M
  for (col = cinfo->image_width; col > 0; col--) { \
150
67.9M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
151
67.9M
    alpha_set_op \
152
67.9M
    ptr += ps; \
153
67.9M
  } \
154
30.7M
}
155
156
METHODDEF(JDIMENSION)
157
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
158
/* This version is for reading text-format PGM files with any maxval and
159
   converting to extended RGB */
160
7.17k
{
161
7.17k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
162
7.17k
  FILE *infile = source->pub.input_file;
163
7.17k
  register _JSAMPROW ptr;
164
7.17k
  register _JSAMPLE *rescale = source->rescale;
165
7.17k
  JDIMENSION col;
166
7.17k
  unsigned int maxval = source->maxval;
167
7.17k
  register int rindex = rgb_red[cinfo->in_color_space];
168
7.17k
  register int gindex = rgb_green[cinfo->in_color_space];
169
7.17k
  register int bindex = rgb_blue[cinfo->in_color_space];
170
7.17k
  register int aindex = alpha_index[cinfo->in_color_space];
171
7.17k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
172
173
7.17k
  ptr = source->pub._buffer[0];
174
7.17k
  if (maxval == _MAXJSAMPLE) {
175
1.59k
    if (aindex >= 0)
176
319
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
177
1.59k
                         ptr[aindex] = _MAXJSAMPLE;)
178
1.27k
    else
179
1.27k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
180
5.58k
  } else {
181
5.58k
    if (aindex >= 0)
182
1.11k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
183
5.58k
                         ptr[aindex] = _MAXJSAMPLE;)
184
4.46k
    else
185
4.46k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
186
5.58k
  }
187
7.17k
  return 1;
188
7.17k
}
189
190
191
METHODDEF(JDIMENSION)
192
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
193
/* This version is for reading text-format PGM files with any maxval and
194
   converting to CMYK */
195
1.43k
{
196
1.43k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
197
1.43k
  FILE *infile = source->pub.input_file;
198
1.43k
  register _JSAMPROW ptr;
199
1.43k
  register _JSAMPLE *rescale = source->rescale;
200
1.43k
  JDIMENSION col;
201
1.43k
  unsigned int maxval = source->maxval;
202
203
1.43k
  ptr = source->pub._buffer[0];
204
1.43k
  if (maxval == _MAXJSAMPLE) {
205
935
    for (col = cinfo->image_width; col > 0; col--) {
206
616
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
207
616
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
208
616
      ptr += 4;
209
616
    }
210
1.11k
  } else {
211
2.71k
    for (col = cinfo->image_width; col > 0; col--) {
212
1.60k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
213
1.60k
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
214
1.60k
      ptr += 4;
215
1.60k
    }
216
1.11k
  }
217
1.43k
  return 1;
218
1.43k
}
219
220
221
107k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
222
2.02M
  for (col = cinfo->image_width; col > 0; col--) { \
223
1.92M
    ptr[rindex] = read_op; \
224
1.92M
    ptr[gindex] = read_op; \
225
1.92M
    ptr[bindex] = read_op; \
226
1.92M
    alpha_set_op \
227
1.92M
    ptr += ps; \
228
1.92M
  } \
229
107k
}
230
231
METHODDEF(JDIMENSION)
232
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
233
/* This version is for reading text-format PPM files with any maxval */
234
10.5k
{
235
10.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
236
10.5k
  FILE *infile = source->pub.input_file;
237
10.5k
  register _JSAMPROW ptr;
238
10.5k
  register _JSAMPLE *rescale = source->rescale;
239
10.5k
  JDIMENSION col;
240
10.5k
  unsigned int maxval = source->maxval;
241
10.5k
  register int rindex = rgb_red[cinfo->in_color_space];
242
10.5k
  register int gindex = rgb_green[cinfo->in_color_space];
243
10.5k
  register int bindex = rgb_blue[cinfo->in_color_space];
244
10.5k
  register int aindex = alpha_index[cinfo->in_color_space];
245
10.5k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
246
247
10.5k
  ptr = source->pub._buffer[0];
248
10.5k
  if (maxval == _MAXJSAMPLE) {
249
3.08k
    if (aindex >= 0)
250
616
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
251
3.08k
                    ptr[aindex] = _MAXJSAMPLE;)
252
2.46k
    else
253
2.46k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
254
7.49k
  } else {
255
7.49k
    if (aindex >= 0)
256
1.49k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
257
7.49k
                    ptr[aindex] = _MAXJSAMPLE;)
258
5.99k
    else
259
5.99k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
260
7.49k
  }
261
10.5k
  return 1;
262
10.5k
}
263
264
265
METHODDEF(JDIMENSION)
266
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
267
/* This version is for reading text-format PPM files with any maxval and
268
   converting to CMYK */
269
2.11k
{
270
2.11k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
271
2.11k
  FILE *infile = source->pub.input_file;
272
2.11k
  register _JSAMPROW ptr;
273
2.11k
  register _JSAMPLE *rescale = source->rescale;
274
2.11k
  JDIMENSION col;
275
2.11k
  unsigned int maxval = source->maxval;
276
277
2.11k
  ptr = source->pub._buffer[0];
278
2.11k
  if (maxval == _MAXJSAMPLE) {
279
2.04k
    for (col = cinfo->image_width; col > 0; col--) {
280
1.43k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
281
1.43k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
282
1.43k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
283
1.43k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
284
1.43k
      ptr += 4;
285
1.43k
    }
286
1.49k
  } else {
287
3.65k
    for (col = cinfo->image_width; col > 0; col--) {
288
2.16k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
289
2.16k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
290
2.16k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
291
2.16k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
2.16k
      ptr += 4;
293
2.16k
    }
294
1.49k
  }
295
2.11k
  return 1;
296
2.11k
}
297
298
299
METHODDEF(JDIMENSION)
300
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
301
/* This version is for reading raw-byte-format PGM files with any maxval */
302
4.45M
{
303
4.45M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
304
4.45M
  register _JSAMPROW ptr;
305
4.45M
  register unsigned char *bufferptr;
306
4.45M
  register _JSAMPLE *rescale = source->rescale;
307
4.45M
  JDIMENSION col;
308
309
4.45M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
310
65
    ERREXIT(cinfo, JERR_INPUT_EOF);
311
4.45M
  ptr = source->pub._buffer[0];
312
4.45M
  bufferptr = source->iobuffer;
313
16.1M
  for (col = cinfo->image_width; col > 0; col--) {
314
11.6M
    *ptr++ = rescale[*bufferptr++];
315
11.6M
  }
316
4.45M
  return 1;
317
4.45M
}
318
319
320
METHODDEF(JDIMENSION)
321
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
322
/* This version is for reading raw-byte-format PGM files with any maxval
323
   and converting to extended RGB */
324
30.7M
{
325
30.7M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
326
30.7M
  register _JSAMPROW ptr;
327
30.7M
  register unsigned char *bufferptr;
328
30.7M
  register _JSAMPLE *rescale = source->rescale;
329
30.7M
  JDIMENSION col;
330
30.7M
  unsigned int maxval = source->maxval;
331
30.7M
  register int rindex = rgb_red[cinfo->in_color_space];
332
30.7M
  register int gindex = rgb_green[cinfo->in_color_space];
333
30.7M
  register int bindex = rgb_blue[cinfo->in_color_space];
334
30.7M
  register int aindex = alpha_index[cinfo->in_color_space];
335
30.7M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
336
337
30.7M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
338
445
    ERREXIT(cinfo, JERR_INPUT_EOF);
339
30.7M
  ptr = source->pub._buffer[0];
340
30.7M
  bufferptr = source->iobuffer;
341
30.7M
  if (maxval == _MAXJSAMPLE) {
342
8.44M
    if (aindex >= 0)
343
1.68M
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
344
6.75M
    else
345
6.75M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
346
22.2M
  } else {
347
22.2M
    if (aindex >= 0)
348
4.45M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], ptr[aindex] = _MAXJSAMPLE;)
349
17.8M
    else
350
17.8M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {})
351
22.2M
  }
352
30.7M
  return 1;
353
30.7M
}
354
355
356
METHODDEF(JDIMENSION)
357
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
358
/* This version is for reading raw-byte-format PGM files with any maxval
359
   and converting to CMYK */
360
6.14M
{
361
6.14M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
362
6.14M
  register _JSAMPROW ptr;
363
6.14M
  register unsigned char *bufferptr;
364
6.14M
  register _JSAMPLE *rescale = source->rescale;
365
6.14M
  JDIMENSION col;
366
6.14M
  unsigned int maxval = source->maxval;
367
368
6.14M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
369
89
    ERREXIT(cinfo, JERR_INPUT_EOF);
370
6.14M
  ptr = source->pub._buffer[0];
371
6.14M
  bufferptr = source->iobuffer;
372
6.14M
  if (maxval == _MAXJSAMPLE) {
373
3.59M
    for (col = cinfo->image_width; col > 0; col--) {
374
1.90M
      _JSAMPLE gray = *bufferptr++;
375
1.90M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
376
1.90M
      ptr += 4;
377
1.90M
    }
378
4.45M
  } else {
379
16.1M
    for (col = cinfo->image_width; col > 0; col--) {
380
11.6M
      _JSAMPLE gray = rescale[*bufferptr++];
381
11.6M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
382
11.6M
      ptr += 4;
383
11.6M
    }
384
4.45M
  }
385
6.14M
  return 1;
386
6.14M
}
387
388
389
METHODDEF(JDIMENSION)
390
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
391
/* This version is for reading raw-byte-format PPM files with any maxval */
392
96.4k
{
393
96.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
394
96.4k
  register _JSAMPROW ptr;
395
96.4k
  register unsigned char *bufferptr;
396
96.4k
  register _JSAMPLE *rescale = source->rescale;
397
96.4k
  JDIMENSION col;
398
96.4k
  unsigned int maxval = source->maxval;
399
96.4k
  register int rindex = rgb_red[cinfo->in_color_space];
400
96.4k
  register int gindex = rgb_green[cinfo->in_color_space];
401
96.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
402
96.4k
  register int aindex = alpha_index[cinfo->in_color_space];
403
96.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
404
405
96.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
406
617
    ERREXIT(cinfo, JERR_INPUT_EOF);
407
96.4k
  ptr = source->pub._buffer[0];
408
96.4k
  bufferptr = source->iobuffer;
409
96.4k
  if (maxval == _MAXJSAMPLE) {
410
3.31k
    if (aindex >= 0)
411
829
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
412
2.48k
    else
413
2.48k
      RGB_READ_LOOP(*bufferptr++, {})
414
93.1k
  } else {
415
93.1k
    if (aindex >= 0)
416
18.5k
      RGB_READ_LOOP(rescale[*bufferptr++], ptr[aindex] = _MAXJSAMPLE;)
417
74.6k
    else
418
74.6k
      RGB_READ_LOOP(rescale[*bufferptr++], {})
419
93.1k
  }
420
96.4k
  return 1;
421
96.4k
}
422
423
424
METHODDEF(JDIMENSION)
425
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
426
/* This version is for reading raw-byte-format PPM files with any maxval and
427
   converting to CMYK */
428
19.4k
{
429
19.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
430
19.4k
  register _JSAMPROW ptr;
431
19.4k
  register unsigned char *bufferptr;
432
19.4k
  register _JSAMPLE *rescale = source->rescale;
433
19.4k
  JDIMENSION col;
434
19.4k
  unsigned int maxval = source->maxval;
435
436
19.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
437
133
    ERREXIT(cinfo, JERR_INPUT_EOF);
438
19.4k
  ptr = source->pub._buffer[0];
439
19.4k
  bufferptr = source->iobuffer;
440
19.4k
  if (maxval == _MAXJSAMPLE) {
441
148k
    for (col = cinfo->image_width; col > 0; col--) {
442
147k
      _JSAMPLE r = *bufferptr++;
443
147k
      _JSAMPLE g = *bufferptr++;
444
147k
      _JSAMPLE b = *bufferptr++;
445
147k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
446
147k
      ptr += 4;
447
147k
    }
448
18.6k
  } else {
449
281k
    for (col = cinfo->image_width; col > 0; col--) {
450
262k
      _JSAMPLE r = rescale[*bufferptr++];
451
262k
      _JSAMPLE g = rescale[*bufferptr++];
452
262k
      _JSAMPLE b = rescale[*bufferptr++];
453
262k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
454
262k
      ptr += 4;
455
262k
    }
456
18.6k
  }
457
19.4k
  return 1;
458
19.4k
}
459
460
461
#if BITS_IN_JSAMPLE == 8
462
463
METHODDEF(JDIMENSION)
464
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
465
/* This version is for reading raw-byte-format files with maxval = _MAXJSAMPLE.
466
 * In this case we just read right into the _JSAMPLE buffer!
467
 * Note that same code works for PPM and PGM files.
468
 */
469
1.69M
{
470
1.69M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
471
472
1.69M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
473
72
    ERREXIT(cinfo, JERR_INPUT_EOF);
474
1.69M
  return 1;
475
1.69M
}
476
477
#endif
478
479
480
METHODDEF(JDIMENSION)
481
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
482
/* This version is for reading raw-word-format PGM files with any maxval */
483
4.91k
{
484
4.91k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
485
4.91k
  register _JSAMPROW ptr;
486
4.91k
  register unsigned char *bufferptr;
487
4.91k
  register _JSAMPLE *rescale = source->rescale;
488
4.91k
  JDIMENSION col;
489
4.91k
  unsigned int maxval = source->maxval;
490
491
4.91k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
492
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
493
4.91k
  ptr = source->pub._buffer[0];
494
4.91k
  bufferptr = source->iobuffer;
495
12.4k
  for (col = cinfo->image_width; col > 0; col--) {
496
7.55k
    register unsigned int temp;
497
7.55k
    temp  = (*bufferptr++) << 8;
498
7.55k
    temp |= (*bufferptr++);
499
7.55k
    if (temp > maxval)
500
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
501
7.55k
    *ptr++ = rescale[temp];
502
7.55k
  }
503
4.91k
  return 1;
504
4.91k
}
505
506
507
METHODDEF(JDIMENSION)
508
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
509
/* This version is for reading raw-word-format PGM files with any maxval */
510
24.5k
{
511
24.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
512
24.5k
  register _JSAMPROW ptr;
513
24.5k
  register unsigned char *bufferptr;
514
24.5k
  register _JSAMPLE *rescale = source->rescale;
515
24.5k
  JDIMENSION col;
516
24.5k
  unsigned int maxval = source->maxval;
517
24.5k
  register int rindex = rgb_red[cinfo->in_color_space];
518
24.5k
  register int gindex = rgb_green[cinfo->in_color_space];
519
24.5k
  register int bindex = rgb_blue[cinfo->in_color_space];
520
24.5k
  register int aindex = alpha_index[cinfo->in_color_space];
521
24.5k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
522
523
24.5k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
524
310
    ERREXIT(cinfo, JERR_INPUT_EOF);
525
24.5k
  ptr = source->pub._buffer[0];
526
24.5k
  bufferptr = source->iobuffer;
527
62.3k
  for (col = cinfo->image_width; col > 0; col--) {
528
37.7k
    register unsigned int temp;
529
37.7k
    temp  = (*bufferptr++) << 8;
530
37.7k
    temp |= (*bufferptr++);
531
37.7k
    if (temp > maxval)
532
180
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
533
37.7k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
534
37.7k
    if (aindex >= 0)
535
7.51k
      ptr[aindex] = _MAXJSAMPLE;
536
37.7k
    ptr += ps;
537
37.7k
  }
538
24.5k
  return 1;
539
24.5k
}
540
541
542
METHODDEF(JDIMENSION)
543
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
544
/* This version is for reading raw-word-format PGM files with any maxval */
545
4.91k
{
546
4.91k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
547
4.91k
  register _JSAMPROW ptr;
548
4.91k
  register unsigned char *bufferptr;
549
4.91k
  register _JSAMPLE *rescale = source->rescale;
550
4.91k
  JDIMENSION col;
551
4.91k
  unsigned int maxval = source->maxval;
552
553
4.91k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
554
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
555
4.91k
  ptr = source->pub._buffer[0];
556
4.91k
  bufferptr = source->iobuffer;
557
12.4k
  for (col = cinfo->image_width; col > 0; col--) {
558
7.55k
    register unsigned int gray;
559
7.55k
    gray  = (*bufferptr++) << 8;
560
7.55k
    gray |= (*bufferptr++);
561
7.55k
    if (gray > maxval)
562
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
563
7.55k
    rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1,
564
7.55k
                ptr + 2, ptr + 3);
565
7.55k
    ptr += 4;
566
7.55k
  }
567
4.91k
  return 1;
568
4.91k
}
569
570
571
METHODDEF(JDIMENSION)
572
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
573
/* This version is for reading raw-word-format PPM files with any maxval */
574
10.1k
{
575
10.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
576
10.1k
  register _JSAMPROW ptr;
577
10.1k
  register unsigned char *bufferptr;
578
10.1k
  register _JSAMPLE *rescale = source->rescale;
579
10.1k
  JDIMENSION col;
580
10.1k
  unsigned int maxval = source->maxval;
581
10.1k
  register int rindex = rgb_red[cinfo->in_color_space];
582
10.1k
  register int gindex = rgb_green[cinfo->in_color_space];
583
10.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
584
10.1k
  register int aindex = alpha_index[cinfo->in_color_space];
585
10.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
586
587
10.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
588
410
    ERREXIT(cinfo, JERR_INPUT_EOF);
589
10.1k
  ptr = source->pub._buffer[0];
590
10.1k
  bufferptr = source->iobuffer;
591
44.6k
  for (col = cinfo->image_width; col > 0; col--) {
592
34.4k
    register unsigned int temp;
593
34.4k
    temp  = (*bufferptr++) << 8;
594
34.4k
    temp |= (*bufferptr++);
595
34.4k
    if (temp > maxval)
596
145
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
597
34.4k
    ptr[rindex] = rescale[temp];
598
34.4k
    temp  = (*bufferptr++) << 8;
599
34.4k
    temp |= (*bufferptr++);
600
34.4k
    if (temp > maxval)
601
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
602
34.4k
    ptr[gindex] = rescale[temp];
603
34.4k
    temp  = (*bufferptr++) << 8;
604
34.4k
    temp |= (*bufferptr++);
605
34.4k
    if (temp > maxval)
606
100
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
607
34.4k
    ptr[bindex] = rescale[temp];
608
34.4k
    if (aindex >= 0)
609
6.82k
      ptr[aindex] = _MAXJSAMPLE;
610
34.4k
    ptr += ps;
611
34.4k
  }
612
10.1k
  return 1;
613
10.1k
}
614
615
616
METHODDEF(JDIMENSION)
617
get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
618
/* This version is for reading raw-word-format PPM files with any maxval */
619
2.02k
{
620
2.02k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
621
2.02k
  register _JSAMPROW ptr;
622
2.02k
  register unsigned char *bufferptr;
623
2.02k
  register _JSAMPLE *rescale = source->rescale;
624
2.02k
  JDIMENSION col;
625
2.02k
  unsigned int maxval = source->maxval;
626
627
2.02k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
628
82
    ERREXIT(cinfo, JERR_INPUT_EOF);
629
2.02k
  ptr = source->pub._buffer[0];
630
2.02k
  bufferptr = source->iobuffer;
631
8.92k
  for (col = cinfo->image_width; col > 0; col--) {
632
6.89k
    register unsigned int r, g, b;
633
6.89k
    r  = (*bufferptr++) << 8;
634
6.89k
    r |= (*bufferptr++);
635
6.89k
    if (r > maxval)
636
29
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
637
6.89k
    g  = (*bufferptr++) << 8;
638
6.89k
    g |= (*bufferptr++);
639
6.89k
    if (g > maxval)
640
24
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
641
6.89k
    b  = (*bufferptr++) << 8;
642
6.89k
    b |= (*bufferptr++);
643
6.89k
    if (b > maxval)
644
20
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
645
6.89k
    rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2,
646
6.89k
                ptr + 3);
647
6.89k
    ptr += 4;
648
6.89k
  }
649
2.02k
  return 1;
650
2.02k
}
651
652
653
/*
654
 * Read the file header; return image size and component count.
655
 */
656
657
METHODDEF(void)
658
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
659
19.7k
{
660
19.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
661
19.7k
  int c;
662
19.7k
  unsigned int w, h, maxval;
663
19.7k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
664
665
19.7k
  if (getc(source->pub.input_file) != 'P')
666
0
    ERREXIT(cinfo, JERR_PPM_NOT);
667
668
19.7k
  c = getc(source->pub.input_file); /* subformat discriminator character */
669
670
  /* detect unsupported variants (ie, PBM) before trying to read header */
671
19.7k
  switch (c) {
672
1.26k
  case '2':                     /* it's a text-format PGM file */
673
2.82k
  case '3':                     /* it's a text-format PPM file */
674
15.6k
  case '5':                     /* it's a raw-format PGM file */
675
19.7k
  case '6':                     /* it's a raw-format PPM file */
676
19.7k
    break;
677
7
  default:
678
7
    ERREXIT(cinfo, JERR_PPM_NOT);
679
7
    break;
680
19.7k
  }
681
682
  /* fetch the remaining header info */
683
19.7k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
684
19.7k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
685
19.7k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
686
687
19.7k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
688
42
    ERREXIT(cinfo, JERR_PPM_NOT);
689
19.7k
  if (w > JPEG_MAX_DIMENSION || h > JPEG_MAX_DIMENSION)
690
21
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
691
19.7k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
692
245
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
693
694
19.7k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
695
19.7k
  cinfo->image_width = (JDIMENSION)w;
696
19.7k
  cinfo->image_height = (JDIMENSION)h;
697
19.7k
  source->maxval = maxval;
698
699
  /* initialize flags to most common settings */
700
19.7k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
701
19.7k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
702
19.7k
  need_rescale = TRUE;          /* do we need a rescale array? */
703
704
19.7k
  switch (c) {
705
896
  case '2':                     /* it's a text-format PGM file */
706
896
    if (cinfo->in_color_space == JCS_UNKNOWN ||
707
896
        cinfo->in_color_space == JCS_RGB)
708
0
      cinfo->in_color_space = JCS_GRAYSCALE;
709
896
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
710
896
    if (cinfo->in_color_space == JCS_GRAYSCALE)
711
128
      source->pub.get_pixel_rows = get_text_gray_row;
712
768
    else if (IsExtRGB(cinfo->in_color_space))
713
640
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
714
128
    else if (cinfo->in_color_space == JCS_CMYK)
715
128
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
716
0
    else
717
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
718
896
    need_iobuffer = FALSE;
719
896
    break;
720
721
1.21k
  case '3':                     /* it's a text-format PPM file */
722
1.21k
    if (cinfo->in_color_space == JCS_UNKNOWN)
723
0
      cinfo->in_color_space = JCS_EXT_RGB;
724
1.21k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
725
1.21k
    if (IsExtRGB(cinfo->in_color_space))
726
865
      source->pub.get_pixel_rows = get_text_rgb_row;
727
346
    else if (cinfo->in_color_space == JCS_CMYK)
728
173
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
729
173
    else
730
173
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
731
1.21k
    need_iobuffer = FALSE;
732
1.21k
    break;
733
734
12.5k
  case '5':                     /* it's a raw-format PGM file */
735
12.5k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
736
12.5k
        cinfo->in_color_space == JCS_RGB)
737
0
      cinfo->in_color_space = JCS_GRAYSCALE;
738
12.5k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
739
12.5k
    if (maxval > 255) {
740
756
      if (cinfo->in_color_space == JCS_GRAYSCALE)
741
108
        source->pub.get_pixel_rows = get_word_gray_row;
742
648
      else if (IsExtRGB(cinfo->in_color_space))
743
540
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
744
108
      else if (cinfo->in_color_space == JCS_CMYK)
745
108
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
746
0
      else
747
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
748
756
#if BITS_IN_JSAMPLE == 8
749
11.7k
    } else if (maxval == _MAXJSAMPLE &&
750
1.28k
               cinfo->in_color_space == JCS_GRAYSCALE) {
751
184
      source->pub.get_pixel_rows = get_raw_row;
752
184
      use_raw_buffer = TRUE;
753
184
      need_rescale = FALSE;
754
184
#endif
755
11.5k
    } else {
756
11.5k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
757
1.49k
        source->pub.get_pixel_rows = get_scaled_gray_row;
758
10.0k
      else if (IsExtRGB(cinfo->in_color_space))
759
8.40k
        source->pub.get_pixel_rows = get_gray_rgb_row;
760
1.68k
      else if (cinfo->in_color_space == JCS_CMYK)
761
1.68k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
762
0
      else
763
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
764
11.5k
    }
765
12.5k
    break;
766
767
3.71k
  case '6':                     /* it's a raw-format PPM file */
768
3.71k
    if (cinfo->in_color_space == JCS_UNKNOWN)
769
0
      cinfo->in_color_space = JCS_EXT_RGB;
770
3.71k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
771
3.71k
    if (maxval > 255) {
772
1.12k
      if (IsExtRGB(cinfo->in_color_space))
773
805
        source->pub.get_pixel_rows = get_word_rgb_row;
774
322
      else if (cinfo->in_color_space == JCS_CMYK)
775
161
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
776
161
      else
777
161
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
778
1.12k
#if BITS_IN_JSAMPLE == 8
779
2.59k
    } else if (maxval == _MAXJSAMPLE &&
780
497
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
781
497
               (cinfo->in_color_space == JCS_EXT_RGB ||
782
426
                cinfo->in_color_space == JCS_RGB)) {
783
#else
784
               cinfo->in_color_space == JCS_EXT_RGB) {
785
#endif
786
71
      source->pub.get_pixel_rows = get_raw_row;
787
71
      use_raw_buffer = TRUE;
788
71
      need_rescale = FALSE;
789
71
#endif
790
2.51k
    } else {
791
2.51k
      if (IsExtRGB(cinfo->in_color_space))
792
1.77k
        source->pub.get_pixel_rows = get_rgb_row;
793
740
      else if (cinfo->in_color_space == JCS_CMYK)
794
370
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
795
370
      else
796
370
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
797
2.51k
    }
798
3.71k
    break;
799
19.7k
  }
800
801
17.6k
  if (IsExtRGB(cinfo->in_color_space))
802
13.1k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
803
4.53k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
804
1.91k
    cinfo->input_components = 1;
805
2.62k
  else if (cinfo->in_color_space == JCS_CMYK)
806
2.62k
    cinfo->input_components = 4;
807
808
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
809
17.6k
  if (need_iobuffer) {
810
15.7k
    if (c == '6')
811
3.18k
      source->buffer_width = (size_t)w * 3 * (maxval <= 255 ? 1 : 2);
812
12.5k
    else
813
12.5k
      source->buffer_width = (size_t)w * (maxval <= 255 ? 1 : 2);
814
15.7k
    source->iobuffer = (unsigned char *)
815
15.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
816
15.7k
                                  source->buffer_width);
817
15.7k
  }
818
819
  /* Create compressor input buffer. */
820
17.6k
  if (use_raw_buffer) {
821
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
822
    /* Synthesize a _JSAMPARRAY pointer structure */
823
255
    source->pixrow = (_JSAMPROW)source->iobuffer;
824
255
    source->pub._buffer = &source->pixrow;
825
255
    source->pub.buffer_height = 1;
826
17.3k
  } else {
827
    /* Need to translate anyway, so make a separate sample buffer. */
828
17.3k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
829
17.3k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
830
17.3k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
831
17.3k
    source->pub.buffer_height = 1;
832
17.3k
  }
833
834
  /* Compute the rescaling array if required. */
835
17.6k
  if (need_rescale) {
836
17.3k
    size_t val, half_maxval;
837
838
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
839
17.3k
    source->rescale = (_JSAMPLE *)
840
17.3k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
841
17.3k
                                  (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
842
17.3k
    memset(source->rescale, 0, (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
843
17.3k
    half_maxval = (size_t)maxval / 2;
844
28.9M
    for (val = 0; val <= (size_t)maxval; val++) {
845
      /* The multiplication here must be done in 32 bits to avoid overflow */
846
28.9M
      source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) /
847
28.9M
                                        maxval);
848
28.9M
    }
849
17.3k
  }
850
17.6k
}
851
852
853
/*
854
 * Finish up at the end of the file.
855
 */
856
857
METHODDEF(void)
858
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
859
12.8k
{
860
  /* no work */
861
12.8k
}
862
863
864
/*
865
 * The module selection routine for PPM format input.
866
 */
867
868
GLOBAL(cjpeg_source_ptr)
869
_jinit_read_ppm(j_compress_ptr cinfo)
870
19.7k
{
871
19.7k
  ppm_source_ptr source;
872
873
19.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
874
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
875
876
  /* Create module interface object */
877
19.7k
  source = (ppm_source_ptr)
878
19.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
879
19.7k
                                sizeof(ppm_source_struct));
880
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
881
19.7k
  source->pub.start_input = start_input_ppm;
882
19.7k
  source->pub.finish_input = finish_input_ppm;
883
19.7k
  source->pub.max_pixels = 0;
884
885
19.7k
  return (cjpeg_source_ptr)source;
886
19.7k
}
jinit_read_ppm
Line
Count
Source
870
19.7k
{
871
19.7k
  ppm_source_ptr source;
872
873
19.7k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
874
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
875
876
  /* Create module interface object */
877
19.7k
  source = (ppm_source_ptr)
878
19.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
879
19.7k
                                sizeof(ppm_source_struct));
880
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
881
19.7k
  source->pub.start_input = start_input_ppm;
882
19.7k
  source->pub.finish_input = finish_input_ppm;
883
19.7k
  source->pub.max_pixels = 0;
884
885
19.7k
  return (cjpeg_source_ptr)source;
886
19.7k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
887
888
#endif /* defined(PPM_SUPPORTED) &&
889
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */