Coverage Report

Created: 2025-10-10 07:04

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-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
29.0M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
16.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
297k
{
80
297k
  register int ch;
81
82
297k
  ch = getc(infile);
83
297k
  if (ch == '#') {
84
34.8k
    do {
85
34.8k
      ch = getc(infile);
86
34.8k
    } while (ch != '\n' && ch != EOF);
87
3.89k
  }
88
297k
  return ch;
89
297k
}
Unexecuted instantiation: rdppm-8.c:pbm_getc
Unexecuted instantiation: rdppm-12.c:pbm_getc
rdppm-16.c:pbm_getc
Line
Count
Source
79
297k
{
80
297k
  register int ch;
81
82
297k
  ch = getc(infile);
83
297k
  if (ch == '#') {
84
34.8k
    do {
85
34.8k
      ch = getc(infile);
86
34.8k
    } while (ch != '\n' && ch != EOF);
87
3.89k
  }
88
297k
  return ch;
89
297k
}
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
126k
{
99
126k
  register int ch;
100
126k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
136k
  do {
104
136k
    ch = pbm_getc(infile);
105
136k
    if (ch == EOF)
106
3.01k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
136k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
126k
  if (ch < '0' || ch > '9')
110
299
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
126k
  val = ch - '0';
113
164k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
37.6k
    val *= 10;
115
37.6k
    val += ch - '0';
116
37.6k
    if (val > maxval)
117
152
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
37.6k
  }
119
120
126k
  return val;
121
126k
}
Unexecuted instantiation: rdppm-8.c:read_pbm_integer
Unexecuted instantiation: rdppm-12.c:read_pbm_integer
rdppm-16.c:read_pbm_integer
Line
Count
Source
98
126k
{
99
126k
  register int ch;
100
126k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
136k
  do {
104
136k
    ch = pbm_getc(infile);
105
136k
    if (ch == EOF)
106
3.01k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
136k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
126k
  if (ch < '0' || ch > '9')
110
299
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
126k
  val = ch - '0';
113
164k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
37.6k
    val *= 10;
115
37.6k
    val += ch - '0';
116
37.6k
    if (val > maxval)
117
152
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
37.6k
  }
119
120
126k
  return val;
121
126k
}
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.03k
{
140
2.03k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.03k
  FILE *infile = source->pub.input_file;
142
2.03k
  register _JSAMPROW ptr;
143
2.03k
  register _JSAMPLE *rescale = source->rescale;
144
2.03k
  JDIMENSION col;
145
2.03k
  unsigned int maxval = source->maxval;
146
147
2.03k
  ptr = source->pub._buffer[0];
148
5.29k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.26k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.26k
  }
151
2.03k
  return 1;
152
2.03k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_row
rdppm-16.c:get_text_gray_row
Line
Count
Source
139
2.03k
{
140
2.03k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.03k
  FILE *infile = source->pub.input_file;
142
2.03k
  register _JSAMPROW ptr;
143
2.03k
  register _JSAMPLE *rescale = source->rescale;
144
2.03k
  JDIMENSION col;
145
2.03k
  unsigned int maxval = source->maxval;
146
147
2.03k
  ptr = source->pub._buffer[0];
148
5.29k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.26k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.26k
  }
151
2.03k
  return 1;
152
2.03k
}
153
154
155
11.4M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
71.5M
  for (col = cinfo->image_width; col > 0; col--) { \
157
60.1M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
60.1M
    alpha_set_op \
159
60.1M
    ptr += ps; \
160
60.1M
  } \
161
11.4M
}
162
163
METHODDEF(JDIMENSION)
164
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
165
/* This version is for reading text-format PGM files with any maxval and
166
   converting to extended RGB */
167
10.1k
{
168
10.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.1k
  FILE *infile = source->pub.input_file;
170
10.1k
  register _JSAMPROW ptr;
171
10.1k
  register _JSAMPLE *rescale = source->rescale;
172
10.1k
  JDIMENSION col;
173
10.1k
  unsigned int maxval = source->maxval;
174
10.1k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.1k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.1k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.1k
  ptr = source->pub._buffer[0];
181
10.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.33k
    if (aindex >= 0)
183
223
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.33k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.11k
    else
186
1.11k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.84k
  } else {
188
8.84k
    if (aindex >= 0)
189
1.81k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.84k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.03k
    else
192
7.03k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.84k
  }
194
10.1k
  return 1;
195
10.1k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_rgb_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_rgb_row
rdppm-16.c:get_text_gray_rgb_row
Line
Count
Source
167
10.1k
{
168
10.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
10.1k
  FILE *infile = source->pub.input_file;
170
10.1k
  register _JSAMPROW ptr;
171
10.1k
  register _JSAMPLE *rescale = source->rescale;
172
10.1k
  JDIMENSION col;
173
10.1k
  unsigned int maxval = source->maxval;
174
10.1k
  register int rindex = rgb_red[cinfo->in_color_space];
175
10.1k
  register int gindex = rgb_green[cinfo->in_color_space];
176
10.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
10.1k
  register int aindex = alpha_index[cinfo->in_color_space];
178
10.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
10.1k
  ptr = source->pub._buffer[0];
181
10.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.33k
    if (aindex >= 0)
183
223
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.33k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.11k
    else
186
1.11k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.84k
  } else {
188
8.84k
    if (aindex >= 0)
189
1.81k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.84k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
7.03k
    else
192
7.03k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.84k
  }
194
10.1k
  return 1;
195
10.1k
}
196
197
198
METHODDEF(JDIMENSION)
199
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
200
/* This version is for reading text-format PGM files with any maxval and
201
   converting to CMYK */
202
2.03k
{
203
2.03k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
2.03k
  FILE *infile = source->pub.input_file;
205
2.03k
  register _JSAMPROW ptr;
206
2.03k
  register _JSAMPLE *rescale = source->rescale;
207
2.03k
  JDIMENSION col;
208
2.03k
  unsigned int maxval = source->maxval;
209
210
2.03k
  ptr = source->pub._buffer[0];
211
2.03k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.21k
    for (col = cinfo->image_width; col > 0; col--) {
213
747
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
747
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
747
      ptr += 4;
216
747
    }
217
1.57k
  } else {
218
4.08k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.51k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.51k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
2.51k
      ptr += 4;
222
2.51k
    }
223
1.57k
  }
224
2.03k
  return 1;
225
2.03k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_text_gray_cmyk_row
rdppm-16.c:get_text_gray_cmyk_row
Line
Count
Source
202
2.03k
{
203
2.03k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
2.03k
  FILE *infile = source->pub.input_file;
205
2.03k
  register _JSAMPROW ptr;
206
2.03k
  register _JSAMPLE *rescale = source->rescale;
207
2.03k
  JDIMENSION col;
208
2.03k
  unsigned int maxval = source->maxval;
209
210
2.03k
  ptr = source->pub._buffer[0];
211
2.03k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.21k
    for (col = cinfo->image_width; col > 0; col--) {
213
747
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
747
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
747
      ptr += 4;
216
747
    }
217
1.57k
  } else {
218
4.08k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.51k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.51k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
2.51k
      ptr += 4;
222
2.51k
    }
223
1.57k
  }
224
2.03k
  return 1;
225
2.03k
}
226
227
228
22.6k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
3.14M
  for (col = cinfo->image_width; col > 0; col--) { \
230
3.12M
    ptr[rindex] = read_op; \
231
3.12M
    ptr[gindex] = read_op; \
232
3.12M
    ptr[bindex] = read_op; \
233
3.12M
    alpha_set_op \
234
3.12M
    ptr += ps; \
235
3.12M
  } \
236
22.6k
}
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
10.3k
{
242
10.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
10.3k
  FILE *infile = source->pub.input_file;
244
10.3k
  register _JSAMPROW ptr;
245
10.3k
  register _JSAMPLE *rescale = source->rescale;
246
10.3k
  JDIMENSION col;
247
10.3k
  unsigned int maxval = source->maxval;
248
10.3k
  register int rindex = rgb_red[cinfo->in_color_space];
249
10.3k
  register int gindex = rgb_green[cinfo->in_color_space];
250
10.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
10.3k
  register int aindex = alpha_index[cinfo->in_color_space];
252
10.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
10.3k
  ptr = source->pub._buffer[0];
255
10.3k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
1.34k
    if (aindex >= 0)
257
91
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
1.34k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
1.25k
    else
260
1.25k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
9.00k
  } else {
262
9.00k
    if (aindex >= 0)
263
1.97k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
9.00k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
7.02k
    else
266
7.02k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
9.00k
  }
268
10.3k
  return 1;
269
10.3k
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_row
Unexecuted instantiation: rdppm-12.c:get_text_rgb_row
rdppm-16.c:get_text_rgb_row
Line
Count
Source
241
10.3k
{
242
10.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
10.3k
  FILE *infile = source->pub.input_file;
244
10.3k
  register _JSAMPROW ptr;
245
10.3k
  register _JSAMPLE *rescale = source->rescale;
246
10.3k
  JDIMENSION col;
247
10.3k
  unsigned int maxval = source->maxval;
248
10.3k
  register int rindex = rgb_red[cinfo->in_color_space];
249
10.3k
  register int gindex = rgb_green[cinfo->in_color_space];
250
10.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
10.3k
  register int aindex = alpha_index[cinfo->in_color_space];
252
10.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
10.3k
  ptr = source->pub._buffer[0];
255
10.3k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
1.34k
    if (aindex >= 0)
257
91
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
1.34k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
1.25k
    else
260
1.25k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
9.00k
  } else {
262
9.00k
    if (aindex >= 0)
263
1.97k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
9.00k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
7.02k
    else
266
7.02k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
9.00k
  }
268
10.3k
  return 1;
269
10.3k
}
270
271
272
METHODDEF(JDIMENSION)
273
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
274
/* This version is for reading text-format PPM files with any maxval and
275
   converting to CMYK */
276
2.06k
{
277
2.06k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.06k
  FILE *infile = source->pub.input_file;
279
2.06k
  register _JSAMPROW ptr;
280
2.06k
  register _JSAMPLE *rescale = source->rescale;
281
2.06k
  JDIMENSION col;
282
2.06k
  unsigned int maxval = source->maxval;
283
284
2.06k
  ptr = source->pub._buffer[0];
285
2.06k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.14k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.61k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.61k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.61k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.61k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.61k
      ptr += 4;
292
1.61k
    }
293
1.53k
  } else {
294
4.11k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.57k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.57k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.57k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.57k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.57k
      ptr += 4;
300
2.57k
    }
301
1.53k
  }
302
2.06k
  return 1;
303
2.06k
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_text_rgb_cmyk_row
rdppm-16.c:get_text_rgb_cmyk_row
Line
Count
Source
276
2.06k
{
277
2.06k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.06k
  FILE *infile = source->pub.input_file;
279
2.06k
  register _JSAMPROW ptr;
280
2.06k
  register _JSAMPLE *rescale = source->rescale;
281
2.06k
  JDIMENSION col;
282
2.06k
  unsigned int maxval = source->maxval;
283
284
2.06k
  ptr = source->pub._buffer[0];
285
2.06k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.14k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.61k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.61k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.61k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.61k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.61k
      ptr += 4;
292
1.61k
    }
293
1.53k
  } else {
294
4.11k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.57k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.57k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.57k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.57k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.57k
      ptr += 4;
300
2.57k
    }
301
1.53k
  }
302
2.06k
  return 1;
303
2.06k
}
304
305
306
METHODDEF(JDIMENSION)
307
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
308
/* This version is for reading raw-byte-format PGM files with any maxval */
309
2.28M
{
310
2.28M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.28M
  register _JSAMPROW ptr;
312
2.28M
  register U_CHAR *bufferptr;
313
2.28M
  register _JSAMPLE *rescale = source->rescale;
314
2.28M
  JDIMENSION col;
315
316
2.28M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
67
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.28M
  ptr = source->pub._buffer[0];
319
2.28M
  bufferptr = source->iobuffer;
320
14.3M
  for (col = cinfo->image_width; col > 0; col--) {
321
12.0M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
12.0M
  }
323
2.28M
  return 1;
324
2.28M
}
Unexecuted instantiation: rdppm-8.c:get_scaled_gray_row
Unexecuted instantiation: rdppm-12.c:get_scaled_gray_row
rdppm-16.c:get_scaled_gray_row
Line
Count
Source
309
2.28M
{
310
2.28M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.28M
  register _JSAMPROW ptr;
312
2.28M
  register U_CHAR *bufferptr;
313
2.28M
  register _JSAMPLE *rescale = source->rescale;
314
2.28M
  JDIMENSION col;
315
316
2.28M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
67
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.28M
  ptr = source->pub._buffer[0];
319
2.28M
  bufferptr = source->iobuffer;
320
14.3M
  for (col = cinfo->image_width; col > 0; col--) {
321
12.0M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
12.0M
  }
323
2.28M
  return 1;
324
2.28M
}
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
11.4M
{
332
11.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
11.4M
  register _JSAMPROW ptr;
334
11.4M
  register U_CHAR *bufferptr;
335
11.4M
  register _JSAMPLE *rescale = source->rescale;
336
11.4M
  JDIMENSION col;
337
11.4M
  unsigned int maxval = source->maxval;
338
11.4M
  register int rindex = rgb_red[cinfo->in_color_space];
339
11.4M
  register int gindex = rgb_green[cinfo->in_color_space];
340
11.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
11.4M
  register int aindex = alpha_index[cinfo->in_color_space];
342
11.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
11.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
335
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
11.4M
  ptr = source->pub._buffer[0];
347
11.4M
  bufferptr = source->iobuffer;
348
11.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
0
    if (aindex >= 0)
350
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
0
    else
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
11.4M
  } else {
354
11.4M
    if (aindex >= 0)
355
2.28M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
11.4M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
9.13M
    else
358
9.13M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
11.4M
  }
360
11.4M
  return 1;
361
11.4M
}
Unexecuted instantiation: rdppm-8.c:get_gray_rgb_row
Unexecuted instantiation: rdppm-12.c:get_gray_rgb_row
rdppm-16.c:get_gray_rgb_row
Line
Count
Source
331
11.4M
{
332
11.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
11.4M
  register _JSAMPROW ptr;
334
11.4M
  register U_CHAR *bufferptr;
335
11.4M
  register _JSAMPLE *rescale = source->rescale;
336
11.4M
  JDIMENSION col;
337
11.4M
  unsigned int maxval = source->maxval;
338
11.4M
  register int rindex = rgb_red[cinfo->in_color_space];
339
11.4M
  register int gindex = rgb_green[cinfo->in_color_space];
340
11.4M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
11.4M
  register int aindex = alpha_index[cinfo->in_color_space];
342
11.4M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
11.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
335
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
11.4M
  ptr = source->pub._buffer[0];
347
11.4M
  bufferptr = source->iobuffer;
348
11.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
0
    if (aindex >= 0)
350
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
0
    else
352
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
11.4M
  } else {
354
11.4M
    if (aindex >= 0)
355
2.28M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
11.4M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
9.13M
    else
358
9.13M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
11.4M
  }
360
11.4M
  return 1;
361
11.4M
}
362
363
364
METHODDEF(JDIMENSION)
365
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
366
/* This version is for reading raw-byte-format PGM files with any maxval
367
   and converting to CMYK */
368
2.28M
{
369
2.28M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.28M
  register _JSAMPROW ptr;
371
2.28M
  register U_CHAR *bufferptr;
372
2.28M
  register _JSAMPLE *rescale = source->rescale;
373
2.28M
  JDIMENSION col;
374
2.28M
  unsigned int maxval = source->maxval;
375
376
2.28M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
67
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.28M
  ptr = source->pub._buffer[0];
379
2.28M
  bufferptr = source->iobuffer;
380
2.28M
  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
2.28M
  } else {
387
14.3M
    for (col = cinfo->image_width; col > 0; col--) {
388
12.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
12.0M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
12.0M
      ptr += 4;
391
12.0M
    }
392
2.28M
  }
393
2.28M
  return 1;
394
2.28M
}
Unexecuted instantiation: rdppm-8.c:get_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_gray_cmyk_row
rdppm-16.c:get_gray_cmyk_row
Line
Count
Source
368
2.28M
{
369
2.28M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.28M
  register _JSAMPROW ptr;
371
2.28M
  register U_CHAR *bufferptr;
372
2.28M
  register _JSAMPLE *rescale = source->rescale;
373
2.28M
  JDIMENSION col;
374
2.28M
  unsigned int maxval = source->maxval;
375
376
2.28M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
67
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.28M
  ptr = source->pub._buffer[0];
379
2.28M
  bufferptr = source->iobuffer;
380
2.28M
  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
2.28M
  } else {
387
14.3M
    for (col = cinfo->image_width; col > 0; col--) {
388
12.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
12.0M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
12.0M
      ptr += 4;
391
12.0M
    }
392
2.28M
  }
393
2.28M
  return 1;
394
2.28M
}
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
12.2k
{
401
12.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
12.2k
  register _JSAMPROW ptr;
403
12.2k
  register U_CHAR *bufferptr;
404
12.2k
  register _JSAMPLE *rescale = source->rescale;
405
12.2k
  JDIMENSION col;
406
12.2k
  unsigned int maxval = source->maxval;
407
12.2k
  register int rindex = rgb_red[cinfo->in_color_space];
408
12.2k
  register int gindex = rgb_green[cinfo->in_color_space];
409
12.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
12.2k
  register int aindex = alpha_index[cinfo->in_color_space];
411
12.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
12.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
475
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
12.2k
  ptr = source->pub._buffer[0];
416
12.2k
  bufferptr = source->iobuffer;
417
12.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
0
    if (aindex >= 0)
419
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
0
    else
421
0
      RGB_READ_LOOP(*bufferptr++, {})
422
12.2k
  } else {
423
12.2k
    if (aindex >= 0)
424
2.36k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
12.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
9.93k
    else
427
9.93k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
12.2k
  }
429
12.2k
  return 1;
430
12.2k
}
Unexecuted instantiation: rdppm-8.c:get_rgb_row
Unexecuted instantiation: rdppm-12.c:get_rgb_row
rdppm-16.c:get_rgb_row
Line
Count
Source
400
12.2k
{
401
12.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
12.2k
  register _JSAMPROW ptr;
403
12.2k
  register U_CHAR *bufferptr;
404
12.2k
  register _JSAMPLE *rescale = source->rescale;
405
12.2k
  JDIMENSION col;
406
12.2k
  unsigned int maxval = source->maxval;
407
12.2k
  register int rindex = rgb_red[cinfo->in_color_space];
408
12.2k
  register int gindex = rgb_green[cinfo->in_color_space];
409
12.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
12.2k
  register int aindex = alpha_index[cinfo->in_color_space];
411
12.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
12.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
475
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
12.2k
  ptr = source->pub._buffer[0];
416
12.2k
  bufferptr = source->iobuffer;
417
12.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
0
    if (aindex >= 0)
419
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
0
    else
421
0
      RGB_READ_LOOP(*bufferptr++, {})
422
12.2k
  } else {
423
12.2k
    if (aindex >= 0)
424
2.36k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
12.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
9.93k
    else
427
9.93k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
12.2k
  }
429
12.2k
  return 1;
430
12.2k
}
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
2.45k
{
438
2.45k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
2.45k
  register _JSAMPROW ptr;
440
2.45k
  register U_CHAR *bufferptr;
441
2.45k
  register _JSAMPLE *rescale = source->rescale;
442
2.45k
  JDIMENSION col;
443
2.45k
  unsigned int maxval = source->maxval;
444
445
2.45k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
95
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
2.45k
  ptr = source->pub._buffer[0];
448
2.45k
  bufferptr = source->iobuffer;
449
2.45k
  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
2.45k
  } else {
458
623k
    for (col = cinfo->image_width; col > 0; col--) {
459
621k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
621k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
621k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
621k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
621k
      ptr += 4;
464
621k
    }
465
2.45k
  }
466
2.45k
  return 1;
467
2.45k
}
Unexecuted instantiation: rdppm-8.c:get_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_rgb_cmyk_row
rdppm-16.c:get_rgb_cmyk_row
Line
Count
Source
437
2.45k
{
438
2.45k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
2.45k
  register _JSAMPROW ptr;
440
2.45k
  register U_CHAR *bufferptr;
441
2.45k
  register _JSAMPLE *rescale = source->rescale;
442
2.45k
  JDIMENSION col;
443
2.45k
  unsigned int maxval = source->maxval;
444
445
2.45k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
95
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
2.45k
  ptr = source->pub._buffer[0];
448
2.45k
  bufferptr = source->iobuffer;
449
2.45k
  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
2.45k
  } else {
458
623k
    for (col = cinfo->image_width; col > 0; col--) {
459
621k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
621k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
621k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
621k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
621k
      ptr += 4;
464
621k
    }
465
2.45k
  }
466
2.45k
  return 1;
467
2.45k
}
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
0
{
478
0
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
0
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
0
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
0
  return 1;
483
0
}
Unexecuted instantiation: rdppm-8.c:get_raw_row
Unexecuted instantiation: rdppm-12.c:get_raw_row
Unexecuted instantiation: rdppm-16.c:get_raw_row
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
8.47k
{
490
8.47k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
8.47k
  register _JSAMPROW ptr;
492
8.47k
  register U_CHAR *bufferptr;
493
8.47k
  register _JSAMPLE *rescale = source->rescale;
494
8.47k
  JDIMENSION col;
495
8.47k
  unsigned int maxval = source->maxval;
496
497
8.47k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
66
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
8.47k
  ptr = source->pub._buffer[0];
500
8.47k
  bufferptr = source->iobuffer;
501
104k
  for (col = cinfo->image_width; col > 0; col--) {
502
95.9k
    register unsigned int temp;
503
95.9k
    temp  = UCH(*bufferptr++) << 8;
504
95.9k
    temp |= UCH(*bufferptr++);
505
95.9k
    if (temp > maxval)
506
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
95.9k
    *ptr++ = rescale[temp];
508
95.9k
  }
509
8.47k
  return 1;
510
8.47k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_row
rdppm-16.c:get_word_gray_row
Line
Count
Source
489
8.47k
{
490
8.47k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
8.47k
  register _JSAMPROW ptr;
492
8.47k
  register U_CHAR *bufferptr;
493
8.47k
  register _JSAMPLE *rescale = source->rescale;
494
8.47k
  JDIMENSION col;
495
8.47k
  unsigned int maxval = source->maxval;
496
497
8.47k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
66
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
8.47k
  ptr = source->pub._buffer[0];
500
8.47k
  bufferptr = source->iobuffer;
501
104k
  for (col = cinfo->image_width; col > 0; col--) {
502
95.9k
    register unsigned int temp;
503
95.9k
    temp  = UCH(*bufferptr++) << 8;
504
95.9k
    temp |= UCH(*bufferptr++);
505
95.9k
    if (temp > maxval)
506
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
95.9k
    *ptr++ = rescale[temp];
508
95.9k
  }
509
8.47k
  return 1;
510
8.47k
}
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
42.3k
{
517
42.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
42.3k
  register _JSAMPROW ptr;
519
42.3k
  register U_CHAR *bufferptr;
520
42.3k
  register _JSAMPLE *rescale = source->rescale;
521
42.3k
  JDIMENSION col;
522
42.3k
  unsigned int maxval = source->maxval;
523
42.3k
  register int rindex = rgb_red[cinfo->in_color_space];
524
42.3k
  register int gindex = rgb_green[cinfo->in_color_space];
525
42.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
42.3k
  register int aindex = alpha_index[cinfo->in_color_space];
527
42.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
42.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
330
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
42.3k
  ptr = source->pub._buffer[0];
532
42.3k
  bufferptr = source->iobuffer;
533
522k
  for (col = cinfo->image_width; col > 0; col--) {
534
479k
    register unsigned int temp;
535
479k
    temp  = UCH(*bufferptr++) << 8;
536
479k
    temp |= UCH(*bufferptr++);
537
479k
    if (temp > maxval)
538
170
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
479k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
479k
    if (aindex >= 0)
541
95.9k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
479k
    ptr += ps;
543
479k
  }
544
42.3k
  return 1;
545
42.3k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_rgb_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_rgb_row
rdppm-16.c:get_word_gray_rgb_row
Line
Count
Source
516
42.3k
{
517
42.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
42.3k
  register _JSAMPROW ptr;
519
42.3k
  register U_CHAR *bufferptr;
520
42.3k
  register _JSAMPLE *rescale = source->rescale;
521
42.3k
  JDIMENSION col;
522
42.3k
  unsigned int maxval = source->maxval;
523
42.3k
  register int rindex = rgb_red[cinfo->in_color_space];
524
42.3k
  register int gindex = rgb_green[cinfo->in_color_space];
525
42.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
42.3k
  register int aindex = alpha_index[cinfo->in_color_space];
527
42.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
42.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
330
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
42.3k
  ptr = source->pub._buffer[0];
532
42.3k
  bufferptr = source->iobuffer;
533
522k
  for (col = cinfo->image_width; col > 0; col--) {
534
479k
    register unsigned int temp;
535
479k
    temp  = UCH(*bufferptr++) << 8;
536
479k
    temp |= UCH(*bufferptr++);
537
479k
    if (temp > maxval)
538
170
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
479k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
479k
    if (aindex >= 0)
541
95.9k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
479k
    ptr += ps;
543
479k
  }
544
42.3k
  return 1;
545
42.3k
}
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
8.47k
{
552
8.47k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
8.47k
  register _JSAMPROW ptr;
554
8.47k
  register U_CHAR *bufferptr;
555
8.47k
  register _JSAMPLE *rescale = source->rescale;
556
8.47k
  JDIMENSION col;
557
8.47k
  unsigned int maxval = source->maxval;
558
559
8.47k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
66
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
8.47k
  ptr = source->pub._buffer[0];
562
8.47k
  bufferptr = source->iobuffer;
563
104k
  for (col = cinfo->image_width; col > 0; col--) {
564
95.9k
    register unsigned int gray;
565
95.9k
    gray  = UCH(*bufferptr++) << 8;
566
95.9k
    gray |= UCH(*bufferptr++);
567
95.9k
    if (gray > maxval)
568
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
95.9k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
95.9k
                ptr + 1, ptr + 2, ptr + 3);
571
95.9k
    ptr += 4;
572
95.9k
  }
573
8.47k
  return 1;
574
8.47k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_word_gray_cmyk_row
rdppm-16.c:get_word_gray_cmyk_row
Line
Count
Source
551
8.47k
{
552
8.47k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
8.47k
  register _JSAMPROW ptr;
554
8.47k
  register U_CHAR *bufferptr;
555
8.47k
  register _JSAMPLE *rescale = source->rescale;
556
8.47k
  JDIMENSION col;
557
8.47k
  unsigned int maxval = source->maxval;
558
559
8.47k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
66
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
8.47k
  ptr = source->pub._buffer[0];
562
8.47k
  bufferptr = source->iobuffer;
563
104k
  for (col = cinfo->image_width; col > 0; col--) {
564
95.9k
    register unsigned int gray;
565
95.9k
    gray  = UCH(*bufferptr++) << 8;
566
95.9k
    gray |= UCH(*bufferptr++);
567
95.9k
    if (gray > maxval)
568
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
95.9k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
95.9k
                ptr + 1, ptr + 2, ptr + 3);
571
95.9k
    ptr += 4;
572
95.9k
  }
573
8.47k
  return 1;
574
8.47k
}
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
19.7k
{
581
19.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
19.7k
  register _JSAMPROW ptr;
583
19.7k
  register U_CHAR *bufferptr;
584
19.7k
  register _JSAMPLE *rescale = source->rescale;
585
19.7k
  JDIMENSION col;
586
19.7k
  unsigned int maxval = source->maxval;
587
19.7k
  register int rindex = rgb_red[cinfo->in_color_space];
588
19.7k
  register int gindex = rgb_green[cinfo->in_color_space];
589
19.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
19.7k
  register int aindex = alpha_index[cinfo->in_color_space];
591
19.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
19.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
405
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
19.7k
  ptr = source->pub._buffer[0];
596
19.7k
  bufferptr = source->iobuffer;
597
271k
  for (col = cinfo->image_width; col > 0; col--) {
598
251k
    register unsigned int temp;
599
251k
    temp  = UCH(*bufferptr++) << 8;
600
251k
    temp |= UCH(*bufferptr++);
601
251k
    if (temp > maxval)
602
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
251k
    ptr[rindex] = rescale[temp];
604
251k
    temp  = UCH(*bufferptr++) << 8;
605
251k
    temp |= UCH(*bufferptr++);
606
251k
    if (temp > maxval)
607
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
251k
    ptr[gindex] = rescale[temp];
609
251k
    temp  = UCH(*bufferptr++) << 8;
610
251k
    temp |= UCH(*bufferptr++);
611
251k
    if (temp > maxval)
612
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
251k
    ptr[bindex] = rescale[temp];
614
251k
    if (aindex >= 0)
615
50.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
251k
    ptr += ps;
617
251k
  }
618
19.7k
  return 1;
619
19.7k
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_row
Unexecuted instantiation: rdppm-12.c:get_word_rgb_row
rdppm-16.c:get_word_rgb_row
Line
Count
Source
580
19.7k
{
581
19.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
19.7k
  register _JSAMPROW ptr;
583
19.7k
  register U_CHAR *bufferptr;
584
19.7k
  register _JSAMPLE *rescale = source->rescale;
585
19.7k
  JDIMENSION col;
586
19.7k
  unsigned int maxval = source->maxval;
587
19.7k
  register int rindex = rgb_red[cinfo->in_color_space];
588
19.7k
  register int gindex = rgb_green[cinfo->in_color_space];
589
19.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
19.7k
  register int aindex = alpha_index[cinfo->in_color_space];
591
19.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
19.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
405
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
19.7k
  ptr = source->pub._buffer[0];
596
19.7k
  bufferptr = source->iobuffer;
597
271k
  for (col = cinfo->image_width; col > 0; col--) {
598
251k
    register unsigned int temp;
599
251k
    temp  = UCH(*bufferptr++) << 8;
600
251k
    temp |= UCH(*bufferptr++);
601
251k
    if (temp > maxval)
602
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
251k
    ptr[rindex] = rescale[temp];
604
251k
    temp  = UCH(*bufferptr++) << 8;
605
251k
    temp |= UCH(*bufferptr++);
606
251k
    if (temp > maxval)
607
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
251k
    ptr[gindex] = rescale[temp];
609
251k
    temp  = UCH(*bufferptr++) << 8;
610
251k
    temp |= UCH(*bufferptr++);
611
251k
    if (temp > maxval)
612
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
251k
    ptr[bindex] = rescale[temp];
614
251k
    if (aindex >= 0)
615
50.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
251k
    ptr += ps;
617
251k
  }
618
19.7k
  return 1;
619
19.7k
}
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
3.94k
{
626
3.94k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
3.94k
  register _JSAMPROW ptr;
628
3.94k
  register U_CHAR *bufferptr;
629
3.94k
  register _JSAMPLE *rescale = source->rescale;
630
3.94k
  JDIMENSION col;
631
3.94k
  unsigned int maxval = source->maxval;
632
633
3.94k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
81
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
3.94k
  ptr = source->pub._buffer[0];
636
3.94k
  bufferptr = source->iobuffer;
637
54.2k
  for (col = cinfo->image_width; col > 0; col--) {
638
50.3k
    register unsigned int r, g, b;
639
50.3k
    r  = UCH(*bufferptr++) << 8;
640
50.3k
    r |= UCH(*bufferptr++);
641
50.3k
    if (r > maxval)
642
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
50.3k
    g  = UCH(*bufferptr++) << 8;
644
50.3k
    g |= UCH(*bufferptr++);
645
50.3k
    if (g > maxval)
646
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
50.3k
    b  = UCH(*bufferptr++) << 8;
648
50.3k
    b |= UCH(*bufferptr++);
649
50.3k
    if (b > maxval)
650
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
50.3k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
50.3k
                ptr + 2, ptr + 3);
653
50.3k
    ptr += 4;
654
50.3k
  }
655
3.94k
  return 1;
656
3.94k
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_cmyk_row
Unexecuted instantiation: rdppm-12.c:get_word_rgb_cmyk_row
rdppm-16.c:get_word_rgb_cmyk_row
Line
Count
Source
625
3.94k
{
626
3.94k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
3.94k
  register _JSAMPROW ptr;
628
3.94k
  register U_CHAR *bufferptr;
629
3.94k
  register _JSAMPLE *rescale = source->rescale;
630
3.94k
  JDIMENSION col;
631
3.94k
  unsigned int maxval = source->maxval;
632
633
3.94k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
81
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
3.94k
  ptr = source->pub._buffer[0];
636
3.94k
  bufferptr = source->iobuffer;
637
54.2k
  for (col = cinfo->image_width; col > 0; col--) {
638
50.3k
    register unsigned int r, g, b;
639
50.3k
    r  = UCH(*bufferptr++) << 8;
640
50.3k
    r |= UCH(*bufferptr++);
641
50.3k
    if (r > maxval)
642
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
50.3k
    g  = UCH(*bufferptr++) << 8;
644
50.3k
    g |= UCH(*bufferptr++);
645
50.3k
    if (g > maxval)
646
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
50.3k
    b  = UCH(*bufferptr++) << 8;
648
50.3k
    b |= UCH(*bufferptr++);
649
50.3k
    if (b > maxval)
650
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
50.3k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
50.3k
                ptr + 2, ptr + 3);
653
50.3k
    ptr += 4;
654
50.3k
  }
655
3.94k
  return 1;
656
3.94k
}
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
10.8k
{
666
10.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
10.8k
  int c;
668
10.8k
  unsigned int w, h, maxval;
669
10.8k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
10.8k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
10.8k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
10.8k
  switch (c) {
678
1.58k
  case '2':                     /* it's a text-format PGM file */
679
3.38k
  case '3':                     /* it's a text-format PPM file */
680
8.46k
  case '5':                     /* it's a raw-format PGM file */
681
10.8k
  case '6':                     /* it's a raw-format PPM file */
682
10.8k
    break;
683
14
  default:
684
14
    ERREXIT(cinfo, JERR_PPM_NOT);
685
14
    break;
686
10.8k
  }
687
688
  /* fetch the remaining header info */
689
10.8k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
10.8k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
10.8k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
10.8k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
10.8k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
231
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
10.8k
  cinfo->image_width = (JDIMENSION)w;
699
10.8k
  cinfo->image_height = (JDIMENSION)h;
700
10.8k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
10.8k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
10.8k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
10.8k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
10.8k
  switch (c) {
708
1.21k
  case '2':                     /* it's a text-format PGM file */
709
1.21k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.21k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.21k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.21k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
173
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.03k
    else if (IsExtRGB(cinfo->in_color_space))
716
865
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
173
    else if (cinfo->in_color_space == JCS_CMYK)
718
173
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.21k
    need_iobuffer = FALSE;
722
1.21k
    break;
723
724
1.44k
  case '3':                     /* it's a text-format PPM file */
725
1.44k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.44k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.44k
    if (IsExtRGB(cinfo->in_color_space))
729
1.03k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
412
    else if (cinfo->in_color_space == JCS_CMYK)
731
206
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
206
    else
733
206
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.44k
    need_iobuffer = FALSE;
735
1.44k
    break;
736
737
4.69k
  case '5':                     /* it's a raw-format PGM file */
738
4.69k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
4.69k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
4.69k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
4.69k
    if (maxval > 255) {
743
868
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
124
        source->pub.get_pixel_rows = get_word_gray_row;
745
744
      else if (IsExtRGB(cinfo->in_color_space))
746
620
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
124
      else if (cinfo->in_color_space == JCS_CMYK)
748
124
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
3.82k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
0
      source->pub.get_pixel_rows = get_raw_row;
755
0
      use_raw_buffer = TRUE;
756
0
      need_rescale = FALSE;
757
3.82k
    } else {
758
3.82k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
546
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
3.27k
      else if (IsExtRGB(cinfo->in_color_space))
761
2.73k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
546
      else if (cinfo->in_color_space == JCS_CMYK)
763
546
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
3.82k
    }
767
4.69k
    break;
768
769
2.16k
  case '6':                     /* it's a raw-format PPM file */
770
2.16k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.16k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.16k
    if (maxval > 255) {
774
1.16k
      if (IsExtRGB(cinfo->in_color_space))
775
835
        source->pub.get_pixel_rows = get_word_rgb_row;
776
334
      else if (cinfo->in_color_space == JCS_CMYK)
777
167
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
167
      else
779
167
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.16k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
0
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
0
      source->pub.get_pixel_rows = get_raw_row;
789
0
      use_raw_buffer = TRUE;
790
0
      need_rescale = FALSE;
791
994
    } else {
792
994
      if (IsExtRGB(cinfo->in_color_space))
793
710
        source->pub.get_pixel_rows = get_rgb_row;
794
284
      else if (cinfo->in_color_space == JCS_CMYK)
795
142
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
142
      else
797
142
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
994
    }
799
2.16k
    break;
800
10.8k
  }
801
802
8.99k
  if (IsExtRGB(cinfo->in_color_space))
803
6.79k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.20k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
843
    cinfo->input_components = 1;
806
1.35k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.35k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
8.99k
  if (need_iobuffer) {
811
6.54k
    if (c == '6')
812
1.85k
      source->buffer_width = (size_t)w * 3 *
813
1.85k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
4.69k
    else
815
4.69k
      source->buffer_width = (size_t)w *
816
4.69k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
6.54k
    source->iobuffer = (U_CHAR *)
818
6.54k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
6.54k
                                  source->buffer_width);
820
6.54k
  }
821
822
  /* Create compressor input buffer. */
823
8.99k
  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
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
0
    source->pub._buffer = &source->pixrow;
828
0
    source->pub.buffer_height = 1;
829
8.99k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
8.99k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
8.99k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
8.99k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
8.99k
    source->pub.buffer_height = 1;
835
8.99k
  }
836
837
  /* Compute the rescaling array if required. */
838
8.99k
  if (need_rescale) {
839
8.99k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
8.99k
    source->rescale = (_JSAMPLE *)
843
8.99k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
8.99k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
8.99k
                                           sizeof(_JSAMPLE)));
846
8.99k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
8.99k
                                        sizeof(_JSAMPLE)));
848
8.99k
    half_maxval = maxval / 2;
849
93.4M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
93.4M
      source->rescale[val] =
852
93.4M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
93.4M
                   maxval);
854
93.4M
    }
855
8.99k
  }
856
8.99k
}
Unexecuted instantiation: rdppm-8.c:start_input_ppm
Unexecuted instantiation: rdppm-12.c:start_input_ppm
rdppm-16.c:start_input_ppm
Line
Count
Source
665
10.8k
{
666
10.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
10.8k
  int c;
668
10.8k
  unsigned int w, h, maxval;
669
10.8k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
10.8k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
10.8k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
10.8k
  switch (c) {
678
1.58k
  case '2':                     /* it's a text-format PGM file */
679
3.38k
  case '3':                     /* it's a text-format PPM file */
680
8.46k
  case '5':                     /* it's a raw-format PGM file */
681
10.8k
  case '6':                     /* it's a raw-format PPM file */
682
10.8k
    break;
683
14
  default:
684
14
    ERREXIT(cinfo, JERR_PPM_NOT);
685
14
    break;
686
10.8k
  }
687
688
  /* fetch the remaining header info */
689
10.8k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
10.8k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
10.8k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
10.8k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
10.8k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
231
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
10.8k
  cinfo->image_width = (JDIMENSION)w;
699
10.8k
  cinfo->image_height = (JDIMENSION)h;
700
10.8k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
10.8k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
10.8k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
10.8k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
10.8k
  switch (c) {
708
1.21k
  case '2':                     /* it's a text-format PGM file */
709
1.21k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.21k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.21k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.21k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
173
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.03k
    else if (IsExtRGB(cinfo->in_color_space))
716
865
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
173
    else if (cinfo->in_color_space == JCS_CMYK)
718
173
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.21k
    need_iobuffer = FALSE;
722
1.21k
    break;
723
724
1.44k
  case '3':                     /* it's a text-format PPM file */
725
1.44k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.44k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.44k
    if (IsExtRGB(cinfo->in_color_space))
729
1.03k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
412
    else if (cinfo->in_color_space == JCS_CMYK)
731
206
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
206
    else
733
206
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.44k
    need_iobuffer = FALSE;
735
1.44k
    break;
736
737
4.69k
  case '5':                     /* it's a raw-format PGM file */
738
4.69k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
4.69k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
4.69k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
4.69k
    if (maxval > 255) {
743
868
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
124
        source->pub.get_pixel_rows = get_word_gray_row;
745
744
      else if (IsExtRGB(cinfo->in_color_space))
746
620
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
124
      else if (cinfo->in_color_space == JCS_CMYK)
748
124
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
3.82k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
0
      source->pub.get_pixel_rows = get_raw_row;
755
0
      use_raw_buffer = TRUE;
756
0
      need_rescale = FALSE;
757
3.82k
    } else {
758
3.82k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
546
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
3.27k
      else if (IsExtRGB(cinfo->in_color_space))
761
2.73k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
546
      else if (cinfo->in_color_space == JCS_CMYK)
763
546
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
3.82k
    }
767
4.69k
    break;
768
769
2.16k
  case '6':                     /* it's a raw-format PPM file */
770
2.16k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.16k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.16k
    if (maxval > 255) {
774
1.16k
      if (IsExtRGB(cinfo->in_color_space))
775
835
        source->pub.get_pixel_rows = get_word_rgb_row;
776
334
      else if (cinfo->in_color_space == JCS_CMYK)
777
167
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
167
      else
779
167
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.16k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
0
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
0
      source->pub.get_pixel_rows = get_raw_row;
789
0
      use_raw_buffer = TRUE;
790
0
      need_rescale = FALSE;
791
994
    } else {
792
994
      if (IsExtRGB(cinfo->in_color_space))
793
710
        source->pub.get_pixel_rows = get_rgb_row;
794
284
      else if (cinfo->in_color_space == JCS_CMYK)
795
142
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
142
      else
797
142
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
994
    }
799
2.16k
    break;
800
10.8k
  }
801
802
8.99k
  if (IsExtRGB(cinfo->in_color_space))
803
6.79k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.20k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
843
    cinfo->input_components = 1;
806
1.35k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.35k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
8.99k
  if (need_iobuffer) {
811
6.54k
    if (c == '6')
812
1.85k
      source->buffer_width = (size_t)w * 3 *
813
1.85k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
4.69k
    else
815
4.69k
      source->buffer_width = (size_t)w *
816
4.69k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
6.54k
    source->iobuffer = (U_CHAR *)
818
6.54k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
6.54k
                                  source->buffer_width);
820
6.54k
  }
821
822
  /* Create compressor input buffer. */
823
8.99k
  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
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
0
    source->pub._buffer = &source->pixrow;
828
0
    source->pub.buffer_height = 1;
829
8.99k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
8.99k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
8.99k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
8.99k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
8.99k
    source->pub.buffer_height = 1;
835
8.99k
  }
836
837
  /* Compute the rescaling array if required. */
838
8.99k
  if (need_rescale) {
839
8.99k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
8.99k
    source->rescale = (_JSAMPLE *)
843
8.99k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
8.99k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
8.99k
                                           sizeof(_JSAMPLE)));
846
8.99k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
8.99k
                                        sizeof(_JSAMPLE)));
848
8.99k
    half_maxval = maxval / 2;
849
93.4M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
93.4M
      source->rescale[val] =
852
93.4M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
93.4M
                   maxval);
854
93.4M
    }
855
8.99k
  }
856
8.99k
}
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
3.96k
{
866
  /* no work */
867
3.96k
}
Unexecuted instantiation: rdppm-8.c:finish_input_ppm
Unexecuted instantiation: rdppm-12.c:finish_input_ppm
rdppm-16.c:finish_input_ppm
Line
Count
Source
865
3.96k
{
866
  /* no work */
867
3.96k
}
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
10.8k
{
877
10.8k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
10.8k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
10.8k
      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
10.8k
  source = (ppm_source_ptr)
889
10.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
10.8k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
10.8k
  source->pub.start_input = start_input_ppm;
893
10.8k
  source->pub.finish_input = finish_input_ppm;
894
10.8k
  source->pub.max_pixels = 0;
895
896
10.8k
  return (cjpeg_source_ptr)source;
897
10.8k
}
Unexecuted instantiation: jinit_read_ppm
Unexecuted instantiation: j12init_read_ppm
j16init_read_ppm
Line
Count
Source
876
10.8k
{
877
10.8k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
10.8k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
10.8k
      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
10.8k
  source = (ppm_source_ptr)
889
10.8k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
10.8k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
10.8k
  source->pub.start_input = start_input_ppm;
893
10.8k
  source->pub.finish_input = finish_input_ppm;
894
10.8k
  source->pub.max_pixels = 0;
895
896
10.8k
  return (cjpeg_source_ptr)source;
897
10.8k
}
898
899
#endif /* defined(PPM_SUPPORTED) &&
900
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */