Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
accu/transform.hh
1 // Copyright (C) 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_ACCU_TRANSFORM_HH
27 # define MLN_ACCU_TRANSFORM_HH
28 
33 
34 # include <mln/core/concept/meta_accumulator.hh>
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/window.hh>
37 # include <mln/extension/adjust.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace accu
44  {
45 
46  template <typename I, typename A, typename W>
47  mln_ch_value(I, mln_result(A))
48  transform(const Image<I>& input,
49  const Accumulator<A>& a,
50  const Window<W>& win);
51 
52  template <typename I, typename A, typename W>
53  mln_ch_value(I, mln_meta_accu_result(A, mln_value(I)))
54  transform(const Image<I>& input,
55  const Meta_Accumulator<A>& a,
56  const Window<W>& win);
57 
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62 
63  namespace impl
64  {
65 
66  // Generic version.
67 
68  namespace generic
69  {
70 
71  template <typename I, typename A, typename W>
72  mln_ch_value(I, mln_result(A))
73  transform(const Image<I>& input_,
74  const Accumulator<A>& a_,
75  const Window<W>& win_)
76  {
77  trace::entering("accu::impl::generic::transform");
78 
79  const I& input = exact(input_);
80  const W& win = exact(win_);
81  A a = exact(a_);
82 
83  mln_precondition(input.is_valid());
84  mln_precondition(win.is_valid());
85 
86  extension::adjust(input, win);
87 
88  mln_ch_value(I, mln_result(A)) output;
89  initialize(output, input);
90 
91  mln_piter(I) p(input.domain());
92  mln_qiter(W) q(win, p);
93  for_all(p)
94  {
95  a.init();
96  for_all(q) if (input.has(q))
97  a.take(input(q));
98  output(p) = a.to_result();
99  }
100 
101  trace::exiting("accu::impl::generic::transform");
102  return output;
103  }
104 
105  } // end of namespace mln::accu::impl::generic
106 
107 
108  // Fastest version.
109 
110  template <typename I, typename A, typename W>
111  mln_ch_value(I, mln_result(A))
112  transform_fastest(const Image<I>& input_, const Accumulator<A>& a_, const Window<W>& win_)
113  {
114  trace::entering("accu::impl::transform_fastest");
115 
116  const I& input = exact(input_);
117  const W& win = exact(win_);
118  A a = exact(a_);
119 
120  mln_precondition(input.is_valid());
121  mln_precondition(win.is_valid());
122 
123  extension::adjust(input, win);
124 
125  typedef mln_ch_value(I, mln_result(A)) O;
126  O output;
127  initialize(output, input);
128  mln_pixter(O) o(output);
129 
130  mln_pixter(const I) p(input);
131  mln_qixter(const I, W) q(p, win);
132  for_all_2(p, o)
133  {
134  a.init();
135  for_all(q)
136  a.take(q.val());
137  o.val() = a.to_result();
138  }
139 
140  trace::exiting("accu::impl::transform_fastest");
141  return output;
142  }
143 
144 
145  } // end of namespace mln::accu::impl
146 
147 
148  // Dispatch.
149 
150  namespace internal
151  {
152 
153  template <typename I, typename A, typename W>
154  mln_ch_value(I, mln_result(A))
155  transform_dispatch(metal::false_,
156  const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
157  {
158  return impl::generic::transform(input, a, win);
159  }
160 
161  template <typename I, typename A, typename W>
162  mln_ch_value(I, mln_result(A))
163  transform_dispatch(metal::true_,
164  const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
165  {
166  return impl::transform_fastest(input, a, win);
167  }
168 
169  template <typename I, typename A, typename W>
170  mln_ch_value(I, mln_result(A))
171  transform_dispatch(const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
172  {
173  return transform_dispatch(mln_is_fastest_IW(I, W)(),
174  input, a, win);
175  }
176 
177  } // end of namespace mln::accu::internal
178 
179 
180  // Facades.
181 
182  template <typename I, typename A, typename W>
183  inline
184  mln_ch_value(I, mln_result(A))
185  transform(const Image<I>& input, const Accumulator<A>& a, const Window<W>& win)
186  {
187  trace::entering("accu::transform");
188 
189  mln_precondition(exact(input).is_valid());
190  mln_precondition(exact(win).is_valid());
191 
192  mln_ch_value(I, mln_result(A)) output;
193  output = internal::transform_dispatch(input, a, win);
194 
195  trace::exiting("accu::transform");
196  return output;
197  }
198 
199  template <typename I, typename A, typename W>
200  mln_ch_value(I, mln_meta_accu_result(A, mln_value(I)))
201  transform(const Image<I>& input, const Meta_Accumulator<A>& a, const Window<W>& win)
202  {
203  trace::entering("accu::transform");
204 
205  mln_precondition(exact(input).is_valid());
206  mln_precondition(exact(win).is_valid());
207 
208  typedef mln_accu_with(A, mln_value(I)) A_;
209  A_ a_ = accu::unmeta(exact(a), mln_value(I)());
210 
211  mln_ch_value(I, mln_result(A_)) output;
212  output = internal::transform_dispatch(input, a_, win);
213 
214  trace::exiting("accu::transform");
215  return output;
216  }
217 
218 # endif // ! MLN_INCLUDE_ONLY
219 
220  } // end of namespace mln::accu
221 
222 } // end of namespace mln
223 
224 
225 #endif // ! MLN_ACCU_TRANSFORM_HH