Coverage Report

Created: 2025-07-11 07:03

/src/libjpeg-turbo.3.0.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-2024, 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
40.0M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
52.0M
  (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
353k
{
80
353k
  register int ch;
81
82
353k
  ch = getc(infile);
83
353k
  if (ch == '#') {
84
80.0k
    do {
85
80.0k
      ch = getc(infile);
86
80.0k
    } while (ch != '\n' && ch != EOF);
87
4.72k
  }
88
353k
  return ch;
89
353k
}
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
144k
{
99
144k
  register int ch;
100
144k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
153k
  do {
104
153k
    ch = pbm_getc(infile);
105
153k
    if (ch == EOF)
106
2.62k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
153k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
144k
  if (ch < '0' || ch > '9')
110
236
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
144k
  val = ch - '0';
113
203k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
58.3k
    val *= 10;
115
58.3k
    val += ch - '0';
116
58.3k
    if (val > maxval)
117
119
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
58.3k
  }
119
120
144k
  return val;
121
144k
}
122
123
124
/*
125
 * Read one row of pixels.
126
 *
127
 * We provide several different versions depending on input file format.
128
 * In all cases, input is scaled to the size of _JSAMPLE.
129
 *
130
 * A really fast path is provided for reading byte/sample raw files with
131
 * maxval = _MAXJSAMPLE, which is the normal case for 8-bit data.
132
 */
133
134
135
METHODDEF(JDIMENSION)
136
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
137
/* This version is for reading text-format PGM files with any maxval */
138
2.76k
{
139
2.76k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
140
2.76k
  FILE *infile = source->pub.input_file;
141
2.76k
  register _JSAMPROW ptr;
142
2.76k
  register _JSAMPLE *rescale = source->rescale;
143
2.76k
  JDIMENSION col;
144
2.76k
  unsigned int maxval = source->maxval;
145
146
2.76k
  ptr = source->pub._buffer[0];
147
6.20k
  for (col = cinfo->image_width; col > 0; col--) {
148
3.43k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
149
3.43k
  }
150
2.76k
  return 1;
151
2.76k
}
152
153
154
36.4M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
155
124M
  for (col = cinfo->image_width; col > 0; col--) { \
156
88.0M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
157
88.0M
    alpha_set_op \
158
88.0M
    ptr += ps; \
159
88.0M
  } \
160
36.4M
}
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
13.8k
{
167
13.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
168
13.8k
  FILE *infile = source->pub.input_file;
169
13.8k
  register _JSAMPROW ptr;
170
13.8k
  register _JSAMPLE *rescale = source->rescale;
171
13.8k
  JDIMENSION col;
172
13.8k
  unsigned int maxval = source->maxval;
173
13.8k
  register int rindex = rgb_red[cinfo->in_color_space];
174
13.8k
  register int gindex = rgb_green[cinfo->in_color_space];
175
13.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
176
13.8k
  register int aindex = alpha_index[cinfo->in_color_space];
177
13.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
178
179
13.8k
  ptr = source->pub._buffer[0];
180
13.8k
  if (maxval == _MAXJSAMPLE) {
181
2.80k
    if (aindex >= 0)
182
561
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
183
2.80k
                         ptr[aindex] = _MAXJSAMPLE;)
184
2.24k
    else
185
2.24k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
186
11.0k
  } else {
187
11.0k
    if (aindex >= 0)
188
2.20k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
189
11.0k
                         ptr[aindex] = _MAXJSAMPLE;)
190
8.80k
    else
191
8.80k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
192
11.0k
  }
193
13.8k
  return 1;
194
13.8k
}
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
2.76k
{
202
2.76k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
203
2.76k
  FILE *infile = source->pub.input_file;
204
2.76k
  register _JSAMPROW ptr;
205
2.76k
  register _JSAMPLE *rescale = source->rescale;
206
2.76k
  JDIMENSION col;
207
2.76k
  unsigned int maxval = source->maxval;
208
209
2.76k
  ptr = source->pub._buffer[0];
210
2.76k
  if (maxval == _MAXJSAMPLE) {
211
1.32k
    for (col = cinfo->image_width; col > 0; col--) {
212
762
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
213
762
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
214
762
      ptr += 4;
215
762
    }
216
2.20k
  } else {
217
4.87k
    for (col = cinfo->image_width; col > 0; col--) {
218
2.67k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
219
2.67k
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
220
2.67k
      ptr += 4;
221
2.67k
    }
222
2.20k
  }
223
2.76k
  return 1;
224
2.76k
}
225
226
227
285k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
228
4.22M
  for (col = cinfo->image_width; col > 0; col--) { \
229
3.94M
    ptr[rindex] = read_op; \
230
3.94M
    ptr[gindex] = read_op; \
231
3.94M
    ptr[bindex] = read_op; \
232
3.94M
    alpha_set_op \
233
3.94M
    ptr += ps; \
234
3.94M
  } \
