/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #include "opencv2/opencv_modules.hpp" #ifndef HAVE_OPENCV_CUDEV #error "opencv_cudev is required" #else #include "opencv2/cudaarithm.hpp" #include "opencv2/cudev.hpp" using namespace cv::cudev; namespace { template struct AddWeightedOp : binary_function { S alpha; S beta; S gamma; __device__ __forceinline__ D operator ()(T1 a, T2 b) const { return saturate_cast(a * alpha + b * beta + gamma); } }; template struct TransformPolicy : DefaultTransformPolicy { }; template <> struct TransformPolicy : DefaultTransformPolicy { enum { shift = 1 }; }; template void addWeightedImpl(const GpuMat& src1, double alpha, const GpuMat& src2, double beta, double gamma, GpuMat& dst, Stream& stream) { typedef typename LargerType::type larger_type1; typedef typename LargerType::type larger_type2; typedef typename LargerType::type scalar_type; AddWeightedOp op; op.alpha = static_cast(alpha); op.beta = static_cast(beta); op.gamma = static_cast(gamma); gridTransformBinary_< TransformPolicy >(globPtr(src1), globPtr(src2), globPtr(dst), op, stream); } } void cv::cuda::addWeighted(InputArray _src1, double alpha, InputArray _src2, double beta, double gamma, OutputArray _dst, int ddepth, Stream& stream) { typedef void (*func_t)(const GpuMat& src1, double alpha, const GpuMat& src2, double beta, double gamma, GpuMat& dst, Stream& stream); static const func_t funcs[7][7][7] = { { { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } }, { { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } }, { { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } }, { { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } }, { { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } }, { { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } }, { { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/, 0/*addWeightedImpl*/ }, { addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl, addWeightedImpl } } }; GpuMat src1 = _src1.getGpuMat(); GpuMat src2 = _src2.getGpuMat(); int sdepth1 = src1.depth(); int sdepth2 = src2.depth(); ddepth = ddepth >= 0 ? CV_MAT_DEPTH(ddepth) : std::max(sdepth1, sdepth2); const int cn = src1.channels(); CV_DbgAssert( src2.size() == src1.size() && src2.channels() == cn ); CV_DbgAssert( sdepth1 <= CV_64F && sdepth2 <= CV_64F && ddepth <= CV_64F ); _dst.create(src1.size(), CV_MAKE_TYPE(ddepth, cn)); GpuMat dst = _dst.getGpuMat(); GpuMat src1_ = src1.reshape(1); GpuMat src2_ = src2.reshape(1); GpuMat dst_ = dst.reshape(1); if (sdepth1 > sdepth2) { src1_.swap(src2_); std::swap(alpha, beta); std::swap(sdepth1, sdepth2); } const func_t func = funcs[sdepth1][sdepth2][ddepth]; if (!func) CV_Error(cv::Error::StsUnsupportedFormat, "Unsupported combination of source and destination types"); func(src1_, alpha, src2_, beta, gamma, dst_, stream); } #endif