Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
linear/local/convolve.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_LINEAR_LOCAL_CONVOLVE_HH
27 # define MLN_LINEAR_LOCAL_CONVOLVE_HH
28 
32 
33 # include <mln/core/concept/image.hh>
34 # include <mln/core/concept/site.hh>
35 # include <mln/core/concept/generalized_pixel.hh>
36 # include <mln/core/concept/weighted_window.hh>
37 # include <mln/metal/const.hh>
38 
39 
40 
41 namespace mln
42 {
43 
44  namespace linear
45  {
46 
47  namespace local
48  {
49 
59  template <typename I, typename P, typename W, typename R>
60  void convolve(const Image<I>& input,
61  const Site<P>& p,
62  const Weighted_Window<W>& w_win,
63  R& result);
64 
65 
75  template <typename P, typename W, typename R>
76  void convolve(const Generalized_Pixel<P>& p,
77  const Weighted_Window<W>& w_win,
78  R& result);
79 
80 
81 # ifndef MLN_INCLUDE_ONLY
82 
83  namespace impl
84  {
85 
86  template <typename I, typename P, typename W, typename R>
87  inline
88  void convolve(trait::image::speed::any,
89  const I& input,
90  const Site<P>& p_,
91  const W& w_win,
92  R& result)
93  {
94  const P& p = exact(p_);
95 
96  R tmp = literal::zero; // FIXME: zero?
97  mln_qiter(W) q(w_win, p);
98  for_all(q) if (input.has(q))
99  tmp += input(q) * q.w();
100  result = tmp;
101  }
102 
103  template <typename I, typename P, typename W, typename R>
104  inline
105  void convolve(trait::image::speed::fastest,
106  const I& input,
107  const Site<P>& p_,
108  const W& w_win,
109  R& result)
110  {
111  const P& p = exact(p_);
112 
113  mln_precondition(input.border() >= w_win.delta());
114 
115  R tmp = 0;
116  unsigned i = 0;
117  mln_qixter(const I, W) q(input, w_win, p);
118  for_all(q)
119  tmp += w_win.w(i++) * q.val();
120  result = tmp;
121  }
122 
123  template <typename P, typename W, typename R>
124  inline
125  void convolve(const Generalized_Pixel<P>& p_,
126  const W& w_win,
127  R& result)
128  {
129  const P& p = mln::internal::force_exact<P>(p_);
130  mln_precondition(p.ima().border() >= w_win.delta());
131 
132  R tmp = 0;
133  unsigned i = 0;
134  // FIXME: mln_qixter(const P, W) should work
135  // FIXME: so make the trait make this job...
136  mln_qixter(mlc_const(mln_image(P)), W) q(p, w_win);
137  for_all(q)
138  tmp += w_win.w(i++) * q.val();
139  result = tmp;
140  }
141 
142  } // end of namespace mln::linear::impl
143 
144 
145  // Facades.
146 
147  template <typename I, typename P, typename W, typename R>
148  inline
149  void convolve(const Image<I>& input,
150  const Site<P>& p,
151  const Weighted_Window<W>& w_win,
152  R& result)
153  {
154  mln_precondition(exact(input).is_valid());
155  impl::convolve(mln_trait_image_speed(I)(), exact(input),
156  p, exact(w_win), result);
157  }
158 
159  template <typename P, typename W, typename R>
160  inline
162  const Weighted_Window<W>& w_win,
163  R& result)
164  {
165  impl::convolve(p, exact(w_win), result);
166  }
167 
168 # endif // ! MLN_INCLUDE_ONLY
169 
170  } // end of namespace mln::linear::local
171 
172  } // end of namespace mln::linear
173 
174 } // end of namespace mln
175 
176 
177 #endif // ! MLN_LINEAR_LOCAL_CONVOLVE_HH