| <!-- Copyright 2008 Lubomir Bourdev and Hailin Jin |
| |
| Distributed under the Boost Software License, Version 1.0. |
| (See accompanying file LICENSE_1_0.txt or copy at |
| http://www.boost.org/LICENSE_1_0.txt) |
| --> |
| |
| <!-- |
| Copyright 2005-2007 Adobe Systems Incorporated |
| Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt |
| or a copy at http://stlab.adobe.com/licenses.html) |
| |
| Some files are held under additional license. |
| Please see "http://stlab.adobe.com/licenses.html" for more information. |
| --> |
| |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
| |
| <head> |
| <TITLE>Generic Image Library: channel_algorithm.hpp Source File</TITLE> |
| <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/> |
| <LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/> |
| </head> |
| <body> |
| <table border="0" cellspacing="0" cellpadding="0" style='width: 100%; margin: 0; padding: 0'><tr> |
| <td width="100%" valign="top" style='padding-left: 10px; padding-right: 10px; padding-bottom: 10px'> |
| <div class="qindex"><a class="qindex" href="index.html">Modules</a> |
| | <a class="qindex" href="classes.html">Alphabetical List</a> |
| | <a class="qindex" href="annotated.html">Class List</a> |
| | <a class="qindex" href="dirs.html">Directories</a> |
| | <a class="qindex" href="files.html">File List</a> |
| | <a class="qindex" href="../index.html">GIL Home Page</a> |
| </div> |
| <!-- End Header --> |
| <!-- Generated by Doxygen 1.5.6 --> |
| <div class="navpath"><a class="el" href="g_i_l_0076.html">boost</a> » <a class="el" href="g_i_l_0079.html">gil</a> |
| </div> |
| <div class="contents"> |
| <h1>channel_algorithm.hpp</h1><a href="g_i_l_0016.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> |
| <a name="l00002"></a>00002 <span class="comment"> Copyright 2005-2007 Adobe Systems Incorporated</span> |
| <a name="l00003"></a>00003 <span class="comment"> </span> |
| <a name="l00004"></a>00004 <span class="comment"> Use, modification and distribution are subject to the Boost Software License,</span> |
| <a name="l00005"></a>00005 <span class="comment"> Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at</span> |
| <a name="l00006"></a>00006 <span class="comment"> http://www.boost.org/LICENSE_1_0.txt).</span> |
| <a name="l00007"></a>00007 <span class="comment"></span> |
| <a name="l00008"></a>00008 <span class="comment"> See http://opensource.adobe.com/gil for most recent version including documentation.</span> |
| <a name="l00009"></a>00009 <span class="comment">*/</span> |
| <a name="l00010"></a>00010 <span class="comment">/*************************************************************************************************/</span> |
| <a name="l00011"></a>00011 |
| <a name="l00012"></a>00012 <span class="preprocessor">#ifndef GIL_CHANNEL_ALGORITHM_HPP</span> |
| <a name="l00013"></a>00013 <span class="preprocessor"></span><span class="preprocessor">#define GIL_CHANNEL_ALGORITHM_HPP</span> |
| <a name="l00014"></a>00014 <span class="preprocessor"></span> |
| <a name="l00025"></a>00025 |
| <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="g_i_l_0094.html" title="GIL configuration file.">gil_config.hpp</a>"</span> |
| <a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="channel_8hpp.html" title="Channel utilities.">channel.hpp</a>"</span> |
| <a name="l00028"></a>00028 <span class="preprocessor">#include <boost/mpl/less.hpp></span> |
| <a name="l00029"></a>00029 <span class="preprocessor">#include <boost/mpl/integral_c.hpp></span> |
| <a name="l00030"></a>00030 <span class="preprocessor">#include <boost/mpl/greater.hpp></span> |
| <a name="l00031"></a>00031 <span class="preprocessor">#include <boost/type_traits.hpp></span> |
| <a name="l00032"></a>00032 |
| <a name="l00033"></a>00033 <span class="keyword">namespace </span>boost { <span class="keyword">namespace </span>gil { |
| <a name="l00034"></a>00034 |
| <a name="l00035"></a>00035 <span class="comment">//#ifdef _MSC_VER</span> |
| <a name="l00036"></a>00036 <span class="comment">//#pragma warning(push)</span> |
| <a name="l00037"></a>00037 <span class="comment">//#pragma warning(disable: 4309) // disable truncation of constant value warning (using -1 to get the max value of an integral)</span> |
| <a name="l00038"></a>00038 <span class="comment">//#endif</span> |
| <a name="l00039"></a>00039 |
| <a name="l00040"></a>00040 <span class="keyword">namespace </span>detail { |
| <a name="l00041"></a>00041 |
| <a name="l00042"></a>00042 <span class="comment">// some forward declarations</span> |
| <a name="l00043"></a>00043 <span class="keyword">template</span> <<span class="keyword">typename</span> SrcChannelV, <span class="keyword">typename</span> DstChannelV, <span class="keywordtype">bool</span> SrcIsIntegral, <span class="keywordtype">bool</span> DstIsIntegral> <span class="keyword">struct </span>channel_converter_unsigned_impl; |
| <a name="l00044"></a>00044 <span class="keyword">template</span> <<span class="keyword">typename</span> SrcChannelV, <span class="keyword">typename</span> DstChannelV, <span class="keywordtype">bool</span> SrcIsGreater> <span class="keyword">struct </span>channel_converter_unsigned_integral; |
| <a name="l00045"></a>00045 <span class="keyword">template</span> <<span class="keyword">typename</span> SrcChannelV, <span class="keyword">typename</span> DstChannelV, <span class="keywordtype">bool</span> SrcLessThanDst, <span class="keywordtype">bool</span> SrcDivisible> <span class="keyword">struct </span>channel_converter_unsigned_integral_impl; |
| <a name="l00046"></a>00046 <span class="keyword">template</span> <<span class="keyword">typename</span> SrcChannelV, <span class="keyword">typename</span> DstChannelV, <span class="keywordtype">bool</span> SrcLessThanDst, <span class="keywordtype">bool</span> CannotFitInInteger> <span class="keyword">struct </span>channel_converter_unsigned_integral_nondivisible; |
| <a name="l00047"></a>00047 |
| <a name="l00051"></a>00051 |
| <a name="l00052"></a>00052 |
| <a name="l00053"></a>00053 <span class="keyword">template</span> <<span class="keyword">typename</span> Un<span class="keywordtype">signed</span>IntegralChannel> |
| <a name="l00054"></a>00054 <span class="keyword">struct </span>unsigned_integral_max_value : <span class="keyword">public</span> mpl::integral_c<UnsignedIntegralChannel,-1> {}; |
| <a name="l00055"></a>00055 |
| <a name="l00056"></a>00056 <span class="keyword">template</span> <> |
| <a name="l00057"></a>00057 <span class="keyword">struct </span>unsigned_integral_max_value<uint8_t> : <span class="keyword">public</span> mpl::integral_c<uint32_t,0xFF> {}; |
| <a name="l00058"></a>00058 <span class="keyword">template</span> <> |
| <a name="l00059"></a>00059 <span class="keyword">struct </span>unsigned_integral_max_value<uint16_t> : <span class="keyword">public</span> mpl::integral_c<uint32_t,0xFFFF> {}; |
| <a name="l00060"></a>00060 <span class="keyword">template</span> <> |
| <a name="l00061"></a>00061 <span class="keyword">struct </span>unsigned_integral_max_value<uint32_t> : <span class="keyword">public</span> mpl::integral_c<uintmax_t,0xFFFFFFFF> {}; |
| <a name="l00062"></a>00062 |
| <a name="l00063"></a>00063 |
| <a name="l00064"></a>00064 <span class="keyword">template</span> <<span class="keywordtype">int</span> K> |
| <a name="l00065"></a>00065 <span class="keyword">struct </span>unsigned_integral_max_value<packed_channel_value<K> > |
| <a name="l00066"></a>00066 : <span class="keyword">public</span> mpl::integral_c<typename packed_channel_value<K>::integer_t, (1<<K)-1> {}; |
| <a name="l00067"></a>00067 |
| <a name="l00071"></a>00071 |
| <a name="l00072"></a>00072 template <typename UnsignedIntegralChannel> |
| <a name="l00073"></a>00073 struct unsigned_integral_num_bits : public mpl::int_<sizeof(UnsignedIntegralChannel)*8> {}; |
| <a name="l00074"></a>00074 |
| <a name="l00075"></a>00075 template <int K> |
| <a name="l00076"></a>00076 struct unsigned_integral_num_bits<packed_channel_value<K> > |
| <a name="l00077"></a>00077 : public mpl::int_<K> {}; |
| <a name="l00078"></a>00078 |
| <a name="l00079"></a>00079 } <span class="comment">// namespace detail</span> |
| <a name="l00080"></a>00080 |
| <a name="l00114"></a>00114 |
| <a name="l00115"></a>00115 |
| <a name="l00116"></a>00116 |
| <a name="l00117"></a>00117 |
| <a name="l00118"></a>00118 template <typename SrcChannelV, typename DstChannelV> <span class="comment">// Model ChannelValueConcept</span> |
| <a name="l00119"></a>00119 struct channel_converter_unsigned |
| <a name="l00120"></a>00120 : public detail::channel_converter_unsigned_impl<SrcChannelV,DstChannelV,is_integral<SrcChannelV>::value,is_integral<DstChannelV>::value> {}; |
| <a name="l00121"></a>00121 |
| <a name="l00122"></a>00122 |
| <a name="l00124"></a><a class="code" href="g_i_l_0410.html">00124</a> template <typename T> struct channel_converter_unsigned<T,T> : public detail::identity<T> {}; |
| <a name="l00125"></a>00125 |
| <a name="l00126"></a>00126 |
| <a name="l00127"></a>00127 namespace detail { |
| <a name="l00128"></a>00128 |
| <a name="l00132"></a>00132 |
| <a name="l00134"></a>00134 template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral> |
| <a name="l00135"></a><a class="code" href="g_i_l_0477.html">00135</a> struct channel_converter_unsigned_impl : public std::unary_function<DstChannelV,SrcChannelV> { |
| <a name="l00136"></a>00136 DstChannelV operator()(SrcChannelV src) const { |
| <a name="l00137"></a>00137 return DstChannelV(channel_traits<DstChannelV>::min_value() + |
| <a name="l00138"></a>00138 (src - channel_traits<SrcChannelV>::min_value()) / channel_range<SrcChannelV>() * channel_range<DstChannelV>()); |
| <a name="l00139"></a>00139 } |
| <a name="l00140"></a>00140 private: |
| <a name="l00141"></a>00141 template <typename C> |
| <a name="l00142"></a>00142 static double channel_range() { |
| <a name="l00143"></a>00143 return double(channel_traits<C>::max_value()) - double(channel_traits<C>::min_value()); |
| <a name="l00144"></a>00144 } |
| <a name="l00145"></a>00145 }; |
| <a name="l00146"></a>00146 |
| <a name="l00147"></a>00147 <span class="comment">// When both the source and the destination are integral channels, perform a faster conversion</span> |
| <a name="l00148"></a>00148 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00149"></a>00149 struct channel_converter_unsigned_impl<SrcChannelV,DstChannelV,true,true> |
| <a name="l00150"></a>00150 : public channel_converter_unsigned_integral<SrcChannelV,DstChannelV, |
| <a name="l00151"></a>00151 mpl::less<unsigned_integral_max_value<SrcChannelV>,unsigned_integral_max_value<DstChannelV> >::value > {}; |
| <a name="l00152"></a>00152 |
| <a name="l00153"></a>00153 |
| <a name="l00157"></a>00157 |
| <a name="l00158"></a>00158 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00159"></a>00159 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,true> |
| <a name="l00160"></a>00160 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true, |
| <a name="l00161"></a>00161 !(unsigned_integral_max_value<DstChannelV>::value % unsigned_integral_max_value<SrcChannelV>::value) > {}; |
| <a name="l00162"></a>00162 |
| <a name="l00163"></a>00163 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00164"></a>00164 struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,false> |
| <a name="l00165"></a>00165 : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false, |
| <a name="l00166"></a>00166 !(unsigned_integral_max_value<SrcChannelV>::value % unsigned_integral_max_value<DstChannelV>::value) > {}; |
| <a name="l00167"></a>00167 |
| <a name="l00168"></a>00168 |
| <a name="l00172"></a>00172 |
| <a name="l00173"></a>00173 <span class="comment">// Both source and destination are unsigned integral channels, </span> |
| <a name="l00174"></a>00174 <span class="comment">// the src max value is less than the dst max value,</span> |
| <a name="l00175"></a>00175 <span class="comment">// and the dst max value is divisible by the src max value</span> |
| <a name="l00176"></a>00176 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00177"></a>00177 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,true> { |
| <a name="l00178"></a>00178 DstChannelV operator()(SrcChannelV src) const { |
| <a name="l00179"></a>00179 typedef typename unsigned_integral_max_value<DstChannelV>::value_type integer_t; |
| <a name="l00180"></a>00180 static const integer_t mul = unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value; |
| <a name="l00181"></a>00181 return DstChannelV(src * mul); |
| <a name="l00182"></a>00182 } |
| <a name="l00183"></a>00183 }; |
| <a name="l00184"></a>00184 |
| <a name="l00185"></a>00185 <span class="comment">// Both source and destination are unsigned integral channels, </span> |
| <a name="l00186"></a>00186 <span class="comment">// the dst max value is less than (or equal to) the src max value,</span> |
| <a name="l00187"></a>00187 <span class="comment">// and the src max value is divisible by the dst max value</span> |
| <a name="l00188"></a>00188 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00189"></a>00189 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,true> { |
| <a name="l00190"></a>00190 DstChannelV operator()(SrcChannelV src) const { |
| <a name="l00191"></a>00191 typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t; |
| <a name="l00192"></a>00192 static const integer_t div = unsigned_integral_max_value<SrcChannelV>::value / unsigned_integral_max_value<DstChannelV>::value; |
| <a name="l00193"></a>00193 static const integer_t div2 = div/2; |
| <a name="l00194"></a>00194 return DstChannelV((src + div2) / div); |
| <a name="l00195"></a>00195 } |
| <a name="l00196"></a>00196 }; |
| <a name="l00197"></a>00197 |
| <a name="l00198"></a>00198 <span class="comment">// Prevent overflow for the largest integral type</span> |
| <a name="l00199"></a>00199 template <typename DstChannelV> |
| <a name="l00200"></a>00200 struct channel_converter_unsigned_integral_impl<uintmax_t,DstChannelV,false,true> { |
| <a name="l00201"></a>00201 DstChannelV operator()(uintmax_t src) const { |
| <a name="l00202"></a>00202 static const uintmax_t div = unsigned_integral_max_value<bits32>::value / unsigned_integral_max_value<DstChannelV>::value; |
| <a name="l00203"></a>00203 static const uintmax_t div2 = div/2; |
| <a name="l00204"></a>00204 if (src > unsigned_integral_max_value<uintmax_t>::value - div2) |
| <a name="l00205"></a>00205 return unsigned_integral_max_value<DstChannelV>::value; |
| <a name="l00206"></a>00206 return DstChannelV((src + div2) / div); |
| <a name="l00207"></a>00207 } |
| <a name="l00208"></a>00208 }; |
| <a name="l00209"></a>00209 |
| <a name="l00210"></a>00210 <span class="comment">// Both source and destination are unsigned integral channels, </span> |
| <a name="l00211"></a>00211 <span class="comment">// and the dst max value is not divisible by the src max value</span> |
| <a name="l00212"></a>00212 <span class="comment">// See if you can represent the expression (src * dst_max) / src_max in integral form</span> |
| <a name="l00213"></a>00213 template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst> |
| <a name="l00214"></a>00214 struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,SrcLessThanDst,false> |
| <a name="l00215"></a>00215 : public channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,SrcLessThanDst, |
| <a name="l00216"></a>00216 mpl::greater< |
| <a name="l00217"></a>00217 mpl::plus<unsigned_integral_num_bits<SrcChannelV>,unsigned_integral_num_bits<DstChannelV> >, |
| <a name="l00218"></a>00218 unsigned_integral_num_bits<uintmax_t> |
| <a name="l00219"></a>00219 >::value> {}; |
| <a name="l00220"></a>00220 |
| <a name="l00221"></a>00221 |
| <a name="l00222"></a>00222 <span class="comment">// Both source and destination are unsigned integral channels, </span> |
| <a name="l00223"></a>00223 <span class="comment">// the src max value is less than the dst max value,</span> |
| <a name="l00224"></a>00224 <span class="comment">// and the dst max value is not divisible by the src max value</span> |
| <a name="l00225"></a>00225 <span class="comment">// The expression (src * dst_max) / src_max fits in an integer</span> |
| <a name="l00226"></a>00226 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00227"></a>00227 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> { |
| <a name="l00228"></a>00228 DstChannelV operator()(SrcChannelV src) const { |
| <a name="l00229"></a>00229 typedef typename detail::min_fast_uint<unsigned_integral_num_bits<SrcChannelV>::value+unsigned_integral_num_bits<DstChannelV>::value>::type integer_t; |
| <a name="l00230"></a>00230 return DstChannelV(integer_t(src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value); |
| <a name="l00231"></a>00231 } |
| <a name="l00232"></a>00232 }; |
| <a name="l00233"></a>00233 |
| <a name="l00234"></a>00234 <span class="comment">// Both source and destination are unsigned integral channels, </span> |
| <a name="l00235"></a>00235 <span class="comment">// the src max value is less than the dst max value,</span> |
| <a name="l00236"></a>00236 <span class="comment">// and the dst max value is not divisible by the src max value</span> |
| <a name="l00237"></a>00237 <span class="comment">// The expression (src * dst_max) / src_max cannot fit in an integer (overflows). Use a double</span> |
| <a name="l00238"></a>00238 template <typename SrcChannelV, typename DstChannelV> |
| <a name="l00239"></a>00239 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,true> { |
| <a name="l00240"></a>00240 DstChannelV operator()(SrcChannelV src) const { |
| <a name="l00241"></a>00241 static const double mul = unsigned_integral_max_value<DstChannelV>::value / double(unsigned_integral_max_value<SrcChannelV>::value); |
| <a name="l00242"></a>00242 return DstChannelV(src * mul); |
| <a name="l00243"></a>00243 } |
| <a name="l00244"></a>00244 }; |
| <a name="l00245"></a>00245 |
| <a name="l00246"></a>00246 |
| <a name="l00247"></a>00247 <span class="comment">// Both source and destination are unsigned integral channels, </span> |
| <a name="l00248"></a>00248 <span class="comment">// the dst max value is less than (or equal to) the src max value,</span> |
| <a name="l00249"></a>00249 <span class="comment">// and the src max value is not divisible by the dst max value</span> |
| <a name="l00250"></a>00250 template <typename SrcChannelV, typename DstChannelV, bool CannotFit> |
| <a name="l00251"></a>00251 struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,false,CannotFit> { |
| <a name="l00252"></a>00252 DstChannelV operator()(SrcChannelV src) const { |
| <a name="l00253"></a>00253 typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t; |
| <a name="l00254"></a>00254 |
| <a name="l00255"></a>00255 static const double div = unsigned_integral_max_value<SrcChannelV>::value / double(unsigned_integral_max_value<DstChannelV>::value); |
| <a name="l00256"></a>00256 static const integer_t div2 = integer_t(div/2); |
| <a name="l00257"></a>00257 return DstChannelV((src + div2) / div); |
| <a name="l00258"></a>00258 } |
| <a name="l00259"></a>00259 }; |
| <a name="l00260"></a>00260 |
| <a name="l00261"></a>00261 } <span class="comment">// namespace detail</span> |
| <a name="l00262"></a>00262 |
| <a name="l00266"></a>00266 |
| <a name="l00267"></a><a class="code" href="g_i_l_0414.html">00267</a> template <typename DstChannelV> struct channel_converter_unsigned<bits32f,DstChannelV> : public std::unary_function<bits32f,DstChannelV> { |
| <a name="l00268"></a>00268 DstChannelV operator()(bits32f x) const { return DstChannelV(x*channel_traits<DstChannelV>::max_value()+0.5f); } |
| <a name="l00269"></a>00269 }; |
| <a name="l00270"></a>00270 |
| <a name="l00271"></a>00271 template <typename SrcChannelV> struct channel_converter_unsigned<SrcChannelV,bits32f> : public std::unary_function<SrcChannelV,bits32f> { |
| <a name="l00272"></a>00272 bits32f operator()(SrcChannelV x) const { return bits32f(x/float(channel_traits<SrcChannelV>::max_value())); } |
| <a name="l00273"></a>00273 }; |
| <a name="l00274"></a>00274 |
| <a name="l00275"></a>00275 template <> struct channel_converter_unsigned<bits32f,bits32f> : public std::unary_function<bits32f,bits32f> { |
| <a name="l00276"></a>00276 bits32f operator()(bits32f x) const { return x; } |
| <a name="l00277"></a>00277 }; |
| <a name="l00278"></a>00278 |
| <a name="l00279"></a>00279 |
| <a name="l00281"></a><a class="code" href="g_i_l_0412.html">00281</a> template <> struct channel_converter_unsigned<bits32,bits32f> : public std::unary_function<bits32,bits32f> { |
| <a name="l00282"></a>00282 bits32f operator()(bits32 x) const { |
| <a name="l00283"></a>00283 <span class="comment">// unfortunately without an explicit check it is possible to get a round-off error. We must ensure that max_value of bits32 matches max_value of bits32f</span> |
| <a name="l00284"></a>00284 if (x>=channel_traits<bits32>::max_value()) return <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits</a><bits32f>::max_value(); |
| <a name="l00285"></a>00285 return float(x) / float(channel_traits<bits32>::max_value()); |
| <a name="l00286"></a>00286 } |
| <a name="l00287"></a>00287 }; |
| <a name="l00289"></a><a class="code" href="g_i_l_0416.html">00289</a> template <> struct channel_converter_unsigned<bits32f,bits32> : <span class="keyword">public</span> std::unary_function<bits32f,bits32> { |
| <a name="l00290"></a>00290 bits32 operator()(<a class="code" href="g_i_l_0620.html" title="A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept...">bits32f</a> x)<span class="keyword"> const </span>{ |
| <a name="l00291"></a>00291 <span class="comment">// unfortunately without an explicit check it is possible to get a round-off error. We must ensure that max_value of bits32 matches max_value of bits32f</span> |
| <a name="l00292"></a>00292 <span class="keywordflow">if</span> (x>=<a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<bits32f>::max_value</a>()) <span class="keywordflow">return</span> <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<bits32>::max_value</a>(); |
| <a name="l00293"></a>00293 <span class="keywordflow">return</span> bits32(x * <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<bits32>::max_value</a>() + 0.5f); |
| <a name="l00294"></a>00294 } |
| <a name="l00295"></a>00295 }; |
| <a name="l00296"></a>00296 |
| <a name="l00298"></a>00298 |
| <a name="l00299"></a>00299 <span class="keyword">namespace </span>detail { |
| <a name="l00300"></a>00300 <span class="comment">// Converting from signed to unsigned integral channel. </span> |
| <a name="l00301"></a>00301 <span class="comment">// It is both a unary function, and a metafunction (thus requires the 'type' nested typedef, which equals result_type)</span> |
| <a name="l00302"></a>00302 <span class="keyword">template</span> <<span class="keyword">typename</span> ChannelValue> <span class="comment">// Model ChannelValueConcept</span> |
| <a name="l00303"></a>00303 <span class="keyword">struct </span>channel_convert_to_unsigned : <span class="keyword">public</span> detail::identity<ChannelValue> { |
| <a name="l00304"></a>00304 <span class="keyword">typedef</span> ChannelValue type; |
| <a name="l00305"></a>00305 }; |
| <a name="l00306"></a>00306 |
| <a name="l00307"></a>00307 <span class="keyword">template</span> <> <span class="keyword">struct </span>channel_convert_to_unsigned<bits8s> : <span class="keyword">public</span> std::unary_function<bits8s,bits8> { |
| <a name="l00308"></a>00308 <span class="keyword">typedef</span> bits8 type; |
| <a name="l00309"></a>00309 type operator()(bits8s val)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> val+128; } |
| <a name="l00310"></a>00310 }; |
| <a name="l00311"></a>00311 |
| <a name="l00312"></a>00312 <span class="keyword">template</span> <> <span class="keyword">struct </span>channel_convert_to_unsigned<bits16s> : <span class="keyword">public</span> std::unary_function<bits16s,bits16> { |
| <a name="l00313"></a>00313 <span class="keyword">typedef</span> bits16 type; |
| <a name="l00314"></a>00314 type operator()(bits16s val)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> val+32768; } |
| <a name="l00315"></a>00315 }; |
| <a name="l00316"></a>00316 |
| <a name="l00317"></a>00317 <span class="keyword">template</span> <> <span class="keyword">struct </span>channel_convert_to_unsigned<bits32s> : <span class="keyword">public</span> std::unary_function<bits32s,bits32> { |
| <a name="l00318"></a>00318 <span class="keyword">typedef</span> bits32 type; |
| <a name="l00319"></a>00319 type operator()(bits32s x)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">static_cast<</span>bits32<span class="keyword">></span>(x+(1<<31)); } |
| <a name="l00320"></a>00320 }; |
| <a name="l00321"></a>00321 |
| <a name="l00322"></a>00322 |
| <a name="l00323"></a>00323 <span class="comment">// Converting from unsigned to signed integral channel</span> |
| <a name="l00324"></a>00324 <span class="comment">// It is both a unary function, and a metafunction (thus requires the 'type' nested typedef, which equals result_type)</span> |
| <a name="l00325"></a>00325 <span class="keyword">template</span> <<span class="keyword">typename</span> ChannelValue> <span class="comment">// Model ChannelValueConcept</span> |
| <a name="l00326"></a>00326 <span class="keyword">struct </span>channel_convert_from_unsigned : <span class="keyword">public</span> detail::identity<ChannelValue> { |
| <a name="l00327"></a>00327 <span class="keyword">typedef</span> ChannelValue type; |
| <a name="l00328"></a>00328 }; |
| <a name="l00329"></a>00329 |
| <a name="l00330"></a>00330 <span class="keyword">template</span> <> <span class="keyword">struct </span>channel_convert_from_unsigned<bits8s> : <span class="keyword">public</span> std::unary_function<bits8,bits8s> { |
| <a name="l00331"></a>00331 <span class="keyword">typedef</span> bits8s type; |
| <a name="l00332"></a>00332 type operator()(bits8 val)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> val-128; } |
| <a name="l00333"></a>00333 }; |
| <a name="l00334"></a>00334 |
| <a name="l00335"></a>00335 <span class="keyword">template</span> <> <span class="keyword">struct </span>channel_convert_from_unsigned<bits16s> : <span class="keyword">public</span> std::unary_function<bits16,bits16s> { |
| <a name="l00336"></a>00336 <span class="keyword">typedef</span> bits16s type; |
| <a name="l00337"></a>00337 type operator()(bits16 val)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> val-32768; } |
| <a name="l00338"></a>00338 }; |
| <a name="l00339"></a>00339 |
| <a name="l00340"></a>00340 <span class="keyword">template</span> <> <span class="keyword">struct </span>channel_convert_from_unsigned<bits32s> : <span class="keyword">public</span> std::unary_function<bits32,bits32s> { |
| <a name="l00341"></a>00341 <span class="keyword">typedef</span> bits32s type; |
| <a name="l00342"></a>00342 type operator()(bits32 x)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <span class="keyword">static_cast<</span>bits32s<span class="keyword">></span>(x-(1<<31)); } |
| <a name="l00343"></a>00343 }; |
| <a name="l00344"></a>00344 |
| <a name="l00345"></a>00345 } <span class="comment">// namespace detail</span> |
| <a name="l00346"></a>00346 |
| <a name="l00349"></a>00349 <span class="keyword">template</span> <<span class="keyword">typename</span> SrcChannelV, <span class="keyword">typename</span> DstChannelV> <span class="comment">// Model ChannelValueConcept</span> |
| <a name="l00350"></a><a class="code" href="g_i_l_0408.html">00350</a> <span class="keyword">struct </span><a class="code" href="g_i_l_0408.html" title="A unary function object converting between channel types.">channel_converter</a> : <span class="keyword">public</span> std::unary_function<SrcChannelV,DstChannelV> { |
| <a name="l00351"></a>00351 DstChannelV operator()(SrcChannelV src)<span class="keyword"> const </span>{ |
| <a name="l00352"></a>00352 <span class="keyword">typedef</span> detail::channel_convert_to_unsigned<SrcChannelV> to_unsigned; |
| <a name="l00353"></a>00353 <span class="keyword">typedef</span> detail::channel_convert_from_unsigned<DstChannelV> from_unsigned; |
| <a name="l00354"></a>00354 <span class="keyword">typedef</span> channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type> converter_unsigned; |
| <a name="l00355"></a>00355 <span class="keywordflow">return</span> from_unsigned()(converter_unsigned()(to_unsigned()(src))); |
| <a name="l00356"></a>00356 } |
| <a name="l00357"></a>00357 }; |
| <a name="l00358"></a>00358 |
| <a name="l00361"></a>00361 <span class="keyword">template</span> <<span class="keyword">typename</span> DstChannel, <span class="keyword">typename</span> SrcChannel> <span class="comment">// Model ChannelConcept (could be channel references)</span> |
| <a name="l00362"></a><a class="code" href="g_i_l_0099.html#gf04e6ac30a35a1f68a8bb84730e34786">00362</a> <span class="keyword">inline</span> <span class="keyword">typename</span> <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<DstChannel>::value_type</a> <a class="code" href="g_i_l_0099.html#gf04e6ac30a35a1f68a8bb84730e34786" title="Converting from one channel type to another.">channel_convert</a>(SrcChannel src) { |
| <a name="l00363"></a>00363 <span class="keywordflow">return</span> <a class="code" href="g_i_l_0408.html" title="A unary function object converting between channel types.">channel_converter<typename channel_traits<SrcChannel>::value_type</a>, |
| <a name="l00364"></a>00364 <span class="keyword">typename</span> <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<DstChannel>::value_type</a>>()(src); |
| <a name="l00365"></a>00365 } |
| <a name="l00366"></a>00366 |
| <a name="l00371"></a><a class="code" href="g_i_l_0450.html">00371</a> <span class="keyword">struct </span><a class="code" href="g_i_l_0450.html" title="Same as channel_converter, except it takes the destination channel by reference,...">default_channel_converter</a> { |
| <a name="l00372"></a>00372 <span class="keyword">template</span> <<span class="keyword">typename</span> Ch1, <span class="keyword">typename</span> Ch2> |
| <a name="l00373"></a>00373 <span class="keywordtype">void</span> operator()(<span class="keyword">const</span> Ch1& src, Ch2& dst)<span class="keyword"> const </span>{ |
| <a name="l00374"></a>00374 dst=channel_convert<Ch2>(src); |
| <a name="l00375"></a>00375 } |
| <a name="l00376"></a>00376 }; |
| <a name="l00377"></a>00377 |
| <a name="l00378"></a>00378 <span class="keyword">namespace </span>detail { |
| <a name="l00379"></a>00379 <span class="comment">// fast integer division by 255</span> |
| <a name="l00380"></a>00380 <span class="keyword">inline</span> uint32_t div255(uint32_t in) { uint32_t tmp=in+128; <span class="keywordflow">return</span> (tmp + (tmp>>8))>>8; } |
| <a name="l00381"></a>00381 |
| <a name="l00382"></a>00382 <span class="comment">// fast integer divison by 32768</span> |
| <a name="l00383"></a>00383 <span class="keyword">inline</span> uint32_t div32768(uint32_t in) { <span class="keywordflow">return</span> (in+16384)>>15; } |
| <a name="l00384"></a>00384 } |
| <a name="l00385"></a>00385 |
| <a name="l00399"></a>00399 |
| <a name="l00400"></a>00400 |
| <a name="l00402"></a>00402 <span class="keyword">template</span> <<span class="keyword">typename</span> ChannelValue> |
| <a name="l00403"></a><a class="code" href="g_i_l_0422.html">00403</a> <span class="keyword">struct </span><a class="code" href="g_i_l_0422.html" title="This is the default implementation. Performance specializatons are provided.">channel_multiplier_unsigned</a> : <span class="keyword">public</span> std::binary_function<ChannelValue,ChannelValue,ChannelValue> { |
| <a name="l00404"></a>00404 ChannelValue operator()(ChannelValue a, ChannelValue b)<span class="keyword"> const </span>{ |
| <a name="l00405"></a>00405 <span class="keywordflow">return</span> ChannelValue(a / <span class="keywordtype">double</span>(<a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<ChannelValue>::max_value</a>()) * b); |
| <a name="l00406"></a>00406 } |
| <a name="l00407"></a>00407 }; |
| <a name="l00408"></a>00408 |
| <a name="l00410"></a><a class="code" href="g_i_l_0428.html">00410</a> <span class="keyword">template</span><> <span class="keyword">struct </span><a class="code" href="g_i_l_0422.html" title="This is the default implementation. Performance specializatons are provided.">channel_multiplier_unsigned</a><bits8> : <span class="keyword">public</span> std::binary_function<bits8,bits8,bits8> { |
| <a name="l00411"></a>00411 bits8 operator()(bits8 a, bits8 b)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> bits8(detail::div255(uint32_t(a) * uint32_t(b))); } |
| <a name="l00412"></a>00412 }; |
| <a name="l00413"></a>00413 |
| <a name="l00415"></a><a class="code" href="g_i_l_0424.html">00415</a> <span class="keyword">template</span><> <span class="keyword">struct </span><a class="code" href="g_i_l_0422.html" title="This is the default implementation. Performance specializatons are provided.">channel_multiplier_unsigned</a><bits16> : <span class="keyword">public</span> std::binary_function<bits16,bits16,bits16> { |
| <a name="l00416"></a>00416 bits16 operator()(bits16 a, bits16 b)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> bits16((uint32_t(a) * uint32_t(b))/65535); } |
| <a name="l00417"></a>00417 }; |
| <a name="l00418"></a>00418 |
| <a name="l00420"></a><a class="code" href="g_i_l_0426.html">00420</a> <span class="keyword">template</span><> <span class="keyword">struct </span><a class="code" href="g_i_l_0422.html" title="This is the default implementation. Performance specializatons are provided.">channel_multiplier_unsigned</a><<a class="code" href="g_i_l_0620.html" title="A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept...">bits32f</a>> : <span class="keyword">public</span> std::binary_function<bits32f,bits32f,bits32f> { |
| <a name="l00421"></a>00421 <a class="code" href="g_i_l_0620.html" title="A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept...">bits32f</a> operator()(<a class="code" href="g_i_l_0620.html" title="A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept...">bits32f</a> a, <a class="code" href="g_i_l_0620.html" title="A channel adaptor that modifies the range of the source channel. Models: ChannelValueConcept...">bits32f</a> b)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> a*b; } |
| <a name="l00422"></a>00422 }; |
| <a name="l00423"></a>00423 |
| <a name="l00425"></a>00425 <span class="keyword">template</span> <<span class="keyword">typename</span> ChannelValue> |
| <a name="l00426"></a><a class="code" href="g_i_l_0420.html">00426</a> <span class="keyword">struct </span><a class="code" href="g_i_l_0420.html" title="A function object to multiply two channels. result = a * b / max_value.">channel_multiplier</a> : <span class="keyword">public</span> std::binary_function<ChannelValue, ChannelValue, ChannelValue> { |
| <a name="l00427"></a>00427 ChannelValue operator()(ChannelValue a, ChannelValue b)<span class="keyword"> const </span>{ |
| <a name="l00428"></a>00428 <span class="keyword">typedef</span> detail::channel_convert_to_unsigned<ChannelValue> to_unsigned; |
| <a name="l00429"></a>00429 <span class="keyword">typedef</span> detail::channel_convert_from_unsigned<ChannelValue> from_unsigned; |
| <a name="l00430"></a>00430 <span class="keyword">typedef</span> <a class="code" href="g_i_l_0422.html" title="This is the default implementation. Performance specializatons are provided.">channel_multiplier_unsigned<typename to_unsigned::result_type></a> multiplier_unsigned; |
| <a name="l00431"></a>00431 <span class="keywordflow">return</span> from_unsigned()(multiplier_unsigned()(to_unsigned()(a), to_unsigned()(b))); |
| <a name="l00432"></a>00432 } |
| <a name="l00433"></a>00433 }; |
| <a name="l00434"></a>00434 |
| <a name="l00436"></a>00436 <span class="keyword">template</span> <<span class="keyword">typename</span> Channel> <span class="comment">// Models ChannelConcept (could be a channel reference)</span> |
| <a name="l00437"></a><a class="code" href="g_i_l_0103.html#gf73b2a47a7877767534df0eee46dda17">00437</a> <span class="keyword">inline</span> <span class="keyword">typename</span> <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<Channel>::value_type</a> <a class="code" href="g_i_l_0103.html#gf73b2a47a7877767534df0eee46dda17" title="A function multiplying two channels. result = a * b / max_value.">channel_multiply</a>(Channel a, Channel b) { |
| <a name="l00438"></a>00438 <span class="keywordflow">return</span> <a class="code" href="g_i_l_0420.html" title="A function object to multiply two channels. result = a * b / max_value.">channel_multiplier<typename channel_traits<Channel>::value_type</a>>()(a,b); |
| <a name="l00439"></a>00439 } |
| <a name="l00441"></a>00441 |
| <a name="l00456"></a>00456 |
| <a name="l00457"></a>00457 |
| <a name="l00458"></a>00458 <span class="keyword">template</span> <<span class="keyword">typename</span> Channel> <span class="comment">// Models ChannelConcept (could be a channel reference)</span> |
| <a name="l00459"></a><a class="code" href="g_i_l_0101.html#ge2e0267865f89610ef26148874a04bb5">00459</a> <span class="keyword">inline</span> <span class="keyword">typename</span> <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<Channel>::value_type</a> <a class="code" href="g_i_l_0101.html#ge2e0267865f89610ef26148874a04bb5" title="Default implementation. Provide overloads for performance.">channel_invert</a>(Channel x) { |
| <a name="l00460"></a>00460 <span class="keywordflow">return</span> <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<Channel>::max_value</a>()-x + <a class="code" href="g_i_l_0429.html" title="Traits for channels. Contains the following members:.">channel_traits<Channel>::min_value</a>(); |
| <a name="l00461"></a>00461 } |
| <a name="l00462"></a>00462 |
| <a name="l00463"></a>00463 <span class="comment">//#ifdef _MSC_VER</span> |
| <a name="l00464"></a>00464 <span class="comment">//#pragma warning(pop)</span> |
| <a name="l00465"></a>00465 <span class="comment">//#endif</span> |
| <a name="l00466"></a>00466 |
| <a name="l00467"></a>00467 } } <span class="comment">// namespace boost::gil</span> |
| <a name="l00468"></a>00468 |
| <a name="l00469"></a>00469 <span class="preprocessor">#endif</span> |
| </pre></div></div> |
| <hr size="1"><address style="text-align: right;"><small>Generated on Sat May 2 13:50:13 2009 for Generic Image Library by |
| <a href="http://www.doxygen.org/index.html"> |
| <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> |
| </body> |
| </html> |