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 
00027 #ifndef MLN_DATA_MEMCPY__HH
00028 # define MLN_DATA_MEMCPY__HH
00029 
00035 
00036 # include <cstring>
00037 # include <mln/core/concept/image.hh>
00038 # include <mln/core/pixel.hh>
00039 # include <mln/metal/is_not_const.hh>
00040 # include <mln/opt/element.hh>
00041 
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace data
00048   {
00049 
00065     template <typename Pd, typename Ps>
00066     void memcpy_(Generalized_Pixel<Pd>& dest, const Generalized_Pixel<Ps>& src,
00067                  std::size_t n);
00068 
00069 
00070 # ifndef MLN_INCLUDE_ONLY
00071 
00072     namespace impl
00073     {
00074 
00075       template <typename Pd, typename Ps>
00076       inline
00077       void memcpy__(Pd& dest, const Ps& src, std::size_t n)
00078       {
00079         
00080 
00081         typedef mln_image(Pd) Id;
00082         typedef mln_image(Ps) Is;
00083         if (n == 0)
00084           return; 
00085 
00086         if (n == 1)
00087         {
00088           dest.val() = src.val(); 
00089           return;
00090         }
00091 
00092         
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103         if (sizeof(mln_value(Id)) == 1)
00104         {
00105           std::memcpy((void*) (&dest.val()), 
00106                       (const void*) (&src.val()),
00107                       n);
00108         }
00109         else
00110         {
00111           mln_value(Id)* p_d = &dest.val();
00112           const mln_value(Is)* p_s = &src.val();
00113           for (std::size_t i = 0; i < n; ++i)
00114             *p_d++ = *p_s++;
00115         }
00116 
00117         
00118       }
00119 
00120     }
00121 
00122     template <typename Pd, typename Ps>
00123     inline
00124     void memcpy_(Generalized_Pixel<Pd>& dest_,
00125                  const Generalized_Pixel<Ps>& src_,
00126                  std::size_t n)
00127     {
00128       
00129 
00130       typedef mln_image(Pd) Id;
00131       metal::is_not_const<Id>::check();
00132       typedef mln_image(Ps) Is;
00133       Pd& dest = mln::internal::force_exact<Pd>(dest_);
00134       Ps& src  = mln::internal::force_exact<Ps>(src_);
00135 
00136       mln_precondition(sizeof(mln_value(Id)) == sizeof(mln_value(Is)));
00137       mln_precondition(dest.ima().is_valid());
00138       mln_precondition(src.ima().is_valid());
00139 
00140       mln_precondition(&dest.val() >= &opt::element(dest.ima(), 0));
00141       mln_precondition(&dest.val() < &opt::element(dest.ima(), 0) +
00142                        opt::nelements(dest.ima()));
00143 
00144       mln_precondition(&dest.val() + n <= &opt::element(dest.ima(), 0) +
00145                        opt::nelements(dest.ima()));
00146 
00147       mln_precondition(&src.val() >= &opt::element(src.ima(), 0));
00148       mln_precondition(&src.val() < &opt::element(src.ima(), 0) +
00149                        opt::nelements(src.ima()));
00150       mln_precondition(&src.val() + n <= &opt::element(src.ima(), 0) +
00151                        opt::nelements(src.ima()));
00152 
00153       impl::memcpy__(dest, src, n);
00154 
00155       
00156     }
00157 
00158 # endif // ! MLN_INCLUDE_ONLY
00159 
00160   } 
00161 
00162 } 
00163 
00164 
00165 #endif // ! MLN_DATA_MEMCPY__HH