Coverage Report

Created: 2026-01-10 06:49

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
41.3M
  (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
308k
{
80
308k
  register int ch;
81
82
308k
  ch = getc(infile);
83
308k
  if (ch == '#') {
84
59.1k
    do {
85
59.1k
      ch = getc(infile);
86
59.1k
    } while (ch != '\n' && ch != EOF);
87
4.00k
  }
88
308k
  return ch;
89
308k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
308k
{
80
308k
  register int ch;
81
82
308k
  ch = getc(infile);
83
308k
  if (ch == '#') {
84
59.1k
    do {
85
59.1k
      ch = getc(infile);
86
59.1k
    } while (ch != '\n' && ch != EOF);
87
4.00k
  }
88
308k
  return ch;
89
308k
}
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
127k
{
99
127k
  register int ch;
100
127k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
134k
  do {
104
134k
    ch = pbm_getc(infile);
105
134k
    if (ch == EOF)
106
2.57k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
134k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
127k
  if (ch < '0' || ch > '9')
110
206
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
127k
  val = ch - '0';
113
177k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
50.3k
    val *= 10;
115
50.3k
    val += ch - '0';
116
50.3k
    if (val > maxval)
117
132
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
50.3k
  }
119
120
127k
  return val;
121
127k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
127k
{
99
127k
  register int ch;
100
127k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
134k
  do {
104
134k
    ch = pbm_getc(infile);
105
134k
    if (ch == EOF)
106
2.57k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
134k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
127k
  if (ch < '0' || ch > '9')
110
206
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
127k
  val = ch - '0';
113
177k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
50.3k
    val *= 10;
115
50.3k
    val += ch - '0';
116
50.3k
    if (val > maxval)
117
132
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
50.3k
  }
119
120
127k
  return val;
121
127k
}
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.27k
{
140
1.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.27k
  FILE *infile = source->pub.input_file;
142
1.27k
  register _JSAMPROW ptr;
143
1.27k
  register _JSAMPLE *rescale = source->rescale;
144
1.27k
  JDIMENSION col;
145
1.27k
  unsigned int maxval = source->maxval;
146
147
1.27k
  ptr = source->pub._buffer[0];
148
3.77k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.50k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.50k
  }
151
1.27k
  return 1;
152
1.27k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
1.27k
{
140
1.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.27k
  FILE *infile = source->pub.input_file;
142
1.27k
  register _JSAMPROW ptr;
143
1.27k
  register _JSAMPLE *rescale = source->rescale;
144
1.27k
  JDIMENSION col;
145
1.27k
  unsigned int maxval = source->maxval;
146
147
1.27k
  ptr = source->pub._buffer[0];
148
3.77k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.50k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.50k
  }
151
1.27k
  return 1;
152
1.27k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
29.4M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
72.0M
  for (col = cinfo->image_width; col > 0; col--) { \
157
42.5M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
42.5M
    alpha_set_op \
159
42.5M
    ptr += ps; \
160
42.5M
  } \
161
29.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
6.35k
{
168
6.35k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
6.35k
  FILE *infile = source->pub.input_file;
170
6.35k
  register _JSAMPROW ptr;
171
6.35k
  register _JSAMPLE *rescale = source->rescale;
172
6.35k
  JDIMENSION col;
173
6.35k
  unsigned int maxval = source->maxval;
174
6.35k
  register int rindex = rgb_red[cinfo->in_color_space];
175
6.35k
  register int gindex = rgb_green[cinfo->in_color_space];
176
6.35k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
6.35k
  register int aindex = alpha_index[cinfo->in_color_space];
178
6.35k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
6.35k
  ptr = source->pub._buffer[0];
181
6.35k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.25k
    if (aindex >= 0)
183
251
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.25k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.00k
    else
186
1.00k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.10k
  } else {
188
5.10k
    if (aindex >= 0)
189
1.02k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.10k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
4.08k
    else
192
4.08k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.10k
  }
194
6.35k
  return 1;
195
6.35k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
6.35k
{
168
6.35k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
6.35k
  FILE *infile = source->pub.input_file;
170
6.35k
  register _JSAMPROW ptr;
171
6.35k
  register _JSAMPLE *rescale = source->rescale;
172
6.35k
  JDIMENSION col;
173
6.35k
  unsigned int maxval = source->maxval;
174
6.35k
  register int rindex = rgb_red[cinfo->in_color_space];
175
6.35k
  register int gindex = rgb_green[cinfo->in_color_space];
176
6.35k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
6.35k
  register int aindex = alpha_index[cinfo->in_color_space];
178
6.35k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
6.35k
  ptr = source->pub._buffer[0];
181
6.35k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.25k
    if (aindex >= 0)
183
251
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.25k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.00k
    else
186
1.00k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.10k
  } else {
188
5.10k
    if (aindex >= 0)
189
1.02k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.10k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
4.08k
    else
192
4.08k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.10k
  }
194
6.35k
  return 1;
195
6.35k
}
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.27k
{
203
1.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.27k
  FILE *infile = source->pub.input_file;
205
1.27k
  register _JSAMPROW ptr;
206
1.27k
  register _JSAMPLE *rescale = source->rescale;
207
1.27k
  JDIMENSION col;
208
1.27k
  unsigned int maxval = source->maxval;
209
210
1.27k
  ptr = source->pub._buffer[0];
211
1.27k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
900
    for (col = cinfo->image_width; col > 0; col--) {
213
649
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
649
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
649
      ptr += 4;
216
649
    }
217
1.02k
  } else {
218
2.87k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.85k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.85k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
1.85k
                  ptr + 1, ptr + 2, ptr + 3);
222
1.85k
      ptr += 4;
223
1.85k
    }
224
1.02k
  }
225
1.27k
  return 1;
226
1.27k
}
rdppm-8.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.27k
{
203
1.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.27k
  FILE *infile = source->pub.input_file;
205
1.27k
  register _JSAMPROW ptr;
206
1.27k
  register _JSAMPLE *rescale = source->rescale;
207
1.27k
  JDIMENSION col;
208
1.27k
  unsigned int maxval = source->maxval;
209
210
1.27k
  ptr = source->pub._buffer[0];
211
1.27k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
900
    for (col = cinfo->image_width; col > 0; col--) {
213
649
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
649
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
649
      ptr += 4;
216
649
    }
217
1.02k
  } else {
218
2.87k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.85k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.85k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
1.85k
                  ptr + 1, ptr + 2, ptr + 3);
222
1.85k
      ptr += 4;
223
1.85k
    }
224
1.02k
  }
225
1.27k
  return 1;
226
1.27k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row
227
228
229
59.5k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
1.92M
  for (col = cinfo->image_width; col > 0; col--) { \
231
1.86M
    ptr[rindex] = read_op; \
232
1.86M
    ptr[gindex] = read_op; \
233
1.86M
    ptr[bindex] = read_op; \
234
1.86M
    alpha_set_op \
235
1.86M
    ptr += ps; \
236
1.86M
  } \
237
59.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
8.75k
{
243
8.75k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.75k
  FILE *infile = source->pub.input_file;
245
8.75k
  register _JSAMPROW ptr;
246
8.75k
  register _JSAMPLE *rescale = source->rescale;
247
8.75k
  JDIMENSION col;
248
8.75k
  unsigned int maxval = source->maxval;
249
8.75k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.75k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.75k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.75k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.75k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.75k
  ptr = source->pub._buffer[0];
256
8.75k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.63k
    if (aindex >= 0)
258
527
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.63k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.10k
    else
261
2.10k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
6.12k
  } else {
263
6.12k
    if (aindex >= 0)
264
1.22k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.12k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
4.89k
    else
267
4.89k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.12k
  }
269
8.75k
  return 1;
270
8.75k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
242
8.75k
{
243
8.75k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.75k
  FILE *infile = source->pub.input_file;
245
8.75k
  register _JSAMPROW ptr;
246
8.75k
  register _JSAMPLE *rescale = source->rescale;
247
8.75k
  JDIMENSION col;
248
8.75k
  unsigned int maxval = source->maxval;
249
8.75k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.75k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.75k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.75k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.75k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.75k
  ptr = source->pub._buffer[0];
256
8.75k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.63k
    if (aindex >= 0)
258
527
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.63k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.10k
    else
261
2.10k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
6.12k
  } else {
263
6.12k
    if (aindex >= 0)
264
1.22k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.12k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
4.89k
    else
267
4.89k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.12k
  }
269
8.75k
  return 1;
270
8.75k
}
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.75k
{
278
1.75k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
1.75k
  FILE *infile = source->pub.input_file;
280
1.75k
  register _JSAMPROW ptr;
281
1.75k
  register _JSAMPLE *rescale = source->rescale;
282
1.75k
  JDIMENSION col;
283
1.75k
  unsigned int maxval = source->maxval;
284
285
1.75k
  ptr = source->pub._buffer[0];
286
1.75k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
1.51k
    for (col = cinfo->image_width; col > 0; col--) {
288
985
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
985
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
985
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
985
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
985
      ptr += 4;
293
985
    }
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.75k
  return 1;
305
1.75k
}
rdppm-8.c:get_text_rgb_cmyk_row
Line
Count
Source
277
1.75k
{
278
1.75k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
1.75k
  FILE *infile = source->pub.input_file;
280
1.75k
  register _JSAMPROW ptr;
281
1.75k
  register _JSAMPLE *rescale = source->rescale;
282
1.75k
  JDIMENSION col;
283
1.75k
  unsigned int maxval = source->maxval;
284
285
1.75k
  ptr = source->pub._buffer[0];
286
1.75k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
1.51k
    for (col = cinfo->image_width; col > 0; col--) {
288
985
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
985
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
985
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
985
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
985
      ptr += 4;
293
985
    }
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.75k
  return 1;
305
1.75k
}
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
5.66M
{
312
5.66M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
5.66M
  register _JSAMPROW ptr;
314
5.66M
  register U_CHAR *bufferptr;
315
5.66M
  register _JSAMPLE *rescale = source->rescale;
316
5.66M
  JDIMENSION col;
317
318
5.66M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
60
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
5.66M
  ptr = source->pub._buffer[0];
321
5.66M
  bufferptr = source->iobuffer;
322
13.6M
  for (col = cinfo->image_width; col > 0; col--) {
323
7.99M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
7.99M
  }
325
5.66M
  return 1;
326
5.66M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
311
5.66M
{
312
5.66M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
5.66M
  register _JSAMPROW ptr;
314
5.66M
  register U_CHAR *bufferptr;
315
5.66M
  register _JSAMPLE *rescale = source->rescale;
316
5.66M
  JDIMENSION col;
317
318
5.66M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
60
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
5.66M
  ptr = source->pub._buffer[0];
321
5.66M
  bufferptr = source->iobuffer;
322
13.6M
  for (col = cinfo->image_width; col > 0; col--) {
323
7.99M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
7.99M
  }
325
5.66M
  return 1;
326
5.66M
}
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
29.4M
{
334
29.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
29.4M
  register _JSAMPROW ptr;
336
29.4M
  register U_CHAR *bufferptr;
337
29.4M
  register _JSAMPLE *rescale = source->rescale;
338
29.4M
  JDIMENSION col;
339
29.4M
  unsigned int maxval = source->maxval;
340
29.4M
  register int rindex = rgb_red[cinfo->in_color_space];
341
29.4M
  register int gindex = rgb_green[cinfo->in_color_space];
342
29.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
29.4M
  register int aindex = alpha_index[cinfo->in_color_space];
344
29.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
29.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
415
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
29.4M
  ptr = source->pub._buffer[0];
349
29.4M
  bufferptr = source->iobuffer;
350
29.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
1.12M
    if (aindex >= 0)
352
225k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
902k
    else
354
902k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
28.3M
  } else {
356
28.3M
    if (aindex >= 0)
357
5.66M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
28.3M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
22.6M
    else
360
22.6M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
28.3M
  }
362
29.4M
  return 1;
363
29.4M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
333
29.4M
{
334
29.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
29.4M
  register _JSAMPROW ptr;
336
29.4M
  register U_CHAR *bufferptr;
337
29.4M
  register _JSAMPLE *rescale = source->rescale;
338
29.4M
  JDIMENSION col;
339
29.4M
  unsigned int maxval = source->maxval;
340
29.4M
  register int rindex = rgb_red[cinfo->in_color_space];
341
29.4M
  register int gindex = rgb_green[cinfo->in_color_space];
342
29.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
29.4M
  register int aindex = alpha_index[cinfo->in_color_space];
344
29.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
29.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
415
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
29.4M
  ptr = source->pub._buffer[0];
349
29.4M
  bufferptr = source->iobuffer;
350
29.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
1.12M
    if (aindex >= 0)
352
225k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
902k
    else
354
902k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
28.3M
  } else {
356
28.3M
    if (aindex >= 0)
357
5.66M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
28.3M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
22.6M
    else
360
22.6M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
28.3M
  }
362
29.4M
  return 1;
363
29.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
5.89M
{
371
5.89M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
5.89M
  register _JSAMPROW ptr;
373
5.89M
  register U_CHAR *bufferptr;
374
5.89M
  register _JSAMPLE *rescale = source->rescale;
375
5.89M
  JDIMENSION col;
376
5.89M
  unsigned int maxval = source->maxval;
377
378
5.89M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
83
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
5.89M
  ptr = source->pub._buffer[0];
381
5.89M
  bufferptr = source->iobuffer;
382
5.89M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
745k
    for (col = cinfo->image_width; col > 0; col--) {
384
519k
      _JSAMPLE gray = *bufferptr++;
385
519k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
519k
      ptr += 4;
387
519k
    }
388
5.66M
  } else {
389
13.6M
    for (col = cinfo->image_width; col > 0; col--) {
390
7.99M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
7.99M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
7.99M
                  ptr + 1, ptr + 2, ptr + 3);
393
7.99M
      ptr += 4;
394
7.99M
    }
395
5.66M
  }
396
5.89M
  return 1;
397
5.89M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
370
5.89M
{
371
5.89M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
5.89M
  register _JSAMPROW ptr;
373
5.89M
  register U_CHAR *bufferptr;
374
5.89M
  register _JSAMPLE *rescale = source->rescale;
375
5.89M
  JDIMENSION col;
376
5.89M
  unsigned int maxval = source->maxval;
377
378
5.89M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
83
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
5.89M
  ptr = source->pub._buffer[0];
381
5.89M
  bufferptr = source->iobuffer;
382
5.89M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
745k
    for (col = cinfo->image_width; col > 0; col--) {
384
519k
      _JSAMPLE gray = *bufferptr++;
385
519k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
519k
      ptr += 4;
387
519k
    }
388
5.66M
  } else {
389
13.6M
    for (col = cinfo->image_width; col > 0; col--) {
390
7.99M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
7.99M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
7.99M
                  ptr + 1, ptr + 2, ptr + 3);
393
7.99M
      ptr += 4;
394
7.99M
    }
395
5.66M
  }
396
5.89M
  return 1;
397
5.89M
}
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
50.7k
{
404
50.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
50.7k
  register _JSAMPROW ptr;
406
50.7k
  register U_CHAR *bufferptr;
407
50.7k
  register _JSAMPLE *rescale = source->rescale;
408
50.7k
  JDIMENSION col;
409
50.7k
  unsigned int maxval = source->maxval;
410
50.7k
  register int rindex = rgb_red[cinfo->in_color_space];
411
50.7k
  register int gindex = rgb_green[cinfo->in_color_space];
412
50.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
50.7k
  register int aindex = alpha_index[cinfo->in_color_space];
414
50.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
50.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
729
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
50.7k
  ptr = source->pub._buffer[0];
419
50.7k
  bufferptr = source->iobuffer;
420
50.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
22.0k
    if (aindex >= 0)
422
5.51k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
16.5k
    else
424
16.5k
      RGB_READ_LOOP(*bufferptr++, {})
425
28.7k
  } else {
426
28.7k
    if (aindex >= 0)
427
5.59k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
28.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
23.1k
    else
430
23.1k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
28.7k
  }
432
50.7k
  return 1;
433
50.7k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
403
50.7k
{
404
50.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
50.7k
  register _JSAMPROW ptr;
406
50.7k
  register U_CHAR *bufferptr;
407
50.7k
  register _JSAMPLE *rescale = source->rescale;
408
50.7k
  JDIMENSION col;
409
50.7k
  unsigned int maxval = source->maxval;
410
50.7k
  register int rindex = rgb_red[cinfo->in_color_space];
411
50.7k
  register int gindex = rgb_green[cinfo->in_color_space];
412
50.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
50.7k
  register int aindex = alpha_index[cinfo->in_color_space];
414
50.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
50.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
729
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
50.7k
  ptr = source->pub._buffer[0];
419
50.7k
  bufferptr = source->iobuffer;
420
50.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
22.0k
    if (aindex >= 0)
422
5.51k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
16.5k
    else
424
16.5k
      RGB_READ_LOOP(*bufferptr++, {})
425
28.7k
  } else {
426
28.7k
    if (aindex >= 0)
427
5.59k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
28.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
23.1k
    else
430
23.1k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
28.7k
  }
432
50.7k
  return 1;
433
50.7k
}
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
11.2k
{
441
11.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
11.2k
  register _JSAMPROW ptr;
443
11.2k
  register U_CHAR *bufferptr;
444
11.2k
  register _JSAMPLE *rescale = source->rescale;
445
11.2k
  JDIMENSION col;
446
11.2k
  unsigned int maxval = source->maxval;
447
448
11.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
157
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
11.2k
  ptr = source->pub._buffer[0];
451
11.2k
  bufferptr = source->iobuffer;
452
11.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
235k
    for (col = cinfo->image_width; col > 0; col--) {
454
230k
      _JSAMPLE r = *bufferptr++;
455
230k
      _JSAMPLE g = *bufferptr++;
456
230k
      _JSAMPLE b = *bufferptr++;
457
230k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
230k
      ptr += 4;
459
230k
    }
460
5.75k
  } else {
461
190k
    for (col = cinfo->image_width; col > 0; col--) {
462
184k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
184k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
184k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
184k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
184k
                  ptr + 2, ptr + 3);
467
184k
      ptr += 4;
468
184k
    }
469
5.75k
  }
470
11.2k
  return 1;
471
11.2k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
440
11.2k
{
441
11.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
11.2k
  register _JSAMPROW ptr;
443
11.2k
  register U_CHAR *bufferptr;
444
11.2k
  register _JSAMPLE *rescale = source->rescale;
445
11.2k
  JDIMENSION col;
446
11.2k
  unsigned int maxval = source->maxval;
447
448
11.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
157
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
11.2k
  ptr = source->pub._buffer[0];
451
11.2k
  bufferptr = source->iobuffer;
452
11.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
235k
    for (col = cinfo->image_width; col > 0; col--) {
454
230k
      _JSAMPLE r = *bufferptr++;
455
230k
      _JSAMPLE g = *bufferptr++;
456
230k
      _JSAMPLE b = *bufferptr++;
457
230k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
230k
      ptr += 4;
459
230k
    }
460
5.75k
  } else {
461
190k
    for (col = cinfo->image_width; col > 0; col--) {
462
184k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
184k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
184k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
184k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
184k
                  ptr + 2, ptr + 3);
467
184k
      ptr += 4;
468
184k
    }
469
5.75k
  }
470
11.2k
  return 1;
471
11.2k
}
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
231k
{
482
231k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
231k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
79
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
231k
  return 1;
487
231k
}
rdppm-8.c:get_raw_row
Line
Count
Source
481
231k
{
482
231k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
231k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
79
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
231k
  return 1;
487
231k
}
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.56k
{
494
1.56k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
1.56k
  register _JSAMPROW ptr;
496
1.56k
  register U_CHAR *bufferptr;
497
1.56k
  register _JSAMPLE *rescale = source->rescale;
498
1.56k
  JDIMENSION col;
499
1.56k
  unsigned int maxval = source->maxval;
500
501
1.56k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
63
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.56k
  ptr = source->pub._buffer[0];
504
1.56k
  bufferptr = source->iobuffer;
505
69.5k
  for (col = cinfo->image_width; col > 0; col--) {
506
68.0k
    register unsigned int temp;
507
68.0k
    temp  = UCH(*bufferptr++) << 8;
508
68.0k
    temp |= UCH(*bufferptr++);
509
68.0k
    if (temp > maxval)
510
40
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
68.0k
    *ptr++ = rescale[temp];
512
68.0k
  }
513
1.56k
  return 1;
514
1.56k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
493
1.56k
{
494
1.56k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
1.56k
  register _JSAMPROW ptr;
496
1.56k
  register U_CHAR *bufferptr;
497
1.56k
  register _JSAMPLE *rescale = source->rescale;
498
1.56k
  JDIMENSION col;
499
1.56k
  unsigned int maxval = source->maxval;
500
501
1.56k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
63
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.56k
  ptr = source->pub._buffer[0];
504
1.56k
  bufferptr = source->iobuffer;
505
69.5k
  for (col = cinfo->image_width; col > 0; col--) {
506
68.0k
    register unsigned int temp;
507
68.0k
    temp  = UCH(*bufferptr++) << 8;
508
68.0k
    temp |= UCH(*bufferptr++);
509
68.0k
    if (temp > maxval)
510
40
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
68.0k
    *ptr++ = rescale[temp];
512
68.0k
  }
513
1.56k
  return 1;
514
1.56k
}
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
7.80k
{
521
7.80k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
7.80k
  register _JSAMPROW ptr;
523
7.80k
  register U_CHAR *bufferptr;
524
7.80k
  register _JSAMPLE *rescale = source->rescale;
525
7.80k
  JDIMENSION col;
526
7.80k
  unsigned int maxval = source->maxval;
527
7.80k
  register int rindex = rgb_red[cinfo->in_color_space];
528
7.80k
  register int gindex = rgb_green[cinfo->in_color_space];
529
7.80k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
7.80k
  register int aindex = alpha_index[cinfo->in_color_space];
531
7.80k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
7.80k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
315
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
7.80k
  ptr = source->pub._buffer[0];
536
7.80k
  bufferptr = source->iobuffer;
537
347k
  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
200
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
340k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
340k
    if (aindex >= 0)
545
67.9k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
340k
    ptr += ps;
547
340k
  }
548
7.80k
  return 1;
549
7.80k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
520
7.80k
{
521
7.80k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
7.80k
  register _JSAMPROW ptr;
523
7.80k
  register U_CHAR *bufferptr;
524
7.80k
  register _JSAMPLE *rescale = source->rescale;
525
7.80k
  JDIMENSION col;
526
7.80k
  unsigned int maxval = source->maxval;
527
7.80k
  register int rindex = rgb_red[cinfo->in_color_space];
528
7.80k
  register int gindex = rgb_green[cinfo->in_color_space];
529
7.80k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
7.80k
  register int aindex = alpha_index[cinfo->in_color_space];
531
7.80k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
7.80k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
315
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
7.80k
  ptr = source->pub._buffer[0];
536
7.80k
  bufferptr = source->iobuffer;
537
347k
  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
200
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
340k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
340k
    if (aindex >= 0)
545
67.9k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
340k
    ptr += ps;
547
340k
  }
548
7.80k
  return 1;
549
7.80k
}
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.56k
{
556
1.56k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
1.56k
  register _JSAMPROW ptr;
558
1.56k
  register U_CHAR *bufferptr;
559
1.56k
  register _JSAMPLE *rescale = source->rescale;
560
1.56k
  JDIMENSION col;
561
1.56k
  unsigned int maxval = source->maxval;
562
563
1.56k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
63
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
1.56k
  ptr = source->pub._buffer[0];
566
1.56k
  bufferptr = source->iobuffer;
567
1.56k
  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.56k
  } else {
579
69.5k
    for (col = cinfo->image_width; col > 0; col--) {
580
68.0k
      register unsigned int temp;
581
68.0k
      _JSAMPLE gray;
582
68.0k
      temp  = UCH(*bufferptr++) << 8;
583
68.0k
      temp |= UCH(*bufferptr++);
584
68.0k
      if (temp > maxval)
585
40
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
68.0k
      gray = rescale[temp];
587
68.0k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
68.0k
                  ptr + 1, ptr + 2, ptr + 3);
589
68.0k
      ptr += 4;
590
68.0k
    }
591
1.56k
  }
592
1.56k
  return 1;
593
1.56k
}
rdppm-8.c:get_word_gray_cmyk_row
Line
Count
Source
555
1.56k
{
556
1.56k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
1.56k
  register _JSAMPROW ptr;
558
1.56k
  register U_CHAR *bufferptr;
559
1.56k
  register _JSAMPLE *rescale = source->rescale;
560
1.56k
  JDIMENSION col;
561
1.56k
  unsigned int maxval = source->maxval;
562
563
1.56k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
63
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
1.56k
  ptr = source->pub._buffer[0];
566
1.56k
  bufferptr = source->iobuffer;
567
1.56k
  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.56k
  } else {
579
69.5k
    for (col = cinfo->image_width; col > 0; col--) {
580
68.0k
      register unsigned int temp;
581
68.0k
      _JSAMPLE gray;
582
68.0k
      temp  = UCH(*bufferptr++) << 8;
583
68.0k
      temp |= UCH(*bufferptr++);
584
68.0k
      if (temp > maxval)
585
40
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
68.0k
      gray = rescale[temp];
587
68.0k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
68.0k
                  ptr + 1, ptr + 2, ptr + 3);
589
68.0k
      ptr += 4;
590
68.0k
    }
591
1.56k
  }
592
1.56k
  return 1;
593
1.56k
}
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
350
    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
125
      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
120
      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
350
    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
125
      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
120
      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
70
    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
25
        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
24
        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
70
    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
25
        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
24
        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
20.0k
{
706
20.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
20.0k
  int c;
708
20.0k
  unsigned int w, h, maxval;
709
20.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
20.0k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
20.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
20.0k
  switch (c) {
718
1.19k
  case '2':                     /* it's a text-format PGM file */
719
2.62k
  case '3':                     /* it's a text-format PPM file */
720
15.7k
  case '5':                     /* it's a raw-format PGM file */
721
20.0k
  case '6':                     /* it's a raw-format PPM file */
722
20.0k
    break;
723
7
  default:
724
7
    ERREXIT(cinfo, JERR_PPM_NOT);
725
7
    break;
726
20.0k
  }
727
728
  /* fetch the remaining header info */
729
20.0k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
20.0k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
20.0k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
20.0k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
35
    ERREXIT(cinfo, JERR_PPM_NOT);
735
20.0k
  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
20.0k
  cinfo->image_width = (JDIMENSION)w;
739
20.0k
  cinfo->image_height = (JDIMENSION)h;
740
20.0k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
20.0k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
20.0k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
20.0k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
20.0k
  switch (c) {
748
917
  case '2':                     /* it's a text-format PGM file */
749
917
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
917
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
917
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
917
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
131
      source->pub.get_pixel_rows = get_text_gray_row;
755
786
    else if (IsExtRGB(cinfo->in_color_space))
756
655
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
131
    else if (cinfo->in_color_space == JCS_CMYK)
758
131
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
917
    need_iobuffer = FALSE;
762
917
    break;
763
764
1.16k
  case '3':                     /* it's a text-format PPM file */
765
1.16k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.16k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.16k
    if (IsExtRGB(cinfo->in_color_space))
769
835
      source->pub.get_pixel_rows = get_text_rgb_row;
770
334
    else if (cinfo->in_color_space == JCS_CMYK)
771
167
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
167
    else
773
167
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.16k
    need_iobuffer = FALSE;
775
1.16k
    break;
776
777
12.6k
  case '5':                     /* it's a raw-format PGM file */
778
12.6k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
12.6k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
12.6k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
12.6k
    if (maxval > 255) {
783
791
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
113
        source->pub.get_pixel_rows = get_word_gray_row;
785
678
      else if (IsExtRGB(cinfo->in_color_space))
786
565
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
113
      else if (cinfo->in_color_space == JCS_CMYK)
788
113
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
11.8k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
11.8k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
882
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
126
      source->pub.get_pixel_rows = get_raw_row;
795
126
      use_raw_buffer = TRUE;
796
126
      need_rescale = FALSE;
797
11.7k
    } else {
798
11.7k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.56k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
10.1k
      else if (IsExtRGB(cinfo->in_color_space))
801
8.47k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
1.69k
      else if (cinfo->in_color_space == JCS_CMYK)
803
1.69k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
11.7k
    }
807
12.6k
    break;
808
809
3.89k
  case '6':                     /* it's a raw-format PPM file */
810
3.89k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
3.89k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
3.89k
    if (maxval > 255) {
814
1.11k
      if (IsExtRGB(cinfo->in_color_space))
815
795
        source->pub.get_pixel_rows = get_word_rgb_row;
816
318
      else if (cinfo->in_color_space == JCS_CMYK)
817
159
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
159
      else
819
159
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
2.78k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
2.78k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
833
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
833
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
714
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
119
      source->pub.get_pixel_rows = get_raw_row;
829
119
      use_raw_buffer = TRUE;
830
119
      need_rescale = FALSE;
831
2.66k
    } else {
832
2.66k
      if (IsExtRGB(cinfo->in_color_space))
833
1.87k
        source->pub.get_pixel_rows = get_rgb_row;
834
796
      else if (cinfo->in_color_space == JCS_CMYK)
835
398
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
398
      else
837
398
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
2.66k
    }
839
3.89k
    break;
840
20.0k
  }
841
842
17.9k
  if (IsExtRGB(cinfo->in_color_space))
843
13.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
4.60k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.93k
    cinfo->input_components = 1;
846
2.66k
  else if (cinfo->in_color_space == JCS_CMYK)
847
2.66k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
17.9k
  if (need_iobuffer) {
851
15.9k
    if (c == '6')
852
3.34k
      source->buffer_width = (size_t)w * 3 *
853
3.34k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
12.6k
    else
855
12.6k
      source->buffer_width = (size_t)w *
856
12.6k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
15.9k
    source->iobuffer = (U_CHAR *)
858
15.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
15.9k
                                  source->buffer_width);
860
15.9k
  }
861
862
  /* Create compressor input buffer. */
863
17.9k
  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
245
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
245
    source->pub._buffer = &source->pixrow;
868
245
    source->pub.buffer_height = 1;
869
17.6k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
17.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
17.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
17.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
17.6k
    source->pub.buffer_height = 1;
875
17.6k
  }
876
877
  /* Compute the rescaling array if required. */
878
17.9k
  if (need_rescale) {
879
17.6k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
17.6k
    source->rescale = (_JSAMPLE *)
883
17.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
17.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
17.6k
                                           sizeof(_JSAMPLE)));
886
17.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
17.6k
                                        sizeof(_JSAMPLE)));
888
17.6k
    half_maxval = maxval / 2;
889
23.1M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
23.1M
      source->rescale[val] =
892
23.1M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
23.1M
                   maxval);
894
23.1M
    }
895
17.6k
  }
896
17.9k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
705
20.0k
{
706
20.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
20.0k
  int c;
708
20.0k
  unsigned int w, h, maxval;
709
20.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
20.0k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
20.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
20.0k
  switch (c) {
718
1.19k
  case '2':                     /* it's a text-format PGM file */
719
2.62k
  case '3':                     /* it's a text-format PPM file */
720
15.7k
  case '5':                     /* it's a raw-format PGM file */
721
20.0k
  case '6':                     /* it's a raw-format PPM file */
722
20.0k
    break;
723
7
  default:
724
7
    ERREXIT(cinfo, JERR_PPM_NOT);
725
7
    break;
726
20.0k
  }
727
728
  /* fetch the remaining header info */
729
20.0k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
20.0k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
20.0k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
20.0k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
35
    ERREXIT(cinfo, JERR_PPM_NOT);
735
20.0k
  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
20.0k
  cinfo->image_width = (JDIMENSION)w;
739
20.0k
  cinfo->image_height = (JDIMENSION)h;
740
20.0k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
20.0k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
20.0k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
20.0k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
20.0k
  switch (c) {
748
917
  case '2':                     /* it's a text-format PGM file */
749
917
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
917
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
917
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
917
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
131
      source->pub.get_pixel_rows = get_text_gray_row;
755
786
    else if (IsExtRGB(cinfo->in_color_space))
756
655
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
131
    else if (cinfo->in_color_space == JCS_CMYK)
758
131
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
917
    need_iobuffer = FALSE;
762
917
    break;
763
764
1.16k
  case '3':                     /* it's a text-format PPM file */
765
1.16k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.16k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.16k
    if (IsExtRGB(cinfo->in_color_space))
769
835
      source->pub.get_pixel_rows = get_text_rgb_row;
770
334
    else if (cinfo->in_color_space == JCS_CMYK)
771
167
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
167
    else
773
167
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.16k
    need_iobuffer = FALSE;
775
1.16k
    break;
776
777
12.6k
  case '5':                     /* it's a raw-format PGM file */
778
12.6k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
12.6k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
12.6k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
12.6k
    if (maxval > 255) {
783
791
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
113
        source->pub.get_pixel_rows = get_word_gray_row;
785
678
      else if (IsExtRGB(cinfo->in_color_space))
786
565
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
113
      else if (cinfo->in_color_space == JCS_CMYK)
788
113
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
11.8k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
11.8k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
882
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
126
      source->pub.get_pixel_rows = get_raw_row;
795
126
      use_raw_buffer = TRUE;
796
126
      need_rescale = FALSE;
797
11.7k
    } else {
798
11.7k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.56k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
10.1k
      else if (IsExtRGB(cinfo->in_color_space))
801
8.47k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
1.69k
      else if (cinfo->in_color_space == JCS_CMYK)
803
1.69k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
11.7k
    }
807
12.6k
    break;
808
809
3.89k
  case '6':                     /* it's a raw-format PPM file */
810
3.89k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
3.89k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
3.89k
    if (maxval > 255) {
814
1.11k
      if (IsExtRGB(cinfo->in_color_space))
815
795
        source->pub.get_pixel_rows = get_word_rgb_row;
816
318
      else if (cinfo->in_color_space == JCS_CMYK)
817
159
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
159
      else
819
159
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
2.78k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
2.78k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
833
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
833
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
714
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
119
      source->pub.get_pixel_rows = get_raw_row;
829
119
      use_raw_buffer = TRUE;
830
119
      need_rescale = FALSE;
831
2.66k
    } else {
832
2.66k
      if (IsExtRGB(cinfo->in_color_space))
833
1.87k
        source->pub.get_pixel_rows = get_rgb_row;
834
796
      else if (cinfo->in_color_space == JCS_CMYK)
835
398
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
398
      else
837
398
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
2.66k
    }
839
3.89k
    break;
840
20.0k
  }
841
842
17.9k
  if (IsExtRGB(cinfo->in_color_space))
843
13.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
4.60k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.93k
    cinfo->input_components = 1;
846
2.66k
  else if (cinfo->in_color_space == JCS_CMYK)
847
2.66k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
17.9k
  if (need_iobuffer) {
851
15.9k
    if (c == '6')
852
3.34k
      source->buffer_width = (size_t)w * 3 *
853
3.34k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
12.6k
    else
855
12.6k
      source->buffer_width = (size_t)w *
856
12.6k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
15.9k
    source->iobuffer = (U_CHAR *)
858
15.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
15.9k
                                  source->buffer_width);
860
15.9k
  }
861
862
  /* Create compressor input buffer. */
863
17.9k
  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
245
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
245
    source->pub._buffer = &source->pixrow;
868
245
    source->pub.buffer_height = 1;
869
17.6k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
17.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
17.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
17.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
17.6k
    source->pub.buffer_height = 1;
875
17.6k
  }
876
877
  /* Compute the rescaling array if required. */
878
17.9k
  if (need_rescale) {
879
17.6k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
17.6k
    source->rescale = (_JSAMPLE *)
883
17.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
17.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
17.6k
                                           sizeof(_JSAMPLE)));
886
17.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
17.6k
                                        sizeof(_JSAMPLE)));
888
17.6k
    half_maxval = maxval / 2;
889
23.1M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
23.1M
      source->rescale[val] =
892
23.1M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
23.1M
                   maxval);
894
23.1M
    }
895
17.6k
  }
896
17.9k
}
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
13.0k
{
906
  /* no work */
907
13.0k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
905
13.0k
{
906
  /* no work */
907
13.0k
}
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
20.0k
{
917
20.0k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
20.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
20.0k
  source = (ppm_source_ptr)
929
20.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
20.0k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
20.0k
  source->pub.start_input = start_input_ppm;
933
20.0k
  source->pub.finish_input = finish_input_ppm;
934
20.0k
  source->pub.max_pixels = 0;
935
936
20.0k
  return (cjpeg_source_ptr)source;
937
20.0k
}
jinit_read_ppm
Line
Count
Source
916
20.0k
{
917
20.0k
  ppm_source_ptr source;
918
919
20.0k
#if BITS_IN_JSAMPLE == 8
920
20.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
20.0k
  source = (ppm_source_ptr)
929
20.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
20.0k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
20.0k
  source->pub.start_input = start_input_ppm;
933
20.0k
  source->pub.finish_input = finish_input_ppm;
934
20.0k
  source->pub.max_pixels = 0;
935
936
20.0k
  return (cjpeg_source_ptr)source;
937
20.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)) */