Coverage Report

Created: 2026-02-26 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/src/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-2025, 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
17.6M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
24.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
286k
{
80
286k
  register int ch;
81
82
286k
  ch = getc(infile);
83
286k
  if (ch == '#') {
84
40.7k
    do {
85
40.7k
      ch = getc(infile);
86
40.7k
    } while (ch != '\n' && ch != EOF);
87
3.65k
  }
88
286k
  return ch;
89
286k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
286k
{
80
286k
  register int ch;
81
82
286k
  ch = getc(infile);
83
286k
  if (ch == '#') {
84
40.7k
    do {
85
40.7k
      ch = getc(infile);
86
40.7k
    } while (ch != '\n' && ch != EOF);
87
3.65k
  }
88
286k
  return ch;
89
286k
}
Unexecuted instantiation: rdppm-12.c:pbm_getc
Unexecuted instantiation: rdppm-16.c:pbm_getc
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
120k
{
99
120k
  register int ch;
100
120k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
126k
  do {
104
126k
    ch = pbm_getc(infile);
105
126k
    if (ch == EOF)
106
2.58k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
126k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
120k
  if (ch < '0' || ch > '9')
110
226
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
120k
  val = ch - '0';
113
162k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
42.0k
    val *= 10;
115
42.0k
    val += ch - '0';
116
42.0k
    if (val > maxval)
117
146
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
42.0k
  }
119
120
120k
  return val;
121
120k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
120k
{
99
120k
  register int ch;
100
120k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
126k
  do {
104
126k
    ch = pbm_getc(infile);
105
126k
    if (ch == EOF)
106
2.58k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
126k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
120k
  if (ch < '0' || ch > '9')
110
226
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
120k
  val = ch - '0';
113
162k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
42.0k
    val *= 10;
115
42.0k
    val += ch - '0';
116
42.0k
    if (val > maxval)
117
146
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
42.0k
  }
119
120
120k
  return val;
121
120k
}
Unexecuted instantiation: rdppm-12.c:read_pbm_integer
Unexecuted instantiation: rdppm-16.c:read_pbm_integer
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 cinfo->data_precision.
129
 *
130
 * A really fast path is provided for reading byte/sample raw files with
131
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U,
132
 * which is the normal case for 8-bit data.
133
 */
134
135
136
METHODDEF(JDIMENSION)
137
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
138
/* This version is for reading text-format PGM files with any maxval */
139
1.40k
{
140
1.40k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.40k
  FILE *infile = source->pub.input_file;
142
1.40k
  register _JSAMPROW ptr;
143
1.40k
  register _JSAMPLE *rescale = source->rescale;
144
1.40k
  JDIMENSION col;
145
1.40k
  unsigned int maxval = source->maxval;
146
147
1.40k
  ptr = source->pub._buffer[0];
148
4.04k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.64k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.64k
  }
151
1.40k
  return 1;
152
1.40k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
1.40k
{
140
1.40k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.40k
  FILE *infile = source->pub.input_file;
142
1.40k
  register _JSAMPROW ptr;
143
1.40k
  register _JSAMPLE *rescale = source->rescale;
144
1.40k
  JDIMENSION col;
145
1.40k
  unsigned int maxval = source->maxval;
146
147
1.40k
  ptr = source->pub._buffer[0];
148
4.04k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.64k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.64k
  }
151
1.40k
  return 1;
152
1.40k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
17.4M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
59.9M
  for (col = cinfo->image_width; col > 0; col--) { \
157
42.4M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
42.4M
    alpha_set_op \
159
42.4M
    ptr += ps; \
160
42.4M
  } \
161
17.4M
}
162
163
METHODDEF(JDIMENSION)
164
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
165
/* This version is for reading text-format PGM files with any maxval and
166
   converting to extended RGB */
167
7.01k
{
168
7.01k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
7.01k
  FILE *infile = source->pub.input_file;
170
7.01k
  register _JSAMPROW ptr;
171
7.01k
  register _JSAMPLE *rescale = source->rescale;
172
7.01k
  JDIMENSION col;
173
7.01k
  unsigned int maxval = source->maxval;
174
7.01k
  register int rindex = rgb_red[cinfo->in_color_space];
175
7.01k
  register int gindex = rgb_green[cinfo->in_color_space];
176
7.01k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
7.01k
  register int aindex = alpha_index[cinfo->in_color_space];
178
7.01k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
7.01k
  ptr = source->pub._buffer[0];
181
7.01k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.57k
    if (aindex >= 0)
183
315
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.57k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.26k
    else
186
1.26k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.43k
  } else {
188
5.43k
    if (aindex >= 0)
189
1.08k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.43k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
4.34k
    else
192
4.34k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.43k
  }
194
7.01k
  return 1;
195
7.01k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
7.01k
{
168
7.01k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
7.01k
  FILE *infile = source->pub.input_file;
170
7.01k
  register _JSAMPROW ptr;
171
7.01k
  register _JSAMPLE *rescale = source->rescale;
172
7.01k
  JDIMENSION col;
173
7.01k
  unsigned int maxval = source->maxval;
174
7.01k
  register int rindex = rgb_red[cinfo->in_color_space];
175
7.01k
  register int gindex = rgb_green[cinfo->in_color_space];
176
7.01k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
7.01k
  register int aindex = alpha_index[cinfo->in_color_space];
178
7.01k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
7.01k
  ptr = source->pub._buffer[0];
181
7.01k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.57k
    if (aindex >= 0)
183
315
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.57k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.26k
    else
186
1.26k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.43k
  } else {
188
5.43k
    if (aindex >= 0)
189
1.08k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.43k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
4.34k
    else
192
4.34k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.43k
  }
194
7.01k
  return 1;
195
7.01k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_rgb_row
196
197
198
METHODDEF(JDIMENSION)
199
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
200
/* This version is for reading text-format PGM files with any maxval and
201
   converting to CMYK */
202
1.40k
{
203
1.40k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.40k
  FILE *infile = source->pub.input_file;
205
1.40k
  register _JSAMPROW ptr;
206
1.40k
  register _JSAMPLE *rescale = source->rescale;
207
1.40k
  JDIMENSION col;
208
1.40k
  unsigned int maxval = source->maxval;
209
210
1.40k
  ptr = source->pub._buffer[0];
211
1.40k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.02k
    for (col = cinfo->image_width; col > 0; col--) {
213
713
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
713
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
713
      ptr += 4;
216
713
    }
217
1.08k
  } else {
218
3.01k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.92k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.92k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
1.92k
                  ptr + 1, ptr + 2, ptr + 3);
222
1.92k
      ptr += 4;
223
1.92k
    }
224
1.08k
  }
225
1.40k
  return 1;
226
1.40k
}
rdppm-8.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.40k
{
203
1.40k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.40k
  FILE *infile = source->pub.input_file;
205
1.40k
  register _JSAMPROW ptr;
206
1.40k
  register _JSAMPLE *rescale = source->rescale;
207
1.40k
  JDIMENSION col;
208
1.40k
  unsigned int maxval = source->maxval;
209
210
1.40k
  ptr = source->pub._buffer[0];
211
1.40k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.02k
    for (col = cinfo->image_width; col > 0; col--) {
213
713
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
713
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
713
      ptr += 4;
216
713
    }
217
1.08k
  } else {
218
3.01k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.92k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.92k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
1.92k
                  ptr + 1, ptr + 2, ptr + 3);
222
1.92k
      ptr += 4;
223
1.92k
    }
224
1.08k
  }
225
1.40k
  return 1;
226
1.40k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row
227
228
229
78.5k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
1.95M
  for (col = cinfo->image_width; col > 0; col--) { \
231
1.87M
    ptr[rindex] = read_op; \
232
1.87M
    ptr[gindex] = read_op; \
233
1.87M
    ptr[bindex] = read_op; \
234
1.87M
    alpha_set_op \
235
1.87M
    ptr += ps; \
236
1.87M
  } \
237
78.5k
}
238
239
METHODDEF(JDIMENSION)
240
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
241
/* This version is for reading text-format PPM files with any maxval */
242
9.08k
{
243
9.08k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
9.08k
  FILE *infile = source->pub.input_file;
245
9.08k
  register _JSAMPROW ptr;
246
9.08k
  register _JSAMPLE *rescale = source->rescale;
247
9.08k
  JDIMENSION col;
248
9.08k
  unsigned int maxval = source->maxval;
249
9.08k
  register int rindex = rgb_red[cinfo->in_color_space];
250
9.08k
  register int gindex = rgb_green[cinfo->in_color_space];
251
9.08k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
9.08k
  register int aindex = alpha_index[cinfo->in_color_space];
253
9.08k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
9.08k
  ptr = source->pub._buffer[0];
256
9.08k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.97k
    if (aindex >= 0)
258
594
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.97k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.37k
    else
261
2.37k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
6.11k
  } else {
263
6.11k
    if (aindex >= 0)
264
1.22k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.11k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
4.88k
    else
267
4.88k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.11k
  }
269
9.08k
  return 1;
270
9.08k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
242
9.08k
{
243
9.08k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
9.08k
  FILE *infile = source->pub.input_file;
245
9.08k
  register _JSAMPROW ptr;
246
9.08k
  register _JSAMPLE *rescale = source->rescale;
247
9.08k
  JDIMENSION col;
248
9.08k
  unsigned int maxval = source->maxval;
249
9.08k
  register int rindex = rgb_red[cinfo->in_color_space];
250
9.08k
  register int gindex = rgb_green[cinfo->in_color_space];
251
9.08k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
9.08k
  register int aindex = alpha_index[cinfo->in_color_space];
253
9.08k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
9.08k
  ptr = source->pub._buffer[0];
256
9.08k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.97k
    if (aindex >= 0)
258
594
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.97k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.37k
    else
261
2.37k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
6.11k
  } else {
263
6.11k
    if (aindex >= 0)
264
1.22k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.11k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
4.88k
    else
267
4.88k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.11k
  }
269
9.08k
  return 1;
270
9.08k
}
Unexecuted instantiation: rdppm-12.c:get_text_rgb_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_row
271
272
273
METHODDEF(JDIMENSION)
274
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
275
/* This version is for reading text-format PPM files with any maxval and
276
   converting to CMYK */
277
1.81k
{
278
1.81k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
1.81k
  FILE *infile = source->pub.input_file;
280
1.81k
  register _JSAMPROW ptr;
281
1.81k
  register _JSAMPLE *rescale = source->rescale;
282
1.81k
  JDIMENSION col;
283
1.81k
  unsigned int maxval = source->maxval;
284
285
1.81k
  ptr = source->pub._buffer[0];
286
1.81k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
1.64k
    for (col = cinfo->image_width; col > 0; col--) {
288
1.05k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.05k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.05k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
1.05k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
1.05k
      ptr += 4;
293
1.05k
    }
294
1.22k
  } else {
295
3.19k
    for (col = cinfo->image_width; col > 0; col--) {
296
1.97k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
1.97k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
1.97k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
1.97k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
1.97k
                  ptr + 2, ptr + 3);
301
1.97k
      ptr += 4;
302
1.97k
    }
303
1.22k
  }
304
1.81k
  return 1;
305
1.81k
}
rdppm-8.c:get_text_rgb_cmyk_row
Line
Count
Source
277
1.81k
{
278
1.81k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
1.81k
  FILE *infile = source->pub.input_file;
280
1.81k
  register _JSAMPROW ptr;
281
1.81k
  register _JSAMPLE *rescale = source->rescale;
282
1.81k
  JDIMENSION col;
283
1.81k
  unsigned int maxval = source->maxval;
284
285
1.81k
  ptr = source->pub._buffer[0];
286
1.81k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
1.64k
    for (col = cinfo->image_width; col > 0; col--) {
288
1.05k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.05k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.05k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
1.05k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
1.05k
      ptr += 4;
293
1.05k
    }
294
1.22k
  } else {
295
3.19k
    for (col = cinfo->image_width; col > 0; col--) {
296
1.97k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
1.97k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
1.97k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
1.97k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
1.97k
                  ptr + 2, ptr + 3);
301
1.97k
      ptr += 4;
302
1.97k
    }
303
1.22k
  }
304
1.81k
  return 1;
305
1.81k
}
Unexecuted instantiation: rdppm-12.c:get_text_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_cmyk_row
306
307
308
METHODDEF(JDIMENSION)
309
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
310
/* This version is for reading raw-byte-format PGM files with any maxval */
311
3.35M
{
312
3.35M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
3.35M
  register _JSAMPROW ptr;
314
3.35M
  register U_CHAR *bufferptr;
315
3.35M
  register _JSAMPLE *rescale = source->rescale;
316
3.35M
  JDIMENSION col;
317
318
3.35M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
67
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
3.35M
  ptr = source->pub._buffer[0];
321
3.35M
  bufferptr = source->iobuffer;
322
11.3M
  for (col = cinfo->image_width; col > 0; col--) {
323
8.02M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
8.02M
  }
325
3.35M
  return 1;
326
3.35M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
311
3.35M
{
312
3.35M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
3.35M
  register _JSAMPROW ptr;
314
3.35M
  register U_CHAR *bufferptr;
315
3.35M
  register _JSAMPLE *rescale = source->rescale;
316
3.35M
  JDIMENSION col;
317
318
3.35M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
67
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
3.35M
  ptr = source->pub._buffer[0];
321
3.35M
  bufferptr = source->iobuffer;
322
11.3M
  for (col = cinfo->image_width; col > 0; col--) {
323
8.02M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
8.02M
  }
325
3.35M
  return 1;
326
3.35M
}
Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row
Unexecuted instantiation: rdppm-16.c:get_scaled_gray_row
327
328
329
METHODDEF(JDIMENSION)
330
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
331
/* This version is for reading raw-byte-format PGM files with any maxval
332
   and converting to extended RGB */
333
17.4M
{
334
17.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
17.4M
  register _JSAMPROW ptr;
336
17.4M
  register U_CHAR *bufferptr;
337
17.4M
  register _JSAMPLE *rescale = source->rescale;
338
17.4M
  JDIMENSION col;
339
17.4M
  unsigned int maxval = source->maxval;
340
17.4M
  register int rindex = rgb_red[cinfo->in_color_space];
341
17.4M
  register int gindex = rgb_green[cinfo->in_color_space];
342
17.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
17.4M
  register int aindex = alpha_index[cinfo->in_color_space];
344
17.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
17.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
465
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
17.4M
  ptr = source->pub._buffer[0];
349
17.4M
  bufferptr = source->iobuffer;
350
17.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
688k
    if (aindex >= 0)
352
137k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
551k
    else
354
551k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
16.7M
  } else {
356
16.7M
    if (aindex >= 0)
357
3.35M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
16.7M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
13.4M
    else
360
13.4M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
16.7M
  }
362
17.4M
  return 1;
363
17.4M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
333
17.4M
{
334
17.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
17.4M
  register _JSAMPROW ptr;
336
17.4M
  register U_CHAR *bufferptr;
337
17.4M
  register _JSAMPLE *rescale = source->rescale;
338
17.4M
  JDIMENSION col;
339
17.4M
  unsigned int maxval = source->maxval;
340
17.4M
  register int rindex = rgb_red[cinfo->in_color_space];
341
17.4M
  register int gindex = rgb_green[cinfo->in_color_space];
342
17.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
17.4M
  register int aindex = alpha_index[cinfo->in_color_space];
344
17.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
17.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
465
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
17.4M
  ptr = source->pub._buffer[0];
349
17.4M
  bufferptr = source->iobuffer;
350
17.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
688k
    if (aindex >= 0)
352
137k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
551k
    else
354
551k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
16.7M
  } else {
356
16.7M
    if (aindex >= 0)
357
3.35M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
16.7M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
13.4M
    else
360
13.4M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
16.7M
  }
362
17.4M
  return 1;
363
17.4M
}
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_gray_rgb_row
364
365
366
METHODDEF(JDIMENSION)
367
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
368
/* This version is for reading raw-byte-format PGM files with any maxval
369
   and converting to CMYK */
370
3.49M
{
371
3.49M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
3.49M
  register _JSAMPROW ptr;
373
3.49M
  register U_CHAR *bufferptr;
374
3.49M
  register _JSAMPLE *rescale = source->rescale;
375
3.49M
  JDIMENSION col;
376
3.49M
  unsigned int maxval = source->maxval;
377
378
3.49M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
93
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
3.49M
  ptr = source->pub._buffer[0];
381
3.49M
  bufferptr = source->iobuffer;
382
3.49M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
595k
    for (col = cinfo->image_width; col > 0; col--) {
384
457k
      _JSAMPLE gray = *bufferptr++;
385
457k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
457k
      ptr += 4;
387
457k
    }
388
3.35M
  } else {
389
11.3M
    for (col = cinfo->image_width; col > 0; col--) {
390
8.02M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
8.02M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
8.02M
                  ptr + 1, ptr + 2, ptr + 3);
393
8.02M
      ptr += 4;
394
8.02M
    }
395
3.35M
  }
396
3.49M
  return 1;
397
3.49M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
370
3.49M
{
371
3.49M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
3.49M
  register _JSAMPROW ptr;
373
3.49M
  register U_CHAR *bufferptr;
374
3.49M
  register _JSAMPLE *rescale = source->rescale;
375
3.49M
  JDIMENSION col;
376
3.49M
  unsigned int maxval = source->maxval;
377
378
3.49M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
93
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
3.49M
  ptr = source->pub._buffer[0];
381
3.49M
  bufferptr = source->iobuffer;
382
3.49M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
595k
    for (col = cinfo->image_width; col > 0; col--) {
384
457k
      _JSAMPLE gray = *bufferptr++;
385
457k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
457k
      ptr += 4;
387
457k
    }
388
3.35M
  } else {
389
11.3M
    for (col = cinfo->image_width; col > 0; col--) {
390
8.02M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
8.02M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
8.02M
                  ptr + 1, ptr + 2, ptr + 3);
393
8.02M
      ptr += 4;
394
8.02M
    }
395
3.35M
  }
396
3.49M
  return 1;
397
3.49M
}
Unexecuted instantiation: rdppm-12.c:get_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_gray_cmyk_row
398
399
400
METHODDEF(JDIMENSION)
401
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
402
/* This version is for reading raw-byte-format PPM files with any maxval */
403
69.4k
{
404
69.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
69.4k
  register _JSAMPROW ptr;
406
69.4k
  register U_CHAR *bufferptr;
407
69.4k
  register _JSAMPLE *rescale = source->rescale;
408
69.4k
  JDIMENSION col;
409
69.4k
  unsigned int maxval = source->maxval;
410
69.4k
  register int rindex = rgb_red[cinfo->in_color_space];
411
69.4k
  register int gindex = rgb_green[cinfo->in_color_space];
412
69.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
69.4k
  register int aindex = alpha_index[cinfo->in_color_space];
414
69.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
69.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
719
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
69.4k
  ptr = source->pub._buffer[0];
419
69.4k
  bufferptr = source->iobuffer;
420
69.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
17.8k
    if (aindex >= 0)
422
4.46k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
13.4k
    else
424
13.4k
      RGB_READ_LOOP(*bufferptr++, {})
425
51.6k
  } else {
426
51.6k
    if (aindex >= 0)
427
10.1k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
51.6k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
41.4k
    else
430
41.4k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
51.6k
  }
432
69.4k
  return 1;
433
69.4k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
403
69.4k
{
404
69.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
69.4k
  register _JSAMPROW ptr;
406
69.4k
  register U_CHAR *bufferptr;
407
69.4k
  register _JSAMPLE *rescale = source->rescale;
408
69.4k
  JDIMENSION col;
409
69.4k
  unsigned int maxval = source->maxval;
410
69.4k
  register int rindex = rgb_red[cinfo->in_color_space];
411
69.4k
  register int gindex = rgb_green[cinfo->in_color_space];
412
69.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
69.4k
  register int aindex = alpha_index[cinfo->in_color_space];
414
69.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
69.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
719
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
69.4k
  ptr = source->pub._buffer[0];
419
69.4k
  bufferptr = source->iobuffer;
420
69.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
17.8k
    if (aindex >= 0)
422
4.46k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
13.4k
    else
424
13.4k
      RGB_READ_LOOP(*bufferptr++, {})
425
51.6k
  } else {
426
51.6k
    if (aindex >= 0)
427
10.1k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
51.6k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
41.4k
    else
430
41.4k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
51.6k
  }
432
69.4k
  return 1;
433
69.4k
}
Unexecuted instantiation: rdppm-12.c:get_rgb_row
Unexecuted instantiation: rdppm-16.c:get_rgb_row
434
435
436
METHODDEF(JDIMENSION)
437
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
438
/* This version is for reading raw-byte-format PPM files with any maxval and
439
   converting to CMYK */
440
14.8k
{
441
14.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
14.8k
  register _JSAMPROW ptr;
443
14.8k
  register U_CHAR *bufferptr;
444
14.8k
  register _JSAMPLE *rescale = source->rescale;
445
14.8k
  JDIMENSION col;
446
14.8k
  unsigned int maxval = source->maxval;
447
448
14.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
156
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
14.8k
  ptr = source->pub._buffer[0];
451
14.8k
  bufferptr = source->iobuffer;
452
14.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
233k
    for (col = cinfo->image_width; col > 0; col--) {
454
228k
      _JSAMPLE r = *bufferptr++;
455
228k
      _JSAMPLE g = *bufferptr++;
456
228k
      _JSAMPLE b = *bufferptr++;
457
228k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
228k
      ptr += 4;
459
228k
    }
460
10.3k
  } else {
461
198k
    for (col = cinfo->image_width; col > 0; col--) {
462
188k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
188k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
188k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
188k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
188k
                  ptr + 2, ptr + 3);
467
188k
      ptr += 4;
468
188k
    }
469
10.3k
  }
470
14.8k
  return 1;
471
14.8k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
440
14.8k
{
441
14.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
14.8k
  register _JSAMPROW ptr;
443
14.8k
  register U_CHAR *bufferptr;
444
14.8k
  register _JSAMPLE *rescale = source->rescale;
445
14.8k
  JDIMENSION col;
446
14.8k
  unsigned int maxval = source->maxval;
447
448
14.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
156
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
14.8k
  ptr = source->pub._buffer[0];
451
14.8k
  bufferptr = source->iobuffer;
452
14.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
233k
    for (col = cinfo->image_width; col > 0; col--) {
454
228k
      _JSAMPLE r = *bufferptr++;
455
228k
      _JSAMPLE g = *bufferptr++;
456
228k
      _JSAMPLE b = *bufferptr++;
457
228k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
228k
      ptr += 4;
459
228k
    }
460
10.3k
  } else {
461
198k
    for (col = cinfo->image_width; col > 0; col--) {
462
188k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
188k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
188k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
188k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
188k
                  ptr + 2, ptr + 3);
467
188k
      ptr += 4;
468
188k
    }
469
10.3k
  }
470
14.8k
  return 1;
471
14.8k
}
Unexecuted instantiation: rdppm-12.c:get_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_rgb_cmyk_row
472
473
474
METHODDEF(JDIMENSION)
475
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
476
/* This version is for reading raw-byte-format files with
477
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U.
478
 * In this case we just read right into the _JSAMPLE buffer!
479
 * Note that same code works for PPM and PGM files.
480
 */
481
142k
{
482
142k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
142k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
87
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
142k
  return 1;
487
142k
}
rdppm-8.c:get_raw_row
Line
Count
Source
481
142k
{
482
142k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
142k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
87
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
142k
  return 1;
487
142k
}
Unexecuted instantiation: rdppm-12.c:get_raw_row
Unexecuted instantiation: rdppm-16.c:get_raw_row
488
489
490
METHODDEF(JDIMENSION)
491
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
492
/* This version is for reading raw-word-format PGM files with any maxval */
493
1.69k
{
494
1.69k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
1.69k
  register _JSAMPROW ptr;
496
1.69k
  register U_CHAR *bufferptr;
497
1.69k
  register _JSAMPLE *rescale = source->rescale;
498
1.69k
  JDIMENSION col;
499
1.69k
  unsigned int maxval = source->maxval;
500
501
1.69k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
68
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.69k
  ptr = source->pub._buffer[0];
504
1.69k
  bufferptr = source->iobuffer;
505
69.8k
  for (col = cinfo->image_width; col > 0; col--) {
506
68.1k
    register unsigned int temp;
507
68.1k
    temp  = UCH(*bufferptr++) << 8;
508
68.1k
    temp |= UCH(*bufferptr++);
509
68.1k
    if (temp > maxval)
510
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
68.1k
    *ptr++ = rescale[temp];
512
68.1k
  }
513
1.69k
  return 1;
514
1.69k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
493
1.69k
{
494
1.69k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
1.69k
  register _JSAMPROW ptr;
496
1.69k
  register U_CHAR *bufferptr;
497
1.69k
  register _JSAMPLE *rescale = source->rescale;
498
1.69k
  JDIMENSION col;
499
1.69k
  unsigned int maxval = source->maxval;
500
501
1.69k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
68
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.69k
  ptr = source->pub._buffer[0];
504
1.69k
  bufferptr = source->iobuffer;
505
69.8k
  for (col = cinfo->image_width; col > 0; col--) {
506
68.1k
    register unsigned int temp;
507
68.1k
    temp  = UCH(*bufferptr++) << 8;
508
68.1k
    temp |= UCH(*bufferptr++);
509
68.1k
    if (temp > maxval)
510
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
68.1k
    *ptr++ = rescale[temp];
512
68.1k
  }
513
1.69k
  return 1;
514
1.69k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_row
515
516
517
METHODDEF(JDIMENSION)
518
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
519
/* This version is for reading raw-word-format PGM files with any maxval */
520
8.49k
{
521
8.49k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
8.49k
  register _JSAMPROW ptr;
523
8.49k
  register U_CHAR *bufferptr;
524
8.49k
  register _JSAMPLE *rescale = source->rescale;
525
8.49k
  JDIMENSION col;
526
8.49k
  unsigned int maxval = source->maxval;
527
8.49k
  register int rindex = rgb_red[cinfo->in_color_space];
528
8.49k
  register int gindex = rgb_green[cinfo->in_color_space];
529
8.49k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
8.49k
  register int aindex = alpha_index[cinfo->in_color_space];
531
8.49k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
8.49k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
340
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
8.49k
  ptr = source->pub._buffer[0];
536
8.49k
  bufferptr = source->iobuffer;
537
349k
  for (col = cinfo->image_width; col > 0; col--) {
538
340k
    register unsigned int temp;
539
340k
    temp  = UCH(*bufferptr++) << 8;
540
340k
    temp |= UCH(*bufferptr++);
541
340k
    if (temp > maxval)
542
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
340k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
340k
    if (aindex >= 0)
545
68.1k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
340k
    ptr += ps;
547
340k
  }
548
8.49k
  return 1;
549
8.49k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
520
8.49k
{
521
8.49k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
8.49k
  register _JSAMPROW ptr;
523
8.49k
  register U_CHAR *bufferptr;
524
8.49k
  register _JSAMPLE *rescale = source->rescale;
525
8.49k
  JDIMENSION col;
526
8.49k
  unsigned int maxval = source->maxval;
527
8.49k
  register int rindex = rgb_red[cinfo->in_color_space];
528
8.49k
  register int gindex = rgb_green[cinfo->in_color_space];
529
8.49k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
8.49k
  register int aindex = alpha_index[cinfo->in_color_space];
531
8.49k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
8.49k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
340
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
8.49k
  ptr = source->pub._buffer[0];
536
8.49k
  bufferptr = source->iobuffer;
537
349k
  for (col = cinfo->image_width; col > 0; col--) {
538
340k
    register unsigned int temp;
539
340k
    temp  = UCH(*bufferptr++) << 8;
540
340k
    temp |= UCH(*bufferptr++);
541
340k
    if (temp > maxval)
542
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
340k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
340k
    if (aindex >= 0)
545
68.1k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
340k
    ptr += ps;
547
340k
  }
548
8.49k
  return 1;
549
8.49k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_rgb_row
550
551
552
METHODDEF(JDIMENSION)
553
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
554
/* This version is for reading raw-word-format PGM files with any maxval */
555
1.69k
{
556
1.69k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
1.69k
  register _JSAMPROW ptr;
558
1.69k
  register U_CHAR *bufferptr;
559
1.69k
  register _JSAMPLE *rescale = source->rescale;
560
1.69k
  JDIMENSION col;
561
1.69k
  unsigned int maxval = source->maxval;
562
563
1.69k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
68
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
1.69k
  ptr = source->pub._buffer[0];
566
1.69k
  bufferptr = source->iobuffer;
567
1.69k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
0
    for (col = cinfo->image_width; col > 0; col--) {
569
0
      register unsigned int gray;
570
0
      gray  = UCH(*bufferptr++) << 8;
571
0
      gray |= UCH(*bufferptr++);
572
0
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
0
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
0
                  ptr + 1, ptr + 2, ptr + 3);
576
0
      ptr += 4;
577
0
    }
578
1.69k
  } else {
579
69.8k
    for (col = cinfo->image_width; col > 0; col--) {
580
68.1k
      register unsigned int temp;
581
68.1k
      _JSAMPLE gray;
582
68.1k
      temp  = UCH(*bufferptr++) << 8;
583
68.1k
      temp |= UCH(*bufferptr++);
584
68.1k
      if (temp > maxval)
585
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
68.1k
      gray = rescale[temp];
587
68.1k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
68.1k
                  ptr + 1, ptr + 2, ptr + 3);
589
68.1k
      ptr += 4;
590
68.1k
    }
591
1.69k
  }
592
1.69k
  return 1;
593
1.69k
}
rdppm-8.c:get_word_gray_cmyk_row
Line
Count
Source
555
1.69k
{
556
1.69k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
1.69k
  register _JSAMPROW ptr;
558
1.69k
  register U_CHAR *bufferptr;
559
1.69k
  register _JSAMPLE *rescale = source->rescale;
560
1.69k
  JDIMENSION col;
561
1.69k
  unsigned int maxval = source->maxval;
562
563
1.69k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
68
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
1.69k
  ptr = source->pub._buffer[0];
566
1.69k
  bufferptr = source->iobuffer;
567
1.69k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
0
    for (col = cinfo->image_width; col > 0; col--) {
569
0
      register unsigned int gray;
570
0
      gray  = UCH(*bufferptr++) << 8;
571
0
      gray |= UCH(*bufferptr++);
572
0
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
0
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
0
                  ptr + 1, ptr + 2, ptr + 3);
576
0
      ptr += 4;
577
0
    }
578
1.69k
  } else {
579
69.8k
    for (col = cinfo->image_width; col > 0; col--) {
580
68.1k
      register unsigned int temp;
581
68.1k
      _JSAMPLE gray;
582
68.1k
      temp  = UCH(*bufferptr++) << 8;
583
68.1k
      temp |= UCH(*bufferptr++);
584
68.1k
      if (temp > maxval)
585
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
68.1k
      gray = rescale[temp];
587
68.1k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
68.1k
                  ptr + 1, ptr + 2, ptr + 3);
589
68.1k
      ptr += 4;
590
68.1k
    }
591
1.69k
  }
592
1.69k
  return 1;
593
1.69k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_cmyk_row
594
595
596
METHODDEF(JDIMENSION)
597
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
598
/* This version is for reading raw-word-format PPM files with any maxval */
599
11.0k
{
600
11.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
11.0k
  register _JSAMPROW ptr;
602
11.0k
  register U_CHAR *bufferptr;
603
11.0k
  register _JSAMPLE *rescale = source->rescale;
604
11.0k
  JDIMENSION col;
605
11.0k
  unsigned int maxval = source->maxval;
606
11.0k
  register int rindex = rgb_red[cinfo->in_color_space];
607
11.0k
  register int gindex = rgb_green[cinfo->in_color_space];
608
11.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
11.0k
  register int aindex = alpha_index[cinfo->in_color_space];
610
11.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
11.0k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
360
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
11.0k
  ptr = source->pub._buffer[0];
615
11.0k
  bufferptr = source->iobuffer;
616
26.9k
  for (col = cinfo->image_width; col > 0; col--) {
617
15.8k
    register unsigned int temp;
618
15.8k
    temp  = UCH(*bufferptr++) << 8;
619
15.8k
    temp |= UCH(*bufferptr++);
620
15.8k
    if (temp > maxval)
621
155
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
15.8k
    ptr[rindex] = rescale[temp];
623
15.8k
    temp  = UCH(*bufferptr++) << 8;
624
15.8k
    temp |= UCH(*bufferptr++);
625
15.8k
    if (temp > maxval)
626
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
15.8k
    ptr[gindex] = rescale[temp];
628
15.8k
    temp  = UCH(*bufferptr++) << 8;
629
15.8k
    temp |= UCH(*bufferptr++);
630
15.8k
    if (temp > maxval)
631
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
15.8k
    ptr[bindex] = rescale[temp];
633
15.8k
    if (aindex >= 0)
634
3.09k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
15.8k
    ptr += ps;
636
15.8k
  }
637
11.0k
  return 1;
638
11.0k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
599
11.0k
{
600
11.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
11.0k
  register _JSAMPROW ptr;
602
11.0k
  register U_CHAR *bufferptr;
603
11.0k
  register _JSAMPLE *rescale = source->rescale;
604
11.0k
  JDIMENSION col;
605
11.0k
  unsigned int maxval = source->maxval;
606
11.0k
  register int rindex = rgb_red[cinfo->in_color_space];
607
11.0k
  register int gindex = rgb_green[cinfo->in_color_space];
608
11.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
11.0k
  register int aindex = alpha_index[cinfo->in_color_space];
610
11.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
11.0k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
360
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
11.0k
  ptr = source->pub._buffer[0];
615
11.0k
  bufferptr = source->iobuffer;
616
26.9k
  for (col = cinfo->image_width; col > 0; col--) {
617
15.8k
    register unsigned int temp;
618
15.8k
    temp  = UCH(*bufferptr++) << 8;
619
15.8k
    temp |= UCH(*bufferptr++);
620
15.8k
    if (temp > maxval)
621
155
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
15.8k
    ptr[rindex] = rescale[temp];
623
15.8k
    temp  = UCH(*bufferptr++) << 8;
624
15.8k
    temp |= UCH(*bufferptr++);
625
15.8k
    if (temp > maxval)
626
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
15.8k
    ptr[gindex] = rescale[temp];
628
15.8k
    temp  = UCH(*bufferptr++) << 8;
629
15.8k
    temp |= UCH(*bufferptr++);
630
15.8k
    if (temp > maxval)
631
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
15.8k
    ptr[bindex] = rescale[temp];
633
15.8k
    if (aindex >= 0)
634
3.09k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
15.8k
    ptr += ps;
636
15.8k
  }
637
11.0k
  return 1;
638
11.0k
}
Unexecuted instantiation: rdppm-12.c:get_word_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_row
639
640
641
METHODDEF(JDIMENSION)
642
get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
643
/* This version is for reading raw-word-format PPM files with any maxval */
644
2.21k
{
645
2.21k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
2.21k
  register _JSAMPROW ptr;
647
2.21k
  register U_CHAR *bufferptr;
648
2.21k
  register _JSAMPLE *rescale = source->rescale;
649
2.21k
  JDIMENSION col;
650
2.21k
  unsigned int maxval = source->maxval;
651
652
2.21k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
72
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
2.21k
  ptr = source->pub._buffer[0];
655
2.21k
  bufferptr = source->iobuffer;
656
2.21k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
0
    for (col = cinfo->image_width; col > 0; col--) {
658
0
      register unsigned int r, g, b;
659
0
      r  = UCH(*bufferptr++) << 8;
660
0
      r |= UCH(*bufferptr++);
661
0
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
0
      g  = UCH(*bufferptr++) << 8;
664
0
      g |= UCH(*bufferptr++);
665
0
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
0
      b  = UCH(*bufferptr++) << 8;
668
0
      b |= UCH(*bufferptr++);
669
0
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
0
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
0
                  ptr + 2, ptr + 3);
673
0
      ptr += 4;
674
0
    }
675
2.21k
  } else {
676
5.39k
    for (col = cinfo->image_width; col > 0; col--) {
677
3.17k
      register unsigned int r, g, b;
678
3.17k
      r  = UCH(*bufferptr++) << 8;
679
3.17k
      r |= UCH(*bufferptr++);
680
3.17k
      if (r > maxval)
681
31
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
3.17k
      g  = UCH(*bufferptr++) << 8;
683
3.17k
      g |= UCH(*bufferptr++);
684
3.17k
      if (g > maxval)
685
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
3.17k
      b  = UCH(*bufferptr++) << 8;
687
3.17k
      b |= UCH(*bufferptr++);
688
3.17k
      if (b > maxval)
689
25
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
3.17k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
3.17k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
3.17k
      ptr += 4;
693
3.17k
    }
694
2.21k
  }
695
2.21k
  return 1;
696
2.21k
}
rdppm-8.c:get_word_rgb_cmyk_row
Line
Count
Source
644
2.21k
{
645
2.21k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
2.21k
  register _JSAMPROW ptr;
647
2.21k
  register U_CHAR *bufferptr;
648
2.21k
  register _JSAMPLE *rescale = source->rescale;
649
2.21k
  JDIMENSION col;
650
2.21k
  unsigned int maxval = source->maxval;
651
652
2.21k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
72
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
2.21k
  ptr = source->pub._buffer[0];
655
2.21k
  bufferptr = source->iobuffer;
656
2.21k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
0
    for (col = cinfo->image_width; col > 0; col--) {
658
0
      register unsigned int r, g, b;
659
0
      r  = UCH(*bufferptr++) << 8;
660
0
      r |= UCH(*bufferptr++);
661
0
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
0
      g  = UCH(*bufferptr++) << 8;
664
0
      g |= UCH(*bufferptr++);
665
0
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
0
      b  = UCH(*bufferptr++) << 8;
668
0
      b |= UCH(*bufferptr++);
669
0
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
0
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
0
                  ptr + 2, ptr + 3);
673
0
      ptr += 4;
674
0
    }
675
2.21k
  } else {
676
5.39k
    for (col = cinfo->image_width; col > 0; col--) {
677
3.17k
      register unsigned int r, g, b;
678
3.17k
      r  = UCH(*bufferptr++) << 8;
679
3.17k
      r |= UCH(*bufferptr++);
680
3.17k
      if (r > maxval)
681
31
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
3.17k
      g  = UCH(*bufferptr++) << 8;
683
3.17k
      g |= UCH(*bufferptr++);
684
3.17k
      if (g > maxval)
685
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
3.17k
      b  = UCH(*bufferptr++) << 8;
687
3.17k
      b |= UCH(*bufferptr++);
688
3.17k
      if (b > maxval)
689
25
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
3.17k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
3.17k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
3.17k
      ptr += 4;
693
3.17k
    }
694
2.21k
  }
695
2.21k
  return 1;
696
2.21k
}
Unexecuted instantiation: rdppm-12.c:get_word_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_cmyk_row
697
698
699
/*
700
 * Read the file header; return image size and component count.
701
 */
702
703
METHODDEF(void)
704
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
705
17.0k
{
706
17.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
17.0k
  int c;
708
17.0k
  unsigned int w, h, maxval;
709
17.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
17.0k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
17.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
17.0k
  switch (c) {
718
1.26k
  case '2':                     /* it's a text-format PGM file */
719
2.73k
  case '3':                     /* it's a text-format PPM file */
720
13.0k
  case '5':                     /* it's a raw-format PGM file */
721
16.9k
  case '6':                     /* it's a raw-format PPM file */
722
16.9k
    break;
723
21
  default:
724
21
    ERREXIT(cinfo, JERR_PPM_NOT);
725
21
    break;
726
17.0k
  }
727
728
  /* fetch the remaining header info */
729
16.9k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
16.9k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
16.9k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
16.9k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
35
    ERREXIT(cinfo, JERR_PPM_NOT);
735
16.9k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
238
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
16.9k
  cinfo->image_width = (JDIMENSION)w;
739
16.9k
  cinfo->image_height = (JDIMENSION)h;
740
16.9k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
16.9k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
16.9k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
16.9k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
16.9k
  switch (c) {
748
945
  case '2':                     /* it's a text-format PGM file */
749
945
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
945
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
945
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
945
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
135
      source->pub.get_pixel_rows = get_text_gray_row;
755
810
    else if (IsExtRGB(cinfo->in_color_space))
756
675
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
135
    else if (cinfo->in_color_space == JCS_CMYK)
758
135
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
945
    need_iobuffer = FALSE;
762
945
    break;
763
764
1.19k
  case '3':                     /* it's a text-format PPM file */
765
1.19k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.19k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.19k
    if (IsExtRGB(cinfo->in_color_space))
769
850
      source->pub.get_pixel_rows = get_text_rgb_row;
770
340
    else if (cinfo->in_color_space == JCS_CMYK)
771
170
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
170
    else
773
170
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.19k
    need_iobuffer = FALSE;
775
1.19k
    break;
776
777
9.78k
  case '5':                     /* it's a raw-format PGM file */
778
9.78k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
9.78k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
9.78k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
9.78k
    if (maxval > 255) {
783
812
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
116
        source->pub.get_pixel_rows = get_word_gray_row;
785
696
      else if (IsExtRGB(cinfo->in_color_space))
786
580
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
116
      else if (cinfo->in_color_space == JCS_CMYK)
788
116
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
8.97k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
8.97k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
959
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
137
      source->pub.get_pixel_rows = get_raw_row;
795
137
      use_raw_buffer = TRUE;
796
137
      need_rescale = FALSE;
797
8.83k
    } else {
798
8.83k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.14k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
7.69k
      else if (IsExtRGB(cinfo->in_color_space))
801
6.41k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
1.28k
      else if (cinfo->in_color_space == JCS_CMYK)
803
1.28k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
8.83k
    }
807
9.78k
    break;
808
809
3.64k
  case '6':                     /* it's a raw-format PPM file */
810
3.64k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
3.64k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
3.64k
    if (maxval > 255) {
814
1.12k
      if (IsExtRGB(cinfo->in_color_space))
815
805
        source->pub.get_pixel_rows = get_word_rgb_row;
816
322
      else if (cinfo->in_color_space == JCS_CMYK)
817
161
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
161
      else
819
161
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
2.52k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
2.52k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
672
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
672
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
576
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
96
      source->pub.get_pixel_rows = get_raw_row;
829
96
      use_raw_buffer = TRUE;
830
96
      need_rescale = FALSE;
831
2.42k
    } else {
832
2.42k
      if (IsExtRGB(cinfo->in_color_space))
833
1.70k
        source->pub.get_pixel_rows = get_rgb_row;
834
720
      else if (cinfo->in_color_space == JCS_CMYK)
835
360
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
360
      else
837
360
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
2.42k
    }
839
3.64k
    break;
840
16.9k
  }
841
842
14.8k
  if (IsExtRGB(cinfo->in_color_space))
843
11.1k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
3.75k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.53k
    cinfo->input_components = 1;
846
2.22k
  else if (cinfo->in_color_space == JCS_CMYK)
847
2.22k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
14.8k
  if (need_iobuffer) {
851
12.9k
    if (c == '6')
852
3.12k
      source->buffer_width = (size_t)w * 3 *
853
3.12k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
9.78k
    else
855
9.78k
      source->buffer_width = (size_t)w *
856
9.78k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
12.9k
    source->iobuffer = (U_CHAR *)
858
12.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
12.9k
                                  source->buffer_width);
860
12.9k
  }
861
862
  /* Create compressor input buffer. */
863
14.8k
  if (use_raw_buffer) {
864
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
865
    /* Synthesize a _JSAMPARRAY pointer structure */
866
233
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
233
    source->pub._buffer = &source->pixrow;
868
233
    source->pub.buffer_height = 1;
869
14.6k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
14.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
14.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
14.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
14.6k
    source->pub.buffer_height = 1;
875
14.6k
  }
876
877
  /* Compute the rescaling array if required. */
878
14.8k
  if (need_rescale) {
879
14.6k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
14.6k
    source->rescale = (_JSAMPLE *)
883
14.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
14.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
14.6k
                                           sizeof(_JSAMPLE)));
886
14.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
14.6k
                                        sizeof(_JSAMPLE)));
888
14.6k
    half_maxval = maxval / 2;
889
23.7M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
23.7M
      source->rescale[val] =
892
23.7M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
23.7M
                   maxval);
894
23.7M
    }
895
14.6k
  }
896
14.8k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
705
17.0k
{
706
17.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
17.0k
  int c;
708
17.0k
  unsigned int w, h, maxval;
709
17.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
17.0k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
17.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
17.0k
  switch (c) {
718
1.26k
  case '2':                     /* it's a text-format PGM file */
719
2.73k
  case '3':                     /* it's a text-format PPM file */
720
13.0k
  case '5':                     /* it's a raw-format PGM file */
721
16.9k
  case '6':                     /* it's a raw-format PPM file */
722
16.9k
    break;
723
21
  default:
724
21
    ERREXIT(cinfo, JERR_PPM_NOT);
725
21
    break;
726
17.0k
  }
727
728
  /* fetch the remaining header info */
729
16.9k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
16.9k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
16.9k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
16.9k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
35
    ERREXIT(cinfo, JERR_PPM_NOT);
735
16.9k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
238
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
16.9k
  cinfo->image_width = (JDIMENSION)w;
739
16.9k
  cinfo->image_height = (JDIMENSION)h;
740
16.9k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
16.9k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
16.9k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
16.9k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
16.9k
  switch (c) {
748
945
  case '2':                     /* it's a text-format PGM file */
749
945
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
945
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
945
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
945
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
135
      source->pub.get_pixel_rows = get_text_gray_row;
755
810
    else if (IsExtRGB(cinfo->in_color_space))
756
675
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
135
    else if (cinfo->in_color_space == JCS_CMYK)
758
135
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
945
    need_iobuffer = FALSE;
762
945
    break;
763
764
1.19k
  case '3':                     /* it's a text-format PPM file */
765
1.19k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.19k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.19k
    if (IsExtRGB(cinfo->in_color_space))
769
850
      source->pub.get_pixel_rows = get_text_rgb_row;
770
340
    else if (cinfo->in_color_space == JCS_CMYK)
771
170
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
170
    else
773
170
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.19k
    need_iobuffer = FALSE;
775
1.19k
    break;
776
777
9.78k
  case '5':                     /* it's a raw-format PGM file */
778
9.78k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
9.78k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
9.78k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
9.78k
    if (maxval > 255) {
783
812
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
116
        source->pub.get_pixel_rows = get_word_gray_row;
785
696
      else if (IsExtRGB(cinfo->in_color_space))
786
580
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
116
      else if (cinfo->in_color_space == JCS_CMYK)
788
116
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
8.97k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
8.97k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
959
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
137
      source->pub.get_pixel_rows = get_raw_row;
795
137
      use_raw_buffer = TRUE;
796
137
      need_rescale = FALSE;
797
8.83k
    } else {
798
8.83k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.14k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
7.69k
      else if (IsExtRGB(cinfo->in_color_space))
801
6.41k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
1.28k
      else if (cinfo->in_color_space == JCS_CMYK)
803
1.28k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
8.83k
    }
807
9.78k
    break;
808
809
3.64k
  case '6':                     /* it's a raw-format PPM file */
810
3.64k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
3.64k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
3.64k
    if (maxval > 255) {
814
1.12k
      if (IsExtRGB(cinfo->in_color_space))
815
805
        source->pub.get_pixel_rows = get_word_rgb_row;
816
322
      else if (cinfo->in_color_space == JCS_CMYK)
817
161
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
161
      else
819
161
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
2.52k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
2.52k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
672
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
672
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
576
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
96
      source->pub.get_pixel_rows = get_raw_row;
829
96
      use_raw_buffer = TRUE;
830
96
      need_rescale = FALSE;
831
2.42k
    } else {
832
2.42k
      if (IsExtRGB(cinfo->in_color_space))
833
1.70k
        source->pub.get_pixel_rows = get_rgb_row;
834
720
      else if (cinfo->in_color_space == JCS_CMYK)
835
360
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
360
      else
837
360
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
2.42k
    }
839
3.64k
    break;
840
16.9k
  }
841
842
14.8k
  if (IsExtRGB(cinfo->in_color_space))
843
11.1k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
3.75k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.53k
    cinfo->input_components = 1;
846
2.22k
  else if (cinfo->in_color_space == JCS_CMYK)
847
2.22k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
14.8k
  if (need_iobuffer) {
851
12.9k
    if (c == '6')
852
3.12k
      source->buffer_width = (size_t)w * 3 *
853
3.12k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
9.78k
    else
855
9.78k
      source->buffer_width = (size_t)w *
856
9.78k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
12.9k
    source->iobuffer = (U_CHAR *)
858
12.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
12.9k
                                  source->buffer_width);
860
12.9k
  }
861
862
  /* Create compressor input buffer. */
863
14.8k
  if (use_raw_buffer) {
864
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
865
    /* Synthesize a _JSAMPARRAY pointer structure */
866
233
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
233
    source->pub._buffer = &source->pixrow;
868
233
    source->pub.buffer_height = 1;
869
14.6k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
14.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
14.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
14.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
14.6k
    source->pub.buffer_height = 1;
875
14.6k
  }
876
877
  /* Compute the rescaling array if required. */
878
14.8k
  if (need_rescale) {
879
14.6k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
14.6k
    source->rescale = (_JSAMPLE *)
883
14.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
14.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
14.6k
                                           sizeof(_JSAMPLE)));
886
14.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
14.6k
                                        sizeof(_JSAMPLE)));
888
14.6k
    half_maxval = maxval / 2;
889
23.7M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
23.7M
      source->rescale[val] =
892
23.7M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
23.7M
                   maxval);
894
23.7M
    }
895
14.6k
  }
896
14.8k
}
Unexecuted instantiation: rdppm-12.c:start_input_ppm
Unexecuted instantiation: rdppm-16.c:start_input_ppm
897
898
899
/*
900
 * Finish up at the end of the file.
901
 */
902
903
METHODDEF(void)
904
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
905
9.83k
{
906
  /* no work */
907
9.83k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
905
9.83k
{
906
  /* no work */
907
9.83k
}
Unexecuted instantiation: rdppm-12.c:finish_input_ppm
Unexecuted instantiation: rdppm-16.c:finish_input_ppm
908
909
910
/*
911
 * The module selection routine for PPM format input.
912
 */
913
914
GLOBAL(cjpeg_source_ptr)
915
_jinit_read_ppm(j_compress_ptr cinfo)
916
17.0k
{
917
17.0k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
17.0k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
921
#else
922
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
924
0
#endif
925
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
926
927
  /* Create module interface object */
928
17.0k
  source = (ppm_source_ptr)
929
17.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
17.0k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
17.0k
  source->pub.start_input = start_input_ppm;
933
17.0k
  source->pub.finish_input = finish_input_ppm;
934
17.0k
  source->pub.max_pixels = 0;
935
936
17.0k
  return (cjpeg_source_ptr)source;
937
17.0k
}
jinit_read_ppm
Line
Count
Source
916
17.0k
{
917
17.0k
  ppm_source_ptr source;
918
919
17.0k
#if BITS_IN_JSAMPLE == 8
920
17.0k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
921
#else
922
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
924
#endif
925
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
926
927
  /* Create module interface object */
928
17.0k
  source = (ppm_source_ptr)
929
17.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
17.0k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
17.0k
  source->pub.start_input = start_input_ppm;
933
17.0k
  source->pub.finish_input = finish_input_ppm;
934
17.0k
  source->pub.max_pixels = 0;
935
936
17.0k
  return (cjpeg_source_ptr)source;
937
17.0k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
938
939
#endif /* defined(PPM_SUPPORTED) &&
940
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */