Line | Count | Source |
1 | | /* pngread.c - read a PNG file |
2 | | * |
3 | | * Copyright (c) 2018-2025 Cosmin Truta |
4 | | * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson |
5 | | * Copyright (c) 1996-1997 Andreas Dilger |
6 | | * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. |
7 | | * |
8 | | * This code is released under the libpng license. |
9 | | * For conditions of distribution and use, see the disclaimer |
10 | | * and license in png.h |
11 | | * |
12 | | * This file contains routines that an application calls directly to |
13 | | * read a PNG file or stream. |
14 | | */ |
15 | | |
16 | | #include "pngpriv.h" |
17 | | #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) |
18 | | # include <errno.h> |
19 | | #endif |
20 | | |
21 | | #ifdef PNG_READ_SUPPORTED |
22 | | |
23 | | /* Create a PNG structure for reading, and allocate any memory needed. */ |
24 | | PNG_FUNCTION(png_struct *, |
25 | | png_create_read_struct,(const char *user_png_ver, void *error_ptr, |
26 | | png_error_ptr error_fn, png_error_ptr warn_fn), |
27 | | PNG_ALLOCATED) |
28 | 0 | { |
29 | | #ifndef PNG_USER_MEM_SUPPORTED |
30 | | png_struct *png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
31 | | error_fn, warn_fn, NULL, NULL, NULL); |
32 | | #else |
33 | 0 | return png_create_read_struct_2(user_png_ver, error_ptr, error_fn, |
34 | 0 | warn_fn, NULL, NULL, NULL); |
35 | 0 | } |
36 | | |
37 | | /* Alternate create PNG structure for reading, and allocate any memory |
38 | | * needed. |
39 | | */ |
40 | | PNG_FUNCTION(png_struct *, |
41 | | png_create_read_struct_2,(const char *user_png_ver, void *error_ptr, |
42 | | png_error_ptr error_fn, png_error_ptr warn_fn, void *mem_ptr, |
43 | | png_malloc_ptr malloc_fn, png_free_ptr free_fn), |
44 | | PNG_ALLOCATED) |
45 | 93.6k | { |
46 | 93.6k | png_struct *png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
47 | 93.6k | error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); |
48 | 93.6k | #endif /* USER_MEM */ |
49 | | |
50 | 93.6k | if (png_ptr != NULL) |
51 | 93.6k | { |
52 | 93.6k | png_ptr->mode = PNG_IS_READ_STRUCT; |
53 | | |
54 | | /* Added in libpng-1.6.0; this can be used to detect a read structure if |
55 | | * required (it will be zero in a write structure.) |
56 | | */ |
57 | 93.6k | # ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
58 | 93.6k | png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; |
59 | 93.6k | # endif |
60 | | |
61 | 93.6k | # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED |
62 | 93.6k | png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; |
63 | | |
64 | | /* In stable builds only warn if an application error can be completely |
65 | | * handled. |
66 | | */ |
67 | | # if PNG_RELEASE_BUILD |
68 | | png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; |
69 | | # endif |
70 | 93.6k | # endif |
71 | | |
72 | 93.6k | # ifdef PNG_TARGET_CODE_IMPLEMENTATION /* target specific code */ |
73 | | /* Current support is read-only so this happens here, not in the |
74 | | * general creation. It could easily be moved. |
75 | | */ |
76 | 93.6k | png_target_init(png_ptr); |
77 | 93.6k | if (png_ptr->target_state != 0U) |
78 | 93.6k | png_set_option(png_ptr, PNG_TARGET_SPECIFIC_CODE, 1); |
79 | 93.6k | # endif |
80 | | |
81 | | /* TODO: delay this, it can be done in png_init_io (if the app doesn't |
82 | | * do it itself) avoiding setting the default function if it is not |
83 | | * required. |
84 | | */ |
85 | 93.6k | png_set_read_fn(png_ptr, NULL, NULL); |
86 | 93.6k | } |
87 | | |
88 | 93.6k | return png_ptr; |
89 | 93.6k | } |
90 | | |
91 | | |
92 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
93 | | /* Read the information before the actual image data. This has been |
94 | | * changed in v0.90 to allow reading a file that already has the magic |
95 | | * bytes read from the stream. You can tell libpng how many bytes have |
96 | | * been read from the beginning of the stream (up to the maximum of 8) |
97 | | * via png_set_sig_bytes(), and we will only check the remaining bytes |
98 | | * here. The application can then have access to the signature bytes we |
99 | | * read if it is determined that this isn't a valid PNG file. |
100 | | */ |
101 | | void |
102 | | png_read_info(png_struct *png_ptr, png_info *info_ptr) |
103 | 93.6k | { |
104 | 93.6k | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
105 | 93.6k | int keep; |
106 | 93.6k | #endif |
107 | | |
108 | 93.6k | png_debug(1, "in png_read_info"); |
109 | | |
110 | 93.6k | if (png_ptr == NULL || info_ptr == NULL) |
111 | 0 | return; |
112 | | |
113 | | /* Read and check the PNG file signature. */ |
114 | 93.6k | png_read_sig(png_ptr, info_ptr); |
115 | | |
116 | 93.6k | for (;;) |
117 | 661k | { |
118 | 661k | png_uint_32 length = png_read_chunk_header(png_ptr); |
119 | 661k | png_uint_32 chunk_name = png_ptr->chunk_name; |
120 | | |
121 | | /* IDAT logic needs to happen here to simplify getting the two flags |
122 | | * right. |
123 | | */ |
124 | 661k | if (chunk_name == png_IDAT) |
125 | 50.4k | { |
126 | 50.4k | if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) |
127 | 18 | png_chunk_error(png_ptr, "Missing IHDR before IDAT"); |
128 | | |
129 | 50.4k | else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
130 | 1.52k | (png_ptr->mode & PNG_HAVE_PLTE) == 0) |
131 | 10 | png_chunk_error(png_ptr, "Missing PLTE before IDAT"); |
132 | | |
133 | 50.3k | else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) |
134 | 0 | png_chunk_benign_error(png_ptr, "Too many IDATs found"); |
135 | | |
136 | 50.3k | png_ptr->mode |= PNG_HAVE_IDAT; |
137 | 50.3k | } |
138 | | |
139 | 610k | else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
140 | 0 | { |
141 | 0 | png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; |
142 | 0 | png_ptr->mode |= PNG_AFTER_IDAT; |
143 | 0 | } |
144 | | |
145 | 661k | if (chunk_name == png_IHDR) |
146 | 76.1k | png_handle_chunk(png_ptr, info_ptr, length); |
147 | | |
148 | 584k | else if (chunk_name == png_IEND) |
149 | 62 | png_handle_chunk(png_ptr, info_ptr, length); |
150 | | |
151 | 584k | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
152 | 584k | else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) |
153 | 176k | { |
154 | 176k | png_handle_unknown(png_ptr, info_ptr, length, keep); |
155 | | |
156 | 176k | if (chunk_name == png_PLTE) |
157 | 0 | png_ptr->mode |= PNG_HAVE_PLTE; |
158 | | |
159 | 176k | else if (chunk_name == png_IDAT) |
160 | 0 | { |
161 | 0 | png_ptr->idat_size = 0; /* It has been consumed */ |
162 | 0 | break; |
163 | 0 | } |
164 | 176k | } |
165 | 408k | #endif |
166 | | |
167 | 408k | else if (chunk_name == png_IDAT) |
168 | 50.3k | { |
169 | 50.3k | #ifdef PNG_READ_APNG_SUPPORTED |
170 | 50.3k | png_have_info(png_ptr, info_ptr); |
171 | 50.3k | #endif |
172 | 50.3k | png_ptr->idat_size = length; |
173 | 50.3k | break; |
174 | 50.3k | } |
175 | | |
176 | 357k | #ifdef PNG_READ_APNG_SUPPORTED |
177 | 357k | else if (chunk_name == png_acTL) |
178 | 0 | png_handle_acTL(png_ptr, info_ptr, length); |
179 | | |
180 | 357k | else if (chunk_name == png_fcTL) |
181 | 0 | png_handle_fcTL(png_ptr, info_ptr, length); |
182 | | |
183 | 357k | else if (chunk_name == png_fdAT) |
184 | 0 | png_handle_fdAT(png_ptr, info_ptr, length); |
185 | 357k | #endif |
186 | | |
187 | 357k | else |
188 | 357k | png_handle_chunk(png_ptr, info_ptr, length); |
189 | 661k | } |
190 | 93.6k | } |
191 | | #endif /* SEQUENTIAL_READ */ |
192 | | |
193 | | #ifdef PNG_READ_APNG_SUPPORTED |
194 | | void PNGAPI |
195 | | png_read_frame_head(png_struct *png_ptr, png_info *info_ptr) |
196 | 0 | { |
197 | 0 | png_byte have_chunk_after_DAT; /* after IDAT or after fdAT */ |
198 | |
|
199 | 0 | png_debug(1, "Reading frame head"); |
200 | |
|
201 | 0 | if (!(png_ptr->mode & PNG_HAVE_acTL)) |
202 | 0 | png_error(png_ptr, "Cannot read APNG frame: missing acTL"); |
203 | | |
204 | | /* Do nothing for the main IDAT. */ |
205 | 0 | if (png_ptr->num_frames_read == 0) |
206 | 0 | return; |
207 | | |
208 | 0 | png_read_reset(png_ptr); |
209 | 0 | png_ptr->flags &= ~PNG_FLAG_ROW_INIT; |
210 | 0 | png_ptr->mode &= ~PNG_HAVE_fcTL; |
211 | |
|
212 | 0 | have_chunk_after_DAT = 0; |
213 | 0 | for (;;) |
214 | 0 | { |
215 | 0 | png_uint_32 length = png_read_chunk_header(png_ptr); |
216 | |
|
217 | 0 | if (png_ptr->chunk_name == png_IDAT) |
218 | 0 | { |
219 | | /* Discard trailing IDATs for the first frame. */ |
220 | 0 | if (have_chunk_after_DAT || png_ptr->num_frames_read > 1) |
221 | 0 | png_error(png_ptr, "Misplaced IDAT in APNG stream"); |
222 | 0 | png_crc_finish(png_ptr, length); |
223 | 0 | } |
224 | 0 | else if (png_ptr->chunk_name == png_fcTL) |
225 | 0 | { |
226 | 0 | png_handle_fcTL(png_ptr, info_ptr, length); |
227 | 0 | have_chunk_after_DAT = 1; |
228 | 0 | } |
229 | 0 | else if (png_ptr->chunk_name == png_fdAT) |
230 | 0 | { |
231 | 0 | png_ensure_sequence_number(png_ptr, length); |
232 | | |
233 | | /* Discard trailing fdATs for all frames except the first. */ |
234 | 0 | if (!have_chunk_after_DAT && png_ptr->num_frames_read > 1) |
235 | 0 | { |
236 | 0 | png_crc_finish(png_ptr, length - 4); |
237 | 0 | } |
238 | 0 | else if (png_ptr->mode & PNG_HAVE_fcTL) |
239 | 0 | { |
240 | 0 | png_ptr->idat_size = length - 4; |
241 | 0 | png_ptr->mode |= PNG_HAVE_IDAT; |
242 | 0 | break; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | { |
246 | 0 | png_error(png_ptr, "Misplaced fdAT in APNG stream"); |
247 | 0 | } |
248 | 0 | } |
249 | 0 | else |
250 | 0 | { |
251 | 0 | png_warning(png_ptr, "Ignoring unexpected chunk in APNG sequence"); |
252 | 0 | png_crc_finish(png_ptr, length); |
253 | 0 | } |
254 | 0 | } |
255 | 0 | } |
256 | | #endif /* PNG_READ_APNG_SUPPORTED */ |
257 | | |
258 | | /* Optional call to update the users info_ptr structure */ |
259 | | void |
260 | | png_read_update_info(png_struct *png_ptr, png_info *info_ptr) |
261 | 50.3k | { |
262 | 50.3k | png_debug(1, "in png_read_update_info"); |
263 | | |
264 | 50.3k | if (png_ptr != NULL) |
265 | 50.3k | { |
266 | 50.3k | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
267 | 50.3k | { |
268 | 50.3k | png_read_start_row(png_ptr); |
269 | | |
270 | 50.3k | # ifdef PNG_READ_TRANSFORMS_SUPPORTED |
271 | 50.3k | png_read_transform_info(png_ptr, info_ptr); |
272 | | # else |
273 | | PNG_UNUSED(info_ptr) |
274 | | # endif |
275 | 50.3k | } |
276 | | |
277 | | /* New in 1.6.0 this avoids the bug of doing the initializations twice */ |
278 | 0 | else |
279 | 0 | png_app_error(png_ptr, |
280 | 0 | "png_read_update_info/png_start_read_image: duplicate call"); |
281 | 50.3k | } |
282 | 50.3k | } |
283 | | |
284 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
285 | | /* Initialize palette, background, etc, after transformations |
286 | | * are set, but before any reading takes place. This allows |
287 | | * the user to obtain a gamma-corrected palette, for example. |
288 | | * If the user doesn't call this, we will do it ourselves. |
289 | | */ |
290 | | void |
291 | | png_start_read_image(png_struct *png_ptr) |
292 | 0 | { |
293 | 0 | png_debug(1, "in png_start_read_image"); |
294 | |
|
295 | 0 | if (png_ptr != NULL) |
296 | 0 | { |
297 | 0 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
298 | 0 | png_read_start_row(png_ptr); |
299 | | |
300 | | /* New in 1.6.0 this avoids the bug of doing the initializations twice */ |
301 | 0 | else |
302 | 0 | png_app_error(png_ptr, |
303 | 0 | "png_start_read_image/png_read_update_info: duplicate call"); |
304 | 0 | } |
305 | 0 | } |
306 | | #endif /* SEQUENTIAL_READ */ |
307 | | |
308 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
309 | | #ifdef PNG_MNG_FEATURES_SUPPORTED |
310 | | /* Undoes intrapixel differencing, |
311 | | * NOTE: this is apparently only supported in the 'sequential' reader. |
312 | | */ |
313 | | static void |
314 | | png_do_read_intrapixel(png_row_info *row_info, png_byte *row) |
315 | 0 | { |
316 | 0 | png_debug(1, "in png_do_read_intrapixel"); |
317 | |
|
318 | 0 | if ( |
319 | 0 | (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
320 | 0 | { |
321 | 0 | int bytes_per_pixel; |
322 | 0 | png_uint_32 row_width = row_info->width; |
323 | |
|
324 | 0 | if (row_info->bit_depth == 8) |
325 | 0 | { |
326 | 0 | png_byte *rp; |
327 | 0 | png_uint_32 i; |
328 | |
|
329 | 0 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
330 | 0 | bytes_per_pixel = 3; |
331 | | |
332 | 0 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
333 | 0 | bytes_per_pixel = 4; |
334 | | |
335 | 0 | else |
336 | 0 | return; |
337 | | |
338 | 0 | for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
339 | 0 | { |
340 | 0 | *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); |
341 | 0 | *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); |
342 | 0 | } |
343 | 0 | } |
344 | 0 | else if (row_info->bit_depth == 16) |
345 | 0 | { |
346 | 0 | png_byte *rp; |
347 | 0 | png_uint_32 i; |
348 | |
|
349 | 0 | if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
350 | 0 | bytes_per_pixel = 6; |
351 | | |
352 | 0 | else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
353 | 0 | bytes_per_pixel = 8; |
354 | | |
355 | 0 | else |
356 | 0 | return; |
357 | | |
358 | 0 | for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
359 | 0 | { |
360 | 0 | png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1); |
361 | 0 | png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3); |
362 | 0 | png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5); |
363 | 0 | png_uint_32 red = (s0 + s1 + 65536) & 0xffff; |
364 | 0 | png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; |
365 | 0 | *(rp ) = (png_byte)((red >> 8) & 0xff); |
366 | 0 | *(rp + 1) = (png_byte)(red & 0xff); |
367 | 0 | *(rp + 4) = (png_byte)((blue >> 8) & 0xff); |
368 | 0 | *(rp + 5) = (png_byte)(blue & 0xff); |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | 0 | } |
373 | | #endif /* MNG_FEATURES */ |
374 | | |
375 | | void |
376 | | png_read_row(png_struct *png_ptr, png_byte *row, png_byte *dsp_row) |
377 | 3.10M | { |
378 | 3.10M | png_row_info row_info; |
379 | | |
380 | 3.10M | if (png_ptr == NULL) |
381 | 0 | return; |
382 | | |
383 | 3.10M | png_debug2(1, "in png_read_row (row %lu, pass %d)", |
384 | 3.10M | (unsigned long)png_ptr->row_number, png_ptr->pass); |
385 | | |
386 | | /* png_read_start_row sets the information (in particular iwidth) for this |
387 | | * interlace pass. |
388 | | */ |
389 | 3.10M | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
390 | 0 | png_read_start_row(png_ptr); |
391 | | |
392 | | /* 1.5.6: row_info moved out of png_struct to a local here. */ |
393 | 3.10M | row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ |
394 | 3.10M | row_info.color_type = png_ptr->color_type; |
395 | 3.10M | row_info.bit_depth = png_ptr->bit_depth; |
396 | 3.10M | row_info.channels = png_ptr->channels; |
397 | 3.10M | row_info.pixel_depth = png_ptr->pixel_depth; |
398 | 3.10M | row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
399 | | |
400 | 3.10M | #ifdef PNG_WARNINGS_SUPPORTED |
401 | 3.10M | if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
402 | 49.3k | { |
403 | | /* Check for transforms that have been set but were defined out */ |
404 | | #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
405 | | if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
406 | | png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); |
407 | | #endif |
408 | | |
409 | | #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
410 | | if ((png_ptr->transformations & PNG_FILLER) != 0) |
411 | | png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); |
412 | | #endif |
413 | | |
414 | | #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
415 | | !defined(PNG_READ_PACKSWAP_SUPPORTED) |
416 | | if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
417 | | png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); |
418 | | #endif |
419 | | |
420 | | #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
421 | | if ((png_ptr->transformations & PNG_PACK) != 0) |
422 | | png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); |
423 | | #endif |
424 | | |
425 | | #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
426 | | if ((png_ptr->transformations & PNG_SHIFT) != 0) |
427 | | png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); |
428 | | #endif |
429 | | |
430 | | #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
431 | | if ((png_ptr->transformations & PNG_BGR) != 0) |
432 | | png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); |
433 | | #endif |
434 | | |
435 | | #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
436 | | if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
437 | | png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); |
438 | | #endif |
439 | 49.3k | } |
440 | 3.10M | #endif /* WARNINGS */ |
441 | | |
442 | 3.10M | #ifdef PNG_READ_INTERLACING_SUPPORTED |
443 | | /* If interlaced and we do not need a new row, combine row and return. |
444 | | * Notice that the pixels we have from previous rows have been transformed |
445 | | * already; we can only combine like with like (transformed or |
446 | | * untransformed) and, because of the libpng API for interlaced images, this |
447 | | * means we must transform before de-interlacing. |
448 | | */ |
449 | 3.10M | if (png_ptr->interlaced != 0 && |
450 | 2.60M | (png_ptr->transformations & PNG_INTERLACE) != 0) |
451 | 2.60M | { |
452 | 2.60M | switch (png_ptr->pass) |
453 | 2.60M | { |
454 | 564k | case 0: |
455 | 564k | if (png_ptr->row_number & 0x07) |
456 | 471k | { |
457 | 471k | if (dsp_row != NULL) |
458 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
459 | 471k | png_read_finish_row(png_ptr); |
460 | 471k | return; |
461 | 471k | } |
462 | 92.8k | break; |
463 | | |
464 | 491k | case 1: |
465 | 491k | if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
466 | 451k | { |
467 | 451k | if (dsp_row != NULL) |
468 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
469 | | |
470 | 451k | png_read_finish_row(png_ptr); |
471 | 451k | return; |
472 | 451k | } |
473 | 39.6k | break; |
474 | | |
475 | 419k | case 2: |
476 | 419k | if ((png_ptr->row_number & 0x07) != 4) |
477 | 370k | { |
478 | 370k | if (dsp_row != NULL && (png_ptr->row_number & 4)) |
479 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
480 | | |
481 | 370k | png_read_finish_row(png_ptr); |
482 | 370k | return; |
483 | 370k | } |
484 | 49.7k | break; |
485 | | |
486 | 370k | case 3: |
487 | 370k | if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
488 | 308k | { |
489 | 308k | if (dsp_row != NULL) |
490 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
491 | | |
492 | 308k | png_read_finish_row(png_ptr); |
493 | 308k | return; |
494 | 308k | } |
495 | 62.0k | break; |
496 | | |
497 | 311k | case 4: |
498 | 311k | if ((png_ptr->row_number & 3) != 2) |
499 | 239k | { |
500 | 239k | if (dsp_row != NULL && (png_ptr->row_number & 2)) |
501 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
502 | | |
503 | 239k | png_read_finish_row(png_ptr); |
504 | 239k | return; |
505 | 239k | } |
506 | 72.2k | break; |
507 | | |
508 | 265k | case 5: |
509 | 265k | if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
510 | 180k | { |
511 | 180k | if (dsp_row != NULL) |
512 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
513 | | |
514 | 180k | png_read_finish_row(png_ptr); |
515 | 180k | return; |
516 | 180k | } |
517 | 85.8k | break; |
518 | | |
519 | 85.8k | default: |
520 | 180k | case 6: |
521 | 180k | if ((png_ptr->row_number & 1) == 0) |
522 | 101k | { |
523 | 101k | png_read_finish_row(png_ptr); |
524 | 101k | return; |
525 | 101k | } |
526 | 79.1k | break; |
527 | 2.60M | } |
528 | 2.60M | } |
529 | 979k | #endif |
530 | | |
531 | 979k | if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) |
532 | 0 | png_error(png_ptr, "Invalid attempt to read row data"); |
533 | | |
534 | | /* Fill the row with IDAT data: */ |
535 | 979k | png_ptr->row_buf[0]=255; /* to force error if no data was found */ |
536 | 979k | png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); |
537 | | |
538 | 979k | if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) |
539 | 434k | { |
540 | 434k | if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) |
541 | 433k | png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, |
542 | 433k | png_ptr->prev_row + 1, png_ptr->row_buf[0]); |
543 | 668 | else |
544 | 668 | png_error(png_ptr, "bad adaptive filter value"); |
545 | 434k | } |
546 | | |
547 | | /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before |
548 | | * 1.5.6, while the buffer really is this big in current versions of libpng |
549 | | * it may not be in the future, so this was changed just to copy the |
550 | | * interlaced count: |
551 | | */ |
552 | 978k | memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); |
553 | | |
554 | 978k | #ifdef PNG_MNG_FEATURES_SUPPORTED |
555 | 978k | if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && |
556 | 35.9k | (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
557 | 0 | { |
558 | | /* Intrapixel differencing */ |
559 | 0 | png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); |
560 | 0 | } |
561 | 978k | #endif |
562 | | |
563 | 978k | #ifdef PNG_READ_TRANSFORMS_SUPPORTED |
564 | 978k | if (png_ptr->transformations |
565 | 289k | # ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED |
566 | 289k | || png_ptr->num_palette_max >= 0 |
567 | 978k | # endif |
568 | 978k | ) |
569 | 680k | png_do_read_transformations(png_ptr, &row_info); |
570 | 978k | #endif |
571 | | |
572 | | /* The transformed pixel depth should match the depth now in row_info. */ |
573 | 978k | if (png_ptr->transformed_pixel_depth == 0) |
574 | 45.1k | { |
575 | 45.1k | png_ptr->transformed_pixel_depth = row_info.pixel_depth; |
576 | 45.1k | if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) |
577 | 0 | png_error(png_ptr, "sequential row overflow"); |
578 | 45.1k | } |
579 | | |
580 | 933k | else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) |
581 | 0 | png_error(png_ptr, "internal sequential row size calculation error"); |
582 | | |
583 | 978k | #ifdef PNG_READ_INTERLACING_SUPPORTED |
584 | | /* Expand interlaced rows to full size */ |
585 | 978k | if (png_ptr->interlaced != 0 && |
586 | 477k | (png_ptr->transformations & PNG_INTERLACE) != 0) |
587 | 477k | { |
588 | 477k | if (png_ptr->pass < 6) |
589 | 398k | png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, |
590 | 398k | png_ptr->transformations); |
591 | | |
592 | 477k | if (dsp_row != NULL) |
593 | 0 | png_combine_row(png_ptr, dsp_row, 1/*display*/); |
594 | | |
595 | 477k | if (row != NULL) |
596 | 477k | png_combine_row(png_ptr, row, 0/*row*/); |
597 | 477k | } |
598 | | |
599 | 501k | else |
600 | 501k | #endif |
601 | 501k | { |
602 | 501k | if (row != NULL) |
603 | 492k | png_combine_row(png_ptr, row, -1/*ignored*/); |
604 | | |
605 | 501k | if (dsp_row != NULL) |
606 | 0 | png_combine_row(png_ptr, dsp_row, -1/*ignored*/); |
607 | 501k | } |
608 | 978k | png_read_finish_row(png_ptr); |
609 | | |
610 | 978k | if (png_ptr->read_row_fn != NULL) |
611 | 0 | (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
612 | 978k | } |
613 | | #endif /* SEQUENTIAL_READ */ |
614 | | |
615 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
616 | | /* Read one or more rows of image data. If the image is interlaced, |
617 | | * and png_set_interlace_handling() has been called, the rows need to |
618 | | * contain the contents of the rows from the previous pass. If the |
619 | | * image has alpha or transparency, and png_handle_alpha()[*] has been |
620 | | * called, the rows contents must be initialized to the contents of the |
621 | | * screen. |
622 | | * |
623 | | * "row" holds the actual image, and pixels are placed in it |
624 | | * as they arrive. If the image is displayed after each pass, it will |
625 | | * appear to "sparkle" in. "display_row" can be used to display a |
626 | | * "chunky" progressive image, with finer detail added as it becomes |
627 | | * available. If you do not want this "chunky" display, you may pass |
628 | | * NULL for display_row. If you do not want the sparkle display, and |
629 | | * you have not called png_handle_alpha(), you may pass NULL for rows. |
630 | | * If you have called png_handle_alpha(), and the image has either an |
631 | | * alpha channel or a transparency chunk, you must provide a buffer for |
632 | | * rows. In this case, you do not have to provide a display_row buffer |
633 | | * also, but you may. If the image is not interlaced, or if you have |
634 | | * not called png_set_interlace_handling(), the display_row buffer will |
635 | | * be ignored, so pass NULL to it. |
636 | | * |
637 | | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
638 | | */ |
639 | | |
640 | | void |
641 | | png_read_rows(png_struct *png_ptr, png_byte **row, |
642 | | png_byte **display_row, png_uint_32 num_rows) |
643 | 0 | { |
644 | 0 | png_uint_32 i; |
645 | 0 | png_byte **rp; |
646 | 0 | png_byte **dp; |
647 | |
|
648 | 0 | png_debug(1, "in png_read_rows"); |
649 | |
|
650 | 0 | if (png_ptr == NULL) |
651 | 0 | return; |
652 | | |
653 | 0 | rp = row; |
654 | 0 | dp = display_row; |
655 | 0 | if (rp != NULL && dp != NULL) |
656 | 0 | for (i = 0; i < num_rows; i++) |
657 | 0 | { |
658 | 0 | png_byte *rptr = *rp++; |
659 | 0 | png_byte *dptr = *dp++; |
660 | |
|
661 | 0 | png_read_row(png_ptr, rptr, dptr); |
662 | 0 | } |
663 | | |
664 | 0 | else if (rp != NULL) |
665 | 0 | for (i = 0; i < num_rows; i++) |
666 | 0 | { |
667 | 0 | png_byte *rptr = *rp; |
668 | 0 | png_read_row(png_ptr, rptr, NULL); |
669 | 0 | rp++; |
670 | 0 | } |
671 | | |
672 | 0 | else if (dp != NULL) |
673 | 0 | for (i = 0; i < num_rows; i++) |
674 | 0 | { |
675 | 0 | png_byte *dptr = *dp; |
676 | 0 | png_read_row(png_ptr, NULL, dptr); |
677 | 0 | dp++; |
678 | 0 | } |
679 | 0 | } |
680 | | #endif /* SEQUENTIAL_READ */ |
681 | | |
682 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
683 | | /* Read the entire image. If the image has an alpha channel or a tRNS |
684 | | * chunk, and you have called png_handle_alpha()[*], you will need to |
685 | | * initialize the image to the current image that PNG will be overlaying. |
686 | | * We set the num_rows again here, in case it was incorrectly set in |
687 | | * png_read_start_row() by a call to png_read_update_info() or |
688 | | * png_start_read_image() if png_set_interlace_handling() wasn't called |
689 | | * prior to either of these functions like it should have been. You can |
690 | | * only call this function once. If you desire to have an image for |
691 | | * each pass of a interlaced image, use png_read_rows() instead. |
692 | | * |
693 | | * [*] png_handle_alpha() does not exist yet, as of this version of libpng |
694 | | */ |
695 | | void |
696 | | png_read_image(png_struct *png_ptr, png_byte **image) |
697 | 0 | { |
698 | 0 | png_uint_32 i, image_height; |
699 | 0 | int pass, j; |
700 | 0 | png_byte **rp; |
701 | |
|
702 | 0 | png_debug(1, "in png_read_image"); |
703 | |
|
704 | 0 | if (png_ptr == NULL) |
705 | 0 | return; |
706 | | |
707 | 0 | #ifdef PNG_READ_INTERLACING_SUPPORTED |
708 | 0 | if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
709 | 0 | { |
710 | 0 | pass = png_set_interlace_handling(png_ptr); |
711 | | /* And make sure transforms are initialized. */ |
712 | 0 | png_start_read_image(png_ptr); |
713 | 0 | } |
714 | 0 | else |
715 | 0 | { |
716 | 0 | if (png_ptr->interlaced != 0 && |
717 | 0 | (png_ptr->transformations & PNG_INTERLACE) == 0) |
718 | 0 | { |
719 | | /* Caller called png_start_read_image or png_read_update_info without |
720 | | * first turning on the PNG_INTERLACE transform. We can fix this here, |
721 | | * but the caller should do it! |
722 | | */ |
723 | 0 | png_warning(png_ptr, "Interlace handling should be turned on when " |
724 | 0 | "using png_read_image"); |
725 | | /* Make sure this is set correctly */ |
726 | 0 | png_ptr->num_rows = png_ptr->height; |
727 | 0 | } |
728 | | |
729 | | /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in |
730 | | * the above error case. |
731 | | */ |
732 | 0 | pass = png_set_interlace_handling(png_ptr); |
733 | 0 | } |
734 | | #else |
735 | | if (png_ptr->interlaced) |
736 | | png_error(png_ptr, |
737 | | "Cannot read interlaced image -- interlace handler disabled"); |
738 | | |
739 | | pass = 1; |
740 | | #endif |
741 | |
|
742 | 0 | image_height=png_ptr->height; |
743 | |
|
744 | 0 | for (j = 0; j < pass; j++) |
745 | 0 | { |
746 | 0 | rp = image; |
747 | 0 | for (i = 0; i < image_height; i++) |
748 | 0 | { |
749 | 0 | png_read_row(png_ptr, *rp, NULL); |
750 | 0 | rp++; |
751 | 0 | } |
752 | 0 | } |
753 | 0 | } |
754 | | #endif /* SEQUENTIAL_READ */ |
755 | | |
756 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
757 | | /* Read the end of the PNG file. Will not read past the end of the |
758 | | * file, will verify the end is accurate, and will read any comments |
759 | | * or time information at the end of the file, if info is not NULL. |
760 | | */ |
761 | | void |
762 | | png_read_end(png_struct *png_ptr, png_info *info_ptr) |
763 | 38.0k | { |
764 | 38.0k | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
765 | 38.0k | int keep; |
766 | 38.0k | #endif |
767 | | |
768 | 38.0k | png_debug(1, "in png_read_end"); |
769 | | |
770 | 38.0k | if (png_ptr == NULL) |
771 | 0 | return; |
772 | | |
773 | | /* If png_read_end is called in the middle of reading the rows there may |
774 | | * still be pending IDAT data and an owned zstream. Deal with this here. |
775 | | */ |
776 | 38.0k | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
777 | 38.0k | if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0) |
778 | 38.0k | #endif |
779 | 38.0k | png_read_finish_IDAT(png_ptr); |
780 | | |
781 | 38.0k | #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
782 | | /* Report invalid palette index; added at libng-1.5.10 */ |
783 | 38.0k | if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
784 | 1.05k | png_ptr->num_palette_max >= png_ptr->num_palette) |
785 | 0 | png_benign_error(png_ptr, "Read palette index exceeding num_palette"); |
786 | 38.0k | #endif |
787 | | |
788 | 38.0k | do |
789 | 89.2k | { |
790 | 89.2k | png_uint_32 length = png_read_chunk_header(png_ptr); |
791 | 89.2k | png_uint_32 chunk_name = png_ptr->chunk_name; |
792 | | |
793 | 89.2k | if (chunk_name != png_IDAT) |
794 | 82.3k | { |
795 | | /* These flags must be set consistently for all non-IDAT chunks, |
796 | | * including the unknown chunks. |
797 | | */ |
798 | 82.3k | png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT | PNG_AFTER_IDAT; |
799 | 82.3k | } |
800 | | |
801 | 89.2k | if (chunk_name == png_IEND) |
802 | 36.8k | png_handle_chunk(png_ptr, info_ptr, length); |
803 | | |
804 | 52.4k | else if (chunk_name == png_IHDR) |
805 | 10 | png_handle_chunk(png_ptr, info_ptr, length); |
806 | | |
807 | 52.4k | else if (info_ptr == NULL) |
808 | 0 | png_crc_finish(png_ptr, length); |
809 | | |
810 | 52.4k | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
811 | 52.4k | else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) |
812 | 6.95k | { |
813 | 6.95k | if (chunk_name == png_IDAT) |
814 | 0 | { |
815 | 0 | if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
816 | 0 | || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) |
817 | 0 | png_benign_error(png_ptr, ".Too many IDATs found"); |
818 | 0 | } |
819 | 6.95k | png_handle_unknown(png_ptr, info_ptr, length, keep); |
820 | 6.95k | if (chunk_name == png_PLTE) |
821 | 0 | png_ptr->mode |= PNG_HAVE_PLTE; |
822 | 6.95k | } |
823 | 45.4k | #endif |
824 | | |
825 | 45.4k | else if (chunk_name == png_IDAT) |
826 | 6.49k | { |
827 | | /* Zero length IDATs are legal after the last IDAT has been |
828 | | * read, but not after other chunks have been read. 1.6 does not |
829 | | * always read all the deflate data; specifically it cannot be relied |
830 | | * upon to read the Adler32 at the end. If it doesn't ignore IDAT |
831 | | * chunks which are longer than zero as well: |
832 | | */ |
833 | 6.49k | if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
834 | 6.49k | || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) |
835 | 2.01k | png_benign_error(png_ptr, "..Too many IDATs found"); |
836 | | |
837 | 6.49k | png_crc_finish(png_ptr, length); |
838 | 6.49k | } |
839 | | |
840 | 38.9k | else |
841 | 38.9k | png_handle_chunk(png_ptr, info_ptr, length); |
842 | 89.2k | } while ((png_ptr->mode & PNG_HAVE_IEND) == 0); |
843 | 38.0k | } |
844 | | #endif /* SEQUENTIAL_READ */ |
845 | | |
846 | | /* Free all memory used in the read struct */ |
847 | | static void |
848 | | png_read_destroy(png_struct *png_ptr) |
849 | 93.6k | { |
850 | 93.6k | png_debug(1, "in png_read_destroy"); |
851 | | |
852 | 93.6k | #ifdef PNG_READ_GAMMA_SUPPORTED |
853 | 93.6k | png_destroy_gamma_table(png_ptr); |
854 | 93.6k | #endif |
855 | | |
856 | 93.6k | png_free(png_ptr, png_ptr->big_row_buf); |
857 | 93.6k | png_ptr->big_row_buf = NULL; |
858 | 93.6k | png_free(png_ptr, png_ptr->big_prev_row); |
859 | 93.6k | png_ptr->big_prev_row = NULL; |
860 | 93.6k | png_free(png_ptr, png_ptr->read_buffer); |
861 | 93.6k | png_ptr->read_buffer = NULL; |
862 | | |
863 | 93.6k | #ifdef PNG_READ_QUANTIZE_SUPPORTED |
864 | 93.6k | png_free(png_ptr, png_ptr->palette_lookup); |
865 | 93.6k | png_ptr->palette_lookup = NULL; |
866 | 93.6k | png_free(png_ptr, png_ptr->quantize_index); |
867 | 93.6k | png_ptr->quantize_index = NULL; |
868 | 93.6k | #endif |
869 | | |
870 | 93.6k | if ((png_ptr->free_me & PNG_FREE_PLTE) != 0) |
871 | 0 | { |
872 | 0 | png_zfree(png_ptr, png_ptr->palette); |
873 | 0 | png_ptr->palette = NULL; |
874 | 0 | } |
875 | 93.6k | png_ptr->free_me &= ~PNG_FREE_PLTE; |
876 | | |
877 | 93.6k | #if defined(PNG_tRNS_SUPPORTED) || \ |
878 | 93.6k | defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
879 | 93.6k | if ((png_ptr->free_me & PNG_FREE_TRNS) != 0) |
880 | 0 | { |
881 | 0 | png_free(png_ptr, png_ptr->trans_alpha); |
882 | 0 | png_ptr->trans_alpha = NULL; |
883 | 0 | } |
884 | 93.6k | png_ptr->free_me &= ~PNG_FREE_TRNS; |
885 | 93.6k | #endif |
886 | | |
887 | 93.6k | inflateEnd(&png_ptr->zstream); |
888 | | |
889 | 93.6k | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
890 | 93.6k | png_free(png_ptr, png_ptr->save_buffer); |
891 | 93.6k | png_ptr->save_buffer = NULL; |
892 | 93.6k | #endif |
893 | | |
894 | 93.6k | #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \ |
895 | 93.6k | defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
896 | 93.6k | png_free(png_ptr, png_ptr->unknown_chunk.data); |
897 | 93.6k | png_ptr->unknown_chunk.data = NULL; |
898 | 93.6k | #endif |
899 | | |
900 | 93.6k | #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
901 | 93.6k | png_free(png_ptr, png_ptr->chunk_list); |
902 | 93.6k | png_ptr->chunk_list = NULL; |
903 | 93.6k | #endif |
904 | | |
905 | | #ifdef PNG_TARGET_STORES_DATA |
906 | | if (png_ptr->target_data != NULL) |
907 | | png_target_free_data(png_ptr); |
908 | | png_ptr->target_data = NULL; |
909 | | #endif |
910 | | |
911 | | /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error |
912 | | * callbacks are still set at this point. They are required to complete the |
913 | | * destruction of the png_struct itself. |
914 | | */ |
915 | 93.6k | } |
916 | | |
917 | | /* Free all memory used by the read */ |
918 | | void |
919 | | png_destroy_read_struct(png_struct **png_ptr_ptr, png_info **info_ptr_ptr, |
920 | | png_info **end_info_ptr_ptr) |
921 | 93.6k | { |
922 | 93.6k | png_struct *png_ptr = NULL; |
923 | | |
924 | 93.6k | png_debug(1, "in png_destroy_read_struct"); |
925 | | |
926 | 93.6k | if (png_ptr_ptr != NULL) |
927 | 93.6k | png_ptr = *png_ptr_ptr; |
928 | | |
929 | 93.6k | if (png_ptr == NULL) |
930 | 0 | return; |
931 | | |
932 | | /* libpng 1.6.0: use the API to destroy info structs to ensure consistent |
933 | | * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. |
934 | | * The extra was, apparently, unnecessary yet this hides memory leak bugs. |
935 | | */ |
936 | 93.6k | png_destroy_info_struct(png_ptr, end_info_ptr_ptr); |
937 | 93.6k | png_destroy_info_struct(png_ptr, info_ptr_ptr); |
938 | | |
939 | 93.6k | *png_ptr_ptr = NULL; |
940 | 93.6k | png_read_destroy(png_ptr); |
941 | 93.6k | png_destroy_png_struct(png_ptr); |
942 | 93.6k | } |
943 | | |
944 | | void |
945 | | png_set_read_status_fn(png_struct *png_ptr, png_read_status_ptr read_row_fn) |
946 | 0 | { |
947 | 0 | if (png_ptr == NULL) |
948 | 0 | return; |
949 | | |
950 | 0 | png_ptr->read_row_fn = read_row_fn; |
951 | 0 | } |
952 | | |
953 | | |
954 | | #ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
955 | | #ifdef PNG_INFO_IMAGE_SUPPORTED |
956 | | void |
957 | | png_read_png(png_struct *png_ptr, png_info *info_ptr, |
958 | | int transforms, void *params) |
959 | 0 | { |
960 | 0 | png_debug(1, "in png_read_png"); |
961 | |
|
962 | 0 | if (png_ptr == NULL || info_ptr == NULL) |
963 | 0 | return; |
964 | | |
965 | | /* png_read_info() gives us all of the information from the |
966 | | * PNG file before the first IDAT (image data chunk). |
967 | | */ |
968 | 0 | png_read_info(png_ptr, info_ptr); |
969 | 0 | if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_byte *))) |
970 | 0 | png_error(png_ptr, "Image is too high to process with png_read_png()"); |
971 | | |
972 | | /* -------------- image transformations start here ------------------- */ |
973 | | /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM |
974 | | * is not implemented. This will only happen in de-configured (non-default) |
975 | | * libpng builds. The results can be unexpected - png_read_png may return |
976 | | * short or mal-formed rows because the transform is skipped. |
977 | | */ |
978 | | |
979 | | /* Tell libpng to strip 16-bit/color files down to 8 bits per color. |
980 | | */ |
981 | 0 | if ((transforms & PNG_TRANSFORM_SCALE_16) != 0) |
982 | | /* Added at libpng-1.5.4. "strip_16" produces the same result that it |
983 | | * did in earlier versions, while "scale_16" is now more accurate. |
984 | | */ |
985 | 0 | #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
986 | 0 | png_set_scale_16(png_ptr); |
987 | | #else |
988 | | png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported"); |
989 | | #endif |
990 | | |
991 | | /* If both SCALE and STRIP are required pngrtran will effectively cancel the |
992 | | * latter by doing SCALE first. This is ok and allows apps not to check for |
993 | | * which is supported to get the right answer. |
994 | | */ |
995 | 0 | if ((transforms & PNG_TRANSFORM_STRIP_16) != 0) |
996 | 0 | #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
997 | 0 | png_set_strip_16(png_ptr); |
998 | | #else |
999 | | png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported"); |
1000 | | #endif |
1001 | | |
1002 | | /* Strip alpha bytes from the input data without combining with |
1003 | | * the background (not recommended). |
1004 | | */ |
1005 | 0 | if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0) |
1006 | 0 | #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
1007 | 0 | png_set_strip_alpha(png_ptr); |
1008 | | #else |
1009 | | png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported"); |
1010 | | #endif |
1011 | | |
1012 | | /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single |
1013 | | * byte into separate bytes (useful for paletted and grayscale images). |
1014 | | */ |
1015 | 0 | if ((transforms & PNG_TRANSFORM_PACKING) != 0) |
1016 | 0 | #ifdef PNG_READ_PACK_SUPPORTED |
1017 | 0 | png_set_packing(png_ptr); |
1018 | | #else |
1019 | | png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); |
1020 | | #endif |
1021 | | |
1022 | | /* Change the order of packed pixels to least significant bit first |
1023 | | * (not useful if you are using png_set_packing). |
1024 | | */ |
1025 | 0 | if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) |
1026 | 0 | #ifdef PNG_READ_PACKSWAP_SUPPORTED |
1027 | 0 | png_set_packswap(png_ptr); |
1028 | | #else |
1029 | | png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); |
1030 | | #endif |
1031 | | |
1032 | | /* Expand paletted colors into true RGB triplets |
1033 | | * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel |
1034 | | * Expand paletted or RGB images with transparency to full alpha |
1035 | | * channels so the data will be available as RGBA quartets. |
1036 | | */ |
1037 | 0 | if ((transforms & PNG_TRANSFORM_EXPAND) != 0) |
1038 | 0 | #ifdef PNG_READ_EXPAND_SUPPORTED |
1039 | 0 | png_set_expand(png_ptr); |
1040 | | #else |
1041 | | png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported"); |
1042 | | #endif |
1043 | | |
1044 | | /* We don't handle background color or gamma transformation or quantizing. |
1045 | | */ |
1046 | | |
1047 | | /* Invert monochrome files to have 0 as white and 1 as black |
1048 | | */ |
1049 | 0 | if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) |
1050 | 0 | #ifdef PNG_READ_INVERT_SUPPORTED |
1051 | 0 | png_set_invert_mono(png_ptr); |
1052 | | #else |
1053 | | png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); |
1054 | | #endif |
1055 | | |
1056 | | /* If you want to shift the pixel values from the range [0,255] or |
1057 | | * [0,65535] to the original [0,7] or [0,31], or whatever range the |
1058 | | * colors were originally in: |
1059 | | */ |
1060 | 0 | if ((transforms & PNG_TRANSFORM_SHIFT) != 0) |
1061 | 0 | #ifdef PNG_READ_SHIFT_SUPPORTED |
1062 | 0 | if ((info_ptr->valid & PNG_INFO_sBIT) != 0) |
1063 | 0 | png_set_shift(png_ptr, &info_ptr->sig_bit); |
1064 | | #else |
1065 | | png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); |
1066 | | #endif |
1067 | | |
1068 | | /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ |
1069 | 0 | if ((transforms & PNG_TRANSFORM_BGR) != 0) |
1070 | 0 | #ifdef PNG_READ_BGR_SUPPORTED |
1071 | 0 | png_set_bgr(png_ptr); |
1072 | | #else |
1073 | | png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); |
1074 | | #endif |
1075 | | |
1076 | | /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ |
1077 | 0 | if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) |
1078 | 0 | #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
1079 | 0 | png_set_swap_alpha(png_ptr); |
1080 | | #else |
1081 | | png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); |
1082 | | #endif |
1083 | | |
1084 | | /* Swap bytes of 16-bit files to least significant byte first */ |
1085 | 0 | if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) |
1086 | 0 | #ifdef PNG_READ_SWAP_SUPPORTED |
1087 | 0 | png_set_swap(png_ptr); |
1088 | | #else |
1089 | | png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); |
1090 | | #endif |
1091 | | |
1092 | | /* Added at libpng-1.2.41 */ |
1093 | | /* Invert the alpha channel from opacity to transparency */ |
1094 | 0 | if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) |
1095 | 0 | #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
1096 | 0 | png_set_invert_alpha(png_ptr); |
1097 | | #else |
1098 | | png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); |
1099 | | #endif |
1100 | | |
1101 | | /* Added at libpng-1.2.41 */ |
1102 | | /* Expand grayscale image to RGB */ |
1103 | 0 | if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0) |
1104 | 0 | #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
1105 | 0 | png_set_gray_to_rgb(png_ptr); |
1106 | | #else |
1107 | | png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported"); |
1108 | | #endif |
1109 | | |
1110 | | /* Added at libpng-1.5.4 */ |
1111 | 0 | if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0) |
1112 | 0 | #ifdef PNG_READ_EXPAND_16_SUPPORTED |
1113 | 0 | png_set_expand_16(png_ptr); |
1114 | | #else |
1115 | | png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported"); |
1116 | | #endif |
1117 | | |
1118 | | /* We don't handle adding filler bytes */ |
1119 | | |
1120 | | /* We use png_read_image and rely on that for interlace handling, but we also |
1121 | | * call png_read_update_info therefore must turn on interlace handling now: |
1122 | | */ |
1123 | 0 | (void)png_set_interlace_handling(png_ptr); |
1124 | | |
1125 | | /* Optional call to gamma correct and add the background to the palette |
1126 | | * and update info structure. REQUIRED if you are expecting libpng to |
1127 | | * update the palette for you (i.e., you selected such a transform above). |
1128 | | */ |
1129 | 0 | png_read_update_info(png_ptr, info_ptr); |
1130 | | |
1131 | | /* -------------- image transformations end here ------------------- */ |
1132 | |
|
1133 | 0 | png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); |
1134 | 0 | if (info_ptr->row_pointers == NULL) |
1135 | 0 | { |
1136 | 0 | png_uint_32 iptr; |
1137 | |
|
1138 | 0 | info_ptr->row_pointers = png_voidcast(png_byte **, png_malloc(png_ptr, |
1139 | 0 | info_ptr->height * (sizeof (png_byte *)))); |
1140 | |
|
1141 | 0 | for (iptr=0; iptr<info_ptr->height; iptr++) |
1142 | 0 | info_ptr->row_pointers[iptr] = NULL; |
1143 | |
|
1144 | 0 | info_ptr->free_me |= PNG_FREE_ROWS; |
1145 | |
|
1146 | 0 | for (iptr = 0; iptr < info_ptr->height; iptr++) |
1147 | 0 | info_ptr->row_pointers[iptr] = png_voidcast(png_byte *, |
1148 | 0 | png_malloc(png_ptr, info_ptr->rowbytes)); |
1149 | 0 | } |
1150 | |
|
1151 | 0 | png_read_image(png_ptr, info_ptr->row_pointers); |
1152 | 0 | info_ptr->valid |= PNG_INFO_IDAT; |
1153 | | |
1154 | | /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ |
1155 | 0 | png_read_end(png_ptr, info_ptr); |
1156 | |
|
1157 | 0 | PNG_UNUSED(params) |
1158 | 0 | } |
1159 | | #endif /* INFO_IMAGE */ |
1160 | | #endif /* SEQUENTIAL_READ */ |
1161 | | |
1162 | | #ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
1163 | | /* SIMPLIFIED READ |
1164 | | * |
1165 | | * This code currently relies on the sequential reader, though it could easily |
1166 | | * be made to work with the progressive one. |
1167 | | */ |
1168 | | /* Arguments to png_image_finish_read: */ |
1169 | | |
1170 | | /* Encoding of PNG data (used by the color-map code) */ |
1171 | 0 | # define P_NOTSET 0 /* File encoding not yet known */ |
1172 | 0 | # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */ |
1173 | 0 | # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ |
1174 | 0 | # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ |
1175 | 0 | # define P_LINEAR8 4 /* 8-bit linear: only from a file value */ |
1176 | | |
1177 | | /* Color-map processing: after libpng has run on the PNG image further |
1178 | | * processing may be needed to convert the data to color-map indices. |
1179 | | */ |
1180 | 0 | #define PNG_CMAP_NONE 0 |
1181 | 0 | #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ |
1182 | 0 | #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ |
1183 | 0 | #define PNG_CMAP_RGB 3 /* Process RGB data */ |
1184 | 0 | #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ |
1185 | | |
1186 | | /* The following document where the background is for each processing case. */ |
1187 | 0 | #define PNG_CMAP_NONE_BACKGROUND 256 |
1188 | 0 | #define PNG_CMAP_GA_BACKGROUND 231 |
1189 | 0 | #define PNG_CMAP_TRANS_BACKGROUND 254 |
1190 | 0 | #define PNG_CMAP_RGB_BACKGROUND 256 |
1191 | 0 | #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 |
1192 | | |
1193 | | typedef struct |
1194 | | { |
1195 | | /* Arguments: */ |
1196 | | png_image *image; |
1197 | | void *buffer; |
1198 | | png_int_32 row_stride; |
1199 | | void *colormap; |
1200 | | const png_color *background; |
1201 | | /* Local variables: */ |
1202 | | void *local_row; |
1203 | | void *first_row; |
1204 | | ptrdiff_t row_bytes; /* step between rows */ |
1205 | | int file_encoding; /* E_ values above */ |
1206 | | png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */ |
1207 | | int colormap_processing; /* PNG_CMAP_ values above */ |
1208 | | } png_image_read_control; |
1209 | | |
1210 | | /* Do all the *safe* initialization - 'safe' means that png_error won't be |
1211 | | * called, so setting up the jmp_buf is not required. This means that anything |
1212 | | * called from here must *not* call png_malloc - it has to call png_malloc_warn |
1213 | | * instead so that control is returned safely back to this routine. |
1214 | | */ |
1215 | | static int |
1216 | | png_image_read_init(png_image *image) |
1217 | 0 | { |
1218 | 0 | if (image->opaque == NULL) |
1219 | 0 | { |
1220 | 0 | png_struct *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, |
1221 | 0 | png_safe_error, png_safe_warning); |
1222 | | |
1223 | | /* And set the rest of the structure to NULL to ensure that the various |
1224 | | * fields are consistent. |
1225 | | */ |
1226 | 0 | memset(image, 0, (sizeof *image)); |
1227 | 0 | image->version = PNG_IMAGE_VERSION; |
1228 | |
|
1229 | 0 | if (png_ptr != NULL) |
1230 | 0 | { |
1231 | 0 | png_info *info_ptr = png_create_info_struct(png_ptr); |
1232 | |
|
1233 | 0 | if (info_ptr != NULL) |
1234 | 0 | { |
1235 | 0 | png_control *control = png_voidcast(png_control *, |
1236 | 0 | png_malloc_warn(png_ptr, (sizeof *control))); |
1237 | |
|
1238 | 0 | if (control != NULL) |
1239 | 0 | { |
1240 | 0 | memset(control, 0, (sizeof *control)); |
1241 | |
|
1242 | 0 | control->png_ptr = png_ptr; |
1243 | 0 | control->info_ptr = info_ptr; |
1244 | 0 | control->for_write = 0; |
1245 | |
|
1246 | 0 | image->opaque = control; |
1247 | 0 | return 1; |
1248 | 0 | } |
1249 | | |
1250 | | /* Error clean up */ |
1251 | 0 | png_destroy_info_struct(png_ptr, &info_ptr); |
1252 | 0 | } |
1253 | | |
1254 | 0 | png_destroy_read_struct(&png_ptr, NULL, NULL); |
1255 | 0 | } |
1256 | | |
1257 | 0 | return png_image_error(image, "png_image_read: out of memory"); |
1258 | 0 | } |
1259 | | |
1260 | 0 | return png_image_error(image, "png_image_read: opaque pointer not NULL"); |
1261 | 0 | } |
1262 | | |
1263 | | /* Utility to find the base format of a PNG file from a png_struct. */ |
1264 | | static png_uint_32 |
1265 | | png_image_format(png_struct *png_ptr) |
1266 | 0 | { |
1267 | 0 | png_uint_32 format = 0; |
1268 | |
|
1269 | 0 | if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
1270 | 0 | format |= PNG_FORMAT_FLAG_COLOR; |
1271 | |
|
1272 | 0 | if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
1273 | 0 | format |= PNG_FORMAT_FLAG_ALPHA; |
1274 | | |
1275 | | /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS |
1276 | | * sets the png_struct fields; that's all we are interested in here. The |
1277 | | * precise interaction with an app call to png_set_tRNS and PNG file reading |
1278 | | * is unclear. |
1279 | | */ |
1280 | 0 | else if (png_ptr->num_trans > 0) |
1281 | 0 | format |= PNG_FORMAT_FLAG_ALPHA; |
1282 | |
|
1283 | 0 | if (png_ptr->bit_depth == 16) |
1284 | 0 | format |= PNG_FORMAT_FLAG_LINEAR; |
1285 | |
|
1286 | 0 | if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0) |
1287 | 0 | format |= PNG_FORMAT_FLAG_COLORMAP; |
1288 | |
|
1289 | 0 | return format; |
1290 | 0 | } |
1291 | | |
1292 | | static int |
1293 | | chromaticities_match_sRGB(const png_xy *xy) |
1294 | 0 | { |
1295 | 0 | # define sRGB_TOLERANCE 1000 |
1296 | 0 | static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */ |
1297 | 0 | { |
1298 | | /* color x y */ |
1299 | 0 | /* red */ 64000, 33000, |
1300 | 0 | /* green */ 30000, 60000, |
1301 | 0 | /* blue */ 15000, 6000, |
1302 | 0 | /* white */ 31270, 32900 |
1303 | 0 | }; |
1304 | |
|
1305 | 0 | if (PNG_OUT_OF_RANGE(xy->whitex, sRGB_xy.whitex,sRGB_TOLERANCE) || |
1306 | 0 | PNG_OUT_OF_RANGE(xy->whitey, sRGB_xy.whitey,sRGB_TOLERANCE) || |
1307 | 0 | PNG_OUT_OF_RANGE(xy->redx, sRGB_xy.redx, sRGB_TOLERANCE) || |
1308 | 0 | PNG_OUT_OF_RANGE(xy->redy, sRGB_xy.redy, sRGB_TOLERANCE) || |
1309 | 0 | PNG_OUT_OF_RANGE(xy->greenx, sRGB_xy.greenx,sRGB_TOLERANCE) || |
1310 | 0 | PNG_OUT_OF_RANGE(xy->greeny, sRGB_xy.greeny,sRGB_TOLERANCE) || |
1311 | 0 | PNG_OUT_OF_RANGE(xy->bluex, sRGB_xy.bluex, sRGB_TOLERANCE) || |
1312 | 0 | PNG_OUT_OF_RANGE(xy->bluey, sRGB_xy.bluey, sRGB_TOLERANCE)) |
1313 | 0 | return 0; |
1314 | 0 | return 1; |
1315 | 0 | } |
1316 | | |
1317 | | /* Is the given gamma significantly different from sRGB? The test is the same |
1318 | | * one used in pngrtran.c when deciding whether to do gamma correction. The |
1319 | | * arithmetic optimizes the division by using the fact that the inverse of the |
1320 | | * file sRGB gamma is 2.2 |
1321 | | */ |
1322 | | static int |
1323 | | png_gamma_not_sRGB(png_fixed_point g) |
1324 | 0 | { |
1325 | | /* 1.6.47: use the same sanity checks as used in pngrtran.c */ |
1326 | 0 | if (g < PNG_LIB_GAMMA_MIN || g > PNG_LIB_GAMMA_MAX) |
1327 | 0 | return 0; /* Includes the uninitialized value 0 */ |
1328 | | |
1329 | 0 | return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); |
1330 | 0 | } |
1331 | | |
1332 | | /* Do the main body of a 'png_image_begin_read' function; read the PNG file |
1333 | | * header and fill in all the information. This is executed in a safe context, |
1334 | | * unlike the init routine above. |
1335 | | */ |
1336 | | static int |
1337 | | png_image_is_not_sRGB(const png_struct *png_ptr) |
1338 | 0 | { |
1339 | | /* Does the colorspace **not** match sRGB? The flag is only set if the |
1340 | | * answer can be determined reliably. |
1341 | | * |
1342 | | * png_struct::chromaticities always exists since the simplified API |
1343 | | * requires rgb-to-gray. The mDCV, cICP and cHRM chunks may all set it to |
1344 | | * a non-sRGB value, so it needs to be checked but **only** if one of |
1345 | | * those chunks occured in the file. |
1346 | | */ |
1347 | | /* Highest priority: check to be safe. */ |
1348 | 0 | if (png_has_chunk(png_ptr, cICP) || png_has_chunk(png_ptr, mDCV)) |
1349 | 0 | return !chromaticities_match_sRGB(&png_ptr->chromaticities); |
1350 | | |
1351 | | /* If the image is marked as sRGB then it is... */ |
1352 | 0 | if (png_has_chunk(png_ptr, sRGB)) |
1353 | 0 | return 0; |
1354 | | |
1355 | | /* Last stop: cHRM, must check: */ |
1356 | 0 | if (png_has_chunk(png_ptr, cHRM)) |
1357 | 0 | return !chromaticities_match_sRGB(&png_ptr->chromaticities); |
1358 | | |
1359 | | /* Else default to sRGB */ |
1360 | 0 | return 0; |
1361 | 0 | } |
1362 | | |
1363 | | static int |
1364 | | png_image_read_header(void *argument) |
1365 | 0 | { |
1366 | 0 | png_image *image = png_voidcast(png_image *, argument); |
1367 | 0 | png_struct *png_ptr = image->opaque->png_ptr; |
1368 | 0 | png_info *info_ptr = image->opaque->info_ptr; |
1369 | |
|
1370 | 0 | #ifdef PNG_BENIGN_ERRORS_SUPPORTED |
1371 | 0 | png_set_benign_errors(png_ptr, 1/*warn*/); |
1372 | 0 | #endif |
1373 | 0 | png_read_info(png_ptr, info_ptr); |
1374 | | |
1375 | | /* Do this the fast way; just read directly out of png_struct. */ |
1376 | 0 | image->width = png_ptr->width; |
1377 | 0 | image->height = png_ptr->height; |
1378 | |
|
1379 | 0 | { |
1380 | 0 | png_uint_32 format = png_image_format(png_ptr); |
1381 | |
|
1382 | 0 | image->format = format; |
1383 | | |
1384 | | /* Greyscale images don't (typically) have colour space information and |
1385 | | * using it is pretty much impossible, so use sRGB for grayscale (it |
1386 | | * doesn't matter r==g==b so the transform is irrelevant.) |
1387 | | */ |
1388 | 0 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && |
1389 | 0 | png_image_is_not_sRGB(png_ptr)) |
1390 | 0 | image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
1391 | 0 | } |
1392 | | |
1393 | | /* We need the maximum number of entries regardless of the format the |
1394 | | * application sets here. |
1395 | | */ |
1396 | 0 | { |
1397 | 0 | png_uint_32 cmap_entries; |
1398 | |
|
1399 | 0 | switch (png_ptr->color_type) |
1400 | 0 | { |
1401 | 0 | case PNG_COLOR_TYPE_GRAY: |
1402 | 0 | cmap_entries = 1U << png_ptr->bit_depth; |
1403 | 0 | break; |
1404 | | |
1405 | 0 | case PNG_COLOR_TYPE_PALETTE: |
1406 | 0 | cmap_entries = (png_uint_32)png_ptr->num_palette; |
1407 | 0 | break; |
1408 | | |
1409 | 0 | default: |
1410 | 0 | cmap_entries = 256; |
1411 | 0 | break; |
1412 | 0 | } |
1413 | | |
1414 | 0 | if (cmap_entries > 256) |
1415 | 0 | cmap_entries = 256; |
1416 | |
|
1417 | 0 | image->colormap_entries = cmap_entries; |
1418 | 0 | } |
1419 | | |
1420 | 0 | return 1; |
1421 | 0 | } |
1422 | | |
1423 | | #ifdef PNG_STDIO_SUPPORTED |
1424 | | int |
1425 | | png_image_begin_read_from_stdio(png_image *image, FILE *file) |
1426 | 0 | { |
1427 | 0 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
1428 | 0 | { |
1429 | 0 | if (file != NULL) |
1430 | 0 | { |
1431 | 0 | if (png_image_read_init(image) != 0) |
1432 | 0 | { |
1433 | | /* This is slightly evil, but png_init_io doesn't do anything other |
1434 | | * than this and we haven't changed the standard IO functions so |
1435 | | * this saves a 'safe' function. |
1436 | | */ |
1437 | 0 | image->opaque->png_ptr->io_ptr = file; |
1438 | 0 | return png_safe_execute(image, png_image_read_header, image); |
1439 | 0 | } |
1440 | 0 | } |
1441 | | |
1442 | 0 | else |
1443 | 0 | return png_image_error(image, |
1444 | 0 | "png_image_begin_read_from_stdio: invalid argument"); |
1445 | 0 | } |
1446 | | |
1447 | 0 | else if (image != NULL) |
1448 | 0 | return png_image_error(image, |
1449 | 0 | "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION"); |
1450 | | |
1451 | 0 | return 0; |
1452 | 0 | } |
1453 | | |
1454 | | int |
1455 | | png_image_begin_read_from_file(png_image *image, const char *file_name) |
1456 | 0 | { |
1457 | 0 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
1458 | 0 | { |
1459 | 0 | if (file_name != NULL) |
1460 | 0 | { |
1461 | 0 | FILE *fp = fopen(file_name, "rb"); |
1462 | |
|
1463 | 0 | if (fp != NULL) |
1464 | 0 | { |
1465 | 0 | if (png_image_read_init(image) != 0) |
1466 | 0 | { |
1467 | 0 | image->opaque->png_ptr->io_ptr = fp; |
1468 | 0 | image->opaque->owned_file = 1; |
1469 | 0 | return png_safe_execute(image, png_image_read_header, image); |
1470 | 0 | } |
1471 | | |
1472 | | /* Clean up: just the opened file. */ |
1473 | 0 | (void)fclose(fp); |
1474 | 0 | } |
1475 | | |
1476 | 0 | else |
1477 | 0 | return png_image_error(image, strerror(errno)); |
1478 | 0 | } |
1479 | | |
1480 | 0 | else |
1481 | 0 | return png_image_error(image, |
1482 | 0 | "png_image_begin_read_from_file: invalid argument"); |
1483 | 0 | } |
1484 | | |
1485 | 0 | else if (image != NULL) |
1486 | 0 | return png_image_error(image, |
1487 | 0 | "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); |
1488 | | |
1489 | 0 | return 0; |
1490 | 0 | } |
1491 | | #endif /* STDIO */ |
1492 | | |
1493 | | static void |
1494 | | png_image_memory_read(png_struct *png_ptr, png_byte *out, size_t need) |
1495 | 0 | { |
1496 | 0 | if (png_ptr != NULL) |
1497 | 0 | { |
1498 | 0 | png_image *image = png_voidcast(png_image *, png_ptr->io_ptr); |
1499 | 0 | if (image != NULL) |
1500 | 0 | { |
1501 | 0 | png_control *cp = image->opaque; |
1502 | 0 | if (cp != NULL) |
1503 | 0 | { |
1504 | 0 | const png_byte *memory = cp->memory; |
1505 | 0 | size_t size = cp->size; |
1506 | |
|
1507 | 0 | if (memory != NULL && size >= need) |
1508 | 0 | { |
1509 | 0 | memcpy(out, memory, need); |
1510 | 0 | cp->memory = memory + need; |
1511 | 0 | cp->size = size - need; |
1512 | 0 | return; |
1513 | 0 | } |
1514 | | |
1515 | 0 | png_error(png_ptr, "read beyond end of data"); |
1516 | 0 | } |
1517 | 0 | } |
1518 | | |
1519 | 0 | png_error(png_ptr, "invalid memory read"); |
1520 | 0 | } |
1521 | 0 | } |
1522 | | |
1523 | | int png_image_begin_read_from_memory(png_image *image, |
1524 | | const void *memory, size_t size) |
1525 | 0 | { |
1526 | 0 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
1527 | 0 | { |
1528 | 0 | if (memory != NULL && size > 0) |
1529 | 0 | { |
1530 | 0 | if (png_image_read_init(image) != 0) |
1531 | 0 | { |
1532 | | /* Now set the IO functions to read from the memory buffer and |
1533 | | * store it into io_ptr. Again do this in-place to avoid calling a |
1534 | | * libpng function that requires error handling. |
1535 | | */ |
1536 | 0 | image->opaque->memory = png_voidcast(const png_byte *, memory); |
1537 | 0 | image->opaque->size = size; |
1538 | 0 | image->opaque->png_ptr->io_ptr = image; |
1539 | 0 | image->opaque->png_ptr->read_data_fn = png_image_memory_read; |
1540 | |
|
1541 | 0 | return png_safe_execute(image, png_image_read_header, image); |
1542 | 0 | } |
1543 | 0 | } |
1544 | | |
1545 | 0 | else |
1546 | 0 | return png_image_error(image, |
1547 | 0 | "png_image_begin_read_from_memory: invalid argument"); |
1548 | 0 | } |
1549 | | |
1550 | 0 | else if (image != NULL) |
1551 | 0 | return png_image_error(image, |
1552 | 0 | "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION"); |
1553 | | |
1554 | 0 | return 0; |
1555 | 0 | } |
1556 | | |
1557 | | /* Utility function to skip chunks that are not used by the simplified image |
1558 | | * read functions and an appropriate macro to call it. |
1559 | | */ |
1560 | | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
1561 | | static void |
1562 | | png_image_skip_unused_chunks(png_struct *png_ptr) |
1563 | 0 | { |
1564 | | /* Prepare the reader to ignore all recognized chunks whose data will not |
1565 | | * be used, i.e., all chunks recognized by libpng except for those |
1566 | | * involved in basic image reading: |
1567 | | * |
1568 | | * IHDR, PLTE, IDAT, IEND |
1569 | | * |
1570 | | * Or image data handling: |
1571 | | * |
1572 | | * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT. |
1573 | | * |
1574 | | * This provides a small performance improvement and eliminates any |
1575 | | * potential vulnerability to security problems in the unused chunks. |
1576 | | * |
1577 | | * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored |
1578 | | * too. This allows the simplified API to be compiled without iCCP support. |
1579 | | */ |
1580 | 0 | { |
1581 | 0 | static const png_byte chunks_to_process[] = { |
1582 | 0 | 98, 75, 71, 68, '\0', /* bKGD */ |
1583 | 0 | 99, 72, 82, 77, '\0', /* cHRM */ |
1584 | 0 | 99, 73, 67, 80, '\0', /* cICP */ |
1585 | 0 | 103, 65, 77, 65, '\0', /* gAMA */ |
1586 | 0 | 109, 68, 67, 86, '\0', /* mDCV */ |
1587 | 0 | 115, 66, 73, 84, '\0', /* sBIT */ |
1588 | 0 | 115, 82, 71, 66, '\0', /* sRGB */ |
1589 | 0 | }; |
1590 | | |
1591 | | /* Ignore unknown chunks and all other chunks except for the |
1592 | | * IHDR, PLTE, tRNS, IDAT, and IEND chunks. |
1593 | | */ |
1594 | 0 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, |
1595 | 0 | NULL, -1); |
1596 | | |
1597 | | /* But do not ignore image data handling chunks */ |
1598 | 0 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, |
1599 | 0 | chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5); |
1600 | 0 | } |
1601 | 0 | } |
1602 | | |
1603 | 0 | # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) |
1604 | | #else |
1605 | | # define PNG_SKIP_CHUNKS(p) ((void)0) |
1606 | | #endif /* HANDLE_AS_UNKNOWN */ |
1607 | | |
1608 | | /* The following macro gives the exact rounded answer for all values in the |
1609 | | * range 0..255 (it actually divides by 51.2, but the rounding still generates |
1610 | | * the correct numbers 0..5 |
1611 | | */ |
1612 | 0 | #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) |
1613 | | |
1614 | | /* Utility functions to make particular color-maps */ |
1615 | | static void |
1616 | | set_file_encoding(png_image_read_control *display) |
1617 | 0 | { |
1618 | 0 | png_struct *png_ptr = display->image->opaque->png_ptr; |
1619 | 0 | png_fixed_point g = png_resolve_file_gamma(png_ptr); |
1620 | | |
1621 | | /* PNGv3: the result may be 0 however the 'default_gamma' should have been |
1622 | | * set before this is called so zero is an error: |
1623 | | */ |
1624 | 0 | if (g == 0) |
1625 | 0 | png_error(png_ptr, "internal: default gamma not set"); |
1626 | | |
1627 | 0 | if (png_gamma_significant(g) != 0) |
1628 | 0 | { |
1629 | 0 | if (png_gamma_not_sRGB(g) != 0) |
1630 | 0 | { |
1631 | 0 | display->file_encoding = P_FILE; |
1632 | 0 | display->gamma_to_linear = png_reciprocal(g); |
1633 | 0 | } |
1634 | | |
1635 | 0 | else |
1636 | 0 | display->file_encoding = P_sRGB; |
1637 | 0 | } |
1638 | | |
1639 | 0 | else |
1640 | 0 | display->file_encoding = P_LINEAR8; |
1641 | 0 | } |
1642 | | |
1643 | | static unsigned int |
1644 | | decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) |
1645 | 0 | { |
1646 | 0 | if (encoding == P_FILE) /* double check */ |
1647 | 0 | encoding = display->file_encoding; |
1648 | |
|
1649 | 0 | if (encoding == P_NOTSET) /* must be the file encoding */ |
1650 | 0 | { |
1651 | 0 | set_file_encoding(display); |
1652 | 0 | encoding = display->file_encoding; |
1653 | 0 | } |
1654 | |
|
1655 | 0 | switch (encoding) |
1656 | 0 | { |
1657 | 0 | case P_FILE: |
1658 | 0 | value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); |
1659 | 0 | break; |
1660 | | |
1661 | 0 | case P_sRGB: |
1662 | 0 | value = png_sRGB_table[value]; |
1663 | 0 | break; |
1664 | | |
1665 | 0 | case P_LINEAR: |
1666 | 0 | break; |
1667 | | |
1668 | 0 | case P_LINEAR8: |
1669 | 0 | value *= 257; |
1670 | 0 | break; |
1671 | | |
1672 | 0 | #ifdef __GNUC__ |
1673 | 0 | default: |
1674 | 0 | png_error(display->image->opaque->png_ptr, |
1675 | 0 | "unexpected encoding (internal error)"); |
1676 | 0 | #endif |
1677 | 0 | } |
1678 | | |
1679 | 0 | return value; |
1680 | 0 | } |
1681 | | |
1682 | | static png_uint_32 |
1683 | | png_colormap_compose(png_image_read_control *display, |
1684 | | png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, |
1685 | | png_uint_32 background, int encoding) |
1686 | 0 | { |
1687 | | /* The file value is composed on the background, the background has the given |
1688 | | * encoding and so does the result, the file is encoded with P_FILE and the |
1689 | | * file and alpha are 8-bit values. The (output) encoding will always be |
1690 | | * P_LINEAR or P_sRGB. |
1691 | | */ |
1692 | 0 | png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); |
1693 | 0 | png_uint_32 b = decode_gamma(display, background, encoding); |
1694 | | |
1695 | | /* The alpha is always an 8-bit value (it comes from the palette), the value |
1696 | | * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. |
1697 | | */ |
1698 | 0 | f = f * alpha + b * (255-alpha); |
1699 | |
|
1700 | 0 | if (encoding == P_LINEAR) |
1701 | 0 | { |
1702 | | /* Scale to 65535; divide by 255, approximately (in fact this is extremely |
1703 | | * accurate, it divides by 255.00000005937181414556, with no overflow.) |
1704 | | */ |
1705 | 0 | f *= 257; /* Now scaled by 65535 */ |
1706 | 0 | f += f >> 16; |
1707 | 0 | f = (f+32768) >> 16; |
1708 | 0 | } |
1709 | | |
1710 | 0 | else /* P_sRGB */ |
1711 | 0 | f = PNG_sRGB_FROM_LINEAR(f); |
1712 | |
|
1713 | 0 | return f; |
1714 | 0 | } |
1715 | | |
1716 | | /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must |
1717 | | * be 8-bit. |
1718 | | */ |
1719 | | static void |
1720 | | png_create_colormap_entry(png_image_read_control *display, |
1721 | | png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, |
1722 | | png_uint_32 alpha, int encoding) |
1723 | 0 | { |
1724 | 0 | png_image *image = display->image; |
1725 | 0 | int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ? |
1726 | 0 | P_LINEAR : P_sRGB; |
1727 | 0 | int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && |
1728 | 0 | (red != green || green != blue); |
1729 | |
|
1730 | 0 | if (ip > 255) |
1731 | 0 | png_error(image->opaque->png_ptr, "color-map index out of range"); |
1732 | | |
1733 | | /* Update the cache with whether the file gamma is significantly different |
1734 | | * from sRGB. |
1735 | | */ |
1736 | 0 | if (encoding == P_FILE) |
1737 | 0 | { |
1738 | 0 | if (display->file_encoding == P_NOTSET) |
1739 | 0 | set_file_encoding(display); |
1740 | | |
1741 | | /* Note that the cached value may be P_FILE too, but if it is then the |
1742 | | * gamma_to_linear member has been set. |
1743 | | */ |
1744 | 0 | encoding = display->file_encoding; |
1745 | 0 | } |
1746 | |
|
1747 | 0 | if (encoding == P_FILE) |
1748 | 0 | { |
1749 | 0 | png_fixed_point g = display->gamma_to_linear; |
1750 | |
|
1751 | 0 | red = png_gamma_16bit_correct(red*257, g); |
1752 | 0 | green = png_gamma_16bit_correct(green*257, g); |
1753 | 0 | blue = png_gamma_16bit_correct(blue*257, g); |
1754 | |
|
1755 | 0 | if (convert_to_Y != 0 || output_encoding == P_LINEAR) |
1756 | 0 | { |
1757 | 0 | alpha *= 257; |
1758 | 0 | encoding = P_LINEAR; |
1759 | 0 | } |
1760 | | |
1761 | 0 | else |
1762 | 0 | { |
1763 | 0 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
1764 | 0 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
1765 | 0 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
1766 | 0 | encoding = P_sRGB; |
1767 | 0 | } |
1768 | 0 | } |
1769 | | |
1770 | 0 | else if (encoding == P_LINEAR8) |
1771 | 0 | { |
1772 | | /* This encoding occurs quite frequently in test cases because PngSuite |
1773 | | * includes a gAMA 1.0 chunk with most images. |
1774 | | */ |
1775 | 0 | red *= 257; |
1776 | 0 | green *= 257; |
1777 | 0 | blue *= 257; |
1778 | 0 | alpha *= 257; |
1779 | 0 | encoding = P_LINEAR; |
1780 | 0 | } |
1781 | | |
1782 | 0 | else if (encoding == P_sRGB && |
1783 | 0 | (convert_to_Y != 0 || output_encoding == P_LINEAR)) |
1784 | 0 | { |
1785 | | /* The values are 8-bit sRGB values, but must be converted to 16-bit |
1786 | | * linear. |
1787 | | */ |
1788 | 0 | red = png_sRGB_table[red]; |
1789 | 0 | green = png_sRGB_table[green]; |
1790 | 0 | blue = png_sRGB_table[blue]; |
1791 | 0 | alpha *= 257; |
1792 | 0 | encoding = P_LINEAR; |
1793 | 0 | } |
1794 | | |
1795 | | /* This is set if the color isn't gray but the output is. */ |
1796 | 0 | if (encoding == P_LINEAR) |
1797 | 0 | { |
1798 | 0 | if (convert_to_Y != 0) |
1799 | 0 | { |
1800 | | /* NOTE: these values are copied from png_do_rgb_to_gray */ |
1801 | 0 | png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + |
1802 | 0 | (png_uint_32)2366 * blue; |
1803 | |
|
1804 | 0 | if (output_encoding == P_LINEAR) |
1805 | 0 | y = (y + 16384) >> 15; |
1806 | | |
1807 | 0 | else |
1808 | 0 | { |
1809 | | /* y is scaled by 32768, we need it scaled by 255: */ |
1810 | 0 | y = (y + 128) >> 8; |
1811 | 0 | y *= 255; |
1812 | 0 | y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); |
1813 | 0 | alpha = PNG_DIV257(alpha); |
1814 | 0 | encoding = P_sRGB; |
1815 | 0 | } |
1816 | |
|
1817 | 0 | blue = red = green = y; |
1818 | 0 | } |
1819 | | |
1820 | 0 | else if (output_encoding == P_sRGB) |
1821 | 0 | { |
1822 | 0 | red = PNG_sRGB_FROM_LINEAR(red * 255); |
1823 | 0 | green = PNG_sRGB_FROM_LINEAR(green * 255); |
1824 | 0 | blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
1825 | 0 | alpha = PNG_DIV257(alpha); |
1826 | 0 | encoding = P_sRGB; |
1827 | 0 | } |
1828 | 0 | } |
1829 | |
|
1830 | 0 | if (encoding != output_encoding) |
1831 | 0 | png_error(image->opaque->png_ptr, "bad encoding (internal error)"); |
1832 | | |
1833 | | /* Store the value. */ |
1834 | 0 | { |
1835 | 0 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
1836 | 0 | int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && |
1837 | 0 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
1838 | | # else |
1839 | | # define afirst 0 |
1840 | | # endif |
1841 | 0 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
1842 | 0 | int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; |
1843 | | # else |
1844 | | # define bgr 0 |
1845 | | # endif |
1846 | |
|
1847 | 0 | if (output_encoding == P_LINEAR) |
1848 | 0 | { |
1849 | 0 | png_uint_16 *entry = png_voidcast(png_uint_16 *, display->colormap); |
1850 | |
|
1851 | 0 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
1852 | | |
1853 | | /* The linear 16-bit values must be pre-multiplied by the alpha channel |
1854 | | * value, if less than 65535 (this is, effectively, composite on black |
1855 | | * if the alpha channel is removed.) |
1856 | | */ |
1857 | 0 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
1858 | 0 | { |
1859 | 0 | case 4: |
1860 | 0 | entry[afirst ? 0 : 3] = (png_uint_16)alpha; |
1861 | | /* FALLTHROUGH */ |
1862 | |
|
1863 | 0 | case 3: |
1864 | 0 | if (alpha < 65535) |
1865 | 0 | { |
1866 | 0 | if (alpha > 0) |
1867 | 0 | { |
1868 | 0 | blue = (blue * alpha + 32767U)/65535U; |
1869 | 0 | green = (green * alpha + 32767U)/65535U; |
1870 | 0 | red = (red * alpha + 32767U)/65535U; |
1871 | 0 | } |
1872 | | |
1873 | 0 | else |
1874 | 0 | red = green = blue = 0; |
1875 | 0 | } |
1876 | 0 | entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; |
1877 | 0 | entry[afirst + 1] = (png_uint_16)green; |
1878 | 0 | entry[afirst + bgr] = (png_uint_16)red; |
1879 | 0 | break; |
1880 | | |
1881 | 0 | case 2: |
1882 | 0 | entry[1 ^ afirst] = (png_uint_16)alpha; |
1883 | | /* FALLTHROUGH */ |
1884 | |
|
1885 | 0 | case 1: |
1886 | 0 | if (alpha < 65535) |
1887 | 0 | { |
1888 | 0 | if (alpha > 0) |
1889 | 0 | green = (green * alpha + 32767U)/65535U; |
1890 | | |
1891 | 0 | else |
1892 | 0 | green = 0; |
1893 | 0 | } |
1894 | 0 | entry[afirst] = (png_uint_16)green; |
1895 | 0 | break; |
1896 | | |
1897 | 0 | default: |
1898 | 0 | break; |
1899 | 0 | } |
1900 | 0 | } |
1901 | | |
1902 | 0 | else /* output encoding is P_sRGB */ |
1903 | 0 | { |
1904 | 0 | png_byte *entry = png_voidcast(png_byte *, display->colormap); |
1905 | |
|
1906 | 0 | entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
1907 | |
|
1908 | 0 | switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
1909 | 0 | { |
1910 | 0 | case 4: |
1911 | 0 | entry[afirst ? 0 : 3] = (png_byte)alpha; |
1912 | | /* FALLTHROUGH */ |
1913 | 0 | case 3: |
1914 | 0 | entry[afirst + (2 ^ bgr)] = (png_byte)blue; |
1915 | 0 | entry[afirst + 1] = (png_byte)green; |
1916 | 0 | entry[afirst + bgr] = (png_byte)red; |
1917 | 0 | break; |
1918 | | |
1919 | 0 | case 2: |
1920 | 0 | entry[1 ^ afirst] = (png_byte)alpha; |
1921 | | /* FALLTHROUGH */ |
1922 | 0 | case 1: |
1923 | 0 | entry[afirst] = (png_byte)green; |
1924 | 0 | break; |
1925 | | |
1926 | 0 | default: |
1927 | 0 | break; |
1928 | 0 | } |
1929 | 0 | } |
1930 | |
|
1931 | | # ifdef afirst |
1932 | | # undef afirst |
1933 | | # endif |
1934 | | # ifdef bgr |
1935 | | # undef bgr |
1936 | | # endif |
1937 | 0 | } |
1938 | 0 | } |
1939 | | |
1940 | | static int |
1941 | | make_gray_file_colormap(png_image_read_control *display) |
1942 | 0 | { |
1943 | 0 | unsigned int i; |
1944 | |
|
1945 | 0 | for (i=0; i<256; ++i) |
1946 | 0 | png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); |
1947 | |
|
1948 | 0 | return (int)i; |
1949 | 0 | } |
1950 | | |
1951 | | static int |
1952 | | make_gray_colormap(png_image_read_control *display) |
1953 | 0 | { |
1954 | 0 | unsigned int i; |
1955 | |
|
1956 | 0 | for (i=0; i<256; ++i) |
1957 | 0 | png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); |
1958 | |
|
1959 | 0 | return (int)i; |
1960 | 0 | } |
1961 | 0 | #define PNG_GRAY_COLORMAP_ENTRIES 256 |
1962 | | |
1963 | | static int |
1964 | | make_ga_colormap(png_image_read_control *display) |
1965 | 0 | { |
1966 | 0 | unsigned int i, a; |
1967 | | |
1968 | | /* Alpha is retained, the output will be a color-map with entries |
1969 | | * selected by six levels of alpha. One transparent entry, 6 gray |
1970 | | * levels for all the intermediate alpha values, leaving 230 entries |
1971 | | * for the opaque grays. The color-map entries are the six values |
1972 | | * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the |
1973 | | * relevant entry. |
1974 | | * |
1975 | | * if (alpha > 229) // opaque |
1976 | | * { |
1977 | | * // The 231 entries are selected to make the math below work: |
1978 | | * base = 0; |
1979 | | * entry = (231 * gray + 128) >> 8; |
1980 | | * } |
1981 | | * else if (alpha < 26) // transparent |
1982 | | * { |
1983 | | * base = 231; |
1984 | | * entry = 0; |
1985 | | * } |
1986 | | * else // partially opaque |
1987 | | * { |
1988 | | * base = 226 + 6 * PNG_DIV51(alpha); |
1989 | | * entry = PNG_DIV51(gray); |
1990 | | * } |
1991 | | */ |
1992 | 0 | i = 0; |
1993 | 0 | while (i < 231) |
1994 | 0 | { |
1995 | 0 | unsigned int gray = (i * 256 + 115) / 231; |
1996 | 0 | png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB); |
1997 | 0 | } |
1998 | | |
1999 | | /* 255 is used here for the component values for consistency with the code |
2000 | | * that undoes premultiplication in pngwrite.c. |
2001 | | */ |
2002 | 0 | png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB); |
2003 | |
|
2004 | 0 | for (a=1; a<5; ++a) |
2005 | 0 | { |
2006 | 0 | unsigned int g; |
2007 | |
|
2008 | 0 | for (g=0; g<6; ++g) |
2009 | 0 | png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, |
2010 | 0 | P_sRGB); |
2011 | 0 | } |
2012 | |
|
2013 | 0 | return (int)i; |
2014 | 0 | } |
2015 | | |
2016 | 0 | #define PNG_GA_COLORMAP_ENTRIES 256 |
2017 | | |
2018 | | static int |
2019 | | make_rgb_colormap(png_image_read_control *display) |
2020 | 0 | { |
2021 | 0 | unsigned int i, r; |
2022 | | |
2023 | | /* Build a 6x6x6 opaque RGB cube */ |
2024 | 0 | for (i=r=0; r<6; ++r) |
2025 | 0 | { |
2026 | 0 | unsigned int g; |
2027 | |
|
2028 | 0 | for (g=0; g<6; ++g) |
2029 | 0 | { |
2030 | 0 | unsigned int b; |
2031 | |
|
2032 | 0 | for (b=0; b<6; ++b) |
2033 | 0 | png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, |
2034 | 0 | P_sRGB); |
2035 | 0 | } |
2036 | 0 | } |
2037 | |
|
2038 | 0 | return (int)i; |
2039 | 0 | } |
2040 | | |
2041 | 0 | #define PNG_RGB_COLORMAP_ENTRIES 216 |
2042 | | |
2043 | | /* Return a palette index to the above palette given three 8-bit sRGB values. */ |
2044 | | #define PNG_RGB_INDEX(r,g,b) \ |
2045 | 0 | ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) |
2046 | | |
2047 | | static int |
2048 | | png_image_read_colormap(void *argument) |
2049 | 0 | { |
2050 | 0 | png_image_read_control *display = |
2051 | 0 | png_voidcast(png_image_read_control*, argument); |
2052 | 0 | png_image *image = display->image; |
2053 | |
|
2054 | 0 | png_struct *png_ptr = image->opaque->png_ptr; |
2055 | 0 | png_uint_32 output_format = image->format; |
2056 | 0 | int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ? |
2057 | 0 | P_LINEAR : P_sRGB; |
2058 | |
|
2059 | 0 | unsigned int cmap_entries; |
2060 | 0 | unsigned int output_processing; /* Output processing option */ |
2061 | 0 | unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */ |
2062 | | |
2063 | | /* Background information; the background color and the index of this color |
2064 | | * in the color-map if it exists (else 256). |
2065 | | */ |
2066 | 0 | unsigned int background_index = 256; |
2067 | 0 | png_uint_32 back_r, back_g, back_b; |
2068 | | |
2069 | | /* Flags to accumulate things that need to be done to the input. */ |
2070 | 0 | int expand_tRNS = 0; |
2071 | | |
2072 | | /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is |
2073 | | * very difficult to do, the results look awful, and it is difficult to see |
2074 | | * what possible use it is because the application can't control the |
2075 | | * color-map. |
2076 | | */ |
2077 | 0 | if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || |
2078 | 0 | png_ptr->num_trans > 0) /* alpha in input */ && |
2079 | 0 | ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) |
2080 | 0 | { |
2081 | 0 | if (output_encoding == P_LINEAR) /* compose on black */ |
2082 | 0 | back_b = back_g = back_r = 0; |
2083 | | |
2084 | 0 | else if (display->background == NULL /* no way to remove it */) |
2085 | 0 | png_error(png_ptr, |
2086 | 0 | "background color must be supplied to remove alpha/transparency"); |
2087 | | |
2088 | | /* Get a copy of the background color (this avoids repeating the checks |
2089 | | * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the |
2090 | | * output format. |
2091 | | */ |
2092 | 0 | else |
2093 | 0 | { |
2094 | 0 | back_g = display->background->green; |
2095 | 0 | if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0) |
2096 | 0 | { |
2097 | 0 | back_r = display->background->red; |
2098 | 0 | back_b = display->background->blue; |
2099 | 0 | } |
2100 | 0 | else |
2101 | 0 | back_b = back_r = back_g; |
2102 | 0 | } |
2103 | 0 | } |
2104 | | |
2105 | 0 | else if (output_encoding == P_LINEAR) |
2106 | 0 | back_b = back_r = back_g = 65535; |
2107 | | |
2108 | 0 | else |
2109 | 0 | back_b = back_r = back_g = 255; |
2110 | | |
2111 | | /* Default the input file gamma if required - this is necessary because |
2112 | | * libpng assumes that if no gamma information is present the data is in the |
2113 | | * output format, but the simplified API deduces the gamma from the input |
2114 | | * format. The 'default' gamma value is also set by png_set_alpha_mode, but |
2115 | | * this is happening before any such call, so: |
2116 | | * |
2117 | | * TODO: should be an internal API and all this code should be copied into a |
2118 | | * single common gamma+colorspace file. |
2119 | | */ |
2120 | 0 | if (png_ptr->bit_depth == 16 && |
2121 | 0 | (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) |
2122 | 0 | png_ptr->default_gamma = PNG_GAMMA_LINEAR; |
2123 | | |
2124 | 0 | else |
2125 | 0 | png_ptr->default_gamma = PNG_GAMMA_sRGB_INVERSE; |
2126 | | |
2127 | | /* Decide what to do based on the PNG color type of the input data. The |
2128 | | * utility function png_create_colormap_entry deals with most aspects of the |
2129 | | * output transformations; this code works out how to produce bytes of |
2130 | | * color-map entries from the original format. |
2131 | | */ |
2132 | 0 | switch (png_ptr->color_type) |
2133 | 0 | { |
2134 | 0 | case PNG_COLOR_TYPE_GRAY: |
2135 | 0 | if (png_ptr->bit_depth <= 8) |
2136 | 0 | { |
2137 | | /* There at most 256 colors in the output, regardless of |
2138 | | * transparency. |
2139 | | */ |
2140 | 0 | unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; |
2141 | |
|
2142 | 0 | cmap_entries = 1U << png_ptr->bit_depth; |
2143 | 0 | if (cmap_entries > image->colormap_entries) |
2144 | 0 | png_error(png_ptr, "gray[8] color-map: too few entries"); |
2145 | | |
2146 | 0 | step = 255 / (cmap_entries - 1); |
2147 | 0 | output_processing = PNG_CMAP_NONE; |
2148 | | |
2149 | | /* If there is a tRNS chunk then this either selects a transparent |
2150 | | * value or, if the output has no alpha, the background color. |
2151 | | */ |
2152 | 0 | if (png_ptr->num_trans > 0) |
2153 | 0 | { |
2154 | 0 | trans = png_ptr->trans_color.gray; |
2155 | |
|
2156 | 0 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) |
2157 | 0 | back_alpha = output_encoding == P_LINEAR ? 65535 : 255; |
2158 | 0 | } |
2159 | | |
2160 | | /* png_create_colormap_entry just takes an RGBA and writes the |
2161 | | * corresponding color-map entry using the format from 'image', |
2162 | | * including the required conversion to sRGB or linear as |
2163 | | * appropriate. The input values are always either sRGB (if the |
2164 | | * gamma correction flag is 0) or 0..255 scaled file encoded values |
2165 | | * (if the function must gamma correct them). |
2166 | | */ |
2167 | 0 | for (i=val=0; i<cmap_entries; ++i, val += step) |
2168 | 0 | { |
2169 | | /* 'i' is a file value. While this will result in duplicated |
2170 | | * entries for 8-bit non-sRGB encoded files it is necessary to |
2171 | | * have non-gamma corrected values to do tRNS handling. |
2172 | | */ |
2173 | 0 | if (i != trans) |
2174 | 0 | png_create_colormap_entry(display, i, val, val, val, 255, |
2175 | 0 | P_FILE/*8-bit with file gamma*/); |
2176 | | |
2177 | | /* Else this entry is transparent. The colors don't matter if |
2178 | | * there is an alpha channel (back_alpha == 0), but it does no |
2179 | | * harm to pass them in; the values are not set above so this |
2180 | | * passes in white. |
2181 | | * |
2182 | | * NOTE: this preserves the full precision of the application |
2183 | | * supplied background color when it is used. |
2184 | | */ |
2185 | 0 | else |
2186 | 0 | png_create_colormap_entry(display, i, back_r, back_g, back_b, |
2187 | 0 | back_alpha, output_encoding); |
2188 | 0 | } |
2189 | | |
2190 | | /* We need libpng to preserve the original encoding. */ |
2191 | 0 | data_encoding = P_FILE; |
2192 | | |
2193 | | /* The rows from libpng, while technically gray values, are now also |
2194 | | * color-map indices; however, they may need to be expanded to 1 |
2195 | | * byte per pixel. This is what png_set_packing does (i.e., it |
2196 | | * unpacks the bit values into bytes.) |
2197 | | */ |
2198 | 0 | if (png_ptr->bit_depth < 8) |
2199 | 0 | png_set_packing(png_ptr); |
2200 | 0 | } |
2201 | | |
2202 | 0 | else /* bit depth is 16 */ |
2203 | 0 | { |
2204 | | /* The 16-bit input values can be converted directly to 8-bit gamma |
2205 | | * encoded values; however, if a tRNS chunk is present 257 color-map |
2206 | | * entries are required. This means that the extra entry requires |
2207 | | * special processing; add an alpha channel, sacrifice gray level |
2208 | | * 254 and convert transparent (alpha==0) entries to that. |
2209 | | * |
2210 | | * Use libpng to chop the data to 8 bits. Convert it to sRGB at the |
2211 | | * same time to minimize quality loss. If a tRNS chunk is present |
2212 | | * this means libpng must handle it too; otherwise it is impossible |
2213 | | * to do the exact match on the 16-bit value. |
2214 | | * |
2215 | | * If the output has no alpha channel *and* the background color is |
2216 | | * gray then it is possible to let libpng handle the substitution by |
2217 | | * ensuring that the corresponding gray level matches the background |
2218 | | * color exactly. |
2219 | | */ |
2220 | 0 | data_encoding = P_sRGB; |
2221 | |
|
2222 | 0 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
2223 | 0 | png_error(png_ptr, "gray[16] color-map: too few entries"); |
2224 | | |
2225 | 0 | cmap_entries = (unsigned int)make_gray_colormap(display); |
2226 | |
|
2227 | 0 | if (png_ptr->num_trans > 0) |
2228 | 0 | { |
2229 | 0 | unsigned int back_alpha; |
2230 | |
|
2231 | 0 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
2232 | 0 | back_alpha = 0; |
2233 | | |
2234 | 0 | else |
2235 | 0 | { |
2236 | 0 | if (back_r == back_g && back_g == back_b) |
2237 | 0 | { |
2238 | | /* Background is gray; no special processing will be |
2239 | | * required. |
2240 | | */ |
2241 | 0 | png_color_16 c; |
2242 | 0 | png_uint_32 gray = back_g; |
2243 | |
|
2244 | 0 | if (output_encoding == P_LINEAR) |
2245 | 0 | { |
2246 | 0 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
2247 | | |
2248 | | /* And make sure the corresponding palette entry |
2249 | | * matches. |
2250 | | */ |
2251 | 0 | png_create_colormap_entry(display, gray, back_g, back_g, |
2252 | 0 | back_g, 65535, P_LINEAR); |
2253 | 0 | } |
2254 | | |
2255 | | /* The background passed to libpng, however, must be the |
2256 | | * sRGB value. |
2257 | | */ |
2258 | 0 | c.index = 0; /*unused*/ |
2259 | 0 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
2260 | | |
2261 | | /* NOTE: does this work without expanding tRNS to alpha? |
2262 | | * It should be the color->gray case below apparently |
2263 | | * doesn't. |
2264 | | */ |
2265 | 0 | png_set_background_fixed(png_ptr, &c, |
2266 | 0 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
2267 | 0 | 0/*gamma: not used*/); |
2268 | |
|
2269 | 0 | output_processing = PNG_CMAP_NONE; |
2270 | 0 | break; |
2271 | 0 | } |
2272 | | #ifdef __COVERITY__ |
2273 | | /* Coverity claims that output_encoding cannot be 2 (P_LINEAR) |
2274 | | * here. |
2275 | | */ |
2276 | | back_alpha = 255; |
2277 | | #else |
2278 | 0 | back_alpha = output_encoding == P_LINEAR ? 65535 : 255; |
2279 | 0 | #endif |
2280 | 0 | } |
2281 | | |
2282 | | /* output_processing means that the libpng-processed row will be |
2283 | | * 8-bit GA and it has to be processing to single byte color-map |
2284 | | * values. Entry 254 is replaced by either a completely |
2285 | | * transparent entry or by the background color at full |
2286 | | * precision (and the background color is not a simple gray |
2287 | | * level in this case.) |
2288 | | */ |
2289 | 0 | expand_tRNS = 1; |
2290 | 0 | output_processing = PNG_CMAP_TRANS; |
2291 | 0 | background_index = 254; |
2292 | | |
2293 | | /* And set (overwrite) color-map entry 254 to the actual |
2294 | | * background color at full precision. |
2295 | | */ |
2296 | 0 | png_create_colormap_entry(display, 254, back_r, back_g, back_b, |
2297 | 0 | back_alpha, output_encoding); |
2298 | 0 | } |
2299 | | |
2300 | 0 | else |
2301 | 0 | output_processing = PNG_CMAP_NONE; |
2302 | 0 | } |
2303 | 0 | break; |
2304 | | |
2305 | 0 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
2306 | | /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum |
2307 | | * of 65536 combinations. If, however, the alpha channel is to be |
2308 | | * removed there are only 256 possibilities if the background is gray. |
2309 | | * (Otherwise there is a subset of the 65536 possibilities defined by |
2310 | | * the triangle between black, white and the background color.) |
2311 | | * |
2312 | | * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to |
2313 | | * worry about tRNS matching - tRNS is ignored if there is an alpha |
2314 | | * channel. |
2315 | | */ |
2316 | 0 | data_encoding = P_sRGB; |
2317 | |
|
2318 | 0 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
2319 | 0 | { |
2320 | 0 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
2321 | 0 | png_error(png_ptr, "gray+alpha color-map: too few entries"); |
2322 | | |
2323 | 0 | cmap_entries = (unsigned int)make_ga_colormap(display); |
2324 | |
|
2325 | 0 | background_index = PNG_CMAP_GA_BACKGROUND; |
2326 | 0 | output_processing = PNG_CMAP_GA; |
2327 | 0 | } |
2328 | | |
2329 | 0 | else /* alpha is removed */ |
2330 | 0 | { |
2331 | | /* Alpha must be removed as the PNG data is processed when the |
2332 | | * background is a color because the G and A channels are |
2333 | | * independent and the vector addition (non-parallel vectors) is a |
2334 | | * 2-D problem. |
2335 | | * |
2336 | | * This can be reduced to the same algorithm as above by making a |
2337 | | * colormap containing gray levels (for the opaque grays), a |
2338 | | * background entry (for a transparent pixel) and a set of four six |
2339 | | * level color values, one set for each intermediate alpha value. |
2340 | | * See the comments in make_ga_colormap for how this works in the |
2341 | | * per-pixel processing. |
2342 | | * |
2343 | | * If the background is gray, however, we only need a 256 entry gray |
2344 | | * level color map. It is sufficient to make the entry generated |
2345 | | * for the background color be exactly the color specified. |
2346 | | */ |
2347 | 0 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || |
2348 | 0 | (back_r == back_g && back_g == back_b)) |
2349 | 0 | { |
2350 | | /* Background is gray; no special processing will be required. */ |
2351 | 0 | png_color_16 c; |
2352 | 0 | png_uint_32 gray = back_g; |
2353 | |
|
2354 | 0 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
2355 | 0 | png_error(png_ptr, "gray-alpha color-map: too few entries"); |
2356 | | |
2357 | 0 | cmap_entries = (unsigned int)make_gray_colormap(display); |
2358 | |
|
2359 | 0 | if (output_encoding == P_LINEAR) |
2360 | 0 | { |
2361 | 0 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
2362 | | |
2363 | | /* And make sure the corresponding palette entry matches. */ |
2364 | 0 | png_create_colormap_entry(display, gray, back_g, back_g, |
2365 | 0 | back_g, 65535, P_LINEAR); |
2366 | 0 | } |
2367 | | |
2368 | | /* The background passed to libpng, however, must be the sRGB |
2369 | | * value. |
2370 | | */ |
2371 | 0 | c.index = 0; /*unused*/ |
2372 | 0 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
2373 | |
|
2374 | 0 | png_set_background_fixed(png_ptr, &c, |
2375 | 0 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
2376 | 0 | 0/*gamma: not used*/); |
2377 | |
|
2378 | 0 | output_processing = PNG_CMAP_NONE; |
2379 | 0 | } |
2380 | | |
2381 | 0 | else |
2382 | 0 | { |
2383 | 0 | png_uint_32 i, a; |
2384 | | |
2385 | | /* This is the same as png_make_ga_colormap, above, except that |
2386 | | * the entries are all opaque. |
2387 | | */ |
2388 | 0 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
2389 | 0 | png_error(png_ptr, "ga-alpha color-map: too few entries"); |
2390 | | |
2391 | 0 | i = 0; |
2392 | 0 | while (i < 231) |
2393 | 0 | { |
2394 | 0 | png_uint_32 gray = (i * 256 + 115) / 231; |
2395 | 0 | png_create_colormap_entry(display, i++, gray, gray, gray, |
2396 | 0 | 255, P_sRGB); |
2397 | 0 | } |
2398 | | |
2399 | | /* NOTE: this preserves the full precision of the application |
2400 | | * background color. |
2401 | | */ |
2402 | 0 | background_index = i; |
2403 | 0 | png_create_colormap_entry(display, i++, back_r, back_g, back_b, |
2404 | | #ifdef __COVERITY__ |
2405 | | /* Coverity claims that output_encoding |
2406 | | * cannot be 2 (P_LINEAR) here. |
2407 | | */ 255U, |
2408 | | #else |
2409 | 0 | output_encoding == P_LINEAR ? 65535U : 255U, |
2410 | 0 | #endif |
2411 | 0 | output_encoding); |
2412 | | |
2413 | | /* For non-opaque input composite on the sRGB background - this |
2414 | | * requires inverting the encoding for each component. The input |
2415 | | * is still converted to the sRGB encoding because this is a |
2416 | | * reasonable approximate to the logarithmic curve of human |
2417 | | * visual sensitivity, at least over the narrow range which PNG |
2418 | | * represents. Consequently 'G' is always sRGB encoded, while |
2419 | | * 'A' is linear. We need the linear background colors. |
2420 | | */ |
2421 | 0 | if (output_encoding == P_sRGB) /* else already linear */ |
2422 | 0 | { |
2423 | | /* This may produce a value not exactly matching the |
2424 | | * background, but that's ok because these numbers are only |
2425 | | * used when alpha != 0 |
2426 | | */ |
2427 | 0 | back_r = png_sRGB_table[back_r]; |
2428 | 0 | back_g = png_sRGB_table[back_g]; |
2429 | 0 | back_b = png_sRGB_table[back_b]; |
2430 | 0 | } |
2431 | |
|
2432 | 0 | for (a=1; a<5; ++a) |
2433 | 0 | { |
2434 | 0 | unsigned int g; |
2435 | | |
2436 | | /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled |
2437 | | * by an 8-bit alpha value (0..255). |
2438 | | */ |
2439 | 0 | png_uint_32 alpha = 51 * a; |
2440 | 0 | png_uint_32 back_rx = (255-alpha) * back_r; |
2441 | 0 | png_uint_32 back_gx = (255-alpha) * back_g; |
2442 | 0 | png_uint_32 back_bx = (255-alpha) * back_b; |
2443 | |
|
2444 | 0 | for (g=0; g<6; ++g) |
2445 | 0 | { |
2446 | 0 | png_uint_32 gray = png_sRGB_table[g*51] * alpha; |
2447 | |
|
2448 | 0 | png_create_colormap_entry(display, i++, |
2449 | 0 | PNG_sRGB_FROM_LINEAR(gray + back_rx), |
2450 | 0 | PNG_sRGB_FROM_LINEAR(gray + back_gx), |
2451 | 0 | PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB); |
2452 | 0 | } |
2453 | 0 | } |
2454 | |
|
2455 | 0 | cmap_entries = i; |
2456 | 0 | output_processing = PNG_CMAP_GA; |
2457 | 0 | } |
2458 | 0 | } |
2459 | 0 | break; |
2460 | | |
2461 | 0 | case PNG_COLOR_TYPE_RGB: |
2462 | 0 | case PNG_COLOR_TYPE_RGB_ALPHA: |
2463 | | /* Exclude the case where the output is gray; we can always handle this |
2464 | | * with the cases above. |
2465 | | */ |
2466 | 0 | if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) |
2467 | 0 | { |
2468 | | /* The color-map will be grayscale, so we may as well convert the |
2469 | | * input RGB values to a simple grayscale and use the grayscale |
2470 | | * code above. |
2471 | | * |
2472 | | * NOTE: calling this apparently damages the recognition of the |
2473 | | * transparent color in background color handling; call |
2474 | | * png_set_tRNS_to_alpha before png_set_background_fixed. |
2475 | | */ |
2476 | 0 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, |
2477 | 0 | -1); |
2478 | 0 | data_encoding = P_sRGB; |
2479 | | |
2480 | | /* The output will now be one or two 8-bit gray or gray+alpha |
2481 | | * channels. The more complex case arises when the input has alpha. |
2482 | | */ |
2483 | 0 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
2484 | 0 | png_ptr->num_trans > 0) && |
2485 | 0 | (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
2486 | 0 | { |
2487 | | /* Both input and output have an alpha channel, so no background |
2488 | | * processing is required; just map the GA bytes to the right |
2489 | | * color-map entry. |
2490 | | */ |
2491 | 0 | expand_tRNS = 1; |
2492 | |
|
2493 | 0 | if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
2494 | 0 | png_error(png_ptr, "rgb[ga] color-map: too few entries"); |
2495 | | |
2496 | 0 | cmap_entries = (unsigned int)make_ga_colormap(display); |
2497 | 0 | background_index = PNG_CMAP_GA_BACKGROUND; |
2498 | 0 | output_processing = PNG_CMAP_GA; |
2499 | 0 | } |
2500 | | |
2501 | 0 | else |
2502 | 0 | { |
2503 | 0 | const png_fixed_point gamma = png_resolve_file_gamma(png_ptr); |
2504 | | |
2505 | | /* Either the input or the output has no alpha channel, so there |
2506 | | * will be no non-opaque pixels in the color-map; it will just be |
2507 | | * grayscale. |
2508 | | */ |
2509 | 0 | if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
2510 | 0 | png_error(png_ptr, "rgb[gray] color-map: too few entries"); |
2511 | | |
2512 | | /* Ideally this code would use libpng to do the gamma correction, |
2513 | | * but if an input alpha channel is to be removed we will hit the |
2514 | | * libpng bug in gamma+compose+rgb-to-gray (the double gamma |
2515 | | * correction bug). Fix this by dropping the gamma correction in |
2516 | | * this case and doing it in the palette; this will result in |
2517 | | * duplicate palette entries, but that's better than the |
2518 | | * alternative of double gamma correction. |
2519 | | * |
2520 | | * NOTE: PNGv3: check the resolved result of all the potentially |
2521 | | * different colour space chunks. |
2522 | | */ |
2523 | 0 | if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
2524 | 0 | png_ptr->num_trans > 0) && |
2525 | 0 | png_gamma_not_sRGB(gamma) != 0) |
2526 | 0 | { |
2527 | 0 | cmap_entries = (unsigned int)make_gray_file_colormap(display); |
2528 | 0 | data_encoding = P_FILE; |
2529 | 0 | } |
2530 | | |
2531 | 0 | else |
2532 | 0 | cmap_entries = (unsigned int)make_gray_colormap(display); |
2533 | | |
2534 | | /* But if the input has alpha or transparency it must be removed |
2535 | | */ |
2536 | 0 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
2537 | 0 | png_ptr->num_trans > 0) |
2538 | 0 | { |
2539 | 0 | png_color_16 c; |
2540 | 0 | png_uint_32 gray = back_g; |
2541 | | |
2542 | | /* We need to ensure that the application background exists in |
2543 | | * the colormap and that completely transparent pixels map to |
2544 | | * it. Achieve this simply by ensuring that the entry |
2545 | | * selected for the background really is the background color. |
2546 | | */ |
2547 | 0 | if (data_encoding == P_FILE) /* from the fixup above */ |
2548 | 0 | { |
2549 | | /* The app supplied a gray which is in output_encoding, we |
2550 | | * need to convert it to a value of the input (P_FILE) |
2551 | | * encoding then set this palette entry to the required |
2552 | | * output encoding. |
2553 | | */ |
2554 | 0 | if (output_encoding == P_sRGB) |
2555 | 0 | gray = png_sRGB_table[gray]; /* now P_LINEAR */ |
2556 | |
|
2557 | 0 | gray = PNG_DIV257(png_gamma_16bit_correct(gray, gamma)); |
2558 | | /* now P_FILE */ |
2559 | | |
2560 | | /* And make sure the corresponding palette entry contains |
2561 | | * exactly the required sRGB value. |
2562 | | */ |
2563 | 0 | png_create_colormap_entry(display, gray, back_g, back_g, |
2564 | 0 | back_g, 0/*unused*/, output_encoding); |
2565 | 0 | } |
2566 | | |
2567 | 0 | else if (output_encoding == P_LINEAR) |
2568 | 0 | { |
2569 | 0 | gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
2570 | | |
2571 | | /* And make sure the corresponding palette entry matches. |
2572 | | */ |
2573 | 0 | png_create_colormap_entry(display, gray, back_g, back_g, |
2574 | 0 | back_g, 0/*unused*/, P_LINEAR); |
2575 | 0 | } |
2576 | | |
2577 | | /* The background passed to libpng, however, must be the |
2578 | | * output (normally sRGB) value. |
2579 | | */ |
2580 | 0 | c.index = 0; /*unused*/ |
2581 | 0 | c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
2582 | | |
2583 | | /* NOTE: the following is apparently a bug in libpng. Without |
2584 | | * it the transparent color recognition in |
2585 | | * png_set_background_fixed seems to go wrong. |
2586 | | */ |
2587 | 0 | expand_tRNS = 1; |
2588 | 0 | png_set_background_fixed(png_ptr, &c, |
2589 | 0 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
2590 | 0 | 0/*gamma: not used*/); |
2591 | 0 | } |
2592 | |
|
2593 | 0 | output_processing = PNG_CMAP_NONE; |
2594 | 0 | } |
2595 | 0 | } |
2596 | | |
2597 | 0 | else /* output is color */ |
2598 | 0 | { |
2599 | | /* We could use png_quantize here so long as there is no transparent |
2600 | | * color or alpha; png_quantize ignores alpha. Easier overall just |
2601 | | * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. |
2602 | | * Consequently we always want libpng to produce sRGB data. |
2603 | | */ |
2604 | 0 | data_encoding = P_sRGB; |
2605 | | |
2606 | | /* Is there any transparency or alpha? */ |
2607 | 0 | if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
2608 | 0 | png_ptr->num_trans > 0) |
2609 | 0 | { |
2610 | | /* Is there alpha in the output too? If so all four channels are |
2611 | | * processed into a special RGB cube with alpha support. |
2612 | | */ |
2613 | 0 | if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
2614 | 0 | { |
2615 | 0 | png_uint_32 r; |
2616 | |
|
2617 | 0 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
2618 | 0 | png_error(png_ptr, "rgb+alpha color-map: too few entries"); |
2619 | | |
2620 | 0 | cmap_entries = (unsigned int)make_rgb_colormap(display); |
2621 | | |
2622 | | /* Add a transparent entry. */ |
2623 | 0 | png_create_colormap_entry(display, cmap_entries, 255, 255, |
2624 | 0 | 255, 0, P_sRGB); |
2625 | | |
2626 | | /* This is stored as the background index for the processing |
2627 | | * algorithm. |
2628 | | */ |
2629 | 0 | background_index = cmap_entries++; |
2630 | | |
2631 | | /* Add 27 r,g,b entries each with alpha 0.5. */ |
2632 | 0 | for (r=0; r<256; r = (r << 1) | 0x7f) |
2633 | 0 | { |
2634 | 0 | png_uint_32 g; |
2635 | |
|
2636 | 0 | for (g=0; g<256; g = (g << 1) | 0x7f) |
2637 | 0 | { |
2638 | 0 | png_uint_32 b; |
2639 | | |
2640 | | /* This generates components with the values 0, 127 and |
2641 | | * 255 |
2642 | | */ |
2643 | 0 | for (b=0; b<256; b = (b << 1) | 0x7f) |
2644 | 0 | png_create_colormap_entry(display, cmap_entries++, |
2645 | 0 | r, g, b, 128, P_sRGB); |
2646 | 0 | } |
2647 | 0 | } |
2648 | |
|
2649 | 0 | expand_tRNS = 1; |
2650 | 0 | output_processing = PNG_CMAP_RGB_ALPHA; |
2651 | 0 | } |
2652 | | |
2653 | 0 | else |
2654 | 0 | { |
2655 | | /* Alpha/transparency must be removed. The background must |
2656 | | * exist in the color map (achieved by setting adding it after |
2657 | | * the 666 color-map). If the standard processing code will |
2658 | | * pick up this entry automatically that's all that is |
2659 | | * required; libpng can be called to do the background |
2660 | | * processing. |
2661 | | */ |
2662 | 0 | unsigned int sample_size = |
2663 | 0 | PNG_IMAGE_SAMPLE_SIZE(output_format); |
2664 | 0 | png_uint_32 r, g, b; /* sRGB background */ |
2665 | |
|
2666 | 0 | if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
2667 | 0 | png_error(png_ptr, "rgb-alpha color-map: too few entries"); |
2668 | | |
2669 | 0 | cmap_entries = (unsigned int)make_rgb_colormap(display); |
2670 | |
|
2671 | 0 | png_create_colormap_entry(display, cmap_entries, back_r, |
2672 | 0 | back_g, back_b, 0/*unused*/, output_encoding); |
2673 | |
|
2674 | 0 | if (output_encoding == P_LINEAR) |
2675 | 0 | { |
2676 | 0 | r = PNG_sRGB_FROM_LINEAR(back_r * 255); |
2677 | 0 | g = PNG_sRGB_FROM_LINEAR(back_g * 255); |
2678 | 0 | b = PNG_sRGB_FROM_LINEAR(back_b * 255); |
2679 | 0 | } |
2680 | | |
2681 | 0 | else |
2682 | 0 | { |
2683 | 0 | r = back_r; |
2684 | 0 | g = back_g; |
2685 | 0 | b = back_g; |
2686 | 0 | } |
2687 | | |
2688 | | /* Compare the newly-created color-map entry with the one the |
2689 | | * PNG_CMAP_RGB algorithm will use. If the two entries don't |
2690 | | * match, add the new one and set this as the background |
2691 | | * index. |
2692 | | */ |
2693 | 0 | if (memcmp((const png_byte *)display->colormap + |
2694 | 0 | sample_size * cmap_entries, |
2695 | 0 | (const png_byte *)display->colormap + |
2696 | 0 | sample_size * PNG_RGB_INDEX(r,g,b), |
2697 | 0 | sample_size) != 0) |
2698 | 0 | { |
2699 | | /* The background color must be added. */ |
2700 | 0 | background_index = cmap_entries++; |
2701 | | |
2702 | | /* Add 27 r,g,b entries each with created by composing with |
2703 | | * the background at alpha 0.5. |
2704 | | */ |
2705 | 0 | for (r=0; r<256; r = (r << 1) | 0x7f) |
2706 | 0 | { |
2707 | 0 | for (g=0; g<256; g = (g << 1) | 0x7f) |
2708 | 0 | { |
2709 | | /* This generates components with the values 0, 127 |
2710 | | * and 255 |
2711 | | */ |
2712 | 0 | for (b=0; b<256; b = (b << 1) | 0x7f) |
2713 | 0 | png_create_colormap_entry(display, cmap_entries++, |
2714 | 0 | png_colormap_compose(display, r, P_sRGB, 128, |
2715 | 0 | back_r, output_encoding), |
2716 | 0 | png_colormap_compose(display, g, P_sRGB, 128, |
2717 | 0 | back_g, output_encoding), |
2718 | 0 | png_colormap_compose(display, b, P_sRGB, 128, |
2719 | 0 | back_b, output_encoding), |
2720 | 0 | 0/*unused*/, output_encoding); |
2721 | 0 | } |
2722 | 0 | } |
2723 | |
|
2724 | 0 | expand_tRNS = 1; |
2725 | 0 | output_processing = PNG_CMAP_RGB_ALPHA; |
2726 | 0 | } |
2727 | | |
2728 | 0 | else /* background color is in the standard color-map */ |
2729 | 0 | { |
2730 | 0 | png_color_16 c; |
2731 | |
|
2732 | 0 | c.index = 0; /*unused*/ |
2733 | 0 | c.red = (png_uint_16)back_r; |
2734 | 0 | c.gray = c.green = (png_uint_16)back_g; |
2735 | 0 | c.blue = (png_uint_16)back_b; |
2736 | |
|
2737 | 0 | png_set_background_fixed(png_ptr, &c, |
2738 | 0 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
2739 | 0 | 0/*gamma: not used*/); |
2740 | |
|
2741 | 0 | output_processing = PNG_CMAP_RGB; |
2742 | 0 | } |
2743 | 0 | } |
2744 | 0 | } |
2745 | | |
2746 | 0 | else /* no alpha or transparency in the input */ |
2747 | 0 | { |
2748 | | /* Alpha in the output is irrelevant, simply map the opaque input |
2749 | | * pixels to the 6x6x6 color-map. |
2750 | | */ |
2751 | 0 | if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) |
2752 | 0 | png_error(png_ptr, "rgb color-map: too few entries"); |
2753 | | |
2754 | 0 | cmap_entries = (unsigned int)make_rgb_colormap(display); |
2755 | 0 | output_processing = PNG_CMAP_RGB; |
2756 | 0 | } |
2757 | 0 | } |
2758 | 0 | break; |
2759 | | |
2760 | 0 | case PNG_COLOR_TYPE_PALETTE: |
2761 | | /* It's already got a color-map. It may be necessary to eliminate the |
2762 | | * tRNS entries though. |
2763 | | */ |
2764 | 0 | { |
2765 | 0 | unsigned int num_trans = png_ptr->num_trans; |
2766 | 0 | const png_byte *trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; |
2767 | 0 | const png_color *colormap = png_ptr->palette; |
2768 | 0 | int do_background = trans != NULL && |
2769 | 0 | (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; |
2770 | 0 | unsigned int i; |
2771 | | |
2772 | | /* Just in case: */ |
2773 | 0 | if (trans == NULL) |
2774 | 0 | num_trans = 0; |
2775 | |
|
2776 | 0 | output_processing = PNG_CMAP_NONE; |
2777 | 0 | data_encoding = P_FILE; /* Don't change from color-map indices */ |
2778 | 0 | cmap_entries = (unsigned int)png_ptr->num_palette; |
2779 | 0 | if (cmap_entries > 256) |
2780 | 0 | cmap_entries = 256; |
2781 | |
|
2782 | 0 | if (cmap_entries > (unsigned int)image->colormap_entries) |
2783 | 0 | png_error(png_ptr, "palette color-map: too few entries"); |
2784 | | |
2785 | 0 | for (i=0; i < cmap_entries; ++i) |
2786 | 0 | { |
2787 | 0 | if (do_background != 0 && i < num_trans && trans[i] < 255) |
2788 | 0 | { |
2789 | 0 | if (trans[i] == 0) |
2790 | 0 | png_create_colormap_entry(display, i, back_r, back_g, |
2791 | 0 | back_b, 0, output_encoding); |
2792 | | |
2793 | 0 | else |
2794 | 0 | { |
2795 | | /* Must compose the PNG file color in the color-map entry |
2796 | | * on the sRGB color in 'back'. |
2797 | | */ |
2798 | 0 | png_create_colormap_entry(display, i, |
2799 | 0 | png_colormap_compose(display, colormap[i].red, |
2800 | 0 | P_FILE, trans[i], back_r, output_encoding), |
2801 | 0 | png_colormap_compose(display, colormap[i].green, |
2802 | 0 | P_FILE, trans[i], back_g, output_encoding), |
2803 | 0 | png_colormap_compose(display, colormap[i].blue, |
2804 | 0 | P_FILE, trans[i], back_b, output_encoding), |
2805 | 0 | output_encoding == P_LINEAR ? trans[i] * 257U : |
2806 | 0 | trans[i], |
2807 | 0 | output_encoding); |
2808 | 0 | } |
2809 | 0 | } |
2810 | | |
2811 | 0 | else |
2812 | 0 | png_create_colormap_entry(display, i, colormap[i].red, |
2813 | 0 | colormap[i].green, colormap[i].blue, |
2814 | 0 | i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/); |
2815 | 0 | } |
2816 | | |
2817 | | /* The PNG data may have indices packed in fewer than 8 bits, it |
2818 | | * must be expanded if so. |
2819 | | */ |
2820 | 0 | if (png_ptr->bit_depth < 8) |
2821 | 0 | png_set_packing(png_ptr); |
2822 | 0 | } |
2823 | 0 | break; |
2824 | | |
2825 | 0 | default: |
2826 | 0 | png_error(png_ptr, "invalid PNG color type"); |
2827 | | /*NOT REACHED*/ |
2828 | 0 | } |
2829 | | |
2830 | | /* Now deal with the output processing */ |
2831 | 0 | if (expand_tRNS != 0 && png_ptr->num_trans > 0 && |
2832 | 0 | (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) |
2833 | 0 | png_set_tRNS_to_alpha(png_ptr); |
2834 | |
|
2835 | 0 | switch (data_encoding) |
2836 | 0 | { |
2837 | 0 | case P_sRGB: |
2838 | | /* Change to 8-bit sRGB */ |
2839 | 0 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); |
2840 | | /* FALLTHROUGH */ |
2841 | |
|
2842 | 0 | case P_FILE: |
2843 | 0 | if (png_ptr->bit_depth > 8) |
2844 | 0 | png_set_scale_16(png_ptr); |
2845 | 0 | break; |
2846 | | |
2847 | 0 | #ifdef __GNUC__ |
2848 | 0 | default: |
2849 | 0 | png_error(png_ptr, "bad data option (internal error)"); |
2850 | 0 | #endif |
2851 | 0 | } |
2852 | | |
2853 | 0 | if (cmap_entries > 256 || cmap_entries > image->colormap_entries) |
2854 | 0 | png_error(png_ptr, "color map overflow (BAD internal error)"); |
2855 | | |
2856 | 0 | image->colormap_entries = cmap_entries; |
2857 | | |
2858 | | /* Double check using the recorded background index */ |
2859 | 0 | switch (output_processing) |
2860 | 0 | { |
2861 | 0 | case PNG_CMAP_NONE: |
2862 | 0 | if (background_index != PNG_CMAP_NONE_BACKGROUND) |
2863 | 0 | goto bad_background; |
2864 | 0 | break; |
2865 | | |
2866 | 0 | case PNG_CMAP_GA: |
2867 | 0 | if (background_index != PNG_CMAP_GA_BACKGROUND) |
2868 | 0 | goto bad_background; |
2869 | 0 | break; |
2870 | | |
2871 | 0 | case PNG_CMAP_TRANS: |
2872 | 0 | if (background_index >= cmap_entries || |
2873 | 0 | background_index != PNG_CMAP_TRANS_BACKGROUND) |
2874 | 0 | goto bad_background; |
2875 | 0 | break; |
2876 | | |
2877 | 0 | case PNG_CMAP_RGB: |
2878 | 0 | if (background_index != PNG_CMAP_RGB_BACKGROUND) |
2879 | 0 | goto bad_background; |
2880 | 0 | break; |
2881 | | |
2882 | 0 | case PNG_CMAP_RGB_ALPHA: |
2883 | 0 | if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) |
2884 | 0 | goto bad_background; |
2885 | 0 | break; |
2886 | | |
2887 | 0 | default: |
2888 | 0 | png_error(png_ptr, "bad processing option (internal error)"); |
2889 | | |
2890 | 0 | bad_background: |
2891 | 0 | png_error(png_ptr, "bad background index (internal error)"); |
2892 | 0 | } |
2893 | | |
2894 | 0 | display->colormap_processing = (int)output_processing; |
2895 | |
|
2896 | 0 | return 1/*ok*/; |
2897 | 0 | } |
2898 | | |
2899 | | /* The final part of the color-map read called from png_image_finish_read. */ |
2900 | | static int |
2901 | | png_image_read_and_map(void *argument) |
2902 | 0 | { |
2903 | 0 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
2904 | 0 | argument); |
2905 | 0 | png_image *image = display->image; |
2906 | 0 | png_struct *png_ptr = image->opaque->png_ptr; |
2907 | 0 | int passes; |
2908 | | |
2909 | | /* Called when the libpng data must be transformed into the color-mapped |
2910 | | * form. There is a local row buffer in display->local and this routine must |
2911 | | * do the interlace handling. |
2912 | | */ |
2913 | 0 | switch (png_ptr->interlaced) |
2914 | 0 | { |
2915 | 0 | case PNG_INTERLACE_NONE: |
2916 | 0 | passes = 1; |
2917 | 0 | break; |
2918 | | |
2919 | 0 | case PNG_INTERLACE_ADAM7: |
2920 | 0 | passes = PNG_INTERLACE_ADAM7_PASSES; |
2921 | 0 | break; |
2922 | | |
2923 | 0 | default: |
2924 | 0 | png_error(png_ptr, "unknown interlace type"); |
2925 | 0 | } |
2926 | | |
2927 | 0 | { |
2928 | 0 | png_uint_32 height = image->height; |
2929 | 0 | png_uint_32 width = image->width; |
2930 | 0 | int proc = display->colormap_processing; |
2931 | 0 | png_byte *first_row = png_voidcast(png_byte *, display->first_row); |
2932 | 0 | ptrdiff_t step_row = display->row_bytes; |
2933 | 0 | int pass; |
2934 | |
|
2935 | 0 | for (pass = 0; pass < passes; ++pass) |
2936 | 0 | { |
2937 | 0 | unsigned int startx, stepx, stepy; |
2938 | 0 | png_uint_32 y; |
2939 | |
|
2940 | 0 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
2941 | 0 | { |
2942 | | /* The row may be empty for a short image: */ |
2943 | 0 | if (PNG_PASS_COLS(width, pass) == 0) |
2944 | 0 | continue; |
2945 | | |
2946 | 0 | startx = PNG_PASS_START_COL(pass); |
2947 | 0 | stepx = PNG_PASS_COL_OFFSET(pass); |
2948 | 0 | y = PNG_PASS_START_ROW(pass); |
2949 | 0 | stepy = PNG_PASS_ROW_OFFSET(pass); |
2950 | 0 | } |
2951 | | |
2952 | 0 | else |
2953 | 0 | { |
2954 | 0 | y = 0; |
2955 | 0 | startx = 0; |
2956 | 0 | stepx = stepy = 1; |
2957 | 0 | } |
2958 | | |
2959 | 0 | for (; y<height; y += stepy) |
2960 | 0 | { |
2961 | 0 | png_byte *inrow = png_voidcast(png_byte *, display->local_row); |
2962 | 0 | png_byte *outrow = first_row + y * step_row; |
2963 | 0 | png_byte *row_end = outrow + width; |
2964 | | |
2965 | | /* Read read the libpng data into the temporary buffer. */ |
2966 | 0 | png_read_row(png_ptr, inrow, NULL); |
2967 | | |
2968 | | /* Now process the row according to the processing option, note |
2969 | | * that the caller verifies that the format of the libpng output |
2970 | | * data is as required. |
2971 | | */ |
2972 | 0 | outrow += startx; |
2973 | 0 | switch (proc) |
2974 | 0 | { |
2975 | 0 | case PNG_CMAP_GA: |
2976 | 0 | for (; outrow < row_end; outrow += stepx) |
2977 | 0 | { |
2978 | | /* The data is always in the PNG order */ |
2979 | 0 | unsigned int gray = *inrow++; |
2980 | 0 | unsigned int alpha = *inrow++; |
2981 | 0 | unsigned int entry; |
2982 | | |
2983 | | /* NOTE: this code is copied as a comment in |
2984 | | * make_ga_colormap above. Please update the |
2985 | | * comment if you change this code! |
2986 | | */ |
2987 | 0 | if (alpha > 229) /* opaque */ |
2988 | 0 | { |
2989 | 0 | entry = (231 * gray + 128) >> 8; |
2990 | 0 | } |
2991 | 0 | else if (alpha < 26) /* transparent */ |
2992 | 0 | { |
2993 | 0 | entry = 231; |
2994 | 0 | } |
2995 | 0 | else /* partially opaque */ |
2996 | 0 | { |
2997 | 0 | entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); |
2998 | 0 | } |
2999 | |
|
3000 | 0 | *outrow = (png_byte)entry; |
3001 | 0 | } |
3002 | 0 | break; |
3003 | | |
3004 | 0 | case PNG_CMAP_TRANS: |
3005 | 0 | for (; outrow < row_end; outrow += stepx) |
3006 | 0 | { |
3007 | 0 | png_byte gray = *inrow++; |
3008 | 0 | png_byte alpha = *inrow++; |
3009 | |
|
3010 | 0 | if (alpha == 0) |
3011 | 0 | *outrow = PNG_CMAP_TRANS_BACKGROUND; |
3012 | | |
3013 | 0 | else if (gray != PNG_CMAP_TRANS_BACKGROUND) |
3014 | 0 | *outrow = gray; |
3015 | | |
3016 | 0 | else |
3017 | 0 | *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); |
3018 | 0 | } |
3019 | 0 | break; |
3020 | | |
3021 | 0 | case PNG_CMAP_RGB: |
3022 | 0 | for (; outrow < row_end; outrow += stepx) |
3023 | 0 | { |
3024 | 0 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); |
3025 | 0 | inrow += 3; |
3026 | 0 | } |
3027 | 0 | break; |
3028 | | |
3029 | 0 | case PNG_CMAP_RGB_ALPHA: |
3030 | 0 | for (; outrow < row_end; outrow += stepx) |
3031 | 0 | { |
3032 | 0 | unsigned int alpha = inrow[3]; |
3033 | | |
3034 | | /* Because the alpha entries only hold alpha==0.5 values |
3035 | | * split the processing at alpha==0.25 (64) and 0.75 |
3036 | | * (196). |
3037 | | */ |
3038 | |
|
3039 | 0 | if (alpha >= 196) |
3040 | 0 | *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], |
3041 | 0 | inrow[2]); |
3042 | | |
3043 | 0 | else if (alpha < 64) |
3044 | 0 | *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; |
3045 | | |
3046 | 0 | else |
3047 | 0 | { |
3048 | | /* Likewise there are three entries for each of r, g |
3049 | | * and b. We could select the entry by popcount on |
3050 | | * the top two bits on those architectures that |
3051 | | * support it, this is what the code below does, |
3052 | | * crudely. |
3053 | | */ |
3054 | 0 | unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; |
3055 | | |
3056 | | /* Here are how the values map: |
3057 | | * |
3058 | | * 0x00 .. 0x3f -> 0 |
3059 | | * 0x40 .. 0xbf -> 1 |
3060 | | * 0xc0 .. 0xff -> 2 |
3061 | | * |
3062 | | * So, as above with the explicit alpha checks, the |
3063 | | * breakpoints are at 64 and 196. |
3064 | | */ |
3065 | 0 | if (inrow[0] & 0x80) back_i += 9; /* red */ |
3066 | 0 | if (inrow[0] & 0x40) back_i += 9; |
3067 | 0 | if (inrow[0] & 0x80) back_i += 3; /* green */ |
3068 | 0 | if (inrow[0] & 0x40) back_i += 3; |
3069 | 0 | if (inrow[0] & 0x80) back_i += 1; /* blue */ |
3070 | 0 | if (inrow[0] & 0x40) back_i += 1; |
3071 | |
|
3072 | 0 | *outrow = (png_byte)back_i; |
3073 | 0 | } |
3074 | |
|
3075 | 0 | inrow += 4; |
3076 | 0 | } |
3077 | 0 | break; |
3078 | | |
3079 | 0 | default: |
3080 | 0 | break; |
3081 | 0 | } |
3082 | 0 | } |
3083 | 0 | } |
3084 | 0 | } |
3085 | | |
3086 | 0 | return 1; |
3087 | 0 | } |
3088 | | |
3089 | | static int |
3090 | | png_image_read_colormapped(void *argument) |
3091 | 0 | { |
3092 | 0 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
3093 | 0 | argument); |
3094 | 0 | png_image *image = display->image; |
3095 | 0 | png_control *control = image->opaque; |
3096 | 0 | png_struct *png_ptr = control->png_ptr; |
3097 | 0 | png_info *info_ptr = control->info_ptr; |
3098 | |
|
3099 | 0 | int passes = 0; /* As a flag */ |
3100 | |
|
3101 | 0 | PNG_SKIP_CHUNKS(png_ptr); |
3102 | | |
3103 | | /* Update the 'info' structure and make sure the result is as required; first |
3104 | | * make sure to turn on the interlace handling if it will be required |
3105 | | * (because it can't be turned on *after* the call to png_read_update_info!) |
3106 | | */ |
3107 | 0 | if (display->colormap_processing == PNG_CMAP_NONE) |
3108 | 0 | passes = png_set_interlace_handling(png_ptr); |
3109 | |
|
3110 | 0 | png_read_update_info(png_ptr, info_ptr); |
3111 | | |
3112 | | /* The expected output can be deduced from the colormap_processing option. */ |
3113 | 0 | switch (display->colormap_processing) |
3114 | 0 | { |
3115 | 0 | case PNG_CMAP_NONE: |
3116 | | /* Output must be one channel and one byte per pixel, the output |
3117 | | * encoding can be anything. |
3118 | | */ |
3119 | 0 | if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
3120 | 0 | info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && |
3121 | 0 | info_ptr->bit_depth == 8) |
3122 | 0 | break; |
3123 | | |
3124 | 0 | goto bad_output; |
3125 | | |
3126 | 0 | case PNG_CMAP_TRANS: |
3127 | 0 | case PNG_CMAP_GA: |
3128 | | /* Output must be two channels and the 'G' one must be sRGB, the latter |
3129 | | * can be checked with an exact number because it should have been set |
3130 | | * to this number above! |
3131 | | */ |
3132 | 0 | if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && |
3133 | 0 | info_ptr->bit_depth == 8 && |
3134 | 0 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
3135 | 0 | image->colormap_entries == 256) |
3136 | 0 | break; |
3137 | | |
3138 | 0 | goto bad_output; |
3139 | | |
3140 | 0 | case PNG_CMAP_RGB: |
3141 | | /* Output must be 8-bit sRGB encoded RGB */ |
3142 | 0 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && |
3143 | 0 | info_ptr->bit_depth == 8 && |
3144 | 0 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
3145 | 0 | image->colormap_entries == 216) |
3146 | 0 | break; |
3147 | | |
3148 | 0 | goto bad_output; |
3149 | | |
3150 | 0 | case PNG_CMAP_RGB_ALPHA: |
3151 | | /* Output must be 8-bit sRGB encoded RGBA */ |
3152 | 0 | if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
3153 | 0 | info_ptr->bit_depth == 8 && |
3154 | 0 | png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
3155 | 0 | image->colormap_entries == 244 /* 216 + 1 + 27 */) |
3156 | 0 | break; |
3157 | | |
3158 | 0 | goto bad_output; |
3159 | | |
3160 | 0 | default: |
3161 | 0 | bad_output: |
3162 | 0 | png_error(png_ptr, "bad color-map processing (internal error)"); |
3163 | 0 | } |
3164 | | |
3165 | | /* Now read the rows. Do this here if it is possible to read directly into |
3166 | | * the output buffer, otherwise allocate a local row buffer of the maximum |
3167 | | * size libpng requires and call the relevant processing routine safely. |
3168 | | */ |
3169 | 0 | { |
3170 | 0 | void *first_row = display->buffer; |
3171 | 0 | ptrdiff_t row_bytes = display->row_stride; |
3172 | | |
3173 | | /* The following expression is designed to work correctly whether it gives |
3174 | | * a signed or an unsigned result. |
3175 | | */ |
3176 | 0 | if (row_bytes < 0) |
3177 | 0 | { |
3178 | 0 | char *ptr = png_voidcast(char*, first_row); |
3179 | 0 | ptr += (image->height-1) * (-row_bytes); |
3180 | 0 | first_row = png_voidcast(void *, ptr); |
3181 | 0 | } |
3182 | |
|
3183 | 0 | display->first_row = first_row; |
3184 | 0 | display->row_bytes = row_bytes; |
3185 | 0 | } |
3186 | |
|
3187 | 0 | if (passes == 0) |
3188 | 0 | { |
3189 | 0 | int result; |
3190 | 0 | void *row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
3191 | |
|
3192 | 0 | display->local_row = row; |
3193 | 0 | result = png_safe_execute(image, png_image_read_and_map, display); |
3194 | 0 | display->local_row = NULL; |
3195 | 0 | png_free(png_ptr, row); |
3196 | |
|
3197 | 0 | return result; |
3198 | 0 | } |
3199 | | |
3200 | 0 | else |
3201 | 0 | { |
3202 | 0 | png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes; |
3203 | |
|
3204 | 0 | while (--passes >= 0) |
3205 | 0 | { |
3206 | 0 | png_uint_32 y = image->height; |
3207 | 0 | png_byte *row = png_voidcast(png_byte *, display->first_row); |
3208 | |
|
3209 | 0 | for (; y > 0; --y) |
3210 | 0 | { |
3211 | 0 | png_read_row(png_ptr, row, NULL); |
3212 | 0 | row += row_bytes; |
3213 | 0 | } |
3214 | 0 | } |
3215 | |
|
3216 | 0 | return 1; |
3217 | 0 | } |
3218 | 0 | } |
3219 | | |
3220 | | /* Just the row reading part of png_image_read. */ |
3221 | | static int |
3222 | | png_image_read_composite(void *argument) |
3223 | 0 | { |
3224 | 0 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
3225 | 0 | argument); |
3226 | 0 | png_image *image = display->image; |
3227 | 0 | png_struct *png_ptr = image->opaque->png_ptr; |
3228 | 0 | int passes; |
3229 | |
|
3230 | 0 | switch (png_ptr->interlaced) |
3231 | 0 | { |
3232 | 0 | case PNG_INTERLACE_NONE: |
3233 | 0 | passes = 1; |
3234 | 0 | break; |
3235 | | |
3236 | 0 | case PNG_INTERLACE_ADAM7: |
3237 | 0 | passes = PNG_INTERLACE_ADAM7_PASSES; |
3238 | 0 | break; |
3239 | | |
3240 | 0 | default: |
3241 | 0 | png_error(png_ptr, "unknown interlace type"); |
3242 | 0 | } |
3243 | | |
3244 | 0 | { |
3245 | 0 | png_uint_32 height = image->height; |
3246 | 0 | png_uint_32 width = image->width; |
3247 | 0 | ptrdiff_t step_row = display->row_bytes; |
3248 | 0 | unsigned int channels = |
3249 | 0 | (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; |
3250 | 0 | int pass; |
3251 | |
|
3252 | 0 | for (pass = 0; pass < passes; ++pass) |
3253 | 0 | { |
3254 | 0 | unsigned int startx, stepx, stepy; |
3255 | 0 | png_uint_32 y; |
3256 | |
|
3257 | 0 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
3258 | 0 | { |
3259 | | /* The row may be empty for a short image: */ |
3260 | 0 | if (PNG_PASS_COLS(width, pass) == 0) |
3261 | 0 | continue; |
3262 | | |
3263 | 0 | startx = PNG_PASS_START_COL(pass) * channels; |
3264 | 0 | stepx = PNG_PASS_COL_OFFSET(pass) * channels; |
3265 | 0 | y = PNG_PASS_START_ROW(pass); |
3266 | 0 | stepy = PNG_PASS_ROW_OFFSET(pass); |
3267 | 0 | } |
3268 | | |
3269 | 0 | else |
3270 | 0 | { |
3271 | 0 | y = 0; |
3272 | 0 | startx = 0; |
3273 | 0 | stepx = channels; |
3274 | 0 | stepy = 1; |
3275 | 0 | } |
3276 | | |
3277 | 0 | for (; y<height; y += stepy) |
3278 | 0 | { |
3279 | 0 | png_byte *inrow = png_voidcast(png_byte *, display->local_row); |
3280 | 0 | png_byte *outrow; |
3281 | 0 | png_byte *row_end; |
3282 | | |
3283 | | /* Read the row, which is packed: */ |
3284 | 0 | png_read_row(png_ptr, inrow, NULL); |
3285 | |
|
3286 | 0 | outrow = png_voidcast(png_byte *, display->first_row); |
3287 | 0 | outrow += y * step_row; |
3288 | 0 | row_end = outrow + width * channels; |
3289 | | |
3290 | | /* Now do the composition on each pixel in this row. */ |
3291 | 0 | outrow += startx; |
3292 | 0 | for (; outrow < row_end; outrow += stepx) |
3293 | 0 | { |
3294 | 0 | png_byte alpha = inrow[channels]; |
3295 | |
|
3296 | 0 | if (alpha > 0) /* else no change to the output */ |
3297 | 0 | { |
3298 | 0 | unsigned int c; |
3299 | |
|
3300 | 0 | for (c=0; c<channels; ++c) |
3301 | 0 | { |
3302 | 0 | png_uint_32 component = inrow[c]; |
3303 | |
|
3304 | 0 | if (alpha < 255) /* else just use component */ |
3305 | 0 | { |
3306 | | /* This is PNG_OPTIMIZED_ALPHA, the component value |
3307 | | * is a linear 8-bit value. Combine this with the |
3308 | | * current outrow[c] value which is sRGB encoded. |
3309 | | * Arithmetic here is 16-bits to preserve the output |
3310 | | * values correctly. |
3311 | | */ |
3312 | 0 | component *= 257*255; /* =65535 */ |
3313 | 0 | component += (255-alpha)*png_sRGB_table[outrow[c]]; |
3314 | | |
3315 | | /* So 'component' is scaled by 255*65535 and is |
3316 | | * therefore appropriate for the sRGB to linear |
3317 | | * conversion table. |
3318 | | */ |
3319 | 0 | component = PNG_sRGB_FROM_LINEAR(component); |
3320 | 0 | } |
3321 | |
|
3322 | 0 | outrow[c] = (png_byte)component; |
3323 | 0 | } |
3324 | 0 | } |
3325 | |
|
3326 | 0 | inrow += channels+1; /* components and alpha channel */ |
3327 | 0 | } |
3328 | 0 | } |
3329 | 0 | } |
3330 | 0 | } |
3331 | |
|
3332 | 0 | return 1; |
3333 | 0 | } |
3334 | | |
3335 | | /* The do_local_background case; called when all the following transforms are to |
3336 | | * be done: |
3337 | | * |
3338 | | * PNG_RGB_TO_GRAY |
3339 | | * PNG_COMPOSITE |
3340 | | * PNG_GAMMA |
3341 | | * |
3342 | | * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and |
3343 | | * PNG_COMPOSITE code performs gamma correction, so we get double gamma |
3344 | | * correction. The fix-up is to prevent the PNG_COMPOSITE operation from |
3345 | | * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha |
3346 | | * row and handles the removal or pre-multiplication of the alpha channel. |
3347 | | */ |
3348 | | static int |
3349 | | png_image_read_background(void *argument) |
3350 | 0 | { |
3351 | 0 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
3352 | 0 | argument); |
3353 | 0 | png_image *image = display->image; |
3354 | 0 | png_struct *png_ptr = image->opaque->png_ptr; |
3355 | 0 | png_info *info_ptr = image->opaque->info_ptr; |
3356 | 0 | png_uint_32 height = image->height; |
3357 | 0 | png_uint_32 width = image->width; |
3358 | 0 | int pass, passes; |
3359 | | |
3360 | | /* Double check the convoluted logic below. We expect to get here with |
3361 | | * libpng doing rgb to gray and gamma correction but background processing |
3362 | | * left to the png_image_read_background function. The rows libpng produce |
3363 | | * might be 8 or 16-bit but should always have two channels; gray plus alpha. |
3364 | | */ |
3365 | 0 | if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
3366 | 0 | png_error(png_ptr, "lost rgb to gray"); |
3367 | | |
3368 | 0 | if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
3369 | 0 | png_error(png_ptr, "unexpected compose"); |
3370 | | |
3371 | 0 | if (png_get_channels(png_ptr, info_ptr) != 2) |
3372 | 0 | png_error(png_ptr, "lost/gained channels"); |
3373 | | |
3374 | | /* Expect the 8-bit case to always remove the alpha channel */ |
3375 | 0 | if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && |
3376 | 0 | (image->format & PNG_FORMAT_FLAG_ALPHA) != 0) |
3377 | 0 | png_error(png_ptr, "unexpected 8-bit transformation"); |
3378 | | |
3379 | 0 | switch (png_ptr->interlaced) |
3380 | 0 | { |
3381 | 0 | case PNG_INTERLACE_NONE: |
3382 | 0 | passes = 1; |
3383 | 0 | break; |
3384 | | |
3385 | 0 | case PNG_INTERLACE_ADAM7: |
3386 | 0 | passes = PNG_INTERLACE_ADAM7_PASSES; |
3387 | 0 | break; |
3388 | | |
3389 | 0 | default: |
3390 | 0 | png_error(png_ptr, "unknown interlace type"); |
3391 | 0 | } |
3392 | | |
3393 | | /* Use direct access to info_ptr here because otherwise the simplified API |
3394 | | * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is |
3395 | | * checking the value after libpng expansions, not the original value in the |
3396 | | * PNG. |
3397 | | */ |
3398 | 0 | switch (info_ptr->bit_depth) |
3399 | 0 | { |
3400 | 0 | case 8: |
3401 | | /* 8-bit sRGB gray values with an alpha channel; the alpha channel is |
3402 | | * to be removed by composing on a background: either the row if |
3403 | | * display->background is NULL or display->background->green if not. |
3404 | | * Unlike the code above ALPHA_OPTIMIZED has *not* been done. |
3405 | | */ |
3406 | 0 | { |
3407 | 0 | png_byte *first_row = png_voidcast(png_byte *, display->first_row); |
3408 | 0 | ptrdiff_t step_row = display->row_bytes; |
3409 | |
|
3410 | 0 | for (pass = 0; pass < passes; ++pass) |
3411 | 0 | { |
3412 | 0 | unsigned int startx, stepx, stepy; |
3413 | 0 | png_uint_32 y; |
3414 | |
|
3415 | 0 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
3416 | 0 | { |
3417 | | /* The row may be empty for a short image: */ |
3418 | 0 | if (PNG_PASS_COLS(width, pass) == 0) |
3419 | 0 | continue; |
3420 | | |
3421 | 0 | startx = PNG_PASS_START_COL(pass); |
3422 | 0 | stepx = PNG_PASS_COL_OFFSET(pass); |
3423 | 0 | y = PNG_PASS_START_ROW(pass); |
3424 | 0 | stepy = PNG_PASS_ROW_OFFSET(pass); |
3425 | 0 | } |
3426 | | |
3427 | 0 | else |
3428 | 0 | { |
3429 | 0 | y = 0; |
3430 | 0 | startx = 0; |
3431 | 0 | stepx = stepy = 1; |
3432 | 0 | } |
3433 | | |
3434 | 0 | if (display->background == NULL) |
3435 | 0 | { |
3436 | 0 | for (; y<height; y += stepy) |
3437 | 0 | { |
3438 | 0 | png_byte *inrow = png_voidcast(png_byte *, |
3439 | 0 | display->local_row); |
3440 | 0 | png_byte *outrow = first_row + y * step_row; |
3441 | 0 | png_byte *row_end = outrow + width; |
3442 | | |
3443 | | /* Read the row, which is packed: */ |
3444 | 0 | png_read_row(png_ptr, inrow, NULL); |
3445 | | |
3446 | | /* Now do the composition on each pixel in this row. */ |
3447 | 0 | outrow += startx; |
3448 | 0 | for (; outrow < row_end; outrow += stepx) |
3449 | 0 | { |
3450 | 0 | png_byte alpha = inrow[1]; |
3451 | |
|
3452 | 0 | if (alpha > 0) /* else no change to the output */ |
3453 | 0 | { |
3454 | 0 | png_uint_32 component = inrow[0]; |
3455 | |
|
3456 | 0 | if (alpha < 255) /* else just use component */ |
3457 | 0 | { |
3458 | | /* Since PNG_OPTIMIZED_ALPHA was not set it is |
3459 | | * necessary to invert the sRGB transfer |
3460 | | * function and multiply the alpha out. |
3461 | | */ |
3462 | 0 | component = png_sRGB_table[component] * alpha; |
3463 | 0 | component += png_sRGB_table[outrow[0]] * |
3464 | 0 | (255-alpha); |
3465 | 0 | component = PNG_sRGB_FROM_LINEAR(component); |
3466 | 0 | } |
3467 | |
|
3468 | 0 | outrow[0] = (png_byte)component; |
3469 | 0 | } |
3470 | |
|
3471 | 0 | inrow += 2; /* gray and alpha channel */ |
3472 | 0 | } |
3473 | 0 | } |
3474 | 0 | } |
3475 | | |
3476 | 0 | else /* constant background value */ |
3477 | 0 | { |
3478 | 0 | png_byte background8 = display->background->green; |
3479 | 0 | png_uint_16 background = png_sRGB_table[background8]; |
3480 | |
|
3481 | 0 | for (; y<height; y += stepy) |
3482 | 0 | { |
3483 | 0 | png_byte *inrow = png_voidcast(png_byte *, |
3484 | 0 | display->local_row); |
3485 | 0 | png_byte *outrow = first_row + y * step_row; |
3486 | 0 | png_byte *row_end = outrow + width; |
3487 | | |
3488 | | /* Read the row, which is packed: */ |
3489 | 0 | png_read_row(png_ptr, inrow, NULL); |
3490 | | |
3491 | | /* Now do the composition on each pixel in this row. */ |
3492 | 0 | outrow += startx; |
3493 | 0 | for (; outrow < row_end; outrow += stepx) |
3494 | 0 | { |
3495 | 0 | png_byte alpha = inrow[1]; |
3496 | |
|
3497 | 0 | if (alpha > 0) /* else use background */ |
3498 | 0 | { |
3499 | 0 | png_uint_32 component = inrow[0]; |
3500 | |
|
3501 | 0 | if (alpha < 255) /* else just use component */ |
3502 | 0 | { |
3503 | 0 | component = png_sRGB_table[component] * alpha; |
3504 | 0 | component += background * (255-alpha); |
3505 | 0 | component = PNG_sRGB_FROM_LINEAR(component); |
3506 | 0 | } |
3507 | |
|
3508 | 0 | outrow[0] = (png_byte)component; |
3509 | 0 | } |
3510 | | |
3511 | 0 | else |
3512 | 0 | outrow[0] = background8; |
3513 | |
|
3514 | 0 | inrow += 2; /* gray and alpha channel */ |
3515 | 0 | } |
3516 | 0 | } |
3517 | 0 | } |
3518 | 0 | } |
3519 | 0 | } |
3520 | 0 | break; |
3521 | | |
3522 | 0 | case 16: |
3523 | | /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must |
3524 | | * still be done and, maybe, the alpha channel removed. This code also |
3525 | | * handles the alpha-first option. |
3526 | | */ |
3527 | 0 | { |
3528 | 0 | png_uint_16 *first_row = png_voidcast(png_uint_16 *, |
3529 | 0 | display->first_row); |
3530 | | /* The division by two is safe because the caller passed in a |
3531 | | * stride which was multiplied by 2 (below) to get row_bytes. |
3532 | | */ |
3533 | 0 | ptrdiff_t step_row = display->row_bytes / 2; |
3534 | 0 | unsigned int preserve_alpha = (image->format & |
3535 | 0 | PNG_FORMAT_FLAG_ALPHA) != 0; |
3536 | 0 | unsigned int outchannels = 1U+preserve_alpha; |
3537 | 0 | int swap_alpha = 0; |
3538 | |
|
3539 | 0 | # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED |
3540 | 0 | if (preserve_alpha != 0 && |
3541 | 0 | (image->format & PNG_FORMAT_FLAG_AFIRST) != 0) |
3542 | 0 | swap_alpha = 1; |
3543 | 0 | # endif |
3544 | |
|
3545 | 0 | for (pass = 0; pass < passes; ++pass) |
3546 | 0 | { |
3547 | 0 | unsigned int startx, stepx, stepy; |
3548 | 0 | png_uint_32 y; |
3549 | | |
3550 | | /* The 'x' start and step are adjusted to output components here. |
3551 | | */ |
3552 | 0 | if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
3553 | 0 | { |
3554 | | /* The row may be empty for a short image: */ |
3555 | 0 | if (PNG_PASS_COLS(width, pass) == 0) |
3556 | 0 | continue; |
3557 | | |
3558 | 0 | startx = PNG_PASS_START_COL(pass) * outchannels; |
3559 | 0 | stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; |
3560 | 0 | y = PNG_PASS_START_ROW(pass); |
3561 | 0 | stepy = PNG_PASS_ROW_OFFSET(pass); |
3562 | 0 | } |
3563 | | |
3564 | 0 | else |
3565 | 0 | { |
3566 | 0 | y = 0; |
3567 | 0 | startx = 0; |
3568 | 0 | stepx = outchannels; |
3569 | 0 | stepy = 1; |
3570 | 0 | } |
3571 | | |
3572 | 0 | for (; y<height; y += stepy) |
3573 | 0 | { |
3574 | 0 | const png_uint_16 *inrow; |
3575 | 0 | png_uint_16 *outrow = first_row + y*step_row; |
3576 | 0 | png_uint_16 *row_end = outrow + width * outchannels; |
3577 | | |
3578 | | /* Read the row, which is packed: */ |
3579 | 0 | png_read_row(png_ptr, png_voidcast(png_byte *, |
3580 | 0 | display->local_row), NULL); |
3581 | 0 | inrow = png_voidcast(const png_uint_16 *, display->local_row); |
3582 | | |
3583 | | /* Now do the pre-multiplication on each pixel in this row. |
3584 | | */ |
3585 | 0 | outrow += startx; |
3586 | 0 | for (; outrow < row_end; outrow += stepx) |
3587 | 0 | { |
3588 | 0 | png_uint_32 component = inrow[0]; |
3589 | 0 | png_uint_16 alpha = inrow[1]; |
3590 | |
|
3591 | 0 | if (alpha > 0) /* else 0 */ |
3592 | 0 | { |
3593 | 0 | if (alpha < 65535) /* else just use component */ |
3594 | 0 | { |
3595 | 0 | component *= alpha; |
3596 | 0 | component += 32767; |
3597 | 0 | component /= 65535; |
3598 | 0 | } |
3599 | 0 | } |
3600 | | |
3601 | 0 | else |
3602 | 0 | component = 0; |
3603 | |
|
3604 | 0 | outrow[swap_alpha] = (png_uint_16)component; |
3605 | 0 | if (preserve_alpha != 0) |
3606 | 0 | outrow[1 ^ swap_alpha] = alpha; |
3607 | |
|
3608 | 0 | inrow += 2; /* components and alpha channel */ |
3609 | 0 | } |
3610 | 0 | } |
3611 | 0 | } |
3612 | 0 | } |
3613 | 0 | break; |
3614 | | |
3615 | 0 | #ifdef __GNUC__ |
3616 | 0 | default: |
3617 | 0 | png_error(png_ptr, "unexpected bit depth"); |
3618 | 0 | #endif |
3619 | 0 | } |
3620 | | |
3621 | 0 | return 1; |
3622 | 0 | } |
3623 | | |
3624 | | /* The guts of png_image_finish_read as a png_safe_execute callback. */ |
3625 | | static int |
3626 | | png_image_read_direct(void *argument) |
3627 | 0 | { |
3628 | 0 | png_image_read_control *display = png_voidcast(png_image_read_control*, |
3629 | 0 | argument); |
3630 | 0 | png_image *image = display->image; |
3631 | 0 | png_struct *png_ptr = image->opaque->png_ptr; |
3632 | 0 | png_info *info_ptr = image->opaque->info_ptr; |
3633 | |
|
3634 | 0 | png_uint_32 format = image->format; |
3635 | 0 | int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; |
3636 | 0 | int do_local_compose = 0; |
3637 | 0 | int do_local_background = 0; /* to avoid double gamma correction bug */ |
3638 | 0 | int passes = 0; |
3639 | | |
3640 | | /* Add transforms to ensure the correct output format is produced then check |
3641 | | * that the required implementation support is there. Always expand; always |
3642 | | * need 8 bits minimum, no palette and expanded tRNS. |
3643 | | */ |
3644 | 0 | png_set_expand(png_ptr); |
3645 | | |
3646 | | /* Now check the format to see if it was modified. */ |
3647 | 0 | { |
3648 | 0 | png_uint_32 base_format = png_image_format(png_ptr) & |
3649 | 0 | ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; |
3650 | 0 | png_uint_32 change = format ^ base_format; |
3651 | 0 | png_fixed_point output_gamma; |
3652 | 0 | int mode; /* alpha mode */ |
3653 | | |
3654 | | /* Do this first so that we have a record if rgb to gray is happening. */ |
3655 | 0 | if ((change & PNG_FORMAT_FLAG_COLOR) != 0) |
3656 | 0 | { |
3657 | | /* gray<->color transformation required. */ |
3658 | 0 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0) |
3659 | 0 | png_set_gray_to_rgb(png_ptr); |
3660 | | |
3661 | 0 | else |
3662 | 0 | { |
3663 | | /* libpng can't do both rgb to gray and |
3664 | | * background/pre-multiplication if there is also significant gamma |
3665 | | * correction, because both operations require linear colors and |
3666 | | * the code only supports one transform doing the gamma correction. |
3667 | | * Handle this by doing the pre-multiplication or background |
3668 | | * operation in this code, if necessary. |
3669 | | * |
3670 | | * TODO: fix this by rewriting pngrtran.c (!) |
3671 | | * |
3672 | | * For the moment (given that fixing this in pngrtran.c is an |
3673 | | * enormous change) 'do_local_background' is used to indicate that |
3674 | | * the problem exists. |
3675 | | */ |
3676 | 0 | if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
3677 | 0 | do_local_background = 1/*maybe*/; |
3678 | |
|
3679 | 0 | png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, |
3680 | 0 | PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); |
3681 | 0 | } |
3682 | |
|
3683 | 0 | change &= ~PNG_FORMAT_FLAG_COLOR; |
3684 | 0 | } |
3685 | | |
3686 | | /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. |
3687 | | */ |
3688 | 0 | { |
3689 | | /* This is safe but should no longer be necessary as |
3690 | | * png_ptr->default_gamma should have been set after the |
3691 | | * info-before-IDAT was read in png_image_read_header. |
3692 | | * |
3693 | | * TODO: 1.8: remove this and see what happens. |
3694 | | */ |
3695 | 0 | png_fixed_point input_gamma_default; |
3696 | |
|
3697 | 0 | if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 && |
3698 | 0 | (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) |
3699 | 0 | input_gamma_default = PNG_GAMMA_LINEAR; |
3700 | 0 | else |
3701 | 0 | input_gamma_default = PNG_DEFAULT_sRGB; |
3702 | | |
3703 | | /* Call png_set_alpha_mode to set the default for the input gamma; the |
3704 | | * output gamma is set by a second call below. |
3705 | | */ |
3706 | 0 | png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); |
3707 | 0 | } |
3708 | |
|
3709 | 0 | if (linear != 0) |
3710 | 0 | { |
3711 | | /* If there *is* an alpha channel in the input it must be multiplied |
3712 | | * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. |
3713 | | */ |
3714 | 0 | if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
3715 | 0 | mode = PNG_ALPHA_STANDARD; /* associated alpha */ |
3716 | | |
3717 | 0 | else |
3718 | 0 | mode = PNG_ALPHA_PNG; |
3719 | |
|
3720 | 0 | output_gamma = PNG_GAMMA_LINEAR; |
3721 | 0 | } |
3722 | | |
3723 | 0 | else |
3724 | 0 | { |
3725 | 0 | mode = PNG_ALPHA_PNG; |
3726 | 0 | output_gamma = PNG_DEFAULT_sRGB; |
3727 | 0 | } |
3728 | |
|
3729 | 0 | if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) |
3730 | 0 | { |
3731 | 0 | mode = PNG_ALPHA_OPTIMIZED; |
3732 | 0 | change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA; |
3733 | 0 | } |
3734 | | |
3735 | | /* If 'do_local_background' is set check for the presence of gamma |
3736 | | * correction; this is part of the work-round for the libpng bug |
3737 | | * described above. |
3738 | | * |
3739 | | * TODO: fix libpng and remove this. |
3740 | | */ |
3741 | 0 | if (do_local_background != 0) |
3742 | 0 | { |
3743 | 0 | png_fixed_point gtest; |
3744 | | |
3745 | | /* This is 'png_gamma_threshold' from pngrtran.c; the test used for |
3746 | | * gamma correction, the screen gamma hasn't been set on png_struct |
3747 | | * yet; it's set below. png_struct::gamma, however, is set to the |
3748 | | * final value. |
3749 | | */ |
3750 | 0 | if (png_muldiv(>est, output_gamma, |
3751 | 0 | png_resolve_file_gamma(png_ptr), PNG_FP_1) != 0 && |
3752 | 0 | png_gamma_significant(gtest) == 0) |
3753 | 0 | do_local_background = 0; |
3754 | | |
3755 | 0 | else if (mode == PNG_ALPHA_STANDARD) |
3756 | 0 | { |
3757 | 0 | do_local_background = 2/*required*/; |
3758 | 0 | mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ |
3759 | 0 | } |
3760 | | |
3761 | | /* else leave as 1 for the checks below */ |
3762 | 0 | } |
3763 | | |
3764 | | /* If the bit-depth changes then handle that here. */ |
3765 | 0 | if ((change & PNG_FORMAT_FLAG_LINEAR) != 0) |
3766 | 0 | { |
3767 | 0 | if (linear != 0 /*16-bit output*/) |
3768 | 0 | png_set_expand_16(png_ptr); |
3769 | | |
3770 | 0 | else /* 8-bit output */ |
3771 | 0 | png_set_scale_16(png_ptr); |
3772 | |
|
3773 | 0 | change &= ~PNG_FORMAT_FLAG_LINEAR; |
3774 | 0 | } |
3775 | | |
3776 | | /* Now the background/alpha channel changes. */ |
3777 | 0 | if ((change & PNG_FORMAT_FLAG_ALPHA) != 0) |
3778 | 0 | { |
3779 | | /* Removing an alpha channel requires composition for the 8-bit |
3780 | | * formats; for the 16-bit it is already done, above, by the |
3781 | | * pre-multiplication and the channel just needs to be stripped. |
3782 | | */ |
3783 | 0 | if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
3784 | 0 | { |
3785 | | /* If RGB->gray is happening the alpha channel must be left and the |
3786 | | * operation completed locally. |
3787 | | * |
3788 | | * TODO: fix libpng and remove this. |
3789 | | */ |
3790 | 0 | if (do_local_background != 0) |
3791 | 0 | do_local_background = 2/*required*/; |
3792 | | |
3793 | | /* 16-bit output: just remove the channel */ |
3794 | 0 | else if (linear != 0) /* compose on black (well, pre-multiply) */ |
3795 | 0 | png_set_strip_alpha(png_ptr); |
3796 | | |
3797 | | /* 8-bit output: do an appropriate compose */ |
3798 | 0 | else if (display->background != NULL) |
3799 | 0 | { |
3800 | 0 | png_color_16 c; |
3801 | |
|
3802 | 0 | c.index = 0; /*unused*/ |
3803 | 0 | c.red = display->background->red; |
3804 | 0 | c.green = display->background->green; |
3805 | 0 | c.blue = display->background->blue; |
3806 | 0 | c.gray = display->background->green; |
3807 | | |
3808 | | /* This is always an 8-bit sRGB value, using the 'green' channel |
3809 | | * for gray is much better than calculating the luminance here; |
3810 | | * we can get off-by-one errors in that calculation relative to |
3811 | | * the app expectations and that will show up in transparent |
3812 | | * pixels. |
3813 | | */ |
3814 | 0 | png_set_background_fixed(png_ptr, &c, |
3815 | 0 | PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, |
3816 | 0 | 0/*gamma: not used*/); |
3817 | 0 | } |
3818 | | |
3819 | 0 | else /* compose on row: implemented below. */ |
3820 | 0 | { |
3821 | 0 | do_local_compose = 1; |
3822 | | /* This leaves the alpha channel in the output, so it has to be |
3823 | | * removed by the code below. Set the encoding to the 'OPTIMIZE' |
3824 | | * one so the code only has to hack on the pixels that require |
3825 | | * composition. |
3826 | | */ |
3827 | 0 | mode = PNG_ALPHA_OPTIMIZED; |
3828 | 0 | } |
3829 | 0 | } |
3830 | | |
3831 | 0 | else /* output needs an alpha channel */ |
3832 | 0 | { |
3833 | | /* This is tricky because it happens before the swap operation has |
3834 | | * been accomplished; however, the swap does *not* swap the added |
3835 | | * alpha channel (weird API), so it must be added in the correct |
3836 | | * place. |
3837 | | */ |
3838 | 0 | png_uint_32 filler; /* opaque filler */ |
3839 | 0 | int where; |
3840 | |
|
3841 | 0 | if (linear != 0) |
3842 | 0 | filler = 65535; |
3843 | | |
3844 | 0 | else |
3845 | 0 | filler = 255; |
3846 | |
|
3847 | 0 | #ifdef PNG_FORMAT_AFIRST_SUPPORTED |
3848 | 0 | if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
3849 | 0 | { |
3850 | 0 | where = PNG_FILLER_BEFORE; |
3851 | 0 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
3852 | 0 | } |
3853 | | |
3854 | 0 | else |
3855 | 0 | #endif |
3856 | 0 | where = PNG_FILLER_AFTER; |
3857 | |
|
3858 | 0 | png_set_add_alpha(png_ptr, filler, where); |
3859 | 0 | } |
3860 | | |
3861 | | /* This stops the (irrelevant) call to swap_alpha below. */ |
3862 | 0 | change &= ~PNG_FORMAT_FLAG_ALPHA; |
3863 | 0 | } |
3864 | | |
3865 | | /* Now set the alpha mode correctly; this is always done, even if there is |
3866 | | * no alpha channel in either the input or the output because it correctly |
3867 | | * sets the output gamma. |
3868 | | */ |
3869 | 0 | png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); |
3870 | |
|
3871 | 0 | # ifdef PNG_FORMAT_BGR_SUPPORTED |
3872 | 0 | if ((change & PNG_FORMAT_FLAG_BGR) != 0) |
3873 | 0 | { |
3874 | | /* Check only the output format; PNG is never BGR; don't do this if |
3875 | | * the output is gray, but fix up the 'format' value in that case. |
3876 | | */ |
3877 | 0 | if ((format & PNG_FORMAT_FLAG_COLOR) != 0) |
3878 | 0 | png_set_bgr(png_ptr); |
3879 | | |
3880 | 0 | else |
3881 | 0 | format &= ~PNG_FORMAT_FLAG_BGR; |
3882 | |
|
3883 | 0 | change &= ~PNG_FORMAT_FLAG_BGR; |
3884 | 0 | } |
3885 | 0 | # endif |
3886 | |
|
3887 | 0 | # ifdef PNG_FORMAT_AFIRST_SUPPORTED |
3888 | 0 | if ((change & PNG_FORMAT_FLAG_AFIRST) != 0) |
3889 | 0 | { |
3890 | | /* Only relevant if there is an alpha channel - it's particularly |
3891 | | * important to handle this correctly because do_local_compose may |
3892 | | * be set above and then libpng will keep the alpha channel for this |
3893 | | * code to remove. |
3894 | | */ |
3895 | 0 | if ((format & PNG_FORMAT_FLAG_ALPHA) != 0) |
3896 | 0 | { |
3897 | | /* Disable this if doing a local background, |
3898 | | * TODO: remove this when local background is no longer required. |
3899 | | */ |
3900 | 0 | if (do_local_background != 2) |
3901 | 0 | png_set_swap_alpha(png_ptr); |
3902 | 0 | } |
3903 | | |
3904 | 0 | else |
3905 | 0 | format &= ~PNG_FORMAT_FLAG_AFIRST; |
3906 | |
|
3907 | 0 | change &= ~PNG_FORMAT_FLAG_AFIRST; |
3908 | 0 | } |
3909 | 0 | # endif |
3910 | | |
3911 | | /* If the *output* is 16-bit then we need to check for a byte-swap on this |
3912 | | * architecture. |
3913 | | */ |
3914 | 0 | if (linear != 0) |
3915 | 0 | { |
3916 | 0 | png_uint_16 le = 0x0001; |
3917 | |
|
3918 | 0 | if ((*(const png_byte *) & le) != 0) |
3919 | 0 | png_set_swap(png_ptr); |
3920 | 0 | } |
3921 | | |
3922 | | /* If change is not now 0 some transformation is missing - error out. */ |
3923 | 0 | if (change != 0) |
3924 | 0 | png_error(png_ptr, "png_read_image: unsupported transformation"); |
3925 | 0 | } |
3926 | | |
3927 | 0 | PNG_SKIP_CHUNKS(png_ptr); |
3928 | | |
3929 | | /* Update the 'info' structure and make sure the result is as required; first |
3930 | | * make sure to turn on the interlace handling if it will be required |
3931 | | * (because it can't be turned on *after* the call to png_read_update_info!) |
3932 | | * |
3933 | | * TODO: remove the do_local_background fixup below. |
3934 | | */ |
3935 | 0 | if (do_local_compose == 0 && do_local_background != 2) |
3936 | 0 | passes = png_set_interlace_handling(png_ptr); |
3937 | |
|
3938 | 0 | png_read_update_info(png_ptr, info_ptr); |
3939 | |
|
3940 | 0 | { |
3941 | 0 | png_uint_32 info_format = 0; |
3942 | |
|
3943 | 0 | if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
3944 | 0 | info_format |= PNG_FORMAT_FLAG_COLOR; |
3945 | |
|
3946 | 0 | if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
3947 | 0 | { |
3948 | | /* do_local_compose removes this channel below. */ |
3949 | 0 | if (do_local_compose == 0) |
3950 | 0 | { |
3951 | | /* do_local_background does the same if required. */ |
3952 | 0 | if (do_local_background != 2 || |
3953 | 0 | (format & PNG_FORMAT_FLAG_ALPHA) != 0) |
3954 | 0 | info_format |= PNG_FORMAT_FLAG_ALPHA; |
3955 | 0 | } |
3956 | 0 | } |
3957 | | |
3958 | 0 | else if (do_local_compose != 0) /* internal error */ |
3959 | 0 | png_error(png_ptr, "png_image_read: alpha channel lost"); |
3960 | | |
3961 | 0 | if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) { |
3962 | 0 | info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA; |
3963 | 0 | } |
3964 | |
|
3965 | 0 | if (info_ptr->bit_depth == 16) |
3966 | 0 | info_format |= PNG_FORMAT_FLAG_LINEAR; |
3967 | |
|
3968 | 0 | #ifdef PNG_FORMAT_BGR_SUPPORTED |
3969 | 0 | if ((png_ptr->transformations & PNG_BGR) != 0) |
3970 | 0 | info_format |= PNG_FORMAT_FLAG_BGR; |
3971 | 0 | #endif |
3972 | |
|
3973 | 0 | #ifdef PNG_FORMAT_AFIRST_SUPPORTED |
3974 | 0 | if (do_local_background == 2) |
3975 | 0 | { |
3976 | 0 | if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
3977 | 0 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
3978 | 0 | } |
3979 | |
|
3980 | 0 | if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || |
3981 | 0 | ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && |
3982 | 0 | (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) |
3983 | 0 | { |
3984 | 0 | if (do_local_background == 2) |
3985 | 0 | png_error(png_ptr, "unexpected alpha swap transformation"); |
3986 | | |
3987 | 0 | info_format |= PNG_FORMAT_FLAG_AFIRST; |
3988 | 0 | } |
3989 | 0 | # endif |
3990 | | |
3991 | | /* This is actually an internal error. */ |
3992 | 0 | if (info_format != format) |
3993 | 0 | png_error(png_ptr, "png_read_image: invalid transformations"); |
3994 | 0 | } |
3995 | | |
3996 | | /* Now read the rows. If do_local_compose is set then it is necessary to use |
3997 | | * a local row buffer. The output will be GA, RGBA or BGRA and must be |
3998 | | * converted to G, RGB or BGR as appropriate. The 'local_row' member of the |
3999 | | * display acts as a flag. |
4000 | | */ |
4001 | 0 | { |
4002 | 0 | void *first_row = display->buffer; |
4003 | 0 | ptrdiff_t row_bytes = display->row_stride; |
4004 | |
|
4005 | 0 | if (linear != 0) |
4006 | 0 | row_bytes *= 2; |
4007 | | |
4008 | | /* The following expression is designed to work correctly whether it gives |
4009 | | * a signed or an unsigned result. |
4010 | | */ |
4011 | 0 | if (row_bytes < 0) |
4012 | 0 | { |
4013 | 0 | char *ptr = png_voidcast(char*, first_row); |
4014 | 0 | ptr += (image->height-1) * (-row_bytes); |
4015 | 0 | first_row = png_voidcast(void *, ptr); |
4016 | 0 | } |
4017 | |
|
4018 | 0 | display->first_row = first_row; |
4019 | 0 | display->row_bytes = row_bytes; |
4020 | 0 | } |
4021 | |
|
4022 | 0 | if (do_local_compose != 0) |
4023 | 0 | { |
4024 | 0 | int result; |
4025 | 0 | void *row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
4026 | |
|
4027 | 0 | display->local_row = row; |
4028 | 0 | result = png_safe_execute(image, png_image_read_composite, display); |
4029 | 0 | display->local_row = NULL; |
4030 | 0 | png_free(png_ptr, row); |
4031 | |
|
4032 | 0 | return result; |
4033 | 0 | } |
4034 | | |
4035 | 0 | else if (do_local_background == 2) |
4036 | 0 | { |
4037 | 0 | int result; |
4038 | 0 | void *row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
4039 | |
|
4040 | 0 | display->local_row = row; |
4041 | 0 | result = png_safe_execute(image, png_image_read_background, display); |
4042 | 0 | display->local_row = NULL; |
4043 | 0 | png_free(png_ptr, row); |
4044 | |
|
4045 | 0 | return result; |
4046 | 0 | } |
4047 | | |
4048 | 0 | else |
4049 | 0 | { |
4050 | 0 | png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes; |
4051 | |
|
4052 | 0 | while (--passes >= 0) |
4053 | 0 | { |
4054 | 0 | png_uint_32 y = image->height; |
4055 | 0 | png_byte *row = png_voidcast(png_byte *, display->first_row); |
4056 | |
|
4057 | 0 | for (; y > 0; --y) |
4058 | 0 | { |
4059 | 0 | png_read_row(png_ptr, row, NULL); |
4060 | 0 | row += row_bytes; |
4061 | 0 | } |
4062 | 0 | } |
4063 | |
|
4064 | 0 | return 1; |
4065 | 0 | } |
4066 | 0 | } |
4067 | | |
4068 | | int |
4069 | | png_image_finish_read(png_image *image, const png_color *background, |
4070 | | void *buffer, png_int_32 row_stride, void *colormap) |
4071 | 0 | { |
4072 | 0 | if (image != NULL && image->version == PNG_IMAGE_VERSION) |
4073 | 0 | { |
4074 | | /* Check for row_stride overflow. This check is not performed on the |
4075 | | * original PNG format because it may not occur in the output PNG format |
4076 | | * and libpng deals with the issues of reading the original. |
4077 | | */ |
4078 | 0 | unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format); |
4079 | | |
4080 | | /* The following checks just the 'row_stride' calculation to ensure it |
4081 | | * fits in a signed 32-bit value. Because channels/components can be |
4082 | | * either 1 or 2 bytes in size the length of a row can still overflow 32 |
4083 | | * bits; this is just to verify that the 'row_stride' argument can be |
4084 | | * represented. |
4085 | | */ |
4086 | 0 | if (image->width <= 0x7fffffffU/channels) /* no overflow */ |
4087 | 0 | { |
4088 | 0 | png_uint_32 check; |
4089 | 0 | png_uint_32 png_row_stride = image->width * channels; |
4090 | |
|
4091 | 0 | if (row_stride == 0) |
4092 | 0 | row_stride = (png_int_32)/*SAFE*/png_row_stride; |
4093 | |
|
4094 | 0 | if (row_stride < 0) |
4095 | 0 | check = (png_uint_32)(-row_stride); |
4096 | | |
4097 | 0 | else |
4098 | 0 | check = (png_uint_32)row_stride; |
4099 | | |
4100 | | /* This verifies 'check', the absolute value of the actual stride |
4101 | | * passed in and detects overflow in the application calculation (i.e. |
4102 | | * if the app did actually pass in a non-zero 'row_stride'. |
4103 | | */ |
4104 | 0 | if (image->opaque != NULL && buffer != NULL && check >= png_row_stride) |
4105 | 0 | { |
4106 | | /* Now check for overflow of the image buffer calculation; this |
4107 | | * limits the whole image size to 32 bits for API compatibility with |
4108 | | * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro. |
4109 | | * |
4110 | | * The PNG_IMAGE_BUFFER_SIZE macro is: |
4111 | | * |
4112 | | * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride)) |
4113 | | * |
4114 | | * And the component size is always 1 or 2, so make sure that the |
4115 | | * number of *bytes* that the application is saying are available |
4116 | | * does actually fit into a 32-bit number. |
4117 | | * |
4118 | | * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE |
4119 | | * will be changed to use png_alloc_size_t; bigger images can be |
4120 | | * accommodated on 64-bit systems. |
4121 | | */ |
4122 | 0 | if (image->height <= |
4123 | 0 | 0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check) |
4124 | 0 | { |
4125 | 0 | if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || |
4126 | 0 | (image->colormap_entries > 0 && colormap != NULL)) |
4127 | 0 | { |
4128 | 0 | int result; |
4129 | 0 | png_image_read_control display; |
4130 | |
|
4131 | 0 | memset(&display, 0, (sizeof display)); |
4132 | 0 | display.image = image; |
4133 | 0 | display.buffer = buffer; |
4134 | 0 | display.row_stride = row_stride; |
4135 | 0 | display.colormap = colormap; |
4136 | 0 | display.background = background; |
4137 | 0 | display.local_row = NULL; |
4138 | | |
4139 | | /* Choose the correct 'end' routine; for the color-map case |
4140 | | * all the setup has already been done. |
4141 | | */ |
4142 | 0 | if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0) |
4143 | 0 | result = |
4144 | 0 | png_safe_execute(image, |
4145 | 0 | png_image_read_colormap, &display) && |
4146 | 0 | png_safe_execute(image, |
4147 | 0 | png_image_read_colormapped, &display); |
4148 | | |
4149 | 0 | else |
4150 | 0 | result = |
4151 | 0 | png_safe_execute(image, |
4152 | 0 | png_image_read_direct, &display); |
4153 | |
|
4154 | 0 | png_image_free(image); |
4155 | 0 | return result; |
4156 | 0 | } |
4157 | | |
4158 | 0 | else |
4159 | 0 | return png_image_error(image, |
4160 | 0 | "png_image_finish_read[color-map]: no color-map"); |
4161 | 0 | } |
4162 | | |
4163 | 0 | else |
4164 | 0 | return png_image_error(image, |
4165 | 0 | "png_image_finish_read: image too large"); |
4166 | 0 | } |
4167 | | |
4168 | 0 | else |
4169 | 0 | return png_image_error(image, |
4170 | 0 | "png_image_finish_read: invalid argument"); |
4171 | 0 | } |
4172 | | |
4173 | 0 | else |
4174 | 0 | return png_image_error(image, |
4175 | 0 | "png_image_finish_read: row_stride too large"); |
4176 | 0 | } |
4177 | | |
4178 | 0 | else if (image != NULL) |
4179 | 0 | return png_image_error(image, |
4180 | 0 | "png_image_finish_read: damaged PNG_IMAGE_VERSION"); |
4181 | | |
4182 | 0 | return 0; |
4183 | 0 | } |
4184 | | |
4185 | | #endif /* SIMPLIFIED_READ */ |
4186 | | #endif /* READ */ |