• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Nathan, liujun@multicorewareinc.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other materials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors "as is" and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45 
46 #include "precomp.hpp"
47 #include "opencl_kernels_imgproc.hpp"
48 
49 namespace cv {
50 
51 template <typename T>
52 class BlendLinearInvoker :
53         public ParallelLoopBody
54 {
55 public:
BlendLinearInvoker(const Mat & _src1,const Mat & _src2,const Mat & _weights1,const Mat & _weights2,Mat & _dst)56     BlendLinearInvoker(const Mat & _src1, const Mat & _src2, const Mat & _weights1,
57                        const Mat & _weights2, Mat & _dst) :
58         src1(&_src1), src2(&_src2), weights1(&_weights1), weights2(&_weights2), dst(&_dst)
59     {
60     }
61 
operator ()(const Range & range) const62     virtual void operator() (const Range & range) const
63     {
64         int cn = src1->channels(), width = src1->cols * cn;
65 
66         for (int y = range.start; y < range.end; ++y)
67         {
68             const float * const weights1_row = weights1->ptr<float>(y);
69             const float * const weights2_row = weights2->ptr<float>(y);
70             const T * const src1_row = src1->ptr<T>(y);
71             const T * const src2_row = src2->ptr<T>(y);
72             T * const dst_row = dst->ptr<T>(y);
73 
74             for (int x = 0; x < width; ++x)
75             {
76                 int x1 = x / cn;
77                 float w1 = weights1_row[x1], w2 = weights2_row[x1];
78                 float den = (w1 + w2 + 1e-5f);
79                 float num = (src1_row[x] * w1 + src2_row[x] * w2);
80 
81                 dst_row[x] = saturate_cast<T>(num / den);
82             }
83         }
84     }
85 
86 private:
87     const BlendLinearInvoker & operator= (const BlendLinearInvoker &);
88     BlendLinearInvoker(const BlendLinearInvoker &);
89 
90     const Mat * src1, * src2, * weights1, * weights2;
91     Mat * dst;
92 };
93 
94 #ifdef HAVE_OPENCL
95 
ocl_blendLinear(InputArray _src1,InputArray _src2,InputArray _weights1,InputArray _weights2,OutputArray _dst)96 static bool ocl_blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst )
97 {
98     int type = _src1.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
99 
100     char cvt[30];
101     ocl::Kernel k("blendLinear", ocl::imgproc::blend_linear_oclsrc,
102                   format("-D T=%s -D cn=%d -D convertToT=%s", ocl::typeToStr(depth),
103                          cn, ocl::convertTypeStr(CV_32F, depth, 1, cvt)));
104     if (k.empty())
105         return false;
106 
107     UMat src1 = _src1.getUMat(), src2 = _src2.getUMat(), weights1 = _weights1.getUMat(),
108             weights2 = _weights2.getUMat(), dst = _dst.getUMat();
109 
110     k.args(ocl::KernelArg::ReadOnlyNoSize(src1), ocl::KernelArg::ReadOnlyNoSize(src2),
111            ocl::KernelArg::ReadOnlyNoSize(weights1), ocl::KernelArg::ReadOnlyNoSize(weights2),
112            ocl::KernelArg::WriteOnly(dst));
113 
114     size_t globalsize[2] = { dst.cols, dst.rows };
115     return k.run(2, globalsize, NULL, false);
116 }
117 
118 #endif
119 
120 }
121 
blendLinear(InputArray _src1,InputArray _src2,InputArray _weights1,InputArray _weights2,OutputArray _dst)122 void cv::blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst )
123 {
124     int type = _src1.type(), depth = CV_MAT_DEPTH(type);
125     Size size = _src1.size();
126 
127     CV_Assert(depth == CV_8U || depth == CV_32F);
128     CV_Assert(size == _src2.size() && size == _weights1.size() && size == _weights2.size());
129     CV_Assert(type == _src2.type() && _weights1.type() == CV_32FC1 && _weights2.type() == CV_32FC1);
130 
131     _dst.create(size, type);
132 
133     CV_OCL_RUN(_dst.isUMat(),
134                ocl_blendLinear(_src1, _src2, _weights1, _weights2, _dst))
135 
136     Mat src1 = _src1.getMat(), src2 = _src2.getMat(), weights1 = _weights1.getMat(),
137             weights2 = _weights2.getMat(), dst = _dst.getMat();
138 
139     if (depth == CV_8U)
140     {
141         BlendLinearInvoker<uchar> invoker(src1, src2, weights1, weights2, dst);
142         parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16));
143     }
144     else if (depth == CV_32F)
145     {
146         BlendLinearInvoker<float> invoker(src1, src2, weights1, weights2, dst);
147         parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16));
148     }
149 }
150