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