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/cudaarithm.hpp"
52 #include "opencv2/cudev.hpp"
53 #include "opencv2/core/private.cuda.hpp"
54
55 using namespace cv;
56 using namespace cv::cuda;
57 using namespace cv::cudev;
58
59 //////////////////////////////////////////////////////////////////////////////
60 // mulSpectrums
61
62 namespace
63 {
real(const float2 & val)64 __device__ __forceinline__ float real(const float2& val)
65 {
66 return val.x;
67 }
68
imag(const float2 & val)69 __device__ __forceinline__ float imag(const float2& val)
70 {
71 return val.y;
72 }
73
cmul(const float2 & a,const float2 & b)74 __device__ __forceinline__ float2 cmul(const float2& a, const float2& b)
75 {
76 return make_float2((real(a) * real(b)) - (imag(a) * imag(b)),
77 (real(a) * imag(b)) + (imag(a) * real(b)));
78 }
79
conj(const float2 & a)80 __device__ __forceinline__ float2 conj(const float2& a)
81 {
82 return make_float2(real(a), -imag(a));
83 }
84
85 struct comlex_mul : binary_function<float2, float2, float2>
86 {
operator ()__anona21cebd80111::comlex_mul87 __device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
88 {
89 return cmul(a, b);
90 }
91 };
92
93 struct comlex_mul_conj : binary_function<float2, float2, float2>
94 {
operator ()__anona21cebd80111::comlex_mul_conj95 __device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
96 {
97 return cmul(a, conj(b));
98 }
99 };
100
101 struct comlex_mul_scale : binary_function<float2, float2, float2>
102 {
103 float scale;
104
operator ()__anona21cebd80111::comlex_mul_scale105 __device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
106 {
107 return scale * cmul(a, b);
108 }
109 };
110
111 struct comlex_mul_conj_scale : binary_function<float2, float2, float2>
112 {
113 float scale;
114
operator ()__anona21cebd80111::comlex_mul_conj_scale115 __device__ __forceinline__ float2 operator ()(const float2& a, const float2& b) const
116 {
117 return scale * cmul(a, conj(b));
118 }
119 };
120 }
121
mulSpectrums(InputArray _src1,InputArray _src2,OutputArray _dst,int flags,bool conjB,Stream & stream)122 void cv::cuda::mulSpectrums(InputArray _src1, InputArray _src2, OutputArray _dst, int flags, bool conjB, Stream& stream)
123 {
124 (void) flags;
125
126 GpuMat src1 = getInputMat(_src1, stream);
127 GpuMat src2 = getInputMat(_src2, stream);
128
129 CV_Assert( src1.type() == src2.type() && src1.type() == CV_32FC2 );
130 CV_Assert( src1.size() == src2.size() );
131
132 GpuMat dst = getOutputMat(_dst, src1.size(), CV_32FC2, stream);
133
134 if (conjB)
135 gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), comlex_mul_conj(), stream);
136 else
137 gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), comlex_mul(), stream);
138
139 syncOutput(dst, _dst, stream);
140 }
141
mulAndScaleSpectrums(InputArray _src1,InputArray _src2,OutputArray _dst,int flags,float scale,bool conjB,Stream & stream)142 void cv::cuda::mulAndScaleSpectrums(InputArray _src1, InputArray _src2, OutputArray _dst, int flags, float scale, bool conjB, Stream& stream)
143 {
144 (void) flags;
145
146 GpuMat src1 = getInputMat(_src1, stream);
147 GpuMat src2 = getInputMat(_src2, stream);
148
149 CV_Assert( src1.type() == src2.type() && src1.type() == CV_32FC2);
150 CV_Assert( src1.size() == src2.size() );
151
152 GpuMat dst = getOutputMat(_dst, src1.size(), CV_32FC2, stream);
153
154 if (conjB)
155 {
156 comlex_mul_conj_scale op;
157 op.scale = scale;
158 gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), op, stream);
159 }
160 else
161 {
162 comlex_mul_scale op;
163 op.scale = scale;
164 gridTransformBinary(globPtr<float2>(src1), globPtr<float2>(src2), globPtr<float2>(dst), op, stream);
165 }
166
167 syncOutput(dst, _dst, stream);
168 }
169
170 #endif
171