/*---------------------------------------------------------------------------
rpng - simple PNG display program readpng.c
---------------------------------------------------------------------------
Copyright (c) 1998-2000 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 <stdio.h>
#include <stdlib.h>
#include "readpng.h" /* typedefs, common macros, public prototypes */
/* future versions of libpng will provide this macro: */
#ifndef png_jmpbuf
#endif
void readpng_version_info(void)
{
}
/* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
{
/* first do a quick check that the file really is a PNG image; could
* have used slightly more general png_sig_cmp() function instead */
return 1; /* bad signature */
/* could pass pointers to user-defined error handlers instead of NULLs: */
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 */
return 2;
}
/* alternatively, could make separate calls to png_get_image_width(),
* etc., but want bit_depth and color_type for later [don't care about
* compression_type and filter_type => NULLs] */
/* OK, that's all we need for now; return happy */
return 0;
}
/* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
* scales values to 8-bit if necessary */
{
/* setjmp() must be called in every function that calls a PNG-reading
* libpng function */
return 2;
}
return 1;
/* 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 {
}
return 0;
}
/* display_exponent == LUT_exponent * CRT_exponent */
{
double gamma;
/* setjmp() must be called in every function that calls a PNG-reading
* libpng function */
return NULL;
}
/* 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 example in the libpng documentation, we have *no* idea where
* this file may have come from--so if it doesn't have a file gamma, don't
* do any correction ("do no harm") */
/* all transformations have been registered; now update info_ptr data,
* get rowbytes and channels, and allocate image memory */
return NULL;
}
image_data = NULL;
return NULL;
}
Trace((stderr, "readpng_get_image: channels = %d, rowbytes = %ld, height = %ld\n", *pChannels, rowbytes, height));
/* set the individual row_pointers to point at the correct offsets */
for (i = 0; i < height; ++i)
/* now we can go ahead and just read the whole image */
/* and we're done! (png_read_end() can be omitted if no processing of
row_pointers = NULL;
return image_data;
}
{
if (free_image_data && image_data) {
image_data = NULL;
}
}
}