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