Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: third_party/libpng/pngread.c

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

Powered by Google App Engine
This is Rietveld 408576698