Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
arith/plus.hh
1 // Copyright (C) 2007, 2008, 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_ARITH_PLUS_HH
27 # define MLN_ARITH_PLUS_HH
28 
34 
35 # include <mln/arith/includes.hh>
36 
37 
38 namespace mln
39 {
40 
41 
42  namespace trait
43  {
44 
45  template <typename L, typename R>
46  struct set_binary_< op::plus, Image, L, Image, R >
47  {
48  typedef mln_trait_op_plus(mln_value(L), mln_value(R)) value;
49  typedef mln_ch_value(L, value) ret;
50  };
51 
52  template <typename I, typename S>
53  struct set_binary_< op::plus, Image, I, mln::value::Scalar, S >
54  {
55  typedef mln_trait_op_plus(mln_value(I), S) value;
56  typedef mln_ch_value(I, value) ret;
57  };
58 
59  } // end of namespace mln::trait
60 
61 
62 
63  template <typename L, typename R>
64  mln_trait_op_plus(L,R)
65  operator+(const Image<L>& lhs, const Image<R>& rhs);
66 
67 
68  template <typename L, typename R>
69  L&
70  operator+=(Image<L>& lhs, const Image<R>& rhs);
71 
72 
73  template <typename I, typename S>
74  mln_trait_op_plus(I,S)
75  operator+(const Image<I>& ima, const value::Scalar<S>& s);
76 
77 
78  template <typename I, typename S>
79  I&
80  operator+=(Image<I>& ima, const value::Scalar<S>& s);
81 
82 
83 
84  namespace arith
85  {
86 
88 
95  template <typename L, typename R>
96  mln_trait_op_plus(L, R)
97  plus(const Image<L>& lhs, const Image<R>& rhs);
98 
99 
101 
109  template <typename L, typename R, typename F>
110  mln_ch_value(L, mln_result(F))
111  plus(const Image<L>& lhs, const Image<R>& rhs, const Function_v2v<F>& f);
112 
113 
115 
124  template <typename V, typename L, typename R>
125  mln_ch_value(L, V)
126  plus(const Image<L>& lhs, const Image<R>& rhs);
127 
128 
130 
140  template <typename L, typename R>
141  void
142  plus_inplace(Image<L>& lhs, const Image<R>& rhs);
143 
144 
146 
153  template <typename I, typename V>
154  mln_trait_op_plus(I, V)
155  plus_cst(const Image<I>& input, const V& val);
156 
157 
159 
167  template <typename I, typename V, typename F>
168  mln_ch_value(I, mln_result(F))
169  plus_cst(const Image<I>& input, const V& val, const Function_v2v<F>& f);
170 
171 
173 
180  template <typename W, typename I, typename V>
181  mln_ch_value(I, W)
182  plus_cst(const Image<I>& input, const V& val);
183 
184 
186 
192  template <typename I, typename V>
193  I&
194  plus_cst_inplace(Image<I>& input, const V& val);
195 
196 
197  } // end of namespace mln::arith
198 
199 
200 
201 
202 # ifndef MLN_INCLUDE_ONLY
203 
204 
205  template <typename L, typename R>
206  inline
207  mln_trait_op_plus(L,R)
208  operator+(const Image<L>& lhs, const Image<R>& rhs)
209  {
210  trace::entering("operator::plus");
211 
212  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
213 
214  mln_trait_op_plus(L,R) output = arith::plus(lhs, rhs);
215 
216  trace::exiting("operator::plus");
217  return output;
218  }
219 
220  template <typename L, typename R>
221  inline
222  L&
223  operator+=(Image<L>& lhs, const Image<R>& rhs)
224  {
225  trace::entering("operator::plus_eq");
226 
227  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
228 
229  arith::plus_inplace(lhs, rhs);
230 
231  trace::exiting("operator::plus_eq");
232  return exact(lhs);
233  }
234 
235 
236  template <typename I, typename S>
237  inline
238  mln_trait_op_plus(I,S)
239  operator+(const Image<I>& ima, const value::Scalar<S>& s)
240  {
241  trace::entering("operator::plus");
242 
243  mln_precondition(exact(ima).is_valid());
244 
245  mln_trait_op_plus(I,S) output = arith::plus_cst(ima, exact(s));
246 
247  trace::exiting("operator::plus");
248  return output;
249  }
250 
251  template <typename I, typename S>
252  inline
253  I&
254  operator+=(Image<I>& ima, const value::Scalar<S>& s)
255  {
256  trace::entering("operator::plus_eq");
257 
258  mln_precondition(exact(ima).is_valid());
259 
260  arith::plus_cst_inplace(ima, exact(s));
261 
262  trace::exiting("operator::plus_eq");
263  return exact(ima);
264  }
265 
266 
267 
268  namespace arith
269  {
270 
271  namespace impl
272  {
273 
274  template <typename L, typename R, typename O>
275  inline
276  void plus_(trait::image::speed::any, const L& lhs,
277  trait::image::speed::any, const R& rhs, O& output)
278  {
279  mln_piter(L) p(lhs.domain());
280  for_all(p)
281  output(p) = lhs(p) + rhs(p);
282  }
283 
284  template <typename L, typename R, typename F, typename O>
285  inline
286  void plus_(trait::image::speed::any, const L& lhs,
287  trait::image::speed::any, const R& rhs, const F& f, O& output)
288  {
289  mln_piter(L) p(lhs.domain());
290  for_all(p)
291  output(p) = f(lhs(p) + rhs(p));
292  }
293 
294  template <typename L, typename R, typename O>
295  inline
296  void plus_(trait::image::speed::fastest, const L& lhs,
297  trait::image::speed::fastest, const R& rhs, O& output)
298  {
299  mln_pixter(const L) lp(lhs);
300  mln_pixter(const R) rp(rhs);
301  mln_pixter(O) op(output);
302  for_all_3(lp, rp, op)
303  op.val() = lp.val() + rp.val();
304  }
305 
306  template <typename L, typename R, typename F, typename O>
307  inline
308  void plus_(trait::image::speed::fastest, const L& lhs,
309  trait::image::speed::fastest, const R& rhs, const F& f, O& output)
310  {
311  mln_pixter(const L) lp(lhs);
312  mln_pixter(const R) rp(rhs);
313  mln_pixter(O) op(output);
314  for_all_3(lp, rp, op)
315  op.val() = f(lp.val() + rp.val());
316  }
317 
318  template <typename L, typename R>
319  inline
320  void plus_inplace_(trait::image::speed::any, L& lhs,
321  trait::image::speed::any, const R& rhs)
322  {
323  mln_piter(L) p(lhs.domain());
324  for_all(p)
325  lhs(p) += rhs(p);
326  }
327 
328  template <typename L, typename R>
329  inline
330  void plus_inplace_(trait::image::speed::fastest, L& lhs,
331  trait::image::speed::fastest, const R& rhs)
332  {
333  mln_pixter(L) lp(lhs);
334  mln_pixter(const R) rp(rhs);
335  for_all_2(lp, rp)
336  lp.val() += rp.val();
337  }
338 
339  } // end of namespace mln::arith::impl
340 
341 
342  // Facades.
343 
344 
345  template <typename L, typename R>
346  inline
347  mln_trait_op_plus(L, R)
348  plus(const Image<L>& lhs, const Image<R>& rhs)
349  {
350  trace::entering("arith::plus");
351 
352  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
353 
354  mln_trait_op_plus(L, R) output;
355  initialize(output, lhs);
356  impl::plus_(mln_trait_image_speed(L)(), exact(lhs),
357  mln_trait_image_speed(R)(), exact(rhs), output);
358 
359  trace::exiting("arith::plus");
360  return output;
361  }
362 
363 
364  template <typename L, typename R, typename F>
365  inline
366  mln_ch_value(L, mln_result(F))
367  plus(const Image<L>& lhs, const Image<R>& rhs, const Function_v2v<F>& f)
368  {
369  trace::entering("arith::plus");
370 
371  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
372 
373  mln_ch_value(L, mln_result(F)) output;
374  initialize(output, lhs);
375  impl::plus_(mln_trait_image_speed(L)(), exact(lhs),
376  mln_trait_image_speed(R)(), exact(rhs), exact(f), output);
377 
378  trace::exiting("arith::plus");
379  return output;
380  }
381 
382 
383  template <typename V, typename L, typename R>
384  inline
385  mln_ch_value(L, V)
386  plus(const Image<L>& lhs, const Image<R>& rhs)
387  {
388  trace::entering("arith::plus");
389 
390  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
391 
392  // Calls the previous version.
393  mln_ch_value(L, V) output = plus(lhs, rhs,
394  mln::fun::v2v::cast<V>());
395 
396  trace::exiting("arith::plus");
397  return output;
398  }
399 
400 
401  template <typename I, typename V>
402  inline
403  mln_trait_op_plus(I, V)
404  plus_cst(const Image<I>& input, const V& val)
405  {
406  trace::entering("arith::plus_cst");
407 
408  mln_precondition(exact(input).is_valid());
409 
410  // Calls the previous version.
411  mln_trait_op_plus(I, V) output = plus(input,
412  pw::cst(val) | exact(input).domain());
413 
414  trace::exiting("arith::plus_cst");
415  return output;
416  }
417 
418 
419  template <typename I, typename V, typename F>
420  inline
421  mln_ch_value(I, mln_result(F))
422  plus_cst(const Image<I>& input, const V& val, const Function_v2v<F>& f)
423  {
424  trace::entering("arith::plus_cst");
425 
426  mln_precondition(exact(input).is_valid());
427 
428  // Calls the previous version.
429  mln_ch_value(I, mln_result(F)) output = plus(input,
430  pw::cst(val) | exact(input).domain(),
431  f);
432 
433  trace::exiting("arith::plus_cst");
434  return output;
435  }
436 
437 
438  template <typename W, typename I, typename V>
439  inline
440  mln_ch_value(I, W)
441  plus_cst(const Image<I>& input, const V& val)
442  {
443  trace::entering("arith::plus_cst");
444 
445  mln_precondition(exact(input).is_valid());
446 
447  // Calls the previous version.
448  mln_ch_value(I, W) output = plus_cst(input, val,
449  mln::fun::v2v::cast<W>());
450 
451  trace::exiting("arith::plus_cst");
452  return output;
453  }
454 
455 
456  template <typename L, typename R>
457  inline
458  void
459  plus_inplace(Image<L>& lhs, const Image<R>& rhs)
460  {
461  trace::entering("arith::plus_inplace");
462 
463  mln_precondition(exact(rhs).domain() == exact(lhs).domain());
464 
465  impl::plus_inplace_(mln_trait_image_speed(L)(), exact(lhs),
466  mln_trait_image_speed(R)(), exact(rhs));
467 
468  trace::exiting("arith::plus_inplace");
469  }
470 
471 
472  template <typename I, typename V>
473  inline
474  I&
475  plus_cst_inplace(Image<I>& input, const V& val)
476  {
477  trace::entering("arith::plus_cst_inplace");
478 
479  mln_precondition(exact(input).is_valid());
480 
481  // Calls the previous version.
482  plus_inplace(input,
483  pw::cst(val) | exact(input).domain());
484 
485  trace::exiting("arith::plus_cst_inplace");
486  return exact(input);
487  }
488 
489  } // end of namespace mln::arith
490 
491 # endif // ! MLN_INCLUDE_ONLY
492 
493 } // end of namespace mln
494 
495 
496 #endif // ! MLN_ARITH_PLUS_HH