minor formating fixes
[profile/ivi/opencv.git] / modules / gpu / src / cuda / blend.cu
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                           License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
15 // Third party copyrights are property of their respective owners.\r
16 //\r
17 // Redistribution and use in source and binary forms, with or without modification,\r
18 // are permitted provided that the following conditions are met:\r
19 //\r
20 //   * Redistribution's of source code must retain the above copyright notice,\r
21 //     this list of conditions and the following disclaimer.\r
22 //\r
23 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
24 //     this list of conditions and the following disclaimer in the documentation\r
25 //     and/or other materials provided with the distribution.\r
26 //\r
27 //   * The name of the copyright holders may not be used to endorse or promote products\r
28 //     derived from this software without specific prior written permission.\r
29 //\r
30 // This software is provided by the copyright holders and contributors "as is" and\r
31 // any express or bpied warranties, including, but not limited to, the bpied\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
34 // indirect, incidental, special, exemplary, or consequential damages\r
35 // (including, but not limited to, procurement of substitute goods or services;\r
36 // loss of use, data, or profits; or business interruption) however caused\r
37 // and on any theory of liability, whether in contract, strict liability,\r
38 // or tort (including negligence or otherwise) arising in any way out of\r
39 // the use of this software, even if advised of the possibility of such damage.\r
40 //\r
41 //M*/\r
42 \r
43 #include "internal_shared.hpp"\r
44 \r
45 namespace cv { namespace gpu { namespace device\r
46 {\r
47     namespace blend\r
48     {\r
49         template <typename T>\r
50         __global__ void blendLinearKernel(int rows, int cols, int cn, const PtrStep<T> img1, const PtrStep<T> img2,\r
51                                           const PtrStepf weights1, const PtrStepf weights2, PtrStep<T> result)\r
52         {\r
53             int x = blockIdx.x * blockDim.x + threadIdx.x;\r
54             int y = blockIdx.y * blockDim.y + threadIdx.y;\r
55 \r
56             if (y < rows && x < cols)\r
57             {\r
58                 int x_ = x / cn;\r
59                 float w1 = weights1.ptr(y)[x_];\r
60                 float w2 = weights2.ptr(y)[x_];\r
61                 T p1 = img1.ptr(y)[x];\r
62                 T p2 = img2.ptr(y)[x];\r
63                 result.ptr(y)[x] = (p1 * w1 + p2 * w2) / (w1 + w2 + 1e-5f);\r
64             }\r
65         }\r
66 \r
67         template <typename T>\r
68         void blendLinearCaller(int rows, int cols, int cn, PtrStep<T> img1, PtrStep<T> img2, PtrStepf weights1, PtrStepf weights2, PtrStep<T> result, cudaStream_t stream)\r
69         {\r
70             dim3 threads(16, 16);\r
71             dim3 grid(divUp(cols * cn, threads.x), divUp(rows, threads.y));\r
72 \r
73             blendLinearKernel<<<grid, threads, 0, stream>>>(rows, cols * cn, cn, img1, img2, weights1, weights2, result);\r
74             cudaSafeCall( cudaGetLastError() );\r
75 \r
76             if (stream == 0)\r
77                 cudaSafeCall(cudaDeviceSynchronize());\r
78         }\r
79 \r
80         template void blendLinearCaller<uchar>(int, int, int, PtrStep<uchar>, PtrStep<uchar>, PtrStepf, PtrStepf, PtrStep<uchar>, cudaStream_t stream);\r
81         template void blendLinearCaller<float>(int, int, int, PtrStep<float>, PtrStep<float>, PtrStepf, PtrStepf, PtrStep<float>, cudaStream_t stream);\r
82 \r
83 \r
84         __global__ void blendLinearKernel8UC4(int rows, int cols, const PtrStepb img1, const PtrStepb img2,\r
85                                               const PtrStepf weights1, const PtrStepf weights2, PtrStepb result)\r
86         {\r
87             int x = blockIdx.x * blockDim.x + threadIdx.x;\r
88             int y = blockIdx.y * blockDim.y + threadIdx.y;\r
89 \r
90             if (y < rows && x < cols)\r
91             {\r
92                 float w1 = weights1.ptr(y)[x];\r
93                 float w2 = weights2.ptr(y)[x];\r
94                 float sum_inv = 1.f / (w1 + w2 + 1e-5f);\r
95                 w1 *= sum_inv;\r
96                 w2 *= sum_inv;\r
97                 uchar4 p1 = ((const uchar4*)img1.ptr(y))[x];\r
98                 uchar4 p2 = ((const uchar4*)img2.ptr(y))[x];\r
99                 ((uchar4*)result.ptr(y))[x] = make_uchar4(p1.x * w1 + p2.x * w2, p1.y * w1 + p2.y * w2,\r
100                                                           p1.z * w1 + p2.z * w2, p1.w * w1 + p2.w * w2);\r
101             }\r
102         }\r
103 \r
104         void blendLinearCaller8UC4(int rows, int cols, PtrStepb img1, PtrStepb img2, PtrStepf weights1, PtrStepf weights2, PtrStepb result, cudaStream_t stream)\r
105         {\r
106             dim3 threads(16, 16);\r
107             dim3 grid(divUp(cols, threads.x), divUp(rows, threads.y));\r
108 \r
109             blendLinearKernel8UC4<<<grid, threads, 0, stream>>>(rows, cols, img1, img2, weights1, weights2, result);\r
110             cudaSafeCall( cudaGetLastError() );\r
111 \r
112             if (stream == 0)\r
113                 cudaSafeCall(cudaDeviceSynchronize());\r
114         }\r
115     } // namespace blend\r
116 }}} // namespace cv { namespace gpu { namespace device\r