Coverage Report

Created: 2026-01-10 06:48

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, 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
28.1M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
50.5M
  (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
336k
{
80
336k
  register int ch;
81
82
336k
  ch = getc(infile);
83
336k
  if (ch == '#') {
84
85.8k
    do {
85
85.8k
      ch = getc(infile);
86
85.8k
    } while (ch != '\n' && ch != EOF);
87
4.21k
  }
88
336k
  return ch;
89
336k
}
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
135k
{
99
135k
  register int ch;
100
135k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
144k
  do {
104
144k
    ch = pbm_getc(infile);
105
144k
    if (ch == EOF)
106
2.53k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
144k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
135k
  if (ch < '0' || ch > '9')
110
199
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
135k
  val = ch - '0';
113
195k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
60.4k
    val *= 10;
115
60.4k
    val += ch - '0';
116
60.4k
    if (val > maxval)
117
137
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
60.4k
  }
119
120
135k
  return val;
121
135k
}
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
1.30k
{
139
1.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
140
1.30k
  FILE *infile = source->pub.input_file;
141
1.30k
  register _JSAMPROW ptr;
142
1.30k
  register _JSAMPLE *rescale = source->rescale;
143
1.30k
  JDIMENSION col;
144
1.30k
  unsigned int maxval = source->maxval;
145
146
1.30k
  ptr = source->pub._buffer[0];
147
3.52k
  for (col = cinfo->image_width; col > 0; col--) {
148
2.21k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
149
2.21k
  }
150
1.30k
  return 1;
151
1.30k
}
152
153
154
35.7M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
155
112M
  for (col = cinfo->image_width; col > 0; col--) { \
156
76.9M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
157
76.9M
    alpha_set_op \
158
76.9M
    ptr += ps; \
159
76.9M
  } \
160
35.7M
}
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
6.53k
{
167
6.53k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
168
6.53k
  FILE *infile = source->pub.input_file;
169
6.53k
  register _JSAMPROW ptr;
170
6.53k
  register _JSAMPLE *rescale = source->rescale;
171
6.53k
  JDIMENSION col;
172
6.53k
  unsigned int maxval = source->maxval;
173
6.53k
  register int rindex = rgb_red[cinfo->in_color_space];
174
6.53k
  register int gindex = rgb_green[cinfo->in_color_space];
175
6.53k
  register int bindex = rgb_blue[cinfo->in_color_space];
176
6.53k
  register int aindex = alpha_index[cinfo->in_color_space];
177
6.53k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
178
179
6.53k
  ptr = source->pub._buffer[0];
180
6.53k
  if (maxval == _MAXJSAMPLE) {
181
1.60k
    if (aindex >= 0)
182
321
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
183
1.60k
                         ptr[aindex] = _MAXJSAMPLE;)
184
1.28k
    else
185
1.28k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
186
4.92k
  } else {
187
4.92k
    if (aindex >= 0)
188
985
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
189
4.92k
                         ptr[aindex] = _MAXJSAMPLE;)
190
3.94k
    else
191
3.94k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
192
4.92k
  }
193
6.53k
  return 1;
194
6.53k
}
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
1.30k
{
202
1.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
203
1.30k
  FILE *infile = source->pub.input_file;
204
1.30k
  register _JSAMPROW ptr;
205
1.30k
  register _JSAMPLE *rescale = source->rescale;
206
1.30k
  JDIMENSION col;
207
1.30k
  unsigned int maxval = source->maxval;
208
209
1.30k
  ptr = source->pub._buffer[0];
210
1.30k
  if (maxval == _MAXJSAMPLE) {
211
939
    for (col = cinfo->image_width; col > 0; col--) {
212
618
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
213
618
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
214
618
      ptr += 4;
215
618
    }
216
985
  } else {
217
2.58k
    for (col = cinfo->image_width; col > 0; col--) {
218
1.59k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
219
1.59k
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
220
1.59k
      ptr += 4;
221
1.59k
    }
222
985
  }
223
1.30k
  return 1;
224
1.30k
}
225
226
227
385k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
228
2.91M
  for (col = cinfo->image_width; col > 0; col--) { \
229
2.52M
    ptr[rindex] = read_op; \
230
2.52M
    ptr[gindex] = read_op; \
231
2.52M
    ptr[bindex] = read_op; \
232
2.52M
    alpha_set_op \
233
2.52M
    ptr += ps; \
234
2.52M
  } \
