1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Peng Xiao, pengxiao@multicorewareinc.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
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 oclMaterials provided with the distribution.
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.
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.
46 #include "precomp.hpp"
49 using namespace cv::ocl;
55 ///////////////////////////OpenCL kernel strings///////////////////////////
56 extern const char *build_warps;
60 //////////////////////////////////////////////////////////////////////////////
63 void cv::ocl::buildWarpPlaneMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T,
64 float scale, oclMat &map_x, oclMat &map_y)
66 CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
67 CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
68 CV_Assert((T.size() == Size(3, 1) || T.size() == Size(1, 3)) && T.type() == CV_32F && T.isContinuous());
70 Mat K_Rinv = K * R.t();
71 CV_Assert(K_Rinv.isContinuous());
73 Mat KRT_mat(1, 12, CV_32FC1); // 9 + 3
74 KRT_mat(Range::all(), Range(0, 8)) = K_Rinv.reshape(1, 1);
75 KRT_mat(Range::all(), Range(9, 11)) = T;
77 oclMat KRT_oclMat(KRT_mat);
78 // transfer K_Rinv and T into a single cl_mem
79 map_x.create(dst_roi.size(), CV_32F);
80 map_y.create(dst_roi.size(), CV_32F);
82 int tl_u = dst_roi.tl().x;
83 int tl_v = dst_roi.tl().y;
85 Context *clCxt = Context::getContext();
86 String kernelName = "buildWarpPlaneMaps";
87 std::vector< std::pair<size_t, const void *> > args;
89 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_x.data));
90 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_y.data));
91 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&KRT_mat.data));
92 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_u));
93 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_v));
94 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.cols));
95 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.rows));
96 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.step));
97 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_y.step));
98 args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
100 size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
101 size_t localThreads[3] = {32, 8, 1};
102 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
105 //////////////////////////////////////////////////////////////////////////////
106 // buildWarpCylyndricalMaps
108 void cv::ocl::buildWarpCylindricalMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, float scale,
109 oclMat &map_x, oclMat &map_y)
111 CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
112 CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
114 Mat K_Rinv = K * R.t();
115 CV_Assert(K_Rinv.isContinuous());
117 oclMat KR_oclMat(K_Rinv.reshape(1, 1));
119 map_x.create(dst_roi.size(), CV_32F);
120 map_y.create(dst_roi.size(), CV_32F);
122 int tl_u = dst_roi.tl().x;
123 int tl_v = dst_roi.tl().y;
125 Context *clCxt = Context::getContext();
126 String kernelName = "buildWarpCylindricalMaps";
127 std::vector< std::pair<size_t, const void *> > args;
129 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_x.data));
130 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_y.data));
131 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&KR_oclMat.data));
132 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_u));
133 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_v));
134 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.cols));
135 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.rows));
136 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.step));
137 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_y.step));
138 args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
140 size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
141 size_t localThreads[3] = {32, 8, 1};
142 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
145 //////////////////////////////////////////////////////////////////////////////
146 // buildWarpSphericalMaps
147 void cv::ocl::buildWarpSphericalMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, float scale,
148 oclMat &map_x, oclMat &map_y)
150 CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
151 CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
153 Mat K_Rinv = K * R.t();
154 CV_Assert(K_Rinv.isContinuous());
156 oclMat KR_oclMat(K_Rinv.reshape(1, 1));
157 // transfer K_Rinv, R_Kinv into a single cl_mem
158 map_x.create(dst_roi.size(), CV_32F);
159 map_y.create(dst_roi.size(), CV_32F);
161 int tl_u = dst_roi.tl().x;
162 int tl_v = dst_roi.tl().y;
164 Context *clCxt = Context::getContext();
165 String kernelName = "buildWarpSphericalMaps";
166 std::vector< std::pair<size_t, const void *> > args;
168 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_x.data));
169 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_y.data));
170 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&KR_oclMat.data));
171 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_u));
172 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_v));
173 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.cols));
174 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.rows));
175 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.step));
176 args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_y.step));
177 args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
179 size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
180 size_t localThreads[3] = {32, 8, 1};
181 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
185 void cv::ocl::buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
188 CV_Assert(M.rows == 2 && M.cols == 3);
190 xmap.create(dsize, CV_32FC1);
191 ymap.create(dsize, CV_32FC1);
194 Mat coeffsMat(2, 3, CV_32F, (void *)coeffs);
197 M.convertTo(coeffsMat, coeffsMat.type());
201 invertAffineTransform(M, iM);
202 iM.convertTo(coeffsMat, coeffsMat.type());
205 oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
207 Context *clCxt = Context::getContext();
208 String kernelName = "buildWarpAffineMaps";
209 std::vector< std::pair<size_t, const void *> > args;
211 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&xmap.data));
212 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&ymap.data));
213 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&coeffsOclMat.data));
214 args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.cols));
215 args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.rows));
216 args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.step));
217 args.push_back( std::make_pair( sizeof(cl_int), (void *)&ymap.step));
219 size_t globalThreads[3] = {xmap.cols, xmap.rows, 1};
220 size_t localThreads[3] = {32, 8, 1};
221 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
224 void cv::ocl::buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
227 CV_Assert(M.rows == 3 && M.cols == 3);
229 xmap.create(dsize, CV_32FC1);
230 ymap.create(dsize, CV_32FC1);
233 Mat coeffsMat(3, 3, CV_32F, (void *)coeffs);
236 M.convertTo(coeffsMat, coeffsMat.type());
241 iM.convertTo(coeffsMat, coeffsMat.type());
244 oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
246 Context *clCxt = Context::getContext();
247 String kernelName = "buildWarpPerspectiveMaps";
248 std::vector< std::pair<size_t, const void *> > args;
250 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&xmap.data));
251 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&ymap.data));
252 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&coeffsOclMat.data));
253 args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.cols));
254 args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.rows));
255 args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.step));
256 args.push_back( std::make_pair( sizeof(cl_int), (void *)&ymap.step));
258 size_t globalThreads[3] = {xmap.cols, xmap.rows, 1};
259 size_t localThreads[3] = {32, 8, 1};
260 openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);