olena: olena-2.0-553-ge279a62 Add data::stretch_inplace.

* mln/data/stretch.hh: Remove fixme. * mln/data/stretch_inplace.hh, * tests/data/stretch_inplace.cc: New. * tests/data/stretch.cc: Remove a useless include. * tests/data/Makefile.am: New target. --- milena/ChangeLog | 13 ++ milena/mln/data/stretch.hh | 10 +- milena/mln/data/stretch_inplace.hh | 137 ++++++++++++++++++++ milena/tests/data/Makefile.am | 3 +- milena/tests/data/stretch.cc | 4 +- .../tests/data/{saturate.cc => stretch_inplace.cc} | 25 ++-- 6 files changed, 175 insertions(+), 17 deletions(-) create mode 100644 milena/mln/data/stretch_inplace.hh copy milena/tests/data/{saturate.cc => stretch_inplace.cc} (76%) diff --git a/milena/ChangeLog b/milena/ChangeLog index edf3a65..3499fd3 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,3 +1,16 @@ +2013-04-29 Guillaume Lazzara <z@lrde.epita.fr> + + Add data::stretch_inplace. + + * mln/data/stretch.hh: Remove fixme. + + * mln/data/stretch_inplace.hh, + * tests/data/stretch_inplace.cc: New. + + * tests/data/stretch.cc: Remove a useless include. + + * tests/data/Makefile.am: New target. + 2013-04-17 Guillaume Lazzara <z@lrde.epita.fr> * doc/mln/convert.dox: Fix from_to_ module name. diff --git a/milena/mln/data/stretch.hh b/milena/mln/data/stretch.hh index 5b36dbe..2473494 100644 --- a/milena/mln/data/stretch.hh +++ b/milena/mln/data/stretch.hh @@ -33,8 +33,6 @@ /// stretching way. /// /// \todo Make it work with other types than scalars (e.g., vectors). -/// -/// \todo Think about adding a stretch_inplace(?) # include <mln/estim/min_max.hh> # include <mln/value/int_u.hh> @@ -82,7 +80,7 @@ namespace mln template <typename V, typename I> inline mln_ch_value(I, V) - stretch(const V& v, const Image<I>& input) + stretch(const V& v, const Image<I>& input) { mln_trace("data::impl::stretch"); @@ -95,6 +93,12 @@ namespace mln estim::min_max(input, min_, max_); if (max_ != min_) { + // We always want to perform this algorithm even if (min_ == + // mln_min(V) and max_ == mln_max(V)) since we need to + // convert the input image towards the given type and a + // default conversion function may not exist between V and + // mln_value(I). + //FIXME: we would like to use float instead of double but we //can't for precision reasons. See ticket #179. double diff --git a/milena/mln/data/stretch_inplace.hh b/milena/mln/data/stretch_inplace.hh new file mode 100644 index 0000000..12c8b8e --- /dev/null +++ b/milena/mln/data/stretch_inplace.hh @@ -0,0 +1,137 @@ +// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena 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 Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project 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_DATA_STRETCH_INPLACE_HH +# define MLN_DATA_STRETCH_INPLACE_HH + +/// \file +/// +/// Transform linearly the contents of an image into another one in a +/// stretching way. + + +# include <mln/estim/min_max.hh> +# include <mln/value/int_u.hh> +# include <mln/fun/v2v/linear.hh> +# include <mln/data/transform_inplace.hh> + + +# include <mln/value/internal/encoding.hh> +# include <iomanip> +namespace mln +{ + + namespace data + { + + /*! \brief Stretch the values of \p ima so that they can be + * stored inplace. + * + * \param[in,out] ima The input image. + * + * If input is filled with a single value, this function does + * nothing. + * + * \pre input.is_valid + * + * \ingroup mlndata + */ + template <typename I> + void + stretch_inplace(Image<I>& ima); + + /*! \brief Stretch the values of \p ima so that they can be + * stored inplace. + * + * \param[in,out] ima The input image. + * \param[out] stretched The image has changed. + * + * If input is filled with a single value, this function does + * nothing. + * + * \pre input.is_valid + * + * \ingroup mlndata + */ + template <typename I> + void + stretch_inplace(Image<I>& ima, bool& stretched); + + +# ifndef MLN_INCLUDE_ONLY + + + template <typename I> + inline + void + stretch_inplace(Image<I>& ima, bool& stretched) + { + mln_trace("data::stretch_inplace"); + typedef mln_value(I) V; + mln_precondition(exact(ima).is_valid()); + + stretched = false; + V min_, max_; + estim::min_max(ima, min_, max_); + if (max_ != min_) + { + // Already stretched ? + if (min_ == mln_min(V) && max_ == mln_max(V)) + return; + + //FIXME: we would like to use float instead of double but we + //can't for precision reasons. See ticket #179. + double + min = double(min_), + max = double(max_), + epsilon = mln_epsilon(float), + M = mln_max(V) + 0.5f - epsilon, + m = 0.0f - 0.5f + epsilon, + a = (M - m) / (max - min), + b = (m * max - M * min) / (max - min); + fun::v2v::linear_sat<V, double, V> f(a, b); + data::transform_inplace(ima, f); + stretched = true; + } + // else nothing to do. + } + + + template <typename I> + void + stretch_inplace(Image<I>& ima) + { + bool b; + stretch_inplace(ima, b); + } + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::data + +} // end of namespace mln + + +#endif // ! MLN_DATA_STRETCH_INPLACE_HH diff --git a/milena/tests/data/Makefile.am b/milena/tests/data/Makefile.am index b733718..565987e 100644 --- a/milena/tests/data/Makefile.am +++ b/milena/tests/data/Makefile.am @@ -1,4 +1,4 @@ -# Copyright (C) 2008, 2009, 2010 EPITA Research and Development +# Copyright (C) 2008, 2009, 2010, 2013 EPITA Research and Development # Laboratory (LRDE) # # This file is part of Olena. @@ -39,6 +39,7 @@ check_PROGRAMS = \ sort_psites \ split \ stretch \ + stretch_inplace \ transform \ transform_inplace \ update diff --git a/milena/tests/data/stretch.cc b/milena/tests/data/stretch.cc index 5144146..e22605e 100644 --- a/milena/tests/data/stretch.cc +++ b/milena/tests/data/stretch.cc @@ -1,4 +1,5 @@ -// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2007, 2008, 2009, 2013 EPITA Research and Development +// Laboratory (LRDE) // // This file is part of Olena. // @@ -25,7 +26,6 @@ #include <mln/core/image/image2d.hh> #include <mln/data/stretch.hh> -#include <mln/debug/iota.hh> #include <mln/value/int_u8.hh> diff --git a/milena/tests/data/saturate.cc b/milena/tests/data/stretch_inplace.cc similarity index 76% copy from milena/tests/data/saturate.cc copy to milena/tests/data/stretch_inplace.cc index 4baee22..fb7b071 100644 --- a/milena/tests/data/saturate.cc +++ b/milena/tests/data/stretch_inplace.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2013 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -24,24 +24,27 @@ // executable file might be covered by the GNU General Public License. #include <mln/core/image/image2d.hh> -#include <mln/data/saturate.hh> -#include <mln/debug/iota.hh> - +#include <mln/data/stretch_inplace.hh> int main() { using namespace mln; - image2d<int> ima(3, 3); int vs[3][3] = { - { 2, 2, 3 }, - { 4, 5, 6 }, - { 6, 6, 6 } + { 1000, 2000, 3000 }, + { 1000, 2000, 3000 }, + { 1000, 2000, 3000 } + }; + image2d<int> ima = make::image(vs); + data::stretch_inplace(ima); + + int ws[3][3] = { + { 0, 1073741824, 2147483647 }, + { 0, 1073741824, 2147483647 }, + { 0, 1073741824, 2147483647 } }; + image2d<int> ref = make::image(ws); - image2d<int> ref = make::image(vs); - debug::iota(ima); - data::saturate_inplace(ima, 2, 6); box_fwd_piter_<point2d> p(ima.domain()); for_all(p) mln_assertion(ima(p) == ref(p)); -- 1.7.2.5
participants (1)
-
Jonathan Fabrizio