Coverage Report

Created: 2026-02-26 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.dev/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
40.8M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
15.8M
  (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
304k
{
80
304k
  register int ch;
81
82
304k
  ch = getc(infile);
83
304k
  if (ch == '#') {
84
44.2k
    do {
85
44.2k
      ch = getc(infile);
86
44.2k
    } while (ch != '\n' && ch != EOF);
87
3.57k
  }
88
304k
  return ch;
89
304k
}
Unexecuted instantiation: rdppm-8.c:pbm_getc
Unexecuted instantiation: rdppm-12.c:pbm_getc
rdppm-16.c:pbm_getc
Line
Count
Source
79
304k
{
80
304k
  register int ch;
81
82
304k
  ch = getc(infile);
83
304k
  if (ch == '#') {
84
44.2k
    do {
85
44.2k
      ch = getc(infile);
86
44.2k
    } while (ch != '\n' && ch != EOF);
87
3.57k
  }
88
304k
  return ch;
89
304k
}
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
130k
{
99
130k
  register int ch;
100
130k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
139k
  do {
104
139k
    ch = pbm_getc(infile);
105
139k
    if (ch == EOF)
106
3.10k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
139k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
130k
  if (ch < '0' || ch > '9')
110
301
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
130k
  val = ch - '0';
113
169k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
38.4k
    val *= 10;
115
38.4k
    val += ch - '0';
116
38.4k
    if (val > maxval)
117
139
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
38.4k
  }
119
120
130k
  return val;
121
130k
}
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
130k
{
99
130k
  register int ch;
100
130k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
139k
  do {
104
139k
    ch = pbm_getc(infile);
105
139k
    if (ch == EOF)
106
3.10k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
139k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
130k
  if (ch < '0' || ch > '9')
110
301
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
130k
  val = ch - '0';
113
169k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
38.4k
    val *= 10;
115
38.4k
    val += ch - '0';
116
38.4k
    if (val > maxval)
117
139
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
38.4k
  }
119
120
130k
  return val;
121
130k
}
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
2.05k
{
140
2.05k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.05k
  FILE *infile = source->pub.input_file;
142
2.05k
  register _JSAMPROW ptr;
143
2.05k
  register _JSAMPLE *rescale = source->rescale;
144
2.05k
  JDIMENSION col;
145
2.05k
  unsigned int maxval = source->maxval;
146
147
2.05k
  ptr = source->pub._buffer[0];
148
5.36k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.31k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.31k
  }
151
2.05k
  return 1;
152
2.05k
}
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
2.05k
{
140
2.05k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.05k
  FILE *infile = source->pub.input_file;
142
2.05k
  register _JSAMPROW ptr;
143
2.05k
  register _JSAMPLE *rescale = source->rescale;
144
2.05k
  JDIMENSION col;
145
2.05k
  unsigned int maxval = source->maxval;
146
147
2.05k
  ptr = source->pub._buffer[0];
148
5.36k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.31k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.31k
  }
151
2.05k
  return 1;
152
2.05k
}
153
154
155
11.2M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
101M
  for (col = cinfo->image_width; col > 0; col--) { \
157
90.2M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
90.2M
    alpha_set_op \
159
90.2M
    ptr += ps; \
160
90.2M
  } \
161
11.2M
}
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
10.2k
{
168
10.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.2k
  FILE *infile = source->pub.input_file;
170
10.2k
  register _JSAMPROW ptr;
171
10.2k
  register _JSAMPLE *rescale = source->rescale;
172
10.2k
  JDIMENSION col;
173
10.2k
  unsigned int maxval = source->maxval;
174
10.2k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.2k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.2k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.2k
  ptr = source->pub._buffer[0];
181
10.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.46k
    if (aindex >= 0)
183
220
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.46k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.24k
    else
186
1.24k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.80k
  } else {
188
8.80k
    if (aindex >= 0)
189
1.83k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.80k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.96k
    else
192
6.96k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.80k
  }
194
10.2k
  return 1;
195
10.2k
}
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
10.2k
{
168
10.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.2k
  FILE *infile = source->pub.input_file;
170
10.2k
  register _JSAMPROW ptr;
171
10.2k
  register _JSAMPLE *rescale = source->rescale;
172
10.2k
  JDIMENSION col;
173
10.2k
  unsigned int maxval = source->maxval;
174
10.2k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.2k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.2k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.2k
  ptr = source->pub._buffer[0];
181
10.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.46k
    if (aindex >= 0)
183
220
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.46k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.24k
    else
186
1.24k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.80k
  } else {
188
8.80k
    if (aindex >= 0)
189
1.83k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.80k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.96k
    else
192
6.96k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.80k
  }
194
10.2k
  return 1;
195
10.2k
}
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
2.05k
{
203
2.05k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
2.05k
  FILE *infile = source->pub.input_file;
205
2.05k
  register _JSAMPROW ptr;
206
2.05k
  register _JSAMPLE *rescale = source->rescale;
207
2.05k
  JDIMENSION col;
208
2.05k
  unsigned int maxval = source->maxval;
209
210
2.05k
  ptr = source->pub._buffer[0];
211
2.05k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.33k
    for (col = cinfo->image_width; col > 0; col--) {
213
809
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
809
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
809
      ptr += 4;
216
809
    }
217
1.52k
  } else {
218
4.03k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.50k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.50k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
2.50k
                  ptr + 1, ptr + 2, ptr + 3);
222
2.50k
      ptr += 4;
223
2.50k
    }
224
1.52k
  }
225
2.05k
  return 1;
