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
14.3M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
33.2M
  (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
264k
{
80
264k
  register int ch;
81
82
264k
  ch = getc(infile);
83
264k
  if (ch == '#') {
84
7.78k
    do {
85
7.78k
      ch = getc(infile);
86
7.78k
    } while (ch != '\n' && ch != EOF);
87
3.53k
  }
88
264k
  return ch;
89
264k
}
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
109k
{
99
109k
  register int ch;
100
109k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
115k
  do {
104
115k
    ch = pbm_getc(infile);
105
115k
    if (ch == EOF)
106
2.03k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
115k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
109k
  if (ch < '0' || ch > '9')
110
189
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
109k
  val = ch - '0';
113
150k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
40.8k
    val *= 10;
115
40.8k
    val += ch - '0';
116
40.8k
    if (val > maxval)
117
131
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
40.8k
  }
119
120
109k
  return val;
121
109k
}
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
2.00k
{
139
2.00k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
140
2.00k
  FILE *infile = source->pub.input_file;
141
2.00k
  register _JSAMPROW ptr;
142
2.00k
  register _JSAMPLE *rescale = source->rescale;
143
2.00k
  JDIMENSION col;
144
2.00k
  unsigned int maxval = source->maxval;
145
146
2.00k
  ptr = source->pub._buffer[0];
147
5.39k
  for (col = cinfo->image_width; col > 0; col--) {
148
3.38k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
149
3.38k
  }
150
2.00k
  return 1;
151
2.00k
}
152
153
154
27.6M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
155
102M
  for (col = cinfo->image_width; col > 0; col--) { \
156
74.3M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
157
74.3M
    alpha_set_op \
158
74.3M
    ptr += ps; \
159
74.3M
  } \
160
27.6M
}
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
10.0k
{
167
10.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
168
10.0k
  FILE *infile = source->pub.input_file;
169
10.0k
  register _JSAMPROW ptr;
170
10.0k
  register _JSAMPLE *rescale = source->rescale;
171
10.0k
  JDIMENSION col;
172
10.0k
  unsigned int maxval = source->maxval;
173
10.0k
  register int rindex = rgb_red[cinfo->in_color_space];
174
10.0k
  register int gindex = rgb_green[cinfo->in_color_space];
175
10.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
176
10.0k
  register int aindex = alpha_index[cinfo->in_color_space];
177
10.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
178
179
10.0k
  ptr = source->pub._buffer[0];
180
10.0k
  if (maxval == _MAXJSAMPLE) {
181
2.66k
    if (aindex >= 0)
182
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
183
2.66k
                         ptr[aindex] = _MAXJSAMPLE;)
184
2.66k
    else
185
2.66k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
186
7.35k
  } else {
187
7.35k
    if (aindex >= 0)
188
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
189
7.35k
                         ptr[aindex] = _MAXJSAMPLE;)
190
7.35k
    else
191
7.35k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
192
7.35k
  }
193
10.0k
  return 1;
194
10.0k
}
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
0
{
202
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
203
0
  FILE *infile = source->pub.input_file;
204
0
  register _JSAMPROW ptr;
205
0
  register _JSAMPLE *rescale = source->rescale;
206
0
  JDIMENSION col;
207
0
  unsigned int maxval = source->maxval;
208
209
0
  ptr = source->pub._buffer[0];
210
0
  if (maxval == _MAXJSAMPLE) {
211
0
    for (col = cinfo->image_width; col > 0; col--) {
212
0
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
213
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
214
0
      ptr += 4;
215
0
    }
216
0
  } else {
217
0
    for (col = cinfo->image_width; col > 0; col--) {
218
0
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
219
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
220
0
      ptr += 4;
221
0
    }
222
0
  }
223
0
  return 1;
224
0
}
225
226
227
67.0k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
228
1.28M
  for (col = cinfo->image_width; col > 0; col--) { \
229
1.21M
    ptr[rindex] = read_op; \
230
1.21M
    ptr[gindex] = read_op; \
231
1.21M
    ptr[bindex] = read_op; \
232
1.21M
    alpha_set_op \
233
1.21M
    ptr += ps; \
234
1.21M
  } \
235
67.0k
}
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.29k
{
241
9.29k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
242
9.29k
  FILE *infile = source->pub.input_file;
243
9.29k
  register _JSAMPROW ptr;
244
9.29k
  register _JSAMPLE *rescale = source->rescale;
245
9.29k
  JDIMENSION col;
246
9.29k
  unsigned int maxval = source->maxval;
247
9.29k
  register int rindex = rgb_red[cinfo->in_color_space];
248
9.29k
  register int gindex = rgb_green[cinfo->in_color_space];
249
9.29k
  register int bindex = rgb_blue[cinfo->in_color_space];
250
9.29k
  register int aindex = alpha_index[cinfo->in_color_space];
251
9.29k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
252
253
9.29k
  ptr = source->pub._buffer[0];
254
9.29k
  if (maxval == _MAXJSAMPLE) {
255
2.83k
    if (aindex >= 0)
256
0
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
257
2.83k
                    ptr[aindex] = _MAXJSAMPLE;)
258
2.83k
    else
259
2.83k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
260
6.46k
  } else {
261
6.46k
    if (aindex >= 0)
262
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
263
6.46k
                    ptr[aindex] = _MAXJSAMPLE;)
264
6.46k
    else
265
6.46k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
266
6.46k
  }
267
9.29k
  return 1;
268
9.29k
}
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
0
{
276
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
277
0
  FILE *infile = source->pub.input_file;
278
0
  register _JSAMPROW ptr;
279
0
  register _JSAMPLE *rescale = source->rescale;
280
0
  JDIMENSION col;
281
0
  unsigned int maxval = source->maxval;
282
283
0
  ptr = source->pub._buffer[0];
284
0
  if (maxval == _MAXJSAMPLE) {
285
0
    for (col = cinfo->image_width; col > 0; col--) {
286
0
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
287
0
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
0
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
290
0
      ptr += 4;
291
0
    }
292
0
  } else {
293
0
    for (col = cinfo->image_width; col > 0; col--) {
294
0
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
295
0
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
0
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
298
0
      ptr += 4;
299
0
    }
300
0
  }
301
0
  return 1;
302
0
}
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
3.47M
{
309
3.47M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
310
3.47M
  register _JSAMPROW ptr;
311
3.47M
  register U_CHAR *bufferptr;
312
3.47M
  register _JSAMPLE *rescale = source->rescale;
313
3.47M
  JDIMENSION col;
314
315
3.47M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
316
51
    ERREXIT(cinfo, JERR_INPUT_EOF);
317
3.47M
  ptr = source->pub._buffer[0];
318
3.47M
  bufferptr = source->iobuffer;
319
16.1M
  for (col = cinfo->image_width; col > 0; col--) {
320
12.7M
    *ptr++ = rescale[UCH(*bufferptr++)];
321
12.7M
  }
322
3.47M
  return 1;
323
3.47M
}
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
27.6M
{
331
27.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
332
27.6M
  register _JSAMPROW ptr;
333
27.6M
  register U_CHAR *bufferptr;
334
27.6M
  register _JSAMPLE *rescale = source->rescale;
335
27.6M
  JDIMENSION col;
336
27.6M
  unsigned int maxval = source->maxval;
337
27.6M
  register int rindex = rgb_red[cinfo->in_color_space];
338
27.6M
  register int gindex = rgb_green[cinfo->in_color_space];
339
27.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
340
27.6M
  register int aindex = alpha_index[cinfo->in_color_space];
341
27.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
342
343
27.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
344
360
    ERREXIT(cinfo, JERR_INPUT_EOF);
345
27.6M
  ptr = source->pub._buffer[0];
346
27.6M
  bufferptr = source->iobuffer;
347
27.6M
  if (maxval == _MAXJSAMPLE) {
348
10.3M
    if (aindex >= 0)
349
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
350
10.3M
    else
351
10.3M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
352
17.3M
  } else {
353
17.3M
    if (aindex >= 0)
354
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
355
17.3M
                         ptr[aindex] = _MAXJSAMPLE;)
356
17.3M
    else
357
17.3M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
358
17.3M
  }
359
27.6M
  return 1;
360
27.6M
}
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
0
{
368
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
369
0
  register _JSAMPROW ptr;
370
0
  register U_CHAR *bufferptr;
371
0
  register _JSAMPLE *rescale = source->rescale;
372
0
  JDIMENSION col;
373
0
  unsigned int maxval = source->maxval;
374
375
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
376
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
377
0
  ptr = source->pub._buffer[0];
378
0
  bufferptr = source->iobuffer;
379
0
  if (maxval == _MAXJSAMPLE) {
380
0
    for (col = cinfo->image_width; col > 0; col--) {
381
0
      _JSAMPLE gray = *bufferptr++;
382
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
383
0
      ptr += 4;
384
0
    }
385
0
  } else {
386
0
    for (col = cinfo->image_width; col > 0; col--) {
387
0
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
388
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
389
0
      ptr += 4;
390
0
    }
391
0
  }
392
0
  return 1;
393
0
}
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
57.7k
{
400
57.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
401
57.7k
  register _JSAMPROW ptr;
402
57.7k
  register U_CHAR *bufferptr;
403
57.7k
  register _JSAMPLE *rescale = source->rescale;
404
57.7k
  JDIMENSION col;
405
57.7k
  unsigned int maxval = source->maxval;
406
57.7k
  register int rindex = rgb_red[cinfo->in_color_space];
407
57.7k
  register int gindex = rgb_green[cinfo->in_color_space];
408
57.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
409
57.7k
  register int aindex = alpha_index[cinfo->in_color_space];
410
57.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
411
412
57.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
413
538
    ERREXIT(cinfo, JERR_INPUT_EOF);
414
57.7k
  ptr = source->pub._buffer[0];
415
57.7k
  bufferptr = source->iobuffer;
416
57.7k
  if (maxval == _MAXJSAMPLE) {
417
17.8k
    if (aindex >= 0)
418
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
419
17.8k
    else
420
17.8k
      RGB_READ_LOOP(*bufferptr++, {})
421
39.9k
  } else {
422
39.9k
    if (aindex >= 0)
423
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = _MAXJSAMPLE;)
424
39.9k
    else
425
39.9k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
426
39.9k
  }
427
57.7k
  return 1;
428
57.7k
}
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
0
{
436
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
437
0
  register _JSAMPROW ptr;
438
0
  register U_CHAR *bufferptr;
439
0
  register _JSAMPLE *rescale = source->rescale;
440
0
  JDIMENSION col;
441
0
  unsigned int maxval = source->maxval;
442
443
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
444
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
445
0
  ptr = source->pub._buffer[0];
446
0
  bufferptr = source->iobuffer;
447
0
  if (maxval == _MAXJSAMPLE) {
448
0
    for (col = cinfo->image_width; col > 0; col--) {
449
0
      _JSAMPLE r = *bufferptr++;
450
0
      _JSAMPLE g = *bufferptr++;
451
0
      _JSAMPLE b = *bufferptr++;
452
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
453
0
      ptr += 4;
454
0
    }
455
0
  } else {
456
0
    for (col = cinfo->image_width; col > 0; col--) {
457
0
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
458
0
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
459
0
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
460
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
461
0
      ptr += 4;
462
0
    }
463
0
  }
464
0
  return 1;
465
0
}
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
2.06M
{
475
2.06M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
476
477
2.06M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
478
48
    ERREXIT(cinfo, JERR_INPUT_EOF);
479
2.06M
  return 1;
480
2.06M
}
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.53k
{
487
1.53k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
488
1.53k
  register _JSAMPROW ptr;
489
1.53k
  register U_CHAR *bufferptr;
490
1.53k
  register _JSAMPLE *rescale = source->rescale;
491
1.53k
  JDIMENSION col;
492
1.53k
  unsigned int maxval = source->maxval;
493
494
1.53k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
495
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
496
1.53k
  ptr = source->pub._buffer[0];
497
1.53k
  bufferptr = source->iobuffer;
498
126k
  for (col = cinfo->image_width; col > 0; col--) {
499
125k
    register unsigned int temp;
500
125k
    temp  = UCH(*bufferptr++) << 8;
501
125k
    temp |= UCH(*bufferptr++);
502
125k
    if (temp > maxval)
503
38
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
504
125k
    *ptr++ = rescale[temp];
505
125k
  }
506
1.53k
  return 1;
507
1.53k
}
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
7.69k
{
514
7.69k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
515
7.69k
  register _JSAMPROW ptr;
516
7.69k
  register U_CHAR *bufferptr;
517
7.69k
  register _JSAMPLE *rescale = source->rescale;
518
7.69k
  JDIMENSION col;
519
7.69k
  unsigned int maxval = source->maxval;
520
7.69k
  register int rindex = rgb_red[cinfo->in_color_space];
521
7.69k
  register int gindex = rgb_green[cinfo->in_color_space];
522
7.69k
  register int bindex = rgb_blue[cinfo->in_color_space];
523
7.69k
  register int aindex = alpha_index[cinfo->in_color_space];
524
7.69k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
525
526
7.69k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
527
280
    ERREXIT(cinfo, JERR_INPUT_EOF);
528
7.69k
  ptr = source->pub._buffer[0];
529
7.69k
  bufferptr = source->iobuffer;
530
632k
  for (col = cinfo->image_width; col > 0; col--) {
531
625k
    register unsigned int temp;
532
625k
    temp  = UCH(*bufferptr++) << 8;
533
625k
    temp |= UCH(*bufferptr++);
534
625k
    if (temp > maxval)
535
190
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
536
625k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
537
625k
    if (aindex >= 0)
538
0
      ptr[aindex] = _MAXJSAMPLE;
539
625k
    ptr += ps;
540
625k
  }
541
7.69k
  return 1;
542
7.69k
}
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
0
{
549
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
550
0
  register _JSAMPROW ptr;
551
0
  register U_CHAR *bufferptr;
552
0
  register _JSAMPLE *rescale = source->rescale;
553
0
  JDIMENSION col;
554
0
  unsigned int maxval = source->maxval;
555
556
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
557
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
558
0
  ptr = source->pub._buffer[0];
559
0
  bufferptr = source->iobuffer;
560
0
  for (col = cinfo->image_width; col > 0; col--) {
561
0
    register unsigned int gray;
562
0
    gray  = UCH(*bufferptr++) << 8;
563
0
    gray |= UCH(*bufferptr++);
564
0
    if (gray > maxval)
565
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
566
0
    rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1,
567
0
                ptr + 2, ptr + 3);
568
0
    ptr += 4;
569
0
  }
570
0
  return 1;
571
0
}
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
12.7k
{
578
12.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
579
12.7k
  register _JSAMPROW ptr;
580
12.7k
  register U_CHAR *bufferptr;
581
12.7k
  register _JSAMPLE *rescale = source->rescale;
582
12.7k
  JDIMENSION col;
583
12.7k
  unsigned int maxval = source->maxval;
584
12.7k
  register int rindex = rgb_red[cinfo->in_color_space];
585
12.7k
  register int gindex = rgb_green[cinfo->in_color_space];
586
12.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
587
12.7k
  register int aindex = alpha_index[cinfo->in_color_space];
588
12.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
589
590
12.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
591
200
    ERREXIT(cinfo, JERR_INPUT_EOF);
592
12.7k
  ptr = source->pub._buffer[0];
593
12.7k
  bufferptr = source->iobuffer;
594
29.1k
  for (col = cinfo->image_width; col > 0; col--) {
595
16.4k
    register unsigned int temp;
596
16.4k
    temp  = UCH(*bufferptr++) << 8;
597
16.4k
    temp |= UCH(*bufferptr++);
598
16.4k
    if (temp > maxval)
599
130
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
600
16.4k
    ptr[rindex] = rescale[temp];
601
16.4k
    temp  = UCH(*bufferptr++) << 8;
602
16.4k
    temp |= UCH(*bufferptr++);
603
16.4k
    if (temp > maxval)
604
115
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
605
16.4k
    ptr[gindex] = rescale[temp];
606
16.4k
    temp  = UCH(*bufferptr++) << 8;
607
16.4k
    temp |= UCH(*bufferptr++);
608
16.4k
    if (temp > maxval)
609
80
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
610
16.4k
    ptr[bindex] = rescale[temp];
611
16.4k
    if (aindex >= 0)
612
0
      ptr[aindex] = _MAXJSAMPLE;
613
16.4k
    ptr += ps;
614
16.4k
  }
615
12.7k
  return 1;
616
12.7k
}
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
0
{
623
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
624
0
  register _JSAMPROW ptr;
625
0
  register U_CHAR *bufferptr;
626
0
  register _JSAMPLE *rescale = source->rescale;
627
0
  JDIMENSION col;
628
0
  unsigned int maxval = source->maxval;
629
630
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
631
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
632
0
  ptr = source->pub._buffer[0];
633
0
  bufferptr = source->iobuffer;
634
0
  for (col = cinfo->image_width; col > 0; col--) {
635
0
    register unsigned int r, g, b;
636
0
    r  = UCH(*bufferptr++) << 8;
637
0
    r |= UCH(*bufferptr++);
638
0
    if (r > maxval)
639
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
640
0
    g  = UCH(*bufferptr++) << 8;
641
0
    g |= UCH(*bufferptr++);
642
0
    if (g > maxval)
643
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
644
0
    b  = UCH(*bufferptr++) << 8;
645
0
    b |= UCH(*bufferptr++);
646
0
    if (b > maxval)
647
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
648
0
    rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2,
649
0
                ptr + 3);
650
0
    ptr += 4;
651
0
  }
652
0
  return 1;
653
0
}
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
16.0k
{
663
16.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
664
16.0k
  int c;
665
16.0k
  unsigned int w, h, maxval;
666
16.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
667
668
16.0k
  if (getc(source->pub.input_file) != 'P')
669
0
    ERREXIT(cinfo, JERR_PPM_NOT);
670
671
16.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
672
673
  /* detect unsupported variants (ie, PBM) before trying to read header */
674
16.0k
  switch (c) {
675
1.05k
  case '2':                     /* it's a text-format PGM file */
676
2.22k
  case '3':                     /* it's a text-format PPM file */
677
13.4k
  case '5':                     /* it's a raw-format PGM file */
678
16.0k
  case '6':                     /* it's a raw-format PPM file */
679
16.0k
    break;
680
12
  default:
681
12
    ERREXIT(cinfo, JERR_PPM_NOT);
682
12
    break;
683
16.0k
  }
684
685
  /* fetch the remaining header info */
686
16.0k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
687
16.0k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
688
16.0k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
689
690
16.0k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
691
18
    ERREXIT(cinfo, JERR_PPM_NOT);
692
16.0k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
693
16.0k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
694
198
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
695
16.0k
#endif
696
697
16.0k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
698
16.0k
  cinfo->image_width = (JDIMENSION)w;
699
16.0k
  cinfo->image_height = (JDIMENSION)h;
700
16.0k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
16.0k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
16.0k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
16.0k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
16.0k
  switch (c) {
708
768
  case '2':                     /* it's a text-format PGM file */
709
768
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
768
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
768
    TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
713
768
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
128
      source->pub.get_pixel_rows = get_text_gray_row;
715
640
    else if (IsExtRGB(cinfo->in_color_space))
716
640
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
0
    else if (cinfo->in_color_space == JCS_CMYK)
718
0
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
768
    need_iobuffer = FALSE;
722
768
    break;
723
724
738
  case '3':                     /* it's a text-format PPM file */
725
738
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
738
    TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
728
738
    if (IsExtRGB(cinfo->in_color_space))
729
615
      source->pub.get_pixel_rows = get_text_rgb_row;
730
123
    else if (cinfo->in_color_space == JCS_CMYK)
731
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
123
    else
733
123
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
738
    need_iobuffer = FALSE;
735
738
    break;
736
737
10.8k
  case '5':                     /* it's a raw-format PGM file */
738
10.8k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
10.8k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
10.8k
    TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
742
10.8k
    if (maxval > 255) {
743
630
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
105
        source->pub.get_pixel_rows = get_word_gray_row;
745
525
      else if (IsExtRGB(cinfo->in_color_space))
746
525
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
0
      else if (cinfo->in_color_space == JCS_CMYK)
748
0
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
10.2k
    } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
10.2k
               cinfo->in_color_space == JCS_GRAYSCALE) {
753
159
      source->pub.get_pixel_rows = get_raw_row;
754
159
      use_raw_buffer = TRUE;
755
159
      need_rescale = FALSE;
756
10.1k
    } else {
757
10.1k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
758
1.55k
        source->pub.get_pixel_rows = get_scaled_gray_row;
759
8.55k
      else if (IsExtRGB(cinfo->in_color_space))
760
8.55k
        source->pub.get_pixel_rows = get_gray_rgb_row;
761
0
      else if (cinfo->in_color_space == JCS_CMYK)
762
0
        source->pub.get_pixel_rows = get_gray_cmyk_row;
763
0
      else
764
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
765
10.1k
    }
766
10.8k
    break;
767
768
2.35k
  case '6':                     /* it's a raw-format PPM file */
769
2.35k
    if (cinfo->in_color_space == JCS_UNKNOWN)
770
0
      cinfo->in_color_space = JCS_EXT_RGB;
771
2.35k
    TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
772
2.35k
    if (maxval > 255) {
773
684
      if (IsExtRGB(cinfo->in_color_space))
774
570
        source->pub.get_pixel_rows = get_word_rgb_row;
775
114
      else if (cinfo->in_color_space == JCS_CMYK)
776
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
777
114
      else
778
114
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
779
1.67k
    } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
780
1.67k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
781
1.67k
               (cinfo->in_color_space == JCS_EXT_RGB ||
782
378
                cinfo->in_color_space == JCS_RGB)) {
783
#else
784
               cinfo->in_color_space == JCS_EXT_RGB) {
785
#endif
786
63
      source->pub.get_pixel_rows = get_raw_row;
787
63
      use_raw_buffer = TRUE;
788
63
      need_rescale = FALSE;
789
1.61k
    } else {
790
1.61k
      if (IsExtRGB(cinfo->in_color_space))
791
1.33k
        source->pub.get_pixel_rows = get_rgb_row;
792
279
      else if (cinfo->in_color_space == JCS_CMYK)
793
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
794
279
      else
795
279
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
796
1.61k
    }
