Coverage Report

Created: 2024-08-17 06:42

/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
#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
0
#define UCH(x)  ((int)(x))
48
49
50
#define ReadOK(file, buffer, len) \
51
0
  (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
0
{
79
0
  register int ch;
80
81
0
  ch = getc(infile);
82
0
  if (ch == '#') {
83
0
    do {
84
0
      ch = getc(infile);
85
0
    } while (ch != '\n' && ch != EOF);
86
0
  }
87
0
  return ch;
88
0
}
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
0
{
98
0
  register int ch;
99
0
  register unsigned int val;
100
101
  /* Skip any leading whitespace */
102
0
  do {
103
0
    ch = pbm_getc(infile);
104
0
    if (ch == EOF)
105
0
      ERREXIT(cinfo, JERR_INPUT_EOF);
106
0
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
107
108
0
  if (ch < '0' || ch > '9')
109
0
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
110
111
0
  val = ch - '0';
112
0
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
113
0
    val *= 10;
114
0
    val += ch - '0';
115
0
    if (val > maxval)
116
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
117
0
  }
118
119
0
  return val;
120
0
}
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
0
{
138
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
139
0
  FILE *infile = source->pub.input_file;
140
0
  register JSAMPROW ptr;
141
0
  register JSAMPLE *rescale = source->rescale;
142
0
  JDIMENSION col;
143
0
  unsigned int maxval = source->maxval;
144
145
0
  ptr = source->pub.buffer[0];
146
0
  for (col = cinfo->image_width; col > 0; col--) {
147
0
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
148
0
  }
149
0
  return 1;
150
0
}
151
152
153
0
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
154
0
  for (col = cinfo->image_width; col > 0; col--) { \
155
0
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
156
0
    alpha_set_op \
157
0
    ptr += ps; \
158
0
  } \
159
0
}
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
0
{
166
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
167
0
  FILE *infile = source->pub.input_file;
168
0
  register JSAMPROW ptr;
169
0
  register JSAMPLE *rescale = source->rescale;
170
0
  JDIMENSION col;
171
0
  unsigned int maxval = source->maxval;
172
0
  register int rindex = rgb_red[cinfo->in_color_space];
173
0
  register int gindex = rgb_green[cinfo->in_color_space];
174
0
  register int bindex = rgb_blue[cinfo->in_color_space];
175
0
  register int aindex = alpha_index[cinfo->in_color_space];
176
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
177
178
0
  ptr = source->pub.buffer[0];
179
0
  if (maxval == MAXJSAMPLE) {
180
0
    if (aindex >= 0)
181
0
      GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
182
0
                         ptr[aindex] = MAXJSAMPLE;)
183
0
    else
184
0
      GRAY_RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
185
0
  } else {
186
0
    if (aindex >= 0)
187
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
188
0
                         ptr[aindex] = MAXJSAMPLE;)
189
0
    else
190
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
191
0
  }
192
0
  return 1;
193
0
}
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
0
{
201
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
202
0
  FILE *infile = source->pub.input_file;
203
0
  register JSAMPROW ptr;
204
0
  register JSAMPLE *rescale = source->rescale;
205
0
  JDIMENSION col;
206
0
  unsigned int maxval = source->maxval;
207
208
0
  ptr = source->pub.buffer[0];
209
0
  if (maxval == MAXJSAMPLE) {
210
0
    for (col = cinfo->image_width; col > 0; col--) {
211
0
      JSAMPLE gray = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
212
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
213
0
      ptr += 4;
214
0
    }
215
0
  } else {
216
0
    for (col = cinfo->image_width; col > 0; col--) {
217
0
      JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
218
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
219
0
      ptr += 4;
220
0
    }
221
0
  }
222
0
  return 1;
223
0
}
224
225
226
0
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
227
0
  for (col = cinfo->image_width; col > 0; col--) { \
228
0
    ptr[rindex] = read_op; \
229
0
    ptr[gindex] = read_op; \
230
0
    ptr[bindex] = read_op; \
231
0
    alpha_set_op \
232
0
    ptr += ps; \
233
0
  } \
234
0
}
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
0
{
240
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
241
0
  FILE *infile = source->pub.input_file;
242
0
  register JSAMPROW ptr;
243
0
  register JSAMPLE *rescale = source->rescale;
244
0
  JDIMENSION col;
245
0
  unsigned int maxval = source->maxval;
246
0
  register int rindex = rgb_red[cinfo->in_color_space];
247
0
  register int gindex = rgb_green[cinfo->in_color_space];
248
0
  register int bindex = rgb_blue[cinfo->in_color_space];
249
0
  register int aindex = alpha_index[cinfo->in_color_space];
250
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
251
252
0
  ptr = source->pub.buffer[0];
253
0
  if (maxval == MAXJSAMPLE) {
254
0
    if (aindex >= 0)
255
0
      RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
256
0
                    ptr[aindex] = MAXJSAMPLE;)
257
0
    else
258
0
      RGB_READ_LOOP((JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
259
0
  } else {
260
0
    if (aindex >= 0)
261
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
262
0
                    ptr[aindex] = MAXJSAMPLE;)
263
0
    else
264
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
265
0
  }
266
0
  return 1;
267
0
}
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
0
{
275
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
276
0
  FILE *infile = source->pub.input_file;
277
0
  register JSAMPROW ptr;
278
0
  register JSAMPLE *rescale = source->rescale;
279
0
  JDIMENSION col;
280
0
  unsigned int maxval = source->maxval;
281
282
0
  ptr = source->pub.buffer[0];
283
0
  if (maxval == MAXJSAMPLE) {
284
0
    for (col = cinfo->image_width; col > 0; col--) {
285
0
      JSAMPLE r = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
286
0
      JSAMPLE g = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
287
0
      JSAMPLE b = (JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
289
0
      ptr += 4;
290
0
    }
291
0
  } else {
292
0
    for (col = cinfo->image_width; col > 0; col--) {
293
0
      JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
294
0
      JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
295
0
      JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
297
0
      ptr += 4;
298
0
    }
299
0
  }
300
0
  return 1;
301
0
}
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
0
{
308
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
309
0
  register JSAMPROW ptr;
310
0
  register U_CHAR *bufferptr;
311
0
  register JSAMPLE *rescale = source->rescale;
312
0
  JDIMENSION col;
313
314
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
315
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
316
0
  ptr = source->pub.buffer[0];
317
0
  bufferptr = source->iobuffer;
318
0
  for (col = cinfo->image_width; col > 0; col--) {
319
0
    *ptr++ = rescale[UCH(*bufferptr++)];
320
0
  }
321
0
  return 1;
322
0
}
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
0
{
330
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
331
0
  register JSAMPROW ptr;
332
0
  register U_CHAR *bufferptr;
333
0
  register JSAMPLE *rescale = source->rescale;
334
0
  JDIMENSION col;
335
0
  unsigned int maxval = source->maxval;
336
0
  register int rindex = rgb_red[cinfo->in_color_space];
337
0
  register int gindex = rgb_green[cinfo->in_color_space];
338
0
  register int bindex = rgb_blue[cinfo->in_color_space];
339
0
  register int aindex = alpha_index[cinfo->in_color_space];
340
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
341
342
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
343
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
344
0
  ptr = source->pub.buffer[0];
345
0
  bufferptr = source->iobuffer;
346
0
  if (maxval == MAXJSAMPLE) {
347
0
    if (aindex >= 0)
348
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;)
349
0
    else
350
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
351
0
  } else {
352
0
    if (aindex >= 0)
353
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;)
354
0
    else
355
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
356
0
  }
357
0
  return 1;
358
0
}
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
0
{
366
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
367
0
  register JSAMPROW ptr;
368
0
  register U_CHAR *bufferptr;
369
0
  register JSAMPLE *rescale = source->rescale;
370
0
  JDIMENSION col;
371
0
  unsigned int maxval = source->maxval;
372
373
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
374
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
375
0
  ptr = source->pub.buffer[0];
376
0
  bufferptr = source->iobuffer;
377
0
  if (maxval == MAXJSAMPLE) {
378
0
    for (col = cinfo->image_width; col > 0; col--) {
379
0
      JSAMPLE gray = *bufferptr++;
380
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
381
0
      ptr += 4;
382
0
    }
383
0
  } else {
384
0
    for (col = cinfo->image_width; col > 0; col--) {
385
0
      JSAMPLE gray = rescale[UCH(*bufferptr++)];
386
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
387
0
      ptr += 4;
388
0
    }
389
0
  }
390
0
  return 1;
391
0
}
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
0
{
398
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
399
0
  register JSAMPROW ptr;
400
0
  register U_CHAR *bufferptr;
401
0
  register JSAMPLE *rescale = source->rescale;
402
0
  JDIMENSION col;
403
0
  unsigned int maxval = source->maxval;
404
0
  register int rindex = rgb_red[cinfo->in_color_space];
405
0
  register int gindex = rgb_green[cinfo->in_color_space];
406
0
  register int bindex = rgb_blue[cinfo->in_color_space];
407
0
  register int aindex = alpha_index[cinfo->in_color_space];
408
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
409
410
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
411
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
412
0
  ptr = source->pub.buffer[0];
413
0
  bufferptr = source->iobuffer;
414
0
  if (maxval == MAXJSAMPLE) {
415
0
    if (aindex >= 0)
416
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = MAXJSAMPLE;)
417
0
    else
418
0
      RGB_READ_LOOP(*bufferptr++, {})
419
0
  } else {
420
0
    if (aindex >= 0)
421
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = MAXJSAMPLE;)
422
0
    else
423
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
424
0
  }
425
0
  return 1;