235
285k
}
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.39k
{
241
9.39k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
242
9.39k
  FILE *infile = source->pub.input_file;
243
9.39k
  register _JSAMPROW ptr;
244
9.39k
  register _JSAMPLE *rescale = source->rescale;
245
9.39k
  JDIMENSION col;
246
9.39k
  unsigned int maxval = source->maxval;
247
9.39k
  register int rindex = rgb_red[cinfo->in_color_space];
248
9.39k
  register int gindex = rgb_green[cinfo->in_color_space];
249
9.39k
  register int bindex = rgb_blue[cinfo->in_color_space];
250
9.39k
  register int aindex = alpha_index[cinfo->in_color_space];
251
9.39k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
252
253
9.39k
  ptr = source->pub._buffer[0];
254
9.39k
  if (maxval == _MAXJSAMPLE) {
255
2.09k
    if (aindex >= 0)
256
418
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
257
2.09k
                    ptr[aindex] = _MAXJSAMPLE;)
258
1.67k
    else
259
1.67k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
260
7.30k
  } else {
261
7.30k
    if (aindex >= 0)
262
1.46k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
263
7.30k
                    ptr[aindex] = _MAXJSAMPLE;)
264
5.84k
    else
265
5.84k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
266
7.30k
  }
267
9.39k
  return 1;
268
9.39k
}
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.87k
{
276
1.87k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
277
1.87k
  FILE *infile = source->pub.input_file;
278
1.87k
  register _JSAMPROW ptr;
279
1.87k
  register _JSAMPLE *rescale = source->rescale;
280
1.87k
  JDIMENSION col;
281
1.87k
  unsigned int maxval = source->maxval;
282
283
1.87k
  ptr = source->pub._buffer[0];
284
1.87k
  if (maxval == _MAXJSAMPLE) {
285
1.86k
    for (col = cinfo->image_width; col > 0; col--) {
286
1.45k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
287
1.45k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.45k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.45k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
290
1.45k
      ptr += 4;
291
1.45k
    }
292
1.46k
  } else {
293
3.62k
    for (col = cinfo->image_width; col > 0; col--) {
294
2.16k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
295
2.16k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.16k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.16k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
298
2.16k
      ptr += 4;
299
2.16k
    }
300
1.46k
  }
301
1.87k
  return 1;
302
1.87k
}
303
304
305
METHODDEF(JDIMENSION)
306
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
307
/* This version is for reading raw-byte-format PGM files with any maxval */
308
7.28M
{
309
7.28M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
310
7.28M
  register _JSAMPROW ptr;
311
7.28M
  register U_CHAR *bufferptr;
312
7.28M
  register _JSAMPLE *rescale = source->rescale;
313
7.28M
  JDIMENSION col;
314
315
7.28M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
316
63
    ERREXIT(cinfo, JERR_INPUT_EOF);
317
7.28M
  ptr = source->pub._buffer[0];
318
7.28M
  bufferptr = source->iobuffer;
319
24.8M
  for (col = cinfo->image_width; col > 0; col--) {
320
17.5M
    *ptr++ = rescale[UCH(*bufferptr++)];
321
17.5M
  }
322
7.28M
  return 1;
323
7.28M
}
324
325
326
METHODDEF(JDIMENSION)
327
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
328
/* This version is for reading raw-byte-format PGM files with any maxval
329
   and converting to extended RGB */
