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_INTERPOLATION_HPP__
47 #define __OPENCV_CUDEV_PTR2D_INTERPOLATION_HPP__
48
49 #include "../common.hpp"
50 #include "../util/vec_traits.hpp"
51 #include "../util/saturate_cast.hpp"
52 #include "../util/type_traits.hpp"
53 #include "../util/limits.hpp"
54 #include "traits.hpp"
55
56 namespace cv { namespace cudev {
57
58 //! @addtogroup cudev
59 //! @{
60
61 // Nearest
62
63 template <class SrcPtr> struct NearestInterPtr
64 {
65 typedef typename PtrTraits<SrcPtr>::value_type value_type;
66 typedef float index_type;
67
68 SrcPtr src;
69
operator ()cv::cudev::NearestInterPtr70 __device__ __forceinline__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
71 {
72 return src(__float2int_rn(y), __float2int_rn(x));
73 }
74 };
75
76 template <class SrcPtr> struct NearestInterPtrSz : NearestInterPtr<SrcPtr>
77 {
78 int rows, cols;
79 };
80
81 template <class SrcPtr>
interNearest(const SrcPtr & src)82 __host__ NearestInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interNearest(const SrcPtr& src)
83 {
84 NearestInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
85 i.src = shrinkPtr(src);
86 i.rows = getRows(src);
87 i.cols = getCols(src);
88 return i;
89 }
90
91 template <class SrcPtr> struct PtrTraits< NearestInterPtrSz<SrcPtr> > : PtrTraitsBase<NearestInterPtrSz<SrcPtr>, NearestInterPtr<SrcPtr> >
92 {
93 };
94
95 // Linear
96
97 template <typename SrcPtr> struct LinearInterPtr
98 {
99 typedef typename PtrTraits<SrcPtr>::value_type value_type;
100 typedef float index_type;
101
102 SrcPtr src;
103
operator ()cv::cudev::LinearInterPtr104 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
105 {
106 typedef typename PtrTraits<SrcPtr>::value_type src_type;
107 typedef typename VecTraits<src_type>::elem_type src_elem_type;
108 typedef typename LargerType<float, src_elem_type>::type work_elem_type;
109 typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
110
111 work_type out = VecTraits<work_type>::all(0);
112
113 const int x1 = __float2int_rd(x);
114 const int y1 = __float2int_rd(y);
115 const int x2 = x1 + 1;
116 const int y2 = y1 + 1;
117
118 typename PtrTraits<SrcPtr>::value_type src_reg = src(y1, x1);
119 out = out + src_reg * static_cast<work_elem_type>((x2 - x) * (y2 - y));
120
121 src_reg = src(y1, x2);
122 out = out + src_reg * static_cast<work_elem_type>((x - x1) * (y2 - y));
123
124 src_reg = src(y2, x1);
125 out = out + src_reg * static_cast<work_elem_type>((x2 - x) * (y - y1));
126
127 src_reg = src(y2, x2);
128 out = out + src_reg * static_cast<work_elem_type>((x - x1) * (y - y1));
129
130 return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(out);
131 }
132 };
133
134 template <class SrcPtr> struct LinearInterPtrSz : LinearInterPtr<SrcPtr>
135 {
136 int rows, cols;
137 };
138
139 template <class SrcPtr>
interLinear(const SrcPtr & src)140 __host__ LinearInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interLinear(const SrcPtr& src)
141 {
142 LinearInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
143 i.src = shrinkPtr(src);
144 i.rows = getRows(src);
145 i.cols = getCols(src);
146 return i;
147 }
148
149 template <class SrcPtr> struct PtrTraits< LinearInterPtrSz<SrcPtr> > : PtrTraitsBase<LinearInterPtrSz<SrcPtr>, LinearInterPtr<SrcPtr> >
150 {
151 };
152
153 // Cubic
154
155 template <typename SrcPtr> struct CubicInterPtr
156 {
157 typedef typename PtrTraits<SrcPtr>::value_type value_type;
158 typedef float index_type;
159
160 SrcPtr src;
161
bicubicCoeffcv::cudev::CubicInterPtr162 __device__ static float bicubicCoeff(float x_)
163 {
164 float x = ::fabsf(x_);
165 if (x <= 1.0f)
166 {
167 return x * x * (1.5f * x - 2.5f) + 1.0f;
168 }
169 else if (x < 2.0f)
170 {
171 return x * (x * (-0.5f * x + 2.5f) - 4.0f) + 2.0f;
172 }
173 else
174 {
175 return 0.0f;
176 }
177 }
178
operator ()cv::cudev::CubicInterPtr179 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
180 {
181 typedef typename PtrTraits<SrcPtr>::value_type src_type;
182 typedef typename VecTraits<src_type>::elem_type src_elem_type;
183 typedef typename LargerType<float, src_elem_type>::type work_elem_type;
184 typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
185
186 const float xmin = ::ceilf(x - 2.0f);
187 const float xmax = ::floorf(x + 2.0f);
188
189 const float ymin = ::ceilf(y - 2.0f);
190 const float ymax = ::floorf(y + 2.0f);
191
192 work_type sum = VecTraits<work_type>::all(0);
193 float wsum = 0.0f;
194
195 for (float cy = ymin; cy <= ymax; cy += 1.0f)
196 {
197 for (float cx = xmin; cx <= xmax; cx += 1.0f)
198 {
199 typename PtrTraits<SrcPtr>::value_type src_reg = src(__float2int_rd(cy), __float2int_rd(cx));
200 const float w = bicubicCoeff(x - cx) * bicubicCoeff(y - cy);
201
202 sum = sum + static_cast<work_elem_type>(w) * src_reg;
203 wsum += w;
204 }
205 }
206
207 work_type res = (wsum > numeric_limits<float>::epsilon()) ? VecTraits<work_type>::all(0) : sum / static_cast<work_elem_type>(wsum);
208
209 return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(res);
210 }
211 };
212
213 template <class SrcPtr> struct CubicInterPtrSz : CubicInterPtr<SrcPtr>
214 {
215 int rows, cols;
216 };
217
218 template <class SrcPtr>
interCubic(const SrcPtr & src)219 __host__ CubicInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interCubic(const SrcPtr& src)
220 {
221 CubicInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
222 i.src = shrinkPtr(src);
223 i.rows = getRows(src);
224 i.cols = getCols(src);
225 return i;
226 }
227
228 template <class SrcPtr> struct PtrTraits< CubicInterPtrSz<SrcPtr> > : PtrTraitsBase<CubicInterPtrSz<SrcPtr>, CubicInterPtr<SrcPtr> >
229 {
230 };
231
232 // IntegerArea
233
234 template <typename SrcPtr> struct IntegerAreaInterPtr
235 {
236 typedef typename PtrTraits<SrcPtr>::value_type value_type;
237 typedef float index_type;
238
239 SrcPtr src;
240 int area_width, area_height;
241
operator ()cv::cudev::IntegerAreaInterPtr242 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
243 {
244 typedef typename PtrTraits<SrcPtr>::value_type src_type;
245 typedef typename VecTraits<src_type>::elem_type src_elem_type;
246 typedef typename LargerType<float, src_elem_type>::type work_elem_type;
247 typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
248
249 const int sx1 = __float2int_rd(x);
250 const int sx2 = sx1 + area_width;
251
252 const int sy1 = __float2int_rd(y);
253 const int sy2 = sy1 + area_height;
254
255 work_type out = VecTraits<work_type>::all(0);
256
257 for (int dy = sy1; dy < sy2; ++dy)
258 {
259 for (int dx = sx1; dx < sx2; ++dx)
260 {
261 out = out + saturate_cast<work_type>(src(dy, dx));
262 }
263 }
264
265 const work_elem_type scale = 1.0f / (area_width * area_height);
266
267 return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(out * scale);
268 }
269 };
270
271 template <class SrcPtr> struct IntegerAreaInterPtrSz : IntegerAreaInterPtr<SrcPtr>
272 {
273 int rows, cols;
274 };
275
276 template <class SrcPtr>
interArea(const SrcPtr & src,Size areaSize)277 __host__ IntegerAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interArea(const SrcPtr& src, Size areaSize)
278 {
279 IntegerAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
280 i.src = shrinkPtr(src);
281 i.area_width = areaSize.width;
282 i.area_height = areaSize.height;
283 i.rows = getRows(src);
284 i.cols = getCols(src);
285 return i;
286 }
287
288 template <class SrcPtr> struct PtrTraits< IntegerAreaInterPtrSz<SrcPtr> > : PtrTraitsBase<IntegerAreaInterPtrSz<SrcPtr>, IntegerAreaInterPtr<SrcPtr> >
289 {
290 };
291
292 // CommonArea
293
294 template <typename SrcPtr> struct CommonAreaInterPtr
295 {
296 typedef typename PtrTraits<SrcPtr>::value_type value_type;
297 typedef float index_type;
298
299 SrcPtr src;
300 float area_width, area_height;
301
operator ()cv::cudev::CommonAreaInterPtr302 __device__ typename PtrTraits<SrcPtr>::value_type operator ()(float y, float x) const
303 {
304 typedef typename PtrTraits<SrcPtr>::value_type src_type;
305 typedef typename VecTraits<src_type>::elem_type src_elem_type;
306 typedef typename LargerType<float, src_elem_type>::type work_elem_type;
307 typedef typename MakeVec<work_elem_type, VecTraits<src_type>::cn>::type work_type;
308
309 const float fsx1 = x;
310 const float fsx2 = fsx1 + area_width;
311
312 const int sx1 = __float2int_rd(fsx1);
313 const int sx2 = __float2int_ru(fsx2);
314
315 const float fsy1 = y;
316 const float fsy2 = fsy1 + area_height;
317
318 const int sy1 = __float2int_rd(fsy1);
319 const int sy2 = __float2int_ru(fsy2);
320
321 work_type out = VecTraits<work_type>::all(0);
322
323 for (int dy = sy1; dy < sy2; ++dy)
324 {
325 for (int dx = sx1; dx < sx2; ++dx)
326 out = out + saturate_cast<work_type>(src(dy, dx));
327
328 if (sx1 > fsx1)
329 out = out + saturate_cast<work_type>(src(dy, sx1 - 1)) * static_cast<work_elem_type>(sx1 - fsx1);
330
331 if (sx2 < fsx2)
332 out = out + saturate_cast<work_type>(src(dy, sx2)) * static_cast<work_elem_type>(fsx2 - sx2);
333 }
334
335 if (sy1 > fsy1)
336 {
337 for (int dx = sx1; dx < sx2; ++dx)
338 out = out + saturate_cast<work_type>(src(sy1 - 1, dx)) * static_cast<work_elem_type>(sy1 - fsy1);
339 }
340
341 if (sy2 < fsy2)
342 {
343 for (int dx = sx1; dx < sx2; ++dx)
344 out = out + saturate_cast<work_type>(src(sy2, dx)) * static_cast<work_elem_type>(fsy2 - sy2);
345 }
346
347 if ((sy1 > fsy1) && (sx1 > fsx1))
348 out = out + saturate_cast<work_type>(src(sy1 - 1, sx1 - 1)) * static_cast<work_elem_type>((sy1 - fsy1) * (sx1 - fsx1));
349
350 if ((sy1 > fsy1) && (sx2 < fsx2))
351 out = out + saturate_cast<work_type>(src(sy1 - 1, sx2)) * static_cast<work_elem_type>((sy1 - fsy1) * (fsx2 - sx2));
352
353 if ((sy2 < fsy2) && (sx2 < fsx2))
354 out = out + saturate_cast<work_type>(src(sy2, sx2)) * static_cast<work_elem_type>((fsy2 - sy2) * (fsx2 - sx2));
355
356 if ((sy2 < fsy2) && (sx1 > fsx1))
357 out = out + saturate_cast<work_type>(src(sy2, sx1 - 1)) * static_cast<work_elem_type>((fsy2 - sy2) * (sx1 - fsx1));
358
359 const work_elem_type scale = 1.0f / (area_width * area_height);
360
361 return saturate_cast<typename PtrTraits<SrcPtr>::value_type>(out * scale);
362 }
363 };
364
365 template <class SrcPtr> struct CommonAreaInterPtrSz : CommonAreaInterPtr<SrcPtr>
366 {
367 int rows, cols;
368 };
369
370 template <class SrcPtr>
interArea(const SrcPtr & src,Size2f areaSize)371 __host__ CommonAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> interArea(const SrcPtr& src, Size2f areaSize)
372 {
373 CommonAreaInterPtrSz<typename PtrTraits<SrcPtr>::ptr_type> i;
374 i.src = shrinkPtr(src);
375 i.area_width = areaSize.width;
376 i.area_height = areaSize.height;
377 i.rows = getRows(src);
378 i.cols = getCols(src);
379 return i;
380 }
381
382 template <class SrcPtr> struct PtrTraits< CommonAreaInterPtrSz<SrcPtr> > : PtrTraitsBase<CommonAreaInterPtrSz<SrcPtr>, CommonAreaInterPtr<SrcPtr> >
383 {
384 };
385
386 //! @}
387
388 }}
389
390 #endif
391