Coverage Report

Created: 2025-11-24 06:36

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
15.4M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
13.8M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
53
54
static int alpha_index[JPEG_NUMCS] = {
55
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
56
};
57
58
59
/* Private version of data source object */
60
61
typedef struct {
62
  struct cjpeg_source_struct pub; /* public fields */
63
64
  /* Usually these two pointers point to the same place: */
65
  U_CHAR *iobuffer;             /* fread's I/O buffer */
66
  _JSAMPROW pixrow;             /* compressor input buffer */
67
  size_t buffer_width;          /* width of I/O buffer */
68
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
69
  unsigned int maxval;
70
} ppm_source_struct;
71
72
typedef ppm_source_struct *ppm_source_ptr;
73
74
75
LOCAL(int)
76
pbm_getc(FILE *infile)
77
/* Read next char, skipping over any comments */
78
/* A comment/newline sequence is returned as a newline */
79
300k
{
80
300k
  register int ch;
81
82
300k
  ch = getc(infile);
83
300k
  if (ch == '#') {
84
73.2k
    do {
85
73.2k
      ch = getc(infile);
86
73.2k
    } while (ch != '\n' && ch != EOF);
87
3.98k
  }
88
300k
  return ch;
89
300k
}
Unexecuted instantiation: rdppm-8.c:pbm_getc
rdppm-12.c:pbm_getc
Line
Count
Source
79
300k
{
80
300k
  register int ch;
81
82
300k
  ch = getc(infile);
83
300k
  if (ch == '#') {
84
73.2k
    do {
85
73.2k
      ch = getc(infile);
86
73.2k
    } while (ch != '\n' && ch != EOF);
87
3.98k
  }
88
300k
  return ch;
89
300k
}
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
123k
{
99
123k
  register int ch;
100
123k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
132k
  do {
104
132k
    ch = pbm_getc(infile);
105
132k
    if (ch == EOF)
106
2.93k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
132k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
123k
  if (ch < '0' || ch > '9')
110
257
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
123k
  val = ch - '0';
113
171k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
47.4k
    val *= 10;
115
47.4k
    val += ch - '0';
116
47.4k
    if (val > maxval)
117
126
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
47.4k
  }
119
120
123k
  return val;
121
123k
}
Unexecuted instantiation: rdppm-8.c:read_pbm_integer
rdppm-12.c:read_pbm_integer
Line
Count
Source
98
123k
{
99
123k
  register int ch;
100
123k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
132k
  do {
104
132k
    ch = pbm_getc(infile);
105
132k
    if (ch == EOF)
106
2.93k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
132k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
123k
  if (ch < '0' || ch > '9')
110
257
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
123k
  val = ch - '0';
113
171k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
47.4k
    val *= 10;
115
47.4k
    val += ch - '0';
116
47.4k
    if (val > maxval)
117
126
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
47.4k
  }
119
120
123k
  return val;
121
123k
}
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.41k
{
140
1.41k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.41k
  FILE *infile = source->pub.input_file;
142
1.41k
  register _JSAMPROW ptr;
143
1.41k
  register _JSAMPLE *rescale = source->rescale;
144
1.41k
  JDIMENSION col;
145
1.41k
  unsigned int maxval = source->maxval;
146
147
1.41k
  ptr = source->pub._buffer[0];
148
4.26k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.85k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.85k
  }
151
1.41k
  return 1;
152
1.41k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_row
rdppm-12.c:get_text_gray_row
Line
Count
Source
139
1.41k
{
140
1.41k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.41k
  FILE *infile = source->pub.input_file;
142
1.41k
  register _JSAMPROW ptr;
143
1.41k
  register _JSAMPLE *rescale = source->rescale;
144
1.41k
  JDIMENSION col;
145
1.41k
  unsigned int maxval = source->maxval;
146
147
1.41k
  ptr = source->pub._buffer[0];
148
4.26k
  for (col = cinfo->image_width; col > 0; col--) {
149
2.85k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
2.85k
  }
151
1.41k
  return 1;
152
1.41k
}
Unexecuted instantiation: rdppm-16.c:get_text_gray_row
153
154
155
9.53M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
26.7M
  for (col = cinfo->image_width; col > 0; col--) { \
157
17.1M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
17.1M
    alpha_set_op \
159
17.1M
    ptr += ps; \
160
17.1M
  } \
161
9.53M
}
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
7.06k
{
168
7.06k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
7.06k
  FILE *infile = source->pub.input_file;
170
7.06k
  register _JSAMPROW ptr;
171
7.06k
  register _JSAMPLE *rescale = source->rescale;
172
7.06k
  JDIMENSION col;
173
7.06k
  unsigned int maxval = source->maxval;
174
7.06k
  register int rindex = rgb_red[cinfo->in_color_space];
175
7.06k
  register int gindex = rgb_green[cinfo->in_color_space];
176
7.06k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
7.06k
  register int aindex = alpha_index[cinfo->in_color_space];
178
7.06k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
7.06k
  ptr = source->pub._buffer[0];
181
7.06k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.74k
    if (aindex >= 0)
183
91
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.74k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.65k
    else
186
1.65k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.31k
  } else {
188
5.31k
    if (aindex >= 0)
189
1.32k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.31k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
3.99k
    else
192
3.99k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.31k
  }
194
7.06k
  return 1;
195
7.06k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_rgb_row
rdppm-12.c:get_text_gray_rgb_row
Line
Count
Source
167
7.06k
{
168
7.06k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
7.06k
  FILE *infile = source->pub.input_file;
170
7.06k
  register _JSAMPROW ptr;
171
7.06k
  register _JSAMPLE *rescale = source->rescale;
172
7.06k
  JDIMENSION col;
173
7.06k
  unsigned int maxval = source->maxval;
174
7.06k
  register int rindex = rgb_red[cinfo->in_color_space];
175
7.06k
  register int gindex = rgb_green[cinfo->in_color_space];
176
7.06k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
7.06k
  register int aindex = alpha_index[cinfo->in_color_space];
178
7.06k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
7.06k
  ptr = source->pub._buffer[0];
181
7.06k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.74k
    if (aindex >= 0)
183
91
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.74k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.65k
    else
186
1.65k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
5.31k
  } else {
188
5.31k
    if (aindex >= 0)
189
1.32k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
5.31k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
3.99k
    else
192
3.99k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
5.31k
  }
194
7.06k
  return 1;
195
7.06k
}
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.41k
{
203
1.41k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.41k
  FILE *infile = source->pub.input_file;
205
1.41k
  register _JSAMPROW ptr;
206
1.41k
  register _JSAMPLE *rescale = source->rescale;
207
1.41k
  JDIMENSION col;
208
1.41k
  unsigned int maxval = source->maxval;
209
210
1.41k
  ptr = source->pub._buffer[0];
211
1.41k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
2.15k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.32k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.32k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.32k
      ptr += 4;
216
1.32k
    }
217
825
  } else {
218
2.11k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.52k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.52k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
1.52k
      ptr += 4;
222
1.52k
    }
223
588
  }
224
1.41k
  return 1;
225
1.41k
}
Unexecuted instantiation: rdppm-8.c:get_text_gray_cmyk_row
rdppm-12.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.41k
{
203
1.41k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.41k
  FILE *infile = source->pub.input_file;
205
1.41k
  register _JSAMPROW ptr;
206
1.41k
  register _JSAMPLE *rescale = source->rescale;
207
1.41k
  JDIMENSION col;
208
1.41k
  unsigned int maxval = source->maxval;
209
210
1.41k
  ptr = source->pub._buffer[0];
211
1.41k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
2.15k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.32k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.32k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.32k
      ptr += 4;
216
1.32k
    }
217
825
  } else {
218
2.11k
    for (col = cinfo->image_width; col > 0; col--) {
219
1.52k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
1.52k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
1.52k
      ptr += 4;
222
1.52k
    }
223
588
  }
224
1.41k
  return 1;
225
1.41k
}
Unexecuted instantiation: rdppm-16.c:get_text_gray_cmyk_row
226
227
228
351k
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
5.42M
  for (col = cinfo->image_width; col > 0; col--) { \
230
5.07M
    ptr[rindex] = read_op; \
231
5.07M
    ptr[gindex] = read_op; \
232
5.07M
    ptr[bindex] = read_op; \
233
5.07M
    alpha_set_op \
234
5.07M
    ptr += ps; \
235
5.07M
  } \
236
351k
}
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
11.1k
{
242
11.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
11.1k
  FILE *infile = source->pub.input_file;
244
11.1k
  register _JSAMPROW ptr;
245
11.1k
  register _JSAMPLE *rescale = source->rescale;
246
11.1k
  JDIMENSION col;
247
11.1k
  unsigned int maxval = source->maxval;
248
11.1k
  register int rindex = rgb_red[cinfo->in_color_space];
249
11.1k
  register int gindex = rgb_green[cinfo->in_color_space];
250
11.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
11.1k
  register int aindex = alpha_index[cinfo->in_color_space];
252
11.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
11.1k
  ptr = source->pub._buffer[0];
255
11.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
1.66k
    if (aindex >= 0)
257
105
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
1.66k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
1.56k
    else
260
1.56k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
9.45k
  } else {
262
9.45k
    if (aindex >= 0)
263
2.12k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
9.45k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
7.33k
    else
266
7.33k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
9.45k
  }
268
11.1k
  return 1;
269
11.1k
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_row
rdppm-12.c:get_text_rgb_row
Line
Count
Source
241
11.1k
{
242
11.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
11.1k
  FILE *infile = source->pub.input_file;
244
11.1k
  register _JSAMPROW ptr;
245
11.1k
  register _JSAMPLE *rescale = source->rescale;
246
11.1k
  JDIMENSION col;
247
11.1k
  unsigned int maxval = source->maxval;
248
11.1k
  register int rindex = rgb_red[cinfo->in_color_space];
249
11.1k
  register int gindex = rgb_green[cinfo->in_color_space];
250
11.1k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
11.1k
  register int aindex = alpha_index[cinfo->in_color_space];
252
11.1k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
11.1k
  ptr = source->pub._buffer[0];
255
11.1k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
1.66k
    if (aindex >= 0)
257
105
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
1.66k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
1.56k
    else
260
1.56k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
9.45k
  } else {
262
9.45k
    if (aindex >= 0)
263
2.12k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
9.45k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
7.33k
    else
266
7.33k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
9.45k
  }
268
11.1k
  return 1;
269
11.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.22k
{
277
2.22k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.22k
  FILE *infile = source->pub.input_file;
279
2.22k
  register _JSAMPROW ptr;
280
2.22k
  register _JSAMPLE *rescale = source->rescale;
281
2.22k
  JDIMENSION col;
282
2.22k
  unsigned int maxval = source->maxval;
283
284
2.22k
  ptr = source->pub._buffer[0];
285
2.22k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.41k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.63k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.63k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.63k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.63k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.63k
      ptr += 4;
292
1.63k
    }
293
1.44k
  } else {
294
4.03k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.58k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.58k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.58k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.58k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.58k
      ptr += 4;
300
2.58k
    }
301
1.44k
  }
302
2.22k
  return 1;
303
2.22k
}
Unexecuted instantiation: rdppm-8.c:get_text_rgb_cmyk_row
rdppm-12.c:get_text_rgb_cmyk_row
Line
Count
Source
276
2.22k
{
277
2.22k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.22k
  FILE *infile = source->pub.input_file;
279
2.22k
  register _JSAMPROW ptr;
280
2.22k
  register _JSAMPLE *rescale = source->rescale;
281
2.22k
  JDIMENSION col;
282
2.22k
  unsigned int maxval = source->maxval;
283
284
2.22k
  ptr = source->pub._buffer[0];
285
2.22k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.41k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.63k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.63k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.63k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.63k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.63k
      ptr += 4;
292
1.63k
    }
293
1.44k
  } else {
294
4.03k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.58k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.58k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.58k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.58k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.58k
      ptr += 4;
300
2.58k
    }
301
1.44k
  }
302
2.22k
  return 1;
303
2.22k
}
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
1.90M
{
310
1.90M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
1.90M
  register _JSAMPROW ptr;
312
1.90M
  register U_CHAR *bufferptr;
313
1.90M
  register _JSAMPLE *rescale = source->rescale;
314
1.90M
  JDIMENSION col;
315
316
1.90M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
59
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
1.90M
  ptr = source->pub._buffer[0];
319
1.90M
  bufferptr = source->iobuffer;
320
5.34M
  for (col = cinfo->image_width; col > 0; col--) {
321
3.43M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
3.43M
  }
323
1.90M
  return 1;
324
1.90M
}
Unexecuted instantiation: rdppm-8.c:get_scaled_gray_row
rdppm-12.c:get_scaled_gray_row
Line
Count
Source
309
1.90M
{
310
1.90M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
1.90M
  register _JSAMPROW ptr;
312
1.90M
  register U_CHAR *bufferptr;
313
1.90M
  register _JSAMPLE *rescale = source->rescale;
314
1.90M
  JDIMENSION col;
315
316
1.90M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
59
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
1.90M
  ptr = source->pub._buffer[0];
319
1.90M
  bufferptr = source->iobuffer;
320
5.34M
  for (col = cinfo->image_width; col > 0; col--) {
321
3.43M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
3.43M
  }
323
1.90M
  return 1;
324
1.90M
}
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
9.52M
{
332
9.52M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
9.52M
  register _JSAMPROW ptr;
334
9.52M
  register U_CHAR *bufferptr;
335
9.52M
  register _JSAMPLE *rescale = source->rescale;
336
9.52M
  JDIMENSION col;
337
9.52M
  unsigned int maxval = source->maxval;
338
9.52M
  register int rindex = rgb_red[cinfo->in_color_space];
339
9.52M
  register int gindex = rgb_green[cinfo->in_color_space];
340
9.52M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
9.52M
  register int aindex = alpha_index[cinfo->in_color_space];
342
9.52M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
9.52M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
295
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
9.52M
  ptr = source->pub._buffer[0];
347
9.52M
  bufferptr = source->iobuffer;
348
9.52M
  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
9.52M
  } else {
354
9.52M
    if (aindex >= 0)
355
1.90M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
9.52M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
7.62M
    else
358
7.62M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
9.52M
  }
360
9.52M
  return 1;
361
9.52M
}
Unexecuted instantiation: rdppm-8.c:get_gray_rgb_row
rdppm-12.c:get_gray_rgb_row
Line
Count
Source
331
9.52M
{
332
9.52M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
9.52M
  register _JSAMPROW ptr;
334
9.52M
  register U_CHAR *bufferptr;
335
9.52M
  register _JSAMPLE *rescale = source->rescale;
336
9.52M
  JDIMENSION col;
337
9.52M
  unsigned int maxval = source->maxval;
338
9.52M
  register int rindex = rgb_red[cinfo->in_color_space];
339
9.52M
  register int gindex = rgb_green[cinfo->in_color_space];
340
9.52M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
9.52M
  register int aindex = alpha_index[cinfo->in_color_space];
342
9.52M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
9.52M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
295
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
9.52M
  ptr = source->pub._buffer[0];
347
9.52M
  bufferptr = source->iobuffer;
348
9.52M
  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
9.52M
  } else {
354
9.52M
    if (aindex >= 0)
355
1.90M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
9.52M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
7.62M
    else
358
7.62M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
9.52M
  }
360
9.52M
  return 1;
361
9.52M
}
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
1.90M
{
369
1.90M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
1.90M
  register _JSAMPROW ptr;
371
1.90M
  register U_CHAR *bufferptr;
372
1.90M
  register _JSAMPLE *rescale = source->rescale;
373
1.90M
  JDIMENSION col;
374
1.90M
  unsigned int maxval = source->maxval;
375
376
1.90M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
59
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
1.90M
  ptr = source->pub._buffer[0];
379
1.90M
  bufferptr = source->iobuffer;
380
1.90M
  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
1.90M
  } else {
387
5.34M
    for (col = cinfo->image_width; col > 0; col--) {
388
3.43M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
3.43M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
3.43M
      ptr += 4;
391
3.43M
    }
392
1.90M
  }
393
1.90M
  return 1;
394
1.90M
}
Unexecuted instantiation: rdppm-8.c:get_gray_cmyk_row
rdppm-12.c:get_gray_cmyk_row
Line
Count
Source
368
1.90M
{
369
1.90M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
1.90M
  register _JSAMPROW ptr;
371
1.90M
  register U_CHAR *bufferptr;
372
1.90M
  register _JSAMPLE *rescale = source->rescale;
373
1.90M
  JDIMENSION col;
374
1.90M
  unsigned int maxval = source->maxval;
375
376
1.90M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
59
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
1.90M
  ptr = source->pub._buffer[0];
379
1.90M
  bufferptr = source->iobuffer;
380
1.90M
  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
1.90M
  } else {
387
5.34M
    for (col = cinfo->image_width; col > 0; col--) {
388
3.43M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
3.43M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
3.43M
      ptr += 4;
391
3.43M
    }
392
1.90M
  }
393
1.90M
  return 1;
394
1.90M
}
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
340k
{
401
340k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
340k
  register _JSAMPROW ptr;
403
340k
  register U_CHAR *bufferptr;
404
340k
  register _JSAMPLE *rescale = source->rescale;
405
340k
  JDIMENSION col;
406
340k
  unsigned int maxval = source->maxval;
407
340k
  register int rindex = rgb_red[cinfo->in_color_space];
408
340k
  register int gindex = rgb_green[cinfo->in_color_space];
409
340k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
340k
  register int aindex = alpha_index[cinfo->in_color_space];
411
340k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
340k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
420
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
340k
  ptr = source->pub._buffer[0];
416
340k
  bufferptr = source->iobuffer;
417
340k
  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
340k
  } else {
423
340k
    if (aindex >= 0)
424
68.0k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
340k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
272k
    else
427
272k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
340k
  }
429
340k
  return 1;
430
340k
}
Unexecuted instantiation: rdppm-8.c:get_rgb_row
rdppm-12.c:get_rgb_row
Line
Count
Source
400
340k
{
401
340k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
340k
  register _JSAMPROW ptr;
403
340k
  register U_CHAR *bufferptr;
404
340k
  register _JSAMPLE *rescale = source->rescale;
405
340k
  JDIMENSION col;
406
340k
  unsigned int maxval = source->maxval;
407
340k
  register int rindex = rgb_red[cinfo->in_color_space];
408
340k
  register int gindex = rgb_green[cinfo->in_color_space];
409
340k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
340k
  register int aindex = alpha_index[cinfo->in_color_space];
411
340k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
340k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
420
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
340k
  ptr = source->pub._buffer[0];
416
340k
  bufferptr = source->iobuffer;
417
340k
  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
340k
  } else {
423
340k
    if (aindex >= 0)
424
68.0k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
340k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
272k
    else
427
272k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
340k
  }
429
340k
  return 1;
430
340k
}
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
68.1k
{
438
68.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
68.1k
  register _JSAMPROW ptr;
440
68.1k
  register U_CHAR *bufferptr;
441
68.1k
  register _JSAMPLE *rescale = source->rescale;
442
68.1k
  JDIMENSION col;
443
68.1k
  unsigned int maxval = source->maxval;
444
445
68.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
84
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
68.1k
  ptr = source->pub._buffer[0];
448
68.1k
  bufferptr = source->iobuffer;
449
68.1k
  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
68.1k
  } else {
458
1.07M
    for (col = cinfo->image_width; col > 0; col--) {
459
1.01M
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
1.01M
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
1.01M
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
1.01M
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
1.01M
      ptr += 4;
464
1.01M
    }
465
68.1k
  }
466
68.1k
  return 1;
467
68.1k
}
Unexecuted instantiation: rdppm-8.c:get_rgb_cmyk_row
rdppm-12.c:get_rgb_cmyk_row
Line
Count
Source
437
68.1k
{
438
68.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
68.1k
  register _JSAMPROW ptr;
440
68.1k
  register U_CHAR *bufferptr;
441
68.1k
  register _JSAMPLE *rescale = source->rescale;
442
68.1k
  JDIMENSION col;
443
68.1k
  unsigned int maxval = source->maxval;
444
445
68.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
84
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
68.1k
  ptr = source->pub._buffer[0];
448
68.1k
  bufferptr = source->iobuffer;
449
68.1k
  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
68.1k
  } else {
458
1.07M
    for (col = cinfo->image_width; col > 0; col--) {
459
1.01M
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
1.01M
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
1.01M
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
1.01M
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
1.01M
      ptr += 4;
464
1.01M
    }
465
68.1k
  }
466
68.1k
  return 1;
467
68.1k
}
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
15.1k
{
490
15.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
15.1k
  register _JSAMPROW ptr;
492
15.1k
  register U_CHAR *bufferptr;
493
15.1k
  register _JSAMPLE *rescale = source->rescale;
494
15.1k
  JDIMENSION col;
495
15.1k
  unsigned int maxval = source->maxval;
496
497
15.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
15.1k
  ptr = source->pub._buffer[0];
500
15.1k
  bufferptr = source->iobuffer;
501
98.2k
  for (col = cinfo->image_width; col > 0; col--) {
502
83.0k
    register unsigned int temp;
503
83.0k
    temp  = UCH(*bufferptr++) << 8;
504
83.0k
    temp |= UCH(*bufferptr++);
505
83.0k
    if (temp > maxval)
506
32
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
83.0k
    *ptr++ = rescale[temp];
508
83.0k
  }
509
15.1k
  return 1;
510
15.1k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_row
rdppm-12.c:get_word_gray_row
Line
Count
Source
489
15.1k
{
490
15.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
15.1k
  register _JSAMPROW ptr;
492
15.1k
  register U_CHAR *bufferptr;
493
15.1k
  register _JSAMPLE *rescale = source->rescale;
494
15.1k
  JDIMENSION col;
495
15.1k
  unsigned int maxval = source->maxval;
496
497
15.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
15.1k
  ptr = source->pub._buffer[0];
500
15.1k
  bufferptr = source->iobuffer;
501
98.2k
  for (col = cinfo->image_width; col > 0; col--) {
502
83.0k
    register unsigned int temp;
503
83.0k
    temp  = UCH(*bufferptr++) << 8;
504
83.0k
    temp |= UCH(*bufferptr++);
505
83.0k
    if (temp > maxval)
506
32
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
83.0k
    *ptr++ = rescale[temp];
508
83.0k
  }
509
15.1k
  return 1;
510
15.1k
}
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
75.7k
{
517
75.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
75.7k
  register _JSAMPROW ptr;
519
75.7k
  register U_CHAR *bufferptr;
520
75.7k
  register _JSAMPLE *rescale = source->rescale;
521
75.7k
  JDIMENSION col;
522
75.7k
  unsigned int maxval = source->maxval;
523
75.7k
  register int rindex = rgb_red[cinfo->in_color_space];
524
75.7k
  register int gindex = rgb_green[cinfo->in_color_space];
525
75.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
75.7k
  register int aindex = alpha_index[cinfo->in_color_space];
527
75.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
75.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
310
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
75.7k
  ptr = source->pub._buffer[0];
532
75.7k
  bufferptr = source->iobuffer;
533
491k
  for (col = cinfo->image_width; col > 0; col--) {
534
415k
    register unsigned int temp;
535
415k
    temp  = UCH(*bufferptr++) << 8;
536
415k
    temp |= UCH(*bufferptr++);
537
415k
    if (temp > maxval)
538
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
415k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
415k
    if (aindex >= 0)
541
83.0k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
415k
    ptr += ps;
543
415k
  }
544
75.7k
  return 1;
545
75.7k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_rgb_row
rdppm-12.c:get_word_gray_rgb_row
Line
Count
Source
516
75.7k
{
517
75.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
75.7k
  register _JSAMPROW ptr;
519
75.7k
  register U_CHAR *bufferptr;
520
75.7k
  register _JSAMPLE *rescale = source->rescale;
521
75.7k
  JDIMENSION col;
522
75.7k
  unsigned int maxval = source->maxval;
523
75.7k
  register int rindex = rgb_red[cinfo->in_color_space];
524
75.7k
  register int gindex = rgb_green[cinfo->in_color_space];
525
75.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
75.7k
  register int aindex = alpha_index[cinfo->in_color_space];
527
75.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
75.7k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
310
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
75.7k
  ptr = source->pub._buffer[0];
532
75.7k
  bufferptr = source->iobuffer;
533
491k
  for (col = cinfo->image_width; col > 0; col--) {
534
415k
    register unsigned int temp;
535
415k
    temp  = UCH(*bufferptr++) << 8;
536
415k
    temp |= UCH(*bufferptr++);
537
415k
    if (temp > maxval)
538
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
415k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
415k
    if (aindex >= 0)
541
83.0k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
415k
    ptr += ps;
543
415k
  }
544
75.7k
  return 1;
545
75.7k
}
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
15.1k
{
552
15.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
15.1k
  register _JSAMPROW ptr;
554
15.1k
  register U_CHAR *bufferptr;
555
15.1k
  register _JSAMPLE *rescale = source->rescale;
556
15.1k
  JDIMENSION col;
557
15.1k
  unsigned int maxval = source->maxval;
558
559
15.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
15.1k
  ptr = source->pub._buffer[0];
562
15.1k
  bufferptr = source->iobuffer;
563
98.2k
  for (col = cinfo->image_width; col > 0; col--) {
564
83.0k
    register unsigned int gray;
565
83.0k
    gray  = UCH(*bufferptr++) << 8;
566
83.0k
    gray |= UCH(*bufferptr++);
567
83.0k
    if (gray > maxval)
568
32
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
83.0k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
83.0k
                ptr + 1, ptr + 2, ptr + 3);
571
83.0k
    ptr += 4;
572
83.0k
  }
573
15.1k
  return 1;
574
15.1k
}
Unexecuted instantiation: rdppm-8.c:get_word_gray_cmyk_row
rdppm-12.c:get_word_gray_cmyk_row
Line
Count
Source
551
15.1k
{
552
15.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
15.1k
  register _JSAMPROW ptr;
554
15.1k
  register U_CHAR *bufferptr;
555
15.1k
  register _JSAMPLE *rescale = source->rescale;
556
15.1k
  JDIMENSION col;
557
15.1k
  unsigned int maxval = source->maxval;
558
559
15.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
15.1k
  ptr = source->pub._buffer[0];
562
15.1k
  bufferptr = source->iobuffer;
563
98.2k
  for (col = cinfo->image_width; col > 0; col--) {
564
83.0k
    register unsigned int gray;
565
83.0k
    gray  = UCH(*bufferptr++) << 8;
566
83.0k
    gray |= UCH(*bufferptr++);
567
83.0k
    if (gray > maxval)
568
32
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
83.0k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
83.0k
                ptr + 1, ptr + 2, ptr + 3);
571
83.0k
    ptr += 4;
572
83.0k
  }
573
15.1k
  return 1;
574
15.1k
}
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
16.3k
{
581
16.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
16.3k
  register _JSAMPROW ptr;
583
16.3k
  register U_CHAR *bufferptr;
584
16.3k
  register _JSAMPLE *rescale = source->rescale;
585
16.3k
  JDIMENSION col;
586
16.3k
  unsigned int maxval = source->maxval;
587
16.3k
  register int rindex = rgb_red[cinfo->in_color_space];
588
16.3k
  register int gindex = rgb_green[cinfo->in_color_space];
589
16.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
16.3k
  register int aindex = alpha_index[cinfo->in_color_space];
591
16.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
16.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
410
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
16.3k
  ptr = source->pub._buffer[0];
596
16.3k
  bufferptr = source->iobuffer;
597
625k
  for (col = cinfo->image_width; col > 0; col--) {
598
609k
    register unsigned int temp;
599
609k
    temp  = UCH(*bufferptr++) << 8;
600
609k
    temp |= UCH(*bufferptr++);
601
609k
    if (temp > maxval)
602
180
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
609k
    ptr[rindex] = rescale[temp];
604
609k
    temp  = UCH(*bufferptr++) << 8;
605
609k
    temp |= UCH(*bufferptr++);
606
609k
    if (temp > maxval)
607
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
609k
    ptr[gindex] = rescale[temp];
609
609k
    temp  = UCH(*bufferptr++) << 8;
610
609k
    temp |= UCH(*bufferptr++);
611
609k
    if (temp > maxval)
612
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
609k
    ptr[bindex] = rescale[temp];
614
609k
    if (aindex >= 0)
615
121k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
609k
    ptr += ps;
617
609k
  }
618
16.3k
  return 1;
619
16.3k
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_row
rdppm-12.c:get_word_rgb_row
Line
Count
Source
580
16.3k
{
581
16.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
16.3k
  register _JSAMPROW ptr;
583
16.3k
  register U_CHAR *bufferptr;
584
16.3k
  register _JSAMPLE *rescale = source->rescale;
585
16.3k
  JDIMENSION col;
586
16.3k
  unsigned int maxval = source->maxval;
587
16.3k
  register int rindex = rgb_red[cinfo->in_color_space];
588
16.3k
  register int gindex = rgb_green[cinfo->in_color_space];
589
16.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
16.3k
  register int aindex = alpha_index[cinfo->in_color_space];
591
16.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
16.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
410
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
16.3k
  ptr = source->pub._buffer[0];
596
16.3k
  bufferptr = source->iobuffer;
597
625k
  for (col = cinfo->image_width; col > 0; col--) {
598
609k
    register unsigned int temp;
599
609k
    temp  = UCH(*bufferptr++) << 8;
600
609k
    temp |= UCH(*bufferptr++);
601
609k
    if (temp > maxval)
602
180
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
609k
    ptr[rindex] = rescale[temp];
604
609k
    temp  = UCH(*bufferptr++) << 8;
605
609k
    temp |= UCH(*bufferptr++);
606
609k
    if (temp > maxval)
607
125
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
609k
    ptr[gindex] = rescale[temp];
609
609k
    temp  = UCH(*bufferptr++) << 8;
610
609k
    temp |= UCH(*bufferptr++);
611
609k
    if (temp > maxval)
612
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
609k
    ptr[bindex] = rescale[temp];
614
609k
    if (aindex >= 0)
615
121k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
609k
    ptr += ps;
617
609k
  }
618
16.3k
  return 1;
619
16.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
3.26k
{
626
3.26k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
3.26k
  register _JSAMPROW ptr;
628
3.26k
  register U_CHAR *bufferptr;
629
3.26k
  register _JSAMPLE *rescale = source->rescale;
630
3.26k
  JDIMENSION col;
631
3.26k
  unsigned int maxval = source->maxval;
632
633
3.26k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
82
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
3.26k
  ptr = source->pub._buffer[0];
636
3.26k
  bufferptr = source->iobuffer;
637
125k
  for (col = cinfo->image_width; col > 0; col--) {
638
121k
    register unsigned int r, g, b;
639
121k
    r  = UCH(*bufferptr++) << 8;
640
121k
    r |= UCH(*bufferptr++);
641
121k
    if (r > maxval)
642
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
121k
    g  = UCH(*bufferptr++) << 8;
644
121k
    g |= UCH(*bufferptr++);
645
121k
    if (g > maxval)
646
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
121k
    b  = UCH(*bufferptr++) << 8;
648
121k
    b |= UCH(*bufferptr++);
649
121k
    if (b > maxval)
650
22
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
121k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
121k
                ptr + 2, ptr + 3);
653
121k
    ptr += 4;
654
121k
  }
655
3.26k
  return 1;
656
3.26k
}
Unexecuted instantiation: rdppm-8.c:get_word_rgb_cmyk_row
rdppm-12.c:get_word_rgb_cmyk_row
Line
Count
Source
625
3.26k
{
626
3.26k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
3.26k
  register _JSAMPROW ptr;
628
3.26k
  register U_CHAR *bufferptr;
629
3.26k
  register _JSAMPLE *rescale = source->rescale;
630
3.26k
  JDIMENSION col;
631
3.26k
  unsigned int maxval = source->maxval;
632
633
3.26k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
82
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
3.26k
  ptr = source->pub._buffer[0];
636
3.26k
  bufferptr = source->iobuffer;
637
125k
  for (col = cinfo->image_width; col > 0; col--) {
638
121k
    register unsigned int r, g, b;
639
121k
    r  = UCH(*bufferptr++) << 8;
640
121k
    r |= UCH(*bufferptr++);
641
121k
    if (r > maxval)
642
36
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
121k
    g  = UCH(*bufferptr++) << 8;
644
121k
    g |= UCH(*bufferptr++);
645
121k
    if (g > maxval)
646
25
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
121k
    b  = UCH(*bufferptr++) << 8;
648
121k
    b |= UCH(*bufferptr++);
649
121k
    if (b > maxval)
650
22
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
121k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
121k
                ptr + 2, ptr + 3);
653
121k
    ptr += 4;
654
121k
  }
655
3.26k
  return 1;
656
3.26k
}
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
10.6k
{
666
10.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
10.6k
  int c;
668
10.6k
  unsigned int w, h, maxval;
669
10.6k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
10.6k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
10.6k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
10.6k
  switch (c) {
678
1.40k
  case '2':                     /* it's a text-format PGM file */
679
3.26k
  case '3':                     /* it's a text-format PPM file */
680
7.91k
  case '5':                     /* it's a raw-format PGM file */
681
10.6k
  case '6':                     /* it's a raw-format PPM file */
682
10.6k
    break;
683
7
  default:
684
7
    ERREXIT(cinfo, JERR_PPM_NOT);
685
7
    break;
686
10.6k
  }
687
688
  /* fetch the remaining header info */
689
10.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
10.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
10.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
10.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
10.6k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
252
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
10.6k
  cinfo->image_width = (JDIMENSION)w;
699
10.6k
  cinfo->image_height = (JDIMENSION)h;
700
10.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
10.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
10.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
10.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
10.6k
  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
161
      source->pub.get_pixel_rows = get_text_gray_row;
715
966
    else if (IsExtRGB(cinfo->in_color_space))
716
805
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
161
    else if (cinfo->in_color_space == JCS_CMYK)
718
161
      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.49k
  case '3':                     /* it's a text-format PPM file */
725
1.49k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.49k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.49k
    if (IsExtRGB(cinfo->in_color_space))
729
1.07k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
428
    else if (cinfo->in_color_space == JCS_CMYK)
731
214
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
214
    else
733
214
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.49k
    need_iobuffer = FALSE;
735
1.49k
    break;
736
737
4.26k
  case '5':                     /* it's a raw-format PGM file */
738
4.26k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
4.26k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
4.26k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
4.26k
    if (maxval > 255) {
743
798
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
114
        source->pub.get_pixel_rows = get_word_gray_row;
745
684
      else if (IsExtRGB(cinfo->in_color_space))
746
570
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
114
      else if (cinfo->in_color_space == JCS_CMYK)
748
114
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
3.46k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
0
      source->pub.get_pixel_rows = get_raw_row;
755
0
      use_raw_buffer = TRUE;
756
0
      need_rescale = FALSE;
757
3.46k
    } else {
758
3.46k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
495
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
2.97k
      else if (IsExtRGB(cinfo->in_color_space))
761
2.47k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
495
      else if (cinfo->in_color_space == JCS_CMYK)
763
495
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
3.46k
    }
767
4.26k
    break;
768
769
2.35k
  case '6':                     /* it's a raw-format PPM file */
770
2.35k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.35k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.35k
    if (maxval > 255) {
774
1.36k
      if (IsExtRGB(cinfo->in_color_space))
775
975
        source->pub.get_pixel_rows = get_word_rgb_row;
776
390
      else if (cinfo->in_color_space == JCS_CMYK)
777
195
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
195
      else
779
195
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.36k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
0
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
0
      source->pub.get_pixel_rows = get_raw_row;
789
0
      use_raw_buffer = TRUE;
790
0
      need_rescale = FALSE;
791
994
    } else {
792
994
      if (IsExtRGB(cinfo->in_color_space))
793
710
        source->pub.get_pixel_rows = get_rgb_row;
794
284
      else if (cinfo->in_color_space == JCS_CMYK)
795
142
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
142
      else
797
142
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
994
    }
799
2.35k
    break;
800
10.6k
  }
801
802
8.69k
  if (IsExtRGB(cinfo->in_color_space))
803
6.60k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.09k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
770
    cinfo->input_components = 1;
806
1.32k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.32k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
8.69k
  if (need_iobuffer) {
811
6.28k
    if (c == '6')
812
2.02k
      source->buffer_width = (size_t)w * 3 *
813
2.02k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
4.26k
    else
815
4.26k
      source->buffer_width = (size_t)w *
816
4.26k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
6.28k
    source->iobuffer = (U_CHAR *)
818
6.28k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
6.28k
                                  source->buffer_width);
820
6.28k
  }
821
822
  /* Create compressor input buffer. */
823
8.69k
  if (use_raw_buffer) {
824
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
825
    /* Synthesize a _JSAMPARRAY pointer structure */
826
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
0
    source->pub._buffer = &source->pixrow;
828
0
    source->pub.buffer_height = 1;
829
8.69k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
8.69k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
8.69k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
8.69k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
8.69k
    source->pub.buffer_height = 1;
835
8.69k
  }
836
837
  /* Compute the rescaling array if required. */
838
8.69k
  if (need_rescale) {
839
8.69k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
8.69k
    source->rescale = (_JSAMPLE *)
843
8.69k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
8.69k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
8.69k
                                           sizeof(_JSAMPLE)));
846
8.69k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
8.69k
                                        sizeof(_JSAMPLE)));
848
8.69k
    half_maxval = maxval / 2;
849
41.1M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
41.1M
      source->rescale[val] =
852
41.1M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
41.1M
                   maxval);
854
41.1M
    }
855
8.69k
  }
856
8.69k
}
Unexecuted instantiation: rdppm-8.c:start_input_ppm
rdppm-12.c:start_input_ppm
Line
Count
Source
665
10.6k
{
666
10.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
10.6k
  int c;
668
10.6k
  unsigned int w, h, maxval;
669
10.6k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
10.6k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
10.6k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
10.6k
  switch (c) {
678
1.40k
  case '2':                     /* it's a text-format PGM file */
679
3.26k
  case '3':                     /* it's a text-format PPM file */
680
7.91k
  case '5':                     /* it's a raw-format PGM file */
681
10.6k
  case '6':                     /* it's a raw-format PPM file */
682
10.6k
    break;
683
7
  default:
684
7
    ERREXIT(cinfo, JERR_PPM_NOT);
685
7
    break;
686
10.6k
  }
687
688
  /* fetch the remaining header info */
689
10.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
10.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
10.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
10.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
10.6k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
252
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
10.6k
  cinfo->image_width = (JDIMENSION)w;
699
10.6k
  cinfo->image_height = (JDIMENSION)h;
700
10.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
10.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
10.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
10.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
10.6k
  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
161
      source->pub.get_pixel_rows = get_text_gray_row;
715
966
    else if (IsExtRGB(cinfo->in_color_space))
716
805
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
161
    else if (cinfo->in_color_space == JCS_CMYK)
718
161
      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.49k
  case '3':                     /* it's a text-format PPM file */
725
1.49k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.49k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.49k
    if (IsExtRGB(cinfo->in_color_space))
729
1.07k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
428
    else if (cinfo->in_color_space == JCS_CMYK)
731
214
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
214
    else
733
214
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.49k
    need_iobuffer = FALSE;
735
1.49k
    break;
736
737
4.26k
  case '5':                     /* it's a raw-format PGM file */
738
4.26k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
4.26k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
4.26k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
4.26k
    if (maxval > 255) {
743
798
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
114
        source->pub.get_pixel_rows = get_word_gray_row;
745
684
      else if (IsExtRGB(cinfo->in_color_space))
746
570
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
114
      else if (cinfo->in_color_space == JCS_CMYK)
748
114
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
3.46k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
0
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
0
      source->pub.get_pixel_rows = get_raw_row;
755
0
      use_raw_buffer = TRUE;
756
0
      need_rescale = FALSE;
757
3.46k
    } else {
758
3.46k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
495
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
2.97k
      else if (IsExtRGB(cinfo->in_color_space))
761
2.47k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
495
      else if (cinfo->in_color_space == JCS_CMYK)
763
495
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
3.46k
    }
767
4.26k
    break;
768
769
2.35k
  case '6':                     /* it's a raw-format PPM file */
770
2.35k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.35k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.35k
    if (maxval > 255) {
774
1.36k
      if (IsExtRGB(cinfo->in_color_space))
775
975
        source->pub.get_pixel_rows = get_word_rgb_row;
776
390
      else if (cinfo->in_color_space == JCS_CMYK)
777
195
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
195
      else
779
195
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.36k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
0
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
0
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
0
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
0
      source->pub.get_pixel_rows = get_raw_row;
789
0
      use_raw_buffer = TRUE;
790
0
      need_rescale = FALSE;
791
994
    } else {
792
994
      if (IsExtRGB(cinfo->in_color_space))
793
710
        source->pub.get_pixel_rows = get_rgb_row;
794
284
      else if (cinfo->in_color_space == JCS_CMYK)
795
142
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
142
      else
797
142
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
994
    }
799
2.35k
    break;
800
10.6k
  }
801
802
8.69k
  if (IsExtRGB(cinfo->in_color_space))
803
6.60k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.09k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
770
    cinfo->input_components = 1;
806
1.32k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.32k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
8.69k
  if (need_iobuffer) {
811
6.28k
    if (c == '6')
812
2.02k
      source->buffer_width = (size_t)w * 3 *
813
2.02k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
4.26k
    else
815
4.26k
      source->buffer_width = (size_t)w *
816
4.26k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
6.28k
    source->iobuffer = (U_CHAR *)
818
6.28k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
6.28k
                                  source->buffer_width);
820
6.28k
  }
821
822
  /* Create compressor input buffer. */
823
8.69k
  if (use_raw_buffer) {
824
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
825
    /* Synthesize a _JSAMPARRAY pointer structure */
826
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
0
    source->pub._buffer = &source->pixrow;
828
0
    source->pub.buffer_height = 1;
829
8.69k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
8.69k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
8.69k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
8.69k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
8.69k
    source->pub.buffer_height = 1;
835
8.69k
  }
836
837
  /* Compute the rescaling array if required. */
838
8.69k
  if (need_rescale) {
839
8.69k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
8.69k
    source->rescale = (_JSAMPLE *)
843
8.69k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
8.69k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
8.69k
                                           sizeof(_JSAMPLE)));
846
8.69k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
8.69k
                                        sizeof(_JSAMPLE)));
848
8.69k
    half_maxval = maxval / 2;
849
41.1M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
41.1M
      source->rescale[val] =
852
41.1M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
41.1M
                   maxval);
854
41.1M
    }
855
8.69k
  }
856
8.69k
}
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
3.91k
{
866
  /* no work */
867
3.91k
}
Unexecuted instantiation: rdppm-8.c:finish_input_ppm
rdppm-12.c:finish_input_ppm
Line
Count
Source
865
3.91k
{
866
  /* no work */
867
3.91k
}
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
10.6k
{
877
10.6k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
10.6k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
10.6k
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
0
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
10.6k
  source = (ppm_source_ptr)
889
10.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
10.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
10.6k
  source->pub.start_input = start_input_ppm;
893
10.6k
  source->pub.finish_input = finish_input_ppm;
894
10.6k
  source->pub.max_pixels = 0;
895
896
10.6k
  return (cjpeg_source_ptr)source;
897
10.6k
}
Unexecuted instantiation: jinit_read_ppm
j12init_read_ppm
Line
Count
Source
876
10.6k
{
877
10.6k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
10.6k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
10.6k
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
0
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
10.6k
  source = (ppm_source_ptr)
889
10.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
10.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
10.6k
  source->pub.start_input = start_input_ppm;
893
10.6k
  source->pub.finish_input = finish_input_ppm;
894
10.6k
  source->pub.max_pixels = 0;
895
896
10.6k
  return (cjpeg_source_ptr)source;
897
10.6k
}
Unexecuted instantiation: j16init_read_ppm
898
899
#endif /* defined(PPM_SUPPORTED) &&
900
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */