00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef MLN_DATA_CONVERT_HH
00027 # define MLN_DATA_CONVERT_HH
00028 
00034 
00035 # include <mln/core/routine/duplicate.hh>
00036 # include <mln/fun/v2v/convert.hh>
00037 # include <mln/data/transform.hh>
00038 
00039 
00040 namespace mln
00041 {
00042 
00043   namespace data
00044   {
00045 
00050     
00051     template <typename V, typename I>
00052     mln_ch_value(I, V)
00053     convert(const V& v, const Image<I>& input);
00054 
00055 
00056 
00057 # ifndef MLN_INCLUDE_ONLY
00058 
00059     namespace internal
00060     {
00061 
00062       template <typename V, typename I>
00063       inline
00064       void
00065       convert_tests(const V&, const Image<I>& input)
00066       {
00067         mln_precondition(exact(input).is_valid());
00068         (void) input;
00069       }
00070 
00071     } 
00072 
00073     namespace impl
00074     {
00075 
00076       namespace generic
00077       {
00078 
00079         template <typename V, typename I>
00080         inline
00081         mln_ch_value(I, V)
00082         convert(const V& v, const Image<I>& input)
00083         {
00084           trace::entering("data::impl::generic::convert");
00085           internal::convert_tests(v, input);
00086 
00087           fun::v2v::convert<V> f;
00088           mln_ch_value(I, V) output = data::transform(input, f);
00089 
00090           trace::exiting("data::impl::generic::convert");
00091           return output;
00092         }
00093 
00094       } 
00095 
00096 
00097       template <typename V, typename I>
00098       inline
00099       mln_ch_value(I,V)
00100       convert_identity(const V& v, const Image<I>& input)
00101       {
00102         trace::entering("data::impl::convert_identity");
00103         internal::convert_tests(v, input);
00104 
00105         mln_concrete(I) output = duplicate(input);
00106 
00107         trace::exiting("data::impl::convert_identity");
00108         return output;
00109       }
00110 
00111 
00112     } 
00113 
00114     namespace internal
00115     {
00116 
00117       template <typename V, typename I>
00118       inline
00119       mln_ch_value(I, V)
00120       convert_dispatch(metal::bool_<true>,
00121                        const V& v, const Image<I>& input)
00122       {
00123         return impl::convert_identity(v, input);
00124       }
00125 
00126       template <typename V, typename I>
00127       inline
00128       mln_ch_value(I, V)
00129       convert_dispatch(metal::bool_<false>,
00130                        const V& v, const Image<I>& input)
00131       {
00132         return impl::generic::convert(v, input);
00133       }
00134 
00135       template <typename V, typename I>
00136       inline
00137       mln_ch_value(I, V)
00138       convert_dispatch(const V& v, const Image<I>& input)
00139       {
00140         enum {
00141           test = mlc_equal(V, mln_value(I))::value
00142         };
00143         return convert_dispatch(metal::bool_<test>(),
00144                                 v, input);
00145       }
00146 
00147     } 
00148 
00149     
00150 
00151     template <typename V, typename I>
00152     inline
00153     mln_ch_value(I, V)
00154     convert(const V& v, const Image<I>& input)
00155     {
00156       trace::entering("data::convert");
00157 
00158       internal::convert_tests(v, input);
00159 
00160       mln_ch_value(I, V) output = internal::convert_dispatch(v, input);
00161 
00162       trace::exiting("data::convert");
00163       return output;
00164     }
00165 
00166 
00167 # endif // ! MLN_INCLUDE_ONLY
00168 
00169   } 
00170 
00171 } 
00172 
00173 
00174 #endif // ! MLN_DATA_CONVERT_HH