$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mln/data/convert.hh
1 // Copyright (C) 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_DATA_CONVERT_HH
28 # define MLN_DATA_CONVERT_HH
29 
35 
36 # include <mln/core/routine/duplicate.hh>
37 # include <mln/fun/v2v/convert.hh>
38 # include <mln/data/transform.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace data
45  {
46 
54  template <typename V, typename I>
55  mln_ch_value(I, V)
56  convert(const V& v, const Image<I>& input);
57 
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62  namespace internal
63  {
64 
65  template <typename V, typename I>
66  inline
67  void
68  convert_tests(const V&, const Image<I>& input)
69  {
70  mln_precondition(exact(input).is_valid());
71  (void) input;
72  }
73 
74  } // using namespace mln::data::internal
75 
76  namespace impl
77  {
78 
79  namespace generic
80  {
81 
82  template <typename V, typename I>
83  inline
84  mln_ch_value(I, V)
85  convert(const V& v, const Image<I>& input)
86  {
87  mln_trace("data::impl::generic::convert");
88  internal::convert_tests(v, input);
89 
90  fun::v2v::convert<V> f;
91  mln_ch_value(I, V) output = data::transform(input, f);
92 
93  return output;
94  }
95 
96  } // end of namespace mln::data::impl::generic
97 
98 
99  template <typename V, typename I>
100  inline
101  mln_ch_value(I,V)
102  convert_identity(const V& v, const Image<I>& input)
103  {
104  mln_trace("data::impl::convert_identity");
105  internal::convert_tests(v, input);
106 
107  mln_concrete(I) output = duplicate(input);
108 
109  return output;
110  }
111 
112 
113  } // end of namespace mln::data::impl
114 
115  namespace internal
116  {
117 
118  template <typename V, typename I>
119  inline
120  mln_ch_value(I, V)
121  convert_dispatch(metal::bool_<true>,
122  const V& v, const Image<I>& input)
123  {
124  return impl::convert_identity(v, input);
125  }
126 
127  template <typename V, typename I>
128  inline
129  mln_ch_value(I, V)
130  convert_dispatch(metal::bool_<false>,
131  const V& v, const Image<I>& input)
132  {
133  return impl::generic::convert(v, input);
134  }
135 
136  template <typename V, typename I>
137  inline
138  mln_ch_value(I, V)
139  convert_dispatch(const V& v, const Image<I>& input)
140  {
141  enum {
142  test = mlc_equal(V, mln_value(I))::value
143  };
144  return convert_dispatch(metal::bool_<test>(),
145  v, input);
146  }
147 
148  } // end of namespace mln::data::internal
149 
150  // Facade.
151 
152  template <typename V, typename I>
153  inline
154  mln_ch_value(I, V)
155  convert(const V& v, const Image<I>& input)
156  {
157  mln_trace("data::convert");
158 
159  internal::convert_tests(v, input);
160 
161  mln_ch_value(I, V) output = internal::convert_dispatch(v, input);
162 
163  return output;
164  }
165 
166 
167 # endif // ! MLN_INCLUDE_ONLY
168 
169  } // end of namespace mln::data
170 
171 } // end of namespace mln
172 
173 
174 #endif // ! MLN_DATA_CONVERT_HH