milena r1633: Add resize function for fast 2d image in mln::geom

URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena ChangeLog: 2008-01-07 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr> Add resize function for fast 2d image in mln::geom. * mln/geom/resize.hh: New function for resize fast iamge. * tests/geom/resize.cc: Test this function. * tests/geom/Makefile.am: And add this test in the Makefile. * mln/core/concept/object.hh: Update copyright. --- mln/core/concept/object.hh | 4 - mln/geom/resize.hh | 174 +++++++++++++++++++++++++++++++++++++++++++++ tests/geom/Makefile.am | 2 tests/geom/resize.cc | 68 +++++++++++++++++ 4 files changed, 246 insertions(+), 2 deletions(-) Index: trunk/milena/tests/geom/resize.cc =================================================================== --- trunk/milena/tests/geom/resize.cc (revision 0) +++ trunk/milena/tests/geom/resize.cc (revision 1633) @@ -0,0 +1,68 @@ +// Copyright (C) 2007, 2008 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/geom/resize.cc + * + * \brief Tests on mln::geom::resize. + */ + +#include <mln/core/image1d.hh> +#include <mln/core/image2d.hh> +#include <mln/core/image3d.hh> +#include <mln/geom/resize.hh> +#include <mln/debug/iota.hh> +#include <mln/debug/println.hh> +#include <mln/level/compare.hh> + +int +main () +{ + using namespace mln; + + { + image2d<int> ima (8, 16); + + debug::iota(ima); + + image2d<int> out = geom::resize(ima, 0.5); + + int ws[4][8] = + { + { 9, 11, 13, 15, 17, 19, 21, 23}, + { 41, 43, 45, 47, 49, 51, 53, 55}, + { 73, 75, 77, 79, 81, 83, 85, 87}, + {105, 107, 109, 111, 113, 115, 117, 119} + }; + + image2d<int> ref(make::image2d(ws)); + + mln_assertion (out == ref); + } + +} Index: trunk/milena/tests/geom/Makefile.am =================================================================== --- trunk/milena/tests/geom/Makefile.am (revision 1632) +++ trunk/milena/tests/geom/Makefile.am (revision 1633) @@ -17,6 +17,7 @@ nrows \ nslis \ pmin_pmax \ +resize \ seed2tiling \ seed2tiling_roundness \ shift \ @@ -36,6 +37,7 @@ nrows_SOURCES = nrows.cc nslis_SOURCES = nslis.cc pmin_pmax_SOURCES = pmin_pmax.cc +resize_SOURCES = resize.cc seed2tiling_SOURCES = seed2tiling.cc seed2tiling_roundness_SOURCES = seed2tiling_roundness.cc shift_SOURCES = shift.cc Index: trunk/milena/mln/core/concept/object.hh =================================================================== --- trunk/milena/mln/core/concept/object.hh (revision 1632) +++ trunk/milena/mln/core/concept/object.hh (revision 1633) @@ -1,4 +1,4 @@ -// Copyright (C) 2007 EPITA Research and Development Laboratory +// Copyright (C) 2007, 2008 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 @@ -93,7 +93,7 @@ * <LI> \ref mln::win * * \section copyright Copyright and License. - * Copyright (C) 2007 EPITA Research and Development Laboratory + * Copyright (C) 2007, 2008 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 Index: trunk/milena/mln/geom/resize.hh =================================================================== --- trunk/milena/mln/geom/resize.hh (revision 0) +++ trunk/milena/mln/geom/resize.hh (revision 1633) @@ -0,0 +1,174 @@ +// Copyright (C) 2007, 2008 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_GEOM_RESIZE_HH +# define MLN_GEOM_RESIZE_HH + +/*! \file mln/geom/resize.hh + * + * \brief Resize an image. + */ + +# include <mln/core/concept/image.hh> +# include <mln/core/clone.hh> +# include <mln/make/w_window1d.hh> +# include <mln/accu/mean.hh> +# include <mln/border/resize.hh> +# include <mln/border/fill.hh> +# include <mln/debug/println.hh> + + +namespace mln +{ + + namespace geom + { + + /*! + * \brief Resize an image \p input_ with \p ratio. + * + * \param[in] input_ The image to resize. + * \param[in] ratio The ratio of the resize image. + * + * \precondition \p input_ has to be initialized. + * \precondition \p ratio <= 1. + * + * \return The resized image. + */ + template <typename I> + I + resize(const Image<I>& input_, const float ratio); + + +# ifndef MLN_INCLUDE_ONLY + + + namespace impl + { + + template <typename I> + I + resize_1d_(const I& input, const float ratio) + { + trace::entering("mln::geom::impl::resize_1d_"); + typedef mln_value(I) V; + + std::size_t cols = input.bbox().len(0); + std::size_t new_cols = (std::size_t)(ratio * cols); + + std::size_t n = (std::size_t)(1 / ratio); + + I output (new_cols); + level::fill(output, 0); + + for (std::size_t j = 0; j < cols; ++j) + { + output(point1d(j / n)) += input(point1d(j)); + if (!((j + 1) % n)) + output(point1d(j / n)) /= n; + } + + trace::exiting("mln::geom::impl::resize_1d_"); + return output; + } + + template <typename I> + I + resize_2d_(const I& input, const float ratio) + { + trace::entering("mln::geom::impl::resize_2d_"); + typedef mln_value(I) V; + + std::size_t rows = input.bbox().len(0); + std::size_t cols = input.bbox().len(1); + std::size_t new_rows = (std::size_t)(ratio * rows); + std::size_t new_cols = (std::size_t)(ratio * cols); + + std::size_t n = (std::size_t)(1 / ratio); + std::size_t nn = n * n; + + I output (new_rows, new_cols); + std::vector<V> v (new_cols); + + for (std::size_t i = 0; i < rows; ++i) + { + std::size_t in = i / n; + + if (!(i % n)) + for (std::size_t j = 0; j < new_cols; ++j) + v[j] = 0; + + for (std::size_t j = 0; j < cols; ++j) + v[j / n] += input(point2d(i, j)); + + if (!((i + 1) % n)) + for (std::size_t j = 0; j < cols; ++j) + output(point2d(in, j / n)) = v[j / n] / nn; + } + + trace::exiting("mln::geom::impl::resize_2d_"); + return output; + } + + } // end of namespace mln::geom::impl + + + template <typename I> + I + resize(const Image<I>& input_, const float ratio) + { + trace::entering("mln::geom::resize"); + + const I input = exact (input_); + mln_precondition(input.has_data()); + mln_precondition(ratio <= 1.0f); + mlc_is(mln_trait_image_speed(I), trait::image::speed::fastest)::check(); + + I output; + typedef mln_point(I) P; + + /// FIXME : Find a better way for the disjunction. + +// if (P::dim == 1) +// output = impl::resize_1d_(input, ratio); + + if (P::dim == 2) + output = impl::resize_2d_(input, ratio); + + trace::exiting("mln::geom::resize"); + return output; + } + + +# endif // ! MLN_INCLUDE_ONLY + + } // end of namespace mln::geom + +} // end of namespace mln + + +#endif // ! MLN_GEOM_RESIZE_HH
participants (1)
-
Guillaume Duhamel