claw 1.9.0
 
Loading...
Searching...
No Matches
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/*----------------------------------------------------------------------------*/
50{
51 return super::operator[](i);
52} // image::scanline::operator[]()
53
54
55
56
57/*----------------------------------------------------------------------------*/
61template<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/*----------------------------------------------------------------------------*/
75template<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/*----------------------------------------------------------------------------*/
88template<typename Image, typename Pixel>
89inline 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/*----------------------------------------------------------------------------*/
106template<typename Image, typename Pixel>
107inline bool
109( const self_type& that ) const
110{
111 return !(*this == that);
112} // image::base_iterator::operator!=()
113
114/*----------------------------------------------------------------------------*/
119template<typename Image, typename Pixel>
120inline bool
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/*----------------------------------------------------------------------------*/
135template<typename Image, typename Pixel>
136inline bool
138( const self_type& that ) const
140 return that < *this;
141} // image::base_iterator::operator>()
143/*----------------------------------------------------------------------------*/
144/**
145 * \brief Tell if the current iterator is before an other, or on the same
146 * address.
147 * \param that The other operand.
148 */
149template<typename Image, typename Pixel>
150inline bool
152( const self_type& that ) const
154 return !(*this > that);
155} // image::base_iterator::operator<=()
156
157/*----------------------------------------------------------------------------*/
163template<typename Image, typename Pixel>
164inline bool
166( const self_type& that ) const
168 return !(*this < that);
169} // image::base_iterator::operator>=()
170
171/*----------------------------------------------------------------------------*/
172/**
173 * \brief Move the iterator.
174 * \param n Number of steps of the move.
175 */
176template<typename Image, typename Pixel>
177inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
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/*----------------------------------------------------------------------------*/
201template<typename Image, typename Pixel>
202inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
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/*----------------------------------------------------------------------------*/
229template<typename Image, typename Pixel>
230inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
232{
233 self_type that(*this);
234
235 return that += n;
236} // image::base_iterator::operator+()
237
238/*----------------------------------------------------------------------------*/
243template<typename Image, typename Pixel>
244inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
246{
247 self_type that(*this);
248
249 return that -= n;
250} // image::base_iterator::operator-()
251
252/*----------------------------------------------------------------------------*/
258template<typename ImageT, typename PixelT>
259inline typename claw::graphic::image::base_iterator<ImageT, PixelT>::self_type
260operator+
261( int n,
262 const typename
263 claw::graphic::image::base_iterator<ImageT, PixelT>::self_type& self )
264{
265 return self + n;
266} // image::base_iterator::operator+()
267
268/*----------------------------------------------------------------------------*/
273template<typename Image, typename Pixel>
274inline
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/*----------------------------------------------------------------------------*/
300template<typename Image, typename Pixel>
301inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
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/*----------------------------------------------------------------------------*/
321template<typename Image, typename Pixel>
322inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
324{
325 self_type that(*this);
326 ++(*this);
327 return that;
328} // image::base_iterator::operator++() [postincrement]
329
330/*----------------------------------------------------------------------------*/
334template<typename Image, typename Pixel>
335inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
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/*----------------------------------------------------------------------------*/
356template<typename Image, typename Pixel>
357inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
359{
360 self_type that(*this);
361 --(*this);
362 return that;
363} // image::base_iterator::operator--() [postdecrement]
364
365/*----------------------------------------------------------------------------*/
369template<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/*----------------------------------------------------------------------------*/
382template<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/*----------------------------------------------------------------------------*/
396template<typename Image, typename Pixel>
399{
400 return *(*this + n);
401} // image::base_iterator::operator[]()
402
403/*----------------------------------------------------------------------------*/
407template<typename Image, typename Pixel>
408inline bool
409claw::graphic::image::base_iterator<Image, Pixel>::is_final() const
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/*----------------------------------------------------------------------------*/
440{
441 return m_data[i];
442} // image::operator[]() [const]
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:94
#define CLAW_POSTCOND(b)
Abort the program if a postcondition is not true.
Definition assert.hpp:96
Base class for iterators on an image.
Definition image.hpp:108
self_type & operator+=(int n)
Move the iterator.
Definition image.ipp:178
self_type operator+(int n) const
Get an iterator at a specific distance of the current iterator.
Definition image.ipp:231
pixel_type * pointer
The type of the pointers to the values accesssed by the iterator.
Definition image.hpp:129
self_type & operator-=(int n)
Move the iterator.
Definition image.ipp:203
self_type & operator++()
Preincrement.
Definition image.ipp:302
pointer operator->() const
Get a pointer on the pointed pixel.
Definition image.ipp:384
self_type operator-(int n) const
Get an iterator at a specific distance of the current iterator.
Definition image.ipp:245
pixel_type & reference
The type of the references to the values accesssed by the iterator.
Definition image.hpp:125
reference operator*() const
Get a reference on the pointed pixel.
Definition image.ipp:371
ptrdiff_t difference_type
The type of the distance between two iterators.
Definition image.hpp:132
reference operator[](int n) const
Get a pixel, using the iterator like an array.
Definition image.ipp:398
self_type & operator--()
Predecrement.
Definition image.ipp:336
One line in the image.
Definition image.hpp:62
super::reference reference
Reference to a pixel..
Definition image.hpp:73
reference operator[](unsigned int i)
Get a pixel from the line.
Definition image.ipp:38
super::const_reference const_reference
Const reference to a pixel.
Definition image.hpp:76
scanline & operator[](unsigned int i)
Gets a line of the image.
Definition image.ipp:429