330
36.4M
{
331
36.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
332
36.4M
  register _JSAMPROW ptr;
333
36.4M
  register U_CHAR *bufferptr;
334
36.4M
  register _JSAMPLE *rescale = source->rescale;
335
36.4M
  JDIMENSION col;
336
36.4M
  unsigned int maxval = source->maxval;
337
36.4M
  register int rindex = rgb_red[cinfo->in_color_space];
338
36.4M
  register int gindex = rgb_green[cinfo->in_color_space];
339
36.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
340
36.4M
  register int aindex = alpha_index[cinfo->in_color_space];
341
36.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
342
343
36.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
344
315
    ERREXIT(cinfo, JERR_INPUT_EOF);
345
36.4M
  ptr = source->pub._buffer[0];
346
36.4M
  bufferptr = source->iobuffer;
347
36.4M
  if (maxval == _MAXJSAMPLE) {
348
0
    if (aindex >= 0)
349
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
350
0
    else
351
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
352
36.4M
  } else {
353
36.4M
    if (aindex >= 0)
354
7.28M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
355
36.4M
                         ptr[aindex] = _MAXJSAMPLE;)
356
29.1M
    else
357
29.1M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
358
36.4M
  }
359
36.4M
  return 1;
360
36.4M
}
361
362
363
METHODDEF(JDIMENSION)
364
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
365
/* This version is for reading raw-byte-format PGM files with any maxval
366
   and converting to CMYK */
367
7.28M
{
368
7.28M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
369
7.28M
  register _JSAMPROW ptr;
370
7.28M
  register U_CHAR *bufferptr;
371
7.28M
  register _JSAMPLE *rescale = source->rescale;
372
7.28M
  JDIMENSION col;
373
7.28M
  unsigned int maxval = source->maxval;
374
375
7.28M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
376
63
    ERREXIT(cinfo, JERR_INPUT_EOF);
377
7.28M
  ptr = source->pub._buffer[0];
378
7.28M
  bufferptr = source->iobuffer;
379
7.28M
  if (maxval == _MAXJSAMPLE) {
380
0
    for (col = cinfo->image_width; col > 0; col--) {
381
0
      _JSAMPLE gray = *bufferptr++;
382
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
383
0
      ptr += 4;
384
0
    }
385
7.28M
  } else {
386
24.8M
    for (col = cinfo->image_width; col > 0; col--) {
387
17.5M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
388
17.5M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
389
17.5M
      ptr += 4;
390
17.5M
    }
391
7.28M
  }
392
7.28M
  return 1;
393
7.28M
}
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
275k
{
400
275k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
401
275k
  register _JSAMPROW ptr;
402
275k
  register U_CHAR *bufferptr;
403
275k
  register _JSAMPLE *rescale = source->rescale;
404
275k
  JDIMENSION col;
405
275k
  unsigned int maxval = source->maxval;
406
275k
  register int rindex = rgb_red[cinfo->in_color_space];
407
275k
  register int gindex = rgb_green[cinfo->in_color_space];
408
275k
  register int bindex = rgb_blue[cinfo->in_color_space];
409
275k
  register int aindex = alpha_index[cinfo->in_color_space];
410
275k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
411
412
275k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
413
455
    ERREXIT(cinfo, JERR_INPUT_EOF);
414
275k
  ptr = source->pub._buffer[0];
415
275k
  bufferptr = source->iobuffer;
416
275k
  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
275k
  } else {
422
275k
    if (aindex >= 0)
423
55.0k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = _MAXJSAMPLE;)
424
220k
    else
425
220k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
426
275k
  }
427
275k
  return 1;
428
275k
}
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
55.1k
{
436
55.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
437
55.1k
  register _JSAMPROW ptr;
438
55.1k
  register U_CHAR *bufferptr;
439
55.1k
  register _JSAMPLE *rescale = source->rescale;
440
55.1k
  JDIMENSION col;
441
55.1k
  unsigned int maxval = source->maxval;
442
443
55.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
444
91
    ERREXIT(cinfo, JERR_INPUT_EOF);
445
55.1k
  ptr = source->pub._buffer[0];
446
55.1k
  bufferptr = source->iobuffer;
447
55.1k
  if (maxval == _MAXJSAMPLE) {
448
0
    for (col = cinfo->image_width; col > 0; col--) {
449
0
      _JSAMPLE r = *bufferptr++;
450
0
      _JSAMPLE g = *bufferptr++;
451
0
      _JSAMPLE b = *bufferptr++;
452
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
453
0
      ptr += 4;
454
0
    }
455
55.1k
  } else {
456
839k
    for (col = cinfo->image_width; col > 0; col--) {
457
784k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
458
784k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
459
784k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
460
784k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
461
784k
      ptr += 4;
462
784k
    }
463
55.1k
  }
464
55.1k
  return 1;
465
55.1k
}
466
467
468
METHODDEF(JDIMENSION)
469
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
470
/* This version is for reading raw-byte-format files with maxval = _MAXJSAMPLE.
471
 * In this case we just read right into the _JSAMPLE buffer!
472
 * Note that same code works for PPM and PGM files.
473
 */
474
0
{
475
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
476
477
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
478
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
479
0
  return 1;
480
0
}
481
482
483
METHODDEF(JDIMENSION)
484
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
485
/* This version is for reading raw-word-format PGM files with any maxval */
486
100k
{
487
100k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
488
100k
  register _JSAMPROW ptr;
489
100k
  register U_CHAR *bufferptr;
490
100k
  register _JSAMPLE *rescale = source->rescale;
491
100k
  JDIMENSION col;
492
100k
  unsigned int maxval = source->maxval;
493
494
100k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
495
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
496
100k
  ptr = source->pub._buffer[0];
497
100k
  bufferptr = source->iobuffer;
498
266k
  for (col = cinfo->image_width; col > 0; col--) {
499
166k
    register unsigned int temp;
500
166k
    temp  = UCH(*bufferptr++) << 8;
501
166k
    temp |= UCH(*bufferptr++);
502
166k
    if (temp > maxval)
503
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
504
166k
    *ptr++ = rescale[temp];
505
166k
  }
506
100k
  return 1;
507
100k
}
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
500k
{
514
500k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
515
500k
  register _JSAMPROW ptr;
516
500k
  register U_CHAR *bufferptr;
517
500k
  register _JSAMPLE *rescale = source->rescale;
518
500k
  JDIMENSION col;
519
500k
  unsigned int maxval = source->maxval;
520
500k
  register int rindex = rgb_red[cinfo->in_color_space];
521
500k
  register int gindex = rgb_green[cinfo->in_color_space];
522
500k
  register int bindex = rgb_blue[cinfo->in_color_space];
523
500k
  register int aindex = alpha_index[cinfo->in_color_space];
524
500k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
525
526
500k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
527
285
    ERREXIT(cinfo, JERR_INPUT_EOF);
528
500k
  ptr = source->pub._buffer[0];
529
500k
  bufferptr = source->iobuffer;
530
1.33M
  for (col = cinfo->image_width; col > 0; col--) {
531
831k
    register unsigned int temp;
532
831k
    temp  = UCH(*bufferptr++) << 8;
533
831k
    temp |= UCH(*bufferptr++);
534
831k
    if (temp > maxval)
535
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
536
831k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
537
831k
    if (aindex >= 0)
538
166k
      ptr[aindex] = _MAXJSAMPLE;
539
831k
    ptr += ps;
540
831k
  }
541
500k
  return 1;
542
500k
}
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
100k
{
549
100k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
550
100k
  register _JSAMPROW ptr;
551
100k
  register U_CHAR *bufferptr;
552
100k
  register _JSAMPLE *rescale = source->rescale;
553
100k
  JDIMENSION col;
554
100k
  unsigned int maxval = source->maxval;
555
556
100k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
557
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
558
100k
  ptr = source->pub._buffer[0];
559
100k
  bufferptr = source->iobuffer;
560
266k
  for (col = cinfo->image_width; col > 0; col--) {
561
166k
    register unsigned int gray;
562
166k
    gray  = UCH(*bufferptr++) << 8;
563
166k
    gray |= UCH(*bufferptr++);
564
166k
    if (gray > maxval)
565
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
566
166k
    rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1,
567
166k
                ptr + 2, ptr + 3);
568
166k
    ptr += 4;
569
166k
  }
570
100k
  return 1;
571
100k
}
572
573
574
METHODDEF(JDIMENSION)
575
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
576
/* This version is for reading raw-word-format PPM files with any maxval */
577
12.0k
{
578
12.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
579
12.0k
  register _JSAMPROW ptr;
580
12.0k
  register U_CHAR *bufferptr;
581
12.0k
  register _JSAMPLE *rescale = source->rescale;
582
12.0k
  JDIMENSION col;
583
12.0k
  unsigned int maxval = source->maxval;
584
12.0k
  register int rindex = rgb_red[cinfo->in_color_space];
585
12.0k
  register int gindex = rgb_green[cinfo->in_color_space];
586
12.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
587
12.0k
  register int aindex = alpha_index[cinfo->in_color_space];
588
12.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
589
590
12.0k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
591
395
    ERREXIT(cinfo, JERR_INPUT_EOF);
592
12.0k
  ptr = source->pub._buffer[0];
593
12.0k
  bufferptr = source->iobuffer;
594
29.5k
  for (col = cinfo->image_width; col > 0; col--) {
595
17.5k
    register unsigned int temp;
596
17.5k
    temp  = UCH(*bufferptr++) << 8;
597
17.5k
    temp |= UCH(*bufferptr++);
598
17.5k
    if (temp > maxval)
599
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
600
17.5k
    ptr[rindex] = rescale[temp];
601
17.5k
    temp  = UCH(*bufferptr++) << 8;
602
17.5k
    temp |= UCH(*bufferptr++);
603
17.5k
    if (temp > maxval)
604
95
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
605
17.5k
    ptr[gindex] = rescale[temp];
606
17.5k
    temp  = UCH(*bufferptr++) << 8;
607
17.5k
    temp |= UCH(*bufferptr++);
608
17.5k
    if (temp > maxval)
609
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
610
17.5k
    ptr[bindex] = rescale[temp];
611
17.5k
    if (aindex >= 0)
612
3.42k
      ptr[aindex] = _MAXJSAMPLE;
613
17.5k
    ptr += ps;
614
17.5k
  }
615
12.0k
  return 1;
616
12.0k
}
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.41k
{
623
2.41k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
624
2.41k
  register _JSAMPROW ptr;
625
2.41k
  register U_CHAR *bufferptr;
626
2.41k
  register _JSAMPLE *rescale = source->rescale;
627
2.41k
  JDIMENSION col;
628
2.41k
  unsigned int maxval = source->maxval;
629
630
2.41k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
631
79
    ERREXIT(cinfo, JERR_INPUT_EOF);
632
2.41k
  ptr = source->pub._buffer[0];
633
2.41k
  bufferptr = source->iobuffer;
634
5.91k
  for (col = cinfo->image_width; col > 0; col--) {
635
3.50k
    register unsigned int r, g, b;
636
3.50k
    r  = UCH(*bufferptr++) << 8;
637
3.50k
    r |= UCH(*bufferptr++);
638
3.50k
    if (r > maxval)
639
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
640
3.50k
    g  = UCH(*bufferptr++) << 8;
641
3.50k
    g |= UCH(*bufferptr++);
642
3.50k
    if (g > maxval)
643
19
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
644
3.50k
    b  = UCH(*bufferptr++) << 8;
645
3.50k
    b |= UCH(*bufferptr++);
646
3.50k
    if (b > maxval)
647
24
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
648
3.50k
    rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2,
649
3.50k
                ptr + 3);
650
3.50k
    ptr += 4;
651
3.50k
  }
652
2.41k
  return 1;
653
2.41k
}
654
655
656
/*
657
 * Read the file header; return image size and component count.
658
 */
659
660
METHODDEF(void)
661
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
662
19.8k
{
663
19.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
664
19.8k
  int c;
665
19.8k
  unsigned int w, h, maxval;
666
19.8k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
667
668
19.8k
  if (getc(source->pub.input_file) != 'P')
669
0
    ERREXIT(cinfo, JERR_PPM_NOT);
670
671
19.8k
  c = getc(source->pub.input_file); /* subformat discriminator character */
672
673
  /* detect unsupported variants (ie, PBM) before trying to read header */
674
19.8k
  switch (c) {
675
1.53k
  case '2':                     /* it's a text-format PGM file */
676
3.13k
  case '3':                     /* it's a text-format PPM file */
677
15.5k
  case '5':                     /* it's a raw-format PGM file */
678
19.8k
  case '6':                     /* it's a raw-format PPM file */
679
19.8k
    break;
680
14
  default:
681
14
    ERREXIT(cinfo, JERR_PPM_NOT);
682
14
    break;
683
19.8k
  }
684
685
  /* fetch the remaining header info */
686
19.8k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
687
19.8k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
688
19.8k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
689
690
19.8k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
691
28
    ERREXIT(cinfo, JERR_PPM_NOT);
692
19.8k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
693
266
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
694
695
19.8k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
696
19.8k
  cinfo->image_width = (JDIMENSION)w;
697
19.8k
  cinfo->image_height = (JDIMENSION)h;
698
19.8k
  source->maxval = maxval;
699
700
  /* initialize flags to most common settings */
701
19.8k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
702
19.8k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
703
19.8k
  need_rescale = TRUE;          /* do we need a rescale array? */
704
705
19.8k
  switch (c) {
706
1.09k
  case '2':                     /* it's a text-format PGM file */
707
1.09k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
708
1.09k
        cinfo->in_color_space == JCS_RGB)
709
0
      cinfo->in_color_space = JCS_GRAYSCALE;
710
1.09k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
711
1.09k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
712
157
      source->pub.get_pixel_rows = get_text_gray_row;
713
942
    else if (IsExtRGB(cinfo->in_color_space))
714
785
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
715
157
    else if (cinfo->in_color_space == JCS_CMYK)
716
157
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
717
0
    else
718
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
719
1.09k
    need_iobuffer = FALSE;
720
1.09k
    break;
721
722
1.22k
  case '3':                     /* it's a text-format PPM file */
723
1.22k
    if (cinfo->in_color_space == JCS_UNKNOWN)
724
0
      cinfo->in_color_space = JCS_EXT_RGB;
725
1.22k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
726
1.22k
    if (IsExtRGB(cinfo->in_color_space))
727
875
      source->pub.get_pixel_rows = get_text_rgb_row;
728
350
    else if (cinfo->in_color_space == JCS_CMYK)
729
175
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
730
175
    else
731
175
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
732
1.22k
    need_iobuffer = FALSE;
733
1.22k
    break;
734
735
12.0k
  case '5':                     /* it's a raw-format PGM file */
736
12.0k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
737
12.0k
        cinfo->in_color_space == JCS_RGB)
738
0
      cinfo->in_color_space = JCS_GRAYSCALE;
739
12.0k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
740
12.0k
    if (maxval > 255) {
741
756
      if (cinfo->in_color_space == JCS_GRAYSCALE)
742
108
        source->pub.get_pixel_rows = get_word_gray_row;
743
648
      else if (IsExtRGB(cinfo->in_color_space))
744
540
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
745
108
      else if (cinfo->in_color_space == JCS_CMYK)
746
108
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
747
0
      else
748
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
749
11.3k
    } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
750
11.3k
               cinfo->in_color_space == JCS_GRAYSCALE) {
751
0
      source->pub.get_pixel_rows = get_raw_row;
752
0
      use_raw_buffer = TRUE;
753
0
      need_rescale = FALSE;
754
11.3k
    } else {
755
11.3k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
756
1.62k
        source->pub.get_pixel_rows = get_scaled_gray_row;
757
9.72k
      else if (IsExtRGB(cinfo->in_color_space))
758
8.10k
        source->pub.get_pixel_rows = get_gray_rgb_row;
759
1.62k
      else if (cinfo->in_color_space == JCS_CMYK)
760
1.62k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
761
0
      else
762
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
763
11.3k
    }
764
12.0k
    break;
765
766
4.02k
  case '6':                     /* it's a raw-format PPM file */
767
4.02k
    if (cinfo->in_color_space == JCS_UNKNOWN)
768
0
      cinfo->in_color_space = JCS_EXT_RGB;
769
4.02k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
770
4.02k
    if (maxval > 255) {
771
1.15k
      if (IsExtRGB(cinfo->in_color_space))
772
825
        source->pub.get_pixel_rows = get_word_rgb_row;
773
330
      else if (cinfo->in_color_space == JCS_CMYK)
774
165
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
775
165
      else
776
165
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
777
2.87k
    } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
