Coverage Report

Created: 2026-01-25 06:04

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.5M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
26.1M
  (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
274k
{
80
274k
  register int ch;
81
82
274k
  ch = getc(infile);
83
274k
  if (ch == '#') {
84
61.0k
    do {
85
61.0k
      ch = getc(infile);
86
61.0k
    } while (ch != '\n' && ch != EOF);
87
3.73k
  }
88
274k
  return ch;
89
274k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
274k
{
80
274k
  register int ch;
81
82
274k
  ch = getc(infile);
83
274k
  if (ch == '#') {
84
61.0k
    do {
85
61.0k
      ch = getc(infile);
86
61.0k
    } while (ch != '\n' && ch != EOF);
87
3.73k
  }
88
274k
  return ch;
89
274k
}
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
115k
{
99
115k
  register int ch;
100
115k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
122k
  do {
104
122k
    ch = pbm_getc(infile);
105
122k
    if (ch == EOF)
106
2.59k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
122k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
115k
  if (ch < '0' || ch > '9')
110
240
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
115k
  val = ch - '0';
113
154k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
39.8k
    val *= 10;
115
39.8k
    val += ch - '0';
116
39.8k
    if (val > maxval)
117
139
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
39.8k
  }
119
120
115k
  return val;
121
115k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
115k
{
99
115k
  register int ch;
100
115k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
122k
  do {
104
122k
    ch = pbm_getc(infile);
105
122k
    if (ch == EOF)
106
2.59k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
122k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
115k
  if (ch < '0' || ch > '9')
110
240
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
115k
  val = ch - '0';
113
154k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
39.8k
    val *= 10;
115
39.8k
    val += ch - '0';
116
39.8k
    if (val > maxval)
117
139
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
39.8k
  }
119
120
115k
  return val;
121
115k
}
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.28k
{
140
1.28k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.28k
  FILE *infile = source->pub.input_file;
142
1.28k
  register _JSAMPROW ptr;
143
1.28k
  register _JSAMPLE *rescale = source->rescale;
144
1.28k
  JDIMENSION col;
145
1.28k
  unsigned int maxval = source->maxval;
146
147
1.28k
  ptr = source->pub._buffer[0];
148
3.80k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.52k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.52k
  }
151
1.28k
  return 1;
152
1.28k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
1.28k
{
140
1.28k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.28k
  FILE *infile = source->pub.input_file;
142
1.28k
  register _JSAMPROW ptr;
143
1.28k
  register _JSAMPLE *rescale = source->rescale;
144
1.28k
  JDIMENSION col;
145
1.28k
  unsigned int maxval = source->maxval;
146
147
1.28k
  ptr = source->pub._buffer[0];
148
3.80k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.52k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.52k
  }
151
1.28k
  return 1;
152
1.28k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
18.6M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
60.5M
  for (col = cinfo->image_width; col > 0; col--) { \
157
41.9M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
41.9M
    alpha_set_op \
159
41.9M
    ptr += ps; \
160
41.9M
  } \
161
18.6M
}
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.42k
{
168
6.42k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
6.42k
  FILE *infile = source->pub.input_file;
170
6.42k
  register _JSAMPROW ptr;
171
6.42k
  register _JSAMPLE *rescale = source->rescale;
172
6.42k
  JDIMENSION col;
173
6.42k
  unsigned int maxval = source->maxval;
174
6.42k
  register int rindex = rgb_red[cinfo->in_color_space];
175
6.42k
  register int gindex = rgb_green[cinfo->in_color_space];
176
6.42k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
6.42k
  register int aindex = alpha_index[cinfo->in_color_space];
178
6.42k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
6.42k
  ptr = source->pub._buffer[0];
181
6.42k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.27k
    if (aindex >= 0)
183
254
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.27k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.01k
    else
186
1.01k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.15k
  } else {
188
5.15k
    if (aindex >= 0)
189
1.03k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.15k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
4.12k
    else
192
4.12k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.15k
  }
194
6.42k
  return 1;
195
6.42k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
6.42k
{
168
6.42k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
6.42k
  FILE *infile = source->pub.input_file;
170
6.42k
  register _JSAMPROW ptr;
171
6.42k
  register _JSAMPLE *rescale = source->rescale;
172
6.42k
  JDIMENSION col;
173
6.42k
  unsigned int maxval = source->maxval;
174
6.42k
  register int rindex = rgb_red[cinfo->in_color_space];
175
6.42k
  register int gindex = rgb_green[cinfo->in_color_space];
176
6.42k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
6.42k
  register int aindex = alpha_index[cinfo->in_color_space];
178
6.42k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
6.42k
  ptr = source->pub._buffer[0];
181
6.42k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.27k
    if (aindex >= 0)
183
254
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.27k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.01k
    else
186
1.01k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.15k
  } else {
188
5.15k
    if (aindex >= 0)
189
1.03k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.15k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
4.12k
    else
192
4.12k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.15k
  }
194
6.42k
  return 1;
195
6.42k
}
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.28k
{
203
1.28k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.28k
  FILE *infile = source->pub.input_file;
205
1.28k
  register _JSAMPROW ptr;
206
1.28k
  register _JSAMPLE *rescale = source->rescale;
207
1.28k
  JDIMENSION col;
208
1.28k
  unsigned int maxval = source->maxval;
209
210
1.28k
  ptr = source->pub._buffer[0];
211
1.28k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
910
    for (col = cinfo->image_width; col > 0; col--) {
213
656
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
656
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
656
      ptr += 4;
216
656
    }
217
1.03k
  } else {
218
2.89k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.86k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.86k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
1.86k
                  ptr + 1, ptr + 2, ptr + 3);
222
1.86k
      ptr += 4;
223
1.86k
    }
224
1.03k
  }
225
1.28k
  return 1;
226
1.28k
}
rdppm-8.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.28k
{
203
1.28k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.28k
  FILE *infile = source->pub.input_file;
205
1.28k
  register _JSAMPROW ptr;
206
1.28k
  register _JSAMPLE *rescale = source->rescale;
207
1.28k
  JDIMENSION col;
208
1.28k
  unsigned int maxval = source->maxval;
209
210
1.28k
  ptr = source->pub._buffer[0];
211
1.28k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
910
    for (col = cinfo->image_width; col > 0; col--) {
213
656
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
656
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
656
      ptr += 4;
216
656
    }
217
1.03k
  } else {
218
2.89k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.86k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.86k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
1.86k
                  ptr + 1, ptr + 2, ptr + 3);
222
1.86k
      ptr += 4;
223
1.86k
    }
224
1.03k
  }
225
1.28k
  return 1;
226
1.28k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row
227
228
229
73.2k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
1.94M
  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
73.2k
}
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.77k
{
243
8.77k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.77k
  FILE *infile = source->pub.input_file;
245
8.77k
  register _JSAMPROW ptr;
246
8.77k
  register _JSAMPLE *rescale = source->rescale;
247
8.77k
  JDIMENSION col;
248
8.77k
  unsigned int maxval = source->maxval;
249
8.77k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.77k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.77k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.77k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.77k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.77k
  ptr = source->pub._buffer[0];
256
8.77k
  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.13k
  } else {
263
6.13k
    if (aindex >= 0)
264
1.22k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.13k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
4.90k
    else
267
4.90k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.13k
  }
269
8.77k
  return 1;
270
8.77k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
242
8.77k
{
243
8.77k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.77k
  FILE *infile = source->pub.input_file;
245
8.77k
  register _JSAMPROW ptr;
246
8.77k
  register _JSAMPLE *rescale = source->rescale;
247
8.77k
  JDIMENSION col;
248
8.77k
  unsigned int maxval = source->maxval;
249
8.77k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.77k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.77k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.77k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.77k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.77k
  ptr = source->pub._buffer[0];
256
8.77k
  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.13k
  } else {
263
6.13k
    if (aindex >= 0)
264
1.22k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.13k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
4.90k
    else
267
4.90k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.13k
  }
269
8.77k
  return 1;
270
8.77k
}
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.20k
    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.20k
    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
3.60M
{
312
3.60M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
3.60M
  register _JSAMPROW ptr;
314
3.60M
  register U_CHAR *bufferptr;
315
3.60M
  register _JSAMPLE *rescale = source->rescale;
316
3.60M
  JDIMENSION col;
317
318
3.60M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
64
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
3.60M
  ptr = source->pub._buffer[0];
321
3.60M
  bufferptr = source->iobuffer;
322
11.5M
  for (col = cinfo->image_width; col > 0; col--) {
323
7.96M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
7.96M
  }
325
3.60M
  return 1;
326
3.60M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
311
3.60M
{
312
3.60M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
3.60M
  register _JSAMPROW ptr;
314
3.60M
  register U_CHAR *bufferptr;
315
3.60M
  register _JSAMPLE *rescale = source->rescale;
316
3.60M
  JDIMENSION col;
317
318
3.60M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
64
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
3.60M
  ptr = source->pub._buffer[0];
321
3.60M
  bufferptr = source->iobuffer;
322
11.5M
  for (col = cinfo->image_width; col > 0; col--) {
323
7.96M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
7.96M
  }
325
3.60M
  return 1;
326
3.60M
}
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
18.6M
{
334
18.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
18.6M
  register _JSAMPROW ptr;
336
18.6M
  register U_CHAR *bufferptr;
337
18.6M
  register _JSAMPLE *rescale = source->rescale;
338
18.6M
  JDIMENSION col;
339
18.6M
  unsigned int maxval = source->maxval;
340
18.6M
  register int rindex = rgb_red[cinfo->in_color_space];
341
18.6M
  register int gindex = rgb_green[cinfo->in_color_space];
342
18.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
18.6M
  register int aindex = alpha_index[cinfo->in_color_space];
344
18.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
18.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
440
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
18.6M
  ptr = source->pub._buffer[0];
349
18.6M
  bufferptr = source->iobuffer;
350
18.6M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
604k
    if (aindex >= 0)
352
120k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
483k
    else
354
483k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
18.0M
  } else {
356
18.0M
    if (aindex >= 0)
357
3.60M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
18.0M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
14.4M
    else
360
14.4M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
18.0M
  }
362
18.6M
  return 1;
363
18.6M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
333
18.6M
{
334
18.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
18.6M
  register _JSAMPROW ptr;
336
18.6M
  register U_CHAR *bufferptr;
337
18.6M
  register _JSAMPLE *rescale = source->rescale;
338
18.6M
  JDIMENSION col;
339
18.6M
  unsigned int maxval = source->maxval;
340
18.6M
  register int rindex = rgb_red[cinfo->in_color_space];
341
18.6M
  register int gindex = rgb_green[cinfo->in_color_space];
342
18.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
18.6M
  register int aindex = alpha_index[cinfo->in_color_space];
344
18.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
18.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
440
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
18.6M
  ptr = source->pub._buffer[0];
349
18.6M
  bufferptr = source->iobuffer;
350
18.6M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
604k
    if (aindex >= 0)
352
120k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
483k
    else
354
483k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
18.0M
  } else {
356
18.0M
    if (aindex >= 0)
357
3.60M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
18.0M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
14.4M
    else
360
14.4M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
18.0M
  }
362
18.6M
  return 1;
363
18.6M
}
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.72M
{
371
3.72M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
3.72M
  register _JSAMPROW ptr;
373
3.72M
  register U_CHAR *bufferptr;
374
3.72M
  register _JSAMPLE *rescale = source->rescale;
375
3.72M
  JDIMENSION col;
376
3.72M
  unsigned int maxval = source->maxval;
377
378
3.72M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
88
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
3.72M
  ptr = source->pub._buffer[0];
381
3.72M
  bufferptr = source->iobuffer;
382
3.72M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
547k
    for (col = cinfo->image_width; col > 0; col--) {
384
426k
      _JSAMPLE gray = *bufferptr++;
385
426k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
426k
      ptr += 4;
387
426k
    }
388
3.60M
  } else {
389
11.5M
    for (col = cinfo->image_width; col > 0; col--) {
390
7.96M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
7.96M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
7.96M
                  ptr + 1, ptr + 2, ptr + 3);
393
7.96M
      ptr += 4;
394
7.96M
    }
395
3.60M
  }
396
3.72M
  return 1;
397
3.72M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
370
3.72M
{
371
3.72M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
3.72M
  register _JSAMPROW ptr;
373
3.72M
  register U_CHAR *bufferptr;
374
3.72M
  register _JSAMPLE *rescale = source->rescale;
375
3.72M
  JDIMENSION col;
376
3.72M
  unsigned int maxval = source->maxval;
377
378
3.72M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
88
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
3.72M
  ptr = source->pub._buffer[0];
381
3.72M
  bufferptr = source->iobuffer;
382
3.72M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
547k
    for (col = cinfo->image_width; col > 0; col--) {
384
426k
      _JSAMPLE gray = *bufferptr++;
385
426k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
426k
      ptr += 4;
387
426k
    }
388
3.60M
  } else {
389
11.5M
    for (col = cinfo->image_width; col > 0; col--) {
390
7.96M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
7.96M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
7.96M
                  ptr + 1, ptr + 2, ptr + 3);
393
7.96M
      ptr += 4;
394
7.96M
    }
395
3.60M
  }
396
3.72M
  return 1;
397
3.72M
}
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
64.4k
{
404
64.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
64.4k
  register _JSAMPROW ptr;
406
64.4k
  register U_CHAR *bufferptr;
407
64.4k
  register _JSAMPLE *rescale = source->rescale;
408
64.4k
  JDIMENSION col;
409
64.4k
  unsigned int maxval = source->maxval;
410
64.4k
  register int rindex = rgb_red[cinfo->in_color_space];
411
64.4k
  register int gindex = rgb_green[cinfo->in_color_space];
412
64.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
64.4k
  register int aindex = alpha_index[cinfo->in_color_space];
414
64.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
64.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
723
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
64.4k
  ptr = source->pub._buffer[0];
419
64.4k
  bufferptr = source->iobuffer;
420
64.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
44.8k
    if (aindex >= 0)
422
11.2k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
33.6k
    else
424
33.6k
      RGB_READ_LOOP(*bufferptr++, {})
425
44.8k
  } else {
426
19.5k
    if (aindex >= 0)
427
3.77k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
19.5k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
15.8k
    else
430
15.8k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
19.5k
  }
432
64.4k
  return 1;
433
64.4k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
403
64.4k
{
404
64.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
64.4k
  register _JSAMPROW ptr;
406
64.4k
  register U_CHAR *bufferptr;
407
64.4k
  register _JSAMPLE *rescale = source->rescale;
408
64.4k
  JDIMENSION col;
409
64.4k
  unsigned int maxval = source->maxval;
410
64.4k
  register int rindex = rgb_red[cinfo->in_color_space];
411
64.4k
  register int gindex = rgb_green[cinfo->in_color_space];
412
64.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
64.4k
  register int aindex = alpha_index[cinfo->in_color_space];
414
64.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
64.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
723
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
64.4k
  ptr = source->pub._buffer[0];
419
64.4k
  bufferptr = source->iobuffer;
420
64.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
44.8k
    if (aindex >= 0)
422
11.2k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
33.6k
    else
424
33.6k
      RGB_READ_LOOP(*bufferptr++, {})
425
44.8k
  } else {
426
19.5k
    if (aindex >= 0)
427
3.77k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
19.5k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
15.8k
    else
430
15.8k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
19.5k
  }
432
64.4k
  return 1;
433
64.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
15.1k
{
441
15.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
15.1k
  register _JSAMPROW ptr;
443
15.1k
  register U_CHAR *bufferptr;
444
15.1k
  register _JSAMPLE *rescale = source->rescale;
445
15.1k
  JDIMENSION col;
446
15.1k
  unsigned int maxval = source->maxval;
447
448
15.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
156
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
15.1k
  ptr = source->pub._buffer[0];
451
15.1k
  bufferptr = source->iobuffer;
452
15.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
246k
    for (col = cinfo->image_width; col > 0; col--) {
454
235k
      _JSAMPLE r = *bufferptr++;
455
235k
      _JSAMPLE g = *bufferptr++;
456
235k
      _JSAMPLE b = *bufferptr++;
457
235k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
235k
      ptr += 4;
459
235k
    }
460
11.2k
  } else {
461
185k
    for (col = cinfo->image_width; col > 0; col--) {
462
182k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
182k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
182k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
182k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
182k
                  ptr + 2, ptr + 3);
467
182k
      ptr += 4;
468
182k
    }
469
3.93k
  }
470
15.1k
  return 1;
471
15.1k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
440
15.1k
{
441
15.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
15.1k
  register _JSAMPROW ptr;
443
15.1k
  register U_CHAR *bufferptr;
444
15.1k
  register _JSAMPLE *rescale = source->rescale;
445
15.1k
  JDIMENSION col;
446
15.1k
  unsigned int maxval = source->maxval;
447
448
15.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
156
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
15.1k
  ptr = source->pub._buffer[0];
451
15.1k
  bufferptr = source->iobuffer;
452
15.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
246k
    for (col = cinfo->image_width; col > 0; col--) {
454
235k
      _JSAMPLE r = *bufferptr++;
455
235k
      _JSAMPLE g = *bufferptr++;
456
235k
      _JSAMPLE b = *bufferptr++;
457
235k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
235k
      ptr += 4;
459
235k
    }
460
11.2k
  } else {
461
185k
    for (col = cinfo->image_width; col > 0; col--) {
462
182k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
182k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
182k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
182k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
182k
                  ptr + 2, ptr + 3);
467
182k
      ptr += 4;
468
182k
    }
469
3.93k
  }
470
15.1k
  return 1;
471
15.1k
}
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
132k
{
482
132k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
132k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
81
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
132k
  return 1;
487
132k
}
rdppm-8.c:get_raw_row
Line
Count
Source
481
132k
{
482
132k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
132k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
81
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
132k
  return 1;
487
132k
}
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
66
    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
41
      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
66
    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
