Coverage Report

Created: 2025-07-18 07:07

/src/libjpeg-turbo.main/src/rdppm.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * rdppm.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * Modified 2009 by Bill Allombert, Guido Vollbeding.
7
 * libjpeg-turbo Modifications:
8
 * Copyright (C) 2015-2017, 2020-2024, D. R. Commander.
9
 * For conditions of distribution and use, see the accompanying README.ijg
10
 * file.
11
 *
12
 * This file contains routines to read input images in PPM/PGM format.
13
 * The extended 2-byte-per-sample raw PPM/PGM formats are supported.
14
 * The PBMPLUS library is NOT required to compile this software
15
 * (but it is highly useful as a set of PPM image manipulation programs).
16
 *
17
 * These routines may need modification for non-Unix environments or
18
 * specialized applications.  As they stand, they assume input from
19
 * an ordinary stdio stream.  They further assume that reading begins
20
 * at the start of the file; start_input may need work if the
21
 * user interface has already read some data (e.g., to determine that
22
 * the file is indeed PPM format).
23
 */
24
25
#include "cmyk.h"
26
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
27
28
#if defined(PPM_SUPPORTED) && \
29
    (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED))
30
31
32
/* Portions of this code are based on the PBMPLUS library, which is:
33
**
34
** Copyright (C) 1988 by Jef Poskanzer.
35
**
36
** Permission to use, copy, modify, and distribute this software and its
37
** documentation for any purpose and without fee is hereby granted, provided
38
** that the above copyright notice appear in all copies and that both that
39
** copyright notice and this permission notice appear in supporting
40
** documentation.  This software is provided "as is" without express or
41
** implied warranty.
42
*/
43
44
45
/* Macros to deal with unsigned chars as efficiently as compiler allows */
46
47
typedef unsigned char U_CHAR;
48
14.0M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
31.0M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
53
54
static int alpha_index[JPEG_NUMCS] = {
55
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
56
};
57
58
59
/* Private version of data source object */
60
61
typedef struct {
62
  struct cjpeg_source_struct pub; /* public fields */
63
64
  /* Usually these two pointers point to the same place: */
65
  U_CHAR *iobuffer;             /* fread's I/O buffer */
66
  _JSAMPROW pixrow;             /* compressor input buffer */
67
  size_t buffer_width;          /* width of I/O buffer */
68
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
69
  unsigned int maxval;
70
} ppm_source_struct;
71
72
typedef ppm_source_struct *ppm_source_ptr;
73
74
75
LOCAL(int)
76
pbm_getc(FILE *infile)
77
/* Read next char, skipping over any comments */
78
/* A comment/newline sequence is returned as a newline */
79
261k
{
80
261k
  register int ch;
81
82
261k
  ch = getc(infile);
83
261k
  if (ch == '#') {
84
14.6k
    do {
85
14.6k
      ch = getc(infile);
86
14.6k
    } while (ch != '\n' && ch != EOF);
87
3.57k
  }
88
261k
  return ch;
89
261k
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
261k
{
80
261k
  register int ch;
81
82
261k
  ch = getc(infile);
83
261k
  if (ch == '#') {
84
14.6k
    do {
85
14.6k
      ch = getc(infile);
86
14.6k
    } while (ch != '\n' && ch != EOF);
87
3.57k
  }
88
261k
  return ch;
89
261k
}
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
109k
{
99
109k
  register int ch;
100
109k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
115k
  do {
104
115k
    ch = pbm_getc(infile);
105
115k
    if (ch == EOF)
106
2.03k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
115k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
109k
  if (ch < '0' || ch > '9')
110
165
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
109k
  val = ch - '0';
113
148k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
39.1k
    val *= 10;
115
39.1k
    val += ch - '0';
116
39.1k
    if (val > maxval)
117
108
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
39.1k
  }
119
120
109k
  return val;
121
109k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
109k
{
99
109k
  register int ch;
100
109k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
115k
  do {
104
115k
    ch = pbm_getc(infile);
105
115k
    if (ch == EOF)
106
2.03k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
115k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
109k
  if (ch < '0' || ch > '9')
110
165
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
109k
  val = ch - '0';
113
148k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
39.1k
    val *= 10;
115
39.1k
    val += ch - '0';
116
39.1k
    if (val > maxval)
117
108
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
39.1k
  }
119
120
109k
  return val;
121
109k
}
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.01k
{
140
2.01k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.01k
  FILE *infile = source->pub.input_file;
142
2.01k
  register _JSAMPROW ptr;
143
2.01k
  register _JSAMPLE *rescale = source->rescale;
144
2.01k
  JDIMENSION col;
145
2.01k
  unsigned int maxval = source->maxval;
146
147
2.01k
  ptr = source->pub._buffer[0];
148
5.43k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.42k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.42k
  }
151
2.01k
  return 1;
152
2.01k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
2.01k
{
140
2.01k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.01k
  FILE *infile = source->pub.input_file;
142
2.01k
  register _JSAMPROW ptr;
143
2.01k
  register _JSAMPLE *rescale = source->rescale;
144
2.01k
  JDIMENSION col;
145
2.01k
  unsigned int maxval = source->maxval;
146
147
2.01k
  ptr = source->pub._buffer[0];
148
5.43k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.42k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.42k
  }
151
2.01k
  return 1;
152
2.01k
}
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
25.7M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
102M
  for (col = cinfo->image_width; col > 0; col--) { \
157
77.1M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
77.1M
    alpha_set_op \
159
77.1M
    ptr += ps; \
160
77.1M
  } \
161
25.7M
}
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.0k
{
168
10.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.0k
  FILE *infile = source->pub.input_file;
170
10.0k
  register _JSAMPROW ptr;
171
10.0k
  register _JSAMPLE *rescale = source->rescale;
172
10.0k
  JDIMENSION col;
173
10.0k
  unsigned int maxval = source->maxval;
174
10.0k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.0k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.0k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.0k
  ptr = source->pub._buffer[0];
181
10.0k
  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.42k
  } else {
188
7.42k
    if (aindex >= 0)
189
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.42k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.42k
    else
192
7.42k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
7.42k
  }
194
10.0k
  return 1;
195
10.0k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
10.0k
{
168
10.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.0k
  FILE *infile = source->pub.input_file;
170
10.0k
  register _JSAMPROW ptr;
171
10.0k
  register _JSAMPLE *rescale = source->rescale;
172
10.0k
  JDIMENSION col;
173
10.0k
  unsigned int maxval = source->maxval;
174
10.0k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.0k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.0k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.0k
  ptr = source->pub._buffer[0];
181
10.0k
  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.42k
  } else {
188
7.42k
    if (aindex >= 0)
189
0
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.42k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.42k
    else
192
7.42k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
7.42k
  }
194
10.0k
  return 1;
195
10.0k
}
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(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
0
      ptr += 4;
222
0
    }
223
0
  }
224
0
  return 1;
225
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
226
227
228
85.2k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
1.16M
  for (col = cinfo->image_width; col > 0; col--) { \
230
1.07M
    ptr[rindex] = read_op; \
231
1.07M
    ptr[gindex] = read_op; \
232
1.07M
    ptr[bindex] = read_op; \
233
1.07M
    alpha_set_op \
234
1.07M
    ptr += ps; \
235
1.07M
  } \
236
85.2k
}
237
238
METHODDEF(JDIMENSION)
239
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
240
/* This version is for reading text-format PPM files with any maxval */
241
9.45k
{
242
9.45k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
9.45k
  FILE *infile = source->pub.input_file;
244
9.45k
  register _JSAMPROW ptr;
245
9.45k
  register _JSAMPLE *rescale = source->rescale;
246
9.45k
  JDIMENSION col;
247
9.45k
  unsigned int maxval = source->maxval;
248
9.45k
  register int rindex = rgb_red[cinfo->in_color_space];
249
9.45k
  register int gindex = rgb_green[cinfo->in_color_space];
250
9.45k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
9.45k
  register int aindex = alpha_index[cinfo->in_color_space];
252
9.45k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
9.45k
  ptr = source->pub._buffer[0];
255
9.45k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
3.00k
    if (aindex >= 0)
257
0
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
3.00k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
3.00k
    else
260
3.00k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
6.45k
  } else {
262
6.45k
    if (aindex >= 0)
263
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
6.45k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
6.45k
    else
266
6.45k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
6.45k
  }
268
9.45k
  return 1;
269
9.45k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
241
9.45k
{
242
9.45k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
9.45k
  FILE *infile = source->pub.input_file;
244
9.45k
  register _JSAMPROW ptr;
245
9.45k
  register _JSAMPLE *rescale = source->rescale;
246
9.45k
  JDIMENSION col;
247
9.45k
  unsigned int maxval = source->maxval;
248
9.45k
  register int rindex = rgb_red[cinfo->in_color_space];
249
9.45k
  register int gindex = rgb_green[cinfo->in_color_space];
250
9.45k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
9.45k
  register int aindex = alpha_index[cinfo->in_color_space];
252
9.45k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
9.45k
  ptr = source->pub._buffer[0];
255
9.45k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
3.00k
    if (aindex >= 0)
257
0
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
3.00k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
3.00k
    else
260
3.00k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
6.45k
  } else {
262
6.45k
    if (aindex >= 0)
263
0
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
6.45k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
6.45k
    else
266
6.45k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
6.45k
  }
268
9.45k
  return 1;
269
9.45k
}
Unexecuted instantiation: rdppm-12.c:get_text_rgb_row
Unexecuted instantiation: rdppm-16.c:get_text_rgb_row
270
271
272
METHODDEF(JDIMENSION)
273
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
274
/* This version is for reading text-format PPM files with any maxval and
275
   converting to CMYK */
276
0
{
277
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
0
  FILE *infile = source->pub.input_file;
279
0
  register _JSAMPROW ptr;
280
0
  register _JSAMPLE *rescale = source->rescale;
281
0
  JDIMENSION col;
282
0
  unsigned int maxval = source->maxval;
283
284
0
  ptr = source->pub._buffer[0];
285
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
0
    for (col = cinfo->image_width; col > 0; col--) {
287
0
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
0
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
0
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
0
      ptr += 4;
292
0
    }
293
0
  } else {
294
0
    for (col = cinfo->image_width; col > 0; col--) {
295
0
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
0
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
0
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
0
      ptr += 4;
300
0
    }
301
0
  }
302
0
  return 1;
303
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
304
305
306
METHODDEF(JDIMENSION)
307
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
308
/* This version is for reading raw-byte-format PGM files with any maxval */
309
3.47M
{
310
3.47M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
3.47M
  register _JSAMPROW ptr;
312
3.47M
  register U_CHAR *bufferptr;
313
3.47M
  register _JSAMPLE *rescale = source->rescale;
314
3.47M
  JDIMENSION col;
315
316
3.47M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
3.47M
  ptr = source->pub._buffer[0];
319
3.47M
  bufferptr = source->iobuffer;
320
17.1M
  for (col = cinfo->image_width; col > 0; col--) {
321
13.6M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
13.6M
  }
323
3.47M
  return 1;
324
3.47M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
309
3.47M
{
310
3.47M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
3.47M
  register _JSAMPROW ptr;
312
3.47M
  register U_CHAR *bufferptr;
313
3.47M
  register _JSAMPLE *rescale = source->rescale;
314
3.47M
  JDIMENSION col;
315
316
3.47M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
3.47M
  ptr = source->pub._buffer[0];
319
3.47M
  bufferptr = source->iobuffer;
320
17.1M
  for (col = cinfo->image_width; col > 0; col--) {
321
13.6M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
13.6M
  }
323
3.47M
  return 1;
324
3.47M
}
Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row
Unexecuted instantiation: rdppm-16.c:get_scaled_gray_row
325
326
327
METHODDEF(JDIMENSION)
328
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
329
/* This version is for reading raw-byte-format PGM files with any maxval
330
   and converting to extended RGB */
331
25.7M
{
332
25.7M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
25.7M
  register _JSAMPROW ptr;
334
25.7M
  register U_CHAR *bufferptr;
335
25.7M
  register _JSAMPLE *rescale = source->rescale;
336
25.7M
  JDIMENSION col;
337
25.7M
  unsigned int maxval = source->maxval;
338
25.7M
  register int rindex = rgb_red[cinfo->in_color_space];
339
25.7M
  register int gindex = rgb_green[cinfo->in_color_space];
340
25.7M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
25.7M
  register int aindex = alpha_index[cinfo->in_color_space];
342
25.7M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
25.7M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
440
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
25.7M
  ptr = source->pub._buffer[0];
347
25.7M
  bufferptr = source->iobuffer;
348
25.7M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
8.34M
    if (aindex >= 0)
350
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
8.34M
    else
352
8.34M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
17.3M
  } else {
354
17.3M
    if (aindex >= 0)
355
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
17.3M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
17.3M
    else
358
17.3M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
17.3M
  }
360
25.7M
  return 1;
361
25.7M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
331
25.7M
{
332
25.7M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
25.7M
  register _JSAMPROW ptr;
334
25.7M
  register U_CHAR *bufferptr;
335
25.7M
  register _JSAMPLE *rescale = source->rescale;
336
25.7M
  JDIMENSION col;
337
25.7M
  unsigned int maxval = source->maxval;
338
25.7M
  register int rindex = rgb_red[cinfo->in_color_space];
339
25.7M
  register int gindex = rgb_green[cinfo->in_color_space];
340
25.7M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
25.7M
  register int aindex = alpha_index[cinfo->in_color_space];
342
25.7M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
25.7M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
440
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
25.7M
  ptr = source->pub._buffer[0];
347
25.7M
  bufferptr = source->iobuffer;
348
25.7M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
8.34M
    if (aindex >= 0)
350
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
8.34M
    else
352
8.34M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
17.3M
  } else {
354
17.3M
    if (aindex >= 0)
355
0
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
17.3M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
17.3M
    else
358
17.3M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
17.3M
  }
360
25.7M
  return 1;
361
25.7M
}
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_gray_rgb_row
362
363
364
METHODDEF(JDIMENSION)
365
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
366
/* This version is for reading raw-byte-format PGM files with any maxval
367
   and converting to CMYK */
368
0
{
369
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
0
  register _JSAMPROW ptr;
371
0
  register U_CHAR *bufferptr;
372
0
  register _JSAMPLE *rescale = source->rescale;
373
0
  JDIMENSION col;
374
0
  unsigned int maxval = source->maxval;
375
376
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
0
  ptr = source->pub._buffer[0];
379
0
  bufferptr = source->iobuffer;
380
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
0
    for (col = cinfo->image_width; col > 0; col--) {
382
0
      _JSAMPLE gray = *bufferptr++;
383
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
0
      ptr += 4;
385
0
    }
386
0
  } else {
387
0
    for (col = cinfo->image_width; col > 0; col--) {
388
0
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
0
      ptr += 4;
391
0
    }
392
0
  }
393
0
  return 1;
394
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
395
396
397
METHODDEF(JDIMENSION)
398
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
399
/* This version is for reading raw-byte-format PPM files with any maxval */
400
75.8k
{
401
75.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
75.8k
  register _JSAMPROW ptr;
403
75.8k
  register U_CHAR *bufferptr;
404
75.8k
  register _JSAMPLE *rescale = source->rescale;
405
75.8k
  JDIMENSION col;
406
75.8k
  unsigned int maxval = source->maxval;
407
75.8k
  register int rindex = rgb_red[cinfo->in_color_space];
408
75.8k
  register int gindex = rgb_green[cinfo->in_color_space];
409
75.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
75.8k
  register int aindex = alpha_index[cinfo->in_color_space];
411
75.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
75.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
396
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
75.8k
  ptr = source->pub._buffer[0];
416
75.8k
  bufferptr = source->iobuffer;
417
75.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
12.6k
    if (aindex >= 0)
419
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
12.6k
    else
421
12.6k
      RGB_READ_LOOP(*bufferptr++, {})
422
63.2k
  } else {
423
63.2k
    if (aindex >= 0)
424
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
63.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
63.2k
    else
427
63.2k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
63.2k
  }
429
75.8k
  return 1;
430
75.8k
}
rdppm-8.c:get_rgb_row
Line
Count
Source
400
75.8k
{
401
75.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
75.8k
  register _JSAMPROW ptr;
403
75.8k
  register U_CHAR *bufferptr;
404
75.8k
  register _JSAMPLE *rescale = source->rescale;
405
75.8k
  JDIMENSION col;
406
75.8k
  unsigned int maxval = source->maxval;
407
75.8k
  register int rindex = rgb_red[cinfo->in_color_space];
408
75.8k
  register int gindex = rgb_green[cinfo->in_color_space];
409
75.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
75.8k
  register int aindex = alpha_index[cinfo->in_color_space];
411
75.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
75.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
396
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
75.8k
  ptr = source->pub._buffer[0];
416
75.8k
  bufferptr = source->iobuffer;
417
75.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
12.6k
    if (aindex >= 0)
419
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
12.6k
    else
421
12.6k
      RGB_READ_LOOP(*bufferptr++, {})
422
63.2k
  } else {
423
63.2k
    if (aindex >= 0)
424
0
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
63.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
63.2k
    else
427
63.2k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
63.2k
  }
429
75.8k
  return 1;
430
75.8k
}
Unexecuted instantiation: rdppm-12.c:get_rgb_row
Unexecuted instantiation: rdppm-16.c:get_rgb_row
431
432
433
METHODDEF(JDIMENSION)
434
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
435
/* This version is for reading raw-byte-format PPM files with any maxval and
436
   converting to CMYK */
437
0
{
438
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
0
  register _JSAMPROW ptr;
440
0
  register U_CHAR *bufferptr;
441
0
  register _JSAMPLE *rescale = source->rescale;
442
0
  JDIMENSION col;
443
0
  unsigned int maxval = source->maxval;
444
445
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
0
  ptr = source->pub._buffer[0];
448
0
  bufferptr = source->iobuffer;
449
0
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
0
    for (col = cinfo->image_width; col > 0; col--) {
451
0
      _JSAMPLE r = *bufferptr++;
452
0
      _JSAMPLE g = *bufferptr++;
453
0
      _JSAMPLE b = *bufferptr++;
454
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
0
      ptr += 4;
456
0
    }
457
0
  } else {
458
0
    for (col = cinfo->image_width; col > 0; col--) {
459
0
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
0
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
0
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
0
      ptr += 4;
464
0
    }
465
0
  }
466
0
  return 1;
467
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
468
469
470
METHODDEF(JDIMENSION)
471
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
472
/* This version is for reading raw-byte-format files with
473
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U.
474
 * In this case we just read right into the _JSAMPLE buffer!
475
 * Note that same code works for PPM and PGM files.
476
 */
477
1.67M
{
478
1.67M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.67M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
50
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.67M
  return 1;
483
1.67M
}
rdppm-8.c:get_raw_row
Line
Count
Source
477
1.67M
{
478
1.67M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.67M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
50
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.67M
  return 1;
483
1.67M
}
Unexecuted instantiation: rdppm-12.c:get_raw_row
Unexecuted instantiation: rdppm-16.c:get_raw_row
484
485
486
METHODDEF(JDIMENSION)
487
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
488
/* This version is for reading raw-word-format PGM files with any maxval */
489
12.6k
{
490
12.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
12.6k
  register _JSAMPROW ptr;
492
12.6k
  register U_CHAR *bufferptr;
493
12.6k
  register _JSAMPLE *rescale = source->rescale;
494
12.6k
  JDIMENSION col;
495
12.6k
  unsigned int maxval = source->maxval;
496
497
12.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
52
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
12.6k
  ptr = source->pub._buffer[0];
500
12.6k
  bufferptr = source->iobuffer;
501
37.3k
  for (col = cinfo->image_width; col > 0; col--) {
502
24.6k
    register unsigned int temp;
503
24.6k
    temp  = UCH(*bufferptr++) << 8;
504
24.6k
    temp |= UCH(*bufferptr++);
505
24.6k
    if (temp > maxval)
506
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
24.6k
    *ptr++ = rescale[temp];
508
24.6k
  }
509
12.6k
  return 1;
510
12.6k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
489
12.6k
{
490
12.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
12.6k
  register _JSAMPROW ptr;
492
12.6k
  register U_CHAR *bufferptr;
493
12.6k
  register _JSAMPLE *rescale = source->rescale;
494
12.6k
  JDIMENSION col;
495
12.6k
  unsigned int maxval = source->maxval;
496
497
12.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
52
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
12.6k
  ptr = source->pub._buffer[0];
500
12.6k
  bufferptr = source->iobuffer;
501
37.3k
  for (col = cinfo->image_width; col > 0; col--) {
502
24.6k
    register unsigned int temp;
503
24.6k
    temp  = UCH(*bufferptr++) << 8;
504
24.6k
    temp |= UCH(*bufferptr++);
505
24.6k
    if (temp > maxval)
506
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
24.6k
    *ptr++ = rescale[temp];
508
24.6k
  }
509
12.6k
  return 1;
510
12.6k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_row
511
512
513
METHODDEF(JDIMENSION)
514
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
515
/* This version is for reading raw-word-format PGM files with any maxval */
516
63.2k
{
517
63.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
63.2k
  register _JSAMPROW ptr;
519
63.2k
  register U_CHAR *bufferptr;
520
63.2k
  register _JSAMPLE *rescale = source->rescale;
521
63.2k
  JDIMENSION col;
522
63.2k
  unsigned int maxval = source->maxval;
523
63.2k
  register int rindex = rgb_red[cinfo->in_color_space];
524
63.2k
  register int gindex = rgb_green[cinfo->in_color_space];
525
63.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
63.2k
  register int aindex = alpha_index[cinfo->in_color_space];
527
63.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
63.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
260
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
63.2k
  ptr = source->pub._buffer[0];
532
63.2k
  bufferptr = source->iobuffer;
533
186k
  for (col = cinfo->image_width; col > 0; col--) {
534
123k
    register unsigned int temp;
535
123k
    temp  = UCH(*bufferptr++) << 8;
536
123k
    temp |= UCH(*bufferptr++);
537
123k
    if (temp > maxval)
538
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
123k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
123k
    if (aindex >= 0)
541
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
123k
    ptr += ps;
543
123k
  }
544
63.2k
  return 1;
545
63.2k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
516
63.2k
{
517
63.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
63.2k
  register _JSAMPROW ptr;
519
63.2k
  register U_CHAR *bufferptr;
520
63.2k
  register _JSAMPLE *rescale = source->rescale;
521
63.2k
  JDIMENSION col;
522
63.2k
  unsigned int maxval = source->maxval;
523
63.2k
  register int rindex = rgb_red[cinfo->in_color_space];
524
63.2k
  register int gindex = rgb_green[cinfo->in_color_space];
525
63.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
63.2k
  register int aindex = alpha_index[cinfo->in_color_space];
527
63.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
63.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
260
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
63.2k
  ptr = source->pub._buffer[0];
532
63.2k
  bufferptr = source->iobuffer;
533
186k
  for (col = cinfo->image_width; col > 0; col--) {
534
123k
    register unsigned int temp;
535
123k
    temp  = UCH(*bufferptr++) << 8;
536
123k
    temp |= UCH(*bufferptr++);
537
123k
    if (temp > maxval)
538
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
123k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
123k
    if (aindex >= 0)
541
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
123k
    ptr += ps;
543
123k
  }
544
63.2k
  return 1;
545
63.2k
}
Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_gray_rgb_row
546
547
548
METHODDEF(JDIMENSION)
549
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
550
/* This version is for reading raw-word-format PGM files with any maxval */
551
0
{
552
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
0
  register _JSAMPROW ptr;
554
0
  register U_CHAR *bufferptr;
555
0
  register _JSAMPLE *rescale = source->rescale;
556
0
  JDIMENSION col;
557
0
  unsigned int maxval = source->maxval;
558
559
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
0
  ptr = source->pub._buffer[0];
562
0
  bufferptr = source->iobuffer;
563
0
  for (col = cinfo->image_width; col > 0; col--) {
564
0
    register unsigned int gray;
565
0
    gray  = UCH(*bufferptr++) << 8;
566
0
    gray |= UCH(*bufferptr++);
567
0
    if (gray > maxval)
568
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
0
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
0
                ptr + 1, ptr + 2, ptr + 3);
571
0
    ptr += 4;
572
0
  }
573
0
  return 1;
574
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
575
576
577
METHODDEF(JDIMENSION)
578
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
579
/* This version is for reading raw-word-format PPM files with any maxval */
580
8.30k
{
581
8.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
8.30k
  register _JSAMPROW ptr;
583
8.30k
  register U_CHAR *bufferptr;
584
8.30k
  register _JSAMPLE *rescale = source->rescale;
585
8.30k
  JDIMENSION col;
586
8.30k
  unsigned int maxval = source->maxval;
587
8.30k
  register int rindex = rgb_red[cinfo->in_color_space];
588
8.30k
  register int gindex = rgb_green[cinfo->in_color_space];
589
8.30k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
8.30k
  register int aindex = alpha_index[cinfo->in_color_space];
591
8.30k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
8.30k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
200
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
8.30k
  ptr = source->pub._buffer[0];
596
8.30k
  bufferptr = source->iobuffer;
597
23.3k
  for (col = cinfo->image_width; col > 0; col--) {
598
15.0k
    register unsigned int temp;
599
15.0k
    temp  = UCH(*bufferptr++) << 8;
600
15.0k
    temp |= UCH(*bufferptr++);
601
15.0k
    if (temp > maxval)
602
155
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
15.0k
    ptr[rindex] = rescale[temp];
604
15.0k
    temp  = UCH(*bufferptr++) << 8;
605
15.0k
    temp |= UCH(*bufferptr++);
606
15.0k
    if (temp > maxval)
607
100
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
15.0k
    ptr[gindex] = rescale[temp];
609
15.0k
    temp  = UCH(*bufferptr++) << 8;
610
15.0k
    temp |= UCH(*bufferptr++);
611
15.0k
    if (temp > maxval)
612
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
15.0k
    ptr[bindex] = rescale[temp];
614
15.0k
    if (aindex >= 0)
615
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
15.0k
    ptr += ps;
617
15.0k
  }
618
8.30k
  return 1;
619
8.30k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
580
8.30k
{
581
8.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
8.30k
  register _JSAMPROW ptr;
583
8.30k
  register U_CHAR *bufferptr;
584
8.30k
  register _JSAMPLE *rescale = source->rescale;
585
8.30k
  JDIMENSION col;
586
8.30k
  unsigned int maxval = source->maxval;
587
8.30k
  register int rindex = rgb_red[cinfo->in_color_space];
588
8.30k
  register int gindex = rgb_green[cinfo->in_color_space];
589
8.30k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
8.30k
  register int aindex = alpha_index[cinfo->in_color_space];
591
8.30k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
8.30k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
200
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
8.30k
  ptr = source->pub._buffer[0];
596
8.30k
  bufferptr = source->iobuffer;
597
23.3k
  for (col = cinfo->image_width; col > 0; col--) {
598
15.0k
    register unsigned int temp;
599
15.0k
    temp  = UCH(*bufferptr++) << 8;
600
15.0k
    temp |= UCH(*bufferptr++);
601
15.0k
    if (temp > maxval)
602
155
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
15.0k
    ptr[rindex] = rescale[temp];
604
15.0k
    temp  = UCH(*bufferptr++) << 8;
605
15.0k
    temp |= UCH(*bufferptr++);
606
15.0k
    if (temp > maxval)
607
100
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
15.0k
    ptr[gindex] = rescale[temp];
609
15.0k
    temp  = UCH(*bufferptr++) << 8;
610
15.0k
    temp |= UCH(*bufferptr++);
611
15.0k
    if (temp > maxval)
612
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
15.0k
    ptr[bindex] = rescale[temp];
614
15.0k
    if (aindex >= 0)
615
0
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
15.0k
    ptr += ps;
617
15.0k
  }
618
8.30k
  return 1;
619
8.30k
}
Unexecuted instantiation: rdppm-12.c:get_word_rgb_row
Unexecuted instantiation: rdppm-16.c:get_word_rgb_row
620
621
622
METHODDEF(JDIMENSION)
623
get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
624
/* This version is for reading raw-word-format PPM files with any maxval */
625
0
{
626
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
0
  register _JSAMPROW ptr;
628
0
  register U_CHAR *bufferptr;
629
0
  register _JSAMPLE *rescale = source->rescale;
630
0
  JDIMENSION col;
631
0
  unsigned int maxval = source->maxval;
632
633
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
0
  ptr = source->pub._buffer[0];
636
0
  bufferptr = source->iobuffer;
637
0
  for (col = cinfo->image_width; col > 0; col--) {
638
0
    register unsigned int r, g, b;
639
0
    r  = UCH(*bufferptr++) << 8;
640
0
    r |= UCH(*bufferptr++);
641
0
    if (r > maxval)
642
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
0
    g  = UCH(*bufferptr++) << 8;
644
0
    g |= UCH(*bufferptr++);
645
0
    if (g > maxval)
646
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
0
    b  = UCH(*bufferptr++) << 8;
648
0
    b |= UCH(*bufferptr++);
649
0
    if (b > maxval)
650
0
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
0
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
0
                ptr + 2, ptr + 3);
653
0
    ptr += 4;
654
0
  }
655
0
  return 1;
656
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
657
658
659
/*
660
 * Read the file header; return image size and component count.
661
 */
662
663
METHODDEF(void)
664
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
665
15.6k
{
666
15.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
15.6k
  int c;
668
15.6k
  unsigned int w, h, maxval;
669
15.6k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
15.6k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
15.6k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
15.6k
  switch (c) {
678
1.13k
  case '2':                     /* it's a text-format PGM file */
679
2.22k
  case '3':                     /* it's a text-format PPM file */
680
13.2k
  case '5':                     /* it's a raw-format PGM file */
681
15.6k
  case '6':                     /* it's a raw-format PPM file */
682
15.6k
    break;
683
6
  default:
684
6
    ERREXIT(cinfo, JERR_PPM_NOT);
685
6
    break;
686
15.6k
  }
687
688
  /* fetch the remaining header info */
689
15.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
15.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
15.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
15.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
24
    ERREXIT(cinfo, JERR_PPM_NOT);
695
15.6k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
204
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
15.6k
  cinfo->image_width = (JDIMENSION)w;
699
15.6k
  cinfo->image_height = (JDIMENSION)h;
700
15.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
15.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
15.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
15.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
15.6k
  switch (c) {
708
828
  case '2':                     /* it's a text-format PGM file */
709
828
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
828
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
828
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
828
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
138
      source->pub.get_pixel_rows = get_text_gray_row;
715
690
    else if (IsExtRGB(cinfo->in_color_space))
716
690
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
0
    else if (cinfo->in_color_space == JCS_CMYK)
718
0
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
828
    need_iobuffer = FALSE;
722
828
    break;
723
724
732
  case '3':                     /* it's a text-format PPM file */
725
732
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
732
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
732
    if (IsExtRGB(cinfo->in_color_space))
729
610
      source->pub.get_pixel_rows = get_text_rgb_row;
730
122
    else if (cinfo->in_color_space == JCS_CMYK)
731
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
122
    else
733
122
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
732
    need_iobuffer = FALSE;
735
732
    break;
736
737
10.6k
  case '5':                     /* it's a raw-format PGM file */
738
10.6k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
10.6k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
10.6k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
10.6k
    if (maxval > 255) {
743
498
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
83
        source->pub.get_pixel_rows = get_word_gray_row;
745
415
      else if (IsExtRGB(cinfo->in_color_space))
746
415
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
0
      else if (cinfo->in_color_space == JCS_CMYK)
748
0
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
10.1k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
10.1k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
10.1k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
121
      source->pub.get_pixel_rows = get_raw_row;
755
121
      use_raw_buffer = TRUE;
756
121
      need_rescale = FALSE;
757
10.0k
    } else {
758
10.0k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
1.56k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
8.43k
      else if (IsExtRGB(cinfo->in_color_space))
761
8.43k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
0
      else if (cinfo->in_color_space == JCS_CMYK)
763
0
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
10.0k
    }
767
10.6k
    break;
768
769
2.29k
  case '6':                     /* it's a raw-format PPM file */
770
2.29k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.29k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.29k
    if (maxval > 255) {
774
714
      if (IsExtRGB(cinfo->in_color_space))
775
595
        source->pub.get_pixel_rows = get_word_rgb_row;
776
119
      else if (cinfo->in_color_space == JCS_CMYK)
777
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
119
      else
779
119
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.57k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
1.57k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.57k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.57k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
258
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
43
      source->pub.get_pixel_rows = get_raw_row;
789
43
      use_raw_buffer = TRUE;
790
43
      need_rescale = FALSE;
791
1.53k
    } else {
792
1.53k
      if (IsExtRGB(cinfo->in_color_space))
793
1.27k
        source->pub.get_pixel_rows = get_rgb_row;
794
263
      else if (cinfo->in_color_space == JCS_CMYK)
795
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
263
      else
797
263
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.53k
    }
799
2.29k
    break;
800
15.6k
  }
801
802
13.9k
  if (IsExtRGB(cinfo->in_color_space))
803
12.0k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
1.90k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
1.90k
    cinfo->input_components = 1;
806
0
  else if (cinfo->in_color_space == JCS_CMYK)
807
0
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
13.9k
  if (need_iobuffer) {
811
12.5k
    if (c == '6')
812
1.91k
      source->buffer_width = (size_t)w * 3 *
813
1.91k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
10.6k
    else
815
10.6k
      source->buffer_width = (size_t)w *
816
10.6k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
12.5k
    source->iobuffer = (U_CHAR *)
818
12.5k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
12.5k
                                  source->buffer_width);
820
12.5k
  }
821
822
  /* Create compressor input buffer. */
823
13.9k
  if (use_raw_buffer) {
824
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
825
    /* Synthesize a _JSAMPARRAY pointer structure */
826
164
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
164
    source->pub._buffer = &source->pixrow;
828
164
    source->pub.buffer_height = 1;
829
13.8k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
13.8k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
13.8k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
13.8k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
13.8k
    source->pub.buffer_height = 1;
835
13.8k
  }
836
837
  /* Compute the rescaling array if required. */
838
13.9k
  if (need_rescale) {
839
13.8k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
13.8k
    source->rescale = (_JSAMPLE *)
843
13.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
13.8k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
13.8k
                                           sizeof(_JSAMPLE)));
846
13.8k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
13.8k
                                        sizeof(_JSAMPLE)));
848
13.8k
    half_maxval = maxval / 2;
849
15.2M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
15.2M
      source->rescale[val] =
852
15.2M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
15.2M
                   maxval);
854
15.2M
    }
855
13.8k
  }
856
13.9k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
665
15.6k
{
666
15.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
15.6k
  int c;
668
15.6k
  unsigned int w, h, maxval;
669
15.6k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
15.6k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
15.6k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
15.6k
  switch (c) {
678
1.13k
  case '2':                     /* it's a text-format PGM file */
679
2.22k
  case '3':                     /* it's a text-format PPM file */
680
13.2k
  case '5':                     /* it's a raw-format PGM file */
681
15.6k
  case '6':                     /* it's a raw-format PPM file */
682
15.6k
    break;
683
6
  default:
684
6
    ERREXIT(cinfo, JERR_PPM_NOT);
685
6
    break;
686
15.6k
  }
687
688
  /* fetch the remaining header info */
689
15.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
15.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
15.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
15.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
24
    ERREXIT(cinfo, JERR_PPM_NOT);
695
15.6k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
204
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
15.6k
  cinfo->image_width = (JDIMENSION)w;
699
15.6k
  cinfo->image_height = (JDIMENSION)h;
700
15.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
15.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
15.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
15.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
15.6k
  switch (c) {
708
828
  case '2':                     /* it's a text-format PGM file */
709
828
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
828
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
828
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
828
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
138
      source->pub.get_pixel_rows = get_text_gray_row;
715
690
    else if (IsExtRGB(cinfo->in_color_space))
716
690
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
0
    else if (cinfo->in_color_space == JCS_CMYK)
718
0
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
828
    need_iobuffer = FALSE;
722
828
    break;
723
724
732
  case '3':                     /* it's a text-format PPM file */
725
732
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
732
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
732
    if (IsExtRGB(cinfo->in_color_space))
729
610
      source->pub.get_pixel_rows = get_text_rgb_row;
730
122
    else if (cinfo->in_color_space == JCS_CMYK)
731
0
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
122
    else
733
122
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
732
    need_iobuffer = FALSE;
735
732
    break;
736
737
10.6k
  case '5':                     /* it's a raw-format PGM file */
738
10.6k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
10.6k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
10.6k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
10.6k
    if (maxval > 255) {
743
498
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
83
        source->pub.get_pixel_rows = get_word_gray_row;
745
415
      else if (IsExtRGB(cinfo->in_color_space))
746
415
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
0
      else if (cinfo->in_color_space == JCS_CMYK)
748
0
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
10.1k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
10.1k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
10.1k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
121
      source->pub.get_pixel_rows = get_raw_row;
755
121
      use_raw_buffer = TRUE;
756
121
      need_rescale = FALSE;
757
10.0k
    } else {
758
10.0k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
1.56k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
8.43k
      else if (IsExtRGB(cinfo->in_color_space))
761
8.43k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
0
      else if (cinfo->in_color_space == JCS_CMYK)
763
0
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
10.0k
    }
767
10.6k
    break;
768
769
2.29k
  case '6':                     /* it's a raw-format PPM file */
770
2.29k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.29k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.29k
    if (maxval > 255) {
774
714
      if (IsExtRGB(cinfo->in_color_space))
775
595
        source->pub.get_pixel_rows = get_word_rgb_row;
776
119
      else if (cinfo->in_color_space == JCS_CMYK)
777
0
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
119
      else
779
119
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.57k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
1.57k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.57k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.57k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
258
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
43
      source->pub.get_pixel_rows = get_raw_row;
789
43
      use_raw_buffer = TRUE;
790
43
      need_rescale = FALSE;
791
1.53k
    } else {
792
1.53k
      if (IsExtRGB(cinfo->in_color_space))
793
1.27k
        source->pub.get_pixel_rows = get_rgb_row;
794
263
      else if (cinfo->in_color_space == JCS_CMYK)
795
0
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
263
      else
797
263
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.53k
    }
799
2.29k
    break;
800
15.6k
  }
801
802
13.9k
  if (IsExtRGB(cinfo->in_color_space))
803
12.0k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
1.90k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
1.90k
    cinfo->input_components = 1;
806
0
  else if (cinfo->in_color_space == JCS_CMYK)
807
0
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
13.9k
  if (need_iobuffer) {
811
12.5k
    if (c == '6')
812
1.91k
      source->buffer_width = (size_t)w * 3 *
813
1.91k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
10.6k
    else
815
10.6k
      source->buffer_width = (size_t)w *
816
10.6k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
12.5k
    source->iobuffer = (U_CHAR *)
818
12.5k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
12.5k
                                  source->buffer_width);
820
12.5k
  }
821
822
  /* Create compressor input buffer. */
823
13.9k
  if (use_raw_buffer) {
824
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
825
    /* Synthesize a _JSAMPARRAY pointer structure */
826
164
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
164
    source->pub._buffer = &source->pixrow;
828
164
    source->pub.buffer_height = 1;
829
13.8k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
13.8k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
13.8k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
13.8k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
13.8k
    source->pub.buffer_height = 1;
835
13.8k
  }
836
837
  /* Compute the rescaling array if required. */
838
13.9k
  if (need_rescale) {
839
13.8k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
13.8k
    source->rescale = (_JSAMPLE *)
843
13.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
13.8k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
13.8k
                                           sizeof(_JSAMPLE)));
846
13.8k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
13.8k
                                        sizeof(_JSAMPLE)));
848
13.8k
    half_maxval = maxval / 2;
849
15.2M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
15.2M
      source->rescale[val] =
852
15.2M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
15.2M
                   maxval);
854
15.2M
    }
855
13.8k
  }
856
13.9k
}
Unexecuted instantiation: rdppm-12.c:start_input_ppm
Unexecuted instantiation: rdppm-16.c:start_input_ppm
857
858
859
/*
860
 * Finish up at the end of the file.
861
 */
862
863
METHODDEF(void)
864
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
865
10.6k
{
866
  /* no work */
867
10.6k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
865
10.6k
{
866
  /* no work */
867
10.6k
}
Unexecuted instantiation: rdppm-12.c:finish_input_ppm
Unexecuted instantiation: rdppm-16.c:finish_input_ppm
868
869
870
/*
871
 * The module selection routine for PPM format input.
872
 */
873
874
GLOBAL(cjpeg_source_ptr)
875
_jinit_read_ppm(j_compress_ptr cinfo)
876
15.6k
{
877
15.6k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
15.6k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
0
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
15.6k
  source = (ppm_source_ptr)
889
15.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
15.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
15.6k
  source->pub.start_input = start_input_ppm;
893
15.6k
  source->pub.finish_input = finish_input_ppm;
894
15.6k
  source->pub.max_pixels = 0;
895
896
15.6k
  return (cjpeg_source_ptr)source;
897
15.6k
}
jinit_read_ppm
Line
Count
Source
876
15.6k
{
877
15.6k
  ppm_source_ptr source;
878
879
15.6k
#if BITS_IN_JSAMPLE == 8
880
15.6k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
15.6k
  source = (ppm_source_ptr)
889
15.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
15.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
15.6k
  source->pub.start_input = start_input_ppm;
893
15.6k
  source->pub.finish_input = finish_input_ppm;
894
15.6k
  source->pub.max_pixels = 0;
895
896
15.6k
  return (cjpeg_source_ptr)source;
897
15.6k
}
Unexecuted instantiation: j12init_read_ppm
Unexecuted instantiation: j16init_read_ppm
898
899
#endif /* defined(PPM_SUPPORTED) &&
900
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */