image.ipp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2009 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien_jorge@yahoo.fr
*/
#include <claw/assert.hpp>
/*----------------------------------------------------------------------------*/
claw::graphic::image::scanline::reference
inline claw::graphic::image::scanline::operator[](unsigned int i)
{
return super::operator[](i);
} // image::scanline::operator[]()
/*----------------------------------------------------------------------------*/
claw::graphic::image::scanline::const_reference
inline claw::graphic::image::scanline::operator[](unsigned int i) const
{
return super::operator[](i);
} // image::scanline::operator[]()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline claw::graphic::image::base_iterator<Image, Pixel>::base_iterator()
: m_owner(NULL), m_pos(0, 0)
{
CLAW_POSTCOND(is_final());
} // image::base_iterator::base_iterator()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline claw::graphic::image::base_iterator<Image, Pixel>::base_iterator
( image_type& owner, unsigned int x, unsigned int y )
: m_owner(&owner), m_pos(x, y)
{
} // image::base_iterator::base_iterator()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::operator==
( const self_type& that ) const
{
if ( is_final() && that.is_final() )
return true;
else if ( m_owner == that.m_owner )
return m_pos == that.m_pos;
else
return false;
} // image::base_iterator::operator==()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::operator!=
( const self_type& that ) const
{
return !(*this == that);
} // image::base_iterator::operator!=()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::operator<
( const self_type& that ) const
{
if ( this->m_pos.y == that.m_pos.y)
return this->m_pos.x < that.m_pos.x;
else
return this->m_pos.y < that.m_pos.y;
} // image::base_iterator::operator<()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::operator>
( const self_type& that ) const
{
return that < *this;
} // image::base_iterator::operator>()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::operator<=
( const self_type& that ) const
{
return !(*this > that);
} // image::base_iterator::operator<=()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::operator>=
( const self_type& that ) const
{
return !(*this < that);
} // image::base_iterator::operator>=()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
claw::graphic::image::base_iterator<Image, Pixel>::operator+=( int n )
{
if (n < 0)
return *this -= -n;
else
{
CLAW_PRECOND( !is_final() );
unsigned int n_y = n / m_owner->width();
unsigned int n_x = n % m_owner->width();
m_pos.x += n_x;
m_pos.y += n_y;
return *this;
}
} // image::base_iterator::operator+=()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
claw::graphic::image::base_iterator<Image, Pixel>::operator-=( int n )
{
if (n < 0)
return *this += -n;
else
{
CLAW_PRECOND( m_owner );
unsigned int n_y = n / m_owner->width();
unsigned int n_x = n % m_owner->width();
CLAW_PRECOND( m_pos.x >= n_x );
CLAW_PRECOND( m_pos.y >= n_y );
m_pos.x -= n_x;
m_pos.y -= n_y;
return *this;
}
} // image::base_iterator::operator-=()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
claw::graphic::image::base_iterator<Image, Pixel>::operator+( int n ) const
{
self_type that(*this);
return that += n;
} // image::base_iterator::operator+()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
claw::graphic::image::base_iterator<Image, Pixel>::operator-( int n ) const
{
self_type that(*this);
return that -= n;
} // image::base_iterator::operator-()
/*----------------------------------------------------------------------------*/
template<typename ImageT, typename PixelT>
inline typename claw::graphic::image::base_iterator<ImageT, PixelT>::self_type
operator+
( int n,
const typename
claw::graphic::image::base_iterator<ImageT, PixelT>::self_type& self )
{
return self + n;
} // image::base_iterator::operator+()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline
typename claw::graphic::image::base_iterator<Image, Pixel>::difference_type
claw::graphic::image::base_iterator<Image, Pixel>::operator-
( const self_type& that ) const
{
CLAW_PRECOND( is_final() || that.is_final() || (m_owner == that.m_owner) );
if ( that.is_final() )
{
if ( is_final() )
return 0;
else
return -(m_owner->height() - m_pos.y) * m_owner->width() - m_pos.x;
}
else if ( is_final() )
return (that.m_owner->height() - that.m_pos.y) * that.m_owner->width()
+ that.m_pos.x;
else
return m_pos.y * m_owner->width() + m_pos.x
- that.m_pos.y * that.m_owner->width() + that.m_pos.x;
} // image::base_iterator::operator-()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
claw::graphic::image::base_iterator<Image, Pixel>::operator++()
{
CLAW_PRECOND( !is_final() );
++m_pos.x;
if ( m_pos.x == m_owner->width() )
{
m_pos.x = 0;
++m_pos.y;
}
return *this;
} // image::base_iterator::operator++()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
claw::graphic::image::base_iterator<Image, Pixel>::operator++(int)
{
self_type that(*this);
++(*this);
return that;
} // image::base_iterator::operator++() [postincrement]
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type&
claw::graphic::image::base_iterator<Image, Pixel>::operator--()
{
CLAW_PRECOND( !is_final() );
CLAW_PRECOND( (m_pos.y > 0) || (m_pos.x > 0) );
if ( m_pos.x == 0 )
{
m_pos.x = m_owner->width() - 1;
--m_pos.y;
}
else
--m_pos.x;
return *this;
} // image::base_iterator::operator--()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::self_type
claw::graphic::image::base_iterator<Image, Pixel>::operator--(int)
{
self_type that(*this);
--(*this);
return that;
} // image::base_iterator::operator--() [postdecrement]
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::reference
claw::graphic::image::base_iterator<Image, Pixel>::operator*() const
{
CLAW_PRECOND( !is_final() );
return (*m_owner)[m_pos.y][m_pos.x];
} // image::base_iterator::operator*()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::pointer
claw::graphic::image::base_iterator<Image, Pixel>::operator->() const
{
CLAW_PRECOND( !is_final() );
return &(*m_owner)[m_pos.y][m_pos.x];
} // image::base_iterator::operator->()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline typename claw::graphic::image::base_iterator<Image, Pixel>::reference
claw::graphic::image::base_iterator<Image, Pixel>::operator[]( int n ) const
{
return *(*this + n);
} // image::base_iterator::operator[]()
/*----------------------------------------------------------------------------*/
template<typename Image, typename Pixel>
inline bool
claw::graphic::image::base_iterator<Image, Pixel>::is_final() const
{
if ( !m_owner )
return true;
else if ( m_pos.y >= m_owner->height() )
return true;
else if ( m_pos.y == m_owner->height() - 1 )
return m_pos.x >= m_owner->width();
else
return false;
} // image::base_iterator::is_final()
/*----------------------------------------------------------------------------*/
inline claw::graphic::image::scanline&
claw::graphic::image::operator[](unsigned int i)
{
return m_data[i];
} // image::operator[]()
/*----------------------------------------------------------------------------*/
inline const claw::graphic::image::scanline&
claw::graphic::image::operator[](unsigned int i) const
{
return m_data[i];
} // image::operator[]() [const]
assert.hpp
Some assert macros to strengthen you code.
CLAW_PRECOND
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:98
CLAW_POSTCOND
#define CLAW_POSTCOND(b)
Abort the program if a postcondition is not true.
Definition assert.hpp:101
claw::graphic::image::base_iterator
Base class for iterators on an image.
Definition image.hpp:108
claw::graphic::image::base_iterator::operator+=
self_type & operator+=(int n)
Move the iterator.
Definition image.ipp:178
claw::graphic::image::base_iterator::operator+
self_type operator+(int n) const
Get an iterator at a specific distance of the current iterator.
Definition image.ipp:231
claw::graphic::image::base_iterator::pointer
pixel_type * pointer
The type of the pointers to the values accesssed by the iterator.
Definition image.hpp:129
claw::graphic::image::base_iterator::operator-=
self_type & operator-=(int n)
Move the iterator.
Definition image.ipp:203
claw::graphic::image::base_iterator::operator++
self_type & operator++()
Preincrement.
Definition image.ipp:302
claw::graphic::image::base_iterator::operator->
pointer operator->() const
Get a pointer on the pointed pixel.
Definition image.ipp:384
claw::graphic::image::base_iterator::operator-
self_type operator-(int n) const
Get an iterator at a specific distance of the current iterator.
Definition image.ipp:245
claw::graphic::image::base_iterator::reference
pixel_type & reference
The type of the references to the values accesssed by the iterator.
Definition image.hpp:125
claw::graphic::image::base_iterator::operator*
reference operator*() const
Get a reference on the pointed pixel.
Definition image.ipp:371
claw::graphic::image::base_iterator::difference_type
ptrdiff_t difference_type
The type of the distance between two iterators.
Definition image.hpp:132
claw::graphic::image::base_iterator::operator[]
reference operator[](int n) const
Get a pixel, using the iterator like an array.
Definition image.ipp:398
claw::graphic::image::base_iterator::operator--
self_type & operator--()
Predecrement.
Definition image.ipp:336
claw::graphic::image::base_iterator::base_iterator
base_iterator()
Constructor.
Definition image.ipp:62
claw::graphic::image::scanline
One line in the image.
Definition image.hpp:61
claw::graphic::image::scanline::reference
super::reference reference
Reference to a pixel..
Definition image.hpp:72
claw::graphic::image::scanline::operator[]
reference operator[](unsigned int i)
Get a pixel from the line.
Definition image.ipp:38
claw::graphic::image::scanline::const_reference
super::const_reference const_reference
Const reference to a pixel.
Definition image.hpp:75
claw::graphic::image::operator[]
scanline & operator[](unsigned int i)
Gets a line of the image.
Definition image.ipp:429