Coverage Report

Created: 2023-06-07 06:03

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