PNGLoader.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2005 Dave Griffiths
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00016 
00017 #include <png.h>
00018 #include "PNGLoader.h"
00019 #include "Trace.h"
00020 
00021 using namespace Fluxus;
00022 
00023 unsigned char *PNGLoader::Load(const string &Filename, int &w, int &h, PixelFormat &pf)
00024 {
00025     unsigned char *ImageData = NULL;
00026     FILE *fp=fopen(Filename.c_str(),"rb");
00027     if (!fp) Trace::Stream<<"Couldn't open image ["<<Filename<<"]"<<endl;
00028     else
00029     {
00030         png_structp png_ptr;
00031         png_infop info_ptr;
00032         
00033         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00034         info_ptr = png_create_info_struct(png_ptr);
00035         png_init_io(png_ptr, fp);
00036         png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);      
00037         fclose(fp);
00038         
00039         ImageData = new unsigned char[png_ptr->rowbytes*png_ptr->height];
00040         int p=0;
00041         for (unsigned int row = 0; row < png_ptr->height; row++)
00042         {
00043             for (unsigned int i=0; i<png_ptr->rowbytes; i++)
00044             {
00045                 ImageData[p]=(unsigned char)info_ptr->row_pointers[row][i];
00046                 p++;
00047             }
00048         }
00049         
00050         w=png_ptr->width;
00051         h=png_ptr->height;
00052         
00053         switch (png_ptr->color_type)
00054         {
00055             case PNG_COLOR_TYPE_RGB : pf=RGB; break;
00056             case PNG_COLOR_TYPE_RGB_ALPHA : pf=RGBA; break;
00057             default : Trace::Stream<<"PNG pixel format not supported : "<<png_ptr->color_type<<endl;
00058         }
00059 
00060         png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); 
00061     }
00062     
00063     return ImageData;
00064 }

Generated on Tue Sep 4 23:22:18 2007 for The Fluxus Renderer (libfluxus) by  doxygen 1.5.1