Coverage Report

Created: 2026-02-26 07:10

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
12.3M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
44.6M
  (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
256k
{
80
256k
  register int ch;
81
82
256k
  ch = getc(infile);
83
256k
  if (ch == '#') {
84
59.2k
    do {
85
59.2k
      ch = getc(infile);
86
59.2k
    } while (ch != '\n' && ch != EOF);
87
3.57k
  }
88
256k
  return ch;
89
256k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
256k
{
80
256k
  register int ch;
81
82
256k
  ch = getc(infile);
83
256k
  if (ch == '#') {
84
59.2k
    do {
85
59.2k
      ch = getc(infile);
86
59.2k
    } while (ch != '\n' && ch != EOF);
87
3.57k
  }
88
256k
  return ch;
89
256k
}
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
105k
{
99
105k
  register int ch;
100
105k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
112k
  do {
104
112k
    ch = pbm_getc(infile);
105
112k
    if (ch == EOF)
106
1.97k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
112k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
105k
  if (ch < '0' || ch > '9')
110
212
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
105k
  val = ch - '0';
113
146k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
41.1k
    val *= 10;
115
41.1k
    val += ch - '0';
116
41.1k
    if (val > maxval)
117
129
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
41.1k
  }
119
120
105k
  return val;
121
105k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
105k
{
99
105k
  register int ch;
100
105k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
112k
  do {
104
112k
    ch = pbm_getc(infile);
105
112k
    if (ch == EOF)
106
1.97k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
112k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
105k
  if (ch < '0' || ch > '9')
110
212
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
105k
  val = ch - '0';
113
146k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
41.1k
    val *= 10;
115
41.1k
    val += ch - '0';
116
41.1k
    if (val > maxval)
117
129
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
41.1k
  }
119
120
105k
  return val;
121
105k
}
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
2.02k
{
140
2.02k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.02k
  FILE *infile = source->pub.input_file;
142
2.02k
  register _JSAMPROW ptr;
143
2.02k
  register _JSAMPLE *rescale = source->rescale;
144
2.02k
  JDIMENSION col;
145
2.02k
  unsigned int maxval = source->maxval;
146
147
2.02k
  ptr = source->pub._buffer[0];
148
5.39k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.36k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.36k
  }
151
2.02k
  return 1;
152
2.02k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
2.02k
{
140
2.02k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.02k
  FILE *infile = source->pub.input_file;
142
2.02k
  register _JSAMPROW ptr;
143
2.02k
  register _JSAMPLE *rescale = source->rescale;
144
2.02k
  JDIMENSION col;
145
2.02k
  unsigned int maxval = source->maxval;
146
147
2.02k
  ptr = source->pub._buffer[0];
148
5.39k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.36k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.36k
  }
151
2.02k
  return 1;
152
2.02k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
37.1M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
102M
  for (col = cinfo->image_width; col > 0; col--) { \
157
65.0M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
65.0M
    alpha_set_op \
159
65.0M
    ptr += ps; \
160
65.0M
  } \
161
37.1M
}
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.1k
{
168
10.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.1k
  FILE *infile = source->pub.input_file;
170
10.1k
  register _JSAMPROW ptr;
171
10.1k
  register _JSAMPLE *rescale = source->rescale;
172
10.1k
  JDIMENSION col;
173
10.1k
  unsigned int maxval = source->maxval;
174
10.1k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.1k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.1k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.1k
  ptr = source->pub._buffer[0];
181
10.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
2.67k
    if (aindex >= 0)
183
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
2.67k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
2.67k
    else
186
2.67k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
7.47k
  } else {
188
7.47k
    if (aindex >= 0)
189
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.47k
                         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
7.47k
  }
194
10.1k
  return 1;
195
10.1k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
10.1k
{
168
10.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.1k
  FILE *infile = source->pub.input_file;
170
10.1k
  register _JSAMPROW ptr;
171
10.1k
  register _JSAMPLE *rescale = source->rescale;
172
10.1k
  JDIMENSION col;
173
10.1k
  unsigned int maxval = source->maxval;
174
10.1k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.1k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.1k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.1k
  ptr = source->pub._buffer[0];
181
10.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
2.67k
    if (aindex >= 0)
183
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
2.67k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
2.67k
    else
186
2.67k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
7.47k
  } else {
188
7.47k
    if (aindex >= 0)
189
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.47k
                         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
7.47k
  }
194
10.1k
  return 1;
195
10.1k
}
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
0
{
203
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
0
  FILE *infile = source->pub.input_file;
205
0
  register _JSAMPROW ptr;
206
0
  register _JSAMPLE *rescale = source->rescale;
207
0
  JDIMENSION col;
208
0
  unsigned int maxval = source->maxval;
209
210
0
  ptr = source->pub._buffer[0];
211
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
0
    for (col = cinfo->image_width; col > 0; col--) {
213
0
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
0
      ptr += 4;
216
0
    }
217
0
  } else {
218
0
    for (col = cinfo->image_width; col > 0; col--) {
219
0
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
221
0
                  ptr + 1, ptr + 2, ptr + 3);
222
0
      ptr += 4;
223
0
    }
224
0
  }
225
0
  return 1;
226
0
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row
227
228
229
87.6k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
881k
  for (col = cinfo->image_width; col > 0; col--) { \
231
793k
    ptr[rindex] = read_op; \
232
793k
    ptr[gindex] = read_op; \
233
793k
    ptr[bindex] = read_op; \
234
793k
    alpha_set_op \
235
793k
    ptr += ps; \
236
793k
  } \
237
87.6k
}
238
239
METHODDEF(JDIMENSION)
240
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
241
/* This version is for reading text-format PPM files with any maxval */
242
8.84k
{
243
8.84k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.84k
  FILE *infile = source->pub.input_file;
245
8.84k
  register _JSAMPROW ptr;
246
8.84k
  register _JSAMPLE *rescale = source->rescale;
247
8.84k
  JDIMENSION col;
248
8.84k
  unsigned int maxval = source->maxval;
249
8.84k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.84k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.84k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.84k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.84k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.84k
  ptr = source->pub._buffer[0];
256
8.84k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.78k
    if (aindex >= 0)
258
0
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.78k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.78k
    else
261
2.78k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
6.06k
  } else {
263
6.06k
    if (aindex >= 0)
264
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.06k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
6.06k
    else
267
6.06k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.06k
  }
269
8.84k
  return 1;
270
8.84k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
242
8.84k
{
243
8.84k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.84k
  FILE *infile = source->pub.input_file;
245
8.84k
  register _JSAMPROW ptr;
246
8.84k
  register _JSAMPLE *rescale = source->rescale;
247
8.84k
  JDIMENSION col;
248
8.84k
  unsigned int maxval = source->maxval;
249
8.84k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.84k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.84k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.84k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.84k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.84k
  ptr = source->pub._buffer[0];
256
8.84k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
257
2.78k
    if (aindex >= 0)
258
0
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
259
2.78k
                    ptr[aindex] = (_JSAMPLE)maxval;)
260
2.78k
    else
261
2.78k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
262
6.06k
  } else {
263
6.06k
    if (aindex >= 0)
264
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
6.06k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
6.06k
    else
267
6.06k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
6.06k
  }
269
8.84k
  return 1;
270
8.84k
}
Unexecuted instantiation: rdppm-12.c:get_text_rgb_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_row
271
272
273
METHODDEF(JDIMENSION)
274
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
275
/* This version is for reading text-format PPM files with any maxval and
276
   converting to CMYK */
277
0
{
278
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
279
0
  FILE *infile = source->pub.input_file;
280
0
  register _JSAMPROW ptr;
281
0
  register _JSAMPLE *rescale = source->rescale;
282
0
  JDIMENSION col;
283
0
  unsigned int maxval = source->maxval;
284
285
0
  ptr = source->pub._buffer[0];
286
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
287
0
    for (col = cinfo->image_width; col > 0; col--) {
288
0
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
0
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
0
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
291
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
292
0
      ptr += 4;
293
0
    }
294
0
  } else {
295
0
    for (col = cinfo->image_width; col > 0; col--) {
296
0
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
0
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
0
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
299
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
300
0
                  ptr + 2, ptr + 3);
301
0
      ptr += 4;
302
0
    }
303
0
  }
304
0
  return 1;
305
0
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_text_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_cmyk_row
306
307
308
METHODDEF(JDIMENSION)
309
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
310
/* This version is for reading raw-byte-format PGM files with any maxval */
311
6.81M
{
312
6.81M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
6.81M
  register _JSAMPROW ptr;
314
6.81M
  register U_CHAR *bufferptr;
315
6.81M
  register _JSAMPLE *rescale = source->rescale;
316
6.81M
  JDIMENSION col;
317
318
6.81M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
6.81M
  ptr = source->pub._buffer[0];
321
6.81M
  bufferptr = source->iobuffer;
322
19.0M
  for (col = cinfo->image_width; col > 0; col--) {
323
12.2M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
12.2M
  }
325
6.81M
  return 1;
326
6.81M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
311
6.81M
{
312
6.81M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
6.81M
  register _JSAMPROW ptr;
314
6.81M
  register U_CHAR *bufferptr;
315
6.81M
  register _JSAMPLE *rescale = source->rescale;
316
6.81M
  JDIMENSION col;
317
318
6.81M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
6.81M
  ptr = source->pub._buffer[0];
321
6.81M
  bufferptr = source->iobuffer;
322
19.0M
  for (col = cinfo->image_width; col > 0; col--) {
323
12.2M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
12.2M
  }
325
6.81M
  return 1;
326
6.81M
}
Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row
Unexecuted instantiation: rdppm-16.c:get_scaled_gray_row
327
328
329
METHODDEF(JDIMENSION)
330
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
331
/* This version is for reading raw-byte-format PGM files with any maxval
332
   and converting to extended RGB */
333
37.1M
{
334
37.1M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
37.1M
  register _JSAMPROW ptr;
336
37.1M
  register U_CHAR *bufferptr;
337
37.1M
  register _JSAMPLE *rescale = source->rescale;
338
37.1M
  JDIMENSION col;
339
37.1M
  unsigned int maxval = source->maxval;
340
37.1M
  register int rindex = rgb_red[cinfo->in_color_space];
341
37.1M
  register int gindex = rgb_green[cinfo->in_color_space];
342
37.1M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
37.1M
  register int aindex = alpha_index[cinfo->in_color_space];
344
37.1M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
37.1M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
430
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
37.1M
  ptr = source->pub._buffer[0];
349
37.1M
  bufferptr = source->iobuffer;
350
37.1M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
3.05M
    if (aindex >= 0)
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
3.05M
    else
354
3.05M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
34.0M
  } else {
356
34.0M
    if (aindex >= 0)
357
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
34.0M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
34.0M
    else
360
34.0M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
34.0M
  }
362
37.1M
  return 1;
363
37.1M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
333
37.1M
{
334
37.1M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
37.1M
  register _JSAMPROW ptr;
336
37.1M
  register U_CHAR *bufferptr;
337
37.1M
  register _JSAMPLE *rescale = source->rescale;
338
37.1M
  JDIMENSION col;
339
37.1M
  unsigned int maxval = source->maxval;
340
37.1M
  register int rindex = rgb_red[cinfo->in_color_space];
341
37.1M
  register int gindex = rgb_green[cinfo->in_color_space];
342
37.1M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
37.1M
  register int aindex = alpha_index[cinfo->in_color_space];
344
37.1M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
37.1M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
430
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
37.1M
  ptr = source->pub._buffer[0];
349
37.1M
  bufferptr = source->iobuffer;
350
37.1M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
3.05M
    if (aindex >= 0)
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
3.05M
    else
354
3.05M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
34.0M
  } else {
356
34.0M
    if (aindex >= 0)
357
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
34.0M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
34.0M
    else
360
34.0M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
34.0M
  }
362
37.1M
  return 1;
363
37.1M
}
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_gray_rgb_row
364
365
366
METHODDEF(JDIMENSION)
367
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
368
/* This version is for reading raw-byte-format PGM files with any maxval
369
   and converting to CMYK */
370
0
{
371
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
372
0
  register _JSAMPROW ptr;
373
0
  register U_CHAR *bufferptr;
374
0
  register _JSAMPLE *rescale = source->rescale;
375
0
  JDIMENSION col;
376
0
  unsigned int maxval = source->maxval;
377
378
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
379
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
380
0
  ptr = source->pub._buffer[0];
381
0
  bufferptr = source->iobuffer;
382
0
  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
0
  } else {
389
0
    for (col = cinfo->image_width; col > 0; col--) {
390
0
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
391
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
392
0
                  ptr + 1, ptr + 2, ptr + 3);
393
0
      ptr += 4;
394
0
    }
395
0
  }
396
0
  return 1;
397
0
}
Unexecuted instantiation: rdppm-8.c:get_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_gray_cmyk_row
398
399
400
METHODDEF(JDIMENSION)
401
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
402
/* This version is for reading raw-byte-format PPM files with any maxval */
403
78.8k
{
404
78.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
78.8k
  register _JSAMPROW ptr;
406
78.8k
  register U_CHAR *bufferptr;
407
78.8k
  register _JSAMPLE *rescale = source->rescale;
408
78.8k
  JDIMENSION col;
409
78.8k
  unsigned int maxval = source->maxval;
410
78.8k
  register int rindex = rgb_red[cinfo->in_color_space];
411
78.8k
  register int gindex = rgb_green[cinfo->in_color_space];
412
78.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
78.8k
  register int aindex = alpha_index[cinfo->in_color_space];
414
78.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
78.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
389
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
78.8k
  ptr = source->pub._buffer[0];
419
78.8k
  bufferptr = source->iobuffer;
420
78.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
27.8k
    if (aindex >= 0)
422
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
27.8k
    else
424
27.8k
      RGB_READ_LOOP(*bufferptr++, {})
425
50.9k
  } else {
426
50.9k
    if (aindex >= 0)
427
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
50.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
50.9k
    else
430
50.9k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
50.9k
  }
432
78.8k
  return 1;
433
78.8k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
403
78.8k
{
404
78.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
78.8k
  register _JSAMPROW ptr;
406
78.8k
  register U_CHAR *bufferptr;
407
78.8k
  register _JSAMPLE *rescale = source->rescale;
408
78.8k
  JDIMENSION col;
409
78.8k
  unsigned int maxval = source->maxval;
410
78.8k
  register int rindex = rgb_red[cinfo->in_color_space];
411
78.8k
  register int gindex = rgb_green[cinfo->in_color_space];
412
78.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
78.8k
  register int aindex = alpha_index[cinfo->in_color_space];
414
78.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
78.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
389
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
78.8k
  ptr = source->pub._buffer[0];
419
78.8k
  bufferptr = source->iobuffer;
420
78.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
27.8k
    if (aindex >= 0)
422
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
27.8k
    else
424
27.8k
      RGB_READ_LOOP(*bufferptr++, {})
425
50.9k
  } else {
426
50.9k
    if (aindex >= 0)
427
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
50.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
50.9k
    else
430
50.9k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
50.9k
  }
432
78.8k
  return 1;
433
78.8k
}
Unexecuted instantiation: rdppm-12.c:get_rgb_row
Unexecuted instantiation: rdppm-16.c:get_rgb_row
434
435
436
METHODDEF(JDIMENSION)
437
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
438
/* This version is for reading raw-byte-format PPM files with any maxval and
439
   converting to CMYK */
440
0
{
441
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
442
0
  register _JSAMPROW ptr;
443
0
  register U_CHAR *bufferptr;
444
0
  register _JSAMPLE *rescale = source->rescale;
445
0
  JDIMENSION col;
446
0
  unsigned int maxval = source->maxval;
447
448
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
449
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
450
0
  ptr = source->pub._buffer[0];
451
0
  bufferptr = source->iobuffer;
452
0
  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
0
  } else {
461
0
    for (col = cinfo->image_width; col > 0; col--) {
462
0
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
463
0
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
464
0
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
465
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
466
0
                  ptr + 2, ptr + 3);
467
0
      ptr += 4;
468
0
    }
469
0
  }
470
0
  return 1;
471
0
}
Unexecuted instantiation: rdppm-8.c:get_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_rgb_cmyk_row
472
473
474
METHODDEF(JDIMENSION)
475
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
476
/* This version is for reading raw-byte-format files with
477
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U.
478
 * In this case we just read right into the _JSAMPLE buffer!
479
 * Note that same code works for PPM and PGM files.
480
 */
481
617k
{
482
617k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
617k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
41
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
617k
  return 1;
487
617k
}
rdppm-8.c:get_raw_row
Line
Count
Source
481
617k
{
482
617k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
617k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
41
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
617k
  return 1;
487
617k
}
Unexecuted instantiation: rdppm-12.c:get_raw_row
Unexecuted instantiation: rdppm-16.c:get_raw_row
488
489
490
METHODDEF(JDIMENSION)
491
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
492
/* This version is for reading raw-word-format PGM files with any maxval */
493
1.32k
{
494
1.32k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
1.32k
  register _JSAMPROW ptr;
496
1.32k
  register U_CHAR *bufferptr;
497
1.32k
  register _JSAMPLE *rescale = source->rescale;
498
1.32k
  JDIMENSION col;
499
1.32k
  unsigned int maxval = source->maxval;
500
501
1.32k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
49
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.32k
  ptr = source->pub._buffer[0];
504
1.32k
  bufferptr = source->iobuffer;
505
3.32k
  for (col = cinfo->image_width; col > 0; col--) {
506
2.00k
    register unsigned int temp;
507
2.00k
    temp  = UCH(*bufferptr++) << 8;
508
2.00k
    temp |= UCH(*bufferptr++);
509
2.00k
    if (temp > maxval)
510
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
2.00k
    *ptr++ = rescale[temp];
512
2.00k
  }
513
1.32k
  return 1;
514
1.32k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
493
1.32k
{
494
1.32k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
495
1.32k
  register _JSAMPROW ptr;
496
1.32k
  register U_CHAR *bufferptr;
497
1.32k
  register _JSAMPLE *rescale = source->rescale;
498
1.32k
  JDIMENSION col;
499
1.32k
  unsigned int maxval = source->maxval;
500
501
1.32k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
502
49
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.32k
  ptr = source->pub._buffer[0];
504
1.32k
  bufferptr = source->iobuffer;
505
3.32k
  for (col = cinfo->image_width; col > 0; col--) {
506
2.00k
    register unsigned int temp;
507
2.00k
    temp  = UCH(*bufferptr++) << 8;
508
2.00k
    temp |= UCH(*bufferptr++);
509
2.00k
    if (temp > maxval)
510
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
2.00k
    *ptr++ = rescale[temp];
512
2.00k
  }
513
1.32k
  return 1;
514
1.32k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_row
515
516
517
METHODDEF(JDIMENSION)
518
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
519
/* This version is for reading raw-word-format PGM files with any maxval */
520
6.61k
{
521
6.61k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
6.61k
  register _JSAMPROW ptr;
523
6.61k
  register U_CHAR *bufferptr;
524
6.61k
  register _JSAMPLE *rescale = source->rescale;
525
6.61k
  JDIMENSION col;
526
6.61k
  unsigned int maxval = source->maxval;
527
6.61k
  register int rindex = rgb_red[cinfo->in_color_space];
528
6.61k
  register int gindex = rgb_green[cinfo->in_color_space];
529
6.61k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
6.61k
  register int aindex = alpha_index[cinfo->in_color_space];
531
6.61k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
6.61k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
245
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
6.61k
  ptr = source->pub._buffer[0];
536
6.61k
  bufferptr = source->iobuffer;
537
16.6k
  for (col = cinfo->image_width; col > 0; col--) {
538
10.0k
    register unsigned int temp;
539
10.0k
    temp  = UCH(*bufferptr++) << 8;
540
10.0k
    temp |= UCH(*bufferptr++);
541
10.0k
    if (temp > maxval)
542
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
10.0k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
10.0k
    if (aindex >= 0)
545
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
10.0k
    ptr += ps;
547
10.0k
  }
548
6.61k
  return 1;
549
6.61k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
520
6.61k
{
521
6.61k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
6.61k
  register _JSAMPROW ptr;
523
6.61k
  register U_CHAR *bufferptr;
524
6.61k
  register _JSAMPLE *rescale = source->rescale;
525
6.61k
  JDIMENSION col;
526
6.61k
  unsigned int maxval = source->maxval;
527
6.61k
  register int rindex = rgb_red[cinfo->in_color_space];
528
6.61k
  register int gindex = rgb_green[cinfo->in_color_space];
529
6.61k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
6.61k
  register int aindex = alpha_index[cinfo->in_color_space];
531
6.61k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
6.61k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
245
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
6.61k
  ptr = source->pub._buffer[0];
536
6.61k
  bufferptr = source->iobuffer;
537
16.6k
  for (col = cinfo->image_width; col > 0; col--) {
538
10.0k
    register unsigned int temp;
539
10.0k
    temp  = UCH(*bufferptr++) << 8;
540
10.0k
    temp |= UCH(*bufferptr++);
541
10.0k
    if (temp > maxval)
542
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
10.0k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
10.0k
    if (aindex >= 0)
545
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
10.0k
    ptr += ps;
547
10.0k
  }
548
6.61k
  return 1;
549
6.61k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_rgb_row
550
551
552
METHODDEF(JDIMENSION)
553
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
554
/* This version is for reading raw-word-format PGM files with any maxval */
555
0
{
556
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
557
0
  register _JSAMPROW ptr;
558
0
  register U_CHAR *bufferptr;
559
0
  register _JSAMPLE *rescale = source->rescale;
560
0
  JDIMENSION col;
561
0
  unsigned int maxval = source->maxval;
562
563
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
564
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
565
0
  ptr = source->pub._buffer[0];
566
0
  bufferptr = source->iobuffer;
567
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
568
0
    for (col = cinfo->image_width; col > 0; col--) {
569
0
      register unsigned int gray;
570
0
      gray  = UCH(*bufferptr++) << 8;
571
0
      gray |= UCH(*bufferptr++);
572
0
      if (gray > maxval)
573
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
574
0
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
575
0
                  ptr + 1, ptr + 2, ptr + 3);
576
0
      ptr += 4;
577
0
    }
578
0
  } else {
579
0
    for (col = cinfo->image_width; col > 0; col--) {
580
0
      register unsigned int temp;
581
0
      _JSAMPLE gray;
582
0
      temp  = UCH(*bufferptr++) << 8;
583
0
      temp |= UCH(*bufferptr++);
584
0
      if (temp > maxval)
585
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
586
0
      gray = rescale[temp];
587
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
588
0
                  ptr + 1, ptr + 2, ptr + 3);
589
0
      ptr += 4;
590
0
    }
591
0
  }
592
0
  return 1;
593
0
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_cmyk_row
594
595
596
METHODDEF(JDIMENSION)
597
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
598
/* This version is for reading raw-word-format PPM files with any maxval */
599
5.64k
{
600
5.64k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
5.64k
  register _JSAMPROW ptr;
602
5.64k
  register U_CHAR *bufferptr;
603
5.64k
  register _JSAMPLE *rescale = source->rescale;
604
5.64k
  JDIMENSION col;
605
5.64k
  unsigned int maxval = source->maxval;
606
5.64k
  register int rindex = rgb_red[cinfo->in_color_space];
607
5.64k
  register int gindex = rgb_green[cinfo->in_color_space];
608
5.64k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
5.64k
  register int aindex = alpha_index[cinfo->in_color_space];
610
5.64k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
5.64k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
240
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
5.64k
  ptr = source->pub._buffer[0];
615
5.64k
  bufferptr = source->iobuffer;
616
14.0k
  for (col = cinfo->image_width; col > 0; col--) {
617
8.39k
    register unsigned int temp;
618
8.39k
    temp  = UCH(*bufferptr++) << 8;
619
8.39k
    temp |= UCH(*bufferptr++);
620
8.39k
    if (temp > maxval)
621
50
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
8.39k
    ptr[rindex] = rescale[temp];
623
8.39k
    temp  = UCH(*bufferptr++) << 8;
624
8.39k
    temp |= UCH(*bufferptr++);
625
8.39k
    if (temp > maxval)
626
90
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
8.39k
    ptr[gindex] = rescale[temp];
628
8.39k
    temp  = UCH(*bufferptr++) << 8;
629
8.39k
    temp |= UCH(*bufferptr++);
630
8.39k
    if (temp > maxval)
631
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
8.39k
    ptr[bindex] = rescale[temp];
633
8.39k
    if (aindex >= 0)
634
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
8.39k
    ptr += ps;
636
8.39k
  }
637
5.64k
  return 1;
638
5.64k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
599
5.64k
{
600
5.64k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
5.64k
  register _JSAMPROW ptr;
602
5.64k
  register U_CHAR *bufferptr;
603
5.64k
  register _JSAMPLE *rescale = source->rescale;
604
5.64k
  JDIMENSION col;
605
5.64k
  unsigned int maxval = source->maxval;
606
5.64k
  register int rindex = rgb_red[cinfo->in_color_space];
607
5.64k
  register int gindex = rgb_green[cinfo->in_color_space];
608
5.64k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
5.64k
  register int aindex = alpha_index[cinfo->in_color_space];
610
5.64k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
5.64k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
240
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
5.64k
  ptr = source->pub._buffer[0];
615
5.64k
  bufferptr = source->iobuffer;
616
14.0k
  for (col = cinfo->image_width; col > 0; col--) {
617
8.39k
    register unsigned int temp;
618
8.39k
    temp  = UCH(*bufferptr++) << 8;
619
8.39k
    temp |= UCH(*bufferptr++);
620
8.39k
    if (temp > maxval)
621
50
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
8.39k
    ptr[rindex] = rescale[temp];
623
8.39k
    temp  = UCH(*bufferptr++) << 8;
624
8.39k
    temp |= UCH(*bufferptr++);
625
8.39k
    if (temp > maxval)
626
90
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
8.39k
    ptr[gindex] = rescale[temp];
628
8.39k
    temp  = UCH(*bufferptr++) << 8;
629
8.39k
    temp |= UCH(*bufferptr++);
630
8.39k
    if (temp > maxval)
631
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
8.39k
    ptr[bindex] = rescale[temp];
633
8.39k
    if (aindex >= 0)
634
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
8.39k
    ptr += ps;
636
8.39k
  }
637
5.64k
  return 1;
638
5.64k
}
Unexecuted instantiation: rdppm-12.c:get_word_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_row
639
640
641
METHODDEF(JDIMENSION)
642
get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
643
/* This version is for reading raw-word-format PPM files with any maxval */
644
0
{
645
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
646
0
  register _JSAMPROW ptr;
647
0
  register U_CHAR *bufferptr;
648
0
  register _JSAMPLE *rescale = source->rescale;
649
0
  JDIMENSION col;
650
0
  unsigned int maxval = source->maxval;
651
652
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
653
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
654
0
  ptr = source->pub._buffer[0];
655
0
  bufferptr = source->iobuffer;
656
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
657
0
    for (col = cinfo->image_width; col > 0; col--) {
658
0
      register unsigned int r, g, b;
659
0
      r  = UCH(*bufferptr++) << 8;
660
0
      r |= UCH(*bufferptr++);
661
0
      if (r > maxval)
662
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
663
0
      g  = UCH(*bufferptr++) << 8;
664
0
      g |= UCH(*bufferptr++);
665
0
      if (g > maxval)
666
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
667
0
      b  = UCH(*bufferptr++) << 8;
668
0
      b |= UCH(*bufferptr++);
669
0
      if (b > maxval)
670
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
671
0
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
672
0
                  ptr + 2, ptr + 3);
673
0
      ptr += 4;
674
0
    }
675
0
  } else {
676
0
    for (col = cinfo->image_width; col > 0; col--) {
677
0
      register unsigned int r, g, b;
678
0
      r  = UCH(*bufferptr++) << 8;
679
0
      r |= UCH(*bufferptr++);
680
0
      if (r > maxval)
681
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
682
0
      g  = UCH(*bufferptr++) << 8;
683
0
      g |= UCH(*bufferptr++);
684
0
      if (g > maxval)
685
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
686
0
      b  = UCH(*bufferptr++) << 8;
687
0
      b |= UCH(*bufferptr++);
688
0
      if (b > maxval)
689
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
690
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
691
0
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
692
0
      ptr += 4;
693
0
    }
694
0
  }
695
0
  return 1;
696
0
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_word_rgb_cmyk_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_cmyk_row
697
698
699
/*
700
 * Read the file header; return image size and component count.
701
 */
702
703
METHODDEF(void)
704
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
705
14.1k
{
706
14.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
14.1k
  int c;
708
14.1k
  unsigned int w, h, maxval;
709
14.1k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
14.1k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
14.1k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
14.1k
  switch (c) {
718
1.04k
  case '2':                     /* it's a text-format PGM file */
719
2.10k
  case '3':                     /* it's a text-format PPM file */
720
11.8k
  case '5':                     /* it's a raw-format PGM file */
721
14.1k
  case '6':                     /* it's a raw-format PPM file */
722
14.1k
    break;
723
54
  default:
724
54
    ERREXIT(cinfo, JERR_PPM_NOT);
725
54
    break;
726
14.1k
  }
727
728
  /* fetch the remaining header info */
729
14.1k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
14.1k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
14.1k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
14.1k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
18
    ERREXIT(cinfo, JERR_PPM_NOT);
735
14.1k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
204
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
14.1k
  cinfo->image_width = (JDIMENSION)w;
739
14.1k
  cinfo->image_height = (JDIMENSION)h;
740
14.1k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
14.1k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
14.1k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
14.1k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
14.1k
  switch (c) {
748
744
  case '2':                     /* it's a text-format PGM file */
749
744
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
744
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
744
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
744
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
124
      source->pub.get_pixel_rows = get_text_gray_row;
755
620
    else if (IsExtRGB(cinfo->in_color_space))
756
620
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
0
    else if (cinfo->in_color_space == JCS_CMYK)
758
0
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
744
    need_iobuffer = FALSE;
762
744
    break;
763
764
804
  case '3':                     /* it's a text-format PPM file */
765
804
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
804
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
804
    if (IsExtRGB(cinfo->in_color_space))
769
670
      source->pub.get_pixel_rows = get_text_rgb_row;
770
134
    else if (cinfo->in_color_space == JCS_CMYK)
771
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
134
    else
773
134
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
804
    need_iobuffer = FALSE;
775
804
    break;
776
777
9.33k
  case '5':                     /* it's a raw-format PGM file */
778
9.33k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
9.33k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
9.33k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
9.33k
    if (maxval > 255) {
783
504
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
84
        source->pub.get_pixel_rows = get_word_gray_row;
785
420
      else if (IsExtRGB(cinfo->in_color_space))
786
420
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
0
      else if (cinfo->in_color_space == JCS_CMYK)
788
0
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
8.82k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
8.82k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
900
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
150
      source->pub.get_pixel_rows = get_raw_row;
795
150
      use_raw_buffer = TRUE;
796
150
      need_rescale = FALSE;
797
8.67k
    } else {
798
8.67k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.32k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
7.35k
      else if (IsExtRGB(cinfo->in_color_space))
801
7.35k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
0
      else if (cinfo->in_color_space == JCS_CMYK)
803
0
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
8.67k
    }
807
9.33k
    break;
808
809
2.06k
  case '6':                     /* it's a raw-format PPM file */
810
2.06k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.06k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.06k
    if (maxval > 255) {
814
594
      if (IsExtRGB(cinfo->in_color_space))
815
495
        source->pub.get_pixel_rows = get_word_rgb_row;
816
99
      else if (cinfo->in_color_space == JCS_CMYK)
817
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
99
      else
819
99
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.47k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
1.47k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
366
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
366
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
305
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
61
      source->pub.get_pixel_rows = get_raw_row;
829
61
      use_raw_buffer = TRUE;
830
61
      need_rescale = FALSE;
831
1.40k
    } else {
832
1.40k
      if (IsExtRGB(cinfo->in_color_space))
833
1.16k
        source->pub.get_pixel_rows = get_rgb_row;
834
245
      else if (cinfo->in_color_space == JCS_CMYK)
835
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
245
      else
837
245
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.40k
    }
839
2.06k
    break;
840
14.1k
  }
841
842
12.4k
  if (IsExtRGB(cinfo->in_color_space))
843
10.7k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
1.67k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.67k
    cinfo->input_components = 1;
846
0
  else if (cinfo->in_color_space == JCS_CMYK)
847
0
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
12.4k
  if (need_iobuffer) {
851
11.0k
    if (c == '6')
852
1.72k
      source->buffer_width = (size_t)w * 3 *
853
1.72k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
9.33k
    else
855
9.33k
      source->buffer_width = (size_t)w *
856
9.33k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
11.0k
    source->iobuffer = (U_CHAR *)
858
11.0k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
11.0k
                                  source->buffer_width);
860
11.0k
  }
861
862
  /* Create compressor input buffer. */
863
12.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
211
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
211
    source->pub._buffer = &source->pixrow;
868
211
    source->pub.buffer_height = 1;
869
12.2k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
12.2k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
12.2k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
12.2k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
12.2k
    source->pub.buffer_height = 1;
875
12.2k
  }
876
877
  /* Compute the rescaling array if required. */
878
12.4k
  if (need_rescale) {
879
12.2k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
12.2k
    source->rescale = (_JSAMPLE *)
883
12.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
12.2k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
12.2k
                                           sizeof(_JSAMPLE)));
886
12.2k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
12.2k
                                        sizeof(_JSAMPLE)));
888
12.2k
    half_maxval = maxval / 2;
889
14.2M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
14.2M
      source->rescale[val] =
892
14.2M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
14.2M
                   maxval);
894
14.2M
    }
895
12.2k
  }
896
12.4k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
705
14.1k
{
706
14.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
14.1k
  int c;
708
14.1k
  unsigned int w, h, maxval;
709
14.1k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
14.1k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
14.1k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
14.1k
  switch (c) {
718
1.04k
  case '2':                     /* it's a text-format PGM file */
719
2.10k
  case '3':                     /* it's a text-format PPM file */
720
11.8k
  case '5':                     /* it's a raw-format PGM file */
721
14.1k
  case '6':                     /* it's a raw-format PPM file */
722
14.1k
    break;
723
54
  default:
724
54
    ERREXIT(cinfo, JERR_PPM_NOT);
725
54
    break;
726
14.1k
  }
727
728
  /* fetch the remaining header info */
729
14.1k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
14.1k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
14.1k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
14.1k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
18
    ERREXIT(cinfo, JERR_PPM_NOT);
735
14.1k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
204
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
14.1k
  cinfo->image_width = (JDIMENSION)w;
739
14.1k
  cinfo->image_height = (JDIMENSION)h;
740
14.1k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
14.1k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
14.1k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
14.1k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
14.1k
  switch (c) {
748
744
  case '2':                     /* it's a text-format PGM file */
749
744
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
744
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
744
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
744
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
124
      source->pub.get_pixel_rows = get_text_gray_row;
755
620
    else if (IsExtRGB(cinfo->in_color_space))
756
620
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
0
    else if (cinfo->in_color_space == JCS_CMYK)
758
0
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
744
    need_iobuffer = FALSE;
762
744
    break;
763
764
804
  case '3':                     /* it's a text-format PPM file */
765
804
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
804
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
804
    if (IsExtRGB(cinfo->in_color_space))
769
670
      source->pub.get_pixel_rows = get_text_rgb_row;
770
134
    else if (cinfo->in_color_space == JCS_CMYK)
771
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
134
    else
773
134
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
804
    need_iobuffer = FALSE;
775
804
    break;
776
777
9.33k
  case '5':                     /* it's a raw-format PGM file */
778
9.33k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
9.33k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
9.33k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
9.33k
    if (maxval > 255) {
783
504
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
84
        source->pub.get_pixel_rows = get_word_gray_row;
785
420
      else if (IsExtRGB(cinfo->in_color_space))
786
420
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
0
      else if (cinfo->in_color_space == JCS_CMYK)
788
0
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
8.82k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
8.82k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
900
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
150
      source->pub.get_pixel_rows = get_raw_row;
795
150
      use_raw_buffer = TRUE;
796
150
      need_rescale = FALSE;
797
8.67k
    } else {
798
8.67k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.32k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
7.35k
      else if (IsExtRGB(cinfo->in_color_space))
801
7.35k
        source->pub.get_pixel_rows = get_gray_rgb_row;
802
0
      else if (cinfo->in_color_space == JCS_CMYK)
803
0
        source->pub.get_pixel_rows = get_gray_cmyk_row;
804
0
      else
805
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
806
8.67k
    }
807
9.33k
    break;
808
809
2.06k
  case '6':                     /* it's a raw-format PPM file */
810
2.06k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.06k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.06k
    if (maxval > 255) {
814
594
      if (IsExtRGB(cinfo->in_color_space))
815
495
        source->pub.get_pixel_rows = get_word_rgb_row;
816
99
      else if (cinfo->in_color_space == JCS_CMYK)
817
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
99
      else
819
99
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.47k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
1.47k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
366
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
366
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
305
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
61
      source->pub.get_pixel_rows = get_raw_row;
829
61
      use_raw_buffer = TRUE;
830
61
      need_rescale = FALSE;
831
1.40k
    } else {
832
1.40k
      if (IsExtRGB(cinfo->in_color_space))
833
1.16k
        source->pub.get_pixel_rows = get_rgb_row;
834
245
      else if (cinfo->in_color_space == JCS_CMYK)
835
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
245
      else
837
245
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.40k
    }
839
2.06k
    break;
840
14.1k
  }
841
842
12.4k
  if (IsExtRGB(cinfo->in_color_space))
843
10.7k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
1.67k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.67k
    cinfo->input_components = 1;
846
0
  else if (cinfo->in_color_space == JCS_CMYK)
847
0
    cinfo->input_components = 4;
848
849
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
850
12.4k
  if (need_iobuffer) {
851
11.0k
    if (c == '6')
852
1.72k
      source->buffer_width = (size_t)w * 3 *
853
1.72k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
9.33k
    else
855
9.33k
      source->buffer_width = (size_t)w *
856
9.33k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
11.0k
    source->iobuffer = (U_CHAR *)
858
11.0k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
11.0k
                                  source->buffer_width);
860
11.0k
  }
861
862
  /* Create compressor input buffer. */
863
12.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
211
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
211
    source->pub._buffer = &source->pixrow;
868
211
    source->pub.buffer_height = 1;
869
12.2k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
12.2k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
12.2k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
12.2k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
12.2k
    source->pub.buffer_height = 1;
875
12.2k
  }
876
877
  /* Compute the rescaling array if required. */
878
12.4k
  if (need_rescale) {
879
12.2k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
12.2k
    source->rescale = (_JSAMPLE *)
883
12.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
12.2k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
12.2k
                                           sizeof(_JSAMPLE)));
886
12.2k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
12.2k
                                        sizeof(_JSAMPLE)));
888
12.2k
    half_maxval = maxval / 2;
889
14.2M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
14.2M
      source->rescale[val] =
892
14.2M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
14.2M
                   maxval);
894
14.2M
    }
895
12.2k
  }
896
12.4k
}
Unexecuted instantiation: rdppm-12.c:start_input_ppm
Unexecuted instantiation: rdppm-16.c:start_input_ppm
897
898
899
/*
900
 * Finish up at the end of the file.
901
 */
902
903
METHODDEF(void)
904
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
905
9.27k
{
906
  /* no work */
907
9.27k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
905
9.27k
{
906
  /* no work */
907
9.27k
}
Unexecuted instantiation: rdppm-12.c:finish_input_ppm
Unexecuted instantiation: rdppm-16.c:finish_input_ppm
908
909
910
/*
911
 * The module selection routine for PPM format input.
912
 */
913
914
GLOBAL(cjpeg_source_ptr)
915
_jinit_read_ppm(j_compress_ptr cinfo)
916
14.1k
{
917
14.1k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
14.1k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
921
#else
922
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
924
0
#endif
925
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
926
927
  /* Create module interface object */
928
14.1k
  source = (ppm_source_ptr)
929
14.1k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
14.1k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
14.1k
  source->pub.start_input = start_input_ppm;
933
14.1k
  source->pub.finish_input = finish_input_ppm;
934
14.1k
  source->pub.max_pixels = 0;
935
936
14.1k
  return (cjpeg_source_ptr)source;
937
14.1k
}
jinit_read_ppm
Line
Count
Source
916
14.1k
{
917
14.1k
  ppm_source_ptr source;
918
919
14.1k
#if BITS_IN_JSAMPLE == 8
920
14.1k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
921
#else
922
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
923
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
924
#endif
925
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
926
927
  /* Create module interface object */
928
14.1k
  source = (ppm_source_ptr)
929
14.1k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
14.1k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
14.1k
  source->pub.start_input = start_input_ppm;
933
14.1k
  source->pub.finish_input = finish_input_ppm;
934
14.1k
  source->pub.max_pixels = 0;
935
936
14.1k
  return (cjpeg_source_ptr)source;
937
14.1k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
938
939
#endif /* defined(PPM_SUPPORTED) &&
940
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */