Coverage Report

Created: 2025-08-26 06:41

/src/libjpeg-turbo.main/src/rdppm.c
Line
Count
Source (jump to first uncovered line)
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-2024, 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
32.4M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
15.1M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
53
54
static int alpha_index[JPEG_NUMCS] = {
55
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
56
};
57
58
59
/* Private version of data source object */
60
61
typedef struct {
62
  struct cjpeg_source_struct pub; /* public fields */
63
64
  /* Usually these two pointers point to the same place: */
65
  U_CHAR *iobuffer;             /* fread's I/O buffer */
66
  _JSAMPROW pixrow;             /* compressor input buffer */
67
  size_t buffer_width;          /* width of I/O buffer */
68
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
69
  unsigned int maxval;
70
} ppm_source_struct;
71
72
typedef ppm_source_struct *ppm_source_ptr;
73
74
75
LOCAL(int)
76
pbm_getc(FILE *infile)
77
/* Read next char, skipping over any comments */
78
/* A comment/newline sequence is returned as a newline */
79
306k
{
80
306k
  register int ch;
81
82
306k
  ch = getc(infile);
83
306k
  if (ch == '#') {
84
33.4k
    do {
85
33.4k
      ch = getc(infile);
86
33.4k
    } while (ch != '\n' && ch != EOF);
87
2.91k
  }
88
306k
  return ch;
89
306k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
306k
{
80
306k
  register int ch;
81
82
306k
  ch = getc(infile);
83
306k
  if (ch == '#') {
84
33.4k
    do {
85
33.4k
      ch = getc(infile);
86
33.4k
    } while (ch != '\n' && ch != EOF);
87
2.91k
  }
88
306k
  return ch;
89
306k
}
Unexecuted instantiation: rdppm-12.c:pbm_getc
Unexecuted instantiation: rdppm-16.c:pbm_getc
90
91
92
LOCAL(unsigned int)
93
read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
94
/* Read an unsigned decimal integer from the PPM file */
95
/* Swallows one trailing character after the integer */
96
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
97
/* This should not be a problem in practice. */
98
137k
{
99
137k
  register int ch;
100
137k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
144k
  do {
104
144k
    ch = pbm_getc(infile);
105
144k
    if (ch == EOF)
106
3.30k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
144k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
137k
  if (ch < '0' || ch > '9')
110
230
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
137k
  val = ch - '0';
113
165k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
28.0k
    val *= 10;
115
28.0k
    val += ch - '0';
116
28.0k
    if (val > maxval)
117
132
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
28.0k
  }
119
120
137k
  return val;
121
137k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
137k
{
99
137k
  register int ch;
100
137k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
144k
  do {
104
144k
    ch = pbm_getc(infile);
105
144k
    if (ch == EOF)
106
3.30k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
144k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
137k
  if (ch < '0' || ch > '9')
110
230
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
137k
  val = ch - '0';
113
165k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
28.0k
    val *= 10;
115
28.0k
    val += ch - '0';
116
28.0k
    if (val > maxval)
117
132
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
28.0k
  }
119
120
137k
  return val;
121
137k
}
Unexecuted instantiation: rdppm-12.c:read_pbm_integer
Unexecuted instantiation: rdppm-16.c:read_pbm_integer
122
123
124
/*
125
 * Read one row of pixels.
126
 *
127
 * We provide several different versions depending on input file format.
128
 * In all cases, input is scaled to cinfo->data_precision.
129
 *
130
 * A really fast path is provided for reading byte/sample raw files with
131
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U,
132
 * which is the normal case for 8-bit data.
133
 */
134
135
136
METHODDEF(JDIMENSION)
137
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
138
/* This version is for reading text-format PGM files with any maxval */
139
1.99k
{
140
1.99k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.99k
  FILE *infile = source->pub.input_file;
142
1.99k
  register _JSAMPROW ptr;
143
1.99k
  register _JSAMPLE *rescale = source->rescale;
144
1.99k
  JDIMENSION col;
145
1.99k
  unsigned int maxval = source->maxval;
146
147
1.99k
  ptr = source->pub._buffer[0];
148
5.84k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.85k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.85k
  }
151
1.99k
  return 1;
152
1.99k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
1.99k
{
140
1.99k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.99k
  FILE *infile = source->pub.input_file;
142
1.99k
  register _JSAMPROW ptr;
143
1.99k
  register _JSAMPLE *rescale = source->rescale;
144
1.99k
  JDIMENSION col;
145
1.99k
  unsigned int maxval = source->maxval;
146
147
1.99k
  ptr = source->pub._buffer[0];
148
5.84k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.85k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.85k
  }
151
1.99k
  return 1;
152
1.99k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
10.8M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
89.9M
  for (col = cinfo->image_width; col > 0; col--) { \
157
79.1M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
79.1M
    alpha_set_op \
159
79.1M
    ptr += ps; \
160
79.1M
  } \
161
10.8M
}
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.95k
{
168
9.95k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
9.95k
  FILE *infile = source->pub.input_file;
170
9.95k
  register _JSAMPROW ptr;
171
9.95k
  register _JSAMPLE *rescale = source->rescale;
172
9.95k
  JDIMENSION col;
173
9.95k
  unsigned int maxval = source->maxval;
174
9.95k
  register int rindex = rgb_red[cinfo->in_color_space];
175
9.95k
  register int gindex = rgb_green[cinfo->in_color_space];
176
9.95k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
9.95k
  register int aindex = alpha_index[cinfo->in_color_space];
178
9.95k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
9.95k
  ptr = source->pub._buffer[0];
181
9.95k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
705
    if (aindex >= 0)
183
223
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
705
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
482
    else
186
482
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
9.24k
  } else {
188
9.24k
    if (aindex >= 0)
189
1.76k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
9.24k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.47k
    else
192
7.47k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
9.24k
  }
194
9.95k
  return 1;
195
9.95k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
9.95k
{
168
9.95k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
9.95k
  FILE *infile = source->pub.input_file;
170
9.95k
  register _JSAMPROW ptr;
171
9.95k
  register _JSAMPLE *rescale = source->rescale;
172
9.95k
  JDIMENSION col;
173
9.95k
  unsigned int maxval = source->maxval;
174
9.95k
  register int rindex = rgb_red[cinfo->in_color_space];
175
9.95k
  register int gindex = rgb_green[cinfo->in_color_space];
176
9.95k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
9.95k
  register int aindex = alpha_index[cinfo->in_color_space];
178
9.95k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
9.95k
  ptr = source->pub._buffer[0];
181
9.95k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
705
    if (aindex >= 0)
183
223
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
705
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
482
    else
186
482
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
9.24k
  } else {
188
9.24k
    if (aindex >= 0)
189
1.76k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
9.24k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.47k
    else
192
7.47k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
9.24k
  }
194
9.95k
  return 1;
195
9.95k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_rgb_row
196
197
198
METHODDEF(JDIMENSION)
199
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
200
/* This version is for reading text-format PGM files with any maxval and
201
   converting to CMYK */
202
1.99k
{
203
1.99k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.99k
  FILE *infile = source->pub.input_file;
205
1.99k
  register _JSAMPROW ptr;
206
1.99k
  register _JSAMPLE *rescale = source->rescale;
207
1.99k
  JDIMENSION col;
208
1.99k
  unsigned int maxval = source->maxval;
209
210
1.99k
  ptr = source->pub._buffer[0];
211
1.99k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
2.09k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.23k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.23k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.23k
      ptr += 4;
216
1.23k
    }
217
1.13k
  } else {
218
3.74k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.61k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.61k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
2.61k
      ptr += 4;
222
2.61k
    }
223
1.13k
  }
224
1.99k
  return 1;
225
1.99k
}
rdppm-8.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.99k
{
203
1.99k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.99k
  FILE *infile = source->pub.input_file;
205
1.99k
  register _JSAMPROW ptr;
206
1.99k
  register _JSAMPLE *rescale = source->rescale;
207
1.99k
  JDIMENSION col;
208
1.99k
  unsigned int maxval = source->maxval;
209
210
1.99k
  ptr = source->pub._buffer[0];
211
1.99k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
2.09k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.23k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.23k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.23k
      ptr += 4;
216
1.23k
    }
217
1.13k
  } else {
218
3.74k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.61k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.61k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
2.61k
      ptr += 4;
222
2.61k
    }
223
1.13k
  }
224
1.99k
  return 1;
225
1.99k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row
226
227
228
30.9k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
1.73M
  for (col = cinfo->image_width; col > 0; col--) { \
230
1.70M
    ptr[rindex] = read_op; \
231
1.70M
    ptr[gindex] = read_op; \
232
1.70M
    ptr[bindex] = read_op; \
233
1.70M
    alpha_set_op \
234
1.70M
    ptr += ps; \
235
1.70M
  } \
236
30.9k
}
237
238
METHODDEF(JDIMENSION)
239
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
240
/* This version is for reading text-format PPM files with any maxval */
241
12.1k
{
242
12.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
12.1k
  FILE *infile = source->pub.input_file;
244
12.1k
  register _JSAMPROW ptr;
245
12.1k
  register _JSAMPLE *rescale = source->rescale;
246
12.1k
  JDIMENSION col;
247
12.1k
  unsigned int maxval = source->maxval;
248
12.1k
  register int rindex = rgb_red[cinfo->in_color_space];
249
12.1k
  register int gindex = rgb_green[cinfo->in_color_space];
250
12.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
12.1k
  register int aindex = alpha_index[cinfo->in_color_space];
252
12.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
12.1k
  ptr = source->pub._buffer[0];
255
12.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
713
    if (aindex >= 0)
257
221
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
713
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
492
    else
260
492
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
11.4k
  } else {
262
11.4k
    if (aindex >= 0)
263
2.21k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
11.4k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
9.25k
    else
266
9.25k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
11.4k
  }
268
12.1k
  return 1;
269
12.1k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
241
12.1k
{
242
12.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
12.1k
  FILE *infile = source->pub.input_file;
244
12.1k
  register _JSAMPROW ptr;
245
12.1k
  register _JSAMPLE *rescale = source->rescale;
246
12.1k
  JDIMENSION col;
247
12.1k
  unsigned int maxval = source->maxval;
248
12.1k
  register int rindex = rgb_red[cinfo->in_color_space];
249
12.1k
  register int gindex = rgb_green[cinfo->in_color_space];
250
12.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
12.1k
  register int aindex = alpha_index[cinfo->in_color_space];
252
12.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
12.1k
  ptr = source->pub._buffer[0];
255
12.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
713
    if (aindex >= 0)
257
221
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
713
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
492
    else
260
492
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
11.4k
  } else {
262
11.4k
    if (aindex >= 0)
263
2.21k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
11.4k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
9.25k
    else
266
9.25k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
11.4k
  }
268
12.1k
  return 1;
269
12.1k
}
Unexecuted instantiation: rdppm-12.c:get_text_rgb_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_row
270
271
272
METHODDEF(JDIMENSION)
273
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
274
/* This version is for reading text-format PPM files with any maxval and
275
   converting to CMYK */
276
2.43k
{
277
2.43k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.43k
  FILE *infile = source->pub.input_file;
279
2.43k
  register _JSAMPROW ptr;
280
2.43k
  register _JSAMPLE *rescale = source->rescale;
281
2.43k
  JDIMENSION col;
282
2.43k
  unsigned int maxval = source->maxval;
283
284
2.43k
  ptr = source->pub._buffer[0];
285
2.43k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.67k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.47k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.47k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.47k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.47k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.47k
      ptr += 4;
292
1.47k
    }
293
1.24k
  } else {
294
4.05k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.80k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.80k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.80k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.80k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.80k
      ptr += 4;
300
2.80k
    }
301
1.24k
  }
302
2.43k
  return 1;
303
2.43k
}
rdppm-8.c:get_text_rgb_cmyk_row
Line
Count
Source
276
2.43k
{
277
2.43k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.43k
  FILE *infile = source->pub.input_file;
279
2.43k
  register _JSAMPROW ptr;
280
2.43k
  register _JSAMPLE *rescale = source->rescale;
281
2.43k
  JDIMENSION col;
282
2.43k
  unsigned int maxval = source->maxval;
283
284
2.43k
  ptr = source->pub._buffer[0];
285
2.43k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.67k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.47k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.47k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.47k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.47k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.47k
      ptr += 4;
292
1.47k
    }
293
1.24k
  } else {
294
4.05k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.80k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.80k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.80k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.80k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.80k
      ptr += 4;
300
2.80k
    }
301
1.24k
  }
302
2.43k
  return 1;
303
2.43k
}
Unexecuted instantiation: rdppm-12.c:get_text_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_cmyk_row
304
305
306
METHODDEF(JDIMENSION)
307
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
308
/* This version is for reading raw-byte-format PGM files with any maxval */
309
2.15M
{
310
2.15M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.15M
  register _JSAMPROW ptr;
312
2.15M
  register U_CHAR *bufferptr;
313
2.15M
  register _JSAMPLE *rescale = source->rescale;
314
2.15M
  JDIMENSION col;
315
316
2.15M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
106
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.15M
  ptr = source->pub._buffer[0];
319
2.15M
  bufferptr = source->iobuffer;
320
17.7M
  for (col = cinfo->image_width; col > 0; col--) {
321
15.6M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
15.6M
  }
323
2.15M
  return 1;
324
2.15M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
309
2.15M
{
310
2.15M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.15M
  register _JSAMPROW ptr;
312
2.15M
  register U_CHAR *bufferptr;
313
2.15M
  register _JSAMPLE *rescale = source->rescale;
314
2.15M
  JDIMENSION col;
315
316
2.15M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
106
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.15M
  ptr = source->pub._buffer[0];
319
2.15M
  bufferptr = source->iobuffer;
320
17.7M
  for (col = cinfo->image_width; col > 0; col--) {
321
15.6M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
15.6M
  }
323
2.15M
  return 1;
324
2.15M
}
Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row
Unexecuted instantiation: rdppm-16.c:get_scaled_gray_row
325
326
327
METHODDEF(JDIMENSION)
328
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
329
/* This version is for reading raw-byte-format PGM files with any maxval
330
   and converting to extended RGB */
331
10.8M
{
332
10.8M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
10.8M
  register _JSAMPROW ptr;
334
10.8M
  register U_CHAR *bufferptr;
335
10.8M
  register _JSAMPLE *rescale = source->rescale;
336
10.8M
  JDIMENSION col;
337
10.8M
  unsigned int maxval = source->maxval;
338
10.8M
  register int rindex = rgb_red[cinfo->in_color_space];
339
10.8M
  register int gindex = rgb_green[cinfo->in_color_space];
340
10.8M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
10.8M
  register int aindex = alpha_index[cinfo->in_color_space];
342
10.8M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
10.8M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
665
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
10.8M
  ptr = source->pub._buffer[0];
347
10.8M
  bufferptr = source->iobuffer;
348
10.8M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
277k
    if (aindex >= 0)
350
1.37k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
276k
    else
352
276k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
10.5M
  } else {
354
10.5M
    if (aindex >= 0)
355
2.16M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
10.5M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
8.37M
    else
358
8.37M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
10.5M
  }
360
10.8M
  return 1;
361
10.8M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
331
10.8M
{
332
10.8M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
10.8M
  register _JSAMPROW ptr;
334
10.8M
  register U_CHAR *bufferptr;
335
10.8M
  register _JSAMPLE *rescale = source->rescale;
336
10.8M
  JDIMENSION col;
337
10.8M
  unsigned int maxval = source->maxval;
338
10.8M
  register int rindex = rgb_red[cinfo->in_color_space];
339
10.8M
  register int gindex = rgb_green[cinfo->in_color_space];
340
10.8M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
10.8M
  register int aindex = alpha_index[cinfo->in_color_space];
342
10.8M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
10.8M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
665
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
10.8M
  ptr = source->pub._buffer[0];
347
10.8M
  bufferptr = source->iobuffer;
348
10.8M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
277k
    if (aindex >= 0)
350
1.37k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
276k
    else
352
276k
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
10.5M
  } else {
354
10.5M
    if (aindex >= 0)
355
2.16M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
10.5M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
8.37M
    else
358
8.37M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
10.5M
  }
360
10.8M
  return 1;
361
10.8M
}
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_gray_rgb_row
362
363
364
METHODDEF(JDIMENSION)
365
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
366
/* This version is for reading raw-byte-format PGM files with any maxval
367
   and converting to CMYK */
368
2.16M
{
369
2.16M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.16M
  register _JSAMPROW ptr;
371
2.16M
  register U_CHAR *bufferptr;
372
2.16M
  register _JSAMPLE *rescale = source->rescale;
373
2.16M
  JDIMENSION col;
374
2.16M
  unsigned int maxval = source->maxval;
375
376
2.16M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
133
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.16M
  ptr = source->pub._buffer[0];
379
2.16M
  bufferptr = source->iobuffer;
380
2.16M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
777k
    for (col = cinfo->image_width; col > 0; col--) {
382
733k
      _JSAMPLE gray = *bufferptr++;
383
733k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
733k
      ptr += 4;
385
733k
    }
386
2.11M
  } else {
387
17.2M
    for (col = cinfo->image_width; col > 0; col--) {
388
15.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
15.0M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
15.0M
      ptr += 4;
391
15.0M
    }
392
2.11M
  }
393
2.16M
  return 1;
394
2.16M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
368
2.16M
{
369
2.16M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.16M
  register _JSAMPROW ptr;
371
2.16M
  register U_CHAR *bufferptr;
372
2.16M
  register _JSAMPLE *rescale = source->rescale;
373
2.16M
  JDIMENSION col;
374
2.16M
  unsigned int maxval = source->maxval;
375
376
2.16M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
133
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.16M
  ptr = source->pub._buffer[0];
379
2.16M
  bufferptr = source->iobuffer;
380
2.16M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
777k
    for (col = cinfo->image_width; col > 0; col--) {
382
733k
      _JSAMPLE gray = *bufferptr++;
383
733k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
733k
      ptr += 4;
385
733k
    }
386
2.11M
  } else {
387
17.2M
    for (col = cinfo->image_width; col > 0; col--) {
388
15.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
15.0M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
15.0M
      ptr += 4;
391
15.0M
    }
392
2.11M
  }
393
2.16M
  return 1;
394
2.16M
}
Unexecuted instantiation: rdppm-12.c:get_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_gray_cmyk_row
395
396
397
METHODDEF(JDIMENSION)
398
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
399
/* This version is for reading raw-byte-format PPM files with any maxval */
400
18.7k
{
401
18.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
18.7k
  register _JSAMPROW ptr;
403
18.7k
  register U_CHAR *bufferptr;
404
18.7k
  register _JSAMPLE *rescale = source->rescale;
405
18.7k
  JDIMENSION col;
406
18.7k
  unsigned int maxval = source->maxval;
407
18.7k
  register int rindex = rgb_red[cinfo->in_color_space];
408
18.7k
  register int gindex = rgb_green[cinfo->in_color_space];
409
18.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
18.7k
  register int aindex = alpha_index[cinfo->in_color_space];
411
18.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
18.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
764
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
18.7k
  ptr = source->pub._buffer[0];
416
18.7k
  bufferptr = source->iobuffer;
417
18.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
669
    if (aindex >= 0)
419
204
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
465
    else
421
465
      RGB_READ_LOOP(*bufferptr++, {})
422
18.0k
  } else {
423
18.0k
    if (aindex >= 0)
424
3.39k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
18.0k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
14.6k
    else
427
14.6k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
18.0k
  }
429
18.7k
  return 1;
430
18.7k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
400
18.7k
{
401
18.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
18.7k
  register _JSAMPROW ptr;
403
18.7k
  register U_CHAR *bufferptr;
404
18.7k
  register _JSAMPLE *rescale = source->rescale;
405
18.7k
  JDIMENSION col;
406
18.7k
  unsigned int maxval = source->maxval;
407
18.7k
  register int rindex = rgb_red[cinfo->in_color_space];
408
18.7k
  register int gindex = rgb_green[cinfo->in_color_space];
409
18.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
18.7k
  register int aindex = alpha_index[cinfo->in_color_space];
411
18.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
18.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
764
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
18.7k
  ptr = source->pub._buffer[0];
416
18.7k
  bufferptr = source->iobuffer;
417
18.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
669
    if (aindex >= 0)
419
204
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
465
    else
421
465
      RGB_READ_LOOP(*bufferptr++, {})
422
18.0k
  } else {
423
18.0k
    if (aindex >= 0)
424
3.39k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
18.0k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
14.6k
    else
427
14.6k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
18.0k
  }
429
18.7k
  return 1;
430
18.7k
}
Unexecuted instantiation: rdppm-12.c:get_rgb_row
Unexecuted instantiation: rdppm-16.c:get_rgb_row
431
432
433
METHODDEF(JDIMENSION)
434
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
435
/* This version is for reading raw-byte-format PPM files with any maxval and
436
   converting to CMYK */
437
3.75k
{
438
3.75k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
3.75k
  register _JSAMPROW ptr;
440
3.75k
  register U_CHAR *bufferptr;
441
3.75k
  register _JSAMPLE *rescale = source->rescale;
442
3.75k
  JDIMENSION col;
443
3.75k
  unsigned int maxval = source->maxval;
444
445
3.75k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
154
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
3.75k
  ptr = source->pub._buffer[0];
448
3.75k
  bufferptr = source->iobuffer;
449
3.75k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
126k
    for (col = cinfo->image_width; col > 0; col--) {
451
124k
      _JSAMPLE r = *bufferptr++;
452
124k
      _JSAMPLE g = *bufferptr++;
453
124k
      _JSAMPLE b = *bufferptr++;
454
124k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
124k
      ptr += 4;
456
124k
    }
457
2.27k
  } else {
458
234k
    for (col = cinfo->image_width; col > 0; col--) {
459
232k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
232k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
232k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
232k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
232k
      ptr += 4;
464
232k
    }
465
2.27k
  }
466
3.75k
  return 1;
467
3.75k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
437
3.75k
{
438
3.75k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
3.75k
  register _JSAMPROW ptr;
440
3.75k
  register U_CHAR *bufferptr;
441
3.75k
  register _JSAMPLE *rescale = source->rescale;
442
3.75k
  JDIMENSION col;
443
3.75k
  unsigned int maxval = source->maxval;
444
445
3.75k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
154
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
3.75k
  ptr = source->pub._buffer[0];
448
3.75k
  bufferptr = source->iobuffer;
449
3.75k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
126k
    for (col = cinfo->image_width; col > 0; col--) {
451
124k
      _JSAMPLE r = *bufferptr++;
452
124k
      _JSAMPLE g = *bufferptr++;
453
124k
      _JSAMPLE b = *bufferptr++;
454
124k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
124k
      ptr += 4;
456
124k
    }
457
2.27k
  } else {
458
234k
    for (col = cinfo->image_width; col > 0; col--) {
459
232k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
232k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
232k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
232k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
232k
      ptr += 4;
464
232k
    }
465
2.27k
  }
466
3.75k
  return 1;
467
3.75k
}
Unexecuted instantiation: rdppm-12.c:get_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_rgb_cmyk_row
468
469
470
METHODDEF(JDIMENSION)
471
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
472
/* This version is for reading raw-byte-format files with
473
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U.
474
 * In this case we just read right into the _JSAMPLE buffer!
475
 * Note that same code works for PPM and PGM files.
476
 */
477
3.37k
{
478
3.37k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
3.37k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
33
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
3.37k
  return 1;
483
3.37k
}
rdppm-8.c:get_raw_row
Line
Count
Source
477
3.37k
{
478
3.37k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
3.37k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
33
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
3.37k
  return 1;
483
3.37k
}
Unexecuted instantiation: rdppm-12.c:get_raw_row
Unexecuted instantiation: rdppm-16.c:get_raw_row
484
485
486
METHODDEF(JDIMENSION)
487
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
488
/* This version is for reading raw-word-format PGM files with any maxval */
489
3.48k
{
490
3.48k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
3.48k
  register _JSAMPROW ptr;
492
3.48k
  register U_CHAR *bufferptr;
493
3.48k
  register _JSAMPLE *rescale = source->rescale;
494
3.48k
  JDIMENSION col;
495
3.48k
  unsigned int maxval = source->maxval;
496
497
3.48k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
3.48k
  ptr = source->pub._buffer[0];
500
3.48k
  bufferptr = source->iobuffer;
501
73.3k
  for (col = cinfo->image_width; col > 0; col--) {
502
69.8k
    register unsigned int temp;
503
69.8k
    temp  = UCH(*bufferptr++) << 8;
504
69.8k
    temp |= UCH(*bufferptr++);
505
69.8k
    if (temp > maxval)
506
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
69.8k
    *ptr++ = rescale[temp];
508
69.8k
  }
509
3.48k
  return 1;
510
3.48k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
489
3.48k
{
490
3.48k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
3.48k
  register _JSAMPROW ptr;
492
3.48k
  register U_CHAR *bufferptr;
493
3.48k
  register _JSAMPLE *rescale = source->rescale;
494
3.48k
  JDIMENSION col;
495
3.48k
  unsigned int maxval = source->maxval;
496
497
3.48k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
3.48k
  ptr = source->pub._buffer[0];
500
3.48k
  bufferptr = source->iobuffer;
501
73.3k
  for (col = cinfo->image_width; col > 0; col--) {
502
69.8k
    register unsigned int temp;
503
69.8k
    temp  = UCH(*bufferptr++) << 8;
504
69.8k
    temp |= UCH(*bufferptr++);
505
69.8k
    if (temp > maxval)
506
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
69.8k
    *ptr++ = rescale[temp];
508
69.8k
  }
509
3.48k
  return 1;
510
3.48k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_row
511
512
513
METHODDEF(JDIMENSION)
514
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
515
/* This version is for reading raw-word-format PGM files with any maxval */
516
17.4k
{
517
17.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
17.4k
  register _JSAMPROW ptr;
519
17.4k
  register U_CHAR *bufferptr;
520
17.4k
  register _JSAMPLE *rescale = source->rescale;
521
17.4k
  JDIMENSION col;
522
17.4k
  unsigned int maxval = source->maxval;
523
17.4k
  register int rindex = rgb_red[cinfo->in_color_space];
524
17.4k
  register int gindex = rgb_green[cinfo->in_color_space];
525
17.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
17.4k
  register int aindex = alpha_index[cinfo->in_color_space];
527
17.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
17.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
280
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
17.4k
  ptr = source->pub._buffer[0];
532
17.4k
  bufferptr = source->iobuffer;
533
366k
  for (col = cinfo->image_width; col > 0; col--) {
534
349k
    register unsigned int temp;
535
349k
    temp  = UCH(*bufferptr++) << 8;
536
349k
    temp |= UCH(*bufferptr++);
537
349k
    if (temp > maxval)
538
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
349k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
349k
    if (aindex >= 0)
541
69.8k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
349k
    ptr += ps;
543
349k
  }
544
17.4k
  return 1;
545
17.4k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
516
17.4k
{
517
17.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
17.4k
  register _JSAMPROW ptr;
519
17.4k
  register U_CHAR *bufferptr;
520
17.4k
  register _JSAMPLE *rescale = source->rescale;
521
17.4k
  JDIMENSION col;
522
17.4k
  unsigned int maxval = source->maxval;
523
17.4k
  register int rindex = rgb_red[cinfo->in_color_space];
524
17.4k
  register int gindex = rgb_green[cinfo->in_color_space];
525
17.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
17.4k
  register int aindex = alpha_index[cinfo->in_color_space];
527
17.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
17.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
280
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
17.4k
  ptr = source->pub._buffer[0];
532
17.4k
  bufferptr = source->iobuffer;
533
366k
  for (col = cinfo->image_width; col > 0; col--) {
534
349k
    register unsigned int temp;
535
349k
    temp  = UCH(*bufferptr++) << 8;
536
349k
    temp |= UCH(*bufferptr++);
537
349k
    if (temp > maxval)
538
185
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
349k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
349k
    if (aindex >= 0)
541
69.8k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
349k
    ptr += ps;
543
349k
  }
544
17.4k
  return 1;
545
17.4k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_rgb_row
546
547
548
METHODDEF(JDIMENSION)
549
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
550
/* This version is for reading raw-word-format PGM files with any maxval */
551
3.48k
{
552
3.48k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
3.48k
  register _JSAMPROW ptr;
554
3.48k
  register U_CHAR *bufferptr;
555
3.48k
  register _JSAMPLE *rescale = source->rescale;
556
3.48k
  JDIMENSION col;
557
3.48k
  unsigned int maxval = source->maxval;
558
559
3.48k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
3.48k
  ptr = source->pub._buffer[0];
562
3.48k
  bufferptr = source->iobuffer;
563
73.3k
  for (col = cinfo->image_width; col > 0; col--) {
564
69.8k
    register unsigned int gray;
565
69.8k
    gray  = UCH(*bufferptr++) << 8;
566
69.8k
    gray |= UCH(*bufferptr++);
567
69.8k
    if (gray > maxval)
568
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
69.8k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
69.8k
                ptr + 1, ptr + 2, ptr + 3);
571
69.8k
    ptr += 4;
572
69.8k
  }
573
3.48k
  return 1;
574
3.48k
}
rdppm-8.c:get_word_gray_cmyk_row
Line
Count
Source
551
3.48k
{
552
3.48k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
3.48k
  register _JSAMPROW ptr;
554
3.48k
  register U_CHAR *bufferptr;
555
3.48k
  register _JSAMPLE *rescale = source->rescale;
556
3.48k
  JDIMENSION col;
557
3.48k
  unsigned int maxval = source->maxval;
558
559
3.48k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
3.48k
  ptr = source->pub._buffer[0];
562
3.48k
  bufferptr = source->iobuffer;
563
73.3k
  for (col = cinfo->image_width; col > 0; col--) {
564
69.8k
    register unsigned int gray;
565
69.8k
    gray  = UCH(*bufferptr++) << 8;
566
69.8k
    gray |= UCH(*bufferptr++);
567
69.8k
    if (gray > maxval)
568
37
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
69.8k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
69.8k
                ptr + 1, ptr + 2, ptr + 3);
571
69.8k
    ptr += 4;
572
69.8k
  }
573
3.48k
  return 1;
574
3.48k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_cmyk_row
575
576
577
METHODDEF(JDIMENSION)
578
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
579
/* This version is for reading raw-word-format PPM files with any maxval */
580
9.08k
{
581
9.08k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
9.08k
  register _JSAMPROW ptr;
583
9.08k
  register U_CHAR *bufferptr;
584
9.08k
  register _JSAMPLE *rescale = source->rescale;
585
9.08k
  JDIMENSION col;
586
9.08k
  unsigned int maxval = source->maxval;
587
9.08k
  register int rindex = rgb_red[cinfo->in_color_space];
588
9.08k
  register int gindex = rgb_green[cinfo->in_color_space];
589
9.08k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
9.08k
  register int aindex = alpha_index[cinfo->in_color_space];
591
9.08k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
9.08k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
355
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
9.08k
  ptr = source->pub._buffer[0];
596
9.08k
  bufferptr = source->iobuffer;
597
24.1k
  for (col = cinfo->image_width; col > 0; col--) {
598
15.1k
    register unsigned int temp;
599
15.1k
    temp  = UCH(*bufferptr++) << 8;
600
15.1k
    temp |= UCH(*bufferptr++);
601
15.1k
    if (temp > maxval)
602
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
15.1k
    ptr[rindex] = rescale[temp];
604
15.1k
    temp  = UCH(*bufferptr++) << 8;
605
15.1k
    temp |= UCH(*bufferptr++);
606
15.1k
    if (temp > maxval)
607
115
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
15.1k
    ptr[gindex] = rescale[temp];
609
15.1k
    temp  = UCH(*bufferptr++) << 8;
610
15.1k
    temp |= UCH(*bufferptr++);
611
15.1k
    if (temp > maxval)
612
95
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
15.1k
    ptr[bindex] = rescale[temp];
614
15.1k
    if (aindex >= 0)
615
2.94k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
15.1k
    ptr += ps;
617
15.1k
  }
618
9.08k
  return 1;
619
9.08k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
580
9.08k
{
581
9.08k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
9.08k
  register _JSAMPROW ptr;
583
9.08k
  register U_CHAR *bufferptr;
584
9.08k
  register _JSAMPLE *rescale = source->rescale;
585
9.08k
  JDIMENSION col;
586
9.08k
  unsigned int maxval = source->maxval;
587
9.08k
  register int rindex = rgb_red[cinfo->in_color_space];
588
9.08k
  register int gindex = rgb_green[cinfo->in_color_space];
589
9.08k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
9.08k
  register int aindex = alpha_index[cinfo->in_color_space];
591
9.08k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
9.08k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
355
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
9.08k
  ptr = source->pub._buffer[0];
596
9.08k
  bufferptr = source->iobuffer;
597
24.1k
  for (col = cinfo->image_width; col > 0; col--) {
598
15.1k
    register unsigned int temp;
599
15.1k
    temp  = UCH(*bufferptr++) << 8;
600
15.1k
    temp |= UCH(*bufferptr++);
601
15.1k
    if (temp > maxval)
602
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
15.1k
    ptr[rindex] = rescale[temp];
604
15.1k
    temp  = UCH(*bufferptr++) << 8;
605
15.1k
    temp |= UCH(*bufferptr++);
606
15.1k
    if (temp > maxval)
607
115
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
15.1k
    ptr[gindex] = rescale[temp];
609
15.1k
    temp  = UCH(*bufferptr++) << 8;
610
15.1k
    temp |= UCH(*bufferptr++);
611
15.1k
    if (temp > maxval)
612
95
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
15.1k
    ptr[bindex] = rescale[temp];
614
15.1k
    if (aindex >= 0)
615
2.94k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
15.1k
    ptr += ps;
617
15.1k
  }
618
9.08k
  return 1;
619
9.08k
}
Unexecuted instantiation: rdppm-12.c:get_word_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_row
620
621
622
METHODDEF(JDIMENSION)
623
get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
624
/* This version is for reading raw-word-format PPM files with any maxval */
625
1.81k
{
626
1.81k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
1.81k
  register _JSAMPROW ptr;
628
1.81k
  register U_CHAR *bufferptr;
629
1.81k
  register _JSAMPLE *rescale = source->rescale;
630
1.81k
  JDIMENSION col;
631
1.81k
  unsigned int maxval = source->maxval;
632
633
1.81k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
71
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
1.81k
  ptr = source->pub._buffer[0];
636
1.81k
  bufferptr = source->iobuffer;
637
4.83k
  for (col = cinfo->image_width; col > 0; col--) {
638
3.02k
    register unsigned int r, g, b;
639
3.02k
    r  = UCH(*bufferptr++) << 8;
640
3.02k
    r |= UCH(*bufferptr++);
641
3.02k
    if (r > maxval)
642
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
3.02k
    g  = UCH(*bufferptr++) << 8;
644
3.02k
    g |= UCH(*bufferptr++);
645
3.02k
    if (g > maxval)
646
23
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
3.02k
    b  = UCH(*bufferptr++) << 8;
648
3.02k
    b |= UCH(*bufferptr++);
649
3.02k
    if (b > maxval)
650
19
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
3.02k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
3.02k
                ptr + 2, ptr + 3);
653
3.02k
    ptr += 4;
654
3.02k
  }
655
1.81k
  return 1;
656
1.81k
}
rdppm-8.c:get_word_rgb_cmyk_row
Line
Count
Source
625
1.81k
{
626
1.81k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
1.81k
  register _JSAMPROW ptr;
628
1.81k
  register U_CHAR *bufferptr;
629
1.81k
  register _JSAMPLE *rescale = source->rescale;
630
1.81k
  JDIMENSION col;
631
1.81k
  unsigned int maxval = source->maxval;
632
633
1.81k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
71
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
1.81k
  ptr = source->pub._buffer[0];
636
1.81k
  bufferptr = source->iobuffer;
637
4.83k
  for (col = cinfo->image_width; col > 0; col--) {
638
3.02k
    register unsigned int r, g, b;
639
3.02k
    r  = UCH(*bufferptr++) << 8;
640
3.02k
    r |= UCH(*bufferptr++);
641
3.02k
    if (r > maxval)
642
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
3.02k
    g  = UCH(*bufferptr++) << 8;
644
3.02k
    g |= UCH(*bufferptr++);
645
3.02k
    if (g > maxval)
646
23
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
3.02k
    b  = UCH(*bufferptr++) << 8;
648
3.02k
    b |= UCH(*bufferptr++);
649
3.02k
    if (b > maxval)
650
19
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
3.02k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
3.02k
                ptr + 2, ptr + 3);
653
3.02k
    ptr += 4;
654
3.02k
  }
655
1.81k
  return 1;
656
1.81k
}
Unexecuted instantiation: rdppm-12.c:get_word_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_cmyk_row
657
658
659
/*
660
 * Read the file header; return image size and component count.
661
 */
662
663
METHODDEF(void)
664
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
665
12.5k
{
666
12.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
12.5k
  int c;
668
12.5k
  unsigned int w, h, maxval;
669
12.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
12.5k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
12.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
12.5k
  switch (c) {
678
1.69k
  case '2':                     /* it's a text-format PGM file */
679
3.59k
  case '3':                     /* it's a text-format PPM file */
680
9.54k
  case '5':                     /* it's a raw-format PGM file */
681
12.5k
  case '6':                     /* it's a raw-format PPM file */
682
12.5k
    break;
683
7
  default:
684
7
    ERREXIT(cinfo, JERR_PPM_NOT);
685
7
    break;
686
12.5k
  }
687
688
  /* fetch the remaining header info */
689
12.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
12.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
12.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
12.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
12.5k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
238
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
12.5k
  cinfo->image_width = (JDIMENSION)w;
699
12.5k
  cinfo->image_height = (JDIMENSION)h;
700
12.5k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
12.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
12.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
12.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
12.5k
  switch (c) {
708
1.41k
  case '2':                     /* it's a text-format PGM file */
709
1.41k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.41k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.41k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.41k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
202
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.21k
    else if (IsExtRGB(cinfo->in_color_space))
716
1.01k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
202
    else if (cinfo->in_color_space == JCS_CMYK)
718
202
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.41k
    need_iobuffer = FALSE;
722
1.41k
    break;
723
724
1.50k
  case '3':                     /* it's a text-format PPM file */
725
1.50k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.50k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.50k
    if (IsExtRGB(cinfo->in_color_space))
729
1.07k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
430
    else if (cinfo->in_color_space == JCS_CMYK)
731
215
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
215
    else
733
215
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.50k
    need_iobuffer = FALSE;
735
1.50k
    break;
736
737
5.59k
  case '5':                     /* it's a raw-format PGM file */
738
5.59k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
5.59k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
5.59k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
5.59k
    if (maxval > 255) {
743
707
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
101
        source->pub.get_pixel_rows = get_word_gray_row;
745
606
      else if (IsExtRGB(cinfo->in_color_space))
746
505
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
101
      else if (cinfo->in_color_space == JCS_CMYK)
748
101
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
4.88k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
4.88k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
4.88k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
71
      source->pub.get_pixel_rows = get_raw_row;
755
71
      use_raw_buffer = TRUE;
756
71
      need_rescale = FALSE;
757
4.81k
    } else {
758
4.81k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
627
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
4.18k
      else if (IsExtRGB(cinfo->in_color_space))
761
3.49k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
698
      else if (cinfo->in_color_space == JCS_CMYK)
763
698
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
4.81k
    }
767
5.59k
    break;
768
769
2.63k
  case '6':                     /* it's a raw-format PPM file */
770
2.63k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.63k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.63k
    if (maxval > 255) {
774
1.07k
      if (IsExtRGB(cinfo->in_color_space))
775
765
        source->pub.get_pixel_rows = get_word_rgb_row;
776
306
      else if (cinfo->in_color_space == JCS_CMYK)
777
153
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
153
      else
779
153
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.56k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
1.56k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.56k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.56k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
119
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
7
      source->pub.get_pixel_rows = get_raw_row;
789
7
      use_raw_buffer = TRUE;
790
7
      need_rescale = FALSE;
791
1.55k
    } else {
792
1.55k
      if (IsExtRGB(cinfo->in_color_space))
793
1.10k
        source->pub.get_pixel_rows = get_rgb_row;
794
446
      else if (cinfo->in_color_space == JCS_CMYK)
795
223
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
223
      else
797
223
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.55k
    }
799
2.63k
    break;
800
12.5k
  }
801
802
10.5k
  if (IsExtRGB(cinfo->in_color_space))
803
7.96k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.59k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
1.00k
    cinfo->input_components = 1;
806
1.59k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.59k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
10.5k
  if (need_iobuffer) {
811
7.84k
    if (c == '6')
812
2.25k
      source->buffer_width = (size_t)w * 3 *
813
2.25k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
5.59k
    else
815
5.59k
      source->buffer_width = (size_t)w *
816
5.59k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
7.84k
    source->iobuffer = (U_CHAR *)
818
7.84k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
7.84k
                                  source->buffer_width);
820
7.84k
  }
821
822
  /* Create compressor input buffer. */
823
10.5k
  if (use_raw_buffer) {
824
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
825
    /* Synthesize a _JSAMPARRAY pointer structure */
826
78
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
78
    source->pub._buffer = &source->pixrow;
828
78
    source->pub.buffer_height = 1;
829
10.4k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
10.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
10.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
10.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
10.4k
    source->pub.buffer_height = 1;
835
10.4k
  }
836
837
  /* Compute the rescaling array if required. */
838
10.5k
  if (need_rescale) {
839
10.4k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
10.4k
    source->rescale = (_JSAMPLE *)
843
10.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
10.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
10.4k
                                           sizeof(_JSAMPLE)));
846
10.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
10.4k
                                        sizeof(_JSAMPLE)));
848
10.4k
    half_maxval = maxval / 2;
849
16.3M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
16.3M
      source->rescale[val] =
852
16.3M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
16.3M
                   maxval);
854
16.3M
    }
855
10.4k
  }
856
10.5k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
665
12.5k
{
666
12.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
12.5k
  int c;
668
12.5k
  unsigned int w, h, maxval;
669
12.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
12.5k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
12.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
12.5k
  switch (c) {
678
1.69k
  case '2':                     /* it's a text-format PGM file */
679
3.59k
  case '3':                     /* it's a text-format PPM file */
680
9.54k
  case '5':                     /* it's a raw-format PGM file */
681
12.5k
  case '6':                     /* it's a raw-format PPM file */
682
12.5k
    break;
683
7
  default:
684
7
    ERREXIT(cinfo, JERR_PPM_NOT);
685
7
    break;
686
12.5k
  }
687
688
  /* fetch the remaining header info */
689
12.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
12.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
12.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
12.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
12.5k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
238
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
12.5k
  cinfo->image_width = (JDIMENSION)w;
699
12.5k
  cinfo->image_height = (JDIMENSION)h;
700
12.5k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
12.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
12.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
12.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
12.5k
  switch (c) {
708
1.41k
  case '2':                     /* it's a text-format PGM file */
709
1.41k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.41k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.41k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.41k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
202
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.21k
    else if (IsExtRGB(cinfo->in_color_space))
716
1.01k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
202
    else if (cinfo->in_color_space == JCS_CMYK)
718
202
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.41k
    need_iobuffer = FALSE;
722
1.41k
    break;
723
724
1.50k
  case '3':                     /* it's a text-format PPM file */
725
1.50k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.50k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.50k
    if (IsExtRGB(cinfo->in_color_space))
729
1.07k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
430
    else if (cinfo->in_color_space == JCS_CMYK)
731
215
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
215
    else
733
215
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.50k
    need_iobuffer = FALSE;
735
1.50k
    break;
736
737
5.59k
  case '5':                     /* it's a raw-format PGM file */
738
5.59k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
5.59k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
5.59k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
5.59k
    if (maxval > 255) {
743
707
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
101
        source->pub.get_pixel_rows = get_word_gray_row;
745
606
      else if (IsExtRGB(cinfo->in_color_space))
746
505
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
101
      else if (cinfo->in_color_space == JCS_CMYK)
748
101
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
4.88k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
4.88k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
4.88k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
71
      source->pub.get_pixel_rows = get_raw_row;
755
71
      use_raw_buffer = TRUE;
756
71
      need_rescale = FALSE;
757
4.81k
    } else {
758
4.81k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
627
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
4.18k
      else if (IsExtRGB(cinfo->in_color_space))
761
3.49k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
698
      else if (cinfo->in_color_space == JCS_CMYK)
763
698
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
4.81k
    }
767
5.59k
    break;
768
769
2.63k
  case '6':                     /* it's a raw-format PPM file */
770
2.63k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.63k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.63k
    if (maxval > 255) {
774
1.07k
      if (IsExtRGB(cinfo->in_color_space))
775
765
        source->pub.get_pixel_rows = get_word_rgb_row;
776
306
      else if (cinfo->in_color_space == JCS_CMYK)
777
153
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
153
      else
779
153
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.56k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
1.56k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.56k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.56k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
119
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
7
      source->pub.get_pixel_rows = get_raw_row;
789
7
      use_raw_buffer = TRUE;
790
7
      need_rescale = FALSE;
791
1.55k
    } else {
792
1.55k
      if (IsExtRGB(cinfo->in_color_space))
793
1.10k
        source->pub.get_pixel_rows = get_rgb_row;
794
446
      else if (cinfo->in_color_space == JCS_CMYK)
795
223
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
223
      else
797
223
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.55k
    }
799
2.63k
    break;
800
12.5k
  }
801
802
10.5k
  if (IsExtRGB(cinfo->in_color_space))
803
7.96k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.59k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
1.00k
    cinfo->input_components = 1;
806
1.59k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.59k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
10.5k
  if (need_iobuffer) {
811
7.84k
    if (c == '6')
812
2.25k
      source->buffer_width = (size_t)w * 3 *
813
2.25k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
5.59k
    else
815
5.59k
      source->buffer_width = (size_t)w *
816
5.59k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
7.84k
    source->iobuffer = (U_CHAR *)
818
7.84k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
7.84k
                                  source->buffer_width);
820
7.84k
  }
821
822
  /* Create compressor input buffer. */
823
10.5k
  if (use_raw_buffer) {
824
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
825
    /* Synthesize a _JSAMPARRAY pointer structure */
826
78
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
78
    source->pub._buffer = &source->pixrow;
828
78
    source->pub.buffer_height = 1;
829
10.4k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
10.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
10.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
10.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
10.4k
    source->pub.buffer_height = 1;
835
10.4k
  }
836
837
  /* Compute the rescaling array if required. */
838
10.5k
  if (need_rescale) {
839
10.4k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
10.4k
    source->rescale = (_JSAMPLE *)
843
10.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
10.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
10.4k
                                           sizeof(_JSAMPLE)));
846
10.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
10.4k
                                        sizeof(_JSAMPLE)));
848
10.4k
    half_maxval = maxval / 2;
849
16.3M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
16.3M
      source->rescale[val] =
852
16.3M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
16.3M
                   maxval);
854
16.3M
    }
855
10.4k
  }
856
10.5k
}
Unexecuted instantiation: rdppm-12.c:start_input_ppm
Unexecuted instantiation: rdppm-16.c:start_input_ppm
857
858
859
/*
860
 * Finish up at the end of the file.
861
 */
862
863
METHODDEF(void)
864
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
865
4.61k
{
866
  /* no work */
867
4.61k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
865
4.61k
{
866
  /* no work */
867
4.61k
}
Unexecuted instantiation: rdppm-12.c:finish_input_ppm
Unexecuted instantiation: rdppm-16.c:finish_input_ppm
868
869
870
/*
871
 * The module selection routine for PPM format input.
872
 */
873
874
GLOBAL(cjpeg_source_ptr)
875
_jinit_read_ppm(j_compress_ptr cinfo)
876
12.5k
{
877
12.5k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
12.5k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
0
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
12.5k
  source = (ppm_source_ptr)
889
12.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
12.5k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
12.5k
  source->pub.start_input = start_input_ppm;
893
12.5k
  source->pub.finish_input = finish_input_ppm;
894
12.5k
  source->pub.max_pixels = 0;
895
896
12.5k
  return (cjpeg_source_ptr)source;
897
12.5k
}
jinit_read_ppm
Line
Count
Source
876
12.5k
{
877
12.5k
  ppm_source_ptr source;
878
879
12.5k
#if BITS_IN_JSAMPLE == 8
880
12.5k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
12.5k
  source = (ppm_source_ptr)
889
12.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
12.5k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
12.5k
  source->pub.start_input = start_input_ppm;
893
12.5k
  source->pub.finish_input = finish_input_ppm;
894
12.5k
  source->pub.max_pixels = 0;
895
896
12.5k
  return (cjpeg_source_ptr)source;
897
12.5k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
898
899
#endif /* defined(PPM_SUPPORTED) &&
900
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */