$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
linear/convolve.hh
1 // Copyright (C) 2007, 2008, 2009, 2012 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_LINEAR_CONVOLVE_HH
28 # define MLN_LINEAR_CONVOLVE_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/weighted_window.hh>
36 # include <mln/linear/ch_convolve.hh>
37 # include <mln/accu/convolve.hh>
38 # include <mln/extension/adjust_duplicate.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace linear
45  {
46 
60  template <typename I, typename W>
61  mln_ch_convolve(I, W)
62  convolve(const Image<I>& input, const Weighted_Window<W>& w_win);
63 
64 
65 
66 # ifndef MLN_INCLUDE_ONLY
67 
68  // Tests.
69 
70  namespace internal
71  {
72 
73  template <typename I, typename W>
74  void
75  convolve_tests(const Image<I>& input,
76  const Weighted_Window<W>& w_win)
77  {
78  mln_precondition(exact(input).is_valid());
79  mln_precondition(exact(w_win).is_valid());
80  (void) input;
81  (void) w_win;
82  }
83 
84  } // end of namespace mln::linear::internal
85 
86 
87  // Implementation.
88 
89  namespace impl
90  {
91 
92  namespace generic
93  {
94 
95  template <typename I, typename W>
96  mln_ch_convolve(I, W)
97  convolve(const Image<I>& input_,
98  const Weighted_Window<W>& w_win_)
99  {
100  mln_trace("linear::impl::generic::convolve");
101 
102  const I& input = exact(input_);
103  const W& w_win = exact(w_win_);
104  internal::convolve_tests(input, w_win);
105 
106  extension::adjust_duplicate(input, w_win);
107 
108  typedef mln_ch_convolve(I, W) O;
109  O output;
110  initialize(output, input);
111 
112  accu::convolve<mln_value(I), mln_weight(W)> a;
113 
114  mln_piter(I) p(input.domain());
115  mln_qiter(W) q(w_win, p);
116 
117  for_all(p)
118  {
119  a.init();
120  for_all(q) if (input.has(q))
121  a.take(input(q), q.w());
122  output(p) = a.to_result();
123  }
124 
125  return output;
126  }
127 
128  } // end of namespace mln::linear::impl::generic
129 
130 
131  template <typename I, typename W>
132  mln_ch_convolve(I, W)
133  convolve_fastest(const Image<I>& input_,
134  const Weighted_Window<W>& w_win_)
135  {
136  mln_trace("linear::impl::convolve_fastest");
137 
138  const I& input = exact(input_);
139  const W& w_win = exact(w_win_);
140  internal::convolve_tests(input, w_win);
141 
142  extension::adjust_duplicate(input, w_win);
143 
144  typedef mln_ch_convolve(I, W) O;
145  O output;
146  initialize(output, input);
147  mln_pixter(O) p_out(output);
148 
149  accu::convolve<mln_value(I), mln_weight(W)> a;
150 
151  mln_pixter(const I) p(input);
152  mln_qixter(const I, W) q(p, w_win);
153 
154  for_all_2(p, p_out)
155  {
156  a.init();
157  unsigned i = 0;
158  for_all(q)
159  a.take(q.val(), w_win.w(i++));
160  p_out.val() = a.to_result();
161  }
162 
163  return output;
164  }
165 
166  } // end of namespace mln::linear::impl
167 
168 
169  // Dispatch.
170 
171  namespace internal
172  {
173 
174  template <typename I, typename W>
175  mln_ch_convolve(I, W)
176  convolve_dispatch(trait::image::speed::any,
177  const Image<I>& input,
178  const Weighted_Window<W>& w_win)
179  {
180  return impl::generic::convolve(input, w_win);
181  }
182 
183  template <typename I, typename W>
184  mln_ch_convolve(I, W)
185  convolve_dispatch(trait::image::speed::fastest,
186  const Image<I>& input,
187  const Weighted_Window<W>& w_win)
188  {
189  return impl::convolve_fastest(input, w_win);
190  }
191 
192  template <typename I, typename W>
193  mln_ch_convolve(I, W)
194  convolve_dispatch(const Image<I>& input,
195  const Weighted_Window<W>& w_win)
196  {
197  return convolve_dispatch(mln_trait_image_speed(I)(),
198  input, w_win);
199  }
200 
201  } // end of namespace mln::linear::internal
202 
203 
204  // Facade.
205 
206  template <typename I, typename W>
207  mln_ch_convolve(I, W)
208  convolve(const Image<I>& input, const Weighted_Window<W>& w_win)
209  {
210  mln_trace("linear::convolve");
211 
212  internal::convolve_tests(input, w_win);
213 
214  mln_ch_convolve(I, W) output;
215  output = internal::convolve_dispatch(mln_trait_image_speed(I)(),
216  input, w_win);
217 
218  return output;
219  }
220 
221 # endif // ! MLN_INCLUDE_ONLY
222 
223  } // end of namespace mln::linear
224 
225 } // end of namespace mln
226 
227 
228 #endif // ! MLN_LINEAR_CONVOLVE_HH