Commit 591901fb authored by Thierry Geraud's avatar Thierry Geraud
Browse files

Add 'decorated image' morpher.

	* mln/core/decorated_image.hh: New.
	* mln/core/internal/image_adaptor.hh (adaptee): New.
	* mln/metal/unconst.hh: New.
	* mln/metal/vec.hh (vec): New ctors.
	(operator==): New.
	* mln/metal/make: New directory.
	* mln/metal/make/vec.hh: New.
	* mln/value/proxy: New.
	* mln/value/stack.hh (helper_stack_image_lvalue_): New.
	(read_, write_): New.
	(operator()): Fix.
	(stack): New overload for n=2 and I mutable.
	* tests/decorated_image.cc: New.
	* tests/stack.cc: Augment.


git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@1050 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent ef1feee9
2007-08-27 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Add 'decorated image' morpher.
* mln/core/decorated_image.hh: New.
* mln/core/internal/image_adaptor.hh (adaptee): New.
* mln/metal/unconst.hh: New.
* mln/metal/vec.hh (vec): New ctors.
(operator==): New.
* mln/metal/make: New directory.
* mln/metal/make/vec.hh: New.
* mln/value/proxy: New.
* mln/value/stack.hh (helper_stack_image_lvalue_): New.
(read_, write_): New.
(operator()): Fix.
(stack): New overload for n=2 and I mutable.
* tests/decorated_image.cc: New.
* tests/stack.cc: Augment.
2007-08-27 Thierry Geraud <thierry.geraud@lrde.epita.fr>
Handle const promotion for morphers.
......
// 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_DECORATED_IMAGE_HH
# define MLN_CORE_DECORATED_IMAGE_HH
# include <mln/core/internal/image_adaptor.hh>
# include <mln/value/proxy.hh>
namespace mln
{
// FIXME: Doc!
template <typename I, typename D>
class decorated_image : public internal::image_adaptor_< I, decorated_image<I,D> >
{
typedef decorated_image<I, D> self_;
typedef internal::image_adaptor_< I, self_ > super_;
public:
decorated_image(I& ima, const D& deco);
~decorated_image();
typedef mln_value(I) value;
typedef mln::value::proxy<const self_> rvalue;
typedef mln::value::proxy<self_> lvalue;
rvalue operator()(const mln_psite(I)& p) const;
lvalue operator()(const mln_psite(I)& p);
mln_value(I) read_(const mln_psite(I)& p) const;
void write_(const mln_psite(I)& p, const mln_value(I)& v);
template <typename V>
struct change_value
{
typedef decorated_image<mln_ch_value(I, V), D> ret;
};
/// Const promotion via convertion.
operator decorated_image<const I, D>() const;
/// Give the decoration.
const D& decoration() const;
/// Give the decoration.
D& decoration();
protected:
D deco_;
};
template <typename I, typename D>
decorated_image<I,D> decorate(Image<I>& ima,
const D& decoration);
template <typename I, typename D>
decorated_image<const I, D> decorate(const Image<I>& ima,
const D& decoration);
# ifndef MLN_INCLUDE_ONLY
// decorated_image<I,D>
template <typename I, typename D>
decorated_image<I,D>::decorated_image(I& ima, const D& deco)
: super_(ima),
deco_(deco)
{
}
template <typename I, typename D>
decorated_image<I,D>::~decorated_image()
{
void (D::*mr)(const I&, const mln_psite(I)&) const = & D::reading;
mr = 0;
typedef mlc_unconst(I) I_;
void (D::*mw)(I_&, const mln_psite(I_)&, const mln_value(I_)&) = & D::writing;
mw = 0;
}
template <typename I, typename D>
mln::value::proxy< const decorated_image<I,D> >
decorated_image<I,D>::operator()(const mln_psite(I)& p) const
{
rvalue tmp(*this, p);
return tmp;
}
template <typename I, typename D>
mln::value::proxy< decorated_image<I,D> >
decorated_image<I,D>::operator()(const mln_psite(I)& p)
{
lvalue tmp(*this, p);
return tmp;
}
namespace internal
{
template <typename I, typename D>
void
helper_decorated_image_write_(decorated_image<I,D>& ima,
const mln_psite(I)& p, const mln_value(I)& v)
{
ima.decoration().writing(ima.adaptee(), p, v);
ima.adaptee()(p) = v;
}
template <typename I, typename D>
void
helper_decorated_image_write_(decorated_image<const I,D>&,
const mln_psite(I)&, const mln_value(I)&)
// FIXME: Static assertion instead.
;
} // end of namespace mln::internal
template <typename I, typename D>
void
decorated_image<I,D>::write_(const mln_psite(I)& p, const mln_value(I)& v)
{
internal::helper_decorated_image_write_(*this, p, v);
}
template <typename I, typename D>
mln_value(I)
decorated_image<I,D>::read_(const mln_psite(I)& p) const
{
deco_.reading(this->adaptee_, p);
return this->adaptee_(p);
}
template <typename I, typename D>
decorated_image<I,D>::operator decorated_image<const I, D>() const
{
decorated_image<const I, D> tmp(this->adaptee_, deco_);
return tmp;
}
template <typename I, typename D>
const D&
decorated_image<I,D>::decoration() const
{
return deco_;
}
template <typename I, typename D>
D&
decorated_image<I,D>::decoration()
{
return deco_;
}
// decorate
template <typename I, typename D>
decorated_image<I, D> decorate(Image<I>& ima,
const D& decoration)
{
decorated_image<I, D> tmp(exact(ima), decoration);
return tmp;
}
template <typename I, typename D>
decorated_image<const I, D> decorate(const Image<I>& ima,
const D& decoration)
{
decorated_image<const I, D> tmp(exact(ima), decoration);
return tmp;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln
#endif // ! MLN_CORE_DECORATED_IMAGE_HH
......@@ -92,6 +92,12 @@ namespace mln
/// Convertion to the underlying (adapted) image.
operator I() const;
/// Give the adaptee (underlying adapted) image.
const I& adaptee() const;
/// Give the adaptee (underlying adapted) image.
I& adaptee();
protected:
I& adaptee_;
......@@ -103,6 +109,20 @@ namespace mln
# ifndef MLN_INCLUDE_ONLY
template <typename I, typename E, typename S>
const I&
image_adaptor_<I,E,S>::adaptee() const
{
return adaptee_;
}
template <typename I, typename E, typename S>
I&
image_adaptor_<I,E,S>::adaptee()
{
return adaptee_;
}
template <typename I, typename E, typename S>
image_adaptor_<I,E,S>::image_adaptor_(I& adaptee)
: adaptee_(adaptee)
......
......@@ -80,7 +80,7 @@ namespace mln
template <typename I>
safe_image<I>::safe_image(I& ima, const mln_value(I)& default_value)
: super_(exact(ima)),
: super_(ima),
default_value_(default_value)
{
}
......
// 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_METAL_MAKE_VEC_HH
# define MLN_CORE_METAL_MAKE_VEC_HH
# include <mln/metal/vec.hh>
namespace mln
{
namespace metal
{
namespace make
{
template <typename T>
mln::metal::vec<1,T> vec(T c0);
template <typename T>
mln::metal::vec<2,T> vec(T c0, T c1);
template <typename T>
mln::metal::vec<3,T> vec(T c0, T c1, T c2);
template <typename T>
mln::metal::vec<4,T> vec(T c0, T c1, T c2, T c3);
# ifndef MLN_INCLUDE_ONLY
template <typename T>
mln::metal::vec<1,T> vec(T c0)
{
mln::metal::vec<1,T> tmp;
tmp[0] = c0;
return tmp;
}
template <typename T>
mln::metal::vec<2,T> vec(T c0, T c1)
{
mln::metal::vec<2,T> tmp;
tmp[0] = c0;
tmp[1] = c1;
return tmp;
}
template <typename T>
mln::metal::vec<3,T> vec(T c0, T c1, T c2)
{
mln::metal::vec<3,T> tmp;
tmp[0] = c0;
tmp[1] = c1;
tmp[2] = c2;
return tmp;
}
template <typename T>
mln::metal::vec<4,T> vec(T c0, T c1, T c2, T c3)
{
mln::metal::vec<4,T> tmp;
tmp[0] = c0;
tmp[1] = c1;
tmp[2] = c2;
tmp[3] = c3;
return tmp;
}
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::metal::make
} // end of namespace mln::metal
} // end of namespace mln
# include <mln/metal/make/vec.hh>
#endif // ! MLN_CORE_METAL_MAKE_VEC_HH
// 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_METAL_UNCONST_HH
# define MLN_METAL_UNCONST_HH
# define mlc_unconst(T) typename mln::metal::unconst< T >::ret
namespace mln
{
namespace metal
{
template <typename T>
struct unconst
{
typedef T ret;
};
template <typename T>
struct unconst< const T >
{
typedef T ret;
};
} // end of namespace mln::metal
} // end of namespace mln
#endif // ! MLN_METAL_UNCONST_HH
......@@ -47,6 +47,9 @@ namespace mln
enum { dim = n };
typedef T coord;
vec();
vec(T (&values)[n]);
T& operator[](unsigned i);
T operator[](unsigned i) const;
......@@ -59,7 +62,23 @@ namespace mln
std::ostream& operator<<(std::ostream& ostr, const vec<n,T>& v);
template <unsigned n, typename T>
bool operator==(const vec<n,T>& lhs, const vec<n,T>& rhs);
# ifndef MLN_INCLUDE_ONLY
template <unsigned n, typename T>
vec<n,T>::vec()
{
}
template <unsigned n, typename T>
vec<n,T>::vec(T (&values)[n])
{
for (unsigned i = 0; i < n; ++i)
coord_[i] = values[i];
}
template <unsigned n, typename T>
T&
......@@ -76,7 +95,9 @@ namespace mln
mln_precondition(i < n);
return coord_[i];
}
// operators
template <unsigned n, typename T>
std::ostream& operator<<(std::ostream& ostr, const vec<n,T>& v)
{
......@@ -86,6 +107,15 @@ namespace mln
return ostr << ']';
}
template <unsigned n, typename T>
bool operator==(const vec<n,T>& lhs, const vec<n,T>& rhs)
{
for (unsigned i = 0; i < n; ++i)
if (lhs[i] != rhs[i])
return false;
return true;
}
# endif // ! MLN_INCLUDE_ONLY
......@@ -94,4 +124,7 @@ namespace mln
} // end of namespace mln
# include <mln/metal/make/vec.hh>
#endif // ! MLN_CORE_METAL_VEC_HH
// 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_VALUE_PROXY_HH
# define MLN_VALUE_PROXY_HH
/*! \file mln/value/proxy.hh
*
* \brief Define a generic proxy class for an image pixel value.
*/
# include <mln/core/concept/value.hh>
# include <mln/value/props.hh>
# include <mln/metal/unconst.hh>
namespace mln
{
namespace value
{
/*! \brief Generic proxy class for an image pixel value.
*
* The parameter \c I is an image type.
*/
template <typename I>
class proxy : public Value< proxy<I> >
{
public:
/// Encoding associated type.
typedef void enc; // FIXME
/// Equivalent associated type.
typedef mln_value(I) equiv;
/// Constructor.
proxy(I& ima, const mln_psite(I)& p);
/// Destructor.
~proxy();
/// Assignment (write access); general version.
template <typename V>
proxy<I>& operator=(const V& v);
/// Assignment (write access); replacement for default op.
proxy<I>& operator=(const proxy<I>& rhs);
/// Assignment (write access); with other proxy.
template <typename II>
proxy<I>& operator=(const proxy<II>& rhs);
/// Convertion (read access); general version.
template <typename V>
operator V() const;
/// Convertion (read access); precise version.
operator mln_value(I)() const;
/// Explicit read access.
mln_value(I) to_value() const;
protected:
I& ima_;
mln_psite(I) p_;
};
/*! \brief Generic proxy class for an image pixel value.
*
* The parameter \c I is an image type.
*/
template <typename I>
class proxy< const I > : public Value< proxy<const I> >
{
public:
/// Encoding associated type.
typedef void enc; // FIXME
/// Equivalent associated type.
typedef mln_value(I) equiv;
/// Constructor.