226
2.05k
}
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
2.05k
{
203
2.05k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
2.05k
  FILE *infile = source->pub.input_file;
205
2.05k
  register _JSAMPROW ptr;
206
2.05k
  register _JSAMPLE *rescale = source->rescale;
207
2.05k
  JDIMENSION col;
208
2.05k
  unsigned int maxval = source->maxval;
209
210
2.05k
  ptr = source->pub._buffer[0];
211
2.05k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.33k
    for (col = cinfo->image_width; col > 0; col--) {
213
809
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
809
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
809
      ptr += 4;
216
809
    }
217
1.52k
  } else {
218
4.03k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.50k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.50k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
2.50k
                  ptr + 1, ptr + 2, ptr + 3);
222
2.50k
      ptr += 4;
223
2.50k
    }
224
1.52k
  }
225
2.05k
  return 1;
226
2.05k
}
227
228
229
29.9k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
1.99M
  for (col = cinfo->image_width; col > 0; col--) { \
231
1.96M
    ptr[rindex] = read_op; \
232
1.96M
    ptr[gindex] = read_op; \
233
1.96M
    ptr[bindex] = read_op; \
234
1.96M
    alpha_set_op \
235
1.96M
    ptr += ps; \
236
1.96M
  } \
237
29.9k
}
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
10.2k
{
243
10.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
10.2k
  FILE *infile = source->pub.input_file;
245
10.2k
  register _JSAMPROW ptr;
246
10.2k
  register _JSAMPLE *rescale = source->rescale;
247
10.2k
  JDIMENSION col;
248
10.2k
  unsigned int maxval = source->maxval;
249
10.2k
  register int rindex = rgb_red[cinfo->in_color_space];
250
10.2k
  register int gindex = rgb_green[cinfo->in_color_space];
251
10.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
10.2k
  register int aindex = alpha_index[cinfo->in_color_space];
253
10.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
10.2k
  ptr = source->pub._buffer[0];
256
10.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
1.04k
    if (aindex >= 0)
258
219
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
1.04k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
826
    else
261
826
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
9.20k
  } else {
263
9.20k
    if (aindex >= 0)
264
1.83k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
9.20k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
7.37k
    else
267
7.37k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
9.20k
  }
269
10.2k
  return 1;
270
10.2k
}
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
10.2k
{
243
10.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
10.2k
  FILE *infile = source->pub.input_file;
245
10.2k
  register _JSAMPROW ptr;
246
10.2k
  register _JSAMPLE *rescale = source->rescale;
247
10.2k
  JDIMENSION col;
248
10.2k
  unsigned int maxval = source->maxval;
249
10.2k
  register int rindex = rgb_red[cinfo->in_color_space];
250
10.2k
  register int gindex = rgb_green[cinfo->in_color_space];
251
10.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
10.2k
  register int aindex = alpha_index[cinfo->in_color_space];
253
10.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
10.2k
  ptr = source->pub._buffer[0];
256
10.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
1.04k
    if (aindex >= 0)
258
219
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
1.04k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
826
    else
261
826
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
9.20k
  } else {
263
9.20k
    if (aindex >= 0)
264
1.83k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
9.20k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
7.37k
    else
267
7.37k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
9.20k
  }
269
10.2k
  return 1;
270
10.2k
}
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.04k
{
278
2.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
2.04k
  FILE *infile = source->pub.input_file;
280
2.04k
  register _JSAMPROW ptr;
281
2.04k
  register _JSAMPLE *rescale = source->rescale;
282
2.04k
  JDIMENSION col;
283
2.04k
  unsigned int maxval = source->maxval;
284
285
2.04k
  ptr = source->pub._buffer[0];
286
2.04k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
1.64k
    for (col = cinfo->image_width; col > 0; col--) {
288
1.32k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.32k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.32k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
1.32k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
1.32k
      ptr += 4;
293
1.32k
    }
294
1.73k
  } else {
295
4.52k
    for (col = cinfo->image_width; col > 0; col--) {
296
2.79k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.79k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.79k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
2.79k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
2.79k
                  ptr + 2, ptr + 3);
301
2.79k
      ptr += 4;
302
2.79k
    }
303
1.73k
  }
304
2.04k
  return 1;
305
2.04k
}
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.04k
{
278
2.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
2.04k
  FILE *infile = source->pub.input_file;
280
2.04k
  register _JSAMPROW ptr;
281
2.04k
  register _JSAMPLE *rescale = source->rescale;
282
2.04k
  JDIMENSION col;
283
2.04k
  unsigned int maxval = source->maxval;
284
285
2.04k
  ptr = source->pub._buffer[0];
286
2.04k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
1.64k
    for (col = cinfo->image_width; col > 0; col--) {
288
1.32k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.32k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.32k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
1.32k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
1.32k
      ptr += 4;
293
1.32k
    }
294
1.73k
  } else {
295
4.52k
    for (col = cinfo->image_width; col > 0; col--) {
296
2.79k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.79k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.79k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
2.79k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
2.79k
                  ptr + 2, ptr + 3);
301
2.79k
      ptr += 4;
302
2.79k
    }
303
1.73k
  }
304
2.04k
  return 1;
305
2.04k
}
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.24M
{
312
2.24M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
2.24M
  register _JSAMPROW ptr;
314
2.24M
  register U_CHAR *bufferptr;
315
2.24M
  register _JSAMPLE *rescale = source->rescale;
316
2.24M
  JDIMENSION col;
317
318
2.24M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
70
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
2.24M
  ptr = source->pub._buffer[0];
321
2.24M
  bufferptr = source->iobuffer;
322
20.2M
  for (col = cinfo->image_width; col > 0; col--) {
323
18.0M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
18.0M
  }
325
2.24M
  return 1;
326
2.24M
}
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.24M
{
312
2.24M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
2.24M
  register _JSAMPROW ptr;
314
2.24M
  register U_CHAR *bufferptr;
315
2.24M
  register _JSAMPLE *rescale = source->rescale;
316
2.24M
  JDIMENSION col;
317
318
2.24M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
70
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
2.24M
  ptr = source->pub._buffer[0];
321
2.24M
  bufferptr = source->iobuffer;
322
20.2M
  for (col = cinfo->image_width; col > 0; col--) {
323
18.0M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
18.0M
  }
325
2.24M
  return 1;
326
2.24M
}
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
11.2M
{
334
11.2M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
11.2M
  register _JSAMPROW ptr;
336
11.2M
  register U_CHAR *bufferptr;
337
11.2M
  register _JSAMPLE *rescale = source->rescale;
338
11.2M
  JDIMENSION col;
339
11.2M
  unsigned int maxval = source->maxval;
340
11.2M
  register int rindex = rgb_red[cinfo->in_color_space];
341
11.2M
  register int gindex = rgb_green[cinfo->in_color_space];
342
11.2M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
11.2M
  register int aindex = alpha_index[cinfo->in_color_space];
344
11.2M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
11.2M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
350
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
11.2M
  ptr = source->pub._buffer[0];
349
11.2M
  bufferptr = source->iobuffer;
350
11.2M
  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
11.2M
  } else {
356
11.2M
    if (aindex >= 0)
357
2.24M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
11.2M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
8.96M
    else
360
8.96M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
11.2M
  }
362
11.2M
  return 1;
363
11.2M
}
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
11.2M
{
334
11.2M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
11.2M
  register _JSAMPROW ptr;
336
11.2M
  register U_CHAR *bufferptr;
337
11.2M
  register _JSAMPLE *rescale = source->rescale;
338
11.2M
  JDIMENSION col;
339
11.2M
  unsigned int maxval = source->maxval;
340
11.2M
  register int rindex = rgb_red[cinfo->in_color_space];
341
11.2M
  register int gindex = rgb_green[cinfo->in_color_space];
342
11.2M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
11.2M
  register int aindex = alpha_index[cinfo->in_color_space];
344
11.2M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
11.2M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
350
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
11.2M
  ptr = source->pub._buffer[0];
349
11.2M
  bufferptr = source->iobuffer;
350
11.2M
  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
11.2M
  } else {
356
11.2M
    if (aindex >= 0)
357
2.24M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
11.2M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
8.96M
    else
360
8.96M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
11.2M
  }
362
11.2M
  return 1;
363
11.2M
}
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.24M
{
371
2.24M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
2.24M
  register _JSAMPROW ptr;
373
2.24M
  register U_CHAR *bufferptr;
374
2.24M
  register _JSAMPLE *rescale = source->rescale;
375
2.24M
  JDIMENSION col;
376
2.24M
  unsigned int maxval = source->maxval;
377
378
2.24M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
70
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
2.24M
  ptr = source->pub._buffer[0];
381
2.24M
  bufferptr = source->iobuffer;
382
2.24M
  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.24M
  } else {
389
20.2M
    for (col = cinfo->image_width; col > 0; col--) {
390
18.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
18.0M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
18.0M
                  ptr + 1, ptr + 2, ptr + 3);
393
18.0M
      ptr += 4;
394
18.0M
    }
395
2.24M
  }
396
2.24M
  return 1;
397
2.24M
}
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.24M
{
371
2.24M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
2.24M
  register _JSAMPROW ptr;
373
2.24M
  register U_CHAR *bufferptr;
374
2.24M
  register _JSAMPLE *rescale = source->rescale;
375
2.24M
  JDIMENSION col;
376
2.24M
  unsigned int maxval = source->maxval;
377
378
2.24M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
70
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
2.24M
  ptr = source->pub._buffer[0];
381
2.24M
  bufferptr = source->iobuffer;
382
2.24M
  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.24M
  } else {
389
20.2M
    for (col = cinfo->image_width; col > 0; col--) {
390
18.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
18.0M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
18.0M
                  ptr + 1, ptr + 2, ptr + 3);
393
18.0M
      ptr += 4;
394
18.0M
    }
395
2.24M
  }
396
2.24M
  return 1;
397
2.24M
}
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
19.7k
{
404
19.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
19.7k
  register _JSAMPROW ptr;
406
19.7k
  register U_CHAR *bufferptr;
407
19.7k
  register _JSAMPLE *rescale = source->rescale;
408
19.7k
  JDIMENSION col;
409
19.7k
  unsigned int maxval = source->maxval;
410
19.7k
  register int rindex = rgb_red[cinfo->in_color_space];
411
19.7k
  register int gindex = rgb_green[cinfo->in_color_space];
412
19.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
19.7k
  register int aindex = alpha_index[cinfo->in_color_space];
414
19.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
19.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
505
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
19.7k
  ptr = source->pub._buffer[0];
419
19.7k
  bufferptr = source->iobuffer;
420
19.7k
  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
19.7k
  } else {
426
19.7k
    if (aindex >= 0)
427
3.84k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
19.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
15.8k
    else
430
15.8k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
19.7k
  }
432
19.7k
  return 1;
