URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-12-03 Simon Nivault <simon.nivault(a)lrde.epita.fr>
Generate mono_rle_image and add his test.
* mln/core/mono_rle_encode.hh: New.
* mln/core/mono_rle_image.hh: New.
* tests/core/mono_rle_image.cc: New.
* tests/core/Makefile.am: Add test.
* tests/core/rle_image.cc: Fix typo.
---
mln/core/mono_rle_encode.hh | 107 ++++++++++++++++++++
mln/core/mono_rle_image.hh | 230 +++++++++++++++++++++++++++++++++++++++++++
tests/core/Makefile.am | 2
tests/core/mono_rle_image.cc | 79 ++++++++++++++
tests/core/rle_image.cc | 4
5 files changed, 420 insertions(+), 2 deletions(-)
Index: trunk/milena/tests/core/mono_rle_image.cc
===================================================================
--- trunk/milena/tests/core/mono_rle_image.cc (revision 0)
+++ trunk/milena/tests/core/mono_rle_image.cc (revision 1583)
@@ -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.
+
+/*! \file tests/mono_rle_iimage.cc
+ *
+ * \brief Test on mln::labeling::blobs.
+ */
+
+#include <mln/core/image2d.hh>
+#include <mln/value/int_u8.hh>
+#include <mln/io/pgm/load.hh>
+#include <mln/pw/all.hh>
+#include <mln/core/neighb2d.hh>
+
+#include <mln/labeling/blobs.hh>
+#include <mln/level/transform.hh>
+#include <mln/level/paste.hh>
+#include <mln/level/compare.hh>
+#include <mln/io/pgm/save.hh>
+
+#include <mln/core/mono_rle_encode.hh>
+
+struct fold_t : public mln::Function_v2v< fold_t >
+{
+ typedef mln::value::int_u8 result;
+ result operator()(unsigned i) const { return i == 0 ? 0 : (i - 1) % 255 + 1; }
+};
+
+struct only_two_t : public mln::Function_v2v< only_two_t >
+{
+ typedef mln::value::int_u8 result;
+ result operator()(unsigned i) const { return i == 2 ? 2 : 0; }
+};
+
+int main()
+{
+ using namespace mln;
+ using value::int_u8;
+
+ image2d<int_u8> lena;
+ io::pgm::load(lena, "../../img/tiny.pgm");
+ image2d<int_u8> cmp(lena.domain());
+
+ unsigned n;
+ image2d<unsigned> labels = labeling::blobs((pw::value(lena) > pw::cst(172u)) |
lena.domain(),
+ c4(), n);
+ std::cout << n << std::endl;
+
+ mono_rle_image<point2d, int_u8> rle = mono_rle_encode(level::transform(labels,
fold_t()), 2);
+
+ level::fill(cmp, literal::zero);
+ level::paste(rle, cmp);
+
+ mln_assertion(cmp == level::transform(labels, only_two_t()));
+}
Index: trunk/milena/tests/core/rle_image.cc
===================================================================
--- trunk/milena/tests/core/rle_image.cc (revision 1582)
+++ trunk/milena/tests/core/rle_image.cc (revision 1583)
@@ -25,9 +25,9 @@
// reasons why the executable file might be covered by the GNU General
// Public License.
-/*! \file tests/labeling/blobs.cc
+/*! \file tests/core/rle_image.cc
*
- * \brief Test on mln::labeling::blobs.
+ * \brief Test on mln::rle_image.hh.
*/
#include <mln/core/image2d.hh>
Index: trunk/milena/tests/core/Makefile.am
===================================================================
--- trunk/milena/tests/core/Makefile.am (revision 1582)
+++ trunk/milena/tests/core/Makefile.am (revision 1583)
@@ -7,6 +7,7 @@
clone \
exact \
initialize \
+ mono_rle_image \
p_runs \
rle_image \
t_image
@@ -15,6 +16,7 @@
clone_SOURCES = clone.cc
exact_SOURCES = exact.cc
initialize_SOURCES = initialize.cc
+mono_rle_image_SOURCES = mono_rle_image.cc
p_runs_SOURCES = p_runs.cc
rle_image_SOURCES = rle_image.cc
t_image_SOURCES = t_image.cc
Index: trunk/milena/mln/core/mono_rle_encode.hh
===================================================================
--- trunk/milena/mln/core/mono_rle_encode.hh (revision 0)
+++ trunk/milena/mln/core/mono_rle_encode.hh (revision 1583)
@@ -0,0 +1,107 @@
+// 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_MONO_RLE_ENCODE_HH
+# define MLN_CORE_MONO_RLE_ENCODE_HH
+
+/*! \file mln/core/mono_rle_encode.hh
+ *
+ * \brief Definintion of function which encodes an image in mono_rle_image.
+ */
+
+# include <mln/core/mono_rle_image.hh>
+
+namespace mln
+{
+
+ /*!
+ ** encode an image class to a mono_rle_image
+ **
+ ** @param input has to respect the Image concept
+ **
+ ** @return mono_rle_image
+ */
+ template <typename I>
+ mono_rle_image<mln_point(I), mln_value(I)>
+ mono_rle_encode(const Image<I>& input, mln_value(I) val);
+
+# ifndef MLN_INCLUDE_ONLY
+ /*!
+ ** test if Point p1 and p2 are on the same line
+ */
+ template <typename P>
+ inline
+ bool
+ on_the_same_line(const P& p1, const P& p2)
+ {
+ const unsigned dim = P::dim;
+ bool same_line = true;
+
+ for (unsigned n = 0; same_line && n < dim - 1; ++n)
+ same_line = (p1[n] == p2[n]);
+ return same_line;
+ }
+
+ template <typename I>
+ inline
+ mono_rle_image<mln_point(I), mln_value(I)>
+ mono_rle_encode(const Image<I>& input, mln_value(I) val)
+ {
+ typedef mln_point(I) P;
+
+ mono_rle_image<mln_point(I), mln_value(I)> output(val);
+ const I& ima = exact(input);
+ mln_piter(I) p (exact(input).domain());
+ unsigned len = 0;
+ mln_point(I) rstart;
+
+ for_all(p)
+ if (ima(p) == val || len)
+ {
+ if (len == 0)
+ {
+ ++len;
+ rstart = p;
+ }
+ else
+ if (val == ima(p)
+ && on_the_same_line(rstart, mln_point(I)(p)))
+ ++len;
+ else
+ {
+ output.insert(p_run<P>(rstart, len));
+ len = 0;
+ }
+ }
+ return output;
+ }
+
+#endif // ! MLN_INCLUDE_ONLY
+
+}
+
+#endif // ! MLN_CORE_MONO_RLE_ENCODE_HH
Index: trunk/milena/mln/core/mono_rle_image.hh
===================================================================
--- trunk/milena/mln/core/mono_rle_image.hh (revision 0)
+++ trunk/milena/mln/core/mono_rle_image.hh (revision 1583)
@@ -0,0 +1,230 @@
+// 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_MONO_RLE_IMAGE_HH
+# define MLN_CORE_MONO_RLE_IMAGE_HH
+
+/*! \file mln/core/mono_rle_image.hh
+ *
+ * \brief Definition of an image with rle encoding.
+ */
+
+# include <mln/core/internal/run_image.hh>
+# include <mln/core/p_runs.hh>
+# include <mln/core/runs_psite.hh>
+# include <mln/value/set.hh>
+# include <vector>
+
+namespace mln
+{
+
+ // Fwd decl.
+ template <typename P, typename T> struct mono_rle_image;
+
+
+ namespace internal
+ {
+
+ template <typename P, typename T>
+ struct data_< mono_rle_image<P,T> >
+ {
+ data_(const T& val);
+
+ /// Image value.
+ T value;
+
+ /// domain of the image
+ p_runs_<P> domain_;
+
+ /// Return the size of the data in memory.
+ unsigned size_mem() const;
+ };
+
+ } // end of namespace mln::internal
+
+
+ namespace trait
+ {
+
+ template <typename P, typename T>
+ struct image_< mono_rle_image<P,T> > : default_image_< T,
mono_rle_image<P,T> >
+ {
+ typedef trait::image::category::primary category;
+
+ typedef trait::image::access::browsing access;
+ // FIXME: Put the right dimension.
+ typedef trait::image::space::two_d space;
+ typedef trait::image::size::regular size;
+ typedef trait::image::support::aligned support;
+
+ typedef trait::image::border::none border;
+ typedef trait::image::data::linear data;
+ typedef trait::image::io::read_only io;
+ typedef trait::image::speed::slow speed;
+ };
+
+ } // end of namespace mln::trait
+
+
+ /*! \brief Mono RLE image.
+ *
+ * RLE image with only one colour.
+ *
+ * Parameter \c P is the type of the image points.
+ * Parameter \c T is the type of the pixel values.
+ * This image is not point wise accessible.
+ */
+ template <typename P, typename T>
+ class mono_rle_image : public internal::run_image_< T, P, mono_rle_image<P, T>
>
+ {
+ public:
+ typedef T value;
+ typedef T& lvalue;
+ typedef const T rvalue;
+ typedef runs_psite<P> psite;
+ typedef mln::value::set<T> vset;
+ typedef p_runs_<P> pset;
+
+
+ /// Skeleton.
+ typedef mono_rle_image< tag::psite_<P>, tag::value_<T> > skeleton;
+
+
+ mono_rle_image(const T& val);
+
+ /// Add a new range to the image.
+ void insert(const p_run<P>& pr);
+
+ /// Read-only access to the image value located at point \p p.
+ rvalue operator() (const psite& site) const;
+
+ /// Read-write access to the image value located at point \p p.
+ lvalue operator() (const psite& site);
+
+ /// Test if this image has been initialized.
+ bool has_data() const;
+
+ /// Give the set of values of the image.
+ const vset& values() const;
+
+ /// Give the definition domain.
+ const pset& domain() const;
+
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ namespace internal
+ {
+
+ // internal::data_< mono_rle_image<I,S> >
+
+ template <typename P, typename T>
+ inline
+ data_< mono_rle_image<P,T> >::data_(const T& val)
+ : value(val)
+ {
+ }
+
+ template <typename P, typename T>
+ inline
+ unsigned
+ data_< mono_rle_image<P,T> >::size_mem() const
+ {
+ return sizeof(T) + domain_.size_mem();
+ }
+
+ } // end of namespace mln::internal
+
+ template <typename P, typename T>
+ inline
+ mono_rle_image<P, T>::mono_rle_image(const T& val)
+ {
+ this->data_ = new internal::data_< mono_rle_image<P,T> >(val);
+ }
+
+ template <typename P, typename T>
+ inline
+ bool
+ mono_rle_image<P, T>::has_data() const
+ {
+ return true;
+ }
+
+ template <typename P, typename T>
+ inline
+ const typename mono_rle_image<P, T>::vset&
+ mono_rle_image<P, T>::values() const
+ {
+ return vset::the();
+ }
+
+ template <typename P, typename T>
+ inline
+ void
+ mono_rle_image<P, T>::insert(const p_run<P>& pr)
+ {
+ if (this->data_->domain_.nruns() != 0)
+ mln_assertion(pr.first() >
this->data_->domain_[this->data_->domain_.nruns() - 1].first());
+ this->data_->domain_.insert(pr);
+ }
+
+ template <typename P, typename T>
+ inline
+ typename mono_rle_image<P, T>::rvalue
+ mono_rle_image<P, T>::operator() (const typename mono_rle_image<P,
T>::psite& site)
+ const
+ {
+ mln_precondition(site.pset_pos_() < this->data_->domain_.nruns());
+ return this->data_->value;
+ }
+
+ template <typename P, typename T>
+ inline
+ typename mono_rle_image<P, T>::lvalue
+ mono_rle_image<P, T>::operator() (const typename mono_rle_image<P,
T>::psite& site)
+ {
+ mln_precondition(site.pset_pos_() < this->data_->domain_.nruns());
+ return this->data_->value;
+ }
+
+ template <typename P, typename T>
+ inline
+ const typename mono_rle_image<P, T>::pset&
+ mono_rle_image<P, T>::domain() const
+ {
+ return this->data_->domain_;
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+
+} // end of namespace mln
+
+
+#endif // ! MLN_CORE_MONO_RLE_IMAGE_HH