2 // Copyright 2005-2007 Adobe Systems Incorporated
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
8 #ifndef BOOST_GIL_EXTENSION_NUMERIC_PIXEL_NUMERIC_OPERATIONS_HPP
9 #define BOOST_GIL_EXTENSION_NUMERIC_PIXEL_NUMERIC_OPERATIONS_HPP
11 #include <boost/gil/extension/numeric/channel_numeric_operations.hpp>
13 #include <boost/gil/color_base_algorithm.hpp>
14 #include <boost/gil/pixel.hpp>
16 namespace boost { namespace gil {
18 // Function objects and utilities for pixel-wise numeric operations.
20 // List of currently defined functors:
23 // pixel_multiplies_scalar_t (*)
24 // pixel_divides_scalar_t (/)
25 // pixel_halves_t (/=2),
27 // pixel_assigns_t (=)
29 /// \ingroup PixelNumericOperations
30 /// \brief Performs channel-wise addition of two pixels.
31 /// \tparam PixelRef1 - models PixelConcept
32 /// \tparam PixelRef2 - models PixelConcept
33 /// \tparam PixelResult - models PixelValueConcept
34 template <typename PixelRef1, typename PixelRef2, typename PixelResult>
37 auto operator()(PixelRef1 const& p1, PixelRef2 const& p2) const -> PixelResult
40 static_transform(p1, p2, result,
43 typename channel_type<PixelRef1>::type,
44 typename channel_type<PixelRef2>::type,
45 typename channel_type<PixelResult>::type
51 /// \ingroup PixelNumericOperations
52 /// \brief Performs channel-wise subtraction of two pixels.
53 /// \tparam PixelRef1 - models PixelConcept
54 /// \tparam PixelRef2 - models PixelConcept
55 /// \tparam PixelResult - models PixelValueConcept
56 template <typename PixelRef1, typename PixelRef2, typename PixelResult>
59 auto operator()(PixelRef1 const& p1, PixelRef2 const& p2) const -> PixelResult
62 static_transform(p1, p2, result,
65 typename channel_type<PixelRef1>::type,
66 typename channel_type<PixelRef2>::type,
67 typename channel_type<PixelResult>::type
73 /// \ingroup PixelNumericOperations
74 /// \brief Performs channel-wise multiplication of pixel elements by scalar.
75 /// \tparam PixelRef - models PixelConcept
76 /// \tparam Scalar - models a scalar type
77 /// \tparam PixelResult - models PixelValueConcept
78 template <typename PixelRef, typename Scalar, typename PixelResult>
79 struct pixel_multiplies_scalar_t
81 auto operator()(PixelRef const& p, Scalar const& s) const -> PixelResult
84 static_transform(p, result,
86 channel_multiplies_scalar_t<typename channel_type<PixelRef>::type,
88 typename channel_type<PixelResult>::type>(),
89 std::placeholders::_1, s));
94 /// \ingroup PixelNumericOperations
95 /// \brief Performs channel-wise multiplication of two pixels.
96 /// \tparam PixelRef1 - models PixelConcept
97 /// \tparam PixelRef1 - models PixelConcept
98 /// \tparam PixelResult - models PixelValueConcept
99 template <typename PixelRef1, typename PixelRef2, typename PixelResult>
100 struct pixel_multiply_t
102 auto operator()(PixelRef1 const& p1, PixelRef2 const& p2) const -> PixelResult
105 static_transform(p1, p2, result,
108 typename channel_type<PixelRef1>::type,
109 typename channel_type<PixelRef2>::type,
110 typename channel_type<PixelResult>::type
116 /// \ingroup PixelNumericOperations
117 /// \brief Performs channel-wise division of pixel elements by scalar.
118 /// \tparam PixelRef - models PixelConcept
119 /// \tparam Scalar - models a scalar type
120 /// \tparam PixelResult - models PixelValueConcept
121 template <typename PixelRef, typename Scalar, typename PixelResult>
122 struct pixel_divides_scalar_t
124 auto operator()(PixelRef const& p, Scalar const& s) const -> PixelResult
127 static_transform(p, result,
128 std::bind(channel_divides_scalar_t<typename channel_type<PixelRef>::type,
130 typename channel_type<PixelResult>::type>(),
131 std::placeholders::_1, s));
136 /// \ingroup PixelNumericOperations
137 /// \brief Performs channel-wise division of two pixels.
138 /// \tparam PixelRef1 - models PixelConcept
139 /// \tparam PixelRef1 - models PixelConcept
140 /// \tparam PixelResult - models PixelValueConcept
141 template <typename PixelRef1, typename PixelRef2, typename PixelResult>
142 struct pixel_divide_t
144 auto operator()(PixelRef1 const& p1, PixelRef2 const& p2) const -> PixelResult
147 static_transform(p1, p2, result,
150 typename channel_type<PixelRef1>::type,
151 typename channel_type<PixelRef2>::type,
152 typename channel_type<PixelResult>::type
158 /// \ingroup PixelNumericOperations
159 /// \brief Performs channel-wise division by 2
160 /// \tparam PixelRef - models PixelConcept
161 template <typename PixelRef>
162 struct pixel_halves_t
164 auto operator()(PixelRef& p) const -> PixelRef&
166 static_for_each(p, channel_halves_t<typename channel_type<PixelRef>::type>());
171 /// \ingroup PixelNumericOperations
172 /// \brief Sets pixel elements to zero (for whatever zero means)
173 /// \tparam PixelRef - models PixelConcept
174 template <typename PixelRef>
177 auto operator()(PixelRef& p) const -> PixelRef&
179 static_for_each(p, channel_zeros_t<typename channel_type<PixelRef>::type>());
184 /// \brief Sets pixel elements to zero (for whatever zero means)
185 /// \tparam Pixel - models PixelConcept
186 template <typename Pixel>
187 void zero_channels(Pixel& p)
189 static_for_each(p, channel_zeros_t<typename channel_type<Pixel>::type>());
192 /// \ingroup PixelNumericOperations
193 /// \brief Casts and assigns a pixel to another
195 /// A generic implementation for casting and assigning a pixel to another.
196 /// User should specialize it for better performance.
198 /// \tparam PixelRef - models PixelConcept
199 /// \tparam PixelResult - models PixelValueConcept
200 template <typename PixelRef, typename PixelResult>
201 struct pixel_assigns_t
203 auto operator()(PixelRef const& src, PixelResult& dst) const -> PixelResult
205 static_for_each(src, dst,
208 typename channel_type<PixelRef>::type,
209 typename channel_type<PixelResult>::type
215 }} // namespace boost::gil