00001
00002 #ifndef _IMAGE_H
00003 #define _IMAGE_H
00004
00005 #include <cassert>
00006 #include <cstdlib>
00007 #include <string>
00008
00009 #include "IOResource.h"
00010
00011 namespace gk {
00012
00013
00014 struct Pixel
00015 {
00016
00017 unsigned char r, g, b, a;
00018
00019 typedef unsigned char type;
00020 enum { d= 4 };
00021
00022 static
00023 bool isColorPixel( )
00024 {
00025 return true;
00026 }
00027
00028 static
00029 bool isHdrPixel( )
00030 {
00031 return false;
00032 }
00033
00034
00035 Pixel( )
00036 :
00037 r(0), g(0), b(0), a(255)
00038 {
00039 assert(sizeof(Pixel) == sizeof(unsigned char[4]));
00040 }
00041
00042
00043 Pixel( const unsigned char _r, const unsigned char _g, const unsigned char _b, const unsigned char _a= 255u )
00044 :
00045 r(_r), g(_g), b(_b), a(_a)
00046 {
00047 assert(sizeof(Pixel) == sizeof(unsigned char[4]));
00048 }
00049
00050 ~Pixel( ) {}
00051 };
00052
00053
00054 struct HDRPixel
00055 {
00056
00057 float r, g, b, a;
00058
00059 typedef float type;
00060 enum { d= 4 };
00061
00062 static
00063 bool isColorPixel( )
00064 {
00065 return false;
00066 }
00067
00068 static
00069 bool isHdrPixel( )
00070 {
00071 return true;
00072 }
00073
00074
00075 HDRPixel( )
00076 :
00077 r(0.f) ,g(0.f), b(0.f), a(1.f)
00078 {
00079 assert(sizeof(HDRPixel) == sizeof(float[4]));
00080 }
00081
00082
00083 HDRPixel( const float _r, const float _g, const float _b, const float _a= 1.f )
00084 :
00085 r(_r), g(_g), b(_b), a(_a)
00086 {
00087 assert(sizeof(HDRPixel) == sizeof(float[4]));
00088 }
00089
00090
00091 HDRPixel( const Pixel& color )
00092 :
00093 r((float) color.r / 255.f),
00094 g((float) color.g / 255.f),
00095 b((float) color.b / 255.f),
00096 a((float) color.a / 255.f)
00097 {}
00098
00099 ~HDRPixel( ) {}
00100 };
00101
00102
00103
00104
00105 template< class T >
00106 class TImage : public IOResource
00107 {
00108
00109 TImage( const TImage& );
00110 TImage& operator=( const TImage& );
00111
00112 T *m_data;
00113 int m_width;
00114 int m_height;
00115
00116 public:
00117
00118 TImage( const int width, const int height )
00119 :
00120 m_width(width),
00121 m_height(height)
00122 {
00123 m_data= new T[width * height];
00124 assert(m_data != NULL);
00125 }
00126
00127
00128 ~TImage( )
00129 {
00130 delete [] m_data;
00131 }
00132
00133 bool isColorImage( ) const
00134 {
00135 return T::isColorPixel();
00136 }
00137
00138 bool isHdrImage( ) const
00139 {
00140 return T::isHdrPixel();
00141 }
00142
00143
00144 void *data( )
00145 {
00146 return m_data;
00147 }
00148
00149
00150 const void *data( ) const
00151 {
00152 return m_data;
00153 }
00154
00155
00156 int width( ) const
00157 {
00158 return m_width;
00159 }
00160
00161
00162 int height( ) const
00163 {
00164 return m_height;
00165 }
00166
00167
00168 void setPixel( const int x, const int y, const T& color )
00169 {
00170 assert(x >= 0 && x < m_width);
00171 assert(y >= 0 && y < m_height);
00172
00173
00174 const unsigned int id= y * m_width + x;
00175 m_data[id]= color;
00176 }
00177
00178
00179 const T& getPixel( const int x, const int y ) const
00180 {
00181 assert(x >= 0 && x < m_width);
00182 assert(y >= 0 && y < m_height);
00183
00184
00185 const unsigned int id= y * m_width + x;
00186 return m_data[id];
00187 }
00188 };
00189
00190
00191 typedef TImage<HDRPixel> HDRImage;
00192
00193
00194 typedef TImage<Pixel> Image;
00195
00196 }
00197
00198 #endif