Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

provider_png.h

Go to the documentation of this file.
00001 /*
00002 
00003         ------------------------------------------------------------------------
00004         PNG-SurfaceProvider Class
00005         written by Karsten-Olaf Laux
00006 
00007         in order to works with
00008         ClanLib, the platform independent game SDK.
00009 
00010         This file is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00011         version 2. See COPYING for details.
00012 
00013         For a total list of contributers see CREDITS.
00014 
00015         ------------------------------------------------------------------------
00016 
00017         File purpose:
00018                 PNG (.png) surface provider.
00019 */
00020 
00022 
00023 #ifndef header_PNGprovider
00024 #define header_PNGprovider
00025 
00026 #include "../Display/Display/pixelformat.h"
00027 #include "../Display/SurfaceProviders/generic_surfaceprovider.h"
00028 #include "../Core/System/clanstring.h"
00029 #include "../Core/System/cl_assert.h"
00030 #include "../Core/IOData/inputsource.h"
00031 #include "../Core/IOData/inputsource_provider.h"
00032 
00033 #include <png.h>
00034 
00035 class CL_PNGProvider : public CL_SurfaceProvider_Generic
00036 //: Surface provider that can load PNG (.png) files.
00037 {
00038 public:
00039         CL_InputSource* get_input_source() {return input_source; };
00040         //:returns Pointer to CL_InputSource
00041         //:(used by libpng-callback CL_PNGProvider::pngread_file() )
00042 
00043         static CL_Surface* create(CL_String handle, 
00044                                   CL_InputSourceProvider* provider=NULL, 
00045                                   bool transparent=true,
00046                                   bool ignore_alphachannel=false);
00047 
00048         //: Loads the PNG file 'handle' from the inputsource provider 'provider. 
00049         //: Creates a CL_Surface using the PNG image and returns it.
00050 
00051         CL_PNGProvider(CL_String name, 
00052                          CL_InputSourceProvider *provider = NULL,
00053                          bool transparent=true,
00054                          bool ignore_alphachannel=false);
00055         //: Constructs a surface provider that can read PNG files.
00063 
00064         virtual ~CL_PNGProvider();
00065 
00066         virtual unsigned int get_pitch() const { return pitch; }
00067         //: Returns the pitch of the image (bytes per line).
00068 
00069         virtual unsigned int get_width() const { return width; }
00070         //: Returns the width of the image.
00071 
00072         virtual unsigned int get_height() const { return height; }
00073         //: Returns the height of the image.
00074         
00075         virtual unsigned int get_num_frames() const { return 1; }
00076         //: Returns the number of subsprites in the image.
00077 
00078         virtual EPixelFormat get_pixel_format() const { return pixel_format; }
00079         //: Returns the pixelformat used by the image.
00080 
00081         virtual CL_Palette *get_palette() const { return palette; }
00082         //: Returns the palette used by the image. NULL if system palette.
00083 
00084         virtual unsigned int get_src_colorkey() const { return trans_col; }
00085         //: Returns the transparency color used.
00086 
00087         virtual bool uses_src_colorkey() const { return m_uses_src_colorkey; }
00088         //: Returns whether a source colorkey is used.
00089 
00090         virtual bool is_indexed() const { return true; }
00091         //: Returns whether the target uses an indexed color mode or not.
00092         
00093         virtual unsigned int get_red_mask() const;
00094         //: Returns the red color mask used by the target.
00095 
00096         virtual unsigned int get_green_mask() const;
00097         //: Returns the green color mask by the target.
00098 
00099         virtual unsigned int get_blue_mask() const;
00100         //: Returns the blue color mask by the target.
00101 
00102         virtual unsigned int get_alpha_mask() const;
00103         //: Returns the alpha mask by the target.
00104 
00105         virtual unsigned int CL_PNGProvider::get_depth() const;
00106         //: Returns the bit depth of the surface provider
00107 
00108         virtual void* get_data() const;
00109         //: Returns the image data. Provider must be locked before pointer is valid.
00110 
00111         virtual void perform_lock();
00112         //: Locks the surface provider.
00113 
00114         virtual void perform_unlock();
00115         //: Unlocks the surface provider.
00116         
00117         static void pngread_file(png_structp png_ptr,
00118                                  png_bytep data, 
00119                                  png_size_t length)
00120         //: Callback used by libpng to retrieve the filedata. 
00121         //: (calls get_input_source()->read_uchar8())
00122           {
00123             //since this method is static, we need to know who we are ...
00124             CL_PNGProvider *instance =  (CL_PNGProvider *)png_get_io_ptr(png_ptr);
00125             //no error-checking here ....
00126             /*png_size_t i;
00127             for(i=0; i< length; i++)
00128             data[i]=instance->get_input_source()->read_uchar8();*/
00129             unsigned int read_length = instance->get_input_source ()->read(data, length);
00130             cl_assert (read_length == length);
00131           }
00132 
00133 private:
00134         CL_String filename;
00135         int locked;
00136 
00137         bool m_uses_src_colorkey;
00138         
00139         unsigned char *image;
00140         int pitch;
00141         int no_sprs;
00142         int width;
00143         int height;
00144 
00145         EPixelFormat pixel_format;
00146 
00147         bool transparent;
00148         bool ignore_alphachannel;
00149 
00150         CL_Palette* palette;
00151         bool indexed;
00152         int trans_col;
00153         int color_type;
00154         unsigned char trans_redcol, trans_greencol, trans_bluecol;
00155 
00156         void read_data();
00157         void read_data_rgb();
00158         void read_data_rgba();
00159         void read_data_grayscale();
00160         void read_data_grayscale_alpha();
00161         void read_data_palette();
00162 
00163         CL_InputSourceProvider *provider;
00164         CL_InputSource *input_source;
00165 
00166         //PNGlib stuff:
00167         png_structp png_ptr;
00168         png_infop info_ptr;
00169         png_infop end_info;
00170 
00171 };
00172 
00173 class CL_SetupPNG
00174 {
00175 public:
00176         static void init();
00177         static void deinit();
00178 };
00179 
00180 #endif

Generated at Wed Apr 4 19:54:02 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001