778
2.87k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
779
2.87k
               (cinfo->in_color_space == JCS_EXT_RGB ||
780
0
                cinfo->in_color_space == JCS_RGB)) {
781
#else
782
               cinfo->in_color_space == JCS_EXT_RGB) {
783
#endif
784
0
      source->pub.get_pixel_rows = get_raw_row;
785
0
      use_raw_buffer = TRUE;
786
0
      need_rescale = FALSE;
787
2.87k
    } else {
788
2.87k
      if (IsExtRGB(cinfo->in_color_space))
789
2.05k
        source->pub.get_pixel_rows = get_rgb_row;
790
820
      else if (cinfo->in_color_space == JCS_CMYK)
791
410
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
792
410
      else
793
410
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
794
2.87k
    }
795
4.02k
    break;
796
19.8k
  }
797
798
17.6k
  if (IsExtRGB(cinfo->in_color_space))
799
13.1k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
800
4.52k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
801
1.88k
    cinfo->input_components = 1;
802
2.63k
  else if (cinfo->in_color_space == JCS_CMYK)
803
2.63k
    cinfo->input_components = 4;
804
805
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
806
17.6k
  if (need_iobuffer) {
807
15.5k
    if (c == '6')
808
3.45k
      source->buffer_width = (size_t)w * 3 *
809
3.45k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
810
12.0k
    else
811
12.0k
      source->buffer_width = (size_t)w *
812
12.0k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
813
15.5k
    source->iobuffer = (U_CHAR *)
814
15.5k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
815
15.5k
                                  source->buffer_width);
816
15.5k
  }
817
818
  /* Create compressor input buffer. */
819
17.6k
  if (use_raw_buffer) {
820
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
821
    /* Synthesize a _JSAMPARRAY pointer structure */
822
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
823
0
    source->pub._buffer = &source->pixrow;
824
0
    source->pub.buffer_height = 1;
825
17.6k
  } else {
826
    /* Need to translate anyway, so make a separate sample buffer. */
827
17.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
828
17.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
829
17.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
830
17.6k
    source->pub.buffer_height = 1;
831
17.6k
  }
832
833
  /* Compute the rescaling array if required. */
834
17.6k
  if (need_rescale) {
835
17.6k
    long val, half_maxval;
836
837
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
838
17.6k
    source->rescale = (_JSAMPLE *)
839
17.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
840
17.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
841
17.6k
                                           sizeof(_JSAMPLE)));
842
17.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
843
17.6k
                                        sizeof(_JSAMPLE)));
844
17.6k
    half_maxval = maxval / 2;
845
38.7M
    for (val = 0; val <= (long)maxval; val++) {
846
      /* The multiplication here must be done in 32 bits to avoid overflow */
847
38.7M
      source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) /
848
38.7M
                                        maxval);
849
38.7M
    }
850
17.6k
  }
851
17.6k
}
852
853
854
/*
855
 * Finish up at the end of the file.
856
 */
857
858
METHODDEF(void)
859
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
860
13.2k
{
861
  /* no work */
862
13.2k
}
863
864
865
/*
866
 * The module selection routine for PPM format input.
867
 */
868
869
GLOBAL(cjpeg_source_ptr)
870
_jinit_read_ppm(j_compress_ptr cinfo)
871
19.8k
{
872
19.8k
  ppm_source_ptr source;
873
874
19.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
875
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
876
877
  /* Create module interface object */
878
19.8k
  source = (ppm_source_ptr)
879
19.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
880
19.8k
                                sizeof(ppm_source_struct));
881
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
882
19.8k
  source->pub.start_input = start_input_ppm;
883
19.8k
  source->pub.finish_input = finish_input_ppm;
884
19.8k
  source->pub.max_pixels = 0;
885
886
19.8k
  return (cjpeg_source_ptr)source;
887
19.8k
}
Unexecuted instantiation: jinit_read_ppm
j12init_read_ppm
Line
Count
Source
871
19.8k
{
872
19.8k
  ppm_source_ptr source;
873
874
19.8k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
875
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
876
877
  /* Create module interface object */
878
19.8k
  source = (ppm_source_ptr)
879
19.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
880
19.8k
                                sizeof(ppm_source_struct));
881
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
882
19.8k
  source->pub.start_input = start_input_ppm;
883
19.8k
  source->pub.finish_input = finish_input_ppm;
884
19.8k
  source->pub.max_pixels = 0;
885
886
19.8k
  return (cjpeg_source_ptr)source;
887
19.8k
}
Unexecuted instantiation: j16init_read_ppm
888
889
#endif /* defined(PPM_SUPPORTED) &&
890
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */