channel_algorithm.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_CHANNEL_ALGORITHM_HPP
  10. #define GIL_CHANNEL_ALGORITHM_HPP
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief Channel algorithms
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated on May 6, 2007
  17. ///
  18. /// Definitions of standard GIL 8-bit, 16-bit, 32-bit channels
  19. ///
  20. ////////////////////////////////////////////////////////////////////////////////////////
  21. #include <boost/config.hpp>
  22. #include <boost/mpl/less.hpp>
  23. #include <boost/mpl/integral_c.hpp>
  24. #include <boost/mpl/greater.hpp>
  25. #include <boost/type_traits.hpp>
  26. #include "gil_config.hpp"
  27. #include "channel.hpp"
  28. #include "promote_integral.hpp"
  29. #include "typedefs.hpp"
  30. #include <limits>
  31. namespace boost { namespace gil {
  32. //#ifdef _MSC_VER
  33. //#pragma warning(push)
  34. //#pragma warning(disable: 4309) // disable truncation of constant value warning (using -1 to get the max value of an integral)
  35. //#endif
  36. namespace detail {
  37. // some forward declarations
  38. template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral> struct channel_converter_unsigned_impl;
  39. template <typename SrcChannelV, typename DstChannelV, bool SrcIsGreater> struct channel_converter_unsigned_integral;
  40. template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst, bool SrcDivisible> struct channel_converter_unsigned_integral_impl;
  41. template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst, bool CannotFitInInteger> struct channel_converter_unsigned_integral_nondivisible;
  42. //////////////////////////////////////
  43. //// unsigned_integral_max_value - given an unsigned integral channel type, returns its maximum value as an MPL integral constant
  44. //////////////////////////////////////
  45. template <typename UnsignedIntegralChannel>
  46. struct unsigned_integral_max_value : public mpl::integral_c<UnsignedIntegralChannel,std::numeric_limits<UnsignedIntegralChannel>::max()> {};
  47. template <>
  48. struct unsigned_integral_max_value<uint8_t> : public mpl::integral_c<uint32_t,0xFF> {};
  49. template <>
  50. struct unsigned_integral_max_value<uint16_t> : public mpl::integral_c<uint32_t,0xFFFF> {};
  51. template <>
  52. struct unsigned_integral_max_value<uint32_t> : public mpl::integral_c<uintmax_t,0xFFFFFFFF> {};
  53. template <int K>
  54. struct unsigned_integral_max_value<packed_channel_value<K> >
  55. : public mpl::integral_c<typename packed_channel_value<K>::integer_t, (uint64_t(1)<<K)-1> {};
  56. //////////////////////////////////////
  57. //// unsigned_integral_num_bits - given an unsigned integral channel type, returns the minimum number of bits needed to represent it
  58. //////////////////////////////////////
  59. template <typename UnsignedIntegralChannel>
  60. struct unsigned_integral_num_bits : public mpl::int_<sizeof(UnsignedIntegralChannel)*8> {};
  61. template <int K>
  62. struct unsigned_integral_num_bits<packed_channel_value<K> >
  63. : public mpl::int_<K> {};
  64. } // namespace detail
  65. /**
  66. \defgroup ChannelConvertAlgorithm channel_convert
  67. \brief Converting from one channel type to another
  68. \ingroup ChannelAlgorithm
  69. Conversion is done as a simple linear mapping of one channel range to the other,
  70. such that the minimum/maximum value of the source maps to the minimum/maximum value of the destination.
  71. One implication of this is that the value 0 of signed channels may not be preserved!
  72. When creating new channel models, it is often a good idea to provide specializations for the channel conversion algorithms, for
  73. example, for performance optimizations. If the new model is an integral type that can be signed, it is easier to define the conversion
  74. only for the unsigned type (\p channel_converter_unsigned) and provide specializations of \p detail::channel_convert_to_unsigned
  75. and \p detail::channel_convert_from_unsigned to convert between the signed and unsigned type.
  76. Example:
  77. \code
  78. // float32_t is a floating point channel with range [0.0f ... 1.0f]
  79. float32_t src_channel = channel_traits<float32_t>::max_value();
  80. assert(src_channel == 1);
  81. // uint8_t is 8-bit unsigned integral channel (typedef-ed from unsigned char)
  82. uint8_t dst_channel = channel_convert<uint8_t>(src_channel);
  83. assert(dst_channel == 255); // max value goes to max value
  84. \endcode
  85. */
  86. /**
  87. \defgroup ChannelConvertUnsignedAlgorithm channel_converter_unsigned
  88. \ingroup ChannelConvertAlgorithm
  89. \brief Convert one unsigned/floating point channel to another. Converts both the channel type and range
  90. @{
  91. */
  92. //////////////////////////////////////
  93. //// channel_converter_unsigned
  94. //////////////////////////////////////
  95. template <typename SrcChannelV, typename DstChannelV> // Model ChannelValueConcept
  96. struct channel_converter_unsigned
  97. : public detail::channel_converter_unsigned_impl<SrcChannelV,DstChannelV,is_integral<SrcChannelV>::value,is_integral<DstChannelV>::value> {};
  98. /// \brief Converting a channel to itself - identity operation
  99. template <typename T> struct channel_converter_unsigned<T,T> : public detail::identity<T> {};
  100. namespace detail {
  101. //////////////////////////////////////
  102. //// channel_converter_unsigned_impl
  103. //////////////////////////////////////
  104. /// \brief This is the default implementation. Performance specializatons are provided
  105. template <typename SrcChannelV, typename DstChannelV, bool SrcIsIntegral, bool DstIsIntegral>
  106. struct channel_converter_unsigned_impl {
  107. typedef SrcChannelV argument_type;
  108. typedef DstChannelV result_type;
  109. DstChannelV operator()(SrcChannelV src) const {
  110. return DstChannelV(channel_traits<DstChannelV>::min_value() +
  111. (src - channel_traits<SrcChannelV>::min_value()) / channel_range<SrcChannelV>() * channel_range<DstChannelV>());
  112. }
  113. private:
  114. template <typename C>
  115. static double channel_range() {
  116. return double(channel_traits<C>::max_value()) - double(channel_traits<C>::min_value());
  117. }
  118. };
  119. // When both the source and the destination are integral channels, perform a faster conversion
  120. template <typename SrcChannelV, typename DstChannelV>
  121. struct channel_converter_unsigned_impl<SrcChannelV,DstChannelV,true,true>
  122. : public channel_converter_unsigned_integral<SrcChannelV,DstChannelV,
  123. mpl::less<unsigned_integral_max_value<SrcChannelV>,unsigned_integral_max_value<DstChannelV> >::value > {};
  124. //////////////////////////////////////
  125. //// channel_converter_unsigned_integral
  126. //////////////////////////////////////
  127. template <typename SrcChannelV, typename DstChannelV>
  128. struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,true>
  129. : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,
  130. !(unsigned_integral_max_value<DstChannelV>::value % unsigned_integral_max_value<SrcChannelV>::value) > {};
  131. template <typename SrcChannelV, typename DstChannelV>
  132. struct channel_converter_unsigned_integral<SrcChannelV,DstChannelV,false>
  133. : public channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,
  134. !(unsigned_integral_max_value<SrcChannelV>::value % unsigned_integral_max_value<DstChannelV>::value) > {};
  135. //////////////////////////////////////
  136. //// channel_converter_unsigned_integral_impl
  137. //////////////////////////////////////
  138. // Both source and destination are unsigned integral channels,
  139. // the src max value is less than the dst max value,
  140. // and the dst max value is divisible by the src max value
  141. template <typename SrcChannelV, typename DstChannelV>
  142. struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,true,true> {
  143. DstChannelV operator()(SrcChannelV src) const {
  144. typedef typename unsigned_integral_max_value<DstChannelV>::value_type integer_t;
  145. static const integer_t mul = unsigned_integral_max_value<DstChannelV>::value / unsigned_integral_max_value<SrcChannelV>::value;
  146. return DstChannelV(src * mul);
  147. }
  148. };
  149. // Both source and destination are unsigned integral channels,
  150. // the dst max value is less than (or equal to) the src max value,
  151. // and the src max value is divisible by the dst max value
  152. template <typename SrcChannelV, typename DstChannelV>
  153. struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,false,true> {
  154. DstChannelV operator()(SrcChannelV src) const {
  155. typedef typename unsigned_integral_max_value<SrcChannelV>::value_type integer_t;
  156. static const integer_t div = unsigned_integral_max_value<SrcChannelV>::value / unsigned_integral_max_value<DstChannelV>::value;
  157. static const integer_t div2 = div/2;
  158. return DstChannelV((src + div2) / div);
  159. }
  160. };
  161. // Prevent overflow for the largest integral type
  162. template <typename DstChannelV>
  163. struct channel_converter_unsigned_integral_impl<uintmax_t,DstChannelV,false,true> {
  164. DstChannelV operator()(uintmax_t src) const {
  165. static const uintmax_t div = unsigned_integral_max_value<uint32_t>::value / unsigned_integral_max_value<DstChannelV>::value;
  166. static const uintmax_t div2 = div/2;
  167. if (src > unsigned_integral_max_value<uintmax_t>::value - div2)
  168. return unsigned_integral_max_value<DstChannelV>::value;
  169. return DstChannelV((src + div2) / div);
  170. }
  171. };
  172. // Both source and destination are unsigned integral channels,
  173. // and the dst max value is not divisible by the src max value
  174. // See if you can represent the expression (src * dst_max) / src_max in integral form
  175. template <typename SrcChannelV, typename DstChannelV, bool SrcLessThanDst>
  176. struct channel_converter_unsigned_integral_impl<SrcChannelV,DstChannelV,SrcLessThanDst,false>
  177. : public channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,SrcLessThanDst,
  178. mpl::greater<
  179. mpl::plus<unsigned_integral_num_bits<SrcChannelV>,unsigned_integral_num_bits<DstChannelV> >,
  180. unsigned_integral_num_bits<uintmax_t>
  181. >::value> {};
  182. // Both source and destination are unsigned integral channels,
  183. // the src max value is less than the dst max value,
  184. // and the dst max value is not divisible by the src max value
  185. // The expression (src * dst_max) / src_max fits in an integer
  186. template <typename SrcChannelV, typename DstChannelV>
  187. struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,false> {
  188. DstChannelV operator()(SrcChannelV src) const {
  189. typedef typename base_channel_type<DstChannelV>::type dest_t;
  190. return DstChannelV(static_cast<dest_t>( src * unsigned_integral_max_value<DstChannelV>::value) / unsigned_integral_max_value<SrcChannelV>::value);
  191. }
  192. };
  193. // Both source and destination are unsigned integral channels,
  194. // the src max value is less than the dst max value,
  195. // and the dst max value is not divisible by the src max value
  196. // The expression (src * dst_max) / src_max cannot fit in an integer (overflows). Use a double
  197. template <typename SrcChannelV, typename DstChannelV>
  198. struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,true,true> {
  199. DstChannelV operator()(SrcChannelV src) const {
  200. static const double mul = unsigned_integral_max_value<DstChannelV>::value / double(unsigned_integral_max_value<SrcChannelV>::value);
  201. return DstChannelV(src * mul);
  202. }
  203. };
  204. // Both source and destination are unsigned integral channels,
  205. // the dst max value is less than (or equal to) the src max value,
  206. // and the src max value is not divisible by the dst max value
  207. template <typename SrcChannelV, typename DstChannelV, bool CannotFit>
  208. struct channel_converter_unsigned_integral_nondivisible<SrcChannelV,DstChannelV,false,CannotFit> {
  209. DstChannelV operator()(SrcChannelV src) const {
  210. typedef typename detail::unsigned_integral_max_value< SrcChannelV >::value_type src_integer_t;
  211. typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
  212. static const double div = unsigned_integral_max_value<SrcChannelV>::value
  213. / static_cast< double >( unsigned_integral_max_value<DstChannelV>::value );
  214. static const src_integer_t div2 = static_cast< src_integer_t >( div / 2.0 );
  215. return DstChannelV( static_cast< dst_integer_t >(( static_cast< double >( src + div2 ) / div )));
  216. }
  217. };
  218. } // namespace detail
  219. /////////////////////////////////////////////////////
  220. /// float32_t conversion
  221. /////////////////////////////////////////////////////
  222. template <typename DstChannelV> struct channel_converter_unsigned<float32_t,DstChannelV> {
  223. typedef float32_t argument_type;
  224. typedef DstChannelV result_type;
  225. DstChannelV operator()(float32_t x) const
  226. {
  227. typedef typename detail::unsigned_integral_max_value< DstChannelV >::value_type dst_integer_t;
  228. return DstChannelV( static_cast< dst_integer_t >(x*channel_traits<DstChannelV>::max_value()+0.5f ));
  229. }
  230. };
  231. template <typename SrcChannelV> struct channel_converter_unsigned<SrcChannelV,float32_t> {
  232. typedef float32_t argument_type;
  233. typedef SrcChannelV result_type;
  234. float32_t operator()(SrcChannelV x) const { return float32_t(x/float(channel_traits<SrcChannelV>::max_value())); }
  235. };
  236. template <> struct channel_converter_unsigned<float32_t,float32_t> {
  237. typedef float32_t argument_type;
  238. typedef float32_t result_type;
  239. float32_t operator()(float32_t x) const { return x; }
  240. };
  241. /// \brief 32 bit <-> float channel conversion
  242. template <> struct channel_converter_unsigned<uint32_t,float32_t> {
  243. typedef uint32_t argument_type;
  244. typedef float32_t result_type;
  245. float32_t operator()(uint32_t x) const {
  246. // unfortunately without an explicit check it is possible to get a round-off error. We must ensure that max_value of uint32_t matches max_value of float32_t
  247. if (x>=channel_traits<uint32_t>::max_value()) return channel_traits<float32_t>::max_value();
  248. return float(x) / float(channel_traits<uint32_t>::max_value());
  249. }
  250. };
  251. /// \brief 32 bit <-> float channel conversion
  252. template <> struct channel_converter_unsigned<float32_t,uint32_t> {
  253. typedef float32_t argument_type;
  254. typedef uint32_t result_type;
  255. uint32_t operator()(float32_t x) const {
  256. // unfortunately without an explicit check it is possible to get a round-off error. We must ensure that max_value of uint32_t matches max_value of float32_t
  257. if (x>=channel_traits<float32_t>::max_value())
  258. return channel_traits<uint32_t>::max_value();
  259. auto const max_value = channel_traits<uint32_t>::max_value();
  260. auto const result = x * static_cast<float32_t::base_channel_t>(max_value) + 0.5f;
  261. return static_cast<uint32_t>(result);
  262. }
  263. };
  264. /// @}
  265. namespace detail {
  266. // Converting from signed to unsigned integral channel.
  267. // It is both a unary function, and a metafunction (thus requires the 'type' nested typedef, which equals result_type)
  268. template <typename ChannelValue> // Model ChannelValueConcept
  269. struct channel_convert_to_unsigned : public detail::identity<ChannelValue> {
  270. typedef ChannelValue type;
  271. };
  272. template <> struct channel_convert_to_unsigned<int8_t> {
  273. typedef int8_t argument_type;
  274. typedef uint8_t result_type;
  275. typedef uint8_t type;
  276. type operator()(int8_t val) const {
  277. return static_cast<uint8_t>(static_cast<uint32_t>(val) + 128u);
  278. }
  279. };
  280. template <> struct channel_convert_to_unsigned<int16_t> {
  281. typedef int16_t argument_type;
  282. typedef uint16_t result_type;
  283. typedef uint16_t type;
  284. type operator()(int16_t val) const {
  285. return static_cast<uint16_t>(static_cast<uint32_t>(val) + 32768u);
  286. }
  287. };
  288. template <> struct channel_convert_to_unsigned<int32_t> {
  289. typedef int32_t argument_type;
  290. typedef uint32_t result_type;
  291. typedef uint32_t type;
  292. type operator()(int32_t val) const {
  293. return static_cast<uint32_t>(val)+(1u<<31);
  294. }
  295. };
  296. // Converting from unsigned to signed integral channel
  297. // It is both a unary function, and a metafunction (thus requires the 'type' nested typedef, which equals result_type)
  298. template <typename ChannelValue> // Model ChannelValueConcept
  299. struct channel_convert_from_unsigned : public detail::identity<ChannelValue> {
  300. typedef ChannelValue type;
  301. };
  302. template <> struct channel_convert_from_unsigned<int8_t> {
  303. typedef uint8_t argument_type;
  304. typedef int8_t result_type;
  305. typedef int8_t type;
  306. type operator()(uint8_t val) const {
  307. return static_cast<int8_t>(static_cast<int32_t>(val) - 128);
  308. }
  309. };
  310. template <> struct channel_convert_from_unsigned<int16_t> {
  311. typedef uint16_t argument_type;
  312. typedef int16_t result_type;
  313. typedef int16_t type;
  314. type operator()(uint16_t val) const {
  315. return static_cast<int16_t>(static_cast<int32_t>(val) - 32768);
  316. }
  317. };
  318. template <> struct channel_convert_from_unsigned<int32_t> {
  319. typedef uint32_t argument_type;
  320. typedef int32_t result_type;
  321. typedef int32_t type;
  322. type operator()(uint32_t val) const {
  323. return static_cast<int32_t>(val - (1u<<31));
  324. }
  325. };
  326. } // namespace detail
  327. /// \ingroup ChannelConvertAlgorithm
  328. /// \brief A unary function object converting between channel types
  329. template <typename SrcChannelV, typename DstChannelV> // Model ChannelValueConcept
  330. struct channel_converter {
  331. typedef SrcChannelV argument_type;
  332. typedef DstChannelV result_type;
  333. DstChannelV operator()(const SrcChannelV& src) const {
  334. typedef detail::channel_convert_to_unsigned<SrcChannelV> to_unsigned;
  335. typedef detail::channel_convert_from_unsigned<DstChannelV> from_unsigned;
  336. typedef channel_converter_unsigned<typename to_unsigned::result_type, typename from_unsigned::argument_type> converter_unsigned;
  337. return from_unsigned()(converter_unsigned()(to_unsigned()(src)));
  338. }
  339. };
  340. /// \ingroup ChannelConvertAlgorithm
  341. /// \brief Converting from one channel type to another.
  342. template <typename DstChannel, typename SrcChannel> // Model ChannelConcept (could be channel references)
  343. inline typename channel_traits<DstChannel>::value_type channel_convert(const SrcChannel& src) {
  344. return channel_converter<typename channel_traits<SrcChannel>::value_type,
  345. typename channel_traits<DstChannel>::value_type>()(src);
  346. }
  347. /// \ingroup ChannelConvertAlgorithm
  348. /// \brief Same as channel_converter, except it takes the destination channel by reference, which allows
  349. /// us to move the templates from the class level to the method level. This is important when invoking it
  350. /// on heterogeneous pixels.
  351. struct default_channel_converter {
  352. template <typename Ch1, typename Ch2>
  353. void operator()(const Ch1& src, Ch2& dst) const {
  354. dst=channel_convert<Ch2>(src);
  355. }
  356. };
  357. namespace detail {
  358. // fast integer division by 255
  359. inline uint32_t div255(uint32_t in) { uint32_t tmp=in+128; return (tmp + (tmp>>8))>>8; }
  360. // fast integer divison by 32768
  361. inline uint32_t div32768(uint32_t in) { return (in+16384)>>15; }
  362. }
  363. /**
  364. \defgroup ChannelMultiplyAlgorithm channel_multiply
  365. \ingroup ChannelAlgorithm
  366. \brief Multiplying unsigned channel values of the same type. Performs scaled multiplication result = a * b / max_value
  367. Example:
  368. \code
  369. uint8_t x=128;
  370. uint8_t y=128;
  371. uint8_t mul = channel_multiply(x,y);
  372. assert(mul == 64); // 64 = 128 * 128 / 255
  373. \endcode
  374. */
  375. /// @{
  376. /// \brief This is the default implementation. Performance specializatons are provided
  377. template <typename ChannelValue>
  378. struct channel_multiplier_unsigned {
  379. typedef ChannelValue first_argument_type;
  380. typedef ChannelValue second_argument_type;
  381. typedef ChannelValue result_type;
  382. ChannelValue operator()(ChannelValue a, ChannelValue b) const {
  383. return ChannelValue(static_cast<typename base_channel_type<ChannelValue>::type>(a / double(channel_traits<ChannelValue>::max_value()) * b));
  384. }
  385. };
  386. /// \brief Specialization of channel_multiply for 8-bit unsigned channels
  387. template<> struct channel_multiplier_unsigned<uint8_t> {
  388. typedef uint8_t first_argument_type;
  389. typedef uint8_t second_argument_type;
  390. typedef uint8_t result_type;
  391. uint8_t operator()(uint8_t a, uint8_t b) const { return uint8_t(detail::div255(uint32_t(a) * uint32_t(b))); }
  392. };
  393. /// \brief Specialization of channel_multiply for 16-bit unsigned channels
  394. template<> struct channel_multiplier_unsigned<uint16_t> {
  395. typedef uint16_t first_argument_type;
  396. typedef uint16_t second_argument_type;
  397. typedef uint16_t result_type;
  398. uint16_t operator()(uint16_t a, uint16_t b) const { return uint16_t((uint32_t(a) * uint32_t(b))/65535); }
  399. };
  400. /// \brief Specialization of channel_multiply for float 0..1 channels
  401. template<> struct channel_multiplier_unsigned<float32_t> {
  402. typedef float32_t first_argument_type;
  403. typedef float32_t second_argument_type;
  404. typedef float32_t result_type;
  405. float32_t operator()(float32_t a, float32_t b) const { return a*b; }
  406. };
  407. /// \brief A function object to multiply two channels. result = a * b / max_value
  408. template <typename ChannelValue>
  409. struct channel_multiplier {
  410. typedef ChannelValue first_argument_type;
  411. typedef ChannelValue second_argument_type;
  412. typedef ChannelValue result_type;
  413. ChannelValue operator()(ChannelValue a, ChannelValue b) const {
  414. typedef detail::channel_convert_to_unsigned<ChannelValue> to_unsigned;
  415. typedef detail::channel_convert_from_unsigned<ChannelValue> from_unsigned;
  416. typedef channel_multiplier_unsigned<typename to_unsigned::result_type> multiplier_unsigned;
  417. return from_unsigned()(multiplier_unsigned()(to_unsigned()(a), to_unsigned()(b)));
  418. }
  419. };
  420. /// \brief A function multiplying two channels. result = a * b / max_value
  421. template <typename Channel> // Models ChannelConcept (could be a channel reference)
  422. inline typename channel_traits<Channel>::value_type channel_multiply(Channel a, Channel b) {
  423. return channel_multiplier<typename channel_traits<Channel>::value_type>()(a,b);
  424. }
  425. /// @}
  426. /**
  427. \defgroup ChannelInvertAlgorithm channel_invert
  428. \ingroup ChannelAlgorithm
  429. \brief Returns the inverse of a channel. result = max_value - x + min_value
  430. Example:
  431. \code
  432. // uint8_t == uint8_t == unsigned char
  433. uint8_t x=255;
  434. uint8_t inv = channel_invert(x);
  435. assert(inv == 0);
  436. \endcode
  437. */
  438. /// \brief Default implementation. Provide overloads for performance
  439. /// \ingroup ChannelInvertAlgorithm channel_invert
  440. template <typename Channel> // Models ChannelConcept (could be a channel reference)
  441. inline typename channel_traits<Channel>::value_type channel_invert(Channel x) {
  442. using base_t = typename base_channel_type<Channel>::type;
  443. using promoted_t = typename promote_integral<base_t>::type;
  444. promoted_t const promoted_x = x;
  445. promoted_t const promoted_max = channel_traits<Channel>::max_value();
  446. promoted_t const promoted_min = channel_traits<Channel>::min_value();
  447. promoted_t const promoted_inverted_x = promoted_max - promoted_x + promoted_min;
  448. auto const inverted_x = static_cast<base_t>(promoted_inverted_x);
  449. return inverted_x;
  450. }
  451. //#ifdef _MSC_VER
  452. //#pragma warning(pop)
  453. //#endif
  454. } } // namespace boost::gil
  455. #endif