Coverage Report

Created: 2026-01-09 06:34

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
7.82M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
30.8M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
53
54
static int alpha_index[JPEG_NUMCS] = {
55
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
56
};
57
58
59
/* Private version of data source object */
60
61
typedef struct {
62
  struct cjpeg_source_struct pub; /* public fields */
63
64
  /* Usually these two pointers point to the same place: */
65
  U_CHAR *iobuffer;             /* fread's I/O buffer */
66
  _JSAMPROW pixrow;             /* compressor input buffer */
67
  size_t buffer_width;          /* width of I/O buffer */
68
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
69
  unsigned int maxval;
70
} ppm_source_struct;
71
72
typedef ppm_source_struct *ppm_source_ptr;
73
74
75
LOCAL(int)
76
pbm_getc(FILE *infile)
77
/* Read next char, skipping over any comments */
78
/* A comment/newline sequence is returned as a newline */
79
263k
{
80
263k
  register int ch;
81
82
263k
  ch = getc(infile);
83
263k
  if (ch == '#') {
84
29.8k
    do {
85
29.8k
      ch = getc(infile);
86
29.8k
    } while (ch != '\n' && ch != EOF);
87
3.45k
  }
88
263k
  return ch;
89
263k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
263k
{
80
263k
  register int ch;
81
82
263k
  ch = getc(infile);
83
263k
  if (ch == '#') {
84
29.8k
    do {
85
29.8k
      ch = getc(infile);
86
29.8k
    } while (ch != '\n' && ch != EOF);
87
3.45k
  }
88
263k
  return ch;
89
263k
}
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
108k
{
99
108k
  register int ch;
100
108k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
114k
  do {
104
114k
    ch = pbm_getc(infile);
105
114k
    if (ch == EOF)
106
2.02k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
114k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
108k
  if (ch < '0' || ch > '9')
110
184
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
108k
  val = ch - '0';
113
150k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
42.3k
    val *= 10;
115
42.3k
    val += ch - '0';
116
42.3k
    if (val > maxval)
117
113
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
42.3k
  }
119
120
108k
  return val;
121
108k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
108k
{
99
108k
  register int ch;
100
108k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
114k
  do {
104
114k
    ch = pbm_getc(infile);
105
114k
    if (ch == EOF)
106
2.02k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
114k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
108k
  if (ch < '0' || ch > '9')
110
184
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
108k
  val = ch - '0';
113
150k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
42.3k
    val *= 10;
115
42.3k
    val += ch - '0';
116
42.3k
    if (val > maxval)
117
113
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
42.3k
  }
119
120
108k
  return val;
121
108k
}
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.04k
{
140
2.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.04k
  FILE *infile = source->pub.input_file;
142
2.04k
  register _JSAMPROW ptr;
143
2.04k
  register _JSAMPLE *rescale = source->rescale;
144
2.04k
  JDIMENSION col;
145
2.04k
  unsigned int maxval = source->maxval;
146
147
2.04k
  ptr = source->pub._buffer[0];
148
5.41k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.37k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.37k
  }
151
2.04k
  return 1;
152
2.04k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
2.04k
{
140
2.04k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.04k
  FILE *infile = source->pub.input_file;
142
2.04k
  register _JSAMPROW ptr;
143
2.04k
  register _JSAMPLE *rescale = source->rescale;
144
2.04k
  JDIMENSION col;
145
2.04k
  unsigned int maxval = source->maxval;
146
147
2.04k
  ptr = source->pub._buffer[0];
148
5.41k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.37k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.37k
  }
151
2.04k
  return 1;
152
2.04k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
25.6M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
66.5M
  for (col = cinfo->image_width; col > 0; col--) { \
157
40.9M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
40.9M
    alpha_set_op \
159
40.9M
    ptr += ps; \
160
40.9M
  } \
161
25.6M
}
162
163
METHODDEF(JDIMENSION)
164
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
165
/* This version is for reading text-format PGM files with any maxval and
166
   converting to extended RGB */
167
10.2k
{
168
10.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.2k
  FILE *infile = source->pub.input_file;
170
10.2k
  register _JSAMPROW ptr;
171
10.2k
  register _JSAMPLE *rescale = source->rescale;
172
10.2k
  JDIMENSION col;
173
10.2k
  unsigned int maxval = source->maxval;
174
10.2k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.2k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.2k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.2k
  ptr = source->pub._buffer[0];
181
10.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
2.66k
    if (aindex >= 0)
183
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
2.66k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
2.66k
    else
186
2.66k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
7.53k
  } else {
188
7.53k
    if (aindex >= 0)
189
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.53k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.53k
    else
192
7.53k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
7.53k
  }
194
10.2k
  return 1;
195
10.2k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
10.2k
{
168
10.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.2k
  FILE *infile = source->pub.input_file;
170
10.2k
  register _JSAMPROW ptr;
171
10.2k
  register _JSAMPLE *rescale = source->rescale;
172
10.2k
  JDIMENSION col;
173
10.2k
  unsigned int maxval = source->maxval;
174
10.2k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.2k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.2k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.2k
  ptr = source->pub._buffer[0];
181
10.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
2.66k
    if (aindex >= 0)
183
0
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
2.66k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
2.66k
    else
186
2.66k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
7.53k
  } else {
188
7.53k
    if (aindex >= 0)
189
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.53k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.53k
    else
192
7.53k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
7.53k
  }
194
10.2k
  return 1;
195
10.2k
}
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
91.8k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
230
1.50M
  for (col = cinfo->image_width; col > 0; col--) { \
231
1.41M
    ptr[rindex] = read_op; \
232
1.41M
    ptr[gindex] = read_op; \
233
1.41M
    ptr[bindex] = read_op; \
234
1.41M
    alpha_set_op \
235
1.41M
    ptr += ps; \
236
1.41M
  } \
237
91.8k
}
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.71k
{
243
8.71k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.71k
  FILE *infile = source->pub.input_file;
245
8.71k
  register _JSAMPROW ptr;
246
8.71k
  register _JSAMPLE *rescale = source->rescale;
247
8.71k
  JDIMENSION col;
248
8.71k
  unsigned int maxval = source->maxval;
249
8.71k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.71k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.71k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.71k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.71k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.71k
  ptr = source->pub._buffer[0];
256
8.71k
  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
5.93k
  } else {
263
5.93k
    if (aindex >= 0)
264
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
5.93k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
5.93k
    else
267
5.93k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
5.93k
  }
269
8.71k
  return 1;
270
8.71k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
242
8.71k
{
243
8.71k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
244
8.71k
  FILE *infile = source->pub.input_file;
245
8.71k
  register _JSAMPROW ptr;
246
8.71k
  register _JSAMPLE *rescale = source->rescale;
247
8.71k
  JDIMENSION col;
248
8.71k
  unsigned int maxval = source->maxval;
249
8.71k
  register int rindex = rgb_red[cinfo->in_color_space];
250
8.71k
  register int gindex = rgb_green[cinfo->in_color_space];
251
8.71k
  register int bindex = rgb_blue[cinfo->in_color_space];
252
8.71k
  register int aindex = alpha_index[cinfo->in_color_space];
253
8.71k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
254
255
8.71k
  ptr = source->pub._buffer[0];
256
8.71k
  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
5.93k
  } else {
263
5.93k
    if (aindex >= 0)
264
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
265
5.93k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
266
5.93k
    else
267
5.93k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
268
5.93k
  }
269
8.71k
  return 1;
270
8.71k
}
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
3.98M
{
312
3.98M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
3.98M
  register _JSAMPROW ptr;
314
3.98M
  register U_CHAR *bufferptr;
315
3.98M
  register _JSAMPLE *rescale = source->rescale;
316
3.98M
  JDIMENSION col;
317
318
3.98M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
3.98M
  ptr = source->pub._buffer[0];
321
3.98M
  bufferptr = source->iobuffer;
322
10.9M
  for (col = cinfo->image_width; col > 0; col--) {
323
6.96M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
6.96M
  }
325
3.98M
  return 1;
326
3.98M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
311
3.98M
{
312
3.98M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
313
3.98M
  register _JSAMPROW ptr;
314
3.98M
  register U_CHAR *bufferptr;
315
3.98M
  register _JSAMPLE *rescale = source->rescale;
316
3.98M
  JDIMENSION col;
317
318
3.98M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
319
56
    ERREXIT(cinfo, JERR_INPUT_EOF);
320
3.98M
  ptr = source->pub._buffer[0];
321
3.98M
  bufferptr = source->iobuffer;
322
10.9M
  for (col = cinfo->image_width; col > 0; col--) {
323
6.96M
    *ptr++ = rescale[UCH(*bufferptr++)];
324
6.96M
  }
325
3.98M
  return 1;
326
3.98M
}
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
25.6M
{
334
25.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
25.6M
  register _JSAMPROW ptr;
336
25.6M
  register U_CHAR *bufferptr;
337
25.6M
  register _JSAMPLE *rescale = source->rescale;
338
25.6M
  JDIMENSION col;
339
25.6M
  unsigned int maxval = source->maxval;
340
25.6M
  register int rindex = rgb_red[cinfo->in_color_space];
341
25.6M
  register int gindex = rgb_green[cinfo->in_color_space];
342
25.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
25.6M
  register int aindex = alpha_index[cinfo->in_color_space];
344
25.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
25.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
400
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
25.6M
  ptr = source->pub._buffer[0];
349
25.6M
  bufferptr = source->iobuffer;
350
25.6M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
5.69M
    if (aindex >= 0)
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
5.69M
    else
354
5.69M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
19.9M
  } else {
356
19.9M
    if (aindex >= 0)
357
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
19.9M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
19.9M
    else
360
19.9M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
19.9M
  }
362
25.6M
  return 1;
363
25.6M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
333
25.6M
{
334
25.6M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
335
25.6M
  register _JSAMPROW ptr;
336
25.6M
  register U_CHAR *bufferptr;
337
25.6M
  register _JSAMPLE *rescale = source->rescale;
338
25.6M
  JDIMENSION col;
339
25.6M
  unsigned int maxval = source->maxval;
340
25.6M
  register int rindex = rgb_red[cinfo->in_color_space];
341
25.6M
  register int gindex = rgb_green[cinfo->in_color_space];
342
25.6M
  register int bindex = rgb_blue[cinfo->in_color_space];
343
25.6M
  register int aindex = alpha_index[cinfo->in_color_space];
344
25.6M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
345
346
25.6M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
347
400
    ERREXIT(cinfo, JERR_INPUT_EOF);
348
25.6M
  ptr = source->pub._buffer[0];
349
25.6M
  bufferptr = source->iobuffer;
350
25.6M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
351
5.69M
    if (aindex >= 0)
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
353
5.69M
    else
354
5.69M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
355
19.9M
  } else {
356
19.9M
    if (aindex >= 0)
357
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
358
19.9M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
359
19.9M
    else
360
19.9M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
361
19.9M
  }
362
25.6M
  return 1;
363
25.6M
}
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_gray_rgb_row
364
365
366
METHODDEF(JDIMENSION)
367
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
368
/* This version is for reading raw-byte-format PGM files with any maxval
369
   and converting to CMYK */
370
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
83.1k
{
404
83.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
83.1k
  register _JSAMPROW ptr;
406
83.1k
  register U_CHAR *bufferptr;
407
83.1k
  register _JSAMPLE *rescale = source->rescale;
408
83.1k
  JDIMENSION col;
409
83.1k
  unsigned int maxval = source->maxval;
410
83.1k
  register int rindex = rgb_red[cinfo->in_color_space];
411
83.1k
  register int gindex = rgb_green[cinfo->in_color_space];
412
83.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
83.1k
  register int aindex = alpha_index[cinfo->in_color_space];
414
83.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
83.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
368
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
83.1k
  ptr = source->pub._buffer[0];
419
83.1k
  bufferptr = source->iobuffer;
420
83.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
24.9k
    if (aindex >= 0)
422
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
24.9k
    else
424
24.9k
      RGB_READ_LOOP(*bufferptr++, {})
425
58.2k
  } else {
426
58.2k
    if (aindex >= 0)
427
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
58.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
58.2k
    else
430
58.2k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
58.2k
  }
432
83.1k
  return 1;
433
83.1k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
403
83.1k
{
404
83.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
405
83.1k
  register _JSAMPROW ptr;
406
83.1k
  register U_CHAR *bufferptr;
407
83.1k
  register _JSAMPLE *rescale = source->rescale;
408
83.1k
  JDIMENSION col;
409
83.1k
  unsigned int maxval = source->maxval;
410
83.1k
  register int rindex = rgb_red[cinfo->in_color_space];
411
83.1k
  register int gindex = rgb_green[cinfo->in_color_space];
412
83.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
413
83.1k
  register int aindex = alpha_index[cinfo->in_color_space];
414
83.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
415
416
83.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
417
368
    ERREXIT(cinfo, JERR_INPUT_EOF);
418
83.1k
  ptr = source->pub._buffer[0];
419
83.1k
  bufferptr = source->iobuffer;
420
83.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
421
24.9k
    if (aindex >= 0)
422
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
423
24.9k
    else
424
24.9k
      RGB_READ_LOOP(*bufferptr++, {})
425
58.2k
  } else {
426
58.2k
    if (aindex >= 0)
427
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
428
58.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
429
58.2k
    else
430
58.2k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
431
58.2k
  }
432
83.1k
  return 1;
433
83.1k
}
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
1.14M
{
482
1.14M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
1.14M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
41
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
1.14M
  return 1;
487
1.14M
}
rdppm-8.c:get_raw_row
Line
Count
Source
481
1.14M
{
482
1.14M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
483
484
1.14M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
485
41
    ERREXIT(cinfo, JERR_INPUT_EOF);
486
1.14M
  return 1;
487
1.14M
}
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
51
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.32k
  ptr = source->pub._buffer[0];
504
1.32k
  bufferptr = source->iobuffer;
505
68.8k
  for (col = cinfo->image_width; col > 0; col--) {
506
67.5k
    register unsigned int temp;
507
67.5k
    temp  = UCH(*bufferptr++) << 8;
508
67.5k
    temp |= UCH(*bufferptr++);
509
67.5k
    if (temp > maxval)
510
27
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
67.5k
    *ptr++ = rescale[temp];
512
67.5k
  }
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
51
    ERREXIT(cinfo, JERR_INPUT_EOF);
503
1.32k
  ptr = source->pub._buffer[0];
504
1.32k
  bufferptr = source->iobuffer;
505
68.8k
  for (col = cinfo->image_width; col > 0; col--) {
506
67.5k
    register unsigned int temp;
507
67.5k
    temp  = UCH(*bufferptr++) << 8;
508
67.5k
    temp |= UCH(*bufferptr++);
509
67.5k
    if (temp > maxval)
510
27
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
511
67.5k
    *ptr++ = rescale[temp];
512
67.5k
  }
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.63k
{
521
6.63k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
6.63k
  register _JSAMPROW ptr;
523
6.63k
  register U_CHAR *bufferptr;
524
6.63k
  register _JSAMPLE *rescale = source->rescale;
525
6.63k
  JDIMENSION col;
526
6.63k
  unsigned int maxval = source->maxval;
527
6.63k
  register int rindex = rgb_red[cinfo->in_color_space];
528
6.63k
  register int gindex = rgb_green[cinfo->in_color_space];
529
6.63k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
6.63k
  register int aindex = alpha_index[cinfo->in_color_space];
531
6.63k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
6.63k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
255
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
6.63k
  ptr = source->pub._buffer[0];
536
6.63k
  bufferptr = source->iobuffer;
537
344k
  for (col = cinfo->image_width; col > 0; col--) {
538
337k
    register unsigned int temp;
539
337k
    temp  = UCH(*bufferptr++) << 8;
540
337k
    temp |= UCH(*bufferptr++);
541
337k
    if (temp > maxval)
542
135
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
337k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
337k
    if (aindex >= 0)
545
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
337k
    ptr += ps;
547
337k
  }
548
6.63k
  return 1;
549
6.63k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
520
6.63k
{
521
6.63k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
522
6.63k
  register _JSAMPROW ptr;
523
6.63k
  register U_CHAR *bufferptr;
524
6.63k
  register _JSAMPLE *rescale = source->rescale;
525
6.63k
  JDIMENSION col;
526
6.63k
  unsigned int maxval = source->maxval;
527
6.63k
  register int rindex = rgb_red[cinfo->in_color_space];
528
6.63k
  register int gindex = rgb_green[cinfo->in_color_space];
529
6.63k
  register int bindex = rgb_blue[cinfo->in_color_space];
530
6.63k
  register int aindex = alpha_index[cinfo->in_color_space];
531
6.63k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
532
533
6.63k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
534
255
    ERREXIT(cinfo, JERR_INPUT_EOF);
535
6.63k
  ptr = source->pub._buffer[0];
536
6.63k
  bufferptr = source->iobuffer;
537
344k
  for (col = cinfo->image_width; col > 0; col--) {
538
337k
    register unsigned int temp;
539
337k
    temp  = UCH(*bufferptr++) << 8;
540
337k
    temp |= UCH(*bufferptr++);
541
337k
    if (temp > maxval)
542
135
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
543
337k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
544
337k
    if (aindex >= 0)
545
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
546
337k
    ptr += ps;
547
337k
  }
548
6.63k
  return 1;
549
6.63k
}
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.60k
{
600
5.60k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
5.60k
  register _JSAMPROW ptr;
602
5.60k
  register U_CHAR *bufferptr;
603
5.60k
  register _JSAMPLE *rescale = source->rescale;
604
5.60k
  JDIMENSION col;
605
5.60k
  unsigned int maxval = source->maxval;
606
5.60k
  register int rindex = rgb_red[cinfo->in_color_space];
607
5.60k
  register int gindex = rgb_green[cinfo->in_color_space];
608
5.60k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
5.60k
  register int aindex = alpha_index[cinfo->in_color_space];
610
5.60k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
5.60k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
225
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
5.60k
  ptr = source->pub._buffer[0];
615
5.60k
  bufferptr = source->iobuffer;
616
13.7k
  for (col = cinfo->image_width; col > 0; col--) {
617
8.15k
    register unsigned int temp;
618
8.15k
    temp  = UCH(*bufferptr++) << 8;
619
8.15k
    temp |= UCH(*bufferptr++);
620
8.15k
    if (temp > maxval)
621
35
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
8.15k
    ptr[rindex] = rescale[temp];
623
8.15k
    temp  = UCH(*bufferptr++) << 8;
624
8.15k
    temp |= UCH(*bufferptr++);
625
8.15k
    if (temp > maxval)
626
95
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
8.15k
    ptr[gindex] = rescale[temp];
628
8.15k
    temp  = UCH(*bufferptr++) << 8;
629
8.15k
    temp |= UCH(*bufferptr++);
630
8.15k
    if (temp > maxval)
631
100
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
8.15k
    ptr[bindex] = rescale[temp];
633
8.15k
    if (aindex >= 0)
634
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
8.15k
    ptr += ps;
636
8.15k
  }
637
5.60k
  return 1;
638
5.60k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
599
5.60k
{
600
5.60k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
601
5.60k
  register _JSAMPROW ptr;
602
5.60k
  register U_CHAR *bufferptr;
603
5.60k
  register _JSAMPLE *rescale = source->rescale;
604
5.60k
  JDIMENSION col;
605
5.60k
  unsigned int maxval = source->maxval;
606
5.60k
  register int rindex = rgb_red[cinfo->in_color_space];
607
5.60k
  register int gindex = rgb_green[cinfo->in_color_space];
608
5.60k
  register int bindex = rgb_blue[cinfo->in_color_space];
609
5.60k
  register int aindex = alpha_index[cinfo->in_color_space];
610
5.60k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
611
612
5.60k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
613
225
    ERREXIT(cinfo, JERR_INPUT_EOF);
614
5.60k
  ptr = source->pub._buffer[0];
615
5.60k
  bufferptr = source->iobuffer;
616
13.7k
  for (col = cinfo->image_width; col > 0; col--) {
617
8.15k
    register unsigned int temp;
618
8.15k
    temp  = UCH(*bufferptr++) << 8;
619
8.15k
    temp |= UCH(*bufferptr++);
620
8.15k
    if (temp > maxval)
621
35
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
622
8.15k
    ptr[rindex] = rescale[temp];
623
8.15k
    temp  = UCH(*bufferptr++) << 8;
624
8.15k
    temp |= UCH(*bufferptr++);
625
8.15k
    if (temp > maxval)
626
95
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
627
8.15k
    ptr[gindex] = rescale[temp];
628
8.15k
    temp  = UCH(*bufferptr++) << 8;
629
8.15k
    temp |= UCH(*bufferptr++);
630
8.15k
    if (temp > maxval)
631
100
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
632
8.15k
    ptr[bindex] = rescale[temp];
633
8.15k
    if (aindex >= 0)
634
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
635
8.15k
    ptr += ps;
636
8.15k
  }
637
5.60k
  return 1;
638
5.60k
}
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
15.3k
{
706
15.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
15.3k
  int c;
708
15.3k
  unsigned int w, h, maxval;
709
15.3k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
15.3k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
15.3k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
15.3k
  switch (c) {
718
1.12k
  case '2':                     /* it's a text-format PGM file */
719
2.15k
  case '3':                     /* it's a text-format PPM file */
720
12.9k
  case '5':                     /* it's a raw-format PGM file */
721
15.3k
  case '6':                     /* it's a raw-format PPM file */
722
15.3k
    break;
723
6
  default:
724
6
    ERREXIT(cinfo, JERR_PPM_NOT);
725
6
    break;
726
15.3k
  }
727
728
  /* fetch the remaining header info */
729
15.3k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
15.3k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
15.3k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
15.3k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
24
    ERREXIT(cinfo, JERR_PPM_NOT);
735
15.3k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
186
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
15.3k
  cinfo->image_width = (JDIMENSION)w;
739
15.3k
  cinfo->image_height = (JDIMENSION)h;
740
15.3k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
15.3k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
15.3k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
15.3k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
15.3k
  switch (c) {
748
810
  case '2':                     /* it's a text-format PGM file */
749
810
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
810
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
810
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
810
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
135
      source->pub.get_pixel_rows = get_text_gray_row;
755
675
    else if (IsExtRGB(cinfo->in_color_space))
756
675
      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
810
    need_iobuffer = FALSE;
762
810
    break;
763
764
798
  case '3':                     /* it's a text-format PPM file */
765
798
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
798
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
798
    if (IsExtRGB(cinfo->in_color_space))
769
665
      source->pub.get_pixel_rows = get_text_rgb_row;
770
133
    else if (cinfo->in_color_space == JCS_CMYK)
771
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
133
    else
773
133
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
798
    need_iobuffer = FALSE;
775
798
    break;
776
777
10.3k
  case '5':                     /* it's a raw-format PGM file */
778
10.3k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
10.3k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
10.3k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
10.3k
    if (maxval > 255) {
783
516
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
86
        source->pub.get_pixel_rows = get_word_gray_row;
785
430
      else if (IsExtRGB(cinfo->in_color_space))
786
430
        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
9.83k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
9.83k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
954
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
159
      source->pub.get_pixel_rows = get_raw_row;
795
159
      use_raw_buffer = TRUE;
796
159
      need_rescale = FALSE;
797
9.67k
    } else {
798
9.67k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.48k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
8.19k
      else if (IsExtRGB(cinfo->in_color_space))
801
8.19k
        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
9.67k
    }
807
10.3k
    break;
808
809
2.19k
  case '6':                     /* it's a raw-format PPM file */
810
2.19k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.19k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.19k
    if (maxval > 255) {
814
546
      if (IsExtRGB(cinfo->in_color_space))
815
455
        source->pub.get_pixel_rows = get_word_rgb_row;
816
91
      else if (cinfo->in_color_space == JCS_CMYK)
817
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
91
      else
819
91
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.64k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
1.64k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
306
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
306
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
255
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
51
      source->pub.get_pixel_rows = get_raw_row;
829
51
      use_raw_buffer = TRUE;
830
51
      need_rescale = FALSE;
831
1.59k
    } else {
832
1.59k
      if (IsExtRGB(cinfo->in_color_space))
833
1.31k
        source->pub.get_pixel_rows = get_rgb_row;
834
274
      else if (cinfo->in_color_space == JCS_CMYK)
835
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
274
      else
837
274
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.59k
    }
839
2.19k
    break;
840
15.3k
  }
841
842
13.6k
  if (IsExtRGB(cinfo->in_color_space))
843
11.7k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
1.86k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.86k
    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
13.6k
  if (need_iobuffer) {
851
12.1k
    if (c == '6')
852
1.82k
      source->buffer_width = (size_t)w * 3 *
853
1.82k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
10.3k
    else
855
10.3k
      source->buffer_width = (size_t)w *
856
10.3k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
12.1k
    source->iobuffer = (U_CHAR *)
858
12.1k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
12.1k
                                  source->buffer_width);
860
12.1k
  }
861
862
  /* Create compressor input buffer. */
863
13.6k
  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
210
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
210
    source->pub._buffer = &source->pixrow;
868
210
    source->pub.buffer_height = 1;
869
13.4k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
13.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
13.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
13.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
13.4k
    source->pub.buffer_height = 1;
875
13.4k
  }
876
877
  /* Compute the rescaling array if required. */
878
13.6k
  if (need_rescale) {
879
13.4k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
13.4k
    source->rescale = (_JSAMPLE *)
883
13.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
13.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
13.4k
                                           sizeof(_JSAMPLE)));
886
13.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
13.4k
                                        sizeof(_JSAMPLE)));
888
13.4k
    half_maxval = maxval / 2;
889
15.4M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
15.4M
      source->rescale[val] =
892
15.4M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
15.4M
                   maxval);
894
15.4M
    }
895
13.4k
  }
896
13.6k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
705
15.3k
{
706
15.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
707
15.3k
  int c;
708
15.3k
  unsigned int w, h, maxval;
709
15.3k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
710
711
15.3k
  if (getc(source->pub.input_file) != 'P')
712
0
    ERREXIT(cinfo, JERR_PPM_NOT);
713
714
15.3k
  c = getc(source->pub.input_file); /* subformat discriminator character */
715
716
  /* detect unsupported variants (ie, PBM) before trying to read header */
717
15.3k
  switch (c) {
718
1.12k
  case '2':                     /* it's a text-format PGM file */
719
2.15k
  case '3':                     /* it's a text-format PPM file */
720
12.9k
  case '5':                     /* it's a raw-format PGM file */
721
15.3k
  case '6':                     /* it's a raw-format PPM file */
722
15.3k
    break;
723
6
  default:
724
6
    ERREXIT(cinfo, JERR_PPM_NOT);
725
6
    break;
726
15.3k
  }
727
728
  /* fetch the remaining header info */
729
15.3k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
15.3k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
731
15.3k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
732
733
15.3k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
734
24
    ERREXIT(cinfo, JERR_PPM_NOT);
735
15.3k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
186
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
15.3k
  cinfo->image_width = (JDIMENSION)w;
739
15.3k
  cinfo->image_height = (JDIMENSION)h;
740
15.3k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
15.3k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
15.3k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
15.3k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
15.3k
  switch (c) {
748
810
  case '2':                     /* it's a text-format PGM file */
749
810
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
810
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
810
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
810
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
135
      source->pub.get_pixel_rows = get_text_gray_row;
755
675
    else if (IsExtRGB(cinfo->in_color_space))
756
675
      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
810
    need_iobuffer = FALSE;
762
810
    break;
763
764
798
  case '3':                     /* it's a text-format PPM file */
765
798
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
798
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
798
    if (IsExtRGB(cinfo->in_color_space))
769
665
      source->pub.get_pixel_rows = get_text_rgb_row;
770
133
    else if (cinfo->in_color_space == JCS_CMYK)
771
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
133
    else
773
133
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
798
    need_iobuffer = FALSE;
775
798
    break;
776
777
10.3k
  case '5':                     /* it's a raw-format PGM file */
778
10.3k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
10.3k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
10.3k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
10.3k
    if (maxval > 255) {
783
516
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
86
        source->pub.get_pixel_rows = get_word_gray_row;
785
430
      else if (IsExtRGB(cinfo->in_color_space))
786
430
        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
9.83k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
792
9.83k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
793
954
               cinfo->in_color_space == JCS_GRAYSCALE) {
794
159
      source->pub.get_pixel_rows = get_raw_row;
795
159
      use_raw_buffer = TRUE;
796
159
      need_rescale = FALSE;
797
9.67k
    } else {
798
9.67k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
799
1.48k
        source->pub.get_pixel_rows = get_scaled_gray_row;
800
8.19k
      else if (IsExtRGB(cinfo->in_color_space))
801
8.19k
        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
9.67k
    }
807
10.3k
    break;
808
809
2.19k
  case '6':                     /* it's a raw-format PPM file */
810
2.19k
    if (cinfo->in_color_space == JCS_UNKNOWN)
811
0
      cinfo->in_color_space = JCS_EXT_RGB;
812
2.19k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
813
2.19k
    if (maxval > 255) {
814
546
      if (IsExtRGB(cinfo->in_color_space))
815
455
        source->pub.get_pixel_rows = get_word_rgb_row;
816
91
      else if (cinfo->in_color_space == JCS_CMYK)
817
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
818
91
      else
819
91
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
820
1.64k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
821
1.64k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
822
306
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
823
306
               (cinfo->in_color_space == JCS_EXT_RGB ||
824
255
                cinfo->in_color_space == JCS_RGB)) {
825
#else
826
               cinfo->in_color_space == JCS_EXT_RGB) {
827
#endif
828
51
      source->pub.get_pixel_rows = get_raw_row;
829
51
      use_raw_buffer = TRUE;
830
51
      need_rescale = FALSE;
831
1.59k
    } else {
832
1.59k
      if (IsExtRGB(cinfo->in_color_space))
833
1.31k
        source->pub.get_pixel_rows = get_rgb_row;
834
274
      else if (cinfo->in_color_space == JCS_CMYK)
835
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
836
274
      else
837
274
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
838
1.59k
    }
839
2.19k
    break;
840
15.3k
  }
841
842
13.6k
  if (IsExtRGB(cinfo->in_color_space))
843
11.7k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
844
1.86k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
845
1.86k
    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
13.6k
  if (need_iobuffer) {
851
12.1k
    if (c == '6')
852
1.82k
      source->buffer_width = (size_t)w * 3 *
853
1.82k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
854
10.3k
    else
855
10.3k
      source->buffer_width = (size_t)w *
856
10.3k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
857
12.1k
    source->iobuffer = (U_CHAR *)
858
12.1k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
859
12.1k
                                  source->buffer_width);
860
12.1k
  }
861
862
  /* Create compressor input buffer. */
863
13.6k
  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
210
    source->pixrow = (_JSAMPROW)source->iobuffer;
867
210
    source->pub._buffer = &source->pixrow;
868
210
    source->pub.buffer_height = 1;
869
13.4k
  } else {
870
    /* Need to translate anyway, so make a separate sample buffer. */
871
13.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
872
13.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
873
13.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
874
13.4k
    source->pub.buffer_height = 1;
875
13.4k
  }
876
877
  /* Compute the rescaling array if required. */
878
13.6k
  if (need_rescale) {
879
13.4k
    long val, half_maxval;
880
881
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
882
13.4k
    source->rescale = (_JSAMPLE *)
883
13.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
884
13.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
885
13.4k
                                           sizeof(_JSAMPLE)));
886
13.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
887
13.4k
                                        sizeof(_JSAMPLE)));
888
13.4k
    half_maxval = maxval / 2;
889
15.4M
    for (val = 0; val <= (long)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
15.4M
      source->rescale[val] =
892
15.4M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
15.4M
                   maxval);
894
15.4M
    }
895
13.4k
  }
896
13.6k
}
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
10.4k
{
906
  /* no work */
907
10.4k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
905
10.4k
{
906
  /* no work */
907
10.4k
}
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
15.3k
{
917
15.3k
  ppm_source_ptr source;
918
919
#if BITS_IN_JSAMPLE == 8
920
15.3k
  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
15.3k
  source = (ppm_source_ptr)
929
15.3k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
15.3k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
15.3k
  source->pub.start_input = start_input_ppm;
933
15.3k
  source->pub.finish_input = finish_input_ppm;
934
15.3k
  source->pub.max_pixels = 0;
935
936
15.3k
  return (cjpeg_source_ptr)source;
937
15.3k
}
jinit_read_ppm
Line
Count
Source
916
15.3k
{
917
15.3k
  ppm_source_ptr source;
918
919
15.3k
#if BITS_IN_JSAMPLE == 8
920
15.3k
  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
15.3k
  source = (ppm_source_ptr)
929
15.3k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
930
15.3k
                                sizeof(ppm_source_struct));
931
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
932
15.3k
  source->pub.start_input = start_input_ppm;
933
15.3k
  source->pub.finish_input = finish_input_ppm;
934
15.3k
  source->pub.max_pixels = 0;
935
936
15.3k
  return (cjpeg_source_ptr)source;
937
15.3k
}
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)) */