Coverage Report

Created: 2026-07-30 06:40

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
81.8k
#define TRY_SPNG(f) { \
34
81.8k
  int __spng_error = (f); \
35
81.8k
  if (__spng_error) \
36
81.8k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(__spng_error)); \
37
81.8k
}
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
87.6k
{
76
87.6k
  png_source_ptr source = (png_source_ptr)sinfo;
77
87.6k
  int spng_error;
78
79
87.6k
  spng_error = spng_decode_row(source->ctx, source->iobuffer,
80
87.6k
                               source->buffer_width);
81
87.6k
  if (spng_error && spng_error != SPNG_EOI)
82
3.89k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(spng_error)); \
83
87.6k
  return 1;
84
87.6k
}
Unexecuted instantiation: rdpng-8.c:get_raw_row
rdpng-12.c:get_raw_row
Line
Count
Source
75
87.6k
{
76
87.6k
  png_source_ptr source = (png_source_ptr)sinfo;
77
87.6k
  int spng_error;
78
79
87.6k
  spng_error = spng_decode_row(source->ctx, source->iobuffer,
80
87.6k
                               source->buffer_width);
81
87.6k
  if (spng_error && spng_error != SPNG_EOI)
82
3.89k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(spng_error)); \
83
87.6k
  return 1;
84
87.6k
}
Unexecuted instantiation: rdpng-16.c:get_raw_row
85
86
87
32.7k
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op, pointer_op) { \
88
3.00M
  for (col = cinfo->image_width; col > 0; col--) { \
89
2.97M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
90
2.97M
    alpha_set_op \
91
2.97M
    ptr += ps; \
92
2.97M
    pointer_op \
93
2.97M
  } \
94
32.7k
}
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
6.54k
{
103
6.54k
  png_source_ptr source = (png_source_ptr)sinfo;
104
6.54k
  register _JSAMPROW ptr;
105
6.54k
  register _JSAMPLE *rescale = source->rescale;
106
6.54k
  JDIMENSION col;
107
108
6.54k
  get_raw_row(cinfo, sinfo);
109
6.54k
  ptr = source->pub._buffer[0];
110
#if BITS_IN_JSAMPLE != 12
111
0
  if (source->png_bit_depth == cinfo->data_precision) {
112
0
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
113
0
    for (col = cinfo->image_width; col > 0; col--) {
114
0
      *ptr++ = *bufferptr++;
115
0
      bufferptr += source->png_alpha;
116
0
    }
117
0
  } else
118
0
#endif
119
6.54k
  if (source->png_bit_depth == 16) {
120
656
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
121
62.5k
    for (col = cinfo->image_width; col > 0; col--) {
122
61.8k
      *ptr++ = rescale[*bufferptr++];
123
61.8k
      bufferptr += source->png_alpha;
124
61.8k
    }
125
5.88k
  } else {
126
5.88k
    register unsigned char *bufferptr = source->iobuffer;
127
538k
    for (col = cinfo->image_width; col > 0; col--) {
128
532k
      *ptr++ = rescale[*bufferptr++];
129
532k
      bufferptr += source->png_alpha;
130
532k
    }
131
5.88k
  }
132
6.54k
  return 1;
133
6.54k
}
Unexecuted instantiation: rdpng-8.c:get_gray_row
rdpng-12.c:get_gray_row
Line
Count
Source
102
6.54k
{
103
6.54k
  png_source_ptr source = (png_source_ptr)sinfo;
104
6.54k
  register _JSAMPROW ptr;
105
6.54k
  register _JSAMPLE *rescale = source->rescale;
106
6.54k
  JDIMENSION col;
107
108
6.54k
  get_raw_row(cinfo, sinfo);
109
6.54k
  ptr = source->pub._buffer[0];
110
#if BITS_IN_JSAMPLE != 12
111
  if (source->png_bit_depth == cinfo->data_precision) {
112
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
113
    for (col = cinfo->image_width; col > 0; col--) {
114
      *ptr++ = *bufferptr++;
115
      bufferptr += source->png_alpha;
116
    }
117
  } else
118
#endif
119
6.54k
  if (source->png_bit_depth == 16) {
120
656
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
121
62.5k
    for (col = cinfo->image_width; col > 0; col--) {
122
61.8k
      *ptr++ = rescale[*bufferptr++];
123
61.8k
      bufferptr += source->png_alpha;
124
61.8k
    }
125
5.88k
  } else {
126
5.88k
    register unsigned char *bufferptr = source->iobuffer;
127
538k
    for (col = cinfo->image_width; col > 0; col--) {
128
532k
      *ptr++ = rescale[*bufferptr++];
129
532k
      bufferptr += source->png_alpha;
130
532k
    }
131
5.88k
  }
132
6.54k
  return 1;
133
6.54k
}
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
32.7k
{
142
32.7k
  png_source_ptr source = (png_source_ptr)sinfo;
143
32.7k
  register _JSAMPROW ptr;
144
32.7k
  register _JSAMPLE *rescale = source->rescale;
145
32.7k
  JDIMENSION col;
146
32.7k
  register int rindex = rgb_red[cinfo->in_color_space];
147
32.7k
  register int gindex = rgb_green[cinfo->in_color_space];
148
32.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
149
32.7k
  register int aindex = alpha_index[cinfo->in_color_space];
150
32.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
151
152
32.7k
  get_raw_row(cinfo, sinfo);
153
32.7k
  ptr = source->pub._buffer[0];
154
#if BITS_IN_JSAMPLE != 12
155
0
  if (source->png_bit_depth == cinfo->data_precision) {
156
0
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
157
0
    if (aindex >= 0)
158
0
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
159
0
                         bufferptr += source->png_alpha;)
160
0
    else
161
0
      GRAY_RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
162
0
  } else
163
0
#endif
164
32.7k
  if (source->png_bit_depth == 16) {
165
3.28k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
166
3.28k
    if (aindex >= 0)
167
656
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
168
3.28k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
169
3.28k
                         bufferptr += source->png_alpha;)
170
2.62k
    else
171
2.62k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
172
3.28k
                         bufferptr += source->png_alpha;)
173
29.4k
  } else {
174
29.4k
    register unsigned char *bufferptr = source->iobuffer;
175
29.4k
    if (aindex >= 0)
176
5.65k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
177
29.4k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
178
29.4k
                         bufferptr += source->png_alpha;)
179
23.7k
    else
180
23.7k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
181
29.4k
                         bufferptr += source->png_alpha;)
182
29.4k
  }
183
32.7k
  return 1;
184
32.7k
}
Unexecuted instantiation: rdpng-8.c:get_gray_rgb_row
rdpng-12.c:get_gray_rgb_row
Line
Count
Source
141
32.7k
{
142
32.7k
  png_source_ptr source = (png_source_ptr)sinfo;
143
32.7k
  register _JSAMPROW ptr;
144
32.7k
  register _JSAMPLE *rescale = source->rescale;
145
32.7k
  JDIMENSION col;
146
32.7k
  register int rindex = rgb_red[cinfo->in_color_space];
147
32.7k
  register int gindex = rgb_green[cinfo->in_color_space];
148
32.7k
  register int bindex = rgb_blue[cinfo->in_color_space];
149
32.7k
  register int aindex = alpha_index[cinfo->in_color_space];
150
32.7k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
151
152
32.7k
  get_raw_row(cinfo, sinfo);
153
32.7k
  ptr = source->pub._buffer[0];
154
#if BITS_IN_JSAMPLE != 12
155
  if (source->png_bit_depth == cinfo->data_precision) {
156
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
157
    if (aindex >= 0)
158
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
159
                         bufferptr += source->png_alpha;)
160
    else
161
      GRAY_RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
162
  } else
163
#endif
164
32.7k
  if (source->png_bit_depth == 16) {
165
3.28k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
166
3.28k
    if (aindex >= 0)
167
656
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
168
3.28k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
169
3.28k
                         bufferptr += source->png_alpha;)
170
2.62k
    else
171
2.62k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
172
3.28k
                         bufferptr += source->png_alpha;)
173
29.4k
  } else {
174
29.4k
    register unsigned char *bufferptr = source->iobuffer;
175
29.4k
    if (aindex >= 0)
176
5.65k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
177
29.4k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
178
29.4k
                         bufferptr += source->png_alpha;)
179
23.7k
    else
180
23.7k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
181
29.4k
                         bufferptr += source->png_alpha;)
182
29.4k
  }
183
32.7k
  return 1;
184
32.7k
}
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
6.54k
{
193
6.54k
  png_source_ptr source = (png_source_ptr)sinfo;
194
6.54k
  register _JSAMPROW ptr;
195
6.54k
  register _JSAMPLE *rescale = source->rescale;
196
6.54k
  JDIMENSION col;
197
198
6.54k
  get_raw_row(cinfo, sinfo);
199
6.54k
  ptr = source->pub._buffer[0];
200
#if BITS_IN_JSAMPLE != 12
201
0
  if (source->png_bit_depth == cinfo->data_precision) {
202
0
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
203
0
    for (col = cinfo->image_width; col > 0; col--) {
204
0
      _JSAMPLE gray = *bufferptr++;
205
0
      bufferptr += source->png_alpha;
206
0
      rgb_to_cmyk(_MAXJSAMPLE, gray, gray, gray, ptr, ptr + 1, ptr + 2,
207
0
                  ptr + 3);
208
0
      ptr += 4;
209
0
    }
210
0
  } else
211
0
#endif
212
6.54k
  if (source->png_bit_depth == 16) {
213
656
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
214
62.5k
    for (col = cinfo->image_width; col > 0; col--) {
215
61.8k
      _JSAMPLE gray = rescale[*bufferptr++];
216
61.8k
      bufferptr += source->png_alpha;
217
61.8k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
218
61.8k
                  ptr + 1, ptr + 2, ptr + 3);
219
61.8k
      ptr += 4;
220
61.8k
    }
221
5.88k
  } else {
222
5.88k
    register unsigned char *bufferptr = source->iobuffer;
223
538k
    for (col = cinfo->image_width; col > 0; col--) {
224
532k
      _JSAMPLE gray = rescale[*bufferptr++];
225
532k
      bufferptr += source->png_alpha;
226
532k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
227
532k
                  ptr + 1, ptr + 2, ptr + 3);
228
532k
      ptr += 4;
229
532k
    }
230
5.88k
  }
231
6.54k
  return 1;
232
6.54k
}
Unexecuted instantiation: rdpng-8.c:get_gray_cmyk_row
rdpng-12.c:get_gray_cmyk_row
Line
Count
Source
192
6.54k
{
193
6.54k
  png_source_ptr source = (png_source_ptr)sinfo;
194
6.54k
  register _JSAMPROW ptr;
195
6.54k
  register _JSAMPLE *rescale = source->rescale;
196
6.54k
  JDIMENSION col;
197
198
6.54k
  get_raw_row(cinfo, sinfo);
199
6.54k
  ptr = source->pub._buffer[0];
200
#if BITS_IN_JSAMPLE != 12
201
  if (source->png_bit_depth == cinfo->data_precision) {
202
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
203
    for (col = cinfo->image_width; col > 0; col--) {
204
      _JSAMPLE gray = *bufferptr++;
205
      bufferptr += source->png_alpha;
206
      rgb_to_cmyk(_MAXJSAMPLE, gray, gray, gray, ptr, ptr + 1, ptr + 2,
207
                  ptr + 3);
208
      ptr += 4;
209
    }
210
  } else
211
#endif
212
6.54k
  if (source->png_bit_depth == 16) {
213
656
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
214
62.5k
    for (col = cinfo->image_width; col > 0; col--) {
215
61.8k
      _JSAMPLE gray = rescale[*bufferptr++];
216
61.8k
      bufferptr += source->png_alpha;
217
61.8k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
218
61.8k
                  ptr + 1, ptr + 2, ptr + 3);
219
61.8k
      ptr += 4;
220
61.8k
    }
221
5.88k
  } else {
222
5.88k
    register unsigned char *bufferptr = source->iobuffer;
223
538k
    for (col = cinfo->image_width; col > 0; col--) {
224
532k
      _JSAMPLE gray = rescale[*bufferptr++];
225
532k
      bufferptr += source->png_alpha;
226
532k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
227
532k
                  ptr + 1, ptr + 2, ptr + 3);
228
532k
      ptr += 4;
229
532k
    }
230
5.88k
  }
231
6.54k
  return 1;
232
6.54k
}
Unexecuted instantiation: rdpng-16.c:get_gray_cmyk_row
233
234
235
34.2k
#define RGB_READ_LOOP(read_op, alpha_set_op, pointer_op) { \
236
3.01M
  for (col = cinfo->image_width; col > 0; col--) { \
237
2.98M
    ptr[rindex] = read_op; \
238
2.98M
    ptr[gindex] = read_op; \
239
2.98M
    ptr[bindex] = read_op; \
240
2.98M
    alpha_set_op \
241
2.98M
    pointer_op \
242
2.98M
    ptr += ps; \
243
2.98M
  } \
244
34.2k
}
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
34.2k
{
253
34.2k
  png_source_ptr source = (png_source_ptr)sinfo;
254
34.2k
  register _JSAMPROW ptr;
255
34.2k
  register _JSAMPLE *rescale = source->rescale;
256
34.2k
  JDIMENSION col;
257
34.2k
  register int rindex = rgb_red[cinfo->in_color_space];
258
34.2k
  register int gindex = rgb_green[cinfo->in_color_space];
259
34.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
260
34.2k
  register int aindex = alpha_index[cinfo->in_color_space];
261
34.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
34.2k
  get_raw_row(cinfo, sinfo);
264
34.2k
  ptr = source->pub._buffer[0];
265
#if BITS_IN_JSAMPLE != 12
266
0
  if (source->png_bit_depth == cinfo->data_precision) {
267
0
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
268
0
    if (aindex >= 0)
269
0
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
270
0
                    bufferptr += source->png_alpha;)
271
0
    else
272
0
      RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
273
0
  } else
274
0
#endif
275
34.2k
  if (source->png_bit_depth == 16) {
276
22.9k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
277
22.9k
    if (aindex >= 0)
278
4.58k
      RGB_READ_LOOP(rescale[*bufferptr++],
279
22.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
280
22.9k
                    bufferptr += source->png_alpha;)
281
18.3k
    else
282
18.3k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
283
22.9k
                    bufferptr += source->png_alpha;)
284
22.9k
  } else {
285
11.2k
    register unsigned char *bufferptr = source->iobuffer;
286
11.2k
    if (aindex >= 0)
287
1.89k
      RGB_READ_LOOP(rescale[*bufferptr++],
288
11.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
289
11.2k
                    bufferptr += source->png_alpha;)
290
9.38k
    else
291
9.38k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
292
11.2k
                    bufferptr += source->png_alpha;)
293
11.2k
  }
294
34.2k
  return 1;
295
34.2k
}
Unexecuted instantiation: rdpng-8.c:get_rgb_row
rdpng-12.c:get_rgb_row
Line
Count
Source
252
34.2k
{
253
34.2k
  png_source_ptr source = (png_source_ptr)sinfo;
254
34.2k
  register _JSAMPROW ptr;
255
34.2k
  register _JSAMPLE *rescale = source->rescale;
256
34.2k
  JDIMENSION col;
257
34.2k
  register int rindex = rgb_red[cinfo->in_color_space];
258
34.2k
  register int gindex = rgb_green[cinfo->in_color_space];
259
34.2k
  register int bindex = rgb_blue[cinfo->in_color_space];
260
34.2k
  register int aindex = alpha_index[cinfo->in_color_space];
261
34.2k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
34.2k
  get_raw_row(cinfo, sinfo);
264
34.2k
  ptr = source->pub._buffer[0];
265
#if BITS_IN_JSAMPLE != 12
266
  if (source->png_bit_depth == cinfo->data_precision) {
267
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
268
    if (aindex >= 0)
269
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
270
                    bufferptr += source->png_alpha;)
271
    else
272
      RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
273
  } else
274
#endif
275
34.2k
  if (source->png_bit_depth == 16) {
276
22.9k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
277
22.9k
    if (aindex >= 0)
278
4.58k
      RGB_READ_LOOP(rescale[*bufferptr++],
279
22.9k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
280
22.9k
                    bufferptr += source->png_alpha;)
281
18.3k
    else
282
18.3k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
283
22.9k
                    bufferptr += source->png_alpha;)
284
22.9k
  } else {
285
11.2k
    register unsigned char *bufferptr = source->iobuffer;
286
11.2k
    if (aindex >= 0)
287
1.89k
      RGB_READ_LOOP(rescale[*bufferptr++],
288
11.2k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
289
11.2k
                    bufferptr += source->png_alpha;)
290
9.38k
    else
291
9.38k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
292
11.2k
                    bufferptr += source->png_alpha;)
293
11.2k
  }
294
34.2k
  return 1;
295
34.2k
}
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
6.84k
{
304
6.84k
  png_source_ptr source = (png_source_ptr)sinfo;
305
6.84k
  register _JSAMPROW ptr;
306
6.84k
  register _JSAMPLE *rescale = source->rescale;
307
6.84k
  JDIMENSION col;
308
309
6.84k
  get_raw_row(cinfo, sinfo);
310
6.84k
  ptr = source->pub._buffer[0];
311
#if BITS_IN_JSAMPLE != 12
312
0
  if (source->png_bit_depth == cinfo->data_precision) {
313
0
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
314
0
    for (col = cinfo->image_width; col > 0; col--) {
315
0
      _JSAMPLE r = *bufferptr++;
316
0
      _JSAMPLE g = *bufferptr++;
317
0
      _JSAMPLE b = *bufferptr++;
318
0
      bufferptr += source->png_alpha;
319
0
      rgb_to_cmyk(_MAXJSAMPLE, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
320
0
      ptr += 4;
321
0
    }
322
0
  } else
323
0
#endif
324
6.84k
  if (source->png_bit_depth == 16) {
325
4.58k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
326
425k
    for (col = cinfo->image_width; col > 0; col--) {
327
420k
      _JSAMPLE r = rescale[*bufferptr++];
328
420k
      _JSAMPLE g = rescale[*bufferptr++];
329
420k
      _JSAMPLE b = rescale[*bufferptr++];
330
420k
      bufferptr += source->png_alpha;
331
420k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
332
420k
                  ptr + 2, ptr + 3);
333
420k
      ptr += 4;
334
420k
    }
335
4.58k
  } else {
336
2.25k
    register unsigned char *bufferptr = source->iobuffer;
337
178k
    for (col = cinfo->image_width; col > 0; col--) {
338
175k
      _JSAMPLE r = rescale[*bufferptr++];
339
175k
      _JSAMPLE g = rescale[*bufferptr++];
340
175k
      _JSAMPLE b = rescale[*bufferptr++];
341
175k
      bufferptr += source->png_alpha;
342
175k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
343
175k
                  ptr + 2, ptr + 3);
344
175k
      ptr += 4;
345
175k
    }
346
2.25k
  }
347
6.84k
  return 1;
348
6.84k
}
Unexecuted instantiation: rdpng-8.c:get_rgb_cmyk_row
rdpng-12.c:get_rgb_cmyk_row
Line
Count
Source
303
6.84k
{
304
6.84k
  png_source_ptr source = (png_source_ptr)sinfo;
305
6.84k
  register _JSAMPROW ptr;
306
6.84k
  register _JSAMPLE *rescale = source->rescale;
307
6.84k
  JDIMENSION col;
308
309
6.84k
  get_raw_row(cinfo, sinfo);
310
6.84k
  ptr = source->pub._buffer[0];
311
#if BITS_IN_JSAMPLE != 12
312
  if (source->png_bit_depth == cinfo->data_precision) {
313
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
314
    for (col = cinfo->image_width; col > 0; col--) {
315
      _JSAMPLE r = *bufferptr++;
316
      _JSAMPLE g = *bufferptr++;
317
      _JSAMPLE b = *bufferptr++;
318
      bufferptr += source->png_alpha;
319
      rgb_to_cmyk(_MAXJSAMPLE, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
320
      ptr += 4;
321
    }
322
  } else
323
#endif
324
6.84k
  if (source->png_bit_depth == 16) {
325
4.58k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
326
425k
    for (col = cinfo->image_width; col > 0; col--) {
327
420k
      _JSAMPLE r = rescale[*bufferptr++];
328
420k
      _JSAMPLE g = rescale[*bufferptr++];
329
420k
      _JSAMPLE b = rescale[*bufferptr++];
330
420k
      bufferptr += source->png_alpha;
331
420k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
332
420k
                  ptr + 2, ptr + 3);
333
420k
      ptr += 4;
334
420k
    }
335
4.58k
  } else {
336
2.25k
    register unsigned char *bufferptr = source->iobuffer;
337
178k
    for (col = cinfo->image_width; col > 0; col--) {
338
175k
      _JSAMPLE r = rescale[*bufferptr++];
339
175k
      _JSAMPLE g = rescale[*bufferptr++];
340
175k
      _JSAMPLE b = rescale[*bufferptr++];
341
175k
      bufferptr += source->png_alpha;
342
175k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
343
175k
                  ptr + 2, ptr + 3);
344
175k
      ptr += 4;
345
175k
    }
346
2.25k
  }
347
6.84k
  return 1;
348
6.84k
}
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
733
{
354
/* This version is for reading 8-bit-per-channel indexed-color PNG files and
355
 * converting to extended RGB or CMYK.
356
 */
357
733
  png_source_ptr source = (png_source_ptr)sinfo;
358
733
  register _JSAMPROW ptr;
359
733
  register JSAMPLE *bufferptr = (JSAMPLE *)source->iobuffer;
360
733
  register _JSAMPLE *rescale = source->rescale;
361
733
  JDIMENSION col;
362
363
733
  get_raw_row(cinfo, sinfo);
364
733
  ptr = source->pub._buffer[0];
365
#if BITS_IN_JSAMPLE == 8
366
0
  if (source->png_bit_depth == cinfo->data_precision) {
367
0
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
368
0
      for (col = cinfo->image_width; col > 0; col--) {
369
0
        JSAMPLE index = *bufferptr++;
370
371
0
        if (index >= source->colormap.n_entries)
372
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
373
0
        *ptr++ = source->colormap.entries[index].red;
374
0
      }
375
0
    } else if (cinfo->in_color_space == JCS_CMYK) {
376
0
      for (col = cinfo->image_width; col > 0; col--) {
377
0
        JSAMPLE index = *bufferptr++;
378
379
0
        if (index >= source->colormap.n_entries)
380
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
381
0
        rgb_to_cmyk(_MAXJSAMPLE, source->colormap.entries[index].red,
382
0
                    source->colormap.entries[index].green,
383
0
                    source->colormap.entries[index].blue, ptr, ptr + 1,
384
0
                    ptr + 2, ptr + 3);
385
0
        ptr += 4;
386
0
      }
387
0
    } else {
388
0
      register int rindex = rgb_red[cinfo->in_color_space];
389
0
      register int gindex = rgb_green[cinfo->in_color_space];
390
0
      register int bindex = rgb_blue[cinfo->in_color_space];
391
0
      register int aindex = alpha_index[cinfo->in_color_space];
392
0
      register int ps = rgb_pixelsize[cinfo->in_color_space];
393
394
0
      for (col = cinfo->image_width; col > 0; col--) {
395
0
        JSAMPLE index = *bufferptr++;
396
397
0
        if (index >= source->colormap.n_entries)
398
0
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
399
0
        ptr[rindex] = source->colormap.entries[index].red;
400
0
        ptr[gindex] = source->colormap.entries[index].green;
401
0
        ptr[bindex] = source->colormap.entries[index].blue;
402
0
        if (aindex >= 0)
403
0
          ptr[aindex] = _MAXJSAMPLE;
404
0
        ptr += ps;
405
0
      }
406
0
    }
407
0
  } else
408
0
#endif
409
0
  {
410
733
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
411
224
      for (col = cinfo->image_width; col > 0; col--) {
412
196
        JSAMPLE index = *bufferptr++;
413
414
196
        if (index >= source->colormap.n_entries)
415
7
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
416
196
        *ptr++ = rescale[source->colormap.entries[index].red];
417
196
      }
418
705
    } else if (cinfo->in_color_space == JCS_CMYK) {
419
915
      for (col = cinfo->image_width; col > 0; col--) {
420
807
        JSAMPLE index = *bufferptr++;
421
422
807
        if (index >= source->colormap.n_entries)
423
15
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
424
807
        rgb_to_cmyk((1 << cinfo->data_precision) - 1,
425
807
                    rescale[source->colormap.entries[index].red],
426
807
                    rescale[source->colormap.entries[index].green],
427
807
                    rescale[source->colormap.entries[index].blue], ptr,
428
807
                    ptr + 1, ptr + 2, ptr + 3);
429
807
        ptr += 4;
430
807
      }
431
597
    } else {
432
597
      register int rindex = rgb_red[cinfo->in_color_space];
433
597
      register int gindex = rgb_green[cinfo->in_color_space];
434
597
      register int bindex = rgb_blue[cinfo->in_color_space];
435
597
      register int aindex = alpha_index[cinfo->in_color_space];
436
597
      register int ps = rgb_pixelsize[cinfo->in_color_space];
437
438
4.63k
      for (col = cinfo->image_width; col > 0; col--) {
439
4.03k
        JSAMPLE index = *bufferptr++;
440
441
4.03k
        if (index >= source->colormap.n_entries)
442
75
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
443
4.03k
        ptr[rindex] = rescale[source->colormap.entries[index].red];
444
4.03k
        ptr[gindex] = rescale[source->colormap.entries[index].green];
445
4.03k
        ptr[bindex] = rescale[source->colormap.entries[index].blue];
446
4.03k
        if (aindex >= 0)
447
792
          ptr[aindex] = (1 << cinfo->data_precision) - 1;
448
4.03k
        ptr += ps;
449
4.03k
      }
450
597
    }
451
0
  }
452
733
  return 1;
453
733
}
Unexecuted instantiation: rdpng-8.c:get_indexed_row
rdpng-12.c:get_indexed_row
Line
Count
Source
353
733
{
354
/* This version is for reading 8-bit-per-channel indexed-color PNG files and
355
 * converting to extended RGB or CMYK.
356
 */
357
733
  png_source_ptr source = (png_source_ptr)sinfo;
358
733
  register _JSAMPROW ptr;
359
733
  register JSAMPLE *bufferptr = (JSAMPLE *)source->iobuffer;
360
733
  register _JSAMPLE *rescale = source->rescale;
361
733
  JDIMENSION col;
362
363
733
  get_raw_row(cinfo, sinfo);
364
733
  ptr = source->pub._buffer[0];
365
#if BITS_IN_JSAMPLE == 8
366
  if (source->png_bit_depth == cinfo->data_precision) {
367
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
368
      for (col = cinfo->image_width; col > 0; col--) {
369
        JSAMPLE index = *bufferptr++;
370
371
        if (index >= source->colormap.n_entries)
372
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
373
        *ptr++ = source->colormap.entries[index].red;
374
      }
375
    } else if (cinfo->in_color_space == JCS_CMYK) {
376
      for (col = cinfo->image_width; col > 0; col--) {
377
        JSAMPLE index = *bufferptr++;
378
379
        if (index >= source->colormap.n_entries)
380
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
381
        rgb_to_cmyk(_MAXJSAMPLE, source->colormap.entries[index].red,
382
                    source->colormap.entries[index].green,
383
                    source->colormap.entries[index].blue, ptr, ptr + 1,
384
                    ptr + 2, ptr + 3);
385
        ptr += 4;
386
      }
387
    } else {
388
      register int rindex = rgb_red[cinfo->in_color_space];
389
      register int gindex = rgb_green[cinfo->in_color_space];
390
      register int bindex = rgb_blue[cinfo->in_color_space];
391
      register int aindex = alpha_index[cinfo->in_color_space];
392
      register int ps = rgb_pixelsize[cinfo->in_color_space];
393
394
      for (col = cinfo->image_width; col > 0; col--) {
395
        JSAMPLE index = *bufferptr++;
396
397
        if (index >= source->colormap.n_entries)
398
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
399
        ptr[rindex] = source->colormap.entries[index].red;
400
        ptr[gindex] = source->colormap.entries[index].green;
401
        ptr[bindex] = source->colormap.entries[index].blue;
402
        if (aindex >= 0)
403
          ptr[aindex] = _MAXJSAMPLE;
404
        ptr += ps;
405
      }
406
    }
407
  } else
408
#endif
409
733
  {
410
733
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
411
224
      for (col = cinfo->image_width; col > 0; col--) {
412
196
        JSAMPLE index = *bufferptr++;
413
414
196
        if (index >= source->colormap.n_entries)
415
7
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
416
196
        *ptr++ = rescale[source->colormap.entries[index].red];
417
196
      }
418
705
    } else if (cinfo->in_color_space == JCS_CMYK) {
419
915
      for (col = cinfo->image_width; col > 0; col--) {
420
807
        JSAMPLE index = *bufferptr++;
421
422
807
        if (index >= source->colormap.n_entries)
423
15
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
424
807
        rgb_to_cmyk((1 << cinfo->data_precision) - 1,
425
807
                    rescale[source->colormap.entries[index].red],
426
807
                    rescale[source->colormap.entries[index].green],
427
807
                    rescale[source->colormap.entries[index].blue], ptr,
428
807
                    ptr + 1, ptr + 2, ptr + 3);
429
807
        ptr += 4;
430
807
      }
431
597
    } else {
432
597
      register int rindex = rgb_red[cinfo->in_color_space];
433
597
      register int gindex = rgb_green[cinfo->in_color_space];
434
597
      register int bindex = rgb_blue[cinfo->in_color_space];
435
597
      register int aindex = alpha_index[cinfo->in_color_space];
436
597
      register int ps = rgb_pixelsize[cinfo->in_color_space];
437
438
4.63k
      for (col = cinfo->image_width; col > 0; col--) {
439
4.03k
        JSAMPLE index = *bufferptr++;
440
441
4.03k
        if (index >= source->colormap.n_entries)
442
75
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
443
4.03k
        ptr[rindex] = rescale[source->colormap.entries[index].red];
444
4.03k
        ptr[gindex] = rescale[source->colormap.entries[index].green];
445
4.03k
        ptr[bindex] = rescale[source->colormap.entries[index].blue];
446
4.03k
        if (aindex >= 0)
447
792
          ptr[aindex] = (1 << cinfo->data_precision) - 1;
448
4.03k
        ptr += ps;
449
4.03k
      }
450
597
    }
451
733
  }
452
733
  return 1;
453
733
}
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
31.1k
{
473
31.1k
  png_source_ptr source = (png_source_ptr)sinfo;
474
31.1k
  struct spng_ihdr ihdr;
475
31.1k
  int png_components = 3;
476
31.1k
  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
31.1k
  source->ctx = spng_ctx_new(0);
487
31.1k
#endif
488
31.1k
  if (!source->ctx)
489
0
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, "Could not create context");
490
491
31.1k
  TRY_SPNG(spng_set_png_file(source->ctx, sinfo->input_file));
492
493
31.1k
  TRY_SPNG(spng_get_ihdr(source->ctx, &ihdr));
494
495
31.1k
  if (ihdr.width > JPEG_MAX_DIMENSION || ihdr.height > JPEG_MAX_DIMENSION)
496
532
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
497
31.1k
  if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16)
498
8.55k
    ERREXIT(cinfo, JERR_PNG_BADDEPTH);
499
31.1k
  if (sinfo->max_pixels &&
500
20.6k
      (unsigned long long)ihdr.width * ihdr.height > sinfo->max_pixels)
501
280
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
502
503
31.1k
  cinfo->image_width = (JDIMENSION)ihdr.width;
504
31.1k
  cinfo->image_height = (JDIMENSION)ihdr.height;
505
31.1k
  source->png_bit_depth = ihdr.bit_depth;
506
31.1k
  source->png_color_type = ihdr.color_type;
507
508
  /* initialize flags to most common settings */
509
31.1k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
510
511
31.1k
  switch (ihdr.color_type) {
512
14
  case SPNG_COLOR_TYPE_GRAYSCALE_ALPHA:
513
11.0k
  case SPNG_COLOR_TYPE_GRAYSCALE:
514
11.0k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
515
11.0k
        cinfo->in_color_space == JCS_RGB)
516
0
      cinfo->in_color_space = JCS_GRAYSCALE;
517
11.0k
    TRACEMS3(cinfo, 1, JTRC_PNG_GRAYSCALE, ihdr.width, ihdr.height,
518
11.0k
             ihdr.bit_depth);
519
11.0k
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
520
1.57k
      if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE &&
521
1.57k
          cinfo->data_precision == ihdr.bit_depth) {
522
0
        source->pub.get_pixel_rows = get_raw_row;
523
0
        use_raw_buffer = TRUE;
524
0
      } else
525
1.57k
        source->pub.get_pixel_rows = get_gray_row;
526
9.43k
    } else if (IsExtRGB(cinfo->in_color_space))
527
7.86k
      source->pub.get_pixel_rows = get_gray_rgb_row;
528
1.57k
    else if (cinfo->in_color_space == JCS_CMYK)
529
1.57k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
530
0
    else
531
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532
11.0k
    if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) {
533
14
      png_components = 2;
534
14
      source->png_alpha = 1;
535
10.9k
    } else {
536
10.9k
      png_components = 1;
537
10.9k
      source->png_alpha = 0;
538
10.9k
    }
539
11.0k
    break;
540
541
6.88k
  case SPNG_COLOR_TYPE_TRUECOLOR:
542
7.56k
  case SPNG_COLOR_TYPE_TRUECOLOR_ALPHA:
543
7.56k
    if (cinfo->in_color_space == JCS_UNKNOWN)
544
0
      cinfo->in_color_space = JCS_EXT_RGB;
545
7.56k
    TRACEMS3(cinfo, 1, JTRC_PNG_TRUECOLOR, ihdr.width, ihdr.height,
546
7.56k
             ihdr.bit_depth);
547
7.56k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR &&
548
6.88k
        cinfo->data_precision == ihdr.bit_depth &&
549
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
550
0
        (cinfo->in_color_space == JCS_EXT_RGB ||
551
0
         cinfo->in_color_space == JCS_RGB)) {
552
#else
553
        cinfo->in_color_space == JCS_EXT_RGB) {
554
#endif
555
0
      source->pub.get_pixel_rows = get_raw_row;
556
0
      use_raw_buffer = TRUE;
557
7.56k
    } else if (IsExtRGB(cinfo->in_color_space))
558
5.40k
      source->pub.get_pixel_rows = get_rgb_row;
559
2.16k
    else if (cinfo->in_color_space == JCS_CMYK)
560
1.08k
      source->pub.get_pixel_rows = get_rgb_cmyk_row;
561
1.08k
    else
562
1.08k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563
7.56k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA) {
564
588
      png_components = 4;
565
588
      source->png_alpha = 1;
566
6.97k
    } else {
567
6.97k
      png_components = 3;
568
6.97k
      source->png_alpha = 0;
569
6.97k
    }
570
7.56k
    break;
571
572
1.83k
  case SPNG_COLOR_TYPE_INDEXED:
573
1.83k
  {
574
1.83k
    int i, gray = 1;
575
576
1.83k
    TRACEMS3(cinfo, 1, JTRC_PNG_INDEXED, ihdr.width, ihdr.height,
577
1.83k
             ihdr.bit_depth);
578
1.83k
    TRY_SPNG(spng_get_plte(source->ctx, &source->colormap));
579
1.83k
    if (source->png_bit_depth != 8 || source->colormap.n_entries > 256)
580
0
      ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
581
582
18.4k
    for (i = 0; i < (int)source->colormap.n_entries; i++) {
583
16.5k
      if (source->colormap.entries[i].red !=
584
16.5k
          source->colormap.entries[i].green ||
585
6.27k
          source->colormap.entries[i].green !=
586
6.27k
          source->colormap.entries[i].blue)
587
10.3k
        gray = 0;
588
16.5k
    }
589
590
1.83k
    if ((cinfo->in_color_space == JCS_UNKNOWN ||
591
273
         cinfo->in_color_space == JCS_RGB) && gray)
592
0
      cinfo->in_color_space = JCS_GRAYSCALE;
593
1.83k
    if (cinfo->in_color_space == JCS_GRAYSCALE && !gray)
594
27
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
596
1.83k
    source->pub.get_pixel_rows = get_indexed_row;
597
1.83k
    png_components = 1;
598
1.83k
    source->png_alpha = 0;
599
1.83k
    break;
600
6.88k
  }
601
602
0
  default:
603
0
    ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
604
31.1k
  }
605
606
17.7k
  if (IsExtRGB(cinfo->in_color_space))
607
13.4k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
608
4.27k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
609
1.58k
    cinfo->input_components = 1;
610
2.69k
  else if (cinfo->in_color_space == JCS_CMYK)
611
2.69k
    cinfo->input_components = 4;
612
613
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
614
17.7k
  source->buffer_width =
615
17.7k
    (size_t)ihdr.width * png_components * source->png_bit_depth / 8;
616
17.7k
  source->iobuffer = (unsigned char *)
617
17.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
618
17.7k
                                source->buffer_width);
619
620
  /* Create compressor input buffer. */
621
17.7k
  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
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
625
0
    source->pub._buffer = &source->pixrow;
626
0
    source->pub.buffer_height = 1;
627
17.7k
  } else {
628
17.7k
    unsigned int maxval = source->png_bit_depth == 16 ? 65535 : 255;
629
17.7k
    size_t val, half_maxval;
630
631
    /* Need to translate anyway, so make a separate sample buffer. */
632
17.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
633
17.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
634
17.7k
       (JDIMENSION)ihdr.width * cinfo->input_components, (JDIMENSION)1);
635
17.7k
    source->pub.buffer_height = 1;
636
637
    /* Compute the rescaling array. */
638
17.7k
    source->rescale = (_JSAMPLE *)
639
17.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
640
17.7k
                                  (maxval + 1L) * sizeof(_JSAMPLE));
641
17.7k
    memset(source->rescale, 0, (maxval + 1L) * sizeof(_JSAMPLE));
642
17.7k
    half_maxval = maxval / 2;
643
234M
    for (val = 0; val <= maxval; val++) {
644
      /* The multiplication here must be done in 32 bits to avoid overflow */
645
234M
      source->rescale[val] =
646
234M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
647
234M
                   maxval);
648
234M
    }
649
17.7k
  }
650
651
17.7k
  TRY_SPNG(spng_decode_image(source->ctx, NULL, 0, SPNG_FMT_PNG,
652
17.7k
                             SPNG_DECODE_PROGRESSIVE));
653
17.7k
}
Unexecuted instantiation: rdpng-8.c:start_input_png
rdpng-12.c:start_input_png
Line
Count
Source
472
31.1k
{
473
31.1k
  png_source_ptr source = (png_source_ptr)sinfo;
474
31.1k
  struct spng_ihdr ihdr;
475
31.1k
  int png_components = 3;
476
31.1k
  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
31.1k
  source->ctx = spng_ctx_new(0);
487
31.1k
#endif
488
31.1k
  if (!source->ctx)
489
0
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, "Could not create context");
490
491
31.1k
  TRY_SPNG(spng_set_png_file(source->ctx, sinfo->input_file));
492
493
31.1k
  TRY_SPNG(spng_get_ihdr(source->ctx, &ihdr));
494
495
31.1k
  if (ihdr.width > JPEG_MAX_DIMENSION || ihdr.height > JPEG_MAX_DIMENSION)
496
532
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
497
31.1k
  if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16)
498
8.55k
    ERREXIT(cinfo, JERR_PNG_BADDEPTH);
499
31.1k
  if (sinfo->max_pixels &&
500
20.6k
      (unsigned long long)ihdr.width * ihdr.height > sinfo->max_pixels)
501
280
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
502
503
31.1k
  cinfo->image_width = (JDIMENSION)ihdr.width;
504
31.1k
  cinfo->image_height = (JDIMENSION)ihdr.height;
505
31.1k
  source->png_bit_depth = ihdr.bit_depth;
506
31.1k
  source->png_color_type = ihdr.color_type;
507
508
  /* initialize flags to most common settings */
509
31.1k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
510
511
31.1k
  switch (ihdr.color_type) {
512
14
  case SPNG_COLOR_TYPE_GRAYSCALE_ALPHA:
513
11.0k
  case SPNG_COLOR_TYPE_GRAYSCALE:
514
11.0k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
515
11.0k
        cinfo->in_color_space == JCS_RGB)
516
0
      cinfo->in_color_space = JCS_GRAYSCALE;
517
11.0k
    TRACEMS3(cinfo, 1, JTRC_PNG_GRAYSCALE, ihdr.width, ihdr.height,
518
11.0k
             ihdr.bit_depth);
519
11.0k
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
520
1.57k
      if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE &&
521
1.57k
          cinfo->data_precision == ihdr.bit_depth) {
522
0
        source->pub.get_pixel_rows = get_raw_row;
523
0
        use_raw_buffer = TRUE;
524
0
      } else
525
1.57k
        source->pub.get_pixel_rows = get_gray_row;
526
9.43k
    } else if (IsExtRGB(cinfo->in_color_space))
527
7.86k
      source->pub.get_pixel_rows = get_gray_rgb_row;
528
1.57k
    else if (cinfo->in_color_space == JCS_CMYK)
529
1.57k
        source->pub.get_pixel_rows = get_gray_cmyk_row;
530
0
    else
531
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532
11.0k
    if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) {
533
14
      png_components = 2;
534
14
      source->png_alpha = 1;
535
10.9k
    } else {
536
10.9k
      png_components = 1;
537
10.9k
      source->png_alpha = 0;
538
10.9k
    }
539
11.0k
    break;
540
541
6.88k
  case SPNG_COLOR_TYPE_TRUECOLOR:
542
7.56k
  case SPNG_COLOR_TYPE_TRUECOLOR_ALPHA:
543
7.56k
    if (cinfo->in_color_space == JCS_UNKNOWN)
544
0
      cinfo->in_color_space = JCS_EXT_RGB;
545
7.56k
    TRACEMS3(cinfo, 1, JTRC_PNG_TRUECOLOR, ihdr.width, ihdr.height,
546
7.56k
             ihdr.bit_depth);
547
7.56k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR &&
548
6.88k
        cinfo->data_precision == ihdr.bit_depth &&
549
0
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
550
0
        (cinfo->in_color_space == JCS_EXT_RGB ||
551
0
         cinfo->in_color_space == JCS_RGB)) {
552
#else
553
        cinfo->in_color_space == JCS_EXT_RGB) {
554
#endif
555
0
      source->pub.get_pixel_rows = get_raw_row;
556
0
      use_raw_buffer = TRUE;
557
7.56k
    } else if (IsExtRGB(cinfo->in_color_space))
558
5.40k
      source->pub.get_pixel_rows = get_rgb_row;
559
2.16k
    else if (cinfo->in_color_space == JCS_CMYK)
560
1.08k
      source->pub.get_pixel_rows = get_rgb_cmyk_row;
561
1.08k
    else
562
1.08k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563
7.56k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA) {
564
588
      png_components = 4;
565
588
      source->png_alpha = 1;
566
6.97k
    } else {
567
6.97k
      png_components = 3;
568
6.97k
      source->png_alpha = 0;
569
6.97k
    }
570
7.56k
    break;
571
572
1.83k
  case SPNG_COLOR_TYPE_INDEXED:
573
1.83k
  {
574
1.83k
    int i, gray = 1;
575
576
1.83k
    TRACEMS3(cinfo, 1, JTRC_PNG_INDEXED, ihdr.width, ihdr.height,
577
1.83k
             ihdr.bit_depth);
578
1.83k
    TRY_SPNG(spng_get_plte(source->ctx, &source->colormap));
579
1.83k
    if (source->png_bit_depth != 8 || source->colormap.n_entries > 256)
580
0
      ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
581
582
18.4k
    for (i = 0; i < (int)source->colormap.n_entries; i++) {
583
16.5k
      if (source->colormap.entries[i].red !=
584
16.5k
          source->colormap.entries[i].green ||
585
6.27k
          source->colormap.entries[i].green !=
586
6.27k
          source->colormap.entries[i].blue)
587
10.3k
        gray = 0;
588
16.5k
    }
589
590
1.83k
    if ((cinfo->in_color_space == JCS_UNKNOWN ||
591
273
         cinfo->in_color_space == JCS_RGB) && gray)
592
0
      cinfo->in_color_space = JCS_GRAYSCALE;
593
1.83k
    if (cinfo->in_color_space == JCS_GRAYSCALE && !gray)
594
27
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
595
596
1.83k
    source->pub.get_pixel_rows = get_indexed_row;
597
1.83k
    png_components = 1;
598
1.83k
    source->png_alpha = 0;
599
1.83k
    break;
600
6.88k
  }
601
602
0
  default:
603
0
    ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
604
31.1k
  }
605
606
17.7k
  if (IsExtRGB(cinfo->in_color_space))
607
13.4k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
608
4.27k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
609
1.58k
    cinfo->input_components = 1;
610
2.69k
  else if (cinfo->in_color_space == JCS_CMYK)
611
2.69k
    cinfo->input_components = 4;
612
613
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
614
17.7k
  source->buffer_width =
615
17.7k
    (size_t)ihdr.width * png_components * source->png_bit_depth / 8;
616
17.7k
  source->iobuffer = (unsigned char *)
617
17.7k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
618
17.7k
                                source->buffer_width);
619
620
  /* Create compressor input buffer. */
621
17.7k
  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
0
    source->pixrow = (_JSAMPROW)source->iobuffer;
625
0
    source->pub._buffer = &source->pixrow;
626
0
    source->pub.buffer_height = 1;
627
17.7k
  } else {
628
17.7k
    unsigned int maxval = source->png_bit_depth == 16 ? 65535 : 255;
629
17.7k
    size_t val, half_maxval;
630
631
    /* Need to translate anyway, so make a separate sample buffer. */
632
17.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
633
17.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
634
17.7k
       (JDIMENSION)ihdr.width * cinfo->input_components, (JDIMENSION)1);
635
17.7k
    source->pub.buffer_height = 1;
636
637
    /* Compute the rescaling array. */
638
17.7k
    source->rescale = (_JSAMPLE *)
639
17.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
640
17.7k
                                  (maxval + 1L) * sizeof(_JSAMPLE));
641
17.7k
    memset(source->rescale, 0, (maxval + 1L) * sizeof(_JSAMPLE));
642
17.7k
    half_maxval = maxval / 2;
643
234M
    for (val = 0; val <= maxval; val++) {
644
      /* The multiplication here must be done in 32 bits to avoid overflow */
645
234M
      source->rescale[val] =
646
234M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
647
234M
                   maxval);
648
234M
    }
649
17.7k
  }
650
651
17.7k
  TRY_SPNG(spng_decode_image(source->ctx, NULL, 0, SPNG_FMT_PNG,
652
17.7k
                             SPNG_DECODE_PROGRESSIVE));
653
17.7k
}
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
5.85k
{
664
5.85k
  png_source_ptr source = (png_source_ptr)sinfo;
665
5.85k
  struct spng_iccp iccp;
666
667
5.85k
  if (!icc_data_ptr || !icc_data_len)
668
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
669
670
5.85k
  if (source->ctx && spng_get_iccp(source->ctx, &iccp) == 0) {
671
6
    *icc_data_ptr = (JOCTET *)iccp.profile;
672
6
    *icc_data_len = (unsigned int)iccp.profile_len;
673
6
    return TRUE;
674
6
  }
675
676
5.84k
  return FALSE;
677
5.85k
}
Unexecuted instantiation: rdpng-8.c:read_icc_profile_png
rdpng-12.c:read_icc_profile_png
Line
Count
Source
663
5.85k
{
664
5.85k
  png_source_ptr source = (png_source_ptr)sinfo;
665
5.85k
  struct spng_iccp iccp;
666
667
5.85k
  if (!icc_data_ptr || !icc_data_len)
668
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
669
670
5.85k
  if (source->ctx && spng_get_iccp(source->ctx, &iccp) == 0) {
671
6
    *icc_data_ptr = (JOCTET *)iccp.profile;
672
6
    *icc_data_len = (unsigned int)iccp.profile_len;
673
6
    return TRUE;
674
6
  }
675
676
5.84k
  return FALSE;
677
5.85k
}
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
31.1k
{
687
31.1k
  png_source_ptr source = (png_source_ptr)sinfo;
688
689
31.1k
  if (source->ctx) {
690
31.1k
    spng_decode_chunks(source->ctx);
691
31.1k
    spng_ctx_free(source->ctx);
692
31.1k
    source->ctx = NULL;
693
31.1k
  }
694
31.1k
}
Unexecuted instantiation: rdpng-8.c:finish_input_png
rdpng-12.c:finish_input_png
Line
Count
Source
686
31.1k
{
687
31.1k
  png_source_ptr source = (png_source_ptr)sinfo;
688
689
31.1k
  if (source->ctx) {
690
31.1k
    spng_decode_chunks(source->ctx);
691
31.1k
    spng_ctx_free(source->ctx);
692
    source->ctx = NULL;
693
31.1k
  }
694
31.1k
}
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
31.1k
{
704
31.1k
  png_source_ptr source;
705
706
#if BITS_IN_JSAMPLE == 8
707
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
708
#else
709
31.1k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
710
31.1k
      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
31.1k
  source = (png_source_ptr)
716
31.1k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
717
31.1k
                                sizeof(png_source_struct));
718
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
719
31.1k
  source->pub.start_input = start_input_png;
720
31.1k
  source->pub.read_icc_profile = read_icc_profile_png;
721
31.1k
  source->pub.finish_input = finish_input_png;
722
31.1k
  source->pub.max_pixels = 0;
723
724
31.1k
  return (cjpeg_source_ptr)source;
725
31.1k
}
Unexecuted instantiation: jinit_read_png
j12init_read_png
Line
Count
Source
703
31.1k
{
704
31.1k
  png_source_ptr source;
705
706
#if BITS_IN_JSAMPLE == 8
707
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
708
#else
709
31.1k
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
710
31.1k
      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
31.1k
  source = (png_source_ptr)
716
31.1k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
717
31.1k
                                sizeof(png_source_struct));
718
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
719
31.1k
  source->pub.start_input = start_input_png;
720
31.1k
  source->pub.read_icc_profile = read_icc_profile_png;
721
31.1k
  source->pub.finish_input = finish_input_png;
722
31.1k
  source->pub.max_pixels = 0;
723
724
31.1k
  return (cjpeg_source_ptr)source;
725
31.1k
}
Unexecuted instantiation: j16init_read_png
726
727
#endif /* defined(PNG_SUPPORTED) &&
728
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */