Coverage Report

Created: 2026-10-08 07:02

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
79.9k
#define TRY_SPNG(f) { \
34
79.9k
  int __spng_error = (f); \
35
79.9k
  if (__spng_error) \
36
79.9k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(__spng_error)); \
37
79.9k
}
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
436k
{
76
436k
  png_source_ptr source = (png_source_ptr)sinfo;
77
436k
  int spng_error;
78
79
436k
  spng_error = spng_decode_row(source->ctx, source->iobuffer,
80
436k
                               source->buffer_width);
81
436k
  if (spng_error && spng_error != SPNG_EOI)
82
3.15k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(spng_error)); \
83
436k
  return 1;
84
436k
}
rdpng-8.c:get_raw_row
Line
Count
Source
75
436k
{
76
436k
  png_source_ptr source = (png_source_ptr)sinfo;
77
436k
  int spng_error;
78
79
436k
  spng_error = spng_decode_row(source->ctx, source->iobuffer,
80
436k
                               source->buffer_width);
81
436k
  if (spng_error && spng_error != SPNG_EOI)
82
3.15k
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, spng_strerror(spng_error)); \
83
436k
  return 1;
84
436k
}
Unexecuted instantiation: rdpng-12.c:get_raw_row
Unexecuted instantiation: rdpng-16.c:get_raw_row
85
86
87
102k
#define GRAY_RGB_READ_LOOP(read_op, alpha_set_op, pointer_op) { \
88
1.77M
  for (col = cinfo->image_width; col > 0; col--) { \
89
1.67M
    ptr[rindex] = ptr[gindex] = ptr[bindex] = read_op; \
90
1.67M
    alpha_set_op \
91
1.67M
    ptr += ps; \
92
1.67M
    pointer_op \
93
1.67M
  } \
94
102k
}
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
19.4k
{
103
19.4k
  png_source_ptr source = (png_source_ptr)sinfo;
104
19.4k
  register _JSAMPROW ptr;
105
19.4k
  register _JSAMPLE *rescale = source->rescale;
106
19.4k
  JDIMENSION col;
107
108
19.4k
  get_raw_row(cinfo, sinfo);
109
19.4k
  ptr = source->pub._buffer[0];
110
#if BITS_IN_JSAMPLE != 12
111
19.4k
  if (source->png_bit_depth == cinfo->data_precision) {
112
11.9k
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
113
95.4k
    for (col = cinfo->image_width; col > 0; col--) {
114
83.4k
      *ptr++ = *bufferptr++;
115
83.4k
      bufferptr += source->png_alpha;
116
83.4k
    }
117
11.9k
  } else
118
7.49k
#endif
119
7.49k
  if (source->png_bit_depth == 16) {
120
7.37k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
121
178k
    for (col = cinfo->image_width; col > 0; col--) {
122
170k
      *ptr++ = rescale[*bufferptr++];
123
170k
      bufferptr += source->png_alpha;
124
170k
    }
125
7.37k
  } else {
126
126
    register unsigned char *bufferptr = source->iobuffer;
127
126
    for (col = cinfo->image_width; col > 0; col--) {
128
0
      *ptr++ = rescale[*bufferptr++];
129
0
      bufferptr += source->png_alpha;
130
0
    }
131
126
  }
132
19.4k
  return 1;
133
19.4k
}
rdpng-8.c:get_gray_row
Line
Count
Source
102
19.4k
{
103
19.4k
  png_source_ptr source = (png_source_ptr)sinfo;
104
19.4k
  register _JSAMPROW ptr;
105
19.4k
  register _JSAMPLE *rescale = source->rescale;
106
19.4k
  JDIMENSION col;
107
108
19.4k
  get_raw_row(cinfo, sinfo);
109
19.4k
  ptr = source->pub._buffer[0];
110
19.4k
#if BITS_IN_JSAMPLE != 12
111
19.4k
  if (source->png_bit_depth == cinfo->data_precision) {
112
11.9k
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
113
95.4k
    for (col = cinfo->image_width; col > 0; col--) {
114
83.4k
      *ptr++ = *bufferptr++;
115
83.4k
      bufferptr += source->png_alpha;
116
83.4k
    }
117
11.9k
  } else
118
7.49k
#endif
119
7.49k
  if (source->png_bit_depth == 16) {
120
7.37k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
121
178k
    for (col = cinfo->image_width; col > 0; col--) {
122
170k
      *ptr++ = rescale[*bufferptr++];
123
170k
      bufferptr += source->png_alpha;
124
170k
    }
125
7.37k
  } else {
126
126
    register unsigned char *bufferptr = source->iobuffer;
127
126
    for (col = cinfo->image_width; col > 0; col--) {
128
0
      *ptr++ = rescale[*bufferptr++];
129
0
      bufferptr += source->png_alpha;
130
0
    }
131
126
  }
132
19.4k
  return 1;
133
19.4k
}
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
102k
{
142
102k
  png_source_ptr source = (png_source_ptr)sinfo;
143
102k
  register _JSAMPROW ptr;
144
102k
  register _JSAMPLE *rescale = source->rescale;
145
102k
  JDIMENSION col;
146
102k
  register int rindex = rgb_red[cinfo->in_color_space];
147
102k
  register int gindex = rgb_green[cinfo->in_color_space];
148
102k
  register int bindex = rgb_blue[cinfo->in_color_space];
149
102k
  register int aindex = alpha_index[cinfo->in_color_space];
150
102k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
151
152
102k
  get_raw_row(cinfo, sinfo);
153
102k
  ptr = source->pub._buffer[0];
154
#if BITS_IN_JSAMPLE != 12
155
102k
  if (source->png_bit_depth == cinfo->data_precision) {
156
64.4k
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
157
64.4k
    if (aindex >= 0)
158
12.8k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
159
64.4k
                         bufferptr += source->png_alpha;)
160
51.5k
    else
161
51.5k
      GRAY_RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
162
64.4k
  } else
163
37.6k
#endif
164
37.6k
  if (source->png_bit_depth == 16) {
165
36.8k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
166
36.8k
    if (aindex >= 0)
167
7.37k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
168
36.8k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
169
36.8k
                         bufferptr += source->png_alpha;)
170
29.4k
    else
171
29.4k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
172
36.8k
                         bufferptr += source->png_alpha;)
173
36.8k
  } else {
174
750
    register unsigned char *bufferptr = source->iobuffer;
175
750
    if (aindex >= 0)
176
0
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
177
750
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
178
750
                         bufferptr += source->png_alpha;)
179
750
    else
180
750
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
181
750
                         bufferptr += source->png_alpha;)
182
750
  }
183
102k
  return 1;
184
102k
}
rdpng-8.c:get_gray_rgb_row
Line
Count
Source
141
102k
{
142
102k
  png_source_ptr source = (png_source_ptr)sinfo;
143
102k
  register _JSAMPROW ptr;
144
102k
  register _JSAMPLE *rescale = source->rescale;
145
102k
  JDIMENSION col;
146
102k
  register int rindex = rgb_red[cinfo->in_color_space];
147
102k
  register int gindex = rgb_green[cinfo->in_color_space];
148
102k
  register int bindex = rgb_blue[cinfo->in_color_space];
149
102k
  register int aindex = alpha_index[cinfo->in_color_space];
150
102k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
151
152
102k
  get_raw_row(cinfo, sinfo);
153
102k
  ptr = source->pub._buffer[0];
154
102k
#if BITS_IN_JSAMPLE != 12
155
102k
  if (source->png_bit_depth == cinfo->data_precision) {
156
64.4k
    _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
157
64.4k
    if (aindex >= 0)
158
12.8k
      GRAY_RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
159
64.4k
                         bufferptr += source->png_alpha;)
160
51.5k
    else
161
51.5k
      GRAY_RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
162
64.4k
  } else
163
37.6k
#endif
164
37.6k
  if (source->png_bit_depth == 16) {
165
36.8k
    register unsigned short *bufferptr = (unsigned short *)source->iobuffer;
166
36.8k
    if (aindex >= 0)
167
7.37k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
168
36.8k
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
169
36.8k
                         bufferptr += source->png_alpha;)
170
29.4k
    else
171
29.4k
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
172
36.8k
                         bufferptr += source->png_alpha;)
173
36.8k
  } else {
174
750
    register unsigned char *bufferptr = source->iobuffer;
175
750
    if (aindex >= 0)
176
0
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++],
177
750
                         ptr[aindex] = (1 << cinfo->data_precision) - 1;,
178
750
                         bufferptr += source->png_alpha;)
179
750
    else
180
750
      GRAY_RGB_READ_LOOP(rescale[*bufferptr++], {},
181
750
                         bufferptr += source->png_alpha;)
182
750
  }
183
102k
  return 1;
184
102k
}
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
20.4k
{
193
20.4k
  png_source_ptr source = (png_source_ptr)sinfo;
194
20.4k
  register _JSAMPROW ptr;
195
20.4k
  register _JSAMPLE *rescale = source->rescale;
196
20.4k
  JDIMENSION col;
197
198
20.4k
  get_raw_row(cinfo, sinfo);
199
20.4k
  ptr = source->pub._buffer[0];
200
#if BITS_IN_JSAMPLE != 12
201
20.4k
  if (source->png_bit_depth == cinfo->data_precision) {
202
12.8k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
203
176k
    for (col = cinfo->image_width; col > 0; col--) {
204
163k
      _JSAMPLE gray = *bufferptr++;
205
163k
      bufferptr += source->png_alpha;
206
163k
      rgb_to_cmyk(_MAXJSAMPLE, gray, gray, gray, ptr, ptr + 1, ptr + 2,
207
163k
                  ptr + 3);
208
163k
      ptr += 4;
209
163k
    }
210
12.8k
  } else
211
7.52k
#endif
212
7.52k
  if (source->png_bit_depth == 16) {
213
7.37k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
214
178k
    for (col = cinfo->image_width; col > 0; col--) {
215
170k
      _JSAMPLE gray = rescale[*bufferptr++];
216
170k
      bufferptr += source->png_alpha;
217
170k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
218
170k
                  ptr + 1, ptr + 2, ptr + 3);
219
170k
      ptr += 4;
220
170k
    }
221
7.37k
  } else {
222
150
    register unsigned char *bufferptr = source->iobuffer;
223
150
    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
150
  }
231
20.4k
  return 1;
232
20.4k
}
rdpng-8.c:get_gray_cmyk_row
Line
Count
Source
192
20.4k
{
193
20.4k
  png_source_ptr source = (png_source_ptr)sinfo;
194
20.4k
  register _JSAMPROW ptr;
195
20.4k
  register _JSAMPLE *rescale = source->rescale;
196
20.4k
  JDIMENSION col;
197
198
20.4k
  get_raw_row(cinfo, sinfo);
199
20.4k
  ptr = source->pub._buffer[0];
200
20.4k
#if BITS_IN_JSAMPLE != 12
201
20.4k
  if (source->png_bit_depth == cinfo->data_precision) {
202
12.8k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
203
176k
    for (col = cinfo->image_width; col > 0; col--) {
204
163k
      _JSAMPLE gray = *bufferptr++;
205
163k
      bufferptr += source->png_alpha;
206
163k
      rgb_to_cmyk(_MAXJSAMPLE, gray, gray, gray, ptr, ptr + 1, ptr + 2,
207
163k
                  ptr + 3);
208
163k
      ptr += 4;
209
163k
    }
210
12.8k
  } else
211
7.52k
#endif
212
7.52k
  if (source->png_bit_depth == 16) {
213
7.37k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
214
178k
    for (col = cinfo->image_width; col > 0; col--) {
215
170k
      _JSAMPLE gray = rescale[*bufferptr++];
216
170k
      bufferptr += source->png_alpha;
217
170k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, gray, gray, gray, ptr,
218
170k
                  ptr + 1, ptr + 2, ptr + 3);
219
170k
      ptr += 4;
220
170k
    }
221
7.37k
  } else {
222
150
    register unsigned char *bufferptr = source->iobuffer;
223
150
    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
150
  }
231
20.4k
  return 1;
232
20.4k
}
Unexecuted instantiation: rdpng-12.c:get_gray_cmyk_row
Unexecuted instantiation: rdpng-16.c:get_gray_cmyk_row
233
234
235
202k
#define RGB_READ_LOOP(read_op, alpha_set_op, pointer_op) { \
236
29.8M
  for (col = cinfo->image_width; col > 0; col--) { \
237
29.6M
    ptr[rindex] = read_op; \
238
29.6M
    ptr[gindex] = read_op; \
239
29.6M
    ptr[bindex] = read_op; \
240
29.6M
    alpha_set_op \
241
29.6M
    pointer_op \
242
29.6M
    ptr += ps; \
243
29.6M
  } \
244
202k
}
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
202k
{
253
202k
  png_source_ptr source = (png_source_ptr)sinfo;
254
202k
  register _JSAMPROW ptr;
255
202k
  register _JSAMPLE *rescale = source->rescale;
256
202k
  JDIMENSION col;
257
202k
  register int rindex = rgb_red[cinfo->in_color_space];
258
202k
  register int gindex = rgb_green[cinfo->in_color_space];
259
202k
  register int bindex = rgb_blue[cinfo->in_color_space];
260
202k
  register int aindex = alpha_index[cinfo->in_color_space];
261
202k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
202k
  get_raw_row(cinfo, sinfo);
264
202k
  ptr = source->pub._buffer[0];
265
#if BITS_IN_JSAMPLE != 12
266
202k
  if (source->png_bit_depth == cinfo->data_precision) {
267
177k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
268
177k
    if (aindex >= 0)
269
43.7k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
270
177k
                    bufferptr += source->png_alpha;)
271
133k
    else
272
133k
      RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
273
177k
  } else
274
25.2k
#endif
275
25.2k
  if (source->png_bit_depth == 16) {
276
23.7k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
277
23.7k
    if (aindex >= 0)
278
4.74k
      RGB_READ_LOOP(rescale[*bufferptr++],
279
23.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
280
23.7k
                    bufferptr += source->png_alpha;)
281
18.9k
    else
282
18.9k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
283
23.7k
                    bufferptr += source->png_alpha;)
284
23.7k
  } else {
285
1.52k
    register unsigned char *bufferptr = source->iobuffer;
286
1.52k
    if (aindex >= 0)
287
0
      RGB_READ_LOOP(rescale[*bufferptr++],
288
1.52k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
289
1.52k
                    bufferptr += source->png_alpha;)
290
1.52k
    else
291
1.52k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
292
1.52k
                    bufferptr += source->png_alpha;)
293
1.52k
  }
294
202k
  return 1;
295
202k
}
rdpng-8.c:get_rgb_row
Line
Count
Source
252
202k
{
253
202k
  png_source_ptr source = (png_source_ptr)sinfo;
254
202k
  register _JSAMPROW ptr;
255
202k
  register _JSAMPLE *rescale = source->rescale;
256
202k
  JDIMENSION col;
257
202k
  register int rindex = rgb_red[cinfo->in_color_space];
258
202k
  register int gindex = rgb_green[cinfo->in_color_space];
259
202k
  register int bindex = rgb_blue[cinfo->in_color_space];
260
202k
  register int aindex = alpha_index[cinfo->in_color_space];
261
202k
  register int ps = rgb_pixelsize[cinfo->in_color_space];
262
263
202k
  get_raw_row(cinfo, sinfo);
264
202k
  ptr = source->pub._buffer[0];
265
202k
#if BITS_IN_JSAMPLE != 12
266
202k
  if (source->png_bit_depth == cinfo->data_precision) {
267
177k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
268
177k
    if (aindex >= 0)
269
43.7k
      RGB_READ_LOOP(*bufferptr++, ptr[aindex] = _MAXJSAMPLE;,
270
177k
                    bufferptr += source->png_alpha;)
271
133k
    else
272
133k
      RGB_READ_LOOP(*bufferptr++, {}, bufferptr += source->png_alpha;)
273
177k
  } else
274
25.2k
#endif
275
25.2k
  if (source->png_bit_depth == 16) {
276
23.7k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
277
23.7k
    if (aindex >= 0)
278
4.74k
      RGB_READ_LOOP(rescale[*bufferptr++],
279
23.7k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
280
23.7k
                    bufferptr += source->png_alpha;)
281
18.9k
    else
282
18.9k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
283
23.7k
                    bufferptr += source->png_alpha;)
284
23.7k
  } else {
285
1.52k
    register unsigned char *bufferptr = source->iobuffer;
286
1.52k
    if (aindex >= 0)
287
0
      RGB_READ_LOOP(rescale[*bufferptr++],
288
1.52k
                    ptr[aindex] = (1 << cinfo->data_precision) - 1;,
289
1.52k
                    bufferptr += source->png_alpha;)
290
1.52k
    else
291
1.52k
      RGB_READ_LOOP(rescale[*bufferptr++], {},
292
1.52k
                    bufferptr += source->png_alpha;)
293
1.52k
  }
294
202k
  return 1;
295
202k
}
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
48.8k
{
304
48.8k
  png_source_ptr source = (png_source_ptr)sinfo;
305
48.8k
  register _JSAMPROW ptr;
306
48.8k
  register _JSAMPLE *rescale = source->rescale;
307
48.8k
  JDIMENSION col;
308
309
48.8k
  get_raw_row(cinfo, sinfo);
310
48.8k
  ptr = source->pub._buffer[0];
311
#if BITS_IN_JSAMPLE != 12
312
48.8k
  if (source->png_bit_depth == cinfo->data_precision) {
313
43.7k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
314
6.70M
    for (col = cinfo->image_width; col > 0; col--) {
315
6.66M
      _JSAMPLE r = *bufferptr++;
316
6.66M
      _JSAMPLE g = *bufferptr++;
317
6.66M
      _JSAMPLE b = *bufferptr++;
318
6.66M
      bufferptr += source->png_alpha;
319
6.66M
      rgb_to_cmyk(_MAXJSAMPLE, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
320
6.66M
      ptr += 4;
321
6.66M
    }
322
43.7k
  } else
323
5.08k
#endif
324
5.08k
  if (source->png_bit_depth == 16) {
325
4.74k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
326
479k
    for (col = cinfo->image_width; col > 0; col--) {
327
475k
      _JSAMPLE r = rescale[*bufferptr++];
328
475k
      _JSAMPLE g = rescale[*bufferptr++];
329
475k
      _JSAMPLE b = rescale[*bufferptr++];
330
475k
      bufferptr += source->png_alpha;
331
475k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
332
475k
                  ptr + 2, ptr + 3);
333
475k
      ptr += 4;
334
475k
    }
335
4.74k
  } else {
336
341
    register unsigned char *bufferptr = source->iobuffer;
337
341
    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
341
  }
347
48.8k
  return 1;
348
48.8k
}
rdpng-8.c:get_rgb_cmyk_row
Line
Count
Source
303
48.8k
{
304
48.8k
  png_source_ptr source = (png_source_ptr)sinfo;
305
48.8k
  register _JSAMPROW ptr;
306
48.8k
  register _JSAMPLE *rescale = source->rescale;
307
48.8k
  JDIMENSION col;
308
309
48.8k
  get_raw_row(cinfo, sinfo);
310
48.8k
  ptr = source->pub._buffer[0];
311
48.8k
#if BITS_IN_JSAMPLE != 12
312
48.8k
  if (source->png_bit_depth == cinfo->data_precision) {
313
43.7k
    register _JSAMPLE *bufferptr = (_JSAMPLE *)source->iobuffer;
314
6.70M
    for (col = cinfo->image_width; col > 0; col--) {
315
6.66M
      _JSAMPLE r = *bufferptr++;
316
6.66M
      _JSAMPLE g = *bufferptr++;
317
6.66M
      _JSAMPLE b = *bufferptr++;
318
6.66M
      bufferptr += source->png_alpha;
319
6.66M
      rgb_to_cmyk(_MAXJSAMPLE, r, g, b, ptr, ptr + 1, ptr + 2, ptr + 3);
320
6.66M
      ptr += 4;
321
6.66M
    }
322
43.7k
  } else
323
5.08k
#endif
324
5.08k
  if (source->png_bit_depth == 16) {
325
4.74k
    unsigned short *bufferptr = (unsigned short *)source->iobuffer;
326
479k
    for (col = cinfo->image_width; col > 0; col--) {
327
475k
      _JSAMPLE r = rescale[*bufferptr++];
328
475k
      _JSAMPLE g = rescale[*bufferptr++];
329
475k
      _JSAMPLE b = rescale[*bufferptr++];
330
475k
      bufferptr += source->png_alpha;
331
475k
      rgb_to_cmyk((1 << cinfo->data_precision) - 1, r, g, b, ptr, ptr + 1,
332
475k
                  ptr + 2, ptr + 3);
333
475k
      ptr += 4;
334
475k
    }
335
4.74k
  } else {
336
341
    register unsigned char *bufferptr = source->iobuffer;
337
341
    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
341
  }
347
48.8k
  return 1;
348
48.8k
}
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
494
{
354
/* This version is for reading 8-bit-per-channel indexed-color PNG files and
355
 * converting to extended RGB or CMYK.
356
 */
357
494
  png_source_ptr source = (png_source_ptr)sinfo;
358
494
  register _JSAMPROW ptr;
359
494
  register JSAMPLE *bufferptr = (JSAMPLE *)source->iobuffer;
360
494
  register _JSAMPLE *rescale = source->rescale;
361
494
  JDIMENSION col;
362
363
494
  get_raw_row(cinfo, sinfo);
364
494
  ptr = source->pub._buffer[0];
365
#if BITS_IN_JSAMPLE == 8
366
494
  if (source->png_bit_depth == cinfo->data_precision) {
367
436
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
368
283
      for (col = cinfo->image_width; col > 0; col--) {
369
249
        JSAMPLE index = *bufferptr++;
370
371
249
        if (index >= source->colormap.n_entries)
372
5
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
373
249
        *ptr++ = source->colormap.entries[index].red;
374
249
      }
375
402
    } else if (cinfo->in_color_space == JCS_CMYK) {
376
1.21k
      for (col = cinfo->image_width; col > 0; col--) {
377
1.14k
        JSAMPLE index = *bufferptr++;
378
379
1.14k
        if (index >= source->colormap.n_entries)
380
13
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
381
1.14k
        rgb_to_cmyk(_MAXJSAMPLE, source->colormap.entries[index].red,
382
1.14k
                    source->colormap.entries[index].green,
383
1.14k
                    source->colormap.entries[index].blue, ptr, ptr + 1,
384
1.14k
                    ptr + 2, ptr + 3);
385
1.14k
        ptr += 4;
386
1.14k
      }
387
335
    } else {
388
335
      register int rindex = rgb_red[cinfo->in_color_space];
389
335
      register int gindex = rgb_green[cinfo->in_color_space];
390
335
      register int bindex = rgb_blue[cinfo->in_color_space];
391
335
      register int aindex = alpha_index[cinfo->in_color_space];
392
335
      register int ps = rgb_pixelsize[cinfo->in_color_space];
393
394
6.08k
      for (col = cinfo->image_width; col > 0; col--) {
395
5.74k
        JSAMPLE index = *bufferptr++;
396
397
5.74k
        if (index >= source->colormap.n_entries)
398
65
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
399
5.74k
        ptr[rindex] = source->colormap.entries[index].red;
400
5.74k
        ptr[gindex] = source->colormap.entries[index].green;
401
5.74k
        ptr[bindex] = source->colormap.entries[index].blue;
402
5.74k
        if (aindex >= 0)
403
1.13k
          ptr[aindex] = _MAXJSAMPLE;
404
5.74k
        ptr += ps;
405
5.74k
      }
406
335
    }
407
436
  } else
408
58
#endif
409
58
  {
410
58
    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
58
    } 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
58
    } else {
432
58
      register int rindex = rgb_red[cinfo->in_color_space];
433
58
      register int gindex = rgb_green[cinfo->in_color_space];
434
58
      register int bindex = rgb_blue[cinfo->in_color_space];
435
58
      register int aindex = alpha_index[cinfo->in_color_space];
436
58
      register int ps = rgb_pixelsize[cinfo->in_color_space];
437
438
58
      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
58
    }
451
58
  }
452
494
  return 1;
453
494
}
rdpng-8.c:get_indexed_row
Line
Count
Source
353
494
{
354
/* This version is for reading 8-bit-per-channel indexed-color PNG files and
355
 * converting to extended RGB or CMYK.
356
 */
357
494
  png_source_ptr source = (png_source_ptr)sinfo;
358
494
  register _JSAMPROW ptr;
359
494
  register JSAMPLE *bufferptr = (JSAMPLE *)source->iobuffer;
360
494
  register _JSAMPLE *rescale = source->rescale;
361
494
  JDIMENSION col;
362
363
494
  get_raw_row(cinfo, sinfo);
364
494
  ptr = source->pub._buffer[0];
365
494
#if BITS_IN_JSAMPLE == 8
366
494
  if (source->png_bit_depth == cinfo->data_precision) {
367
436
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
368
283
      for (col = cinfo->image_width; col > 0; col--) {
369
249
        JSAMPLE index = *bufferptr++;
370
371
249
        if (index >= source->colormap.n_entries)
372
5
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
373
249
        *ptr++ = source->colormap.entries[index].red;
374
249
      }
375
402
    } else if (cinfo->in_color_space == JCS_CMYK) {
376
1.21k
      for (col = cinfo->image_width; col > 0; col--) {
377
1.14k
        JSAMPLE index = *bufferptr++;
378
379
1.14k
        if (index >= source->colormap.n_entries)
380
13
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
381
1.14k
        rgb_to_cmyk(_MAXJSAMPLE, source->colormap.entries[index].red,
382
1.14k
                    source->colormap.entries[index].green,
383
1.14k
                    source->colormap.entries[index].blue, ptr, ptr + 1,
384
1.14k
                    ptr + 2, ptr + 3);
385
1.14k
        ptr += 4;
386
1.14k
      }
387
335
    } else {
388
335
      register int rindex = rgb_red[cinfo->in_color_space];
389
335
      register int gindex = rgb_green[cinfo->in_color_space];
390
335
      register int bindex = rgb_blue[cinfo->in_color_space];
391
335
      register int aindex = alpha_index[cinfo->in_color_space];
392
335
      register int ps = rgb_pixelsize[cinfo->in_color_space];
393
394
6.08k
      for (col = cinfo->image_width; col > 0; col--) {
395
5.74k
        JSAMPLE index = *bufferptr++;
396
397
5.74k
        if (index >= source->colormap.n_entries)
398
65
          ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
399
5.74k
        ptr[rindex] = source->colormap.entries[index].red;
400
5.74k
        ptr[gindex] = source->colormap.entries[index].green;
401
5.74k
        ptr[bindex] = source->colormap.entries[index].blue;
402
5.74k
        if (aindex >= 0)
403
1.13k
          ptr[aindex] = _MAXJSAMPLE;
404
5.74k
        ptr += ps;
405
5.74k
      }
406
335
    }
407
436
  } else
408
58
#endif
409
58
  {
410
58
    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
58
    } 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
58
    } else {
432
58
      register int rindex = rgb_red[cinfo->in_color_space];
433
58
      register int gindex = rgb_green[cinfo->in_color_space];
434
58
      register int bindex = rgb_blue[cinfo->in_color_space];
435
58
      register int aindex = alpha_index[cinfo->in_color_space];
436
58
      register int ps = rgb_pixelsize[cinfo->in_color_space];
437
438
58
      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
58
    }
451
58
  }
452
494
  return 1;
453
494
}
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
30.2k
{
473
30.2k
  png_source_ptr source = (png_source_ptr)sinfo;
474
30.2k
  struct spng_ihdr ihdr;
475
30.2k
  int png_components = 3;
476
30.2k
  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
30.2k
  source->ctx = spng_ctx_new(0);
487
30.2k
#endif
488
30.2k
  if (!source->ctx)
489
0
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, "Could not create context");
490
491
30.2k
  TRY_SPNG(spng_set_png_file(source->ctx, sinfo->input_file));
492
493
30.2k
  TRY_SPNG(spng_get_ihdr(source->ctx, &ihdr));
494
495
30.2k
  if (ihdr.width > JPEG_MAX_DIMENSION || ihdr.height > JPEG_MAX_DIMENSION)
496
1.63k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
497
30.2k
  if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16)
498
5.64k
    ERREXIT(cinfo, JERR_PNG_BADDEPTH);
499
30.2k
  if (sinfo->max_pixels &&
500
21.7k
      (unsigned long long)ihdr.width * ihdr.height > sinfo->max_pixels)
501
266
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
502
503
30.2k
  cinfo->image_width = (JDIMENSION)ihdr.width;
504
30.2k
  cinfo->image_height = (JDIMENSION)ihdr.height;
505
30.2k
  source->png_bit_depth = ihdr.bit_depth;
506
30.2k
  source->png_color_type = ihdr.color_type;
507
508
  /* initialize flags to most common settings */
509
30.2k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
510
511
30.2k
  switch (ihdr.color_type) {
512
1.56k
  case SPNG_COLOR_TYPE_GRAYSCALE_ALPHA:
513
4.55k
  case SPNG_COLOR_TYPE_GRAYSCALE:
514
4.55k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
515
4.55k
        cinfo->in_color_space == JCS_RGB)
516
0
      cinfo->in_color_space = JCS_GRAYSCALE;
517
4.55k
    TRACEMS3(cinfo, 1, JTRC_PNG_GRAYSCALE, ihdr.width, ihdr.height,
518
4.55k
             ihdr.bit_depth);
519
4.55k
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
520
651
      if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE &&
521
427
          cinfo->data_precision == ihdr.bit_depth) {
522
282
        source->pub.get_pixel_rows = get_raw_row;
523
282
        use_raw_buffer = TRUE;
524
282
      } else
525
369
        source->pub.get_pixel_rows = get_gray_row;
526
3.90k
    } else if (IsExtRGB(cinfo->in_color_space))
527
3.25k
      source->pub.get_pixel_rows = get_gray_rgb_row;
528
651
    else if (cinfo->in_color_space == JCS_CMYK)
529
651
        source->pub.get_pixel_rows = get_gray_cmyk_row;
530
0
    else
531
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532
4.55k
    if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) {
533
1.56k
      png_components = 2;
534
1.56k
      source->png_alpha = 1;
535
2.98k
    } else {
536
2.98k
      png_components = 1;
537
2.98k
      source->png_alpha = 0;
538
2.98k
    }
539
4.55k
    break;
540
541
11.2k
  case SPNG_COLOR_TYPE_TRUECOLOR:
542
15.7k
  case SPNG_COLOR_TYPE_TRUECOLOR_ALPHA:
543
15.7k
    if (cinfo->in_color_space == JCS_UNKNOWN)
544
0
      cinfo->in_color_space = JCS_EXT_RGB;
545
15.7k
    TRACEMS3(cinfo, 1, JTRC_PNG_TRUECOLOR, ihdr.width, ihdr.height,
546
15.7k
             ihdr.bit_depth);
547
15.7k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR &&
548
11.2k
        cinfo->data_precision == ihdr.bit_depth &&
549
8.79k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
550
8.79k
        (cinfo->in_color_space == JCS_EXT_RGB ||
551
7.53k
         cinfo->in_color_space == JCS_RGB)) {
552
#else
553
        cinfo->in_color_space == JCS_EXT_RGB) {
554
#endif
555
1.25k
      source->pub.get_pixel_rows = get_raw_row;
556
1.25k
      use_raw_buffer = TRUE;
557
14.4k
    } else if (IsExtRGB(cinfo->in_color_space))
558
9.97k
      source->pub.get_pixel_rows = get_rgb_row;
559
4.49k
    else if (cinfo->in_color_space == JCS_CMYK)
560
2.24k
      source->pub.get_pixel_rows = get_rgb_cmyk_row;
561
2.24k
    else
562
2.24k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563
15.7k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA) {
564
3.81k
      png_components = 4;
565
3.81k
      source->png_alpha = 1;
566
11.9k
    } else {
567
11.9k
      png_components = 3;
568
11.9k
      source->png_alpha = 0;
569
11.9k
    }
570
15.7k
    break;
571
572
1.16k
  case SPNG_COLOR_TYPE_INDEXED:
573
1.16k
  {
574
1.16k
    int i, gray = 1;
575
576
1.16k
    TRACEMS3(cinfo, 1, JTRC_PNG_INDEXED, ihdr.width, ihdr.height,
577
1.16k
             ihdr.bit_depth);
578
1.16k
    TRY_SPNG(spng_get_plte(source->ctx, &source->colormap));
579
1.16k
    if (source->png_bit_depth != 8 || source->colormap.n_entries > 256)
580
0
      ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
581
582
12.5k
    for (i = 0; i < (int)source->colormap.n_entries; i++) {
583
11.3k
      if (source->colormap.entries[i].red !=
584
11.3k
          source->colormap.entries[i].green ||
585
5.58k
          source->colormap.entries[i].green !=
586
5.58k
          source->colormap.entries[i].blue)
587
6.27k
        gray = 0;
588
11.3k
    }
589
590
1.16k
    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.16k
    else if (cinfo->in_color_space == JCS_UNKNOWN)
594
0
      cinfo->in_color_space = JCS_EXT_RGB;
595
1.16k
    if (cinfo->in_color_space == JCS_GRAYSCALE && !gray)
596
27
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
597
598
1.16k
    source->pub.get_pixel_rows = get_indexed_row;
599
1.16k
    png_components = 1;
600
1.16k
    source->png_alpha = 0;
601
1.16k
    break;
602
11.2k
  }
603
604
0
  default:
605
0
    ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
606
30.2k
  }
607
608
18.2k
  if (IsExtRGB(cinfo->in_color_space))
609
14.6k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
610
3.59k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
611
663
    cinfo->input_components = 1;
612
2.93k
  else if (cinfo->in_color_space == JCS_CMYK)
613
2.93k
    cinfo->input_components = 4;
614
615
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
616
18.2k
  source->buffer_width =
617
18.2k
    (size_t)ihdr.width * png_components * source->png_bit_depth / 8;
618
18.2k
  source->iobuffer = (unsigned char *)
619
18.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
620
18.2k
                                source->buffer_width);
621
622
  /* Create compressor input buffer. */
623
18.2k
  if (use_raw_buffer) {
624
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
625
    /* Synthesize a _JSAMPARRAY pointer structure */
626
1.53k
    source->pixrow = (_JSAMPROW)source->iobuffer;
627
1.53k
    source->pub._buffer = &source->pixrow;
628
1.53k
    source->pub.buffer_height = 1;
629
16.7k
  } else {
630
16.7k
    unsigned int maxval = source->png_bit_depth == 16 ? 65535 : 255;
631
16.7k
    size_t val, half_maxval;
632
633
    /* Need to translate anyway, so make a separate sample buffer. */
634
16.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
635
16.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
636
16.7k
       (JDIMENSION)ihdr.width * cinfo->input_components, (JDIMENSION)1);
637
16.7k
    source->pub.buffer_height = 1;
638
639
    /* Compute the rescaling array. */
640
16.7k
    source->rescale = (_JSAMPLE *)
641
16.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
642
16.7k
                                  (maxval + 1L) * sizeof(_JSAMPLE));
643
16.7k
    memset(source->rescale, 0, (maxval + 1L) * sizeof(_JSAMPLE));
644
16.7k
    half_maxval = maxval / 2;
645
353M
    for (val = 0; val <= maxval; val++) {
646
      /* The multiplication here must be done in 32 bits to avoid overflow */
647
353M
      source->rescale[val] =
648
353M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
649
353M
                   maxval);
650
353M
    }
651
16.7k
  }
652
653
18.2k
  TRY_SPNG(spng_decode_image(source->ctx, NULL, 0, SPNG_FMT_PNG,
654
18.2k
                             SPNG_DECODE_PROGRESSIVE));
655
18.2k
}
rdpng-8.c:start_input_png
Line
Count
Source
472
30.2k
{
473
30.2k
  png_source_ptr source = (png_source_ptr)sinfo;
474
30.2k
  struct spng_ihdr ihdr;
475
30.2k
  int png_components = 3;
476
30.2k
  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
30.2k
  source->ctx = spng_ctx_new(0);
487
30.2k
#endif
488
30.2k
  if (!source->ctx)
489
0
    ERREXITS(cinfo, JERR_PNG_LIBSPNG, "Could not create context");
490
491
30.2k
  TRY_SPNG(spng_set_png_file(source->ctx, sinfo->input_file));
492
493
30.2k
  TRY_SPNG(spng_get_ihdr(source->ctx, &ihdr));
494
495
30.2k
  if (ihdr.width > JPEG_MAX_DIMENSION || ihdr.height > JPEG_MAX_DIMENSION)
496
1.63k
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, JPEG_MAX_DIMENSION);
497
30.2k
  if (ihdr.bit_depth != 8 && ihdr.bit_depth != 16)
498
5.64k
    ERREXIT(cinfo, JERR_PNG_BADDEPTH);
499
30.2k
  if (sinfo->max_pixels &&
500
21.7k
      (unsigned long long)ihdr.width * ihdr.height > sinfo->max_pixels)
501
266
    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, sinfo->max_pixels);
502
503
30.2k
  cinfo->image_width = (JDIMENSION)ihdr.width;
504
30.2k
  cinfo->image_height = (JDIMENSION)ihdr.height;
505
30.2k
  source->png_bit_depth = ihdr.bit_depth;
506
30.2k
  source->png_color_type = ihdr.color_type;
507
508
  /* initialize flags to most common settings */
509
30.2k
  use_raw_buffer = FALSE;       /* do we map input buffer onto I/O buffer? */
510
511
30.2k
  switch (ihdr.color_type) {
512
1.56k
  case SPNG_COLOR_TYPE_GRAYSCALE_ALPHA:
513
4.55k
  case SPNG_COLOR_TYPE_GRAYSCALE:
514
4.55k
    if (cinfo->in_color_space == JCS_UNKNOWN ||
515
4.55k
        cinfo->in_color_space == JCS_RGB)
516
0
      cinfo->in_color_space = JCS_GRAYSCALE;
517
4.55k
    TRACEMS3(cinfo, 1, JTRC_PNG_GRAYSCALE, ihdr.width, ihdr.height,
518
4.55k
             ihdr.bit_depth);
519
4.55k
    if (cinfo->in_color_space == JCS_GRAYSCALE) {
520
651
      if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE &&
521
427
          cinfo->data_precision == ihdr.bit_depth) {
522
282
        source->pub.get_pixel_rows = get_raw_row;
523
282
        use_raw_buffer = TRUE;
524
282
      } else
525
369
        source->pub.get_pixel_rows = get_gray_row;
526
3.90k
    } else if (IsExtRGB(cinfo->in_color_space))
527
3.25k
      source->pub.get_pixel_rows = get_gray_rgb_row;
528
651
    else if (cinfo->in_color_space == JCS_CMYK)
529
651
        source->pub.get_pixel_rows = get_gray_cmyk_row;
530
0
    else
531
0
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
532
4.55k
    if (ihdr.color_type == SPNG_COLOR_TYPE_GRAYSCALE_ALPHA) {
533
1.56k
      png_components = 2;
534
1.56k
      source->png_alpha = 1;
535
2.98k
    } else {
536
2.98k
      png_components = 1;
537
2.98k
      source->png_alpha = 0;
538
2.98k
    }
539
4.55k
    break;
540
541
11.2k
  case SPNG_COLOR_TYPE_TRUECOLOR:
542
15.7k
  case SPNG_COLOR_TYPE_TRUECOLOR_ALPHA:
543
15.7k
    if (cinfo->in_color_space == JCS_UNKNOWN)
544
0
      cinfo->in_color_space = JCS_EXT_RGB;
545
15.7k
    TRACEMS3(cinfo, 1, JTRC_PNG_TRUECOLOR, ihdr.width, ihdr.height,
546
15.7k
             ihdr.bit_depth);
547
15.7k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR &&
548
11.2k
        cinfo->data_precision == ihdr.bit_depth &&
549
8.79k
#if RGB_RED == 0 && RGB_GREEN == 1 && RGB_BLUE == 2 && RGB_PIXELSIZE == 3
550
8.79k
        (cinfo->in_color_space == JCS_EXT_RGB ||
551
7.53k
         cinfo->in_color_space == JCS_RGB)) {
552
#else
553
        cinfo->in_color_space == JCS_EXT_RGB) {
554
#endif
555
1.25k
      source->pub.get_pixel_rows = get_raw_row;
556
1.25k
      use_raw_buffer = TRUE;
557
14.4k
    } else if (IsExtRGB(cinfo->in_color_space))
558
9.97k
      source->pub.get_pixel_rows = get_rgb_row;
559
4.49k
    else if (cinfo->in_color_space == JCS_CMYK)
560
2.24k
      source->pub.get_pixel_rows = get_rgb_cmyk_row;
561
2.24k
    else
562
2.24k
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
563
15.7k
    if (ihdr.color_type == SPNG_COLOR_TYPE_TRUECOLOR_ALPHA) {
564
3.81k
      png_components = 4;
565
3.81k
      source->png_alpha = 1;
566
11.9k
    } else {
567
11.9k
      png_components = 3;
568
11.9k
      source->png_alpha = 0;
569
11.9k
    }
570
15.7k
    break;
571
572
1.16k
  case SPNG_COLOR_TYPE_INDEXED:
573
1.16k
  {
574
1.16k
    int i, gray = 1;
575
576
1.16k
    TRACEMS3(cinfo, 1, JTRC_PNG_INDEXED, ihdr.width, ihdr.height,
577
1.16k
             ihdr.bit_depth);
578
1.16k
    TRY_SPNG(spng_get_plte(source->ctx, &source->colormap));
579
1.16k
    if (source->png_bit_depth != 8 || source->colormap.n_entries > 256)
580
0
      ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
581
582
12.5k
    for (i = 0; i < (int)source->colormap.n_entries; i++) {
583
11.3k
      if (source->colormap.entries[i].red !=
584
11.3k
          source->colormap.entries[i].green ||
585
5.58k
          source->colormap.entries[i].green !=
586
5.58k
          source->colormap.entries[i].blue)
587
6.27k
        gray = 0;
588
11.3k
    }
589
590
1.16k
    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.16k
    else if (cinfo->in_color_space == JCS_UNKNOWN)
594
0
      cinfo->in_color_space = JCS_EXT_RGB;
595
1.16k
    if (cinfo->in_color_space == JCS_GRAYSCALE && !gray)
596
27
      ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
597
598
1.16k
    source->pub.get_pixel_rows = get_indexed_row;
599
1.16k
    png_components = 1;
600
1.16k
    source->png_alpha = 0;
601
1.16k
    break;
602
11.2k
  }
603
604
0
  default:
605
0
    ERREXIT(cinfo, JERR_PNG_OUTOFRANGE);
606
30.2k
  }
607
608
18.2k
  if (IsExtRGB(cinfo->in_color_space))
609
14.6k
    cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
610
3.59k
  else if (cinfo->in_color_space == JCS_GRAYSCALE)
611
663
    cinfo->input_components = 1;
612
2.93k
  else if (cinfo->in_color_space == JCS_CMYK)
613
2.93k
    cinfo->input_components = 4;
614
615
  /* Allocate space for I/O buffer: 1 or 3 bytes or words/pixel. */
616
18.2k
  source->buffer_width =
617
18.2k
    (size_t)ihdr.width * png_components * source->png_bit_depth / 8;
618
18.2k
  source->iobuffer = (unsigned char *)
619
18.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
620
18.2k
                                source->buffer_width);
621
622
  /* Create compressor input buffer. */
623
18.2k
  if (use_raw_buffer) {
624
    /* For unscaled raw-input case, we can just map it onto the I/O buffer. */
625
    /* Synthesize a _JSAMPARRAY pointer structure */
626
1.53k
    source->pixrow = (_JSAMPROW)source->iobuffer;
627
1.53k
    source->pub._buffer = &source->pixrow;
628
1.53k
    source->pub.buffer_height = 1;
629
16.7k
  } else {
630
16.7k
    unsigned int maxval = source->png_bit_depth == 16 ? 65535 : 255;
631
16.7k
    size_t val, half_maxval;
632
633
    /* Need to translate anyway, so make a separate sample buffer. */
634
16.7k
    source->pub._buffer = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
635
16.7k
      ((j_common_ptr)cinfo, JPOOL_IMAGE,
636
16.7k
       (JDIMENSION)ihdr.width * cinfo->input_components, (JDIMENSION)1);
637
16.7k
    source->pub.buffer_height = 1;
638
639
    /* Compute the rescaling array. */
640
16.7k
    source->rescale = (_JSAMPLE *)
641
16.7k
      (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
642
16.7k
                                  (maxval + 1L) * sizeof(_JSAMPLE));
643
16.7k
    memset(source->rescale, 0, (maxval + 1L) * sizeof(_JSAMPLE));
644
16.7k
    half_maxval = maxval / 2;
645
353M
    for (val = 0; val <= maxval; val++) {
646
      /* The multiplication here must be done in 32 bits to avoid overflow */
647
353M
      source->rescale[val] =
648
353M
        (_JSAMPLE)((val * ((1 << cinfo->data_precision) - 1) + half_maxval) /
649
353M
                   maxval);
650
353M
    }
651
16.7k
  }
652
653
18.2k
  TRY_SPNG(spng_decode_image(source->ctx, NULL, 0, SPNG_FMT_PNG,
654
18.2k
                             SPNG_DECODE_PROGRESSIVE));
655
18.2k
}
Unexecuted instantiation: rdpng-12.c:start_input_png
Unexecuted instantiation: rdpng-16.c:start_input_png
656
657
658
/*
659
 * Return the ICC profile (if any) embedded in the PNG image.
660
 */
661
662
METHODDEF(boolean)
663
read_icc_profile_png(j_compress_ptr cinfo, cjpeg_source_ptr sinfo,
664
                     JOCTET **icc_data_ptr, unsigned int *icc_data_len)
665
6.00k
{
666
6.00k
  png_source_ptr source = (png_source_ptr)sinfo;
667
6.00k
  struct spng_iccp iccp;
668
669
6.00k
  if (!icc_data_ptr || !icc_data_len)
670
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
671
672
6.00k
  if (source->ctx && spng_get_iccp(source->ctx, &iccp) == 0) {
673
6
    *icc_data_ptr = (JOCTET *)iccp.profile;
674
6
    *icc_data_len = (unsigned int)iccp.profile_len;
675
6
    return TRUE;
676
6
  }
677
678
6.00k
  return FALSE;
679
6.00k
}
rdpng-8.c:read_icc_profile_png
Line
Count
Source
665
6.00k
{
666
6.00k
  png_source_ptr source = (png_source_ptr)sinfo;
667
6.00k
  struct spng_iccp iccp;
668
669
6.00k
  if (!icc_data_ptr || !icc_data_len)
670
0
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
671
672
6.00k
  if (source->ctx && spng_get_iccp(source->ctx, &iccp) == 0) {
673
6
    *icc_data_ptr = (JOCTET *)iccp.profile;
674
6
    *icc_data_len = (unsigned int)iccp.profile_len;
675
6
    return TRUE;
676
6
  }
677
678
6.00k
  return FALSE;
679
6.00k
}
Unexecuted instantiation: rdpng-12.c:read_icc_profile_png
Unexecuted instantiation: rdpng-16.c:read_icc_profile_png
680
681
682
/*
683
 * Finish up at the end of the file.
684
 */
685
686
METHODDEF(void)
687
finish_input_png(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
688
30.2k
{
689
30.2k
  png_source_ptr source = (png_source_ptr)sinfo;
690
691
30.2k
  if (source->ctx) {
692
30.2k
    spng_decode_chunks(source->ctx);
693
30.2k
    spng_ctx_free(source->ctx);
694
30.2k
    source->ctx = NULL;
695
30.2k
  }
696
30.2k
}
rdpng-8.c:finish_input_png
Line
Count
Source
688
30.2k
{
689
30.2k
  png_source_ptr source = (png_source_ptr)sinfo;
690
691
30.2k
  if (source->ctx) {
692
30.2k
    spng_decode_chunks(source->ctx);
693
30.2k
    spng_ctx_free(source->ctx);
694
    source->ctx = NULL;
695
30.2k
  }
696
30.2k
}
Unexecuted instantiation: rdpng-12.c:finish_input_png
Unexecuted instantiation: rdpng-16.c:finish_input_png
697
698
699
/*
700
 * The module selection routine for PNG format input.
701
 */
702
703
GLOBAL(cjpeg_source_ptr)
704
_jinit_read_png(j_compress_ptr cinfo)
705
30.2k
{
706
30.2k
  png_source_ptr source;
707
708
#if BITS_IN_JSAMPLE == 8
709
30.2k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
710
#else
711
0
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
712
0
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
713
0
#endif
714
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
715
716
  /* Create module interface object */
717
30.2k
  source = (png_source_ptr)
718
30.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
719
30.2k
                                sizeof(png_source_struct));
720
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
721
30.2k
  source->pub.start_input = start_input_png;
722
30.2k
  source->pub.read_icc_profile = read_icc_profile_png;
723
30.2k
  source->pub.finish_input = finish_input_png;
724
30.2k
  source->pub.max_pixels = 0;
725
726
30.2k
  return (cjpeg_source_ptr)source;
727
30.2k
}
jinit_read_png
Line
Count
Source
705
30.2k
{
706
30.2k
  png_source_ptr source;
707
708
30.2k
#if BITS_IN_JSAMPLE == 8
709
30.2k
  if (cinfo->data_precision > BITS_IN_JSAMPLE || cinfo->data_precision < 2)
710
#else
711
  if (cinfo->data_precision > BITS_IN_JSAMPLE ||
712
      cinfo->data_precision < BITS_IN_JSAMPLE - 3)
713
#endif
714
0
    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
715
716
  /* Create module interface object */
717
30.2k
  source = (png_source_ptr)
718
30.2k
    (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
719
30.2k
                                sizeof(png_source_struct));
720
  /* Fill in method ptrs, except get_pixel_rows which start_input sets */
721
30.2k
  source->pub.start_input = start_input_png;
722
30.2k
  source->pub.read_icc_profile = read_icc_profile_png;
723
30.2k
  source->pub.finish_input = finish_input_png;
724
30.2k
  source->pub.max_pixels = 0;
725
726
30.2k
  return (cjpeg_source_ptr)source;
727
30.2k
}
Unexecuted instantiation: j12init_read_png
Unexecuted instantiation: j16init_read_png
728
729
#endif /* defined(PNG_SUPPORTED) &&
730
          (BITS_IN_JSAMPLE != 16 || defined(C_LOSSLESS_SUPPORTED)) */