Merge remote-tracking branch 'origin/2.4'
[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 oclMaterials 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
48 using namespace cv;
49 using namespace cv::ocl;
50
51 namespace cv
52 {
53     namespace ocl
54     {
55         ///////////////////////////OpenCL kernel strings///////////////////////////
56         extern const char *build_warps;
57     }
58 }
59
60 //////////////////////////////////////////////////////////////////////////////
61 // buildWarpPlaneMaps
62
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)
65 {
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());
69
70     Mat K_Rinv = K * R.t();
71     CV_Assert(K_Rinv.isContinuous());
72
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;
76
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);
81
82     int tl_u = dst_roi.tl().x;
83     int tl_v = dst_roi.tl().y;
84
85     Context *clCxt = Context::getContext();
86     String kernelName = "buildWarpPlaneMaps";
87     std::vector< std::pair<size_t, const void *> > args;
88
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));
99
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);
103 }
104
105 //////////////////////////////////////////////////////////////////////////////
106 // buildWarpCylyndricalMaps
107
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)
110 {
111     CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
112     CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
113
114     Mat K_Rinv = K * R.t();
115     CV_Assert(K_Rinv.isContinuous());
116
117     oclMat KR_oclMat(K_Rinv.reshape(1, 1));
118
119     map_x.create(dst_roi.size(), CV_32F);
120     map_y.create(dst_roi.size(), CV_32F);
121
122     int tl_u = dst_roi.tl().x;
123     int tl_v = dst_roi.tl().y;
124
125     Context *clCxt = Context::getContext();
126     String kernelName = "buildWarpCylindricalMaps";
127     std::vector< std::pair<size_t, const void *> > args;
128
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));
139
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);
143 }
144
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)
149 {
150     CV_Assert(K.size() == Size(3, 3) && K.type() == CV_32F);
151     CV_Assert(R.size() == Size(3, 3) && R.type() == CV_32F);
152
153     Mat K_Rinv = K * R.t();
154     CV_Assert(K_Rinv.isContinuous());
155
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);
160
161     int tl_u = dst_roi.tl().x;
162     int tl_v = dst_roi.tl().y;
163
164     Context *clCxt = Context::getContext();
165     String kernelName = "buildWarpSphericalMaps";
166     std::vector< std::pair<size_t, const void *> > args;
167
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));
178
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);
182 }
183
184
185 void cv::ocl::buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
186 {
187
188     CV_Assert(M.rows == 2 && M.cols == 3);
189
190     xmap.create(dsize, CV_32FC1);
191     ymap.create(dsize, CV_32FC1);
192
193     float coeffs[2 * 3];
194     Mat coeffsMat(2, 3, CV_32F, (void *)coeffs);
195
196     if (inverse)
197         M.convertTo(coeffsMat, coeffsMat.type());
198     else
199     {
200         cv::Mat iM;
201         invertAffineTransform(M, iM);
202         iM.convertTo(coeffsMat, coeffsMat.type());
203     }
204
205     oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
206
207     Context *clCxt = Context::getContext();
208     String kernelName = "buildWarpAffineMaps";
209     std::vector< std::pair<size_t, const void *> > args;
210
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));
218
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);
222 }
223
224 void cv::ocl::buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap)
225 {
226
227     CV_Assert(M.rows == 3 && M.cols == 3);
228
229     xmap.create(dsize, CV_32FC1);
230     ymap.create(dsize, CV_32FC1);
231
232     float coeffs[3 * 3];
233     Mat coeffsMat(3, 3, CV_32F, (void *)coeffs);
234
235     if (inverse)
236         M.convertTo(coeffsMat, coeffsMat.type());
237     else
238     {
239         cv::Mat iM;
240         invert(M, iM);
241         iM.convertTo(coeffsMat, coeffsMat.type());
242     }
243
244     oclMat coeffsOclMat(coeffsMat.reshape(1, 1));
245
246     Context *clCxt = Context::getContext();
247     String kernelName = "buildWarpPerspectiveMaps";
248     std::vector< std::pair<size_t, const void *> > args;
249
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));
257
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);
261 }