1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #pragma once
45 
46 #ifndef __OPENCV_CUDEV_PTR2D_EXTRAPOLATION_HPP__
47 #define __OPENCV_CUDEV_PTR2D_EXTRAPOLATION_HPP__
48 
49 #include "../common.hpp"
50 #include "../util/vec_traits.hpp"
51 #include "traits.hpp"
52 
53 namespace cv { namespace cudev {
54 
55 //! @addtogroup cudev
56 //! @{
57 
58 // BrdConstant
59 
60 template <class SrcPtr> struct BrdConstant
61 {
62     typedef typename PtrTraits<SrcPtr>::value_type value_type;
63     typedef int                                    index_type;
64 
65     SrcPtr src;
66     int rows, cols;
67     typename PtrTraits<SrcPtr>::value_type val;
68 
operator ()cv::cudev::BrdConstant69     __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
70     {
71         return (x >= 0 && x < cols && y >= 0 && y < rows) ? src(y, x) : val;
72     }
73 };
74 
75 template <class SrcPtr>
brdConstant(const SrcPtr & src,typename PtrTraits<SrcPtr>::value_type val)76 __host__ BrdConstant<typename PtrTraits<SrcPtr>::ptr_type> brdConstant(const SrcPtr& src, typename PtrTraits<SrcPtr>::value_type val)
77 {
78     BrdConstant<typename PtrTraits<SrcPtr>::ptr_type> b;
79     b.src = shrinkPtr(src);
80     b.rows = getRows(src);
81     b.cols = getCols(src);
82     b.val = val;
83     return b;
84 }
85 
86 template <class SrcPtr>
brdConstant(const SrcPtr & src)87 __host__ BrdConstant<typename PtrTraits<SrcPtr>::ptr_type> brdConstant(const SrcPtr& src)
88 {
89     return brdConstant(src, VecTraits<typename PtrTraits<SrcPtr>::value_type>::all(0));
90 }
91 
92 // BrdBase
93 
94 template <class BrdImpl, class SrcPtr> struct BrdBase
95 {
96     typedef typename PtrTraits<SrcPtr>::value_type value_type;
97     typedef int                                    index_type;
98 
99     SrcPtr src;
100     int rows, cols;
101 
idx_rowcv::cudev::BrdBase102     __device__ __forceinline__ int idx_row(int y) const
103     {
104         return BrdImpl::idx_low(BrdImpl::idx_high(y, rows), rows);
105     }
106 
idx_colcv::cudev::BrdBase107     __device__ __forceinline__ int idx_col(int x) const
108     {
109         return BrdImpl::idx_low(BrdImpl::idx_high(x, cols), cols);
110     }
111 
operator ()cv::cudev::BrdBase112     __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(int y, int x) const
113     {
114         return src(idx_row(y), idx_col(x));
115     }
116 };
117 
118 // BrdReplicate
119 
120 struct BrdReplicate
121 {
idx_lowcv::cudev::BrdReplicate122     __device__ __forceinline__ static int idx_low(int i, int len)
123     {
124         return ::max(i, 0);
125     }
126 
idx_highcv::cudev::BrdReplicate127     __device__ __forceinline__ static int idx_high(int i, int len)
128     {
129         return ::min(i, len - 1);
130     }
131 };
132 
133 template <class SrcPtr>
brdReplicate(const SrcPtr & src)134 __host__ BrdBase<BrdReplicate, typename PtrTraits<SrcPtr>::ptr_type> brdReplicate(const SrcPtr& src)
135 {
136     BrdBase<BrdReplicate, typename PtrTraits<SrcPtr>::ptr_type> b;
137     b.src = shrinkPtr(src);
138     b.rows = getRows(src);
139     b.cols = getCols(src);
140     return b;
141 }
142 
143 // BrdReflect101
144 
145 struct BrdReflect101
146 {
idx_lowcv::cudev::BrdReflect101147     __device__ __forceinline__ static int idx_low(int i, int len)
148     {
149         return ::abs(i) % len;
150     }
151 
idx_highcv::cudev::BrdReflect101152     __device__ __forceinline__ static int idx_high(int i, int len)
153     {
154         const int last_ind = len - 1;
155         return ::abs(last_ind - ::abs(last_ind - i)) % len;
156     }
157 };
158 
159 template <class SrcPtr>
brdReflect101(const SrcPtr & src)160 __host__ BrdBase<BrdReflect101, typename PtrTraits<SrcPtr>::ptr_type> brdReflect101(const SrcPtr& src)
161 {
162     BrdBase<BrdReflect101, typename PtrTraits<SrcPtr>::ptr_type> b;
163     b.src = shrinkPtr(src);
164     b.rows = getRows(src);
165     b.cols = getCols(src);
166     return b;
167 }
168 
169 // BrdReflect
170 
171 struct BrdReflect
172 {
idx_lowcv::cudev::BrdReflect173     __device__ __forceinline__ static int idx_low(int i, int len)
174     {
175         return (::abs(i) - (i < 0)) % len;
176     }
177 
idx_highcv::cudev::BrdReflect178     __device__ __forceinline__ static int idx_high(int i, int len)
179     {
180         const int last_ind = len - 1;
181         return (last_ind - ::abs(last_ind - i) + (i > last_ind));
182     }
183 };
184 
185 template <class SrcPtr>
brdReflect(const SrcPtr & src)186 __host__ BrdBase<BrdReflect, typename PtrTraits<SrcPtr>::ptr_type> brdReflect(const SrcPtr& src)
187 {
188     BrdBase<BrdReflect, typename PtrTraits<SrcPtr>::ptr_type> b;
189     b.src = shrinkPtr(src);
190     b.rows = getRows(src);
191     b.cols = getCols(src);
192     return b;
193 }
194 
195 // BrdWrap
196 
197 struct BrdWrap
198 {
idx_lowcv::cudev::BrdWrap199     __device__ __forceinline__ static int idx_low(int i, int len)
200     {
201         return (i >= 0) * i + (i < 0) * (i - ((i - len + 1) / len) * len);
202     }
203 
idx_highcv::cudev::BrdWrap204     __device__ __forceinline__ static int idx_high(int i, int len)
205     {
206         return (i < len) * i + (i >= len) * (i % len);
207     }
208 };
209 
210 template <class SrcPtr>
brdWrap(const SrcPtr & src)211 __host__ BrdBase<BrdWrap, typename PtrTraits<SrcPtr>::ptr_type> brdWrap(const SrcPtr& src)
212 {
213     BrdBase<BrdWrap, typename PtrTraits<SrcPtr>::ptr_type> b;
214     b.src = shrinkPtr(src);
215     b.rows = getRows(src);
216     b.cols = getCols(src);
217     return b;
218 }
219 
220 //! @}
221 
222 }}
223 
224 #endif
225