Turi Create  4.0
algorithm.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_NUMERIC_ALGORITHM_HPP
15 #define GIL_NUMERIC_ALGORITHM_HPP
16 
17 /*!
18 /// \file
19 /// \brief Numeric algorithms
20 /// \author Hailin Jin and Lubomir Bourdev \n
21 /// Adobe Systems Incorporated
22 /// \date 2005-2007 \n Last updated on February 6, 2007
23 */
24 
25 #include <cassert>
26 #include <iterator>
27 #include <algorithm>
28 #include <numeric>
29 #include "../../gil_config.hpp"
30 #include "../../pixel_iterator.hpp"
31 #include "../../metafunctions.hpp"
32 
33 namespace boost { namespace gil {
34 
35 /// \brief Returns the reference proxy associated with a type that has a \p "reference" member typedef.
36 ///
37 /// The reference proxy is the reference type, but with stripped-out C++ reference. It models PixelConcept
38 template <typename T>
39 struct pixel_proxy : public remove_reference<typename T::reference> {};
40 
41 /// \brief std::for_each for a pair of iterators
42 template <typename Iterator1,typename Iterator2,typename BinaryFunction>
43 BinaryFunction for_each(Iterator1 first1,Iterator1 last1,Iterator2 first2,BinaryFunction f) {
44  while(first1!=last1)
45  f(*first1++,*first2++);
46  return f;
47 }
48 
49 template <typename SrcIterator,typename DstIterator>
50 inline DstIterator assign_pixels(SrcIterator src,SrcIterator src_end,DstIterator dst) {
51  for_each(src,src_end,dst,pixel_assigns_t<typename pixel_proxy<typename std::iterator_traits<SrcIterator>::value_type>::type,
52  typename pixel_proxy<typename std::iterator_traits<DstIterator>::value_type>::type>());
53  return dst+(src_end-src);
54 }
55 
56 namespace detail {
57 template <std::size_t Size>
58 struct inner_product_k_t {
59  template <class _InputIterator1, class _InputIterator2, class _Tp,
60  class _BinaryOperation1, class _BinaryOperation2>
61  static _Tp apply(_InputIterator1 __first1,
62  _InputIterator2 __first2, _Tp __init,
63  _BinaryOperation1 __binary_op1,
64  _BinaryOperation2 __binary_op2) {
65  __init = __binary_op1(__init, __binary_op2(*__first1, *__first2));
66  return inner_product_k_t<Size-1>::template apply(__first1+1,__first2+1,__init,
67  __binary_op1, __binary_op2);
68  }
69 };
70 
71 template <>
72 struct inner_product_k_t<0> {
73  template <class _InputIterator1, class _InputIterator2, class _Tp,
74  class _BinaryOperation1, class _BinaryOperation2>
75  static _Tp apply(_InputIterator1 __first1,
76  _InputIterator2 __first2, _Tp __init,
77  _BinaryOperation1 __binary_op1,
78  _BinaryOperation2 __binary_op2) {
79  return __init;
80  }
81 };
82 } // namespace detail
83 
84 /// static version of std::inner_product
85 template <std::size_t Size,
86  class _InputIterator1, class _InputIterator2, class _Tp,
87  class _BinaryOperation1, class _BinaryOperation2>
88 
89 _Tp inner_product_k(_InputIterator1 __first1,
90  _InputIterator2 __first2,
91  _Tp __init,
92  _BinaryOperation1 __binary_op1,
93  _BinaryOperation2 __binary_op2) {
94  return detail::inner_product_k_t<Size>::template apply(__first1,__first2,__init,
95  __binary_op1, __binary_op2);
96 }
97 
98 /// \brief 1D un-guarded correlation with a variable-size kernel
99 template <typename PixelAccum,typename SrcIterator,typename KernelIterator,typename Integer,typename DstIterator>
100 inline DstIterator correlate_pixels_n(SrcIterator src_begin,SrcIterator src_end,
101  KernelIterator ker_begin,Integer ker_size,
102  DstIterator dst_begin) {
103  typedef typename pixel_proxy<typename std::iterator_traits<SrcIterator>::value_type>::type PIXEL_SRC_REF;
104  typedef typename pixel_proxy<typename std::iterator_traits<DstIterator>::value_type>::type PIXEL_DST_REF;
105  typedef typename std::iterator_traits<KernelIterator>::value_type kernel_type;
106  PixelAccum acc_zero; pixel_zeros_t<PixelAccum>()(acc_zero);
107  while(src_begin!=src_end) {
109  std::inner_product(src_begin,src_begin+ker_size,ker_begin,acc_zero,
112  *dst_begin);
113  ++src_begin; ++dst_begin;
114  }
115  return dst_begin;
116 }
117 
118 /// \brief 1D un-guarded correlation with a fixed-size kernel
119 template <std::size_t Size,typename PixelAccum,typename SrcIterator,typename KernelIterator,typename DstIterator>
120 inline DstIterator correlate_pixels_k(SrcIterator src_begin,SrcIterator src_end,
121  KernelIterator ker_begin,
122  DstIterator dst_begin) {
123  typedef typename pixel_proxy<typename std::iterator_traits<SrcIterator>::value_type>::type PIXEL_SRC_REF;
124  typedef typename pixel_proxy<typename std::iterator_traits<DstIterator>::value_type>::type PIXEL_DST_REF;
125  typedef typename std::iterator_traits<KernelIterator>::value_type kernel_type;
126  PixelAccum acc_zero; pixel_zeros_t<PixelAccum>()(acc_zero);
127  while(src_begin!=src_end) {
129  inner_product_k<Size>(src_begin,ker_begin,acc_zero,
132  *dst_begin);
133  ++src_begin; ++dst_begin;
134  }
135  return dst_begin;
136 }
137 
138 /// \brief destination is set to be product of the source and a scalar
139 template <typename PixelAccum,typename SrcView,typename Scalar,typename DstView>
140 inline void view_multiplies_scalar(const SrcView& src,const Scalar& scalar,const DstView& dst) {
141  assert(src.dimensions()==dst.dimensions());
142  typedef typename pixel_proxy<typename SrcView::value_type>::type PIXEL_SRC_REF;
143  typedef typename pixel_proxy<typename DstView::value_type>::type PIXEL_DST_REF;
144  int height=src.height();
145  for(int rr=0;rr<height;++rr) {
146  typename SrcView::x_iterator it_src=src.row_begin(rr);
147  typename DstView::x_iterator it_dst=dst.row_begin(rr);
148  typename SrcView::x_iterator it_src_end=src.row_end(rr);
149  while(it_src!=it_src_end) {
152  *it_dst);
153  ++it_src; ++it_dst;
154  }
155  }
156 }
157 
158 } } // namespace boost::gil
159 
160 #endif
_Tp inner_product_k(_InputIterator1 __first1, _InputIterator2 __first2, _Tp __init, _BinaryOperation1 __binary_op1, _BinaryOperation2 __binary_op2)
static version of std::inner_product
Definition: algorithm.hpp:89
BinaryFunction for_each(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryFunction f)
std::for_each for a pair of iterators
Definition: algorithm.hpp:43
construct for adding two pixels
DstIterator correlate_pixels_n(SrcIterator src_begin, SrcIterator src_end, KernelIterator ker_begin, Integer ker_size, DstIterator dst_begin)
1D un-guarded correlation with a variable-size kernel
Definition: algorithm.hpp:100
Returns the reference proxy associated with a type that has a "reference" member typedef.
Definition: algorithm.hpp:39
construct for setting a pixel to zero (for whatever zero means)
DstIterator correlate_pixels_k(SrcIterator src_begin, SrcIterator src_end, KernelIterator ker_begin, DstIterator dst_begin)
1D un-guarded correlation with a fixed-size kernel
Definition: algorithm.hpp:120
void view_multiplies_scalar(const SrcView &src, const Scalar &scalar, const DstView &dst)
destination is set to be product of the source and a scalar
Definition: algorithm.hpp:140
construct for multiplying scalar to a pixel