mx::mxPng Class Reference

List of all members.

Public Member Functions

 mxPng ()
 mxPng (std::string fname)
int pngOpen (std::string fname)
int pngClose ()
SDL_Surface * LoadPNG ()


Detailed Description

loader class

Definition at line 31 of file mxpng.h.


Constructor & Destructor Documentation

mx::mxPng::mxPng (  ) 

mxPng constructor

Definition at line 25 of file mxpng.cpp.

00025 {}

mx::mxPng::mxPng ( std::string  fname  ) 

constructor

Parameters:
fname name of file to load

Definition at line 28 of file mxpng.cpp.

References pngOpen().

00029         {
00030                 pngOpen(fname);
00031 
00032         }


Member Function Documentation

SDL_Surface * mx::mxPng::LoadPNG (  ) 

LoadPNG Load the PNG File should be called after pngOpen and before pngClose

Returns:
SDL_Surface * of png graphic

Definition at line 110 of file mxpng.cpp.

Referenced by mx::mxImage::loadIMG().

00111         {
00112 
00113                 SDL_Surface *surf;
00114                 int pixel_space = 3;
00115 
00116                 if(color_type == PNG_COLOR_TYPE_RGB)
00117                 {
00118 
00119                         pixel_space = 3;
00120 
00121                 }
00122 
00123                 if(color_type == PNG_COLOR_TYPE_RGBA)
00124                         pixel_space = 4;
00125 
00126                 surf = CreateBuffer(width,height);
00127 
00128                 SDL_FillRect(surf, 0, SDL_MapRGB(surf->format, 255,255,255));
00129 
00130                 int x,y;
00131 
00132                 if (SDL_MUSTLOCK(surf))
00133                 {
00134                         if(SDL_LockSurface(surf) < 0)
00135                                 return 0;
00136 
00137                 }
00138 
00139                 Uint32 *buffer = (Uint32*) surf->pixels;
00140 
00141 
00142                 for(y=0;y<height;y++)
00143                 {
00144 
00145                         png_byte *row = row_pointers[y];
00146 
00147                         for(x=0; x<width;x++)
00148                         {
00149 
00150                                 png_byte *ptr = &(row[x*pixel_space]);
00151                                 Uint32 *uptr = &buffer[x+y*width];
00152                                 Uint32 value = SDL_MapRGB(surf->format, ptr[0], ptr[1], ptr[2]);
00153                                 *uptr = value;
00154                         }
00155 
00156 
00157                 }
00158 
00159                 if(SDL_MUSTLOCK(surf))
00160                         SDL_UnlockSurface(surf);
00161 
00162                 return surf;
00163         }

int mx::mxPng::pngClose (  ) 

pngClose close png file

Definition at line 92 of file mxpng.cpp.

Referenced by mx::mxImage::loadIMG().

00093         {
00094 
00095                 for (y=0; y<height; y++)
00096                         free(row_pointers[y]);
00097 
00098                 free(row_pointers);
00099 
00100                 // whoops forgot this
00101                 png_destroy_info_struct (png_ptr, &info_ptr);
00102                 png_destroy_read_struct (&png_ptr, 0, 0);
00103 
00104 
00105                 return 1;
00106         }

int mx::mxPng::pngOpen ( std::string  fname  ) 

pngOpen open a png graphic

Parameters:
fname file name to load

Definition at line 35 of file mxpng.cpp.

Referenced by mx::mxImage::loadIMG(), and mxPng().

00036         {
00037 
00038                 png_byte header[8];
00039 
00040 
00041                 FILE *fp = fopen(fname.c_str(), "rb");
00042                 if (!fp)
00043                         return 0;
00044                 fread(header, 1, 8, fp);
00045                 if (png_sig_cmp(header, 0, 8))
00046                         return 0;
00047 
00048 
00049 
00050                 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00051 
00052                 if (!png_ptr)
00053                         return 0;
00054 
00055                 info_ptr = png_create_info_struct(png_ptr);
00056                 if (!info_ptr)
00057                         return 0;
00058 
00059                 if (setjmp(png_jmpbuf(png_ptr)))
00060                         return 0;
00061 
00062                 png_init_io(png_ptr, fp);
00063                 png_set_sig_bytes(png_ptr, 8);
00064 
00065                 png_read_info(png_ptr, info_ptr);
00066 
00067                 width = info_ptr->width;
00068                 height = info_ptr->height;
00069                 color_type = info_ptr->color_type;
00070                 bit_depth = info_ptr->bit_depth;
00071 
00072                 number_of_passes = png_set_interlace_handling(png_ptr);
00073                 png_read_update_info(png_ptr, info_ptr);
00074 
00075 
00076                 if (setjmp(png_jmpbuf(png_ptr)))
00077                         return 0;
00078 
00079                 row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
00080                 for (y=0; y<height; y++)
00081                         row_pointers[y] = (png_byte*) malloc(info_ptr->rowbytes);
00082 
00083                 png_read_image(png_ptr, row_pointers);
00084 
00085             fclose(fp);
00086 
00087                 return 1;
00088         }


The documentation for this class was generated from the following files:

Generated on Wed Jun 10 14:52:02 2009 for libmx by  doxygen 1.5.8