Coverage Report

Created: 2025-10-10 07:04

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