channel_numeric_operations.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. */
  7. /*************************************************************************************************/
  8. #ifndef BOOST_GIL_EXTENSION_NUMERIC_CHANNEL_NUMERIC_OPERATIONS_HPP
  9. #define BOOST_GIL_EXTENSION_NUMERIC_CHANNEL_NUMERIC_OPERATIONS_HPP
  10. /*!
  11. /// \file
  12. /// \brief Structures for channel-wise numeric operations
  13. /// \author Hailin Jin and Lubomir Bourdev \n
  14. /// Adobe Systems Incorporated
  15. /// \date 2005-2007 \n
  16. /// Currently defined structures:
  17. /// channel_plus_t (+), channel_minus_t (-),
  18. /// channel_multiplies_t (*), channel_divides_t (/),
  19. /// channel_plus_scalar_t (+s), channel_minus_scalar_t (-s),
  20. /// channel_multiplies_scalar_t (*s), channel_divides_scalar_t (/s),
  21. /// channel_halves_t (/=2), channel_zeros_t (=0), channel_assigns_t (=)
  22. */
  23. #include <functional>
  24. #include <boost/gil/gil_config.hpp>
  25. #include <boost/gil/channel.hpp>
  26. namespace boost { namespace gil {
  27. /// \ingroup ChannelNumericOperations
  28. /// structure for adding one channel to another
  29. /// this is a generic implementation; user should specialize it for better performance
  30. template <typename Channel1,typename Channel2,typename ChannelR>
  31. struct channel_plus_t : public std::binary_function<Channel1,Channel2,ChannelR> {
  32. ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
  33. typename channel_traits<Channel2>::const_reference ch2) const {
  34. return ChannelR(ch1)+ChannelR(ch2);
  35. }
  36. };
  37. /// \ingroup ChannelNumericOperations
  38. /// structure for subtracting one channel from another
  39. /// this is a generic implementation; user should specialize it for better performance
  40. template <typename Channel1,typename Channel2,typename ChannelR>
  41. struct channel_minus_t : public std::binary_function<Channel1,Channel2,ChannelR> {
  42. ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
  43. typename channel_traits<Channel2>::const_reference ch2) const {
  44. return ChannelR(ch1)-ChannelR(ch2);
  45. }
  46. };
  47. /// \ingroup ChannelNumericOperations
  48. /// structure for multiplying one channel to another
  49. /// this is a generic implementation; user should specialize it for better performance
  50. template <typename Channel1,typename Channel2,typename ChannelR>
  51. struct channel_multiplies_t : public std::binary_function<Channel1,Channel2,ChannelR> {
  52. ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
  53. typename channel_traits<Channel2>::const_reference ch2) const {
  54. return ChannelR(ch1)*ChannelR(ch2);
  55. }
  56. };
  57. /// \ingroup ChannelNumericOperations
  58. /// structure for dividing channels
  59. /// this is a generic implementation; user should specialize it for better performance
  60. template <typename Channel1,typename Channel2,typename ChannelR>
  61. struct channel_divides_t : public std::binary_function<Channel1,Channel2,ChannelR> {
  62. ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
  63. typename channel_traits<Channel2>::const_reference ch2) const {
  64. return ChannelR(ch1)/ChannelR(ch2);
  65. }
  66. };
  67. /// \ingroup ChannelNumericOperations
  68. /// structure for adding a scalar to a channel
  69. /// this is a generic implementation; user should specialize it for better performance
  70. template <typename Channel,typename Scalar,typename ChannelR>
  71. struct channel_plus_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
  72. ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
  73. const Scalar& s) const {
  74. return ChannelR(ch)+ChannelR(s);
  75. }
  76. };
  77. /// \ingroup ChannelNumericOperations
  78. /// structure for subtracting a scalar from a channel
  79. /// this is a generic implementation; user should specialize it for better performance
  80. template <typename Channel,typename Scalar,typename ChannelR>
  81. struct channel_minus_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
  82. ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
  83. const Scalar& s) const {
  84. return ChannelR(ch-s);
  85. }
  86. };
  87. /// \ingroup ChannelNumericOperations
  88. /// structure for multiplying a scalar to one channel
  89. /// this is a generic implementation; user should specialize it for better performance
  90. template <typename Channel,typename Scalar,typename ChannelR>
  91. struct channel_multiplies_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
  92. ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
  93. const Scalar& s) const {
  94. return ChannelR(ch)*ChannelR(s);
  95. }
  96. };
  97. /// \ingroup ChannelNumericOperations
  98. /// structure for dividing a channel by a scalar
  99. /// this is a generic implementation; user should specialize it for better performance
  100. template <typename Channel,typename Scalar,typename ChannelR>
  101. struct channel_divides_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
  102. ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
  103. const Scalar& s) const {
  104. return ChannelR(ch)/ChannelR(s);
  105. }
  106. };
  107. /// \ingroup ChannelNumericOperations
  108. /// structure for halving a channel
  109. /// this is a generic implementation; user should specialize it for better performance
  110. template <typename Channel>
  111. struct channel_halves_t : public std::unary_function<Channel,Channel> {
  112. typename channel_traits<Channel>::reference
  113. operator()(typename channel_traits<Channel>::reference ch) const {
  114. return ch/=2.0;
  115. }
  116. };
  117. /// \ingroup ChannelNumericOperations
  118. /// structure for setting a channel to zero
  119. /// this is a generic implementation; user should specialize it for better performance
  120. template <typename Channel>
  121. struct channel_zeros_t : public std::unary_function<Channel,Channel> {
  122. typename channel_traits<Channel>::reference
  123. operator()(typename channel_traits<Channel>::reference ch) const {
  124. return ch=Channel(0);
  125. }
  126. };
  127. /// \ingroup ChannelNumericOperations
  128. /// structure for assigning one channel to another
  129. /// this is a generic implementation; user should specialize it for better performance
  130. template <typename Channel1,typename Channel2>
  131. struct channel_assigns_t : public std::binary_function<Channel1,Channel2,Channel2> {
  132. typename channel_traits<Channel2>::reference
  133. operator()(typename channel_traits<Channel1>::const_reference ch1,
  134. typename channel_traits<Channel2>::reference ch2) const {
  135. return ch2=Channel2(ch1);
  136. }
  137. };
  138. } } // namespace boost::gil
  139. #endif // BOOST_GIL_EXTENSION_NUMERIC_CHANNEL_NUMERIC_OPERATIONS_HPP