Turi Create  4.0
channel_numeric_operations.hpp
Go to the documentation of this file.
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 /*
7  Copyright 2005-2007 Adobe Systems Incorporated
8  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
9  or a copy at http://opensource.adobe.com/licenses.html)
10 */
11 
12 /*************************************************************************************************/
13 
14 #ifndef GIL_CHANNEL_NUMERIC_OPERATIONS_HPP
15 #define GIL_CHANNEL_NUMERIC_OPERATIONS_HPP
16 
17 /*!
18 /// \file
19 /// \brief Structures for channel-wise numeric operations
20 /// \author Hailin Jin and Lubomir Bourdev \n
21 /// Adobe Systems Incorporated
22 /// \date 2005-2007 \n Last updated on September 30, 2006
23 /// Currently defined structures:
24 /// channel_plus_t (+), channel_minus_t (-),
25 /// channel_multiplies_t (*), channel_divides_t (/),
26 /// channel_plus_scalar_t (+s), channel_minus_scalar_t (-s),
27 /// channel_multiplies_scalar_t (*s), channel_divides_scalar_t (/s),
28 /// channel_halves_t (/=2), channel_zeros_t (=0), channel_assigns_t (=)
29 */
30 
31 #include <functional>
32 #include <boost/gil/gil_config.hpp>
33 #include <boost/gil/channel.hpp>
34 
35 namespace boost { namespace gil {
36 
37 /// \ingroup ChannelNumericOperations
38 /// structure for adding one channel to 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_plus_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 
48 /// \ingroup ChannelNumericOperations
49 /// structure for subtracting one channel from another
50 /// this is a generic implementation; user should specialize it for better performance
51 template <typename Channel1,typename Channel2,typename ChannelR>
52 struct channel_minus_t : public std::binary_function<Channel1,Channel2,ChannelR> {
53  ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
54  typename channel_traits<Channel2>::const_reference ch2) const {
55  return ChannelR(ch1)-ChannelR(ch2);
56  }
57 };
58 
59 /// \ingroup ChannelNumericOperations
60 /// structure for multiplying one channel to another
61 /// this is a generic implementation; user should specialize it for better performance
62 template <typename Channel1,typename Channel2,typename ChannelR>
63 struct channel_multiplies_t : public std::binary_function<Channel1,Channel2,ChannelR> {
64  ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
65  typename channel_traits<Channel2>::const_reference ch2) const {
66  return ChannelR(ch1)*ChannelR(ch2);
67  }
68 };
69 
70 /// \ingroup ChannelNumericOperations
71 /// structure for dividing channels
72 /// this is a generic implementation; user should specialize it for better performance
73 template <typename Channel1,typename Channel2,typename ChannelR>
74 struct channel_divides_t : public std::binary_function<Channel1,Channel2,ChannelR> {
75  ChannelR operator()(typename channel_traits<Channel1>::const_reference ch1,
76  typename channel_traits<Channel2>::const_reference ch2) const {
77  return ChannelR(ch1)/ChannelR(ch2);
78  }
79 };
80 
81 /// \ingroup ChannelNumericOperations
82 /// structure for adding a scalar to a channel
83 /// this is a generic implementation; user should specialize it for better performance
84 template <typename Channel,typename Scalar,typename ChannelR>
85 struct channel_plus_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
86  ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
87  const Scalar& s) const {
88  return ChannelR(ch)+ChannelR(s);
89  }
90 };
91 
92 /// \ingroup ChannelNumericOperations
93 /// structure for subtracting a scalar from a channel
94 /// this is a generic implementation; user should specialize it for better performance
95 template <typename Channel,typename Scalar,typename ChannelR>
96 struct channel_minus_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
97  ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
98  const Scalar& s) const {
99  return ChannelR(ch-s);
100  }
101 };
102 
103 /// \ingroup ChannelNumericOperations
104 /// structure for multiplying a scalar to one channel
105 /// this is a generic implementation; user should specialize it for better performance
106 template <typename Channel,typename Scalar,typename ChannelR>
107 struct channel_multiplies_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
108  ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
109  const Scalar& s) const {
110  return ChannelR(ch)*ChannelR(s);
111  }
112 };
113 
114 /// \ingroup ChannelNumericOperations
115 /// structure for dividing a channel by a scalar
116 /// this is a generic implementation; user should specialize it for better performance
117 template <typename Channel,typename Scalar,typename ChannelR>
118 struct channel_divides_scalar_t : public std::binary_function<Channel,Scalar,ChannelR> {
119  ChannelR operator()(typename channel_traits<Channel>::const_reference ch,
120  const Scalar& s) const {
121  return ChannelR(ch)/ChannelR(s);
122  }
123 };
124 
125 /// \ingroup ChannelNumericOperations
126 /// structure for halving a channel
127 /// this is a generic implementation; user should specialize it for better performance
128 template <typename Channel>
129 struct channel_halves_t : public std::unary_function<Channel,Channel> {
130  typename channel_traits<Channel>::reference
131  operator()(typename channel_traits<Channel>::reference ch) const {
132  return ch/=2.0;
133  }
134 };
135 
136 /// \ingroup ChannelNumericOperations
137 /// structure for setting a channel to zero
138 /// this is a generic implementation; user should specialize it for better performance
139 template <typename Channel>
140 struct channel_zeros_t : public std::unary_function<Channel,Channel> {
141  typename channel_traits<Channel>::reference
142  operator()(typename channel_traits<Channel>::reference ch) const {
143  return ch=Channel(0);
144  }
145 };
146 
147 /// \ingroup ChannelNumericOperations
148 /// structure for assigning one channel to another
149 /// this is a generic implementation; user should specialize it for better performance
150 template <typename Channel1,typename Channel2>
151 struct channel_assigns_t : public std::binary_function<Channel1,Channel2,Channel2> {
152  typename channel_traits<Channel2>::reference
153  operator()(typename channel_traits<Channel1>::const_reference ch1,
154  typename channel_traits<Channel2>::reference ch2) const {
155  return ch2=Channel2(ch1);
156  }
157 };
158 
159 } } // namespace boost::gil
160 
161 #endif