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