1 #include <sys/cdefs.h>
2 __RCSID("$MirOS: src/lib/libpng/pngread.c,v 1.5 2013/08/06 18:49:30 tg Exp $");
3
4 /* pngread.c - read a PNG file
5 *
6 * Last changed in libpng 1.2.48 [March 8, 2012]
7 * Copyright (c) 1998-2012 Glenn Randers-Pehrson
8 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
9 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
10 *
11 * This code is released under the libpng license.
12 * For conditions of distribution and use, see the disclaimer
13 * and license in png.h
14 *
15 * This file contains routines that an application calls directly to
16 * read a PNG file or stream.
17 */
18
19 #define PNG_INTERNAL
20 #define PNG_NO_PEDANTIC_WARNINGS
21 #include "png.h"
22 #ifdef PNG_READ_SUPPORTED
23
24 /* Create a PNG structure for reading, and allocate any memory needed. */
25 png_structp PNGAPI
png_create_read_struct(png_const_charp user_png_ver,png_voidp error_ptr,png_error_ptr error_fn,png_error_ptr warn_fn)26 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
27 png_error_ptr error_fn, png_error_ptr warn_fn)
28 {
29
30 #ifdef PNG_USER_MEM_SUPPORTED
31 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
32 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
33 }
34
35 /* Alternate create PNG structure for reading, and allocate any memory
36 * needed.
37 */
38 png_structp PNGAPI
png_create_read_struct_2(png_const_charp user_png_ver,png_voidp error_ptr,png_error_ptr error_fn,png_error_ptr warn_fn,png_voidp mem_ptr,png_malloc_ptr malloc_fn,png_free_ptr free_fn)39 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
40 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
41 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
42 {
43 #endif /* PNG_USER_MEM_SUPPORTED */
44
45 #ifdef PNG_SETJMP_SUPPORTED
46 volatile
47 #endif
48 png_structp png_ptr;
49
50 #ifdef PNG_SETJMP_SUPPORTED
51 #ifdef USE_FAR_KEYWORD
52 jmp_buf jmpbuf;
53 #endif
54 #endif
55
56 int i;
57
58 png_debug(1, "in png_create_read_struct");
59
60 #ifdef PNG_USER_MEM_SUPPORTED
61 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
62 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
63 #else
64 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
65 #endif
66 if (png_ptr == NULL)
67 return (NULL);
68
69 /* Added at libpng-1.2.6 */
70 #ifdef PNG_USER_LIMITS_SUPPORTED
71 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
72 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
73 /* Added at libpng-1.2.43 and 1.4.0 */
74 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
75 #endif
76
77 #ifdef PNG_SETJMP_SUPPORTED
78 #ifdef USE_FAR_KEYWORD
79 if (setjmp(jmpbuf))
80 #else
81 if (setjmp(png_ptr->jmpbuf))
82 #endif
83 {
84 png_free(png_ptr, png_ptr->zbuf);
85 png_ptr->zbuf = NULL;
86 #ifdef PNG_USER_MEM_SUPPORTED
87 png_destroy_struct_2((png_voidp)png_ptr,
88 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
89 #else
90 png_destroy_struct((png_voidp)png_ptr);
91 #endif
92 return (NULL);
93 }
94 #ifdef USE_FAR_KEYWORD
95 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
96 #endif
97 #endif /* PNG_SETJMP_SUPPORTED */
98
99 #ifdef PNG_USER_MEM_SUPPORTED
100 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
101 #endif
102
103 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
104
105 if (user_png_ver)
106 {
107 i = 0;
108 do
109 {
110 if (user_png_ver[i] != png_libpng_ver[i])
111 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
112 } while (png_libpng_ver[i++]);
113 }
114 else
115 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
116
117
118 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
119 {
120 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
121 * we must recompile any applications that use any older library version.
122 * For versions after libpng 1.0, we will be compatible, so we need
123 * only check the first digit.
124 */
125 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
126 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
127 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
128 {
129 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
130 char msg[80];
131 if (user_png_ver)
132 {
133 png_snprintf(msg, 80,
134 "Application was compiled with png.h from libpng-%.20s",
135 user_png_ver);
136 png_warning(png_ptr, msg);
137 }
138 png_snprintf(msg, 80,
139 "Application is running with png.c from libpng-%.20s",
140 png_libpng_ver);
141 png_warning(png_ptr, msg);
142 #endif
143 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
144 png_ptr->flags = 0;
145 #endif
146 png_error(png_ptr,
147 "Incompatible libpng version in application and library");
148 }
149 }
150
151 /* Initialize zbuf - compression buffer */
152 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
153 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
154 (png_uint_32)png_ptr->zbuf_size);
155 png_ptr->zstream.zalloc = png_zalloc;
156 png_ptr->zstream.zfree = png_zfree;
157 png_ptr->zstream.opaque = (voidpf)png_ptr;
158
159 switch (inflateInit(&png_ptr->zstream))
160 {
161 case Z_OK: /* Do nothing */ break;
162 case Z_MEM_ERROR:
163 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error");
164 break;
165 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error");
166 break;
167 default: png_error(png_ptr, "Unknown zlib error");
168 }
169
170
171 png_ptr->zstream.next_out = png_ptr->zbuf;
172 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
173
174 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
175
176 #ifdef PNG_SETJMP_SUPPORTED
177 /* Applications that neglect to set up their own setjmp() and then
178 encounter a png_error() will longjmp here. Since the jmpbuf is
179 then meaningless we abort instead of returning. */
180 #ifdef USE_FAR_KEYWORD
181 if (setjmp(jmpbuf))
182 PNG_ABORT();
183 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
184 #else
185 if (setjmp(png_ptr->jmpbuf))
186 PNG_ABORT();
187 #endif
188 #endif /* PNG_SETJMP_SUPPORTED */
189
190 return (png_ptr);
191 }
192
193 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
194 /* Initialize PNG structure for reading, and allocate any memory needed.
195 * This interface is deprecated in favour of the png_create_read_struct(),
196 * and it will disappear as of libpng-1.3.0.
197 */
198 #undef png_read_init
199 void PNGAPI
png_read_init(png_structp png_ptr)200 png_read_init(png_structp png_ptr)
201 {
202 /* We only come here via pre-1.0.7-compiled applications */
203 png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
204 }
205
206 void PNGAPI
png_read_init_2(png_structp png_ptr,png_const_charp user_png_ver,png_size_t png_struct_size,png_size_t png_info_size)207 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
208 png_size_t png_struct_size, png_size_t png_info_size)
209 {
210 /* We only come here via pre-1.0.12-compiled applications */
211 if (png_ptr == NULL)
212 return;
213 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
214 if (png_sizeof(png_struct) > png_struct_size ||
215 png_sizeof(png_info) > png_info_size)
216 {
217 char msg[80];
218 png_ptr->warning_fn = NULL;
219 if (user_png_ver)
220 {
221 png_snprintf(msg, 80,
222 "Application was compiled with png.h from libpng-%.20s",
223 user_png_ver);
224 png_warning(png_ptr, msg);
225 }
226 png_snprintf(msg, 80,
227 "Application is running with png.c from libpng-%.20s",
228 png_libpng_ver);
229 png_warning(png_ptr, msg);
230 }
231 #endif
232 if (png_sizeof(png_struct) > png_struct_size)
233 {
234 png_ptr->error_fn = NULL;
235 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
236 png_ptr->flags = 0;
237 #endif
238 png_error(png_ptr,
239 "The png struct allocated by the application for reading is"
240 " too small.");
241 }
242 if (png_sizeof(png_info) > png_info_size)
243 {
244 png_ptr->error_fn = NULL;
245 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
246 png_ptr->flags = 0;
247 #endif
248 png_error(png_ptr,
249 "The info struct allocated by application for reading is"
250 " too small.");
251 }
252 png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
253 }
254 #endif /* PNG_1_0_X || PNG_1_2_X */
255
256 void PNGAPI
png_read_init_3(png_structpp ptr_ptr,png_const_charp user_png_ver,png_size_t png_struct_size)257 png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
258 png_size_t png_struct_size)
259 {
260 #ifdef PNG_SETJMP_SUPPORTED
261 jmp_buf tmp_jmp; /* to save current jump buffer */
262 #endif
263
264 int i = 0;
265
266 png_structp png_ptr=*ptr_ptr;
267
268 if (png_ptr == NULL)
269 return;
270
271 do
272 {
273 if (user_png_ver[i] != png_libpng_ver[i])
274 {
275 #ifdef PNG_LEGACY_SUPPORTED
276 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
277 #else
278 png_ptr->warning_fn = NULL;
279 png_warning(png_ptr,
280 "Application uses deprecated png_read_init() and should be"
281 " recompiled.");
282 break;
283 #endif
284 }
285 } while (png_libpng_ver[i++]);
286
287 png_debug(1, "in png_read_init_3");
288
289 #ifdef PNG_SETJMP_SUPPORTED
290 /* Save jump buffer and error functions */
291 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
292 #endif
293
294 if (png_sizeof(png_struct) > png_struct_size)
295 {
296 png_destroy_struct(png_ptr);
297 *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
298 png_ptr = *ptr_ptr;
299 }
300
301 /* Reset all variables to 0 */
302 png_memset(png_ptr, 0, png_sizeof(png_struct));
303
304 #ifdef PNG_SETJMP_SUPPORTED
305 /* Restore jump buffer */
306 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
307 #endif
308
309 /* Added at libpng-1.2.6 */
310 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
311 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
312 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
313 #endif
314
315 /* Initialize zbuf - compression buffer */
316 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
317 png_ptr->zstream.zalloc = png_zalloc;
318 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
319 (png_uint_32)png_ptr->zbuf_size);
320 png_ptr->zstream.zalloc = png_zalloc;
321 png_ptr->zstream.zfree = png_zfree;
322 png_ptr->zstream.opaque = (voidpf)png_ptr;
323
324 switch (inflateInit(&png_ptr->zstream))
325 {
326 case Z_OK: /* Do nothing */ break;
327 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
328 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error");
329 break;
330 default: png_error(png_ptr, "Unknown zlib error");
331 }
332
333 png_ptr->zstream.next_out = png_ptr->zbuf;
334 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
335
336 png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
337 }
338
339 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
340 /* Read the information before the actual image data. This has been
341 * changed in v0.90 to allow reading a file that already has the magic
342 * bytes read from the stream. You can tell libpng how many bytes have
343 * been read from the beginning of the stream (up to the maximum of 8)
344 * via png_set_sig_bytes(), and we will only check the remaining bytes
345 * here. The application can then have access to the signature bytes we
346 * read if it is determined that this isn't a valid PNG file.
347 */
348 void PNGAPI
png_read_info(png_structp png_ptr,png_infop info_ptr)349 png_read_info(png_structp png_ptr, png_infop info_ptr)
350 {
351 png_debug(1, "in png_read_info");
352
353 if (png_ptr == NULL || info_ptr == NULL)
354 return;
355
356 /* If we haven't checked all of the PNG signature bytes, do so now. */
357 if (png_ptr->sig_bytes < 8)
358 {
359 png_size_t num_checked = png_ptr->sig_bytes,
360 num_to_check = 8 - num_checked;
361
362 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
363 png_ptr->sig_bytes = 8;
364
365 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
366 {
367 if (num_checked < 4 &&
368 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
369 png_error(png_ptr, "Not a PNG file");
370 else
371 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
372 }
373 if (num_checked < 3)
374 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
375 }
376
377 for (;;)
378 {
379 #ifdef PNG_USE_LOCAL_ARRAYS
380 PNG_CONST PNG_IHDR;
381 PNG_CONST PNG_IDAT;
382 PNG_CONST PNG_IEND;
383 PNG_CONST PNG_PLTE;
384 #ifdef PNG_READ_bKGD_SUPPORTED
385 PNG_CONST PNG_bKGD;
386 #endif
387 #ifdef PNG_READ_cHRM_SUPPORTED
388 PNG_CONST PNG_cHRM;
389 #endif
390 #ifdef PNG_READ_gAMA_SUPPORTED
391 PNG_CONST PNG_gAMA;
392 #endif
393 #ifdef PNG_READ_hIST_SUPPORTED
394 PNG_CONST PNG_hIST;
395 #endif
396 #ifdef PNG_READ_iCCP_SUPPORTED
397 PNG_CONST PNG_iCCP;
398 #endif
399 #ifdef PNG_READ_iTXt_SUPPORTED
400 PNG_CONST PNG_iTXt;
401 #endif
402 #ifdef PNG_READ_oFFs_SUPPORTED
403 PNG_CONST PNG_oFFs;
404 #endif
405 #ifdef PNG_READ_pCAL_SUPPORTED
406 PNG_CONST PNG_pCAL;
407 #endif
408 #ifdef PNG_READ_pHYs_SUPPORTED
409 PNG_CONST PNG_pHYs;
410 #endif
411 #ifdef PNG_READ_sBIT_SUPPORTED
412 PNG_CONST PNG_sBIT;
413 #endif
414 #ifdef PNG_READ_sCAL_SUPPORTED
415 PNG_CONST PNG_sCAL;
416 #endif
417 #ifdef PNG_READ_sPLT_SUPPORTED
418 PNG_CONST PNG_sPLT;
419 #endif
420 #ifdef PNG_READ_sRGB_SUPPORTED
421 PNG_CONST PNG_sRGB;
422 #endif
423 #ifdef PNG_READ_tEXt_SUPPORTED
424 PNG_CONST PNG_tEXt;
425 #endif
426 #ifdef PNG_READ_tIME_SUPPORTED
427 PNG_CONST PNG_tIME;
428 #endif
429 #ifdef PNG_READ_tRNS_SUPPORTED
430 PNG_CONST PNG_tRNS;
431 #endif
432 #ifdef PNG_READ_zTXt_SUPPORTED
433 PNG_CONST PNG_zTXt;
434 #endif
435 #endif /* PNG_USE_LOCAL_ARRAYS */
436 png_uint_32 length = png_read_chunk_header(png_ptr);
437 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
438
439 /* This should be a binary subdivision search or a hash for
440 * matching the chunk name rather than a linear search.
441 */
442 if (!png_memcmp(chunk_name, png_IDAT, 4))
443 if (png_ptr->mode & PNG_AFTER_IDAT)
444 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
445
446 if (!png_memcmp(chunk_name, png_IHDR, 4))
447 png_handle_IHDR(png_ptr, info_ptr, length);
448 else if (!png_memcmp(chunk_name, png_IEND, 4))
449 png_handle_IEND(png_ptr, info_ptr, length);
450 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
451 else if (png_handle_as_unknown(png_ptr, chunk_name))
452 {
453 if (!png_memcmp(chunk_name, png_IDAT, 4))
454 png_ptr->mode |= PNG_HAVE_IDAT;
455 png_handle_unknown(png_ptr, info_ptr, length);
456 if (!png_memcmp(chunk_name, png_PLTE, 4))
457 png_ptr->mode |= PNG_HAVE_PLTE;
458 else if (!png_memcmp(chunk_name, png_IDAT, 4))
459 {
460 if (!(png_ptr->mode & PNG_HAVE_IHDR))
461 png_error(png_ptr, "Missing IHDR before IDAT");
462 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
463 !(png_ptr->mode & PNG_HAVE_PLTE))
464 png_error(png_ptr, "Missing PLTE before IDAT");
465 break;
466 }
467 }
468 #endif
469 else if (!png_memcmp(chunk_name, png_PLTE, 4))
470 png_handle_PLTE(png_ptr, info_ptr, length);
471 else if (!png_memcmp(chunk_name, png_IDAT, 4))
472 {
473 if (!(png_ptr->mode & PNG_HAVE_IHDR))
474 png_error(png_ptr, "Missing IHDR before IDAT");
475 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
476 !(png_ptr->mode & PNG_HAVE_PLTE))
477 png_error(png_ptr, "Missing PLTE before IDAT");
478
479 png_ptr->idat_size = length;
480 png_ptr->mode |= PNG_HAVE_IDAT;
481 break;
482 }
483 #ifdef PNG_READ_bKGD_SUPPORTED
484 else if (!png_memcmp(chunk_name, png_bKGD, 4))
485 png_handle_bKGD(png_ptr, info_ptr, length);
486 #endif
487 #ifdef PNG_READ_cHRM_SUPPORTED
488 else if (!png_memcmp(chunk_name, png_cHRM, 4))
489 png_handle_cHRM(png_ptr, info_ptr, length);
490 #endif
491 #ifdef PNG_READ_gAMA_SUPPORTED
492 else if (!png_memcmp(chunk_name, png_gAMA, 4))
493 png_handle_gAMA(png_ptr, info_ptr, length);
494 #endif
495 #ifdef PNG_READ_hIST_SUPPORTED
496 else if (!png_memcmp(chunk_name, png_hIST, 4))
497 png_handle_hIST(png_ptr, info_ptr, length);
498 #endif
499 #ifdef PNG_READ_oFFs_SUPPORTED
500 else if (!png_memcmp(chunk_name, png_oFFs, 4))
501 png_handle_oFFs(png_ptr, info_ptr, length);
502 #endif
503 #ifdef PNG_READ_pCAL_SUPPORTED
504 else if (!png_memcmp(chunk_name, png_pCAL, 4))
505 png_handle_pCAL(png_ptr, info_ptr, length);
506 #endif
507 #ifdef PNG_READ_sCAL_SUPPORTED
508 else if (!png_memcmp(chunk_name, png_sCAL, 4))
509 png_handle_sCAL(png_ptr, info_ptr, length);
510 #endif
511 #ifdef PNG_READ_pHYs_SUPPORTED
512 else if (!png_memcmp(chunk_name, png_pHYs, 4))
513 png_handle_pHYs(png_ptr, info_ptr, length);
514 #endif
515 #ifdef PNG_READ_sBIT_SUPPORTED
516 else if (!png_memcmp(chunk_name, png_sBIT, 4))
517 png_handle_sBIT(png_ptr, info_ptr, length);
518 #endif
519 #ifdef PNG_READ_sRGB_SUPPORTED
520 else if (!png_memcmp(chunk_name, png_sRGB, 4))
521 png_handle_sRGB(png_ptr, info_ptr, length);
522 #endif
523 #ifdef PNG_READ_iCCP_SUPPORTED
524 else if (!png_memcmp(chunk_name, png_iCCP, 4))
525 png_handle_iCCP(png_ptr, info_ptr, length);
526 #endif
527 #ifdef PNG_READ_sPLT_SUPPORTED
528 else if (!png_memcmp(chunk_name, png_sPLT, 4))
529 png_handle_sPLT(png_ptr, info_ptr, length);
530 #endif
531 #ifdef PNG_READ_tEXt_SUPPORTED
532 else if (!png_memcmp(chunk_name, png_tEXt, 4))
533 png_handle_tEXt(png_ptr, info_ptr, length);
534 #endif
535 #ifdef PNG_READ_tIME_SUPPORTED
536 else if (!png_memcmp(chunk_name, png_tIME, 4))
537 png_handle_tIME(png_ptr, info_ptr, length);
538 #endif
539 #ifdef PNG_READ_tRNS_SUPPORTED
540 else if (!png_memcmp(chunk_name, png_tRNS, 4))
541 png_handle_tRNS(png_ptr, info_ptr, length);
542 #endif
543 #ifdef PNG_READ_zTXt_SUPPORTED
544 else if (!png_memcmp(chunk_name, png_zTXt, 4))
545 png_handle_zTXt(png_ptr, info_ptr, length);
546 #endif
547 #ifdef PNG_READ_iTXt_SUPPORTED
548 else if (!png_memcmp(chunk_name, png_iTXt, 4))
549 png_handle_iTXt(png_ptr, info_ptr, length);
550 #endif
551 else
552 png_handle_unknown(png_ptr, info_ptr, length);
553 }
554 }
555 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
556
557 /* Optional call to update the users info_ptr structure */
558 void PNGAPI
png_read_update_info(png_structp png_ptr,png_infop info_ptr)559 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
560 {
561 png_debug(1, "in png_read_update_info");
562
563 if (png_ptr == NULL)
564 return;
565 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
566 png_read_start_row(png_ptr);
567 else
568 png_warning(png_ptr,
569 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
570
571 png_read_transform_info(png_ptr, info_ptr);
572 }
573
574 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
575 /* Initialize palette, background, etc, after transformations
576 * are set, but before any reading takes place. This allows
577 * the user to obtain a gamma-corrected palette, for example.
578 * If the user doesn't call this, we will do it ourselves.
579 */
580 void PNGAPI
png_start_read_image(png_structp png_ptr)581 png_start_read_image(png_structp png_ptr)
582 {
583 png_debug(1, "in png_start_read_image");
584
585 if (png_ptr == NULL)
586 return;
587 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
588 png_read_start_row(png_ptr);
589 }
590 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
591
592 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
593 void PNGAPI
png_read_row(png_structp png_ptr,png_bytep row,png_bytep dsp_row)594 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
595 {
596 #ifndef PNG_USE_GLOBAL_ARRAYS
597 PNG_CONST PNG_IDAT;
598 PNG_CONST int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55,
599 0xff};
600 PNG_CONST int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
601 #endif
602 int ret;
603
604 if (png_ptr == NULL)
605 return;
606
607 png_debug2(1, "in png_read_row (row %lu, pass %d)",
608 png_ptr->row_number, png_ptr->pass);
609
610 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
611 png_read_start_row(png_ptr);
612 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
613 {
614 /* Check for transforms that have been set but were defined out */
615 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
616 if (png_ptr->transformations & PNG_INVERT_MONO)
617 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
618 #endif
619 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
620 if (png_ptr->transformations & PNG_FILLER)
621 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
622 #endif
623 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
624 !defined(PNG_READ_PACKSWAP_SUPPORTED)
625 if (png_ptr->transformations & PNG_PACKSWAP)
626 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
627 #endif
628 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
629 if (png_ptr->transformations & PNG_PACK)
630 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
631 #endif
632 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
633 if (png_ptr->transformations & PNG_SHIFT)
634 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
635 #endif
636 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
637 if (png_ptr->transformations & PNG_BGR)
638 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
639 #endif
640 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
641 if (png_ptr->transformations & PNG_SWAP_BYTES)
642 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
643 #endif
644 }
645
646 #ifdef PNG_READ_INTERLACING_SUPPORTED
647 /* If interlaced and we do not need a new row, combine row and return */
648 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
649 {
650 switch (png_ptr->pass)
651 {
652 case 0:
653 if (png_ptr->row_number & 0x07)
654 {
655 if (dsp_row != NULL)
656 png_combine_row(png_ptr, dsp_row,
657 png_pass_dsp_mask[png_ptr->pass]);
658 png_read_finish_row(png_ptr);
659 return;
660 }
661 break;
662 case 1:
663 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
664 {
665 if (dsp_row != NULL)
666 png_combine_row(png_ptr, dsp_row,
667 png_pass_dsp_mask[png_ptr->pass]);
668 png_read_finish_row(png_ptr);
669 return;
670 }
671 break;
672 case 2:
673 if ((png_ptr->row_number & 0x07) != 4)
674 {
675 if (dsp_row != NULL && (png_ptr->row_number & 4))
676 png_combine_row(png_ptr, dsp_row,
677 png_pass_dsp_mask[png_ptr->pass]);
678 png_read_finish_row(png_ptr);
679 return;
680 }
681 break;
682 case 3:
683 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
684 {
685 if (dsp_row != NULL)
686 png_combine_row(png_ptr, dsp_row,
687 png_pass_dsp_mask[png_ptr->pass]);
688 png_read_finish_row(png_ptr);
689 return;
690 }
691 break;
692 case 4:
693 if ((png_ptr->row_number & 3) != 2)
694 {
695 if (dsp_row != NULL && (png_ptr->row_number & 2))
696 png_combine_row(png_ptr, dsp_row,
697 png_pass_dsp_mask[png_ptr->pass]);
698 png_read_finish_row(png_ptr);
699 return;
700 }
701 break;
702 case 5:
703 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
704 {
705 if (dsp_row != NULL)
706 png_combine_row(png_ptr, dsp_row,
707 png_pass_dsp_mask[png_ptr->pass]);
708 png_read_finish_row(png_ptr);
709 return;
710 }
711 break;
712 case 6:
713 if (!(png_ptr->row_number & 1))
714 {
715 png_read_finish_row(png_ptr);
716 return;
717 }
718 break;
719 }
720 }
721 #endif
722
723 if (!(png_ptr->mode & PNG_HAVE_IDAT))
724 png_error(png_ptr, "Invalid attempt to read row data");
725
726 png_ptr->zstream.next_out = png_ptr->row_buf;
727 png_ptr->zstream.avail_out =
728 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
729 png_ptr->iwidth) + 1);
730 do
731 {
732 if (!(png_ptr->zstream.avail_in))
733 {
734 while (!png_ptr->idat_size)
735 {
736 png_crc_finish(png_ptr, 0);
737
738 png_ptr->idat_size = png_read_chunk_header(png_ptr);
739 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
740 png_error(png_ptr, "Not enough image data");
741 }
742 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
743 png_ptr->zstream.next_in = png_ptr->zbuf;
744 if (png_ptr->zbuf_size > png_ptr->idat_size)
745 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
746 png_crc_read(png_ptr, png_ptr->zbuf,
747 (png_size_t)png_ptr->zstream.avail_in);
748 png_ptr->idat_size -= png_ptr->zstream.avail_in;
749 }
750 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
751 if (ret == Z_STREAM_END)
752 {
753 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
754 png_ptr->idat_size)
755 png_error(png_ptr, "Extra compressed data");
756 png_ptr->mode |= PNG_AFTER_IDAT;
757 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
758 break;
759 }
760 if (ret != Z_OK)
761 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
762 "Decompression error");
763
764 } while (png_ptr->zstream.avail_out);
765
766 png_ptr->row_info.color_type = png_ptr->color_type;
767 png_ptr->row_info.width = png_ptr->iwidth;
768 png_ptr->row_info.channels = png_ptr->channels;
769 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
770 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
771 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
772 png_ptr->row_info.width);
773
774 if (png_ptr->row_buf[0])
775 png_read_filter_row(png_ptr, &(png_ptr->row_info),
776 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
777 (int)(png_ptr->row_buf[0]));
778
779 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
780 png_ptr->rowbytes + 1);
781
782 #ifdef PNG_MNG_FEATURES_SUPPORTED
783 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
784 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
785 {
786 /* Intrapixel differencing */
787 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
788 }
789 #endif
790
791
792 if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA))
793 png_do_read_transformations(png_ptr);
794
795 #ifdef PNG_READ_INTERLACING_SUPPORTED
796 /* Blow up interlaced rows to full size */
797 if (png_ptr->interlaced &&
798 (png_ptr->transformations & PNG_INTERLACE))
799 {
800 if (png_ptr->pass < 6)
801 /* Old interface (pre-1.0.9):
802 * png_do_read_interlace(&(png_ptr->row_info),
803 * png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
804 */
805 png_do_read_interlace(png_ptr);
806
807 if (dsp_row != NULL)
808 png_combine_row(png_ptr, dsp_row,
809 png_pass_dsp_mask[png_ptr->pass]);
810 if (row != NULL)
811 png_combine_row(png_ptr, row,
812 png_pass_mask[png_ptr->pass]);
813 }
814 else
815 #endif
816 {
817 if (row != NULL)
818 png_combine_row(png_ptr, row, 0xff);
819 if (dsp_row != NULL)
820 png_combine_row(png_ptr, dsp_row, 0xff);
821 }
822 png_read_finish_row(png_ptr);
823
824 if (png_ptr->read_row_fn != NULL)
825 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
826 }
827 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
828
829 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
830 /* Read one or more rows of image data. If the image is interlaced,
831 * and png_set_interlace_handling() has been called, the rows need to
832 * contain the contents of the rows from the previous pass. If the
833 * image has alpha or transparency, and png_handle_alpha()[*] has been
834 * called, the rows contents must be initialized to the contents of the
835 * screen.
836 *
837 * "row" holds the actual image, and pixels are placed in it
838 * as they arrive. If the image is displayed after each pass, it will
839 * appear to "sparkle" in. "display_row" can be used to display a
840 * "chunky" progressive image, with finer detail added as it becomes
841 * available. If you do not want this "chunky" display, you may pass
842 * NULL for display_row. If you do not want the sparkle display, and
843 * you have not called png_handle_alpha(), you may pass NULL for rows.
844 * If you have called png_handle_alpha(), and the image has either an
845 * alpha channel or a transparency chunk, you must provide a buffer for
846 * rows. In this case, you do not have to provide a display_row buffer
847 * also, but you may. If the image is not interlaced, or if you have
848 * not called png_set_interlace_handling(), the display_row buffer will
849 * be ignored, so pass NULL to it.
850 *
851 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
852 */
853
854 void PNGAPI
png_read_rows(png_structp png_ptr,png_bytepp row,png_bytepp display_row,png_uint_32 num_rows)855 png_read_rows(png_structp png_ptr, png_bytepp row,
856 png_bytepp display_row, png_uint_32 num_rows)
857 {
858 png_uint_32 i;
859 png_bytepp rp;
860 png_bytepp dp;
861
862 png_debug(1, "in png_read_rows");
863
864 if (png_ptr == NULL)
865 return;
866 rp = row;
867 dp = display_row;
868 if (rp != NULL && dp != NULL)
869 for (i = 0; i < num_rows; i++)
870 {
871 png_bytep rptr = *rp++;
872 png_bytep dptr = *dp++;
873
874 png_read_row(png_ptr, rptr, dptr);
875 }
876 else if (rp != NULL)
877 for (i = 0; i < num_rows; i++)
878 {
879 png_bytep rptr = *rp;
880 png_read_row(png_ptr, rptr, png_bytep_NULL);
881 rp++;
882 }
883 else if (dp != NULL)
884 for (i = 0; i < num_rows; i++)
885 {
886 png_bytep dptr = *dp;
887 png_read_row(png_ptr, png_bytep_NULL, dptr);
888 dp++;
889 }
890 }
891 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
892
893 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
894 /* Read the entire image. If the image has an alpha channel or a tRNS
895 * chunk, and you have called png_handle_alpha()[*], you will need to
896 * initialize the image to the current image that PNG will be overlaying.
897 * We set the num_rows again here, in case it was incorrectly set in
898 * png_read_start_row() by a call to png_read_update_info() or
899 * png_start_read_image() if png_set_interlace_handling() wasn't called
900 * prior to either of these functions like it should have been. You can
901 * only call this function once. If you desire to have an image for
902 * each pass of a interlaced image, use png_read_rows() instead.
903 *
904 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
905 */
906 void PNGAPI
png_read_image(png_structp png_ptr,png_bytepp image)907 png_read_image(png_structp png_ptr, png_bytepp image)
908 {
909 png_uint_32 i, image_height;
910 int pass, j;
911 png_bytepp rp;
912
913 png_debug(1, "in png_read_image");
914
915 if (png_ptr == NULL)
916 return;
917
918 #ifdef PNG_READ_INTERLACING_SUPPORTED
919 pass = png_set_interlace_handling(png_ptr);
920 #else
921 if (png_ptr->interlaced)
922 png_error(png_ptr,
923 "Cannot read interlaced image -- interlace handler disabled.");
924 pass = 1;
925 #endif
926
927
928 image_height=png_ptr->height;
929 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
930
931 for (j = 0; j < pass; j++)
932 {
933 rp = image;
934 for (i = 0; i < image_height; i++)
935 {
936 png_read_row(png_ptr, *rp, png_bytep_NULL);
937 rp++;
938 }
939 }
940 }
941 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
942
943 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
944 /* Read the end of the PNG file. Will not read past the end of the
945 * file, will verify the end is accurate, and will read any comments
946 * or time information at the end of the file, if info is not NULL.
947 */
948 void PNGAPI
png_read_end(png_structp png_ptr,png_infop info_ptr)949 png_read_end(png_structp png_ptr, png_infop info_ptr)
950 {
951 png_debug(1, "in png_read_end");
952
953 if (png_ptr == NULL)
954 return;
955 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
956
957 do
958 {
959 #ifdef PNG_USE_LOCAL_ARRAYS
960 PNG_CONST PNG_IHDR;
961 PNG_CONST PNG_IDAT;
962 PNG_CONST PNG_IEND;
963 PNG_CONST PNG_PLTE;
964 #ifdef PNG_READ_bKGD_SUPPORTED
965 PNG_CONST PNG_bKGD;
966 #endif
967 #ifdef PNG_READ_cHRM_SUPPORTED
968 PNG_CONST PNG_cHRM;
969 #endif
970 #ifdef PNG_READ_gAMA_SUPPORTED
971 PNG_CONST PNG_gAMA;
972 #endif
973 #ifdef PNG_READ_hIST_SUPPORTED
974 PNG_CONST PNG_hIST;
975 #endif
976 #ifdef PNG_READ_iCCP_SUPPORTED
977 PNG_CONST PNG_iCCP;
978 #endif
979 #ifdef PNG_READ_iTXt_SUPPORTED
980 PNG_CONST PNG_iTXt;
981 #endif
982 #ifdef PNG_READ_oFFs_SUPPORTED
983 PNG_CONST PNG_oFFs;
984 #endif
985 #ifdef PNG_READ_pCAL_SUPPORTED
986 PNG_CONST PNG_pCAL;
987 #endif
988 #ifdef PNG_READ_pHYs_SUPPORTED
989 PNG_CONST PNG_pHYs;
990 #endif
991 #ifdef PNG_READ_sBIT_SUPPORTED
992 PNG_CONST PNG_sBIT;
993 #endif
994 #ifdef PNG_READ_sCAL_SUPPORTED
995 PNG_CONST PNG_sCAL;
996 #endif
997 #ifdef PNG_READ_sPLT_SUPPORTED
998 PNG_CONST PNG_sPLT;
999 #endif
1000 #ifdef PNG_READ_sRGB_SUPPORTED
1001 PNG_CONST PNG_sRGB;
1002 #endif
1003 #ifdef PNG_READ_tEXt_SUPPORTED
1004 PNG_CONST PNG_tEXt;
1005 #endif
1006 #ifdef PNG_READ_tIME_SUPPORTED
1007 PNG_CONST PNG_tIME;
1008 #endif
1009 #ifdef PNG_READ_tRNS_SUPPORTED
1010 PNG_CONST PNG_tRNS;
1011 #endif
1012 #ifdef PNG_READ_zTXt_SUPPORTED
1013 PNG_CONST PNG_zTXt;
1014 #endif
1015 #endif /* PNG_USE_LOCAL_ARRAYS */
1016 png_uint_32 length = png_read_chunk_header(png_ptr);
1017 PNG_CONST png_bytep chunk_name = png_ptr->chunk_name;
1018
1019 if (!png_memcmp(chunk_name, png_IHDR, 4))
1020 png_handle_IHDR(png_ptr, info_ptr, length);
1021 else if (!png_memcmp(chunk_name, png_IEND, 4))
1022 png_handle_IEND(png_ptr, info_ptr, length);
1023 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1024 else if (png_handle_as_unknown(png_ptr, chunk_name))
1025 {
1026 if (!png_memcmp(chunk_name, png_IDAT, 4))
1027 {
1028 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
1029 png_error(png_ptr, "Too many IDAT's found");
1030 }
1031 png_handle_unknown(png_ptr, info_ptr, length);
1032 if (!png_memcmp(chunk_name, png_PLTE, 4))
1033 png_ptr->mode |= PNG_HAVE_PLTE;
1034 }
1035 #endif
1036 else if (!png_memcmp(chunk_name, png_IDAT, 4))
1037 {
1038 /* Zero length IDATs are legal after the last IDAT has been
1039 * read, but not after other chunks have been read.
1040 */
1041 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
1042 png_error(png_ptr, "Too many IDAT's found");
1043 png_crc_finish(png_ptr, length);
1044 }
1045 else if (!png_memcmp(chunk_name, png_PLTE, 4))
1046 png_handle_PLTE(png_ptr, info_ptr, length);
1047 #ifdef PNG_READ_bKGD_SUPPORTED
1048 else if (!png_memcmp(chunk_name, png_bKGD, 4))
1049 png_handle_bKGD(png_ptr, info_ptr, length);
1050 #endif
1051 #ifdef PNG_READ_cHRM_SUPPORTED
1052 else if (!png_memcmp(chunk_name, png_cHRM, 4))
1053 png_handle_cHRM(png_ptr, info_ptr, length);
1054 #endif
1055 #ifdef PNG_READ_gAMA_SUPPORTED
1056 else if (!png_memcmp(chunk_name, png_gAMA, 4))
1057 png_handle_gAMA(png_ptr, info_ptr, length);
1058 #endif
1059 #ifdef PNG_READ_hIST_SUPPORTED
1060 else if (!png_memcmp(chunk_name, png_hIST, 4))
1061 png_handle_hIST(png_ptr, info_ptr, length);
1062 #endif
1063 #ifdef PNG_READ_oFFs_SUPPORTED
1064 else if (!png_memcmp(chunk_name, png_oFFs, 4))
1065 png_handle_oFFs(png_ptr, info_ptr, length);
1066 #endif
1067 #ifdef PNG_READ_pCAL_SUPPORTED
1068 else if (!png_memcmp(chunk_name, png_pCAL, 4))
1069 png_handle_pCAL(png_ptr, info_ptr, length);
1070 #endif
1071 #ifdef PNG_READ_sCAL_SUPPORTED
1072 else if (!png_memcmp(chunk_name, png_sCAL, 4))
1073 png_handle_sCAL(png_ptr, info_ptr, length);
1074 #endif
1075 #ifdef PNG_READ_pHYs_SUPPORTED
1076 else if (!png_memcmp(chunk_name, png_pHYs, 4))
1077 png_handle_pHYs(png_ptr, info_ptr, length);
1078 #endif
1079 #ifdef PNG_READ_sBIT_SUPPORTED
1080 else if (!png_memcmp(chunk_name, png_sBIT, 4))
1081 png_handle_sBIT(png_ptr, info_ptr, length);
1082 #endif
1083 #ifdef PNG_READ_sRGB_SUPPORTED
1084 else if (!png_memcmp(chunk_name, png_sRGB, 4))
1085 png_handle_sRGB(png_ptr, info_ptr, length);
1086 #endif
1087 #ifdef PNG_READ_iCCP_SUPPORTED
1088 else if (!png_memcmp(chunk_name, png_iCCP, 4))
1089 png_handle_iCCP(png_ptr, info_ptr, length);
1090 #endif
1091 #ifdef PNG_READ_sPLT_SUPPORTED
1092 else if (!png_memcmp(chunk_name, png_sPLT, 4))
1093 png_handle_sPLT(png_ptr, info_ptr, length);
1094 #endif
1095 #ifdef PNG_READ_tEXt_SUPPORTED
1096 else if (!png_memcmp(chunk_name, png_tEXt, 4))
1097 png_handle_tEXt(png_ptr, info_ptr, length);
1098 #endif
1099 #ifdef PNG_READ_tIME_SUPPORTED
1100 else if (!png_memcmp(chunk_name, png_tIME, 4))
1101 png_handle_tIME(png_ptr, info_ptr, length);
1102 #endif
1103 #ifdef PNG_READ_tRNS_SUPPORTED
1104 else if (!png_memcmp(chunk_name, png_tRNS, 4))
1105 png_handle_tRNS(png_ptr, info_ptr, length);
1106 #endif
1107 #ifdef PNG_READ_zTXt_SUPPORTED
1108 else if (!png_memcmp(chunk_name, png_zTXt, 4))
1109 png_handle_zTXt(png_ptr, info_ptr, length);
1110 #endif
1111 #ifdef PNG_READ_iTXt_SUPPORTED
1112 else if (!png_memcmp(chunk_name, png_iTXt, 4))
1113 png_handle_iTXt(png_ptr, info_ptr, length);
1114 #endif
1115 else
1116 png_handle_unknown(png_ptr, info_ptr, length);
1117 } while (!(png_ptr->mode & PNG_HAVE_IEND));
1118 }
1119 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1120
1121 /* Free all memory used by the read */
1122 void PNGAPI
png_destroy_read_struct(png_structpp png_ptr_ptr,png_infopp info_ptr_ptr,png_infopp end_info_ptr_ptr)1123 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
1124 png_infopp end_info_ptr_ptr)
1125 {
1126 png_structp png_ptr = NULL;
1127 png_infop info_ptr = NULL, end_info_ptr = NULL;
1128 #ifdef PNG_USER_MEM_SUPPORTED
1129 png_free_ptr free_fn = NULL;
1130 png_voidp mem_ptr = NULL;
1131 #endif
1132
1133 png_debug(1, "in png_destroy_read_struct");
1134
1135 if (png_ptr_ptr != NULL)
1136 png_ptr = *png_ptr_ptr;
1137 if (png_ptr == NULL)
1138 return;
1139
1140 #ifdef PNG_USER_MEM_SUPPORTED
1141 free_fn = png_ptr->free_fn;
1142 mem_ptr = png_ptr->mem_ptr;
1143 #endif
1144
1145 if (info_ptr_ptr != NULL)
1146 info_ptr = *info_ptr_ptr;
1147
1148 if (end_info_ptr_ptr != NULL)
1149 end_info_ptr = *end_info_ptr_ptr;
1150
1151 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
1152
1153 if (info_ptr != NULL)
1154 {
1155 #ifdef PNG_TEXT_SUPPORTED
1156 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1157 #endif
1158
1159 #ifdef PNG_USER_MEM_SUPPORTED
1160 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1161 (png_voidp)mem_ptr);
1162 #else
1163 png_destroy_struct((png_voidp)info_ptr);
1164 #endif
1165 *info_ptr_ptr = NULL;
1166 }
1167
1168 if (end_info_ptr != NULL)
1169 {
1170 #ifdef PNG_READ_TEXT_SUPPORTED
1171 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1172 #endif
1173 #ifdef PNG_USER_MEM_SUPPORTED
1174 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
1175 (png_voidp)mem_ptr);
1176 #else
1177 png_destroy_struct((png_voidp)end_info_ptr);
1178 #endif
1179 *end_info_ptr_ptr = NULL;
1180 }
1181
1182 if (png_ptr != NULL)
1183 {
1184 #ifdef PNG_USER_MEM_SUPPORTED
1185 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1186 (png_voidp)mem_ptr);
1187 #else
1188 png_destroy_struct((png_voidp)png_ptr);
1189 #endif
1190 *png_ptr_ptr = NULL;
1191 }
1192 }
1193
1194 /* Free all memory used by the read (old method) */
1195 void /* PRIVATE */
png_read_destroy(png_structp png_ptr,png_infop info_ptr,png_infop end_info_ptr)1196 png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1197 png_infop end_info_ptr)
1198 {
1199 #ifdef PNG_SETJMP_SUPPORTED
1200 jmp_buf tmp_jmp;
1201 #endif
1202 png_error_ptr error_fn;
1203 png_error_ptr warning_fn;
1204 png_voidp error_ptr;
1205 #ifdef PNG_USER_MEM_SUPPORTED
1206 png_free_ptr free_fn;
1207 #endif
1208
1209 png_debug(1, "in png_read_destroy");
1210
1211 if (info_ptr != NULL)
1212 png_info_destroy(png_ptr, info_ptr);
1213
1214 if (end_info_ptr != NULL)
1215 png_info_destroy(png_ptr, end_info_ptr);
1216
1217 png_free(png_ptr, png_ptr->zbuf);
1218 png_free(png_ptr, png_ptr->big_row_buf);
1219 png_free(png_ptr, png_ptr->prev_row);
1220 png_free(png_ptr, png_ptr->chunkdata);
1221 #ifdef PNG_READ_DITHER_SUPPORTED
1222 png_free(png_ptr, png_ptr->palette_lookup);
1223 png_free(png_ptr, png_ptr->dither_index);
1224 #endif
1225 #ifdef PNG_READ_GAMMA_SUPPORTED
1226 png_free(png_ptr, png_ptr->gamma_table);
1227 #endif
1228 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1229 png_free(png_ptr, png_ptr->gamma_from_1);
1230 png_free(png_ptr, png_ptr->gamma_to_1);
1231 #endif
1232 #ifdef PNG_FREE_ME_SUPPORTED
1233 if (png_ptr->free_me & PNG_FREE_PLTE)
1234 png_zfree(png_ptr, png_ptr->palette);
1235 png_ptr->free_me &= ~PNG_FREE_PLTE;
1236 #else
1237 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1238 png_zfree(png_ptr, png_ptr->palette);
1239 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1240 #endif
1241 #if defined(PNG_tRNS_SUPPORTED) || \
1242 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1243 #ifdef PNG_FREE_ME_SUPPORTED
1244 if (png_ptr->free_me & PNG_FREE_TRNS)
1245 png_free(png_ptr, png_ptr->trans);
1246 png_ptr->free_me &= ~PNG_FREE_TRNS;
1247 #else
1248 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1249 png_free(png_ptr, png_ptr->trans);
1250 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1251 #endif
1252 #endif
1253 #ifdef PNG_READ_hIST_SUPPORTED
1254 #ifdef PNG_FREE_ME_SUPPORTED
1255 if (png_ptr->free_me & PNG_FREE_HIST)
1256 png_free(png_ptr, png_ptr->hist);
1257 png_ptr->free_me &= ~PNG_FREE_HIST;
1258 #else
1259 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1260 png_free(png_ptr, png_ptr->hist);
1261 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1262 #endif
1263 #endif
1264 #ifdef PNG_READ_GAMMA_SUPPORTED
1265 if (png_ptr->gamma_16_table != NULL)
1266 {
1267 int i;
1268 int istop = (1 << (8 - png_ptr->gamma_shift));
1269 for (i = 0; i < istop; i++)
1270 {
1271 png_free(png_ptr, png_ptr->gamma_16_table[i]);
1272 }
1273 png_free(png_ptr, png_ptr->gamma_16_table);
1274 }
1275 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1276 if (png_ptr->gamma_16_from_1 != NULL)
1277 {
1278 int i;
1279 int istop = (1 << (8 - png_ptr->gamma_shift));
1280 for (i = 0; i < istop; i++)
1281 {
1282 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
1283 }
1284 png_free(png_ptr, png_ptr->gamma_16_from_1);
1285 }
1286 if (png_ptr->gamma_16_to_1 != NULL)
1287 {
1288 int i;
1289 int istop = (1 << (8 - png_ptr->gamma_shift));
1290 for (i = 0; i < istop; i++)
1291 {
1292 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
1293 }
1294 png_free(png_ptr, png_ptr->gamma_16_to_1);
1295 }
1296 #endif
1297 #endif
1298 #ifdef PNG_TIME_RFC1123_SUPPORTED
1299 png_free(png_ptr, png_ptr->time_buffer);
1300 #endif
1301
1302 inflateEnd(&png_ptr->zstream);
1303 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1304 png_free(png_ptr, png_ptr->save_buffer);
1305 #endif
1306
1307 /* Save the important info out of the png_struct, in case it is
1308 * being used again.
1309 */
1310 #ifdef PNG_SETJMP_SUPPORTED
1311 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
1312 #endif
1313
1314 error_fn = png_ptr->error_fn;
1315 warning_fn = png_ptr->warning_fn;
1316 error_ptr = png_ptr->error_ptr;
1317 #ifdef PNG_USER_MEM_SUPPORTED
1318 free_fn = png_ptr->free_fn;
1319 #endif
1320
1321 png_memset(png_ptr, 0, png_sizeof(png_struct));
1322
1323 png_ptr->error_fn = error_fn;
1324 png_ptr->warning_fn = warning_fn;
1325 png_ptr->error_ptr = error_ptr;
1326 #ifdef PNG_USER_MEM_SUPPORTED
1327 png_ptr->free_fn = free_fn;
1328 #endif
1329
1330 #ifdef PNG_SETJMP_SUPPORTED
1331 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
1332 #endif
1333
1334 }
1335
1336 void PNGAPI
png_set_read_status_fn(png_structp png_ptr,png_read_status_ptr read_row_fn)1337 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1338 {
1339 if (png_ptr == NULL)
1340 return;
1341 png_ptr->read_row_fn = read_row_fn;
1342 }
1343
1344
1345 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1346 #ifdef PNG_INFO_IMAGE_SUPPORTED
1347 void PNGAPI
png_read_png(png_structp png_ptr,png_infop info_ptr,int transforms,voidp params)1348 png_read_png(png_structp png_ptr, png_infop info_ptr,
1349 int transforms,
1350 voidp params)
1351 {
1352 int row;
1353
1354 if (png_ptr == NULL)
1355 return;
1356 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1357 /* Invert the alpha channel from opacity to transparency
1358 */
1359 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1360 png_set_invert_alpha(png_ptr);
1361 #endif
1362
1363 /* png_read_info() gives us all of the information from the
1364 * PNG file before the first IDAT (image data chunk).
1365 */
1366 png_read_info(png_ptr, info_ptr);
1367 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
1368 png_error(png_ptr, "Image is too high to process with png_read_png()");
1369
1370 /* -------------- image transformations start here ------------------- */
1371
1372 #ifdef PNG_READ_16_TO_8_SUPPORTED
1373 /* Tell libpng to strip 16 bit/color files down to 8 bits per color.
1374 */
1375 if (transforms & PNG_TRANSFORM_STRIP_16)
1376 png_set_strip_16(png_ptr);
1377 #endif
1378
1379 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1380 /* Strip alpha bytes from the input data without combining with
1381 * the background (not recommended).
1382 */
1383 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1384 png_set_strip_alpha(png_ptr);
1385 #endif
1386
1387 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1388 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1389 * byte into separate bytes (useful for paletted and grayscale images).
1390 */
1391 if (transforms & PNG_TRANSFORM_PACKING)
1392 png_set_packing(png_ptr);
1393 #endif
1394
1395 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1396 /* Change the order of packed pixels to least significant bit first
1397 * (not useful if you are using png_set_packing).
1398 */
1399 if (transforms & PNG_TRANSFORM_PACKSWAP)
1400 png_set_packswap(png_ptr);
1401 #endif
1402
1403 #ifdef PNG_READ_EXPAND_SUPPORTED
1404 /* Expand paletted colors into true RGB triplets
1405 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1406 * Expand paletted or RGB images with transparency to full alpha
1407 * channels so the data will be available as RGBA quartets.
1408 */
1409 if (transforms & PNG_TRANSFORM_EXPAND)
1410 if ((png_ptr->bit_depth < 8) ||
1411 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1412 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1413 png_set_expand(png_ptr);
1414 #endif
1415
1416 /* We don't handle background color or gamma transformation or dithering.
1417 */
1418
1419 #ifdef PNG_READ_INVERT_SUPPORTED
1420 /* Invert monochrome files to have 0 as white and 1 as black
1421 */
1422 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1423 png_set_invert_mono(png_ptr);
1424 #endif
1425
1426 #ifdef PNG_READ_SHIFT_SUPPORTED
1427 /* If you want to shift the pixel values from the range [0,255] or
1428 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1429 * colors were originally in:
1430 */
1431 if ((transforms & PNG_TRANSFORM_SHIFT)
1432 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1433 {
1434 png_color_8p sig_bit;
1435
1436 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1437 png_set_shift(png_ptr, sig_bit);
1438 }
1439 #endif
1440
1441 #ifdef PNG_READ_BGR_SUPPORTED
1442 /* Flip the RGB pixels to BGR (or RGBA to BGRA)
1443 */
1444 if (transforms & PNG_TRANSFORM_BGR)
1445 png_set_bgr(png_ptr);
1446 #endif
1447
1448 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1449 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR)
1450 */
1451 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1452 png_set_swap_alpha(png_ptr);
1453 #endif
1454
1455 #ifdef PNG_READ_SWAP_SUPPORTED
1456 /* Swap bytes of 16 bit files to least significant byte first
1457 */
1458 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1459 png_set_swap(png_ptr);
1460 #endif
1461
1462 /* Added at libpng-1.2.41 */
1463 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1464 /* Invert the alpha channel from opacity to transparency
1465 */
1466 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1467 png_set_invert_alpha(png_ptr);
1468 #endif
1469
1470 /* Added at libpng-1.2.41 */
1471 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1472 /* Expand grayscale image to RGB
1473 */
1474 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
1475 png_set_gray_to_rgb(png_ptr);
1476 #endif
1477
1478 /* We don't handle adding filler bytes */
1479
1480 /* Optional call to gamma correct and add the background to the palette
1481 * and update info structure. REQUIRED if you are expecting libpng to
1482 * update the palette for you (i.e., you selected such a transform above).
1483 */
1484 png_read_update_info(png_ptr, info_ptr);
1485
1486 /* -------------- image transformations end here ------------------- */
1487
1488 #ifdef PNG_FREE_ME_SUPPORTED
1489 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1490 #endif
1491 if (info_ptr->row_pointers == NULL)
1492 {
1493 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1494 info_ptr->height * png_sizeof(png_bytep));
1495 png_memset(info_ptr->row_pointers, 0, info_ptr->height
1496 * png_sizeof(png_bytep));
1497
1498 #ifdef PNG_FREE_ME_SUPPORTED
1499 info_ptr->free_me |= PNG_FREE_ROWS;
1500 #endif
1501
1502 for (row = 0; row < (int)info_ptr->height; row++)
1503 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1504 png_get_rowbytes(png_ptr, info_ptr));
1505 }
1506
1507 png_read_image(png_ptr, info_ptr->row_pointers);
1508 info_ptr->valid |= PNG_INFO_IDAT;
1509
1510 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1511 png_read_end(png_ptr, info_ptr);
1512
1513 transforms = transforms; /* Quiet compiler warnings */
1514 params = params;
1515
1516 }
1517 #endif /* PNG_INFO_IMAGE_SUPPORTED */
1518 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
1519 #endif /* PNG_READ_SUPPORTED */
1520