Coverage Report

Created: 2026-03-12 07:02

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
27.3M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
18.0M
  (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
327k
{
80
327k
  register int ch;
81
82
327k
  ch = getc(infile);
83
327k
  if (ch == '#') {
84
62.1k
    do {
85
62.1k
      ch = getc(infile);
86
62.1k
    } while (ch != '\n' && ch != EOF);
87
4.87k
  }
88
327k
  return ch;
89
327k
}
Unexecuted instantiation: rdppm-8.c:pbm_getc
Unexecuted instantiation: rdppm-12.c:pbm_getc
rdppm-16.c:pbm_getc
Line
Count
Source
79
327k
{
80
327k
  register int ch;
81
82
327k
  ch = getc(infile);
83
327k
  if (ch == '#') {
84
62.1k
    do {
85
62.1k
      ch = getc(infile);
86
62.1k
    } while (ch != '\n' && ch != EOF);
87
4.87k
  }
88
327k
  return ch;
89
327k
}
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
142k
{
99
142k
  register int ch;
100
142k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
150k
  do {
104
150k
    ch = pbm_getc(infile);
105
150k
    if (ch == EOF)
106
3.36k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
150k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
142k
  if (ch < '0' || ch > '9')
110
265
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
142k
  val = ch - '0';
113
181k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
39.4k
    val *= 10;
115
39.4k
    val += ch - '0';
116
39.4k
    if (val > maxval)
117
145
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
39.4k
  }
119
120
142k
  return val;
121
142k
}
Unexecuted instantiation: rdppm-8.c:read_pbm_integer
Unexecuted instantiation: rdppm-12.c:read_pbm_integer
rdppm-16.c:read_pbm_integer
Line
Count
Source
98
142k
{
99
142k
  register int ch;
100
142k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
150k
  do {
104
150k
    ch = pbm_getc(infile);
105
150k
    if (ch == EOF)
106
3.36k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
150k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
142k
  if (ch < '0' || ch > '9')
110
265
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
142k
  val = ch - '0';
113
181k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
39.4k
    val *= 10;
115
39.4k
    val += ch - '0';
116
39.4k
    if (val > maxval)
117
145
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
39.4k
  }
119
120
142k
  return val;
121
142k
}
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.89k
{
140
1.89k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.89k
  FILE *infile = source->pub.input_file;
142
1.89k
  register _JSAMPROW ptr;
143
1.89k
  register _JSAMPLE *rescale = source->rescale;
144
1.89k
  JDIMENSION col;
145
1.89k
  unsigned int maxval = source->maxval;
146
147
1.89k
  ptr = source->pub._buffer[0];
148
5.44k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.54k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.54k
  }
151
1.89k
  return 1;
152
1.89k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
rdppm-16.c:get_text_gray_row
Line
Count
Source
139
1.89k
{
140
1.89k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.89k
  FILE *infile = source->pub.input_file;
142
1.89k
  register _JSAMPROW ptr;
143
1.89k
  register _JSAMPLE *rescale = source->rescale;
144
1.89k
  JDIMENSION col;
145
1.89k
  unsigned int maxval = source->maxval;
146
147
1.89k
  ptr = source->pub._buffer[0];
148
5.44k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.54k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.54k
  }
151
1.89k
  return 1;
152
1.89k
}
153
154
155
12.6M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
66.2M
  for (col = cinfo->image_width; col > 0; col--) { \
157
53.5M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
53.5M
    alpha_set_op \
159
53.5M
    ptr += ps; \
160
53.5M
  } \
161
12.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
9.49k
{
168
9.49k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
9.49k
  FILE *infile = source->pub.input_file;
170
9.49k
  register _JSAMPROW ptr;
171
9.49k
  register _JSAMPLE *rescale = source->rescale;
172
9.49k
  JDIMENSION col;
173
9.49k
  unsigned int maxval = source->maxval;
174
9.49k
  register int rindex = rgb_red[cinfo->in_color_space];
175
9.49k
  register int gindex = rgb_green[cinfo->in_color_space];
176
9.49k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
9.49k
  register int aindex = alpha_index[cinfo->in_color_space];
178
9.49k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
9.49k
  ptr = source->pub._buffer[0];
181
9.49k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.22k
    if (aindex >= 0)
183
223
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.22k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
998
    else
186
998
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.27k
  } else {
188
8.27k
    if (aindex >= 0)
189
1.67k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.27k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.59k
    else
192
6.59k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.27k
  }
194
9.49k
  return 1;
195
9.49k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_rgb_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_rgb_row
rdppm-16.c:get_text_gray_rgb_row
Line
Count
Source
167
9.49k
{
168
9.49k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
9.49k
  FILE *infile = source->pub.input_file;
170
9.49k
  register _JSAMPROW ptr;
171
9.49k
  register _JSAMPLE *rescale = source->rescale;
172
9.49k
  JDIMENSION col;
173
9.49k
  unsigned int maxval = source->maxval;
174
9.49k
  register int rindex = rgb_red[cinfo->in_color_space];
175
9.49k
  register int gindex = rgb_green[cinfo->in_color_space];
176
9.49k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
9.49k
  register int aindex = alpha_index[cinfo->in_color_space];
178
9.49k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
9.49k
  ptr = source->pub._buffer[0];
181
9.49k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.22k
    if (aindex >= 0)
183
223
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.22k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
998
    else
186
998
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.27k
  } else {
188
8.27k
    if (aindex >= 0)
189
1.67k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.27k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.59k
    else
192
6.59k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.27k
  }
194
9.49k
  return 1;
195
9.49k
}
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.89k
{
203
1.89k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.89k
  FILE *infile = source->pub.input_file;
205
1.89k
  register _JSAMPROW ptr;
206
1.89k
  register _JSAMPLE *rescale = source->rescale;
207
1.89k
  JDIMENSION col;
208
1.89k
  unsigned int maxval = source->maxval;
209
210
1.89k
  ptr = source->pub._buffer[0];
211
1.89k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.25k
    for (col = cinfo->image_width; col > 0; col--) {
213
848
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
848
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
848
      ptr += 4;
216
848
    }
217
1.49k
  } else {
218
4.18k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.69k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.69k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
2.69k
                  ptr + 1, ptr + 2, ptr + 3);
222
2.69k
      ptr += 4;
223
2.69k
    }
224
1.49k
  }
225
1.89k
  return 1;
226
1.89k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
rdppm-16.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.89k
{
203
1.89k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.89k
  FILE *infile = source->pub.input_file;
205
1.89k
  register _JSAMPROW ptr;
206
1.89k
  register _JSAMPLE *rescale = source->rescale;
207
1.89k
  JDIMENSION col;
208
1.89k
  unsigned int maxval = source->maxval;
209
210
1.89k
  ptr = source->pub._buffer[0];
211
1.89k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.25k
    for (col = cinfo->image_width; col > 0; col--) {
213
848
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
848
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
848
      ptr += 4;
216
848
    }
217
1.49k
  } else {
218
4.18k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.69k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.69k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
2.69k
                  ptr + 1, ptr + 2, ptr + 3);
222
2.69k
      ptr += 4;
223
2.69k
    }
224
1.49k
  }
225
1.89k
  return 1;
226
1.89k
}
227
228
229
124k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
3.69M
  for (col = cinfo->image_width; col > 0; col--) { \
231
3.57M
    ptr[rindex] = read_op; \
232
3.57M
    ptr[gindex] = read_op; \
233
3.57M
    ptr[bindex] = read_op; \
234
3.57M
    alpha_set_op \
235
3.57M
    ptr += ps; \
236
3.57M
  } \
237
124k
}
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
14.9k
{
243
14.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
14.9k
  FILE *infile = source->pub.input_file;
245
14.9k
  register _JSAMPROW ptr;
246
14.9k
  register _JSAMPLE *rescale = source->rescale;
247
14.9k
  JDIMENSION col;
248
14.9k
  unsigned int maxval = source->maxval;
249
14.9k
  register int rindex = rgb_red[cinfo->in_color_space];
250
14.9k
  register int gindex = rgb_green[cinfo->in_color_space];
251
14.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
14.9k
  register int aindex = alpha_index[cinfo->in_color_space];
253
14.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
14.9k
  ptr = source->pub._buffer[0];
256
14.9k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.28k
    if (aindex >= 0)
258
227
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.28k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.06k
    else
261
2.06k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
12.7k
  } else {
263
12.7k
    if (aindex >= 0)
264
2.77k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
12.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
9.93k
    else
267
9.93k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
12.7k
  }
269
14.9k
  return 1;
270
14.9k
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_row
Unexecuted instantiation: rdppm-12.c:get_text_rgb_row
rdppm-16.c:get_text_rgb_row
Line
Count
Source
242
14.9k
{
243
14.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
14.9k
  FILE *infile = source->pub.input_file;
245
14.9k
  register _JSAMPROW ptr;
246
14.9k
  register _JSAMPLE *rescale = source->rescale;
247
14.9k
  JDIMENSION col;
248
14.9k
  unsigned int maxval = source->maxval;
249
14.9k
  register int rindex = rgb_red[cinfo->in_color_space];
250
14.9k
  register int gindex = rgb_green[cinfo->in_color_space];
251
14.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
14.9k
  register int aindex = alpha_index[cinfo->in_color_space];
253
14.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
14.9k
  ptr = source->pub._buffer[0];
256
14.9k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.28k
    if (aindex >= 0)
258
227
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.28k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.06k
    else
261
2.06k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
12.7k
  } else {
263
12.7k
    if (aindex >= 0)
264
2.77k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
12.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
9.93k
    else
267
9.93k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
12.7k
  }
269
14.9k
  return 1;
270
14.9k
}
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
2.99k
{
278
2.99k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
2.99k
  FILE *infile = source->pub.input_file;
280
2.99k
  register _JSAMPROW ptr;
281
2.99k
  register _JSAMPLE *rescale = source->rescale;
282
2.99k
  JDIMENSION col;
283
2.99k
  unsigned int maxval = source->maxval;
284
285
2.99k
  ptr = source->pub._buffer[0];
286
2.99k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
2.24k
    for (col = cinfo->image_width; col > 0; col--) {
288
1.37k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.37k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.37k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
1.37k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
1.37k
      ptr += 4;
293
1.37k
    }
294
2.12k
  } else {
295
5.36k
    for (col = cinfo->image_width; col > 0; col--) {
296
3.24k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
3.24k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
3.24k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
3.24k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
3.24k
                  ptr + 2, ptr + 3);
301
3.24k
      ptr += 4;
302
3.24k
    }
303
2.12k
  }
304
2.99k
  return 1;
305
2.99k
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_text_rgb_cmyk_row
rdppm-16.c:get_text_rgb_cmyk_row
Line
Count
Source
277
2.99k
{
278
2.99k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
2.99k
  FILE *infile = source->pub.input_file;
280
2.99k
  register _JSAMPROW ptr;
281
2.99k
  register _JSAMPLE *rescale = source->rescale;
282
2.99k
  JDIMENSION col;
283
2.99k
  unsigned int maxval = source->maxval;
284
285
2.99k
  ptr = source->pub._buffer[0];
286
2.99k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
2.24k
    for (col = cinfo->image_width; col > 0; col--) {
288
1.37k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.37k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.37k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
1.37k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
1.37k
      ptr += 4;
293
1.37k
    }
294
2.12k
  } else {
295
5.36k
    for (col = cinfo->image_width; col > 0; col--) {
296
3.24k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
3.24k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
3.24k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
3.24k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
3.24k
                  ptr + 2, ptr + 3);
301
3.24k
      ptr += 4;
302
3.24k
    }
303
2.12k
  }
304
2.99k
  return 1;
305
2.99k
}
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
2.53M
{
312
2.53M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
2.53M
  register _JSAMPROW ptr;
314
2.53M
  register U_CHAR *bufferptr;
315
2.53M
  register _JSAMPLE *rescale = source->rescale;
316
2.53M
  JDIMENSION col;
317
318
2.53M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
2.53M
  ptr = source->pub._buffer[0];
321
2.53M
  bufferptr = source->iobuffer;
322
13.2M
  for (col = cinfo->image_width; col > 0; col--) {
323
10.7M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
10.7M
  }
325
2.53M
  return 1;
326
2.53M
}
Unexecuted instantiation: rdppm-8.c:get_scaled_gray_row
Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row
rdppm-16.c:get_scaled_gray_row
Line
Count
Source
311
2.53M
{
312
2.53M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
2.53M
  register _JSAMPROW ptr;
314
2.53M
  register U_CHAR *bufferptr;
315
2.53M
  register _JSAMPLE *rescale = source->rescale;
316
2.53M
  JDIMENSION col;
317
318
2.53M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
2.53M
  ptr = source->pub._buffer[0];
321
2.53M
  bufferptr = source->iobuffer;
322
13.2M
  for (col = cinfo->image_width; col > 0; col--) {
323
10.7M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
10.7M
  }
325
2.53M
  return 1;
326
2.53M
}
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
12.6M
{
334
12.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
12.6M
  register _JSAMPROW ptr;
336
12.6M
  register U_CHAR *bufferptr;
337
12.6M
  register _JSAMPLE *rescale = source->rescale;
338
12.6M
  JDIMENSION col;
339
12.6M
  unsigned int maxval = source->maxval;
340
12.6M
  register int rindex = rgb_red[cinfo->in_color_space];
341
12.6M
  register int gindex = rgb_green[cinfo->in_color_space];
342
12.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
12.6M
  register int aindex = alpha_index[cinfo->in_color_space];
344
12.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
12.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
305
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
12.6M
  ptr = source->pub._buffer[0];
349
12.6M
  bufferptr = source->iobuffer;
350
12.6M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
0
    if (aindex >= 0)
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
0
    else
354
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
12.6M
  } else {
356
12.6M
    if (aindex >= 0)
357
2.53M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
12.6M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
10.1M
    else
360
10.1M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
12.6M
  }
362
12.6M
  return 1;
363
12.6M
}
Unexecuted instantiation: rdppm-8.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
rdppm-16.c:get_gray_rgb_row
Line
Count
Source
333
12.6M
{
334
12.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
12.6M
  register _JSAMPROW ptr;
336
12.6M
  register U_CHAR *bufferptr;
337
12.6M
  register _JSAMPLE *rescale = source->rescale;
338
12.6M
  JDIMENSION col;
339
12.6M
  unsigned int maxval = source->maxval;
340
12.6M
  register int rindex = rgb_red[cinfo->in_color_space];
341
12.6M
  register int gindex = rgb_green[cinfo->in_color_space];
342
12.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
12.6M
  register int aindex = alpha_index[cinfo->in_color_space];
344
12.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
12.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
305
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
12.6M
  ptr = source->pub._buffer[0];
349
12.6M
  bufferptr = source->iobuffer;
350
12.6M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
0
    if (aindex >= 0)
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
0
    else
354
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
12.6M
  } else {
356
12.6M
    if (aindex >= 0)
357
2.53M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
12.6M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
10.1M
    else
360
10.1M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
12.6M
  }
362
12.6M
  return 1;
363
12.6M
}
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
2.53M
{
371
2.53M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
2.53M
  register _JSAMPROW ptr;
373
2.53M
  register U_CHAR *bufferptr;
374
2.53M
  register _JSAMPLE *rescale = source->rescale;
375
2.53M
  JDIMENSION col;
376
2.53M
  unsigned int maxval = source->maxval;
377
378
2.53M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
2.53M
  ptr = source->pub._buffer[0];
381
2.53M
  bufferptr = source->iobuffer;
382
2.53M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
0
    for (col = cinfo->image_width; col > 0; col--) {
384
0
      _JSAMPLE gray = *bufferptr++;
385
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
0
      ptr += 4;
387
0
    }
388
2.53M
  } else {
389
13.2M
    for (col = cinfo->image_width; col > 0; col--) {
390
10.7M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
10.7M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
10.7M
                  ptr + 1, ptr + 2, ptr + 3);
393
10.7M
      ptr += 4;
394
10.7M
    }
395
2.53M
  }
396
2.53M
  return 1;
397
2.53M
}
Unexecuted instantiation: rdppm-8.c:get_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_gray_cmyk_row
rdppm-16.c:get_gray_cmyk_row
Line
Count
Source
370
2.53M
{
371
2.53M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
2.53M
  register _JSAMPROW ptr;
373
2.53M
  register U_CHAR *bufferptr;
374
2.53M
  register _JSAMPLE *rescale = source->rescale;
375
2.53M
  JDIMENSION col;
376
2.53M
  unsigned int maxval = source->maxval;
377
378
2.53M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
2.53M
  ptr = source->pub._buffer[0];
381
2.53M
  bufferptr = source->iobuffer;
382
2.53M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
383
0
    for (col = cinfo->image_width; col > 0; col--) {
384
0
      _JSAMPLE gray = *bufferptr++;
385
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
386
0
      ptr += 4;
387
0
    }
388
2.53M
  } else {
389
13.2M
    for (col = cinfo->image_width; col > 0; col--) {
390
10.7M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
10.7M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
10.7M
                  ptr + 1, ptr + 2, ptr + 3);
393
10.7M
      ptr += 4;
394
10.7M
    }
395
2.53M
  }
396
2.53M
  return 1;
397
2.53M
}
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
109k
{
404
109k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
109k
  register _JSAMPROW ptr;
406
109k
  register U_CHAR *bufferptr;
407
109k
  register _JSAMPLE *rescale = source->rescale;
408
109k
  JDIMENSION col;
409
109k
  unsigned int maxval = source->maxval;
410
109k
  register int rindex = rgb_red[cinfo->in_color_space];
411
109k
  register int gindex = rgb_green[cinfo->in_color_space];
412
109k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
109k
  register int aindex = alpha_index[cinfo->in_color_space];
414
109k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
109k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
530
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
109k
  ptr = source->pub._buffer[0];
419
109k
  bufferptr = source->iobuffer;
420
109k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
0
    if (aindex >= 0)
422
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
0
    else
424
0
      RGB_READ_LOOP(*bufferptr++, {})
425
109k
  } else {
426
109k
    if (aindex >= 0)
427
21.7k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
109k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
87.5k
    else
430
87.5k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
109k
  }
432
109k
  return 1;
433
109k
}
Unexecuted instantiation: rdppm-8.c:get_rgb_row
Unexecuted instantiation: rdppm-12.c:get_rgb_row
rdppm-16.c:get_rgb_row
Line
Count
Source
403
109k
{
404
109k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
109k
  register _JSAMPROW ptr;
406
109k
  register U_CHAR *bufferptr;
407
109k
  register _JSAMPLE *rescale = source->rescale;
408
109k
  JDIMENSION col;
409
109k
  unsigned int maxval = source->maxval;
410
109k
  register int rindex = rgb_red[cinfo->in_color_space];
411
109k
  register int gindex = rgb_green[cinfo->in_color_space];
412
109k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
109k
  register int aindex = alpha_index[cinfo->in_color_space];
414
109k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
109k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
530
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
109k
  ptr = source->pub._buffer[0];
419
109k
  bufferptr = source->iobuffer;
420
109k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
0
    if (aindex >= 0)
422
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
0
    else
424
0
      RGB_READ_LOOP(*bufferptr++, {})
425
109k
  } else {
426
109k
    if (aindex >= 0)
427
21.7k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
109k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
87.5k
    else
430
87.5k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
109k
  }
432
109k
  return 1;
433
109k
}
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
21.8k
{
441
21.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
21.8k
  register _JSAMPROW ptr;
443
21.8k
  register U_CHAR *bufferptr;
444
21.8k
  register _JSAMPLE *rescale = source->rescale;
445
21.8k
  JDIMENSION col;
446
21.8k
  unsigned int maxval = source->maxval;
447
448
21.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
106
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
21.8k
  ptr = source->pub._buffer[0];
451
21.8k
  bufferptr = source->iobuffer;
452
21.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
0
    for (col = cinfo->image_width; col > 0; col--) {
454
0
      _JSAMPLE r = *bufferptr++;
455
0
      _JSAMPLE g = *bufferptr++;
456
0
      _JSAMPLE b = *bufferptr++;
457
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
0
      ptr += 4;
459
0
    }
460
21.8k
  } else {
461
731k
    for (col = cinfo->image_width; col > 0; col--) {
462
710k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
710k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
710k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
710k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
710k
                  ptr + 2, ptr + 3);
467
710k
      ptr += 4;
468
710k
    }
469
21.8k
  }
470
21.8k
  return 1;
471
21.8k
}
Unexecuted instantiation: rdppm-8.c:get_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_rgb_cmyk_row
rdppm-16.c:get_rgb_cmyk_row
Line
Count
Source
440
21.8k
{
441
21.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
21.8k
  register _JSAMPROW ptr;
443
21.8k
  register U_CHAR *bufferptr;
444
21.8k
  register _JSAMPLE *rescale = source->rescale;
445
21.8k
  JDIMENSION col;
446
21.8k
  unsigned int maxval = source->maxval;
447
448
21.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
106
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
21.8k
  ptr = source->pub._buffer[0];
451
21.8k
  bufferptr = source->iobuffer;
452
21.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
453
0
    for (col = cinfo->image_width; col > 0; col--) {
454
0
      _JSAMPLE r = *bufferptr++;
455
0
      _JSAMPLE g = *bufferptr++;
456
0
      _JSAMPLE b = *bufferptr++;
457
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
458
0
      ptr += 4;
459
0
    }
460
21.8k
  } else {
461
731k
    for (col = cinfo->image_width; col > 0; col--) {
462
710k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
710k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
710k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
710k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
710k
                  ptr + 2, ptr + 3);
467
710k
      ptr += 4;
468
710k
    }
469
21.8k
  }
470
21.8k
  return 1;
471
21.8k
}
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
0
{
482
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
0
  return 1;
487
0
}
Unexecuted instantiation: rdppm-8.c:get_raw_row
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
21.2k
{
494
21.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
21.2k
  register _JSAMPROW ptr;
496
21.2k
  register U_CHAR *bufferptr;
497
21.2k
  register _JSAMPLE *rescale = source->rescale;
498
21.2k
  JDIMENSION col;
499
21.2k
  unsigned int maxval = source->maxval;
500
501
21.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
115
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
21.2k
  ptr = source->pub._buffer[0];
504
21.2k
  bufferptr = source->iobuffer;
505
236k
  for (col = cinfo->image_width; col > 0; col--) {
506
215k
    register unsigned int temp;
507
215k
    temp  = UCH(*bufferptr++) << 8;
508
215k
    temp |= UCH(*bufferptr++);
509
215k
    if (temp > maxval)
510
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
215k
    *ptr++ = rescale[temp];
512
215k
  }
513
21.2k
  return 1;
514
21.2k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_row
rdppm-16.c:get_word_gray_row
Line
Count
Source
493
21.2k
{
494
21.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
21.2k
  register _JSAMPROW ptr;
496
21.2k
  register U_CHAR *bufferptr;
497
21.2k
  register _JSAMPLE *rescale = source->rescale;
498
21.2k
  JDIMENSION col;
499
21.2k
  unsigned int maxval = source->maxval;
500
501
21.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
115
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
21.2k
  ptr = source->pub._buffer[0];
504
21.2k
  bufferptr = source->iobuffer;
505
236k
  for (col = cinfo->image_width; col > 0; col--) {
506
215k
    register unsigned int temp;
507
215k
    temp  = UCH(*bufferptr++) << 8;
508
215k
    temp |= UCH(*bufferptr++);
509
215k
    if (temp > maxval)
510
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
215k
    *ptr++ = rescale[temp];
512
215k
  }
513
21.2k
  return 1;
514
21.2k
}
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
106k
{
521
106k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
106k
  register _JSAMPROW ptr;
523
106k
  register U_CHAR *bufferptr;
524
106k
  register _JSAMPLE *rescale = source->rescale;
525
106k
  JDIMENSION col;
526
106k
  unsigned int maxval = source->maxval;
527
106k
  register int rindex = rgb_red[cinfo->in_color_space];
528
106k
  register int gindex = rgb_green[cinfo->in_color_space];
529
106k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
106k
  register int aindex = alpha_index[cinfo->in_color_space];
531
106k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
106k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
575
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
106k
  ptr = source->pub._buffer[0];
536
106k
  bufferptr = source->iobuffer;
537
1.18M
  for (col = cinfo->image_width; col > 0; col--) {
538
1.07M
    register unsigned int temp;
539
1.07M
    temp  = UCH(*bufferptr++) << 8;
540
1.07M
    temp |= UCH(*bufferptr++);
541
1.07M
    if (temp > maxval)
542
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
1.07M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
1.07M
    if (aindex >= 0)
545
215k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
1.07M
    ptr += ps;
547
1.07M
  }
548
106k
  return 1;
549
106k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_rgb_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row
rdppm-16.c:get_word_gray_rgb_row
Line
Count
Source
520
106k
{
521
106k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
106k
  register _JSAMPROW ptr;
523
106k
  register U_CHAR *bufferptr;
524
106k
  register _JSAMPLE *rescale = source->rescale;
525
106k
  JDIMENSION col;
526
106k
  unsigned int maxval = source->maxval;
527
106k
  register int rindex = rgb_red[cinfo->in_color_space];
528
106k
  register int gindex = rgb_green[cinfo->in_color_space];
529
106k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
106k
  register int aindex = alpha_index[cinfo->in_color_space];
531
106k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
106k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
575
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
106k
  ptr = source->pub._buffer[0];
536
106k
  bufferptr = source->iobuffer;
537
1.18M
  for (col = cinfo->image_width; col > 0; col--) {
538
1.07M
    register unsigned int temp;
539
1.07M
    temp  = UCH(*bufferptr++) << 8;
540
1.07M
    temp |= UCH(*bufferptr++);
541
1.07M
    if (temp > maxval)
542
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
1.07M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
1.07M
    if (aindex >= 0)
545
215k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
1.07M
    ptr += ps;
547
1.07M
  }
548
106k
  return 1;
549
106k
}
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
21.2k
{
556
21.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
21.2k
  register _JSAMPROW ptr;
558
21.2k
  register U_CHAR *bufferptr;
559
21.2k
  register _JSAMPLE *rescale = source->rescale;
560
21.2k
  JDIMENSION col;
561
21.2k
  unsigned int maxval = source->maxval;
562
563
21.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
115
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
21.2k
  ptr = source->pub._buffer[0];
566
21.2k
  bufferptr = source->iobuffer;
567
21.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
226k
    for (col = cinfo->image_width; col > 0; col--) {
569
207k
      register unsigned int gray;
570
207k
      gray  = UCH(*bufferptr++) << 8;
571
207k
      gray |= UCH(*bufferptr++);
572
207k
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
207k
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
207k
                  ptr + 1, ptr + 2, ptr + 3);
576
207k
      ptr += 4;
577
207k
    }
578
19.3k
  } else {
579
9.80k
    for (col = cinfo->image_width; col > 0; col--) {
580
7.88k
      register unsigned int temp;
581
7.88k
      _JSAMPLE gray;
582
7.88k
      temp  = UCH(*bufferptr++) << 8;
583
7.88k
      temp |= UCH(*bufferptr++);
584
7.88k
      if (temp > maxval)
585
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
7.88k
      gray = rescale[temp];
587
7.88k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
7.88k
                  ptr + 1, ptr + 2, ptr + 3);
589
7.88k
      ptr += 4;
590
7.88k
    }
591
1.92k
  }
592
21.2k
  return 1;
593
21.2k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_cmyk_row
rdppm-16.c:get_word_gray_cmyk_row
Line
Count
Source
555
21.2k
{
556
21.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
21.2k
  register _JSAMPROW ptr;
558
21.2k
  register U_CHAR *bufferptr;
559
21.2k
  register _JSAMPLE *rescale = source->rescale;
560
21.2k
  JDIMENSION col;
561
21.2k
  unsigned int maxval = source->maxval;
562
563
21.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
115
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
21.2k
  ptr = source->pub._buffer[0];
566
21.2k
  bufferptr = source->iobuffer;
567
21.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
226k
    for (col = cinfo->image_width; col > 0; col--) {
569
207k
      register unsigned int gray;
570
207k
      gray  = UCH(*bufferptr++) << 8;
571
207k
      gray |= UCH(*bufferptr++);
572
207k
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
207k
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
207k
                  ptr + 1, ptr + 2, ptr + 3);
576
207k
      ptr += 4;
577
207k
    }
578
19.3k
  } else {
579
9.80k
    for (col = cinfo->image_width; col > 0; col--) {
580
7.88k
      register unsigned int temp;
581
7.88k
      _JSAMPLE gray;
582
7.88k
      temp  = UCH(*bufferptr++) << 8;
583
7.88k
      temp |= UCH(*bufferptr++);
584
7.88k
      if (temp > maxval)
585
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
7.88k
      gray = rescale[temp];
587
7.88k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
7.88k
                  ptr + 1, ptr + 2, ptr + 3);
589
7.88k
      ptr += 4;
590
7.88k
    }
591
1.92k
  }
592
21.2k
  return 1;
593
21.2k
}
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
15.2k
{
600
15.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
15.2k
  register _JSAMPROW ptr;
602
15.2k
  register U_CHAR *bufferptr;
603
15.2k
  register _JSAMPLE *rescale = source->rescale;
604
15.2k
  JDIMENSION col;
605
15.2k
  unsigned int maxval = source->maxval;
606
15.2k
  register int rindex = rgb_red[cinfo->in_color_space];
607
15.2k
  register int gindex = rgb_green[cinfo->in_color_space];
608
15.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
15.2k
  register int aindex = alpha_index[cinfo->in_color_space];
610
15.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
15.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
660
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
15.2k
  ptr = source->pub._buffer[0];
615
15.2k
  bufferptr = source->iobuffer;
616
126k
  for (col = cinfo->image_width; col > 0; col--) {
617
111k
    register unsigned int temp;
618
111k
    temp  = UCH(*bufferptr++) << 8;
619
111k
    temp |= UCH(*bufferptr++);
620
111k
    if (temp > maxval)
621
140
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
111k
    ptr[rindex] = rescale[temp];
623
111k
    temp  = UCH(*bufferptr++) << 8;
624
111k
    temp |= UCH(*bufferptr++);
625
111k
    if (temp > maxval)
626
130
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
111k
    ptr[gindex] = rescale[temp];
628
111k
    temp  = UCH(*bufferptr++) << 8;
629
111k
    temp |= UCH(*bufferptr++);
630
111k
    if (temp > maxval)
631
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
111k
    ptr[bindex] = rescale[temp];
633
111k
    if (aindex >= 0)
634
22.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
111k
    ptr += ps;
636
111k
  }
637
15.2k
  return 1;
638
15.2k
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_row
Unexecuted instantiation: rdppm-12.c:get_word_rgb_row
rdppm-16.c:get_word_rgb_row
Line
Count
Source
599
15.2k
{
600
15.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
15.2k
  register _JSAMPROW ptr;
602
15.2k
  register U_CHAR *bufferptr;
603
15.2k
  register _JSAMPLE *rescale = source->rescale;
604
15.2k
  JDIMENSION col;
605
15.2k
  unsigned int maxval = source->maxval;
606
15.2k
  register int rindex = rgb_red[cinfo->in_color_space];
607
15.2k
  register int gindex = rgb_green[cinfo->in_color_space];
608
15.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
15.2k
  register int aindex = alpha_index[cinfo->in_color_space];
610
15.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
15.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
660
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
15.2k
  ptr = source->pub._buffer[0];
615
15.2k
  bufferptr = source->iobuffer;
616
126k
  for (col = cinfo->image_width; col > 0; col--) {
617
111k
    register unsigned int temp;
618
111k
    temp  = UCH(*bufferptr++) << 8;
619
111k
    temp |= UCH(*bufferptr++);
620
111k
    if (temp > maxval)
621
140
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
111k
    ptr[rindex] = rescale[temp];
623
111k
    temp  = UCH(*bufferptr++) << 8;
624
111k
    temp |= UCH(*bufferptr++);
625
111k
    if (temp > maxval)
626
130
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
111k
    ptr[gindex] = rescale[temp];
628
111k
    temp  = UCH(*bufferptr++) << 8;
629
111k
    temp |= UCH(*bufferptr++);
630
111k
    if (temp > maxval)
631
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
111k
    ptr[bindex] = rescale[temp];
633
111k
    if (aindex >= 0)
634
22.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
111k
    ptr += ps;
636
111k
  }
637
15.2k
  return 1;
638
15.2k
}
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
3.04k
{
645
3.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
3.04k
  register _JSAMPROW ptr;
647
3.04k
  register U_CHAR *bufferptr;
648
3.04k
  register _JSAMPLE *rescale = source->rescale;
649
3.04k
  JDIMENSION col;
650
3.04k
  unsigned int maxval = source->maxval;
651
652
3.04k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
132
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
3.04k
  ptr = source->pub._buffer[0];
655
3.04k
  bufferptr = source->iobuffer;
656
3.04k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
20.7k
    for (col = cinfo->image_width; col > 0; col--) {
658
19.5k
      register unsigned int r, g, b;
659
19.5k
      r  = UCH(*bufferptr++) << 8;
660
19.5k
      r |= UCH(*bufferptr++);
661
19.5k
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
19.5k
      g  = UCH(*bufferptr++) << 8;
664
19.5k
      g |= UCH(*bufferptr++);
665
19.5k
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
19.5k
      b  = UCH(*bufferptr++) << 8;
668
19.5k
      b |= UCH(*bufferptr++);
669
19.5k
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
19.5k
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
19.5k
                  ptr + 2, ptr + 3);
673
19.5k
      ptr += 4;
674
19.5k
    }
675
1.79k
  } else {
676
4.56k
    for (col = cinfo->image_width; col > 0; col--) {
677
2.77k
      register unsigned int r, g, b;
678
2.77k
      r  = UCH(*bufferptr++) << 8;
679
2.77k
      r |= UCH(*bufferptr++);
680
2.77k
      if (r > maxval)
681
28
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
2.77k
      g  = UCH(*bufferptr++) << 8;
683
2.77k
      g |= UCH(*bufferptr++);
684
2.77k
      if (g > maxval)
685
26
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
2.77k
      b  = UCH(*bufferptr++) << 8;
687
2.77k
      b |= UCH(*bufferptr++);
688
2.77k
      if (b > maxval)
689
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
2.77k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
2.77k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
2.77k
      ptr += 4;
693
2.77k
    }
694
1.79k
  }
695
3.04k
  return 1;
696
3.04k
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_word_rgb_cmyk_row
rdppm-16.c:get_word_rgb_cmyk_row
Line
Count
Source
644
3.04k
{
645
3.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
3.04k
  register _JSAMPROW ptr;
647
3.04k
  register U_CHAR *bufferptr;
648
3.04k
  register _JSAMPLE *rescale = source->rescale;
649
3.04k
  JDIMENSION col;
650
3.04k
  unsigned int maxval = source->maxval;
651
652
3.04k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
132
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
3.04k
  ptr = source->pub._buffer[0];
655
3.04k
  bufferptr = source->iobuffer;
656
3.04k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
20.7k
    for (col = cinfo->image_width; col > 0; col--) {
658
19.5k
      register unsigned int r, g, b;
659
19.5k
      r  = UCH(*bufferptr++) << 8;
660
19.5k
      r |= UCH(*bufferptr++);
661
19.5k
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
19.5k
      g  = UCH(*bufferptr++) << 8;
664
19.5k
      g |= UCH(*bufferptr++);
665
19.5k
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
19.5k
      b  = UCH(*bufferptr++) << 8;
668
19.5k
      b |= UCH(*bufferptr++);
669
19.5k
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
19.5k
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
19.5k
                  ptr + 2, ptr + 3);
673
19.5k
      ptr += 4;
674
19.5k
    }
675
1.79k
  } else {
676
4.56k
    for (col = cinfo->image_width; col > 0; col--) {
677
2.77k
      register unsigned int r, g, b;
678
2.77k
      r  = UCH(*bufferptr++) << 8;
679
2.77k
      r |= UCH(*bufferptr++);
680
2.77k
      if (r > maxval)
681
28
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
2.77k
      g  = UCH(*bufferptr++) << 8;
683
2.77k
      g |= UCH(*bufferptr++);
684
2.77k
      if (g > maxval)
685
26
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
2.77k
      b  = UCH(*bufferptr++) << 8;
687
2.77k
      b |= UCH(*bufferptr++);
688
2.77k
      if (b > maxval)
689
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
2.77k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
2.77k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
2.77k
      ptr += 4;
693
2.77k
    }
694
1.79k
  }
695
3.04k
  return 1;
696
3.04k
}
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
12.9k
{
706
12.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
12.9k
  int c;
708
12.9k
  unsigned int w, h, maxval;
709
12.9k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
12.9k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
12.9k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
12.9k
  switch (c) {
718
1.58k
  case '2':                     /* it's a text-format PGM file */
719
3.73k
  case '3':                     /* it's a text-format PPM file */
720
9.84k
  case '5':                     /* it's a raw-format PGM file */
721
12.8k
  case '6':                     /* it's a raw-format PPM file */
722
12.8k
    break;
723
70
  default:
724
70
    ERREXIT(cinfo, JERR_PPM_NOT);
725
70
    break;
726
12.9k
  }
727
728
  /* fetch the remaining header info */
729
12.8k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
12.8k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
12.8k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
12.8k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
42
    ERREXIT(cinfo, JERR_PPM_NOT);
735
12.8k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
273
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
12.8k
  cinfo->image_width = (JDIMENSION)w;
739
12.8k
  cinfo->image_height = (JDIMENSION)h;
740
12.8k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
12.8k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
12.8k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
12.8k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
12.8k
  switch (c) {
748
1.30k
  case '2':                     /* it's a text-format PGM file */
749
1.30k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
1.30k
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
1.30k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
1.30k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
186
      source->pub.get_pixel_rows = get_text_gray_row;
755
1.11k
    else if (IsExtRGB(cinfo->in_color_space))
756
930
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
186
    else if (cinfo->in_color_space == JCS_CMYK)
758
186
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
1.30k
    need_iobuffer = FALSE;
762
1.30k
    break;
763
764
1.60k
  case '3':                     /* it's a text-format PPM file */
765
1.60k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.60k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.60k
    if (IsExtRGB(cinfo->in_color_space))
769
1.14k
      source->pub.get_pixel_rows = get_text_rgb_row;
770
458
    else if (cinfo->in_color_space == JCS_CMYK)
771
229
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
229
    else
773
229
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.60k
    need_iobuffer = FALSE;
775
1.60k
    break;
776
777
5.74k
  case '5':                     /* it's a raw-format PGM file */
778
5.74k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
5.74k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
5.74k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
5.74k
    if (maxval > 255) {
783
1.43k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
205
        source->pub.get_pixel_rows = get_word_gray_row;
785
1.23k
      else if (IsExtRGB(cinfo->in_color_space))
786
1.02k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
205
      else if (cinfo->in_color_space == JCS_CMYK)
788
205
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
4.31k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
0
      source->pub.get_pixel_rows = get_raw_row;
795
0
      use_raw_buffer = TRUE;
796
0
      need_rescale = FALSE;
797
4.31k
    } else {
798
4.31k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
616
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
3.69k
      else if (IsExtRGB(cinfo->in_color_space))
801
3.08k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
616
      else if (cinfo->in_color_space == JCS_CMYK)
803
616
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
4.31k
    }
807
5.74k
    break;
808
809
2.74k
  case '6':                     /* it's a raw-format PPM file */
810
2.74k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.74k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.74k
    if (maxval > 255) {
814
1.66k
      if (IsExtRGB(cinfo->in_color_space))
815
1.19k
        source->pub.get_pixel_rows = get_word_rgb_row;
816
476
      else if (cinfo->in_color_space == JCS_CMYK)
817
238
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
238
      else
819
238
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.66k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
0
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
0
      source->pub.get_pixel_rows = get_raw_row;
829
0
      use_raw_buffer = TRUE;
830
0
      need_rescale = FALSE;
831
1.07k
    } else {
832
1.07k
      if (IsExtRGB(cinfo->in_color_space))
833
770
        source->pub.get_pixel_rows = get_rgb_row;
834
308
      else if (cinfo->in_color_space == JCS_CMYK)
835
154
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
154
      else
837
154
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.07k
    }
839
2.74k
    break;
840
12.8k
  }
841
842
10.7k
  if (IsExtRGB(cinfo->in_color_space))
843
8.14k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
2.63k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.00k
    cinfo->input_components = 1;
846
1.62k
  else if (cinfo->in_color_space == JCS_CMYK)
847
1.62k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
10.7k
  if (need_iobuffer) {
851
8.09k
    if (c == '6')
852
2.35k
      source->buffer_width = (size_t)w * 3 *
853
2.35k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
5.74k
    else
855
5.74k
      source->buffer_width = (size_t)w *
856
5.74k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
8.09k
    source->iobuffer = (U_CHAR *)
858
8.09k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
8.09k
                                  source->buffer_width);
860
8.09k
  }
861
862
  /* Create compressor input buffer. */
863
10.7k
  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
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
0
    source->pub._buffer = &source->pixrow;
868
0
    source->pub.buffer_height = 1;
869
10.7k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
10.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
10.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
10.7k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
10.7k
    source->pub.buffer_height = 1;
875
10.7k
  }
876
877
  /* Compute the rescaling array if required. */
878
10.7k
  if (need_rescale) {
879
10.7k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
10.7k
    source->rescale = (_JSAMPLE *)
883
10.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
10.7k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
10.7k
                                           sizeof(_JSAMPLE)));
886
10.7k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
10.7k
                                        sizeof(_JSAMPLE)));
888
10.7k
    half_maxval = maxval / 2;
889
149M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
149M
      source->rescale[val] =
892
149M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
149M
                   maxval);
894
149M
    }
895
10.7k
  }
896
10.7k
}
Unexecuted instantiation: rdppm-8.c:start_input_ppm
Unexecuted instantiation: rdppm-12.c:start_input_ppm
rdppm-16.c:start_input_ppm
Line
Count
Source
705
12.9k
{
706
12.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
12.9k
  int c;
708
12.9k
  unsigned int w, h, maxval;
709
12.9k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
12.9k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
12.9k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
12.9k
  switch (c) {
718
1.58k
  case '2':                     /* it's a text-format PGM file */
719
3.73k
  case '3':                     /* it's a text-format PPM file */
720
9.84k
  case '5':                     /* it's a raw-format PGM file */
721
12.8k
  case '6':                     /* it's a raw-format PPM file */
722
12.8k
    break;
723
70
  default:
724
70
    ERREXIT(cinfo, JERR_PPM_NOT);
725
70
    break;
726
12.9k
  }
727
728
  /* fetch the remaining header info */
729
12.8k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
12.8k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
12.8k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
12.8k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
42
    ERREXIT(cinfo, JERR_PPM_NOT);
735
12.8k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
273
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
12.8k
  cinfo->image_width = (JDIMENSION)w;
739
12.8k
  cinfo->image_height = (JDIMENSION)h;
740
12.8k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
12.8k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
12.8k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
12.8k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
12.8k
  switch (c) {
748
1.30k
  case '2':                     /* it's a text-format PGM file */
749
1.30k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
1.30k
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
1.30k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
1.30k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
186
      source->pub.get_pixel_rows = get_text_gray_row;
755
1.11k
    else if (IsExtRGB(cinfo->in_color_space))
756
930
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
186
    else if (cinfo->in_color_space == JCS_CMYK)
758
186
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
1.30k
    need_iobuffer = FALSE;
762
1.30k
    break;
763
764
1.60k
  case '3':                     /* it's a text-format PPM file */
765
1.60k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.60k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.60k
    if (IsExtRGB(cinfo->in_color_space))
769
1.14k
      source->pub.get_pixel_rows = get_text_rgb_row;
770
458
    else if (cinfo->in_color_space == JCS_CMYK)
771
229
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
229
    else
773
229
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.60k
    need_iobuffer = FALSE;
775
1.60k
    break;
776
777
5.74k
  case '5':                     /* it's a raw-format PGM file */
778
5.74k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
5.74k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
5.74k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
5.74k
    if (maxval > 255) {
783
1.43k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
205
        source->pub.get_pixel_rows = get_word_gray_row;
785
1.23k
      else if (IsExtRGB(cinfo->in_color_space))
786
1.02k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
205
      else if (cinfo->in_color_space == JCS_CMYK)
788
205
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
4.31k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
0
      source->pub.get_pixel_rows = get_raw_row;
795
0
      use_raw_buffer = TRUE;
796
0
      need_rescale = FALSE;
797
4.31k
    } else {
798
4.31k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
616
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
3.69k
      else if (IsExtRGB(cinfo->in_color_space))
801
3.08k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
616
      else if (cinfo->in_color_space == JCS_CMYK)
803
616
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
4.31k
    }
807
5.74k
    break;
808
809
2.74k
  case '6':                     /* it's a raw-format PPM file */
810
2.74k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.74k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.74k
    if (maxval > 255) {
814
1.66k
      if (IsExtRGB(cinfo->in_color_space))
815
1.19k
        source->pub.get_pixel_rows = get_word_rgb_row;
816
476
      else if (cinfo->in_color_space == JCS_CMYK)
817
238
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
238
      else
819
238
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.66k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
0
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
0
      source->pub.get_pixel_rows = get_raw_row;
829
0
      use_raw_buffer = TRUE;
830
0
      need_rescale = FALSE;
831
1.07k
    } else {
832
1.07k
      if (IsExtRGB(cinfo->in_color_space))
833
770
        source->pub.get_pixel_rows = get_rgb_row;
834
308
      else if (cinfo->in_color_space == JCS_CMYK)
835
154
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
154
      else
837
154
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.07k
    }
839
2.74k
    break;
840
12.8k
  }
841
842
10.7k
  if (IsExtRGB(cinfo->in_color_space))
843
8.14k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
2.63k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.00k
    cinfo->input_components = 1;
846
1.62k
  else if (cinfo->in_color_space == JCS_CMYK)
847
1.62k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
10.7k
  if (need_iobuffer) {
851
8.09k
    if (c == '6')
852
2.35k
      source->buffer_width = (size_t)w * 3 *
853
2.35k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
5.74k
    else
855
5.74k
      source->buffer_width = (size_t)w *
856
5.74k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
8.09k
    source->iobuffer = (U_CHAR *)
858
8.09k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
8.09k
                                  source->buffer_width);
860
8.09k
  }
861
862
  /* Create compressor input buffer. */
863
10.7k
  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
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
0
    source->pub._buffer = &source->pixrow;
868
0
    source->pub.buffer_height = 1;
869
10.7k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
10.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
10.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
10.7k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
10.7k
    source->pub.buffer_height = 1;
875
10.7k
  }
876
877
  /* Compute the rescaling array if required. */
878
10.7k
  if (need_rescale) {
879
10.7k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
10.7k
    source->rescale = (_JSAMPLE *)
883
10.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
10.7k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
10.7k
                                           sizeof(_JSAMPLE)));
886
10.7k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
10.7k
                                        sizeof(_JSAMPLE)));
888
10.7k
    half_maxval = maxval / 2;
889
149M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
149M
      source->rescale[val] =
892
149M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
149M
                   maxval);
894
149M
    }
895
10.7k
  }
896
10.7k
}
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
4.79k
{
906
  /* no work */
907
4.79k
}
Unexecuted instantiation: rdppm-8.c:finish_input_ppm
Unexecuted instantiation: rdppm-12.c:finish_input_ppm
rdppm-16.c:finish_input_ppm
Line
Count
Source
905
4.79k
{
906
  /* no work */
907
4.79k
}
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
12.9k
{
917
12.9k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
921
#else
922
12.9k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
12.9k
      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
12.9k
  source = (ppm_source_ptr)
929
12.9k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
12.9k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
12.9k
  source->pub.start_input = start_input_ppm;
933
12.9k
  source->pub.finish_input = finish_input_ppm;
934
12.9k
  source->pub.max_pixels = 0;
935
936
12.9k
  return (cjpeg_source_ptr)source;
937
12.9k
}
Unexecuted instantiation: jinit_read_ppm
Unexecuted instantiation: j12init_read_ppm
j16init_read_ppm
Line
Count
Source
916
12.9k
{
917
12.9k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
921
#else
922
12.9k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
12.9k
      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
12.9k
  source = (ppm_source_ptr)
929
12.9k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
12.9k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
12.9k
  source->pub.start_input = start_input_ppm;
933
12.9k
  source->pub.finish_input = finish_input_ppm;
934
12.9k
  source->pub.max_pixels = 0;
935
936
12.9k
  return (cjpeg_source_ptr)source;
937
12.9k
}
938
939
#endif /* defined(PPM_SUPPORTED) &&
940
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */