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