https://svn.lrde.epita.fr/svn/oln/trunk/milena Index: ChangeLog from Nicolas Ballas <ballas@lrde.epita.fr> Add pixel iterators to milena. * tests/pixter_dpoint2d.cc, * tests/pixter_point2d.cc: New tests. * mln/core/dpoints_piter.hh: Delete trailing whitespace. * mln/core/macros.hh: Add macro to get pixel iterator from an image. * mln/core/concept/image.hh, * mln/core/concept/doc/image.hh: Delte trailing whitespace. * mln/core/concept/fast_iterator.hh: New fast iterator concept. * mln/core/pixter2d_b.hh: New pixel iterator on image2d_b. * mln/core/image2d_b.hh: Add a method to get an offset from a dpoint. * mln/core/dpoints_pixter.hh: New pixel iterator. mln/core/concept/doc/image.hh | 5 + mln/core/concept/fast_iterator.hh | 79 +++++++++++++++++ mln/core/dpoints_piter.hh | 2 mln/core/dpoints_pixter.hh | 145 ++++++++++++++++++++++++++++++++ mln/core/image2d_b.hh | 21 ++++ mln/core/internal/fast_iterator_base.hh | 138 ++++++++++++++++++++++++++++++ mln/core/macros.hh | 16 +++ mln/core/pixter2d_b.hh | 103 ++++++++++++++++++++++ tests/pixter_dpoint2d.cc | 35 +++++++ tests/pixter_point2d.cc | 66 ++++++++++++++ 10 files changed, 608 insertions(+), 2 deletions(-) Index: tests/pixter_dpoint2d.cc --- tests/pixter_dpoint2d.cc (revision 0) +++ tests/pixter_dpoint2d.cc (revision 0) @@ -0,0 +1,35 @@ +#include <cassert> +#include <iostream> +#include <mln/core/image2d.hh> +#include <mln/core/internal/set_of.hh> +#include <mln/level/fill.hh> +#include <mln/core/dpoints_pixter.hh> + +int +main() +{ + using namespace mln; + typedef image2d<int> I; + + + I ima(20, 20); + level::fill(ima, 5); + I::piter f(ima.domain()); + internal::set_of_<I::dpoint> set; + + set.insert(mk_dpoint2d(-1, -1)); + set.insert(mk_dpoint2d(0, -1)); + set.insert(mk_dpoint2d(1, 0)); + set.insert(mk_dpoint2d(1, 1)); + + for_all(f) + { + I::qixter dit(set, f, ima); + + if (f[0] > 0 && f[1] > 0 && f[0] < 19 && f[1] < 19) + for_all(dit) + { + assert(*dit == 5); + } + } +} Index: tests/pixter_point2d.cc --- tests/pixter_point2d.cc (revision 0) +++ tests/pixter_point2d.cc (revision 0) @@ -0,0 +1,66 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// 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 +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#include <cassert> +#include <mln/core/image2d_b.hh> +#include <iostream> + + +int +main() +{ + using namespace mln; + + image2d_b<int> ima(2, 2); + + image2d_b<int>::piter p(ima.domain()); + image2d_b<int>::pixter f(ima); + int i = 0; + + for_all(p) + { + ima(p) = i++; + } + i = 0; + + for_all(f) + { + std::cout << *f << std::endl; + assert(*f == i ++); + *f = 5; + } + + for_all(p) + { + assert(ima(p) == 5); + } + + f.start(); + assert(f.is_valid()); + f.invalidate(); + assert(!f.is_valid()); +} Index: mln/core/dpoints_piter.hh --- mln/core/dpoints_piter.hh (revision 1006) +++ mln/core/dpoints_piter.hh (working copy) @@ -88,7 +88,7 @@ /// Start an iteration. void start(); - +xb /// Go to the next point. void next_(); Index: mln/core/macros.hh --- mln/core/macros.hh (revision 1006) +++ mln/core/macros.hh (working copy) @@ -81,13 +81,21 @@ /// Shortcut to access the fwd_piter type associated to T. # define mln_fwd_piter(T) typename T::fwd_piter +/// Shortcut to access the fwd_pixter type associated to T. +# define mln_fwd_pixter(T) typename T::fwd_pixter + +/// Shortcut to access the bkd_piter type associated to T. +# define mln_bkd_piter(T) typename T::bkd_piter + /// Shortcut to access the fwd_qiter type associated to T. # define mln_fwd_qiter(T) typename T::fwd_qiter +/// Shortcut to access the fwd_qixter type associated to T. +# define mln_fwd_qixter(T) typename T::fwd_qixter + /// Shortcut to access the fwd_viter type associated to T. # define mln_fwd_viter(T) typename T::fwd_viter - // l @@ -100,6 +108,9 @@ /// Shortcut to access the piter type associated to T. # define mln_piter(T) typename T::piter +/// Shortcut to access the pixter type associated to T. +# define mln_pixter(T) typename T::pixter + /// Shortcut to access the pset type associated to T. # define mln_pset(T) typename T::pset @@ -115,6 +126,9 @@ /// Shortcut to access the qiter type associated to T. # define mln_qiter(T) typename T::qiter +// Shortcut to access to the qixter type associated to T +# define mln_qixter(T) typename T::qixter + // n Index: mln/core/concept/image.hh Index: mln/core/concept/fast_iterator.hh --- mln/core/concept/fast_iterator.hh (revision 0) +++ mln/core/concept/fast_iterator.hh (revision 0) @@ -0,0 +1,79 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// 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 +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_CORE_CONCEPT_FAST_ITERATOR_HH +# define MLN_CORE_CONCEPT_FAST_ITERATOR_HH + +/*! \file mln/core/concept/fast_iterator.hh + * \brief Definition of the concept of mln::Fast_Iterator. + */ + +# include <mln/core/concept/iterator.hh> + +namespace mln +{ + + + template <typename E> + struct Fast_Iterator : public Iterator<E> + { + /* + typedef rvalue + typedef lvalue + rvalue operator* (); + lvalue operator* () const; + */ + protected: + Fast_Iterator(); + }; + +#ifndef MLN_INCLUDE_ONLY + + /*! \brief Fast Iterator concept class. + * + * \see FIXME + */ + template <typename E> + Fast_Iterator<E>::Fast_Iterator() + { + typedef mln_value(E) value; + typedef mln_rvalue(E) rvalue; + typedef mln_lvalue(E) lvalue; + + rvalue (E::*m1)() = &E::operator*; + m1 = 0; + lvalue (E::*m2)() const = &E::operator*; + m2 = 0; + } + +#endif // ! MLN_INCLUDE_ONLY + + +} // end of namespace mln + + +#endif // ! MLN_CORE_CONCEPT_FAST_ITERATOR_HH Index: mln/core/concept/doc/image.hh --- mln/core/concept/doc/image.hh (revision 1006) +++ mln/core/concept/doc/image.hh (working copy) @@ -162,6 +162,11 @@ std::size_t npoints() const; }; + template <typename E> + struct Fast_Image : public Image<E> + { + }; + } // end of namespace mln::doc } // end of namespace mln Index: mln/core/pixter2d_b.hh --- mln/core/pixter2d_b.hh (revision 0) +++ mln/core/pixter2d_b.hh (revision 0) @@ -0,0 +1,103 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// 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 +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_CORE_PIXTER2D_B_HH +# define MLN_CORE_PIXTER2D_B_HH + +# include <mln/core/internal/fast_iterator_base.hh> +# include <mln/core/point2d.hh> +# include <iostream> + + +//FIXME comment +/*! \file mln/core/pixter2d_b.hh + * + * \brief Pixel iterator class on a image 2d with border. + */ + +# include <mln/core/concept/fast_iterator.hh> + + +namespace mln +{ + + // Forward declaration + template <typename T> class image2d_b; + + template <typename T> + class fwd_pixter2d_b : + public internal::fast_iterator_base_< fwd_pixter2d_b<T>, image2d_b<T> > + { + public: + /// Image pixel value type. + typedef mln_value(image2d_b<T>) value; + + /*! \brief Constructor. + * + * \param[in] ima Image to iterate. + */ + fwd_pixter2d_b(image2d_b<T>& ima); + /// Move the iterator to the next elements + void next_(); + + private: + unsigned border_size_; + unsigned row_offset_; + value *eor_; + }; + +#ifndef MLN_INCLUDE_ONLY + + template <typename T> + fwd_pixter2d_b<T>::fwd_pixter2d_b(image2d_b<T>& ima) : + border_size_(ima.border()), + row_offset_((ima.domain().pmax()[1] - ima.domain().pmin()[1]) + + 2 * border_size_ + 1) + { + this->start_ = &ima(ima.domain().pmin()); + this->eor_ = &ima(mk_point2d(ima.domain().pmin()[0], ima.domain().pmax()[1])) + 1; + this->eoi_ = &ima(ima.domain().pmax()) + 1; + } + + template <typename T> + void fwd_pixter2d_b<T>::next_() + { + ++(this->current_); + + if (this->current_ == this->eor_ and this->current_ != this->eoi_) + { + this->current_ += 2 * this->border_size_; + this->eor_ += this->row_offset_; + } + } + +#endif // ! MLN_INCLUDE_ONLY + + +} // end of namespace mln + +#endif // ! MLN_CORE_PIXTER2D_B_HH Index: mln/core/image2d_b.hh --- mln/core/image2d_b.hh (revision 1006) +++ mln/core/image2d_b.hh (working copy) @@ -39,6 +39,8 @@ # include <mln/border/thickness.hh> # include <mln/fun/all.hh> +# include <mln/core/pixter2d_b.hh> +# include <mln/core/dpoints_pixter.hh> namespace mln @@ -64,6 +66,15 @@ typedef fwd_piter piter; // end of warning + /// Forward pixel iterator associated to image2d + typedef fwd_pixter2d_b<T> fwd_pixter; + + /// Foward pixel iterator on dpoints assoicated to image 2d + typedef dpoints_pixter< image2d_b<T> > fwd_qixter; + + typedef fwd_pixter pixter; + typedef fwd_qixter qixter; + /// Value associated type. typedef T value; @@ -131,6 +142,9 @@ /// Read-write access to the image value located at \p p. T& operator()(const point2d& p); + /// Return an offset from a dpoints + int offset(const dpoint2d& dp); + /// Destructor. ~image2d_b(); @@ -350,6 +364,13 @@ } } + template <typename T> + int + image2d_b<T>::offset(const dpoint2d& dp) + { + return dp[0] * vb_.pmax()[1] + dp[1]; + } + # endif // ! MLN_INCLUDE_ONLY } // end of namespace mln Index: mln/core/dpoints_pixter.hh --- mln/core/dpoints_pixter.hh (revision 0) +++ mln/core/dpoints_pixter.hh (revision 0) @@ -0,0 +1,145 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// 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 +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_CORE_DPOINTS_PIXTER_HH +# define MLN_CORE_DPOINTS_PIXTER_HH + +# include <cassert> +# include <vector> +# include <mln/core/concept/fast_iterator.hh> + + +namespace mln +{ + /*! \brief A generic forward iterator on image pixel of a windows and of + * neighborhoods. + * + * The parameter \c I is the image type. + */ + template <typename I> + class dpoints_pixter : public Fast_Iterator< dpoints_pixter<I> > + { + public: + /// Image pixel value + typedef mln_value(I) value; + /// Image pixel rvalue + typedef mln_rvalue(I) rvalue; + /// Image pixel lvalue + typedef mln_lvalue(I) lvalue; + + /*! \brief Constructo. + * + * \param[in] dps Object that can provide an array of delta-points. + * \param[in] p_ref Center point to iterate around. + * \param[in] ima Image to iterate. + */ + template <typename Dps> + dpoints_pixter(const Dps& dps, const typename I::point& p_ref,I& ima); + + /// Set the iterator at the start. + void start(); + /// Go to the next pixel. + void next_(); + /// Invalidate the iterator. + void invalidate(); + /// Is the iterator valid? + bool is_valid() const; + + /// Get the iterator value + rvalue operator* (); + lvalue operator* () const; + + private: + /// offset of each dpoints + std::vector<int> offset_; + /// current offset + std::vector<int>::iterator i_; + /// reference pixel in the image + value* pixref_; + }; + + +#ifndef MLN_INCLUDE_ONLY + + template <typename I> + template <typename Dps> + dpoints_pixter<I>::dpoints_pixter(const Dps& dps, const typename I::point& ref, I& ima) + { + pixref_ = &ima(ref); + for (typename std::vector<typename I::dpoint>::const_iterator it = dps.vec().begin(); + it != dps.vec().end(); + ++it) + offset_.push_back(ima.offset(*it)); + } + + + template <typename I> + void dpoints_pixter<I>::start() + { + i_ = offset_.begin(); + } + + template <typename I> + void dpoints_pixter<I>::next_() + { + ++i_; + } + + template <typename I> + bool dpoints_pixter<I>::is_valid() const + { + return i_ != offset_.end(); + } + + template <typename I> + void dpoints_pixter<I>::invalidate() + { + i_ = offset_.end(); + } + + template <typename I> + typename dpoints_pixter<I>::rvalue + dpoints_pixter<I>::operator* () + { + assert(this->is_valid()); + return *(pixref_ + *i_); + } + + template <typename I> + typename dpoints_pixter<I>::lvalue + dpoints_pixter<I>::operator* () const + { + assert(this->is_valid()); + return *(pixref_ + *i_); + } + +#endif // ! MLN_INCLUDE_ONLY + +} + + +#endif // MLN_CORE_DPOINTS_PIXTER_HH Index: mln/core/internal/fast_iterator_base.hh --- mln/core/internal/fast_iterator_base.hh (revision 0) +++ mln/core/internal/fast_iterator_base.hh (revision 0) @@ -0,0 +1,138 @@ +// Copyright (C) 2007 EPITA Research and Development Laboratory +// +// This file is part of the Olena Library. This library is free +// software; you can redistribute it and/or modify it under the terms +// of the GNU General Public License version 2 as published by the +// Free Software Foundation. +// +// 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 +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this library; see the file COPYING. If not, write to +// the Free Software Foundation, 51 Franklin Street, Fifth Floor, +// Boston, MA 02111-1307, USA. +// +// As a special exception, you may use this file as part of a free +// software library without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to +// produce an executable, this file does not by itself cause the +// resulting executable to be covered by the GNU General Public +// License. This exception does not however invalidate any other +// reasons why the executable file might be covered by the GNU General +// Public License. + +#ifndef MLN_CORE_INTERNAL_FAST_ITERATOR_BASE_HH +# define MLN_CORE_INTERNAL_FAST_ITERATOR_BASE_HH + +/*! \file mln/core/internal/fast_iterator_base.hh + * + * \brief Base class for Fast_Iterator concept implementation. + * Usefull for code factorisation + */ + +# include <mln/core/concept/fast_iterator.hh> + + +namespace mln +{ + + namespace internal + { + + + /*! \brief fast_iterator_base_ class + */ + template <typename Exact, typename Image> + class fast_iterator_base_ : public Fast_Iterator<Exact> + { + public: + /// Image pixel value type. + typedef mln_value(Image) value; + /// Image pixel rvalue type. + typedef mln_value(Image)& rvalue; + /// Image pixel lvalue type + typedef mln_value(Image) lvalue; + + // Go to the beginning of the image. + void start(); + // Go on the next element. + void next_(); + // Invalidate the iterator. + void invalidate(); + // Is the iterator referencing a correct position in the image? + bool is_valid() const; + + + // Return the current pixel. + rvalue operator* (); + lvalue operator* () const; + + protected: + // beginning of the image + value* start_; + // end of the image + value* eoi_; + // current position in the image + value* current_; + // End of Factoriasable + + fast_iterator_base_(); + }; + +#ifndef MLN_INCLUDE_ONLY + + template <typename Exact, typename Image> + fast_iterator_base_<Exact, Image>::fast_iterator_base_() + { + } + + template <typename Exact, typename Image> + void fast_iterator_base_<Exact, Image>::start() + { + current_ = start_; + } + + template <typename Exact, typename Image> + void fast_iterator_base_<Exact, Image>::next_() + { + ++current_; + } + + template <typename Exact, typename Image> + void fast_iterator_base_<Exact, Image>::invalidate() + { + current_ = eoi_; + } + + template <typename Exact, typename Image> + bool fast_iterator_base_<Exact, Image>::is_valid() const + { + return (current_ != eoi_); + } + + template <typename Exact, typename Image> + typename fast_iterator_base_<Exact, Image>::rvalue + fast_iterator_base_<Exact, Image>::operator*() + { + return *current_; + } + + + template <typename Exact, typename Image> + typename fast_iterator_base_<Exact, Image>::lvalue + fast_iterator_base_<Exact, Image>::operator*() const + { + return *current_; + } + +#endif // ! MLN_INCLUDE_ONLY + } // end of namespace internal + +} // end of namespace mln + + +#endif // ! MLN_CORE_INTERNAL_FAST_ITERATOR_BASE_HH