$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
extension_ima.hh
1 // Copyright (C) 2008, 2009, 2011, 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_DMORPH_EXTENSION_IMA_HH
28 # define MLN_CORE_IMAGE_DMORPH_EXTENSION_IMA_HH
29 
34 
35 # include <mln/core/internal/image_identity.hh>
36 
37 
38 
39 namespace mln
40 {
41 
42  // Forward declaration.
43  template <typename I, typename J> class extension_ima;
44 
45 
46  namespace internal
47  {
48 
52  template <typename I, typename J>
53  struct data< extension_ima<I, J> >
54  {
55  data(I& ima, const J& ext);
56 
57  I ima_;
58  J ext_;
59  };
60 
61  } // end of namespace mln::internal
62 
63 
64  namespace trait
65  {
66 
67  template <typename I, typename J>
68  struct image_< extension_ima<I, J> > : image_< I > // Same as I except...
69  {
70  // ...these changes.
71  typedef trait::image::category::identity_morpher category;
72  typedef mln_internal_trait_image_speed_from(I) speed; // Un-fastest.
73  typedef trait::image::value_access::indirect value_access;
74 
75  // extended domain
76  typedef trait::image::ext_domain::extendable ext_domain;
77  typedef trait::image::ext_value::multiple ext_value;
78  typedef trait::image::ext_io::read_only ext_io;
79  };
80 
81  template <typename I, typename J, typename V>
82  struct ch_value< extension_ima<I, J>, V >
83  {
84  typedef mlc_converts_to(mln_value(J), V) keep_ext;
85  typedef mln_ch_value(I, V) Iv;
86  typedef extension_ima<Iv, J> Iv_ext;
87  typedef mlc_if(keep_ext, Iv_ext, Iv) ret;
88  };
89 
90  } // end of namespace mln::trait
91 
92 
93 
97  //
98  template <typename I, typename J>
100 
101  : public internal::image_identity< I, mln_domain(I), extension_ima<I, J> >,
102  private mlc_converts_to(mln_value(J), mln_value(I))::check_t
103  {
104  public:
105 
107  typedef extension_ima< tag::image_<I>, tag::ext_<J> > skeleton;
108 
110  typedef mln_value(I) value;
111 
113  typedef mln_value(I) rvalue;
114 
116  extension_ima();
117 
119  extension_ima(I& ima, const J& ext);
120 
124  void init_(I& ima, const J& ext);
126 
127 
129  // Tech note: the 'template' allows for multiple input.
130  template <typename P>
131  bool has(const P& p) const;
132 
133 
135  mln_value(I) operator()(const mln_psite(I)& p) const;
136 
138  mln_morpher_lvalue(I) operator()(const mln_psite(I)& p);
139 
140 
142  const J& extension() const;
143  };
144 
145 
147 
148  // init_
149 
150  template <typename I, typename J, typename M>
151  void init_(tag::image_t, extension_ima<I,J>& target, const M& model);
152 
153  template <typename J, typename I>
154  void init_(tag::extension_t, J& target, const extension_ima<I,J>& model);
155 
157 
158 
159 # ifndef MLN_INCLUDE_ONLY
160 
161  // internal::data< extension_ima<I,S> >
162 
163  namespace internal
164  {
165 
166  template <typename I, typename J>
167  inline
168  data< extension_ima<I, J> >::data(I& ima, const J& ext)
169  : ima_(ima),
170  ext_(const_cast<J&>(ext))
171  {
172  }
173 
174  } // end of namespace mln::internal
175 
176  // extension_ima<I, J>
177 
178  template <typename I, typename J>
179  inline
181  {
182  }
183 
184  template <typename I, typename J>
185  inline
186  extension_ima<I, J>::extension_ima(I& ima, const J& ext)
187  {
188  init_(ima, ext);
189  }
190 
191  template <typename I, typename J>
192  inline
193  void
194  extension_ima<I, J>::init_(I& ima, const J& ext)
195  {
196  this->data_ = new internal::data< extension_ima<I, J> >(ima, ext);
197  }
198 
199  template <typename I, typename J>
200  template <typename P>
201  inline
202  bool
203  extension_ima<I, J>::has(const P& p) const
204  {
205  mln_precondition(this->is_valid());
206  mln_precondition(this->data_->ext_.is_valid());
207  return
208  this->data_->ima_.domain().has(p)
209  || this->data_->ext_.has(p);
210  }
211 
212  template <typename I, typename J>
213  inline
214  mln_value(I)
215  extension_ima<I, J>::operator()(const mln_psite(I)& p) const
216  {
217  mln_precondition(this->is_valid());
218  mln_precondition(has(p));
219  // if-else is preferred to the ternary op to allow for the
220  // function result to convert towards the expected return type.
221  if (this->data_->ima_.domain().has(p))
222  return this->data_->ima_(p);
223  else
224  return this->data_->ext_(p);
225  }
226 
227  template <typename I, typename J>
228  inline
229  mln_morpher_lvalue(I)
230  extension_ima<I, J>::operator()(const mln_psite(I)& p)
231  {
232  static mln_value(I) cpy;
233  mln_precondition(this->is_valid());
234  mln_precondition(has(p));
235  // See the above comment about if-else v. ternary.
236  if (this->data_->ima_.domain().has(p))
237  return this->data_->ima_(p);
238  else
239  {
240  // This hack makes this signature valid both in the image
241  // domain and in its extension. It works even if
242  // mln_morpher_lvalue(I) is a mutable reference.
243  cpy = this->data_->ext_(p);
244  return cpy;
245  }
246  }
247 
248  template <typename I, typename J>
249  inline
250  const J&
251  extension_ima<I, J>::extension() const
252  {
253  mln_precondition(this->is_valid());
254  return this->data_->ext_;
255  }
256 
257  // init_
258 
259  template <typename I, typename J, typename M>
260  inline
261  void init_(tag::image_t, extension_ima<I,J>& target, const M& model)
262  {
263  I ima;
264  init_(tag::image, ima, model);
265  J ext;
266  init_(tag::extension, ext, model);
267  target.init_(ima, ext);
268  }
269 
270  template <typename J, typename I>
271  inline
272  void init_(tag::extension_t, J& target, const extension_ima<I,J>& model)
273  {
274  typedef mlc_unconst(J) J_;
275  J_& ext_ = const_cast<J_&>(model.extension());
276  J_& target_ = const_cast<J_&>(target);
277  target_ = ext_;
278  }
279 
280 # endif // ! MLN_INCLUDE_ONLY
281 
282 } // end of namespace mln
283 
284 
285 #endif // ! MLN_CORE_IMAGE_DMORPH_EXTENSION_IMA_HH