// pngimage.cpp -- image data from png file // // Written by Frederic Bouvier, started March 2004. // // Copyright (C) 2004 Frederic Bouvier - fredb@users.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: pngimage.cpp,v 1.2 2005/05/09 07:02:12 fredb Exp $ #include "pngimage.hpp" #include #include #include FGSD_PNGImage::FGSD_PNGImage( bool __reverted ) : _reverted( __reverted ) { } FGSD_PNGImage::~FGSD_PNGImage () { } bool FGSD_PNGImage::load (const char *__fileName) { bool ret = false; FILE *infile = fopen( __fileName, "rb" ); if ( infile != 0 ) { char *header[8]; fread( header, 1, 8, infile ); if ( png_sig_cmp( (png_bytep)header, 0, 8) == 0 ) { // allocate some structures png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); if ( png_ptr ) { png_infop info_ptr = png_create_info_struct( png_ptr ); if ( info_ptr ) { png_infop end_info = png_create_info_struct( png_ptr ); if ( end_info ) { // initialize IO png_init_io(png_ptr, infile); png_set_sig_bytes( png_ptr, 8 ); png_read_info( png_ptr, info_ptr ); double gamma; if ( png_get_gAMA(png_ptr, info_ptr, &gamma) ) { png_set_gamma( png_ptr, 2.0, gamma ); } else { png_set_gamma( png_ptr, 2.0, 0.45455 ); } if ( png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE ) png_set_expand( png_ptr ); png_read_update_info( png_ptr, info_ptr ); _width = png_get_image_width (png_ptr, info_ptr); _height = png_get_image_height(png_ptr, info_ptr); // allocate image chunk png_bytep *rows = new png_bytep[_height]; allocImage( _width, _height, 3 ); for ( unsigned int i = 0; i < _height; i++ ) { if ( _reverted ) rows[_height - i - 1] = (png_bytep)(_buffer + i * (_width) * 3); else rows[i] = (png_bytep)(_buffer + i * (_width) * 3); } png_read_image(png_ptr, rows); delete[] rows; png_read_end( png_ptr, end_info ); png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ); } } } } fclose(infile); ret = true; } return ret; }