235
385k
}
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.91k
{
241
9.91k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
242
9.91k
  FILE *infile = source->pub.input_file;
243
9.91k
  register _JSAMPROW ptr;
244
9.91k
  register _JSAMPLE *rescale = source->rescale;
245
9.91k
  JDIMENSION col;
246
9.91k
  unsigned int maxval = source->maxval;
247
9.91k
  register int rindex = rgb_red[cinfo->in_color_space];
248
9.91k
  register int gindex = rgb_green[cinfo->in_color_space];
249
9.91k
  register int bindex = rgb_blue[cinfo->in_color_space];
250
9.91k
  register int aindex = alpha_index[cinfo->in_color_space];
251
9.91k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
252
253
9.91k
  ptr = source->pub._buffer[0];
254
9.91k
  if (maxval == _MAXJSAMPLE) {
255
3.10k
    if (aindex >= 0)
256
620
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
257
3.10k
                    ptr[aindex] = _MAXJSAMPLE;)
258
2.48k
    else
259
2.48k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
260
6.81k
  } else {
261
6.81k
    if (aindex >= 0)
262
1.36k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
263
6.81k
                    ptr[aindex] = _MAXJSAMPLE;)
264
5.45k
    else
265
5.45k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
266
6.81k
  }
267
9.91k
  return 1;
268
9.91k
}
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.98k
{
276
1.98k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
277
1.98k
  FILE *infile = source->pub.input_file;
278
1.98k
  register _JSAMPROW ptr;
279
1.98k
  register _JSAMPLE *rescale = source->rescale;
280
1.98k
  JDIMENSION col;
281
1.98k
  unsigned int maxval = source->maxval;
282
283
1.98k
  ptr = source->pub._buffer[0];
284
1.98k
  if (maxval == _MAXJSAMPLE) {
285
2.05k
    for (col = cinfo->image_width; col > 0; col--) {
286
1.43k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
287
1.43k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.43k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.43k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
290
1.43k
      ptr += 4;
291
1.43k
    }
292
1.36k
  } else {
293
3.40k
    for (col = cinfo->image_width; col > 0; col--) {
294
2.04k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
295
2.04k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.04k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.04k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
298
2.04k
      ptr += 4;
299
2.04k
    }
300
1.36k
  }
301
1.98k
  return 1;
302
1.98k
}
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
5.44M
{
309
5.44M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
310
5.44M
  register _JSAMPROW ptr;
311
5.44M
  register U_CHAR *bufferptr;
312
5.44M
  register _JSAMPLE *rescale = source->rescale;
313
5.44M
  JDIMENSION col;
314
315
5.44M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
316
65
    ERREXIT(cinfo, JERR_INPUT_EOF);
317
5.44M
  ptr = source->pub._buffer[0];
318
5.44M
  bufferptr = source->iobuffer;
319
18.9M
  for (col = cinfo->image_width; col > 0; col--) {
320
13.5M
    *ptr++ = rescale[UCH(*bufferptr++)];
321
13.5M
  }
322
5.44M
  return 1;
323
5.44M
}
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
35.7M
{
331
35.7M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
332
35.7M
  register _JSAMPROW ptr;
333
35.7M
  register U_CHAR *bufferptr;
334
35.7M
  register _JSAMPLE *rescale = source->rescale;
335
35.7M
  JDIMENSION col;
336
35.7M
  unsigned int maxval = source->maxval;
337
35.7M
  register int rindex = rgb_red[cinfo->in_color_space];
338
35.7M
  register int gindex = rgb_green[cinfo->in_color_space];
339
35.7M
  register int bindex = rgb_blue[cinfo->in_color_space];
340
35.7M
  register int aindex = alpha_index[cinfo->in_color_space];
341
35.7M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
342
343
35.7M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
344
465
    ERREXIT(cinfo, JERR_INPUT_EOF);
345
35.7M
  ptr = source->pub._buffer[0];
346
35.7M
  bufferptr = source->iobuffer;
347
35.7M
  if (maxval == _MAXJSAMPLE) {
348
8.48M
    if (aindex >= 0)
349
1.69M
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
350
6.79M
    else
351
6.79M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
352
27.2M
  } else {
353
27.2M
    if (aindex >= 0)
354
5.44M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
355
27.2M
                         ptr[aindex] = _MAXJSAMPLE;)
356
21.7M
    else
357
21.7M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
358
27.2M
  }
359
35.7M
  return 1;
360
35.7M
}
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.14M
{
368
7.14M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
369
7.14M
  register _JSAMPROW ptr;
370
7.14M
  register U_CHAR *bufferptr;
371
7.14M
  register _JSAMPLE *rescale = source->rescale;
372
7.14M
  JDIMENSION col;
373
7.14M
  unsigned int maxval = source->maxval;
374
375
7.14M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
376
93
    ERREXIT(cinfo, JERR_INPUT_EOF);
377
7.14M
  ptr = source->pub._buffer[0];
378
7.14M
  bufferptr = source->iobuffer;
379
7.14M
  if (maxval == _MAXJSAMPLE) {
380
3.58M
    for (col = cinfo->image_width; col > 0; col--) {
381
1.88M
      _JSAMPLE gray = *bufferptr++;
382
1.88M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
383
1.88M
      ptr += 4;
384
1.88M
    }
385
5.44M
  } else {
386
18.9M
    for (col = cinfo->image_width; col > 0; col--) {
387
13.5M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
388
13.5M
      rgb_to_cmyk(gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
389
13.5M
      ptr += 4;
390
13.5M
    }
391
5.44M
  }
392
7.14M
  return 1;
393
7.14M
}
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
376k
{
400
376k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
401
376k
  register _JSAMPROW ptr;
402
376k
  register U_CHAR *bufferptr;
403
376k
  register _JSAMPLE *rescale = source->rescale;
404
376k
  JDIMENSION col;
405
376k
  unsigned int maxval = source->maxval;
406
376k
  register int rindex = rgb_red[cinfo->in_color_space];
407
376k
  register int gindex = rgb_green[cinfo->in_color_space];
408
376k
  register int bindex = rgb_blue[cinfo->in_color_space];
409
376k
  register int aindex = alpha_index[cinfo->in_color_space];
410
376k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
411
412
376k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
413
650
    ERREXIT(cinfo, JERR_INPUT_EOF);
414
376k
  ptr = source->pub._buffer[0];
415
376k
  bufferptr = source->iobuffer;
416
376k
  if (maxval == _MAXJSAMPLE) {
417
3.36k
    if (aindex >= 0)
418
840
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;)
419
2.52k
    else
420
2.52k
      RGB_READ_LOOP(*bufferptr++, {})
421
372k
  } else {
422
372k
    if (aindex >= 0)
423
74.4k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], ptr[aindex] = _MAXJSAMPLE;)
424
298k
    else
425
298k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
426
372k
  }
427
376k
  return 1;
428
376k
}
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
75.3k
{
436
75.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
437
75.3k
  register _JSAMPROW ptr;
438
75.3k
  register U_CHAR *bufferptr;
439
75.3k
  register _JSAMPLE *rescale = source->rescale;
440
75.3k
  JDIMENSION col;
441
75.3k
  unsigned int maxval = source->maxval;
442
443
75.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
444
142
    ERREXIT(cinfo, JERR_INPUT_EOF);
445
75.3k
  ptr = source->pub._buffer[0];
446
75.3k
  bufferptr = source->iobuffer;
447
75.3k
  if (maxval == _MAXJSAMPLE) {
448
230k
    for (col = cinfo->image_width; col > 0; col--) {
449
229k
      _JSAMPLE r = *bufferptr++;
450
229k
      _JSAMPLE g = *bufferptr++;
451
229k
      _JSAMPLE b = *bufferptr++;
452
229k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
453
229k
      ptr += 4;
454
229k
    }
455
74.5k
  } else {
456
392k
    for (col = cinfo->image_width; col > 0; col--) {
457
317k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
458
317k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
459
317k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
460
317k
      rgb_to_cmyk(r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
461
317k
      ptr += 4;
462
317k
    }
463
74.5k
  }
464
75.3k
  return 1;
465
75.3k
}
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
1.69M
{
475
1.69M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
476
477
1.69M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
478
88
    ERREXIT(cinfo, JERR_INPUT_EOF);
479
1.69M
  return 1;
480
1.69M
}
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
5.12k
{
487
5.12k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
488
5.12k
  register _JSAMPROW ptr;
489
5.12k
  register U_CHAR *bufferptr;
490
5.12k
  register _JSAMPLE *rescale = source->rescale;
491
5.12k
  JDIMENSION col;
492
5.12k
  unsigned int maxval = source->maxval;
493
494
5.12k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
495
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
496
5.12k
  ptr = source->pub._buffer[0];
497
5.12k
  bufferptr = source->iobuffer;
498
12.8k
  for (col = cinfo->image_width; col > 0; col--) {
499
7.69k
    register unsigned int temp;
500
7.69k
    temp  = UCH(*bufferptr++) << 8;
501
7.69k
    temp |= UCH(*bufferptr++);
502
7.69k
    if (temp > maxval)
503
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
504
7.69k
    *ptr++ = rescale[temp];
505
7.69k
  }
506
5.12k
  return 1;
507
5.12k
}
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
25.6k
{
514
25.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
515
25.6k
  register _JSAMPROW ptr;
516
25.6k
  register U_CHAR *bufferptr;
517
25.6k
  register _JSAMPLE *rescale = source->rescale;
518
25.6k
  JDIMENSION col;
519
25.6k
  unsigned int maxval = source->maxval;
520
25.6k
  register int rindex = rgb_red[cinfo->in_color_space];
521
25.6k
  register int gindex = rgb_green[cinfo->in_color_space];
522
25.6k
  register int bindex = rgb_blue[cinfo->in_color_space];
523
25.6k
  register int aindex = alpha_index[cinfo->in_color_space];
524
25.6k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
525
526
25.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
527
305
    ERREXIT(cinfo, JERR_INPUT_EOF);
528
25.6k
  ptr = source->pub._buffer[0];
529
25.6k
  bufferptr = source->iobuffer;
530
64.0k
  for (col = cinfo->image_width; col > 0; col--) {
531
38.4k
    register unsigned int temp;
532
38.4k
    temp  = UCH(*bufferptr++) << 8;
533
38.4k
    temp |= UCH(*bufferptr++);
534
38.4k
    if (temp > maxval)
535
180
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
536
38.4k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
537
38.4k
    if (aindex >= 0)
538
7.65k
      ptr[aindex] = _MAXJSAMPLE;
539
38.4k
    ptr += ps;
540
38.4k
  }
541
25.6k
  return 1;
542
25.6k
}
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
5.12k
{
549
5.12k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
550
5.12k
  register _JSAMPROW ptr;
551
5.12k
  register U_CHAR *bufferptr;
552
5.12k
  register _JSAMPLE *rescale = source->rescale;
553
5.12k
  JDIMENSION col;
554
5.12k
  unsigned int maxval = source->maxval;
555
556
5.12k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
557
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
558
5.12k
  ptr = source->pub._buffer[0];
559
5.12k
  bufferptr = source->iobuffer;
560
12.8k
  for (col = cinfo->image_width; col > 0; col--) {
561
7.69k
    register unsigned int gray;
562
7.69k
    gray  = UCH(*bufferptr++) << 8;
563
7.69k
    gray |= UCH(*bufferptr++);
564
7.69k
    if (gray > maxval)
565
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
566
7.69k
    rgb_to_cmyk(rescale[gray], rescale[gray], rescale[gray], ptr, ptr + 1,
567
7.69k
                ptr + 2, ptr + 3);
568
7.69k
    ptr += 4;
569
7.69k
  }
570
5.12k
  return 1;
571
5.12k
}
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
10.1k
{
578
10.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
579
10.1k
  register _JSAMPROW ptr;
580
10.1k
  register U_CHAR *bufferptr;
581
10.1k
  register _JSAMPLE *rescale = source->rescale;
582
10.1k
  JDIMENSION col;
583
10.1k
  unsigned int maxval = source->maxval;
584
10.1k
  register int rindex = rgb_red[cinfo->in_color_space];
585
10.1k
  register int gindex = rgb_green[cinfo->in_color_space];
586
10.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
587
10.1k
  register int aindex = alpha_index[cinfo->in_color_space];
588
10.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
589
590
10.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
591
410
    ERREXIT(cinfo, JERR_INPUT_EOF);
592
10.1k
  ptr = source->pub._buffer[0];
593
10.1k
  bufferptr = source->iobuffer;
594
27.0k
  for (col = cinfo->image_width; col > 0; col--) {
595
16.8k
    register unsigned int temp;
596
16.8k
    temp  = UCH(*bufferptr++) << 8;
597
16.8k
    temp |= UCH(*bufferptr++);
598
16.8k
    if (temp > maxval)
599
140
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
600
16.8k
    ptr[rindex] = rescale[temp];
601
16.8k
    temp  = UCH(*bufferptr++) << 8;
602
16.8k
    temp |= UCH(*bufferptr++);
603
16.8k
    if (temp > maxval)
604
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
605
16.8k
    ptr[gindex] = rescale[temp];
606
16.8k
    temp  = UCH(*bufferptr++) << 8;
607
16.8k
    temp |= UCH(*bufferptr++);
608
16.8k
    if (temp > maxval)
609
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
610
16.8k
    ptr[bindex] = rescale[temp];
611
16.8k
    if (aindex >= 0)
612
3.30k
      ptr[aindex] = _MAXJSAMPLE;
613
16.8k
    ptr += ps;
614
16.8k
  }
615
10.1k
  return 1;
616
10.1k
}
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.03k
{
623
2.03k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
624
2.03k
  register _JSAMPROW ptr;
625
2.03k
  register U_CHAR *bufferptr;
626
2.03k
  register _JSAMPLE *rescale = source->rescale;
627
2.03k
  JDIMENSION col;
628
2.03k
  unsigned int maxval = source->maxval;
629
630
2.03k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
631
82
    ERREXIT(cinfo, JERR_INPUT_EOF);
632
2.03k
  ptr = source->pub._buffer[0];
633
2.03k
  bufferptr = source->iobuffer;
634
5.40k
  for (col = cinfo->image_width; col > 0; col--) {
635
3.37k
    register unsigned int r, g, b;
636
3.37k
    r  = UCH(*bufferptr++) << 8;
637
3.37k
    r |= UCH(*bufferptr++);
638
3.37k
    if (r > maxval)
639
28
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
640
3.37k
    g  = UCH(*bufferptr++) << 8;
641
3.37k
    g |= UCH(*bufferptr++);
642
3.37k
    if (g > maxval)
643
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
644
3.37k
    b  = UCH(*bufferptr++) << 8;
645
3.37k
    b |= UCH(*bufferptr++);
646
3.37k
    if (b > maxval)
647
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
648
3.37k
    rgb_to_cmyk(rescale[r], rescale[g], rescale[b], ptr, ptr + 1, ptr + 2,
649
3.37k
                ptr + 3);
650
3.37k
    ptr += 4;
651
3.37k
  }
652
2.03k
  return 1;
653
2.03k
}
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
20.2k
{
663
20.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
664
20.2k
  int c;
665
20.2k
  unsigned int w, h, maxval;
666
20.2k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
667
668
20.2k
  if (getc(source->pub.input_file) != 'P')
669
0
    ERREXIT(cinfo, JERR_PPM_NOT);
670
671
20.2k
  c = getc(source->pub.input_file); /* subformat discriminator character */
672
673
  /* detect unsupported variants (ie, PBM) before trying to read header */
674
20.2k
  switch (c) {
675
1.18k
  case '2':                     /* it's a text-format PGM file */
676
2.68k
  case '3':                     /* it's a text-format PPM file */
677
16.0k
  case '5':                     /* it's a raw-format PGM file */
678
20.2k
  case '6':                     /* it's a raw-format PPM file */
679
20.2k
    break;
680
14
  default:
681
14
    ERREXIT(cinfo, JERR_PPM_NOT);
682
14
    break;
683
20.2k
  }
684
685
  /* fetch the remaining header info */
686
20.2k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
687
20.2k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
688
20.2k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
689
690
20.2k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
691
49
    ERREXIT(cinfo, JERR_PPM_NOT);
692
20.2k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
693
245
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
694
695
20.2k
  cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
696
20.2k
  cinfo->image_width = (JDIMENSION)w;
697
20.2k
  cinfo->image_height = (JDIMENSION)h;
698
20.2k
  source->maxval = maxval;
699
700
  /* initialize flags to most common settings */
701
20.2k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
702
20.2k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
703
20.2k
  need_rescale = TRUE;          /* do we need a rescale array? */
704
705
20.2k
  switch (c) {
706
889
  case '2':                     /* it's a text-format PGM file */
707
889
    if (cinfo->in_color_space == JCS_UNKNOWN ||
708
889
        cinfo->in_color_space == JCS_RGB)
709
0
      cinfo->in_color_space = JCS_GRAYSCALE;
710
889
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
711
889
    if (cinfo->in_color_space == JCS_GRAYSCALE)
712
127
      source->pub.get_pixel_rows = get_text_gray_row;
713
762
    else if (IsExtRGB(cinfo->in_color_space))
714
635
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
715
127
    else if (cinfo->in_color_space == JCS_CMYK)
716
127
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
717
0
    else
718
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
719
889
    need_iobuffer = FALSE;
720
889
    break;
721
722
1.16k
  case '3':                     /* it's a text-format PPM file */
723
1.16k
    if (cinfo->in_color_space == JCS_UNKNOWN)
724
0
      cinfo->in_color_space = JCS_EXT_RGB;
725
1.16k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
726
1.16k
    if (IsExtRGB(cinfo->in_color_space))
727
835
      source->pub.get_pixel_rows = get_text_rgb_row;
728
334
    else if (cinfo->in_color_space == JCS_CMYK)
729
167
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
730
167
    else
731
167
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
732
1.16k
    need_iobuffer = FALSE;
733
1.16k
    break;
734
735
12.9k
  case '5':                     /* it's a raw-format PGM file */
736
12.9k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
737
12.9k
        cinfo->in_color_space == JCS_RGB)
738
0
      cinfo->in_color_space = JCS_GRAYSCALE;
739
12.9k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
740
12.9k
    if (maxval > 255) {
741
749
      if (cinfo->in_color_space == JCS_GRAYSCALE)
742
107
        source->pub.get_pixel_rows = get_word_gray_row;
743
642
      else if (IsExtRGB(cinfo->in_color_space))
744
535
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
745
107
      else if (cinfo->in_color_space == JCS_CMYK)
746
107
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
747
0
      else
748
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
749
12.2k
    } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
750
1.23k
               cinfo->in_color_space == JCS_GRAYSCALE) {
751
176
      source->pub.get_pixel_rows = get_raw_row;
752
176
      use_raw_buffer = TRUE;
753
176
      need_rescale = FALSE;
754
12.0k
    } else {
755
12.0k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
756
1.56k
        source->pub.get_pixel_rows = get_scaled_gray_row;
757
10.4k
      else if (IsExtRGB(cinfo->in_color_space))
758
8.72k
        source->pub.get_pixel_rows = get_gray_rgb_row;
759
1.74k
      else if (cinfo->in_color_space == JCS_CMYK)
760
1.74k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
761
0
      else
762
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
763
12.0k
    }
764
12.9k
    break;
765
766
3.78k
  case '6':                     /* it's a raw-format PPM file */
767
3.78k
    if (cinfo->in_color_space == JCS_UNKNOWN)
768
0
      cinfo->in_color_space = JCS_EXT_RGB;
769
3.78k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
770
3.78k
    if (maxval > 255) {
771
1.11k
      if (IsExtRGB(cinfo->in_color_space))
772
795
        source->pub.get_pixel_rows = get_word_rgb_row;
773
318
      else if (cinfo->in_color_space == JCS_CMYK)
774
159
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
775
159
      else
776
159
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
777
2.67k
    } else if (maxval == _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
778
588
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
779
588
               (cinfo->in_color_space == JCS_EXT_RGB ||
780
504
                cinfo->in_color_space == JCS_RGB)) {
781
#else
782
               cinfo->in_color_space == JCS_EXT_RGB) {
783
#endif
784
84
      source->pub.get_pixel_rows = get_raw_row;
785
84
      use_raw_buffer = TRUE;
786
84
      need_rescale = FALSE;
787
2.59k
    } else {
788
2.59k
      if (IsExtRGB(cinfo->in_color_space))
789
1.82k
        source->pub.get_pixel_rows = get_rgb_row;
790
764
      else if (cinfo->in_color_space == JCS_CMYK)
791
382
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
792
382
      else
793
382
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
794
2.59k
    }
795
3.78k
    break;
796
20.2k
  }
797
798
18.1k
  if (IsExtRGB(cinfo->in_color_space))
799
13.4k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
800
4.66k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
801
1.97k
    cinfo->input_components = 1;
802
2.68k
  else if (cinfo->in_color_space == JCS_CMYK)
803
2.68k
    cinfo->input_components = 4;
804
805
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
806
18.1k
  if (need_iobuffer) {
807
16.2k
    if (c == '6')
808
3.24k
      source->buffer_width = (size_t)w * 3 *
809
3.24k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
810
12.9k
    else
811
12.9k
      source->buffer_width = (size_t)w *
812
12.9k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
813
16.2k
    source->iobuffer = (U_CHAR *)
814
16.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
815
16.2k
                                  source->buffer_width);
816
16.2k
  }
817
818
  /* Create compressor input buffer. */
819
18.1k
  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
260
    source->pixrow = (_JSAMPROW)source->iobuffer;
823
260
    source->pub._buffer = &source->pixrow;
824
260
    source->pub.buffer_height = 1;
825
17.8k
  } else {
826
    /* Need to translate anyway, so make a separate sample buffer. */
827
17.8k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
828
17.8k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
829
17.8k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
830
17.8k
    source->pub.buffer_height = 1;
831
17.8k
  }
832
833
  /* Compute the rescaling array if required. */
834
18.1k
  if (need_rescale) {
835
17.8k
    long val, half_maxval;
836
837
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
838
17.8k
    source->rescale = (_JSAMPLE *)
839
17.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
840
17.8k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
841
17.8k
                                           sizeof(_JSAMPLE)));
842
17.8k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
843
17.8k
                                        sizeof(_JSAMPLE)));
844
17.8k
    half_maxval = maxval / 2;
845
27.6M
    for (val = 0; val <= (long)maxval; val++) {
846
      /* The multiplication here must be done in 32 bits to avoid overflow */
847
27.6M
      source->rescale[val] = (_JSAMPLE)((val * _MAXJSAMPLE + half_maxval) /
848
27.6M
                                        maxval);
849
27.6M
    }
850
17.8k
  }
851
18.1k
}
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
20.2k
{
872
20.2k
  ppm_source_ptr source;
873
874
20.2k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
875
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
876
877
  /* Create module interface object */
878
20.2k
  source = (ppm_source_ptr)
879
20.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
880
20.2k
                                sizeof(ppm_source_struct));
881
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
882
20.2k
  source->pub.start_input = start_input_ppm;
883
20.2k
  source->pub.finish_input = finish_input_ppm;
884
20.2k
  source->pub.max_pixels = 0;
885
886
20.2k
  return (cjpeg_source_ptr)source;
887
20.2k
}
jinit_read_ppm
Line
Count
Source
871
20.2k
{
872
20.2k
  ppm_source_ptr source;
873
874
20.2k
  if (cinfo->data_precision != BITS_IN_JSAMPLE)
875
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
876
877
  /* Create module interface object */
878
20.2k
  source = (ppm_source_ptr)
879
20.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
880
20.2k
                                sizeof(ppm_source_struct));
881
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
882
20.2k
  source->pub.start_input = start_input_ppm;
883
20.2k
  source->pub.finish_input = finish_input_ppm;
884
20.2k
  source->pub.max_pixels = 0;
885
886
20.2k
  return (cjpeg_source_ptr)source;
887
20.2k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
888
889
#endif /* defined(PPM_SUPPORTED) &&
890
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */