Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
decorated_image.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_CORE_IMAGE_IMORPH_DECORATED_IMAGE_HH
27 # define MLN_CORE_IMAGE_IMORPH_DECORATED_IMAGE_HH
28 
29 # include <mln/core/internal/image_identity.hh>
30 
31 # include <mln/value/proxy.hh>
32 
36 
37 
38 namespace mln
39 {
40 
41  // Forward declaration.
42  template <typename I, typename D> struct decorated_image;
43 
44 
45  namespace internal
46  {
47 
48  template <typename I, typename E>
49  struct decorated_image_impl_
50  {
51  typedef mln::value::proxy<E> lvalue;
53  void write_(const mln_psite(I)& p, const mln_value(I)& v);
54  };
55 
56  template <typename I, typename E>
57  struct decorated_image_impl_< const I, E >
58  {
59  typedef mln::value::proxy<const E> lvalue;
60  };
61 
63  template <typename I, typename D>
64  struct data< decorated_image<I,D> >
65  {
66  data(I& ima, const D& deco);
67 
68  I ima_;
69  D deco_;
70  };
71 
72  } // end of namespace mln::internal
73 
74 
75 
79  //
80  template <typename I, typename D>
81  struct decorated_image :
82  public internal::decorated_image_impl_< I, decorated_image<I,D> >,
83  public internal::image_identity< I, mln_domain(I), decorated_image<I,D> >
84  {
85  public:
87  typedef internal::decorated_image_impl_< I, self_ > impl_;
88 
90  typedef mln_psite(I) psite;
91 
93  typedef mln_rvalue(I) rvalue;
95  typedef typename impl_::lvalue lvalue;
96 
99  decorated_image(I& ima, const D& deco);
100 
102  void init_(I& ima, const D& deco);
103 
105  ~decorated_image();
106 
108  typedef decorated_image< tag::image_<I>, tag::data_<D> > skeleton;
109 
111  rvalue operator()(const psite& p) const;
112 
114  lvalue operator()(const psite& p);
115 
117  rvalue read_(const mln_psite(I)& p) const;
118 
120  operator decorated_image<const I, D>() const;
121 
123  const D& decoration() const;
124 
126  D& decoration();
127  };
128 
129 
130 
131  template <typename I, typename D>
132  decorated_image<I,D> decorate(Image<I>& ima,
133  const D& decoration);
134 
135  template <typename I, typename D>
136  decorated_image<const I, D> decorate(const Image<I>& ima,
137  const D& decoration);
138 
139 
140 
141 # ifndef MLN_INCLUDE_ONLY
142 
143  namespace internal
144  {
145 
147  template <typename I, typename D>
148  inline
149  data< decorated_image<I,D> >::data(I& ima, const D& deco)
150  : ima_(ima),
151  deco_(deco)
152  {
153  }
154 
155  } // end of namespace mln::internal
156 
157  // decorated_image<I,D>
158 
159  template <typename I, typename D>
160  inline
162  {
163  }
164 
165  template <typename I, typename D>
166  inline
167  decorated_image<I,D>::decorated_image(I& ima, const D& deco)
168  {
169  mln_precondition(exact(ima).is_valid());
170  this->data_ = new internal::data< decorated_image<I,D> >(ima, deco);
171  }
172 
173  template <typename I, typename D>
174  inline
175  void
176  decorated_image<I,D>::init_(I& ima, const D& deco)
177  {
178  mln_precondition(exact(ima).is_valid());
179  this->data_ = new internal::data<decorated_image<I,D> >(ima, deco);
180  }
181 
182  template <typename I, typename D>
183  inline
184  decorated_image<I,D>::~decorated_image()
185  {
186  void (D::*mr)(const I&, const mln_psite(I)&) const = & D::reading;
187  mr = 0;
188  typedef mlc_unconst(I) I_;
189  void (D::*mw)(I_&, const mln_psite(I_)&, const mln_value(I_)&) =
190  & D::writing;
191  mw = 0;
192  }
193 
194  template <typename I, typename D>
195  inline
196  typename decorated_image<I,D>::rvalue
197  decorated_image<I,D>::operator()(const psite& p) const
198  {
199  mln_precondition(this->delegatee_() != 0);
200  return read_(p);
201  }
202 
203  template <typename I, typename D>
204  inline
207  {
208  mln_precondition(this->delegatee_() != 0);
209  // Return a proxy.
210  return lvalue(*this, p);
211  }
212 
213  template <typename I, typename D>
214  inline
215  mln_rvalue(I)
216  decorated_image<I,D>::read_(const mln_psite(I)& p) const
217  {
218  this->data_->deco_.reading(this->data_->ima_, p);
219  return this->data_->ima_(p);
220  }
221 
222  namespace internal
223  {
224  template <typename I, typename E>
225  inline
226  void
227  decorated_image_impl_<I,E>::write_(const mln_psite(I)& p,
228  const mln_value(I)& v)
229  {
230  E& ima = internal::force_exact<E>(*this);
231  ima.decoration().writing(*ima.delegatee_(), p, v);
232  (*ima.delegatee_())(p) = v;
233  }
234 
235  } // end of namespace mln::internal
236 
237  template <typename I, typename D>
238  inline
240  {
241  decorated_image<const I, D> tmp(this->data_->ima_, this->data_->deco_);
242  return tmp;
243  }
244 
245 
246  template <typename I, typename D>
247  inline
248  const D&
250  {
251  return this->data_->deco_;
252  }
253 
254  template <typename I, typename D>
255  inline
256  D&
258  {
259  return this->data_->deco_;
260  }
261 
262  // decorate
263 
264  template <typename I, typename D>
265  inline
266  decorated_image<I, D> decorate(Image<I>& ima,
267  const D& decoration)
268  {
269  decorated_image<I, D> tmp(exact(ima), decoration);
270  return tmp;
271  }
272 
273  template <typename I, typename D>
274  inline
275  decorated_image<const I, D> decorate(const Image<I>& ima,
276  const D& decoration)
277  {
278  decorated_image<const I, D> tmp(exact(ima), decoration);
279  return tmp;
280  }
281 
282 # endif // ! MLN_INCLUDE_ONLY
283 
284 } // end of namespace mln
285 
286 
287 #endif // ! MLN_CORE_IMAGE_IMORPH_DECORATED_IMAGE_HH