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
4.22k
  (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
27.2k
{
74
27.2k
  register int ch;
75
76
27.2k
  ch = getc(infile);
77
27.2k
  if (ch == '#') {
78
2.02k
    do {
79
2.02k
      ch = getc(infile);
80
2.02k
    } while (ch != '\n' && ch != EOF);
81
522
  }
82
27.2k
  return ch;
83
27.2k
}
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
11.0k
{
93
11.0k
  register int ch;
94
11.0k
  register unsigned int val;
95
96
  /* Skip any leading whitespace */
97
12.2k
  do {
98
12.2k
    ch = pbm_getc(infile);
99
12.2k
    if (ch == EOF)
100
491
      ERREXIT(cinfo, JERR_INPUT_EOF);
101
12.2k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
102
103
11.0k
  if (ch < '0' || ch > '9')
104
63
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
105
106
11.0k
  val = ch - '0';
107
15.6k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
108
4.54k
    val *= 10;
109
4.54k
    val += ch - '0';
110
4.54k
    if (val > maxval)
111
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
112
4.54k
  }
113
114
11.0k
  return val;
115
11.0k
}
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
597
{
133
597
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
134
597
  FILE *infile = source->pub.input_file;
135
597
  register _JSAMPROW ptr;
136
597
  register _JSAMPLE *rescale = source->rescale;
137
597
  JDIMENSION col;
138
597
  unsigned int maxval = source->maxval;
139
140
597
  ptr = source->pub._buffer[0];
141
2.83k
  for (col = cinfo->image_width; col > 0; col--) {
142
2.23k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
143
2.23k
  }
144
597
  return 1;
145
597
}
146
147
148
0
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
149
0
  for (col = cinfo->image_width; col > 0; col--) { \
150
0
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
151
0
    alpha_set_op \
152
0
    ptr += ps; \
153
0
  } \
154
0
}
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
0
{
161
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
162
0
  FILE *infile = source->pub.input_file;
163
0
  register _JSAMPROW ptr;
164
0
  register _JSAMPLE *rescale = source->rescale;
165
0
  JDIMENSION col;
166
0
  unsigned int maxval = source->maxval;
167
0
  register int rindex = rgb_red[cinfo->in_color_space];
168
0
  register int gindex = rgb_green[cinfo->in_color_space];
169
0
  register int bindex = rgb_blue[cinfo->in_color_space];
170
0
  register int aindex = alpha_index[cinfo->in_color_space];
171
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
172
173
0
  ptr = source->pub._buffer[0];
174
0
  if (maxval == _MAXJSAMPLE) {
175
0
    if (aindex >= 0)
176
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
177
0
                         ptr[aindex] = _MAXJSAMPLE;)
178
0
    else
179
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
180
0
  } else {
181
0
    if (aindex >= 0)
182
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
183
0
                         ptr[aindex] = _MAXJSAMPLE;)
184
0
    else
185
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
186
0
  }
187
0
  return 1;
188
0
}
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
0
{
196
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
197
0
  FILE *infile = source->pub.input_file;
198
0
  register _JSAMPROW ptr;
199
0
  register _JSAMPLE *rescale = source->rescale;
200
0
  JDIMENSION col;
201
0
  unsigned int maxval = source->maxval;
202
203
0
  ptr = source->pub._buffer[0];
204
0
  if (maxval == _MAXJSAMPLE) {
205
0
    for (col = cinfo->image_width; col > 0; col--) {
206
0
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
207
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
208
0
      ptr += 4;
209
0
    }
210
0
  } else {
211
0
    for (col = cinfo->image_width; col > 0; col--) {
212
0
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
213
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
214
0
      ptr += 4;
215
0
    }
216
0
  }
217
0
  return 1;
218
0
}
219
220
221
2.35k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
222
31.4k
  for (col = cinfo->image_width; col > 0; col--) { \
223
29.0k
    ptr[rindex] = read_op; \
224
29.0k
    ptr[gindex] = read_op; \
225
29.0k
    ptr[bindex] = read_op; \
226
29.0k
    alpha_set_op \
227
29.0k
    ptr += ps; \
228
29.0k
  } \
229
2.35k
}
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
766
{
235
766
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
236
766
  FILE *infile = source->pub.input_file;
237
766
  register _JSAMPROW ptr;
238
766
  register _JSAMPLE *rescale = source->rescale;
239
766
  JDIMENSION col;
240
766
  unsigned int maxval = source->maxval;
241
766
  register int rindex = rgb_red[cinfo->in_color_space];
242
766
  register int gindex = rgb_green[cinfo->in_color_space];
243
766
  register int bindex = rgb_blue[cinfo->in_color_space];
244
766
  register int aindex = alpha_index[cinfo->in_color_space];
245
766
  register int ps = rgb_pixelsize[cinfo->in_color_space];
246
247
766
  ptr = source->pub._buffer[0];
248
766
  if (maxval == _MAXJSAMPLE) {
249
228
    if (aindex >= 0)
250
0
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
251
228
                    ptr[aindex] = _MAXJSAMPLE;)
252
228
    else
253
228
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
254
538
  } else {
255
538
    if (aindex >= 0)
256
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
257
538
                    ptr[aindex] = _MAXJSAMPLE;)
258
538
    else
259
538
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
260
538
  }
261
766
  return 1;
262
766
}
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
0
{
270
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
271
0
  FILE *infile = source->pub.input_file;
272
0
  register _JSAMPROW ptr;
273
0
  register _JSAMPLE *rescale = source->rescale;
274
0
  JDIMENSION col;
275
0
  unsigned int maxval = source->maxval;
276
277
0
  ptr = source->pub._buffer[0];
278
0
  if (maxval == _MAXJSAMPLE) {
279
0
    for (col = cinfo->image_width; col > 0; col--) {
280
0
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
281
0
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
282
0
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
283
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
284
0
      ptr += 4;
285
0
    }
286
0
  } else {
287
0
    for (col = cinfo->image_width; col > 0; col--) {
288
0
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
289
0
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
290
0
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
291
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
0
      ptr += 4;
293
0
    }
294
0
  }
295
0
  return 1;
296
0
}
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
536
{
303
536
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
304
536
  register _JSAMPROW ptr;
305
536
  register unsigned char *bufferptr;
306
536
  register _JSAMPLE *rescale = source->rescale;
307
536
  JDIMENSION col;
308
309
536
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
310
70
    ERREXIT(cinfo, JERR_INPUT_EOF);
311
536
  ptr = source->pub._buffer[0];
312
536
  bufferptr = source->iobuffer;
313
148k
  for (col = cinfo->image_width; col > 0; col--) {
314
147k
    *ptr++ = rescale[*bufferptr++];
315
147k
  }
316
536
  return 1;
317
536
}
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
0
{
325
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
326
0
  register _JSAMPROW ptr;
327
0
  register unsigned char *bufferptr;
328
0
  register _JSAMPLE *rescale = source->rescale;
329
0
  JDIMENSION col;
330
0
  unsigned int maxval = source->maxval;
331
0
  register int rindex = rgb_red[cinfo->in_color_space];
332
0
  register int gindex = rgb_green[cinfo->in_color_space];
333
0
  register int bindex = rgb_blue[cinfo->in_color_space];
334
0
  register int aindex = alpha_index[cinfo->in_color_space];
335
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
336
337
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
338
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
339
0
  ptr = source->pub._buffer[0];
340
0
  bufferptr = source->iobuffer;
341
0
  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
0
  } else {
347
0
    if (aindex >= 0)
348
0
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], ptr[aindex] = _MAXJSAMPLE;)
349
0
    else
350
0
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {})
351
0
  }
352
0
  return 1;
353
0
}
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
0
{
361
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
362
0
  register _JSAMPROW ptr;
363
0
  register unsigned char *bufferptr;
364
0
  register _JSAMPLE *rescale = source->rescale;
365
0
  JDIMENSION col;
366
0
  unsigned int maxval = source->maxval;
367
368
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
369
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
370
0
  ptr = source->pub._buffer[0];
371
0
  bufferptr = source->iobuffer;
372
0
  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
0
  } else {
379
0
    for (col = cinfo->image_width; col > 0; col--) {
380
0
      _JSAMPLE gray = rescale[*bufferptr++];
381
0
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
382
0
      ptr += 4;
383
0
    }
384
0
  }
385
0
  return 1;
386
0
}
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
1.59k
{
393
1.59k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
394
1.59k
  register _JSAMPROW ptr;
395
1.59k
  register unsigned char *bufferptr;
396
1.59k
  register _JSAMPLE *rescale = source->rescale;
397
1.59k
  JDIMENSION col;
398
1.59k
  unsigned int maxval = source->maxval;
399
1.59k
  register int rindex = rgb_red[cinfo->in_color_space];
400
1.59k
  register int gindex = rgb_green[cinfo->in_color_space];
401
1.59k
  register int bindex = rgb_blue[cinfo->in_color_space];
402
1.59k
  register int aindex = alpha_index[cinfo->in_color_space];
403
1.59k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
404
405
1.59k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
406
218
    ERREXIT(cinfo, JERR_INPUT_EOF);
407
1.59k
  ptr = source->pub._buffer[0];
408
1.59k
  bufferptr = source->iobuffer;
409
1.59k
  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
1.59k
  } else {
415
1.59k
    if (aindex >= 0)
416
0
      RGB_READ_LOOP(rescale[*bufferptr++], ptr[aindex] = _MAXJSAMPLE;)
417
1.59k
    else
418
1.59k
      RGB_READ_LOOP(rescale[*bufferptr++], {})
419
1.59k
  }
420
1.59k
  return 1;
421
1.59k
}
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
0
{
429
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
430
0
  register _JSAMPROW ptr;
431
0
  register unsigned char *bufferptr;
432
0
  register _JSAMPLE *rescale = source->rescale;
433
0
  JDIMENSION col;
434
0
  unsigned int maxval = source->maxval;
435
436
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
437
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
438
0
  ptr = source->pub._buffer[0];
439
0
  bufferptr = source->iobuffer;
440
0
  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
0
  } else {
449
0
    for (col = cinfo->image_width; col > 0; col--) {
450
0
      _JSAMPLE r = rescale[*bufferptr++];
451
0
      _JSAMPLE g = rescale[*bufferptr++];
452
0
      _JSAMPLE b = rescale[*bufferptr++];
453
0
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
454
0
      ptr += 4;
455
0
    }
456
0
  }
457
0
  return 1;
458
0
}
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
293
{
470
293
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
471
472
293
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
473
17
    ERREXIT(cinfo, JERR_INPUT_EOF);
474
293
  return 1;
475
293
}
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
504
{
484
504
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
485
504
  register _JSAMPROW ptr;
486
504
  register unsigned char *bufferptr;
487
504
  register _JSAMPLE *rescale = source->rescale;
488
504
  JDIMENSION col;
489
504
  unsigned int maxval = source->maxval;
490
491
504
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
492
39
    ERREXIT(cinfo, JERR_INPUT_EOF);
493
504
  ptr = source->pub._buffer[0];
494
504
  bufferptr = source->iobuffer;
495
1.33k
  for (col = cinfo->image_width; col > 0; col--) {
496
835
    register unsigned int temp;
497
835
    temp  = (*bufferptr++) << 8;
498
835
    temp |= (*bufferptr++);
499
835
    if (temp > maxval)
500
31
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
501
835
    *ptr++ = rescale[temp];
502
835
  }
503
504
  return 1;
504
504
}
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
0
{
511
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
512
0
  register _JSAMPROW ptr;
513
0
  register unsigned char *bufferptr;
514
0
  register _JSAMPLE *rescale = source->rescale;
515
0
  JDIMENSION col;
516
0
  unsigned int maxval = source->maxval;
517
0
  register int rindex = rgb_red[cinfo->in_color_space];
518
0
  register int gindex = rgb_green[cinfo->in_color_space];
519
0
  register int bindex = rgb_blue[cinfo->in_color_space];
520
0
  register int aindex = alpha_index[cinfo->in_color_space];
521
0
  register int ps = rgb_pixelsize[cinfo->in_color_space];
522
523
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
524
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
525
0
  ptr = source->pub._buffer[0];
526
0
  bufferptr = source->iobuffer;
527
0
  for (col = cinfo->image_width; col > 0; col--) {
528
0
    register unsigned int temp;
529
0
    temp  = (*bufferptr++) << 8;
530
0
    temp |= (*bufferptr++);
531
0
    if (temp > maxval)
532
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
533
0
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
534
0
    if (aindex >= 0)
535
0
      ptr[aindex] = _MAXJSAMPLE;
536
0
    ptr += ps;
537
0
  }
538
0
  return 1;
539
0
}
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
0
{
546
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
547
0
  register _JSAMPROW ptr;
548
0
  register unsigned char *bufferptr;
549
0
  register _JSAMPLE *rescale = source->rescale;
550
0
  JDIMENSION col;
551
0
  unsigned int maxval = source->maxval;
552
553
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
554
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
555
0
  ptr = source->pub._buffer[0];
556
0
  bufferptr = source->iobuffer;
557
0
  for (col = cinfo->image_width; col > 0; col--) {
558
0
    register unsigned int gray;
559
0
    gray  = (*bufferptr++) << 8;
560
0
    gray |= (*bufferptr++);
561
0
    if (gray > maxval)
562
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
563
0
    rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1,
564
0
                ptr + 2, ptr + 3);
565
0
    ptr += 4;
566
0
  }
567
0
  return 1;
568
0
}
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
1.30k
{
575
1.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
576
1.30k
  register _JSAMPROW ptr;
577
1.30k
  register unsigned char *bufferptr;
578
1.30k
  register _JSAMPLE *rescale = source->rescale;
579
1.30k
  JDIMENSION col;
580
1.30k
  unsigned int maxval = source->maxval;
581
1.30k
  register int rindex = rgb_red[cinfo->in_color_space];
582
1.30k
  register int gindex = rgb_green[cinfo->in_color_space];
583
1.30k
  register int bindex = rgb_blue[cinfo->in_color_space];
584
1.30k
  register int aindex = alpha_index[cinfo->in_color_space];
585
1.30k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
586
587
1.30k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
588
96
    ERREXIT(cinfo, JERR_INPUT_EOF);
589
1.30k
  ptr = source->pub._buffer[0];
590
1.30k
  bufferptr = source->iobuffer;
591
75.1k
  for (col = cinfo->image_width; col > 0; col--) {
592
73.8k
    register unsigned int temp;
593
73.8k
    temp  = (*bufferptr++) << 8;
594
73.8k
    temp |= (*bufferptr++);
595
73.8k
    if (temp > maxval)
596
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
597
73.8k
    ptr[rindex] = rescale[temp];
598
73.8k
    temp  = (*bufferptr++) << 8;
599
73.8k
    temp |= (*bufferptr++);
600
73.8k
    if (temp > maxval)
601
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
602
73.8k
    ptr[gindex] = rescale[temp];
603
73.8k
    temp  = (*bufferptr++) << 8;
604
73.8k
    temp |= (*bufferptr++);
605
73.8k
    if (temp > maxval)
606
48
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
607
73.8k
    ptr[bindex] = rescale[temp];
608
73.8k
    if (aindex >= 0)
609
0
      ptr[aindex] = _MAXJSAMPLE;
610
73.8k
    ptr += ps;
611
73.8k
  }
612
1.30k
  return 1;
613
1.30k
}
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
0
{
620
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
621
0
  register _JSAMPROW ptr;
622
0
  register unsigned char *bufferptr;
623
0
  register _JSAMPLE *rescale = source->rescale;
624
0
  JDIMENSION col;
625
0
  unsigned int maxval = source->maxval;
626
627
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
628
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
629
0
  ptr = source->pub._buffer[0];
630
0
  bufferptr = source->iobuffer;
631
0
  for (col = cinfo->image_width; col > 0; col--) {
632
0
    register unsigned int r, g, b;
633
0
    r  = (*bufferptr++) << 8;
634
0
    r |= (*bufferptr++);
635
0
    if (r > maxval)
636
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
637
0
    g  = (*bufferptr++) << 8;
638
0
    g |= (*bufferptr++);
639
0
    if (g > maxval)
640
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
641
0
    b  = (*bufferptr++) << 8;
642
0
    b |= (*bufferptr++);
643
0
    if (b > maxval)
644
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
645
0
    rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2,
646
0
                ptr + 3);
647
0
    ptr += 4;
648
0
  }
649
0
  return 1;
650
0
}
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
2.00k
{
660
2.00k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
661
2.00k
  int c;
662
2.00k
  unsigned int w, h, maxval;
663
2.00k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
664
665
2.00k
  if (getc(source->pub.input_file) != 'P')
666
0
    ERREXIT(cinfo, JERR_PPM_NOT);
667
668
2.00k
  c = getc(source->pub.input_file); /* subformat discriminator character */
669
670
  /* detect unsupported variants (ie, PBM) before trying to read header */
671
2.00k
  switch (c) {
672
394
  case '2':                     /* it's a text-format PGM file */
673
648
  case '3':                     /* it's a text-format PPM file */
674
1.09k
  case '5':                     /* it's a raw-format PGM file */
675
2.00k
  case '6':                     /* it's a raw-format PPM file */
676
2.00k
    break;
677
4
  default:
678
4
    ERREXIT(cinfo, JERR_PPM_NOT);
679
4
    break;
680
2.00k
  }
681
682
  /* fetch the remaining header info */
683
2.00k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
684
2.00k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
685
2.00k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
686
687
2.00k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
688
6
    ERREXIT(cinfo, JERR_PPM_NOT);
689
2.00k
  if (w > JPEG_MAX_DIMENSION || h > JPEG_MAX_DIMENSION)
690
6
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
691
2.00k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
692
60
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
693
694
2.00k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
695
2.00k
  cinfo->image_width = (JDIMENSION)w;
696
2.00k
  cinfo->image_height = (JDIMENSION)h;
697
2.00k
  source->maxval = maxval;
698
699
  /* initialize flags to most common settings */
700
2.00k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
701
2.00k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
702
2.00k
  need_rescale = TRUE;          /* do we need a rescale array? */
703
704
2.00k
  switch (c) {
705
246
  case '2':                     /* it's a text-format PGM file */
706
246
    if (cinfo->in_color_space == JCS_UNKNOWN ||
707
246
        cinfo->in_color_space == JCS_RGB)
708
246
      cinfo->in_color_space = JCS_GRAYSCALE;
709
246
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
710
246
    if (cinfo->in_color_space == JCS_GRAYSCALE)
711
246
      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
246
    need_iobuffer = FALSE;
719
246
    break;
720
721
182
  case '3':                     /* it's a text-format PPM file */
722
182
    if (cinfo->in_color_space == JCS_UNKNOWN)
723
0
      cinfo->in_color_space = JCS_EXT_RGB;
724
182
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
725
182
    if (IsExtRGB(cinfo->in_color_space))
726
182
      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
182
    need_iobuffer = FALSE;
732
182
    break;
733
734
384
  case '5':                     /* it's a raw-format PGM file */
735
384
    if (cinfo->in_color_space == JCS_UNKNOWN ||
736
384
        cinfo->in_color_space == JCS_RGB)
737
384
      cinfo->in_color_space = JCS_GRAYSCALE;
738
384
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
739
384
    if (maxval > 255) {
740
146
      if (cinfo->in_color_space == JCS_GRAYSCALE)
741
146
        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
146
#if BITS_IN_JSAMPLE == 8
749
238
    } else if (maxval == _MAXJSAMPLE &&
750
24
               cinfo->in_color_space == JCS_GRAYSCALE) {
751
24
      source->pub.get_pixel_rows = get_raw_row;
752
24
      use_raw_buffer = TRUE;
753
24
      need_rescale = FALSE;
754
24
#endif
755
214
    } else {
756
214
      if (cinfo->in_color_space == JCS_GRAYSCALE)
757
214
        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
214
    }
765
384
    break;
766
767
810
  case '6':                     /* it's a raw-format PPM file */
768
810
    if (cinfo->in_color_space == JCS_UNKNOWN)
769
0
      cinfo->in_color_space = JCS_EXT_RGB;
770
810
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
771
810
    if (maxval > 255) {
772
234
      if (IsExtRGB(cinfo->in_color_space))
773
234
        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
234
#if BITS_IN_JSAMPLE == 8
779
576
    } else if (maxval == _MAXJSAMPLE &&
780
20
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
781
20
               (cinfo->in_color_space == JCS_EXT_RGB ||
782
20
                cinfo->in_color_space == JCS_RGB)) {
783
#else
784
               cinfo->in_color_space == JCS_EXT_RGB) {
785
#endif
786
20
      source->pub.get_pixel_rows = get_raw_row;
787
20
      use_raw_buffer = TRUE;
788
20
      need_rescale = FALSE;
789
20
#endif
790
556
    } else {
791
556
      if (IsExtRGB(cinfo->in_color_space))
792
556
        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
556
    }
798
810
    break;
799
2.00k
  }
800
801
1.62k
  if (IsExtRGB(cinfo->in_color_space))
802
992
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
803
630
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
804
630
    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
1.62k
  if (need_iobuffer) {
810
1.19k
    if (c == '6')
811
810
      source->buffer_width = (size_t)w * 3 * (maxval <= 255 ? 1 : 2);
812
384
    else
813
384
      source->buffer_width = (size_t)w * (maxval <= 255 ? 1 : 2);
814
1.19k
    source->iobuffer = (unsigned char *)
815
1.19k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
816
1.19k
                                  source->buffer_width);
817
1.19k
  }
818
819
  /* Create compressor input buffer. */
820
1.62k
  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
44
    source->pixrow = (_JSAMPROW)source->iobuffer;
824
44
    source->pub._buffer = &source->pixrow;
825
44
    source->pub.buffer_height = 1;
826
1.57k
  } else {
827
    /* Need to translate anyway, so make a separate sample buffer. */
828
1.57k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
829
1.57k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
830
1.57k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
831
1.57k
    source->pub.buffer_height = 1;
832
1.57k
  }
833
834
  /* Compute the rescaling array if required. */
835
1.62k
  if (need_rescale) {
836
1.57k
    size_t val, half_maxval;
837
838
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
839
1.57k
    source->rescale = (_JSAMPLE *)
840
1.57k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
841
1.57k
                                  (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
842
1.57k
    memset(source->rescale, 0, (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
843
1.57k
    half_maxval = (size_t)maxval / 2;
844
7.39M
    for (val = 0; val <= (size_t)maxval; val++) {
845
      /* The multiplication here must be done in 32 bits to avoid overflow */
846
7.39M
      source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) /
847
7.39M
                                        maxval);
848
7.39M
    }
849
1.57k
  }
850
1.62k
}
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
434
{
860
  /* no work */
861
434
}
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
2.00k
{
871
2.00k
  ppm_source_ptr source;
872
873
2.00k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
874
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
875
876
  /* Create module interface object */
877
2.00k
  source = (ppm_source_ptr)
878
2.00k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
879
2.00k
                                sizeof(ppm_source_struct));
880
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
881
2.00k
  source->pub.start_input = start_input_ppm;
882
2.00k
  source->pub.finish_input = finish_input_ppm;
883
2.00k
  source->pub.max_pixels = 0;
884
885
2.00k
  return (cjpeg_source_ptr)source;
886
2.00k
}
jinit_read_ppm
Line
Count
Source
870
2.00k
{
871
2.00k
  ppm_source_ptr source;
872
873
2.00k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
874
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
875
876
  /* Create module interface object */
877
2.00k
  source = (ppm_source_ptr)
878
2.00k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
879
2.00k
                                sizeof(ppm_source_struct));
880
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
881
2.00k
  source->pub.start_input = start_input_ppm;
882
2.00k
  source->pub.finish_input = finish_input_ppm;
883
2.00k
  source->pub.max_pixels = 0;
884
885
2.00k
  return (cjpeg_source_ptr)source;
886
2.00k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
887
888
#endif /* defined(PPM_SUPPORTED) &&
889
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */