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