797
2.35k
    break;
798
16.0k
  }
799
800
14.2k
  if (IsExtRGB(cinfo->in_color_space))
801
12.2k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
802
1.94k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
803
1.94k
    cinfo->input_components = 1;
804
0
  else if (cinfo->in_color_space == JCS_CMYK)
805
0
    cinfo->input_components = 4;
806
807
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
808
14.2k
  if (need_iobuffer) {
809
12.8k
    if (c == '6')
810
1.96k
      source->buffer_width = (size_t)w * 3 *
811
1.96k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
812
10.8k
    else
813
10.8k
      source->buffer_width = (size_t)w *
814
10.8k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
815
12.8k
    source->iobuffer = (U_CHAR *)
816
12.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
817
12.8k
                                  source->buffer_width);
818
12.8k
  }
819
820
  /* Create compressor input buffer. */
821
14.2k
  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
222
    source->pixrow = (_JSAMPROW)source->iobuffer;
825
222
    source->pub._buffer = &source->pixrow;
826
222
    source->pub.buffer_height = 1;
827
14.0k
  } else {
828
    /* Need to translate anyway, so make a separate sample buffer. */
829
14.0k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
830
14.0k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
831
14.0k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
832
14.0k
    source->pub.buffer_height = 1;
833
14.0k
  }
834
835
  /* Compute the rescaling array if required. */
836
14.2k
  if (need_rescale) {
837
14.0k
    long val, half_maxval;
838
839
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
840
14.0k
    source->rescale = (_JSAMPLE *)
841
14.0k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
842
14.0k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
843
14.0k
                                           sizeof(_JSAMPLE)));
844
14.0k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
845
14.0k
                                        sizeof(_JSAMPLE)));
846
14.0k
    half_maxval = maxval / 2;
847
17.6M
    for (val = 0; val <= (long)maxval; val++) {
848
      /* The multiplication here must be done in 32 bits to avoid overflow */
849
17.5M
      source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) /
850
17.5M
                                        maxval);
851
17.5M
    }
852
14.0k
  }
853
14.2k
}
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
10.8k
{
863
  /* no work */
864
10.8k
}
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
16.0k
{
874
16.0k
  ppm_source_ptr source;
875
876
16.0k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
877
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
878
879
  /* Create module interface object */
880
16.0k
  source = (ppm_source_ptr)
881
16.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
882
16.0k
                                sizeof(ppm_source_struct));
883
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
884
16.0k
  source->pub.start_input = start_input_ppm;
885
16.0k
  source->pub.finish_input = finish_input_ppm;
886
16.0k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
887
16.0k
  source->pub.max_pixels = 0;
888
16.0k
#endif
889
890
16.0k
  return (cjpeg_source_ptr)source;
891
16.0k
}
jinit_read_ppm
Line
Count
Source
873
16.0k
{
874
16.0k
  ppm_source_ptr source;
875
876
16.0k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
877
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
878
879
  /* Create module interface object */
880
16.0k
  source = (ppm_source_ptr)
881
16.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
882
16.0k
                                sizeof(ppm_source_struct));
883
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
884
16.0k
  source->pub.start_input = start_input_ppm;
885
16.0k
  source->pub.finish_input = finish_input_ppm;
886
16.0k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
887
16.0k
  source->pub.max_pixels = 0;
888
16.0k
#endif
889
890
16.0k
  return (cjpeg_source_ptr)source;
891
16.0k
}
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)) */