433
19.7k
}
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
19.7k
{
404
19.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
19.7k
  register _JSAMPROW ptr;
406
19.7k
  register U_CHAR *bufferptr;
407
19.7k
  register _JSAMPLE *rescale = source->rescale;
408
19.7k
  JDIMENSION col;
409
19.7k
  unsigned int maxval = source->maxval;
410
19.7k
  register int rindex = rgb_red[cinfo->in_color_space];
411
19.7k
  register int gindex = rgb_green[cinfo->in_color_space];
412
19.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
19.7k
  register int aindex = alpha_index[cinfo->in_color_space];
414
19.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
19.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
505
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
19.7k
  ptr = source->pub._buffer[0];
419
19.7k
  bufferptr = source->iobuffer;
420
19.7k
  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
19.7k
  } else {
426
19.7k
    if (aindex >= 0)
427
3.84k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
19.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
15.8k
    else
430
15.8k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
19.7k
  }
432
19.7k
  return 1;
433
19.7k
}
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
3.94k
{
441
3.94k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
3.94k
  register _JSAMPROW ptr;
443
3.94k
  register U_CHAR *bufferptr;
444
3.94k
  register _JSAMPLE *rescale = source->rescale;
445
3.94k
  JDIMENSION col;
446
3.94k
  unsigned int maxval = source->maxval;
447
448
3.94k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
101
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
3.94k
  ptr = source->pub._buffer[0];
451
3.94k
  bufferptr = source->iobuffer;
452
3.94k
  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
3.94k
  } else {
461
392k
    for (col = cinfo->image_width; col > 0; col--) {
462
388k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
388k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
388k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
388k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
388k
                  ptr + 2, ptr + 3);
467
388k
      ptr += 4;
468
388k
    }
469
3.94k
  }
470
3.94k
  return 1;
471
3.94k
}
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
3.94k
{
441
3.94k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
3.94k
  register _JSAMPROW ptr;
443
3.94k
  register U_CHAR *bufferptr;
444
3.94k
  register _JSAMPLE *rescale = source->rescale;
445
3.94k
  JDIMENSION col;
446
3.94k
  unsigned int maxval = source->maxval;
447
448
3.94k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
101
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
3.94k
  ptr = source->pub._buffer[0];
451
3.94k
  bufferptr = source->iobuffer;
452
3.94k
  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
3.94k
  } else {
461
392k
    for (col = cinfo->image_width; col > 0; col--) {
462
388k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
388k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
388k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
388k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
388k
                  ptr + 2, ptr + 3);
467
388k
      ptr += 4;
468
388k
    }
469
3.94k
  }
470
3.94k
  return 1;
471
3.94k
}
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
10.4k
{
494
10.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
10.4k
  register _JSAMPROW ptr;
496
10.4k
  register U_CHAR *bufferptr;
497
10.4k
  register _JSAMPLE *rescale = source->rescale;
498
10.4k
  JDIMENSION col;
499
10.4k
  unsigned int maxval = source->maxval;
500
501
10.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
110
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
10.4k
  ptr = source->pub._buffer[0];
504
10.4k
  bufferptr = source->iobuffer;
505
216k
  for (col = cinfo->image_width; col > 0; col--) {
506
205k
    register unsigned int temp;
507
205k
    temp  = UCH(*bufferptr++) << 8;
508
205k
    temp |= UCH(*bufferptr++);
509
205k
    if (temp > maxval)
510
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
205k
    *ptr++ = rescale[temp];
512
205k
  }
513
10.4k
  return 1;
514
10.4k
}
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
10.4k
{
494
10.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
10.4k
  register _JSAMPROW ptr;
496
10.4k
  register U_CHAR *bufferptr;
497
10.4k
  register _JSAMPLE *rescale = source->rescale;
498
10.4k
  JDIMENSION col;
499
10.4k
  unsigned int maxval = source->maxval;
500
501
10.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
110
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
10.4k
  ptr = source->pub._buffer[0];
504
10.4k
  bufferptr = source->iobuffer;
505
216k
  for (col = cinfo->image_width; col > 0; col--) {
506
205k
    register unsigned int temp;
507
205k
    temp  = UCH(*bufferptr++) << 8;
508
205k
    temp |= UCH(*bufferptr++);
509
205k
    if (temp > maxval)
510
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
205k
    *ptr++ = rescale[temp];
512
205k
  }
513
10.4k
  return 1;
514
10.4k
}
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
52.2k
{
521
52.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
52.2k
  register _JSAMPROW ptr;
523
52.2k
  register U_CHAR *bufferptr;
524
52.2k
  register _JSAMPLE *rescale = source->rescale;
525
52.2k
  JDIMENSION col;
526
52.2k
  unsigned int maxval = source->maxval;
527
52.2k
  register int rindex = rgb_red[cinfo->in_color_space];
528
52.2k
  register int gindex = rgb_green[cinfo->in_color_space];
529
52.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
52.2k
  register int aindex = alpha_index[cinfo->in_color_space];
531
52.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
52.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
550
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
52.2k
  ptr = source->pub._buffer[0];
536
52.2k
  bufferptr = source->iobuffer;
537
1.08M
  for (col = cinfo->image_width; col > 0; col--) {
538
1.02M
    register unsigned int temp;
539
1.02M
    temp  = UCH(*bufferptr++) << 8;
540
1.02M
    temp |= UCH(*bufferptr++);
541
1.02M
    if (temp > maxval)
542
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
1.02M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
1.02M
    if (aindex >= 0)
545
205k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
1.02M
    ptr += ps;
547
1.02M
  }
548
52.2k
  return 1;
549
52.2k
}
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
52.2k
{
521
52.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
52.2k
  register _JSAMPROW ptr;
523
52.2k
  register U_CHAR *bufferptr;
524
52.2k
  register _JSAMPLE *rescale = source->rescale;
525
52.2k
  JDIMENSION col;
526
52.2k
  unsigned int maxval = source->maxval;
527
52.2k
  register int rindex = rgb_red[cinfo->in_color_space];
528
52.2k
  register int gindex = rgb_green[cinfo->in_color_space];
529
52.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
52.2k
  register int aindex = alpha_index[cinfo->in_color_space];
531
52.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
52.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
550
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
52.2k
  ptr = source->pub._buffer[0];
536
52.2k
  bufferptr = source->iobuffer;
537
1.08M
  for (col = cinfo->image_width; col > 0; col--) {
538
1.02M
    register unsigned int temp;
539
1.02M
    temp  = UCH(*bufferptr++) << 8;
540
1.02M
    temp |= UCH(*bufferptr++);
541
1.02M
    if (temp > maxval)
542
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
1.02M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
1.02M
    if (aindex >= 0)
545
205k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
1.02M
    ptr += ps;
547
1.02M
  }
548
52.2k
  return 1;
549
52.2k
}
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
10.4k
{
556
10.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
10.4k
  register _JSAMPROW ptr;
558
10.4k
  register U_CHAR *bufferptr;
559
10.4k
  register _JSAMPLE *rescale = source->rescale;
560
10.4k
  JDIMENSION col;
561
10.4k
  unsigned int maxval = source->maxval;
562
563
10.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
110
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
10.4k
  ptr = source->pub._buffer[0];
566
10.4k
  bufferptr = source->iobuffer;
567
10.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
210k
    for (col = cinfo->image_width; col > 0; col--) {
569
201k
      register unsigned int gray;
570
201k
      gray  = UCH(*bufferptr++) << 8;
571
201k
      gray |= UCH(*bufferptr++);
572
201k
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
201k
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
201k
                  ptr + 1, ptr + 2, ptr + 3);
576
201k
      ptr += 4;
577
201k
    }
578
8.69k
  } else {
579
6.08k
    for (col = cinfo->image_width; col > 0; col--) {
580
4.33k
      register unsigned int temp;
581
4.33k
      _JSAMPLE gray;
582
4.33k
      temp  = UCH(*bufferptr++) << 8;
583
4.33k
      temp |= UCH(*bufferptr++);
584
4.33k
      if (temp > maxval)
585
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
4.33k
      gray = rescale[temp];
587
4.33k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
4.33k
                  ptr + 1, ptr + 2, ptr + 3);
589
4.33k
      ptr += 4;
590
4.33k
    }
591
1.75k
  }
592
10.4k
  return 1;
593
10.4k
}
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
10.4k
{
556
10.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
10.4k
  register _JSAMPROW ptr;
558
10.4k
  register U_CHAR *bufferptr;
559
10.4k
  register _JSAMPLE *rescale = source->rescale;
560
10.4k
  JDIMENSION col;
561
10.4k
  unsigned int maxval = source->maxval;
562
563
10.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
110
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
10.4k
  ptr = source->pub._buffer[0];
566
10.4k
  bufferptr = source->iobuffer;
567
10.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
210k
    for (col = cinfo->image_width; col > 0; col--) {
569
201k
      register unsigned int gray;
570
201k
      gray  = UCH(*bufferptr++) << 8;
571
201k
      gray |= UCH(*bufferptr++);
572
201k
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
201k
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
201k
                  ptr + 1, ptr + 2, ptr + 3);
576
201k
      ptr += 4;
577
201k
    }
578
8.69k
  } else {
579
6.08k
    for (col = cinfo->image_width; col > 0; col--) {
580
4.33k
      register unsigned int temp;
581
4.33k
      _JSAMPLE gray;
582
4.33k
      temp  = UCH(*bufferptr++) << 8;
583
4.33k
      temp |= UCH(*bufferptr++);
584
4.33k
      if (temp > maxval)
585
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
4.33k
      gray = rescale[temp];
587
4.33k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
4.33k
                  ptr + 1, ptr + 2, ptr + 3);
589
4.33k
      ptr += 4;
590
4.33k
    }
591
1.75k
  }
592
10.4k
  return 1;
593
10.4k
}
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
16.2k
{
600
16.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
16.2k
  register _JSAMPROW ptr;
602
16.2k
  register U_CHAR *bufferptr;
603
16.2k
  register _JSAMPLE *rescale = source->rescale;
604
16.2k
  JDIMENSION col;
605
16.2k
  unsigned int maxval = source->maxval;
606
16.2k
  register int rindex = rgb_red[cinfo->in_color_space];
607
16.2k
  register int gindex = rgb_green[cinfo->in_color_space];
608
16.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
16.2k
  register int aindex = alpha_index[cinfo->in_color_space];
610
16.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
16.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
685
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
16.2k
  ptr = source->pub._buffer[0];
615
16.2k
  bufferptr = source->iobuffer;
616
122k
  for (col = cinfo->image_width; col > 0; col--) {
617
106k
    register unsigned int temp;
618
106k
    temp  = UCH(*bufferptr++) << 8;
619
106k
    temp |= UCH(*bufferptr++);
620
106k
    if (temp > maxval)
621
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
106k
    ptr[rindex] = rescale[temp];
623
106k
    temp  = UCH(*bufferptr++) << 8;
624
106k
    temp |= UCH(*bufferptr++);
625
106k
    if (temp > maxval)
626
130
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
106k
    ptr[gindex] = rescale[temp];
628
106k
    temp  = UCH(*bufferptr++) << 8;
629
106k
    temp |= UCH(*bufferptr++);
630
106k
    if (temp > maxval)
631
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
106k
    ptr[bindex] = rescale[temp];
633
106k
    if (aindex >= 0)
634
21.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
106k
    ptr += ps;
636
106k
  }
637
16.2k
  return 1;
638
16.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
16.2k
{
600
16.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
16.2k
  register _JSAMPROW ptr;
602
16.2k
  register U_CHAR *bufferptr;
603
16.2k
  register _JSAMPLE *rescale = source->rescale;
604
16.2k
  JDIMENSION col;
605
16.2k
  unsigned int maxval = source->maxval;
606
16.2k
  register int rindex = rgb_red[cinfo->in_color_space];
607
16.2k
  register int gindex = rgb_green[cinfo->in_color_space];
608
16.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
16.2k
  register int aindex = alpha_index[cinfo->in_color_space];
610
16.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
16.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
685
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
16.2k
  ptr = source->pub._buffer[0];
615
16.2k
  bufferptr = source->iobuffer;
616
122k
  for (col = cinfo->image_width; col > 0; col--) {
617
106k
    register unsigned int temp;
618
106k
    temp  = UCH(*bufferptr++) << 8;
619
106k
    temp |= UCH(*bufferptr++);
620
106k
    if (temp > maxval)
621
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
106k
    ptr[rindex] = rescale[temp];
623
106k
    temp  = UCH(*bufferptr++) << 8;
624
106k
    temp |= UCH(*bufferptr++);
625
106k
    if (temp > maxval)
626
130
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
106k
    ptr[gindex] = rescale[temp];
628
106k
    temp  = UCH(*bufferptr++) << 8;
629
106k
    temp |= UCH(*bufferptr++);
630
106k
    if (temp > maxval)
631
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
106k
    ptr[bindex] = rescale[temp];
633
106k
    if (aindex >= 0)
634
21.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
106k
    ptr += ps;
636
106k
  }
637
16.2k
  return 1;
638
16.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.24k
{
645
3.24k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
3.24k
  register _JSAMPROW ptr;
647
3.24k
  register U_CHAR *bufferptr;
648
3.24k
  register _JSAMPLE *rescale = source->rescale;
649
3.24k
  JDIMENSION col;
650
3.24k
  unsigned int maxval = source->maxval;
651
652
3.24k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
137
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
3.24k
  ptr = source->pub._buffer[0];
655
3.24k
  bufferptr = source->iobuffer;
656
3.24k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
20.4k
    for (col = cinfo->image_width; col > 0; col--) {
658
18.9k
      register unsigned int r, g, b;
659
18.9k
      r  = UCH(*bufferptr++) << 8;
660
18.9k
      r |= UCH(*bufferptr++);
661
18.9k
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
18.9k
      g  = UCH(*bufferptr++) << 8;
664
18.9k
      g |= UCH(*bufferptr++);
665
18.9k
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
18.9k
      b  = UCH(*bufferptr++) << 8;
668
18.9k
      b |= UCH(*bufferptr++);
669
18.9k
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
18.9k
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
18.9k
                  ptr + 2, ptr + 3);
673
18.9k
      ptr += 4;
674
18.9k
    }
675
1.74k
  } else {
676
4.13k
    for (col = cinfo->image_width; col > 0; col--) {
677
2.39k
      register unsigned int r, g, b;
678
2.39k
      r  = UCH(*bufferptr++) << 8;
679
2.39k
      r |= UCH(*bufferptr++);
680
2.39k
      if (r > maxval)
681
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
2.39k
      g  = UCH(*bufferptr++) << 8;
683
2.39k
      g |= UCH(*bufferptr++);
684
2.39k
      if (g > maxval)
685
26
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
2.39k
      b  = UCH(*bufferptr++) << 8;
687
2.39k
      b |= UCH(*bufferptr++);
688
2.39k
      if (b > maxval)
689
22
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
2.39k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
2.39k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
2.39k
      ptr += 4;
693
2.39k
    }
694
1.74k
  }
695
3.24k
  return 1;
696
3.24k
}
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.24k
{
645
3.24k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
3.24k
  register _JSAMPROW ptr;
647
3.24k
  register U_CHAR *bufferptr;
648
3.24k
  register _JSAMPLE *rescale = source->rescale;
649
3.24k
  JDIMENSION col;
650
3.24k
  unsigned int maxval = source->maxval;
651
652
3.24k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
137
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
3.24k
  ptr = source->pub._buffer[0];
655
3.24k
  bufferptr = source->iobuffer;
656
3.24k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
20.4k
    for (col = cinfo->image_width; col > 0; col--) {
658
18.9k
      register unsigned int r, g, b;
659
18.9k
      r  = UCH(*bufferptr++) << 8;
660
18.9k
      r |= UCH(*bufferptr++);
661
18.9k
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
18.9k
      g  = UCH(*bufferptr++) << 8;
664
18.9k
      g |= UCH(*bufferptr++);
665
18.9k
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
18.9k
      b  = UCH(*bufferptr++) << 8;
668
18.9k
      b |= UCH(*bufferptr++);
669
18.9k
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
18.9k
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
18.9k
                  ptr + 2, ptr + 3);
673
18.9k
      ptr += 4;
674
18.9k
    }
675
1.74k
  } else {
676
4.13k
    for (col = cinfo->image_width; col > 0; col--) {
677
2.39k
      register unsigned int r, g, b;
678
2.39k
      r  = UCH(*bufferptr++) << 8;
679
2.39k
      r |= UCH(*bufferptr++);
680
2.39k
      if (r > maxval)
681
37
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
2.39k
      g  = UCH(*bufferptr++) << 8;
683
2.39k
      g |= UCH(*bufferptr++);
684
2.39k
      if (g > maxval)
685
26
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
2.39k
      b  = UCH(*bufferptr++) << 8;
687
2.39k
      b |= UCH(*bufferptr++);
688
2.39k
      if (b > maxval)
689
22
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
2.39k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
2.39k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
2.39k
      ptr += 4;
693
2.39k
    }
694
1.74k
  }
695
3.24k
  return 1;
696
3.24k
}
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.5k
{
706
12.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
12.5k
  int c;
708
12.5k
  unsigned int w, h, maxval;
709
12.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
12.5k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
12.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
12.5k
  switch (c) {
718
1.51k
  case '2':                     /* it's a text-format PGM file */
719
3.43k
  case '3':                     /* it's a text-format PPM file */
720
9.39k
  case '5':                     /* it's a raw-format PGM file */
721
12.4k
  case '6':                     /* it's a raw-format PPM file */
722
12.4k
    break;
723
35
  default:
724
35
    ERREXIT(cinfo, JERR_PPM_NOT);
725
35
    break;
726
12.5k
  }
727
728
  /* fetch the remaining header info */
729
12.4k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
12.4k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
12.4k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
12.4k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
35
    ERREXIT(cinfo, JERR_PPM_NOT);
735
12.4k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
245
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
12.4k
  cinfo->image_width = (JDIMENSION)w;
739
12.4k
  cinfo->image_height = (JDIMENSION)h;
740
12.4k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
12.4k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
12.4k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
12.4k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
12.4k
  switch (c) {
748
1.22k
  case '2':                     /* it's a text-format PGM file */
749
1.22k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
1.22k
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
1.22k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
1.22k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
175
      source->pub.get_pixel_rows = get_text_gray_row;
755
1.05k
    else if (IsExtRGB(cinfo->in_color_space))
756
875
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
175
    else if (cinfo->in_color_space == JCS_CMYK)
758
175
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
1.22k
    need_iobuffer = FALSE;
762
1.22k
    break;
763
764
1.45k
  case '3':                     /* it's a text-format PPM file */
765
1.45k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.45k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.45k
    if (IsExtRGB(cinfo->in_color_space))
769
1.04k
      source->pub.get_pixel_rows = get_text_rgb_row;
770
416
    else if (cinfo->in_color_space == JCS_CMYK)
771
208
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
208
    else
773
208
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.45k
    need_iobuffer = FALSE;
775
1.45k
    break;
776
777
5.61k
  case '5':                     /* it's a raw-format PGM file */
778
5.61k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
5.61k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
5.61k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
5.61k
    if (maxval > 255) {
783
1.32k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
189
        source->pub.get_pixel_rows = get_word_gray_row;
785
1.13k
      else if (IsExtRGB(cinfo->in_color_space))
786
945
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
189
      else if (cinfo->in_color_space == JCS_CMYK)
788
189
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
4.29k
    } 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.29k
    } else {
798
4.29k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
613
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
3.67k
      else if (IsExtRGB(cinfo->in_color_space))
801
3.06k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
613
      else if (cinfo->in_color_space == JCS_CMYK)
803
613
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
4.29k
    }
807
5.61k
    break;
808
809
2.73k
  case '6':                     /* it's a raw-format PPM file */
810
2.73k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.73k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.73k
    if (maxval > 255) {
814
1.63k
      if (IsExtRGB(cinfo->in_color_space))
815
1.17k
        source->pub.get_pixel_rows = get_word_rgb_row;
816
468
      else if (cinfo->in_color_space == JCS_CMYK)
817
234
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
234
      else
819
234
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.63k
    } 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.09k
    } else {
832
1.09k
      if (IsExtRGB(cinfo->in_color_space))
833
780
        source->pub.get_pixel_rows = get_rgb_row;
834
312
      else if (cinfo->in_color_space == JCS_CMYK)
835
156
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
156
      else
837
156
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.09k
    }
839
2.73k
    break;
840
12.4k
  }
841
842
10.4k
  if (IsExtRGB(cinfo->in_color_space))
843
7.87k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
2.55k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
977
    cinfo->input_components = 1;
846
1.57k
  else if (cinfo->in_color_space == JCS_CMYK)
847
1.57k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
10.4k
  if (need_iobuffer) {
851
7.95k
    if (c == '6')
852
2.34k
      source->buffer_width = (size_t)w * 3 *
853
2.34k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
5.61k
    else
855
5.61k
      source->buffer_width = (size_t)w *
856
5.61k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
7.95k
    source->iobuffer = (U_CHAR *)
858
7.95k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
7.95k
                                  source->buffer_width);
860
7.95k
  }
861
862
  /* Create compressor input buffer. */
863
10.4k
  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.4k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
10.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
10.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
10.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
10.4k
    source->pub.buffer_height = 1;
875
10.4k
  }
876
877
  /* Compute the rescaling array if required. */
878
10.4k
  if (need_rescale) {
879
10.4k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
10.4k
    source->rescale = (_JSAMPLE *)
883
10.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
10.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
10.4k
                                           sizeof(_JSAMPLE)));
886
10.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
10.4k
                                        sizeof(_JSAMPLE)));
888
10.4k
    half_maxval = maxval / 2;
889
146M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
146M
      source->rescale[val] =
892
146M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
146M
                   maxval);
894
146M
    }
895
10.4k
  }
896
10.4k
}
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.5k
{
706
12.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
12.5k
  int c;
708
12.5k
  unsigned int w, h, maxval;
709
12.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
12.5k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
12.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
12.5k
  switch (c) {
718
1.51k
  case '2':                     /* it's a text-format PGM file */
719
3.43k
  case '3':                     /* it's a text-format PPM file */
720
9.39k
  case '5':                     /* it's a raw-format PGM file */
721
12.4k
  case '6':                     /* it's a raw-format PPM file */
722
12.4k
    break;
723
35
  default:
724
35
    ERREXIT(cinfo, JERR_PPM_NOT);
725
35
    break;
726
12.5k
  }
727
728
  /* fetch the remaining header info */
729
12.4k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
12.4k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
12.4k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
12.4k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
35
    ERREXIT(cinfo, JERR_PPM_NOT);
735
12.4k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
245
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
12.4k
  cinfo->image_width = (JDIMENSION)w;
739
12.4k
  cinfo->image_height = (JDIMENSION)h;
740
12.4k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
12.4k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
12.4k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
12.4k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
12.4k
  switch (c) {
748
1.22k
  case '2':                     /* it's a text-format PGM file */
749
1.22k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
1.22k
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
1.22k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
1.22k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
175
      source->pub.get_pixel_rows = get_text_gray_row;
755
1.05k
    else if (IsExtRGB(cinfo->in_color_space))
756
875
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
175
    else if (cinfo->in_color_space == JCS_CMYK)
758
175
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
1.22k
    need_iobuffer = FALSE;
762
1.22k
    break;
763
764
1.45k
  case '3':                     /* it's a text-format PPM file */
765
1.45k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.45k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.45k
    if (IsExtRGB(cinfo->in_color_space))
769
1.04k
      source->pub.get_pixel_rows = get_text_rgb_row;
770
416
    else if (cinfo->in_color_space == JCS_CMYK)
771
208
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
208
    else
773
208
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.45k
    need_iobuffer = FALSE;
775
1.45k
    break;
776
777
5.61k
  case '5':                     /* it's a raw-format PGM file */
778
5.61k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
5.61k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
5.61k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
5.61k
    if (maxval > 255) {
783
1.32k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
189
        source->pub.get_pixel_rows = get_word_gray_row;
785
1.13k
      else if (IsExtRGB(cinfo->in_color_space))
786
945
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
189
      else if (cinfo->in_color_space == JCS_CMYK)
788
189
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
4.29k
    } 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.29k
    } else {
798
4.29k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
613
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
3.67k
      else if (IsExtRGB(cinfo->in_color_space))
801
3.06k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
613
      else if (cinfo->in_color_space == JCS_CMYK)
803
613
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
4.29k
    }
807
5.61k
    break;
808
809
2.73k
  case '6':                     /* it's a raw-format PPM file */
810
2.73k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.73k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.73k
    if (maxval > 255) {
814
1.63k
      if (IsExtRGB(cinfo->in_color_space))
815
1.17k
        source->pub.get_pixel_rows = get_word_rgb_row;
816
468
      else if (cinfo->in_color_space == JCS_CMYK)
817
234
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
234
      else
819
234
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.63k
    } 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.09k
    } else {
832
1.09k
      if (IsExtRGB(cinfo->in_color_space))
833
780
        source->pub.get_pixel_rows = get_rgb_row;
834
312
      else if (cinfo->in_color_space == JCS_CMYK)
835
156
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
156
      else
837
156
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.09k
    }
839
2.73k
    break;
840
12.4k
  }
841
842
10.4k
  if (IsExtRGB(cinfo->in_color_space))
843
7.87k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
2.55k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
977
    cinfo->input_components = 1;
846
1.57k
  else if (cinfo->in_color_space == JCS_CMYK)
847
1.57k
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
10.4k
  if (need_iobuffer) {
851
7.95k
    if (c == '6')
852
2.34k
      source->buffer_width = (size_t)w * 3 *
853
2.34k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
5.61k
    else
855
5.61k
      source->buffer_width = (size_t)w *
856
5.61k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
7.95k
    source->iobuffer = (U_CHAR *)
858
7.95k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
7.95k
                                  source->buffer_width);
860
7.95k
  }
861
862
  /* Create compressor input buffer. */
863
10.4k
  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.4k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
10.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
10.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
10.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
10.4k
    source->pub.buffer_height = 1;
875
10.4k
  }
876
877
  /* Compute the rescaling array if required. */
878
10.4k
  if (need_rescale) {
879
10.4k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
10.4k
    source->rescale = (_JSAMPLE *)
883
10.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
10.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
10.4k
                                           sizeof(_JSAMPLE)));
886
10.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
10.4k
                                        sizeof(_JSAMPLE)));
888
10.4k
    half_maxval = maxval / 2;
889
146M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
146M
      source->rescale[val] =
892
146M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
146M
                   maxval);
894
146M
    }
895
10.4k
  }
896
10.4k
}
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.59k
{
906
  /* no work */
907
4.59k
}
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.59k
{
906
  /* no work */
907
4.59k
}
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.5k
{
917
12.5k
  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.5k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
12.5k
      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.5k
  source = (ppm_source_ptr)
929
12.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
12.5k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
12.5k
  source->pub.start_input = start_input_ppm;
933
12.5k
  source->pub.finish_input = finish_input_ppm;
934
12.5k
  source->pub.max_pixels = 0;
935
936
12.5k
  return (cjpeg_source_ptr)source;
937
12.5k
}
Unexecuted instantiation: jinit_read_ppm
Unexecuted instantiation: j12init_read_ppm
j16init_read_ppm
Line
Count
Source
916
12.5k
{
917
12.5k
  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.5k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
12.5k
      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.5k
  source = (ppm_source_ptr)
929
12.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
12.5k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
12.5k
  source->pub.start_input = start_input_ppm;
933
12.5k
  source->pub.finish_input = finish_input_ppm;
934
12.5k
  source->pub.max_pixels = 0;
935
936
12.5k
  return (cjpeg_source_ptr)source;
937
12.5k
}
938
939
#endif /* defined(PPM_SUPPORTED) &&
940
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */