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
00015 struct Pixel
00016 {
00017
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
00036 Pixel( )
00037 :
00038 r(0), g(0), b(0), a(255)
00039 {
00040 assert(sizeof(Pixel) == sizeof(unsigned char[4]));
00041 }
00042
00043
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
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
00063 struct HDRPixel
00064 {
00065
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
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
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
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
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
00121
00122 template< class T >
00123 class TImage : public IOResource
00124 {
00125
00126 TImage( const TImage& );
00127 TImage& operator=( const TImage& );
00128
00129 T *m_data;
00130 int m_width;
00131 int m_height;
00132
00133 public:
00134
00135 TImage( const int width, const int height, const void *data= NULL )
00136 :
00137 m_width(width),
00138 m_height(height)
00139 {
00140 m_data= new T[width * height];
00141 assert(m_data != NULL);
00142
00143 if(data == NULL)
00144 return;
00145
00146 memcpy(m_data, data, width * height * sizeof(T));
00147 }
00148
00149
00150 ~TImage( )
00151 {
00152 delete [] m_data;
00153 }
00154
00155 bool isColorImage( ) const
00156 {
00157 return T::isColorPixel();
00158 }
00159
00160 bool isHdrImage( ) const
00161 {
00162 return T::isHdrPixel();
00163 }
00164
00165
00166 void *data( )
00167 {
00168 return m_data;
00169 }
00170
00171
00172 const void *data( ) const
00173 {
00174 return m_data;
00175 }
00176
00177
00178 int width( ) const
00179 {
00180 return m_width;
00181 }
00182
00183
00184 int height( ) const
00185 {
00186 return m_height;
00187 }
00188
00189
00190 void setPixel( const int x, const int y, const T& color )
00191 {
00192 assert(x >= 0 && x < m_width);
00193 assert(y >= 0 && y < m_height);
00194
00195 const unsigned int id= y * m_width + x;
00196 m_data[id]= color;
00197 }
00198
00199
00200 const T& getPixel( const int x, const int y ) const
00201 {
00202 assert(x >= 0 && x < m_width);
00203 assert(y >= 0 && y < m_height);
00204
00205 const unsigned int id= y * m_width + x;
00206 return m_data[id];
00207 }
00208 };
00209
00210
00211 typedef TImage<HDRPixel> HDRImage;
00212
00213
00214 typedef TImage<Pixel> Image;
00215
00216 }
00217
00218 #endif