Claw  1.7.3
image.ipp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2009 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien_jorge@yahoo.fr
24 */
30 #include <claw/assert.hpp>
31 
32 /*----------------------------------------------------------------------------*/
39 {
40  return super::operator[](i);
41 } // image::scanline::operator[]()
42 
43 /*----------------------------------------------------------------------------*/
49 inline claw::graphic::image::scanline::operator[](unsigned int i) const
50 {
51  return super::operator[](i);
52 } // image::scanline::operator[]()
53 
54 
55 
56 
57 /*----------------------------------------------------------------------------*/
61 template<typename Image, typename Pixel>
63  : m_owner(NULL), m_pos(0, 0)
64 {
65  CLAW_POSTCOND(is_final());
66 } // image::base_iterator::base_iterator()
67 
68 /*----------------------------------------------------------------------------*/
75 template<typename Image, typename Pixel>
77 ( image_type& owner, unsigned int x, unsigned int y )
78  : m_owner(&owner), m_pos(x, y)
79 {
80 
81 } // image::base_iterator::base_iterator()
82 
83 /*----------------------------------------------------------------------------*/
88 template<typename Image, typename Pixel>
89 inline bool
91 ( const self_type& that ) const
92 {
93  if ( is_final() && that.is_final() )
94  return true;
95  else if ( m_owner == that.m_owner )
96  return m_pos == that.m_pos;
97  else
98  return false;
99 } // image::base_iterator::operator==()
100 
101 /*----------------------------------------------------------------------------*/
106 template<typename Image, typename Pixel>
107 inline bool
108 claw::graphic::image::base_iterator<Image, Pixel>::operator!=
109 ( const self_type& that ) const
110 {
111  return !(*this == that);
112 } // image::base_iterator::operator!=()
113 
114 /*----------------------------------------------------------------------------*/
119 template<typename Image, typename Pixel>
120 inline bool
121 claw::graphic::image::base_iterator<Image, Pixel>::operator<
122  ( const self_type& that ) const
123 {
124  if ( this->m_pos.y == that.m_pos.y)
125  return this->m_pos.x < that.m_pos.x;
126  else
127  return this->m_pos.y < that.m_pos.y;
128 } // image::base_iterator::operator<()
129 
130 /*----------------------------------------------------------------------------*/
135 template<typename Image, typename Pixel>
136 inline bool
137 claw::graphic::image::base_iterator<Image, Pixel>::operator>
138 ( const self_type& that ) const
139 {
140  return that < *this;
141 } // image::base_iterator::operator>()
142 
143 /*----------------------------------------------------------------------------*/
149 template<typename Image, typename Pixel>
150 inline bool
151 claw::graphic::image::base_iterator<Image, Pixel>::operator<=
152 ( const self_type& that ) const
153 {
154  return !(*this > that);
155 } // image::base_iterator::operator<=()
156 
157 /*----------------------------------------------------------------------------*/
163 template<typename Image, typename Pixel>
164 inline bool
165 claw::graphic::image::base_iterator<Image, Pixel>::operator>=
166 ( const self_type& that ) const
167 {
168  return !(*this < that);
169 } // image::base_iterator::operator>=()
170 
171 /*----------------------------------------------------------------------------*/
176 template<typename Image, typename Pixel>
179 {
180  if (n < 0)
181  return *this -= -n;
182  else
183  {
184  CLAW_PRECOND( !is_final() );
185 
186  unsigned int n_y = n / m_owner->width();
187  unsigned int n_x = n % m_owner->width();
188 
189  m_pos.x += n_x;
190  m_pos.y += n_y;
191 
192  return *this;
193  }
194 } // image::base_iterator::operator+=()
195 
196 /*----------------------------------------------------------------------------*/
201 template<typename Image, typename Pixel>
204 {
205  if (n < 0)
206  return *this += -n;
207  else
208  {
209  CLAW_PRECOND( m_owner );
210 
211  unsigned int n_y = n / m_owner->width();
212  unsigned int n_x = n % m_owner->width();
213 
214  CLAW_PRECOND( m_pos.x >= n_x );
215  CLAW_PRECOND( m_pos.y >= n_y );
216 
217  m_pos.x -= n_x;
218  m_pos.y -= n_y;
219 
220  return *this;
221  }
222 } // image::base_iterator::operator-=()
223 
224 /*----------------------------------------------------------------------------*/
229 template<typename Image, typename Pixel>
232 {
233  self_type that(*this);
234 
235  return that += n;
236 } // image::base_iterator::operator+()
237 
238 /*----------------------------------------------------------------------------*/
243 template<typename Image, typename Pixel>
246 {
247  self_type that(*this);
248 
249  return that -= n;
250 } // image::base_iterator::operator-()
251 
252 /*----------------------------------------------------------------------------*/
258 template<typename ImageT, typename PixelT>
260 operator+
261 ( int n,
262  const typename
264 {
265  return self + n;
266 } // image::base_iterator::operator+()
267 
268 /*----------------------------------------------------------------------------*/
273 template<typename Image, typename Pixel>
274 inline
276 claw::graphic::image::base_iterator<Image, Pixel>::operator-
277 ( const self_type& that ) const
278 {
279  CLAW_PRECOND( is_final() || that.is_final() || (m_owner == that.m_owner) );
280 
281  if ( that.is_final() )
282  {
283  if ( is_final() )
284  return 0;
285  else
286  return -(m_owner->height() - m_pos.y) * m_owner->width() - m_pos.x;
287  }
288  else if ( is_final() )
289  return (that.m_owner->height() - that.m_pos.y) * that.m_owner->width()
290  + that.m_pos.x;
291  else
292  return m_pos.y * m_owner->width() + m_pos.x
293  - that.m_pos.y * that.m_owner->width() + that.m_pos.x;
294 } // image::base_iterator::operator-()
295 
296 /*----------------------------------------------------------------------------*/
300 template<typename Image, typename Pixel>
303 {
304  CLAW_PRECOND( !is_final() );
305 
306  ++m_pos.x;
307 
308  if ( m_pos.x == m_owner->width() )
309  {
310  m_pos.x = 0;
311  ++m_pos.y;
312  }
313 
314  return *this;
315 } // image::base_iterator::operator++()
316 
317 /*----------------------------------------------------------------------------*/
321 template<typename Image, typename Pixel>
324 {
325  self_type that(*this);
326  ++(*this);
327  return that;
328 } // image::base_iterator::operator++() [postincrement]
329 
330 /*----------------------------------------------------------------------------*/
334 template<typename Image, typename Pixel>
337 {
338  CLAW_PRECOND( !is_final() );
339  CLAW_PRECOND( (m_pos.y > 0) || (m_pos.x > 0) );
340 
341  if ( m_pos.x == 0 )
342  {
343  m_pos.x = m_owner->width() - 1;
344  --m_pos.y;
345  }
346  else
347  --m_pos.x;
348 
349  return *this;
350 } // image::base_iterator::operator--()
351 
352 /*----------------------------------------------------------------------------*/
356 template<typename Image, typename Pixel>
359 {
360  self_type that(*this);
361  --(*this);
362  return that;
363 } // image::base_iterator::operator--() [postdecrement]
364 
365 /*----------------------------------------------------------------------------*/
369 template<typename Image, typename Pixel>
372 {
373  CLAW_PRECOND( !is_final() );
374 
375  return (*m_owner)[m_pos.y][m_pos.x];
376 } // image::base_iterator::operator*()
377 
378 /*----------------------------------------------------------------------------*/
382 template<typename Image, typename Pixel>
385 {
386  CLAW_PRECOND( !is_final() );
387 
388  return &(*m_owner)[m_pos.y][m_pos.x];
389 } // image::base_iterator::operator->()
390 
391 /*----------------------------------------------------------------------------*/
396 template<typename Image, typename Pixel>
399 {
400  return *(*this + n);
401 } // image::base_iterator::operator[]()
402 
403 /*----------------------------------------------------------------------------*/
407 template<typename Image, typename Pixel>
408 inline bool
410 {
411  if ( !m_owner )
412  return true;
413  else if ( m_pos.y >= m_owner->height() )
414  return true;
415  else if ( m_pos.y == m_owner->height() - 1 )
416  return m_pos.x >= m_owner->width();
417  else
418  return false;
419 } // image::base_iterator::is_final()
420 
421 
422 
423 
424 /*----------------------------------------------------------------------------*/
430 {
431  return m_data[i];
432 } // image::operator[]()
433 
434 /*----------------------------------------------------------------------------*/
438 inline const claw::graphic::image::scanline&
439 claw::graphic::image::operator[](unsigned int i) const
440 {
441  return m_data[i];
442 } // image::operator[]() [const]