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