$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
transform_inplace.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_DATA_TRANSFORM_INPLACE_HH
27 # define MLN_DATA_TRANSFORM_INPLACE_HH
28 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/core/concept/function.hh>
37 # include <mln/value/set.hh>
38 # include <mln/value/lut_vec.hh>
39 # include <mln/opt/value.hh>
40 
41 
42 namespace mln
43 {
44 
45  namespace data
46  {
47 
57  template <typename I, typename F>
58  void
59  transform_inplace(Image<I>& ima, const Function_v2v<F>& f);
60 
61 
72  template <typename I1, typename I2, typename F>
73  void
74  transform_inplace(Image<I1>& ima, const Image<I2>& aux,
75  const Function_vv2v<F>& f);
76 
77 
78 
79 # ifndef MLN_INCLUDE_ONLY
80 
81 
82  // Tests.
83 
84  namespace internal
85  {
86 
87  template <typename I, typename F>
88  inline
89  void transform_inplace_tests(const Image<I>& ima,
90  const Function_v2v<F>& f)
91  {
92  // Properties checks.
93  mlc_or(mlc_is(mln_trait_image_pw_io(I),
94  trait::image::pw_io::read_write),
95  mlc_is(mln_trait_image_vw_io(I),
96  trait::image::vw_io::read_write))::check();
97  mlc_converts_to(mln_result(F), mln_value(I))::check();
98 
99  // Dynamic test.
100  mln_precondition(exact(ima).is_valid());
101 
102  // Avoid a warning.
103  (void) ima;
104  (void) f;
105  }
106 
107  template <typename I1, typename I2, typename F>
108  inline
109  void transform_inplace_tests(Image<I1>& ima,
110  const Image<I2>& aux,
111  const Function_vv2v<F>& f)
112  {
113  // Properties checks.
114  mlc_or(mlc_is(mln_trait_image_pw_io(I1),
115  trait::image::pw_io::read_write),
116  mlc_is(mln_trait_image_vw_io(I1),
117  trait::image::vw_io::read_write))::check();
118  mlc_converts_to(mln_result(F), mln_value(I1))::check();
119 
120  // Dynamic test.
121  mln_precondition(exact(ima).is_valid());
122  mln_precondition(exact(aux).is_valid());
123  mln_precondition(exact(aux).domain() == exact(ima).domain());
124 
125  // Avoid warnings.
126  (void) ima;
127  (void) aux;
128  (void) f;
129  }
130 
131  } // end of namespace mln::data::internal
132 
133 
134  namespace impl
135  {
136 
137  // Generic implementations.
138 
139  namespace generic
140  {
141 
146  //
147  template <typename I, typename F>
148  void
149  transform_inplace(Image<I>& ima_, const Function_v2v<F>& f_)
150  {
151  mln_trace("data::impl::generic::transform_inplace");
152 
153  mlc_is(mln_trait_image_pw_io(I),
154  trait::image::pw_io::read_write)::check();
155 
156  I& ima = exact(ima_);
157  const F& f = exact(f_);
158 
159  internal::transform_inplace_tests(ima, f);
160 
161  mln_piter(I) p(ima.domain());
162  for_all(p)
163  ima(p) = f(ima(p));
164 
165  }
166 
172  //
173  template <typename I1, typename I2, typename F>
174  void
175  transform_inplace(Image<I1>& ima_, const Image<I2>& aux_,
176  const Function_vv2v<F>& f_)
177  {
178  mln_trace("data::impl::generic::transform_inplace");
179 
180  mlc_is(mln_trait_image_pw_io(I1),
181  trait::image::pw_io::read_write)::check();
182 
183  I1& ima = exact(ima_);
184  const I2& aux = exact(aux_);
185  const F& f = exact(f_);
186 
187  internal::transform_inplace_tests(ima, aux, f);
188 
189  mln_piter(I1) p(ima.domain());
190  for_all(p)
191  ima(p) = f(ima(p), aux(p));
192 
193  }
194 
195  } // end of namespace mln::data::impl::generic
196 
198 
199  template <typename I, typename F>
200  void
201  transform_inplace_lowq(Image<I>& input_,
202  const Function_v2v<F>& f_)
203  {
204  mln_trace("data::impl::transform_inplace_lowq");
205 
206  mlc_is(mln_trait_image_pw_io(I),
207  trait::image::pw_io::read_write)::check();
208 
209  I& input = exact(input_);
210  const F& f = exact(f_);
211 
212  internal::transform_inplace_tests(input, f);
213 
214  value::lut_vec<mln_vset(I), mln_result(F)>
215  lut(input.values_eligible(), f);
216 
217  mln_piter(I) p(input.domain());
218  for_all(p)
219  input(p) = lut(input(p));
220 
221  }
222 
223  template <typename I, typename F>
224  void
225  transform_inplace_taken(Image<I>& input_,
226  const Function_v2v<F>& f_)
227  {
228  mln_trace("data::impl::transform_inplace_taken");
229 
230  mlc_is(mln_trait_image_pw_io(I),
231  trait::image::pw_io::read_write)::check();
232 
233  I& input = exact(input_);
234  const F& f = exact(f_);
235 
236  internal::transform_inplace_tests(input, f);
237 
238  value::lut_vec<mln_vset(I), mln_result(F)>
239  lut(input.taken_values(), f);
240 
241  mln_piter(I) p(input.domain());
242  for_all(p)
243  input(p) = lut(input(p));
244 
245  }
246 
247 
248  template <typename I, typename F>
249  void
250  transform_inplace_singleton(Image<I>& input_,
251  const Function_v2v<F>& f_)
252  {
253  mln_trace("data::impl::transform_inplace_singleton");
254 
255  I& input = exact(input_);
256  const F& f = exact(f_);
257 
258  internal::transform_inplace_tests(input, f);
259 
260  opt::value(input) = f(opt::value(input));
261 
262  }
263 
264  template <typename I, typename F>
265  void
266  transform_inplace_fastest(Image<I>& ima_, const Function_v2v<F>& f_)
267  {
268  mln_trace("data::impl::transform_inplace_fastest");
269 
270  I& ima = exact(ima_);
271  const F& f = exact(f_);
272 
273  internal::transform_inplace_tests(ima, f);
274 
275  mln_pixter(I) p(ima);
276  for_all(p)
277  p.val() = f(p.val());
278 
279  }
280 
281 
282  template <typename I, typename F>
283  void
284  transform_inplace_fastest_lowq(Image<I>& input_,
285  const Function_v2v<F>& f_)
286  {
287  mln_trace("data::impl::transform_inplace_fastest_lowq");
288 
289  I& input = exact(input_);
290  const F& f = exact(f_);
291 
292  internal::transform_inplace_tests(input, f);
293 
294  value::lut_vec<mln_vset(I), mln_result(F)>
295  lut(input.values_eligible(), f);
296 
297  mln_pixter(I) pi(input);
298  for_all(pi)
299  pi.val() = lut(pi.val());
300 
301  }
302 
303 
304  template <typename I1, typename I2, typename F>
305  void
306  transform_inplace_fastest(Image<I1>& ima_, const Image<I2>& aux_,
307  const Function_vv2v<F>& f_)
308  {
309  mln_trace("data::impl::transform_inplace_fastest");
310 
311  mlc_is(mln_trait_image_pw_io(I1),
312  trait::image::pw_io::read_write)::check();
313 
314  I1& ima = exact(ima_);
315  const I2& aux = exact(aux_);
316  const F& f = exact(f_);
317 
318  internal::transform_inplace_tests(ima, aux, f);
319 
320  mln_pixter(I1) pi(ima);
321  mln_pixter(const I2) pa(aux);
322  for_all_2(pi, pa)
323  pi.val() = f(pi.val(), pa.val());
324 
325  }
326 
327 
328  } // end of namespace mln::data::impl
329 
330 
331 
332  // Dispatch.
333 
334  namespace internal
335  {
336 
337  // (ima, f) version.
338 
340  template <typename I, typename F>
341  void
342  transform_inplace_dispatch(trait::image::vw_set::any,
343  trait::image::quant::any,
344  Image<I>& ima, const Function_v2v<F>& f)
345  {
347  }
348 
349  template <typename I, typename F>
350  void
351  transform_inplace_dispatch(trait::image::vw_set::uni,
352  trait::image::quant::any,
353  Image<I>& ima, const Function_v2v<F>& f)
354  {
355  data::impl::transform_inplace_taken(ima, f);
356  }
357 
358  template <typename I, typename F>
359  void
360  transform_inplace_dispatch(trait::image::vw_set::any,
361  trait::image::quant::low,
362  Image<I>& ima, const Function_v2v<F>& f)
363  {
364  data::impl::transform_inplace_lowq(ima, f);
365  }
366 
367 
368 
370  template <typename I, typename F>
371  void
372  transform_inplace_dispatch_fast(trait::image::quant::any,
373  Image<I>& ima, const Function_v2v<F>& f)
374  {
375  data::impl::transform_inplace_fastest(ima, f);
376  }
377 
378  template <typename I, typename F>
379  void
380  transform_inplace_dispatch_fast(trait::image::quant::low,
381  Image<I>& ima, const Function_v2v<F>& f)
382  {
383  data::impl::transform_inplace_fastest_lowq(ima, f);
384  }
385 
386 
387 
388 
390  template <typename I, typename F>
391  void
392  transform_inplace_dispatch(trait::image::value_storage::any,
393  trait::image::value_access::any,
394  Image<I>& ima, const Function_v2v<F>& f)
395  {
396  transform_inplace_dispatch(mln_trait_image_vw_set(I)(),
397  mln_trait_image_quant(I)(),
398  ima, f);
399  }
400 
401  template <typename I, typename F>
402  void
403  transform_inplace_dispatch(trait::image::value_storage::singleton,
404  trait::image::value_access::any,
405  Image<I>& ima, const Function_v2v<F>& f)
406  {
407  data::impl::transform_inplace_singleton(ima, f);
408  }
409 
410 
411  template <typename I, typename F>
412  void
413  transform_inplace_dispatch(trait::image::value_storage::one_block,
414  trait::image::value_access::direct,
415  Image<I>& ima, const Function_v2v<F>& f)
416  {
417  transform_inplace_dispatch_fast(mln_trait_image_quant(I)(),
418  ima, f);
419  }
420 
421 
422 
423 
425  template <typename I, typename F>
426  void
427  transform_inplace_dispatch(Image<I>& ima, const Function_v2v<F>& f)
428  {
429  transform_inplace_dispatch(mln_trait_image_value_storage(I)(),
430  mln_trait_image_value_access(I)(),
431  ima, f);
432  }
433 
434 
435 
436  // (ima, aux, f) version.
437 
438  template <typename I1, typename I2, typename F>
439  void
440  transform_inplace_dispatch(trait::image::value_alignment::any,
441  trait::image::value_alignment::any,
442  trait::image::speed::any,
443  trait::image::speed::any,
444  Image<I1>& ima, const Image<I2>& aux,
445  const Function_vv2v<F>& f)
446  {
448  }
449 
450  template <typename I1, typename I2, typename F>
451  void
452  transform_inplace_dispatch(trait::image::value_alignment::with_grid,
453  trait::image::value_alignment::with_grid,
454  trait::image::speed::fastest,
455  trait::image::speed::fastest,
456  Image<I1>& ima, const Image<I2>& aux,
457  const Function_vv2v<F>& f)
458  {
459  data::impl::transform_inplace_fastest(ima, aux, f);
460  }
461 
462  template <typename I1, typename I2, typename F>
463  void
464  transform_inplace_dispatch(Image<I1>& ima, const Image<I2>& aux,
465  const Function_vv2v<F>& f)
466  {
467  transform_inplace_dispatch(mln_trait_image_value_alignment(I1)(),
468  mln_trait_image_value_alignment(I2)(),
469  mln_trait_image_speed(I1)(),
470  mln_trait_image_speed(I2)(),
471  ima, aux, f);
472  }
473 
474  } // end of namespace mln::data::internal
475 
476 
477 
478  // Facades.
479 
480  template <typename I, typename F>
481  void
482  transform_inplace(Image<I>& ima, const Function_v2v<F>& f)
483  {
484  mln_trace("data::transform_inplace");
485 
486  internal::transform_inplace_tests(ima, f);
487  internal::transform_inplace_dispatch(ima, f);
488 
489  }
490 
491  template <typename I1, typename I2, typename F>
492  void
493  transform_inplace(Image<I1>& ima, const Image<I2>& aux,
494  const Function_vv2v<F>& f)
495  {
496  mln_trace("data::transform_inplace");
497 
498  internal::transform_inplace_tests(ima, aux, f);
499  internal::transform_inplace_dispatch(ima, aux, f);
500 
501  }
502 
503 
504 # endif // ! MLN_INCLUDE_ONLY
505 
506  } // end of namespace mln::data
507 
508 } // end of namespace mln
509 
510 
511 #endif // ! MLN_DATA_TRANSFORM_INPLACE_HH