~paparazzi-uav/paparazzi/v5.0-manual

« back to all changes in this revision

Viewing changes to sw/ext/opencv_bebop/opencv/modules/cudaarithm/src/cuda/math.cu

  • Committer: Paparazzi buildbot
  • Date: 2016-05-18 15:00:29 UTC
  • Revision ID: felix.ruess+docbot@gmail.com-20160518150029-e8lgzi5kvb4p7un9
Manual import commit 4b8bbb730080dac23cf816b98908dacfabe2a8ec from v5.0 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
namespace
 
60
{
 
61
    template <typename ScalarDepth> struct TransformPolicy : DefaultTransformPolicy
 
62
    {
 
63
    };
 
64
    template <> struct TransformPolicy<double> : DefaultTransformPolicy
 
65
    {
 
66
        enum {
 
67
            shift = 1
 
68
        };
 
69
    };
 
70
}
 
71
 
 
72
//////////////////////////////////////////////////////////////////////////////
 
73
/// abs
 
74
 
 
75
namespace
 
76
{
 
77
    template <typename T>
 
78
    void absMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
 
79
    {
 
80
        gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), abs_func<T>(), stream);
 
81
    }
 
82
}
 
83
 
 
84
void cv::cuda::abs(InputArray _src, OutputArray _dst, Stream& stream)
 
85
{
 
86
    typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
 
87
    static const func_t funcs[] =
 
88
    {
 
89
        absMat<uchar>,
 
90
        absMat<schar>,
 
91
        absMat<ushort>,
 
92
        absMat<short>,
 
93
        absMat<int>,
 
94
        absMat<float>,
 
95
        absMat<double>
 
96
    };
 
97
 
 
98
    GpuMat src = getInputMat(_src, stream);
 
99
 
 
100
    CV_Assert( src.depth() <= CV_64F );
 
101
 
 
102
    GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
 
103
 
 
104
    funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
 
105
 
 
106
    syncOutput(dst, _dst, stream);
 
107
}
 
108
 
 
109
//////////////////////////////////////////////////////////////////////////////
 
110
/// sqr
 
111
 
 
112
namespace
 
113
{
 
114
    template <typename T> struct SqrOp : unary_function<T, T>
 
115
    {
 
116
        __device__ __forceinline__ T operator ()(T x) const
 
117
        {
 
118
            return cudev::saturate_cast<T>(x * x);
 
119
        }
 
120
    };
 
121
 
 
122
    template <typename T>
 
123
    void sqrMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
 
124
    {
 
125
        gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), SqrOp<T>(), stream);
 
126
    }
 
127
}
 
128
 
 
129
void cv::cuda::sqr(InputArray _src, OutputArray _dst, Stream& stream)
 
130
{
 
131
    typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
 
132
    static const func_t funcs[] =
 
133
    {
 
134
        sqrMat<uchar>,
 
135
        sqrMat<schar>,
 
136
        sqrMat<ushort>,
 
137
        sqrMat<short>,
 
138
        sqrMat<int>,
 
139
        sqrMat<float>,
 
140
        sqrMat<double>
 
141
    };
 
142
 
 
143
    GpuMat src = getInputMat(_src, stream);
 
144
 
 
145
    CV_Assert( src.depth() <= CV_64F );
 
146
 
 
147
    GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
 
148
 
 
149
    funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
 
150
 
 
151
    syncOutput(dst, _dst, stream);
 
152
}
 
153
 
 
154
//////////////////////////////////////////////////////////////////////////////
 
155
/// sqrt
 
156
 
 
157
namespace
 
158
{
 
159
    template <typename T>
 
160
    void sqrtMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
 
161
    {
 
162
        gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), sqrt_func<T>(), stream);
 
163
    }
 
164
}
 
165
 
 
166
void cv::cuda::sqrt(InputArray _src, OutputArray _dst, Stream& stream)
 
167
{
 
168
    typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
 
169
    static const func_t funcs[] =
 
170
    {
 
171
        sqrtMat<uchar>,
 
172
        sqrtMat<schar>,
 
173
        sqrtMat<ushort>,
 
174
        sqrtMat<short>,
 
175
        sqrtMat<int>,
 
176
        sqrtMat<float>,
 
177
        sqrtMat<double>
 
178
    };
 
179
 
 
180
    GpuMat src = getInputMat(_src, stream);
 
181
 
 
182
    CV_Assert( src.depth() <= CV_64F );
 
183
 
 
184
    GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
 
185
 
 
186
    funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
 
187
 
 
188
    syncOutput(dst, _dst, stream);
 
189
}
 
190
 
 
191
////////////////////////////////////////////////////////////////////////
 
192
/// exp
 
193
 
 
194
namespace
 
195
{
 
196
    template <typename T> struct ExpOp : unary_function<T, T>
 
197
    {
 
198
        __device__ __forceinline__ T operator ()(T x) const
 
199
        {
 
200
            exp_func<T> f;
 
201
            return cudev::saturate_cast<T>(f(x));
 
202
        }
 
203
    };
 
204
 
 
205
    template <typename T>
 
206
    void expMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
 
207
    {
 
208
        gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), ExpOp<T>(), stream);
 
209
    }
 
210
}
 
