channel_algorithm.hpp 23 KB

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