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 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "opencv2/opencv_modules.hpp"
44
45 #ifndef HAVE_OPENCV_CUDEV
46
47 #error "opencv_cudev is required"
48
49 #else
50
51 #include "opencv2/cudev.hpp"
52
53 using namespace cv::cudev;
54
55 void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int);
56
57 namespace
58 {
_abs(int a)59 __device__ __forceinline__ int _abs(int a)
60 {
61 return ::abs(a);
62 }
_abs(float a)63 __device__ __forceinline__ float _abs(float a)
64 {
65 return ::fabsf(a);
66 }
_abs(double a)67 __device__ __forceinline__ double _abs(double a)
68 {
69 return ::fabs(a);
70 }
71
72 template <typename T> struct AbsDiffOp1 : binary_function<T, T, T>
73 {
operator ()__anon0204dbd50111::AbsDiffOp174 __device__ __forceinline__ T operator ()(T a, T b) const
75 {
76 return saturate_cast<T>(_abs(a - b));
77 }
78 };
79
80 template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
81 {
82 };
83 template <> struct TransformPolicy<double> : DefaultTransformPolicy
84 {
85 enum {
86 shift = 1
87 };
88 };
89
90 template <typename T>
absDiffMat_v1(const GpuMat & src1,const GpuMat & src2,GpuMat & dst,Stream & stream)91 void absDiffMat_v1(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
92 {
93 gridTransformBinary_< TransformPolicy<T> >(globPtr<T>(src1), globPtr<T>(src2), globPtr<T>(dst), AbsDiffOp1<T>(), stream);
94 }
95
96 struct AbsDiffOp2 : binary_function<uint, uint, uint>
97 {
operator ()__anon0204dbd50111::AbsDiffOp298 __device__ __forceinline__ uint operator ()(uint a, uint b) const
99 {
100 return vabsdiff2(a, b);
101 }
102 };
103
absDiffMat_v2(const GpuMat & src1,const GpuMat & src2,GpuMat & dst,Stream & stream)104 void absDiffMat_v2(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
105 {
106 const int vcols = src1.cols >> 1;
107
108 GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
109 GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
110 GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
111
112 gridTransformBinary(src1_, src2_, dst_, AbsDiffOp2(), stream);
113 }
114
115 struct AbsDiffOp4 : binary_function<uint, uint, uint>
116 {
operator ()__anon0204dbd50111::AbsDiffOp4117 __device__ __forceinline__ uint operator ()(uint a, uint b) const
118 {
119 return vabsdiff4(a, b);
120 }
121 };
122
absDiffMat_v4(const GpuMat & src1,const GpuMat & src2,GpuMat & dst,Stream & stream)123 void absDiffMat_v4(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream)
124 {
125 const int vcols = src1.cols >> 2;
126
127 GlobPtrSz<uint> src1_ = globPtr((uint*) src1.data, src1.step, src1.rows, vcols);
128 GlobPtrSz<uint> src2_ = globPtr((uint*) src2.data, src2.step, src1.rows, vcols);
129 GlobPtrSz<uint> dst_ = globPtr((uint*) dst.data, dst.step, src1.rows, vcols);
130
131 gridTransformBinary(src1_, src2_, dst_, AbsDiffOp4(), stream);
132 }
133 }
134
absDiffMat(const GpuMat & src1,const GpuMat & src2,GpuMat & dst,const GpuMat &,double,Stream & stream,int)135 void absDiffMat(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat&, double, Stream& stream, int)
136 {
137 typedef void (*func_t)(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream);
138 static const func_t funcs[] =
139 {
140 absDiffMat_v1<uchar>,
141 absDiffMat_v1<schar>,
142 absDiffMat_v1<ushort>,
143 absDiffMat_v1<short>,
144 absDiffMat_v1<int>,
145 absDiffMat_v1<float>,
146 absDiffMat_v1<double>
147 };
148
149 const int depth = src1.depth();
150
151 CV_DbgAssert( depth <= CV_64F );
152
153 GpuMat src1_ = src1.reshape(1);
154 GpuMat src2_ = src2.reshape(1);
155 GpuMat dst_ = dst.reshape(1);
156
157 if (depth == CV_8U || depth == CV_16U)
158 {
159 const intptr_t src1ptr = reinterpret_cast<intptr_t>(src1_.data);
160 const intptr_t src2ptr = reinterpret_cast<intptr_t>(src2_.data);
161 const intptr_t dstptr = reinterpret_cast<intptr_t>(dst_.data);
162
163 const bool isAllAligned = (src1ptr & 31) == 0 && (src2ptr & 31) == 0 && (dstptr & 31) == 0;
164
165 if (isAllAligned)
166 {
167 if (depth == CV_8U && (src1_.cols & 3) == 0)
168 {
169 absDiffMat_v4(src1_, src2_, dst_, stream);
170 return;
171 }
172 else if (depth == CV_16U && (src1_.cols & 1) == 0)
173 {
174 absDiffMat_v2(src1_, src2_, dst_, stream);
175 return;
176 }
177 }
178 }
179
180 const func_t func = funcs[depth];
181
182 if (!func)
183 CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types");
184
185 func(src1_, src2_, dst_, stream);
186 }
187
188 #endif
189