211
 
 
212
void cv::cuda::exp(InputArray _src, OutputArray _dst, Stream& stream)
 
213
{
 
214
    typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
 
215
    static const func_t funcs[] =
 
216
    {
 
217
        expMat<uchar>,
 
218
        expMat<schar>,
 
219
        expMat<ushort>,
 
220
        expMat<short>,
 
221
        expMat<int>,
 
222
        expMat<float>,
 
223
        expMat<double>
 
224
    };
 
225
 
 
226
    GpuMat src = getInputMat(_src, stream);
 
227
 
 
228
    CV_Assert( src.depth() <= CV_64F );
 
229
 
 
230
    GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
 
231
 
 
232
    funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
 
233
 
 
234
    syncOutput(dst, _dst, stream);
 
235
}
 
236
 
 
237
////////////////////////////////////////////////////////////////////////
 
238
// log
 
239
 
 
240
namespace
 
241
{
 
242
    template <typename T>
 
243
    void logMat(const GpuMat& src, const GpuMat& dst, Stream& stream)
 
244
    {
 
245
        gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), log_func<T>(), stream);
 
246
    }
 
247
}
 
248
 
 
249
void cv::cuda::log(InputArray _src, OutputArray _dst, Stream& stream)
 
250
{
 
251
    typedef void (*func_t)(const GpuMat& src, const GpuMat& dst, Stream& stream);
 
252
    static const func_t funcs[] =
 
253
    {
 
254
        logMat<uchar>,
 
255
        logMat<schar>,
 
256
        logMat<ushort>,
 
257
        logMat<short>,
 
258
        logMat<int>,
 
259
        logMat<float>,
 
260
        logMat<double>
 
261
    };
 
262
 
 
263
    GpuMat src = getInputMat(_src, stream);
 
264
 
 
265
    CV_Assert( src.depth() <= CV_64F );
 
266
 
 
267
    GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
 
268
 
 
269
    funcs[src.depth()](src.reshape(1), dst.reshape(1), stream);
 
270
 
 
271
    syncOutput(dst, _dst, stream);
 
272
}
 
273
 
 
274
////////////////////////////////////////////////////////////////////////
 
275
// pow
 
276
 
 
277
namespace
 
278
{
 
279
    template<typename T, bool Signed = numeric_limits<T>::is_signed> struct PowOp : unary_function<T, T>
 
280
    {
 
281
        float power;
 
282
 
 
283
        __device__ __forceinline__ T operator()(T e) const
 
284
        {
 
285
            return cudev::saturate_cast<T>(__powf((float)e, power));
 
286
        }
 
287
    };
 
288
    template<typename T> struct PowOp<T, true> : unary_function<T, T>
 
289
    {
 
290
        float power;
 
291
 
 
292
        __device__ __forceinline__ T operator()(T e) const
 
293
        {
 
294
            T res = cudev::saturate_cast<T>(__powf((float)e, power));
 
295
 
 
296
            if ((e < 0) && (1 & static_cast<int>(power)))
 
297
                res *= -1;
 
298
 
 
299
            return res;
 
300
        }
 
301
    };
 
302
    template<> struct PowOp<float> : unary_function<float, float>
 
303
    {
 
304
        float power;
 
305
 
 
306
        __device__ __forceinline__ float operator()(float e) const
 
307
        {
 
308
            return __powf(::fabs(e), power);
 
309
        }
 
310
    };
 
311
    template<> struct PowOp<double> : unary_function<double, double>
 
312
    {
 
313
        double power;
 
314
 
 
315
        __device__ __forceinline__ double operator()(double e) const
 
316
        {
 
317
            return ::pow(::fabs(e), power);
 
318
        }
 
319
    };
 
320
 
 
321
    template<typename T>
 
322
    void powMat(const GpuMat& src, double power, const GpuMat& dst, Stream& stream)
 
323
    {
 
324
        PowOp<T> op;
 
325
        op.power = static_cast<typename LargerType<T, float>::type>(power);
 
326
 
 
327
        gridTransformUnary_< TransformPolicy<T> >(globPtr<T>(src), globPtr<T>(dst), op, stream);
 
328
    }
 
329
}
 
330
 
 
331
void cv::cuda::pow(InputArray _src, double power, OutputArray _dst, Stream& stream)
 
332
{
 
333
    typedef void (*func_t)(const GpuMat& src, double power, const GpuMat& dst, Stream& stream);
 
334
    static const func_t funcs[] =
 
335
    {
 
336
        powMat<uchar>,
 
337
        powMat<schar>,
 
338
        powMat<ushort>,
 
339
        powMat<short>,
 
340
        powMat<int>,
 
341
        powMat<float>,
 
342
        powMat<double>
 
343
    };
 
344
 
 
345
    GpuMat src = getInputMat(_src, stream);
 
346
 
 
347
    CV_Assert( src.depth() <= CV_64F );
 
348
 
 
349
    GpuMat dst = getOutputMat(_dst, src.size(), src.type(), stream);
 
350
 
 
351
    funcs[src.depth()](src.reshape(1), power, dst.reshape(1), stream);
 
352
 
 
353
    syncOutput(dst, _dst, stream);
 
354
}
 
355
 
 
356
#endif