gKitGL
Image.h
00001 
00002 #ifndef _IMAGE_H
00003 #define _IMAGE_H
00004 
00005 #include <cassert>
00006 #include <cstdlib>
00007 #include <cstring>
00008 #include <string>
00009 
00010 #include "IOResource.h"
00011 
00012 namespace gk {
00013 
00014 //! representation d'un pixel 'classique' rgba.
00015 struct Pixel
00016 {
00017     //! composantes publiques.
00018     unsigned char r, g, b, a;
00019     
00020     typedef unsigned char type;
00021     enum { d= 4 };
00022     
00023     static
00024     bool isColorPixel( )
00025     {
00026         return true;
00027     }
00028     
00029     static 
00030     bool isHdrPixel( )
00031     {
00032         return false;
00033     }
00034     
00035     //! par defaut, un pixel noir (opaque).
00036     Pixel( )
00037         :
00038         r(0), g(0), b(0), a(255)
00039     {
00040         assert(sizeof(Pixel) == sizeof(unsigned char[4]));
00041     }
00042     
00043     //! cree un pixel vvva (gris).
00044     Pixel( const unsigned char v, const unsigned char _a= 255u )
00045         :
00046         r(v), g(v), b(v), a(_a)
00047     {
00048         assert(sizeof(Pixel) == sizeof(unsigned char[4]));
00049     }
00050     
00051     //! cree un pixel rgba.
00052     Pixel( const unsigned char _r, const unsigned char _g, const unsigned char _b, const unsigned char _a= 255u )
00053         :
00054         r(_r), g(_g), b(_b), a(_a)
00055     {
00056         assert(sizeof(Pixel) == sizeof(unsigned char[4]));
00057     }
00058 
00059     ~Pixel( ) {}
00060 };
00061 
00062 //! representation d'un pixel 'hdr' rgba.
00063 struct HDRPixel
00064 {
00065     //! composantes publiques.
00066     float r, g, b, a;
00067     
00068     typedef float type;
00069     enum { d= 4 };
00070 
00071     static
00072     bool isColorPixel( )
00073     {
00074         return false;
00075     }
00076     
00077     static 
00078     bool isHdrPixel( )
00079     {
00080         return true;
00081     }
00082     
00083     //! par defaut, pixel noir.
00084     HDRPixel( )
00085         :
00086         r(0.f) ,g(0.f), b(0.f), a(1.f)
00087     {
00088         assert(sizeof(HDRPixel) == sizeof(float[4]));
00089     }
00090     
00091     //! cree un pixel hdr vvva (gris).
00092     HDRPixel( const float v, const float _a= 1.f )
00093         :
00094         r(v), g(v), b(v), a(_a)
00095     {
00096         assert(sizeof(HDRPixel) == sizeof(float[4]));
00097     }
00098     
00099     //! cree un pixel hdr rgba.
00100     HDRPixel( const float _r, const float _g, const float _b, const float _a= 1.f )
00101         :
00102         r(_r), g(_g), b(_b), a(_a)
00103     {
00104         assert(sizeof(HDRPixel) == sizeof(float[4]));
00105     }
00106     
00107     //! cree un pixel hdr rgba [0 .. 1] a partir d'un pixel rgba [0 .. 255].
00108     HDRPixel( const Pixel& color )
00109         :
00110         r((float) color.r / 255.f), 
00111         g((float) color.g / 255.f), 
00112         b((float) color.b / 255.f),
00113         a((float) color.a / 255.f)
00114     {}
00115     
00116     ~HDRPixel( ) {}
00117 };
00118 
00119 
00120 //! utilisation interne. representation d'une image, parametree par le type de pixel, cf. gk::Pixel et gk::HDRPixel.
00121 //! utiliser gk::Image et gk::HDRImage.
00122 template< class T >
00123 class TImage : public IOResource
00124 {
00125     // non copyable
00126     TImage( const TImage& );
00127     TImage& operator=( const TImage& );
00128     
00129     T *m_data;
00130     unsigned int m_width;
00131     unsigned int m_height;
00132 
00133 public:
00134     typedef T pixel;
00135     
00136     //! construction d'une image de dimension width x height.
00137     TImage( const unsigned int width, const unsigned int height, const void *data= NULL ) 
00138         :
00139         m_width(width), 
00140         m_height(height)
00141     {
00142         m_data= new T[width * height];
00143         assert(m_data != NULL);
00144         
00145         if(data == NULL)
00146             return;
00147         
00148         memcpy(m_data, data, width * height * sizeof(T));
00149     }
00150 
00151     //! destructeur.
00152     ~TImage( )
00153     {
00154         delete [] m_data;
00155     }
00156 
00157     bool isColorImage( ) const
00158     {
00159         return T::isColorPixel();
00160     }
00161     
00162     bool isHdrImage( ) const
00163     {
00164         return T::isHdrPixel();
00165     }    
00166     
00167     //! renvoie les donnees brutes de l'image.
00168     void *data( )
00169     {
00170         return m_data;
00171     }
00172 
00173     //! renvoie les donnees brutes de l'image. 
00174     const void *data( ) const
00175     {
00176         return m_data;
00177     }
00178 
00179     //! renvoie la largeur de l'image.
00180     unsigned int width( ) const
00181     {
00182         return m_width;
00183     }
00184 
00185     //! renvoie la hauteur de l'image.
00186     unsigned int height( ) const
00187     {
00188         return m_height;
00189     }
00190 
00191     //! remplace la couleur d'un pixel.
00192     void setPixel( const unsigned int x, const unsigned int y, const T& color )
00193     {
00194         assert(x < m_width);
00195         assert(y < m_height);
00196         
00197         const unsigned int id= y * m_width + x;
00198         m_data[id]= color;
00199     }
00200 
00201     T operator( ) ( const unsigned int x, const unsigned int y ) const
00202     {
00203         assert(x < m_width);
00204         assert(y < m_height);
00205         
00206         const unsigned int id= y * m_width + x;
00207         return m_data[id];
00208     }
00209     
00210     T& operator( ) ( const unsigned int x, const unsigned int y )
00211     {
00212         assert(x < m_width);
00213         assert(y < m_height);
00214         
00215         const unsigned int id= y * m_width + x;
00216         return m_data[id];
00217     }
00218     
00219     //! renvoie la couleur d'un pixel.
00220     const T& getPixel( const unsigned int x, const unsigned int y ) const
00221     {
00222         assert(x < m_width);
00223         assert(y < m_height);
00224         
00225         const unsigned int id= y * m_width + x;
00226         return m_data[id];
00227     }
00228 };
00229 
00230 //! declaration d'une image hdr, pixels rgba.
00231 typedef TImage<HDRPixel> HDRImage;
00232 
00233 //! declaration d'une image 'classique', avec pixels rgba.
00234 typedef TImage<Pixel> Image;
00235 
00236 }       // namespace
00237 
00238 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerator Friends