fukasawa e60969
/*---------------------------------------------------------------------------
fukasawa e60969
fukasawa e60969
   rpng - simple PNG display program                              readpng.c
fukasawa e60969
fukasawa e60969
  ---------------------------------------------------------------------------
fukasawa e60969
fukasawa e60969
      Copyright (c) 1998-2007 Greg Roelofs.  All rights reserved.
fukasawa e60969
fukasawa e60969
      This software is provided "as is," without warranty of any kind,
fukasawa e60969
      express or implied.  In no event shall the author or contributors
fukasawa e60969
      be held liable for any damages arising in any way from the use of
fukasawa e60969
      this software.
fukasawa e60969
fukasawa e60969
      The contents of this file are DUAL-LICENSED.  You may modify and/or
fukasawa e60969
      redistribute this software according to the terms of one of the
fukasawa e60969
      following two licenses (at your option):
fukasawa e60969
fukasawa e60969
fukasawa e60969
      LICENSE 1 ("BSD-like with advertising clause"):
fukasawa e60969
fukasawa e60969
      Permission is granted to anyone to use this software for any purpose,
fukasawa e60969
      including commercial applications, and to alter it and redistribute
fukasawa e60969
      it freely, subject to the following restrictions:
fukasawa e60969
fukasawa e60969
      1. Redistributions of source code must retain the above copyright
fukasawa e60969
         notice, disclaimer, and this list of conditions.
fukasawa e60969
      2. Redistributions in binary form must reproduce the above copyright
fukasawa e60969
         notice, disclaimer, and this list of conditions in the documenta-
fukasawa e60969
         tion and/or other materials provided with the distribution.
fukasawa e60969
      3. All advertising materials mentioning features or use of this
fukasawa e60969
         software must display the following acknowledgment:
fukasawa e60969
fukasawa e60969
            This product includes software developed by Greg Roelofs
fukasawa e60969
            and contributors for the book, "PNG: The Definitive Guide,"
fukasawa e60969
            published by O'Reilly and Associates.
fukasawa e60969
fukasawa e60969
fukasawa e60969
      LICENSE 2 (GNU GPL v2 or later):
fukasawa e60969
fukasawa e60969
      This program is free software; you can redistribute it and/or modify
fukasawa e60969
      it under the terms of the GNU General Public License as published by
fukasawa e60969
      the Free Software Foundation; either version 2 of the License, or
fukasawa e60969
      (at your option) any later version.
fukasawa e60969
fukasawa e60969
      This program is distributed in the hope that it will be useful,
fukasawa e60969
      but WITHOUT ANY WARRANTY; without even the implied warranty of
fukasawa e60969
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
fukasawa e60969
      GNU General Public License for more details.
fukasawa e60969
fukasawa e60969
      You should have received a copy of the GNU General Public License
fukasawa e60969
      along with this program; if not, write to the Free Software Foundation,
fukasawa e60969
      Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
fukasawa e60969
fukasawa e60969
  ---------------------------------------------------------------------------*/
fukasawa e60969
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <zlib.h></zlib.h>
fukasawa e60969
fukasawa e60969
#include "png.h"        /* libpng header */
fukasawa e60969
#include "readpng.h"    /* typedefs, common macros, public prototypes */
fukasawa e60969
fukasawa e60969
/* future versions of libpng will provide this macro: */
fukasawa e60969
#ifndef png_jmpbuf
fukasawa e60969
#  define png_jmpbuf(png_ptr)   ((png_ptr)->jmpbuf)
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
fukasawa e60969
static png_structp png_ptr = NULL;
fukasawa e60969
static png_infop info_ptr = NULL;
fukasawa e60969
fukasawa e60969
png_uint_32  width, height;
fukasawa e60969
int  bit_depth, color_type;
fukasawa e60969
uch  *image_data = NULL;
fukasawa e60969
fukasawa e60969
fukasawa e60969
void readpng_version_info(void)
fukasawa e60969
{
fukasawa e60969
    fprintf(stderr, "   Compiled with libpng %s; using libpng %s.\n",
fukasawa e60969
      PNG_LIBPNG_VER_STRING, png_libpng_ver);
fukasawa e60969
    fprintf(stderr, "   Compiled with zlib %s; using zlib %s.\n",
fukasawa e60969
      ZLIB_VERSION, zlib_version);
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
fukasawa e60969
fukasawa e60969
int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
fukasawa e60969
{
fukasawa e60969
    uch sig[8];
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* first do a quick check that the file really is a PNG image; could
fukasawa e60969
     * have used slightly more general png_sig_cmp() function instead */
fukasawa e60969
fukasawa e60969
    fread(sig, 1, 8, infile);
fukasawa e60969
    if (png_sig_cmp(sig, 0, 8))
fukasawa e60969
        return 1;   /* bad signature */
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* could pass pointers to user-defined error handlers instead of NULLs: */
fukasawa e60969
fukasawa e60969
    png_ptr = png_create_read_struct(png_get_libpng_ver(NULL), NULL, NULL,
fukasawa e60969
        NULL);
fukasawa e60969
    if (!png_ptr)
fukasawa e60969
        return 4;   /* out of memory */
fukasawa e60969
fukasawa e60969
    info_ptr = png_create_info_struct(png_ptr);
fukasawa e60969
    if (!info_ptr) {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, NULL, NULL);
fukasawa e60969
        return 4;   /* out of memory */
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* we could create a second info struct here (end_info), but it's only
fukasawa e60969
     * useful if we want to keep pre- and post-IDAT chunk info separated
fukasawa e60969
     * (mainly for PNG-aware image editors and converters) */
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* setjmp() must be called in every function that calls a PNG-reading
fukasawa e60969
     * libpng function */
fukasawa e60969
fukasawa e60969
    if (setjmp(png_jmpbuf(png_ptr))) {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
        return 2;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
fukasawa e60969
    png_init_io(png_ptr, infile);
fukasawa e60969
    png_set_sig_bytes(png_ptr, 8);  /* we already read the 8 signature bytes */
fukasawa e60969
fukasawa e60969
    png_read_info(png_ptr, info_ptr);  /* read all PNG info up to image data */
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* alternatively, could make separate calls to png_get_image_width(),
fukasawa e60969
     * etc., but want bit_depth and color_type for later [don't care about
fukasawa e60969
     * compression_type and filter_type => NULLs] */
fukasawa e60969
fukasawa e60969
    png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
fukasawa e60969
      NULL, NULL, NULL);
fukasawa e60969
    *pWidth = width;
fukasawa e60969
    *pHeight = height;
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* OK, that's all we need for now; return happy */
fukasawa e60969
fukasawa e60969
    return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
fukasawa e60969
 * scales values to 8-bit if necessary */
fukasawa e60969
fukasawa e60969
int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
fukasawa e60969
{
fukasawa e60969
    png_color_16p pBackground;
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* setjmp() must be called in every function that calls a PNG-reading
fukasawa e60969
     * libpng function */
fukasawa e60969
fukasawa e60969
    if (setjmp(png_jmpbuf(png_ptr))) {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
        return 2;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
fukasawa e60969
    if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD))
fukasawa e60969
        return 1;
fukasawa e60969
fukasawa e60969
    /* it is not obvious from the libpng documentation, but this function
fukasawa e60969
     * takes a pointer to a pointer, and it always returns valid red, green
fukasawa e60969
     * and blue values, regardless of color_type: */
fukasawa e60969
fukasawa e60969
    png_get_bKGD(png_ptr, info_ptr, &pBackground);
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* however, it always returns the raw bKGD data, regardless of any
fukasawa e60969
     * bit-depth transformations, so check depth and adjust if necessary */
fukasawa e60969
fukasawa e60969
    if (bit_depth == 16) {
fukasawa e60969
        *red   = pBackground->red   >> 8;
fukasawa e60969
        *green = pBackground->green >> 8;
fukasawa e60969
        *blue  = pBackground->blue  >> 8;
fukasawa e60969
    } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
fukasawa e60969
        if (bit_depth == 1)
fukasawa e60969
            *red = *green = *blue = pBackground->gray? 255 : 0;
fukasawa e60969
        else if (bit_depth == 2)
fukasawa e60969
            *red = *green = *blue = (255/3) * pBackground->gray;
fukasawa e60969
        else /* bit_depth == 4 */
fukasawa e60969
            *red = *green = *blue = (255/15) * pBackground->gray;
fukasawa e60969
    } else {
fukasawa e60969
        *red   = (uch)pBackground->red;
fukasawa e60969
        *green = (uch)pBackground->green;
fukasawa e60969
        *blue  = (uch)pBackground->blue;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
fukasawa e60969
fukasawa e60969
/* display_exponent == LUT_exponent * CRT_exponent */
fukasawa e60969
fukasawa e60969
uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
fukasawa e60969
{
fukasawa e60969
    double  gamma;
fukasawa e60969
    png_uint_32  i, rowbytes;
fukasawa e60969
    png_bytepp  row_pointers = NULL;
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* setjmp() must be called in every function that calls a PNG-reading
fukasawa e60969
     * libpng function */
fukasawa e60969
fukasawa e60969
    if (setjmp(png_jmpbuf(png_ptr))) {
fukasawa e60969
        free(image_data);
fukasawa e60969
        image_data = NULL;
fukasawa e60969
        free(row_pointers);
fukasawa e60969
        row_pointers = NULL;
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
        return NULL;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
fukasawa e60969
     * transparency chunks to full alpha channel; strip 16-bit-per-sample
fukasawa e60969
     * images to 8 bits per sample; and convert grayscale to RGB[A] */
fukasawa e60969
fukasawa e60969
    if (color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
        png_set_expand(png_ptr);
fukasawa e60969
    if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
fukasawa e60969
        png_set_expand(png_ptr);
fukasawa e60969
    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
fukasawa e60969
        png_set_expand(png_ptr);
fukasawa e60969
#ifdef PNG_READ_16_TO_8_SUPPORTED
fukasawa e60969
    if (bit_depth == 16)
fukasawa e60969
#  ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
fukasawa e60969
        png_set_scale_16(png_ptr);
fukasawa e60969
#  else
fukasawa e60969
        png_set_strip_16(png_ptr);
fukasawa e60969
#  endif
fukasawa e60969
#endif
fukasawa e60969
    if (color_type == PNG_COLOR_TYPE_GRAY ||
fukasawa e60969
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
fukasawa e60969
        png_set_gray_to_rgb(png_ptr);
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* unlike the example in the libpng documentation, we have *no* idea where
fukasawa e60969
     * this file may have come from--so if it doesn't have a file gamma, don't
fukasawa e60969
     * do any correction ("do no harm") */
fukasawa e60969
fukasawa e60969
    if (png_get_gAMA(png_ptr, info_ptr, &gamma))
fukasawa e60969
        png_set_gamma(png_ptr, display_exponent, gamma);
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* all transformations have been registered; now update info_ptr data,
fukasawa e60969
     * get rowbytes and channels, and allocate image memory */
fukasawa e60969
fukasawa e60969
    png_read_update_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
    *pRowbytes = rowbytes = png_get_rowbytes(png_ptr, info_ptr);
fukasawa e60969
    *pChannels = (int)png_get_channels(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
    if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
        return NULL;
fukasawa e60969
    }
fukasawa e60969
    if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
        free(image_data);
fukasawa e60969
        image_data = NULL;
fukasawa e60969
        return NULL;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    Trace((stderr, "readpng_get_image:  channels = %d, rowbytes = %ld, height = %ld\n",
fukasawa e60969
        *pChannels, rowbytes, height));
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* set the individual row_pointers to point at the correct offsets */
fukasawa e60969
fukasawa e60969
    for (i = 0;  i < height;  ++i)
fukasawa e60969
        row_pointers[i] = image_data + i*rowbytes;
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* now we can go ahead and just read the whole image */
fukasawa e60969
fukasawa e60969
    png_read_image(png_ptr, row_pointers);
fukasawa e60969
fukasawa e60969
fukasawa e60969
    /* and we're done!  (png_read_end() can be omitted if no processing of
fukasawa e60969
     * post-IDAT text/time/etc. is desired) */
fukasawa e60969
fukasawa e60969
    free(row_pointers);
fukasawa e60969
    row_pointers = NULL;
fukasawa e60969
fukasawa e60969
    png_read_end(png_ptr, NULL);
fukasawa e60969
fukasawa e60969
    return image_data;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
fukasawa e60969
void readpng_cleanup(int free_image_data)
fukasawa e60969
{
fukasawa e60969
    if (free_image_data && image_data) {
fukasawa e60969
        free(image_data);
fukasawa e60969
        image_data = NULL;
fukasawa e60969
    }
fukasawa e60969
fukasawa e60969
    if (png_ptr && info_ptr) {
fukasawa e60969
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
        png_ptr = NULL;
fukasawa e60969
        info_ptr = NULL;
fukasawa e60969
    }
fukasawa e60969
}