671242f350d172e106580348e24bab66b0d7e6a5vboxsync/*---------------------------------------------------------------------------
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync rpng2 - progressive-model PNG display program readpng2.c
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ---------------------------------------------------------------------------
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync Changelog:
671242f350d172e106580348e24bab66b0d7e6a5vboxsync - 1.01: initial public release
671242f350d172e106580348e24bab66b0d7e6a5vboxsync - 1.02: added code to skip unused chunks (GR-P)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ---------------------------------------------------------------------------
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync Copyright (c) 1998-2002 Greg Roelofs. All rights reserved.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync This software is provided "as is," without warranty of any kind,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync express or implied. In no event shall the author or contributors
671242f350d172e106580348e24bab66b0d7e6a5vboxsync be held liable for any damages arising in any way from the use of
671242f350d172e106580348e24bab66b0d7e6a5vboxsync this software.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync Permission is granted to anyone to use this software for any purpose,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync including commercial applications, and to alter it and redistribute
671242f350d172e106580348e24bab66b0d7e6a5vboxsync it freely, subject to the following restrictions:
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 1. Redistributions of source code must retain the above copyright
671242f350d172e106580348e24bab66b0d7e6a5vboxsync notice, disclaimer, and this list of conditions.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 2. Redistributions in binary form must reproduce the above copyright
671242f350d172e106580348e24bab66b0d7e6a5vboxsync notice, disclaimer, and this list of conditions in the documenta-
671242f350d172e106580348e24bab66b0d7e6a5vboxsync tion and/or other materials provided with the distribution.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 3. All advertising materials mentioning features or use of this
671242f350d172e106580348e24bab66b0d7e6a5vboxsync software must display the following acknowledgment:
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync This product includes software developed by Greg Roelofs
671242f350d172e106580348e24bab66b0d7e6a5vboxsync and contributors for the book, "PNG: The Definitive Guide,"
671242f350d172e106580348e24bab66b0d7e6a5vboxsync published by O'Reilly and Associates.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ---------------------------------------------------------------------------*/
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#include <stdlib.h> /* for exit() prototype */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#include "png.h" /* libpng header; includes zlib.h and setjmp.h */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#include "readpng2.h" /* typedefs, common macros, public prototypes */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync/* local prototypes */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_info_callback(png_structp png_ptr, png_infop info_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_row_callback(png_structp png_ptr, png_bytep new_row,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_uint_32 row_num, int pass);
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_end_callback(png_structp png_ptr, png_infop info_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_error_handler(png_structp png_ptr, png_const_charp msg);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncvoid readpng2_version_info(void)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (defined(__i386__) || defined(_M_IX86)) && \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /*
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * WARNING: This preprocessor approach means that the following code
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * cannot be used with a libpng DLL older than 1.2.0--the
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * compiled-in symbols for the new functions will not exist.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * (Could use dlopen() and dlsym() on Unix and corresponding
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * calls for Windows, but not portable...)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync int mmxsupport = png_mmx_support();
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmxsupport < 0)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " Compiled with libpng %s; using libpng %s "
671242f350d172e106580348e24bab66b0d7e6a5vboxsync "without MMX support.\n", PNG_LIBPNG_VER_STRING, png_libpng_ver);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync else {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync int compilerID;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_uint_32 mmx_mask = png_get_mmx_flagmask(
671242f350d172e106580348e24bab66b0d7e6a5vboxsync PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " Compiled with libpng %s; using libpng %s "
671242f350d172e106580348e24bab66b0d7e6a5vboxsync "with MMX support\n (%s version).", PNG_LIBPNG_VER_STRING,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_libpng_ver, compilerID == 1? "MSVC++" :
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (compilerID == 2? "GNU C" : "unknown"));
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " Processor %s MMX instructions.\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mmxsupport? "supports" : "does not support");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmxsupport > 0) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync int num_optims = 0;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync " Potential MMX optimizations supported by libpng:\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_SUB)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_UP)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_AVG)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask & PNG_ASM_FLAG_MMX_READ_FILTER_PAETH)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (num_optims)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync " decoding %s row filters (reading)\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (num_optims == 4)? "all non-trivial" : "some");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " combining rows (reading)\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask & PNG_ASM_FLAG_MMX_READ_INTERLACE) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync " expanding interlacing (reading)\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mmx_mask &= ~( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_INTERLACE \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_UP \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH );
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mmx_mask) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " other (unknown)\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ++num_optims;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (num_optims == 0)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " (none)\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#else
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " Compiled with libpng %s; using libpng %s "
671242f350d172e106580348e24bab66b0d7e6a5vboxsync "without MMX support.\n", PNG_LIBPNG_VER_STRING, png_libpng_ver);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#endif
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync ZLIB_VERSION, zlib_version);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncint readpng2_check_sig(uch *sig, int num)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return png_check_sig(sig, num);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync/* returns 0 for success, 2 for libpng problem, 4 for out of memory */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncint readpng2_init(mainprog_info *mainprog_ptr)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_structp png_ptr; /* note: temporary variables! */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_infop info_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* could also replace libpng warning-handler (final NULL), but no need: */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, mainprog_ptr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync readpng2_error_handler, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (!png_ptr)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return 4; /* out of memory */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync info_ptr = png_create_info_struct(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (!info_ptr) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_read_struct(&png_ptr, NULL, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return 4; /* out of memory */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* we could create a second info struct here (end_info), but it's only
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * useful if we want to keep pre- and post-IDAT chunk info separated
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * (mainly for PNG-aware image editors and converters) */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* setjmp() must be called in every function that calls a PNG-reading
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * libpng function, unless an alternate error handler was installed--
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * but compatible error handlers must either use longjmp() themselves
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * (as in this program) or exit immediately, so here we are: */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (setjmp(mainprog_ptr->jmpbuf)) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return 2;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* prepare the reader to ignore all recognized chunks whose data isn't
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * going to be used, i.e., all chunks recognized by libpng except for
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * IHDR, PLTE, IDAT, IEND, tRNS, bKGD, gAMA, and sRGB : */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#ifndef HANDLE_CHUNK_NEVER
671242f350d172e106580348e24bab66b0d7e6a5vboxsync/* prior to libpng-1.2.5, this macro was internal, so we define it here. */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync# define HANDLE_CHUNK_NEVER 1
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#endif
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* these byte strings were copied from png.h.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * If a future libpng version recognizes more chunks, add them
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * to this list. If a future version of readpng2.c recognizes
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * more chunks, delete them from this list. */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_byte png_chunk_types_to_ignore[]=
671242f350d172e106580348e24bab66b0d7e6a5vboxsync { 99, 72, 82, 77, '\0', /* cHRM */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 104, 73, 83, 84, '\0', /* hIST */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 105, 67, 67, 80, '\0', /* iCCP */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 105, 84, 88, 116, '\0', /* iTXt */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 111, 70, 70, 115, '\0', /* oFFs */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 112, 67, 65, 76, '\0', /* pCAL */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 115, 67, 65, 76, '\0', /* sCAL */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 112, 72, 89, 115, '\0', /* pHYs */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 115, 66, 73, 84, '\0', /* sBIT */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 115, 80, 76, 84, '\0', /* sPLT */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 116, 69, 88, 116, '\0', /* tEXt */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 116, 73, 77, 69, '\0', /* tIME */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync 122, 84, 88, 116, '\0'}; /* zTXt */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#define NUM_PNG_CHUNK_TYPES_TO_IGNORE 13
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_keep_unknown_chunks(png_ptr, HANDLE_CHUNK_NEVER,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_chunk_types_to_ignore, NUM_PNG_CHUNK_TYPES_TO_IGNORE);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#endif
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* instead of doing png_init_io() here, now we set up our callback
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * functions for progressive decoding */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_progressive_read_fn(png_ptr, mainprog_ptr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync readpng2_info_callback, readpng2_row_callback, readpng2_end_callback);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /*
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * may as well enable or disable MMX routines here, if supported;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync *
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * to enable all: mask = png_get_mmx_flagmask (
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * flags = png_get_asm_flags (png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * flags |= mask;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * png_set_asm_flags (png_ptr, flags);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync *
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * to disable all: mask = png_get_mmx_flagmask (
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * PNG_SELECT_READ | PNG_SELECT_WRITE, &compilerID);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * flags = png_get_asm_flags (png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * flags &= ~mask;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * png_set_asm_flags (png_ptr, flags);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#if (defined(__i386__) || defined(_M_IX86)) && \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /*
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * WARNING: This preprocessor approach means that the following code
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * cannot be used with a libpng DLL older than 1.2.0--the
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * compiled-in symbols for the new functions will not exist.
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * (Could use dlopen() and dlsym() on Unix and corresponding
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * calls for Windows, but not portable...)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#ifdef PNG_ASSEMBLER_CODE_SUPPORTED
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_uint_32 mmx_disable_mask = 0;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_uint_32 asm_flags, mmx_mask;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync int compilerID;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mainprog_ptr->nommxfilters)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mmx_disable_mask |= ( PNG_ASM_FLAG_MMX_READ_FILTER_SUB \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_UP \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH );
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mainprog_ptr->nommxcombine)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mmx_disable_mask |= PNG_ASM_FLAG_MMX_READ_COMBINE_ROW;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mainprog_ptr->nommxinterlace)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mmx_disable_mask |= PNG_ASM_FLAG_MMX_READ_INTERLACE;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync asm_flags = png_get_asm_flags(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_asm_flags(png_ptr, asm_flags & ~mmx_disable_mask);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* Now query libpng's asm settings, just for yuks. Note that this
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * differs from the querying of its *potential* MMX capabilities
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * in readpng2_version_info(); this is true runtime verification. */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync asm_flags = png_get_asm_flags(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mmx_mask = png_get_mmx_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync &compilerID);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_COMPILED)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync " MMX support (%s version) is compiled into libpng\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync compilerID == 1? "MSVC++" :
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (compilerID == 2? "GNU C" : "unknown"));
671242f350d172e106580348e24bab66b0d7e6a5vboxsync else
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX support is not compiled into libpng\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX instructions are %ssupported by CPU\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU)? "" : "not ");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX read support for combining rows is %sabled\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_READ_COMBINE_ROW)? "en" : "dis");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync " MMX read support for expanding interlacing is %sabled\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_READ_INTERLACE)? "en" : "dis");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX read support for \"sub\" filter is %sabled\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_SUB)? "en" : "dis");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX read support for \"up\" filter is %sabled\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_UP)? "en" : "dis");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX read support for \"avg\" filter is %sabled\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_AVG)? "en" : "dis");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX read support for \"Paeth\" filter is %sabled\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (asm_flags & PNG_ASM_FLAG_MMX_READ_FILTER_PAETH)? "en" : "dis");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync asm_flags &= (mmx_mask & ~( PNG_ASM_FLAG_MMX_READ_COMBINE_ROW \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_INTERLACE \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_SUB \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_UP \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_AVG \
671242f350d172e106580348e24bab66b0d7e6a5vboxsync | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ));
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (asm_flags)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync " additional MMX support is also enabled (0x%02lx)\n",
671242f350d172e106580348e24bab66b0d7e6a5vboxsync asm_flags);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#else /* !PNG_ASSEMBLER_CODE_SUPPORTED */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, " MMX querying is disabled in libpng.\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync#endif
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* make sure we save our pointers for use in readpng2_decode_data() */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->png_ptr = png_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->info_ptr = info_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* and that's all there is to initialization */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return 0;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync/* returns 0 for success, 2 for libpng (longjmp) problem */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncint readpng2_decode_data(mainprog_info *mainprog_ptr, uch *rawbuf, ulg length)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* setjmp() must be called in every function that calls a PNG-reading
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * libpng function */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (setjmp(mainprog_ptr->jmpbuf)) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->png_ptr = NULL;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->info_ptr = NULL;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return 2;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* hand off the next chunk of input data to libpng for decoding */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_process_data(png_ptr, info_ptr, rawbuf, length);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return 0;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_info_callback(png_structp png_ptr, png_infop info_ptr)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_info *mainprog_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync int color_type, bit_depth;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync double gamma;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* setjmp() doesn't make sense here, because we'd either have to exit(),
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * longjmp() ourselves, or return control to libpng, which doesn't want
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * to see us again. By not doing anything here, libpng will instead jump
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * to readpng2_decode_data(), which can return an error value to the main
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * program. */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* retrieve the pointer to our special-purpose struct, using the png_ptr
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * that libpng passed back to us (i.e., not a global this time--there's
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * no real difference for a single image, but for a multithreaded browser
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * decoding several PNG images at the same time, one needs to avoid mixing
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * up different images' structs) */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr = png_get_progressive_ptr(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mainprog_ptr == NULL) { /* we be hosed */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync "readpng2 error: main struct not recoverable in info_callback.\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fflush(stderr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /*
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * Alternatively, we could call our error-handler just like libpng
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * does, which would effectively terminate the program. Since this
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * can only happen if png_ptr gets redirected somewhere odd or the
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * main PNG struct gets wiped, we're probably toast anyway. (If
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * png_ptr itself is NULL, we would not have been called.)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* this is just like in the non-progressive case */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_get_IHDR(png_ptr, info_ptr, &mainprog_ptr->width,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync &mainprog_ptr->height, &bit_depth, &color_type, NULL, NULL, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* since we know we've read all of the PNG file's "header" (i.e., up
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * to IDAT), we can check for a background color here */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mainprog_ptr->need_bgcolor &&
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD))
671242f350d172e106580348e24bab66b0d7e6a5vboxsync {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_color_16p pBackground;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* it is not obvious from the libpng documentation, but this function
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * takes a pointer to a pointer, and it always returns valid red,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * green and blue values, regardless of color_type: */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_get_bKGD(png_ptr, info_ptr, &pBackground);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* however, it always returns the raw bKGD data, regardless of any
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * bit-depth transformations, so check depth and adjust if necessary */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (bit_depth == 16) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_red = pBackground->red >> 8;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_green = pBackground->green >> 8;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_blue = pBackground->blue >> 8;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (bit_depth == 1)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_red = mainprog_ptr->bg_green =
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_blue = pBackground->gray? 255 : 0;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync else if (bit_depth == 2)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_red = mainprog_ptr->bg_green =
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_blue = (255/3) * pBackground->gray;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync else /* bit_depth == 4 */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_red = mainprog_ptr->bg_green =
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_blue = (255/15) * pBackground->gray;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync } else {
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_red = (uch)pBackground->red;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_green = (uch)pBackground->green;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->bg_blue = (uch)pBackground->blue;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* as before, let libpng expand palette images to RGB, low-bit-depth
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * grayscale images to 8 bits, transparency chunks to full alpha channel;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * strip 16-bit-per-sample images to 8 bits per sample; and convert
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * grayscale to RGB[A] */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (color_type == PNG_COLOR_TYPE_PALETTE)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_expand(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_expand(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_expand(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (bit_depth == 16)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_strip_16(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (color_type == PNG_COLOR_TYPE_GRAY ||
671242f350d172e106580348e24bab66b0d7e6a5vboxsync color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_gray_to_rgb(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* Unlike the basic viewer, which was designed to operate on local files,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * this program is intended to simulate a web browser--even though we
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * actually read from a local file, too. But because we are pretending
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * that most of the images originate on the Internet, we follow the recom-
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * mendation of the sRGB proposal and treat unlabelled images (no gAMA
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * chunk) as existing in the sRGB color space. That is, we assume that
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * such images have a file gamma of 0.45455, which corresponds to a PC-like
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * display system. This change in assumptions will have no effect on a
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * PC-like system, but on a Mac, SGI, NeXT or other system with a non-
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * identity lookup table, it will darken unlabelled images, which effec-
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * tively favors images from PC-like systems over those originating on
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * the local platform. Note that mainprog_ptr->display_exponent is the
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * "gamma" value for the entire display system, i.e., the product of
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * LUT_exponent and CRT_exponent. */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (png_get_gAMA(png_ptr, info_ptr, &gamma))
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_gamma(png_ptr, mainprog_ptr->display_exponent, gamma);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync else
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_set_gamma(png_ptr, mainprog_ptr->display_exponent, 0.45455);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* we'll let libpng expand interlaced images, too */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->passes = png_set_interlace_handling(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* all transformations have been registered; now update info_ptr data and
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * then get rowbytes and channels */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_read_update_info(png_ptr, info_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->rowbytes = (int)png_get_rowbytes(png_ptr, info_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->channels = png_get_channels(png_ptr, info_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* Call the main program to allocate memory for the image buffer and
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * initialize windows and whatnot. (The old-style function-pointer
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * invocation is used for compatibility with a few supposedly ANSI
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * compilers that nevertheless barf on "fn_ptr()"-style syntax.) */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (*mainprog_ptr->mainprog_init)();
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* and that takes care of initialization */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_row_callback(png_structp png_ptr, png_bytep new_row,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_uint_32 row_num, int pass)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_info *mainprog_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* first check whether the row differs from the previous pass; if not,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * nothing to combine or display */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (!new_row)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* retrieve the pointer to our special-purpose struct so we can access
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * the old rows and image-display callback function */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr = png_get_progressive_ptr(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* save the pass number for optional use by the front end */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->pass = pass;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* have libpng either combine the new row data with the existing row data
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * from previous passes (if interlaced) or else just copy the new row
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * into the main program's image buffer */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_progressive_combine_row(png_ptr, mainprog_ptr->row_pointers[row_num],
671242f350d172e106580348e24bab66b0d7e6a5vboxsync new_row);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* finally, call the display routine in the main program with the number
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * of the row we just updated */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (*mainprog_ptr->mainprog_display_row)(row_num);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* and we're ready for more */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_end_callback(png_structp png_ptr, png_infop info_ptr)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_info *mainprog_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* retrieve the pointer to our special-purpose struct */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr = png_get_progressive_ptr(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* let the main program know that it should flush any buffered image
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * data to the display now and set a "done" flag or whatever, but note
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * that it SHOULD NOT DESTROY THE PNG STRUCTS YET--in other words, do
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * NOT call readpng2_cleanup() either here or in the finish_display()
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * routine; wait until control returns to the main program via
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * readpng2_decode_data() */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync (*mainprog_ptr->mainprog_finish_display)();
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* all done */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync return;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncvoid readpng2_cleanup(mainprog_info *mainprog_ptr)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_structp png_ptr = (png_structp)mainprog_ptr->png_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_infop info_ptr = (png_infop)mainprog_ptr->info_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (png_ptr && info_ptr)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->png_ptr = NULL;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr->info_ptr = NULL;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsyncstatic void readpng2_error_handler(png_structp png_ptr, png_const_charp msg)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync{
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_info *mainprog_ptr;
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync /* This function, aside from the extra step of retrieving the "error
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * pointer" (below) and the fact that it exists within the application
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * rather than within libpng, is essentially identical to libpng's
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * default error handler. The second point is critical: since both
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * setjmp() and longjmp() are called from the same code, they are
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * guaranteed to have compatible notions of how big a jmp_buf is,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * regardless of whether _BSD_SOURCE or anything else has (or has not)
671242f350d172e106580348e24bab66b0d7e6a5vboxsync * been defined. */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr, "readpng2 libpng error: %s\n", msg);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fflush(stderr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync mainprog_ptr = png_get_error_ptr(png_ptr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync if (mainprog_ptr == NULL) { /* we are completely hosed now */
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fprintf(stderr,
671242f350d172e106580348e24bab66b0d7e6a5vboxsync "readpng2 severe error: jmpbuf not recoverable; terminating.\n");
671242f350d172e106580348e24bab66b0d7e6a5vboxsync fflush(stderr);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync exit(99);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync }
671242f350d172e106580348e24bab66b0d7e6a5vboxsync
671242f350d172e106580348e24bab66b0d7e6a5vboxsync longjmp(mainprog_ptr->jmpbuf, 1);
671242f350d172e106580348e24bab66b0d7e6a5vboxsync}