Coverage Report

Created: 2026-03-12 07:03

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