Turi Create  4.0
pixel_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_PIXEL_NUMERIC_OPERATIONS_HPP
15 #define GIL_PIXEL_NUMERIC_OPERATIONS_HPP
16 
17 /*!
18 /// \file
19 /// \brief Structures for pixel-wise numeric operations
20 /// \author Lubomir Bourdev and Hailin Jin \n
21 /// Adobe Systems Incorporated
22 /// \date 2005-2007 \n Last updated on February 6, 2007
23 /// Currently defined structures:
24 /// pixel_plus_t (+), pixel_minus_t (-)
25 /// pixel_multiplies_scalar_t (*), pixel_divides_scalar_t (/)
26 /// pixel_halves_t (/=2), pixel_zeros_t (=0)
27 /// pixel_assigns_t (=)
28 */
29 
30 #include <functional>
31 #include <boost/gil/gil_config.hpp>
32 #include <boost/gil/pixel.hpp>
33 #include <boost/gil/color_base_algorithm.hpp>
35 
36 namespace boost { namespace gil {
37 
38 /// \ingroup PixelNumericOperations
39 /// \brief construct for adding two pixels
40 template <typename PixelRef1, // models pixel concept
41  typename PixelRef2, // models pixel concept
42  typename PixelR> // models pixel value concept
43 struct pixel_plus_t {
44  PixelR operator() (const PixelRef1& p1,
45  const PixelRef2& p2) const {
46  PixelR result;
47  static_transform(p1,p2,result,
48  channel_plus_t<typename channel_type<PixelRef1>::type,
49  typename channel_type<PixelRef2>::type,
50  typename channel_type<PixelR>::type>());
51  return result;
52  }
53 };
54 
55 /// \ingroup PixelNumericOperations
56 /// \brief construct for subtracting two pixels
57 template <typename PixelRef1, // models pixel concept
58  typename PixelRef2, // models pixel concept
59  typename PixelR> // models pixel value concept
60 struct pixel_minus_t {
61  PixelR operator() (const PixelRef1& p1,
62  const PixelRef2& p2) const {
63  PixelR result;
64  static_transform(p1,p2,result,
65  channel_minus_t<typename channel_type<PixelRef1>::type,
66  typename channel_type<PixelRef2>::type,
67  typename channel_type<PixelR>::type>());
68  return result;
69  }
70 };
71 
72 /// \ingroup PixelNumericOperations
73 /// \brief construct for multiplying scalar to a pixel
74 template <typename PixelRef, // models pixel concept
75  typename Scalar, // models a scalar type
76  typename PixelR> // models pixel value concept
78  PixelR operator () (const PixelRef& p,
79  const Scalar& s) const {
80  PixelR result;
81  static_transform(p,result,
82  std::bind2nd(channel_multiplies_scalar_t<typename channel_type<PixelRef>::type,
83  Scalar,
84  typename channel_type<PixelR>::type>(),s));
85  return result;
86  }
87 };
88 
89 /// \ingroup PixelNumericOperations
90 /// \brief construct for dividing a pixel by a scalar
91 template <typename PixelRef, // models pixel concept
92  typename Scalar, // models a scalar type
93  typename PixelR> // models pixel value concept
95  PixelR operator () (const PixelRef& p,
96  const Scalar& s) const {
97  PixelR result;
98  static_transform(p,result,
99  std::bind2nd(channel_divides_scalar_t<typename channel_type<PixelRef>::type,
100  Scalar,
101  typename channel_type<PixelR>::type>(),s));
102  return result;
103  }
104 };
105 
106 /// \ingroup PixelNumericOperations
107 /// \brief construct for dividing a pixel by 2
108 template <typename PixelRef> // models pixel concept
110  PixelRef& operator () (PixelRef& p) const {
111  static_for_each(p,channel_halves_t<typename channel_type<PixelRef>::type>());
112  return p;
113  }
114 };
115 
116 /// \ingroup PixelNumericOperations
117 /// \brief construct for setting a pixel to zero (for whatever zero means)
118 template <typename PixelRef> // models pixel concept
120  PixelRef& operator () (PixelRef& p) const {
121  static_for_each(p,channel_zeros_t<typename channel_type<PixelRef>::type>());
122  return p;
123  }
124 };
125 
126 // Hailin: This is how you can do it:
127 template <typename Pixel>
128 void zero_channels(Pixel& p) {
129  static_for_each(p,channel_zeros_t<typename channel_type<Pixel>::type>());
130 }
131 
132 
133 /// \ingroup PixelNumericOperations
134 ///definition and a generic implementation for casting and assigning a pixel to another
135 ///user should specialize it for better performance
136 template <typename PixelRef, // models pixel concept
137  typename PixelRefR> // models pixel concept
139  PixelRefR operator () (const PixelRef& src,
140  PixelRefR& dst) const {
141  static_for_each(src,dst,channel_assigns_t<typename channel_type<PixelRef>::type,
142  typename channel_type<PixelRefR>::type>());
143  return dst;
144  }
145 };
146 
147 } } // namespace boost::gil
148 
149 #endif
Structures for channel-wise numeric operations /.
construct for subtracting two pixels
construct for dividing a pixel by 2
construct for adding two pixels
construct for dividing a pixel by a scalar
construct for setting a pixel to zero (for whatever zero means)
construct for multiplying scalar to a pixel