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#if !defined CUDA_DISABLER 44 45#include "opencv2/core/cuda/common.hpp" 46 47namespace cv { namespace cuda { namespace device 48{ 49 namespace blend 50 { 51 template <typename T> 52 __global__ void blendLinearKernel(int rows, int cols, int cn, const PtrStep<T> img1, const PtrStep<T> img2, 53 const PtrStepf weights1, const PtrStepf weights2, PtrStep<T> result) 54 { 55 int x = blockIdx.x * blockDim.x + threadIdx.x; 56 int y = blockIdx.y * blockDim.y + threadIdx.y; 57 58 if (y < rows && x < cols) 59 { 60 int x_ = x / cn; 61 float w1 = weights1.ptr(y)[x_]; 62 float w2 = weights2.ptr(y)[x_]; 63 T p1 = img1.ptr(y)[x]; 64 T p2 = img2.ptr(y)[x]; 65 result.ptr(y)[x] = (p1 * w1 + p2 * w2) / (w1 + w2 + 1e-5f); 66 } 67 } 68 69 template <typename T> 70 void blendLinearCaller(int rows, int cols, int cn, PtrStep<T> img1, PtrStep<T> img2, PtrStepf weights1, PtrStepf weights2, PtrStep<T> result, cudaStream_t stream) 71 { 72 dim3 threads(16, 16); 73 dim3 grid(divUp(cols * cn, threads.x), divUp(rows, threads.y)); 74 75 blendLinearKernel<<<grid, threads, 0, stream>>>(rows, cols * cn, cn, img1, img2, weights1, weights2, result); 76 cudaSafeCall( cudaGetLastError() ); 77 78 if (stream == 0) 79 cudaSafeCall(cudaDeviceSynchronize()); 80 } 81 82 template void blendLinearCaller<uchar>(int, int, int, PtrStep<uchar>, PtrStep<uchar>, PtrStepf, PtrStepf, PtrStep<uchar>, cudaStream_t stream); 83 template void blendLinearCaller<float>(int, int, int, PtrStep<float>, PtrStep<float>, PtrStepf, PtrStepf, PtrStep<float>, cudaStream_t stream); 84 85 86 __global__ void blendLinearKernel8UC4(int rows, int cols, const PtrStepb img1, const PtrStepb img2, 87 const PtrStepf weights1, const PtrStepf weights2, PtrStepb result) 88 { 89 int x = blockIdx.x * blockDim.x + threadIdx.x; 90 int y = blockIdx.y * blockDim.y + threadIdx.y; 91 92 if (y < rows && x < cols) 93 { 94 float w1 = weights1.ptr(y)[x]; 95 float w2 = weights2.ptr(y)[x]; 96 float sum_inv = 1.f / (w1 + w2 + 1e-5f); 97 w1 *= sum_inv; 98 w2 *= sum_inv; 99 uchar4 p1 = ((const uchar4*)img1.ptr(y))[x]; 100 uchar4 p2 = ((const uchar4*)img2.ptr(y))[x]; 101 ((uchar4*)result.ptr(y))[x] = make_uchar4(p1.x * w1 + p2.x * w2, p1.y * w1 + p2.y * w2, 102 p1.z * w1 + p2.z * w2, p1.w * w1 + p2.w * w2); 103 } 104 } 105 106 void blendLinearCaller8UC4(int rows, int cols, PtrStepb img1, PtrStepb img2, PtrStepf weights1, PtrStepf weights2, PtrStepb result, cudaStream_t stream) 107 { 108 dim3 threads(16, 16); 109 dim3 grid(divUp(cols, threads.x), divUp(rows, threads.y)); 110 111 blendLinearKernel8UC4<<<grid, threads, 0, stream>>>(rows, cols, img1, img2, weights1, weights2, result); 112 cudaSafeCall( cudaGetLastError() ); 113 114 if (stream == 0) 115 cudaSafeCall(cudaDeviceSynchronize()); 116 } 117 } // namespace blend 118}}} // namespace cv { namespace cuda { namespace cudev 119 120 121#endif /* CUDA_DISABLER */ 122