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