channel_numeric_operations.hpp 5.7 KB

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