Coverage Report

Created: 2025-08-26 06:42

/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
168M
#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.12M
{
80
2.12M
  register int ch;
81
82
2.12M
  ch = getc(infile);
83
2.12M
  if (ch == '#') {
84
330k
    do {
85
330k
      ch = getc(infile);
86
330k
    } while (ch != '\n' && ch != EOF);
87
21.6k
  }
88
2.12M
  return ch;
89
2.12M
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
876k
{
80
876k
  register int ch;
81
82
876k
  ch = getc(infile);
83
876k
  if (ch == '#') {
84
117k
    do {
85
117k
      ch = getc(infile);
86
117k
    } while (ch != '\n' && ch != EOF);
87
10.3k
  }
88
876k
  return ch;
89
876k
}
rdppm-12.c:pbm_getc
Line
Count
Source
79
940k
{
80
940k
  register int ch;
81
82
940k
  ch = getc(infile);
83
940k
  if (ch == '#') {
84
144k
    do {
85
144k
      ch = getc(infile);
86
144k
    } while (ch != '\n' && ch != EOF);
87
6.98k
  }
88
940k
  return ch;
89
940k
}
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.3k
    do {
85
68.3k
      ch = getc(infile);
86
68.3k
    } while (ch != '\n' && ch != EOF);
87
4.25k
  }
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
877k
{
99
877k
  register int ch;
100
877k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
926k
  do {
104
926k
    ch = pbm_getc(infile);
105
926k
    if (ch == EOF)
106
16.3k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
926k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
877k
  if (ch < '0' || ch > '9')
110
1.34k
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
877k
  val = ch - '0';
113
1.21M
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
335k
    val *= 10;
115
335k
    val += ch - '0';
116
335k
    if (val > maxval)
117
757
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
335k
  }
119
120
877k
  return val;
121
877k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
374k
{
99
374k
  register int ch;
100
374k
  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.91k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
393k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
374k
  if (ch < '0' || ch > '9')
110
601
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
374k
  val = ch - '0';
113
491k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
117k
    val *= 10;
115
117k
    val += ch - '0';
116
117k
    if (val > maxval)
117
366
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
117k
  }
119
120
374k
  return val;
121
374k
}
rdppm-12.c:read_pbm_integer
Line
Count
Source
98
369k
{
99
369k
  register int ch;
100
369k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
391k
  do {
104
391k
    ch = pbm_getc(infile);
105
391k
    if (ch == EOF)
106
5.26k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
391k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
369k
  if (ch < '0' || ch > '9')
110
528
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
369k
  val = ch - '0';
113
554k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
185k
    val *= 10;
115
185k
    val += ch - '0';
116
185k
    if (val > maxval)
117
252
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
185k
  }
119
120
369k
  return val;
121
369k
}
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.16k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
141k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
133k
  if (ch < '0' || ch > '9')
110
218
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
133k
  val = ch - '0';
113
166k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
33.0k
    val *= 10;
115
33.0k
    val += ch - '0';
116
33.0k
    if (val > maxval)
117
139
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
33.0k
  }
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.4k
  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.27k
{
140
5.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
5.27k
  FILE *infile = source->pub.input_file;
142
5.27k
  register _JSAMPROW ptr;
143
5.27k
  register _JSAMPLE *rescale = source->rescale;
144
5.27k
  JDIMENSION col;
145
5.27k
  unsigned int maxval = source->maxval;
146
147
5.27k
  ptr = source->pub._buffer[0];
148
15.0k
  for (col = cinfo->image_width; col > 0; col--) {
149
9.78k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
9.78k
  }
151
5.27k
  return 1;
152
5.27k
}
rdppm-12.c:get_text_gray_row
Line
Count
Source
139
2.95k
{
140
2.95k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
2.95k
  FILE *infile = source->pub.input_file;
142
2.95k
  register _JSAMPROW ptr;
143
2.95k
  register _JSAMPLE *rescale = source->rescale;
144
2.95k
  JDIMENSION col;
145
2.95k
  unsigned int maxval = source->maxval;
146
147
2.95k
  ptr = source->pub._buffer[0];
148
8.03k
  for (col = cinfo->image_width; col > 0; col--) {
149
5.08k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
5.08k
  }
151
2.95k
  return 1;
152
2.95k
}
rdppm-16.c:get_text_gray_row
Line
Count
Source
139
1.85k
{
140
1.85k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.85k
  FILE *infile = source->pub.input_file;
142
1.85k
  register _JSAMPROW ptr;
143
1.85k
  register _JSAMPLE *rescale = source->rescale;
144
1.85k
  JDIMENSION col;
145
1.85k
  unsigned int maxval = source->maxval;
146
147
1.85k
  ptr = source->pub._buffer[0];
148
5.35k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.49k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.49k
  }
151
1.85k
  return 1;
152
1.85k
}
153
154
155
148M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
570M
  for (col = cinfo->image_width; col > 0; col--) { \
157
422M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
422M
    alpha_set_op \
159
422M
    ptr += ps; \
160
422M
  } \
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.4k
{
168
50.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
50.4k
  FILE *infile = source->pub.input_file;
170
50.4k
  register _JSAMPROW ptr;
171
50.4k
  register _JSAMPLE *rescale = source->rescale;
172
50.4k
  JDIMENSION col;
173
50.4k
  unsigned int maxval = source->maxval;
174
50.4k
  register int rindex = rgb_red[cinfo->in_color_space];
175
50.4k
  register int gindex = rgb_green[cinfo->in_color_space];
176
50.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
50.4k
  register int aindex = alpha_index[cinfo->in_color_space];
178
50.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
50.4k
  ptr = source->pub._buffer[0];
181
50.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
8.01k
    if (aindex >= 0)
183
1.16k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
8.01k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
6.85k
    else
186
6.85k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
42.4k
  } else {
188
42.4k
    if (aindex >= 0)
189
6.91k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
42.4k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
35.5k
    else
192
35.5k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
42.4k
  }
194
50.4k
  return 1;
195
50.4k
}
rdppm-8.c:get_text_gray_rgb_row
Line
Count
Source
167
26.3k
{
168
26.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
26.3k
  FILE *infile = source->pub.input_file;
170
26.3k
  register _JSAMPROW ptr;
171
26.3k
  register _JSAMPLE *rescale = source->rescale;
172
26.3k
  JDIMENSION col;
173
26.3k
  unsigned int maxval = source->maxval;
174
26.3k
  register int rindex = rgb_red[cinfo->in_color_space];
175
26.3k
  register int gindex = rgb_green[cinfo->in_color_space];
176
26.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
26.3k
  register int aindex = alpha_index[cinfo->in_color_space];
178
26.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
26.3k
  ptr = source->pub._buffer[0];
181
26.3k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
4.60k
    if (aindex >= 0)
183
471
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
4.60k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
4.13k
    else
186
4.13k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
21.7k
  } else {
188
21.7k
    if (aindex >= 0)
189
2.79k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
21.7k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
18.9k
    else
192
18.9k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
21.7k
  }
194
26.3k
  return 1;
195
26.3k
}
rdppm-12.c:get_text_gray_rgb_row
Line
Count
Source
167
14.7k
{
168
14.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
14.7k
  FILE *infile = source->pub.input_file;
170
14.7k
  register _JSAMPROW ptr;
171
14.7k
  register _JSAMPLE *rescale = source->rescale;
172
14.7k
  JDIMENSION col;
173
14.7k
  unsigned int maxval = source->maxval;
174
14.7k
  register int rindex = rgb_red[cinfo->in_color_space];
175
14.7k
  register int gindex = rgb_green[cinfo->in_color_space];
176
14.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
14.7k
  register int aindex = alpha_index[cinfo->in_color_space];
178
14.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
14.7k
  ptr = source->pub._buffer[0];
181
14.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
2.41k
    if (aindex >= 0)
183
482
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
2.41k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
1.92k
    else
186
1.92k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
12.3k
  } else {
188
12.3k
    if (aindex >= 0)
189
2.47k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
12.3k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
9.88k
    else
192
9.88k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
12.3k
  }
194
14.7k
  return 1;
195
14.7k
}
rdppm-16.c:get_text_gray_rgb_row
Line
Count
Source
167
9.27k
{
168
9.27k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
9.27k
  FILE *infile = source->pub.input_file;
170
9.27k
  register _JSAMPROW ptr;
171
9.27k
  register _JSAMPLE *rescale = source->rescale;
172
9.27k
  JDIMENSION col;
173
9.27k
  unsigned int maxval = source->maxval;
174
9.27k
  register int rindex = rgb_red[cinfo->in_color_space];
175
9.27k
  register int gindex = rgb_green[cinfo->in_color_space];
176
9.27k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
9.27k
  register int aindex = alpha_index[cinfo->in_color_space];
178
9.27k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
9.27k
  ptr = source->pub._buffer[0];
181
9.27k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.00k
    if (aindex >= 0)
183
209
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.00k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
794
    else
186
794
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.27k
  } else {
188
8.27k
    if (aindex >= 0)
189
1.64k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.27k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.62k
    else
192
6.62k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.27k
  }
194
9.27k
  return 1;
195
9.27k
}
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.07k
{
203
8.07k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
8.07k
  FILE *infile = source->pub.input_file;
205
8.07k
  register _JSAMPROW ptr;
206
8.07k
  register _JSAMPLE *rescale = source->rescale;
207
8.07k
  JDIMENSION col;
208
8.07k
  unsigned int maxval = source->maxval;
209
210
8.07k
  ptr = source->pub._buffer[0];
211
8.07k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
5.54k
    for (col = cinfo->image_width; col > 0; col--) {
213
3.65k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
3.65k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
3.65k
      ptr += 4;
216
3.65k
    }
217
6.18k
  } else {
218
17.4k
    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.18k
  }
224
8.07k
  return 1;
225
8.07k
}
rdppm-8.c:get_text_gray_cmyk_row
Line
Count
Source
202
3.26k
{
203
3.26k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
3.26k
  FILE *infile = source->pub.input_file;
205
3.26k
  register _JSAMPROW ptr;
206
3.26k
  register _JSAMPLE *rescale = source->rescale;
207
3.26k
  JDIMENSION col;
208
3.26k
  unsigned int maxval = source->maxval;
209
210
3.26k
  ptr = source->pub._buffer[0];
211
3.26k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
2.99k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.88k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.88k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.88k
      ptr += 4;
216
1.88k
    }
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.26k
  return 1;
225
3.26k
}
rdppm-12.c:get_text_gray_cmyk_row
Line
Count
Source
202
2.95k
{
203
2.95k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
2.95k
  FILE *infile = source->pub.input_file;
205
2.95k
  register _JSAMPROW ptr;
206
2.95k
  register _JSAMPLE *rescale = source->rescale;
207
2.95k
  JDIMENSION col;
208
2.95k
  unsigned int maxval = source->maxval;
209
210
2.95k
  ptr = source->pub._buffer[0];
211
2.95k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.50k
    for (col = cinfo->image_width; col > 0; col--) {
213
1.02k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
1.02k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
1.02k
      ptr += 4;
216
1.02k
    }
217
2.47k
  } else {
218
6.53k
    for (col = cinfo->image_width; col > 0; col--) {
219
4.06k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
4.06k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
4.06k
      ptr += 4;
222
4.06k
    }
223
2.47k
  }
224
2.95k
  return 1;
225
2.95k
}
rdppm-16.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.85k
{
203
1.85k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.85k
  FILE *infile = source->pub.input_file;
205
1.85k
  register _JSAMPROW ptr;
206
1.85k
  register _JSAMPLE *rescale = source->rescale;
207
1.85k
  JDIMENSION col;
208
1.85k
  unsigned int maxval = source->maxval;
209
210
1.85k
  ptr = source->pub._buffer[0];
211
1.85k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.05k
    for (col = cinfo->image_width; col > 0; col--) {
213
746
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
746
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
746
      ptr += 4;
216
746
    }
217
1.55k
  } else {
218
4.30k
    for (col = cinfo->image_width; col > 0; col--) {
219
2.75k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
2.75k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
2.75k
      ptr += 4;
222
2.75k
    }
223
1.55k
  }
224
1.85k
  return 1;
225
1.85k
}
226
227
228
1.25M
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
22.0M
  for (col = cinfo->image_width; col > 0; col--) { \
230
20.7M
    ptr[rindex] = read_op; \
231
20.7M
    ptr[gindex] = read_op; \
232
20.7M
    ptr[bindex] = read_op; \
233
20.7M
    alpha_set_op \
234
20.7M
    ptr += ps; \
235
20.7M
  } \
236
1.25M
}
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.4k
{
242
62.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
62.4k
  FILE *infile = source->pub.input_file;
244
62.4k
  register _JSAMPROW ptr;
245
62.4k
  register _JSAMPLE *rescale = source->rescale;
246
62.4k
  JDIMENSION col;
247
62.4k
  unsigned int maxval = source->maxval;
248
62.4k
  register int rindex = rgb_red[cinfo->in_color_space];
249
62.4k
  register int gindex = rgb_green[cinfo->in_color_space];
250
62.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
62.4k
  register int aindex = alpha_index[cinfo->in_color_space];
252
62.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
62.4k
  ptr = source->pub._buffer[0];
255
62.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
13.4k
    if (aindex >= 0)
257
1.92k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
13.4k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
11.4k
    else
260
11.4k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
49.0k
  } else {
262
49.0k
    if (aindex >= 0)
263
8.68k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
49.0k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
40.3k
    else
266
40.3k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
49.0k
  }
268
62.4k
  return 1;
269
62.4k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
241
30.3k
{
242
30.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
30.3k
  FILE *infile = source->pub.input_file;
244
30.3k
  register _JSAMPROW ptr;
245
30.3k
  register _JSAMPLE *rescale = source->rescale;
246
30.3k
  JDIMENSION col;
247
30.3k
  unsigned int maxval = source->maxval;
248
30.3k
  register int rindex = rgb_red[cinfo->in_color_space];
249
30.3k
  register int gindex = rgb_green[cinfo->in_color_space];
250
30.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
30.3k
  register int aindex = alpha_index[cinfo->in_color_space];
252
30.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
30.3k
  ptr = source->pub._buffer[0];
255
30.3k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
6.36k
    if (aindex >= 0)
257
751
      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.43k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
24.0k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
20.5k
    else
266
20.5k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
24.0k
  }
268
30.3k
  return 1;
269
30.3k
}
rdppm-12.c:get_text_rgb_row
Line
Count
Source
241
17.3k
{
242
17.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
17.3k
  FILE *infile = source->pub.input_file;
244
17.3k
  register _JSAMPROW ptr;
245
17.3k
  register _JSAMPLE *rescale = source->rescale;
246
17.3k
  JDIMENSION col;
247
17.3k
  unsigned int maxval = source->maxval;
248
17.3k
  register int rindex = rgb_red[cinfo->in_color_space];
249
17.3k
  register int gindex = rgb_green[cinfo->in_color_space];
250
17.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
17.3k
  register int aindex = alpha_index[cinfo->in_color_space];
252
17.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
17.3k
  ptr = source->pub._buffer[0];
255
17.3k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
4.77k
    if (aindex >= 0)
257
954
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
4.77k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
3.81k
    else
260
3.81k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
12.5k
  } else {
262
12.5k
    if (aindex >= 0)
263
2.51k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
12.5k
                    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.5k
  }
268
17.3k
  return 1;
269
17.3k
}
rdppm-16.c:get_text_rgb_row
Line
Count
Source
241
14.7k
{
242
14.7k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
14.7k
  FILE *infile = source->pub.input_file;
244
14.7k
  register _JSAMPROW ptr;
245
14.7k
  register _JSAMPLE *rescale = source->rescale;
246
14.7k
  JDIMENSION col;
247
14.7k
  unsigned int maxval = source->maxval;
248
14.7k
  register int rindex = rgb_red[cinfo->in_color_space];
249
14.7k
  register int gindex = rgb_green[cinfo->in_color_space];
250
14.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
14.7k
  register int aindex = alpha_index[cinfo->in_color_space];
252
14.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
14.7k
  ptr = source->pub._buffer[0];
255
14.7k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
2.27k
    if (aindex >= 0)
257
219
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
2.27k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
2.05k
    else
260
2.05k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
12.4k
  } else {
262
12.4k
    if (aindex >= 0)
263
2.73k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
12.4k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
9.76k
    else
266
9.76k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
12.4k
  }
268
14.7k
  return 1;
269
14.7k
}
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.6k
{
277
10.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
10.6k
  FILE *infile = source->pub.input_file;
279
10.6k
  register _JSAMPROW ptr;
280
10.6k
  register _JSAMPLE *rescale = source->rescale;
281
10.6k
  JDIMENSION col;
282
10.6k
  unsigned int maxval = source->maxval;
283
284
10.6k
  ptr = source->pub._buffer[0];
285
10.6k
  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.05k
  } else {
294
19.3k
    for (col = cinfo->image_width; col > 0; col--) {
295
12.3k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
12.3k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
12.3k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
12.3k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
12.3k
      ptr += 4;
300
12.3k
    }
301
7.05k
  }
302
10.6k
  return 1;
303
10.6k
}
rdppm-8.c:get_text_rgb_cmyk_row
Line
Count
Source
276
4.18k
{
277
4.18k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
4.18k
  FILE *infile = source->pub.input_file;
279
4.18k
  register _JSAMPROW ptr;
280
4.18k
  register _JSAMPLE *rescale = source->rescale;
281
4.18k
  JDIMENSION col;
282
4.18k
  unsigned int maxval = source->maxval;
283
284
4.18k
  ptr = source->pub._buffer[0];
285
4.18k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
4.19k
    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.45k
  } else {
294
7.23k
    for (col = cinfo->image_width; col > 0; col--) {
295
4.77k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
4.77k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
4.77k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
4.77k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
4.77k
      ptr += 4;
300
4.77k
    }
301
2.45k
  }
302
4.18k
  return 1;
303
4.18k
}
rdppm-12.c:get_text_rgb_cmyk_row
Line
Count
Source
276
3.46k
{
277
3.46k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
3.46k
  FILE *infile = source->pub.input_file;
279
3.46k
  register _JSAMPROW ptr;
280
3.46k
  register _JSAMPLE *rescale = source->rescale;
281
3.46k
  JDIMENSION col;
282
3.46k
  unsigned int maxval = source->maxval;
283
284
3.46k
  ptr = source->pub._buffer[0];
285
3.46k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
8.76k
    for (col = cinfo->image_width; col > 0; col--) {
287
7.80k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
7.80k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
7.80k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
7.80k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
7.80k
      ptr += 4;
292
7.80k
    }
293
2.51k
  } else {
294
7.05k
    for (col = cinfo->image_width; col > 0; col--) {
295
4.54k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
4.54k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
4.54k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
4.54k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
4.54k
      ptr += 4;
300
4.54k
    }
301
2.51k
  }
302
3.46k
  return 1;
303
3.46k
}
rdppm-16.c:get_text_rgb_cmyk_row
Line
Count
Source
276
2.95k
{
277
2.95k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.95k
  FILE *infile = source->pub.input_file;
279
2.95k
  register _JSAMPROW ptr;
280
2.95k
  register _JSAMPLE *rescale = source->rescale;
281
2.95k
  JDIMENSION col;
282
2.95k
  unsigned int maxval = source->maxval;
283
284
2.95k
  ptr = source->pub._buffer[0];
285
2.95k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
2.24k
    for (col = cinfo->image_width; col > 0; col--) {
287
1.37k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
1.37k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
1.37k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
1.37k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
1.37k
      ptr += 4;
292
1.37k
    }
293
2.08k
  } else {
294
5.07k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.99k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.99k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.99k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.99k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.99k
      ptr += 4;
300
2.99k
    }
301
2.08k
  }
302
2.95k
  return 1;
303
2.95k
}
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
415
    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
82.1M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
82.1M
  }
323
27.7M
  return 1;
324
27.7M
}
rdppm-8.c:get_scaled_gray_row
Line
Count
Source
309
10.9M
{
310
10.9M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
10.9M
  register _JSAMPROW ptr;
312
10.9M
  register U_CHAR *bufferptr;
313
10.9M
  register _JSAMPLE *rescale = source->rescale;
314
10.9M
  JDIMENSION col;
315
316
10.9M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
227
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
10.9M
  ptr = source->pub._buffer[0];
319
10.9M
  bufferptr = source->iobuffer;
320
47.3M
  for (col = cinfo->image_width; col > 0; col--) {
321
36.4M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
36.4M
  }
323
10.9M
  return 1;
324
10.9M
}
rdppm-12.c:get_scaled_gray_row
Line
Count
Source
309
14.2M
{
310
14.2M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
14.2M
  register _JSAMPROW ptr;
312
14.2M
  register U_CHAR *bufferptr;
313
14.2M
  register _JSAMPLE *rescale = source->rescale;
314
14.2M
  JDIMENSION col;
315
316
14.2M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
122
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
14.2M
  ptr = source->pub._buffer[0];
319
14.2M
  bufferptr = source->iobuffer;
320
51.0M
  for (col = cinfo->image_width; col > 0; col--) {
321
36.8M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
36.8M
  }
323
14.2M
  return 1;
324
14.2M
}
rdppm-16.c:get_scaled_gray_row
Line
Count
Source
309
2.60M
{
310
2.60M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.60M
  register _JSAMPROW ptr;
312
2.60M
  register U_CHAR *bufferptr;
313
2.60M
  register _JSAMPLE *rescale = source->rescale;
314
2.60M
  JDIMENSION col;
315
316
2.60M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
66
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.60M
  ptr = source->pub._buffer[0];
319
2.60M
  bufferptr = source->iobuffer;
320
11.4M
  for (col = cinfo->image_width; col > 0; col--) {
321
8.82M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
8.82M
  }
323
2.60M
  return 1;
324
2.60M
}
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.48k
    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
9.66M
    if (aindex >= 0)
350
230k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
9.43M
    else
352
9.43M
      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.9M
{
332
63.9M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
63.9M
  register _JSAMPROW ptr;
334
63.9M
  register U_CHAR *bufferptr;
335
63.9M
  register _JSAMPLE *rescale = source->rescale;
336
63.9M
  JDIMENSION col;
337
63.9M
  unsigned int maxval = source->maxval;
338
63.9M
  register int rindex = rgb_red[cinfo->in_color_space];
339
63.9M
  register int gindex = rgb_green[cinfo->in_color_space];
340
63.9M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
63.9M
  register int aindex = alpha_index[cinfo->in_color_space];
342
63.9M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
63.9M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
1.54k
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
63.9M
  ptr = source->pub._buffer[0];
347
63.9M
  bufferptr = source->iobuffer;
348
63.9M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
9.66M
    if (aindex >= 0)
350
230k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
9.43M
    else
352
9.43M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
54.2M
  } else {
354
54.2M
    if (aindex >= 0)
355
7.44M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
54.2M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
46.8M
    else
358
46.8M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
54.2M
  }
360
63.9M
  return 1;
361
63.9M
}
rdppm-12.c:get_gray_rgb_row
Line
Count
Source
331
71.0M
{
332
71.0M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
71.0M
  register _JSAMPROW ptr;
334
71.0M
  register U_CHAR *bufferptr;
335
71.0M
  register _JSAMPLE *rescale = source->rescale;
336
71.0M
  JDIMENSION col;
337
71.0M
  unsigned int maxval = source->maxval;
338
71.0M
  register int rindex = rgb_red[cinfo->in_color_space];
339
71.0M
  register int gindex = rgb_green[cinfo->in_color_space];
340
71.0M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
71.0M
  register int aindex = alpha_index[cinfo->in_color_space];
342
71.0M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
71.0M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
610
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
71.0M
  ptr = source->pub._buffer[0];
347
71.0M
  bufferptr = source->iobuffer;
348
71.0M
  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.0M
  } else {
354
71.0M
    if (aindex >= 0)
355
14.2M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
71.0M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
56.8M
    else
358
56.8M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
71.0M
  }
360
71.0M
  return 1;
361
71.0M
}
rdppm-16.c:get_gray_rgb_row
Line
Count
Source
331
13.0M
{
332
13.0M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
13.0M
  register _JSAMPROW ptr;
334
13.0M
  register U_CHAR *bufferptr;
335
13.0M
  register _JSAMPLE *rescale = source->rescale;
336
13.0M
  JDIMENSION col;
337
13.0M
  unsigned int maxval = source->maxval;
338
13.0M
  register int rindex = rgb_red[cinfo->in_color_space];
339
13.0M
  register int gindex = rgb_green[cinfo->in_color_space];
340
13.0M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
13.0M
  register int aindex = alpha_index[cinfo->in_color_space];
342
13.0M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
13.0M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
330
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
13.0M
  ptr = source->pub._buffer[0];
347
13.0M
  bufferptr = source->iobuffer;
348
13.0M
  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.0M
  } else {
354
13.0M
    if (aindex >= 0)
355
2.60M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
13.0M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
10.4M
    else
358
10.4M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
13.0M
  }
360
13.0M
  return 1;
361
13.0M
}
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.4M
{
369
24.4M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
24.4M
  register _JSAMPROW ptr;
371
24.4M
  register U_CHAR *bufferptr;
372
24.4M
  register _JSAMPLE *rescale = source->rescale;
373
24.4M
  JDIMENSION col;
374
24.4M
  unsigned int maxval = source->maxval;
375
376
24.4M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
405
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
24.4M
  ptr = source->pub._buffer[0];
379
24.4M
  bufferptr = source->iobuffer;
380
24.4M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
1.51M
    for (col = cinfo->image_width; col > 0; col--) {
382
1.23M
      _JSAMPLE gray = *bufferptr++;
383
1.23M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
1.23M
      ptr += 4;
385
1.23M
    }
386
24.2M
  } else {
387
92.5M
    for (col = cinfo->image_width; col > 0; col--) {
388
68.3M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
68.3M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
68.3M
      ptr += 4;
391
68.3M
    }
392
24.2M
  }
393
24.4M
  return 1;
394
24.4M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
368
7.67M
{
369
7.67M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
7.67M
  register _JSAMPROW ptr;
371
7.67M
  register U_CHAR *bufferptr;
372
7.67M
  register _JSAMPLE *rescale = source->rescale;
373
7.67M
  JDIMENSION col;
374
7.67M
  unsigned int maxval = source->maxval;
375
376
7.67M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
217
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
7.67M
  ptr = source->pub._buffer[0];
379
7.67M
  bufferptr = source->iobuffer;
380
7.67M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
1.51M
    for (col = cinfo->image_width; col > 0; col--) {
382
1.23M
      _JSAMPLE gray = *bufferptr++;
383
1.23M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
384
1.23M
      ptr += 4;
385
1.23M
    }
386
7.39M
  } else {
387
30.0M
    for (col = cinfo->image_width; col > 0; col--) {
388
22.6M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
22.6M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
22.6M
      ptr += 4;
391
22.6M
    }
392
7.39M
  }
393
7.67M
  return 1;
394
7.67M
}
rdppm-12.c:get_gray_cmyk_row
Line
Count
Source
368
14.2M
{
369
14.2M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
14.2M
  register _JSAMPROW ptr;
371
14.2M
  register U_CHAR *bufferptr;
372
14.2M
  register _JSAMPLE *rescale = source->rescale;
373
14.2M
  JDIMENSION col;
374
14.2M
  unsigned int maxval = source->maxval;
375
376
14.2M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
122
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
14.2M
  ptr = source->pub._buffer[0];
379
14.2M
  bufferptr = source->iobuffer;
380
14.2M
  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.2M
  } else {
387
51.0M
    for (col = cinfo->image_width; col > 0; col--) {
388
36.8M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
36.8M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
36.8M
      ptr += 4;
391
36.8M
    }
392
14.2M
  }
393
14.2M
  return 1;
394
14.2M
}
rdppm-16.c:get_gray_cmyk_row
Line
Count
Source
368
2.60M
{
369
2.60M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.60M
  register _JSAMPROW ptr;
371
2.60M
  register U_CHAR *bufferptr;
372
2.60M
  register _JSAMPLE *rescale = source->rescale;
373
2.60M
  JDIMENSION col;
374
2.60M
  unsigned int maxval = source->maxval;
375
376
2.60M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
66
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.60M
  ptr = source->pub._buffer[0];
379
2.60M
  bufferptr = source->iobuffer;
380
2.60M
  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.60M
  } else {
387
11.4M
    for (col = cinfo->image_width; col > 0; col--) {
388
8.82M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
8.82M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
8.82M
      ptr += 4;
391
8.82M
    }
392
2.60M
  }
393
2.60M
  return 1;
394
2.60M
}
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.18M
{
401
1.18M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
1.18M
  register _JSAMPROW ptr;
403
1.18M
  register U_CHAR *bufferptr;
404
1.18M
  register _JSAMPLE *rescale = source->rescale;
405
1.18M
  JDIMENSION col;
406
1.18M
  unsigned int maxval = source->maxval;
407
1.18M
  register int rindex = rgb_red[cinfo->in_color_space];
408
1.18M
  register int gindex = rgb_green[cinfo->in_color_space];
409
1.18M
  register int bindex = rgb_blue[cinfo->in_color_space];
410
1.18M
  register int aindex = alpha_index[cinfo->in_color_space];
411
1.18M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
1.18M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
3.27k
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
1.18M
  ptr = source->pub._buffer[0];
416
1.18M
  bufferptr = source->iobuffer;
417
1.18M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
34.6k
    if (aindex >= 0)
419
5.54k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
29.1k
    else
421
29.1k
      RGB_READ_LOOP(*bufferptr++, {})
422
1.15M
  } else {
423
1.15M
    if (aindex >= 0)
424
224k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
1.15M
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
929k
    else
427
929k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
1.15M
  }
429
1.18M
  return 1;
430
1.18M
}
rdppm-8.c:get_rgb_row
Line
Count
Source
400
112k
{
401
112k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
112k
  register _JSAMPROW ptr;
403
112k
  register U_CHAR *bufferptr;
404
112k
  register _JSAMPLE *rescale = source->rescale;
405
112k
  JDIMENSION col;
406
112k
  unsigned int maxval = source->maxval;
407
112k
  register int rindex = rgb_red[cinfo->in_color_space];
408
112k
  register int gindex = rgb_green[cinfo->in_color_space];
409
112k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
112k
  register int aindex = alpha_index[cinfo->in_color_space];
411
112k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
112k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
1.84k
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
112k
  ptr = source->pub._buffer[0];
416
112k
  bufferptr = source->iobuffer;
417
112k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
34.6k
    if (aindex >= 0)
419
5.54k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
29.1k
    else
421
29.1k
      RGB_READ_LOOP(*bufferptr++, {})
422
77.7k
  } else {
423
77.7k
    if (aindex >= 0)
424
9.23k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
77.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
68.5k
    else
427
68.5k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
77.7k
  }
429
112k
  return 1;
430
112k
}
rdppm-12.c:get_rgb_row
Line
Count
Source
400
1.06M
{
401
1.06M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
1.06M
  register _JSAMPROW ptr;
403
1.06M
  register U_CHAR *bufferptr;
404
1.06M
  register _JSAMPLE *rescale = source->rescale;
405
1.06M
  JDIMENSION col;
406
1.06M
  unsigned int maxval = source->maxval;
407
1.06M
  register int rindex = rgb_red[cinfo->in_color_space];
408
1.06M
  register int gindex = rgb_green[cinfo->in_color_space];
409
1.06M
  register int bindex = rgb_blue[cinfo->in_color_space];
410
1.06M
  register int aindex = alpha_index[cinfo->in_color_space];
411
1.06M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
1.06M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
935
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
1.06M
  ptr = source->pub._buffer[0];
416
1.06M
  bufferptr = source->iobuffer;
417
1.06M
  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.06M
  } else {
423
1.06M
    if (aindex >= 0)
424
212k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
1.06M
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
851k
    else
427
851k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
1.06M
  }
429
1.06M
  return 1;
430
1.06M
}
rdppm-16.c:get_rgb_row
Line
Count
Source
400
11.9k
{
401
11.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
11.9k
  register _JSAMPROW ptr;
403
11.9k
  register U_CHAR *bufferptr;
404
11.9k
  register _JSAMPLE *rescale = source->rescale;
405
11.9k
  JDIMENSION col;
406
11.9k
  unsigned int maxval = source->maxval;
407
11.9k
  register int rindex = rgb_red[cinfo->in_color_space];
408
11.9k
  register int gindex = rgb_green[cinfo->in_color_space];
409
11.9k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
11.9k
  register int aindex = alpha_index[cinfo->in_color_space];
411
11.9k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
11.9k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
490
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
11.9k
  ptr = source->pub._buffer[0];
416
11.9k
  bufferptr = source->iobuffer;
417
11.9k
  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.9k
  } else {
423
11.9k
    if (aindex >= 0)
424
2.28k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
11.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
9.63k
    else
427
9.63k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
11.9k
  }
429
11.9k
  return 1;
430
11.9k
}
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
230k
{
438
230k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
230k
  register _JSAMPROW ptr;
440
230k
  register U_CHAR *bufferptr;
441
230k
  register _JSAMPLE *rescale = source->rescale;
442
230k
  JDIMENSION col;
443
230k
  unsigned int maxval = source->maxval;
444
445
230k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
587
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
230k
  ptr = source->pub._buffer[0];
448
230k
  bufferptr = source->iobuffer;
449
230k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
361k
    for (col = cinfo->image_width; col > 0; col--) {
451
354k
      _JSAMPLE r = *bufferptr++;
452
354k
      _JSAMPLE g = *bufferptr++;
453
354k
      _JSAMPLE b = *bufferptr++;
454
354k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
354k
      ptr += 4;
456
354k
    }
457
223k
  } else {
458
3.88M
    for (col = cinfo->image_width; col > 0; col--) {
459
3.66M
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
3.66M
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
3.66M
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
3.66M
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
3.66M
      ptr += 4;
464
3.66M
    }
465
223k
  }
466
230k
  return 1;
467
230k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
437
15.0k
{
438
15.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
15.0k
  register _JSAMPROW ptr;
440
15.0k
  register U_CHAR *bufferptr;
441
15.0k
  register _JSAMPLE *rescale = source->rescale;
442
15.0k
  JDIMENSION col;
443
15.0k
  unsigned int maxval = source->maxval;
444
445
15.0k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
302
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
15.0k
  ptr = source->pub._buffer[0];
448
15.0k
  bufferptr = source->iobuffer;
449
15.0k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
450
361k
    for (col = cinfo->image_width; col > 0; col--) {
451
354k
      _JSAMPLE r = *bufferptr++;
452
354k
      _JSAMPLE g = *bufferptr++;
453
354k
      _JSAMPLE b = *bufferptr++;
454
354k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
455
354k
      ptr += 4;
456
354k
    }
457
8.25k
  } else {
458
512k
    for (col = cinfo->image_width; col > 0; col--) {
459
504k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
504k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
504k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
504k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
504k
      ptr += 4;
464
504k
    }
465
8.25k
  }
466
15.0k
  return 1;
467
15.0k
}
rdppm-12.c:get_rgb_cmyk_row
Line
Count
Source
437
212k
{
438
212k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
212k
  register _JSAMPROW ptr;
440
212k
  register U_CHAR *bufferptr;
441
212k
  register _JSAMPLE *rescale = source->rescale;
442
212k
  JDIMENSION col;
443
212k
  unsigned int maxval = source->maxval;
444
445
212k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
187
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
212k
  ptr = source->pub._buffer[0];
448
212k
  bufferptr = source->iobuffer;
449
212k
  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
212k
  } else {
458
2.88M
    for (col = cinfo->image_width; col > 0; col--) {
459
2.66M
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
2.66M
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
2.66M
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
2.66M
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
2.66M
      ptr += 4;
464
2.66M
    }
465
212k
  }
466
212k
  return 1;
467
212k
}
rdppm-16.c:get_rgb_cmyk_row
Line
Count
Source
437
2.38k
{
438
2.38k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
2.38k
  register _JSAMPROW ptr;
440
2.38k
  register U_CHAR *bufferptr;
441
2.38k
  register _JSAMPLE *rescale = source->rescale;
442
2.38k
  JDIMENSION col;
443
2.38k
  unsigned int maxval = source->maxval;
444
445
2.38k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
98
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
2.38k
  ptr = source->pub._buffer[0];
448
2.38k
  bufferptr = source->iobuffer;
449
2.38k
  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.38k
  } else {
458
492k
    for (col = cinfo->image_width; col > 0; col--) {
459
489k
      _JSAMPLE r = rescale[UCH(*bufferptr++)];
460
489k
      _JSAMPLE g = rescale[UCH(*bufferptr++)];
461
489k
      _JSAMPLE b = rescale[UCH(*bufferptr++)];
462
489k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
463
489k
      ptr += 4;
464
489k
    }
465
2.38k
  }
466
2.38k
  return 1;
467
2.38k
}
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.88M
{
478
1.88M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.88M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
160
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.88M
  return 1;
483
1.88M
}
rdppm-8.c:get_raw_row
Line
Count
Source
477
1.88M
{
478
1.88M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.88M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
160
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.88M
  return 1;
483
1.88M
}
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
347
    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
168
    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
73
      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.60k
{
490
4.60k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
4.60k
  register _JSAMPROW ptr;
492
4.60k
  register U_CHAR *bufferptr;
493
4.60k
  register _JSAMPLE *rescale = source->rescale;
494
4.60k
  JDIMENSION col;
495
4.60k
  unsigned int maxval = source->maxval;
496
497
4.60k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
60
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
4.60k
  ptr = source->pub._buffer[0];
500
4.60k
  bufferptr = source->iobuffer;
501
102k
  for (col = cinfo->image_width; col > 0; col--) {
502
97.5k
    register unsigned int temp;
503
97.5k
    temp  = UCH(*bufferptr++) << 8;
504
97.5k
    temp |= UCH(*bufferptr++);
505
97.5k
    if (temp > maxval)
506
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
97.5k
    *ptr++ = rescale[temp];
508
97.5k
  }
509
4.60k
  return 1;
510
4.60k
}
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.73k
    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.4k
{
517
88.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
88.4k
  register _JSAMPROW ptr;
519
88.4k
  register U_CHAR *bufferptr;
520
88.4k
  register _JSAMPLE *rescale = source->rescale;
521
88.4k
  JDIMENSION col;
522
88.4k
  unsigned int maxval = source->maxval;
523
88.4k
  register int rindex = rgb_red[cinfo->in_color_space];
524
88.4k
  register int gindex = rgb_green[cinfo->in_color_space];
525
88.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
88.4k
  register int aindex = alpha_index[cinfo->in_color_space];
527
88.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
88.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
840
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
88.4k
  ptr = source->pub._buffer[0];
532
88.4k
  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.4k
  return 1;
545
88.4k
}
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
365
      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.0k
{
517
23.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
23.0k
  register _JSAMPROW ptr;
519
23.0k
  register U_CHAR *bufferptr;
520
23.0k
  register _JSAMPLE *rescale = source->rescale;
521
23.0k
  JDIMENSION col;
522
23.0k
  unsigned int maxval = source->maxval;
523
23.0k
  register int rindex = rgb_red[cinfo->in_color_space];
524
23.0k
  register int gindex = rgb_green[cinfo->in_color_space];
525
23.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
23.0k
  register int aindex = alpha_index[cinfo->in_color_space];
527
23.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
23.0k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
300
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
23.0k
  ptr = source->pub._buffer[0];
532
23.0k
  bufferptr = source->iobuffer;
533
510k
  for (col = cinfo->image_width; col > 0; col--) {
534
487k
    register unsigned int temp;
535
487k
    temp  = UCH(*bufferptr++) << 8;
536
487k
    temp |= UCH(*bufferptr++);
537
487k
    if (temp > maxval)
538
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
487k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
487k
    if (aindex >= 0)
541
97.5k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
487k
    ptr += ps;
543
487k
  }
544
23.0k
  return 1;
545
23.0k
}
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
295
    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
180
      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.02k
{
552
5.02k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
5.02k
  register _JSAMPROW ptr;
554
5.02k
  register U_CHAR *bufferptr;
555
5.02k
  register _JSAMPLE *rescale = source->rescale;
556
5.02k
  JDIMENSION col;
557
5.02k
  unsigned int maxval = source->maxval;
558
559
5.02k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
116
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
5.02k
  ptr = source->pub._buffer[0];
562
5.02k
  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
74
      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.02k
  return 1;
574
5.02k
}
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
73
      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.60k
{
552
4.60k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
4.60k
  register _JSAMPROW ptr;
554
4.60k
  register U_CHAR *bufferptr;
555
4.60k
  register _JSAMPLE *rescale = source->rescale;
556
4.60k
  JDIMENSION col;
557
4.60k
  unsigned int maxval = source->maxval;
558
559
4.60k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
60
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
4.60k
  ptr = source->pub._buffer[0];
562
4.60k
  bufferptr = source->iobuffer;
563
102k
  for (col = cinfo->image_width; col > 0; col--) {
564
97.5k
    register unsigned int gray;
565
97.5k
    gray  = UCH(*bufferptr++) << 8;
566
97.5k
    gray |= UCH(*bufferptr++);
567
97.5k
    if (gray > maxval)
568
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
97.5k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
97.5k
                ptr + 1, ptr + 2, ptr + 3);
571
97.5k
    ptr += 4;
572
97.5k
  }
573
4.60k
  return 1;
574
4.60k
}
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
95.0k
{
581
95.0k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
95.0k
  register _JSAMPROW ptr;
583
95.0k
  register U_CHAR *bufferptr;
584
95.0k
  register _JSAMPLE *rescale = source->rescale;
585
95.0k
  JDIMENSION col;
586
95.0k
  unsigned int maxval = source->maxval;
587
95.0k
  register int rindex = rgb_red[cinfo->in_color_space];
588
95.0k
  register int gindex = rgb_green[cinfo->in_color_space];
589
95.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
95.0k
  register int aindex = alpha_index[cinfo->in_color_space];
591
95.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
95.0k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
2.04k
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
95.0k
  ptr = source->pub._buffer[0];
596
95.0k
  bufferptr = source->iobuffer;
597
370k
  for (col = cinfo->image_width; col > 0; col--) {
598
275k
    register unsigned int temp;
599
275k
    temp  = UCH(*bufferptr++) << 8;
600
275k
    temp |= UCH(*bufferptr++);
601
275k
    if (temp > maxval)
602
835
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
275k
    ptr[rindex] = rescale[temp];
604
275k
    temp  = UCH(*bufferptr++) << 8;
605
275k
    temp |= UCH(*bufferptr++);
606
275k
    if (temp > maxval)
607
660
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
275k
    ptr[gindex] = rescale[temp];
609
275k
    temp  = UCH(*bufferptr++) << 8;
610
275k
    temp |= UCH(*bufferptr++);
611
275k
    if (temp > maxval)
612
630
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
275k
    ptr[bindex] = rescale[temp];
614
275k
    if (aindex >= 0)
615
51.7k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
275k
    ptr += ps;
617
275k
  }
618
95.0k
  return 1;
619
95.0k
}
rdppm-8.c:get_word_rgb_row
Line
Count
Source
580
26.2k
{
581
26.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
26.2k
  register _JSAMPROW ptr;
583
26.2k
  register U_CHAR *bufferptr;
584
26.2k
  register _JSAMPLE *rescale = source->rescale;
585
26.2k
  JDIMENSION col;
586
26.2k
  unsigned int maxval = source->maxval;
587
26.2k
  register int rindex = rgb_red[cinfo->in_color_space];
588
26.2k
  register int gindex = rgb_green[cinfo->in_color_space];
589
26.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
26.2k
  register int aindex = alpha_index[cinfo->in_color_space];
591
26.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
26.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
900
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
26.2k
  ptr = source->pub._buffer[0];
596
26.2k
  bufferptr = source->iobuffer;
597
71.4k
  for (col = cinfo->image_width; col > 0; col--) {
598
45.2k
    register unsigned int temp;
599
45.2k
    temp  = UCH(*bufferptr++) << 8;
600
45.2k
    temp |= UCH(*bufferptr++);
601
45.2k
    if (temp > maxval)
602
445
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
45.2k
    ptr[rindex] = rescale[temp];
604
45.2k
    temp  = UCH(*bufferptr++) << 8;
605
45.2k
    temp |= UCH(*bufferptr++);
606
45.2k
    if (temp > maxval)
607
355
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
45.2k
    ptr[gindex] = rescale[temp];
609
45.2k
    temp  = UCH(*bufferptr++) << 8;
610
45.2k
    temp |= UCH(*bufferptr++);
611
45.2k
    if (temp > maxval)
612
310
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
45.2k
    ptr[bindex] = rescale[temp];
614
45.2k
    if (aindex >= 0)
615
5.89k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
45.2k
    ptr += ps;
617
45.2k
  }
618
26.2k
  return 1;
619
26.2k
}
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
220
      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
210
      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
12.3k
{
581
12.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
582
12.3k
  register _JSAMPROW ptr;
583
12.3k
  register U_CHAR *bufferptr;
584
12.3k
  register _JSAMPLE *rescale = source->rescale;
585
12.3k
  JDIMENSION col;
586
12.3k
  unsigned int maxval = source->maxval;
587
12.3k
  register int rindex = rgb_red[cinfo->in_color_space];
588
12.3k
  register int gindex = rgb_green[cinfo->in_color_space];
589
12.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
590
12.3k
  register int aindex = alpha_index[cinfo->in_color_space];
591
12.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
592
593
12.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
594
390
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
12.3k
  ptr = source->pub._buffer[0];
596
12.3k
  bufferptr = source->iobuffer;
597
144k
  for (col = cinfo->image_width; col > 0; col--) {
598
132k
    register unsigned int temp;
599
132k
    temp  = UCH(*bufferptr++) << 8;
600
132k
    temp |= UCH(*bufferptr++);
601
132k
    if (temp > maxval)
602
170
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
132k
    ptr[rindex] = rescale[temp];
604
132k
    temp  = UCH(*bufferptr++) << 8;
605
132k
    temp |= UCH(*bufferptr++);
606
132k
    if (temp > maxval)
607
105
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
132k
    ptr[gindex] = rescale[temp];
609
132k
    temp  = UCH(*bufferptr++) << 8;
610
132k
    temp |= UCH(*bufferptr++);
611
132k
    if (temp > maxval)
612
110
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
132k
    ptr[bindex] = rescale[temp];
614
132k
    if (aindex >= 0)
615
26.4k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
132k
    ptr += ps;
617
132k
  }
618
12.3k
  return 1;
619
12.3k
}
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.3k
{
626
17.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
17.3k
  register _JSAMPROW ptr;
628
17.3k
  register U_CHAR *bufferptr;
629
17.3k
  register _JSAMPLE *rescale = source->rescale;
630
17.3k
  JDIMENSION col;
631
17.3k
  unsigned int maxval = source->maxval;
632
633
17.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
368
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
17.3k
  ptr = source->pub._buffer[0];
636
17.3k
  bufferptr = source->iobuffer;
637
69.4k
  for (col = cinfo->image_width; col > 0; col--) {
638
52.1k
    register unsigned int r, g, b;
639
52.1k
    r  = UCH(*bufferptr++) << 8;
640
52.1k
    r |= UCH(*bufferptr++);
641
52.1k
    if (r > maxval)
642
136
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
52.1k
    g  = UCH(*bufferptr++) << 8;
644
52.1k
    g |= UCH(*bufferptr++);
645
52.1k
    if (g > maxval)
646
111
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
52.1k
    b  = UCH(*bufferptr++) << 8;
648
52.1k
    b |= UCH(*bufferptr++);
649
52.1k
    if (b > maxval)
650
104
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
52.1k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
52.1k
                ptr + 2, ptr + 3);
653
52.1k
    ptr += 4;
654
52.1k
  }
655
17.3k
  return 1;
656
17.3k
}
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.63k
  for (col = cinfo->image_width; col > 0; col--) {
638
6.04k
    register unsigned int r, g, b;
639
6.04k
    r  = UCH(*bufferptr++) << 8;
640
6.04k
    r |= UCH(*bufferptr++);
641
6.04k
    if (r > maxval)
642
58
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
6.04k
    g  = UCH(*bufferptr++) << 8;
644
6.04k
    g |= UCH(*bufferptr++);
645
6.04k
    if (g > maxval)
646
50
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
6.04k
    b  = UCH(*bufferptr++) << 8;
648
6.04k
    b |= UCH(*bufferptr++);
649
6.04k
    if (b > maxval)
650
40
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
6.04k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
6.04k
                ptr + 2, ptr + 3);
653
6.04k
    ptr += 4;
654
6.04k
  }
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
44
      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
42
      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.46k
{
626
2.46k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
627
2.46k
  register _JSAMPROW ptr;
628
2.46k
  register U_CHAR *bufferptr;
629
2.46k
  register _JSAMPLE *rescale = source->rescale;
630
2.46k
  JDIMENSION col;
631
2.46k
  unsigned int maxval = source->maxval;
632
633
2.46k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
634
78
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
2.46k
  ptr = source->pub._buffer[0];
636
2.46k
  bufferptr = source->iobuffer;
637
28.9k
  for (col = cinfo->image_width; col > 0; col--) {
638
26.5k
    register unsigned int r, g, b;
639
26.5k
    r  = UCH(*bufferptr++) << 8;
640
26.5k
    r |= UCH(*bufferptr++);
641
26.5k
    if (r > maxval)
642
34
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
26.5k
    g  = UCH(*bufferptr++) << 8;
644
26.5k
    g |= UCH(*bufferptr++);
645
26.5k
    if (g > maxval)
646
21
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
26.5k
    b  = UCH(*bufferptr++) << 8;
648
26.5k
    b |= UCH(*bufferptr++);
649
26.5k
    if (b > maxval)
650
22
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
26.5k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
26.5k
                ptr + 2, ptr + 3);
653
26.5k
    ptr += 4;
654
26.5k
  }
655
2.46k
  return 1;
656
2.46k
}
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.51k
  case '2':                     /* it's a text-format PGM file */
679
18.2k
  case '3':                     /* it's a text-format PPM file */
680
79.6k
  case '5':                     /* it's a raw-format PGM file */
681
99.6k
  case '6':                     /* it's a raw-format PPM file */
682
99.6k
    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.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
99.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
99.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
99.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
199
    ERREXIT(cinfo, JERR_PPM_NOT);
695
99.6k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
1.43k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
99.6k
  cinfo->image_width = (JDIMENSION)w;
699
99.6k
  cinfo->image_height = (JDIMENSION)h;
700
99.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
99.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
99.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
99.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
99.6k
  switch (c) {
708
6.56k
  case '2':                     /* it's a text-format PGM file */
709
6.56k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
6.56k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
6.56k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
6.56k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
957
      source->pub.get_pixel_rows = get_text_gray_row;
715
5.60k
    else if (IsExtRGB(cinfo->in_color_space))
716
4.78k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
823
    else if (cinfo->in_color_space == JCS_CMYK)
718
823
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
6.56k
    need_iobuffer = FALSE;
722
6.56k
    break;
723
724
7.48k
  case '3':                     /* it's a text-format PPM file */
725
7.48k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
7.48k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
7.48k
    if (IsExtRGB(cinfo->in_color_space))
729
5.43k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
2.05k
    else if (cinfo->in_color_space == JCS_CMYK)
731
965
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
1.08k
    else
733
1.08k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
7.48k
    need_iobuffer = FALSE;
735
7.48k
    break;
736
737
58.9k
  case '5':                     /* it's a raw-format PGM file */
738
58.9k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
58.9k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
58.9k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
58.9k
    if (maxval > 255) {
743
4.19k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
611
        source->pub.get_pixel_rows = get_word_gray_row;
745
3.58k
      else if (IsExtRGB(cinfo->in_color_space))
746
3.05k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
526
      else if (cinfo->in_color_space == JCS_CMYK)
748
526
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
54.7k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
54.7k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
54.7k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
322
      source->pub.get_pixel_rows = get_raw_row;
755
322
      use_raw_buffer = TRUE;
756
322
      need_rescale = FALSE;
757
54.3k
    } else {
758
54.3k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
7.73k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
46.6k
      else if (IsExtRGB(cinfo->in_color_space))
761
40.2k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
6.35k
      else if (cinfo->in_color_space == JCS_CMYK)
763
6.35k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
54.3k
    }
767
58.9k
    break;
768
769
18.3k
  case '6':                     /* it's a raw-format PPM file */
770
18.3k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
18.3k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
18.3k
    if (maxval > 255) {
774
6.12k
      if (IsExtRGB(cinfo->in_color_space))
775
4.46k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
1.66k
      else if (cinfo->in_color_space == JCS_CMYK)
777
772
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
892
      else
779
892
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
12.2k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
12.2k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
12.2k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
12.2k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
1.24k
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
174
      source->pub.get_pixel_rows = get_raw_row;
789
174
      use_raw_buffer = TRUE;
790
174
      need_rescale = FALSE;
791
12.0k
    } else {
792
12.0k
      if (IsExtRGB(cinfo->in_color_space))
793
8.77k
        source->pub.get_pixel_rows = get_rgb_row;
794
3.31k
      else if (cinfo->in_color_space == JCS_CMYK)
795
1.52k
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
1.78k
      else
797
1.78k
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
12.0k
    }
799
18.3k
    break;
800
99.6k
  }
801
802
87.5k
  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.62k
    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.5k
  if (need_iobuffer) {
811
74.6k
    if (c == '6')
812
15.7k
      source->buffer_width = (size_t)w * 3 *
813
15.7k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
58.9k
    else
815
58.9k
      source->buffer_width = (size_t)w *
816
58.9k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
74.6k
    source->iobuffer = (U_CHAR *)
818
74.6k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
74.6k
                                  source->buffer_width);
820
74.6k
  }
821
822
  /* Create compressor input buffer. */
823
87.5k
  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
496
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
496
    source->pub._buffer = &source->pixrow;
828
496
    source->pub.buffer_height = 1;
829
87.0k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
87.0k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
87.0k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
87.0k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
87.0k
    source->pub.buffer_height = 1;
835
87.0k
  }
836
837
  /* Compute the rescaling array if required. */
838
87.5k
  if (need_rescale) {
839
87.0k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
87.0k
    source->rescale = (_JSAMPLE *)
843
87.0k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
87.0k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
87.0k
                                           sizeof(_JSAMPLE)));
846
87.0k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
87.0k
                                        sizeof(_JSAMPLE)));
848
87.0k
    half_maxval = maxval / 2;
849
180M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
180M
      source->rescale[val] =
852
180M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
180M
                   maxval);
854
180M
    }
855
87.0k
  }
856
87.5k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
665
48.3k
{
666
48.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
48.3k
  int c;
668
48.3k
  unsigned int w, h, maxval;
669
48.3k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
48.3k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
48.3k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
48.3k
  switch (c) {
678
4.04k
  case '2':                     /* it's a text-format PGM file */
679
8.53k
  case '3':                     /* it's a text-format PPM file */
680
38.5k
  case '5':                     /* it's a raw-format PGM file */
681
48.3k
  case '6':                     /* it's a raw-format PPM file */
682
48.3k
    break;
683
20
  default:
684
20
    ERREXIT(cinfo, JERR_PPM_NOT);
685
20
    break;
686
48.3k
  }
687
688
  /* fetch the remaining header info */
689
48.3k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
48.3k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
48.3k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
48.3k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
94
    ERREXIT(cinfo, JERR_PPM_NOT);
695
48.3k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
694
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
48.3k
  cinfo->image_width = (JDIMENSION)w;
699
48.3k
  cinfo->image_height = (JDIMENSION)h;
700
48.3k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
48.3k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
48.3k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
48.3k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
48.3k
  switch (c) {
708
3.15k
  case '2':                     /* it's a text-format PGM file */
709
3.15k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
3.15k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
3.15k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
3.15k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
470
      source->pub.get_pixel_rows = get_text_gray_row;
715
2.68k
    else if (IsExtRGB(cinfo->in_color_space))
716
2.35k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
336
    else if (cinfo->in_color_space == JCS_CMYK)
718
336
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
3.15k
    need_iobuffer = FALSE;
722
3.15k
    break;
723
724
3.39k
  case '3':                     /* it's a text-format PPM file */
725
3.39k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
3.39k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
3.39k
    if (IsExtRGB(cinfo->in_color_space))
729
2.51k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
884
    else if (cinfo->in_color_space == JCS_CMYK)
731
381
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
503
    else
733
503
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
3.39k
    need_iobuffer = FALSE;
735
3.39k
    break;
736
737
28.8k
  case '5':                     /* it's a raw-format PGM file */
738
28.8k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
28.8k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
28.8k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
28.8k
    if (maxval > 255) {
743
1.95k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
291
        source->pub.get_pixel_rows = get_word_gray_row;
745
1.66k
      else if (IsExtRGB(cinfo->in_color_space))
746
1.45k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
206
      else if (cinfo->in_color_space == JCS_CMYK)
748
206
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
26.8k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
26.8k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
26.8k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
322
      source->pub.get_pixel_rows = get_raw_row;
755
322
      use_raw_buffer = TRUE;
756
322
      need_rescale = FALSE;
757
26.5k
    } else {
758
26.5k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
3.76k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
22.7k
      else if (IsExtRGB(cinfo->in_color_space))
761
20.4k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
2.37k
      else if (cinfo->in_color_space == JCS_CMYK)
763
2.37k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
26.5k
    }
767
28.8k
    break;
768
769
8.91k
  case '6':                     /* it's a raw-format PPM file */
770
8.91k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
8.91k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
8.91k
    if (maxval > 255) {
774
2.84k
      if (IsExtRGB(cinfo->in_color_space))
775
2.11k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
726
      else if (cinfo->in_color_space == JCS_CMYK)
777
303
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
423
      else
779
423
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
6.07k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
6.07k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
6.07k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
6.07k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
1.24k
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
174
      source->pub.get_pixel_rows = get_raw_row;
789
174
      use_raw_buffer = TRUE;
790
174
      need_rescale = FALSE;
791
5.90k
    } else {
792
5.90k
      if (IsExtRGB(cinfo->in_color_space))
793
4.35k
        source->pub.get_pixel_rows = get_rgb_row;
794
1.54k
      else if (cinfo->in_color_space == JCS_CMYK)
795
640
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
906
      else
797
906
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
5.90k
    }
799
8.91k
    break;
800
48.3k
  }
801
802
42.4k
  if (IsExtRGB(cinfo->in_color_space))
803
33.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
9.08k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
4.84k
    cinfo->input_components = 1;
806
4.24k
  else if (cinfo->in_color_space == JCS_CMYK)
807
4.24k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
42.4k
  if (need_iobuffer) {
811
36.4k
    if (c == '6')
812
7.58k
      source->buffer_width = (size_t)w * 3 *
813
7.58k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
28.8k
    else
815
28.8k
      source->buffer_width = (size_t)w *
816
28.8k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
36.4k
    source->iobuffer = (U_CHAR *)
818
36.4k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
36.4k
                                  source->buffer_width);
820
36.4k
  }
821
822
  /* Create compressor input buffer. */
823
42.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
496
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
496
    source->pub._buffer = &source->pixrow;
828
496
    source->pub.buffer_height = 1;
829
41.9k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
41.9k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
41.9k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
41.9k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
41.9k
    source->pub.buffer_height = 1;
835
41.9k
  }
836
837
  /* Compute the rescaling array if required. */
838
42.4k
  if (need_rescale) {
839
41.9k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
41.9k
    source->rescale = (_JSAMPLE *)
843
41.9k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
41.9k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
41.9k
                                           sizeof(_JSAMPLE)));
846
41.9k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
41.9k
                                        sizeof(_JSAMPLE)));
848
41.9k
    half_maxval = maxval / 2;
849
48.3M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
48.3M
      source->rescale[val] =
852
48.3M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
48.3M
                   maxval);
854
48.3M
    }
855
41.9k
  }
856
42.4k
}
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
2.96k
  case '2':                     /* it's a text-format PGM file */
679
6.26k
  case '3':                     /* it's a text-format PPM file */
680
31.9k
  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
70
    ERREXIT(cinfo, JERR_PPM_NOT);
695
39.7k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
483
    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.20k
  case '2':                     /* it's a text-format PGM file */
709
2.20k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
2.20k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
2.20k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
2.20k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
315
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.89k
    else if (IsExtRGB(cinfo->in_color_space))
716
1.57k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
315
    else if (cinfo->in_color_space == JCS_CMYK)
718
315
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
2.20k
    need_iobuffer = FALSE;
722
2.20k
    break;
723
724
2.57k
  case '3':                     /* it's a text-format PPM file */
725
2.57k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
2.57k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
2.57k
    if (IsExtRGB(cinfo->in_color_space))
729
1.84k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
736
    else if (cinfo->in_color_space == JCS_CMYK)
731
368
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
368
    else
733
368
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
2.57k
    need_iobuffer = FALSE;
735
2.57k
    break;
736
737
24.8k
  case '5':                     /* it's a raw-format PGM file */
738
24.8k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
24.8k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
24.8k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
24.8k
    if (maxval > 255) {
743
1.43k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
205
        source->pub.get_pixel_rows = get_word_gray_row;
745
1.23k
      else if (IsExtRGB(cinfo->in_color_space))
746
1.02k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
205
      else if (cinfo->in_color_space == JCS_CMYK)
748
205
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
23.4k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
23.4k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
23.4k
               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.4k
    } else {
758
23.4k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
3.34k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
20.0k
      else if (IsExtRGB(cinfo->in_color_space))
761
16.7k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
3.34k
      else if (cinfo->in_color_space == JCS_CMYK)
763
3.34k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
23.4k
    }
767
24.8k
    break;
768
769
7.25k
  case '6':                     /* it's a raw-format PPM file */
770
7.25k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
7.25k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
7.25k
    if (maxval > 255) {
774
2.10k
      if (IsExtRGB(cinfo->in_color_space))
775
1.50k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
602
      else if (cinfo->in_color_space == JCS_CMYK)
777
301
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
301
      else
779
301
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
5.14k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
5.14k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
5.14k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
5.14k
               (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.14k
    } else {
792
5.14k
      if (IsExtRGB(cinfo->in_color_space))
793
3.67k
        source->pub.get_pixel_rows = get_rgb_row;
794
1.47k
      else if (cinfo->in_color_space == JCS_CMYK)
795
735
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
735
      else
797
735
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
5.14k
    }
799
7.25k
    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.13k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
3.86k
    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
31.0k
    if (c == '6')
812
6.21k
      source->buffer_width = (size_t)w * 3 *
813
6.21k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
24.8k
    else
815
24.8k
      source->buffer_width = (size_t)w *
816
24.8k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
31.0k
    source->iobuffer = (U_CHAR *)
818
31.0k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
31.0k
                                  source->buffer_width);
820
31.0k
  }
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
52.0M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
52.0M
      source->rescale[val] =
852
52.0M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
52.0M
                   maxval);
854
52.0M
    }
855
35.4k
  }
856
35.4k
}
rdppm-16.c:start_input_ppm
Line
Count
Source
665
11.6k
{
666
11.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
11.6k
  int c;
668
11.6k
  unsigned int w, h, maxval;
669
11.6k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
11.6k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
11.6k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
11.6k
  switch (c) {
678
1.50k
  case '2':                     /* it's a text-format PGM file */
679
3.45k
  case '3':                     /* it's a text-format PPM file */
680
9.11k
  case '5':                     /* it's a raw-format PGM file */
681
11.5k
  case '6':                     /* it's a raw-format PPM file */
682
11.5k
    break;
683
14
  default:
684
14
    ERREXIT(cinfo, JERR_PPM_NOT);
685
14
    break;
686
11.6k
  }
687
688
  /* fetch the remaining header info */
689
11.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
11.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
11.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
11.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
35
    ERREXIT(cinfo, JERR_PPM_NOT);
695
11.5k
  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.5k
  cinfo->image_width = (JDIMENSION)w;
699
11.5k
  cinfo->image_height = (JDIMENSION)h;
700
11.5k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
11.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
11.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
11.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
11.5k
  switch (c) {
708
1.20k
  case '2':                     /* it's a text-format PGM file */
709
1.20k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.20k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.20k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.20k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
172
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.03k
    else if (IsExtRGB(cinfo->in_color_space))
716
860
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
172
    else if (cinfo->in_color_space == JCS_CMYK)
718
172
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.20k
    need_iobuffer = FALSE;
722
1.20k
    break;
723
724
1.51k
  case '3':                     /* it's a text-format PPM file */
725
1.51k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.51k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.51k
    if (IsExtRGB(cinfo->in_color_space))
729
1.08k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
432
    else if (cinfo->in_color_space == JCS_CMYK)
731
216
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
216
    else
733
216
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.51k
    need_iobuffer = FALSE;
735
1.51k
    break;
736
737
5.23k
  case '5':                     /* it's a raw-format PGM file */
738
5.23k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
5.23k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
5.23k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
5.23k
    if (maxval > 255) {
743
805
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
115
        source->pub.get_pixel_rows = get_word_gray_row;
745
690
      else if (IsExtRGB(cinfo->in_color_space))
746
575
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
115
      else if (cinfo->in_color_space == JCS_CMYK)
748
115
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
4.43k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
4.43k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
4.43k
               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.43k
    } else {
758
4.43k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
633
        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
633
      else if (cinfo->in_color_space == JCS_CMYK)
763
633
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
4.43k
    }
767
5.23k
    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.03k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.03k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.03k
               (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.03k
    } else {
792
1.03k
      if (IsExtRGB(cinfo->in_color_space))
793
740
        source->pub.get_pixel_rows = get_rgb_row;
794
296
      else if (cinfo->in_color_space == JCS_CMYK)
795
148
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
148
      else
797
148
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.03k
    }
799
2.21k
    break;
800
11.5k
  }
801
802
9.63k
  if (IsExtRGB(cinfo->in_color_space))
803
7.26k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.37k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
920
    cinfo->input_components = 1;
806
1.45k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.45k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
9.63k
  if (need_iobuffer) {
811
7.13k
    if (c == '6')
812
1.89k
      source->buffer_width = (size_t)w * 3 *
813
1.89k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
5.23k
    else
815
5.23k
      source->buffer_width = (size_t)w *
816
5.23k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
7.13k
    source->iobuffer = (U_CHAR *)
818
7.13k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
7.13k
                                  source->buffer_width);
820
7.13k
  }
821
822
  /* Create compressor input buffer. */
823
9.63k
  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.63k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
9.63k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
9.63k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
9.63k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
9.63k
    source->pub.buffer_height = 1;
835
9.63k
  }
836
837
  /* Compute the rescaling array if required. */
838
9.63k
  if (need_rescale) {
839
9.63k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
9.63k
    source->rescale = (_JSAMPLE *)
843
9.63k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
9.63k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
9.63k
                                           sizeof(_JSAMPLE)));
846
9.63k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
9.63k
                                        sizeof(_JSAMPLE)));
848
9.63k
    half_maxval = maxval / 2;
849
80.3M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
80.3M
      source->rescale[val] =
852
80.3M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
80.3M
                   maxval);
854
80.3M
    }
855
9.63k
  }
856
9.63k
}
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.7k
{
866
  /* no work */
867
59.7k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
865
28.4k
{
866
  /* no work */
867
28.4k
}
rdppm-12.c:finish_input_ppm
Line
Count
Source
865
26.7k
{
866
  /* no work */
867
26.7k
}
rdppm-16.c:finish_input_ppm
Line
Count
Source
865
4.60k
{
866
  /* no work */
867
4.60k
}
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.3k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
51.3k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
51.3k
      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.3k
{
877
48.3k
  ppm_source_ptr source;
878
879
48.3k
#if BITS_IN_JSAMPLE == 8
880
48.3k
  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.3k
  source = (ppm_source_ptr)
889
48.3k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
48.3k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
48.3k
  source->pub.start_input = start_input_ppm;
893
48.3k
  source->pub.finish_input = finish_input_ppm;
894
48.3k
  source->pub.max_pixels = 0;
895
896
48.3k
  return (cjpeg_source_ptr)source;
897
48.3k
}
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.6k
{
877
11.6k
  ppm_source_ptr source;
878
879
#if BITS_IN_JSAMPLE == 8
880
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
11.6k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
11.6k
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
0
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
11.6k
  source = (ppm_source_ptr)
889
11.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
11.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
11.6k
  source->pub.start_input = start_input_ppm;
893
11.6k
  source->pub.finish_input = finish_input_ppm;
894
11.6k
  source->pub.max_pixels = 0;
895
896
11.6k
  return (cjpeg_source_ptr)source;
897
11.6k
}
898
899
#endif /* defined(PPM_SUPPORTED) &&
900
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */