/*---------------------------------------------------------------------------
rpng2 - progressive-model PNG display program readpng2.c
---------------------------------------------------------------------------
Changelog:
- 1.01: initial public release
- 1.02: added code to skip unused chunks (GR-P)
---------------------------------------------------------------------------
Copyright (c) 1998-2002 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.
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-
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.
---------------------------------------------------------------------------*/
#include <stdlib.h> /* for exit() prototype */
#include "readpng2.h" /* typedefs, common macros, public prototypes */
/* local prototypes */
void readpng2_version_info(void)
{
#if defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
/*
* 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...)
*/
{
if (mmxsupport < 0)
else {
int compilerID;
"with MMX support\n (%s version).", PNG_LIBPNG_VER_STRING,
if (mmxsupport > 0) {
int num_optims = 0;
" Potential MMX optimizations supported by libpng:\n");
++num_optims;
++num_optims;
++num_optims;
++num_optims;
if (num_optims)
" decoding %s row filters (reading)\n",
++num_optims;
}
if (mmx_mask & PNG_ASM_FLAG_MMX_READ_INTERLACE) {
" expanding interlacing (reading)\n");
++num_optims;
}
if (mmx_mask) {
++num_optims;
}
if (num_optims == 0)
}
}
}
#else
#endif
}
{
}
/* returns 0 for success, 2 for libpng problem, 4 for out of memory */
{
/* could also replace libpng warning-handler (final NULL), but no need: */
if (!png_ptr)
return 4; /* out of memory */
if (!info_ptr) {
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: */
return 2;
}
/* prepare the reader to ignore all recognized chunks whose data isn't
* going to be used, i.e., all chunks recognized by libpng except for
* IHDR, PLTE, IDAT, IEND, tRNS, bKGD, gAMA, and sRGB : */
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
{
#ifndef HANDLE_CHUNK_NEVER
/* prior to libpng-1.2.5, this macro was internal, so we define it here. */
#endif
/* 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. */
{ 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 */
115, 67, 65, 76, '\0', /* sCAL */
112, 72, 89, 115, '\0', /* pHYs */
115, 66, 73, 84, '\0', /* sBIT */
115, 80, 76, 84, '\0', /* sPLT */
116, 69, 88, 116, '\0', /* tEXt */
116, 73, 77, 69, '\0', /* tIME */
122, 84, 88, 116, '\0'}; /* zTXt */
}
#endif
/* instead of doing png_init_io() here, now we set up our callback
* functions for progressive decoding */
/*
* 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);
*/
/*
* 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
int compilerID;
if (mainprog_ptr->nommxfilters)
if (mainprog_ptr->nommxcombine)
if (mainprog_ptr->nommxinterlace)
/* 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. */
&compilerID);
" MMX support (%s version) is compiled into libpng\n",
else
" MMX read support for expanding interlacing is %sabled\n",
if (asm_flags)
" additional MMX support is also enabled (0x%02lx)\n",
#else /* !PNG_ASSEMBLER_CODE_SUPPORTED */
#endif /* ?PNG_ASSEMBLER_CODE_SUPPORTED */
}
#endif
/* make sure we save our pointers for use in readpng2_decode_data() */
/* and that's all there is to initialization */
return 0;
}
/* returns 0 for success, 2 for libpng (longjmp) problem */
{
/* setjmp() must be called in every function that calls a PNG-reading
* libpng function */
return 2;
}
/* hand off the next chunk of input data to libpng for decoding */
return 0;
}
{
double gamma;
/* setjmp() doesn't make sense here, because we'd either have to exit(),
* longjmp() ourselves, or return control to libpng, which doesn't want
* to see us again. By not doing anything here, libpng will instead jump
* to readpng2_decode_data(), which can return an error value to the main
* program. */
/* retrieve the pointer to our special-purpose struct, using the png_ptr
* that libpng passed back to us (i.e., not a global this time--there's
* no real difference for a single image, but for a multithreaded browser
* decoding several PNG images at the same time, one needs to avoid mixing
* up different images' structs) */
"readpng2 error: main struct not recoverable in info_callback.\n");
return;
/*
* Alternatively, we could call our error-handler just like libpng
* does, which would effectively terminate the program. Since this
* can only happen if png_ptr gets redirected somewhere odd or the
* main PNG struct gets wiped, we're probably toast anyway. (If
* png_ptr itself is NULL, we would not have been called.)
*/
}
/* this is just like in the non-progressive case */
/* since we know we've read all of the PNG file's "header" (i.e., up
* to IDAT), we can check for a background color here */
if (mainprog_ptr->need_bgcolor &&
{
/* it is not obvious from the libpng documentation, but this function
* takes a pointer to a pointer, and it always returns valid red,
* green and blue values, regardless of color_type: */
/* however, it always returns the raw bKGD data, regardless of any
* bit-depth transformations, so check depth and adjust if necessary */
if (bit_depth == 16) {
if (bit_depth == 1)
else if (bit_depth == 2)
else /* bit_depth == 4 */
} else {
}
}
/* as before, let libpng expand palette images to RGB, low-bit-depth
* grayscale images to 8 bits, transparency chunks to full alpha channel;
* strip 16-bit-per-sample images to 8 bits per sample; and convert
* grayscale to RGB[A] */
if (color_type == PNG_COLOR_TYPE_PALETTE)
if (bit_depth == 16)
if (color_type == PNG_COLOR_TYPE_GRAY ||
/* Unlike the basic viewer, which was designed to operate on local files,
* this program is intended to simulate a web browser--even though we
* actually read from a local file, too. But because we are pretending
* that most of the images originate on the Internet, we follow the recom-
* mendation of the sRGB proposal and treat unlabelled images (no gAMA
* chunk) as existing in the sRGB color space. That is, we assume that
* such images have a file gamma of 0.45455, which corresponds to a PC-like
* display system. This change in assumptions will have no effect on a
* PC-like system, but on a Mac, SGI, NeXT or other system with a non-
* identity lookup table, it will darken unlabelled images, which effec-
* tively favors images from PC-like systems over those originating on
* the local platform. Note that mainprog_ptr->display_exponent is the
* "gamma" value for the entire display system, i.e., the product of
* LUT_exponent and CRT_exponent. */
else
/* we'll let libpng expand interlaced images, too */
/* all transformations have been registered; now update info_ptr data and
* then get rowbytes and channels */
/* Call the main program to allocate memory for the image buffer and
* initialize windows and whatnot. (The old-style function-pointer
* invocation is used for compatibility with a few supposedly ANSI
* compilers that nevertheless barf on "fn_ptr()"-style syntax.) */
(*mainprog_ptr->mainprog_init)();
/* and that takes care of initialization */
return;
}
{
/* first check whether the row differs from the previous pass; if not,
* nothing to combine or display */
if (!new_row)
return;
/* retrieve the pointer to our special-purpose struct so we can access
* the old rows and image-display callback function */
/* save the pass number for optional use by the front end */
/* have libpng either combine the new row data with the existing row data
* from previous passes (if interlaced) or else just copy the new row
* into the main program's image buffer */
new_row);
/* finally, call the display routine in the main program with the number
* of the row we just updated */
/* and we're ready for more */
return;
}
{
/* retrieve the pointer to our special-purpose struct */
/* let the main program know that it should flush any buffered image
* data to the display now and set a "done" flag or whatever, but note
* that it SHOULD NOT DESTROY THE PNG STRUCTS YET--in other words, do
* NOT call readpng2_cleanup() either here or in the finish_display()
* routine; wait until control returns to the main program via
* readpng2_decode_data() */
/* all done */
return;
}
{
}
{
/* This function, aside from the extra step of retrieving the "error
* pointer" (below) and the fact that it exists within the application
* rather than within libpng, is essentially identical to libpng's
* default error handler. The second point is critical: since both
* setjmp() and longjmp() are called from the same code, they are
* guaranteed to have compatible notions of how big a jmp_buf is,
* regardless of whether _BSD_SOURCE or anything else has (or has not)
* been defined. */
"readpng2 severe error: jmpbuf not recoverable; terminating.\n");
exit(99);
}
}