00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 }