426
0
}
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
0
{
434
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
435
0
  register JSAMPROW ptr;
436
0
  register U_CHAR *bufferptr;
437
0
  register JSAMPLE *rescale = source->rescale;
438
0
  JDIMENSION col;
439
0
  unsigned int maxval = source->maxval;
440
441
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
442
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
443
0
  ptr = source->pub.buffer[0];
444
0
  bufferptr = source->iobuffer;
445
0
  if (maxval == MAXJSAMPLE) {
446
0
    for (col = cinfo->image_width; col > 0; col--) {
447
0
      JSAMPLE r = *bufferptr++;
448
0
      JSAMPLE g = *bufferptr++;
449
0
      JSAMPLE b = *bufferptr++;
450
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
451
0
      ptr += 4;
452
0
    }
453
0
  } else {
454
0
    for (col = cinfo->image_width; col > 0; col--) {
455
0
      JSAMPLE r = rescale[UCH(*bufferptr++)];
456
0
      JSAMPLE g = rescale[UCH(*bufferptr++)];
457
0
      JSAMPLE b = rescale[UCH(*bufferptr++)];
458
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
459
0
      ptr += 4;
460
0
    }
461
0
  }
462
0
  return 1;
463
0
}
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
0
{
473
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
474
475
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
476
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
477
0
  return 1;
478
0
}
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
0
{
485
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
486
0
  register JSAMPROW ptr;
487
0
  register U_CHAR *bufferptr;
488
0
  register JSAMPLE *rescale = source->rescale;
489
0
  JDIMENSION col;
490
0
  unsigned int maxval = source->maxval;
491
492
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
493
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
494
0
  ptr = source->pub.buffer[0];
495
0
  bufferptr = source->iobuffer;
496
0
  for (col = cinfo->image_width; col > 0; col--) {
497
0
    register unsigned int temp;
498
0
    temp  = UCH(*bufferptr++) << 8;
499
0
    temp |= UCH(*bufferptr++);
500
0
    if (temp > maxval)
501
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
502
0
    *ptr++ = rescale[temp];
503
0
  }
504
0
  return 1;
505
0
}
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
0
{
512
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
513
0
  register JSAMPROW ptr;
514
0
  register U_CHAR *bufferptr;
515
0
  register JSAMPLE *rescale = source->rescale;
516
0
  JDIMENSION col;
517
0
  unsigned int maxval = source->maxval;
518
0
  register int rindex = rgb_red[cinfo->in_color_space];
519
0
  register int gindex = rgb_green[cinfo->in_color_space];
520
0
  register int bindex = rgb_blue[cinfo->in_color_space];
521
0
  register int aindex = alpha_index[cinfo->in_color_space];
522
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
523
524
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
525
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
526
0
  ptr = source->pub.buffer[0];
527
0
  bufferptr = source->iobuffer;
528
0
  for (col = cinfo->image_width; col > 0; col--) {
529
0
    register unsigned int temp;
530
0
    temp  = UCH(*bufferptr++) << 8;
531
0
    temp |= UCH(*bufferptr++);
532
0
    if (temp > maxval)
533
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
534
0
    ptr[rindex] = rescale[temp];
535
0
    temp  = UCH(*bufferptr++) << 8;
536
0
    temp |= UCH(*bufferptr++);
537
0
    if (temp > maxval)
538
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
0
    ptr[gindex] = rescale[temp];
540
0
    temp  = UCH(*bufferptr++) << 8;
541
0
    temp |= UCH(*bufferptr++);
542
0
    if (temp > maxval)
543
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
544
0
    ptr[bindex] = rescale[temp];
545
0
    if (aindex >= 0)
546
0
      ptr[aindex] = MAXJSAMPLE;
547
0
    ptr += ps;
548
0
  }
549
0
  return 1;
550
0
}
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
0
{
560
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
561
0
  int c;
562
0
  unsigned int w, h, maxval;
563
0
  boolean need_iobuffer, use_raw_buffer, need_rescale;
564
565
0
  if (getc(source->pub.input_file) != 'P')
566
0
    ERREXIT(cinfo, JERR_PPM_NOT);
567
568
0
  c = getc(source->pub.input_file); /* subformat discriminator character */
569
570
  /* detect unsupported variants (ie, PBM) before trying to read header */
571
0
  switch (c) {
572
0
  case '2':                     /* it's a text-format PGM file */
573
0
  case '3':                     /* it's a text-format PPM file */
574
0
  case '5':                     /* it's a raw-format PGM file */
575
0
  case '6':                     /* it's a raw-format PPM file */
576
0
    break;
577
0
  default:
578
0
    ERREXIT(cinfo, JERR_PPM_NOT);
579
0
    break;
580
0
  }
581
582
  /* fetch the remaining header info */
583
0
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
584
0
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
585
0
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
586
587
0
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
588
0
    ERREXIT(cinfo, JERR_PPM_NOT);
589
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
590
0
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
591
0
    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
592
0
#endif
593
594
0
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
595
0
  cinfo->image_width = (JDIMENSION)w;
596
0
  cinfo->image_height = (JDIMENSION)h;
597
0
  source->maxval = maxval;
598
599
  /* initialize flags to most common settings */
600
0
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
601
0
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
602
0
  need_rescale = TRUE;          /* do we need a rescale array? */
603
604
0
  switch (c) {
605
0
  case '2':                     /* it's a text-format PGM file */
606
0
    if (cinfo->in_color_space == JCS_UNKNOWN ||
607
0
        cinfo->in_color_space == JCS_RGB)
608
0
      cinfo->in_color_space = JCS_GRAYSCALE;
609
0
    TRACEMS2(cinfo, 1, JTRC_PGM_TEXT, w, h);
610
0
    if (cinfo->in_color_space == JCS_GRAYSCALE)
611
0
      source->pub.get_pixel_rows = get_text_gray_row;
612
0
    else if (IsExtRGB(cinfo->in_color_space))
613
0
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
614
0
    else if (cinfo->in_color_space == JCS_CMYK)
615
0
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
616
0
    else
617
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
618
0
    need_iobuffer = FALSE;
619
0
    break;
620
621
0
  case '3':                     /* it's a text-format PPM file */
622
0
    if (cinfo->in_color_space == JCS_UNKNOWN)
623
0
      cinfo->in_color_space = JCS_EXT_RGB;
624
0
    TRACEMS2(cinfo, 1, JTRC_PPM_TEXT, w, h);
625
0
    if (IsExtRGB(cinfo->in_color_space))
626
0
      source->pub.get_pixel_rows = get_text_rgb_row;
627
0
    else if (cinfo->in_color_space == JCS_CMYK)
628
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
629
0
    else
630
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
631
0
    need_iobuffer = FALSE;
632
0
    break;
633
634
0
  case '5':                     /* it's a raw-format PGM file */
635
0
    if (cinfo->in_color_space == JCS_UNKNOWN ||
636
0
        cinfo->in_color_space == JCS_RGB)
637
0
      cinfo->in_color_space = JCS_GRAYSCALE;
638
0
    TRACEMS2(cinfo, 1, JTRC_PGM, w, h);
639
0
    if (maxval > 255) {
640
0
      if (cinfo->in_color_space == JCS_GRAYSCALE)
641
0
        source->pub.get_pixel_rows = get_word_gray_row;
642
0
      else
643
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
644
0
    } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
645
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
646
0
      source->pub.get_pixel_rows = get_raw_row;
647
0
      use_raw_buffer = TRUE;
648
0
      need_rescale = FALSE;
649
0
    } else {
650
0
      if (cinfo->in_color_space == JCS_GRAYSCALE)
651
0
        source->pub.get_pixel_rows = get_scaled_gray_row;
652
0
      else if (IsExtRGB(cinfo->in_color_space))
653
0
        source->pub.get_pixel_rows = get_gray_rgb_row;
654
0
      else if (cinfo->in_color_space == JCS_CMYK)
655
0
        source->pub.get_pixel_rows = get_gray_cmyk_row;
656
0
      else
657
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
658
0
    }
659
0
    break;
660
661
0
  case '6':                     /* it's a raw-format PPM file */
662
0
    if (cinfo->in_color_space == JCS_UNKNOWN)
663
0
      cinfo->in_color_space = JCS_EXT_RGB;
664
0
    TRACEMS2(cinfo, 1, JTRC_PPM, w, h);
665
0
    if (maxval > 255) {
666
0
      if (IsExtRGB(cinfo->in_color_space))
667
0
        source->pub.get_pixel_rows = get_word_rgb_row;
668
0
      else
669
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
670
0
    } else if (maxval == MAXJSAMPLE && sizeof(JSAMPLE) == sizeof(U_CHAR) &&
671
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
672
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
673
0
                cinfo->in_color_space == JCS_RGB)) {
674
#else
675
               cinfo->in_color_space == JCS_EXT_RGB) {
676
#endif
677
0
      source->pub.get_pixel_rows = get_raw_row;
678
0
      use_raw_buffer = TRUE;
679
0
      need_rescale = FALSE;
680
0
    } else {
681
0
      if (IsExtRGB(cinfo->in_color_space))
682
0
        source->pub.get_pixel_rows = get_rgb_row;
683
0
      else if (cinfo->in_color_space == JCS_CMYK)
684
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
685
0
      else
686
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
687
0
    }
688
0
    break;
689
0
  }
690
691
0
  if (IsExtRGB(cinfo->in_color_space))
692
0
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
693
0
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
694
0
    cinfo->input_components = 1;
695
0
  else if (cinfo->in_color_space == JCS_CMYK)
696
0
    cinfo->input_components = 4;
697
698
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
699
0
  if (need_iobuffer) {
700
0
    if (c == '6')
701
0
      source->buffer_width = (size_t)w * 3 *
702
0
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
703
0
    else
704
0
      source->buffer_width = (size_t)w *
705
0
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
706
0
    source->iobuffer = (U_CHAR *)
707
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
708
0
                                  source->buffer_width);
709
0
  }
710
711
  /* Create compressor input buffer. */
712
0
  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
0
    source->pixrow = (JSAMPROW)source->iobuffer;
716
0
    source->pub.buffer = &source->pixrow;
717
0
    source->pub.buffer_height = 1;
718
0
  } else {
719
    /* Need to translate anyway, so make a separate sample buffer. */
720
0
    source->pub.buffer = (*cinfo->mem->alloc_sarray)
721
0
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
722
0
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
723
0
    source->pub.buffer_height = 1;
724
0
  }
725
726
  /* Compute the rescaling array if required. */
727
0
  if (need_rescale) {
728
0
    long val, half_maxval;
729
730
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
731
0
    source->rescale = (JSAMPLE *)
732
0
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
733
0
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
734
0
                                           sizeof(JSAMPLE)));
735
0
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
736
0
                                        sizeof(JSAMPLE)));
737
0
    half_maxval = maxval / 2;
738
0
    for (val = 0; val <= (long)maxval; val++) {
739
      /* The multiplication here must be done in 32 bits to avoid overflow */
740
0
      source->rescale[val] = (JSAMPLE)((val * MAXJSAMPLE + half_maxval) /
741
0
                                        maxval);
742
0
    }
743
0
  }
744
0
}
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
0
{
754
  /* no work */
755
0
}
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
0
{
765
0
  ppm_source_ptr source;
766
767
  /* Create module interface object */
768
0
  source = (ppm_source_ptr)
769
0
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
770
0
                                sizeof(ppm_source_struct));
771
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
772
0
  source->pub.start_input = start_input_ppm;
773
0
  source->pub.finish_input = finish_input_ppm;
774
0
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
775
0
  source->pub.max_pixels = 0;
776
0
#endif
777
778
0
  return (cjpeg_source_ptr)source;
779
0
}
780
781
#endif /* PPM_SUPPORTED */