Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
data/transform.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_DATA_TRANSFORM_HH
27 # define MLN_DATA_TRANSFORM_HH
28 
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/concept/function.hh>
39 # include <mln/value/set.hh>
40 
41 // Specializations are in:
42 # include <mln/data/transform.spe.hh>
43 
44 
45 namespace mln
46 {
47 
48  namespace data
49  {
50 
60  template <typename I, typename F>
61  mln_ch_value(I, mln_result(F))
62  transform(const Image<I>& input, const Function_v2v<F>& f);
63 
64 
74  template <typename I1, typename I2, typename F>
75  mln_ch_value(I1, mln_result(F))
76  transform(const Image<I1>& input1,
77  const Image<I2>& input2,
78  const Function_vv2v<F>& f);
79 
80 
81 
82 # ifndef MLN_INCLUDE_ONLY
83 
84  namespace internal
85  {
86 
87  template <typename I, typename F>
88  void transform_tests(const Image<I>& input,
89  const Function_v2v<F>& f)
90  {
91  // Dynamic test.
92  mln_precondition(exact(input).is_valid());
93 
94  // Avoid a warning about an undefined variable when NDEBUG
95  // is not defined.
96  (void) input;
97  (void) f;
98  }
99 
100  template <typename I1, typename I2, typename F>
101  void transform_tests(const Image<I1>& input1, const Image<I2>& input2,
102  const Function_vv2v<F>& f)
103  {
104  // Dynamic tests.
105  mln_precondition(exact(input1).is_valid());
106  mln_precondition(exact(input2).is_valid());
107  mln_precondition(exact(input2).domain() == exact(input1).domain());
108 
109  // Avoid a warning about an undefined variable when NDEBUG
110  // is not defined.
111  (void) input1;
112  (void) input2;
113  (void) f;
114  }
115 
116  } // end of namespace mln::data::internal
117 
118 
119 
120  namespace impl
121  {
122 
123 
124  // Generic implementations.
125 
126 
127  namespace generic
128  {
129 
134  //
135  template <typename I, typename F>
136  mln_ch_value(I, mln_result(F))
137  transform(const Image<I>& input_, const Function_v2v<F>& f_)
138  {
139  trace::entering("data::impl::generic::transform");
140 
141  const I& input = exact(input_);
142  const F& f = exact(f_);
143 
144  data::internal::transform_tests(input, f);
145 
146  mln_ch_value(I, mln_result(F)) output;
147  initialize(output, input);
148 
149  mln_piter(I) p(input.domain());
150  for_all(p)
151  output(p) = f(input(p));
152 
153  trace::exiting("data::impl::generic::transform");
154  return output;
155  }
156 
157 
163  //
164  template <typename I1, typename I2, typename F>
165  mln_ch_value(I1, mln_result(F))
166  transform(const Image<I1>& input1_,
167  const Image<I2>& input2_,
168  const Function_vv2v<F>& f_)
169  {
170  trace::entering("data::impl::generic::transform");
171 
172  const I1& input1 = exact(input1_);
173  const I2& input2 = exact(input2_);
174  const F& f = exact(f_);
175 
176  data::internal::transform_tests(input1, input2, f);
177 
178  mln_ch_value(I1, mln_result(F)) output;
179  initialize(output, input1);
180 
181  mln_piter(I1) p(input1.domain());
182  for_all(p)
183  output(p) = f(input1(p), input2(p));
184 
185  trace::exiting("data::impl::generic::transform");
186  return output;
187  }
188 
189  } // end of namespace mln::data::impl::generic
190 
191 
192  } // end of namespace mln::data::impl
193 
194 
195 
196  // Facades.
197 
198 
199  template <typename I, typename F>
200  inline
201  mln_ch_value(I, mln_result(F))
202  transform(const Image<I>& input, const Function_v2v<F>& f)
203  {
204  trace::entering("data::transform");
205 
206  internal::transform_tests(input, f);
207 
208  mln_ch_value(I, mln_result(F)) output;
209  output = internal::transform_dispatch(input, f);
210 
211  trace::exiting("data::transform");
212  return output;
213  }
214 
215 
216  template <typename I1, typename I2, typename F>
217  inline
218  mln_ch_value(I1, mln_result(F))
219  transform(const Image<I1>& input1, const Image<I2>& input2,
220  const Function_vv2v<F>& f)
221  {
222  trace::entering("data::transform");
223 
224  internal::transform_tests(input1, input2, f);
225 
226  mln_ch_value(I1, mln_result(F)) output;
227  output = internal::transform_dispatch(input1, input2, f);
228 
229  trace::exiting("data::transform");
230  return output;
231  }
232 
233 
234 # endif // ! MLN_INCLUDE_ONLY
235 
236  } // end of namespace mln::data
237 
238 } // end of namespace mln
239 
240 
241 #endif // ! MLN_DATA_TRANSFORM_HH