Coverage Report

Created: 2026-06-10 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjpeg-turbo.main/src/rdpng.c
Line
Count
Source
1
/*
2
 * rdpng.c
3
 *
4
 * This file was part of the Independent JPEG Group's software:
5
 * Copyright (C) 1991-1997, Thomas G. Lane.
6
 * libjpeg-turbo Modifications:
7
 * Copyright (C) 2015-2017, 2020-2026, D. R. Commander.
8
 * For conditions of distribution and use, see the accompanying README.ijg
9
 * file.
10
 *
11
 * This file contains routines to read input images in 8-bit-per-channel or
12
 * 16-bit-per-channel PNG format.  libspng is required to compile this
13
 * software.
14
 */
15
16
#ifdef _MSC_VER
17
#define _CRT_SECURE_NO_DEPRECATE
18
#endif
19
20
#include "cmyk.h"
21
#include "cdjpeg.h"             /* Common decls for cjpeg/djpeg applications */
22
#include "spng/spng.h"
23
24
#if defined(PNG_SUPPORTED) && \
25
    (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED))
26
27
28
static int alpha_index[JPEG_NUMCS] = {
29
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
30
};
31
32
33
74.6k
#define TRY_SPNG(f) { \
34
74.6k
  int __spng_error = (f); \
35
74.6k
  if (__spng_error) \
36
74.6k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(__spng_error)); \
37
74.6k
}
38
39
40
/* Private version of data source object */
41
42
typedef struct {
43
  struct cjpeg_source_struct pub; /* public fields */
44
45
  spng_ctx *ctx;
46
  uint8_t png_bit_depth, png_color_type;
47
  int png_alpha;
48
  struct spng_plte colormap;    /* PNG colormap */
49
50
  /* Usually these two pointers point to the same place: */
51
  unsigned char *iobuffer;      /* libspng's I/O buffer */
52
  _JSAMPROW pixrow;             /* compressor input buffer */
53
  size_t buffer_width;          /* width of I/O buffer */
54
  _JSAMPLE *rescale;            /* PNG bit depth => data precision remapping
55
                                   array, or NULL */
56
} png_source_struct;
57
58
typedef png_source_struct *png_source_ptr;
59
60
61
/*
62
 * Read one row of pixels.
63
 *
64
 * We provide several different versions depending on input file format.
65
 * In all cases, input is scaled to cinfo->data_precision.
66
 *
67
 * get_raw_row() is used when the pixel format is JCS_EXT_RGB/JCS_RGB or
68
 * JCS_GRAYSCALE, cinfo->data_precision is 8 or 16, and the PNG file doesn't
69
 * have an alpha channel.  Thus, we can read the pixels directly from the PNG
70
 * file.
71
 */
72
73
METHODDEF(JDIMENSION)
74
get_raw_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
75
89.4k
{
76
89.4k
  png_source_ptr source = (png_source_ptr)sinfo;
77
89.4k
  int spng_error;
78
79
89.4k
  spng_error = spng_decode_row(source->ctx, source->iobuffer,
80
89.4k
                               source->buffer_width);
81
89.4k
  if (spng_error && spng_error != SPNG_EOI)
82
2.56k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(spng_error)); \
83
89.4k
  return 1;
84
89.4k
}
rdpng-8.c:get_raw_row
Line
Count
Source
75
89.4k
{
76
89.4k
  png_source_ptr source = (png_source_ptr)sinfo;
77
89.4k
  int spng_error;
78
79
89.4k
  spng_error = spng_decode_row(source->ctx, source->iobuffer,
80
89.4k
                               source->buffer_width);
81
89.4k
  if (spng_error && spng_error != SPNG_EOI)
82
2.56k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(spng_error)); \
83
89.4k
  return 1;
84
89.4k
}
Unexecuted instantiation: rdpng-12.c:get_raw_row
Unexecuted instantiation: rdpng-16.c:get_raw_row
85
86
87
13.0k
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op, pointer_op) { \
88
992k
  for (col = cinfo->image_width; col > 0; col--) { \
89
979k
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
90
979k
    alpha_set_op \
91
979k
    ptr += ps; \
92
979k
    pointer_op \
93
979k
  } \
94
13.0k
}
95
96
97
METHODDEF(JDIMENSION)
98
get_gray_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
99
/* This version is for reading 8-bit-per-channel or 16-bit-per-channel
100
 * grayscale or grayscale + alpha PNG files.
101
 */
102
1.27k
{
103
1.27k
  png_source_ptr source = (png_source_ptr)sinfo;
104
1.27k
  register _JSAMPROW ptr;
105
1.27k
  register _JSAMPLE *rescale = source->rescale;
106
1.27k
  JDIMENSION col;
107
108
1.27k
  get_raw_row(cinfo, sinfo);
109
1.27k
  ptr = source->pub._buffer[0];
110
#if BITS_IN_JSAMPLE != 12
111
1.27k
  if (source->png_bit_depth == cinfo->data_precision) {
112
236
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
113
1.87k
    for (col = cinfo->image_width; col > 0; col--) {
114
1.63k
      *ptr++ = *bufferptr++;
115
1.63k
      bufferptr += source->png_alpha;
116
1.63k
    }
117
236
  } else
118
1.03k
#endif
119
1.03k
  if (source->png_bit_depth == 16) {
120
969
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
121
79.7k
    for (col = cinfo->image_width; col > 0; col--) {
122
78.7k
      *ptr++ = rescale[*bufferptr++];
123
78.7k
      bufferptr += source->png_alpha;
124
78.7k
    }
125
969
  } else {
126
65
    register unsigned char *bufferptr = source->iobuffer;
127
65
    for (col = cinfo->image_width; col > 0; col--) {
128
0
      *ptr++ = rescale[*bufferptr++];
129
0
      bufferptr += source->png_alpha;
130
0
    }
131
65
  }
132
1.27k
  return 1;
133
1.27k
}
rdpng-8.c:get_gray_row
Line
Count
Source
102
1.27k
{
103
1.27k
  png_source_ptr source = (png_source_ptr)sinfo;
104
1.27k
  register _JSAMPROW ptr;
105
1.27k
  register _JSAMPLE *rescale = source->rescale;
106
1.27k
  JDIMENSION col;
107
108
1.27k
  get_raw_row(cinfo, sinfo);
109
1.27k
  ptr = source->pub._buffer[0];
110
1.27k
#if BITS_IN_JSAMPLE != 12
111
1.27k
  if (source->png_bit_depth == cinfo->data_precision) {
112
236
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
113
1.87k
    for (col = cinfo->image_width; col > 0; col--) {
114
1.63k
      *ptr++ = *bufferptr++;
115
1.63k
      bufferptr += source->png_alpha;
116
1.63k
    }
117
236
  } else
118
1.03k
#endif
119
1.03k
  if (source->png_bit_depth == 16) {
120
969
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
121
79.7k
    for (col = cinfo->image_width; col > 0; col--) {
122
78.7k
      *ptr++ = rescale[*bufferptr++];
123
78.7k
      bufferptr += source->png_alpha;
124
78.7k
    }
125
969
  } else {
126
65
    register unsigned char *bufferptr = source->iobuffer;
127
65
    for (col = cinfo->image_width; col > 0; col--) {
128
0
      *ptr++ = rescale[*bufferptr++];
129
0
      bufferptr += source->png_alpha;
130
0
    }
131
65
  }
132
1.27k
  return 1;
133
1.27k
}
Unexecuted instantiation: rdpng-12.c:get_gray_row
Unexecuted instantiation: rdpng-16.c:get_gray_row
134
135
136
METHODDEF(JDIMENSION)
137
get_gray_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
138
/* This version is for reading 8-bit-per-channel or 16-bit-per-channel
139
 * grayscale or grayscale + alpha PNG files and converting to extended RGB.
140
 */
141
13.0k
{
142
13.0k
  png_source_ptr source = (png_source_ptr)sinfo;
143
13.0k
  register _JSAMPROW ptr;
144
13.0k
  register _JSAMPLE *rescale = source->rescale;
145
13.0k
  JDIMENSION col;
146
13.0k
  register int rindex = rgb_red[cinfo->in_color_space];
147
13.0k
  register int gindex = rgb_green[cinfo->in_color_space];
148
13.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
149
13.0k
  register int aindex = alpha_index[cinfo->in_color_space];
150
13.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
151
152
13.0k
  get_raw_row(cinfo, sinfo);
153
13.0k
  ptr = source->pub._buffer[0];
154
#if BITS_IN_JSAMPLE != 12
155
13.0k
  if (source->png_bit_depth == cinfo->data_precision) {
156
7.74k
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
157
7.74k
    if (aindex >= 0)
158
1.54k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
159
7.74k
                         bufferptr += source->png_alpha;)
160
6.19k
    else
161
6.19k
      GRAY_RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
162
7.74k
  } else
163
5.34k
#endif
164
5.34k
  if (source->png_bit_depth == 16) {
165
4.84k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
166
4.84k
    if (aindex >= 0)
167
969
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
168
4.84k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
169
4.84k
                         bufferptr += source->png_alpha;)
170
3.87k
    else
171
3.87k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
172
4.84k
                         bufferptr += source->png_alpha;)
173
4.84k
  } else {
174
495
    register unsigned char *bufferptr = source->iobuffer;
175
495
    if (aindex >= 0)
176
0
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
177
495
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
178
495
                         bufferptr += source->png_alpha;)
179
495
    else
180
495
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
181
495
                         bufferptr += source->png_alpha;)
182
495
  }
183
13.0k
  return 1;
184
13.0k
}
rdpng-8.c:get_gray_rgb_row
Line
Count
Source
141
13.0k
{
142
13.0k
  png_source_ptr source = (png_source_ptr)sinfo;
143
13.0k
  register _JSAMPROW ptr;
144
13.0k
  register _JSAMPLE *rescale = source->rescale;
145
13.0k
  JDIMENSION col;
146
13.0k
  register int rindex = rgb_red[cinfo->in_color_space];
147
13.0k
  register int gindex = rgb_green[cinfo->in_color_space];
148
13.0k
  register int bindex = rgb_blue[cinfo->in_color_space];
149
13.0k
  register int aindex = alpha_index[cinfo->in_color_space];
150
13.0k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
151
152
13.0k
  get_raw_row(cinfo, sinfo);
153
13.0k
  ptr = source->pub._buffer[0];
154
13.0k
#if BITS_IN_JSAMPLE != 12
155
13.0k
  if (source->png_bit_depth == cinfo->data_precision) {
156
7.74k
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
157
7.74k
    if (aindex >= 0)
158
1.54k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
159
7.74k
                         bufferptr += source->png_alpha;)
160
6.19k
    else
161
6.19k
      GRAY_RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
162
7.74k
  } else
163
5.34k
#endif
164
5.34k
  if (source->png_bit_depth == 16) {
165
4.84k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
166
4.84k
    if (aindex >= 0)
167
969
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
168
4.84k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
169
4.84k
                         bufferptr += source->png_alpha;)
170
3.87k
    else
171
3.87k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
172
4.84k
                         bufferptr += source->png_alpha;)
173
4.84k
  } else {
174
495
    register unsigned char *bufferptr = source->iobuffer;
175
495
    if (aindex >= 0)
176
0
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
177
495
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
178
495
                         bufferptr += source->png_alpha;)
179
495
    else
180
495
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
181
495
                         bufferptr += source->png_alpha;)
182
495
  }
183
13.0k
  return 1;
184
13.0k
}
Unexecuted instantiation: rdpng-12.c:get_gray_rgb_row
Unexecuted instantiation: rdpng-16.c:get_gray_rgb_row
185
186
187
METHODDEF(JDIMENSION)
188
get_gray_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
189
/* This version is for reading 8-bit-per-channel or 16-bit-per-channel
190
 * grayscale or grayscale + alpha PNG files and converting to CMYK.
191
 */
192
2.61k
{
193
2.61k
  png_source_ptr source = (png_source_ptr)sinfo;
194
2.61k
  register _JSAMPROW ptr;
195
2.61k
  register _JSAMPLE *rescale = source->rescale;
196
2.61k
  JDIMENSION col;
197
198
2.61k
  get_raw_row(cinfo, sinfo);
199
2.61k
  ptr = source->pub._buffer[0];
200
#if BITS_IN_JSAMPLE != 12
201
2.61k
  if (source->png_bit_depth == cinfo->data_precision) {
202
1.54k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
203
118k
    for (col = cinfo->image_width; col > 0; col--) {
204
117k
      _JSAMPLE gray = *bufferptr++;
205
117k
      bufferptr += source->png_alpha;
206
117k
      rgb_to_cmyk(_MAXJSAMPLE, gray, gray, gray, ptr, ptr + 1, ptr + 2,
207
117k
                  ptr + 3);
208
117k
      ptr += 4;
209
117k
    }
210
1.54k
  } else
211
1.06k
#endif
212
1.06k
  if (source->png_bit_depth == 16) {
213
969
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
214
79.7k
    for (col = cinfo->image_width; col > 0; col--) {
215
78.7k
      _JSAMPLE gray = rescale[*bufferptr++];
216
78.7k
      bufferptr += source->png_alpha;
217
78.7k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
218
78.7k
                  ptr + 1, ptr + 2, ptr + 3);
219
78.7k
      ptr += 4;
220
78.7k
    }
221
969
  } else {
222
99
    register unsigned char *bufferptr = source->iobuffer;
223
99
    for (col = cinfo->image_width; col > 0; col--) {
224
0
      _JSAMPLE gray = rescale[*bufferptr++];
225
0
      bufferptr += source->png_alpha;
226
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
227
0
                  ptr + 1, ptr + 2, ptr + 3);
228
0
      ptr += 4;
229
0
    }
230
99
  }
231
2.61k
  return 1;
232
2.61k
}
rdpng-8.c:get_gray_cmyk_row
Line
Count
Source
192
2.61k
{
193
2.61k
  png_source_ptr source = (png_source_ptr)sinfo;
194
2.61k
  register _JSAMPROW ptr;
195
2.61k
  register _JSAMPLE *rescale = source->rescale;
196
2.61k
  JDIMENSION col;
197
198
2.61k
  get_raw_row(cinfo, sinfo);
199
2.61k
  ptr = source->pub._buffer[0];
200
2.61k
#if BITS_IN_JSAMPLE != 12
201
2.61k
  if (source->png_bit_depth == cinfo->data_precision) {
202
1.54k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
203
118k
    for (col = cinfo->image_width; col > 0; col--) {
204
117k
      _JSAMPLE gray = *bufferptr++;
205
117k
      bufferptr += source->png_alpha;
206
117k
      rgb_to_cmyk(_MAXJSAMPLE, gray, gray, gray, ptr, ptr + 1, ptr + 2,
207
117k
                  ptr + 3);
208
117k
      ptr += 4;
209
117k
    }
210
1.54k
  } else
211
1.06k
#endif
212
1.06k
  if (source->png_bit_depth == 16) {
213
969
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
214
79.7k
    for (col = cinfo->image_width; col > 0; col--) {
215
78.7k
      _JSAMPLE gray = rescale[*bufferptr++];
216
78.7k
      bufferptr += source->png_alpha;
217
78.7k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
218
78.7k
                  ptr + 1, ptr + 2, ptr + 3);
219
78.7k
      ptr += 4;
220
78.7k
    }
221
969
  } else {
222
99
    register unsigned char *bufferptr = source->iobuffer;
223
99
    for (col = cinfo->image_width; col > 0; col--) {
224
0
      _JSAMPLE gray = rescale[*bufferptr++];
225
0
      bufferptr += source->png_alpha;
226
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
227
0
                  ptr + 1, ptr + 2, ptr + 3);
228
0
      ptr += 4;
229
0
    }
230
99
  }
231
2.61k
  return 1;
232
2.61k
}
Unexecuted instantiation: rdpng-12.c:get_gray_cmyk_row
Unexecuted instantiation: rdpng-16.c:get_gray_cmyk_row
233
234
235
54.3k
#define RGB_READ_LOOP(read_op, alpha_set_op, pointer_op) { \
236
15.8M
  for (col = cinfo->image_width; col > 0; col--) { \
237
15.7M
    ptr[rindex] = read_op; \
238
15.7M
    ptr[gindex] = read_op; \
239
15.7M
    ptr[bindex] = read_op; \
240
15.7M
    alpha_set_op \
241
15.7M
    pointer_op \
242
15.7M
    ptr += ps; \
243
15.7M
  } \
244
54.3k
}
245
246
247
METHODDEF(JDIMENSION)
248
get_rgb_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
249
/* This version is for reading 8-bit-per-channel or 16-bit-per-channel
250
 * truecolor or truecolor + alpha PNG files and converting to extended RGB.
251
 */
252
54.3k
{
253
54.3k
  png_source_ptr source = (png_source_ptr)sinfo;
254
54.3k
  register _JSAMPROW ptr;
255
54.3k
  register _JSAMPLE *rescale = source->rescale;
256
54.3k
  JDIMENSION col;
257
54.3k
  register int rindex = rgb_red[cinfo->in_color_space];
258
54.3k
  register int gindex = rgb_green[cinfo->in_color_space];
259
54.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
260
54.3k
  register int aindex = alpha_index[cinfo->in_color_space];
261
54.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
54.3k
  get_raw_row(cinfo, sinfo);
264
54.3k
  ptr = source->pub._buffer[0];
265
#if BITS_IN_JSAMPLE != 12
266
54.3k
  if (source->png_bit_depth == cinfo->data_precision) {
267
27.7k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
268
27.7k
    if (aindex >= 0)
269
6.40k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
270
27.7k
                    bufferptr += source->png_alpha;)
271
21.3k
    else
272
21.3k
      RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
273
27.7k
  } else
274
26.5k
#endif
275
26.5k
  if (source->png_bit_depth == 16) {
276
25.2k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
277
25.2k
    if (aindex >= 0)
278
5.04k
      RGB_READ_LOOP(rescale[*bufferptr++],
279
25.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
280
25.2k
                    bufferptr += source->png_alpha;)
281
20.1k
    else
282
20.1k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
283
25.2k
                    bufferptr += source->png_alpha;)
284
25.2k
  } else {
285
1.36k
    register unsigned char *bufferptr = source->iobuffer;
286
1.36k
    if (aindex >= 0)
287
0
      RGB_READ_LOOP(rescale[*bufferptr++],
288
1.36k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
289
1.36k
                    bufferptr += source->png_alpha;)
290
1.36k
    else
291
1.36k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
292
1.36k
                    bufferptr += source->png_alpha;)
293
1.36k
  }
294
54.3k
  return 1;
295
54.3k
}
rdpng-8.c:get_rgb_row
Line
Count
Source
252
54.3k
{
253
54.3k
  png_source_ptr source = (png_source_ptr)sinfo;
254
54.3k
  register _JSAMPROW ptr;
255
54.3k
  register _JSAMPLE *rescale = source->rescale;
256
54.3k
  JDIMENSION col;
257
54.3k
  register int rindex = rgb_red[cinfo->in_color_space];
258
54.3k
  register int gindex = rgb_green[cinfo->in_color_space];
259
54.3k
  register int bindex = rgb_blue[cinfo->in_color_space];
260
54.3k
  register int aindex = alpha_index[cinfo->in_color_space];
261
54.3k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
54.3k
  get_raw_row(cinfo, sinfo);
264
54.3k
  ptr = source->pub._buffer[0];
265
54.3k
#if BITS_IN_JSAMPLE != 12
266
54.3k
  if (source->png_bit_depth == cinfo->data_precision) {
267
27.7k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
268
27.7k
    if (aindex >= 0)
269
6.40k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
270
27.7k
                    bufferptr += source->png_alpha;)
271
21.3k
    else
272
21.3k
      RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
273
27.7k
  } else
274
26.5k
#endif
275
26.5k
  if (source->png_bit_depth == 16) {
276
25.2k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
277
25.2k
    if (aindex >= 0)
278
5.04k
      RGB_READ_LOOP(rescale[*bufferptr++],
279
25.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
280
25.2k
                    bufferptr += source->png_alpha;)
281
20.1k
    else
282
20.1k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
283
25.2k
                    bufferptr += source->png_alpha;)
284
25.2k
  } else {
285
1.36k
    register unsigned char *bufferptr = source->iobuffer;
286
1.36k
    if (aindex >= 0)
287
0
      RGB_READ_LOOP(rescale[*bufferptr++],
288
1.36k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
289
1.36k
                    bufferptr += source->png_alpha;)
290
1.36k
    else
291
1.36k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
292
1.36k
                    bufferptr += source->png_alpha;)
293
1.36k
  }
294
54.3k
  return 1;
295
54.3k
}
Unexecuted instantiation: rdpng-12.c:get_rgb_row
Unexecuted instantiation: rdpng-16.c:get_rgb_row
296
297
298
METHODDEF(JDIMENSION)
299
get_rgb_cmyk_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
300
/* This version is for reading 8-bit-per-channel or 16-bit-per-channel
301
 * truecolor or truecolor + alpha PNG files and converting to CMYK.
302
 */
303
11.7k
{
304
11.7k
  png_source_ptr source = (png_source_ptr)sinfo;
305
11.7k
  register _JSAMPROW ptr;
306
11.7k
  register _JSAMPLE *rescale = source->rescale;
307
11.7k
  JDIMENSION col;
308
309
11.7k
  get_raw_row(cinfo, sinfo);
310
11.7k
  ptr = source->pub._buffer[0];
311
#if BITS_IN_JSAMPLE != 12
312
11.7k
  if (source->png_bit_depth == cinfo->data_precision) {
313
6.40k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
314
3.25M
    for (col = cinfo->image_width; col > 0; col--) {
315
3.24M
      _JSAMPLE r = *bufferptr++;
316
3.24M
      _JSAMPLE g = *bufferptr++;
317
3.24M
      _JSAMPLE b = *bufferptr++;
318
3.24M
      bufferptr += source->png_alpha;
319
3.24M
      rgb_to_cmyk(_MAXJSAMPLE, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
320
3.24M
      ptr += 4;
321
3.24M
    }
322
6.40k
  } else
323
5.34k
#endif
324
5.34k
  if (source->png_bit_depth == 16) {
325
5.04k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
326
403k
    for (col = cinfo->image_width; col > 0; col--) {
327
398k
      _JSAMPLE r = rescale[*bufferptr++];
328
398k
      _JSAMPLE g = rescale[*bufferptr++];
329
398k
      _JSAMPLE b = rescale[*bufferptr++];
330
398k
      bufferptr += source->png_alpha;
331
398k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
332
398k
                  ptr + 2, ptr + 3);
333
398k
      ptr += 4;
334
398k
    }
335
5.04k
  } else {
336
296
    register unsigned char *bufferptr = source->iobuffer;
337
296
    for (col = cinfo->image_width; col > 0; col--) {
338
0
      _JSAMPLE r = rescale[*bufferptr++];
339
0
      _JSAMPLE g = rescale[*bufferptr++];
340
0
      _JSAMPLE b = rescale[*bufferptr++];
341
0
      bufferptr += source->png_alpha;
342
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
343
0
                  ptr + 2, ptr + 3);
344
0
      ptr += 4;
345
0
    }
346
296
  }
347
11.7k
  return 1;
348
11.7k
}
rdpng-8.c:get_rgb_cmyk_row
Line
Count
Source
303
11.7k
{
304
11.7k
  png_source_ptr source = (png_source_ptr)sinfo;
305
11.7k
  register _JSAMPROW ptr;
306
11.7k
  register _JSAMPLE *rescale = source->rescale;
307
11.7k
  JDIMENSION col;
308
309
11.7k
  get_raw_row(cinfo, sinfo);
310
11.7k
  ptr = source->pub._buffer[0];
311
11.7k
#if BITS_IN_JSAMPLE != 12
312
11.7k
  if (source->png_bit_depth == cinfo->data_precision) {
313
6.40k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
314
3.25M
    for (col = cinfo->image_width; col > 0; col--) {
315
3.24M
      _JSAMPLE r = *bufferptr++;
316
3.24M
      _JSAMPLE g = *bufferptr++;
317
3.24M
      _JSAMPLE b = *bufferptr++;
318
3.24M
      bufferptr += source->png_alpha;
319
3.24M
      rgb_to_cmyk(_MAXJSAMPLE, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
320
3.24M
      ptr += 4;
321
3.24M
    }
322
6.40k
  } else
323
5.34k
#endif
324
5.34k
  if (source->png_bit_depth == 16) {
325
5.04k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
326
403k
    for (col = cinfo->image_width; col > 0; col--) {
327
398k
      _JSAMPLE r = rescale[*bufferptr++];
328
398k
      _JSAMPLE g = rescale[*bufferptr++];
329
398k
      _JSAMPLE b = rescale[*bufferptr++];
330
398k
      bufferptr += source->png_alpha;
331
398k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
332
398k
                  ptr + 2, ptr + 3);
333
398k
      ptr += 4;
334
398k
    }
335
5.04k
  } else {
336
296
    register unsigned char *bufferptr = source->iobuffer;
337
296
    for (col = cinfo->image_width; col > 0; col--) {
338
0
      _JSAMPLE r = rescale[*bufferptr++];
339
0
      _JSAMPLE g = rescale[*bufferptr++];
340
0
      _JSAMPLE b = rescale[*bufferptr++];
341
0
      bufferptr += source->png_alpha;
342
0
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
343
0
                  ptr + 2, ptr + 3);
344
0
      ptr += 4;
345
0
    }
346
296
  }
347
11.7k
  return 1;
348
11.7k
}
Unexecuted instantiation: rdpng-12.c:get_rgb_cmyk_row
Unexecuted instantiation: rdpng-16.c:get_rgb_cmyk_row
349
350
351
METHODDEF(JDIMENSION)
352
get_indexed_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
353
671
{
354
/* This version is for reading 8-bit-per-channel indexed-color PNG files and
355
 * converting to extended RGB or CMYK.
356
 */
357
671
  png_source_ptr source = (png_source_ptr)sinfo;
358
671
  register _JSAMPROW ptr;
359
671
  register JSAMPLE *bufferptr = (JSAMPLE *)source->iobuffer;
360
671
  register _JSAMPLE *rescale = source->rescale;
361
671
  JDIMENSION col;
362
363
671
  get_raw_row(cinfo, sinfo);
364
671
  ptr = source->pub._buffer[0];
365
#if BITS_IN_JSAMPLE == 8
366
671
  if (source->png_bit_depth == cinfo->data_precision) {
367
579
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
368
437
      for (col = cinfo->image_width; col > 0; col--) {
369
386
        JSAMPLE index = *bufferptr++;
370
371
386
        if (index >= source->colormap.n_entries)
372
6
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
373
386
        *ptr++ = source->colormap.entries[index].red;
374
386
      }
375
528
    } else if (cinfo->in_color_space == JCS_CMYK) {
376
764
      for (col = cinfo->image_width; col > 0; col--) {
377
676
        JSAMPLE index = *bufferptr++;
378
379
676
        if (index >= source->colormap.n_entries)
380
8
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
381
676
        rgb_to_cmyk(_MAXJSAMPLE, source->colormap.entries[index].red,
382
676
                    source->colormap.entries[index].green,
383
676
                    source->colormap.entries[index].blue, ptr, ptr + 1,
384
676
                    ptr + 2, ptr + 3);
385
676
        ptr += 4;
386
676
      }
387
440
    } else {
388
440
      register int rindex = rgb_red[cinfo->in_color_space];
389
440
      register int gindex = rgb_green[cinfo->in_color_space];
390
440
      register int bindex = rgb_blue[cinfo->in_color_space];
391
440
      register int aindex = alpha_index[cinfo->in_color_space];
392
440
      register int ps = rgb_pixelsize[cinfo->in_color_space];
393
394
3.82k
      for (col = cinfo->image_width; col > 0; col--) {
395
3.38k
        JSAMPLE index = *bufferptr++;
396
397
3.38k
        if (index >= source->colormap.n_entries)
398
40
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
399
3.38k
        ptr[rindex] = source->colormap.entries[index].red;
400
3.38k
        ptr[gindex] = source->colormap.entries[index].green;
401
3.38k
        ptr[bindex] = source->colormap.entries[index].blue;
402
3.38k
        if (aindex >= 0)
403
668
          ptr[aindex] = _MAXJSAMPLE;
404
3.38k
        ptr += ps;
405
3.38k
      }
406
440
    }
407
579
  } else
408
92
#endif
409
92
  {
410
92
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
411
0
      for (col = cinfo->image_width; col > 0; col--) {
412
0
        JSAMPLE index = *bufferptr++;
413
414
0
        if (index >= source->colormap.n_entries)
415
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
416
0
        *ptr++ = rescale[source->colormap.entries[index].red];
417
0
      }
418
92
    } else if (cinfo->in_color_space == JCS_CMYK) {
419
0
      for (col = cinfo->image_width; col > 0; col--) {
420
0
        JSAMPLE index = *bufferptr++;
421
422
0
        if (index >= source->colormap.n_entries)
423
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
424
0
        rgb_to_cmyk((1 << cinfo->data_precision) - 1,
425
0
                    rescale[source->colormap.entries[index].red],
426
0
                    rescale[source->colormap.entries[index].green],
427
0
                    rescale[source->colormap.entries[index].blue], ptr,
428
0
                    ptr + 1, ptr + 2, ptr + 3);
429
0
        ptr += 4;
430
0
      }
431
92
    } else {
432
92
      register int rindex = rgb_red[cinfo->in_color_space];
433
92
      register int gindex = rgb_green[cinfo->in_color_space];
434
92
      register int bindex = rgb_blue[cinfo->in_color_space];
435
92
      register int aindex = alpha_index[cinfo->in_color_space];
436
92
      register int ps = rgb_pixelsize[cinfo->in_color_space];
437
438
92
      for (col = cinfo->image_width; col > 0; col--) {
439
0
        JSAMPLE index = *bufferptr++;
440
441
0
        if (index >= source->colormap.n_entries)
442
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
443
0
        ptr[rindex] = rescale[source->colormap.entries[index].red];
444
0
        ptr[gindex] = rescale[source->colormap.entries[index].green];
445
0
        ptr[bindex] = rescale[source->colormap.entries[index].blue];
446
0
        if (aindex >= 0)
447
0
          ptr[aindex] = (1 << cinfo->data_precision) - 1;
448
0
        ptr += ps;
449
0
      }
450
92
    }
451
92
  }
452
671
  return 1;
453
671
}
rdpng-8.c:get_indexed_row
Line
Count
Source
353
671
{
354
/* This version is for reading 8-bit-per-channel indexed-color PNG files and
355
 * converting to extended RGB or CMYK.
356
 */
357
671
  png_source_ptr source = (png_source_ptr)sinfo;
358
671
  register _JSAMPROW ptr;
359
671
  register JSAMPLE *bufferptr = (JSAMPLE *)source->iobuffer;
360
671
  register _JSAMPLE *rescale = source->rescale;
361
671
  JDIMENSION col;
362
363
671
  get_raw_row(cinfo, sinfo);
364
671
  ptr = source->pub._buffer[0];
365
671
#if BITS_IN_JSAMPLE == 8
366
671
  if (source->png_bit_depth == cinfo->data_precision) {
367
579
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
368
437
      for (col = cinfo->image_width; col > 0; col--) {
369
386
        JSAMPLE index = *bufferptr++;
370
371
386
        if (index >= source->colormap.n_entries)
372
6
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
373
386
        *ptr++ = source->colormap.entries[index].red;
374
386
      }
375
528
    } else if (cinfo->in_color_space == JCS_CMYK) {
376
764
      for (col = cinfo->image_width; col > 0; col--) {
377
676
        JSAMPLE index = *bufferptr++;
378
379
676
        if (index >= source->colormap.n_entries)
380
8
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
381
676
        rgb_to_cmyk(_MAXJSAMPLE, source->colormap.entries[index].red,
382
676
                    source->colormap.entries[index].green,
383
676
                    source->colormap.entries[index].blue, ptr, ptr + 1,
384
676
                    ptr + 2, ptr + 3);
385
676
        ptr += 4;
386
676
      }
387
440
    } else {
388
440
      register int rindex = rgb_red[cinfo->in_color_space];
389
440
      register int gindex = rgb_green[cinfo->in_color_space];
390
440
      register int bindex = rgb_blue[cinfo->in_color_space];
391
440
      register int aindex = alpha_index[cinfo->in_color_space];
392
440
      register int ps = rgb_pixelsize[cinfo->in_color_space];
393
394
3.82k
      for (col = cinfo->image_width; col > 0; col--) {
395
3.38k
        JSAMPLE index = *bufferptr++;
396
397
3.38k
        if (index >= source->colormap.n_entries)
398
40
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
399
3.38k
        ptr[rindex] = source->colormap.entries[index].red;
400
3.38k
        ptr[gindex] = source->colormap.entries[index].green;
401
3.38k
        ptr[bindex] = source->colormap.entries[index].blue;
402
3.38k
        if (aindex >= 0)
403
668
          ptr[aindex] = _MAXJSAMPLE;
404
3.38k
        ptr += ps;
405
3.38k
      }
406
440
    }
407
579
  } else
408
92
#endif
409
92
  {
410
92
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
411
0
      for (col = cinfo->image_width; col > 0; col--) {
412
0
        JSAMPLE index = *bufferptr++;
413
414
0
        if (index >= source->colormap.n_entries)
415
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
416
0
        *ptr++ = rescale[source->colormap.entries[index].red];
417
0
      }
418
92
    } else if (cinfo->in_color_space == JCS_CMYK) {
419
0
      for (col = cinfo->image_width; col > 0; col--) {
420
0
        JSAMPLE index = *bufferptr++;
421
422
0
        if (index >= source->colormap.n_entries)
423
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
424
0
        rgb_to_cmyk((1 << cinfo->data_precision) - 1,
425
0
                    rescale[source->colormap.entries[index].red],
426
0
                    rescale[source->colormap.entries[index].green],
427
0
                    rescale[source->colormap.entries[index].blue], ptr,
428
0
                    ptr + 1, ptr + 2, ptr + 3);
429
0
        ptr += 4;
430
0
      }
431
92
    } else {
432
92
      register int rindex = rgb_red[cinfo->in_color_space];
433
92
      register int gindex = rgb_green[cinfo->in_color_space];
434
92
      register int bindex = rgb_blue[cinfo->in_color_space];
435
92
      register int aindex = alpha_index[cinfo->in_color_space];
436
92
      register int ps = rgb_pixelsize[cinfo->in_color_space];
437
438
92
      for (col = cinfo->image_width; col > 0; col--) {
439
0
        JSAMPLE index = *bufferptr++;
440
441
0
        if (index >= source->colormap.n_entries)
442
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
443
0
        ptr[rindex] = rescale[source->colormap.entries[index].red];
444
0
        ptr[gindex] = rescale[source->colormap.entries[index].green];
445
0
        ptr[bindex] = rescale[source->colormap.entries[index].blue];
446
0
        if (aindex >= 0)
447
0
          ptr[aindex] = (1 << cinfo->data_precision) - 1;
448
0
        ptr += ps;
449
0
      }
450
92
    }
451
92
  }
452
671
  return 1;
453
671
}
Unexecuted instantiation: rdpng-12.c:get_indexed_row
Unexecuted instantiation: rdpng-16.c:get_indexed_row
454
455
456
#ifdef ZERO_BUFFERS
457
458
static void *spng_malloc(size_t size)
459
{
460
  return calloc(1, size);
461
}
462
463
#endif
464
465
466
/*
467
 * Read the file header; return image size and component count.
468
 */
469
470
METHODDEF(void)
471
start_input_png(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
472
28.7k
{
473
28.7k
  png_source_ptr source = (png_source_ptr)sinfo;
474
28.7k
  struct spng_ihdr ihdr;
475
28.7k
  int png_components = 3;
476
28.7k
  boolean use_raw_buffer;
477
#ifdef ZERO_BUFFERS
478
  struct spng_alloc alloc;
479
480
  alloc.malloc_fn = spng_malloc;
481
  alloc.realloc_fn = realloc;
482
  alloc.calloc_fn = calloc;
483
  alloc.free_fn = free;
484
  source->ctx = spng_ctx_new2(&alloc, 0);
485
#else
486
28.7k
  source->ctx = spng_ctx_new(0);
487
28.7k
#endif
488
28.7k
  if (!source->ctx)
489
0
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, "Could not create context");
490
491
28.7k
  TRY_SPNG(spng_set_png_file(source->ctx, sinfo->input_file));
492
493
28.7k
  TRY_SPNG(spng_get_ihdr(source->ctx, &ihdr));
494
495
28.7k
  if (ihdr.width > JPEG_MAX_DIMENSION || ihdr.height > JPEG_MAX_DIMENSION)
496
2.02k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
497
28.7k
  if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16)
498
6.14k
    ERREXIT(cinfo, JERR_PNG_BADDEPTH);
499
28.7k
  if (sinfo->max_pixels &&
500
19.1k
      (unsigned long long)ihdr.width * ihdr.height > sinfo->max_pixels)
501
280
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
502
503
28.7k
  cinfo->image_width = (JDIMENSION)ihdr.width;
504
28.7k
  cinfo->image_height = (JDIMENSION)ihdr.height;
505
28.7k
  source->png_bit_depth = ihdr.bit_depth;
506
28.7k
  source->png_color_type = ihdr.color_type;
507
508
  /* initialize flags to most common settings */
509
28.7k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
510
511
28.7k
  switch (ihdr.color_type) {
512
567
  case SPNG_COLOR_TYPE_GRAYSCALE_ALPHA:
513
3.36k
  case SPNG_COLOR_TYPE_GRAYSCALE:
514
3.36k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
515
3.36k
        cinfo->in_color_space == JCS_RGB)
516
0
      cinfo->in_color_space = JCS_GRAYSCALE;
517
3.36k
    TRACEMS3(cinfo, 1, JTRC_PNG_GRAYSCALE, ihdr.width, ihdr.height,
518
3.36k
             ihdr.bit_depth);
519
3.36k
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
520
480
      if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE &&
521
399
          cinfo->data_precision == ihdr.bit_depth) {
522
246
        source->pub.get_pixel_rows = get_raw_row;
523
246
        use_raw_buffer = TRUE;
524
246
      } else
525
234
        source->pub.get_pixel_rows = get_gray_row;
526
2.88k
    } else if (IsExtRGB(cinfo->in_color_space))
527
2.40k
      source->pub.get_pixel_rows = get_gray_rgb_row;
528
480
    else if (cinfo->in_color_space == JCS_CMYK)
529
480
        source->pub.get_pixel_rows = get_gray_cmyk_row;
530
0
    else
531
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532
3.36k
    if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) {
533
567
      png_components = 2;
534
567
      source->png_alpha = 1;
535
2.79k
    } else {
536
2.79k
      png_components = 1;
537
2.79k
      source->png_alpha = 0;
538
2.79k
    }
539
3.36k
    break;
540
541
10.1k
  case SPNG_COLOR_TYPE_TRUECOLOR:
542
14.0k
  case SPNG_COLOR_TYPE_TRUECOLOR_ALPHA:
543
14.0k
    if (cinfo->in_color_space == JCS_UNKNOWN)
544
0
      cinfo->in_color_space = JCS_EXT_RGB;
545
14.0k
    TRACEMS3(cinfo, 1, JTRC_PNG_TRUECOLOR, ihdr.width, ihdr.height,
546
14.0k
             ihdr.bit_depth);
547
14.0k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR &&
548
10.1k
        cinfo->data_precision == ihdr.bit_depth &&
549
7.72k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
550
7.72k
        (cinfo->in_color_space == JCS_EXT_RGB ||
551
6.62k
         cinfo->in_color_space == JCS_RGB)) {
552
#else
553
        cinfo->in_color_space == JCS_EXT_RGB) {
554
#endif
555
1.10k
      source->pub.get_pixel_rows = get_raw_row;
556
1.10k
      use_raw_buffer = TRUE;
557
12.9k
    } else if (IsExtRGB(cinfo->in_color_space))
558
8.93k
      source->pub.get_pixel_rows = get_rgb_row;
559
4.01k
    else if (cinfo->in_color_space == JCS_CMYK)
560
2.00k
      source->pub.get_pixel_rows = get_rgb_cmyk_row;
561
2.00k
    else
562
2.00k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563
14.0k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA) {
564
3.31k
      png_components = 4;
565
3.31k
      source->png_alpha = 1;
566
10.7k
    } else {
567
10.7k
      png_components = 3;
568
10.7k
      source->png_alpha = 0;
569
10.7k
    }
570
14.0k
    break;
571
572
1.47k
  case SPNG_COLOR_TYPE_INDEXED:
573
1.47k
  {
574
1.47k
    int i, gray = 1;
575
576
1.47k
    TRACEMS3(cinfo, 1, JTRC_PNG_INDEXED, ihdr.width, ihdr.height,
577
1.47k
             ihdr.bit_depth);
578
1.47k
    TRY_SPNG(spng_get_plte(source->ctx, &source->colormap));
579
1.47k
    if (source->png_bit_depth != 8 || source->colormap.n_entries > 256)
580
0
      ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
581
582
15.2k
    for (i = 0; i < (int)source->colormap.n_entries; i++) {
583
13.7k
      if (source->colormap.entries[i].red !=
584
13.7k
          source->colormap.entries[i].green ||
585
7.14k
          source->colormap.entries[i].green !=
586
7.14k
          source->colormap.entries[i].blue)
587
7.09k
        gray = 0;
588
13.7k
    }
589
590
1.47k
    if ((cinfo->in_color_space == JCS_UNKNOWN ||
591
252
         cinfo->in_color_space == JCS_RGB) && gray)
592
0
      cinfo->in_color_space = JCS_GRAYSCALE;
593
1.47k
    if (cinfo->in_color_space == JCS_GRAYSCALE && !gray)
594
20
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
596
1.47k
    source->pub.get_pixel_rows = get_indexed_row;
597
1.47k
    png_components = 1;
598
1.47k
    source->png_alpha = 0;
599
1.47k
    break;
600
10.1k
  }
601
602
0
  default:
603
0
    ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
604
28.7k
  }
605
606
15.6k
  if (IsExtRGB(cinfo->in_color_space))
607
12.6k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
608
3.02k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
609
496
    cinfo->input_components = 1;
610
2.52k
  else if (cinfo->in_color_space == JCS_CMYK)
611
2.52k
    cinfo->input_components = 4;
612
613
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
614
15.6k
  source->buffer_width =
615
15.6k
    (size_t)ihdr.width * png_components * source->png_bit_depth / 8;
616
15.6k
  source->iobuffer = (unsigned char *)
617
15.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
618
15.6k
                                source->buffer_width);
619
620
  /* Create compressor input buffer. */
621
15.6k
  if (use_raw_buffer) {
622
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
623
    /* Synthesize a _JSAMPARRAY pointer structure */
624
1.35k
    source->pixrow = (_JSAMPROW)source->iobuffer;
625
1.35k
    source->pub._buffer = &source->pixrow;
626
1.35k
    source->pub.buffer_height = 1;
627
14.2k
  } else {
628
14.2k
    unsigned int maxval = source->png_bit_depth == 16 ? 65535 : 255;
629
14.2k
    size_t val, half_maxval;
630
631
    /* Need to translate anyway, so make a separate sample buffer. */
632
14.2k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
633
14.2k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
634
14.2k
       (JDIMENSION)ihdr.width * cinfo->input_components, (JDIMENSION)1);
635
14.2k
    source->pub.buffer_height = 1;
636
637
    /* Compute the rescaling array. */
638
14.2k
    source->rescale = (_JSAMPLE *)
639
14.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
640
14.2k
                                  (maxval + 1L) * sizeof(_JSAMPLE));
641
14.2k
    memset(source->rescale, 0, (maxval + 1L) * sizeof(_JSAMPLE));
642
14.2k
    half_maxval = maxval / 2;
643
323M
    for (val = 0; val <= maxval; val++) {
644
      /* The multiplication here must be done in 32 bits to avoid overflow */
645
323M
      source->rescale[val] =
646
323M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
647
323M
                   maxval);
648
323M
    }
649
14.2k
  }
650
651
15.6k
  TRY_SPNG(spng_decode_image(source->ctx, NULL, 0, SPNG_FMT_PNG,
652
15.6k
                             SPNG_DECODE_PROGRESSIVE));
653
15.6k
}
rdpng-8.c:start_input_png
Line
Count
Source
472
28.7k
{
473
28.7k
  png_source_ptr source = (png_source_ptr)sinfo;
474
28.7k
  struct spng_ihdr ihdr;
475
28.7k
  int png_components = 3;
476
28.7k
  boolean use_raw_buffer;
477
#ifdef ZERO_BUFFERS
478
  struct spng_alloc alloc;
479
480
  alloc.malloc_fn = spng_malloc;
481
  alloc.realloc_fn = realloc;
482
  alloc.calloc_fn = calloc;
483
  alloc.free_fn = free;
484
  source->ctx = spng_ctx_new2(&alloc, 0);
485
#else
486
28.7k
  source->ctx = spng_ctx_new(0);
487
28.7k
#endif
488
28.7k
  if (!source->ctx)
489
0
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, "Could not create context");
490
491
28.7k
  TRY_SPNG(spng_set_png_file(source->ctx, sinfo->input_file));
492
493
28.7k
  TRY_SPNG(spng_get_ihdr(source->ctx, &ihdr));
494
495
28.7k
  if (ihdr.width > JPEG_MAX_DIMENSION || ihdr.height > JPEG_MAX_DIMENSION)
496
2.02k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
497
28.7k
  if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16)
498
6.14k
    ERREXIT(cinfo, JERR_PNG_BADDEPTH);
499
28.7k
  if (sinfo->max_pixels &&
500
19.1k
      (unsigned long long)ihdr.width * ihdr.height > sinfo->max_pixels)
501
280
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
502
503
28.7k
  cinfo->image_width = (JDIMENSION)ihdr.width;
504
28.7k
  cinfo->image_height = (JDIMENSION)ihdr.height;
505
28.7k
  source->png_bit_depth = ihdr.bit_depth;
506
28.7k
  source->png_color_type = ihdr.color_type;
507
508
  /* initialize flags to most common settings */
509
28.7k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
510
511
28.7k
  switch (ihdr.color_type) {
512
567
  case SPNG_COLOR_TYPE_GRAYSCALE_ALPHA:
513
3.36k
  case SPNG_COLOR_TYPE_GRAYSCALE:
514
3.36k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
515
3.36k
        cinfo->in_color_space == JCS_RGB)
516
0
      cinfo->in_color_space = JCS_GRAYSCALE;
517
3.36k
    TRACEMS3(cinfo, 1, JTRC_PNG_GRAYSCALE, ihdr.width, ihdr.height,
518
3.36k
             ihdr.bit_depth);
519
3.36k
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
520
480
      if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE &&
521
399
          cinfo->data_precision == ihdr.bit_depth) {
522
246
        source->pub.get_pixel_rows = get_raw_row;
523
246
        use_raw_buffer = TRUE;
524
246
      } else
525
234
        source->pub.get_pixel_rows = get_gray_row;
526
2.88k
    } else if (IsExtRGB(cinfo->in_color_space))
527
2.40k
      source->pub.get_pixel_rows = get_gray_rgb_row;
528
480
    else if (cinfo->in_color_space == JCS_CMYK)
529
480
        source->pub.get_pixel_rows = get_gray_cmyk_row;
530
0
    else
531
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532
3.36k
    if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) {
533
567
      png_components = 2;
534
567
      source->png_alpha = 1;
535
2.79k
    } else {
536
2.79k
      png_components = 1;
537
2.79k
      source->png_alpha = 0;
538
2.79k
    }
539
3.36k
    break;
540
541
10.1k
  case SPNG_COLOR_TYPE_TRUECOLOR:
542
14.0k
  case SPNG_COLOR_TYPE_TRUECOLOR_ALPHA:
543
14.0k
    if (cinfo->in_color_space == JCS_UNKNOWN)
544
0
      cinfo->in_color_space = JCS_EXT_RGB;
545
14.0k
    TRACEMS3(cinfo, 1, JTRC_PNG_TRUECOLOR, ihdr.width, ihdr.height,
546
14.0k
             ihdr.bit_depth);
547
14.0k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR &&
548
10.1k
        cinfo->data_precision == ihdr.bit_depth &&
549
7.72k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
550
7.72k
        (cinfo->in_color_space == JCS_EXT_RGB ||
551
6.62k
         cinfo->in_color_space == JCS_RGB)) {
552
#else
553
        cinfo->in_color_space == JCS_EXT_RGB) {
554
#endif
555
1.10k
      source->pub.get_pixel_rows = get_raw_row;
556
1.10k
      use_raw_buffer = TRUE;
557
12.9k
    } else if (IsExtRGB(cinfo->in_color_space))
558
8.93k
      source->pub.get_pixel_rows = get_rgb_row;
559
4.01k
    else if (cinfo->in_color_space == JCS_CMYK)
560
2.00k
      source->pub.get_pixel_rows = get_rgb_cmyk_row;
561
2.00k
    else
562
2.00k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563
14.0k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA) {
564
3.31k
      png_components = 4;
565
3.31k
      source->png_alpha = 1;
566
10.7k
    } else {
567
10.7k
      png_components = 3;
568
10.7k
      source->png_alpha = 0;
569
10.7k
    }
570
14.0k
    break;
571
572
1.47k
  case SPNG_COLOR_TYPE_INDEXED:
573
1.47k
  {
574
1.47k
    int i, gray = 1;
575
576
1.47k
    TRACEMS3(cinfo, 1, JTRC_PNG_INDEXED, ihdr.width, ihdr.height,
577
1.47k
             ihdr.bit_depth);
578
1.47k
    TRY_SPNG(spng_get_plte(source->ctx, &source->colormap));
579
1.47k
    if (source->png_bit_depth != 8 || source->colormap.n_entries > 256)
580
0
      ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
581
582
15.2k
    for (i = 0; i < (int)source->colormap.n_entries; i++) {
583
13.7k
      if (source->colormap.entries[i].red !=
584
13.7k
          source->colormap.entries[i].green ||
585
7.14k
          source->colormap.entries[i].green !=
586
7.14k
          source->colormap.entries[i].blue)
587
7.09k
        gray = 0;
588
13.7k
    }
589
590
1.47k
    if ((cinfo->in_color_space == JCS_UNKNOWN ||
591
252
         cinfo->in_color_space == JCS_RGB) && gray)
592
0
      cinfo->in_color_space = JCS_GRAYSCALE;
593
1.47k
    if (cinfo->in_color_space == JCS_GRAYSCALE && !gray)
594
20
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
596
1.47k
    source->pub.get_pixel_rows = get_indexed_row;
597
1.47k
    png_components = 1;
598
1.47k
    source->png_alpha = 0;
599
1.47k
    break;
600
10.1k
  }
601
602
0
  default:
603
0
    ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
604
28.7k
  }
605
606
15.6k
  if (IsExtRGB(cinfo->in_color_space))
607
12.6k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
608
3.02k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
609
496
    cinfo->input_components = 1;
610
2.52k
  else if (cinfo->in_color_space == JCS_CMYK)
611
2.52k
    cinfo->input_components = 4;
612
613
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
614
15.6k
  source->buffer_width =
615
15.6k
    (size_t)ihdr.width * png_components * source->png_bit_depth / 8;
616
15.6k
  source->iobuffer = (unsigned char *)
617
15.6k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
618
15.6k
                                source->buffer_width);
619
620
  /* Create compressor input buffer. */
621
15.6k
  if (use_raw_buffer) {
622
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
623
    /* Synthesize a _JSAMPARRAY pointer structure */
624
1.35k
    source->pixrow = (_JSAMPROW)source->iobuffer;
625
1.35k
    source->pub._buffer = &source->pixrow;
626
1.35k
    source->pub.buffer_height = 1;
627
14.2k
  } else {
628
14.2k
    unsigned int maxval = source->png_bit_depth == 16 ? 65535 : 255;
629
14.2k
    size_t val, half_maxval;
630
631
    /* Need to translate anyway, so make a separate sample buffer. */
632
14.2k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
633
14.2k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
634
14.2k
       (JDIMENSION)ihdr.width * cinfo->input_components, (JDIMENSION)1);
635
14.2k
    source->pub.buffer_height = 1;
636
637
    /* Compute the rescaling array. */
638
14.2k
    source->rescale = (_JSAMPLE *)
639
14.2k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
640
14.2k
                                  (maxval + 1L) * sizeof(_JSAMPLE));
641
14.2k
    memset(source->rescale, 0, (maxval + 1L) * sizeof(_JSAMPLE));
642
14.2k
    half_maxval = maxval / 2;
643
323M
    for (val = 0; val <= maxval; val++) {
644
      /* The multiplication here must be done in 32 bits to avoid overflow */
645
323M
      source->rescale[val] =
646
323M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
647
323M
                   maxval);
648
323M
    }
649
14.2k
  }
650
651
15.6k
  TRY_SPNG(spng_decode_image(source->ctx, NULL, 0, SPNG_FMT_PNG,
652
15.6k
                             SPNG_DECODE_PROGRESSIVE));
653
15.6k
}
Unexecuted instantiation: rdpng-12.c:start_input_png
Unexecuted instantiation: rdpng-16.c:start_input_png
654
655
656
/*
657
 * Return the ICC profile (if any) embedded in the PNG image.
658
 */
659
660
METHODDEF(boolean)
661
read_icc_profile_png(j_compress_ptr cinfo, cjpeg_source_ptr sinfo,
662
                     JOCTET **icc_data_ptr, unsigned int *icc_data_len)
663
4.11k
{
664
4.11k
  png_source_ptr source = (png_source_ptr)sinfo;
665
4.11k
  struct spng_iccp iccp;
666
667
4.11k
  if (!icc_data_ptr || !icc_data_len)
668
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
669
670
4.11k
  if (source->ctx && spng_get_iccp(source->ctx, &iccp) == 0) {
671
12
    *icc_data_ptr = (JOCTET *)iccp.profile;
672
12
    *icc_data_len = (unsigned int)iccp.profile_len;
673
12
    return TRUE;
674
12
  }
675
676
4.10k
  return FALSE;
677
4.11k
}
rdpng-8.c:read_icc_profile_png
Line
Count
Source
663
4.11k
{
664
4.11k
  png_source_ptr source = (png_source_ptr)sinfo;
665
4.11k
  struct spng_iccp iccp;
666
667
4.11k
  if (!icc_data_ptr || !icc_data_len)
668
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
669
670
4.11k
  if (source->ctx && spng_get_iccp(source->ctx, &iccp) == 0) {
671
12
    *icc_data_ptr = (JOCTET *)iccp.profile;
672
12
    *icc_data_len = (unsigned int)iccp.profile_len;
673
12
    return TRUE;
674
12
  }
675
676
4.10k
  return FALSE;
677
4.11k
}
Unexecuted instantiation: rdpng-12.c:read_icc_profile_png
Unexecuted instantiation: rdpng-16.c:read_icc_profile_png
678
679
680
/*
681
 * Finish up at the end of the file.
682
 */
683
684
METHODDEF(void)
685
finish_input_png(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
686
28.7k
{
687
28.7k
  png_source_ptr source = (png_source_ptr)sinfo;
688
689
28.7k
  if (source->ctx) {
690
28.7k
    spng_decode_chunks(source->ctx);
691
28.7k
    spng_ctx_free(source->ctx);
692
28.7k
    source->ctx = NULL;
693
28.7k
  }
694
28.7k
}
rdpng-8.c:finish_input_png
Line
Count
Source
686
28.7k
{
687
28.7k
  png_source_ptr source = (png_source_ptr)sinfo;
688
689
28.7k
  if (source->ctx) {
690
28.7k
    spng_decode_chunks(source->ctx);
691
28.7k
    spng_ctx_free(source->ctx);
692
    source->ctx = NULL;
693
28.7k
  }
694
28.7k
}
Unexecuted instantiation: rdpng-12.c:finish_input_png
Unexecuted instantiation: rdpng-16.c:finish_input_png
695
696
697
/*
698
 * The module selection routine for PNG format input.
699
 */
700
701
GLOBAL(cjpeg_source_ptr)
702
_jinit_read_png(j_compress_ptr cinfo)
703
28.7k
{
704
28.7k
  png_source_ptr source;
705
706
#if BITS_IN_JSAMPLE == 8
707
28.7k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
708
#else
709
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
710
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
711
0
#endif
712
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
713
714
  /* Create module interface object */
715
28.7k
  source = (png_source_ptr)
716
28.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
717
28.7k
                                sizeof(png_source_struct));
718
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
719
28.7k
  source->pub.start_input = start_input_png;
720
28.7k
  source->pub.read_icc_profile = read_icc_profile_png;
721
28.7k
  source->pub.finish_input = finish_input_png;
722
28.7k
  source->pub.max_pixels = 0;
723
724
28.7k
  return (cjpeg_source_ptr)source;
725
28.7k
}
jinit_read_png
Line
Count
Source
703
28.7k
{
704
28.7k
  png_source_ptr source;
705
706
28.7k
#if BITS_IN_JSAMPLE == 8
707
28.7k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
708
#else
709
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
710
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
711
#endif
712
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
713
714
  /* Create module interface object */
715
28.7k
  source = (png_source_ptr)
716
28.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
717
28.7k
                                sizeof(png_source_struct));
718
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
719
28.7k
  source->pub.start_input = start_input_png;
720
28.7k
  source->pub.read_icc_profile = read_icc_profile_png;
721
28.7k
  source->pub.finish_input = finish_input_png;
722
28.7k
  source->pub.max_pixels = 0;
723
724
28.7k
  return (cjpeg_source_ptr)source;
725
28.7k
}
Unexecuted instantiation: j12init_read_png
Unexecuted instantiation: j16init_read_png
726
727
#endif /* defined(PNG_SUPPORTED) &&
728
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */