Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development 00002 // Laboratory (LRDE) 00003 // 00004 // This file is part of Olena. 00005 // 00006 // Olena is free software: you can redistribute it and/or modify it under 00007 // the terms of the GNU General Public License as published by the Free 00008 // Software Foundation, version 2 of the License. 00009 // 00010 // Olena is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00017 // 00018 // As a special exception, you may use this file as part of a free 00019 // software project without restriction. Specifically, if other files 00020 // instantiate templates or use macros or inline functions from this 00021 // file, or you compile this file and link it with other files to produce 00022 // an executable, this file does not by itself cause the resulting 00023 // executable to be covered by the GNU General Public License. This 00024 // exception does not however invalidate any other reasons why the 00025 // executable file might be covered by the GNU General Public License. 00026 00027 #ifndef MLN_DATA_MEMSET__HH 00028 # define MLN_DATA_MEMSET__HH 00029 00033 00034 # include <cstring> 00035 00036 # include <mln/core/concept/image.hh> 00037 # include <mln/core/pixel.hh> 00038 # include <mln/metal/is_not_const.hh> 00039 # include <mln/opt/element.hh> 00040 00041 00042 00043 namespace mln 00044 { 00045 00046 namespace data 00047 { 00048 00059 template <typename P> 00060 void memset_(Generalized_Pixel<P>& pix, 00061 const mln_value(P)& v, std::size_t n); 00062 00075 template <typename I> 00076 void memset_(I& input, const mln_psite(I)& p, 00077 const mln_value(I)& v, std::size_t n); 00078 00079 00080 # ifndef MLN_INCLUDE_ONLY 00081 00082 namespace impl 00083 { 00084 00085 template <typename P> 00086 inline 00087 void memset__(P& pix, const mln_value(P)& v, std::size_t n) 00088 { 00089 trace::entering("data::impl::memset_"); 00090 00091 typedef mln_image(P) I; 00092 if (n == 0) 00093 { 00094 return; // no-op 00095 } 00096 00097 if (n == 1) 00098 { 00099 pix.val() = v; // one assignment 00100 return; 00101 } 00102 00103 if (sizeof(mln_value(I)) == 1) 00104 { 00105 /* Because of strict-aliasing rules, we cannot use 00106 00107 *(const int*)(&v) 00108 00109 as second argument for std::memset. Hence the use of 00110 std::memcpy. See the `-fstrict-aliasing' entry in GCC's 00111 documentation for more information. */ 00112 char c; 00113 std::memcpy(&c, &v, 1); 00114 std::memset((void*)(& pix.val()), c, n); 00115 } 00116 else 00117 { 00118 mln_value(I)* ptr = & pix.val(); 00119 for (std::size_t i = 0; i < n; ++i) 00120 *ptr++ = v; 00121 } 00122 00123 trace::exiting("data::impl::memset_"); 00124 } 00125 00126 } // end of namespace mln::data::impl 00127 00128 00129 template <typename P> 00130 inline 00131 void memset_(Generalized_Pixel<P>& pix_, 00132 const mln_value(P)& v, std::size_t n) 00133 { 00134 trace::entering("data::memset_"); 00135 00136 typedef mln_image(P) I; 00137 metal::is_not_const<I>::check(); 00138 00139 P& pix = mln::internal::force_exact<P>(pix_); 00140 mln_precondition(pix.ima().is_valid()); 00141 mln_precondition(& pix.val() >= & pix.ima()[0]); 00142 mln_precondition(& pix.val() < & pix.ima()[0] + 00143 opt::nelements(pix.ima())); 00144 mln_precondition(& pix.val() + n <= & pix.ima()[0] + 00145 opt::nelements(pix.ima())); 00146 00147 impl::memset__(pix, v, n); 00148 00149 trace::exiting("data::memset_"); 00150 } 00151 00152 template <typename I> 00153 inline 00154 void memset_(I& input, const mln_psite(I)& p, 00155 const mln_value(I)& v, std::size_t n) 00156 { 00157 trace::entering("data::memset_"); 00158 00159 mlc_is(mln_trait_image_speed(I), trait::image::speed::fastest)::check(); 00160 00161 mln_precondition(input.is_valid()); 00162 mln_precondition(input.has(p)); 00163 mln_precondition(input.index_of_point(p) + n <= opt::nelements(input)); 00164 00165 pixel<I> pix(input, p); 00166 impl::memset__(pix, v, n); 00167 00168 trace::exiting("data::memset_"); 00169 } 00170 00171 # endif // ! MLN_INCLUDE_ONLY 00172 00173 } // end of namespace mln::data 00174 00175 } // end of namespace mln 00176 00177 00178 #endif // ! MLN_DATA_MEMSET__HH