Claw
1.7.3
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2009 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien_jorge@yahoo.fr 00024 */ 00030 #include <claw/assert.hpp> 00031 00032 /*----------------------------------------------------------------------------*/ 00037 claw::graphic::image::scanline::reference 00038 inline claw::graphic::image::scanline::operator[](unsigned int i) 00039 { 00040 return super::operator[](i); 00041 } // image::scanline::operator[]() 00042 00043 /*----------------------------------------------------------------------------*/ 00048 claw::graphic::image::scanline::const_reference 00049 inline claw::graphic::image::scanline::operator[](unsigned int i) const 00050 { 00051 return super::operator[](i); 00052 } // image::scanline::operator[]() 00053 00054 00055 00056 00057 /*----------------------------------------------------------------------------*/ 00061 template<typename Image, typename Pixel> 00062 inline claw::graphic::image::base_iterator<Image, Pixel>::base_iterator() 00063 : m_owner(NULL), m_pos(0, 0) 00064 { 00065 CLAW_POSTCOND(is_final()); 00066 } // image::base_iterator::base_iterator() 00067 00068 /*----------------------------------------------------------------------------*/ 00075 template<typename Image, typename Pixel> 00076 inline claw::graphic::image::base_iterator<Image, Pixel>::base_iterator 00077 ( image_type& owner, unsigned int x, unsigned int y ) 00078 : m_owner(&owner), m_pos(x, y) 00079 { 00080 00081 } // image::base_iterator::base_iterator() 00082 00083 /*----------------------------------------------------------------------------*/ 00088 template<typename Image, typename Pixel> 00089 inline bool 00090 claw::graphic::image::base_iterator<Image, Pixel>::operator== 00091 ( const self_type& that ) const 00092 { 00093 if ( is_final() && that.is_final() ) 00094 return true; 00095 else if ( m_owner == that.m_owner ) 00096 return m_pos == that.m_pos; 00097 else 00098 return false; 00099 } // image::base_iterator::operator==() 00100 00101 /*----------------------------------------------------------------------------*/ 00106 template<typename Image, typename Pixel> 00107 inline bool 00108 claw::graphic::image::base_iterator<Image, Pixel>::operator!= 00109 ( const self_type& that ) const 00110 { 00111 return !(*this == that); 00112 } // image::base_iterator::operator!=() 00113 00114 /*----------------------------------------------------------------------------*/ 00119 template<typename Image, typename Pixel> 00120 inline bool 00121 claw::graphic::image::base_iterator<Image, Pixel>::operator< 00122 ( const self_type& that ) const 00123 { 00124 if ( this->m_pos.y == that.m_pos.y) 00125 return this->m_pos.x < that.m_pos.x; 00126 else 00127 return this->m_pos.y < that.m_pos.y; 00128 } // image::base_iterator::operator<() 00129 00130 /*----------------------------------------------------------------------------*/ 00135 template<typename Image, typename Pixel> 00136 inline bool 00137 claw::graphic::image::base_iterator<Image, Pixel>::operator> 00138 ( const self_type& that ) const 00139 { 00140 return that < *this; 00141 } // image::base_iterator::operator>() 00142 00143 /*----------------------------------------------------------------------------*/ 00149 template<typename Image, typename Pixel> 00150 inline bool 00151 claw::graphic::image::base_iterator<Image, Pixel>::operator<= 00152 ( const self_type& that ) const 00153 { 00154 return !(*this > that); 00155 } // image::base_iterator::operator<=() 00156 00157 /*----------------------------------------------------------------------------*/ 00163 template<typename Image, typename Pixel> 00164 inline bool 00165 claw::graphic::image::base_iterator<Image, Pixel>::operator>= 00166 ( const self_type& that ) const 00167 { 00168 return !(*this < that); 00169 } // image::base_iterator::operator>=() 00170 00171 /*----------------------------------------------------------------------------*/ 00176 template<typename Image, typename Pixel> 00177 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type& 00178 claw::graphic::image::base_iterator<Image, Pixel>::operator+=( int n ) 00179 { 00180 if (n < 0) 00181 return *this -= -n; 00182 else 00183 { 00184 CLAW_PRECOND( !is_final() ); 00185 00186 unsigned int n_y = n / m_owner->width(); 00187 unsigned int n_x = n % m_owner->width(); 00188 00189 m_pos.x += n_x; 00190 m_pos.y += n_y; 00191 00192 return *this; 00193 } 00194 } // image::base_iterator::operator+=() 00195 00196 /*----------------------------------------------------------------------------*/ 00201 template<typename Image, typename Pixel> 00202 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type& 00203 claw::graphic::image::base_iterator<Image, Pixel>::operator-=( int n ) 00204 { 00205 if (n < 0) 00206 return *this += -n; 00207 else 00208 { 00209 CLAW_PRECOND( m_owner ); 00210 00211 unsigned int n_y = n / m_owner->width(); 00212 unsigned int n_x = n % m_owner->width(); 00213 00214 CLAW_PRECOND( m_pos.x >= n_x ); 00215 CLAW_PRECOND( m_pos.y >= n_y ); 00216 00217 m_pos.x -= n_x; 00218 m_pos.y -= n_y; 00219 00220 return *this; 00221 } 00222 } // image::base_iterator::operator-=() 00223 00224 /*----------------------------------------------------------------------------*/ 00229 template<typename Image, typename Pixel> 00230 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type 00231 claw::graphic::image::base_iterator<Image, Pixel>::operator+( int n ) const 00232 { 00233 self_type that(*this); 00234 00235 return that += n; 00236 } // image::base_iterator::operator+() 00237 00238 /*----------------------------------------------------------------------------*/ 00243 template<typename Image, typename Pixel> 00244 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type 00245 claw::graphic::image::base_iterator<Image, Pixel>::operator-( int n ) const 00246 { 00247 self_type that(*this); 00248 00249 return that -= n; 00250 } // image::base_iterator::operator-() 00251 00252 /*----------------------------------------------------------------------------*/ 00258 template<typename ImageT, typename PixelT> 00259 inline typename claw::graphic::image::base_iterator<ImageT, PixelT>::self_type 00260 operator+ 00261 ( int n, 00262 const typename 00263 claw::graphic::image::base_iterator<ImageT, PixelT>::self_type& self ) 00264 { 00265 return self + n; 00266 } // image::base_iterator::operator+() 00267 00268 /*----------------------------------------------------------------------------*/ 00273 template<typename Image, typename Pixel> 00274 inline 00275 typename claw::graphic::image::base_iterator<Image, Pixel>::difference_type 00276 claw::graphic::image::base_iterator<Image, Pixel>::operator- 00277 ( const self_type& that ) const 00278 { 00279 CLAW_PRECOND( is_final() || that.is_final() || (m_owner == that.m_owner) ); 00280 00281 if ( that.is_final() ) 00282 { 00283 if ( is_final() ) 00284 return 0; 00285 else 00286 return -(m_owner->height() - m_pos.y) * m_owner->width() - m_pos.x; 00287 } 00288 else if ( is_final() ) 00289 return (that.m_owner->height() - that.m_pos.y) * that.m_owner->width() 00290 + that.m_pos.x; 00291 else 00292 return m_pos.y * m_owner->width() + m_pos.x 00293 - that.m_pos.y * that.m_owner->width() + that.m_pos.x; 00294 } // image::base_iterator::operator-() 00295 00296 /*----------------------------------------------------------------------------*/ 00300 template<typename Image, typename Pixel> 00301 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type& 00302 claw::graphic::image::base_iterator<Image, Pixel>::operator++() 00303 { 00304 CLAW_PRECOND( !is_final() ); 00305 00306 ++m_pos.x; 00307 00308 if ( m_pos.x == m_owner->width() ) 00309 { 00310 m_pos.x = 0; 00311 ++m_pos.y; 00312 } 00313 00314 return *this; 00315 } // image::base_iterator::operator++() 00316 00317 /*----------------------------------------------------------------------------*/ 00321 template<typename Image, typename Pixel> 00322 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type 00323 claw::graphic::image::base_iterator<Image, Pixel>::operator++(int) 00324 { 00325 self_type that(*this); 00326 ++(*this); 00327 return that; 00328 } // image::base_iterator::operator++() [postincrement] 00329 00330 /*----------------------------------------------------------------------------*/ 00334 template<typename Image, typename Pixel> 00335 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type& 00336 claw::graphic::image::base_iterator<Image, Pixel>::operator--() 00337 { 00338 CLAW_PRECOND( !is_final() ); 00339 CLAW_PRECOND( (m_pos.y > 0) || (m_pos.x > 0) ); 00340 00341 if ( m_pos.x == 0 ) 00342 { 00343 m_pos.x = m_owner->width() - 1; 00344 --m_pos.y; 00345 } 00346 else 00347 --m_pos.x; 00348 00349 return *this; 00350 } // image::base_iterator::operator--() 00351 00352 /*----------------------------------------------------------------------------*/ 00356 template<typename Image, typename Pixel> 00357 inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type 00358 claw::graphic::image::base_iterator<Image, Pixel>::operator--(int) 00359 { 00360 self_type that(*this); 00361 --(*this); 00362 return that; 00363 } // image::base_iterator::operator--() [postdecrement] 00364 00365 /*----------------------------------------------------------------------------*/ 00369 template<typename Image, typename Pixel> 00370 inline typename claw::graphic::image::base_iterator<Image, Pixel>::reference 00371 claw::graphic::image::base_iterator<Image, Pixel>::operator*() const 00372 { 00373 CLAW_PRECOND( !is_final() ); 00374 00375 return (*m_owner)[m_pos.y][m_pos.x]; 00376 } // image::base_iterator::operator*() 00377 00378 /*----------------------------------------------------------------------------*/ 00382 template<typename Image, typename Pixel> 00383 inline typename claw::graphic::image::base_iterator<Image, Pixel>::pointer 00384 claw::graphic::image::base_iterator<Image, Pixel>::operator->() const 00385 { 00386 CLAW_PRECOND( !is_final() ); 00387 00388 return &(*m_owner)[m_pos.y][m_pos.x]; 00389 } // image::base_iterator::operator->() 00390 00391 /*----------------------------------------------------------------------------*/ 00396 template<typename Image, typename Pixel> 00397 inline typename claw::graphic::image::base_iterator<Image, Pixel>::reference 00398 claw::graphic::image::base_iterator<Image, Pixel>::operator[]( int n ) const 00399 { 00400 return *(*this + n); 00401 } // image::base_iterator::operator[]() 00402 00403 /*----------------------------------------------------------------------------*/ 00407 template<typename Image, typename Pixel> 00408 inline bool 00409 claw::graphic::image::base_iterator<Image, Pixel>::is_final() const 00410 { 00411 if ( !m_owner ) 00412 return true; 00413 else if ( m_pos.y >= m_owner->height() ) 00414 return true; 00415 else if ( m_pos.y == m_owner->height() - 1 ) 00416 return m_pos.x >= m_owner->width(); 00417 else 00418 return false; 00419 } // image::base_iterator::is_final() 00420 00421 00422 00423 00424 /*----------------------------------------------------------------------------*/ 00428 inline claw::graphic::image::scanline& 00429 claw::graphic::image::operator[](unsigned int i) 00430 { 00431 return m_data[i]; 00432 } // image::operator[]() 00433 00434 /*----------------------------------------------------------------------------*/ 00438 inline const claw::graphic::image::scanline& 00439 claw::graphic::image::operator[](unsigned int i) const 00440 { 00441 return m_data[i]; 00442 } // image::operator[]() [const]