Merge pull request #1629 from lluisgomez:er_tree_clean_bug_fix
[profile/ivi/opencv.git] / modules / ocl / src / build_warps.cpp
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 //    Peng Xiao, pengxiao@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.hpp"
48
49 using namespace cv;
50 using namespace cv::ocl;
51
52 //////////////////////////////////////////////////////////////////////////////
53 // buildWarpPlaneMaps
54
55 void cv::ocl::buildWarpPlaneMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T,
56                                  float scale, oclMat &map_x, oclMat &map_y)
57 {
58     CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
59     CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
60     CV_Assert((T.size() == Size(3, 1) || T.size() == Size(1, 3)) && T.type() == CV_32F && T.isContinuous());
61
62     Mat K_Rinv = K * R.t();
63     CV_Assert(K_Rinv.isContinuous());
64
65     Mat KRT_mat(1, 12, CV_32FC1); // 9 + 3
66     KRT_mat(Range::all(), Range(0, 8)) = K_Rinv.reshape(1, 1);
67     KRT_mat(Range::all(), Range(9, 11)) = T;
68
69     oclMat KRT_oclMat(KRT_mat);
70     // transfer K_Rinv and T into a single cl_mem
71     map_x.create(dst_roi.size(), CV_32F);
72     map_y.create(dst_roi.size(), CV_32F);
73
74     int tl_u = dst_roi.tl().x;
75     int tl_v = dst_roi.tl().y;
76
77     Context *clCxt = Context::getContext();
78     String kernelName = "buildWarpPlaneMaps";
79     std::vector< std::pair<size_t, const void *> > args;
80
81     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_x.data));
82     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_y.data));
83     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&KRT_mat.data));
84     args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_u));
85     args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_v));
86     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.cols));
87     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.rows));
88     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.step));
89     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_y.step));
90     args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
91
92     size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
93     size_t localThreads[3]  = {32, 8, 1};
94     openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
95 }
96
97 //////////////////////////////////////////////////////////////////////////////
98 // buildWarpCylyndricalMaps
99
100 void cv::ocl::buildWarpCylindricalMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, float scale,
101                                        oclMat &map_x, oclMat &map_y)
102 {
103     CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
104     CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
105
106     Mat K_Rinv = K * R.t();
107     CV_Assert(K_Rinv.isContinuous());
108
109     oclMat KR_oclMat(K_Rinv.reshape(1, 1));
110
111     map_x.create(dst_roi.size(), CV_32F);
112     map_y.create(dst_roi.size(), CV_32F);
113
114     int tl_u = dst_roi.tl().x;
115     int tl_v = dst_roi.tl().y;
116
117     Context *clCxt = Context::getContext();
118     String kernelName = "buildWarpCylindricalMaps";
119     std::vector< std::pair<size_t, const void *> > args;
120
121     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_x.data));
122     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_y.data));
123     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&KR_oclMat.data));
124     args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_u));
125     args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_v));
126     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.cols));
127     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.rows));
128     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.step));
129     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_y.step));
130     args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
131
132     size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
133     size_t localThreads[3]  = {32, 8, 1};
134     openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
135 }
136
137 //////////////////////////////////////////////////////////////////////////////
138 // buildWarpSphericalMaps
139 void cv::ocl::buildWarpSphericalMaps(Size /*src_size*/, Rect dst_roi, const Mat &K, const Mat &R, float scale,
140                                      oclMat &map_x, oclMat &map_y)
141 {
142     CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
143     CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
144
145     Mat K_Rinv = K * R.t();
146     CV_Assert(K_Rinv.isContinuous());
147
148     oclMat KR_oclMat(K_Rinv.reshape(1, 1));
149     // transfer K_Rinv, R_Kinv into a single cl_mem
150     map_x.create(dst_roi.size(), CV_32F);
151     map_y.create(dst_roi.size(), CV_32F);
152
153     int tl_u = dst_roi.tl().x;
154     int tl_v = dst_roi.tl().y;
155
156     Context *clCxt = Context::getContext();
157     String kernelName = "buildWarpSphericalMaps";
158     std::vector< std::pair<size_t, const void *> > args;
159
160     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_x.data));
161     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&map_y.data));
162     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&KR_oclMat.data));
163     args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_u));
164     args.push_back( std::make_pair( sizeof(cl_int), (void *)&tl_v));
165     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.cols));
166     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.rows));
167     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_x.step));
168     args.push_back( std::make_pair( sizeof(cl_int), (void *)&map_y.step));
169     args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
170
171     size_t globalThreads[3] = {map_x.cols, map_x.rows, 1};
172     size_t localThreads[3]  = {32, 8, 1};
173     openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
174 }
175
176
177 void cv::ocl::buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
178 {
179
180     CV_Assert(M.rows == 2 && M.cols == 3);
181
182     xmap.create(dsize, CV_32FC1);
183     ymap.create(dsize, CV_32FC1);
184
185     float coeffs[2 * 3];
186     Mat coeffsMat(2, 3, CV_32F, (void *)coeffs);
187
188     if (inverse)
189         M.convertTo(coeffsMat, coeffsMat.type());
190     else
191     {
192         cv::Mat iM;
193         invertAffineTransform(M, iM);
194         iM.convertTo(coeffsMat, coeffsMat.type());
195     }
196
197     oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
198
199     Context *clCxt = Context::getContext();
200     String kernelName = "buildWarpAffineMaps";
201     std::vector< std::pair<size_t, const void *> > args;
202
203     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&xmap.data));
204     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&ymap.data));
205     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&coeffsOclMat.data));
206     args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.cols));
207     args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.rows));
208     args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.step));
209     args.push_back( std::make_pair( sizeof(cl_int), (void *)&ymap.step));
210
211     size_t globalThreads[3] = {xmap.cols, xmap.rows, 1};
212     size_t localThreads[3]  = {32, 8, 1};
213     openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
214 }
215
216 void cv::ocl::buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
217 {
218
219     CV_Assert(M.rows == 3 && M.cols == 3);
220
221     xmap.create(dsize, CV_32FC1);
222     ymap.create(dsize, CV_32FC1);
223
224     float coeffs[3 * 3];
225     Mat coeffsMat(3, 3, CV_32F, (void *)coeffs);
226
227     if (inverse)
228         M.convertTo(coeffsMat, coeffsMat.type());
229     else
230     {
231         cv::Mat iM;
232         invert(M, iM);
233         iM.convertTo(coeffsMat, coeffsMat.type());
234     }
235
236     oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
237
238     Context *clCxt = Context::getContext();
239     String kernelName = "buildWarpPerspectiveMaps";
240     std::vector< std::pair<size_t, const void *> > args;
241
242     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&xmap.data));
243     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&ymap.data));
244     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&coeffsOclMat.data));
245     args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.cols));
246     args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.rows));
247     args.push_back( std::make_pair( sizeof(cl_int), (void *)&xmap.step));
248     args.push_back( std::make_pair( sizeof(cl_int), (void *)&ymap.step));
249
250     size_t globalThreads[3] = {xmap.cols, xmap.rows, 1};
251     size_t localThreads[3]  = {32, 8, 1};
252     openCLExecuteKernel(clCxt, &build_warps, kernelName, globalThreads, localThreads, args, -1, -1);
253 }