Coverage Report

Created: 2023-06-07 06:03

/src/libjpeg-turbo.2.1.x/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
#ifdef PPM_SUPPORTED
29
30
31
/* Portions of this code are based on the PBMPLUS library, which is:
32
**
33
** Copyright (C) 1988 by Jef Poskanzer.
34
**
35
** Permission to use, copy, modify, and distribute this software and its
36
** documentation for any purpose and without fee is hereby granted, provided
37
** that the above copyright notice appear in all copies and that both that
38
** copyright notice and this permission notice appear in supporting
39
** documentation.  This software is provided "as is" without express or
40
** implied warranty.
41
*/
42
43
44
/* Macros to deal with unsigned chars as efficiently as compiler allows */
45
46
typedef unsigned char U_CHAR;
47
169M
#define UCH(x)  ((int)(x))
48
49
50
#define ReadOK(file, buffer, len) \
51
313M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
52
53
static int alpha_index[JPEG_NUMCS] = {
54
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
55
};
56
57
58
/* Private version of data source object */
59
60
typedef struct {
61
  struct cjpeg_source_struct pub; /* public fields */
62
63
  /* Usually these two pointers point to the same place: */
64
  U_CHAR *iobuffer;             /* fread's I/O buffer */
65
  JSAMPROW pixrow;              /* compressor input buffer */
66
  size_t buffer_width;          /* width of I/O buffer */
67
  JSAMPLE *rescale;             /* => maxval-remapping array, or NULL */
68
  unsigned int maxval;
69
} ppm_source_struct;
70
71
typedef ppm_source_struct *ppm_source_ptr;
72
73
74
LOCAL(int)
75
pbm_getc(FILE *infile)
76
/* Read next char, skipping over any comments */
77
/* A comment/newline sequence is returned as a newline */
78
3.07M
{
79
3.07M
  register int ch;
80
81
3.07M
  ch = getc(infile);
82
3.07M
  if (ch == '#') {
83
582k
    do {
84
582k
      ch = getc(infile);
85
582k
    } while (ch != '\n' && ch != EOF);
86
56.1k
  }
87
3.07M
  return ch;
88
3.07M
}
89
90
91
LOCAL(unsigned int)
92
read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
93
/* Read an unsigned decimal integer from the PPM file */
94
/* Swallows one trailing character after the integer */
95
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
96
/* This should not be a problem in practice. */
97
1.25M
{
98
1.25M
  register int ch;
99
1.25M
  register unsigned int val;
100
101
  /* Skip any leading whitespace */
102
1.36M
  do {
103
1.36M
    ch = pbm_getc(infile);
104
1.36M
    if (ch == EOF)
105
25.4k
      ERREXIT(cinfo, JERR_INPUT_EOF);
106
1.36M
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
107
108
1.25M
  if (ch < '0' || ch > '9')
109
2.56k
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
110
111
1.25M
  val = ch - '0';
112
1.73M
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
113
477k
    val *= 10;
114
477k
    val += ch - '0';
115
477k
    if (val > maxval)
116
1.58k
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
117
477k
  }
118
119
1.25M
  return val;
120
1.25M
}
121
122
123
/*
124
 * Read one row of pixels.
125
 *
126
 * We provide several different versions depending on input file format.
127
 * In all cases, input is scaled to the size of JSAMPLE.
128
 *
129
 * A really fast path is provided for reading byte/sample raw files with
130
 * maxval = MAXJSAMPLE, which is the normal case for 8-bit data.
131
 */
132
133
134
METHODDEF(JDIMENSION)
135
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
136
/* This version is for reading text-format PGM files with any maxval */
137
22.7k
{
138
22.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
139
22.7k
  FILE *infile = source->pub.input_file;
140
22.7k
  register JSAMPROW ptr;
141
22.7k
  register JSAMPLE *rescale = source->rescale;
142
22.7k
  JDIMENSION col;
143
22.7k
  unsigned int maxval = source->maxval;
144
145
22.7k
  ptr = source->pub.buffer[0];
146
61.7k
  for (col = cinfo->image_width; col > 0; col--) {
147
39.0k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
148
39.0k
  }
149
22.7k
  return 1;
150
22.7k
}
151
152
153
232M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
154
747M
  for (col = cinfo->image_width; col > 0; col--) { \
155
514M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
156
514M
    alpha_set_op \
157
514M
    ptr += ps; \
158
514M
  } \
159
232M
}
160
161
METHODDEF(JDIMENSION)
162
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
163
/* This version is for reading text-format PGM files with any maxval and
164
   converting to extended RGB */
165
110k
{
166
110k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
167
110k
  FILE *infile = source->pub.input_file;
168
110k
  register JSAMPROW ptr;
169
110k
  register JSAMPLE *rescale = source->rescale;
170
110k
  JDIMENSION col;
171
110k
  unsigned int maxval = source->maxval;
172
110k
  register int rindex = rgb_red[cinfo->in_color_space];
173
110k
  register int gindex = rgb_green[cinfo->in_color_space];
174
110k
  register int bindex = rgb_blue[cinfo->in_color_space];
175
110k
  register int aindex = alpha_index[cinfo->in_color_space];
176
110k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
177
178
110k
  ptr = source->pub.buffer[0];
179
110k
  if (maxval == MAXJSAMPLE) {
180
18.9k
    if (aindex >= 0)
181
2.18k
      GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
182
18.9k
                         ptr[aindex] = MAXJSAMPLE;)
183
16.8k
    else
184
16.8k
      GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
185
91.1k
  } else {
186
91.1k
    if (aindex >= 0)
187
13.9k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
188
91.1k
                         ptr[aindex] = MAXJSAMPLE;)
189
77.1k
    else
190
77.1k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
191
91.1k
  }
192
110k
  return 1;
193
110k
}
194
195
196
METHODDEF(JDIMENSION)
197
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
198
/* This version is for reading text-format PGM files with any maxval and
199
   converting to CMYK */
200
16.1k
{
201
16.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
202
16.1k
  FILE *infile = source->pub.input_file;
203
16.1k
  register JSAMPROW ptr;
204
16.1k
  register JSAMPLE *rescale = source->rescale;
205
16.1k
  JDIMENSION col;
206
16.1k
  unsigned int maxval = source->maxval;
207
208
16.1k
  ptr = source->pub.buffer[0];
209
16.1k
  if (maxval == MAXJSAMPLE) {
210
7.62k
    for (col = cinfo->image_width; col > 0; col--) {
211
5.44k
      JSAMPLE gray = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
212
5.44k
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
213
5.44k
      ptr += 4;
214
5.44k
    }
215
13.9k
  } else {
216
34.3k
    for (col = cinfo->image_width; col > 0; col--) {
217
20.3k
      JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
218
20.3k
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
219
20.3k
      ptr += 4;
220
20.3k
    }
221
13.9k
  }
222
16.1k
  return 1;
223
16.1k
}
224
225
226
3.01M
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
227
29.3M
  for (col = cinfo->image_width; col > 0; col--) { \
228
26.3M
    ptr[rindex] = read_op; \
229
26.3M
    ptr[gindex] = read_op; \
230
26.3M
    ptr[bindex] = read_op; \
231
26.3M
    alpha_set_op \
232
26.3M
    ptr += ps; \
233
26.3M
  } \
234
3.01M
}
235
236
METHODDEF(JDIMENSION)
237
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
238
/* This version is for reading text-format PPM files with any maxval */
239
89.7k
{
240
89.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
241
89.7k
  FILE *infile = source->pub.input_file;
242
89.7k
  register JSAMPROW ptr;
243
89.7k
  register JSAMPLE *rescale = source->rescale;
244
89.7k
  JDIMENSION col;
245
89.7k
  unsigned int maxval = source->maxval;
246
89.7k
  register int rindex = rgb_red[cinfo->in_color_space];
247
89.7k
  register int gindex = rgb_green[cinfo->in_color_space];
248
89.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
249
89.7k
  register int aindex = alpha_index[cinfo->in_color_space];
250
89.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
251
252
89.7k
  ptr = source->pub.buffer[0];
253
89.7k
  if (maxval == MAXJSAMPLE) {
254
29.3k
    if (aindex >= 0)
255
3.98k
      RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
256
29.3k
                    ptr[aindex] = MAXJSAMPLE;)
257
25.4k
    else
258
25.4k
      RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
259
60.3k
  } else {
260
60.3k
    if (aindex >= 0)
261
8.17k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
262
60.3k
                    ptr[aindex] = MAXJSAMPLE;)
263
52.1k
    else
264
52.1k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
265
60.3k
  }
266
89.7k
  return 1;
267
89.7k
}
268
269
270
METHODDEF(JDIMENSION)
271
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
272
/* This version is for reading text-format PPM files with any maxval and
273
   converting to CMYK */
274
12.1k
{
275
12.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
276
12.1k
  FILE *infile = source->pub.input_file;
277
12.1k
  register JSAMPROW ptr;
278
12.1k
  register JSAMPLE *rescale = source->rescale;
279
12.1k
  JDIMENSION col;
280
12.1k
  unsigned int maxval = source->maxval;
281
282
12.1k
  ptr = source->pub.buffer[0];
283
12.1k
  if (maxval == MAXJSAMPLE) {
284
14.0k
    for (col = cinfo->image_width; col > 0; col--) {
285
10.0k
      JSAMPLE r = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
286
10.0k
      JSAMPLE g = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
287
10.0k
      JSAMPLE b = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
10.0k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
289
10.0k
      ptr += 4;
290
10.0k
    }
291
8.17k
  } else {
292
21.9k
    for (col = cinfo->image_width; col > 0; col--) {
293
13.7k
      JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
294
13.7k
      JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
295
13.7k
      JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
13.7k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
297
13.7k
      ptr += 4;
298
13.7k
    }
299
8.17k
  }
300
12.1k
  return 1;
301
12.1k
}
302
303
304
METHODDEF(JDIMENSION)
305
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
306
/* This version is for reading raw-byte-format PGM files with any maxval */
307
42.1M
{
308
42.1M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
309
42.1M
  register JSAMPROW ptr;
310
42.1M
  register U_CHAR *bufferptr;
311
42.1M
  register JSAMPLE *rescale = source->rescale;
312
42.1M
  JDIMENSION col;
313
314
42.1M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
315
805
    ERREXIT(cinfo, JERR_INPUT_EOF);
316
42.1M
  ptr = source->pub.buffer[0];
317
42.1M
  bufferptr = source->iobuffer;
318
135M
  for (col = cinfo->image_width; col > 0; col--) {
319
93.0M
    *ptr++ = rescale[UCH(*bufferptr++)];
320
93.0M
  }
321
42.1M
  return 1;
322
42.1M
}
323
324
325
METHODDEF(JDIMENSION)
326
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
327
/* This version is for reading raw-byte-format PGM files with any maxval
328
   and converting to extended RGB */
329
232M
{
330
232M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
331
232M
  register JSAMPROW ptr;
332
232M
  register U_CHAR *bufferptr;
333
232M
  register JSAMPLE *rescale = source->rescale;
334
232M
  JDIMENSION col;
335
232M
  unsigned int maxval = source->maxval;
336
232M
  register int rindex = rgb_red[cinfo->in_color_space];
337
232M
  register int gindex = rgb_green[cinfo->in_color_space];
338
232M
  register int bindex = rgb_blue[cinfo->in_color_space];
339
232M
  register int aindex = alpha_index[cinfo->in_color_space];
340
232M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
341
342
232M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
343
4.22k
    ERREXIT(cinfo, JERR_INPUT_EOF);
344
232M
  ptr = source->pub.buffer[0];
345
232M
  bufferptr = source->iobuffer;
346
232M
  if (maxval == MAXJSAMPLE) {
347
21.7M
    if (aindex >= 0)
348
827k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;)
349
20.9M
    else
350
20.9M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
351
210M
  } else {
352
210M
    if (aindex >= 0)
353
30.1M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;)
354
180M
    else
355
180M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
356
210M
  }
357
232M
  return 1;
358
232M
}
359
360
361
METHODDEF(JDIMENSION)
362
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
363
/* This version is for reading raw-byte-format PGM files with any maxval
364
   and converting to CMYK */
365
31.0M
{
366
31.0M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
367
31.0M
  register JSAMPROW ptr;
368
31.0M
  register U_CHAR *bufferptr;
369
31.0M
  register JSAMPLE *rescale = source->rescale;
370
31.0M
  JDIMENSION col;
371
31.0M
  unsigned int maxval = source->maxval;
372
373
31.0M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
374
595
    ERREXIT(cinfo, JERR_INPUT_EOF);
375
31.0M
  ptr = source->pub.buffer[0];
376
31.0M
  bufferptr = source->iobuffer;
377
31.0M
  if (maxval == MAXJSAMPLE) {
378
5.43M
    for (col = cinfo->image_width; col > 0; col--) {
379
4.60M
      JSAMPLE gray = *bufferptr++;
380
4.60M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
381
4.60M
      ptr += 4;
382
4.60M
    }
383
30.1M
  } else {
384
93.3M
    for (col = cinfo->image_width; col > 0; col--) {
385
63.1M
      JSAMPLE gray = rescale[UCH(*bufferptr++)];
386
63.1M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
387
63.1M
      ptr += 4;
388
63.1M
    }
389
30.1M
  }
390
31.0M
  return 1;
391
31.0M
}
392
393
394
METHODDEF(JDIMENSION)
395
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
396
/* This version is for reading raw-byte-format PPM files with any maxval */
397
2.92M
{
398
2.92M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
399
2.92M
  register JSAMPROW ptr;
400
2.92M
  register U_CHAR *bufferptr;
401
2.92M
  register JSAMPLE *rescale = source->rescale;
402
2.92M
  JDIMENSION col;
403
2.92M
  unsigned int maxval = source->maxval;
404
2.92M
  register int rindex = rgb_red[cinfo->in_color_space];
405
2.92M
  register int gindex = rgb_green[cinfo->in_color_space];
406
2.92M
  register int bindex = rgb_blue[cinfo->in_color_space];
407
2.92M
  register int aindex = alpha_index[cinfo->in_color_space];
408
2.92M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
409
410
2.92M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
411
6.48k
    ERREXIT(cinfo, JERR_INPUT_EOF);
412
2.92M
  ptr = source->pub.buffer[0];
413
2.92M
  bufferptr = source->iobuffer;
414
2.92M
  if (maxval == MAXJSAMPLE) {
415
190k
    if (aindex >= 0)
416
20.1k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;)
417
170k
    else
418
170k
      RGB_READ_LOOP(*bufferptr++, {})
419
2.73M
  } else {
420
2.73M
    if (aindex >= 0)
421
462k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;)
422
2.27M
    else
423
2.27M
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
424
2.73M
  }
425
2.92M
  return 1;
426
2.92M
}
427
428
429
METHODDEF(JDIMENSION)
430
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
431
/* This version is for reading raw-byte-format PPM files with any maxval and
432
   converting to CMYK */
433
483k
{
434
483k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
435
483k
  register JSAMPROW ptr;
436
483k
  register U_CHAR *bufferptr;
437
483k
  register JSAMPLE *rescale = source->rescale;
438
483k
  JDIMENSION col;
439
483k
  unsigned int maxval = source->maxval;
440
441
483k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
442
968
    ERREXIT(cinfo, JERR_INPUT_EOF);
443
483k
  ptr = source->pub.buffer[0];
444
483k
  bufferptr = source->iobuffer;
445
483k
  if (maxval == MAXJSAMPLE) {
446
743k
    for (col = cinfo->image_width; col > 0; col--) {
447
723k
      JSAMPLE r = *bufferptr++;
448
723k
      JSAMPLE g = *bufferptr++;
449
723k
      JSAMPLE b = *bufferptr++;
450
723k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
451
723k
      ptr += 4;
452
723k
    }
453
463k
  } else {
454
3.99M
    for (col = cinfo->image_width; col > 0; col--) {
455
3.53M
      JSAMPLE r = rescale[UCH(*bufferptr++)];
456
3.53M
      JSAMPLE g = rescale[UCH(*bufferptr++)];
457
3.53M
      JSAMPLE b = rescale[UCH(*bufferptr++)];
458
3.53M
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
459
3.53M
      ptr += 4;
460
3.53M
    }
461
463k
  }
462
483k
  return 1;
463
483k
}
464
465
466
METHODDEF(JDIMENSION)
467
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
468
/* This version is for reading raw-byte-format files with maxval = MAXJSAMPLE.
469
 * In this case we just read right into the JSAMPLE buffer!
470
 * Note that same code works for PPM and PGM files.
471
 */
472
4.39M
{
473
4.39M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
474
475
4.39M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
476
584
    ERREXIT(cinfo, JERR_INPUT_EOF);
477
4.39M
  return 1;
478
4.39M
}
479
480
481
METHODDEF(JDIMENSION)
482
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
483
/* This version is for reading raw-word-format PGM files with any maxval */
484
215k
{
485
215k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
486
215k
  register JSAMPROW ptr;
487
215k
  register U_CHAR *bufferptr;
488
215k
  register JSAMPLE *rescale = source->rescale;
489
215k
  JDIMENSION col;
490
215k
  unsigned int maxval = source->maxval;
491
492
215k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
493
646
    ERREXIT(cinfo, JERR_INPUT_EOF);
494
215k
  ptr = source->pub.buffer[0];
495
215k
  bufferptr = source->iobuffer;
496
812k
  for (col = cinfo->image_width; col > 0; col--) {
497
596k
    register unsigned int temp;
498
596k
    temp  = UCH(*bufferptr++) << 8;
499
596k
    temp |= UCH(*bufferptr++);
500
596k
    if (temp > maxval)
501
314
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
502
596k
    *ptr++ = rescale[temp];
503
596k
  }
504
215k
  return 1;
505
215k
}
506
507
508
METHODDEF(JDIMENSION)
509
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
510
/* This version is for reading raw-word-format PPM files with any maxval */
511
94.8k
{
512
94.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
513
94.8k
  register JSAMPROW ptr;
514
94.8k
  register U_CHAR *bufferptr;
515
94.8k
  register JSAMPLE *rescale = source->rescale;
516
94.8k
  JDIMENSION col;
517
94.8k
  unsigned int maxval = source->maxval;
518
94.8k
  register int rindex = rgb_red[cinfo->in_color_space];
519
94.8k
  register int gindex = rgb_green[cinfo->in_color_space];
520
94.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
521
94.8k
  register int aindex = alpha_index[cinfo->in_color_space];
522
94.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
523
524
94.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
525
3.37k
    ERREXIT(cinfo, JERR_INPUT_EOF);
526
94.8k
  ptr = source->pub.buffer[0];
527
94.8k
  bufferptr = source->iobuffer;
528
351k
  for (col = cinfo->image_width; col > 0; col--) {
529
256k
    register unsigned int temp;
530
256k
    temp  = UCH(*bufferptr++) << 8;
531
256k
    temp |= UCH(*bufferptr++);
532
256k
    if (temp > maxval)
533
1.33k
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
534
256k
    ptr[rindex] = rescale[temp];
535
256k
    temp  = UCH(*bufferptr++) << 8;
536
256k
    temp |= UCH(*bufferptr++);
537
256k
    if (temp > maxval)
538
1.13k
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
256k
    ptr[gindex] = rescale[temp];
540
256k
    temp  = UCH(*bufferptr++) << 8;
541
256k
    temp |= UCH(*bufferptr++);
542
256k
    if (temp > maxval)
543
1.08k
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
544
256k
    ptr[bindex] = rescale[temp];
545
256k
    if (aindex >= 0)
546
44.0k
      ptr[aindex] = MAXJSAMPLE;
547
256k
    ptr += ps;
548
256k
  }
549
94.8k
  return 1;
550
94.8k
}
551
552
553
/*
554
 * Read the file header; return image size and component count.
555
 */
556
557
METHODDEF(void)
558
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
559
61.8k
{
560
61.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
561
61.8k
  int c;
562
61.8k
  unsigned int w, h, maxval;
563
61.8k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
564
565
61.8k
  if (getc(source->pub.input_file) != 'P')
566
0
    ERREXIT(cinfo, JERR_PPM_NOT);
567
568
61.8k
  c = getc(source->pub.input_file); /* subformat discriminator character */
569
570
  /* detect unsupported variants (ie, PBM) before trying to read header */
571
61.8k
  switch (c) {
572
5.17k
  case '2':                     /* it's a text-format PGM file */
573
10.9k
  case '3':                     /* it's a text-format PPM file */
574
47.5k
  case '5':                     /* it's a raw-format PGM file */
575
61.7k
  case '6':                     /* it's a raw-format PPM file */
576
61.7k
    break;
577
45
  default:
578
45
    ERREXIT(cinfo, JERR_PPM_NOT);
579
45
    break;
580
61.8k
  }
581
582
  /* fetch the remaining header info */
583
61.7k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
584
61.7k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
585
61.7k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
586
587
61.7k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
588
103
    ERREXIT(cinfo, JERR_PPM_NOT);
589
61.7k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
590
61.7k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
591
940
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
592
61.7k
#endif
593
594
61.7k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
595
61.7k
  cinfo->image_width = (JDIMENSION)w;
596
61.7k
  cinfo->image_height = (JDIMENSION)h;
597
61.7k
  source->maxval = maxval;
598
599
  /* initialize flags to most common settings */
600
61.7k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
601
61.7k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
602
61.7k
  need_rescale = TRUE;          /* do we need a rescale array? */
603
604
61.7k
  switch (c) {
605
3.64k
  case '2':                     /* it's a text-format PGM file */
606
3.64k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
607
3.64k
        cinfo->in_color_space == JCS_RGB)
608
438
      cinfo->in_color_space = JCS_GRAYSCALE;
609
3.64k
    TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
610
3.64k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
611
931
      source->pub.get_pixel_rows = get_text_gray_row;
612
2.71k
    else if (IsExtRGB(cinfo->in_color_space))
613
2.46k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
614
245
    else if (cinfo->in_color_space == JCS_CMYK)
615
245
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
616
0
    else
617
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
618
3.64k
    need_iobuffer = FALSE;
619
3.64k
    break;
620
621
4.33k
  case '3':                     /* it's a text-format PPM file */
622
4.33k
    if (cinfo->in_color_space == JCS_UNKNOWN)
623
0
      cinfo->in_color_space = JCS_EXT_RGB;
624
4.33k
    TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
625
4.33k
    if (IsExtRGB(cinfo->in_color_space))
626
3.40k
      source->pub.get_pixel_rows = get_text_rgb_row;
627
933
    else if (cinfo->in_color_space == JCS_CMYK)
628
341
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
629
592
    else
630
592
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
631
4.33k
    need_iobuffer = FALSE;
632
4.33k
    break;
633
634
34.9k
  case '5':                     /* it's a raw-format PGM file */
635
34.9k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
636
34.9k
        cinfo->in_color_space == JCS_RGB)
637
938
      cinfo->in_color_space = JCS_GRAYSCALE;
638
34.9k
    TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
639
34.9k
    if (maxval > 255) {
640
2.39k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
641
609
        source->pub.get_pixel_rows = get_word_gray_row;
642
1.79k
      else
643
1.79k
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
644
32.5k
    } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
645
32.5k
               cinfo->in_color_space == JCS_GRAYSCALE) {
646
656
      source->pub.get_pixel_rows = get_raw_row;
647
656
      use_raw_buffer = TRUE;
648
656
      need_rescale = FALSE;
649
31.8k
    } else {
650
31.8k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
651
5.01k
        source->pub.get_pixel_rows = get_scaled_gray_row;
652
26.8k
      else if (IsExtRGB(cinfo->in_color_space))
653
25.0k
        source->pub.get_pixel_rows = get_gray_rgb_row;
654
1.76k
      else if (cinfo->in_color_space == JCS_CMYK)
655
1.76k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
656
0
      else
657
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
658
31.8k
    }
659
34.9k
    break;
660
661
12.6k
  case '6':                     /* it's a raw-format PPM file */
662
12.6k
    if (cinfo->in_color_space == JCS_UNKNOWN)
663
0
      cinfo->in_color_space = JCS_EXT_RGB;
664
12.6k
    TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
665
12.6k
    if (maxval > 255) {
666
3.45k
      if (IsExtRGB(cinfo->in_color_space))
667
2.74k
        source->pub.get_pixel_rows = get_word_rgb_row;
668
703
      else
669
703
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
670
9.18k
    } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
671
9.18k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
672
9.18k
               (cinfo->in_color_space == JCS_EXT_RGB ||
673
2.10k
                cinfo->in_color_space == JCS_RGB)) {
674
#else
675
               cinfo->in_color_space == JCS_EXT_RGB) {
676
#endif
677
379
      source->pub.get_pixel_rows = get_raw_row;
678
379
      use_raw_buffer = TRUE;
679
379
      need_rescale = FALSE;
680
8.80k
    } else {
681
8.80k
      if (IsExtRGB(cinfo->in_color_space))
682
6.97k
        source->pub.get_pixel_rows = get_rgb_row;
683
1.83k
      else if (cinfo->in_color_space == JCS_CMYK)
684
634
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
685
1.20k
      else
686
1.20k
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
687
8.80k
    }
688
12.6k
    break;
689
61.7k
  }
690
691
51.2k
  if (IsExtRGB(cinfo->in_color_space))
692
41.0k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
693
10.2k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
694
7.21k
    cinfo->input_components = 1;
695
2.98k
  else if (cinfo->in_color_space == JCS_CMYK)
696
2.98k
    cinfo->input_components = 4;
697
698
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
699
51.2k
  if (need_iobuffer) {
700
43.8k
    if (c == '6')
701
10.7k
      source->buffer_width = (size_t)w * 3 *
702
10.7k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
703
33.1k
    else
704
33.1k
      source->buffer_width = (size_t)w *
705
33.1k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
706
43.8k
    source->iobuffer = (U_CHAR *)
707
43.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
708
43.8k
                                  source->buffer_width);
709
43.8k
  }
710
711
  /* Create compressor input buffer. */
712
51.2k
  if (use_raw_buffer) {
713
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
714
    /* Synthesize a JSAMPARRAY pointer structure */
715
1.03k
    source->pixrow = (JSAMPROW)source->iobuffer;
716
1.03k
    source->pub.buffer = &source->pixrow;
717
1.03k
    source->pub.buffer_height = 1;
718
50.2k
  } else {
719
    /* Need to translate anyway, so make a separate sample buffer. */
720
50.2k
    source->pub.buffer = (*cinfo->mem->alloc_sarray)
721
50.2k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
722
50.2k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
723
50.2k
    source->pub.buffer_height = 1;
724
50.2k
  }
725
726
  /* Compute the rescaling array if required. */
727
51.2k
  if (need_rescale) {
728
50.2k
    long val, half_maxval;
729
730
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
731
50.2k
    source->rescale = (JSAMPLE *)
732
50.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
733
50.2k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
734
50.2k
                                           sizeof(JSAMPLE)));
735
50.2k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
736
50.2k
                                        sizeof(JSAMPLE)));
737
50.2k
    half_maxval = maxval / 2;
738
57.5M
    for (val = 0; val <= (long)maxval; val++) {
739
      /* The multiplication here must be done in 32 bits to avoid overflow */
740
57.5M
      source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) /
741
57.5M
                                        maxval);
742
57.5M
    }
743
50.2k
  }
744
51.2k
}
745
746
747
/*
748
 * Finish up at the end of the file.
749
 */
750
751
METHODDEF(void)
752
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
753
93.4k
{
754
  /* no work */
755
93.4k
}
756
757
758
/*
759
 * The module selection routine for PPM format input.
760
 */
761
762
GLOBAL(cjpeg_source_ptr)
763
jinit_read_ppm(j_compress_ptr cinfo)
764
61.8k
{
765
61.8k
  ppm_source_ptr source;
766
767
  /* Create module interface object */
768
61.8k
  source = (ppm_source_ptr)
769
61.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
770
61.8k
                                sizeof(ppm_source_struct));
771
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
772
61.8k
  source->pub.start_input = start_input_ppm;
773
61.8k
  source->pub.finish_input = finish_input_ppm;
774
61.8k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
775
61.8k
  source->pub.max_pixels = 0;
776
61.8k
#endif
777
778
61.8k
  return (cjpeg_source_ptr)source;
779
61.8k
}
780
781
#endif /* PPM_SUPPORTED */