Coverage Report

Created: 2026-04-12 06:05

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