?? readpng2.c
字號:
/*--------------------------------------------------------------------------- rpng2 - progressive-model PNG display program readpng2.c --------------------------------------------------------------------------- Copyright (c) 1998-2007 Greg Roelofs. All rights reserved. This software is provided "as is," without warranty of any kind, express or implied. In no event shall the author or contributors be held liable for any damages arising in any way from the use of this software. The contents of this file are DUAL-LICENSED. You may modify and/or redistribute this software according to the terms of one of the following two licenses (at your option): LICENSE 1 ("BSD-like with advertising clause"): Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. Redistributions of source code must retain the above copyright notice, disclaimer, and this list of conditions. 2. Redistributions in binary form must reproduce the above copyright notice, disclaimer, and this list of conditions in the documenta- tion and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgment: This product includes software developed by Greg Roelofs and contributors for the book, "PNG: The Definitive Guide," published by O'Reilly and Associates. LICENSE 2 (GNU GPL v2 or later): This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---------------------------------------------------------------------------*/#include <stdlib.h> /* for exit() prototype */#include "png.h" /* libpng header; includes zlib.h and setjmp.h */#include "readpng2.h" /* typedefs, common macros, public prototypes *//* local prototypes */static void readpng2_info_callback(png_structp png_ptr, png_infop info_ptr);static void readpng2_row_callback(png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass);static void readpng2_end_callback(png_structp png_ptr, png_infop info_ptr);static void readpng2_error_handler(png_structp png_ptr, png_const_charp msg);void readpng2_version_info(void){#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \ (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__)) && \ defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) /* * WARNING: This preprocessor approach means that the following code * cannot be used with a libpng DLL older than 1.2.0--the * compiled-in symbols for the new functions will not exist. * (Could use dlopen() and dlsym() on Unix and corresponding * calls for Windows, but not portable...) */ { int mmxsupport = png_mmx_support(); if (mmxsupport < 0) fprintf(stderr, " Compiled with libpng %s; using libpng %s " "without MMX support.\n", PNG_LIBPNG_VER_STRING, png_libpng_ver); else { int compilerID; png_uint_32 mmx_mask = png_get_mmx_flagmask( PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID); fprintf(stderr, " Compiled with libpng %s; using libpng %s " "with MMX support\n (%s version).", PNG_LIBPNG_VER_STRING, png_libpng_ver, compilerID == 1? "MSVC++" : (compilerID == 2? "GNU C" : "unknown")); fprintf(stderr, " Processor (x86%s) %s MMX instructions.\n",#if defined(__x86_64__) "_64",#else "",#endif mmxsupport? "supports" : "does not support"); if (mmxsupport > 0) { int num_optims = 0; fprintf(stderr, " Potential MMX optimizations supported by libpng:\n"); if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_SUB) ++num_optims; if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_UP) ++num_optims; if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_AVG) ++num_optims; if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_PAETH) ++num_optims; if (num_optims) fprintf(stderr, " decoding %s row filters (reading)\n", (num_optims == 4)? "all non-trivial" : "some"); if (mmx_mask & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) { fprintf(stderr, " combining rows (reading)\n"); ++num_optims; } if (mmx_mask & PNG_ASM_FLAG_MMX_READ_INTERLACE) { fprintf(stderr, " expanding interlacing (reading)\n"); ++num_optims; } mmx_mask &= ~( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ | PNG_ASM_FLAG_MMX_READ_INTERLACE \ | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ | PNG_ASM_FLAG_MMX_READ_FILTER_UP \ | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ); if (mmx_mask) { fprintf(stderr, " other (unknown)\n"); ++num_optims; } if (num_optims == 0) fprintf(stderr, " (none)\n"); } } }#else fprintf(stderr, " Compiled with libpng %s; using libpng %s " "without MMX support.\n", PNG_LIBPNG_VER_STRING, png_libpng_ver);#endif fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n", ZLIB_VERSION, zlib_version);}int readpng2_check_sig(uch *sig, int num){ return png_check_sig(sig, num);}/* returns 0 for success, 2 for libpng problem, 4 for out of memory */int readpng2_init(mainprog_info *mainprog_ptr){ png_structp png_ptr; /* note: temporary variables! */ png_infop info_ptr; /* could also replace libpng warning-handler (final NULL), but no need: */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr, readpng2_error_handler, NULL); if (!png_ptr) return 4; /* out of memory */ info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_read_struct(&png_ptr, NULL, NULL); return 4; /* out of memory */ } /* we could create a second info struct here (end_info), but it's only * useful if we want to keep pre- and post-IDAT chunk info separated * (mainly for PNG-aware image editors and converters) */ /* setjmp() must be called in every function that calls a PNG-reading * libpng function, unless an alternate error handler was installed-- * but compatible error handlers must either use longjmp() themselves * (as in this program) or exit immediately, so here we are: */ if (setjmp(mainprog_ptr->jmpbuf)) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return 2; }#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED /* prepare the reader to ignore all recognized chunks whose data won't be * used, i.e., all chunks recognized by libpng except for IHDR, PLTE, IDAT, * IEND, tRNS, bKGD, gAMA, and sRGB (small performance improvement) */ { /* These byte strings were copied from png.h. If a future libpng * version recognizes more chunks, add them to this list. If a * future version of readpng2.c recognizes more chunks, delete them * from this list. */ static const png_byte chunks_to_ignore[] = { 99, 72, 82, 77, '\0', /* cHRM */ 104, 73, 83, 84, '\0', /* hIST */ 105, 67, 67, 80, '\0', /* iCCP */ 105, 84, 88, 116, '\0', /* iTXt */ 111, 70, 70, 115, '\0', /* oFFs */ 112, 67, 65, 76, '\0', /* pCAL */ 112, 72, 89, 115, '\0', /* pHYs */ 115, 66, 73, 84, '\0', /* sBIT */ 115, 67, 65, 76, '\0', /* sCAL */ 115, 80, 76, 84, '\0', /* sPLT */ 115, 84, 69, 82, '\0', /* sTER */ 116, 69, 88, 116, '\0', /* tEXt */ 116, 73, 77, 69, '\0', /* tIME */ 122, 84, 88, 116, '\0' /* zTXt */ }; png_set_keep_unknown_chunks(png_ptr, 1 /* PNG_HANDLE_CHUNK_NEVER */, chunks_to_ignore, sizeof(chunks_to_ignore)/5); }#endif /* PNG_UNKNOWN_CHUNKS_SUPPORTED */ /* instead of doing png_init_io() here, now we set up our callback * functions for progressive decoding */ png_set_progressive_read_fn(png_ptr, mainprog_ptr, readpng2_info_callback, readpng2_row_callback, readpng2_end_callback); /* * may as well enable or disable MMX routines here, if supported; * * to enable all: mask = png_get_mmx_flagmask ( * PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID); * flags = png_get_asm_flags (png_ptr); * flags |= mask; * png_set_asm_flags (png_ptr, flags); * * to disable all: mask = png_get_mmx_flagmask ( * PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID); * flags = png_get_asm_flags (png_ptr); * flags &= ~mask; * png_set_asm_flags (png_ptr, flags); */#if (defined(__i386__) || defined(_M_IX86) || defined(__x86_64__)) && \ defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) /* * WARNING: This preprocessor approach means that the following code * cannot be used with a libpng DLL older than 1.2.0--the * compiled-in symbols for the new functions will not exist. * (Could use dlopen() and dlsym() on Unix and corresponding * calls for Windows, but not portable...) */ {#ifdef PNG_ASSEMBLER_CODE_SUPPORTED png_uint_32 mmx_disable_mask = 0; png_uint_32 asm_flags, mmx_mask; int compilerID; if (mainprog_ptr->nommxfilters) mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ | PNG_ASM_FLAG_MMX_READ_FILTER_UP \ | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ); if (mainprog_ptr->nommxcombine) mmx_disable_mask |= PNG_ASM_FLAG_MMX_READ_COMBINE_ROW; if (mainprog_ptr->nommxinterlace) mmx_disable_mask |= PNG_ASM_FLAG_MMX_READ_INTERLACE; asm_flags = png_get_asm_flags(png_ptr); png_set_asm_flags(png_ptr, asm_flags & ~mmx_disable_mask); /* Now query libpng's asm settings, just for yuks. Note that this * differs from the querying of its *potential* MMX capabilities * in readpng2_version_info(); this is true runtime verification. */ asm_flags = png_get_asm_flags(png_ptr); mmx_mask = png_get_mmx_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID); if (asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_COMPILED) fprintf(stderr, " MMX support (%s version) is compiled into libpng\n", compilerID == 1? "MSVC++" : (compilerID == 2? "GNU C" : "unknown")); else fprintf(stderr, " MMX support is not compiled into libpng\n"); fprintf(stderr, " MMX instructions are %ssupported by CPU\n", (asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU)? "" : "not "); fprintf(stderr, " MMX read support for combining rows is %sabled\n", (asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW)? "en" : "dis"); fprintf(stderr, " MMX read support for expanding interlacing is %sabled\n", (asm_flags & PNG_ASM_FLAG_MMX_READ_INTERLACE)? "en" : "dis"); fprintf(stderr, " MMX read support for \"sub\" filter is %sabled\n", (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_SUB)? "en" : "dis"); fprintf(stderr, " MMX read support for \"up\" filter is %sabled\n", (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_UP)? "en" : "dis"); fprintf(stderr, " MMX read support for \"avg\" filter is %sabled\n", (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_AVG)? "en" : "dis"); fprintf(stderr, " MMX read support for \"Paeth\" filter is %sabled\n", (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_PAETH)? "en" : "dis"); asm_flags &= (mmx_mask & ~( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \ | PNG_ASM_FLAG_MMX_READ_INTERLACE \ | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \ | PNG_ASM_FLAG_MMX_READ_FILTER_UP \ | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \ | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ));
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -