Coverage Report

Created: 2025-07-14 06:10

/src/libjpeg-turbo.main/src/rdppm.c
Line
Count
Source (jump to first uncovered line)
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
167M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
204M
  (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
2.11M
{
80
2.11M
  register int ch;
81
82
2.11M
  ch = getc(infile);
83
2.11M
  if (ch == '#') {
84
333k
    do {
85
333k
      ch = getc(infile);
86
333k
    } while (ch != '\n' && ch != EOF);
87
22.0k
  }
88
2.11M
  return ch;
89
2.11M
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
874k
{
80
874k
  register int ch;
81
82
874k
  ch = getc(infile);
83
874k
  if (ch == '#') {
84
117k
    do {
85
117k
      ch = getc(infile);
86
117k
    } while (ch != '\n' && ch != EOF);
87
10.3k
  }
88
874k
  return ch;
89
874k
}
rdppm-12.c:pbm_getc
Line
Count
Source
79
931k
{
80
931k
  register int ch;
81
82
931k
  ch = getc(infile);
83
931k
  if (ch == '#') {
84
147k
    do {
85
147k
      ch = getc(infile);
86
147k
    } while (ch != '\n' && ch != EOF);
87
7.09k
  }
88
931k
  return ch;
89
931k
}
rdppm-16.c:pbm_getc
Line
Count
Source
79
304k
{
80
304k
  register int ch;
81
82
304k
  ch = getc(infile);
83
304k
  if (ch == '#') {
84
68.7k
    do {
85
68.7k
      ch = getc(infile);
86
68.7k
    } while (ch != '\n' && ch != EOF);
87
4.58k
  }
88
304k
  return ch;
89
304k
}
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
872k
{
99
872k
  register int ch;
100
872k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
922k
  do {
104
922k
    ch = pbm_getc(infile);
105
922k
    if (ch == EOF)
106
16.5k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
922k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
872k
  if (ch < '0' || ch > '9')
110
1.37k
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
872k
  val = ch - '0';
113
1.20M
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
334k
    val *= 10;
115
334k
    val += ch - '0';
116
334k
    if (val > maxval)
117
764
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
334k
  }
119
120
872k
  return val;
121
872k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
373k
{
99
373k
  register int ch;
100
373k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
393k
  do {
104
393k
    ch = pbm_getc(infile);
105
393k
    if (ch == EOF)
106
7.97k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
393k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
373k
  if (ch < '0' || ch > '9')
110
615
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
373k
  val = ch - '0';
113
489k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
116k
    val *= 10;
115
116k
    val += ch - '0';
116
116k
    if (val > maxval)
117
366
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
116k
  }
119
120
373k
  return val;
121
373k
}
rdppm-12.c:read_pbm_integer
Line
Count
Source
98
365k
{
99
365k
  register int ch;
100
365k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
387k
  do {
104
387k
    ch = pbm_getc(infile);
105
387k
    if (ch == EOF)
106
5.32k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
387k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
365k
  if (ch < '0' || ch > '9')
110
529
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
365k
  val = ch - '0';
113
550k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
184k
    val *= 10;
115
184k
    val += ch - '0';
116
184k
    if (val > maxval)
117
259
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
184k
  }
119
120
365k
  return val;
121
365k
}
rdppm-16.c:read_pbm_integer
Line
Count
Source
98
133k
{
99
133k
  register int ch;
100
133k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
141k
  do {
104
141k
    ch = pbm_getc(infile);
105
141k
    if (ch == EOF)
106
3.21k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
141k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
133k
  if (ch < '0' || ch > '9')
110
231
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
133k
  val = ch - '0';
113
166k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
33.3k
    val *= 10;
115
33.3k
    val += ch - '0';
116
33.3k
    if (val > maxval)
117
139
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
33.3k
  }
119
120
133k
  return val;
121
133k
}
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
10.0k
{
140
10.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
10.0k
  FILE *infile = source->pub.input_file;
142
10.0k
  register _JSAMPROW ptr;
143
10.0k
  register _JSAMPLE *rescale = source->rescale;
144
10.0k
  JDIMENSION col;
145
10.0k
  unsigned int maxval = source->maxval;
146
147
10.0k
  ptr = source->pub._buffer[0];
148
28.3k
  for (col = cinfo->image_width; col > 0; col--) {
149
18.3k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
18.3k
  }
151
10.0k
  return 1;
152
10.0k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
5.29k
{
140
5.29k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
5.29k
  FILE *infile = source->pub.input_file;
142
5.29k
  register _JSAMPROW ptr;
143
5.29k
  register _JSAMPLE *rescale = source->rescale;
144
5.29k
  JDIMENSION col;
145
5.29k
  unsigned int maxval = source->maxval;
146
147
5.29k
  ptr = source->pub._buffer[0];
148
15.0k
  for (col = cinfo->image_width; col > 0; col--) {
149
9.79k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
9.79k
  }
151
5.29k
  return 1;
152
5.29k
}
rdppm-12.c:get_text_gray_row
Line
Count
Source
139
2.97k
{
140
2.97k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.97k
  FILE *infile = source->pub.input_file;
142
2.97k
  register _JSAMPROW ptr;
143
2.97k
  register _JSAMPLE *rescale = source->rescale;
144
2.97k
  JDIMENSION col;
145
2.97k
  unsigned int maxval = source->maxval;
146
147
2.97k
  ptr = source->pub._buffer[0];
148
8.08k
  for (col = cinfo->image_width; col > 0; col--) {
149
5.11k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
5.11k
  }
151
2.97k
  return 1;
152
2.97k
}
rdppm-16.c:get_text_gray_row
Line
Count
Source
139
1.78k
{
140
1.78k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.78k
  FILE *infile = source->pub.input_file;
142
1.78k
  register _JSAMPROW ptr;
143
1.78k
  register _JSAMPLE *rescale = source->rescale;
144
1.78k
  JDIMENSION col;
145
1.78k
  unsigned int maxval = source->maxval;
146
147
1.78k
  ptr = source->pub._buffer[0];
148
5.22k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.43k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.43k
  }
151
1.78k
  return 1;
152
1.78k
}
153
154
155
148M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
569M
  for (col = cinfo->image_width; col > 0; col--) { \
157
421M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
421M
    alpha_set_op \
159
421M
    ptr += ps; \
160
421M
  } \
161
148M
}
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
50.2k
{
168
50.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
50.2k
  FILE *infile = source->pub.input_file;
170
50.2k
  register _JSAMPROW ptr;
171
50.2k
  register _JSAMPLE *rescale = source->rescale;
172
50.2k
  JDIMENSION col;
173
50.2k
  unsigned int maxval = source->maxval;
174
50.2k
  register int rindex = rgb_red[cinfo->in_color_space];
175
50.2k
  register int gindex = rgb_green[cinfo->in_color_space];
176
50.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
50.2k
  register int aindex = alpha_index[cinfo->in_color_space];
178
50.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
50.2k
  ptr = source->pub._buffer[0];
181
50.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
8.02k
    if (aindex >= 0)
183
1.16k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
8.02k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
6.85k
    else
186
6.85k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
42.2k
  } else {
188
42.2k
    if (aindex >= 0)
189
6.86k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
42.2k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
35.3k
    else
192
35.3k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
42.2k
  }
194
50.2k
  return 1;
195
50.2k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
26.4k
{
168
26.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
26.4k
  FILE *infile = source->pub.input_file;
170
26.4k
  register _JSAMPROW ptr;
171
26.4k
  register _JSAMPLE *rescale = source->rescale;
172
26.4k
  JDIMENSION col;
173
26.4k
  unsigned int maxval = source->maxval;
174
26.4k
  register int rindex = rgb_red[cinfo->in_color_space];
175
26.4k
  register int gindex = rgb_green[cinfo->in_color_space];
176
26.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
26.4k
  register int aindex = alpha_index[cinfo->in_color_space];
178
26.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
26.4k
  ptr = source->pub._buffer[0];
181
26.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
4.61k
    if (aindex >= 0)
183
472
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
4.61k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
4.13k
    else
186
4.13k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
21.8k
  } else {
188
21.8k
    if (aindex >= 0)
189
2.80k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
21.8k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
19.0k
    else
192
19.0k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
21.8k
  }
194
26.4k
  return 1;
195
26.4k
}
rdppm-12.c:get_text_gray_rgb_row
Line
Count
Source
167
14.8k
{
168
14.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
14.8k
  FILE *infile = source->pub.input_file;
170
14.8k
  register _JSAMPROW ptr;
171
14.8k
  register _JSAMPLE *rescale = source->rescale;
172
14.8k
  JDIMENSION col;
173
14.8k
  unsigned int maxval = source->maxval;
174
14.8k
  register int rindex = rgb_red[cinfo->in_color_space];
175
14.8k
  register int gindex = rgb_green[cinfo->in_color_space];
176
14.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
14.8k
  register int aindex = alpha_index[cinfo->in_color_space];
178
14.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
14.8k
  ptr = source->pub._buffer[0];
181
14.8k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
2.40k
    if (aindex >= 0)
183
481
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
2.40k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.92k
    else
186
1.92k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
12.4k
  } else {
188
12.4k
    if (aindex >= 0)
189
2.48k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
12.4k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
9.95k
    else
192
9.95k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
12.4k
  }
194
14.8k
  return 1;
195
14.8k
}
rdppm-16.c:get_text_gray_rgb_row
Line
Count
Source
167
8.94k
{
168
8.94k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
8.94k
  FILE *infile = source->pub.input_file;
170
8.94k
  register _JSAMPROW ptr;
171
8.94k
  register _JSAMPLE *rescale = source->rescale;
172
8.94k
  JDIMENSION col;
173
8.94k
  unsigned int maxval = source->maxval;
174
8.94k
  register int rindex = rgb_red[cinfo->in_color_space];
175
8.94k
  register int gindex = rgb_green[cinfo->in_color_space];
176
8.94k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
8.94k
  register int aindex = alpha_index[cinfo->in_color_space];
178
8.94k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
8.94k
  ptr = source->pub._buffer[0];
181
8.94k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.00k
    if (aindex >= 0)
183
212
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.00k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
795
    else
186
795
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
7.93k
  } else {
188
7.93k
    if (aindex >= 0)
189
1.57k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
7.93k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.35k
    else
192
6.35k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
7.93k
  }
194
8.94k
  return 1;
195
8.94k
}
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
8.03k
{
203
8.03k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
8.03k
  FILE *infile = source->pub.input_file;
205
8.03k
  register _JSAMPROW ptr;
206
8.03k
  register _JSAMPLE *rescale = source->rescale;
207
8.03k
  JDIMENSION col;
208
8.03k
  unsigned int maxval = source->maxval;
209
210
8.03k
  ptr = source->pub._buffer[0];
211
8.03k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
5.56k
    for (col = cinfo->image_width; col > 0; col--) {
213
3.66k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
3.66k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
3.66k
      ptr += 4;
216
3.66k
    }
217
6.13k
  } else {
218
17.3k
    for (col = cinfo->image_width; col > 0; col--) {
219
11.2k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
11.2k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
11.2k
      ptr += 4;
222
11.2k
    }
223
6.13k
  }
224
8.03k
  return 1;
225
8.03k
}
rdppm-8.c:get_text_gray_cmyk_row
Line
Count
Source
202
3.27k
{
203
3.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
3.27k
  FILE *infile = source->pub.input_file;
205
3.27k
  register _JSAMPROW ptr;
206
3.27k
  register _JSAMPLE *rescale = source->rescale;
207
3.27k
  JDIMENSION col;
208
3.27k
  unsigned int maxval = source->maxval;
209
210
3.27k
  ptr = source->pub._buffer[0];
211
3.27k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
3.00k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.89k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.89k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.89k
      ptr += 4;
216
1.89k
    }
217
2.16k
  } else {
218
6.63k
    for (col = cinfo->image_width; col > 0; col--) {
219
4.47k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
4.47k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
4.47k
      ptr += 4;
222
4.47k
    }
223
2.16k
  }
224
3.27k
  return 1;
225
3.27k
}
rdppm-12.c:get_text_gray_cmyk_row
Line
Count
Source
202
2.97k
{
203
2.97k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
2.97k
  FILE *infile = source->pub.input_file;
205
2.97k
  register _JSAMPROW ptr;
206
2.97k
  register _JSAMPLE *rescale = source->rescale;
207
2.97k
  JDIMENSION col;
208
2.97k
  unsigned int maxval = source->maxval;
209
210
2.97k
  ptr = source->pub._buffer[0];
211
2.97k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.50k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.01k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.01k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.01k
      ptr += 4;
216
1.01k
    }
217
2.48k
  } else {
218
6.58k
    for (col = cinfo->image_width; col > 0; col--) {
219
4.09k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
4.09k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
4.09k
      ptr += 4;
222
4.09k
    }
223
2.48k
  }
224
2.97k
  return 1;
225
2.97k
}
rdppm-16.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.78k
{
203
1.78k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.78k
  FILE *infile = source->pub.input_file;
205
1.78k
  register _JSAMPROW ptr;
206
1.78k
  register _JSAMPLE *rescale = source->rescale;
207
1.78k
  JDIMENSION col;
208
1.78k
  unsigned int maxval = source->maxval;
209
210
1.78k
  ptr = source->pub._buffer[0];
211
1.78k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.05k
    for (col = cinfo->image_width; col > 0; col--) {
213
749
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
749
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
749
      ptr += 4;
216
749
    }
217
1.48k
  } else {
218
4.16k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.68k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.68k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
2.68k
      ptr += 4;
222
2.68k
    }
223
1.48k
  }
224
1.78k
  return 1;
225
1.78k
}
226
227
228
1.32M
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
22.4M
  for (col = cinfo->image_width; col > 0; col--) { \
230
21.1M
    ptr[rindex] = read_op; \
231
21.1M
    ptr[gindex] = read_op; \
232
21.1M
    ptr[bindex] = read_op; \
233
21.1M
    alpha_set_op \
234
21.1M
    ptr += ps; \
235
21.1M
  } \
236
1.32M
}
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
62.0k
{
242
62.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
62.0k
  FILE *infile = source->pub.input_file;
244
62.0k
  register _JSAMPROW ptr;
245
62.0k
  register _JSAMPLE *rescale = source->rescale;
246
62.0k
  JDIMENSION col;
247
62.0k
  unsigned int maxval = source->maxval;
248
62.0k
  register int rindex = rgb_red[cinfo->in_color_space];
249
62.0k
  register int gindex = rgb_green[cinfo->in_color_space];
250
62.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
62.0k
  register int aindex = alpha_index[cinfo->in_color_space];
252
62.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
62.0k
  ptr = source->pub._buffer[0];
255
62.0k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
12.8k
    if (aindex >= 0)
257
1.79k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
12.8k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
11.1k
    else
260
11.1k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
49.2k
  } else {
262
49.2k
    if (aindex >= 0)
263
8.73k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
49.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
40.4k
    else
266
40.4k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
49.2k
  }
268
62.0k
  return 1;
269
62.0k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
241
30.4k
{
242
30.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
30.4k
  FILE *infile = source->pub.input_file;
244
30.4k
  register _JSAMPROW ptr;
245
30.4k
  register _JSAMPLE *rescale = source->rescale;
246
30.4k
  JDIMENSION col;
247
30.4k
  unsigned int maxval = source->maxval;
248
30.4k
  register int rindex = rgb_red[cinfo->in_color_space];
249
30.4k
  register int gindex = rgb_green[cinfo->in_color_space];
250
30.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
30.4k
  register int aindex = alpha_index[cinfo->in_color_space];
252
30.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
30.4k
  ptr = source->pub._buffer[0];
255
30.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
6.36k
    if (aindex >= 0)
257
750
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
6.36k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
5.61k
    else
260
5.61k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
24.0k
  } else {
262
24.0k
    if (aindex >= 0)
263
3.44k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
24.0k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
20.6k
    else
266
20.6k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
24.0k
  }
268
30.4k
  return 1;
269
30.4k
}
rdppm-12.c:get_text_rgb_row
Line
Count
Source
241
16.7k
{
242
16.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
16.7k
  FILE *infile = source->pub.input_file;
244
16.7k
  register _JSAMPROW ptr;
245
16.7k
  register _JSAMPLE *rescale = source->rescale;
246
16.7k
  JDIMENSION col;
247
16.7k
  unsigned int maxval = source->maxval;
248
16.7k
  register int rindex = rgb_red[cinfo->in_color_space];
249
16.7k
  register int gindex = rgb_green[cinfo->in_color_space];
250
16.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
16.7k
  register int aindex = alpha_index[cinfo->in_color_space];
252
16.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
16.7k
  ptr = source->pub._buffer[0];
255
16.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
4.12k
    if (aindex >= 0)
257
824
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
4.12k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
3.29k
    else
260
3.29k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
12.6k
  } else {
262
12.6k
    if (aindex >= 0)
263
2.52k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
12.6k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
10.0k
    else
266
10.0k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
12.6k
  }
268
16.7k
  return 1;
269
16.7k
}
rdppm-16.c:get_text_rgb_row
Line
Count
Source
241
14.9k
{
242
14.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
14.9k
  FILE *infile = source->pub.input_file;
244
14.9k
  register _JSAMPROW ptr;
245
14.9k
  register _JSAMPLE *rescale = source->rescale;
246
14.9k
  JDIMENSION col;
247
14.9k
  unsigned int maxval = source->maxval;
248
14.9k
  register int rindex = rgb_red[cinfo->in_color_space];
249
14.9k
  register int gindex = rgb_green[cinfo->in_color_space];
250
14.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
14.9k
  register int aindex = alpha_index[cinfo->in_color_space];
252
14.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
14.9k
  ptr = source->pub._buffer[0];
255
14.9k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
2.41k
    if (aindex >= 0)
257
219
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
2.41k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
2.19k
    else
260
2.19k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
12.5k
  } else {
262
12.5k
    if (aindex >= 0)
263
2.76k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
12.5k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
9.74k
    else
266
9.74k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
12.5k
  }
268
14.9k
  return 1;
269
14.9k
}
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
10.5k
{
277
10.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
10.5k
  FILE *infile = source->pub.input_file;
279
10.5k
  register _JSAMPROW ptr;
280
10.5k
  register _JSAMPLE *rescale = source->rescale;
281
10.5k
  JDIMENSION col;
282
10.5k
  unsigned int maxval = source->maxval;
283
284
10.5k
  ptr = source->pub._buffer[0];
285
10.5k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
15.1k
    for (col = cinfo->image_width; col > 0; col--) {
287
11.6k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
11.6k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
11.6k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
11.6k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
11.6k
      ptr += 4;
292
11.6k
    }
293
7.04k
  } else {
294
19.1k
    for (col = cinfo->image_width; col > 0; col--) {
295
12.1k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
12.1k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
12.1k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
12.1k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
12.1k
      ptr += 4;
300
12.1k
    }
301
7.04k
  }
302
10.5k
  return 1;
303
10.5k
}
rdppm-8.c:get_text_rgb_cmyk_row
Line
Count
Source
276
4.19k
{
277
4.19k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
4.19k
  FILE *infile = source->pub.input_file;
279
4.19k
  register _JSAMPROW ptr;
280
4.19k
  register _JSAMPLE *rescale = source->rescale;
281
4.19k
  JDIMENSION col;
282
4.19k
  unsigned int maxval = source->maxval;
283
284
4.19k
  ptr = source->pub._buffer[0];
285
4.19k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
4.18k
    for (col = cinfo->image_width; col > 0; col--) {
287
2.46k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
2.46k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
2.46k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
2.46k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
2.46k
      ptr += 4;
292
2.46k
    }
293
2.47k
  } else {
294
7.25k
    for (col = cinfo->image_width; col > 0; col--) {
295
4.78k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
4.78k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
4.78k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
4.78k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
4.78k
      ptr += 4;
300
4.78k
    }
301
2.47k
  }
302
4.19k
  return 1;
303
4.19k
}
rdppm-12.c:get_text_rgb_cmyk_row
Line
Count
Source
276
3.34k
{
277
3.34k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
3.34k
  FILE *infile = source->pub.input_file;
279
3.34k
  register _JSAMPROW ptr;
280
3.34k
  register _JSAMPLE *rescale = source->rescale;
281
3.34k
  JDIMENSION col;
282
3.34k
  unsigned int maxval = source->maxval;
283
284
3.34k
  ptr = source->pub._buffer[0];
285
3.34k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
8.51k
    for (col = cinfo->image_width; col > 0; col--) {
287
7.68k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
7.68k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
7.68k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
7.68k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
7.68k
      ptr += 4;
292
7.68k
    }
293
2.52k
  } else {
294
6.96k
    for (col = cinfo->image_width; col > 0; col--) {
295
4.43k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
4.43k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
4.43k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
4.43k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
4.43k
      ptr += 4;
300
4.43k
    }
301
2.52k
  }
302
3.34k
  return 1;
303
3.34k
}
rdppm-16.c:get_text_rgb_cmyk_row
Line
Count
Source
276
2.98k
{
277
2.98k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.98k
  FILE *infile = source->pub.input_file;
279
2.98k
  register _JSAMPROW ptr;
280
2.98k
  register _JSAMPLE *rescale = source->rescale;
281
2.98k
  JDIMENSION col;
282
2.98k
  unsigned int maxval = source->maxval;
283
284
2.98k
  ptr = source->pub._buffer[0];
285
2.98k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.43k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.49k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.49k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.49k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.49k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.49k
      ptr += 4;
292
1.49k
    }
293
2.04k
  } else {
294
4.92k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.88k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.88k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.88k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.88k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.88k
      ptr += 4;
300
2.88k
    }
301
2.04k
  }
302
2.98k
  return 1;
303
2.98k
}
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
27.7M
{
310
27.7M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
27.7M
  register _JSAMPROW ptr;
312
27.7M
  register U_CHAR *bufferptr;
313
27.7M
  register _JSAMPLE *rescale = source->rescale;
314
27.7M
  JDIMENSION col;
315
316
27.7M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
406
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
27.7M
  ptr = source->pub._buffer[0];
319
27.7M
  bufferptr = source->iobuffer;
320
109M
  for (col = cinfo->image_width; col > 0; col--) {
321
81.7M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
81.7M
  }
323
27.7M
  return 1;
324
27.7M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
309
10.8M
{
310
10.8M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
10.8M
  register _JSAMPROW ptr;
312
10.8M
  register U_CHAR *bufferptr;
313
10.8M
  register _JSAMPLE *rescale = source->rescale;
314
10.8M
  JDIMENSION col;
315
316
10.8M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
222
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
10.8M
  ptr = source->pub._buffer[0];
319
10.8M
  bufferptr = source->iobuffer;
320
46.7M
  for (col = cinfo->image_width; col > 0; col--) {
321
35.9M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
35.9M
  }
323
10.8M
  return 1;
324
10.8M
}
rdppm-12.c:get_scaled_gray_row
Line
Count
Source
309
14.3M
{
310
14.3M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
14.3M
  register _JSAMPROW ptr;
312
14.3M
  register U_CHAR *bufferptr;
313
14.3M
  register _JSAMPLE *rescale = source->rescale;
314
14.3M
  JDIMENSION col;
315
316
14.3M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
123
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
14.3M
  ptr = source->pub._buffer[0];
319
14.3M
  bufferptr = source->iobuffer;
320
50.4M
  for (col = cinfo->image_width; col > 0; col--) {
321
36.1M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
36.1M
  }
323
14.3M
  return 1;
324
14.3M
}
rdppm-16.c:get_scaled_gray_row
Line
Count
Source
309
2.64M
{
310
2.64M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.64M
  register _JSAMPROW ptr;
312
2.64M
  register U_CHAR *bufferptr;
313
2.64M
  register _JSAMPLE *rescale = source->rescale;
314
2.64M
  JDIMENSION col;
315
316
2.64M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.64M
  ptr = source->pub._buffer[0];
319
2.64M
  bufferptr = source->iobuffer;
320
12.2M
  for (col = cinfo->image_width; col > 0; col--) {
321
9.61M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
9.61M
  }
323
2.64M
  return 1;
324
2.64M
}
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
148M
{
332
148M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
148M
  register _JSAMPROW ptr;
334
148M
  register U_CHAR *bufferptr;
335
148M
  register _JSAMPLE *rescale = source->rescale;
336
148M
  JDIMENSION col;
337
148M
  unsigned int maxval = source->maxval;
338
148M
  register int rindex = rgb_red[cinfo->in_color_space];
339
148M
  register int gindex = rgb_green[cinfo->in_color_space];
340
148M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
148M
  register int aindex = alpha_index[cinfo->in_color_space];
342
148M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
148M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
2.43k
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
148M
  ptr = source->pub._buffer[0];
347
148M
  bufferptr = source->iobuffer;
348
148M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
10.0M
    if (aindex >= 0)
350
277k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
9.76M
    else
352
9.76M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
138M
  } else {
354
138M
    if (aindex >= 0)
355
24.2M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
138M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
114M
    else
358
114M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
138M
  }
360
148M
  return 1;
361
148M
}
rdppm-8.c:get_gray_rgb_row
Line
Count
Source
331
63.8M
{
332
63.8M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
63.8M
  register _JSAMPROW ptr;
334
63.8M
  register U_CHAR *bufferptr;
335
63.8M
  register _JSAMPLE *rescale = source->rescale;
336
63.8M
  JDIMENSION col;
337
63.8M
  unsigned int maxval = source->maxval;
338
63.8M
  register int rindex = rgb_red[cinfo->in_color_space];
339
63.8M
  register int gindex = rgb_green[cinfo->in_color_space];
340
63.8M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
63.8M
  register int aindex = alpha_index[cinfo->in_color_space];
342
63.8M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
63.8M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
1.51k
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
63.8M
  ptr = source->pub._buffer[0];
347
63.8M
  bufferptr = source->iobuffer;
348
63.8M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
10.0M
    if (aindex >= 0)
350
277k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
9.76M
    else
352
9.76M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
53.7M
  } else {
354
53.7M
    if (aindex >= 0)
355
7.34M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
53.7M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
46.4M
    else
358
46.4M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
53.7M
  }
360
63.8M
  return 1;
361
63.8M
}
rdppm-12.c:get_gray_rgb_row
Line
Count
Source
331
71.5M
{
332
71.5M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
71.5M
  register _JSAMPROW ptr;
334
71.5M
  register U_CHAR *bufferptr;
335
71.5M
  register _JSAMPLE *rescale = source->rescale;
336
71.5M
  JDIMENSION col;
337
71.5M
  unsigned int maxval = source->maxval;
338
71.5M
  register int rindex = rgb_red[cinfo->in_color_space];
339
71.5M
  register int gindex = rgb_green[cinfo->in_color_space];
340
71.5M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
71.5M
  register int aindex = alpha_index[cinfo->in_color_space];
342
71.5M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
71.5M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
615
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
71.5M
  ptr = source->pub._buffer[0];
347
71.5M
  bufferptr = source->iobuffer;
348
71.5M
  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
71.5M
  } else {
354
71.5M
    if (aindex >= 0)
355
14.3M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
71.5M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
57.2M
    else
358
57.2M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
71.5M
  }
360
71.5M
  return 1;
361
71.5M
}
rdppm-16.c:get_gray_rgb_row
Line
Count
Source
331
13.2M
{
332
13.2M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
13.2M
  register _JSAMPROW ptr;
334
13.2M
  register U_CHAR *bufferptr;
335
13.2M
  register _JSAMPLE *rescale = source->rescale;
336
13.2M
  JDIMENSION col;
337
13.2M
  unsigned int maxval = source->maxval;
338
13.2M
  register int rindex = rgb_red[cinfo->in_color_space];
339
13.2M
  register int gindex = rgb_green[cinfo->in_color_space];
340
13.2M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
13.2M
  register int aindex = alpha_index[cinfo->in_color_space];
342
13.2M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
13.2M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
305
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
13.2M
  ptr = source->pub._buffer[0];
347
13.2M
  bufferptr = source->iobuffer;
348
13.2M
  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
13.2M
  } else {
354
13.2M
    if (aindex >= 0)
355
2.64M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
13.2M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
10.5M
    else
358
10.5M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
13.2M
  }
360
13.2M
  return 1;
361
13.2M
}
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
24.5M
{
369
24.5M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
24.5M
  register _JSAMPROW ptr;
371
24.5M
  register U_CHAR *bufferptr;
372
24.5M
  register _JSAMPLE *rescale = source->rescale;
373
24.5M
  JDIMENSION col;
374
24.5M
  unsigned int maxval = source->maxval;
375
376
24.5M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
398
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
24.5M
  ptr = source->pub._buffer[0];
379
24.5M
  bufferptr = source->iobuffer;
380
24.5M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
1.63M
    for (col = cinfo->image_width; col > 0; col--) {
382
1.31M
      _JSAMPLE gray = *bufferptr++;
383
1.31M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
1.31M
      ptr += 4;
385
1.31M
    }
386
24.2M
  } else {
387
92.3M
    for (col = cinfo->image_width; col > 0; col--) {
388
68.0M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
68.0M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
68.0M
      ptr += 4;
391
68.0M
    }
392
24.2M
  }
393
24.5M
  return 1;
394
24.5M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
368
7.61M
{
369
7.61M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
7.61M
  register _JSAMPROW ptr;
371
7.61M
  register U_CHAR *bufferptr;
372
7.61M
  register _JSAMPLE *rescale = source->rescale;
373
7.61M
  JDIMENSION col;
374
7.61M
  unsigned int maxval = source->maxval;
375
376
7.61M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
214
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
7.61M
  ptr = source->pub._buffer[0];
379
7.61M
  bufferptr = source->iobuffer;
380
7.61M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
1.63M
    for (col = cinfo->image_width; col > 0; col--) {
382
1.31M
      _JSAMPLE gray = *bufferptr++;
383
1.31M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
1.31M
      ptr += 4;
385
1.31M
    }
386
7.30M
  } else {
387
29.6M
    for (col = cinfo->image_width; col > 0; col--) {
388
22.3M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
22.3M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
22.3M
      ptr += 4;
391
22.3M
    }
392
7.30M
  }
393
7.61M
  return 1;
394
7.61M
}
rdppm-12.c:get_gray_cmyk_row
Line
Count
Source
368
14.3M
{
369
14.3M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
14.3M
  register _JSAMPROW ptr;
371
14.3M
  register U_CHAR *bufferptr;
372
14.3M
  register _JSAMPLE *rescale = source->rescale;
373
14.3M
  JDIMENSION col;
374
14.3M
  unsigned int maxval = source->maxval;
375
376
14.3M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
123
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
14.3M
  ptr = source->pub._buffer[0];
379
14.3M
  bufferptr = source->iobuffer;
380
14.3M
  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
14.3M
  } else {
387
50.4M
    for (col = cinfo->image_width; col > 0; col--) {
388
36.1M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
36.1M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
36.1M
      ptr += 4;
391
36.1M
    }
392
14.3M
  }
393
14.3M
  return 1;
394
14.3M
}
rdppm-16.c:get_gray_cmyk_row
Line
Count
Source
368
2.64M
{
369
2.64M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.64M
  register _JSAMPROW ptr;
371
2.64M
  register U_CHAR *bufferptr;
372
2.64M
  register _JSAMPLE *rescale = source->rescale;
373
2.64M
  JDIMENSION col;
374
2.64M
  unsigned int maxval = source->maxval;
375
376
2.64M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.64M
  ptr = source->pub._buffer[0];
379
2.64M
  bufferptr = source->iobuffer;
380
2.64M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
0
    for (col = cinfo->image_width; col > 0; col--) {
382
0
      _JSAMPLE gray = *bufferptr++;
383
0
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
0
      ptr += 4;
385
0
    }
386
2.64M
  } else {
387
12.2M
    for (col = cinfo->image_width; col > 0; col--) {
388
9.61M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
9.61M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
9.61M
      ptr += 4;
391
9.61M
    }
392
2.64M
  }
393
2.64M
  return 1;
394
2.64M
}
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
1.26M
{
401
1.26M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
1.26M
  register _JSAMPROW ptr;
403
1.26M
  register U_CHAR *bufferptr;
404
1.26M
  register _JSAMPLE *rescale = source->rescale;
405
1.26M
  JDIMENSION col;
406
1.26M
  unsigned int maxval = source->maxval;
407
1.26M
  register int rindex = rgb_red[cinfo->in_color_space];
408
1.26M
  register int gindex = rgb_green[cinfo->in_color_space];
409
1.26M
  register int bindex = rgb_blue[cinfo->in_color_space];
410
1.26M
  register int aindex = alpha_index[cinfo->in_color_space];
411
1.26M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
1.26M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
3.30k
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
1.26M
  ptr = source->pub._buffer[0];
416
1.26M
  bufferptr = source->iobuffer;
417
1.26M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
36.3k
    if (aindex >= 0)
419
5.74k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
30.5k
    else
421
30.5k
      RGB_READ_LOOP(*bufferptr++, {})
422
1.22M
  } else {
423
1.22M
    if (aindex >= 0)
424
231k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
1.22M
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
992k
    else
427
992k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
1.22M
  }
429
1.26M
  return 1;
430
1.26M
}
rdppm-8.c:get_rgb_row
Line
Count
Source
400
146k
{
401
146k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
146k
  register _JSAMPROW ptr;
403
146k
  register U_CHAR *bufferptr;
404
146k
  register _JSAMPLE *rescale = source->rescale;
405
146k
  JDIMENSION col;
406
146k
  unsigned int maxval = source->maxval;
407
146k
  register int rindex = rgb_red[cinfo->in_color_space];
408
146k
  register int gindex = rgb_green[cinfo->in_color_space];
409
146k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
146k
  register int aindex = alpha_index[cinfo->in_color_space];
411
146k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
146k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
1.88k
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
146k
  ptr = source->pub._buffer[0];
416
146k
  bufferptr = source->iobuffer;
417
146k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
36.3k
    if (aindex >= 0)
419
5.74k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
30.5k
    else
421
30.5k
      RGB_READ_LOOP(*bufferptr++, {})
422
110k
  } else {
423
110k
    if (aindex >= 0)
424
9.16k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
110k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
101k
    else
427
101k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
110k
  }
429
146k
  return 1;
430
146k
}
rdppm-12.c:get_rgb_row
Line
Count
Source
400
1.10M
{
401
1.10M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
1.10M
  register _JSAMPROW ptr;
403
1.10M
  register U_CHAR *bufferptr;
404
1.10M
  register _JSAMPLE *rescale = source->rescale;
405
1.10M
  JDIMENSION col;
406
1.10M
  unsigned int maxval = source->maxval;
407
1.10M
  register int rindex = rgb_red[cinfo->in_color_space];
408
1.10M
  register int gindex = rgb_green[cinfo->in_color_space];
409
1.10M
  register int bindex = rgb_blue[cinfo->in_color_space];
410
1.10M
  register int aindex = alpha_index[cinfo->in_color_space];
411
1.10M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
1.10M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
955
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
1.10M
  ptr = source->pub._buffer[0];
416
1.10M
  bufferptr = source->iobuffer;
417
1.10M
  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
1.10M
  } else {
423
1.10M
    if (aindex >= 0)
424
220k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
1.10M
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
881k
    else
427
881k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
1.10M
  }
429
1.10M
  return 1;
430
1.10M
}
rdppm-16.c:get_rgb_row
Line
Count
Source
400
11.8k
{
401
11.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
11.8k
  register _JSAMPROW ptr;
403
11.8k
  register U_CHAR *bufferptr;
404
11.8k
  register _JSAMPLE *rescale = source->rescale;
405
11.8k
  JDIMENSION col;
406
11.8k
  unsigned int maxval = source->maxval;
407
11.8k
  register int rindex = rgb_red[cinfo->in_color_space];
408
11.8k
  register int gindex = rgb_green[cinfo->in_color_space];
409
11.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
11.8k
  register int aindex = alpha_index[cinfo->in_color_space];
411
11.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
11.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
460
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
11.8k
  ptr = source->pub._buffer[0];
416
11.8k
  bufferptr = source->iobuffer;
417
11.8k
  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
11.8k
  } else {
423
11.8k
    if (aindex >= 0)
424
2.28k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
11.8k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
9.59k
    else
427
9.59k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
11.8k
  }
429
11.8k
  return 1;
430
11.8k
}
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
237k
{
438
237k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
237k
  register _JSAMPROW ptr;
440
237k
  register U_CHAR *bufferptr;
441
237k
  register _JSAMPLE *rescale = source->rescale;
442
237k
  JDIMENSION col;
443
237k
  unsigned int maxval = source->maxval;
444
445
237k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
590
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
237k
  ptr = source->pub._buffer[0];
448
237k
  bufferptr = source->iobuffer;
449
237k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
362k
    for (col = cinfo->image_width; col > 0; col--) {
451
355k
      _JSAMPLE r = *bufferptr++;
452
355k
      _JSAMPLE g = *bufferptr++;
453
355k
      _JSAMPLE b = *bufferptr++;
454
355k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
355k
      ptr += 4;
456
355k
    }
457
230k
  } else {
458
3.93M
    for (col = cinfo->image_width; col > 0; col--) {
459
3.70M
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
3.70M
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
3.70M
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
3.70M
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
3.70M
      ptr += 4;
464
3.70M
    }
465
230k
  }
466
237k
  return 1;
467
237k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
437
15.2k
{
438
15.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
15.2k
  register _JSAMPROW ptr;
440
15.2k
  register U_CHAR *bufferptr;
441
15.2k
  register _JSAMPLE *rescale = source->rescale;
442
15.2k
  JDIMENSION col;
443
15.2k
  unsigned int maxval = source->maxval;
444
445
15.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
307
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
15.2k
  ptr = source->pub._buffer[0];
448
15.2k
  bufferptr = source->iobuffer;
449
15.2k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
362k
    for (col = cinfo->image_width; col > 0; col--) {
451
355k
      _JSAMPLE r = *bufferptr++;
452
355k
      _JSAMPLE g = *bufferptr++;
453
355k
      _JSAMPLE b = *bufferptr++;
454
355k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
355k
      ptr += 4;
456
355k
    }
457
8.19k
  } else {
458
556k
    for (col = cinfo->image_width; col > 0; col--) {
459
548k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
548k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
548k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
548k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
548k
      ptr += 4;
464
548k
    }
465
8.19k
  }
466
15.2k
  return 1;
467
15.2k
}
rdppm-12.c:get_rgb_cmyk_row
Line
Count
Source
437
220k
{
438
220k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
220k
  register _JSAMPROW ptr;
440
220k
  register U_CHAR *bufferptr;
441
220k
  register _JSAMPLE *rescale = source->rescale;
442
220k
  JDIMENSION col;
443
220k
  unsigned int maxval = source->maxval;
444
445
220k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
191
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
220k
  ptr = source->pub._buffer[0];
448
220k
  bufferptr = source->iobuffer;
449
220k
  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
220k
  } else {
458
2.82M
    for (col = cinfo->image_width; col > 0; col--) {
459
2.60M
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
2.60M
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
2.60M
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
2.60M
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
2.60M
      ptr += 4;
464
2.60M
    }
465
220k
  }
466
220k
  return 1;
467
220k
}
rdppm-16.c:get_rgb_cmyk_row
Line
Count
Source
437
2.37k
{
438
2.37k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
2.37k
  register _JSAMPROW ptr;
440
2.37k
  register U_CHAR *bufferptr;
441
2.37k
  register _JSAMPLE *rescale = source->rescale;
442
2.37k
  JDIMENSION col;
443
2.37k
  unsigned int maxval = source->maxval;
444
445
2.37k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
92
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
2.37k
  ptr = source->pub._buffer[0];
448
2.37k
  bufferptr = source->iobuffer;
449
2.37k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
0
    for (col = cinfo->image_width; col > 0; col--) {
451
0
      _JSAMPLE r = *bufferptr++;
452
0
      _JSAMPLE g = *bufferptr++;
453
0
      _JSAMPLE b = *bufferptr++;
454
0
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
0
      ptr += 4;
456
0
    }
457
2.37k
  } else {
458
552k
    for (col = cinfo->image_width; col > 0; col--) {
459
550k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
550k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
550k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
550k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
550k
      ptr += 4;
464
550k
    }
465
2.37k
  }
466
2.37k
  return 1;
467
2.37k
}
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.95M
{
478
1.95M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.95M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
160
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.95M
  return 1;
483
1.95M
}
rdppm-8.c:get_raw_row
Line
Count
Source
477
1.95M
{
478
1.95M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.95M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
160
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.95M
  return 1;
483
1.95M
}
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
71.1k
{
490
71.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
71.1k
  register _JSAMPROW ptr;
492
71.1k
  register U_CHAR *bufferptr;
493
71.1k
  register _JSAMPLE *rescale = source->rescale;
494
71.1k
  JDIMENSION col;
495
71.1k
  unsigned int maxval = source->maxval;
496
497
71.1k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
351
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
71.1k
  ptr = source->pub._buffer[0];
500
71.1k
  bufferptr = source->iobuffer;
501
447k
  for (col = cinfo->image_width; col > 0; col--) {
502
376k
    register unsigned int temp;
503
376k
    temp  = UCH(*bufferptr++) << 8;
504
376k
    temp |= UCH(*bufferptr++);
505
376k
    if (temp > maxval)
506
207
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
376k
    *ptr++ = rescale[temp];
508
376k
  }
509
71.1k
  return 1;
510
71.1k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
489
17.6k
{
490
17.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
17.6k
  register _JSAMPROW ptr;
492
17.6k
  register U_CHAR *bufferptr;
493
17.6k
  register _JSAMPLE *rescale = source->rescale;
494
17.6k
  JDIMENSION col;
495
17.6k
  unsigned int maxval = source->maxval;
496
497
17.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
172
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
17.6k
  ptr = source->pub._buffer[0];
500
17.6k
  bufferptr = source->iobuffer;
501
180k
  for (col = cinfo->image_width; col > 0; col--) {
502
162k
    register unsigned int temp;
503
162k
    temp  = UCH(*bufferptr++) << 8;
504
162k
    temp |= UCH(*bufferptr++);
505
162k
    if (temp > maxval)
506
101
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
162k
    *ptr++ = rescale[temp];
508
162k
  }
509
17.6k
  return 1;
510
17.6k
}
rdppm-12.c:get_word_gray_row
Line
Count
Source
489
48.8k
{
490
48.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
48.8k
  register _JSAMPROW ptr;
492
48.8k
  register U_CHAR *bufferptr;
493
48.8k
  register _JSAMPLE *rescale = source->rescale;
494
48.8k
  JDIMENSION col;
495
48.8k
  unsigned int maxval = source->maxval;
496
497
48.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
119
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
48.8k
  ptr = source->pub._buffer[0];
500
48.8k
  bufferptr = source->iobuffer;
501
164k
  for (col = cinfo->image_width; col > 0; col--) {
502
116k
    register unsigned int temp;
503
116k
    temp  = UCH(*bufferptr++) << 8;
504
116k
    temp |= UCH(*bufferptr++);
505
116k
    if (temp > maxval)
506
72
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
116k
    *ptr++ = rescale[temp];
508
116k
  }
509
48.8k
  return 1;
510
48.8k
}
rdppm-16.c:get_word_gray_row
Line
Count
Source
489
4.64k
{
490
4.64k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
4.64k
  register _JSAMPROW ptr;
492
4.64k
  register U_CHAR *bufferptr;
493
4.64k
  register _JSAMPLE *rescale = source->rescale;
494
4.64k
  JDIMENSION col;
495
4.64k
  unsigned int maxval = source->maxval;
496
497
4.64k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
60
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
4.64k
  ptr = source->pub._buffer[0];
500
4.64k
  bufferptr = source->iobuffer;
501
102k
  for (col = cinfo->image_width; col > 0; col--) {
502
97.6k
    register unsigned int temp;
503
97.6k
    temp  = UCH(*bufferptr++) << 8;
504
97.6k
    temp |= UCH(*bufferptr++);
505
97.6k
    if (temp > maxval)
506
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
97.6k
    *ptr++ = rescale[temp];
508
97.6k
  }
509
4.64k
  return 1;
510
4.64k
}
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
355k
{
517
355k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
355k
  register _JSAMPROW ptr;
519
355k
  register U_CHAR *bufferptr;
520
355k
  register _JSAMPLE *rescale = source->rescale;
521
355k
  JDIMENSION col;
522
355k
  unsigned int maxval = source->maxval;
523
355k
  register int rindex = rgb_red[cinfo->in_color_space];
524
355k
  register int gindex = rgb_green[cinfo->in_color_space];
525
355k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
355k
  register int aindex = alpha_index[cinfo->in_color_space];
527
355k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
355k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
1.75k
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
355k
  ptr = source->pub._buffer[0];
532
355k
  bufferptr = source->iobuffer;
533
2.23M
  for (col = cinfo->image_width; col > 0; col--) {
534
1.88M
    register unsigned int temp;
535
1.88M
    temp  = UCH(*bufferptr++) << 8;
536
1.88M
    temp |= UCH(*bufferptr++);
537
1.88M
    if (temp > maxval)
538
1.03k
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
1.88M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
1.88M
    if (aindex >= 0)
541
351k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
1.88M
    ptr += ps;
543
1.88M
  }
544
355k
  return 1;
545
355k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
516
88.3k
{
517
88.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
88.3k
  register _JSAMPROW ptr;
519
88.3k
  register U_CHAR *bufferptr;
520
88.3k
  register _JSAMPLE *rescale = source->rescale;
521
88.3k
  JDIMENSION col;
522
88.3k
  unsigned int maxval = source->maxval;
523
88.3k
  register int rindex = rgb_red[cinfo->in_color_space];
524
88.3k
  register int gindex = rgb_green[cinfo->in_color_space];
525
88.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
88.3k
  register int aindex = alpha_index[cinfo->in_color_space];
527
88.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
88.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
860
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
88.3k
  ptr = source->pub._buffer[0];
532
88.3k
  bufferptr = source->iobuffer;
533
901k
  for (col = cinfo->image_width; col > 0; col--) {
534
812k
    register unsigned int temp;
535
812k
    temp  = UCH(*bufferptr++) << 8;
536
812k
    temp |= UCH(*bufferptr++);
537
812k
    if (temp > maxval)
538
505
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
812k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
812k
    if (aindex >= 0)
541
137k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
812k
    ptr += ps;
543
812k
  }
544
88.3k
  return 1;
545
88.3k
}
rdppm-12.c:get_word_gray_rgb_row
Line
Count
Source
516
244k
{
517
244k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
244k
  register _JSAMPROW ptr;
519
244k
  register U_CHAR *bufferptr;
520
244k
  register _JSAMPLE *rescale = source->rescale;
521
244k
  JDIMENSION col;
522
244k
  unsigned int maxval = source->maxval;
523
244k
  register int rindex = rgb_red[cinfo->in_color_space];
524
244k
  register int gindex = rgb_green[cinfo->in_color_space];
525
244k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
244k
  register int aindex = alpha_index[cinfo->in_color_space];
527
244k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
244k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
595
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
244k
  ptr = source->pub._buffer[0];
532
244k
  bufferptr = source->iobuffer;
533
824k
  for (col = cinfo->image_width; col > 0; col--) {
534
580k
    register unsigned int temp;
535
580k
    temp  = UCH(*bufferptr++) << 8;
536
580k
    temp |= UCH(*bufferptr++);
537
580k
    if (temp > maxval)
538
360
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
580k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
580k
    if (aindex >= 0)
541
115k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
580k
    ptr += ps;
543
580k
  }
544
244k
  return 1;
545
244k
}
rdppm-16.c:get_word_gray_rgb_row
Line
Count
Source
516
23.2k
{
517
23.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
23.2k
  register _JSAMPROW ptr;
519
23.2k
  register U_CHAR *bufferptr;
520
23.2k
  register _JSAMPLE *rescale = source->rescale;
521
23.2k
  JDIMENSION col;
522
23.2k
  unsigned int maxval = source->maxval;
523
23.2k
  register int rindex = rgb_red[cinfo->in_color_space];
524
23.2k
  register int gindex = rgb_green[cinfo->in_color_space];
525
23.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
23.2k
  register int aindex = alpha_index[cinfo->in_color_space];
527
23.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
23.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
300
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
23.2k
  ptr = source->pub._buffer[0];
532
23.2k
  bufferptr = source->iobuffer;
533
511k
  for (col = cinfo->image_width; col > 0; col--) {
534
488k
    register unsigned int temp;
535
488k
    temp  = UCH(*bufferptr++) << 8;
536
488k
    temp |= UCH(*bufferptr++);
537
488k
    if (temp > maxval)
538
170
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
488k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
488k
    if (aindex >= 0)
541
97.5k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
488k
    ptr += ps;
543
488k
  }
544
23.2k
  return 1;
545
23.2k
}
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
58.4k
{
552
58.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
58.4k
  register _JSAMPROW ptr;
554
58.4k
  register U_CHAR *bufferptr;
555
58.4k
  register _JSAMPLE *rescale = source->rescale;
556
58.4k
  JDIMENSION col;
557
58.4k
  unsigned int maxval = source->maxval;
558
559
58.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
298
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
58.4k
  ptr = source->pub._buffer[0];
562
58.4k
  bufferptr = source->iobuffer;
563
409k
  for (col = cinfo->image_width; col > 0; col--) {
564
351k
    register unsigned int gray;
565
351k
    gray  = UCH(*bufferptr++) << 8;
566
351k
    gray |= UCH(*bufferptr++);
567
351k
    if (gray > maxval)
568
181
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
351k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
351k
                ptr + 1, ptr + 2, ptr + 3);
571
351k
    ptr += 4;
572
351k
  }
573
58.4k
  return 1;
574
58.4k
}
rdppm-8.c:get_word_gray_cmyk_row
Line
Count
Source
551
5.00k
{
552
5.00k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
5.00k
  register _JSAMPROW ptr;
554
5.00k
  register U_CHAR *bufferptr;
555
5.00k
  register _JSAMPLE *rescale = source->rescale;
556
5.00k
  JDIMENSION col;
557
5.00k
  unsigned int maxval = source->maxval;
558
559
5.00k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
119
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
5.00k
  ptr = source->pub._buffer[0];
562
5.00k
  bufferptr = source->iobuffer;
563
142k
  for (col = cinfo->image_width; col > 0; col--) {
564
137k
    register unsigned int gray;
565
137k
    gray  = UCH(*bufferptr++) << 8;
566
137k
    gray |= UCH(*bufferptr++);
567
137k
    if (gray > maxval)
568
75
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
137k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
137k
                ptr + 1, ptr + 2, ptr + 3);
571
137k
    ptr += 4;
572
137k
  }
573
5.00k
  return 1;
574
5.00k
}
rdppm-12.c:get_word_gray_cmyk_row
Line
Count
Source
551
48.8k
{
552
48.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
48.8k
  register _JSAMPROW ptr;
554
48.8k
  register U_CHAR *bufferptr;
555
48.8k
  register _JSAMPLE *rescale = source->rescale;
556
48.8k
  JDIMENSION col;
557
48.8k
  unsigned int maxval = source->maxval;
558
559
48.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
119
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
48.8k
  ptr = source->pub._buffer[0];
562
48.8k
  bufferptr = source->iobuffer;
563
164k
  for (col = cinfo->image_width; col > 0; col--) {
564
116k
    register unsigned int gray;
565
116k
    gray  = UCH(*bufferptr++) << 8;
566
116k
    gray |= UCH(*bufferptr++);
567
116k
    if (gray > maxval)
568
72
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
116k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
116k
                ptr + 1, ptr + 2, ptr + 3);
571
116k
    ptr += 4;
572
116k
  }
573
48.8k
  return 1;
574
48.8k
}
rdppm-16.c:get_word_gray_cmyk_row
Line
Count
Source
551
4.64k
{
552
4.64k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
4.64k
  register _JSAMPROW ptr;
554
4.64k
  register U_CHAR *bufferptr;
555
4.64k
  register _JSAMPLE *rescale = source->rescale;
556
4.64k
  JDIMENSION col;
557
4.64k
  unsigned int maxval = source->maxval;
558
559
4.64k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
60
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
4.64k
  ptr = source->pub._buffer[0];
562
4.64k
  bufferptr = source->iobuffer;
563
102k
  for (col = cinfo->image_width; col > 0; col--) {
564
97.6k
    register unsigned int gray;
565
97.6k
    gray  = UCH(*bufferptr++) << 8;
566
97.6k
    gray |= UCH(*bufferptr++);
567
97.6k
    if (gray > maxval)
568
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
97.6k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
97.6k
                ptr + 1, ptr + 2, ptr + 3);
571
97.6k
    ptr += 4;
572
97.6k
  }
573
4.64k
  return 1;
574
4.64k
}
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
94.6k
{
581
94.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
94.6k
  register _JSAMPROW ptr;
583
94.6k
  register U_CHAR *bufferptr;
584
94.6k
  register _JSAMPLE *rescale = source->rescale;
585
94.6k
  JDIMENSION col;
586
94.6k
  unsigned int maxval = source->maxval;
587
94.6k
  register int rindex = rgb_red[cinfo->in_color_space];
588
94.6k
  register int gindex = rgb_green[cinfo->in_color_space];
589
94.6k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
94.6k
  register int aindex = alpha_index[cinfo->in_color_space];
591
94.6k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
94.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
2.05k
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
94.6k
  ptr = source->pub._buffer[0];
596
94.6k
  bufferptr = source->iobuffer;
597
325k
  for (col = cinfo->image_width; col > 0; col--) {
598
230k
    register unsigned int temp;
599
230k
    temp  = UCH(*bufferptr++) << 8;
600
230k
    temp |= UCH(*bufferptr++);
601
230k
    if (temp > maxval)
602
825
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
230k
    ptr[rindex] = rescale[temp];
604
230k
    temp  = UCH(*bufferptr++) << 8;
605
230k
    temp |= UCH(*bufferptr++);
606
230k
    if (temp > maxval)
607
655
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
230k
    ptr[gindex] = rescale[temp];
609
230k
    temp  = UCH(*bufferptr++) << 8;
610
230k
    temp |= UCH(*bufferptr++);
611
230k
    if (temp > maxval)
612
660
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
230k
    ptr[bindex] = rescale[temp];
614
230k
    if (aindex >= 0)
615
42.8k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
230k
    ptr += ps;
617
230k
  }
618
94.6k
  return 1;
619
94.6k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
580
26.3k
{
581
26.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
26.3k
  register _JSAMPROW ptr;
583
26.3k
  register U_CHAR *bufferptr;
584
26.3k
  register _JSAMPLE *rescale = source->rescale;
585
26.3k
  JDIMENSION col;
586
26.3k
  unsigned int maxval = source->maxval;
587
26.3k
  register int rindex = rgb_red[cinfo->in_color_space];
588
26.3k
  register int gindex = rgb_green[cinfo->in_color_space];
589
26.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
26.3k
  register int aindex = alpha_index[cinfo->in_color_space];
591
26.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
26.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
895
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
26.3k
  ptr = source->pub._buffer[0];
596
26.3k
  bufferptr = source->iobuffer;
597
72.2k
  for (col = cinfo->image_width; col > 0; col--) {
598
45.9k
    register unsigned int temp;
599
45.9k
    temp  = UCH(*bufferptr++) << 8;
600
45.9k
    temp |= UCH(*bufferptr++);
601
45.9k
    if (temp > maxval)
602
440
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
45.9k
    ptr[rindex] = rescale[temp];
604
45.9k
    temp  = UCH(*bufferptr++) << 8;
605
45.9k
    temp |= UCH(*bufferptr++);
606
45.9k
    if (temp > maxval)
607
355
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
45.9k
    ptr[gindex] = rescale[temp];
609
45.9k
    temp  = UCH(*bufferptr++) << 8;
610
45.9k
    temp |= UCH(*bufferptr++);
611
45.9k
    if (temp > maxval)
612
335
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
45.9k
    ptr[bindex] = rescale[temp];
614
45.9k
    if (aindex >= 0)
615
6.03k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
45.9k
    ptr += ps;
617
45.9k
  }
618
26.3k
  return 1;
619
26.3k
}
rdppm-12.c:get_word_rgb_row
Line
Count
Source
580
56.4k
{
581
56.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
56.4k
  register _JSAMPROW ptr;
583
56.4k
  register U_CHAR *bufferptr;
584
56.4k
  register _JSAMPLE *rescale = source->rescale;
585
56.4k
  JDIMENSION col;
586
56.4k
  unsigned int maxval = source->maxval;
587
56.4k
  register int rindex = rgb_red[cinfo->in_color_space];
588
56.4k
  register int gindex = rgb_green[cinfo->in_color_space];
589
56.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
56.4k
  register int aindex = alpha_index[cinfo->in_color_space];
591
56.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
56.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
750
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
56.4k
  ptr = source->pub._buffer[0];
596
56.4k
  bufferptr = source->iobuffer;
597
154k
  for (col = cinfo->image_width; col > 0; col--) {
598
97.8k
    register unsigned int temp;
599
97.8k
    temp  = UCH(*bufferptr++) << 8;
600
97.8k
    temp |= UCH(*bufferptr++);
601
97.8k
    if (temp > maxval)
602
225
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
97.8k
    ptr[rindex] = rescale[temp];
604
97.8k
    temp  = UCH(*bufferptr++) << 8;
605
97.8k
    temp |= UCH(*bufferptr++);
606
97.8k
    if (temp > maxval)
607
200
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
97.8k
    ptr[gindex] = rescale[temp];
609
97.8k
    temp  = UCH(*bufferptr++) << 8;
610
97.8k
    temp |= UCH(*bufferptr++);
611
97.8k
    if (temp > maxval)
612
215
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
97.8k
    ptr[bindex] = rescale[temp];
614
97.8k
    if (aindex >= 0)
615
19.4k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
97.8k
    ptr += ps;
617
97.8k
  }
618
56.4k
  return 1;
619
56.4k
}
rdppm-16.c:get_word_rgb_row
Line
Count
Source
580
11.8k
{
581
11.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
11.8k
  register _JSAMPROW ptr;
583
11.8k
  register U_CHAR *bufferptr;
584
11.8k
  register _JSAMPLE *rescale = source->rescale;
585
11.8k
  JDIMENSION col;
586
11.8k
  unsigned int maxval = source->maxval;
587
11.8k
  register int rindex = rgb_red[cinfo->in_color_space];
588
11.8k
  register int gindex = rgb_green[cinfo->in_color_space];
589
11.8k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
11.8k
  register int aindex = alpha_index[cinfo->in_color_space];
591
11.8k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
11.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
410
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
11.8k
  ptr = source->pub._buffer[0];
596
11.8k
  bufferptr = source->iobuffer;
597
98.9k
  for (col = cinfo->image_width; col > 0; col--) {
598
87.0k
    register unsigned int temp;
599
87.0k
    temp  = UCH(*bufferptr++) << 8;
600
87.0k
    temp |= UCH(*bufferptr++);
601
87.0k
    if (temp > maxval)
602
160
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
87.0k
    ptr[rindex] = rescale[temp];
604
87.0k
    temp  = UCH(*bufferptr++) << 8;
605
87.0k
    temp |= UCH(*bufferptr++);
606
87.0k
    if (temp > maxval)
607
100
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
87.0k
    ptr[gindex] = rescale[temp];
609
87.0k
    temp  = UCH(*bufferptr++) << 8;
610
87.0k
    temp |= UCH(*bufferptr++);
611
87.0k
    if (temp > maxval)
612
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
87.0k
    ptr[bindex] = rescale[temp];
614
87.0k
    if (aindex >= 0)
615
17.3k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
87.0k
    ptr += ps;
617
87.0k
  }
618
11.8k
  return 1;
619
11.8k
}
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
17.2k
{
626
17.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
17.2k
  register _JSAMPROW ptr;
628
17.2k
  register U_CHAR *bufferptr;
629
17.2k
  register _JSAMPLE *rescale = source->rescale;
630
17.2k
  JDIMENSION col;
631
17.2k
  unsigned int maxval = source->maxval;
632
633
17.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
372
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
17.2k
  ptr = source->pub._buffer[0];
636
17.2k
  bufferptr = source->iobuffer;
637
60.4k
  for (col = cinfo->image_width; col > 0; col--) {
638
43.1k
    register unsigned int r, g, b;
639
43.1k
    r  = UCH(*bufferptr++) << 8;
640
43.1k
    r |= UCH(*bufferptr++);
641
43.1k
    if (r > maxval)
642
134
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
43.1k
    g  = UCH(*bufferptr++) << 8;
644
43.1k
    g |= UCH(*bufferptr++);
645
43.1k
    if (g > maxval)
646
109
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
43.1k
    b  = UCH(*bufferptr++) << 8;
648
43.1k
    b |= UCH(*bufferptr++);
649
43.1k
    if (b > maxval)
650
109
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
43.1k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
43.1k
                ptr + 2, ptr + 3);
653
43.1k
    ptr += 4;
654
43.1k
  }
655
17.2k
  return 1;
656
17.2k
}
rdppm-8.c:get_word_rgb_cmyk_row
Line
Count
Source
625
3.59k
{
626
3.59k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
3.59k
  register _JSAMPROW ptr;
628
3.59k
  register U_CHAR *bufferptr;
629
3.59k
  register _JSAMPLE *rescale = source->rescale;
630
3.59k
  JDIMENSION col;
631
3.59k
  unsigned int maxval = source->maxval;
632
633
3.59k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
140
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
3.59k
  ptr = source->pub._buffer[0];
636
3.59k
  bufferptr = source->iobuffer;
637
9.77k
  for (col = cinfo->image_width; col > 0; col--) {
638
6.18k
    register unsigned int r, g, b;
639
6.18k
    r  = UCH(*bufferptr++) << 8;
640
6.18k
    r |= UCH(*bufferptr++);
641
6.18k
    if (r > maxval)
642
57
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
6.18k
    g  = UCH(*bufferptr++) << 8;
644
6.18k
    g |= UCH(*bufferptr++);
645
6.18k
    if (g > maxval)
646
49
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
6.18k
    b  = UCH(*bufferptr++) << 8;
648
6.18k
    b |= UCH(*bufferptr++);
649
6.18k
    if (b > maxval)
650
44
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
6.18k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
6.18k
                ptr + 2, ptr + 3);
653
6.18k
    ptr += 4;
654
6.18k
  }
655
3.59k
  return 1;
656
3.59k
}
rdppm-12.c:get_word_rgb_cmyk_row
Line
Count
Source
625
11.2k
{
626
11.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
11.2k
  register _JSAMPROW ptr;
628
11.2k
  register U_CHAR *bufferptr;
629
11.2k
  register _JSAMPLE *rescale = source->rescale;
630
11.2k
  JDIMENSION col;
631
11.2k
  unsigned int maxval = source->maxval;
632
633
11.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
150
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
11.2k
  ptr = source->pub._buffer[0];
636
11.2k
  bufferptr = source->iobuffer;
637
30.8k
  for (col = cinfo->image_width; col > 0; col--) {
638
19.5k
    register unsigned int r, g, b;
639
19.5k
    r  = UCH(*bufferptr++) << 8;
640
19.5k
    r |= UCH(*bufferptr++);
641
19.5k
    if (r > maxval)
642
45
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
19.5k
    g  = UCH(*bufferptr++) << 8;
644
19.5k
    g |= UCH(*bufferptr++);
645
19.5k
    if (g > maxval)
646
40
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
19.5k
    b  = UCH(*bufferptr++) << 8;
648
19.5k
    b |= UCH(*bufferptr++);
649
19.5k
    if (b > maxval)
650
43
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
19.5k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
19.5k
                ptr + 2, ptr + 3);
653
19.5k
    ptr += 4;
654
19.5k
  }
655
11.2k
  return 1;
656
11.2k
}
rdppm-16.c:get_word_rgb_cmyk_row
Line
Count
Source
625
2.37k
{
626
2.37k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
2.37k
  register _JSAMPROW ptr;
628
2.37k
  register U_CHAR *bufferptr;
629
2.37k
  register _JSAMPLE *rescale = source->rescale;
630
2.37k
  JDIMENSION col;
631
2.37k
  unsigned int maxval = source->maxval;
632
633
2.37k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
82
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
2.37k
  ptr = source->pub._buffer[0];
636
2.37k
  bufferptr = source->iobuffer;
637
19.7k
  for (col = cinfo->image_width; col > 0; col--) {
638
17.4k
    register unsigned int r, g, b;
639
17.4k
    r  = UCH(*bufferptr++) << 8;
640
17.4k
    r |= UCH(*bufferptr++);
641
17.4k
    if (r > maxval)
642
32
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
17.4k
    g  = UCH(*bufferptr++) << 8;
644
17.4k
    g |= UCH(*bufferptr++);
645
17.4k
    if (g > maxval)
646
20
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
17.4k
    b  = UCH(*bufferptr++) << 8;
648
17.4k
    b |= UCH(*bufferptr++);
649
17.4k
    if (b > maxval)
650
22
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
17.4k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
17.4k
                ptr + 2, ptr + 3);
653
17.4k
    ptr += 4;
654
17.4k
  }
655
2.37k
  return 1;
656
2.37k
}
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
99.6k
{
666
99.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
99.6k
  int c;
668
99.6k
  unsigned int w, h, maxval;
669
99.6k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
99.6k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
99.6k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
99.6k
  switch (c) {
678
8.78k
  case '2':                     /* it's a text-format PGM file */
679
18.4k
  case '3':                     /* it's a text-format PPM file */
680
79.1k
  case '5':                     /* it's a raw-format PGM file */
681
99.5k
  case '6':                     /* it's a raw-format PPM file */
682
99.5k
    break;
683
62
  default:
684
62
    ERREXIT(cinfo, JERR_PPM_NOT);
685
62
    break;
686
99.6k
  }
687
688
  /* fetch the remaining header info */
689
99.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
99.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
99.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
99.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
192
    ERREXIT(cinfo, JERR_PPM_NOT);
695
99.5k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
1.44k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
99.5k
  cinfo->image_width = (JDIMENSION)w;
699
99.5k
  cinfo->image_height = (JDIMENSION)h;
700
99.5k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
99.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
99.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
99.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
99.5k
  switch (c) {
708
6.72k
  case '2':                     /* it's a text-format PGM file */
709
6.72k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
6.72k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
6.72k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
6.72k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
980
      source->pub.get_pixel_rows = get_text_gray_row;
715
5.74k
    else if (IsExtRGB(cinfo->in_color_space))
716
4.90k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
840
    else if (cinfo->in_color_space == JCS_CMYK)
718
840
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
6.72k
    need_iobuffer = FALSE;
722
6.72k
    break;
723
724
7.55k
  case '3':                     /* it's a text-format PPM file */
725
7.55k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
7.55k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
7.55k
    if (IsExtRGB(cinfo->in_color_space))
729
5.48k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
2.07k
    else if (cinfo->in_color_space == JCS_CMYK)
731
976
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
1.09k
    else
733
1.09k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
7.55k
    need_iobuffer = FALSE;
735
7.55k
    break;
736
737
58.4k
  case '5':                     /* it's a raw-format PGM file */
738
58.4k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
58.4k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
58.4k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
58.4k
    if (maxval > 255) {
743
4.24k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
618
        source->pub.get_pixel_rows = get_word_gray_row;
745
3.62k
      else if (IsExtRGB(cinfo->in_color_space))
746
3.09k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
533
      else if (cinfo->in_color_space == JCS_CMYK)
748
533
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
54.1k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
54.1k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
54.1k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
319
      source->pub.get_pixel_rows = get_raw_row;
755
319
      use_raw_buffer = TRUE;
756
319
      need_rescale = FALSE;
757
53.8k
    } else {
758
53.8k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
7.66k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
46.1k
      else if (IsExtRGB(cinfo->in_color_space))
761
39.8k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
6.28k
      else if (cinfo->in_color_space == JCS_CMYK)
763
6.28k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
53.8k
    }
767
58.4k
    break;
768
769
18.5k
  case '6':                     /* it's a raw-format PPM file */
770
18.5k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
18.5k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
18.5k
    if (maxval > 255) {
774
6.17k
      if (IsExtRGB(cinfo->in_color_space))
775
4.49k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
1.67k
      else if (cinfo->in_color_space == JCS_CMYK)
777
778
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
899
      else
779
899
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
12.3k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
12.3k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
12.3k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
12.3k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
1.25k
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
175
      source->pub.get_pixel_rows = get_raw_row;
789
175
      use_raw_buffer = TRUE;
790
175
      need_rescale = FALSE;
791
12.2k
    } else {
792
12.2k
      if (IsExtRGB(cinfo->in_color_space))
793
8.86k
        source->pub.get_pixel_rows = get_rgb_row;
794
3.34k
      else if (cinfo->in_color_space == JCS_CMYK)
795
1.53k
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
1.80k
      else
797
1.80k
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
12.2k
    }
799
18.5k
    break;
800
99.5k
  }
801
802
87.4k
  if (IsExtRGB(cinfo->in_color_space))
803
66.9k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
20.5k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
9.57k
    cinfo->input_components = 1;
806
10.9k
  else if (cinfo->in_color_space == JCS_CMYK)
807
10.9k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
87.4k
  if (need_iobuffer) {
811
74.2k
    if (c == '6')
812
15.8k
      source->buffer_width = (size_t)w * 3 *
813
15.8k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
58.4k
    else
815
58.4k
      source->buffer_width = (size_t)w *
816
58.4k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
74.2k
    source->iobuffer = (U_CHAR *)
818
74.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
74.2k
                                  source->buffer_width);
820
74.2k
  }
821
822
  /* Create compressor input buffer. */
823
87.4k
  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
494
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
494
    source->pub._buffer = &source->pixrow;
828
494
    source->pub.buffer_height = 1;
829
86.9k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
86.9k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
86.9k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
86.9k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
86.9k
    source->pub.buffer_height = 1;
835
86.9k
  }
836
837
  /* Compute the rescaling array if required. */
838
87.4k
  if (need_rescale) {
839
86.9k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
86.9k
    source->rescale = (_JSAMPLE *)
843
86.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
86.9k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
86.9k
                                           sizeof(_JSAMPLE)));
846
86.9k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
86.9k
                                        sizeof(_JSAMPLE)));
848
86.9k
    half_maxval = maxval / 2;
849
186M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
186M
      source->rescale[val] =
852
186M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
186M
                   maxval);
854
186M
    }
855
86.9k
  }
856
87.4k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
665
48.1k
{
666
48.1k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
48.1k
  int c;
668
48.1k
  unsigned int w, h, maxval;
669
48.1k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
48.1k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
48.1k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
48.1k
  switch (c) {
678
4.22k
  case '2':                     /* it's a text-format PGM file */
679
8.58k
  case '3':                     /* it's a text-format PPM file */
680
38.1k
  case '5':                     /* it's a raw-format PGM file */
681
48.1k
  case '6':                     /* it's a raw-format PPM file */
682
48.1k
    break;
683
20
  default:
684
20
    ERREXIT(cinfo, JERR_PPM_NOT);
685
20
    break;
686
48.1k
  }
687
688
  /* fetch the remaining header info */
689
48.1k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
48.1k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
48.1k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
48.1k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
94
    ERREXIT(cinfo, JERR_PPM_NOT);
695
48.1k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
693
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
48.1k
  cinfo->image_width = (JDIMENSION)w;
699
48.1k
  cinfo->image_height = (JDIMENSION)h;
700
48.1k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
48.1k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
48.1k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
48.1k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
48.1k
  switch (c) {
708
3.22k
  case '2':                     /* it's a text-format PGM file */
709
3.22k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
3.22k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
3.22k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
3.22k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
480
      source->pub.get_pixel_rows = get_text_gray_row;
715
2.74k
    else if (IsExtRGB(cinfo->in_color_space))
716
2.40k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
340
    else if (cinfo->in_color_space == JCS_CMYK)
718
340
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
3.22k
    need_iobuffer = FALSE;
722
3.22k
    break;
723
724
3.40k
  case '3':                     /* it's a text-format PPM file */
725
3.40k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
3.40k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
3.40k
    if (IsExtRGB(cinfo->in_color_space))
729
2.51k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
885
    else if (cinfo->in_color_space == JCS_CMYK)
731
382
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
503
    else
733
503
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
3.40k
    need_iobuffer = FALSE;
735
3.40k
    break;
736
737
28.4k
  case '5':                     /* it's a raw-format PGM file */
738
28.4k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
28.4k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
28.4k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
28.4k
    if (maxval > 255) {
743
1.96k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
293
        source->pub.get_pixel_rows = get_word_gray_row;
745
1.67k
      else if (IsExtRGB(cinfo->in_color_space))
746
1.46k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
208
      else if (cinfo->in_color_space == JCS_CMYK)
748
208
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
26.5k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
26.5k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
26.5k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
319
      source->pub.get_pixel_rows = get_raw_row;
755
319
      use_raw_buffer = TRUE;
756
319
      need_rescale = FALSE;
757
26.1k
    } else {
758
26.1k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
3.71k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
22.4k
      else if (IsExtRGB(cinfo->in_color_space))
761
20.1k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
2.33k
      else if (cinfo->in_color_space == JCS_CMYK)
763
2.33k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
26.1k
    }
767
28.4k
    break;
768
769
9.00k
  case '6':                     /* it's a raw-format PPM file */
770
9.00k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
9.00k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
9.00k
    if (maxval > 255) {
774
2.86k
      if (IsExtRGB(cinfo->in_color_space))
775
2.13k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
733
      else if (cinfo->in_color_space == JCS_CMYK)
777
306
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
427
      else
779
427
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
6.13k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
6.13k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
6.13k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
6.13k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
1.25k
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
175
      source->pub.get_pixel_rows = get_raw_row;
789
175
      use_raw_buffer = TRUE;
790
175
      need_rescale = FALSE;
791
5.96k
    } else {
792
5.96k
      if (IsExtRGB(cinfo->in_color_space))
793
4.40k
        source->pub.get_pixel_rows = get_rgb_row;
794
1.56k
      else if (cinfo->in_color_space == JCS_CMYK)
795
646
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
915
      else
797
915
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
5.96k
    }
799
9.00k
    break;
800
48.1k
  }
801
802
42.2k
  if (IsExtRGB(cinfo->in_color_space))
803
33.2k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
9.02k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
4.80k
    cinfo->input_components = 1;
806
4.22k
  else if (cinfo->in_color_space == JCS_CMYK)
807
4.22k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
42.2k
  if (need_iobuffer) {
811
36.1k
    if (c == '6')
812
7.66k
      source->buffer_width = (size_t)w * 3 *
813
7.66k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
28.4k
    else
815
28.4k
      source->buffer_width = (size_t)w *
816
28.4k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
36.1k
    source->iobuffer = (U_CHAR *)
818
36.1k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
36.1k
                                  source->buffer_width);
820
36.1k
  }
821
822
  /* Create compressor input buffer. */
823
42.2k
  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
494
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
494
    source->pub._buffer = &source->pixrow;
828
494
    source->pub.buffer_height = 1;
829
41.7k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
41.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
41.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
41.7k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
41.7k
    source->pub.buffer_height = 1;
835
41.7k
  }
836
837
  /* Compute the rescaling array if required. */
838
42.2k
  if (need_rescale) {
839
41.7k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
41.7k
    source->rescale = (_JSAMPLE *)
843
41.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
41.7k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
41.7k
                                           sizeof(_JSAMPLE)));
846
41.7k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
41.7k
                                        sizeof(_JSAMPLE)));
848
41.7k
    half_maxval = maxval / 2;
849
51.3M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
51.3M
      source->rescale[val] =
852
51.3M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
51.3M
                   maxval);
854
51.3M
    }
855
41.7k
  }
856
42.2k
}
rdppm-12.c:start_input_ppm
Line
Count
Source
665
39.7k
{
666
39.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
39.7k
  int c;
668
39.7k
  unsigned int w, h, maxval;
669
39.7k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
39.7k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
39.7k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
39.7k
  switch (c) {
678
3.03k
  case '2':                     /* it's a text-format PGM file */
679
6.32k
  case '3':                     /* it's a text-format PPM file */
680
31.7k
  case '5':                     /* it's a raw-format PGM file */
681
39.7k
  case '6':                     /* it's a raw-format PPM file */
682
39.7k
    break;
683
28
  default:
684
28
    ERREXIT(cinfo, JERR_PPM_NOT);
685
28
    break;
686
39.7k
  }
687
688
  /* fetch the remaining header info */
689
39.7k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
39.7k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
39.7k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
39.7k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
63
    ERREXIT(cinfo, JERR_PPM_NOT);
695
39.7k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
490
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
39.7k
  cinfo->image_width = (JDIMENSION)w;
699
39.7k
  cinfo->image_height = (JDIMENSION)h;
700
39.7k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
39.7k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
39.7k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
39.7k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
39.7k
  switch (c) {
708
2.27k
  case '2':                     /* it's a text-format PGM file */
709
2.27k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
2.27k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
2.27k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
2.27k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
325
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.95k
    else if (IsExtRGB(cinfo->in_color_space))
716
1.62k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
325
    else if (cinfo->in_color_space == JCS_CMYK)
718
325
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
2.27k
    need_iobuffer = FALSE;
722
2.27k
    break;
723
724
2.59k
  case '3':                     /* it's a text-format PPM file */
725
2.59k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
2.59k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
2.59k
    if (IsExtRGB(cinfo->in_color_space))
729
1.85k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
740
    else if (cinfo->in_color_space == JCS_CMYK)
731
370
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
370
    else
733
370
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
2.59k
    need_iobuffer = FALSE;
735
2.59k
    break;
736
737
24.6k
  case '5':                     /* it's a raw-format PGM file */
738
24.6k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
24.6k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
24.6k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
24.6k
    if (maxval > 255) {
743
1.44k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
207
        source->pub.get_pixel_rows = get_word_gray_row;
745
1.24k
      else if (IsExtRGB(cinfo->in_color_space))
746
1.03k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
207
      else if (cinfo->in_color_space == JCS_CMYK)
748
207
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
23.2k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
23.2k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
23.2k
               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
23.2k
    } else {
758
23.2k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
3.31k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
19.9k
      else if (IsExtRGB(cinfo->in_color_space))
761
16.5k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
3.31k
      else if (cinfo->in_color_space == JCS_CMYK)
763
3.31k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
23.2k
    }
767
24.6k
    break;
768
769
7.33k
  case '6':                     /* it's a raw-format PPM file */
770
7.33k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
7.33k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
7.33k
    if (maxval > 255) {
774
2.12k
      if (IsExtRGB(cinfo->in_color_space))
775
1.52k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
608
      else if (cinfo->in_color_space == JCS_CMYK)
777
304
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
304
      else
779
304
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
5.20k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
5.20k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
5.20k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
5.20k
               (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
5.20k
    } else {
792
5.20k
      if (IsExtRGB(cinfo->in_color_space))
793
3.72k
        source->pub.get_pixel_rows = get_rgb_row;
794
1.48k
      else if (cinfo->in_color_space == JCS_CMYK)
795
744
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
744
      else
797
744
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
5.20k
    }
799
7.33k
    break;
800
39.7k
  }
801
802
35.4k
  if (IsExtRGB(cinfo->in_color_space))
803
26.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
9.11k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
3.85k
    cinfo->input_components = 1;
806
5.26k
  else if (cinfo->in_color_space == JCS_CMYK)
807
5.26k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
35.4k
  if (need_iobuffer) {
811
30.9k
    if (c == '6')
812
6.28k
      source->buffer_width = (size_t)w * 3 *
813
6.28k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
24.6k
    else
815
24.6k
      source->buffer_width = (size_t)w *
816
24.6k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
30.9k
    source->iobuffer = (U_CHAR *)
818
30.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
30.9k
                                  source->buffer_width);
820
30.9k
  }
821
822
  /* Create compressor input buffer. */
823
35.4k
  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
35.4k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
35.4k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
35.4k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
35.4k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
35.4k
    source->pub.buffer_height = 1;
835
35.4k
  }
836
837
  /* Compute the rescaling array if required. */
838
35.4k
  if (need_rescale) {
839
35.4k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
35.4k
    source->rescale = (_JSAMPLE *)
843
35.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
35.4k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
35.4k
                                           sizeof(_JSAMPLE)));
846
35.4k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
35.4k
                                        sizeof(_JSAMPLE)));
848
35.4k
    half_maxval = maxval / 2;
849
53.4M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
53.4M
      source->rescale[val] =
852
53.4M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
53.4M
                   maxval);
854
53.4M
    }
855
35.4k
  }
856
35.4k
}
rdppm-16.c:start_input_ppm
Line
Count
Source
665
11.7k
{
666
11.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
11.7k
  int c;
668
11.7k
  unsigned int w, h, maxval;
669
11.7k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
11.7k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
11.7k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
11.7k
  switch (c) {
678
1.52k
  case '2':                     /* it's a text-format PGM file */
679
3.58k
  case '3':                     /* it's a text-format PPM file */
680
9.21k
  case '5':                     /* it's a raw-format PGM file */
681
11.7k
  case '6':                     /* it's a raw-format PPM file */
682
11.7k
    break;
683
14
  default:
684
14
    ERREXIT(cinfo, JERR_PPM_NOT);
685
14
    break;
686
11.7k
  }
687
688
  /* fetch the remaining header info */
689
11.7k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
11.7k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
11.7k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
11.7k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
11.7k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
259
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
11.7k
  cinfo->image_width = (JDIMENSION)w;
699
11.7k
  cinfo->image_height = (JDIMENSION)h;
700
11.7k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
11.7k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
11.7k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
11.7k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
11.7k
  switch (c) {
708
1.22k
  case '2':                     /* it's a text-format PGM file */
709
1.22k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.22k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.22k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.22k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
175
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.05k
    else if (IsExtRGB(cinfo->in_color_space))
716
875
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
175
    else if (cinfo->in_color_space == JCS_CMYK)
718
175
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.22k
    need_iobuffer = FALSE;
722
1.22k
    break;
723
724
1.56k
  case '3':                     /* it's a text-format PPM file */
725
1.56k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.56k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.56k
    if (IsExtRGB(cinfo->in_color_space))
729
1.12k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
448
    else if (cinfo->in_color_space == JCS_CMYK)
731
224
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
224
    else
733
224
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.56k
    need_iobuffer = FALSE;
735
1.56k
    break;
736
737
5.25k
  case '5':                     /* it's a raw-format PGM file */
738
5.25k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
5.25k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
5.25k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
5.25k
    if (maxval > 255) {
743
826
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
118
        source->pub.get_pixel_rows = get_word_gray_row;
745
708
      else if (IsExtRGB(cinfo->in_color_space))
746
590
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
118
      else if (cinfo->in_color_space == JCS_CMYK)
748
118
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
4.42k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
4.42k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
4.42k
               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
4.42k
    } else {
758
4.42k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
632
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
3.79k
      else if (IsExtRGB(cinfo->in_color_space))
761
3.16k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
632
      else if (cinfo->in_color_space == JCS_CMYK)
763
632
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
4.42k
    }
767
5.25k
    break;
768
769
2.21k
  case '6':                     /* it's a raw-format PPM file */
770
2.21k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.21k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.21k
    if (maxval > 255) {
774
1.17k
      if (IsExtRGB(cinfo->in_color_space))
775
840
        source->pub.get_pixel_rows = get_word_rgb_row;
776
336
      else if (cinfo->in_color_space == JCS_CMYK)
777
168
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
168
      else
779
168
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.17k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
1.04k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.04k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.04k
               (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
1.04k
    } else {
792
1.04k
      if (IsExtRGB(cinfo->in_color_space))
793
745
        source->pub.get_pixel_rows = get_rgb_row;
794
298
      else if (cinfo->in_color_space == JCS_CMYK)
795
149
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
149
      else
797
149
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.04k
    }
799
2.21k
    break;
800
11.7k
  }
801
802
9.72k
  if (IsExtRGB(cinfo->in_color_space))
803
7.33k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.39k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
925
    cinfo->input_components = 1;
806
1.46k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.46k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
9.72k
  if (need_iobuffer) {
811
7.15k
    if (c == '6')
812
1.90k
      source->buffer_width = (size_t)w * 3 *
813
1.90k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
5.25k
    else
815
5.25k
      source->buffer_width = (size_t)w *
816
5.25k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
7.15k
    source->iobuffer = (U_CHAR *)
818
7.15k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
7.15k
                                  source->buffer_width);
820
7.15k
  }
821
822
  /* Create compressor input buffer. */
823
9.72k
  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
9.72k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
9.72k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
9.72k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
9.72k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
9.72k
    source->pub.buffer_height = 1;
835
9.72k
  }
836
837
  /* Compute the rescaling array if required. */
838
9.72k
  if (need_rescale) {
839
9.72k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
9.72k
    source->rescale = (_JSAMPLE *)
843
9.72k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
9.72k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
9.72k
                                           sizeof(_JSAMPLE)));
846
9.72k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
9.72k
                                        sizeof(_JSAMPLE)));
848
9.72k
    half_maxval = maxval / 2;
849
81.5M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
81.5M
      source->rescale[val] =
852
81.5M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
81.5M
                   maxval);
854
81.5M
    }
855
9.72k
  }
856
9.72k
}
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
59.4k
{
866
  /* no work */
867
59.4k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
865
28.0k
{
866
  /* no work */
867
28.0k
}
rdppm-12.c:finish_input_ppm
Line
Count
Source
865
26.6k
{
866
  /* no work */
867
26.6k
}
rdppm-16.c:finish_input_ppm
Line
Count
Source
865
4.71k
{
866
  /* no work */
867
4.71k
}
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
99.6k
{
877
99.6k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
48.1k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
51.4k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
51.4k
      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
99.6k
  source = (ppm_source_ptr)
889
99.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
99.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
99.6k
  source->pub.start_input = start_input_ppm;
893
99.6k
  source->pub.finish_input = finish_input_ppm;
894
99.6k
  source->pub.max_pixels = 0;
895
896
99.6k
  return (cjpeg_source_ptr)source;
897
99.6k
}
jinit_read_ppm
Line
Count
Source
876
48.1k
{
877
48.1k
  ppm_source_ptr source;
878
879
48.1k
#if BITS_IN_JSAMPLE == 8
880
48.1k
  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
48.1k
  source = (ppm_source_ptr)
889
48.1k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
48.1k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
48.1k
  source->pub.start_input = start_input_ppm;
893
48.1k
  source->pub.finish_input = finish_input_ppm;
894
48.1k
  source->pub.max_pixels = 0;
895
896
48.1k
  return (cjpeg_source_ptr)source;
897
48.1k
}
j12init_read_ppm
Line
Count
Source
876
39.7k
{
877
39.7k
  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
39.7k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
39.7k
      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
39.7k
  source = (ppm_source_ptr)
889
39.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
39.7k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
39.7k
  source->pub.start_input = start_input_ppm;
893
39.7k
  source->pub.finish_input = finish_input_ppm;
894
39.7k
  source->pub.max_pixels = 0;
895
896
39.7k
  return (cjpeg_source_ptr)source;
897
39.7k
}
j16init_read_ppm
Line
Count
Source
876
11.7k
{
877
11.7k
  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
11.7k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
11.7k
      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
11.7k
  source = (ppm_source_ptr)
889
11.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
11.7k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
11.7k
  source->pub.start_input = start_input_ppm;
893
11.7k
  source->pub.finish_input = finish_input_ppm;
894
11.7k
  source->pub.max_pixels = 0;
895
896
11.7k
  return (cjpeg_source_ptr)source;
897
11.7k
}
898
899
#endif /* defined(PPM_SUPPORTED) &&
900
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */