00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef IMAGE_HPP
00023 #define IMAGE_HPP
00024 #include <vector>
00025 #include <exception>
00026
00034 namespace Fractale
00035 {
00036
00041 class MauvaiseCoordonnee : public std::exception
00042 {
00043 public :
00044 const char * what () const throw ()
00045 {
00046 return "les coordonnees i,j ne sont pas dans l'image";
00047 }
00048 };
00049
00054 class MauvaiseTailleImage : public std::exception
00055 {
00056 public :
00057 const char * what () const throw ()
00058 {
00059 return "les images n'ont pas la meme taille";
00060 }
00061 };
00062
00063
00069 template<typename PIX>
00070 class Image
00071 {
00072
00073
00074 unsigned int m_h;
00075 unsigned int m_l;
00076 std::vector<PIX> m_buffer;
00083 void PredIJ(unsigned int i,unsigned int j) const
00084 {
00085 if( !( i < m_h && j < m_l))
00086 {
00087 throw MauvaiseCoordonnee();
00088 };
00089 }
00090
00096 void PredImgTaille(const Image &I1,const Image & I2)
00097 {
00098 if( I1.m_h != I2.m_h || I1.m_l != I2.m_l)
00099 {
00100 throw MauvaiseTailleImage();
00101 };
00102 }
00103 public :
00107 typedef typename std::vector<PIX>::iterator iterator;
00108
00112 typedef typename std::vector<PIX>::const_iterator const_iterator;
00113
00120 Image(unsigned int h,unsigned int l)
00121 :m_h(h),m_l(l),m_buffer(h*l)
00122 {
00123 }
00127 Image(const Image & I)
00128 :m_h(I.m_h),m_l(I.m_l),m_buffer(I.m_buffer)
00129 {
00130 }
00131
00138 void resize(unsigned int h,unsigned int l)
00139 {
00140 m_h = h;
00141 m_l = l;
00142 m_buffer.resize(h * l);
00143 }
00144
00150 bool isValid(unsigned int i,unsigned int j)
00151 {
00152 return i < m_h && j < m_l;
00153 }
00154
00160 PIX & pixel(unsigned int i, unsigned int j)
00161 {
00162 PredIJ(i,j);
00163 return m_buffer[i*m_l + j];
00164 }
00165
00171 const PIX & pixel(unsigned int i, unsigned int j) const
00172 {
00173 PredIJ(i,j);
00174 return m_buffer[i*m_l + j];
00175 }
00176
00180 unsigned int hauteur() const {return m_h;}
00181
00185 unsigned int largeur() const {return m_l;}
00186
00190 iterator begin() {return m_buffer.begin();}
00191
00195 iterator end() {return m_buffer.end();}
00196
00200 const_iterator begin()const {return m_buffer.begin();}
00201
00205 const_iterator end()const {return m_buffer.end();}
00206
00214 Image<PIX> operator+ (const Image & I) const
00215 {
00216
00217 PredImgTaille(*this,I);
00218 Image<PIX> tmp(*this);
00219 tmp += I;
00220 return tmp;
00221 }
00222
00231 Image<PIX> & operator+= (const Image & I)
00232 {
00233
00234 PredImgTaille(*this,I);
00235
00236 typename Image<PIX>::iterator it = begin();
00237 typename Image<PIX>::iterator end = end();
00238 typename Image<PIX>::const_iterator it1 = I.begin();
00239 while(it != end)
00240 {
00241 *it += *it1 ;
00242 ++it;
00243 ++it1;
00244 }
00245 return *this;
00246 }
00247
00248 };
00249 }
00250
00251 #endif