00001 #ifndef __IMAGE_HPP__
00002 #define __IMAGE_HPP__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <vector>
00027 #include <exception>
00028
00036 namespace Fractale
00037 {
00038
00043 class MauvaiseCoordonnee : public std::exception
00044 {
00045 public :
00046 const char * what () const throw ()
00047 {
00048 return "les coordonnees i,j ne sont pas dans l'image";
00049 }
00050 };
00051
00056 class MauvaiseTailleImage : public std::exception
00057 {
00058 public :
00059 const char * what () const throw ()
00060 {
00061 return "les images n'ont pas la meme taille";
00062 }
00063 };
00064
00065
00071 template<typename PIX>
00072 class Image
00073 {
00074
00075
00076 unsigned int m_h;
00077 unsigned int m_l;
00078 std::vector<PIX> m_buffer;
00085 void PredIJ(unsigned int i,unsigned int j) const
00086 {
00087 if( !( i < m_h && j < m_l))
00088 {
00089 throw MauvaiseCoordonnee();
00090 };
00091 }
00092
00098 void PredImgTaille(const Image &I1,const Image & I2)
00099 {
00100 if( I1.m_h != I2.m_h || I1.m_l != I2.m_l)
00101 {
00102 throw MauvaiseTailleImage();
00103 };
00104 }
00105 public :
00109 typedef typename std::vector<PIX>::iterator iterator;
00110
00114 typedef typename std::vector<PIX>::const_iterator const_iterator;
00115
00119 Image(void)
00120 :m_h(0),m_l(0),m_buffer(0)
00121 {
00122 }
00123
00130 Image(unsigned int h,unsigned int l)
00131 :m_h(h),m_l(l),m_buffer(h*l)
00132 {
00133 }
00137 Image(const Image & I)
00138 :m_h(I.m_h),m_l(I.m_l),m_buffer(I.m_buffer)
00139 {
00140 }
00141
00148 void resize(unsigned int h,unsigned int l)
00149 {
00150 m_h = h;
00151 m_l = l;
00152 m_buffer.resize(h * l);
00153 }
00154
00160 bool isValid(unsigned int i,unsigned int j)
00161 {
00162 return i < m_h && j < m_l;
00163 }
00164
00170 PIX & pixel(unsigned int i, unsigned int j)
00171 {
00172 PredIJ(i,j);
00173 return m_buffer[i*m_l + j];
00174 }
00175
00181 const PIX & pixel(unsigned int i, unsigned int j) const
00182 {
00183 PredIJ(i,j);
00184 return m_buffer[i*m_l + j];
00185 }
00186
00190 unsigned int hauteur() const {return m_h;}
00191
00195 unsigned int largeur() const {return m_l;}
00196
00200 iterator begin() {return m_buffer.begin();}
00201
00205 iterator end() {return m_buffer.end();}
00206
00210 const_iterator begin()const {return m_buffer.begin();}
00211
00215 const_iterator end()const {return m_buffer.end();}
00216
00224 Image<PIX> operator+ (const Image & I) const
00225 {
00226
00227 PredImgTaille(*this,I);
00228 Image<PIX> tmp(*this);
00229 tmp += I;
00230 return tmp;
00231 }
00232
00241 Image<PIX> & operator+= (const Image & I)
00242 {
00243
00244 PredImgTaille(*this,I);
00245
00246 typename Image<PIX>::iterator it = begin();
00247 typename Image<PIX>::iterator end = end();
00248 typename Image<PIX>::const_iterator it1 = I.begin();
00249 while(it != end)
00250 {
00251 *it += *it1 ;
00252 ++it;
00253 ++it1;
00254 }
00255 return *this;
00256 }
00257
00258 };
00259 }
00260
00261 #endif