41
      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.84k
{
521
7.84k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
7.84k
  register _JSAMPROW ptr;
523
7.84k
  register U_CHAR *bufferptr;
524
7.84k
  register _JSAMPLE *rescale = source->rescale;
525
7.84k
  JDIMENSION col;
526
7.84k
  unsigned int maxval = source->maxval;
527
7.84k
  register int rindex = rgb_red[cinfo->in_color_space];
528
7.84k
  register int gindex = rgb_green[cinfo->in_color_space];
529
7.84k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
7.84k
  register int aindex = alpha_index[cinfo->in_color_space];
531
7.84k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
7.84k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
330
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
7.84k
  ptr = source->pub._buffer[0];
536
7.84k
  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
205
      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.84k
  return 1;
549
7.84k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
520
7.84k
{
521
7.84k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
7.84k
  register _JSAMPROW ptr;
523
7.84k
  register U_CHAR *bufferptr;
524
7.84k
  register _JSAMPLE *rescale = source->rescale;
525
7.84k
  JDIMENSION col;
526
7.84k
  unsigned int maxval = source->maxval;
527
7.84k
  register int rindex = rgb_red[cinfo->in_color_space];
528
7.84k
  register int gindex = rgb_green[cinfo->in_color_space];
529
7.84k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
7.84k
  register int aindex = alpha_index[cinfo->in_color_space];
531
7.84k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
7.84k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
330
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
7.84k
  ptr = source->pub._buffer[0];
536
7.84k
  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
205
      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.84k
  return 1;
549
7.84k
}
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
66
    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
41
        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
66
    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
41
        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.5k
{
600
11.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
11.5k
  register _JSAMPROW ptr;
602
11.5k
  register U_CHAR *bufferptr;
603
11.5k
  register _JSAMPLE *rescale = source->rescale;
604
11.5k
  JDIMENSION col;
605
11.5k
  unsigned int maxval = source->maxval;
606
11.5k
  register int rindex = rgb_red[cinfo->in_color_space];
607
11.5k
  register int gindex = rgb_green[cinfo->in_color_space];
608
11.5k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
11.5k
  register int aindex = alpha_index[cinfo->in_color_space];
610
11.5k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
11.5k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
365
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
11.5k
  ptr = source->pub._buffer[0];
615
11.5k
  bufferptr = source->iobuffer;
616
27.7k
  for (col = cinfo->image_width; col > 0; col--) {
617
16.2k
    register unsigned int temp;
618
16.2k
    temp  = UCH(*bufferptr++) << 8;
619
16.2k
    temp |= UCH(*bufferptr++);
620
16.2k
    if (temp > maxval)
621
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
16.2k
    ptr[rindex] = rescale[temp];
623
16.2k
    temp  = UCH(*bufferptr++) << 8;
624
16.2k
    temp |= UCH(*bufferptr++);
625
16.2k
    if (temp > maxval)
626
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
16.2k
    ptr[gindex] = rescale[temp];
628
16.2k
    temp  = UCH(*bufferptr++) << 8;
629
16.2k
    temp |= UCH(*bufferptr++);
630
16.2k
    if (temp > maxval)
631
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
16.2k
    ptr[bindex] = rescale[temp];
633
16.2k
    if (aindex >= 0)
634
3.17k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
16.2k
    ptr += ps;
636
16.2k
  }
637
11.5k
  return 1;
638
11.5k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
599
11.5k
{
600
11.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
11.5k
  register _JSAMPROW ptr;
602
11.5k
  register U_CHAR *bufferptr;
603
11.5k
  register _JSAMPLE *rescale = source->rescale;
604
11.5k
  JDIMENSION col;
605
11.5k
  unsigned int maxval = source->maxval;
606
11.5k
  register int rindex = rgb_red[cinfo->in_color_space];
607
11.5k
  register int gindex = rgb_green[cinfo->in_color_space];
608
11.5k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
11.5k
  register int aindex = alpha_index[cinfo->in_color_space];
610
11.5k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
11.5k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
365
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
11.5k
  ptr = source->pub._buffer[0];
615
11.5k
  bufferptr = source->iobuffer;
616
27.7k
  for (col = cinfo->image_width; col > 0; col--) {
617
16.2k
    register unsigned int temp;
618
16.2k
    temp  = UCH(*bufferptr++) << 8;
619
16.2k
    temp |= UCH(*bufferptr++);
620
16.2k
    if (temp > maxval)
621
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
16.2k
    ptr[rindex] = rescale[temp];
623
16.2k
    temp  = UCH(*bufferptr++) << 8;
624
16.2k
    temp |= UCH(*bufferptr++);
625
16.2k
    if (temp > maxval)
626
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
16.2k
    ptr[gindex] = rescale[temp];
628
16.2k
    temp  = UCH(*bufferptr++) << 8;
629
16.2k
    temp |= UCH(*bufferptr++);
630
16.2k
    if (temp > maxval)
631
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
16.2k
    ptr[bindex] = rescale[temp];
633
16.2k
    if (aindex >= 0)
634
3.17k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
16.2k
    ptr += ps;
636
16.2k
  }
637
11.5k
  return 1;
638
11.5k
}
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.30k
{
645
2.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
2.30k
  register _JSAMPROW ptr;
647
2.30k
  register U_CHAR *bufferptr;
648
2.30k
  register _JSAMPLE *rescale = source->rescale;
649
2.30k
  JDIMENSION col;
650
2.30k
  unsigned int maxval = source->maxval;
651
652
2.30k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
73
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
2.30k
  ptr = source->pub._buffer[0];
655
2.30k
  bufferptr = source->iobuffer;
656
2.30k
  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.30k
  } else {
676
5.55k
    for (col = cinfo->image_width; col > 0; col--) {
677
3.25k
      register unsigned int r, g, b;
678
3.25k
      r  = UCH(*bufferptr++) << 8;
679
3.25k
      r |= UCH(*bufferptr++);
680
3.25k
      if (r > maxval)
681
32
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
3.25k
      g  = UCH(*bufferptr++) << 8;
683
3.25k
      g |= UCH(*bufferptr++);
684
3.25k
      if (g > maxval)
685
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
3.25k
      b  = UCH(*bufferptr++) << 8;
687
3.25k
      b |= UCH(*bufferptr++);
688
3.25k
      if (b > maxval)
689
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
3.25k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
3.25k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
3.25k
      ptr += 4;
693
3.25k
    }
694
2.30k
  }
695
2.30k
  return 1;
696
2.30k
}
rdppm-8.c:get_word_rgb_cmyk_row
Line
Count
Source
644
2.30k
{
645
2.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
2.30k
  register _JSAMPROW ptr;
647
2.30k
  register U_CHAR *bufferptr;
648
2.30k
  register _JSAMPLE *rescale = source->rescale;
649
2.30k
  JDIMENSION col;
650
2.30k
  unsigned int maxval = source->maxval;
651
652
2.30k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
73
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
2.30k
  ptr = source->pub._buffer[0];
655
2.30k
  bufferptr = source->iobuffer;
656
2.30k
  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.30k
  } else {
676
5.55k
    for (col = cinfo->image_width; col > 0; col--) {
677
3.25k
      register unsigned int r, g, b;
678
3.25k
      r  = UCH(*bufferptr++) << 8;
679
3.25k
      r |= UCH(*bufferptr++);
680
3.25k
      if (r > maxval)
681
32
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
3.25k
      g  = UCH(*bufferptr++) << 8;
683
3.25k
      g |= UCH(*bufferptr++);
684
3.25k
      if (g > maxval)
685
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
3.25k
      b  = UCH(*bufferptr++) << 8;
687
3.25k
      b |= UCH(*bufferptr++);
688
3.25k
      if (b > maxval)
689
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
3.25k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
3.25k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
3.25k
      ptr += 4;
693
3.25k
    }
694
2.30k
  }
695
2.30k
  return 1;
696
2.30k
}
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
16.0k
{
706
16.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
16.0k
  int c;
708
16.0k
  unsigned int w, h, maxval;
709
16.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
16.0k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
16.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
16.0k
  switch (c) {
718
1.23k
  case '2':                     /* it's a text-format PGM file */
719
2.66k
  case '3':                     /* it's a text-format PPM file */
720
12.0k
  case '5':                     /* it's a raw-format PGM file */
721
16.0k
  case '6':                     /* it's a raw-format PPM file */
722
16.0k
    break;
723
56
  default:
724
56
    ERREXIT(cinfo, JERR_PPM_NOT);
725
56
    break;
726
16.0k
  }
727
728
  /* fetch the remaining header info */
729
16.0k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
16.0k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
16.0k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
16.0k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
42
    ERREXIT(cinfo, JERR_PPM_NOT);
735
16.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
16.0k
  cinfo->image_width = (JDIMENSION)w;
739
16.0k
  cinfo->image_height = (JDIMENSION)h;
740
16.0k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
16.0k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
16.0k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
16.0k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
16.0k
  switch (c) {
748
931
  case '2':                     /* it's a text-format PGM file */
749
931
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
931
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
931
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
931
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
133
      source->pub.get_pixel_rows = get_text_gray_row;
755
798
    else if (IsExtRGB(cinfo->in_color_space))
756
665
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
133
    else if (cinfo->in_color_space == JCS_CMYK)
758
133
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
931
    need_iobuffer = FALSE;
762
931
    break;
763
764
1.18k
  case '3':                     /* it's a text-format PPM file */
765
1.18k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.18k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.18k
    if (IsExtRGB(cinfo->in_color_space))
769
845
      source->pub.get_pixel_rows = get_text_rgb_row;
770
338
    else if (cinfo->in_color_space == JCS_CMYK)
771
169
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
169
    else
773
169
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.18k
    need_iobuffer = FALSE;
775
1.18k
    break;
776
777
8.82k
  case '5':                     /* it's a raw-format PGM file */
778
8.82k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
8.82k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
8.82k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
8.82k
    if (maxval > 255) {
783
819
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
117
        source->pub.get_pixel_rows = get_word_gray_row;
785
702
      else if (IsExtRGB(cinfo->in_color_space))
786
585
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
117
      else if (cinfo->in_color_space == JCS_CMYK)
788
117
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
8.00k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
8.00k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
693
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
99
      source->pub.get_pixel_rows = get_raw_row;
795
99
      use_raw_buffer = TRUE;
796
99
      need_rescale = FALSE;
797
7.90k
    } else {
798
7.90k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.04k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
6.85k
      else if (IsExtRGB(cinfo->in_color_space))
801
5.71k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
1.14k
      else if (cinfo->in_color_space == JCS_CMYK)
803
1.14k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
7.90k
    }
807
8.82k
    break;
808
809
3.60k
  case '6':                     /* it's a raw-format PPM file */
810
3.60k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
3.60k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
3.60k
    if (maxval > 255) {
814
1.13k
      if (IsExtRGB(cinfo->in_color_space))
815
810
        source->pub.get_pixel_rows = get_word_rgb_row;
816
324
      else if (cinfo->in_color_space == JCS_CMYK)
817
162
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
162
      else
819
162
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
2.47k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
2.47k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
819
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
819
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
702
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
117
      source->pub.get_pixel_rows = get_raw_row;
829
117
      use_raw_buffer = TRUE;
830
117
      need_rescale = FALSE;
831
2.35k
    } else {
832
2.35k
      if (IsExtRGB(cinfo->in_color_space))
833
1.64k
        source->pub.get_pixel_rows = get_rgb_row;
834
706
      else if (cinfo->in_color_space == JCS_CMYK)
835
353
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
353
      else
837
353
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
2.35k
    }
839
3.60k
    break;
840
16.0k
  }
841
842
13.8k
  if (IsExtRGB(cinfo->in_color_space))
843
10.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
3.47k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.39k
    cinfo->input_components = 1;
846
2.07k
  else if (cinfo->in_color_space == JCS_CMYK)
847
2.07k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
13.8k
  if (need_iobuffer) {
851
11.9k
    if (c == '6')
852
3.09k
      source->buffer_width = (size_t)w * 3 *
853
3.09k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
8.82k
    else
855
8.82k
      source->buffer_width = (size_t)w *
856
8.82k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
11.9k
    source->iobuffer = (U_CHAR *)
858
11.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
11.9k
                                  source->buffer_width);
860
11.9k
  }
