Coverage Report

Created: 2025-10-13 06:05

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