📄 pngread.c
字号:
/* pngread.c - read a PNG file * * libpng 1.0.12 - June 8, 2001 * For conditions of distribution and use, see copyright notice in png.h * Copyright (c) 1998-2001 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * * This file contains routines that an application calls directly to * read a PNG file or stream. */#define PNG_INTERNAL#include "png.h"/* Create a PNG structure for reading, and allocate any memory needed. */png_structp PNGAPIpng_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn){#ifdef PNG_USER_MEM_SUPPORTED return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn, warn_fn, NULL, NULL, NULL));}/* Alternate create PNG structure for reading, and allocate any memory needed. */png_structp PNGAPIpng_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn){#endif /* PNG_USER_MEM_SUPPORTED */ png_structp png_ptr;#ifdef PNG_SETJMP_SUPPORTED#ifdef USE_FAR_KEYWORD jmp_buf jmpbuf;#endif#endif int i; png_debug(1, "in png_create_read_struct\n");#ifdef PNG_USER_MEM_SUPPORTED if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG, (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr)) == NULL)#else if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)#endif { return (png_structp)NULL; }#ifdef PNG_SETJMP_SUPPORTED#ifdef USE_FAR_KEYWORD if (setjmp(jmpbuf))#else if (setjmp(png_ptr->jmpbuf))#endif { png_free(png_ptr, png_ptr->zbuf); png_ptr->zbuf=NULL; png_destroy_struct(png_ptr); return (png_structp)NULL; }#ifdef USE_FAR_KEYWORD png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));#endif#endif#ifdef PNG_USER_MEM_SUPPORTED png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);#endif png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn); i=0; do { if(user_png_ver[i] != png_libpng_ver[i]) png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; } while (png_libpng_ver[i++]); if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) { /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so * we must recompile any applications that use any older library version. * For versions after libpng 1.0, we will be compatible, so we need * only check the first digit. */ if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || (user_png_ver[0] == '0' && user_png_ver[2] < '9')) {#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) char msg[80]; if (user_png_ver) { sprintf(msg, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } sprintf(msg, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg);#endif#ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags=0;#endif png_error(png_ptr, "Incompatible libpng version in application and library"); } /* Libpng 1.0.6 was not binary compatible, due to insertion of the info_ptr->free_me member. Libpng-1.0.1 and earlier were not compatible due to insertion of the user transform function. Note to maintainer: this test can be removed from version 1.2.0 and beyond because the previous test would have already rejected it. */ if (user_png_ver[0] == '1' && user_png_ver[2] == '0' && (user_png_ver[4] < '2' || user_png_ver[4] == '6') && user_png_ver[5] == '\0') {#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) char msg[80]; if (user_png_ver) { sprintf(msg, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } sprintf(msg, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg);#endif#ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags=0;#endif png_error(png_ptr, "Application must be recompiled; versions <= 1.0.6 were incompatible"); } } /* initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_ptr->zstream.zalloc = png_zalloc; png_ptr->zstream.zfree = png_zfree; png_ptr->zstream.opaque = (voidpf)png_ptr; switch (inflateInit(&png_ptr->zstream)) { case Z_OK: /* Do nothing */ break; case Z_MEM_ERROR: case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break; case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break; default: png_error(png_ptr, "Unknown zlib error"); } png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_set_read_fn(png_ptr, NULL, NULL); return (png_ptr);}/* Initialize PNG structure for reading, and allocate any memory needed. This interface is deprecated in favour of the png_create_read_struct(), and it will eventually disappear. */#undef png_read_initvoid PNGAPIpng_read_init(png_structp png_ptr){ /* We only come here via pre-1.0.7-compiled applications */ png_read_init_2(png_ptr, "1.0.6 or earlier", 0, 0);}#undef png_read_init_2void PNGAPIpng_read_init_2(png_structp png_ptr, png_const_charp user_png_ver, png_size_t png_struct_size, png_size_t png_info_size){ /* We only come here via pre-1.0.12-compiled applications */#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) if(sizeof(png_struct) > png_struct_size || sizeof(png_info) > png_info_size) { char msg[80]; png_ptr->warning_fn=(png_error_ptr)NULL; if (user_png_ver) { sprintf(msg, "Application was compiled with png.h from libpng-%.20s", user_png_ver); png_warning(png_ptr, msg); } sprintf(msg, "Application is running with png.c from libpng-%.20s", png_libpng_ver); png_warning(png_ptr, msg); }#endif if(sizeof(png_struct) > png_struct_size) { png_ptr->error_fn=(png_error_ptr)NULL;#ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags=0;#endif png_error(png_ptr, "The png struct allocated by the application for reading is too small."); } if(sizeof(png_info) > png_info_size) { png_ptr->error_fn=(png_error_ptr)NULL;#ifdef PNG_ERROR_NUMBERS_SUPPORTED png_ptr->flags=0;#endif png_error(png_ptr, "The info struct allocated by application for reading is too small."); } png_read_init_3(&png_ptr, user_png_ver, png_struct_size);}void PNGAPIpng_read_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver, png_size_t png_struct_size){#ifdef PNG_SETJMP_SUPPORTED jmp_buf tmp_jmp; /* to save current jump buffer */#endif int i=0; png_structp png_ptr=*ptr_ptr; do { if(user_png_ver[i] != png_libpng_ver[i]) {#ifdef PNG_LEGACY_SUPPORTED png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;#else png_ptr->warning_fn=(png_error_ptr)NULL; png_warning(png_ptr, "Application uses deprecated png_read_init() and should be recompiled."); break;#endif } } while (png_libpng_ver[i++]); png_debug(1, "in png_read_init_3\n");#ifdef PNG_SETJMP_SUPPORTED /* save jump buffer and error functions */ png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));#endif if(sizeof(png_struct) > png_struct_size) { png_destroy_struct(png_ptr); *ptr_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG); png_ptr = *ptr_ptr; } /* reset all variables to 0 */ png_memset(png_ptr, 0, sizeof (png_struct));#ifdef PNG_SETJMP_SUPPORTED /* restore jump buffer */ png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));#endif /* initialize zbuf - compression buffer */ png_ptr->zbuf_size = PNG_ZBUF_SIZE; png_ptr->zbuf = (png_bytep)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size); png_ptr->zstream.zalloc = png_zalloc; png_ptr->zstream.zfree = png_zfree; png_ptr->zstream.opaque = (voidpf)png_ptr; switch (inflateInit(&png_ptr->zstream)) { case Z_OK: /* Do nothing */ break; case Z_MEM_ERROR: case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break; case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break; default: png_error(png_ptr, "Unknown zlib error"); } png_ptr->zstream.next_out = png_ptr->zbuf; png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; png_set_read_fn(png_ptr, NULL, NULL);}/* Read the information before the actual image data. This has been * changed in v0.90 to allow reading a file that already has the magic * bytes read from the stream. You can tell libpng how many bytes have * been read from the beginning of the stream (up to the maximum of 8) * via png_set_sig_bytes(), and we will only check the remaining bytes * here. The application can then have access to the signature bytes we * read if it is determined that this isn't a valid PNG file. */void PNGAPIpng_read_info(png_structp png_ptr, png_infop info_ptr){ png_debug(1, "in png_read_info\n"); /* save jump buffer and error functions */ /* If we haven't checked all of the PNG signature bytes, do so now. */ if (png_ptr->sig_bytes < 8) { png_size_t num_checked = png_ptr->sig_bytes, num_to_check = 8 - num_checked; png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); png_ptr->sig_bytes = 8; if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) { if (num_checked < 4 && png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) png_error(png_ptr, "Not a PNG file"); else png_error(png_ptr, "PNG file corrupted by ASCII conversion"); } if (num_checked < 3) png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; } for(;;) {#ifdef PNG_USE_LOCAL_ARRAYS PNG_IHDR; PNG_IDAT; PNG_IEND; PNG_PLTE;#if defined(PNG_READ_bKGD_SUPPORTED) PNG_bKGD;#endif#if defined(PNG_READ_cHRM_SUPPORTED) PNG_cHRM;#endif#if defined(PNG_READ_gAMA_SUPPORTED) PNG_gAMA;#endif#if defined(PNG_READ_hIST_SUPPORTED) PNG_hIST;#endif#if defined(PNG_READ_iCCP_SUPPORTED) PNG_iCCP;#endif#if defined(PNG_READ_iTXt_SUPPORTED) PNG_iTXt;#endif#if defined(PNG_READ_oFFs_SUPPORTED) PNG_oFFs;#endif#if defined(PNG_READ_pCAL_SUPPORTED) PNG_pCAL;#endif#if defined(PNG_READ_pHYs_SUPPORTED) PNG_pHYs;#endif#if defined(PNG_READ_sBIT_SUPPORTED) PNG_sBIT;#endif#if defined(PNG_READ_sCAL_SUPPORTED) PNG_sCAL;#endif#if defined(PNG_READ_sPLT_SUPPORTED) PNG_sPLT;#endif#if defined(PNG_READ_sRGB_SUPPORTED) PNG_sRGB;#endif#if defined(PNG_READ_tEXt_SUPPORTED) PNG_tEXt;#endif#if defined(PNG_READ_tIME_SUPPORTED) PNG_tIME;#endif#if defined(PNG_READ_tRNS_SUPPORTED) PNG_tRNS;#endif#if defined(PNG_READ_zTXt_SUPPORTED) PNG_zTXt;#endif#endif /* PNG_GLOBAL_ARRAYS */ png_byte chunk_length[4]; png_uint_32 length; png_read_data(png_ptr, chunk_length, 4); length = png_get_uint_32(chunk_length); png_reset_crc(png_ptr); png_crc_read(png_ptr, png_ptr->chunk_name, 4); png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name, length); /* This should be a binary subdivision search or a hash for * matching the chunk name rather than a linear search. */ if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) png_handle_IHDR(png_ptr, info_ptr, length); else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) png_handle_IEND(png_ptr, info_ptr, length);#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name)) { if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) png_ptr->mode |= PNG_HAVE_IDAT; png_handle_unknown(png_ptr, info_ptr, length); if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) png_ptr->mode |= PNG_HAVE_PLTE; else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) png_warning(png_ptr, "Missing PLTE before IDAT"); break; } }#endif else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) png_handle_PLTE(png_ptr, info_ptr, length); else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) { if (!(png_ptr->mode & PNG_HAVE_IHDR)) png_error(png_ptr, "Missing IHDR before IDAT"); else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && !(png_ptr->mode & PNG_HAVE_PLTE)) png_warning(png_ptr, "Missing PLTE before IDAT"); png_ptr->idat_size = length; png_ptr->mode |= PNG_HAVE_IDAT; break; }#if defined(PNG_READ_bKGD_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) png_handle_bKGD(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_cHRM_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) png_handle_cHRM(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_gAMA_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) png_handle_gAMA(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_hIST_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) png_handle_hIST(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_oFFs_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) png_handle_oFFs(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_pCAL_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) png_handle_pCAL(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_sCAL_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4)) png_handle_sCAL(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_pHYs_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) png_handle_pHYs(png_ptr, info_ptr, length);#endif#if defined(PNG_READ_sBIT_SUPPORTED) else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -