Coverage Report

Created: 2026-05-11 06:55

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
44.0M
  (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
343k
{
74
343k
  register int ch;
75
76
343k
  ch = getc(infile);
77
343k
  if (ch == '#') {
78
83.1k
    do {
79
83.1k
      ch = getc(infile);
80
83.1k
    } while (ch != '\n' && ch != EOF);
81
4.10k
  }
82
343k
  return ch;
83
343k
}
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
138k
{
93
138k
  register int ch;
94
138k
  register unsigned int val;
95
96
  /* Skip any leading whitespace */
97
147k
  do {
98
147k
    ch = pbm_getc(infile);
99
147k
    if (ch == EOF)
100
2.65k
      ERREXIT(cinfo, JERR_INPUT_EOF);
101
147k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
102
103
138k
  if (ch < '0' || ch > '9')
104
213
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
105
106
138k
  val = ch - '0';
107
198k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
108
60.1k
    val *= 10;
109
60.1k
    val += ch - '0';
110
60.1k
    if (val > maxval)
111
143
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
112
60.1k
  }
113
114
138k
  return val;
115
138k
}
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.60k
{
133
1.60k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
134
1.60k
  FILE *infile = source->pub.input_file;
135
1.60k
  register _JSAMPROW ptr;
136
1.60k
  register _JSAMPLE *rescale = source->rescale;
137
1.60k
  JDIMENSION col;
138
1.60k
  unsigned int maxval = source->maxval;
139
140
1.60k
  ptr = source->pub._buffer[0];
141
4.00k
  for (col = cinfo->image_width; col > 0; col--) {
142
2.39k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
143
2.39k
  }
144
1.60k
  return 1;
145
1.60k
}
146
147
148
31.0M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
149
98.9M
  for (col = cinfo->image_width; col > 0; col--) { \
150
67.8M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
151
67.8M
    alpha_set_op \
152
67.8M
    ptr += ps; \
153
67.8M
  } \
154
31.0M
}
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
8.04k
{
161
8.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
162
8.04k
  FILE *infile = source->pub.input_file;
163
8.04k
  register _JSAMPROW ptr;
164
8.04k
  register _JSAMPLE *rescale = source->rescale;
165
8.04k
  JDIMENSION col;
166
8.04k
  unsigned int maxval = source->maxval;
167
8.04k
  register int rindex = rgb_red[cinfo->in_color_space];
168
8.04k
  register int gindex = rgb_green[cinfo->in_color_space];
169
8.04k
  register int bindex = rgb_blue[cinfo->in_color_space];
170
8.04k
  register int aindex = alpha_index[cinfo->in_color_space];
171
8.04k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
172
173
8.04k
  ptr = source->pub._buffer[0];
174
8.04k
  if (maxval == _MAXJSAMPLE) {
175
1.60k
    if (aindex >= 0)
176
320
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
177
1.60k
                         ptr[aindex] = _MAXJSAMPLE;)
178
1.28k
    else
179
1.28k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
180
6.44k
  } else {
181
6.44k
    if (aindex >= 0)
182
1.28k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
183
6.44k
                         ptr[aindex] = _MAXJSAMPLE;)
184
5.15k
    else
185
5.15k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
186
6.44k
  }
187
8.04k
  return 1;
188
8.04k
}
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.60k
{
196
1.60k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
197
1.60k
  FILE *infile = source->pub.input_file;
198
1.60k
  register _JSAMPROW ptr;
199
1.60k
  register _JSAMPLE *rescale = source->rescale;
200
1.60k
  JDIMENSION col;
201
1.60k
  unsigned int maxval = source->maxval;
202
203
1.60k
  ptr = source->pub._buffer[0];
204
1.60k
  if (maxval == _MAXJSAMPLE) {
205
942
    for (col = cinfo->image_width; col > 0; col--) {
206
622
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
207
622
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
208
622
      ptr += 4;
209
622
    }
210
1.28k
  } else {
211
3.06k
    for (col = cinfo->image_width; col > 0; col--) {
212
1.77k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
213
1.77k
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
214
1.77k
      ptr += 4;
215
1.77k
    }
216
1.28k
  }
217
1.60k
  return 1;
218
1.60k
}
219
220
221
424k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
222
2.66M
  for (col = cinfo->image_width; col > 0; col--) { \
223
2.23M
    ptr[rindex] = read_op; \
224
2.23M
    ptr[gindex] = read_op; \
225
2.23M
    ptr[bindex] = read_op; \
226
2.23M
    alpha_set_op \
227
2.23M
    ptr += ps; \
228
2.23M
  } \
229
424k
}
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.9k
{
235
10.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
236
10.9k
  FILE *infile = source->pub.input_file;
237
10.9k
  register _JSAMPROW ptr;
238
10.9k
  register _JSAMPLE *rescale = source->rescale;
239
10.9k
  JDIMENSION col;
240
10.9k
  unsigned int maxval = source->maxval;
241
10.9k
  register int rindex = rgb_red[cinfo->in_color_space];
242
10.9k
  register int gindex = rgb_green[cinfo->in_color_space];
243
10.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
244
10.9k
  register int aindex = alpha_index[cinfo->in_color_space];
245
10.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
246
247
10.9k
  ptr = source->pub._buffer[0];
248
10.9k
  if (maxval == _MAXJSAMPLE) {
249
3.40k
    if (aindex >= 0)
250
681
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
251
3.40k
                    ptr[aindex] = _MAXJSAMPLE;)
252
2.72k
    else
253
2.72k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
254
7.56k
  } else {
255
7.56k
    if (aindex >= 0)
256
1.51k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
257
7.56k
                    ptr[aindex] = _MAXJSAMPLE;)
258
6.04k
    else
259
6.04k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
260
7.56k
  }
261
10.9k
  return 1;
262
10.9k
}
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.19k
{
270
2.19k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
271
2.19k
  FILE *infile = source->pub.input_file;
272
2.19k
  register _JSAMPROW ptr;
273
2.19k
  register _JSAMPLE *rescale = source->rescale;
274
2.19k
  JDIMENSION col;
275
2.19k
  unsigned int maxval = source->maxval;
276
277
2.19k
  ptr = source->pub._buffer[0];
278
2.19k
  if (maxval == _MAXJSAMPLE) {
279
2.09k
    for (col = cinfo->image_width; col > 0; col--) {
280
1.41k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
281
1.41k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
282
1.41k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
283
1.41k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
284
1.41k
      ptr += 4;
285
1.41k
    }
286
1.51k
  } else {
287
3.64k
    for (col = cinfo->image_width; col > 0; col--) {
288
2.13k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
289
2.13k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
290
2.13k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
291
2.13k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
2.13k
      ptr += 4;
293
2.13k
    }
294
1.51k
  }
295
2.19k
  return 1;
296
2.19k
}
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.38M
{
303
4.38M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
304
4.38M
  register _JSAMPROW ptr;
305
4.38M
  register unsigned char *bufferptr;
306
4.38M
  register _JSAMPLE *rescale = source->rescale;
307
4.38M
  JDIMENSION col;
308
309
4.38M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
310
69
    ERREXIT(cinfo, JERR_INPUT_EOF);
311
4.38M
  ptr = source->pub._buffer[0];
312
4.38M
  bufferptr = source->iobuffer;
313
15.9M
  for (col = cinfo->image_width; col > 0; col--) {
314
11.5M
    *ptr++ = rescale[*bufferptr++];
315
11.5M
  }
316
4.38M
  return 1;
317
4.38M
}
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
31.0M
{
325
31.0M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
326
31.0M
  register _JSAMPROW ptr;
327
31.0M
  register unsigned char *bufferptr;
328
31.0M
  register _JSAMPLE *rescale = source->rescale;
329
31.0M
  JDIMENSION col;
330
31.0M
  unsigned int maxval = source->maxval;
331
31.0M
  register int rindex = rgb_red[cinfo->in_color_space];
332
31.0M
  register int gindex = rgb_green[cinfo->in_color_space];
333
31.0M
  register int bindex = rgb_blue[cinfo->in_color_space];
334
31.0M
  register int aindex = alpha_index[cinfo->in_color_space];
335
31.0M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
336
337
31.0M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
338
475
    ERREXIT(cinfo, JERR_INPUT_EOF);
339
31.0M
  ptr = source->pub._buffer[0];
340
31.0M
  bufferptr = source->iobuffer;
341
31.0M
  if (maxval == _MAXJSAMPLE) {
342
9.13M
    if (aindex >= 0)
343
1.82M
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
344
7.30M
    else
345
7.30M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
346
21.9M
  } else {
347
21.9M
    if (aindex >= 0)
348
4.38M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], ptr[aindex] = _MAXJSAMPLE;)
349
17.5M
    else
350
17.5M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {})
351
21.9M
  }
352
31.0M
  return 1;
353
31.0M
}
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.21M
{
361
6.21M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
362
6.21M
  register _JSAMPROW ptr;
363
6.21M
  register unsigned char *bufferptr;
364
6.21M
  register _JSAMPLE *rescale = source->rescale;
365
6.21M
  JDIMENSION col;
366
6.21M
  unsigned int maxval = source->maxval;
367
368
6.21M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
369
95
    ERREXIT(cinfo, JERR_INPUT_EOF);
370
6.21M
  ptr = source->pub._buffer[0];
371
6.21M
  bufferptr = source->iobuffer;
372
6.21M
  if (maxval == _MAXJSAMPLE) {
373
3.87M
    for (col = cinfo->image_width; col > 0; col--) {
374
2.04M
      _JSAMPLE gray = *bufferptr++;
375
2.04M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
376
2.04M
      ptr += 4;
377
2.04M
    }
378
4.38M
  } else {
379
15.9M
    for (col = cinfo->image_width; col > 0; col--) {
380
11.5M
      _JSAMPLE gray = rescale[*bufferptr++];
381
11.5M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
382
11.5M
      ptr += 4;
383
11.5M
    }
384
4.38M
  }
385
6.21M
  return 1;
386
6.21M
}
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
413k
{
393
413k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
394
413k
  register _JSAMPROW ptr;
395
413k
  register unsigned char *bufferptr;
396
413k
  register _JSAMPLE *rescale = source->rescale;
397
413k
  JDIMENSION col;
398
413k
  unsigned int maxval = source->maxval;
399
413k
  register int rindex = rgb_red[cinfo->in_color_space];
400
413k
  register int gindex = rgb_green[cinfo->in_color_space];
401
413k
  register int bindex = rgb_blue[cinfo->in_color_space];
402
413k
  register int aindex = alpha_index[cinfo->in_color_space];
403
413k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
404
405
413k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
406
642
    ERREXIT(cinfo, JERR_INPUT_EOF);
407
413k
  ptr = source->pub._buffer[0];
408
413k
  bufferptr = source->iobuffer;
409
413k
  if (maxval == _MAXJSAMPLE) {
410
3.38k
    if (aindex >= 0)
411
845
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
412
2.53k
    else
413
2.53k
      RGB_READ_LOOP(*bufferptr++, {})
414
410k
  } else {
415
410k
    if (aindex >= 0)
416
81.9k
      RGB_READ_LOOP(rescale[*bufferptr++], ptr[aindex] = _MAXJSAMPLE;)
417
328k
    else
418
328k
      RGB_READ_LOOP(rescale[*bufferptr++], {})
419
410k
  }
420
413k
  return 1;
421
413k
}
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
82.9k
{
429
82.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
430
82.9k
  register _JSAMPROW ptr;
431
82.9k
  register unsigned char *bufferptr;
432
82.9k
  register _JSAMPLE *rescale = source->rescale;
433
82.9k
  JDIMENSION col;
434
82.9k
  unsigned int maxval = source->maxval;
435
436
82.9k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
437
138
    ERREXIT(cinfo, JERR_INPUT_EOF);
438
82.9k
  ptr = source->pub._buffer[0];
439
82.9k
  bufferptr = source->iobuffer;
440
82.9k
  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
82.0k
  } else {
449
407k
    for (col = cinfo->image_width; col > 0; col--) {
450
325k
      _JSAMPLE r = rescale[*bufferptr++];
451
325k
      _JSAMPLE g = rescale[*bufferptr++];
452
325k
      _JSAMPLE b = rescale[*bufferptr++];
453
325k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
454
325k
      ptr += 4;
455
325k
    }
456
82.0k
  }
457
82.9k
  return 1;
458
82.9k
}
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.82M
{
470
1.82M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
471
472
1.82M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
473
74
    ERREXIT(cinfo, JERR_INPUT_EOF);
474
1.82M
  return 1;
475
1.82M
}
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
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
493
4.91k
  ptr = source->pub._buffer[0];
494
4.91k
  bufferptr = source->iobuffer;
495
11.1k
  for (col = cinfo->image_width; col > 0; col--) {
496
6.27k
    register unsigned int temp;
497
6.27k
    temp  = (*bufferptr++) << 8;
498
6.27k
    temp |= (*bufferptr++);
499
6.27k
    if (temp > maxval)
500
39
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
501
6.27k
    *ptr++ = rescale[temp];
502
6.27k
  }
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
305
    ERREXIT(cinfo, JERR_INPUT_EOF);
525
24.5k
  ptr = source->pub._buffer[0];
526
24.5k
  bufferptr = source->iobuffer;
527
55.9k
  for (col = cinfo->image_width; col > 0; col--) {
528
31.3k
    register unsigned int temp;
529
31.3k
    temp  = (*bufferptr++) << 8;
530
31.3k
    temp |= (*bufferptr++);
531
31.3k
    if (temp > maxval)
532
195
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
533
31.3k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
534
31.3k
    if (aindex >= 0)
535
6.23k
      ptr[aindex] = _MAXJSAMPLE;
536
31.3k
    ptr += ps;
537
31.3k
  }
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
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
555
4.91k
  ptr = source->pub._buffer[0];
556
4.91k
  bufferptr = source->iobuffer;
557
11.1k
  for (col = cinfo->image_width; col > 0; col--) {
558
6.27k
    register unsigned int gray;
559
6.27k
    gray  = (*bufferptr++) << 8;
560
6.27k
    gray |= (*bufferptr++);
561
6.27k
    if (gray > maxval)
562
39
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
563
6.27k
    rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1,
564
6.27k
                ptr + 2, ptr + 3);
565
6.27k
    ptr += 4;
566
6.27k
  }
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
11.8k
{
575
11.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
576
11.8k
  register _JSAMPROW ptr;
577
11.8k
  register unsigned char *bufferptr;
578
11.8k
  register _JSAMPLE *rescale = source->rescale;
579
11.8k
  JDIMENSION col;
580
11.8k
  unsigned int maxval = source->maxval;
581
11.8k
  register int rindex = rgb_red[cinfo->in_color_space];
582
11.8k
  register int gindex = rgb_green[cinfo->in_color_space];
583
11.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
584
11.8k
  register int aindex = alpha_index[cinfo->in_color_space];
585
11.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
586
587
11.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
588
405
    ERREXIT(cinfo, JERR_INPUT_EOF);
589
11.8k
  ptr = source->pub._buffer[0];
590
11.8k
  bufferptr = source->iobuffer;
591
31.1k
  for (col = cinfo->image_width; col > 0; col--) {
592
19.3k
    register unsigned int temp;
593
19.3k
    temp  = (*bufferptr++) << 8;
594
19.3k
    temp |= (*bufferptr++);
595
19.3k
    if (temp > maxval)
596
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
597
19.3k
    ptr[rindex] = rescale[temp];
598
19.3k
    temp  = (*bufferptr++) << 8;
599
19.3k
    temp |= (*bufferptr++);
600
19.3k
    if (temp > maxval)
601
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
602
19.3k
    ptr[gindex] = rescale[temp];
603
19.3k
    temp  = (*bufferptr++) << 8;
604
19.3k
    temp |= (*bufferptr++);
605
19.3k
    if (temp > maxval)
606
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
607
19.3k
    ptr[bindex] = rescale[temp];
608
19.3k
    if (aindex >= 0)
609
3.78k
      ptr[aindex] = _MAXJSAMPLE;
610
19.3k
    ptr += ps;
611
19.3k
  }
612
11.8k
  return 1;
613
11.8k
}
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.36k
{
620
2.36k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
621
2.36k
  register _JSAMPROW ptr;
622
2.36k
  register unsigned char *bufferptr;
623
2.36k
  register _JSAMPLE *rescale = source->rescale;
624
2.36k
  JDIMENSION col;
625
2.36k
  unsigned int maxval = source->maxval;
626
627
2.36k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
628
81
    ERREXIT(cinfo, JERR_INPUT_EOF);
629
2.36k
  ptr = source->pub._buffer[0];
630
2.36k
  bufferptr = source->iobuffer;
631
6.22k
  for (col = cinfo->image_width; col > 0; col--) {
632
3.86k
    register unsigned int r, g, b;
633
3.86k
    r  = (*bufferptr++) << 8;
634
3.86k
    r |= (*bufferptr++);
635
3.86k
    if (r > maxval)
636
32
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
637
3.86k
    g  = (*bufferptr++) << 8;
638
3.86k
    g |= (*bufferptr++);
639
3.86k
    if (g > maxval)
640
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
641
3.86k
    b  = (*bufferptr++) << 8;
642
3.86k
    b |= (*bufferptr++);
643
3.86k
    if (b > maxval)
644
22
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
645
3.86k
    rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2,
646
3.86k
                ptr + 3);
647
3.86k
    ptr += 4;
648
3.86k
  }
649
2.36k
  return 1;
650
2.36k
}
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
20.5k
{
660
20.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
661
20.5k
  int c;
662
20.5k
  unsigned int w, h, maxval;
663
20.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
664
665
20.5k
  if (getc(source->pub.input_file) != 'P')
666
0
    ERREXIT(cinfo, JERR_PPM_NOT);
667
668
20.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
669
670
  /* detect unsupported variants (ie, PBM) before trying to read header */
671
20.5k
  switch (c) {
672
1.28k
  case '2':                     /* it's a text-format PGM file */
673
2.83k
  case '3':                     /* it's a text-format PPM file */
674
16.3k
  case '5':                     /* it's a raw-format PGM file */
675
20.5k
  case '6':                     /* it's a raw-format PPM file */
676
20.5k
    break;
677
7
  default:
678
7
    ERREXIT(cinfo, JERR_PPM_NOT);
679
7
    break;
680
20.5k
  }
681
682
  /* fetch the remaining header info */
683
20.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
684
20.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
685
20.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
686
687
20.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
688
42
    ERREXIT(cinfo, JERR_PPM_NOT);
689
20.5k
  if (w > JPEG_MAX_DIMENSION || h > JPEG_MAX_DIMENSION)
690
14
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
691
20.5k
  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
20.5k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
695
20.5k
  cinfo->image_width = (JDIMENSION)w;
696
20.5k
  cinfo->image_height = (JDIMENSION)h;
697
20.5k
  source->maxval = maxval;
698
699
  /* initialize flags to most common settings */
700
20.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
701
20.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
702
20.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
703
704
20.5k
  switch (c) {
705
966
  case '2':                     /* it's a text-format PGM file */
706
966
    if (cinfo->in_color_space == JCS_UNKNOWN ||
707
966
        cinfo->in_color_space == JCS_RGB)
708
0
      cinfo->in_color_space = JCS_GRAYSCALE;
709
966
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
710
966
    if (cinfo->in_color_space == JCS_GRAYSCALE)
711
138
      source->pub.get_pixel_rows = get_text_gray_row;
712
828
    else if (IsExtRGB(cinfo->in_color_space))
713
690
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
714
138
    else if (cinfo->in_color_space == JCS_CMYK)
715
138
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
716
0
    else
717
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
718
966
    need_iobuffer = FALSE;
719
966
    break;
720
721
1.22k
  case '3':                     /* it's a text-format PPM file */
722
1.22k
    if (cinfo->in_color_space == JCS_UNKNOWN)
723
0
      cinfo->in_color_space = JCS_EXT_RGB;
724
1.22k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
725
1.22k
    if (IsExtRGB(cinfo->in_color_space))
726
875
      source->pub.get_pixel_rows = get_text_rgb_row;
727
350
    else if (cinfo->in_color_space == JCS_CMYK)
728
175
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
729
175
    else
730
175
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
731
1.22k
    need_iobuffer = FALSE;
732
1.22k
    break;
733
734
13.0k
  case '5':                     /* it's a raw-format PGM file */
735
13.0k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
736
13.0k
        cinfo->in_color_space == JCS_RGB)
737
0
      cinfo->in_color_space = JCS_GRAYSCALE;
738
13.0k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
739
13.0k
    if (maxval > 255) {
740
777
      if (cinfo->in_color_space == JCS_GRAYSCALE)
741
111
        source->pub.get_pixel_rows = get_word_gray_row;
742
666
      else if (IsExtRGB(cinfo->in_color_space))
743
555
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
744
111
      else if (cinfo->in_color_space == JCS_CMYK)
745
111
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
746
0
      else
747
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
748
777
#if BITS_IN_JSAMPLE == 8
749
12.2k
    } else if (maxval == _MAXJSAMPLE &&
750
1.33k
               cinfo->in_color_space == JCS_GRAYSCALE) {
751
190
      source->pub.get_pixel_rows = get_raw_row;
752
190
      use_raw_buffer = TRUE;
753
190
      need_rescale = FALSE;
754
190
#endif
755
12.1k
    } else {
756
12.1k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
757
1.56k
        source->pub.get_pixel_rows = get_scaled_gray_row;
758
10.5k
      else if (IsExtRGB(cinfo->in_color_space))
759
8.78k
        source->pub.get_pixel_rows = get_gray_rgb_row;
760
1.75k
      else if (cinfo->in_color_space == JCS_CMYK)
761
1.75k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
762
0
      else
763
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
764
12.1k
    }
765
13.0k
    break;
766
767
3.87k
  case '6':                     /* it's a raw-format PPM file */
768
3.87k
    if (cinfo->in_color_space == JCS_UNKNOWN)
769
0
      cinfo->in_color_space = JCS_EXT_RGB;
770
3.87k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
771
3.87k
    if (maxval > 255) {
772
1.17k
      if (IsExtRGB(cinfo->in_color_space))
773
840
        source->pub.get_pixel_rows = get_word_rgb_row;
774
336
      else if (cinfo->in_color_space == JCS_CMYK)
775
168
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
776
168
      else
777
168
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
778
1.17k
#if BITS_IN_JSAMPLE == 8
779
2.70k
    } else if (maxval == _MAXJSAMPLE &&
780
532
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
781
532
               (cinfo->in_color_space == JCS_EXT_RGB ||
782
456
                cinfo->in_color_space == JCS_RGB)) {
783
#else
784
               cinfo->in_color_space == JCS_EXT_RGB) {
785
#endif
786
76
      source->pub.get_pixel_rows = get_raw_row;
787
76
      use_raw_buffer = TRUE;
788
76
      need_rescale = FALSE;
789
76
#endif
790
2.62k
    } else {
791
2.62k
      if (IsExtRGB(cinfo->in_color_space))
792
1.85k
        source->pub.get_pixel_rows = get_rgb_row;
793
772
      else if (cinfo->in_color_space == JCS_CMYK)
794
386
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
795
386
      else
796
386
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
797
2.62k
    }
798
3.87k
    break;
799
20.5k
  }
800
801
18.4k
  if (IsExtRGB(cinfo->in_color_space))
802
13.6k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
803
4.73k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
804
2.00k
    cinfo->input_components = 1;
805
2.73k
  else if (cinfo->in_color_space == JCS_CMYK)
806
2.73k
    cinfo->input_components = 4;
807
808
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
809
18.4k
  if (need_iobuffer) {
810
16.3k
    if (c == '6')
811
3.32k
      source->buffer_width = (size_t)w * 3 * (maxval <= 255 ? 1 : 2);
812
13.0k
    else
813
13.0k
      source->buffer_width = (size_t)w * (maxval <= 255 ? 1 : 2);
814
16.3k
    source->iobuffer = (unsigned char *)
815
16.3k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
816
16.3k
                                  source->buffer_width);
817
16.3k
  }
818
819
  /* Create compressor input buffer. */
820
18.4k
  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
266
    source->pixrow = (_JSAMPROW)source->iobuffer;
824
266
    source->pub._buffer = &source->pixrow;
825
266
    source->pub.buffer_height = 1;
826
18.1k
  } else {
827
    /* Need to translate anyway, so make a separate sample buffer. */
828
18.1k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
829
18.1k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
830
18.1k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
831
18.1k
    source->pub.buffer_height = 1;
832
18.1k
  }
833
834
  /* Compute the rescaling array if required. */
835
18.4k
  if (need_rescale) {
836
18.1k
    size_t val, half_maxval;
837
838
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
839
18.1k
    source->rescale = (_JSAMPLE *)
840
18.1k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
841
18.1k
                                  (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
842
18.1k
    memset(source->rescale, 0, (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
843
18.1k
    half_maxval = (size_t)maxval / 2;
844
27.9M
    for (val = 0; val <= (size_t)maxval; val++) {
845
      /* The multiplication here must be done in 32 bits to avoid overflow */
846
27.9M
      source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) /
847
27.9M
                                        maxval);
848
27.9M
    }
849
18.1k
  }
850
18.4k
}
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
13.3k
{
860
  /* no work */
861
13.3k
}
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
20.5k
{
871
20.5k
  ppm_source_ptr source;
872
873
20.5k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
874
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
875
876
  /* Create module interface object */
877
20.5k
  source = (ppm_source_ptr)
878
20.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
879
20.5k
                                sizeof(ppm_source_struct));
880
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
881
20.5k
  source->pub.start_input = start_input_ppm;
882
20.5k
  source->pub.finish_input = finish_input_ppm;
883
20.5k
  source->pub.max_pixels = 0;
884
885
20.5k
  return (cjpeg_source_ptr)source;
886
20.5k
}
jinit_read_ppm
Line
Count
Source
870
20.5k
{
871
20.5k
  ppm_source_ptr source;
872
873
20.5k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
874
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
875
876
  /* Create module interface object */
877
20.5k
  source = (ppm_source_ptr)
878
20.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
879
20.5k
                                sizeof(ppm_source_struct));
880
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
881
20.5k
  source->pub.start_input = start_input_ppm;
882
20.5k
  source->pub.finish_input = finish_input_ppm;
883
20.5k
  source->pub.max_pixels = 0;
884
885
20.5k
  return (cjpeg_source_ptr)source;
886
20.5k
}
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)) */