$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
take_as_init.hh
1 // Copyright (C) 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_ACCU_IMAGE_TAKE_AS_INIT_HH
27 # define MLN_ACCU_IMAGE_TAKE_AS_INIT_HH
28 
32 
33 # include <mln/core/concept/accumulator.hh>
34 # include <mln/core/concept/image.hh>
35 # include <mln/border/resize_equal.hh>
36 
37 
38 namespace mln
39 {
40 
41  namespace accu
42  {
43 
44  namespace image
45  {
46 
47  template <typename I>
48  void
49  take_as_init(Image<I>& input, const mln_deduce(I, value, argument)& v);
50 
51  template <typename I, typename J>
52  void
53  take_as_init(Image<I>& input, const Image<J>& values);
54 
55 
56 
57 # ifndef MLN_INCLUDE_ONLY
58 
59  // Tests.
60 
61  namespace internal
62  {
63 
64  template <typename I, typename J>
65  inline
66  void
67  take_as_init_tests(Image<I>& input_, const Image<J>& values_)
68  {
69  I& input = exact(input_);
70  const J& values = exact(values_);
71 
72  mln_precondition(input.is_valid());
73  mln_precondition(values.is_valid());
74 
75  mln_precondition(values.domain() <= input.domain());
76 
77  (void) input;
78  (void) values;
79  }
80 
81  } // end of namespace mln::accu::image::internal
82 
83 
84  namespace impl
85  {
86 
87  // Generic versions.
88 
89  namespace generic
90  {
91 
92  template <typename I>
93  void
94  take_as_init(Image<I>& input_, const mln_deduce(I, value, argument)& v)
95  {
96  mln_trace("accu::impl::image::generic::take_as_init");
97 
98  mlc_is_a(mln_value(I), Accumulator)::check();
99 
100  I& input = exact(input_);
101  mln_precondition(input.is_valid());
102 
103  mln_piter(I) p(input.domain());
104  for_all(p)
105  input(p).take_as_init(v);
106 
107  }
108 
109  template <typename I, typename J>
110  void
111  take_as_init(Image<I>& input_, const Image<J>& values_)
112  {
113  mln_trace("accu::impl::image::generic::take_as_init");
114 
115  typedef mln_value(I) A;
116  mlc_is_a(A, Accumulator)::check();
117  mlc_converts_to(mln_value(J), mln_argument(A))::check();
118 
119  I& input = exact(input_);
120  const J& values = exact(values_);
121 
122  internal::take_as_init_tests(input, values);
123 
124  mln_piter(J) p(values.domain());
125  for_all(p)
126  input(p).take_as_init(values(p));
127 
128  }
129 
130  } // end of namespace mln::accu::image::impl::generic
131 
132 
133  // Fastest versions.
134 
135  template <typename I>
136  void
137  take_as_init_fastest(Image<I>& input_, const mln_deduce(I, value, argument)& v)
138  {
139  mln_trace("accu::impl::image::take_as_init_fastest");
140 
141  mlc_is_a(mln_value(I), Accumulator)::check();
142 
143  I& input = exact(input_);
144  mln_precondition(input.is_valid());
145 
146  mln_pixter(I) px(input);
147  for_all(px)
148  px.val().take_as_init(v);
149 
150  }
151 
152  template <typename I, typename J>
153  void
154  take_as_init_fastest(Image<I>& input_, const Image<J>& values_)
155  {
156  mln_trace("accu::impl::image::take_as_init_fastest");
157 
158  typedef mln_value(I) A;
159  mlc_is_a(A, Accumulator)::check();
160  mlc_converts_to(mln_value(J), mln_argument(A))::check();
161 
162  I& input = exact(input_);
163  const J& values = exact(values_);
164 
165  internal::take_as_init_tests(input, values);
166  // Extra test:
167  mln_precondition(values.domain() == input.domain());
168 
169  border::resize_equal(input, values);
170 
171  mln_pixter(I) p_in(input);
172  mln_pixter(const J) p_v(values);
173  for_all_2(p_in, p_v)
174  p_in.val().take_as_init(p_v.val());
175 
176  }
177 
178  } // end of namespace mln::accu::image::impl
179 
180 
181 
182  // Dispatch.
183 
184  namespace internal
185  {
186 
187  // With a single value.
188 
189  template <typename I, typename V>
190  inline
191  void
192  take_as_init_dispatch(trait::image::speed::any,
193  Image<I>& input, const V& v)
194  {
195  impl::generic::take_as_init(input, v);
196  }
197 
198  template <typename I, typename V>
199  inline
200  void
201  take_as_init_dispatch(trait::image::speed::fastest,
202  Image<I>& input, const V& v)
203  {
204  impl::take_as_init_fastest(input, v);
205  }
206 
207  template <typename I, typename V>
208  inline
209  void
210  take_as_init_dispatch(Image<I>& input, const V& v)
211  {
212  take_as_init_dispatch(mln_trait_image_speed(I)(),
213  input, v);
214  }
215 
216  // With an image of values.
217 
218  template <typename I, typename J>
219  inline
220  void
221  take_as_init_dispatch(trait::image::speed::any,
222  trait::image::speed::any,
223  Image<I>& input, const Image<J>& values)
224  {
225  impl::generic::take_as_init(input, values);
226  }
227 
228  template <typename I, typename J>
229  inline
230  void
231  take_as_init_dispatch(trait::image::speed::fastest,
232  trait::image::speed::fastest,
233  Image<I>& input, const Image<J>& values)
234  {
235  if (exact(values).domain() == exact(input).domain())
236  impl::take_as_init_fastest(input, values);
237  else
238  impl::generic::take_as_init(input, values);
239  }
240 
241  template <typename I, typename J>
242  inline
243  void
244  take_as_init_dispatch(Image<I>& input,
245  const Image<J>& values)
246  {
247  take_as_init_dispatch(mln_trait_image_speed(I)(),
248  mln_trait_image_speed(J)(),
249  input, values);
250  }
251 
252  } // end of namespace mln::accu::image::internal
253 
254 
255  // Facades.
256 
257  template <typename I>
258  inline
259  void
260  take_as_init(Image<I>& input, const mln_deduce(I, value, argument)& v)
261  {
262  mln_trace("accu::image::take_as_init");
263 
264  mlc_is_a(mln_value(I), Accumulator)::check();
265 
266  mln_precondition(exact(input).is_valid());
267  internal::take_as_init_dispatch(input, v);
268 
269  }
270 
271  template <typename I, typename J>
272  inline
273  void
274  take_as_init(Image<I>& input, const Image<J>& values)
275  {
276  mln_trace("accu::image::take_as_init");
277 
278  typedef mln_value(I) A;
279  mlc_is_a(A, Accumulator)::check();
280  mlc_converts_to(mln_value(J), mln_argument(A))::check();
281 
282  internal::take_as_init_tests(input, values);
283  internal::take_as_init_dispatch(input, values);
284 
285  }
286 
287 # endif // ! MLN_INCLUDE_ONLY
288 
289  } // end of namespace mln::accu::image
290 
291  } // end of namespace mln::accu
292 
293 } // end of namespace mln
294 
295 
296 #endif // ! MLN_ACCU_IMAGE_TAKE_AS_INIT_HH