Toshihiro Shimizu 890ddd
#if _MSC_VER >= 1400
Toshihiro Shimizu 890ddd
#define _CRT_SECURE_NO_DEPRECATE 1
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
/* pngread.c - read a PNG file
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * libpng 1.2.4 - July 8, 2002
Toshihiro Shimizu 890ddd
 * For conditions of distribution and use, see copyright notice in png.h
Toshihiro Shimizu 890ddd
 * Copyright (c) 1998-2002 Glenn Randers-Pehrson
Toshihiro Shimizu 890ddd
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
Toshihiro Shimizu 890ddd
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * This file contains routines that an application calls directly to
Toshihiro Shimizu 890ddd
 * read a PNG file or stream.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define PNG_INTERNAL
Toshihiro Shimizu 890ddd
#include "png.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Create a PNG structure for reading, and allocate any memory needed. */
Toshihiro Shimizu 890ddd
png_structp PNGAPI
Toshihiro Shimizu 890ddd
png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Toshihiro Shimizu 890ddd
					   png_error_ptr error_fn, png_error_ptr warn_fn)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
Toshihiro Shimizu 890ddd
									 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Alternate create PNG structure for reading, and allocate any memory needed. */
Toshihiro Shimizu 890ddd
png_structp PNGAPI
Toshihiro Shimizu 890ddd
png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
Toshihiro Shimizu 890ddd
						 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
Toshihiro Shimizu 890ddd
						 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#endif /* PNG_USER_MEM_SUPPORTED */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_structp png_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
#ifdef USE_FAR_KEYWORD
Toshihiro Shimizu 890ddd
	jmp_buf jmpbuf;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_create_read_struct\n");
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
Toshihiro Shimizu 890ddd
											   (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	if (png_ptr == NULL)
Toshihiro Shimizu 890ddd
		return (NULL);
Toshihiro Shimizu 890ddd
	png_ptr->m_canDelete = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if !defined(PNG_1_0_X)
Toshihiro Shimizu 890ddd
#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
Toshihiro Shimizu 890ddd
	png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif /* PNG_1_0_X */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
#ifdef USE_FAR_KEYWORD
Toshihiro Shimizu 890ddd
	if (setjmp(jmpbuf))
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (setjmp(png_ptr->jmpbuf))
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->zbuf);
Toshihiro Shimizu 890ddd
		png_ptr->zbuf = NULL;
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
		png_destroy_struct_2((png_voidp)png_ptr,
Toshihiro Shimizu 890ddd
							 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		png_destroy_struct((png_voidp)png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		return (NULL);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#ifdef USE_FAR_KEYWORD
Toshihiro Shimizu 890ddd
	png_memcpy(png_ptr->jmpbuf, jmpbuf, sizeof(jmp_buf));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	i = 0;
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		if (user_png_ver[i] != png_libpng_ver[i])
Toshihiro Shimizu 890ddd
			png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Toshihiro Shimizu 890ddd
	} while (png_libpng_ver[i++]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) {
Toshihiro Shimizu 890ddd
		/* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
Toshihiro Shimizu 890ddd
      * we must recompile any applications that use any older library version.
Toshihiro Shimizu 890ddd
      * For versions after libpng 1.0, we will be compatible, so we need
Toshihiro Shimizu 890ddd
      * only check the first digit.
Toshihiro Shimizu 890ddd
      */
Toshihiro Shimizu 890ddd
		if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
Toshihiro Shimizu 890ddd
			(user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
Toshihiro Shimizu 890ddd
			(user_png_ver[0] == '0' && user_png_ver[2] < '9')) {
Toshihiro Shimizu 890ddd
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Toshihiro Shimizu 890ddd
			char msg[80];
Toshihiro Shimizu 890ddd
			if (user_png_ver) {
Toshihiro Shimizu 890ddd
				sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
Toshihiro Shimizu 890ddd
						user_png_ver);
Toshihiro Shimizu 890ddd
				png_warning(png_ptr, msg);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
Toshihiro Shimizu 890ddd
					png_libpng_ver);
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, msg);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Toshihiro Shimizu 890ddd
			png_ptr->flags = 0;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			png_error(png_ptr,
Toshihiro Shimizu 890ddd
					  "Incompatible libpng version in application and library");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* initialize zbuf - compression buffer */
Toshihiro Shimizu 890ddd
	png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Toshihiro Shimizu 890ddd
	png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
Toshihiro Shimizu 890ddd
										  (png_uint_32)png_ptr->zbuf_size);
Toshihiro Shimizu 890ddd
	png_ptr->zstream.zalloc = png_zalloc;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.zfree = png_zfree;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.opaque = (voidpf)png_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (inflateInit(&png_ptr->zstream)) {
Toshihiro Shimizu 890ddd
	case Z_OK: /* Do nothing */
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case Z_MEM_ERROR:
Toshihiro Shimizu 890ddd
	case Z_STREAM_ERROR:
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "zlib memory error");
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case Z_VERSION_ERROR:
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "zlib version error");
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "Unknown zlib error");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_ptr->zstream.next_out = png_ptr->zbuf;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
/* Applications that neglect to set up their own setjmp() and then encounter
Toshihiro Shimizu 890ddd
   a png_error() will longjmp here.  Since the jmpbuf is then meaningless we
Toshihiro Shimizu 890ddd
   abort instead of returning. */
Toshihiro Shimizu 890ddd
#ifdef USE_FAR_KEYWORD
Toshihiro Shimizu 890ddd
	if (setjmp(jmpbuf))
Toshihiro Shimizu 890ddd
		PNG_ABORT();
Toshihiro Shimizu 890ddd
	png_memcpy(png_ptr->jmpbuf, jmpbuf, sizeof(jmp_buf));
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (setjmp(png_ptr->jmpbuf))
Toshihiro Shimizu 890ddd
		PNG_ABORT();
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	return (png_ptr);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Initialize PNG structure for reading, and allocate any memory needed.
Toshihiro Shimizu 890ddd
   This interface is deprecated in favour of the png_create_read_struct(),
Toshihiro Shimizu 890ddd
   and it will eventually disappear. */
Toshihiro Shimizu 890ddd
#undef png_read_init
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_init(png_structp png_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/* We only come here via pre-1.0.7-compiled applications */
Toshihiro Shimizu 890ddd
	png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
Toshihiro Shimizu 890ddd
				png_size_t png_struct_size, png_size_t png_info_size)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
/* We only come here via pre-1.0.12-compiled applications */
Toshihiro Shimizu 890ddd
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
Toshihiro Shimizu 890ddd
	if (sizeof(png_struct) > png_struct_size || sizeof(png_info) > png_info_size) {
Toshihiro Shimizu 890ddd
		char msg[80];
Toshihiro Shimizu 890ddd
		png_ptr->warning_fn = NULL;
Toshihiro Shimizu 890ddd
		if (user_png_ver) {
Toshihiro Shimizu 890ddd
			sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
Toshihiro Shimizu 890ddd
					user_png_ver);
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, msg);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		sprintf(msg, "Application  is  running with png.c from libpng-%.20s",
Toshihiro Shimizu 890ddd
				png_libpng_ver);
Toshihiro Shimizu 890ddd
		png_warning(png_ptr, msg);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	if (sizeof(png_struct) > png_struct_size) {
Toshihiro Shimizu 890ddd
		png_ptr->error_fn = NULL;
Toshihiro Shimizu 890ddd
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Toshihiro Shimizu 890ddd
		png_ptr->flags = 0;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		png_error(png_ptr,
Toshihiro Shimizu 890ddd
				  "The png struct allocated by the application for reading is too small.");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (sizeof(png_info) > png_info_size) {
Toshihiro Shimizu 890ddd
		png_ptr->error_fn = NULL;
Toshihiro Shimizu 890ddd
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
Toshihiro Shimizu 890ddd
		png_ptr->flags = 0;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		png_error(png_ptr,
Toshihiro Shimizu 890ddd
				  "The info struct allocated by application for reading is too small.");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	png_read_init_3(&png_ptr, user_png_ver, png_struct_size);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
Toshihiro Shimizu 890ddd
				png_size_t png_struct_size)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
	jmp_buf tmp_jmp; /* to save current jump buffer */
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_structp png_ptr = *ptr_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		if (user_png_ver[i] != png_libpng_ver[i]) {
Toshihiro Shimizu 890ddd
#ifdef PNG_LEGACY_SUPPORTED
Toshihiro Shimizu 890ddd
			png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
			png_ptr->warning_fn = NULL;
Toshihiro Shimizu 890ddd
			png_warning(png_ptr,
Toshihiro Shimizu 890ddd
						"Application uses deprecated png_read_init() and should be recompiled.");
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} while (png_libpng_ver[i++]);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_init_3\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
	/* save jump buffer and error functions */
Toshihiro Shimizu 890ddd
	png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof(jmp_buf));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (sizeof(png_struct) > png_struct_size) {
Toshihiro Shimizu 890ddd
		png_destroy_struct(png_ptr);
Toshihiro Shimizu 890ddd
		*ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
Toshihiro Shimizu 890ddd
		png_ptr = *ptr_ptr;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* reset all variables to 0 */
Toshihiro Shimizu 890ddd
	png_memset(png_ptr, 0, sizeof(png_struct));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
	/* restore jump buffer */
Toshihiro Shimizu 890ddd
	png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof(jmp_buf));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* initialize zbuf - compression buffer */
Toshihiro Shimizu 890ddd
	png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Toshihiro Shimizu 890ddd
	png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
Toshihiro Shimizu 890ddd
										  (png_uint_32)png_ptr->zbuf_size);
Toshihiro Shimizu 890ddd
	png_ptr->zstream.zalloc = png_zalloc;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.zfree = png_zfree;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.opaque = (voidpf)png_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (inflateInit(&png_ptr->zstream)) {
Toshihiro Shimizu 890ddd
	case Z_OK: /* Do nothing */
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case Z_MEM_ERROR:
Toshihiro Shimizu 890ddd
	case Z_STREAM_ERROR:
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "zlib memory");
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	case Z_VERSION_ERROR:
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "zlib version");
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "Unknown zlib error");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_ptr->zstream.next_out = png_ptr->zbuf;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_set_read_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Read the information before the actual image data.  This has been
Toshihiro Shimizu 890ddd
 * changed in v0.90 to allow reading a file that already has the magic
Toshihiro Shimizu 890ddd
 * bytes read from the stream.  You can tell libpng how many bytes have
Toshihiro Shimizu 890ddd
 * been read from the beginning of the stream (up to the maximum of 8)
Toshihiro Shimizu 890ddd
 * via png_set_sig_bytes(), and we will only check the remaining bytes
Toshihiro Shimizu 890ddd
 * here.  The application can then have access to the signature bytes we
Toshihiro Shimizu 890ddd
 * read if it is determined that this isn't a valid PNG file.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_info(png_structp png_ptr, png_infop info_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_info\n");
Toshihiro Shimizu 890ddd
	/* If we haven't checked all of the PNG signature bytes, do so now. */
Toshihiro Shimizu 890ddd
	if (png_ptr->sig_bytes < 8) {
Toshihiro Shimizu 890ddd
		png_size_t num_checked = png_ptr->sig_bytes,
Toshihiro Shimizu 890ddd
				   num_to_check = 8 - num_checked;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Toshihiro Shimizu 890ddd
		png_ptr->sig_bytes = 8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) {
Toshihiro Shimizu 890ddd
			if (num_checked < 4 &&
Toshihiro Shimizu 890ddd
				png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
Toshihiro Shimizu 890ddd
				png_error(png_ptr, "Not a PNG file");
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				png_error(png_ptr, "PNG file corrupted by ASCII conversion");
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (num_checked < 3)
Toshihiro Shimizu 890ddd
			png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (;;) {
Toshihiro Shimizu 890ddd
#ifdef PNG_USE_LOCAL_ARRAYS
Toshihiro Shimizu 890ddd
		PNG_IHDR;
Toshihiro Shimizu 890ddd
		PNG_IDAT;
Toshihiro Shimizu 890ddd
		PNG_IEND;
Toshihiro Shimizu 890ddd
		PNG_PLTE;
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_bKGD_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_bKGD;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_cHRM_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_cHRM;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_gAMA_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_gAMA;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_hIST_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_hIST;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iCCP_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_iCCP;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_iTXt;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_oFFs_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_oFFs;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_pCAL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pHYs_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_pHYs;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sBIT_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sBIT;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sCAL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sPLT_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sPLT;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sRGB_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sRGB;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tEXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_tEXt;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tIME_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_tIME;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tRNS_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_tRNS;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_zTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_zTXt;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif /* PNG_GLOBAL_ARRAYS */
Toshihiro Shimizu 890ddd
		png_byte chunk_length[4];
Toshihiro Shimizu 890ddd
		png_uint_32 length;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_read_data(png_ptr, chunk_length, 4);
Toshihiro Shimizu 890ddd
		length = png_get_uint_32(chunk_length);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_reset_crc(png_ptr);
Toshihiro Shimizu 890ddd
		png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
Toshihiro Shimizu 890ddd
				   length);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (length > PNG_MAX_UINT)
Toshihiro Shimizu 890ddd
			png_error(png_ptr, "Invalid chunk length.");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* This should be a binary subdivision search or a hash for
Toshihiro Shimizu 890ddd
       * matching the chunk name rather than a linear search.
Toshihiro Shimizu 890ddd
       */
Toshihiro Shimizu 890ddd
		if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
Toshihiro Shimizu 890ddd
			png_handle_IHDR(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
Toshihiro Shimizu 890ddd
			png_handle_IEND(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Toshihiro Shimizu 890ddd
		else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) {
Toshihiro Shimizu 890ddd
			if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Toshihiro Shimizu 890ddd
				png_ptr->mode |= PNG_HAVE_IDAT;
Toshihiro Shimizu 890ddd
			png_handle_unknown(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
			if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Toshihiro Shimizu 890ddd
				png_ptr->mode |= PNG_HAVE_PLTE;
Toshihiro Shimizu 890ddd
			else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) {
Toshihiro Shimizu 890ddd
				if (!(png_ptr->mode & PNG_HAVE_IHDR))
Toshihiro Shimizu 890ddd
					png_error(png_ptr, "Missing IHDR before IDAT");
Toshihiro Shimizu 890ddd
				else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Toshihiro Shimizu 890ddd
						 !(png_ptr->mode & PNG_HAVE_PLTE))
Toshihiro Shimizu 890ddd
					png_error(png_ptr, "Missing PLTE before IDAT");
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Toshihiro Shimizu 890ddd
			png_handle_PLTE(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) {
Toshihiro Shimizu 890ddd
			if (!(png_ptr->mode & PNG_HAVE_IHDR))
Toshihiro Shimizu 890ddd
				png_error(png_ptr, "Missing IHDR before IDAT");
Toshihiro Shimizu 890ddd
			else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
Toshihiro Shimizu 890ddd
					 !(png_ptr->mode & PNG_HAVE_PLTE))
Toshihiro Shimizu 890ddd
				png_error(png_ptr, "Missing PLTE before IDAT");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			png_ptr->idat_size = length;
Toshihiro Shimizu 890ddd
			png_ptr->mode |= PNG_HAVE_IDAT;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_bKGD_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Toshihiro Shimizu 890ddd
			png_handle_bKGD(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_cHRM_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Toshihiro Shimizu 890ddd
			png_handle_cHRM(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_gAMA_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
Toshihiro Shimizu 890ddd
			png_handle_gAMA(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_hIST_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Toshihiro Shimizu 890ddd
			png_handle_hIST(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_oFFs_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Toshihiro Shimizu 890ddd
			png_handle_oFFs(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
Toshihiro Shimizu 890ddd
			png_handle_pCAL(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
Toshihiro Shimizu 890ddd
			png_handle_sCAL(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pHYs_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Toshihiro Shimizu 890ddd
			png_handle_pHYs(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sBIT_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Toshihiro Shimizu 890ddd
			png_handle_sBIT(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sRGB_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
Toshihiro Shimizu 890ddd
			png_handle_sRGB(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iCCP_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
Toshihiro Shimizu 890ddd
			png_handle_iCCP(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sPLT_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
Toshihiro Shimizu 890ddd
			png_handle_sPLT(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tEXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Toshihiro Shimizu 890ddd
			png_handle_tEXt(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tIME_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Toshihiro Shimizu 890ddd
			png_handle_tIME(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tRNS_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Toshihiro Shimizu 890ddd
			png_handle_tRNS(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_zTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Toshihiro Shimizu 890ddd
			png_handle_zTXt(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Toshihiro Shimizu 890ddd
			png_handle_iTXt(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			png_handle_unknown(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* optional call to update the users info_ptr structure */
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_update_info\n");
Toshihiro Shimizu 890ddd
	if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Toshihiro Shimizu 890ddd
		png_read_start_row(png_ptr);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		png_warning(png_ptr,
Toshihiro Shimizu 890ddd
					"Ignoring extra png_read_update_info() call; row buffer not reallocated");
Toshihiro Shimizu 890ddd
	png_read_transform_info(png_ptr, info_ptr);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Initialize palette, background, etc, after transformations
Toshihiro Shimizu 890ddd
 * are set, but before any reading takes place.  This allows
Toshihiro Shimizu 890ddd
 * the user to obtain a gamma-corrected palette, for example.
Toshihiro Shimizu 890ddd
 * If the user doesn't call this, we will do it ourselves.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_start_read_image(png_structp png_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_start_read_image\n");
Toshihiro Shimizu 890ddd
	if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Toshihiro Shimizu 890ddd
		png_read_start_row(png_ptr);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef PNG_USE_LOCAL_ARRAYS
Toshihiro Shimizu 890ddd
	PNG_IDAT;
Toshihiro Shimizu 890ddd
	const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
Toshihiro Shimizu 890ddd
	const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	int ret;
Toshihiro Shimizu 890ddd
	png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
Toshihiro Shimizu 890ddd
			   png_ptr->row_number, png_ptr->pass);
Toshihiro Shimizu 890ddd
	if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Toshihiro Shimizu 890ddd
		png_read_start_row(png_ptr);
Toshihiro Shimizu 890ddd
	if (png_ptr->row_number == 0 && png_ptr->pass == 0) {
Toshihiro Shimizu 890ddd
/* check for transforms that have been set but were defined out */
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_INVERT_MONO)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_FILLER)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_PACKSWAP)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_PACK)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_SHIFT)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_BGR)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
Toshihiro Shimizu 890ddd
		if (png_ptr->transformations & PNG_SWAP_BYTES)
Toshihiro Shimizu 890ddd
			png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_INTERLACING_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* if interlaced and we do not need a new row, combine row and return */
Toshihiro Shimizu 890ddd
	if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) {
Toshihiro Shimizu 890ddd
		switch (png_ptr->pass) {
Toshihiro Shimizu 890ddd
		case 0:
Toshihiro Shimizu 890ddd
			if (png_ptr->row_number & 0x07) {
Toshihiro Shimizu 890ddd
				if (dsp_row != NULL)
Toshihiro Shimizu 890ddd
					png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
									png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 1:
Toshihiro Shimizu 890ddd
			if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) {
Toshihiro Shimizu 890ddd
				if (dsp_row != NULL)
Toshihiro Shimizu 890ddd
					png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
									png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 2:
Toshihiro Shimizu 890ddd
			if ((png_ptr->row_number & 0x07) != 4) {
Toshihiro Shimizu 890ddd
				if (dsp_row != NULL && (png_ptr->row_number & 4))
Toshihiro Shimizu 890ddd
					png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
									png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 3:
Toshihiro Shimizu 890ddd
			if ((png_ptr->row_number & 3) || png_ptr->width < 3) {
Toshihiro Shimizu 890ddd
				if (dsp_row != NULL)
Toshihiro Shimizu 890ddd
					png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
									png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 4:
Toshihiro Shimizu 890ddd
			if ((png_ptr->row_number & 3) != 2) {
Toshihiro Shimizu 890ddd
				if (dsp_row != NULL && (png_ptr->row_number & 2))
Toshihiro Shimizu 890ddd
					png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
									png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 5:
Toshihiro Shimizu 890ddd
			if ((png_ptr->row_number & 1) || png_ptr->width < 2) {
Toshihiro Shimizu 890ddd
				if (dsp_row != NULL)
Toshihiro Shimizu 890ddd
					png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
									png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		case 6:
Toshihiro Shimizu 890ddd
			if (!(png_ptr->row_number & 1)) {
Toshihiro Shimizu 890ddd
				png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!(png_ptr->mode & PNG_HAVE_IDAT))
Toshihiro Shimizu 890ddd
		png_error(png_ptr, "Invalid attempt to read row data");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_ptr->zstream.next_out = png_ptr->row_buf;
Toshihiro Shimizu 890ddd
	png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		if (!(png_ptr->zstream.avail_in)) {
Toshihiro Shimizu 890ddd
			while (!png_ptr->idat_size) {
Toshihiro Shimizu 890ddd
				png_byte chunk_length[4];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				png_crc_finish(png_ptr, 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				png_read_data(png_ptr, chunk_length, 4);
Toshihiro Shimizu 890ddd
				png_ptr->idat_size = png_get_uint_32(chunk_length);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (png_ptr->idat_size > PNG_MAX_UINT)
Toshihiro Shimizu 890ddd
					png_error(png_ptr, "Invalid chunk length.");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				png_reset_crc(png_ptr);
Toshihiro Shimizu 890ddd
				png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Toshihiro Shimizu 890ddd
				if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Toshihiro Shimizu 890ddd
					png_error(png_ptr, "Not enough image data");
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
Toshihiro Shimizu 890ddd
			png_ptr->zstream.next_in = png_ptr->zbuf;
Toshihiro Shimizu 890ddd
			if (png_ptr->zbuf_size > png_ptr->idat_size)
Toshihiro Shimizu 890ddd
				png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Toshihiro Shimizu 890ddd
			png_crc_read(png_ptr, png_ptr->zbuf,
Toshihiro Shimizu 890ddd
						 (png_size_t)png_ptr->zstream.avail_in);
Toshihiro Shimizu 890ddd
			png_ptr->idat_size -= png_ptr->zstream.avail_in;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Toshihiro Shimizu 890ddd
		if (ret == Z_STREAM_END) {
Toshihiro Shimizu 890ddd
			if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Toshihiro Shimizu 890ddd
				png_ptr->idat_size)
Toshihiro Shimizu 890ddd
				png_error(png_ptr, "Extra compressed data");
Toshihiro Shimizu 890ddd
			png_ptr->mode |= PNG_AFTER_IDAT;
Toshihiro Shimizu 890ddd
			png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (ret != Z_OK)
Toshihiro Shimizu 890ddd
			png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg : "Decompression error");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	} while (png_ptr->zstream.avail_out);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_ptr->row_info.color_type = png_ptr->color_type;
Toshihiro Shimizu 890ddd
	png_ptr->row_info.width = png_ptr->iwidth;
Toshihiro Shimizu 890ddd
	png_ptr->row_info.channels = png_ptr->channels;
Toshihiro Shimizu 890ddd
	png_ptr->row_info.bit_depth = png_ptr->bit_depth;
Toshihiro Shimizu 890ddd
	png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Toshihiro Shimizu 890ddd
	png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
Toshihiro Shimizu 890ddd
									   (png_uint_32)png_ptr->row_info.pixel_depth +
Toshihiro Shimizu 890ddd
								   7) >>
Toshihiro Shimizu 890ddd
								  3);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (png_ptr->row_buf[0])
Toshihiro Shimizu 890ddd
		png_read_filter_row(png_ptr, &(png_ptr->row_info),
Toshihiro Shimizu 890ddd
							png_ptr->row_buf + 1, png_ptr->prev_row + 1,
Toshihiro Shimizu 890ddd
							(int)(png_ptr->row_buf[0]));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Toshihiro Shimizu 890ddd
					 png_ptr->rowbytes + 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_MNG_FEATURES_SUPPORTED)
Toshihiro Shimizu 890ddd
	if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
Toshihiro Shimizu 890ddd
		(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) {
Toshihiro Shimizu 890ddd
		/* Intrapixel differencing */
Toshihiro Shimizu 890ddd
		png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (png_ptr->transformations)
Toshihiro Shimizu 890ddd
		png_do_read_transformations(png_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_INTERLACING_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* blow up interlaced rows to full size */
Toshihiro Shimizu 890ddd
	if (png_ptr->interlaced &&
Toshihiro Shimizu 890ddd
		(png_ptr->transformations & PNG_INTERLACE)) {
Toshihiro Shimizu 890ddd
		if (png_ptr->pass < 6)
Toshihiro Shimizu 890ddd
			/*       old interface (pre-1.0.9):
Toshihiro Shimizu 890ddd
         png_do_read_interlace(&(png_ptr->row_info),
Toshihiro Shimizu 890ddd
            png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
			png_do_read_interlace(png_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (dsp_row != NULL)
Toshihiro Shimizu 890ddd
			png_combine_row(png_ptr, dsp_row,
Toshihiro Shimizu 890ddd
							png_pass_dsp_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
		if (row != NULL)
Toshihiro Shimizu 890ddd
			png_combine_row(png_ptr, row,
Toshihiro Shimizu 890ddd
							png_pass_mask[png_ptr->pass]);
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (row != NULL)
Toshihiro Shimizu 890ddd
			png_combine_row(png_ptr, row, 0xff);
Toshihiro Shimizu 890ddd
		if (dsp_row != NULL)
Toshihiro Shimizu 890ddd
			png_combine_row(png_ptr, dsp_row, 0xff);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	png_read_finish_row(png_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (png_ptr->read_row_fn != NULL)
Toshihiro Shimizu 890ddd
		(*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Read one or more rows of image data.  If the image is interlaced,
Toshihiro Shimizu 890ddd
 * and png_set_interlace_handling() has been called, the rows need to
Toshihiro Shimizu 890ddd
 * contain the contents of the rows from the previous pass.  If the
Toshihiro Shimizu 890ddd
 * image has alpha or transparency, and png_handle_alpha()[*] has been
Toshihiro Shimizu 890ddd
 * called, the rows contents must be initialized to the contents of the
Toshihiro Shimizu 890ddd
 * screen.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * "row" holds the actual image, and pixels are placed in it
Toshihiro Shimizu 890ddd
 * as they arrive.  If the image is displayed after each pass, it will
Toshihiro Shimizu 890ddd
 * appear to "sparkle" in.  "display_row" can be used to display a
Toshihiro Shimizu 890ddd
 * "chunky" progressive image, with finer detail added as it becomes
Toshihiro Shimizu 890ddd
 * available.  If you do not want this "chunky" display, you may pass
Toshihiro Shimizu 890ddd
 * NULL for display_row.  If you do not want the sparkle display, and
Toshihiro Shimizu 890ddd
 * you have not called png_handle_alpha(), you may pass NULL for rows.
Toshihiro Shimizu 890ddd
 * If you have called png_handle_alpha(), and the image has either an
Toshihiro Shimizu 890ddd
 * alpha channel or a transparency chunk, you must provide a buffer for
Toshihiro Shimizu 890ddd
 * rows.  In this case, you do not have to provide a display_row buffer
Toshihiro Shimizu 890ddd
 * also, but you may.  If the image is not interlaced, or if you have
Toshihiro Shimizu 890ddd
 * not called png_set_interlace_handling(), the display_row buffer will
Toshihiro Shimizu 890ddd
 * be ignored, so pass NULL to it.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.4
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_rows(png_structp png_ptr, png_bytepp row,
Toshihiro Shimizu 890ddd
			  png_bytepp display_row, png_uint_32 num_rows)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_uint_32 i;
Toshihiro Shimizu 890ddd
	png_bytepp rp;
Toshihiro Shimizu 890ddd
	png_bytepp dp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_rows\n");
Toshihiro Shimizu 890ddd
	rp = row;
Toshihiro Shimizu 890ddd
	dp = display_row;
Toshihiro Shimizu 890ddd
	if (rp != NULL && dp != NULL)
Toshihiro Shimizu 890ddd
		for (i = 0; i < num_rows; i++) {
Toshihiro Shimizu 890ddd
			png_bytep rptr = *rp++;
Toshihiro Shimizu 890ddd
			png_bytep dptr = *dp++;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			png_read_row(png_ptr, rptr, dptr);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else if (rp != NULL)
Toshihiro Shimizu 890ddd
		for (i = 0; i < num_rows; i++) {
Toshihiro Shimizu 890ddd
			png_bytep rptr = *rp;
Toshihiro Shimizu 890ddd
			png_read_row(png_ptr, rptr, png_bytep_NULL);
Toshihiro Shimizu 890ddd
			rp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else if (dp != NULL)
Toshihiro Shimizu 890ddd
		for (i = 0; i < num_rows; i++) {
Toshihiro Shimizu 890ddd
			png_bytep dptr = *dp;
Toshihiro Shimizu 890ddd
			png_read_row(png_ptr, png_bytep_NULL, dptr);
Toshihiro Shimizu 890ddd
			dp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Read the entire image.  If the image has an alpha channel or a tRNS
Toshihiro Shimizu 890ddd
 * chunk, and you have called png_handle_alpha()[*], you will need to
Toshihiro Shimizu 890ddd
 * initialize the image to the current image that PNG will be overlaying.
Toshihiro Shimizu 890ddd
 * We set the num_rows again here, in case it was incorrectly set in
Toshihiro Shimizu 890ddd
 * png_read_start_row() by a call to png_read_update_info() or
Toshihiro Shimizu 890ddd
 * png_start_read_image() if png_set_interlace_handling() wasn't called
Toshihiro Shimizu 890ddd
 * prior to either of these functions like it should have been.  You can
Toshihiro Shimizu 890ddd
 * only call this function once.  If you desire to have an image for
Toshihiro Shimizu 890ddd
 * each pass of a interlaced image, use png_read_rows() instead.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.2.4
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_image(png_structp png_ptr, png_bytepp image)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_uint_32 i, image_height;
Toshihiro Shimizu 890ddd
	int pass, j;
Toshihiro Shimizu 890ddd
	png_bytepp rp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_image\n");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_READ_INTERLACING_SUPPORTED
Toshihiro Shimizu 890ddd
	pass = png_set_interlace_handling(png_ptr);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (png_ptr->interlaced)
Toshihiro Shimizu 890ddd
		png_error(png_ptr,
Toshihiro Shimizu 890ddd
				  "Cannot read interlaced image -- interlace handler disabled.");
Toshihiro Shimizu 890ddd
	pass = 1;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	image_height = png_ptr->height;
Toshihiro Shimizu 890ddd
	png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (j = 0; j < pass; j++) {
Toshihiro Shimizu 890ddd
		rp = image;
Toshihiro Shimizu 890ddd
		for (i = 0; i < image_height; i++) {
Toshihiro Shimizu 890ddd
			png_read_row(png_ptr, *rp, png_bytep_NULL);
Toshihiro Shimizu 890ddd
			rp++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Read the end of the PNG file.  Will not read past the end of the
Toshihiro Shimizu 890ddd
 * file, will verify the end is accurate, and will read any comments
Toshihiro Shimizu 890ddd
 * or time information at the end of the file, if info is not NULL.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_end(png_structp png_ptr, png_infop info_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_byte chunk_length[4];
Toshihiro Shimizu 890ddd
	png_uint_32 length;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_end\n");
Toshihiro Shimizu 890ddd
	if (png_crc_finish(png_ptr, 0) != 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	/* Finish off CRC from last IDAT chunk */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
#ifdef PNG_USE_LOCAL_ARRAYS
Toshihiro Shimizu 890ddd
		PNG_IHDR;
Toshihiro Shimizu 890ddd
		PNG_IDAT;
Toshihiro Shimizu 890ddd
		PNG_IEND;
Toshihiro Shimizu 890ddd
		PNG_PLTE;
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_bKGD_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_bKGD;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_cHRM_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_cHRM;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_gAMA_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_gAMA;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_hIST_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_hIST;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iCCP_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_iCCP;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_iTXt;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_oFFs_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_oFFs;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_pCAL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pHYs_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_pHYs;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sBIT_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sBIT;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sCAL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sPLT_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sPLT;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sRGB_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_sRGB;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tEXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_tEXt;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tIME_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_tIME;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tRNS_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_tRNS;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_zTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		PNG_zTXt;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif /* PNG_GLOBAL_ARRAYS */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_read_data(png_ptr, chunk_length, 4);
Toshihiro Shimizu 890ddd
		length = png_get_uint_32(chunk_length);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_reset_crc(png_ptr);
Toshihiro Shimizu 890ddd
		png_crc_read(png_ptr, png_ptr->chunk_name, 4);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (length > PNG_MAX_UINT)
Toshihiro Shimizu 890ddd
			png_error(png_ptr, "Invalid chunk length.");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
Toshihiro Shimizu 890ddd
			png_handle_IHDR(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
Toshihiro Shimizu 890ddd
			png_handle_IEND(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
Toshihiro Shimizu 890ddd
		else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) {
Toshihiro Shimizu 890ddd
			if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) {
Toshihiro Shimizu 890ddd
				if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
Toshihiro Shimizu 890ddd
					png_error(png_ptr, "Too many IDAT's found");
Toshihiro Shimizu 890ddd
			} else
Toshihiro Shimizu 890ddd
				png_ptr->mode |= PNG_AFTER_IDAT;
Toshihiro Shimizu 890ddd
			png_handle_unknown(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
			if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Toshihiro Shimizu 890ddd
				png_ptr->mode |= PNG_HAVE_PLTE;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) {
Toshihiro Shimizu 890ddd
			/* Zero length IDATs are legal after the last IDAT has been
Toshihiro Shimizu 890ddd
          * read, but not after other chunks have been read.
Toshihiro Shimizu 890ddd
          */
Toshihiro Shimizu 890ddd
			if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
Toshihiro Shimizu 890ddd
				png_error(png_ptr, "Too many IDAT's found");
Toshihiro Shimizu 890ddd
			png_crc_finish(png_ptr, length);
Toshihiro Shimizu 890ddd
		} else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
Toshihiro Shimizu 890ddd
			png_handle_PLTE(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_bKGD_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
Toshihiro Shimizu 890ddd
			png_handle_bKGD(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_cHRM_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
Toshihiro Shimizu 890ddd
			png_handle_cHRM(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_gAMA_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
Toshihiro Shimizu 890ddd
			png_handle_gAMA(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_hIST_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
Toshihiro Shimizu 890ddd
			png_handle_hIST(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_oFFs_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
Toshihiro Shimizu 890ddd
			png_handle_oFFs(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
Toshihiro Shimizu 890ddd
			png_handle_pCAL(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sCAL_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
Toshihiro Shimizu 890ddd
			png_handle_sCAL(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_pHYs_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
Toshihiro Shimizu 890ddd
			png_handle_pHYs(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sBIT_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
Toshihiro Shimizu 890ddd
			png_handle_sBIT(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sRGB_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
Toshihiro Shimizu 890ddd
			png_handle_sRGB(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iCCP_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
Toshihiro Shimizu 890ddd
			png_handle_iCCP(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_sPLT_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
Toshihiro Shimizu 890ddd
			png_handle_sPLT(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tEXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
Toshihiro Shimizu 890ddd
			png_handle_tEXt(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tIME_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
Toshihiro Shimizu 890ddd
			png_handle_tIME(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_tRNS_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
Toshihiro Shimizu 890ddd
			png_handle_tRNS(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_zTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
Toshihiro Shimizu 890ddd
			png_handle_zTXt(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_iTXt_SUPPORTED)
Toshihiro Shimizu 890ddd
		else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
Toshihiro Shimizu 890ddd
			png_handle_iTXt(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			png_handle_unknown(png_ptr, info_ptr, length);
Toshihiro Shimizu 890ddd
	} while (!(png_ptr->mode & PNG_HAVE_IEND));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* free all memory used by the read */
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Toshihiro Shimizu 890ddd
						png_infopp end_info_ptr_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_structp png_ptr = NULL;
Toshihiro Shimizu 890ddd
	png_infop info_ptr = NULL, end_info_ptr = NULL;
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	png_free_ptr free_fn = NULL;
Toshihiro Shimizu 890ddd
	png_voidp mem_ptr = NULL;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_destroy_read_struct\n");
Toshihiro Shimizu 890ddd
	if (png_ptr_ptr != NULL)
Toshihiro Shimizu 890ddd
		png_ptr = *png_ptr_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (info_ptr_ptr != NULL)
Toshihiro Shimizu 890ddd
		info_ptr = *info_ptr_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (end_info_ptr_ptr != NULL)
Toshihiro Shimizu 890ddd
		end_info_ptr = *end_info_ptr_ptr;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	free_fn = png_ptr->free_fn;
Toshihiro Shimizu 890ddd
	mem_ptr = png_ptr->mem_ptr;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (info_ptr != NULL) {
Toshihiro Shimizu 890ddd
#if defined(PNG_TEXT_SUPPORTED)
Toshihiro Shimizu 890ddd
		png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
		png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
Toshihiro Shimizu 890ddd
							 (png_voidp)mem_ptr);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		png_destroy_struct((png_voidp)info_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		*info_ptr_ptr = NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (end_info_ptr != NULL) {
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_TEXT_SUPPORTED)
Toshihiro Shimizu 890ddd
		png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
		png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
Toshihiro Shimizu 890ddd
							 (png_voidp)mem_ptr);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		png_destroy_struct((png_voidp)end_info_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		*end_info_ptr_ptr = NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (png_ptr != NULL) {
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
		png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
Toshihiro Shimizu 890ddd
							 (png_voidp)mem_ptr);
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
		png_destroy_struct((png_voidp)png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		*png_ptr_ptr = NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* free all memory used by the read (old method) */
Toshihiro Shimizu 890ddd
void /* PRIVATE */
Toshihiro Shimizu 890ddd
	png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
	jmp_buf tmp_jmp;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	png_error_ptr error_fn;
Toshihiro Shimizu 890ddd
	png_error_ptr warning_fn;
Toshihiro Shimizu 890ddd
	png_voidp error_ptr;
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	png_free_ptr free_fn;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_debug(1, "in png_read_destroy\n");
Toshihiro Shimizu 890ddd
	if (info_ptr != NULL)
Toshihiro Shimizu 890ddd
		png_info_destroy(png_ptr, info_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (end_info_ptr != NULL)
Toshihiro Shimizu 890ddd
		png_info_destroy(png_ptr, end_info_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->zbuf);
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->big_row_buf);
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->prev_row);
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_DITHER_SUPPORTED)
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->palette_lookup);
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->dither_index);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_GAMMA_SUPPORTED)
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->gamma_table);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->gamma_from_1);
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->gamma_to_1);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#ifdef PNG_FREE_ME_SUPPORTED
Toshihiro Shimizu 890ddd
	if (png_ptr->free_me & PNG_FREE_PLTE)
Toshihiro Shimizu 890ddd
		png_zfree(png_ptr, png_ptr->palette);
Toshihiro Shimizu 890ddd
	png_ptr->free_me &= ~PNG_FREE_PLTE;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
Toshihiro Shimizu 890ddd
		png_zfree(png_ptr, png_ptr->palette);
Toshihiro Shimizu 890ddd
	png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_tRNS_SUPPORTED) || \
Toshihiro Shimizu 890ddd
	defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Toshihiro Shimizu 890ddd
#ifdef PNG_FREE_ME_SUPPORTED
Toshihiro Shimizu 890ddd
	if (png_ptr->free_me & PNG_FREE_TRNS)
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->trans);
Toshihiro Shimizu 890ddd
	png_ptr->free_me &= ~PNG_FREE_TRNS;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->trans);
Toshihiro Shimizu 890ddd
	png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_hIST_SUPPORTED)
Toshihiro Shimizu 890ddd
#ifdef PNG_FREE_ME_SUPPORTED
Toshihiro Shimizu 890ddd
	if (png_ptr->free_me & PNG_FREE_HIST)
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->hist);
Toshihiro Shimizu 890ddd
	png_ptr->free_me &= ~PNG_FREE_HIST;
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
	if (png_ptr->flags & PNG_FLAG_FREE_HIST)
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->hist);
Toshihiro Shimizu 890ddd
	png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_GAMMA_SUPPORTED)
Toshihiro Shimizu 890ddd
	if (png_ptr->gamma_16_table != NULL) {
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		int istop = (1 << (8 - png_ptr->gamma_shift));
Toshihiro Shimizu 890ddd
		for (i = 0; i < istop; i++) {
Toshihiro Shimizu 890ddd
			png_free(png_ptr, png_ptr->gamma_16_table[i]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->gamma_16_table);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Toshihiro Shimizu 890ddd
	if (png_ptr->gamma_16_from_1 != NULL) {
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		int istop = (1 << (8 - png_ptr->gamma_shift));
Toshihiro Shimizu 890ddd
		for (i = 0; i < istop; i++) {
Toshihiro Shimizu 890ddd
			png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->gamma_16_from_1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (png_ptr->gamma_16_to_1 != NULL) {
Toshihiro Shimizu 890ddd
		int i;
Toshihiro Shimizu 890ddd
		int istop = (1 << (8 - png_ptr->gamma_shift));
Toshihiro Shimizu 890ddd
		for (i = 0; i < istop; i++) {
Toshihiro Shimizu 890ddd
			png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		png_free(png_ptr, png_ptr->gamma_16_to_1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#if defined(PNG_TIME_RFC1123_SUPPORTED)
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->time_buffer);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inflateEnd(&png_ptr->zstream);
Toshihiro Shimizu 890ddd
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->save_buffer);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Toshihiro Shimizu 890ddd
#ifdef PNG_TEXT_SUPPORTED
Toshihiro Shimizu 890ddd
	png_free(png_ptr, png_ptr->current_text);
Toshihiro Shimizu 890ddd
#endif /* PNG_TEXT_SUPPORTED */
Toshihiro Shimizu 890ddd
#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Save the important info out of the png_struct, in case it is
Toshihiro Shimizu 890ddd
    * being used again.
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
	png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof(jmp_buf));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	error_fn = png_ptr->error_fn;
Toshihiro Shimizu 890ddd
	warning_fn = png_ptr->warning_fn;
Toshihiro Shimizu 890ddd
	error_ptr = png_ptr->error_ptr;
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	free_fn = png_ptr->free_fn;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_memset(png_ptr, 0, sizeof(png_struct));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_ptr->error_fn = error_fn;
Toshihiro Shimizu 890ddd
	png_ptr->warning_fn = warning_fn;
Toshihiro Shimizu 890ddd
	png_ptr->error_ptr = error_ptr;
Toshihiro Shimizu 890ddd
#ifdef PNG_USER_MEM_SUPPORTED
Toshihiro Shimizu 890ddd
	png_ptr->free_fn = free_fn;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_SETJMP_SUPPORTED
Toshihiro Shimizu 890ddd
	png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof(jmp_buf));
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	png_ptr->read_row_fn = read_row_fn;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_INFO_IMAGE_SUPPORTED)
Toshihiro Shimizu 890ddd
void PNGAPI
Toshihiro Shimizu 890ddd
png_read_png(png_structp png_ptr, png_infop info_ptr,
Toshihiro Shimizu 890ddd
			 int transforms,
Toshihiro Shimizu 890ddd
			 voidp params)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int row;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* invert the alpha channel from opacity to transparency */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Toshihiro Shimizu 890ddd
		png_set_invert_alpha(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* The call to png_read_info() gives us all of the information from the
Toshihiro Shimizu 890ddd
    * PNG file before the first IDAT (image data chunk).
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	png_read_info(png_ptr, info_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* -------------- image transformations start here ------------------- */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_16_TO_8_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* tell libpng to strip 16 bit/color files down to 8 bits/color */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_STRIP_16)
Toshihiro Shimizu 890ddd
		png_set_strip_16(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* Strip alpha bytes from the input data without combining with the
Toshihiro Shimizu 890ddd
    * background (not recommended).
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
Toshihiro Shimizu 890ddd
		png_set_strip_alpha(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
Toshihiro Shimizu 890ddd
    * byte into separate bytes (useful for paletted and grayscale images).
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_PACKING)
Toshihiro Shimizu 890ddd
		png_set_packing(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* Change the order of packed pixels to least significant bit first
Toshihiro Shimizu 890ddd
    * (not useful if you are using png_set_packing). */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_PACKSWAP)
Toshihiro Shimizu 890ddd
		png_set_packswap(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_EXPAND_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* Expand paletted colors into true RGB triplets
Toshihiro Shimizu 890ddd
    * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
Toshihiro Shimizu 890ddd
    * Expand paletted or RGB images with transparency to full alpha
Toshihiro Shimizu 890ddd
    * channels so the data will be available as RGBA quartets.
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_EXPAND)
Toshihiro Shimizu 890ddd
		if ((png_ptr->bit_depth < 8) ||
Toshihiro Shimizu 890ddd
			(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
Toshihiro Shimizu 890ddd
			(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
Toshihiro Shimizu 890ddd
			png_set_expand(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* We don't handle background color or gamma transformation or dithering. */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_INVERT_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* invert monochrome files to have 0 as white and 1 as black */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_INVERT_MONO)
Toshihiro Shimizu 890ddd
		png_set_invert_mono(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_SHIFT_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* If you want to shift the pixel values from the range [0,255] or
Toshihiro Shimizu 890ddd
    * [0,65535] to the original [0,7] or [0,31], or whatever range the
Toshihiro Shimizu 890ddd
    * colors were originally in:
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	if ((transforms & PNG_TRANSFORM_SHIFT) && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) {
Toshihiro Shimizu 890ddd
		png_color_8p sig_bit;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		png_get_sBIT(png_ptr, info_ptr, &sig_bit);
Toshihiro Shimizu 890ddd
		png_set_shift(png_ptr, sig_bit);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_BGR_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* flip the RGB pixels to BGR (or RGBA to BGRA) */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_BGR)
Toshihiro Shimizu 890ddd
		png_set_bgr(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Toshihiro Shimizu 890ddd
		png_set_swap_alpha(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#if defined(PNG_READ_SWAP_SUPPORTED)
Toshihiro Shimizu 890ddd
	/* swap bytes of 16 bit files to least significant byte first */
Toshihiro Shimizu 890ddd
	if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Toshihiro Shimizu 890ddd
		png_set_swap(png_ptr);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* We don't handle adding filler bytes */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* Optional call to gamma correct and add the background to the palette
Toshihiro Shimizu 890ddd
    * and update info structure.  REQUIRED if you are expecting libpng to
Toshihiro Shimizu 890ddd
    * update the palette for you (i.e., you selected such a transform above).
Toshihiro Shimizu 890ddd
    */
Toshihiro Shimizu 890ddd
	png_read_update_info(png_ptr, info_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* -------------- image transformations end here ------------------- */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef PNG_FREE_ME_SUPPORTED
Toshihiro Shimizu 890ddd
	png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
	if (info_ptr->row_pointers == NULL) {
Toshihiro Shimizu 890ddd
		info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Toshihiro Shimizu 890ddd
														info_ptr->height * sizeof(png_bytep));
Toshihiro Shimizu 890ddd
#ifdef PNG_FREE_ME_SUPPORTED
Toshihiro Shimizu 890ddd
		info_ptr->free_me |= PNG_FREE_ROWS;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
		for (row = 0; row < (int)info_ptr->height; row++) {
Toshihiro Shimizu 890ddd
			info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
Toshihiro Shimizu 890ddd
																png_get_rowbytes(png_ptr, info_ptr));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	png_read_image(png_ptr, info_ptr->row_pointers);
Toshihiro Shimizu 890ddd
	info_ptr->valid |= PNG_INFO_IDAT;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* read rest of file, and get additional chunks in info_ptr - REQUIRED */
Toshihiro Shimizu 890ddd
	png_read_end(png_ptr, info_ptr);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (transforms == 0 || params == NULL)
Toshihiro Shimizu 890ddd
		/* quiet compiler warnings */ return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif