Coverage Report

Created: 2025-08-09 06:49

/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
171M
#define UCH(x)  ((int)(x))
49
50
51
#define ReadOK(file, buffer, len) \
52
205M
  (fread(buffer, 1, len, file) == ((size_t)(len)))
53
54
static int alpha_index[JPEG_NUMCS] = {
55
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
56
};
57
58
59
/* Private version of data source object */
60
61
typedef struct {
62
  struct cjpeg_source_struct pub; /* public fields */
63
64
  /* Usually these two pointers point to the same place: */
65
  U_CHAR *iobuffer;             /* fread's I/O buffer */
66
  _JSAMPROW pixrow;             /* compressor input buffer */
67
  size_t buffer_width;          /* width of I/O buffer */
68
  _JSAMPLE *rescale;            /* => maxval-remapping array, or NULL */
69
  unsigned int maxval;
70
} ppm_source_struct;
71
72
typedef ppm_source_struct *ppm_source_ptr;
73
74
75
LOCAL(int)
76
pbm_getc(FILE *infile)
77
/* Read next char, skipping over any comments */
78
/* A comment/newline sequence is returned as a newline */
79
2.11M
{
80
2.11M
  register int ch;
81
82
2.11M
  ch = getc(infile);
83
2.11M
  if (ch == '#') {
84
330k
    do {
85
330k
      ch = getc(infile);
86
330k
    } while (ch != '\n' && ch != EOF);
87
21.7k
  }
88
2.11M
  return ch;
89
2.11M
}
rdppm-8.c:pbm_getc
Line
Count
Source
79
875k
{
80
875k
  register int ch;
81
82
875k
  ch = getc(infile);
83
875k
  if (ch == '#') {
84
117k
    do {
85
117k
      ch = getc(infile);
86
117k
    } while (ch != '\n' && ch != EOF);
87
10.4k
  }
88
875k
  return ch;
89
875k
}
rdppm-12.c:pbm_getc
Line
Count
Source
79
936k
{
80
936k
  register int ch;
81
82
936k
  ch = getc(infile);
83
936k
  if (ch == '#') {
84
145k
    do {
85
145k
      ch = getc(infile);
86
145k
    } while (ch != '\n' && ch != EOF);
87
6.98k
  }
88
936k
  return ch;
89
936k
}
rdppm-16.c:pbm_getc
Line
Count
Source
79
301k
{
80
301k
  register int ch;
81
82
301k
  ch = getc(infile);
83
301k
  if (ch == '#') {
84
67.7k
    do {
85
67.7k
      ch = getc(infile);
86
67.7k
    } while (ch != '\n' && ch != EOF);
87
4.30k
  }
88
301k
  return ch;
89
301k
}
90
91
92
LOCAL(unsigned int)
93
read_pbm_integer(j_compress_ptr cinfo, FILE *infile, unsigned int maxval)
94
/* Read an unsigned decimal integer from the PPM file */
95
/* Swallows one trailing character after the integer */
96
/* Note that on a 16-bit-int machine, only values up to 64k can be read. */
97
/* This should not be a problem in practice. */
98
872k
{
99
872k
  register int ch;
100
872k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
922k
  do {
104
922k
    ch = pbm_getc(infile);
105
922k
    if (ch == EOF)
106
16.3k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
922k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
872k
  if (ch < '0' || ch > '9')
110
1.36k
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
872k
  val = ch - '0';
113
1.20M
  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
872k
  return val;
121
872k
}
rdppm-8.c:read_pbm_integer
Line
Count
Source
98
373k
{
99
373k
  register int ch;
100
373k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
393k
  do {
104
393k
    ch = pbm_getc(infile);
105
393k
    if (ch == EOF)
106
7.91k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
393k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
373k
  if (ch < '0' || ch > '9')
110
601
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
373k
  val = ch - '0';
113
490k
  while ((ch = pbm_getc(infile)) >= '0' && ch <= '9') {
114
116k
    val *= 10;
115
116k
    val += ch - '0';
116
116k
    if (val > maxval)
117
366
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
118
116k
  }
119
120
373k
  return val;
121
373k
}
rdppm-12.c:read_pbm_integer
Line
Count
Source
98
367k
{
99
367k
  register int ch;
100
367k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
389k
  do {
104
389k
    ch = pbm_getc(infile);
105
389k
    if (ch == EOF)
106
5.27k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
389k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
367k
  if (ch < '0' || ch > '9')
110
528
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
367k
  val = ch - '0';
113
553k
  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
367k
  return val;
121
367k
}
rdppm-16.c:read_pbm_integer
Line
Count
Source
98
131k
{
99
131k
  register int ch;
100
131k
  register unsigned int val;
101
102
  /* Skip any leading whitespace */
103
139k
  do {
104
139k
    ch = pbm_getc(infile);
105
139k
    if (ch == EOF)
106
3.18k
      ERREXIT(cinfo, JERR_INPUT_EOF);
107
139k
  } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
108
109
131k
  if (ch < '0' || ch > '9')
110
239
    ERREXIT(cinfo, JERR_PPM_NONNUMERIC);
111
112
131k
  val = ch - '0';
113
164k
  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
131k
  return val;
121
131k
}
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.5k
  for (col = cinfo->image_width; col > 0; col--) {
149
18.4k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
18.4k
  }
151
10.0k
  return 1;
152
10.0k
}
rdppm-8.c:get_text_gray_row
Line
Count
Source
139
5.28k
{
140
5.28k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
5.28k
  FILE *infile = source->pub.input_file;
142
5.28k
  register _JSAMPROW ptr;
143
5.28k
  register _JSAMPLE *rescale = source->rescale;
144
5.28k
  JDIMENSION col;
145
5.28k
  unsigned int maxval = source->maxval;
146
147
5.28k
  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.28k
  return 1;
152
5.28k
}
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.04k
  for (col = cinfo->image_width; col > 0; col--) {
149
5.09k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
5.09k
  }
151
2.95k
  return 1;
152
2.95k
}
rdppm-16.c:get_text_gray_row
Line
Count
Source
139
1.86k
{
140
1.86k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
141
1.86k
  FILE *infile = source->pub.input_file;
142
1.86k
  register _JSAMPROW ptr;
143
1.86k
  register _JSAMPLE *rescale = source->rescale;
144
1.86k
  JDIMENSION col;
145
1.86k
  unsigned int maxval = source->maxval;
146
147
1.86k
  ptr = source->pub._buffer[0];
148
5.41k
  for (col = cinfo->image_width; col > 0; col--) {
149
3.55k
    *ptr++ = rescale[read_pbm_integer(cinfo, infile, maxval)];
150
3.55k
  }
151
1.86k
  return 1;
152
1.86k
}
153
154
155
148M
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op) { \
156
578M
  for (col = cinfo->image_width; col > 0; col--) { \
157
429M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
158
429M
    alpha_set_op \
159
429M
    ptr += ps; \
160
429M
  } \
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.03k
    if (aindex >= 0)
183
1.16k
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
8.03k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
6.87k
    else
186
6.87k
      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.4k
{
168
26.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
26.4k
  FILE *infile = source->pub.input_file;
170
26.4k
  register _JSAMPROW ptr;
171
26.4k
  register _JSAMPLE *rescale = source->rescale;
172
26.4k
  JDIMENSION col;
173
26.4k
  unsigned int maxval = source->maxval;
174
26.4k
  register int rindex = rgb_red[cinfo->in_color_space];
175
26.4k
  register int gindex = rgb_green[cinfo->in_color_space];
176
26.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
26.4k
  register int aindex = alpha_index[cinfo->in_color_space];
178
26.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
26.4k
  ptr = source->pub._buffer[0];
181
26.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
4.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
19.0k
    else
192
19.0k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
21.7k
  }
194
26.4k
  return 1;
195
26.4k
}
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.89k
    else
192
9.89k
      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.30k
{
168
9.30k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
169
9.30k
  FILE *infile = source->pub.input_file;
170
9.30k
  register _JSAMPROW ptr;
171
9.30k
  register _JSAMPLE *rescale = source->rescale;
172
9.30k
  JDIMENSION col;
173
9.30k
  unsigned int maxval = source->maxval;
174
9.30k
  register int rindex = rgb_red[cinfo->in_color_space];
175
9.30k
  register int gindex = rgb_green[cinfo->in_color_space];
176
9.30k
  register int bindex = rgb_blue[cinfo->in_color_space];
177
9.30k
  register int aindex = alpha_index[cinfo->in_color_space];
178
9.30k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
179
180
9.30k
  ptr = source->pub._buffer[0];
181
9.30k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
182
1.02k
    if (aindex >= 0)
183
210
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
184
1.02k
                         ptr[aindex] = (_JSAMPLE)maxval;)
185
811
    else
186
811
      GRAY_RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
187
8.28k
  } else {
188
8.28k
    if (aindex >= 0)
189
1.65k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
190
8.28k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
191
6.63k
    else
192
6.63k
      GRAY_RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
193
8.28k
  }
194
9.30k
  return 1;
195
9.30k
}
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.08k
{
203
8.08k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
8.08k
  FILE *infile = source->pub.input_file;
205
8.08k
  register _JSAMPROW ptr;
206
8.08k
  register _JSAMPLE *rescale = source->rescale;
207
8.08k
  JDIMENSION col;
208
8.08k
  unsigned int maxval = source->maxval;
209
210
8.08k
  ptr = source->pub._buffer[0];
211
8.08k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
5.61k
    for (col = cinfo->image_width; col > 0; col--) {
213
3.70k
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
3.70k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
3.70k
      ptr += 4;
216
3.70k
    }
217
6.17k
  } 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.17k
  }
224
8.08k
  return 1;
225
8.08k
}
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.15k
  } 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.15k
  }
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.54k
    for (col = cinfo->image_width; col > 0; col--) {
219
4.07k
      _JSAMPLE gray = rescale[read_pbm_integer(cinfo, infile, maxval)];
220
4.07k
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
221
4.07k
      ptr += 4;
222
4.07k
    }
223
2.47k
  }
224
2.95k
  return 1;
225
2.95k
}
rdppm-16.c:get_text_gray_cmyk_row
Line
Count
Source
202
1.86k
{
203
1.86k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
204
1.86k
  FILE *infile = source->pub.input_file;
205
1.86k
  register _JSAMPROW ptr;
206
1.86k
  register _JSAMPLE *rescale = source->rescale;
207
1.86k
  JDIMENSION col;
208
1.86k
  unsigned int maxval = source->maxval;
209
210
1.86k
  ptr = source->pub._buffer[0];
211
1.86k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
212
1.11k
    for (col = cinfo->image_width; col > 0; col--) {
213
803
      _JSAMPLE gray = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
214
803
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
215
803
      ptr += 4;
216
803
    }
217
1.54k
  } else {
218
4.29k
    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.54k
  }
224
1.86k
  return 1;
225
1.86k
}
226
227
228
1.29M
#define RGB_READ_LOOP(read_op, alpha_set_op) { \
229
22.2M
  for (col = cinfo->image_width; col > 0; col--) { \
230
20.9M
    ptr[rindex] = read_op; \
231
20.9M
    ptr[gindex] = read_op; \
232
20.9M
    ptr[bindex] = read_op; \
233
20.9M
    alpha_set_op \
234
20.9M
    ptr += ps; \
235
20.9M
  } \
236
1.29M
}
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.2k
{
242
62.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
62.2k
  FILE *infile = source->pub.input_file;
244
62.2k
  register _JSAMPROW ptr;
245
62.2k
  register _JSAMPLE *rescale = source->rescale;
246
62.2k
  JDIMENSION col;
247
62.2k
  unsigned int maxval = source->maxval;
248
62.2k
  register int rindex = rgb_red[cinfo->in_color_space];
249
62.2k
  register int gindex = rgb_green[cinfo->in_color_space];
250
62.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
62.2k
  register int aindex = alpha_index[cinfo->in_color_space];
252
62.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
62.2k
  ptr = source->pub._buffer[0];
255
62.2k
  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.5k
    else
260
11.5k
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval), {})
261
48.7k
  } else {
262
48.7k
    if (aindex >= 0)
263
8.62k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
48.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
40.1k
    else
266
40.1k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
48.7k
  }
268
62.2k
  return 1;
269
62.2k
}
rdppm-8.c:get_text_rgb_row
Line
Count
Source
241
30.4k
{
242
30.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
30.4k
  FILE *infile = source->pub.input_file;
244
30.4k
  register _JSAMPROW ptr;
245
30.4k
  register _JSAMPLE *rescale = source->rescale;
246
30.4k
  JDIMENSION col;
247
30.4k
  unsigned int maxval = source->maxval;
248
30.4k
  register int rindex = rgb_red[cinfo->in_color_space];
249
30.4k
  register int gindex = rgb_green[cinfo->in_color_space];
250
30.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
30.4k
  register int aindex = alpha_index[cinfo->in_color_space];
252
30.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
30.4k
  ptr = source->pub._buffer[0];
255
30.4k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
256
6.36k
    if (aindex >= 0)
257
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.44k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
24.0k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
20.6k
    else
266
20.6k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
24.0k
  }
268
30.4k
  return 1;
269
30.4k
}
rdppm-12.c:get_text_rgb_row
Line
Count
Source
241
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.79k
    if (aindex >= 0)
257
959
      RGB_READ_LOOP((_JSAMPLE)read_pbm_integer(cinfo, infile, maxval),
258
4.79k
                    ptr[aindex] = (_JSAMPLE)maxval;)
259
3.83k
    else
260
3.83k
      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.4k
{
242
14.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
243
14.4k
  FILE *infile = source->pub.input_file;
244
14.4k
  register _JSAMPROW ptr;
245
14.4k
  register _JSAMPLE *rescale = source->rescale;
246
14.4k
  JDIMENSION col;
247
14.4k
  unsigned int maxval = source->maxval;
248
14.4k
  register int rindex = rgb_red[cinfo->in_color_space];
249
14.4k
  register int gindex = rgb_green[cinfo->in_color_space];
250
14.4k
  register int bindex = rgb_blue[cinfo->in_color_space];
251
14.4k
  register int aindex = alpha_index[cinfo->in_color_space];
252
14.4k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
253
254
14.4k
  ptr = source->pub._buffer[0];
255
14.4k
  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.1k
  } else {
262
12.1k
    if (aindex >= 0)
263
2.67k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)],
264
12.1k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
265
9.52k
    else
266
9.52k
      RGB_READ_LOOP(rescale[read_pbm_integer(cinfo, infile, maxval)], {})
267
12.1k
  }
268
14.4k
  return 1;
269
14.4k
}
270
271
272
METHODDEF(JDIMENSION)
273
get_text_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
274
/* This version is for reading text-format PPM files with any maxval and
275
   converting to CMYK */
276
10.5k
{
277
10.5k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
10.5k
  FILE *infile = source->pub.input_file;
279
10.5k
  register _JSAMPROW ptr;
280
10.5k
  register _JSAMPLE *rescale = source->rescale;
281
10.5k
  JDIMENSION col;
282
10.5k
  unsigned int maxval = source->maxval;
283
284
10.5k
  ptr = source->pub._buffer[0];
285
10.5k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
15.2k
    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.00k
  } else {
294
19.0k
    for (col = cinfo->image_width; col > 0; col--) {
295
12.0k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
12.0k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
12.0k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
12.0k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
12.0k
      ptr += 4;
300
12.0k
    }
301
7.00k
  }
302
10.5k
  return 1;
303
10.5k
}
rdppm-8.c:get_text_rgb_cmyk_row
Line
Count
Source
276
4.19k
{
277
4.19k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
4.19k
  FILE *infile = source->pub.input_file;
279
4.19k
  register _JSAMPROW ptr;
280
4.19k
  register _JSAMPLE *rescale = source->rescale;
281
4.19k
  JDIMENSION col;
282
4.19k
  unsigned int maxval = source->maxval;
283
284
4.19k
  ptr = source->pub._buffer[0];
285
4.19k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
4.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.46k
  } else {
294
7.24k
    for (col = cinfo->image_width; col > 0; col--) {
295
4.78k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
4.78k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
4.78k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
4.78k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
4.78k
      ptr += 4;
300
4.78k
    }
301
2.46k
  }
302
4.19k
  return 1;
303
4.19k
}
rdppm-12.c:get_text_rgb_cmyk_row
Line
Count
Source
276
3.47k
{
277
3.47k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
3.47k
  FILE *infile = source->pub.input_file;
279
3.47k
  register _JSAMPROW ptr;
280
3.47k
  register _JSAMPLE *rescale = source->rescale;
281
3.47k
  JDIMENSION col;
282
3.47k
  unsigned int maxval = source->maxval;
283
284
3.47k
  ptr = source->pub._buffer[0];
285
3.47k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
286
8.77k
    for (col = cinfo->image_width; col > 0; col--) {
287
7.81k
      _JSAMPLE r = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
288
7.81k
      _JSAMPLE g = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
289
7.81k
      _JSAMPLE b = (_JSAMPLE)read_pbm_integer(cinfo, infile, maxval);
290
7.81k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
291
7.81k
      ptr += 4;
292
7.81k
    }
293
2.51k
  } else {
294
6.92k
    for (col = cinfo->image_width; col > 0; col--) {
295
4.41k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
4.41k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
4.41k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
4.41k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
4.41k
      ptr += 4;
300
4.41k
    }
301
2.51k
  }
302
3.47k
  return 1;
303
3.47k
}
rdppm-16.c:get_text_rgb_cmyk_row
Line
Count
Source
276
2.89k
{
277
2.89k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
278
2.89k
  FILE *infile = source->pub.input_file;
279
2.89k
  register _JSAMPROW ptr;
280
2.89k
  register _JSAMPLE *rescale = source->rescale;
281
2.89k
  JDIMENSION col;
282
2.89k
  unsigned int maxval = source->maxval;
283
284
2.89k
  ptr = source->pub._buffer[0];
285
2.89k
  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.02k
  } else {
294
4.89k
    for (col = cinfo->image_width; col > 0; col--) {
295
2.87k
      _JSAMPLE r = rescale[read_pbm_integer(cinfo, infile, maxval)];
296
2.87k
      _JSAMPLE g = rescale[read_pbm_integer(cinfo, infile, maxval)];
297
2.87k
      _JSAMPLE b = rescale[read_pbm_integer(cinfo, infile, maxval)];
298
2.87k
      rgb_to_cmyk(maxval, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
299
2.87k
      ptr += 4;
300
2.87k
    }
301
2.02k
  }
302
2.89k
  return 1;
303
2.89k
}
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.8M
{
310
27.8M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
27.8M
  register _JSAMPROW ptr;
312
27.8M
  register U_CHAR *bufferptr;
313
27.8M
  register _JSAMPLE *rescale = source->rescale;
314
27.8M
  JDIMENSION col;
315
316
27.8M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
413
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
27.8M
  ptr = source->pub._buffer[0];
319
27.8M
  bufferptr = source->iobuffer;
320
111M
  for (col = cinfo->image_width; col > 0; col--) {
321
83.4M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
83.4M
  }
323
27.8M
  return 1;
324
27.8M
}
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
229
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
10.9M
  ptr = source->pub._buffer[0];
319
10.9M
  bufferptr = source->iobuffer;
320
47.8M
  for (col = cinfo->image_width; col > 0; col--) {
321
36.9M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
36.9M
  }
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.7M
  for (col = cinfo->image_width; col > 0; col--) {
321
37.5M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
37.5M
  }
323
14.2M
  return 1;
324
14.2M
}
rdppm-16.c:get_scaled_gray_row
Line
Count
Source
309
2.66M
{
310
2.66M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
311
2.66M
  register _JSAMPROW ptr;
312
2.66M
  register U_CHAR *bufferptr;
313
2.66M
  register _JSAMPLE *rescale = source->rescale;
314
2.66M
  JDIMENSION col;
315
316
2.66M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
317
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
318
2.66M
  ptr = source->pub._buffer[0];
319
2.66M
  bufferptr = source->iobuffer;
320
11.6M
  for (col = cinfo->image_width; col > 0; col--) {
321
8.99M
    *ptr++ = rescale[UCH(*bufferptr++)];
322
8.99M
  }
323
2.66M
  return 1;
324
2.66M
}
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.47k
    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.76M
    if (aindex >= 0)
350
231k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
9.53M
    else
352
9.53M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
138M
  } else {
354
138M
    if (aindex >= 0)
355
24.3M
      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
64.1M
{
332
64.1M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
64.1M
  register _JSAMPROW ptr;
334
64.1M
  register U_CHAR *bufferptr;
335
64.1M
  register _JSAMPLE *rescale = source->rescale;
336
64.1M
  JDIMENSION col;
337
64.1M
  unsigned int maxval = source->maxval;
338
64.1M
  register int rindex = rgb_red[cinfo->in_color_space];
339
64.1M
  register int gindex = rgb_green[cinfo->in_color_space];
340
64.1M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
64.1M
  register int aindex = alpha_index[cinfo->in_color_space];
342
64.1M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
64.1M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
1.55k
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
64.1M
  ptr = source->pub._buffer[0];
347
64.1M
  bufferptr = source->iobuffer;
348
64.1M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
349
9.76M
    if (aindex >= 0)
350
231k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
351
9.53M
    else
352
9.53M
      GRAY_RGB_READ_LOOP(*bufferptr++, {})
353
54.3M
  } else {
354
54.3M
    if (aindex >= 0)
355
7.46M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
54.3M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
46.9M
    else
358
46.9M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
54.3M
  }
360
64.1M
  return 1;
361
64.1M
}
rdppm-12.c:get_gray_rgb_row
Line
Count
Source
331
71.1M
{
332
71.1M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
71.1M
  register _JSAMPROW ptr;
334
71.1M
  register U_CHAR *bufferptr;
335
71.1M
  register _JSAMPLE *rescale = source->rescale;
336
71.1M
  JDIMENSION col;
337
71.1M
  unsigned int maxval = source->maxval;
338
71.1M
  register int rindex = rgb_red[cinfo->in_color_space];
339
71.1M
  register int gindex = rgb_green[cinfo->in_color_space];
340
71.1M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
71.1M
  register int aindex = alpha_index[cinfo->in_color_space];
342
71.1M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
71.1M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
610
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
71.1M
  ptr = source->pub._buffer[0];
347
71.1M
  bufferptr = source->iobuffer;
348
71.1M
  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.1M
  } else {
354
71.1M
    if (aindex >= 0)
355
14.2M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
71.1M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
56.9M
    else
358
56.9M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
71.1M
  }
360
71.1M
  return 1;
361
71.1M
}
rdppm-16.c:get_gray_rgb_row
Line
Count
Source
331
13.3M
{
332
13.3M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
333
13.3M
  register _JSAMPROW ptr;
334
13.3M
  register U_CHAR *bufferptr;
335
13.3M
  register _JSAMPLE *rescale = source->rescale;
336
13.3M
  JDIMENSION col;
337
13.3M
  unsigned int maxval = source->maxval;
338
13.3M
  register int rindex = rgb_red[cinfo->in_color_space];
339
13.3M
  register int gindex = rgb_green[cinfo->in_color_space];
340
13.3M
  register int bindex = rgb_blue[cinfo->in_color_space];
341
13.3M
  register int aindex = alpha_index[cinfo->in_color_space];
342
13.3M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
343
344
13.3M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
345
310
    ERREXIT(cinfo, JERR_INPUT_EOF);
346
13.3M
  ptr = source->pub._buffer[0];
347
13.3M
  bufferptr = source->iobuffer;
348
13.3M
  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.3M
  } else {
354
13.3M
    if (aindex >= 0)
355
2.66M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
356
13.3M
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;)
357
10.6M
    else
358
10.6M
      GRAY_RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
359
13.3M
  }
360
13.3M
  return 1;
361
13.3M
}
362
363
364
METHODDEF(JDIMENSION)
365
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
366
/* This version is for reading raw-byte-format PGM files with any maxval
367
   and converting to CMYK */
368
24.5M
{
369
24.5M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
24.5M
  register _JSAMPROW ptr;
371
24.5M
  register U_CHAR *bufferptr;
372
24.5M
  register _JSAMPLE *rescale = source->rescale;
373
24.5M
  JDIMENSION col;
374
24.5M
  unsigned int maxval = source->maxval;
375
376
24.5M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
405
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
24.5M
  ptr = source->pub._buffer[0];
379
24.5M
  bufferptr = source->iobuffer;
380
24.5M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
381
1.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.3M
  } else {
387
93.9M
    for (col = cinfo->image_width; col > 0; col--) {
388
69.6M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
69.6M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
69.6M
      ptr += 4;
391
69.6M
    }
392
24.3M
  }
393
24.5M
  return 1;
394
24.5M
}
rdppm-8.c:get_gray_cmyk_row
Line
Count
Source
368
7.69M
{
369
7.69M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
7.69M
  register _JSAMPROW ptr;
371
7.69M
  register U_CHAR *bufferptr;
372
7.69M
  register _JSAMPLE *rescale = source->rescale;
373
7.69M
  JDIMENSION col;
374
7.69M
  unsigned int maxval = source->maxval;
375
376
7.69M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
221
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
7.69M
  ptr = source->pub._buffer[0];
379
7.69M
  bufferptr = source->iobuffer;
380
7.69M
  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.41M
  } else {
387
30.5M
    for (col = cinfo->image_width; col > 0; col--) {
388
23.1M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
23.1M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
23.1M
      ptr += 4;
391
23.1M
    }
392
7.41M
  }
393
7.69M
  return 1;
394
7.69M
}
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.7M
    for (col = cinfo->image_width; col > 0; col--) {
388
37.5M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
37.5M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
37.5M
      ptr += 4;
391
37.5M
    }
392
14.2M
  }
393
14.2M
  return 1;
394
14.2M
}
rdppm-16.c:get_gray_cmyk_row
Line
Count
Source
368
2.66M
{
369
2.66M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
370
2.66M
  register _JSAMPROW ptr;
371
2.66M
  register U_CHAR *bufferptr;
372
2.66M
  register _JSAMPLE *rescale = source->rescale;
373
2.66M
  JDIMENSION col;
374
2.66M
  unsigned int maxval = source->maxval;
375
376
2.66M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
377
62
    ERREXIT(cinfo, JERR_INPUT_EOF);
378
2.66M
  ptr = source->pub._buffer[0];
379
2.66M
  bufferptr = source->iobuffer;
380
2.66M
  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.66M
  } else {
387
11.6M
    for (col = cinfo->image_width; col > 0; col--) {
388
8.99M
      _JSAMPLE gray = rescale[UCH(*bufferptr++)];
389
8.99M
      rgb_to_cmyk(maxval, gray, gray, gray, ptr, ptr + 1, ptr + 2, ptr + 3);
390
8.99M
      ptr += 4;
391
8.99M
    }
392
2.66M
  }
393
2.66M
  return 1;
394
2.66M
}
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.22M
{
401
1.22M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
1.22M
  register _JSAMPROW ptr;
403
1.22M
  register U_CHAR *bufferptr;
404
1.22M
  register _JSAMPLE *rescale = source->rescale;
405
1.22M
  JDIMENSION col;
406
1.22M
  unsigned int maxval = source->maxval;
407
1.22M
  register int rindex = rgb_red[cinfo->in_color_space];
408
1.22M
  register int gindex = rgb_green[cinfo->in_color_space];
409
1.22M
  register int bindex = rgb_blue[cinfo->in_color_space];
410
1.22M
  register int aindex = alpha_index[cinfo->in_color_space];
411
1.22M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
1.22M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
3.30k
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
1.22M
  ptr = source->pub._buffer[0];
416
1.22M
  bufferptr = source->iobuffer;
417
1.22M
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
34.1k
    if (aindex >= 0)
419
5.42k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
28.7k
    else
421
28.7k
      RGB_READ_LOOP(*bufferptr++, {})
422
1.19M
  } else {
423
1.19M
    if (aindex >= 0)
424
225k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
1.19M
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
969k
    else
427
969k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
1.19M
  }
429
1.22M
  return 1;
430
1.22M
}
rdppm-8.c:get_rgb_row
Line
Count
Source
400
145k
{
401
145k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
145k
  register _JSAMPROW ptr;
403
145k
  register U_CHAR *bufferptr;
404
145k
  register _JSAMPLE *rescale = source->rescale;
405
145k
  JDIMENSION col;
406
145k
  unsigned int maxval = source->maxval;
407
145k
  register int rindex = rgb_red[cinfo->in_color_space];
408
145k
  register int gindex = rgb_green[cinfo->in_color_space];
409
145k
  register int bindex = rgb_blue[cinfo->in_color_space];
410
145k
  register int aindex = alpha_index[cinfo->in_color_space];
411
145k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
145k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
1.84k
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
145k
  ptr = source->pub._buffer[0];
416
145k
  bufferptr = source->iobuffer;
417
145k
  if (maxval == (1U << cinfo->data_precision) - 1U) {
418
34.1k
    if (aindex >= 0)
419
5.42k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = (_JSAMPLE)maxval;)
420
28.7k
    else
421
28.7k
      RGB_READ_LOOP(*bufferptr++, {})
422
111k
  } else {
423
111k
    if (aindex >= 0)
424
9.23k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
111k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
101k
    else
427
101k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
111k
  }
429
145k
  return 1;
430
145k
}
rdppm-12.c:get_rgb_row
Line
Count
Source
400
1.07M
{
401
1.07M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
402
1.07M
  register _JSAMPROW ptr;
403
1.07M
  register U_CHAR *bufferptr;
404
1.07M
  register _JSAMPLE *rescale = source->rescale;
405
1.07M
  JDIMENSION col;
406
1.07M
  unsigned int maxval = source->maxval;
407
1.07M
  register int rindex = rgb_red[cinfo->in_color_space];
408
1.07M
  register int gindex = rgb_green[cinfo->in_color_space];
409
1.07M
  register int bindex = rgb_blue[cinfo->in_color_space];
410
1.07M
  register int aindex = alpha_index[cinfo->in_color_space];
411
1.07M
  register int ps = rgb_pixelsize[cinfo->in_color_space];
412
413
1.07M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
414
945
    ERREXIT(cinfo, JERR_INPUT_EOF);
415
1.07M
  ptr = source->pub._buffer[0];
416
1.07M
  bufferptr = source->iobuffer;
417
1.07M
  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.07M
  } else {
423
1.07M
    if (aindex >= 0)
424
214k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)],
425
1.07M
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;)
426
858k
    else
427
858k
      RGB_READ_LOOP(rescale[UCH(*bufferptr++)], {})
428
1.07M
  }
429
1.07M
  return 1;
430
1.07M
}
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
515
    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.67k
    else
427
9.67k
      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
231k
{
438
231k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
231k
  register _JSAMPROW ptr;
440
231k
  register U_CHAR *bufferptr;
441
231k
  register _JSAMPLE *rescale = source->rescale;
442
231k
  JDIMENSION col;
443
231k
  unsigned int maxval = source->maxval;
444
445
231k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
595
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
231k
  ptr = source->pub._buffer[0];
448
231k
  bufferptr = source->iobuffer;
449
231k
  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
225k
  } 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
225k
  }
466
231k
  return 1;
467
231k
}
rdppm-8.c:get_rgb_cmyk_row
Line
Count
Source
437
14.9k
{
438
14.9k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
14.9k
  register _JSAMPROW ptr;
440
14.9k
  register U_CHAR *bufferptr;
441
14.9k
  register _JSAMPLE *rescale = source->rescale;
442
14.9k
  JDIMENSION col;
443
14.9k
  unsigned int maxval = source->maxval;
444
445
14.9k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
303
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
14.9k
  ptr = source->pub._buffer[0];
448
14.9k
  bufferptr = source->iobuffer;
449
14.9k
  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.17k
  } 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.17k
  }
466
14.9k
  return 1;
467
14.9k
}
rdppm-12.c:get_rgb_cmyk_row
Line
Count
Source
437
214k
{
438
214k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
214k
  register _JSAMPROW ptr;
440
214k
  register U_CHAR *bufferptr;
441
214k
  register _JSAMPLE *rescale = source->rescale;
442
214k
  JDIMENSION col;
443
214k
  unsigned int maxval = source->maxval;
444
445
214k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
189
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
214k
  ptr = source->pub._buffer[0];
448
214k
  bufferptr = source->iobuffer;
449
214k
  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
214k
  } 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
214k
  }
466
214k
  return 1;
467
214k
}
rdppm-16.c:get_rgb_cmyk_row
Line
Count
Source
437
2.39k
{
438
2.39k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
439
2.39k
  register _JSAMPROW ptr;
440
2.39k
  register U_CHAR *bufferptr;
441
2.39k
  register _JSAMPLE *rescale = source->rescale;
442
2.39k
  JDIMENSION col;
443
2.39k
  unsigned int maxval = source->maxval;
444
445
2.39k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
446
103
    ERREXIT(cinfo, JERR_INPUT_EOF);
447
2.39k
  ptr = source->pub._buffer[0];
448
2.39k
  bufferptr = source->iobuffer;
449
2.39k
  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.39k
  } 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.39k
  }
466
2.39k
  return 1;
467
2.39k
}
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.90M
{
478
1.90M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.90M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
162
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.90M
  return 1;
483
1.90M
}
rdppm-8.c:get_raw_row
Line
Count
Source
477
1.90M
{
478
1.90M
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
479
480
1.90M
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
481
162
    ERREXIT(cinfo, JERR_INPUT_EOF);
482
1.90M
  return 1;
483
1.90M
}
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
352
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
71.1k
  ptr = source->pub._buffer[0];
500
71.1k
  bufferptr = source->iobuffer;
501
447k
  for (col = cinfo->image_width; col > 0; col--) {
502
376k
    register unsigned int temp;
503
376k
    temp  = UCH(*bufferptr++) << 8;
504
376k
    temp |= UCH(*bufferptr++);
505
376k
    if (temp > maxval)
506
207
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
376k
    *ptr++ = rescale[temp];
508
376k
  }
509
71.1k
  return 1;
510
71.1k
}
rdppm-8.c:get_word_gray_row
Line
Count
Source
489
17.6k
{
490
17.6k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
17.6k
  register _JSAMPROW ptr;
492
17.6k
  register U_CHAR *bufferptr;
493
17.6k
  register _JSAMPLE *rescale = source->rescale;
494
17.6k
  JDIMENSION col;
495
17.6k
  unsigned int maxval = source->maxval;
496
497
17.6k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
172
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
17.6k
  ptr = source->pub._buffer[0];
500
17.6k
  bufferptr = source->iobuffer;
501
180k
  for (col = cinfo->image_width; col > 0; col--) {
502
162k
    register unsigned int temp;
503
162k
    temp  = UCH(*bufferptr++) << 8;
504
162k
    temp |= UCH(*bufferptr++);
505
162k
    if (temp > maxval)
506
102
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
162k
    *ptr++ = rescale[temp];
508
162k
  }
509
17.6k
  return 1;
510
17.6k
}
rdppm-12.c:get_word_gray_row
Line
Count
Source
489
48.8k
{
490
48.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
48.8k
  register _JSAMPROW ptr;
492
48.8k
  register U_CHAR *bufferptr;
493
48.8k
  register _JSAMPLE *rescale = source->rescale;
494
48.8k
  JDIMENSION col;
495
48.8k
  unsigned int maxval = source->maxval;
496
497
48.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
119
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
48.8k
  ptr = source->pub._buffer[0];
500
48.8k
  bufferptr = source->iobuffer;
501
164k
  for (col = cinfo->image_width; col > 0; col--) {
502
116k
    register unsigned int temp;
503
116k
    temp  = UCH(*bufferptr++) << 8;
504
116k
    temp |= UCH(*bufferptr++);
505
116k
    if (temp > maxval)
506
72
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
116k
    *ptr++ = rescale[temp];
508
116k
  }
509
48.8k
  return 1;
510
48.8k
}
rdppm-16.c:get_word_gray_row
Line
Count
Source
489
4.64k
{
490
4.64k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
491
4.64k
  register _JSAMPROW ptr;
492
4.64k
  register U_CHAR *bufferptr;
493
4.64k
  register _JSAMPLE *rescale = source->rescale;
494
4.64k
  JDIMENSION col;
495
4.64k
  unsigned int maxval = source->maxval;
496
497
4.64k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
498
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
499
4.64k
  ptr = source->pub._buffer[0];
500
4.64k
  bufferptr = source->iobuffer;
501
102k
  for (col = cinfo->image_width; col > 0; col--) {
502
97.6k
    register unsigned int temp;
503
97.6k
    temp  = UCH(*bufferptr++) << 8;
504
97.6k
    temp |= UCH(*bufferptr++);
505
97.6k
    if (temp > maxval)
506
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
507
97.6k
    *ptr++ = rescale[temp];
508
97.6k
  }
509
4.64k
  return 1;
510
4.64k
}
511
512
513
METHODDEF(JDIMENSION)
514
get_word_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
515
/* This version is for reading raw-word-format PGM files with any maxval */
516
355k
{
517
355k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
355k
  register _JSAMPROW ptr;
519
355k
  register U_CHAR *bufferptr;
520
355k
  register _JSAMPLE *rescale = source->rescale;
521
355k
  JDIMENSION col;
522
355k
  unsigned int maxval = source->maxval;
523
355k
  register int rindex = rgb_red[cinfo->in_color_space];
524
355k
  register int gindex = rgb_green[cinfo->in_color_space];
525
355k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
355k
  register int aindex = alpha_index[cinfo->in_color_space];
527
355k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
355k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
1.76k
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
355k
  ptr = source->pub._buffer[0];
532
355k
  bufferptr = source->iobuffer;
533
2.23M
  for (col = cinfo->image_width; col > 0; col--) {
534
1.88M
    register unsigned int temp;
535
1.88M
    temp  = UCH(*bufferptr++) << 8;
536
1.88M
    temp |= UCH(*bufferptr++);
537
1.88M
    if (temp > maxval)
538
1.03k
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
1.88M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
1.88M
    if (aindex >= 0)
541
351k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
1.88M
    ptr += ps;
543
1.88M
  }
544
355k
  return 1;
545
355k
}
rdppm-8.c:get_word_gray_rgb_row
Line
Count
Source
516
88.3k
{
517
88.3k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
88.3k
  register _JSAMPROW ptr;
519
88.3k
  register U_CHAR *bufferptr;
520
88.3k
  register _JSAMPLE *rescale = source->rescale;
521
88.3k
  JDIMENSION col;
522
88.3k
  unsigned int maxval = source->maxval;
523
88.3k
  register int rindex = rgb_red[cinfo->in_color_space];
524
88.3k
  register int gindex = rgb_green[cinfo->in_color_space];
525
88.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
88.3k
  register int aindex = alpha_index[cinfo->in_color_space];
527
88.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
88.3k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
860
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
88.3k
  ptr = source->pub._buffer[0];
532
88.3k
  bufferptr = source->iobuffer;
533
901k
  for (col = cinfo->image_width; col > 0; col--) {
534
812k
    register unsigned int temp;
535
812k
    temp  = UCH(*bufferptr++) << 8;
536
812k
    temp |= UCH(*bufferptr++);
537
812k
    if (temp > maxval)
538
510
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
812k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
812k
    if (aindex >= 0)
541
137k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
812k
    ptr += ps;
543
812k
  }
544
88.3k
  return 1;
545
88.3k
}
rdppm-12.c:get_word_gray_rgb_row
Line
Count
Source
516
244k
{
517
244k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
244k
  register _JSAMPROW ptr;
519
244k
  register U_CHAR *bufferptr;
520
244k
  register _JSAMPLE *rescale = source->rescale;
521
244k
  JDIMENSION col;
522
244k
  unsigned int maxval = source->maxval;
523
244k
  register int rindex = rgb_red[cinfo->in_color_space];
524
244k
  register int gindex = rgb_green[cinfo->in_color_space];
525
244k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
244k
  register int aindex = alpha_index[cinfo->in_color_space];
527
244k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
244k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
595
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
244k
  ptr = source->pub._buffer[0];
532
244k
  bufferptr = source->iobuffer;
533
824k
  for (col = cinfo->image_width; col > 0; col--) {
534
580k
    register unsigned int temp;
535
580k
    temp  = UCH(*bufferptr++) << 8;
536
580k
    temp |= UCH(*bufferptr++);
537
580k
    if (temp > maxval)
538
360
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
580k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
580k
    if (aindex >= 0)
541
115k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
580k
    ptr += ps;
543
580k
  }
544
244k
  return 1;
545
244k
}
rdppm-16.c:get_word_gray_rgb_row
Line
Count
Source
516
23.2k
{
517
23.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
518
23.2k
  register _JSAMPROW ptr;
519
23.2k
  register U_CHAR *bufferptr;
520
23.2k
  register _JSAMPLE *rescale = source->rescale;
521
23.2k
  JDIMENSION col;
522
23.2k
  unsigned int maxval = source->maxval;
523
23.2k
  register int rindex = rgb_red[cinfo->in_color_space];
524
23.2k
  register int gindex = rgb_green[cinfo->in_color_space];
525
23.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
526
23.2k
  register int aindex = alpha_index[cinfo->in_color_space];
527
23.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
528
529
23.2k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
530
305
    ERREXIT(cinfo, JERR_INPUT_EOF);
531
23.2k
  ptr = source->pub._buffer[0];
532
23.2k
  bufferptr = source->iobuffer;
533
511k
  for (col = cinfo->image_width; col > 0; col--) {
534
488k
    register unsigned int temp;
535
488k
    temp  = UCH(*bufferptr++) << 8;
536
488k
    temp |= UCH(*bufferptr++);
537
488k
    if (temp > maxval)
538
165
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
539
488k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = rescale[temp];
540
488k
    if (aindex >= 0)
541
97.6k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
542
488k
    ptr += ps;
543
488k
  }
544
23.2k
  return 1;
545
23.2k
}
546
547
548
METHODDEF(JDIMENSION)
549
get_word_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
550
/* This version is for reading raw-word-format PGM files with any maxval */
551
58.4k
{
552
58.4k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
58.4k
  register _JSAMPROW ptr;
554
58.4k
  register U_CHAR *bufferptr;
555
58.4k
  register _JSAMPLE *rescale = source->rescale;
556
58.4k
  JDIMENSION col;
557
58.4k
  unsigned int maxval = source->maxval;
558
559
58.4k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
300
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
58.4k
  ptr = source->pub._buffer[0];
562
58.4k
  bufferptr = source->iobuffer;
563
410k
  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.00k
{
552
5.00k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
5.00k
  register _JSAMPROW ptr;
554
5.00k
  register U_CHAR *bufferptr;
555
5.00k
  register _JSAMPLE *rescale = source->rescale;
556
5.00k
  JDIMENSION col;
557
5.00k
  unsigned int maxval = source->maxval;
558
559
5.00k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
120
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
5.00k
  ptr = source->pub._buffer[0];
562
5.00k
  bufferptr = source->iobuffer;
563
142k
  for (col = cinfo->image_width; col > 0; col--) {
564
137k
    register unsigned int gray;
565
137k
    gray  = UCH(*bufferptr++) << 8;
566
137k
    gray |= UCH(*bufferptr++);
567
137k
    if (gray > maxval)
568
75
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
137k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
137k
                ptr + 1, ptr + 2, ptr + 3);
571
137k
    ptr += 4;
572
137k
  }
573
5.00k
  return 1;
574
5.00k
}
rdppm-12.c:get_word_gray_cmyk_row
Line
Count
Source
551
48.8k
{
552
48.8k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
48.8k
  register _JSAMPROW ptr;
554
48.8k
  register U_CHAR *bufferptr;
555
48.8k
  register _JSAMPLE *rescale = source->rescale;
556
48.8k
  JDIMENSION col;
557
48.8k
  unsigned int maxval = source->maxval;
558
559
48.8k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
119
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
48.8k
  ptr = source->pub._buffer[0];
562
48.8k
  bufferptr = source->iobuffer;
563
164k
  for (col = cinfo->image_width; col > 0; col--) {
564
116k
    register unsigned int gray;
565
116k
    gray  = UCH(*bufferptr++) << 8;
566
116k
    gray |= UCH(*bufferptr++);
567
116k
    if (gray > maxval)
568
72
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
116k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
116k
                ptr + 1, ptr + 2, ptr + 3);
571
116k
    ptr += 4;
572
116k
  }
573
48.8k
  return 1;
574
48.8k
}
rdppm-16.c:get_word_gray_cmyk_row
Line
Count
Source
551
4.64k
{
552
4.64k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
553
4.64k
  register _JSAMPROW ptr;
554
4.64k
  register U_CHAR *bufferptr;
555
4.64k
  register _JSAMPLE *rescale = source->rescale;
556
4.64k
  JDIMENSION col;
557
4.64k
  unsigned int maxval = source->maxval;
558
559
4.64k
  if (!ReadOK(source->pub.input_file, source->iobuffer, source->buffer_width))
560
61
    ERREXIT(cinfo, JERR_INPUT_EOF);
561
4.64k
  ptr = source->pub._buffer[0];
562
4.64k
  bufferptr = source->iobuffer;
563
102k
  for (col = cinfo->image_width; col > 0; col--) {
564
97.6k
    register unsigned int gray;
565
97.6k
    gray  = UCH(*bufferptr++) << 8;
566
97.6k
    gray |= UCH(*bufferptr++);
567
97.6k
    if (gray > maxval)
568
33
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
569
97.6k
    rgb_to_cmyk(maxval, rescale[gray], rescale[gray], rescale[gray], ptr,
570
97.6k
                ptr + 1, ptr + 2, ptr + 3);
571
97.6k
    ptr += 4;
572
97.6k
  }
573
4.64k
  return 1;
574
4.64k
}
575
576
577
METHODDEF(JDIMENSION)
578
get_word_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
579
/* This version is for reading raw-word-format PPM files with any maxval */
580
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.05k
    ERREXIT(cinfo, JERR_INPUT_EOF);
595
95.0k
  ptr = source->pub._buffer[0];
596
95.0k
  bufferptr = source->iobuffer;
597
371k
  for (col = cinfo->image_width; col > 0; col--) {
598
276k
    register unsigned int temp;
599
276k
    temp  = UCH(*bufferptr++) << 8;
600
276k
    temp |= UCH(*bufferptr++);
601
276k
    if (temp > maxval)
602
825
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
276k
    ptr[rindex] = rescale[temp];
604
276k
    temp  = UCH(*bufferptr++) << 8;
605
276k
    temp |= UCH(*bufferptr++);
606
276k
    if (temp > maxval)
607
645
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
276k
    ptr[gindex] = rescale[temp];
609
276k
    temp  = UCH(*bufferptr++) << 8;
610
276k
    temp |= UCH(*bufferptr++);
611
276k
    if (temp > maxval)
612
635
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
276k
    ptr[bindex] = rescale[temp];
614
276k
    if (aindex >= 0)
615
51.9k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
276k
    ptr += ps;
617
276k
  }
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
72.0k
  for (col = cinfo->image_width; col > 0; col--) {
598
45.8k
    register unsigned int temp;
599
45.8k
    temp  = UCH(*bufferptr++) << 8;
600
45.8k
    temp |= UCH(*bufferptr++);
601
45.8k
    if (temp > maxval)
602
445
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
603
45.8k
    ptr[rindex] = rescale[temp];
604
45.8k
    temp  = UCH(*bufferptr++) << 8;
605
45.8k
    temp |= UCH(*bufferptr++);
606
45.8k
    if (temp > maxval)
607
345
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
608
45.8k
    ptr[gindex] = rescale[temp];
609
45.8k
    temp  = UCH(*bufferptr++) << 8;
610
45.8k
    temp |= UCH(*bufferptr++);
611
45.8k
    if (temp > maxval)
612
310
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
45.8k
    ptr[bindex] = rescale[temp];
614
45.8k
    if (aindex >= 0)
615
6.02k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
45.8k
    ptr += ps;
617
45.8k
  }
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
215
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
613
97.8k
    ptr[bindex] = rescale[temp];
614
97.8k
    if (aindex >= 0)
615
19.4k
      ptr[aindex] = (1 << cinfo->data_precision) - 1;
616
97.8k
    ptr += ps;
617
97.8k
  }
618
56.4k
  return 1;
619
56.4k
}
rdppm-16.c:get_word_rgb_row
Line
Count
Source
580
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
400
    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
160
      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
100
      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
370
    ERREXIT(cinfo, JERR_INPUT_EOF);
635
17.3k
  ptr = source->pub._buffer[0];
636
17.3k
  bufferptr = source->iobuffer;
637
69.5k
  for (col = cinfo->image_width; col > 0; col--) {
638
52.2k
    register unsigned int r, g, b;
639
52.2k
    r  = UCH(*bufferptr++) << 8;
640
52.2k
    r |= UCH(*bufferptr++);
641
52.2k
    if (r > maxval)
642
133
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
52.2k
    g  = UCH(*bufferptr++) << 8;
644
52.2k
    g |= UCH(*bufferptr++);
645
52.2k
    if (g > maxval)
646
109
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
52.2k
    b  = UCH(*bufferptr++) << 8;
648
52.2k
    b |= UCH(*bufferptr++);
649
52.2k
    if (b > maxval)
650
106
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
52.2k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
52.2k
                ptr + 2, ptr + 3);
653
52.2k
    ptr += 4;
654
52.2k
  }
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.75k
  for (col = cinfo->image_width; col > 0; col--) {
638
6.16k
    register unsigned int r, g, b;
639
6.16k
    r  = UCH(*bufferptr++) << 8;
640
6.16k
    r |= UCH(*bufferptr++);
641
6.16k
    if (r > maxval)
642
57
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
643
6.16k
    g  = UCH(*bufferptr++) << 8;
644
6.16k
    g |= UCH(*bufferptr++);
645
6.16k
    if (g > maxval)
646
49
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
647
6.16k
    b  = UCH(*bufferptr++) << 8;
648
6.16k
    b |= UCH(*bufferptr++);
649
6.16k
    if (b > maxval)
650
41
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
6.16k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
6.16k
                ptr + 2, ptr + 3);
653
6.16k
    ptr += 4;
654
6.16k
  }
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
43
      ERREXIT(cinfo, JERR_PPM_OUTOFRANGE);
651
19.5k
    rgb_to_cmyk(maxval, rescale[r], rescale[g], rescale[b], ptr, ptr + 1,
652
19.5k
                ptr + 2, ptr + 3);
653
19.5k
    ptr += 4;
654
19.5k
  }
655
11.2k
  return 1;
656
11.2k
}
rdppm-16.c:get_word_rgb_cmyk_row
Line
Count
Source
625
2.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
80
    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
32
      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
20
      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.61k
  case '2':                     /* it's a text-format PGM file */
679
18.2k
  case '3':                     /* it's a text-format PPM file */
680
79.5k
  case '5':                     /* it's a raw-format PGM file */
681
99.5k
  case '6':                     /* it's a raw-format PPM file */
682
99.5k
    break;
683
62
  default:
684
62
    ERREXIT(cinfo, JERR_PPM_NOT);
685
62
    break;
686
99.6k
  }
687
688
  /* fetch the remaining header info */
689
99.5k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
99.5k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
99.5k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
99.5k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
185
    ERREXIT(cinfo, JERR_PPM_NOT);
695
99.5k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
1.42k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
99.5k
  cinfo->image_width = (JDIMENSION)w;
699
99.5k
  cinfo->image_height = (JDIMENSION)h;
700
99.5k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
99.5k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
99.5k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
99.5k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
99.5k
  switch (c) {
708
6.61k
  case '2':                     /* it's a text-format PGM file */
709
6.61k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
6.61k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
6.61k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
6.61k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
964
      source->pub.get_pixel_rows = get_text_gray_row;
715
5.64k
    else if (IsExtRGB(cinfo->in_color_space))
716
4.82k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
828
    else if (cinfo->in_color_space == JCS_CMYK)
718
828
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
6.61k
    need_iobuffer = FALSE;
722
6.61k
    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
966
      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.7k
  case '5':                     /* it's a raw-format PGM file */
738
58.7k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
58.7k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
58.7k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
58.7k
    if (maxval > 255) {
743
4.22k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
616
        source->pub.get_pixel_rows = get_word_gray_row;
745
3.61k
      else if (IsExtRGB(cinfo->in_color_space))
746
3.08k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
531
      else if (cinfo->in_color_space == JCS_CMYK)
748
531
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
54.5k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
54.5k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
54.5k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
320
      source->pub.get_pixel_rows = get_raw_row;
755
320
      use_raw_buffer = TRUE;
756
320
      need_rescale = FALSE;
757
54.2k
    } else {
758
54.2k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
7.71k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
46.5k
      else if (IsExtRGB(cinfo->in_color_space))
761
40.1k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
6.34k
      else if (cinfo->in_color_space == JCS_CMYK)
763
6.34k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
54.2k
    }
767
58.7k
    break;
768
769
18.4k
  case '6':                     /* it's a raw-format PPM file */
770
18.4k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
18.4k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
18.4k
    if (maxval > 255) {
774
6.11k
      if (IsExtRGB(cinfo->in_color_space))
775
4.45k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
1.66k
      else if (cinfo->in_color_space == JCS_CMYK)
777
771
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
890
      else
779
890
        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.22k
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
172
      source->pub.get_pixel_rows = get_raw_row;
789
172
      use_raw_buffer = TRUE;
790
172
      need_rescale = FALSE;
791
12.1k
    } else {
792
12.1k
      if (IsExtRGB(cinfo->in_color_space))
793
8.79k
        source->pub.get_pixel_rows = get_rgb_row;
794
3.32k
      else if (cinfo->in_color_space == JCS_CMYK)
795
1.53k
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
1.79k
      else
797
1.79k
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
12.1k
    }
799
18.4k
    break;
800
99.5k
  }
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.61k
    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.5k
    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.7k
    else
815
58.7k
      source->buffer_width = (size_t)w *
816
58.7k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
74.5k
    source->iobuffer = (U_CHAR *)
818
74.5k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
74.5k
                                  source->buffer_width);
820
74.5k
  }
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
492
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
492
    source->pub._buffer = &source->pixrow;
828
492
    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
184M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
184M
      source->rescale[val] =
852
184M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
184M
                   maxval);
854
184M
    }
855
87.0k
  }
856
87.5k
}
rdppm-8.c:start_input_ppm
Line
Count
Source
665
48.2k
{
666
48.2k
  ppm_source_ptr source = (ppm_source_ptr)sinfo;
667
48.2k
  int c;
668
48.2k
  unsigned int w, h, maxval;
669
48.2k
  boolean need_iobuffer, use_raw_buffer, need_rescale;
670
671
48.2k
  if (getc(source->pub.input_file) != 'P')
672
0
    ERREXIT(cinfo, JERR_PPM_NOT);
673
674
48.2k
  c = getc(source->pub.input_file); /* subformat discriminator character */
675
676
  /* detect unsupported variants (ie, PBM) before trying to read header */
677
48.2k
  switch (c) {
678
4.19k
  case '2':                     /* it's a text-format PGM file */
679
8.55k
  case '3':                     /* it's a text-format PPM file */
680
38.4k
  case '5':                     /* it's a raw-format PGM file */
681
48.1k
  case '6':                     /* it's a raw-format PPM file */
682
48.1k
    break;
683
20
  default:
684
20
    ERREXIT(cinfo, JERR_PPM_NOT);
685
20
    break;
686
48.2k
  }
687
688
  /* fetch the remaining header info */
689
48.1k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
48.1k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
48.1k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
48.1k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
94
    ERREXIT(cinfo, JERR_PPM_NOT);
695
48.1k
  if (sinfo->max_pixels && (unsigned long long)w * h > sinfo->max_pixels)
696
687
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
697
698
48.1k
  cinfo->image_width = (JDIMENSION)w;
699
48.1k
  cinfo->image_height = (JDIMENSION)h;
700
48.1k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
48.1k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
48.1k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
48.1k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
48.1k
  switch (c) {
708
3.17k
  case '2':                     /* it's a text-format PGM file */
709
3.17k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
3.17k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
3.17k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
3.17k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
473
      source->pub.get_pixel_rows = get_text_gray_row;
715
2.70k
    else if (IsExtRGB(cinfo->in_color_space))
716
2.36k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
337
    else if (cinfo->in_color_space == JCS_CMYK)
718
337
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
3.17k
    need_iobuffer = FALSE;
722
3.17k
    break;
723
724
3.38k
  case '3':                     /* it's a text-format PPM file */
725
3.38k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
3.38k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
3.38k
    if (IsExtRGB(cinfo->in_color_space))
729
2.50k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
881
    else if (cinfo->in_color_space == JCS_CMYK)
731
380
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
501
    else
733
501
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
3.38k
    need_iobuffer = FALSE;
735
3.38k
    break;
736
737
28.7k
  case '5':                     /* it's a raw-format PGM file */
738
28.7k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
28.7k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
28.7k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
28.7k
    if (maxval > 255) {
743
1.97k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
294
        source->pub.get_pixel_rows = get_word_gray_row;
745
1.67k
      else if (IsExtRGB(cinfo->in_color_space))
746
1.47k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
209
      else if (cinfo->in_color_space == JCS_CMYK)
748
209
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
26.7k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
26.7k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
26.7k
               cinfo->in_color_space == JCS_GRAYSCALE) {
754
320
      source->pub.get_pixel_rows = get_raw_row;
755
320
      use_raw_buffer = TRUE;
756
320
      need_rescale = FALSE;
757
26.4k
    } else {
758
26.4k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
3.74k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
22.7k
      else if (IsExtRGB(cinfo->in_color_space))
761
20.3k
        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.4k
    }
767
28.7k
    break;
768
769
8.86k
  case '6':                     /* it's a raw-format PPM file */
770
8.86k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
8.86k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
8.86k
    if (maxval > 255) {
774
2.82k
      if (IsExtRGB(cinfo->in_color_space))
775
2.10k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
723
      else if (cinfo->in_color_space == JCS_CMYK)
777
302
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
421
      else
779
421
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
6.03k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
6.03k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
6.03k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
6.03k
               (cinfo->in_color_space == JCS_EXT_RGB ||
784
1.22k
                cinfo->in_color_space == JCS_RGB)) {
785
#else
786
               cinfo->in_color_space == JCS_EXT_RGB) {
787
#endif
788
172
      source->pub.get_pixel_rows = get_raw_row;
789
172
      use_raw_buffer = TRUE;
790
172
      need_rescale = FALSE;
791
5.86k
    } else {
792
5.86k
      if (IsExtRGB(cinfo->in_color_space))
793
4.32k
        source->pub.get_pixel_rows = get_rgb_row;
794
1.53k
      else if (cinfo->in_color_space == JCS_CMYK)
795
636
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
900
      else
797
900
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
5.86k
    }
799
8.86k
    break;
800
48.1k
  }
801
802
42.3k
  if (IsExtRGB(cinfo->in_color_space))
803
33.2k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
9.07k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
4.83k
    cinfo->input_components = 1;
806
4.23k
  else if (cinfo->in_color_space == JCS_CMYK)
807
4.23k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
42.3k
  if (need_iobuffer) {
811
36.2k
    if (c == '6')
812
7.54k
      source->buffer_width = (size_t)w * 3 *
813
7.54k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
28.7k
    else
815
28.7k
      source->buffer_width = (size_t)w *
816
28.7k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
36.2k
    source->iobuffer = (U_CHAR *)
818
36.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
36.2k
                                  source->buffer_width);
820
36.2k
  }
821
822
  /* Create compressor input buffer. */
823
42.3k
  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
492
    source->pixrow = (_JSAMPROW)source->iobuffer;
827
492
    source->pub._buffer = &source->pixrow;
828
492
    source->pub.buffer_height = 1;
829
41.8k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
41.8k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
41.8k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
41.8k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
41.8k
    source->pub.buffer_height = 1;
835
41.8k
  }
836
837
  /* Compute the rescaling array if required. */
838
42.3k
  if (need_rescale) {
839
41.8k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
41.8k
    source->rescale = (_JSAMPLE *)
843
41.8k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
41.8k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
41.8k
                                           sizeof(_JSAMPLE)));
846
41.8k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
41.8k
                                        sizeof(_JSAMPLE)));
848
41.8k
    half_maxval = maxval / 2;
849
50.3M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
50.3M
      source->rescale[val] =
852
50.3M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
50.3M
                   maxval);
854
50.3M
    }
855
41.8k
  }
856
42.3k
}
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.94k
  case '2':                     /* it's a text-format PGM file */
679
6.25k
  case '3':                     /* it's a text-format PPM file */
680
31.8k
  case '5':                     /* it's a raw-format PGM file */
681
39.6k
  case '6':                     /* it's a raw-format PPM file */
682
39.6k
    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.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
39.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
39.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
39.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
63
    ERREXIT(cinfo, JERR_PPM_NOT);
695
39.6k
  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.6k
  cinfo->image_width = (JDIMENSION)w;
699
39.6k
  cinfo->image_height = (JDIMENSION)h;
700
39.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
39.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
39.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
39.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
39.6k
  switch (c) {
708
2.22k
  case '2':                     /* it's a text-format PGM file */
709
2.22k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
2.22k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
2.22k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
2.22k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
318
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.90k
    else if (IsExtRGB(cinfo->in_color_space))
716
1.59k
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
318
    else if (cinfo->in_color_space == JCS_CMYK)
718
318
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
2.22k
    need_iobuffer = FALSE;
722
2.22k
    break;
723
724
2.56k
  case '3':                     /* it's a text-format PPM file */
725
2.56k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
2.56k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
2.56k
    if (IsExtRGB(cinfo->in_color_space))
729
1.83k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
734
    else if (cinfo->in_color_space == JCS_CMYK)
731
367
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
367
    else
733
367
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
2.56k
    need_iobuffer = FALSE;
735
2.56k
    break;
736
737
24.7k
  case '5':                     /* it's a raw-format PGM file */
738
24.7k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
24.7k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
24.7k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
24.7k
    if (maxval > 255) {
743
1.42k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
204
        source->pub.get_pixel_rows = get_word_gray_row;
745
1.22k
      else if (IsExtRGB(cinfo->in_color_space))
746
1.02k
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
204
      else if (cinfo->in_color_space == JCS_CMYK)
748
204
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
23.3k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
752
23.3k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
753
23.3k
               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.3k
    } else {
758
23.3k
      if (cinfo->in_color_space == JCS_GRAYSCALE)
759
3.33k
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
20.0k
      else if (IsExtRGB(cinfo->in_color_space))
761
16.6k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
3.33k
      else if (cinfo->in_color_space == JCS_CMYK)
763
3.33k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
764
0
      else
765
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
766
23.3k
    }
767
24.7k
    break;
768
769
7.30k
  case '6':                     /* it's a raw-format PPM file */
770
7.30k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
7.30k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
7.30k
    if (maxval > 255) {
774
2.11k
      if (IsExtRGB(cinfo->in_color_space))
775
1.51k
        source->pub.get_pixel_rows = get_word_rgb_row;
776
604
      else if (cinfo->in_color_space == JCS_CMYK)
777
302
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
302
      else
779
302
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
5.19k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
5.19k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
5.19k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
5.19k
               (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.19k
    } else {
792
5.19k
      if (IsExtRGB(cinfo->in_color_space))
793
3.71k
        source->pub.get_pixel_rows = get_rgb_row;
794
1.48k
      else if (cinfo->in_color_space == JCS_CMYK)
795
742
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
742
      else
797
742
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
5.19k
    }
799
7.30k
    break;
800
39.6k
  }
801
802
35.4k
  if (IsExtRGB(cinfo->in_color_space))
803
26.3k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
9.12k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
3.85k
    cinfo->input_components = 1;
806
5.26k
  else if (cinfo->in_color_space == JCS_CMYK)
807
5.26k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
35.4k
  if (need_iobuffer) {
811
31.0k
    if (c == '6')
812
6.26k
      source->buffer_width = (size_t)w * 3 *
813
6.26k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
24.7k
    else
815
24.7k
      source->buffer_width = (size_t)w *
816
24.7k
        ((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
53.0M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
52.9M
      source->rescale[val] =
852
52.9M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
52.9M
                   maxval);
854
52.9M
    }
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.47k
  case '2':                     /* it's a text-format PGM file */
679
3.45k
  case '3':                     /* it's a text-format PPM file */
680
9.20k
  case '5':                     /* it's a raw-format PGM file */
681
11.6k
  case '6':                     /* it's a raw-format PPM file */
682
11.6k
    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.6k
  w = read_pbm_integer(cinfo, source->pub.input_file, 65535);
690
11.6k
  h = read_pbm_integer(cinfo, source->pub.input_file, 65535);
691
11.6k
  maxval = read_pbm_integer(cinfo, source->pub.input_file, 65535);
692
693
11.6k
  if (w <= 0 || h <= 0 || maxval <= 0) /* error check */
694
28
    ERREXIT(cinfo, JERR_PPM_NOT);
695
11.6k
  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.6k
  cinfo->image_width = (JDIMENSION)w;
699
11.6k
  cinfo->image_height = (JDIMENSION)h;
700
11.6k
  source->maxval = maxval;
701
702
  /* initialize flags to most common settings */
703
11.6k
  need_iobuffer = TRUE;         /* do we need an I/O buffer? */
704
11.6k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
705
11.6k
  need_rescale = TRUE;          /* do we need a rescale array? */
706
707
11.6k
  switch (c) {
708
1.21k
  case '2':                     /* it's a text-format PGM file */
709
1.21k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
710
1.21k
        cinfo->in_color_space == JCS_RGB)
711
0
      cinfo->in_color_space = JCS_GRAYSCALE;
712
1.21k
    TRACEMS3(cinfo, 1, JTRC_PGM_TEXT, w, h, maxval);
713
1.21k
    if (cinfo->in_color_space == JCS_GRAYSCALE)
714
173
      source->pub.get_pixel_rows = get_text_gray_row;
715
1.03k
    else if (IsExtRGB(cinfo->in_color_space))
716
865
      source->pub.get_pixel_rows = get_text_gray_rgb_row;
717
173
    else if (cinfo->in_color_space == JCS_CMYK)
718
173
      source->pub.get_pixel_rows = get_text_gray_cmyk_row;
719
0
    else
720
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
721
1.21k
    need_iobuffer = FALSE;
722
1.21k
    break;
723
724
1.53k
  case '3':                     /* it's a text-format PPM file */
725
1.53k
    if (cinfo->in_color_space == JCS_UNKNOWN)
726
0
      cinfo->in_color_space = JCS_EXT_RGB;
727
1.53k
    TRACEMS3(cinfo, 1, JTRC_PPM_TEXT, w, h, maxval);
728
1.53k
    if (IsExtRGB(cinfo->in_color_space))
729
1.09k
      source->pub.get_pixel_rows = get_text_rgb_row;
730
438
    else if (cinfo->in_color_space == JCS_CMYK)
731
219
      source->pub.get_pixel_rows = get_text_rgb_cmyk_row;
732
219
    else
733
219
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
734
1.53k
    need_iobuffer = FALSE;
735
1.53k
    break;
736
737
5.26k
  case '5':                     /* it's a raw-format PGM file */
738
5.26k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
739
5.26k
        cinfo->in_color_space == JCS_RGB)
740
0
      cinfo->in_color_space = JCS_GRAYSCALE;
741
5.26k
    TRACEMS3(cinfo, 1, JTRC_PGM, w, h, maxval);
742
5.26k
    if (maxval > 255) {
743
826
      if (cinfo->in_color_space == JCS_GRAYSCALE)
744
118
        source->pub.get_pixel_rows = get_word_gray_row;
745
708
      else if (IsExtRGB(cinfo->in_color_space))
746
590
        source->pub.get_pixel_rows = get_word_gray_rgb_row;
747
118
      else if (cinfo->in_color_space == JCS_CMYK)
748
118
        source->pub.get_pixel_rows = get_word_gray_cmyk_row;
749
0
      else
750
0
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
751
4.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
634
        source->pub.get_pixel_rows = get_scaled_gray_row;
760
3.80k
      else if (IsExtRGB(cinfo->in_color_space))
761
3.17k
        source->pub.get_pixel_rows = get_gray_rgb_row;
762
634
      else if (cinfo->in_color_space == JCS_CMYK)
763
634
        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.26k
    break;
768
769
2.23k
  case '6':                     /* it's a raw-format PPM file */
770
2.23k
    if (cinfo->in_color_space == JCS_UNKNOWN)
771
0
      cinfo->in_color_space = JCS_EXT_RGB;
772
2.23k
    TRACEMS3(cinfo, 1, JTRC_PPM, w, h, maxval);
773
2.23k
    if (maxval > 255) {
774
1.16k
      if (IsExtRGB(cinfo->in_color_space))
775
835
        source->pub.get_pixel_rows = get_word_rgb_row;
776
334
      else if (cinfo->in_color_space == JCS_CMYK)
777
167
        source->pub.get_pixel_rows = get_word_rgb_cmyk_row;
778
167
      else
779
167
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
780
1.16k
    } else if (maxval <= _MAXJSAMPLE && sizeof(_JSAMPLE) == sizeof(U_CHAR) &&
781
1.06k
               maxval == ((1U << cinfo->data_precision) - 1U) &&
782
1.06k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
783
1.06k
               (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.06k
    } else {
792
1.06k
      if (IsExtRGB(cinfo->in_color_space))
793
760
        source->pub.get_pixel_rows = get_rgb_row;
794
304
      else if (cinfo->in_color_space == JCS_CMYK)
795
152
        source->pub.get_pixel_rows = get_rgb_cmyk_row;
796
152
      else
797
152
        ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
798
1.06k
    }
799
2.23k
    break;
800
11.6k
  }
801
802
9.70k
  if (IsExtRGB(cinfo->in_color_space))
803
7.31k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
804
2.38k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
805
925
    cinfo->input_components = 1;
806
1.46k
  else if (cinfo->in_color_space == JCS_CMYK)
807
1.46k
    cinfo->input_components = 4;
808
809
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
810
9.70k
  if (need_iobuffer) {
811
7.17k
    if (c == '6')
812
1.91k
      source->buffer_width = (size_t)w * 3 *
813
1.91k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
814
5.26k
    else
815
5.26k
      source->buffer_width = (size_t)w *
816
5.26k
        ((maxval <= 255) ? sizeof(U_CHAR) : (2 * sizeof(U_CHAR)));
817
7.17k
    source->iobuffer = (U_CHAR *)
818
7.17k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
819
7.17k
                                  source->buffer_width);
820
7.17k
  }
821
822
  /* Create compressor input buffer. */
823
9.70k
  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.70k
  } else {
830
    /* Need to translate anyway, so make a separate sample buffer. */
831
9.70k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
832
9.70k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
833
9.70k
       (JDIMENSION)w * cinfo->input_components, (JDIMENSION)1);
834
9.70k
    source->pub.buffer_height = 1;
835
9.70k
  }
836
837
  /* Compute the rescaling array if required. */
838
9.70k
  if (need_rescale) {
839
9.70k
    long val, half_maxval;
840
841
    /* On 16-bit-int machines we have to be careful of maxval = 65535 */
842
9.70k
    source->rescale = (_JSAMPLE *)
843
9.70k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
844
9.70k
                                  (size_t)(((long)MAX(maxval, 255) + 1L) *
845
9.70k
                                           sizeof(_JSAMPLE)));
846
9.70k
    memset(source->rescale, 0, (size_t)(((long)MAX(maxval, 255) + 1L) *
847
9.70k
                                        sizeof(_JSAMPLE)));
848
9.70k
    half_maxval = maxval / 2;
849
80.9M
    for (val = 0; val <= (long)maxval; val++) {
850
      /* The multiplication here must be done in 32 bits to avoid overflow */
851
80.9M
      source->rescale[val] =
852
80.9M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
853
80.9M
                   maxval);
854
80.9M
    }
855
9.70k
  }
856
9.70k
}
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.6k
{
866
  /* no work */
867
59.6k
}
rdppm-8.c:finish_input_ppm
Line
Count
Source
865
28.2k
{
866
  /* no work */
867
28.2k
}
rdppm-12.c:finish_input_ppm
Line
Count
Source
865
26.6k
{
866
  /* no work */
867
26.6k
}
rdppm-16.c:finish_input_ppm
Line
Count
Source
865
4.65k
{
866
  /* no work */
867
4.65k
}
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.2k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
881
#else
882
51.4k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
883
51.4k
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
884
0
#endif
885
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
886
887
  /* Create module interface object */
888
99.6k
  source = (ppm_source_ptr)
889
99.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
99.6k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
99.6k
  source->pub.start_input = start_input_ppm;
893
99.6k
  source->pub.finish_input = finish_input_ppm;
894
99.6k
  source->pub.max_pixels = 0;
895
896
99.6k
  return (cjpeg_source_ptr)source;
897
99.6k
}
jinit_read_ppm
Line
Count
Source
876
48.2k
{
877
48.2k
  ppm_source_ptr source;
878
879
48.2k
#if BITS_IN_JSAMPLE == 8
880
48.2k
  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.2k
  source = (ppm_source_ptr)
889
48.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
890
48.2k
                                sizeof(ppm_source_struct));
891
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
892
48.2k
  source->pub.start_input = start_input_ppm;
893
48.2k
  source->pub.finish_input = finish_input_ppm;
894
48.2k
  source->pub.max_pixels = 0;
895
896
48.2k
  return (cjpeg_source_ptr)source;
897
48.2k
}
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)) */