Coverage Report

Created: 2026-09-13 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/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-2026, 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
#define ReadOK(file, buffer, len) \
46
12.5M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
47
48
static int alpha_index[JPEG_NUMCS] = {
49
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
50
};
51
52
53
/* Private version of data source object */
54
55
typedef struct {
56
  struct cjpeg_source_struct pub; /* public fields */
57
58
  /* Usually these two pointers point to the same place: */
59
  unsigned char *iobuffer;      /* fread's I/O buffer */
60
  _JSAMPROW pixrow;             /* compressor input buffer */
61
  size_t buffer_width;          /* width of I/O buffer */
62
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
63
  unsigned int maxval;
64
} ppm_source_struct;
65
66
typedef ppm_source_struct *ppm_source_ptr;
67
68
69
LOCAL(int)
70
pbm_getc(FILE *infile)
71
/* Read next char, skipping over any comments */
72
/* A comment/newline sequence is returned as a newline */
73
304k
{
74
304k
  register int ch;
75
76
304k
  ch = getc(infile);
77
304k
  if (ch == '#') {
78
128k
    do {
79
128k
      ch = getc(infile);
80
128k
    } while (ch != '\n' && ch != EOF);
81
4.66k
  }
82
304k
  return ch;
83
304k
}
Unexecuted instantiation: rdppm-8.c:pbm_getc
Unexecuted instantiation: rdppm-12.c:pbm_getc
rdppm-16.c:pbm_getc
Line
Count
Source
73
304k
{
74
304k
  register int ch;
75
76
304k
  ch = getc(infile);
77
304k
  if (ch == '#') {
78
128k
    do {
79
128k
      ch = getc(infile);
80
128k
    } while (ch != '\n' && ch != EOF);
81
4.66k
  }
82
304k
  return ch;
83
304k
}
84
85
86
LOCAL(unsigned int)
87
read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
88
/* Read an unsigned decimal integer from the PPM file */
89
/* Swallows one trailing character after the integer */
90
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
91
/* This should not be a problem in practice. */
92
133k
{
93
133k
  register int ch;
94
133k
  register unsigned int val;
95
96
  /* Skip any leading whitespace */
97
141k
  do {
98
141k
    ch = pbm_getc(infile);
99
141k
    if (ch == EOF)
100
3.40k
      ERREXIT(cinfo, JERR_INPUT_EOF);
101
141k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
102
103
133k
  if (ch < '0' || ch > '9')
104
227
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
105
106
133k
  val = ch - '0';
107
166k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
108
33.6k
    val *= 10;
109
33.6k
    val += ch - '0';
110
33.6k
    if (val > maxval)
111
132
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
112
33.6k
  }
113
114
133k
  return val;
115
133k
}
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
92
133k
{
93
133k
  register int ch;
94
133k
  register unsigned int val;
95
96
  /* Skip any leading whitespace */
97
141k
  do {
98
141k
    ch = pbm_getc(infile);
99
141k
    if (ch == EOF)
100
3.40k
      ERREXIT(cinfo, JERR_INPUT_EOF);
101
141k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
102
103
133k
  if (ch < '0' || ch > '9')
104
227
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
105
106
133k
  val = ch - '0';
107
166k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
108
33.6k
    val *= 10;
109
33.6k
    val += ch - '0';
110
33.6k
    if (val > maxval)
111
132
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
112
33.6k
  }
113
114
133k
  return val;
115
133k
}
116
117
118
/*
119
 * Read one row of pixels.
120
 *
121
 * We provide several different versions depending on input file format.
122
 * In all cases, input is scaled to cinfo->data_precision.
123
 *
124
 * A really fast path is provided for reading byte/sample raw files with
125
 * maxval <= _MAXJSAMPLE and maxval == (1U << cinfo->data_precision) - 1U,
126
 * which is the normal case for 8-bit data.
127
 */
128
129
130
METHODDEF(JDIMENSION)
131
get_text_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
132
/* This version is for reading text-format PGM files with any maxval */
133
1.79k
{
134
1.79k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
135
1.79k
  FILE *infile = source->pub.input_file;
136
1.79k
  register _JSAMPROW ptr;
137
1.79k
  register _JSAMPLE *rescale = source->rescale;
138
1.79k
  JDIMENSION col;
139
1.79k
  unsigned int maxval = source->maxval;
140
141
1.79k
  ptr = source->pub._buffer[0];
142
5.23k
  for (col = cinfo->image_width; col > 0; col--) {
143
3.44k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
144
3.44k
  }
145
1.79k
  return 1;
146
1.79k
}
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
133
1.79k
{
134
1.79k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
135
1.79k
  FILE *infile = source->pub.input_file;
136
1.79k
  register _JSAMPROW ptr;
137
1.79k
  register _JSAMPLE *rescale = source->rescale;
138
1.79k
  JDIMENSION col;
139
1.79k
  unsigned int maxval = source->maxval;
140
141
1.79k
  ptr = source->pub._buffer[0];
142
5.23k
  for (col = cinfo->image_width; col > 0; col--) {
143
3.44k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
144
3.44k
  }
145
1.79k
  return 1;
146
1.79k
}
147
148
149
8.87M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
150
42.6M
  for (col = cinfo->image_width; col > 0; col--) { \
151
33.7M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
152
33.7M
    alpha_set_op \
153
33.7M
    ptr += ps; \
154
33.7M
  } \
155
8.87M
}
156
157
METHODDEF(JDIMENSION)
158
get_text_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
159
/* This version is for reading text-format PGM files with any maxval and
160
   converting to extended RGB */
161
8.96k
{
162
8.96k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
163
8.96k
  FILE *infile = source->pub.input_file;
164
8.96k
  register _JSAMPROW ptr;
165
8.96k
  register _JSAMPLE *rescale = source->rescale;
166
8.96k
  JDIMENSION col;
167
8.96k
  unsigned int maxval = source->maxval;
168
8.96k
  register int rindex = rgb_red[cinfo->in_color_space];
169
8.96k
  register int gindex = rgb_green[cinfo->in_color_space];
170
8.96k
  register int bindex = rgb_blue[cinfo->in_color_space];
171
8.96k
  register int aindex = alpha_index[cinfo->in_color_space];
172
8.96k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
173
174
8.96k
  ptr = source->pub._buffer[0];
175
8.96k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
176
1.00k
    if (aindex >= 0)
177
217
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
178
1.00k
                         ptr[aindex] = (_JSAMPLE)maxval;)
179
787
    else
180
787
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
181
7.96k
  } else {
182
7.96k
    if (aindex >= 0)
183
1.57k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
184
7.96k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
185
6.38k
    else
186
6.38k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
187
7.96k
  }
188
8.96k
  return 1;
189
8.96k
}
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
161
8.96k
{
162
8.96k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
163
8.96k
  FILE *infile = source->pub.input_file;
164
8.96k
  register _JSAMPROW ptr;
165
8.96k
  register _JSAMPLE *rescale = source->rescale;
166
8.96k
  JDIMENSION col;
167
8.96k
  unsigned int maxval = source->maxval;
168
8.96k
  register int rindex = rgb_red[cinfo->in_color_space];
169
8.96k
  register int gindex = rgb_green[cinfo->in_color_space];
170
8.96k
  register int bindex = rgb_blue[cinfo->in_color_space];
171
8.96k
  register int aindex = alpha_index[cinfo->in_color_space];
172
8.96k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
173
174
8.96k
  ptr = source->pub._buffer[0];
175
8.96k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
176
1.00k
    if (aindex >= 0)
177
217
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
178
1.00k
                         ptr[aindex] = (_JSAMPLE)maxval;)
179
787
    else
180
787
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
181
7.96k
  } else {
182
7.96k
    if (aindex >= 0)
183
1.57k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
184
7.96k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
185
6.38k
    else
186
6.38k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
187
7.96k
  }
188
8.96k
  return 1;
189
8.96k
}
190
191
192
METHODDEF(JDIMENSION)
193
get_text_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
194
/* This version is for reading text-format PGM files with any maxval and
195
   converting to CMYK */
196
1.79k
{
197
1.79k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
198
1.79k
  FILE *infile = source->pub.input_file;
199
1.79k
  register _JSAMPROW ptr;
200
1.79k
  register _JSAMPLE *rescale = source->rescale;
201
1.79k
  JDIMENSION col;
202
1.79k
  unsigned int maxval = source->maxval;
203
204
1.79k
  ptr = source->pub._buffer[0];
205
1.79k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
206
1.04k
    for (col = cinfo->image_width; col > 0; col--) {
207
745
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
208
745
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
209
745
      ptr += 4;
210
745
    }
211
1.49k
  } else {
212
4.18k
    for (col = cinfo->image_width; col > 0; col--) {
213
2.69k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
214
2.69k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
215
2.69k
                  ptr + 1, ptr + 2, ptr + 3);
216
2.69k
      ptr += 4;
217
2.69k
    }
218
1.49k
  }
219
1.79k
  return 1;
220
1.79k
}
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
196
1.79k
{
197
1.79k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
198
1.79k
  FILE *infile = source->pub.input_file;
199
1.79k
  register _JSAMPROW ptr;
200
1.79k
  register _JSAMPLE *rescale = source->rescale;
201
1.79k
  JDIMENSION col;
202
1.79k
  unsigned int maxval = source->maxval;
203
204
1.79k
  ptr = source->pub._buffer[0];
205
1.79k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
206
1.04k
    for (col = cinfo->image_width; col > 0; col--) {
207
745
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
208
745
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
209
745
      ptr += 4;
210
745
    }
211
1.49k
  } else {
212
4.18k
    for (col = cinfo->image_width; col > 0; col--) {
213
2.69k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
214
2.69k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
215
2.69k
                  ptr + 1, ptr + 2, ptr + 3);
216
2.69k
      ptr += 4;
217
2.69k
    }
218
1.49k
  }
219
1.79k
  return 1;
220
1.79k
}
221
222
223
77.8k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
224
3.06M
  for (col = cinfo->image_width; col > 0; col--) { \
225
2.98M
    ptr[rindex] = read_op; \
226
2.98M
    ptr[gindex] = read_op; \
227
2.98M
    ptr[bindex] = read_op; \
228
2.98M
    alpha_set_op \
229
2.98M
    ptr += ps; \
230
2.98M
  } \
231
77.8k
}
232
233
METHODDEF(JDIMENSION)
234
get_text_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
235
/* This version is for reading text-format PPM files with any maxval */
236
14.8k
{
237
14.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
238
14.8k
  FILE *infile = source->pub.input_file;
239
14.8k
  register _JSAMPROW ptr;
240
14.8k
  register _JSAMPLE *rescale = source->rescale;
241
14.8k
  JDIMENSION col;
242
14.8k
  unsigned int maxval = source->maxval;
243
14.8k
  register int rindex = rgb_red[cinfo->in_color_space];
244
14.8k
  register int gindex = rgb_green[cinfo->in_color_space];
245
14.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
246
14.8k
  register int aindex = alpha_index[cinfo->in_color_space];
247
14.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
248
249
14.8k
  ptr = source->pub._buffer[0];
250
14.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
251
2.27k
    if (aindex >= 0)
252
224
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
253
2.27k
                    ptr[aindex] = (_JSAMPLE)maxval;)
254
2.05k
    else
255
2.05k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
256
12.6k
  } else {
257
12.6k
    if (aindex >= 0)
258
2.75k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
259
12.6k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
260
9.86k
    else
261
9.86k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
262
12.6k
  }
263
14.8k
  return 1;
264
14.8k
}
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
236
14.8k
{
237
14.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
238
14.8k
  FILE *infile = source->pub.input_file;
239
14.8k
  register _JSAMPROW ptr;
240
14.8k
  register _JSAMPLE *rescale = source->rescale;
241
14.8k
  JDIMENSION col;
242
14.8k
  unsigned int maxval = source->maxval;
243
14.8k
  register int rindex = rgb_red[cinfo->in_color_space];
244
14.8k
  register int gindex = rgb_green[cinfo->in_color_space];
245
14.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
246
14.8k
  register int aindex = alpha_index[cinfo->in_color_space];
247
14.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
248
249
14.8k
  ptr = source->pub._buffer[0];
250
14.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
251
2.27k
    if (aindex >= 0)
252
224
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
253
2.27k
                    ptr[aindex] = (_JSAMPLE)maxval;)
254
2.05k
    else
255
2.05k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
256
12.6k
  } else {
257
12.6k
    if (aindex >= 0)
258
2.75k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
259
12.6k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
260
9.86k
    else
261
9.86k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
262
12.6k
  }
263
14.8k
  return 1;
264
14.8k
}
265
266
267
METHODDEF(JDIMENSION)
268
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
269
/* This version is for reading text-format PPM files with any maxval and
270
   converting to CMYK */
271
2.97k
{
272
2.97k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
273
2.97k
  FILE *infile = source->pub.input_file;
274
2.97k
  register _JSAMPROW ptr;
275
2.97k
  register _JSAMPLE *rescale = source->rescale;
276
2.97k
  JDIMENSION col;
277
2.97k
  unsigned int maxval = source->maxval;
278
279
2.97k
  ptr = source->pub._buffer[0];
280
2.97k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
281
2.23k
    for (col = cinfo->image_width; col > 0; col--) {
282
1.36k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
283
1.36k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
284
1.36k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
285
1.36k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
286
1.36k
      ptr += 4;
287
1.36k
    }
288
2.10k
  } else {
289
5.13k
    for (col = cinfo->image_width; col > 0; col--) {
290
3.02k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
291
3.02k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
292
3.02k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
293
3.02k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
294
3.02k
                  ptr + 2, ptr + 3);
295
3.02k
      ptr += 4;
296
3.02k
    }
297
2.10k
  }
298
2.97k
  return 1;
299
2.97k
}
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
271
2.97k
{
272
2.97k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
273
2.97k
  FILE *infile = source->pub.input_file;
274
2.97k
  register _JSAMPROW ptr;
275
2.97k
  register _JSAMPLE *rescale = source->rescale;
276
2.97k
  JDIMENSION col;
277
2.97k
  unsigned int maxval = source->maxval;
278
279
2.97k
  ptr = source->pub._buffer[0];
280
2.97k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
281
2.23k
    for (col = cinfo->image_width; col > 0; col--) {
282
1.36k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
283
1.36k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
284
1.36k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
285
1.36k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
286
1.36k
      ptr += 4;
287
1.36k
    }
288
2.10k
  } else {
289
5.13k
    for (col = cinfo->image_width; col > 0; col--) {
290
3.02k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
291
3.02k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
292
3.02k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
293
3.02k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
294
3.02k
                  ptr + 2, ptr + 3);
295
3.02k
      ptr += 4;
296
3.02k
    }
297
2.10k
  }
298
2.97k
  return 1;
299
2.97k
}
300
301
302
METHODDEF(JDIMENSION)
303
get_scaled_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
304
/* This version is for reading raw-byte-format PGM files with any maxval */
305
1.77M
{
306
1.77M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
307
1.77M
  register _JSAMPROW ptr;
308
1.77M
  register unsigned char *bufferptr;
309
1.77M
  register _JSAMPLE *rescale = source->rescale;
310
1.77M
  JDIMENSION col;
311
312
1.77M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
313
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
314
1.77M
  ptr = source->pub._buffer[0];
315
1.77M
  bufferptr = source->iobuffer;
316
8.51M
  for (col = cinfo->image_width; col > 0; col--) {
317
6.74M
    *ptr++ = rescale[*bufferptr++];
318
6.74M
  }
319
1.77M
  return 1;
320
1.77M
}
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
305
1.77M
{
306
1.77M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
307
1.77M
  register _JSAMPROW ptr;
308
1.77M
  register unsigned char *bufferptr;
309
1.77M
  register _JSAMPLE *rescale = source->rescale;
310
1.77M
  JDIMENSION col;
311
312
1.77M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
313
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
314
1.77M
  ptr = source->pub._buffer[0];
315
1.77M
  bufferptr = source->iobuffer;
316
8.51M
  for (col = cinfo->image_width; col > 0; col--) {
317
6.74M
    *ptr++ = rescale[*bufferptr++];
318
6.74M
  }
319
1.77M
  return 1;
320
1.77M
}
321
322
323
METHODDEF(JDIMENSION)
324
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
325
/* This version is for reading raw-byte-format PGM files with any maxval
326
   and converting to extended RGB */
327
8.86M
{
328
8.86M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
329
8.86M
  register _JSAMPROW ptr;
330
8.86M
  register unsigned char *bufferptr;
331
8.86M
  register _JSAMPLE *rescale = source->rescale;
332
8.86M
  JDIMENSION col;
333
8.86M
  unsigned int maxval = source->maxval;
334
8.86M
  register int rindex = rgb_red[cinfo->in_color_space];
335
8.86M
  register int gindex = rgb_green[cinfo->in_color_space];
336
8.86M
  register int bindex = rgb_blue[cinfo->in_color_space];
337
8.86M
  register int aindex = alpha_index[cinfo->in_color_space];
338
8.86M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
339
340
8.86M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
341
285
    ERREXIT(cinfo, JERR_INPUT_EOF);
342
8.86M
  ptr = source->pub._buffer[0];
343
8.86M
  bufferptr = source->iobuffer;
344
8.86M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
345
0
    if (aindex >= 0)
346
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
347
0
    else
348
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
349
8.86M
  } else {
350
8.86M
    if (aindex >= 0)
351
1.77M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
352
8.86M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
353
7.09M
    else
354
7.09M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {})
355
8.86M
  }
356
8.86M
  return 1;
357
8.86M
}
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
327
8.86M
{
328
8.86M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
329
8.86M
  register _JSAMPROW ptr;
330
8.86M
  register unsigned char *bufferptr;
331
8.86M
  register _JSAMPLE *rescale = source->rescale;
332
8.86M
  JDIMENSION col;
333
8.86M
  unsigned int maxval = source->maxval;
334
8.86M
  register int rindex = rgb_red[cinfo->in_color_space];
335
8.86M
  register int gindex = rgb_green[cinfo->in_color_space];
336
8.86M
  register int bindex = rgb_blue[cinfo->in_color_space];
337
8.86M
  register int aindex = alpha_index[cinfo->in_color_space];
338
8.86M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
339
340
8.86M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
341
285
    ERREXIT(cinfo, JERR_INPUT_EOF);
342
8.86M
  ptr = source->pub._buffer[0];
343
8.86M
  bufferptr = source->iobuffer;
344
8.86M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
345
0
    if (aindex >= 0)
346
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
347
0
    else
348
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
349
8.86M
  } else {
350
8.86M
    if (aindex >= 0)
351
1.77M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
352
8.86M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
353
7.09M
    else
354
7.09M
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {})
355
8.86M
  }
356
8.86M
  return 1;
357
8.86M
}
358
359
360
METHODDEF(JDIMENSION)
361
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
362
/* This version is for reading raw-byte-format PGM files with any maxval
363
   and converting to CMYK */
364
1.77M
{
365
1.77M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
366
1.77M
  register _JSAMPROW ptr;
367
1.77M
  register unsigned char *bufferptr;
368
1.77M
  register _JSAMPLE *rescale = source->rescale;
369
1.77M
  JDIMENSION col;
370
1.77M
  unsigned int maxval = source->maxval;
371
372
1.77M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
373
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
374
1.77M
  ptr = source->pub._buffer[0];
375
1.77M
  bufferptr = source->iobuffer;
376
1.77M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
377
0
    for (col = cinfo->image_width; col > 0; col--) {
378
0
      _JSAMPLE gray = *bufferptr++;
379
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
380
0
      ptr += 4;
381
0
    }
382
1.77M
  } else {
383
8.51M
    for (col = cinfo->image_width; col > 0; col--) {
384
6.74M
      _JSAMPLE gray = rescale[*bufferptr++];
385
6.74M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
386
6.74M
                  ptr + 1, ptr + 2, ptr + 3);
387
6.74M
      ptr += 4;
388
6.74M
    }
389
1.77M
  }
390
1.77M
  return 1;
391
1.77M
}
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
364
1.77M
{
365
1.77M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
366
1.77M
  register _JSAMPROW ptr;
367
1.77M
  register unsigned char *bufferptr;
368
1.77M
  register _JSAMPLE *rescale = source->rescale;
369
1.77M
  JDIMENSION col;
370
1.77M
  unsigned int maxval = source->maxval;
371
372
1.77M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
373
57
    ERREXIT(cinfo, JERR_INPUT_EOF);
374
1.77M
  ptr = source->pub._buffer[0];
375
1.77M
  bufferptr = source->iobuffer;
376
1.77M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
377
0
    for (col = cinfo->image_width; col > 0; col--) {
378
0
      _JSAMPLE gray = *bufferptr++;
379
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
380
0
      ptr += 4;
381
0
    }
382
1.77M
  } else {
383
8.51M
    for (col = cinfo->image_width; col > 0; col--) {
384
6.74M
      _JSAMPLE gray = rescale[*bufferptr++];
385
6.74M
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
386
6.74M
                  ptr + 1, ptr + 2, ptr + 3);
387
6.74M
      ptr += 4;
388
6.74M
    }
389
1.77M
  }
390
1.77M
  return 1;
391
1.77M
}
392
393
394
METHODDEF(JDIMENSION)
395
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
396
/* This version is for reading raw-byte-format PPM files with any maxval */
397
62.9k
{
398
62.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
399
62.9k
  register _JSAMPROW ptr;
400
62.9k
  register unsigned char *bufferptr;
401
62.9k
  register _JSAMPLE *rescale = source->rescale;
402
62.9k
  JDIMENSION col;
403
62.9k
  unsigned int maxval = source->maxval;
404
62.9k
  register int rindex = rgb_red[cinfo->in_color_space];
405
62.9k
  register int gindex = rgb_green[cinfo->in_color_space];
406
62.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
407
62.9k
  register int aindex = alpha_index[cinfo->in_color_space];
408
62.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
409
410
62.9k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
411
560
    ERREXIT(cinfo, JERR_INPUT_EOF);
412
62.9k
  ptr = source->pub._buffer[0];
413
62.9k
  bufferptr = source->iobuffer;
414
62.9k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
415
0
    if (aindex >= 0)
416
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
417
0
    else
418
0
      RGB_READ_LOOP(*bufferptr++, {})
419
62.9k
  } else {
420
62.9k
    if (aindex >= 0)
421
12.4k
      RGB_READ_LOOP(rescale[*bufferptr++],
422
62.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
423
50.4k
    else
424
50.4k
      RGB_READ_LOOP(rescale[*bufferptr++], {})
425
62.9k
  }
426
62.9k
  return 1;
427
62.9k
}
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
397
62.9k
{
398
62.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
399
62.9k
  register _JSAMPROW ptr;
400
62.9k
  register unsigned char *bufferptr;
401
62.9k
  register _JSAMPLE *rescale = source->rescale;
402
62.9k
  JDIMENSION col;
403
62.9k
  unsigned int maxval = source->maxval;
404
62.9k
  register int rindex = rgb_red[cinfo->in_color_space];
405
62.9k
  register int gindex = rgb_green[cinfo->in_color_space];
406
62.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
407
62.9k
  register int aindex = alpha_index[cinfo->in_color_space];
408
62.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
409
410
62.9k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
411
560
    ERREXIT(cinfo, JERR_INPUT_EOF);
412
62.9k
  ptr = source->pub._buffer[0];
413
62.9k
  bufferptr = source->iobuffer;
414
62.9k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
415
0
    if (aindex >= 0)
416
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
417
0
    else
418
0
      RGB_READ_LOOP(*bufferptr++, {})
419
62.9k
  } else {
420
62.9k
    if (aindex >= 0)
421
12.4k
      RGB_READ_LOOP(rescale[*bufferptr++],
422
62.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
423
50.4k
    else
424
50.4k
      RGB_READ_LOOP(rescale[*bufferptr++], {})
425
62.9k
  }
426
62.9k
  return 1;
427
62.9k
}
428
429
430
METHODDEF(JDIMENSION)
431
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
432
/* This version is for reading raw-byte-format PPM files with any maxval and
433
   converting to CMYK */
434
12.5k
{
435
12.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
436
12.5k
  register _JSAMPROW ptr;
437
12.5k
  register unsigned char *bufferptr;
438
12.5k
  register _JSAMPLE *rescale = source->rescale;
439
12.5k
  JDIMENSION col;
440
12.5k
  unsigned int maxval = source->maxval;
441
442
12.5k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
443
112
    ERREXIT(cinfo, JERR_INPUT_EOF);
444
12.5k
  ptr = source->pub._buffer[0];
445
12.5k
  bufferptr = source->iobuffer;
446
12.5k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
447
0
    for (col = cinfo->image_width; col > 0; col--) {
448
0
      _JSAMPLE r = *bufferptr++;
449
0
      _JSAMPLE g = *bufferptr++;
450
0
      _JSAMPLE b = *bufferptr++;
451
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
452
0
      ptr += 4;
453
0
    }
454
12.5k
  } else {
455
606k
    for (col = cinfo->image_width; col > 0; col--) {
456
593k
      _JSAMPLE r = rescale[*bufferptr++];
457
593k
      _JSAMPLE g = rescale[*bufferptr++];
458
593k
      _JSAMPLE b = rescale[*bufferptr++];
459
593k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
460
593k
                  ptr + 2, ptr + 3);
461
593k
      ptr += 4;
462
593k
    }
463
12.5k
  }
464
12.5k
  return 1;
465
12.5k
}
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
434
12.5k
{
435
12.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
436
12.5k
  register _JSAMPROW ptr;
437
12.5k
  register unsigned char *bufferptr;
438
12.5k
  register _JSAMPLE *rescale = source->rescale;
439
12.5k
  JDIMENSION col;
440
12.5k
  unsigned int maxval = source->maxval;
441
442
12.5k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
443
112
    ERREXIT(cinfo, JERR_INPUT_EOF);
444
12.5k
  ptr = source->pub._buffer[0];
445
12.5k
  bufferptr = source->iobuffer;
446
12.5k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
447
0
    for (col = cinfo->image_width; col > 0; col--) {
448
0
      _JSAMPLE r = *bufferptr++;
449
0
      _JSAMPLE g = *bufferptr++;
450
0
      _JSAMPLE b = *bufferptr++;
451
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
452
0
      ptr += 4;
453
0
    }
454
12.5k
  } else {
455
606k
    for (col = cinfo->image_width; col > 0; col--) {
456
593k
      _JSAMPLE r = rescale[*bufferptr++];
457
593k
      _JSAMPLE g = rescale[*bufferptr++];
458
593k
      _JSAMPLE b = rescale[*bufferptr++];
459
593k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
460
593k
                  ptr + 2, ptr + 3);
461
593k
      ptr += 4;
462
593k
    }
463
12.5k
  }
464
12.5k
  return 1;
465
12.5k
}
466
467
468
#if BITS_IN_JSAMPLE == 8
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
}
484
485
#endif
486
487
488
METHODDEF(JDIMENSION)
489
get_word_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
490
/* This version is for reading raw-word-format PGM files with any maxval */
491
12.4k
{
492
12.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
493
12.4k
  register _JSAMPROW ptr;
494
12.4k
  register unsigned char *bufferptr;
495
12.4k
  register _JSAMPLE *rescale = source->rescale;
496
12.4k
  JDIMENSION col;
497
12.4k
  unsigned int maxval = source->maxval;
498
499
12.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
500
103
    ERREXIT(cinfo, JERR_INPUT_EOF);
501
12.4k
  ptr = source->pub._buffer[0];
502
12.4k
  bufferptr = source->iobuffer;
503
36.7k
  for (col = cinfo->image_width; col > 0; col--) {
504
24.2k
    register unsigned int temp;
505
24.2k
    temp  = (*bufferptr++) << 8;
506
24.2k
    temp |= (*bufferptr++);
507
24.2k
    if (temp > maxval)
508
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
509
24.2k
    *ptr++ = rescale[temp];
510
24.2k
  }
511
12.4k
  return 1;
512
12.4k
}
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
491
12.4k
{
492
12.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
493
12.4k
  register _JSAMPROW ptr;
494
12.4k
  register unsigned char *bufferptr;
495
12.4k
  register _JSAMPLE *rescale = source->rescale;
496
12.4k
  JDIMENSION col;
497
12.4k
  unsigned int maxval = source->maxval;
498
499
12.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
500
103
    ERREXIT(cinfo, JERR_INPUT_EOF);
501
12.4k
  ptr = source->pub._buffer[0];
502
12.4k
  bufferptr = source->iobuffer;
503
36.7k
  for (col = cinfo->image_width; col > 0; col--) {
504
24.2k
    register unsigned int temp;
505
24.2k
    temp  = (*bufferptr++) << 8;
506
24.2k
    temp |= (*bufferptr++);
507
24.2k
    if (temp > maxval)
508
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
509
24.2k
    *ptr++ = rescale[temp];
510
24.2k
  }
511
12.4k
  return 1;
512
12.4k
}
513
514
515
METHODDEF(JDIMENSION)
516
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
517
/* This version is for reading raw-word-format PGM files with any maxval */
518
62.2k
{
519
62.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
520
62.2k
  register _JSAMPROW ptr;
521
62.2k
  register unsigned char *bufferptr;
522
62.2k
  register _JSAMPLE *rescale = source->rescale;
523
62.2k
  JDIMENSION col;
524
62.2k
  unsigned int maxval = source->maxval;
525
62.2k
  register int rindex = rgb_red[cinfo->in_color_space];
526
62.2k
  register int gindex = rgb_green[cinfo->in_color_space];
527
62.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
528
62.2k
  register int aindex = alpha_index[cinfo->in_color_space];
529
62.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
530
531
62.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
532
515
    ERREXIT(cinfo, JERR_INPUT_EOF);
533
62.2k
  ptr = source->pub._buffer[0];
534
62.2k
  bufferptr = source->iobuffer;
535
183k
  for (col = cinfo->image_width; col > 0; col--) {
536
121k
    register unsigned int temp;
537
121k
    temp  = (*bufferptr++) << 8;
538
121k
    temp |= (*bufferptr++);
539
121k
    if (temp > maxval)
540
180
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
541
121k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
542
121k
    if (aindex >= 0)
543
24.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
544
121k
    ptr += ps;
545
121k
  }
546
62.2k
  return 1;
547
62.2k
}
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
518
62.2k
{
519
62.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
520
62.2k
  register _JSAMPROW ptr;
521
62.2k
  register unsigned char *bufferptr;
522
62.2k
  register _JSAMPLE *rescale = source->rescale;
523
62.2k
  JDIMENSION col;
524
62.2k
  unsigned int maxval = source->maxval;
525
62.2k
  register int rindex = rgb_red[cinfo->in_color_space];
526
62.2k
  register int gindex = rgb_green[cinfo->in_color_space];
527
62.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
528
62.2k
  register int aindex = alpha_index[cinfo->in_color_space];
529
62.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
530
531
62.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
532
515
    ERREXIT(cinfo, JERR_INPUT_EOF);
533
62.2k
  ptr = source->pub._buffer[0];
534
62.2k
  bufferptr = source->iobuffer;
535
183k
  for (col = cinfo->image_width; col > 0; col--) {
536
121k
    register unsigned int temp;
537
121k
    temp  = (*bufferptr++) << 8;
538
121k
    temp |= (*bufferptr++);
539
121k
    if (temp > maxval)
540
180
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
541
121k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
542
121k
    if (aindex >= 0)
543
24.2k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
544
121k
    ptr += ps;
545
121k
  }
546
62.2k
  return 1;
547
62.2k
}
548
549
550
METHODDEF(JDIMENSION)
551
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
552
/* This version is for reading raw-word-format PGM files with any maxval */
553
12.4k
{
554
12.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
555
12.4k
  register _JSAMPROW ptr;
556
12.4k
  register unsigned char *bufferptr;
557
12.4k
  register _JSAMPLE *rescale = source->rescale;
558
12.4k
  JDIMENSION col;
559
12.4k
  unsigned int maxval = source->maxval;
560
561
12.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
562
103
    ERREXIT(cinfo, JERR_INPUT_EOF);
563
12.4k
  ptr = source->pub._buffer[0];
564
12.4k
  bufferptr = source->iobuffer;
565
12.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
566
30.6k
    for (col = cinfo->image_width; col > 0; col--) {
567
19.8k
      register unsigned int gray;
568
19.8k
      gray  = (*bufferptr++) << 8;
569
19.8k
      gray |= (*bufferptr++);
570
19.8k
      if (gray > maxval)
571
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
572
19.8k
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
573
19.8k
                  ptr + 1, ptr + 2, ptr + 3);
574
19.8k
      ptr += 4;
575
19.8k
    }
576
10.7k
  } else {
577
6.10k
    for (col = cinfo->image_width; col > 0; col--) {
578
4.42k
      register unsigned int temp;
579
4.42k
      _JSAMPLE gray;
580
4.42k
      temp  = (*bufferptr++) << 8;
581
4.42k
      temp |= (*bufferptr++);
582
4.42k
      if (temp > maxval)
583
36
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
584
4.42k
      gray = rescale[temp];
585
4.42k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
586
4.42k
                  ptr + 1, ptr + 2, ptr + 3);
587
4.42k
      ptr += 4;
588
4.42k
    }
589
1.68k
  }
590
12.4k
  return 1;
591
12.4k
}
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
553
12.4k
{
554
12.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
555
12.4k
  register _JSAMPROW ptr;
556
12.4k
  register unsigned char *bufferptr;
557
12.4k
  register _JSAMPLE *rescale = source->rescale;
558
12.4k
  JDIMENSION col;
559
12.4k
  unsigned int maxval = source->maxval;
560
561
12.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
562
103
    ERREXIT(cinfo, JERR_INPUT_EOF);
563
12.4k
  ptr = source->pub._buffer[0];
564
12.4k
  bufferptr = source->iobuffer;
565
12.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
566
30.6k
    for (col = cinfo->image_width; col > 0; col--) {
567
19.8k
      register unsigned int gray;
568
19.8k
      gray  = (*bufferptr++) << 8;
569
19.8k
      gray |= (*bufferptr++);
570
19.8k
      if (gray > maxval)
571
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
572
19.8k
      rgb_to_cmyk(maxval, (_JSAMPLE)gray, (_JSAMPLE)gray, (_JSAMPLE)gray, ptr,
573
19.8k
                  ptr + 1, ptr + 2, ptr + 3);
574
19.8k
      ptr += 4;
575
19.8k
    }
576
10.7k
  } else {
577
6.10k
    for (col = cinfo->image_width; col > 0; col--) {
578
4.42k
      register unsigned int temp;
579
4.42k
      _JSAMPLE gray;
580
4.42k
      temp  = (*bufferptr++) << 8;
581
4.42k
      temp |= (*bufferptr++);
582
4.42k
      if (temp > maxval)
583
36
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
584
4.42k
      gray = rescale[temp];
585
4.42k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
586
4.42k
                  ptr + 1, ptr + 2, ptr + 3);
587
4.42k
      ptr += 4;
588
4.42k
    }
589
1.68k
  }
590
12.4k
  return 1;
591
12.4k
}
592
593
594
METHODDEF(JDIMENSION)
595
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
596
/* This version is for reading raw-word-format PPM files with any maxval */
597
14.6k
{
598
14.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
599
14.6k
  register _JSAMPROW ptr;
600
14.6k
  register unsigned char *bufferptr;
601
14.6k
  register _JSAMPLE *rescale = source->rescale;
602
14.6k
  JDIMENSION col;
603
14.6k
  unsigned int maxval = source->maxval;
604
14.6k
  register int rindex = rgb_red[cinfo->in_color_space];
605
14.6k
  register int gindex = rgb_green[cinfo->in_color_space];
606
14.6k
  register int bindex = rgb_blue[cinfo->in_color_space];
607
14.6k
  register int aindex = alpha_index[cinfo->in_color_space];
608
14.6k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
609
610
14.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
611
645
    ERREXIT(cinfo, JERR_INPUT_EOF);
612
14.6k
  ptr = source->pub._buffer[0];
613
14.6k
  bufferptr = source->iobuffer;
614
100k
  for (col = cinfo->image_width; col > 0; col--) {
615
85.9k
    register unsigned int temp;
616
85.9k
    temp  = (*bufferptr++) << 8;
617
85.9k
    temp |= (*bufferptr++);
618
85.9k
    if (temp > maxval)
619
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
620
85.9k
    ptr[rindex] = rescale[temp];
621
85.9k
    temp  = (*bufferptr++) << 8;
622
85.9k
    temp |= (*bufferptr++);
623
85.9k
    if (temp > maxval)
624
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
625
85.9k
    ptr[gindex] = rescale[temp];
626
85.9k
    temp  = (*bufferptr++) << 8;
627
85.9k
    temp |= (*bufferptr++);
628
85.9k
    if (temp > maxval)
629
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
630
85.9k
    ptr[bindex] = rescale[temp];
631
85.9k
    if (aindex >= 0)
632
17.1k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
633
85.9k
    ptr += ps;
634
85.9k
  }
635
14.6k
  return 1;
636
14.6k
}
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
597
14.6k
{
598
14.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
599
14.6k
  register _JSAMPROW ptr;
600
14.6k
  register unsigned char *bufferptr;
601
14.6k
  register _JSAMPLE *rescale = source->rescale;
602
14.6k
  JDIMENSION col;
603
14.6k
  unsigned int maxval = source->maxval;
604
14.6k
  register int rindex = rgb_red[cinfo->in_color_space];
605
14.6k
  register int gindex = rgb_green[cinfo->in_color_space];
606
14.6k
  register int bindex = rgb_blue[cinfo->in_color_space];
607
14.6k
  register int aindex = alpha_index[cinfo->in_color_space];
608
14.6k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
609
610
14.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
611
645
    ERREXIT(cinfo, JERR_INPUT_EOF);
612
14.6k
  ptr = source->pub._buffer[0];
613
14.6k
  bufferptr = source->iobuffer;
614
100k
  for (col = cinfo->image_width; col > 0; col--) {
615
85.9k
    register unsigned int temp;
616
85.9k
    temp  = (*bufferptr++) << 8;
617
85.9k
    temp |= (*bufferptr++);
618
85.9k
    if (temp > maxval)
619
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
620
85.9k
    ptr[rindex] = rescale[temp];
621
85.9k
    temp  = (*bufferptr++) << 8;
622
85.9k
    temp |= (*bufferptr++);
623
85.9k
    if (temp > maxval)
624
120
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
625
85.9k
    ptr[gindex] = rescale[temp];
626
85.9k
    temp  = (*bufferptr++) << 8;
627
85.9k
    temp |= (*bufferptr++);
628
85.9k
    if (temp > maxval)
629
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
630
85.9k
    ptr[bindex] = rescale[temp];
631
85.9k
    if (aindex >= 0)
632
17.1k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
633
85.9k
    ptr += ps;
634
85.9k
  }
635
14.6k
  return 1;
636
14.6k
}
637
638
639
METHODDEF(JDIMENSION)
640
get_word_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
641
/* This version is for reading raw-word-format PPM files with any maxval */
642
2.92k
{
643
2.92k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
644
2.92k
  register _JSAMPROW ptr;
645
2.92k
  register unsigned char *bufferptr;
646
2.92k
  register _JSAMPLE *rescale = source->rescale;
647
2.92k
  JDIMENSION col;
648
2.92k
  unsigned int maxval = source->maxval;
649
650
2.92k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
651
129
    ERREXIT(cinfo, JERR_INPUT_EOF);
652
2.92k
  ptr = source->pub._buffer[0];
653
2.92k
  bufferptr = source->iobuffer;
654
2.92k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
655
15.7k
    for (col = cinfo->image_width; col > 0; col--) {
656
14.5k
      register unsigned int r, g, b;
657
14.5k
      r  = (*bufferptr++) << 8;
658
14.5k
      r |= (*bufferptr++);
659
14.5k
      if (r > maxval)
660
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
661
14.5k
      g  = (*bufferptr++) << 8;
662
14.5k
      g |= (*bufferptr++);
663
14.5k
      if (g > maxval)
664
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
665
14.5k
      b  = (*bufferptr++) << 8;
666
14.5k
      b |= (*bufferptr++);
667
14.5k
      if (b > maxval)
668
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
669
14.5k
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
670
14.5k
                  ptr + 2, ptr + 3);
671
14.5k
      ptr += 4;
672
14.5k
    }
673
1.75k
  } else {
674
4.37k
    for (col = cinfo->image_width; col > 0; col--) {
675
2.61k
      register unsigned int r, g, b;
676
2.61k
      r  = (*bufferptr++) << 8;
677
2.61k
      r |= (*bufferptr++);
678
2.61k
      if (r > maxval)
679
32
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
680
2.61k
      g  = (*bufferptr++) << 8;
681
2.61k
      g |= (*bufferptr++);
682
2.61k
      if (g > maxval)
683
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
684
2.61k
      b  = (*bufferptr++) << 8;
685
2.61k
      b |= (*bufferptr++);
686
2.61k
      if (b > maxval)
687
25
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
688
2.61k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
689
2.61k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
690
2.61k
      ptr += 4;
691
2.61k
    }
692
1.75k
  }
693
2.92k
  return 1;
694
2.92k
}
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
642
2.92k
{
643
2.92k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
644
2.92k
  register _JSAMPROW ptr;
645
2.92k
  register unsigned char *bufferptr;
646
2.92k
  register _JSAMPLE *rescale = source->rescale;
647
2.92k
  JDIMENSION col;
648
2.92k
  unsigned int maxval = source->maxval;
649
650
2.92k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
651
129
    ERREXIT(cinfo, JERR_INPUT_EOF);
652
2.92k
  ptr = source->pub._buffer[0];
653
2.92k
  bufferptr = source->iobuffer;
654
2.92k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
655
15.7k
    for (col = cinfo->image_width; col > 0; col--) {
656
14.5k
      register unsigned int r, g, b;
657
14.5k
      r  = (*bufferptr++) << 8;
658
14.5k
      r |= (*bufferptr++);
659
14.5k
      if (r > maxval)
660
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
661
14.5k
      g  = (*bufferptr++) << 8;
662
14.5k
      g |= (*bufferptr++);
663
14.5k
      if (g > maxval)
664
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
665
14.5k
      b  = (*bufferptr++) << 8;
666
14.5k
      b |= (*bufferptr++);
667
14.5k
      if (b > maxval)
668
0
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
669
14.5k
      rgb_to_cmyk(maxval, (_JSAMPLE)r, (_JSAMPLE)g, (_JSAMPLE)b, ptr, ptr + 1,
670
14.5k
                  ptr + 2, ptr + 3);
671
14.5k
      ptr += 4;
672
14.5k
    }
673
1.75k
  } else {
674
4.37k
    for (col = cinfo->image_width; col > 0; col--) {
675
2.61k
      register unsigned int r, g, b;
676
2.61k
      r  = (*bufferptr++) << 8;
677
2.61k
      r |= (*bufferptr++);
678
2.61k
      if (r > maxval)
679
32
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
680
2.61k
      g  = (*bufferptr++) << 8;
681
2.61k
      g |= (*bufferptr++);
682
2.61k
      if (g > maxval)
683
24
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
684
2.61k
      b  = (*bufferptr++) << 8;
685
2.61k
      b |= (*bufferptr++);
686
2.61k
      if (b > maxval)
687
25
        ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
688
2.61k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, rescale[r], rescale[g],
689
2.61k
                  rescale[b], ptr, ptr + 1, ptr + 2, ptr + 3);
690
2.61k
      ptr += 4;
691
2.61k
    }
692
1.75k
  }
693
2.92k
  return 1;
694
2.92k
}
695
696
697
/*
698
 * Read the file header; return image size and component count.
699
 */
700
701
METHODDEF(void)
702
start_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
703
11.5k
{
704
11.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
705
11.5k
  int c;
706
11.5k
  unsigned int w, h, maxval;
707
11.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
708
709
11.5k
  if (getc(source->pub.input_file) != 'P')
710
0
    ERREXIT(cinfo, JERR_PPM_NOT);
711
712
11.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
713
714
  /* detect unsupported variants (ie, PBM) before trying to read header */
715
11.5k
  switch (c) {
716
1.54k
  case '2':                     /* it's a text-format PGM file */
717
3.61k
  case '3':                     /* it's a text-format PPM file */
718
8.39k
  case '5':                     /* it's a raw-format PGM file */
719
11.5k
  case '6':                     /* it's a raw-format PPM file */
720
11.5k
    break;
721
21
  default:
722
21
    ERREXIT(cinfo, JERR_PPM_NOT);
723
21
    break;
724
11.5k
  }
725
726
  /* fetch the remaining header info */
727
11.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
728
11.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
729
11.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
731
11.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
732
35
    ERREXIT(cinfo, JERR_PPM_NOT);
733
11.5k
  if (w > JPEG_MAX_DIMENSION || h > JPEG_MAX_DIMENSION)
734
28
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
735
11.5k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
252
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
11.5k
  cinfo->image_width = (JDIMENSION)w;
739
11.5k
  cinfo->image_height = (JDIMENSION)h;
740
11.5k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
11.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
11.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
11.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
11.5k
  switch (c) {
748
1.27k
  case '2':                     /* it's a text-format PGM file */
749
1.27k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
1.27k
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
1.27k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
1.27k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
182
      source->pub.get_pixel_rows = get_text_gray_row;
755
1.09k
    else if (IsExtRGB(cinfo->in_color_space))
756
910
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
182
    else if (cinfo->in_color_space == JCS_CMYK)
758
182
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
1.27k
    need_iobuffer = FALSE;
762
1.27k
    break;
763
764
1.63k
  case '3':                     /* it's a text-format PPM file */
765
1.63k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.63k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.63k
    if (IsExtRGB(cinfo->in_color_space))
769
1.17k
      source->pub.get_pixel_rows = get_text_rgb_row;
770
468
    else if (cinfo->in_color_space == JCS_CMYK)
771
234
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
234
    else
773
234
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.63k
    need_iobuffer = FALSE;
775
1.63k
    break;
776
777
4.36k
  case '5':                     /* it's a raw-format PGM file */
778
4.36k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
4.36k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
4.36k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
4.36k
    if (maxval > 255) {
783
1.24k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
178
        source->pub.get_pixel_rows = get_word_gray_row;
785
1.06k
      else if (IsExtRGB(cinfo->in_color_space))
786
890
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
178
      else if (cinfo->in_color_space == JCS_CMYK)
788
178
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
#if BITS_IN_JSAMPLE == 8
792
0
    } else if (maxval <= _MAXJSAMPLE &&
793
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
794
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
795
0
      source->pub.get_pixel_rows = get_raw_row;
796
0
      use_raw_buffer = TRUE;
797
0
      need_rescale = FALSE;
798
#endif
799
3.11k
    } else {
800
3.11k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
801
445
        source->pub.get_pixel_rows = get_scaled_gray_row;
802
2.67k
      else if (IsExtRGB(cinfo->in_color_space))
803
2.22k
        source->pub.get_pixel_rows = get_gray_rgb_row;
804
445
      else if (cinfo->in_color_space == JCS_CMYK)
805
445
        source->pub.get_pixel_rows = get_gray_cmyk_row;
806
0
      else
807
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
808
3.11k
    }
809
4.36k
    break;
810
811
2.78k
  case '6':                     /* it's a raw-format PPM file */
812
2.78k
    if (cinfo->in_color_space == JCS_UNKNOWN)
813
0
      cinfo->in_color_space = JCS_EXT_RGB;
814
2.78k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
815
2.78k
    if (maxval > 255) {
816
1.65k
      if (IsExtRGB(cinfo->in_color_space))
817
1.18k
        source->pub.get_pixel_rows = get_word_rgb_row;
818
474
      else if (cinfo->in_color_space == JCS_CMYK)
819
237
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
820
237
      else
821
237
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
822
#if BITS_IN_JSAMPLE == 8
823
0
    } else if (maxval <= _MAXJSAMPLE &&
824
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
825
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
826
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
827
0
                cinfo->in_color_space == JCS_RGB)) {
828
#else
829
               cinfo->in_color_space == JCS_EXT_RGB) {
830
#endif
831
0
      source->pub.get_pixel_rows = get_raw_row;
832
0
      use_raw_buffer = TRUE;
833
0
      need_rescale = FALSE;
834
#endif
835
1.12k
    } else {
836
1.12k
      if (IsExtRGB(cinfo->in_color_space))
837
805
        source->pub.get_pixel_rows = get_rgb_row;
838
322
      else if (cinfo->in_color_space == JCS_CMYK)
839
161
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
840
161
      else
841
161
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
842
1.12k
    }
843
2.78k
    break;
844
11.5k
  }
845
846
9.42k
  if (IsExtRGB(cinfo->in_color_space))
847
7.18k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
848
2.24k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
849
805
    cinfo->input_components = 1;
850
1.43k
  else if (cinfo->in_color_space == JCS_CMYK)
851
1.43k
    cinfo->input_components = 4;
852
853
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
854
9.42k
  if (need_iobuffer) {
855
6.74k
    if (c == '6')
856
2.38k
      source->buffer_width = (size_t)w * 3 * (maxval <= 255 ? 1 : 2);
857
4.36k
    else
858
4.36k
      source->buffer_width = (size_t)w * (maxval <= 255 ? 1 : 2);
859
6.74k
    source->iobuffer = (unsigned char *)
860
6.74k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
861
6.74k
                                  source->buffer_width);
862
6.74k
  }
863
864
  /* Create compressor input buffer. */
865
9.42k
  if (use_raw_buffer) {
866
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
867
    /* Synthesize a _JSAMPARRAY pointer structure */
868
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
869
0
    source->pub._buffer = &source->pixrow;
870
0
    source->pub.buffer_height = 1;
871
9.42k
  } else {
872
    /* Need to translate anyway, so make a separate sample buffer. */
873
9.42k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
874
9.42k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
875
9.42k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
876
9.42k
    source->pub.buffer_height = 1;
877
9.42k
  }
878
879
  /* Compute the rescaling array if required. */
880
9.42k
  if (need_rescale) {
881
9.42k
    size_t val, half_maxval;
882
883
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
884
9.42k
    source->rescale = (_JSAMPLE *)
885
9.42k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
886
9.42k
                                  (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
887
9.42k
    memset(source->rescale, 0, (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
888
9.42k
    half_maxval = (size_t)maxval / 2;
889
132M
    for (val = 0; val <= (size_t)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
132M
      source->rescale[val] =
892
132M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
132M
                   maxval);
894
132M
    }
895
9.42k
  }
896
9.42k
}
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
703
11.5k
{
704
11.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
705
11.5k
  int c;
706
11.5k
  unsigned int w, h, maxval;
707
11.5k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
708
709
11.5k
  if (getc(source->pub.input_file) != 'P')
710
0
    ERREXIT(cinfo, JERR_PPM_NOT);
711
712
11.5k
  c = getc(source->pub.input_file); /* subformat discriminator character */
713
714
  /* detect unsupported variants (ie, PBM) before trying to read header */
715
11.5k
  switch (c) {
716
1.54k
  case '2':                     /* it's a text-format PGM file */
717
3.61k
  case '3':                     /* it's a text-format PPM file */
718
8.39k
  case '5':                     /* it's a raw-format PGM file */
719
11.5k
  case '6':                     /* it's a raw-format PPM file */
720
11.5k
    break;
721
21
  default:
722
21
    ERREXIT(cinfo, JERR_PPM_NOT);
723
21
    break;
724
11.5k
  }
725
726
  /* fetch the remaining header info */
727
11.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
728
11.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
729
11.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
730
731
11.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
732
35
    ERREXIT(cinfo, JERR_PPM_NOT);
733
11.5k
  if (w > JPEG_MAX_DIMENSION || h > JPEG_MAX_DIMENSION)
734
28
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
735
11.5k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
736
252
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
737
738
11.5k
  cinfo->image_width = (JDIMENSION)w;
739
11.5k
  cinfo->image_height = (JDIMENSION)h;
740
11.5k
  source->maxval = maxval;
741
742
  /* initialize flags to most common settings */
743
11.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
744
11.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
745
11.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
746
747
11.5k
  switch (c) {
748
1.27k
  case '2':                     /* it's a text-format PGM file */
749
1.27k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
750
1.27k
        cinfo->in_color_space == JCS_RGB)
751
0
      cinfo->in_color_space = JCS_GRAYSCALE;
752
1.27k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
753
1.27k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
754
182
      source->pub.get_pixel_rows = get_text_gray_row;
755
1.09k
    else if (IsExtRGB(cinfo->in_color_space))
756
910
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
757
182
    else if (cinfo->in_color_space == JCS_CMYK)
758
182
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
759
0
    else
760
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
761
1.27k
    need_iobuffer = FALSE;
762
1.27k
    break;
763
764
1.63k
  case '3':                     /* it's a text-format PPM file */
765
1.63k
    if (cinfo->in_color_space == JCS_UNKNOWN)
766
0
      cinfo->in_color_space = JCS_EXT_RGB;
767
1.63k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
768
1.63k
    if (IsExtRGB(cinfo->in_color_space))
769
1.17k
      source->pub.get_pixel_rows = get_text_rgb_row;
770
468
    else if (cinfo->in_color_space == JCS_CMYK)
771
234
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
772
234
    else
773
234
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
774
1.63k
    need_iobuffer = FALSE;
775
1.63k
    break;
776
777
4.36k
  case '5':                     /* it's a raw-format PGM file */
778
4.36k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
779
4.36k
        cinfo->in_color_space == JCS_RGB)
780
0
      cinfo->in_color_space = JCS_GRAYSCALE;
781
4.36k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
782
4.36k
    if (maxval > 255) {
783
1.24k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
784
178
        source->pub.get_pixel_rows = get_word_gray_row;
785
1.06k
      else if (IsExtRGB(cinfo->in_color_space))
786
890
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
787
178
      else if (cinfo->in_color_space == JCS_CMYK)
788
178
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
789
0
      else
790
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
791
#if BITS_IN_JSAMPLE == 8
792
    } else if (maxval <= _MAXJSAMPLE &&
793
               maxval == ((1U << cinfo->data_precision) - 1U) &&
794
               cinfo->in_color_space == JCS_GRAYSCALE) {
795
      source->pub.get_pixel_rows = get_raw_row;
796
      use_raw_buffer = TRUE;
797
      need_rescale = FALSE;
798
#endif
799
3.11k
    } else {
800
3.11k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
801
445
        source->pub.get_pixel_rows = get_scaled_gray_row;
802
2.67k
      else if (IsExtRGB(cinfo->in_color_space))
803
2.22k
        source->pub.get_pixel_rows = get_gray_rgb_row;
804
445
      else if (cinfo->in_color_space == JCS_CMYK)
805
445
        source->pub.get_pixel_rows = get_gray_cmyk_row;
806
0
      else
807
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
808
3.11k
    }
809
4.36k
    break;
810
811
2.78k
  case '6':                     /* it's a raw-format PPM file */
812
2.78k
    if (cinfo->in_color_space == JCS_UNKNOWN)
813
0
      cinfo->in_color_space = JCS_EXT_RGB;
814
2.78k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
815
2.78k
    if (maxval > 255) {
816
1.65k
      if (IsExtRGB(cinfo->in_color_space))
817
1.18k
        source->pub.get_pixel_rows = get_word_rgb_row;
818
474
      else if (cinfo->in_color_space == JCS_CMYK)
819
237
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
820
237
      else
821
237
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
822
#if BITS_IN_JSAMPLE == 8
823
    } else if (maxval <= _MAXJSAMPLE &&
824
               maxval == ((1U << cinfo->data_precision) - 1U) &&
825
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
826
               (cinfo->in_color_space == JCS_EXT_RGB ||
827
                cinfo->in_color_space == JCS_RGB)) {
828
#else
829
               cinfo->in_color_space == JCS_EXT_RGB) {
830
#endif
831
      source->pub.get_pixel_rows = get_raw_row;
832
      use_raw_buffer = TRUE;
833
      need_rescale = FALSE;
834
#endif
835
1.65k
    } else {
836
1.12k
      if (IsExtRGB(cinfo->in_color_space))
837
805
        source->pub.get_pixel_rows = get_rgb_row;
838
322
      else if (cinfo->in_color_space == JCS_CMYK)
839
161
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
840
161
      else
841
161
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
842
1.12k
    }
843
2.78k
    break;
844
11.5k
  }
845
846
9.42k
  if (IsExtRGB(cinfo->in_color_space))
847
7.18k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
848
2.24k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
849
805
    cinfo->input_components = 1;
850
1.43k
  else if (cinfo->in_color_space == JCS_CMYK)
851
1.43k
    cinfo->input_components = 4;
852
853
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
854
9.42k
  if (need_iobuffer) {
855
6.74k
    if (c == '6')
856
2.38k
      source->buffer_width = (size_t)w * 3 * (maxval <= 255 ? 1 : 2);
857
4.36k
    else
858
4.36k
      source->buffer_width = (size_t)w * (maxval <= 255 ? 1 : 2);
859
6.74k
    source->iobuffer = (unsigned char *)
860
6.74k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
861
6.74k
                                  source->buffer_width);
862
6.74k
  }
863
864
  /* Create compressor input buffer. */
865
9.42k
  if (use_raw_buffer) {
866
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
867
    /* Synthesize a _JSAMPARRAY pointer structure */
868
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
869
0
    source->pub._buffer = &source->pixrow;
870
0
    source->pub.buffer_height = 1;
871
9.42k
  } else {
872
    /* Need to translate anyway, so make a separate sample buffer. */
873
9.42k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
874
9.42k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
875
9.42k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
876
9.42k
    source->pub.buffer_height = 1;
877
9.42k
  }
878
879
  /* Compute the rescaling array if required. */
880
9.42k
  if (need_rescale) {
881
9.42k
    size_t val, half_maxval;
882
883
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
884
9.42k
    source->rescale = (_JSAMPLE *)
885
9.42k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
886
9.42k
                                  (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
887
9.42k
    memset(source->rescale, 0, (MAX(maxval, 255) + 1L) * sizeof(_JSAMPLE));
888
9.42k
    half_maxval = (size_t)maxval / 2;
889
132M
    for (val = 0; val <= (size_t)maxval; val++) {
890
      /* The multiplication here must be done in 32 bits to avoid overflow */
891
132M
      source->rescale[val] =
892
132M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
893
132M
                   maxval);
894
132M
    }
895
9.42k
  }
896
9.42k
}
897
898
899
METHODDEF(boolean)
900
read_icc_profile_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo,
901
                     JOCTET **icc_data_ptr, unsigned int *icc_data_len)
902
0
{
903
0
  return FALSE;
904
0
}
Unexecuted instantiation: rdppm-8.c:read_icc_profile_ppm
Unexecuted instantiation: rdppm-12.c:read_icc_profile_ppm
Unexecuted instantiation: rdppm-16.c:read_icc_profile_ppm
905
906
907
/*
908
 * Finish up at the end of the file.
909
 */
910
911
METHODDEF(void)
912
finish_input_ppm(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
913
11.5k
{
914
  /* no work */
915
11.5k
}
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
913
11.5k
{
914
  /* no work */
915
11.5k
}
916
917
918
/*
919
 * The module selection routine for PPM format input.
920
 */
921
922
GLOBAL(cjpeg_source_ptr)
923
_jinit_read_ppm(j_compress_ptr cinfo)
924
11.5k
{
925
11.5k
  ppm_source_ptr source;
926
927
#if BITS_IN_JSAMPLE == 8
928
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
929
#else
930
11.5k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
931
11.5k
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
932
0
#endif
933
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
934
935
  /* Create module interface object */
936
11.5k
  source = (ppm_source_ptr)
937
11.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
938
11.5k
                                sizeof(ppm_source_struct));
939
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
940
11.5k
  source->pub.start_input = start_input_ppm;
941
11.5k
  source->pub.read_icc_profile = read_icc_profile_ppm;
942
11.5k
  source->pub.finish_input = finish_input_ppm;
943
11.5k
  source->pub.max_pixels = 0;
944
945
11.5k
  return (cjpeg_source_ptr)source;
946
11.5k
}
Unexecuted instantiation: jinit_read_ppm
Unexecuted instantiation: j12init_read_ppm
j16init_read_ppm
Line
Count
Source
924
11.5k
{
925
11.5k
  ppm_source_ptr source;
926
927
#if BITS_IN_JSAMPLE == 8
928
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
929
#else
930
11.5k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
931
11.5k
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
932
0
#endif
933
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
934
935
  /* Create module interface object */
936
11.5k
  source = (ppm_source_ptr)
937
11.5k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
938
11.5k
                                sizeof(ppm_source_struct));
939
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
940
11.5k
  source->pub.start_input = start_input_ppm;
941
11.5k
  source->pub.read_icc_profile = read_icc_profile_ppm;
942
11.5k
  source->pub.finish_input = finish_input_ppm;
943
11.5k
  source->pub.max_pixels = 0;
944
945
11.5k
  return (cjpeg_source_ptr)source;
946
11.5k
}
947
948
#endif /* defined(PPM_SUPPORTED) &&
949
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */