Blame contrib/gregbook/readpng.c

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