861
862
  /* Create compressor input buffer. */
863
13.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
216
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
216
    source->pub._buffer = &source->pixrow;
868
216
    source->pub.buffer_height = 1;
869
13.6k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
13.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
13.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
13.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
13.6k
    source->pub.buffer_height = 1;
875
13.6k
  }
876
877
  /* Compute the rescaling array if required. */
878
13.8k
  if (need_rescale) {
879
13.6k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
13.6k
    source->rescale = (_JSAMPLE *)
883
13.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
13.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
13.6k
                                           sizeof(_JSAMPLE)));
886
13.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
13.6k
                                        sizeof(_JSAMPLE)));
888
13.6k
    half_maxval = maxval / 2;
889
23.9M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
23.9M
      source->rescale[val] =
892
23.9M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
23.9M
                   maxval);
894
23.9M
    }
895
13.6k
  }
896
13.8k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
705
16.0k
{
706
16.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
16.0k
  int c;
708
16.0k
  unsigned int w, h, maxval;
709
16.0k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
16.0k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
16.0k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
16.0k
  switch (c) {
718
1.23k
  case '2':                     /* it's a text-format PGM file */
719
2.66k
  case '3':                     /* it's a text-format PPM file */
720
12.0k
  case '5':                     /* it's a raw-format PGM file */
721
16.0k
  case '6':                     /* it's a raw-format PPM file */
722
16.0k
    break;
723
56
  default:
724
56
    ERREXIT(cinfo, JERR_PPM_NOT);
725
56
    break;
726
16.0k
  }
727
728
  /* fetch the remaining header info */
729
16.0k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
16.0k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
16.0k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
16.0k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
42
    ERREXIT(cinfo, JERR_PPM_NOT);
735
16.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
16.0k
  cinfo->image_width = (JDIMENSION)w;
739
16.0k
  cinfo->image_height = (JDIMENSION)h;
740
16.0k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
16.0k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
16.0k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
16.0k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
16.0k
  switch (c) {
748
931
  case '2':                     /* it's a text-format PGM file */
749
931
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
931
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
931
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
931
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
133
      source->pub.get_pixel_rows = get_text_gray_row;
755
798
    else if (IsExtRGB(cinfo->in_color_space))
756
665
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
133
    else if (cinfo->in_color_space == JCS_CMYK)
758
133
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
931
    need_iobuffer = FALSE;
762
931
    break;
763
764
1.18k
  case '3':                     /* it's a text-format PPM file */
765
1.18k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.18k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.18k
    if (IsExtRGB(cinfo->in_color_space))
769
845
      source->pub.get_pixel_rows = get_text_rgb_row;
770
338
    else if (cinfo->in_color_space == JCS_CMYK)
771
169
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
169
    else
773
169
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.18k
    need_iobuffer = FALSE;
775
1.18k
    break;
776
777
8.82k
  case '5':                     /* it's a raw-format PGM file */
778
8.82k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
8.82k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
8.82k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
8.82k
    if (maxval > 255) {
783
819
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
117
        source->pub.get_pixel_rows = get_word_gray_row;
785
702
      else if (IsExtRGB(cinfo->in_color_space))
786
585
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
117
      else if (cinfo->in_color_space == JCS_CMYK)
788
117
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
8.00k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
8.00k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
693
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
99
      source->pub.get_pixel_rows = get_raw_row;
795
99
      use_raw_buffer = TRUE;
796
99
      need_rescale = FALSE;
797
7.90k
    } else {
798
7.90k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.04k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
6.85k
      else if (IsExtRGB(cinfo->in_color_space))
801
5.71k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
1.14k
      else if (cinfo->in_color_space == JCS_CMYK)
803
1.14k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
7.90k
    }
807
8.82k
    break;
808
809
3.60k
  case '6':                     /* it's a raw-format PPM file */
810
3.60k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
3.60k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
3.60k
    if (maxval > 255) {
814
1.13k
      if (IsExtRGB(cinfo->in_color_space))
815
810
        source->pub.get_pixel_rows = get_word_rgb_row;
816
324
      else if (cinfo->in_color_space == JCS_CMYK)
817
162
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
162
      else
819
162
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
2.47k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
2.47k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
819
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
819
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
702
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
117
      source->pub.get_pixel_rows = get_raw_row;
829
117
      use_raw_buffer = TRUE;
830
117
      need_rescale = FALSE;
831
2.35k
    } else {
832
2.35k
      if (IsExtRGB(cinfo->in_color_space))
833
1.64k
        source->pub.get_pixel_rows = get_rgb_row;
834
706
      else if (cinfo->in_color_space == JCS_CMYK)
835
353
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
353
      else
837
353
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
2.35k
    }
839
3.60k
    break;
840
16.0k
  }
841
842
13.8k
  if (IsExtRGB(cinfo->in_color_space))
843
10.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
3.47k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.39k
    cinfo->input_components = 1;
846
2.07k
  else if (cinfo->in_color_space == JCS_CMYK)
847
2.07k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
13.8k
  if (need_iobuffer) {
851
11.9k
    if (c == '6')
852
3.09k
      source->buffer_width = (size_t)w * 3 *
853
3.09k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
8.82k
    else
855
8.82k
      source->buffer_width = (size_t)w *
856
8.82k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
11.9k
    source->iobuffer = (U_CHAR *)
858
11.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
11.9k
                                  source->buffer_width);
860
11.9k
  }
861
862
  /* Create compressor input buffer. */
863
13.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
216
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
216
    source->pub._buffer = &source->pixrow;
868
216
    source->pub.buffer_height = 1;
869
13.6k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
13.6k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
13.6k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
13.6k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
13.6k
    source->pub.buffer_height = 1;
875
13.6k
  }
876
877
  /* Compute the rescaling array if required. */
878
13.8k
  if (need_rescale) {
879
13.6k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
13.6k
    source->rescale = (_JSAMPLE *)
883
13.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
13.6k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
13.6k
                                           sizeof(_JSAMPLE)));
886
13.6k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
13.6k
                                        sizeof(_JSAMPLE)));
888
13.6k
    half_maxval = maxval / 2;
889
23.9M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
23.9M
      source->rescale[val] =
892
23.9M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
23.9M
                   maxval);
894
23.9M
    }
895
13.6k
  }
896
13.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
8.84k
{
906
  /* no work */
907
8.84k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
905
8.84k
{
906
  /* no work */
907
8.84k
}
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
16.0k
{
917
16.0k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
16.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
16.0k
  source = (ppm_source_ptr)
929
16.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
16.0k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
16.0k
  source->pub.start_input = start_input_ppm;
933
16.0k
  source->pub.finish_input = finish_input_ppm;
934
16.0k
  source->pub.max_pixels = 0;
935
936
16.0k
  return (cjpeg_source_ptr)source;
937
16.0k
}
jinit_read_ppm
Line
Count
Source
916
16.0k
{
917
16.0k
  ppm_source_ptr source;
918
919
16.0k
#if BITS_IN_JSAMPLE == 8
920
16.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
16.0k
  source = (ppm_source_ptr)
929
16.0k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
16.0k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
16.0k
  source->pub.start_input = start_input_ppm;
933
16.0k
  source->pub.finish_input = finish_input_ppm;
934
16.0k
  source->pub.max_pixels = 0;
935
936
16.0k
  return (cjpeg_source_ptr)source;
937
16.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)) */