Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / ocl / src / imgproc.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, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // @Authors
19 //    Niko Li, newlife20080214@gmail.com
20 //    Jia Haipeng, jiahaipeng95@gmail.com
21 //    Shengen Yan, yanshengen@gmail.com
22 //    Rock Li, Rock.Li@amd.com
23 //    Zero Lin, Zero.Lin@amd.com
24 //    Zhang Ying, zhangying913@gmail.com
25 //    Xu Pang, pangxu010@163.com
26 //    Wu Zailong, bullet@yeah.net
27 //    Wenju He, wenju@multicorewareinc.com
28 //    Peng Xiao, pengxiao@outlook.com
29 //    Sen Liu, swjtuls1987@126.com
30 //
31 // Redistribution and use in source and binary forms, with or without modification,
32 // are permitted provided that the following conditions are met:
33 //
34 //   * Redistribution's of source code must retain the above copyright notice,
35 //     this list of conditions and the following disclaimer.
36 //
37 //   * Redistribution's in binary form must reproduce the above copyright notice,
38 //     this list of conditions and the following disclaimer in the documentation
39 //     and/or other materials provided with the distribution.
40 //
41 //   * The name of the copyright holders may not be used to endorse or promote products
42 //     derived from this software without specific prior written permission.
43 //
44 // This software is provided by the copyright holders and contributors "as is" and
45 // any express or implied warranties, including, but not limited to, the implied
46 // warranties of merchantability and fitness for a particular purpose are disclaimed.
47 // In no event shall the Intel Corporation or contributors be liable for any direct,
48 // indirect, incidental, special, exemplary, or consequential damages
49 // (including, but not limited to, procurement of substitute goods or services;
50 // loss of use, data, or profits; or business interruption) however caused
51 // and on any theory of liability, whether in contract, strict liability,
52 // or tort (including negligence or otherwise) arising in any way out of
53 // the use of this software, even if advised of the possibility of such damage.
54 //
55 //M*/
56
57 #include "precomp.hpp"
58 #include "opencl_kernels.hpp"
59
60 using namespace cv;
61 using namespace cv::ocl;
62
63 namespace cv
64 {
65     namespace ocl
66     {
67         ////////////////////////////////////OpenCL call wrappers////////////////////////////
68
69         template <typename T> struct index_and_sizeof;
70         template <> struct index_and_sizeof<char>
71         {
72             enum { index = 1 };
73         };
74         template <> struct index_and_sizeof<unsigned char>
75         {
76             enum { index = 2 };
77         };
78         template <> struct index_and_sizeof<short>
79         {
80             enum { index = 3 };
81         };
82         template <> struct index_and_sizeof<unsigned short>
83         {
84             enum { index = 4 };
85         };
86         template <> struct index_and_sizeof<int>
87         {
88             enum { index = 5 };
89         };
90         template <> struct index_and_sizeof<float>
91         {
92             enum { index = 6 };
93         };
94         template <> struct index_and_sizeof<double>
95         {
96             enum { index = 7 };
97         };
98
99         /////////////////////////////////////////////////////////////////////////////////////
100         // threshold
101
102         static std::vector<uchar> scalarToVector(const cv::Scalar & sc, int depth, int ocn, int cn)
103         {
104             CV_Assert(ocn == cn || (ocn == 4 && cn == 3));
105
106             static const int sizeMap[] = { sizeof(uchar), sizeof(char), sizeof(ushort),
107                                        sizeof(short), sizeof(int), sizeof(float), sizeof(double) };
108
109             int elemSize1 = sizeMap[depth];
110             int bufSize = elemSize1 * ocn;
111             std::vector<uchar> _buf(bufSize);
112             uchar * buf = &_buf[0];
113             scalarToRawData(sc, buf, CV_MAKE_TYPE(depth, cn));
114             memset(buf + elemSize1 * cn, 0, (ocn - cn) * elemSize1);
115
116             return _buf;
117         }
118
119         static void threshold_runner(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
120         {
121             bool ival = src.depth() < CV_32F;
122             int cn = src.channels(), vecSize = 4, depth = src.depth();
123             std::vector<uchar> thresholdValue = scalarToVector(cv::Scalar::all(ival ? cvFloor(thresh) : thresh), dst.depth(),
124                                                                dst.oclchannels(), dst.channels());
125             std::vector<uchar> maxValue = scalarToVector(cv::Scalar::all(maxVal), dst.depth(), dst.oclchannels(), dst.channels());
126
127             const char * const thresholdMap[] = { "THRESH_BINARY", "THRESH_BINARY_INV", "THRESH_TRUNC",
128                                                   "THRESH_TOZERO", "THRESH_TOZERO_INV" };
129             const char * const channelMap[] = { "", "", "2", "4", "4" };
130             const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
131             std::string buildOptions = format("-D T=%s%s -D %s", typeMap[depth], channelMap[cn], thresholdMap[thresholdType]);
132
133             int elemSize = src.elemSize();
134             int src_step = src.step / elemSize, src_offset = src.offset / elemSize;
135             int dst_step = dst.step / elemSize, dst_offset = dst.offset / elemSize;
136
137             std::vector< std::pair<size_t, const void *> > args;
138             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
139             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
140             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
141             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
142             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
143             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
144             args.push_back( std::make_pair(thresholdValue.size(), (void *)&thresholdValue[0]));
145             args.push_back( std::make_pair(maxValue.size(), (void *)&maxValue[0]));
146
147             int max_index = dst.cols, cols = dst.cols;
148             if (cn == 1 && vecSize > 1)
149             {
150                 CV_Assert(((vecSize - 1) & vecSize) == 0 && vecSize <= 16);
151                 cols = divUp(cols, vecSize);
152                 buildOptions += format(" -D VECTORIZED -D VT=%s%d -D VLOADN=vload%d -D VECSIZE=%d -D VSTOREN=vstore%d",
153                                        typeMap[depth], vecSize, vecSize, vecSize, vecSize);
154
155                 int vecSizeBytes = vecSize * dst.elemSize1();
156                 if ((dst.offset % dst.step) % vecSizeBytes == 0 && dst.step % vecSizeBytes == 0)
157                     buildOptions += " -D DST_ALIGNED";
158                 if ((src.offset % src.step) % vecSizeBytes == 0 && src.step % vecSizeBytes == 0)
159                     buildOptions += " -D SRC_ALIGNED";
160
161                 args.push_back( std::make_pair(sizeof(cl_int), (void *)&max_index));
162             }
163
164             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
165             args.push_back( std::make_pair(sizeof(cl_int), (void *)&cols));
166
167             size_t localThreads[3] = { 16, 16, 1 };
168             size_t globalThreads[3] = { cols, dst.rows, 1 };
169
170             openCLExecuteKernel(src.clCxt, &imgproc_threshold, "threshold", globalThreads, localThreads, args,
171                                 -1, -1, buildOptions.c_str());
172         }
173
174         double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int thresholdType)
175         {
176             CV_Assert(thresholdType == THRESH_BINARY || thresholdType == THRESH_BINARY_INV || thresholdType == THRESH_TRUNC
177                       || thresholdType == THRESH_TOZERO || thresholdType == THRESH_TOZERO_INV);
178
179             dst.create(src.size(), src.type());
180             threshold_runner(src, dst, thresh, maxVal, thresholdType);
181
182             return thresh;
183         }
184
185         ////////////////////////////////////////////////////////////////////////////////////////////
186         ///////////////////////////////   remap   //////////////////////////////////////////////////
187         ////////////////////////////////////////////////////////////////////////////////////////////
188
189         void remap( const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int borderType, const Scalar &borderValue )
190         {
191             Context *clCxt = src.clCxt;
192             bool supportsDouble = clCxt->supportsFeature(FEATURE_CL_DOUBLE);
193             if (!supportsDouble && src.depth() == CV_64F)
194             {
195                 CV_Error(CV_OpenCLDoubleNotSupported, "Selected device does not support double");
196                 return;
197             }
198
199             if (map1.empty())
200                 map1.swap(map2);
201
202             CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
203             CV_Assert((map1.type() == CV_16SC2 && (map2.empty() || (map2.type() == CV_16UC1 || map2.type() == CV_16SC1)) ) ||
204                       (map1.type() == CV_32FC2 && !map2.data) ||
205                       (map1.type() == CV_32FC1 && map2.type() == CV_32FC1));
206             CV_Assert(!map2.data || map2.size() == map1.size());
207             CV_Assert(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_WRAP
208                       || borderType == BORDER_REFLECT_101 || borderType == BORDER_REFLECT);
209
210             dst.create(map1.size(), src.type());
211
212             const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
213             const char * const channelMap[] = { "", "", "2", "4", "4" };
214             const char * const interMap[] = { "INTER_NEAREST", "INTER_LINEAR", "INTER_CUBIC", "INTER_LINEAR", "INTER_LANCZOS" };
215             const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP",
216                                    "BORDER_REFLECT_101", "BORDER_TRANSPARENT" };
217
218             String kernelName = "remap";
219             if (map1.type() == CV_32FC2 && map2.empty())
220                 kernelName = kernelName + "_32FC2";
221             else if (map1.type() == CV_16SC2)
222             {
223                 kernelName = kernelName + "_16SC2";
224                 if (!map2.empty())
225                     kernelName = kernelName + "_16UC1";
226             }
227             else if (map1.type() == CV_32FC1 && map2.type() == CV_32FC1)
228                 kernelName = kernelName + "_2_32FC1";
229             else
230                 CV_Error(Error::StsBadArg, "Unsupported map types");
231
232             int ocn = dst.oclchannels();
233             size_t localThreads[3] = { 256, 1, 1 };
234             size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
235
236             Mat scalar(1, 1, CV_MAKE_TYPE(dst.depth(), ocn), borderValue);
237             String buildOptions = format("-D %s -D %s -D T=%s%s", interMap[interpolation],
238                                          borderMap[borderType], typeMap[src.depth()], channelMap[ocn]);
239
240             if (interpolation != INTER_NEAREST)
241             {
242                 int wdepth = std::max(CV_32F, dst.depth());
243                 buildOptions = buildOptions
244                               + format(" -D WT=%s%s -D convertToT=convert_%s%s%s -D convertToWT=convert_%s%s"
245                                        " -D convertToWT2=convert_%s2 -D WT2=%s2",
246                                        typeMap[wdepth], channelMap[ocn],
247                                        typeMap[src.depth()], channelMap[ocn], src.depth() < CV_32F ? "_sat_rte" : "",
248                                        typeMap[wdepth], channelMap[ocn],
249                                        typeMap[wdepth], typeMap[wdepth]);
250             }
251
252             int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
253             int map1_step = map1.step / map1.elemSize(), map1_offset = map1.offset / map1.elemSize();
254             int map2_step = map2.step / map2.elemSize(), map2_offset = map2.offset / map2.elemSize();
255             int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
256
257             std::vector< std::pair<size_t, const void *> > args;
258             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
259             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
260             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map1.data));
261             if (!map2.empty())
262                 args.push_back( std::make_pair(sizeof(cl_mem), (void *)&map2.data));
263             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
264             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
265             args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_offset));
266             if (!map2.empty())
267                 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_offset));
268             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
269             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
270             args.push_back( std::make_pair(sizeof(cl_int), (void *)&map1_step));
271             if (!map2.empty())
272                 args.push_back( std::make_pair(sizeof(cl_int), (void *)&map2_step));
273             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
274             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
275             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
276             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
277             args.push_back( std::make_pair(scalar.elemSize(), (void *)scalar.data));
278
279             openCLExecuteKernel(clCxt, &imgproc_remap, kernelName, globalThreads, localThreads, args, -1, -1, buildOptions.c_str());
280         }
281
282         ////////////////////////////////////////////////////////////////////////////////////////////
283         // resize
284
285         static void resize_gpu( const oclMat &src, oclMat &dst, double fx, double fy, int interpolation)
286         {
287             float ifx = 1.f / fx, ify = 1.f / fy;
288             int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
289             int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
290             int ocn = interpolation == INTER_LINEAR ? dst.oclchannels() : -1;
291             int depth = interpolation == INTER_LINEAR ? dst.depth() : -1;
292
293             const char * const interMap[] = { "NN", "LN", "CUBIC", "AREA", "LAN4" };
294             std::string kernelName = std::string("resize") + interMap[interpolation];
295
296             const char * const typeMap[] = { "uchar", "uchar", "ushort", "ushort", "int", "int", "double" };
297             const char * const channelMap[] = { "" , "", "2", "4", "4" };
298             std::string buildOption = format("-D %s -D T=%s%s", interMap[interpolation], typeMap[dst.depth()], channelMap[dst.oclchannels()]);
299
300             //TODO: improve this kernel
301             size_t blkSizeX = 16, blkSizeY = 16;
302             size_t glbSizeX;
303             if (src.type() == CV_8UC1 && interpolation == INTER_LINEAR)
304             {
305                 size_t cols = (dst.cols + dst.offset % 4 + 3) / 4;
306                 glbSizeX = cols % blkSizeX == 0 && cols != 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
307             }
308             else
309                 glbSizeX = dst.cols;
310
311             size_t globalThreads[3] = { glbSizeX, dst.rows, 1 };
312             size_t localThreads[3] = { blkSizeX, blkSizeY, 1 };
313
314             std::vector< std::pair<size_t, const void *> > args;
315             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&dst.data));
316             args.push_back( std::make_pair(sizeof(cl_mem), (void *)&src.data));
317             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_offset));
318             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_offset));
319             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst_step));
320             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src_step));
321             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.cols));
322             args.push_back( std::make_pair(sizeof(cl_int), (void *)&src.rows));
323             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
324             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
325             args.push_back( std::make_pair(sizeof(cl_float), (void *)&ifx));
326             args.push_back( std::make_pair(sizeof(cl_float), (void *)&ify));
327
328             openCLExecuteKernel(src.clCxt, &imgproc_resize, kernelName, globalThreads, localThreads, args,
329                                 ocn, depth, buildOption.c_str());
330         }
331
332         void resize(const oclMat &src, oclMat &dst, Size dsize, double fx, double fy, int interpolation)
333         {
334             CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4
335                       || src.type() == CV_32FC1 || src.type() == CV_32FC3 || src.type() == CV_32FC4);
336             CV_Assert(interpolation == INTER_LINEAR || interpolation == INTER_NEAREST);
337             CV_Assert(dsize.area() > 0 || (fx > 0 && fy > 0));
338
339             if (dsize.area() == 0)
340             {
341                 dsize = Size(saturate_cast<int>(src.cols * fx), saturate_cast<int>(src.rows * fy));
342                 CV_Assert(dsize.area() > 0);
343             }
344             else
345             {
346                 fx = (double)dsize.width / src.cols;
347                 fy = (double)dsize.height / src.rows;
348             }
349
350             dst.create(dsize, src.type());
351
352             resize_gpu( src, dst, fx, fy, interpolation);
353         }
354
355         ////////////////////////////////////////////////////////////////////////
356         // medianFilter
357
358         void medianFilter(const oclMat &src, oclMat &dst, int m)
359         {
360             CV_Assert( m % 2 == 1 && m > 1 );
361             CV_Assert( (src.depth() == CV_8U || src.depth() == CV_32F) && (src.channels() == 1 || src.channels() == 4));
362             dst.create(src.size(), src.type());
363
364             int srcStep = src.step / src.elemSize(), dstStep = dst.step / dst.elemSize();
365             int srcOffset = src.offset /  src.elemSize(), dstOffset = dst.offset / dst.elemSize();
366
367             Context *clCxt = src.clCxt;
368
369             std::vector< std::pair<size_t, const void *> > args;
370             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data));
371             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
372             args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
373             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
374             args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols));
375             args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows));
376             args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
377             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
378
379             size_t globalThreads[3] = {(src.cols + 18) / 16 * 16, (src.rows + 15) / 16 * 16, 1};
380             size_t localThreads[3] = {16, 16, 1};
381
382             if (m == 3)
383             {
384                 String kernelName = "medianFilter3";
385                 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
386             }
387             else if (m == 5)
388             {
389                 String kernelName = "medianFilter5";
390                 openCLExecuteKernel(clCxt, &imgproc_median, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
391             }
392             else
393                 CV_Error(Error::StsBadArg, "Non-supported filter length");
394         }
395
396         ////////////////////////////////////////////////////////////////////////
397         // copyMakeBorder
398
399         void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int bordertype, const Scalar &scalar)
400         {
401             if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
402             {
403                 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device does not support double");
404                 return;
405             }
406
407             oclMat _src = src;
408
409             CV_Assert(top >= 0 && bottom >= 0 && left >= 0 && right >= 0);
410
411             if( (_src.wholecols != _src.cols || _src.wholerows != _src.rows) && (bordertype & BORDER_ISOLATED) == 0 )
412             {
413                 Size wholeSize;
414                 Point ofs;
415                 _src.locateROI(wholeSize, ofs);
416                 int dtop = std::min(ofs.y, top);
417                 int dbottom = std::min(wholeSize.height - _src.rows - ofs.y, bottom);
418                 int dleft = std::min(ofs.x, left);
419                 int dright = std::min(wholeSize.width - _src.cols - ofs.x, right);
420                 _src.adjustROI(dtop, dbottom, dleft, dright);
421                 top -= dtop;
422                 left -= dleft;
423                 bottom -= dbottom;
424                 right -= dright;
425             }
426             bordertype &= ~cv::BORDER_ISOLATED;
427
428             dst.create(_src.rows + top + bottom, _src.cols + left + right, _src.type());
429             int srcStep = _src.step / _src.elemSize(),  dstStep = dst.step / dst.elemSize();
430             int srcOffset = _src.offset / _src.elemSize(), dstOffset = dst.offset / dst.elemSize();
431             int depth = _src.depth(), ochannels = _src.oclchannels();
432
433             int __bordertype[] = { BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT, BORDER_WRAP, BORDER_REFLECT_101 };
434             const char *borderstr[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
435
436             int bordertype_index = -1;
437             for (int i = 0, end = sizeof(__bordertype) / sizeof(int); i < end; i++)
438                 if (__bordertype[i] == bordertype)
439                 {
440                     bordertype_index = i;
441                     break;
442                 }
443             if (bordertype_index < 0)
444                 CV_Error(Error::StsBadArg, "Unsupported border type");
445
446             size_t localThreads[3] = { 16, 16, 1 };
447             size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
448
449             std::vector< std::pair<size_t, const void *> > args;
450             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&_src.data));
451             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data));
452             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols));
453             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows));
454             args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.cols));
455             args.push_back( std::make_pair( sizeof(cl_int), (void *)&_src.rows));
456             args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcStep));
457             args.push_back( std::make_pair( sizeof(cl_int), (void *)&srcOffset));
458             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstStep));
459             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dstOffset));
460             args.push_back( std::make_pair( sizeof(cl_int), (void *)&top));
461             args.push_back( std::make_pair( sizeof(cl_int), (void *)&left));
462
463             const char * const typeMap[] = { "uchar", "char", "ushort", "short", "int", "float", "double" };
464             const char * const channelMap[] = { "", "", "2", "4", "4" };
465             std::string buildOptions = format("-D GENTYPE=%s%s -D %s",
466                                               typeMap[depth], channelMap[ochannels],
467                                               borderstr[bordertype_index]);
468
469             int cn = src.channels(), ocn = src.oclchannels();
470             int bufSize = src.elemSize1() * ocn;
471             AutoBuffer<uchar> _buf(bufSize);
472             uchar * buf = (uchar *)_buf;
473             scalarToRawData(scalar, buf, dst.type());
474             memset(buf + src.elemSize1() * cn, 0, (ocn - cn) * src.elemSize1());
475
476             args.push_back( std::make_pair( bufSize , (void *)buf ));
477
478             openCLExecuteKernel(src.clCxt, &imgproc_copymakeboder, "copymakeborder", globalThreads,
479                                 localThreads, args, -1, -1, buildOptions.c_str());
480         }
481
482         ////////////////////////////////////////////////////////////////////////
483         // warp
484
485         namespace
486         {
487 #define F double
488
489             void convert_coeffs(F *M)
490             {
491                 double D = M[0] * M[4] - M[1] * M[3];
492                 D = D != 0 ? 1. / D : 0;
493                 double A11 = M[4] * D, A22 = M[0] * D;
494                 M[0] = A11;
495                 M[1] *= -D;
496                 M[3] *= -D;
497                 M[4] = A22;
498                 double b1 = -M[0] * M[2] - M[1] * M[5];
499                 double b2 = -M[3] * M[2] - M[4] * M[5];
500                 M[2] = b1;
501                 M[5] = b2;
502             }
503
504             double invert(double *M)
505             {
506 #define Sd(y,x) (Sd[y*3+x])
507 #define Dd(y,x) (Dd[y*3+x])
508 #define det3(m)    (m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) -  \
509                     m(0,1)*(m(1,0)*m(2,2) - m(1,2)*m(2,0)) +  \
510                     m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0)))
511                 double *Sd = M;
512                 double *Dd = M;
513                 double d = det3(Sd);
514                 double result = 0;
515                 if ( d != 0)
516                 {
517                     double t[9];
518                     result = d;
519                     d = 1. / d;
520
521                     t[0] = (Sd(1, 1) * Sd(2, 2) - Sd(1, 2) * Sd(2, 1)) * d;
522                     t[1] = (Sd(0, 2) * Sd(2, 1) - Sd(0, 1) * Sd(2, 2)) * d;
523                     t[2] = (Sd(0, 1) * Sd(1, 2) - Sd(0, 2) * Sd(1, 1)) * d;
524
525                     t[3] = (Sd(1, 2) * Sd(2, 0) - Sd(1, 0) * Sd(2, 2)) * d;
526                     t[4] = (Sd(0, 0) * Sd(2, 2) - Sd(0, 2) * Sd(2, 0)) * d;
527                     t[5] = (Sd(0, 2) * Sd(1, 0) - Sd(0, 0) * Sd(1, 2)) * d;
528
529                     t[6] = (Sd(1, 0) * Sd(2, 1) - Sd(1, 1) * Sd(2, 0)) * d;
530                     t[7] = (Sd(0, 1) * Sd(2, 0) - Sd(0, 0) * Sd(2, 1)) * d;
531                     t[8] = (Sd(0, 0) * Sd(1, 1) - Sd(0, 1) * Sd(1, 0)) * d;
532
533                     Dd(0, 0) = t[0];
534                     Dd(0, 1) = t[1];
535                     Dd(0, 2) = t[2];
536                     Dd(1, 0) = t[3];
537                     Dd(1, 1) = t[4];
538                     Dd(1, 2) = t[5];
539                     Dd(2, 0) = t[6];
540                     Dd(2, 1) = t[7];
541                     Dd(2, 2) = t[8];
542                 }
543                 return result;
544             }
545
546             void warpAffine_gpu(const oclMat &src, oclMat &dst, F coeffs[2][3], int interpolation)
547             {
548                 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
549                 int srcStep = src.step1();
550                 int dstStep = dst.step1();
551                 float float_coeffs[2][3];
552                 cl_mem coeffs_cm;
553
554                 Context *clCxt = src.clCxt;
555                 String s[3] = {"NN", "Linear", "Cubic"};
556                 String kernelName = "warpAffine" + s[interpolation];
557
558                 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
559                 {
560                     cl_int st;
561                     coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(F) * 2 * 3, NULL, &st );
562                     openCLVerifyCall(st);
563                     openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
564                                                         sizeof(F) * 2 * 3, coeffs, 0, 0, 0));
565                 }
566                 else
567                 {
568                     cl_int st;
569                     for(int m = 0; m < 2; m++)
570                         for(int n = 0; n < 3; n++)
571                             float_coeffs[m][n] = coeffs[m][n];
572
573                     coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 2 * 3, NULL, &st );
574                     openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm,
575                                                         1, 0, sizeof(float) * 2 * 3, float_coeffs, 0, 0, 0));
576
577                 }
578                 //TODO: improve this kernel
579                 size_t blkSizeX = 16, blkSizeY = 16;
580                 size_t glbSizeX;
581                 size_t cols;
582
583                 if (src.type() == CV_8UC1 && interpolation != 2)
584                 {
585                     cols = (dst.cols + dst.offset % 4 + 3) / 4;
586                     glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
587                 }
588                 else
589                 {
590                     cols = dst.cols;
591                     glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
592                 }
593
594                 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
595                 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
596                 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
597
598                 std::vector< std::pair<size_t, const void *> > args;
599
600                 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
601                 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
602                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
603                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
604                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
605                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
606                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
607                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
608                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
609                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
610                 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
611                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
612
613                 openCLExecuteKernel(clCxt, &imgproc_warpAffine, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
614                 openCLSafeCall(clReleaseMemObject(coeffs_cm));
615             }
616
617             void warpPerspective_gpu(const oclMat &src, oclMat &dst, double coeffs[3][3], int interpolation)
618             {
619                 CV_Assert( (src.oclchannels() == dst.oclchannels()) );
620                 int srcStep = src.step1();
621                 int dstStep = dst.step1();
622                 float float_coeffs[3][3];
623                 cl_mem coeffs_cm;
624
625                 Context *clCxt = src.clCxt;
626                 String s[3] = {"NN", "Linear", "Cubic"};
627                 String kernelName = "warpPerspective" + s[interpolation];
628
629                 if (src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
630                 {
631                     cl_int st;
632                     coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(double) * 3 * 3, NULL, &st );
633                     openCLVerifyCall(st);
634                     openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
635                                                         sizeof(double) * 3 * 3, coeffs, 0, 0, 0));
636                 }
637                 else
638                 {
639                     cl_int st;
640                     for(int m = 0; m < 3; m++)
641                         for(int n = 0; n < 3; n++)
642                             float_coeffs[m][n] = coeffs[m][n];
643
644                     coeffs_cm = clCreateBuffer(*(cl_context*)clCxt->getOpenCLContextPtr(), CL_MEM_READ_WRITE, sizeof(float) * 3 * 3, NULL, &st );
645                     openCLVerifyCall(st);
646                     openCLSafeCall(clEnqueueWriteBuffer(*(cl_command_queue*)clCxt->getOpenCLCommandQueuePtr(), (cl_mem)coeffs_cm, 1, 0,
647                                                         sizeof(float) * 3 * 3, float_coeffs, 0, 0, 0));
648                 }
649
650                 //TODO: improve this kernel
651                 size_t blkSizeX = 16, blkSizeY = 16;
652                 size_t glbSizeX;
653                 size_t cols;
654                 if (src.type() == CV_8UC1 && interpolation == 0)
655                 {
656                     cols = (dst.cols + dst.offset % 4 + 3) / 4;
657                     glbSizeX = cols % blkSizeX == 0 ? cols : (cols / blkSizeX + 1) * blkSizeX;
658                 }
659                 else
660                 {
661                     cols = dst.cols;
662                     glbSizeX = dst.cols % blkSizeX == 0 ? dst.cols : (dst.cols / blkSizeX + 1) * blkSizeX;
663                 }
664
665                 size_t glbSizeY = dst.rows % blkSizeY == 0 ? dst.rows : (dst.rows / blkSizeY + 1) * blkSizeY;
666                 size_t globalThreads[3] = {glbSizeX, glbSizeY, 1};
667                 size_t localThreads[3] = {blkSizeX, blkSizeY, 1};
668
669                 std::vector< std::pair<size_t, const void *> > args;
670
671                 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&src.data));
672                 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&dst.data));
673                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.cols));
674                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.rows));
675                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.cols));
676                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.rows));
677                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&srcStep));
678                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dstStep));
679                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&src.offset));
680                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&dst.offset));
681                 args.push_back(std::make_pair(sizeof(cl_mem), (void *)&coeffs_cm));
682                 args.push_back(std::make_pair(sizeof(cl_int), (void *)&cols));
683
684                 openCLExecuteKernel(clCxt, &imgproc_warpPerspective, kernelName, globalThreads, localThreads, args, src.oclchannels(), src.depth());
685                 openCLSafeCall(clReleaseMemObject(coeffs_cm));
686             }
687         }
688
689         void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
690         {
691             int interpolation = flags & INTER_MAX;
692
693             CV_Assert((src.depth() == CV_8U  || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
694             CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
695
696             dst.create(dsize, src.type());
697
698             CV_Assert(M.rows == 2 && M.cols == 3);
699
700             int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
701             F coeffs[2][3];
702
703             double coeffsM[2*3];
704             Mat coeffsMat(2, 3, CV_64F, (void *)coeffsM);
705             M.convertTo(coeffsMat, coeffsMat.type());
706             if (!warpInd)
707                 convert_coeffs(coeffsM);
708
709             for(int i = 0; i < 2; ++i)
710                 for(int j = 0; j < 3; ++j)
711                     coeffs[i][j] = coeffsM[i*3+j];
712
713             warpAffine_gpu(src, dst, coeffs, interpolation);
714         }
715
716         void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags)
717         {
718             int interpolation = flags & INTER_MAX;
719
720             CV_Assert((src.depth() == CV_8U  || src.depth() == CV_32F) && src.oclchannels() != 2 && src.oclchannels() != 3);
721             CV_Assert(interpolation == INTER_NEAREST || interpolation == INTER_LINEAR || interpolation == INTER_CUBIC);
722
723             dst.create(dsize, src.type());
724
725
726             CV_Assert(M.rows == 3 && M.cols == 3);
727
728             int warpInd = (flags & WARP_INVERSE_MAP) >> 4;
729             double coeffs[3][3];
730
731             double coeffsM[3*3];
732             Mat coeffsMat(3, 3, CV_64F, (void *)coeffsM);
733             M.convertTo(coeffsMat, coeffsMat.type());
734             if (!warpInd)
735                 invert(coeffsM);
736
737             for(int i = 0; i < 3; ++i)
738                 for(int j = 0; j < 3; ++j)
739                     coeffs[i][j] = coeffsM[i*3+j];
740
741             warpPerspective_gpu(src, dst, coeffs, interpolation);
742         }
743
744         ////////////////////////////////////////////////////////////////////////
745         // integral
746
747         void integral(const oclMat &src, oclMat &sum, oclMat &sqsum, int sdepth)
748         {
749             CV_Assert(src.type() == CV_8UC1);
750             if (!src.clCxt->supportsFeature(ocl::FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
751             {
752                 CV_Error(Error::OpenCLDoubleNotSupported, "Select device doesn't support double");
753                 return;
754             }
755
756             if( sdepth <= 0 )
757                 sdepth = CV_32S;
758             sdepth = CV_MAT_DEPTH(sdepth);
759             int type = CV_MAKE_TYPE(sdepth, 1);
760
761             int vlen = 4;
762             int offset = src.offset / vlen;
763             int pre_invalid = src.offset % vlen;
764             int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
765
766             oclMat t_sum , t_sqsum;
767             int w = src.cols + 1, h = src.rows + 1;
768
769             char build_option[250];
770             if(Context::getContext()->supportsFeature(ocl::FEATURE_CL_DOUBLE))
771             {
772                 t_sqsum.create(src.cols, src.rows, CV_64FC1);
773                 sqsum.create(h, w, CV_64FC1);
774                 sprintf(build_option, "-D TYPE=double -D TYPE4=double4 -D convert_TYPE4=convert_double4");
775             }
776             else
777             {
778                 t_sqsum.create(src.cols, src.rows, CV_32FC1);
779                 sqsum.create(h, w, CV_32FC1);
780                 sprintf(build_option, "-D TYPE=float -D TYPE4=float4 -D convert_TYPE4=convert_float4");
781             }
782
783             t_sum.create(src.cols, src.rows, type);
784             sum.create(h, w, type);
785
786             int sum_offset = sum.offset / sum.elemSize();
787             int sqsum_offset = sqsum.offset / sqsum.elemSize();
788
789             std::vector<std::pair<size_t , const void *> > args;
790             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
791             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
792             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
793             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
794             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
795             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
796             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
797             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
798             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
799             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sqsum.step));
800             size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
801             openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_cols", gt, lt, args, -1, sdepth, build_option);
802
803             args.clear();
804             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
805             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sqsum.data ));
806             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
807             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sqsum.data ));
808             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
809             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
810             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
811             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sqsum.step));
812             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
813             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum.step));
814             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
815             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sqsum_offset));
816             size_t gt2[3] = {t_sum.cols  * 32, 1, 1}, lt2[3] = {256, 1, 1};
817             openCLExecuteKernel(src.clCxt, &imgproc_integral, "integral_rows", gt2, lt2, args, -1, sdepth, build_option);
818         }
819
820         void integral(const oclMat &src, oclMat &sum, int sdepth)
821         {
822             CV_Assert(src.type() == CV_8UC1);
823             int vlen = 4;
824             int offset = src.offset / vlen;
825             int pre_invalid = src.offset % vlen;
826             int vcols = (pre_invalid + src.cols + vlen - 1) / vlen;
827
828             if( sdepth <= 0 )
829                 sdepth = CV_32S;
830             sdepth = CV_MAT_DEPTH(sdepth);
831             int type = CV_MAKE_TYPE(sdepth, 1);
832
833             oclMat t_sum;
834             int w = src.cols + 1, h = src.rows + 1;
835
836             t_sum.create(src.cols, src.rows, type);
837             sum.create(h, w, type);
838
839             int sum_offset = sum.offset / vlen;
840             std::vector<std::pair<size_t , const void *> > args;
841             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
842             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
843             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&offset ));
844             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&pre_invalid ));
845             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
846             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
847             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
848             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step));
849             size_t gt[3] = {((vcols + 1) / 2) * 256, 1, 1}, lt[3] = {256, 1, 1};
850             openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_cols", gt, lt, args, -1, sdepth);
851
852             args.clear();
853             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&t_sum.data ));
854             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&sum.data ));
855             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.rows ));
856             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.cols ));
857             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&t_sum.step ));
858             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum.step));
859             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sum_offset));
860             size_t gt2[3] = {t_sum.cols  * 32, 1, 1}, lt2[3] = {256, 1, 1};
861             openCLExecuteKernel(src.clCxt, &imgproc_integral_sum, "integral_sum_rows", gt2, lt2, args, -1, sdepth);
862         }
863
864         /////////////////////// corner //////////////////////////////
865
866         static void extractCovData(const oclMat &src, oclMat &Dx, oclMat &Dy,
867                             int blockSize, int ksize, int borderType)
868         {
869             CV_Assert(src.type() == CV_8UC1 || src.type() == CV_32FC1);
870             double scale = static_cast<double>(1 << ((ksize > 0 ? ksize : 3) - 1)) * blockSize;
871             if (ksize < 0)
872                 scale *= 2.;
873
874             if (src.depth() == CV_8U)
875             {
876                 scale *= 255.;
877                 scale = 1. / scale;
878             }
879             else
880                 scale = 1. / scale;
881
882             if (ksize > 0)
883             {
884                 Context* clCxt = Context::getContext();
885                 if(clCxt->supportsFeature(FEATURE_CL_INTEL_DEVICE) && src.type() == CV_8UC1 &&
886                     src.cols % 8 == 0 && src.rows % 8 == 0 &&
887                     ksize==3 &&
888                     (borderType ==cv::BORDER_REFLECT ||
889                      borderType == cv::BORDER_REPLICATE ||
890                      borderType ==cv::BORDER_REFLECT101 ||
891                      borderType ==cv::BORDER_WRAP))
892                 {
893                     Dx.create(src.size(), CV_32FC1);
894                     Dy.create(src.size(), CV_32FC1);
895
896                     const unsigned int block_x = 8;
897                     const unsigned int block_y = 8;
898
899                     unsigned int src_pitch = src.step;
900                     unsigned int dst_pitch = Dx.cols;
901
902                     float _scale = scale;
903
904                     std::vector<std::pair<size_t , const void *> > args;
905                     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
906                     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
907                     args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data ));
908                     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.cols ));
909                     args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.rows ));
910                     args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&src_pitch ));
911                     args.push_back( std::make_pair( sizeof(cl_uint) , (void *)&dst_pitch ));
912                     args.push_back( std::make_pair( sizeof(cl_float) , (void *)&_scale ));
913                     size_t gt2[3] = {src.cols, src.rows, 1}, lt2[3] = {block_x, block_y, 1};
914
915                     String option = "-D BLK_X=8 -D BLK_Y=8";
916                     switch(borderType)
917                     {
918                     case cv::BORDER_REPLICATE:
919                         option = option + " -D BORDER_REPLICATE";
920                         break;
921                     case cv::BORDER_REFLECT:
922                         option = option + " -D BORDER_REFLECT";
923                         break;
924                     case cv::BORDER_REFLECT101:
925                         option = option + " -D BORDER_REFLECT101";
926                         break;
927                     case cv::BORDER_WRAP:
928                         option = option + " -D BORDER_WRAP";
929                         break;
930                     }
931                     openCLExecuteKernel(src.clCxt, &imgproc_sobel3, "sobel3", gt2, lt2, args, -1, -1, option.c_str() );
932                 }
933                 else
934                 {
935                     Sobel(src, Dx, CV_32F, 1, 0, ksize, scale, 0, borderType);
936                     Sobel(src, Dy, CV_32F, 0, 1, ksize, scale, 0, borderType);
937                 }
938             }
939             else
940             {
941                 Scharr(src, Dx, CV_32F, 1, 0, scale, 0, borderType);
942                 Scharr(src, Dy, CV_32F, 0, 1, scale, 0, borderType);
943             }
944             CV_Assert(Dx.offset == 0 && Dy.offset == 0);
945         }
946
947         static void corner_ocl(const cv::ocl::ProgramEntry* source, String kernelName, int block_size, float k, oclMat &Dx, oclMat &Dy,
948                         oclMat &dst, int border_type)
949         {
950             char borderType[30];
951             switch (border_type)
952             {
953             case cv::BORDER_CONSTANT:
954                 sprintf(borderType, "BORDER_CONSTANT");
955                 break;
956             case cv::BORDER_REFLECT101:
957                 sprintf(borderType, "BORDER_REFLECT101");
958                 break;
959             case cv::BORDER_REFLECT:
960                 sprintf(borderType, "BORDER_REFLECT");
961                 break;
962             case cv::BORDER_REPLICATE:
963                 sprintf(borderType, "BORDER_REPLICATE");
964                 break;
965             default:
966                 CV_Error(Error::StsBadFlag, "BORDER type is not supported!");
967             }
968
969             std::string buildOptions = format("-D anX=%d -D anY=%d -D ksX=%d -D ksY=%d -D %s",
970                     block_size / 2, block_size / 2, block_size, block_size, borderType);
971
972             size_t blockSizeX = 256, blockSizeY = 1;
973             size_t gSize = blockSizeX - block_size / 2 * 2;
974             size_t globalSizeX = (Dx.cols) % gSize == 0 ? Dx.cols / gSize * blockSizeX : (Dx.cols / gSize + 1) * blockSizeX;
975             size_t rows_per_thread = 2;
976             size_t globalSizeY = ((Dx.rows + rows_per_thread - 1) / rows_per_thread) % blockSizeY == 0 ?
977                                  ((Dx.rows + rows_per_thread - 1) / rows_per_thread) :
978                                  (((Dx.rows + rows_per_thread - 1) / rows_per_thread) / blockSizeY + 1) * blockSizeY;
979
980             size_t gt[3] = { globalSizeX, globalSizeY, 1 };
981             size_t lt[3]  = { blockSizeX, blockSizeY, 1 };
982             std::vector<std::pair<size_t , const void *> > args;
983             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dx.data ));
984             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&Dy.data));
985             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data));
986             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.offset ));
987             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholerows ));
988             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dx.wholecols ));
989             args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dx.step));
990             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.offset ));
991             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholerows ));
992             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&Dy.wholecols ));
993             args.push_back( std::make_pair(sizeof(cl_int), (void *)&Dy.step));
994             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.offset));
995             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.rows));
996             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.cols));
997             args.push_back( std::make_pair(sizeof(cl_int), (void *)&dst.step));
998             args.push_back( std::make_pair( sizeof(cl_float) , (void *)&k));
999
1000             openCLExecuteKernel(dst.clCxt, source, kernelName, gt, lt, args, -1, -1, buildOptions.c_str());
1001         }
1002
1003         void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize,
1004                           double k, int borderType)
1005         {
1006             oclMat dx, dy;
1007             cornerHarris_dxdy(src, dst, dx, dy, blockSize, ksize, k, borderType);
1008         }
1009
1010         void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize,
1011                           double k, int borderType)
1012         {
1013             if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1014             {
1015                 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1016                 return;
1017             }
1018
1019             CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 || borderType == cv::BORDER_REPLICATE
1020                       || borderType == cv::BORDER_REFLECT);
1021
1022             extractCovData(src, dx, dy, blockSize, ksize, borderType);
1023             dst.create(src.size(), CV_32FC1);
1024             corner_ocl(&imgproc_calcHarris, "calcHarris", blockSize, static_cast<float>(k), dx, dy, dst, borderType);
1025         }
1026
1027         void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int borderType)
1028         {
1029             oclMat dx, dy;
1030             cornerMinEigenVal_dxdy(src, dst, dx, dy, blockSize, ksize, borderType);
1031         }
1032
1033         void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &dx, oclMat &dy, int blockSize, int ksize, int borderType)
1034         {
1035             if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.depth() == CV_64F)
1036             {
1037                 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
1038                 return;
1039             }
1040
1041             CV_Assert(borderType == cv::BORDER_CONSTANT || borderType == cv::BORDER_REFLECT101 ||
1042                       borderType == cv::BORDER_REPLICATE || borderType == cv::BORDER_REFLECT);
1043
1044             extractCovData(src, dx, dy, blockSize, ksize, borderType);
1045             dst.create(src.size(), CV_32F);
1046
1047             corner_ocl(&imgproc_calcMinEigenVal, "calcMinEigenVal", blockSize, 0, dx, dy, dst, borderType);
1048         }
1049
1050         /////////////////////////////////// MeanShiftfiltering ///////////////////////////////////////////////
1051
1052         static void meanShiftFiltering_gpu(const oclMat &src, oclMat dst, int sp, int sr, int maxIter, float eps)
1053         {
1054             CV_Assert( (src.cols == dst.cols) && (src.rows == dst.rows) );
1055             CV_Assert( !(dst.step & 0x3) );
1056
1057             //Arrange the NDRange
1058             int col = src.cols, row = src.rows;
1059             int ltx = 16, lty = 8;
1060             if (src.cols % ltx != 0)
1061                 col = (col / ltx + 1) * ltx;
1062             if (src.rows % lty != 0)
1063                 row = (row / lty + 1) * lty;
1064
1065             size_t globalThreads[3] = {col, row, 1};
1066             size_t localThreads[3]  = {ltx, lty, 1};
1067
1068             //set args
1069             std::vector<std::pair<size_t , const void *> > args;
1070             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dst.data ));
1071             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.step ));
1072             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1073             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1074             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.offset ));
1075             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1076             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.cols ));
1077             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dst.rows ));
1078             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1079             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1080             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1081             args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1082
1083             openCLExecuteKernel(src.clCxt, &meanShift, "meanshift_kernel", globalThreads, localThreads, args, -1, -1);
1084         }
1085
1086         void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr, TermCriteria criteria)
1087         {
1088             if (src.empty())
1089                 CV_Error(Error::StsBadArg, "The input image is empty");
1090
1091             if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1092                 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1093
1094             dst.create( src.size(), CV_8UC4 );
1095
1096             if ( !(criteria.type & TermCriteria::MAX_ITER) )
1097                 criteria.maxCount = 5;
1098
1099             int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1100
1101             float eps;
1102             if ( !(criteria.type & TermCriteria::EPS) )
1103                 eps = 1.f;
1104             eps = (float)std::max(criteria.epsilon, 0.0);
1105
1106             meanShiftFiltering_gpu(src, dst, sp, sr, maxIter, eps);
1107         }
1108
1109         static void meanShiftProc_gpu(const oclMat &src, oclMat dstr, oclMat dstsp, int sp, int sr, int maxIter, float eps)
1110         {
1111             //sanity checks
1112             CV_Assert( (src.cols == dstr.cols) && (src.rows == dstr.rows) &&
1113                        (src.rows == dstsp.rows) && (src.cols == dstsp.cols));
1114             CV_Assert( !(dstsp.step & 0x3) );
1115
1116             //Arrange the NDRange
1117             int col = src.cols, row = src.rows;
1118             int ltx = 16, lty = 8;
1119             if (src.cols % ltx != 0)
1120                 col = (col / ltx + 1) * ltx;
1121             if (src.rows % lty != 0)
1122                 row = (row / lty + 1) * lty;
1123
1124             size_t globalThreads[3] = {col, row, 1};
1125             size_t localThreads[3]  = {ltx, lty, 1};
1126
1127             //set args
1128             std::vector<std::pair<size_t , const void *> > args;
1129             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&src.data ));
1130             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstr.data ));
1131             args.push_back( std::make_pair( sizeof(cl_mem) , (void *)&dstsp.data ));
1132             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.step ));
1133             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.step ));
1134             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.step ));
1135             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&src.offset ));
1136             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.offset ));
1137             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstsp.offset ));
1138             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.cols ));
1139             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&dstr.rows ));
1140             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sp ));
1141             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&sr ));
1142             args.push_back( std::make_pair( sizeof(cl_int) , (void *)&maxIter ));
1143             args.push_back( std::make_pair( sizeof(cl_float) , (void *)&eps ));
1144
1145             openCLExecuteKernel(src.clCxt, &meanShift, "meanshiftproc_kernel", globalThreads, localThreads, args, -1, -1);
1146         }
1147
1148         void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr, TermCriteria criteria)
1149         {
1150             if (src.empty())
1151                 CV_Error(Error::StsBadArg, "The input image is empty");
1152
1153             if ( src.depth() != CV_8U || src.oclchannels() != 4 )
1154                 CV_Error(Error::StsUnsupportedFormat, "Only 8-bit, 4-channel images are supported");
1155
1156 //            if (!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE))
1157 //            {
1158 //                CV_Error(Error::OpenCLDoubleNotSupportedNotSupported, "Selected device doesn't support double, so a deviation exists.\nIf the accuracy is acceptable, the error can be ignored.\n");
1159 //                return;
1160 //            }
1161
1162             dstr.create( src.size(), CV_8UC4 );
1163             dstsp.create( src.size(), CV_16SC2 );
1164
1165             if ( !(criteria.type & TermCriteria::MAX_ITER) )
1166                 criteria.maxCount = 5;
1167
1168             int maxIter = std::min(std::max(criteria.maxCount, 1), 100);
1169
1170             float eps;
1171             if ( !(criteria.type & TermCriteria::EPS) )
1172                 eps = 1.f;
1173             eps = (float)std::max(criteria.epsilon, 0.0);
1174
1175             meanShiftProc_gpu(src, dstr, dstsp, sp, sr, maxIter, eps);
1176         }
1177
1178         ///////////////////////////////////////////////////////////////////////////////////////////////////
1179         ////////////////////////////////////////////////////hist///////////////////////////////////////////////
1180         /////////////////////////////////////////////////////////////////////////////////////////////////////
1181
1182         namespace histograms
1183         {
1184             const int PARTIAL_HISTOGRAM256_COUNT = 256;
1185             const int HISTOGRAM256_BIN_COUNT = 256;
1186         }
1187         ///////////////////////////////calcHist/////////////////////////////////////////////////////////////////
1188         static void calc_sub_hist(const oclMat &mat_src, const oclMat &mat_sub_hist)
1189         {
1190             using namespace histograms;
1191
1192             int depth = mat_src.depth();
1193
1194             size_t localThreads[3]  = { HISTOGRAM256_BIN_COUNT, 1, 1 };
1195             size_t globalThreads[3] = { PARTIAL_HISTOGRAM256_COUNT *localThreads[0], 1, 1};
1196
1197             int dataWidth = 16;
1198             int dataWidth_bits = 4;
1199             int mask = dataWidth - 1;
1200
1201             int cols = mat_src.cols * mat_src.oclchannels();
1202             int src_offset = mat_src.offset;
1203             int hist_step = mat_sub_hist.step >> 2;
1204             int left_col = 0, right_col = 0;
1205
1206             if (cols >= dataWidth * 2 - 1)
1207             {
1208                 left_col = dataWidth - (src_offset & mask);
1209                 left_col &= mask;
1210                 src_offset += left_col;
1211                 cols -= left_col;
1212                 right_col = cols & mask;
1213                 cols -= right_col;
1214             }
1215             else
1216             {
1217                 left_col = cols;
1218                 right_col = 0;
1219                 cols = 0;
1220                 globalThreads[0] = 0;
1221             }
1222
1223             std::vector<std::pair<size_t , const void *> > args;
1224             if (globalThreads[0] != 0)
1225             {
1226                 int tempcols = cols >> dataWidth_bits;
1227                 int inc_x = globalThreads[0] % tempcols;
1228                 int inc_y = globalThreads[0] / tempcols;
1229                 src_offset >>= dataWidth_bits;
1230                 int src_step = mat_src.step >> dataWidth_bits;
1231                 int datacount = tempcols * mat_src.rows;
1232
1233                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1234                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1235                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1236                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1237                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&datacount));
1238                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tempcols));
1239                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_x));
1240                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&inc_y));
1241                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1242
1243                 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist", globalThreads, localThreads, args, -1, depth);
1244             }
1245
1246             if (left_col != 0 || right_col != 0)
1247             {
1248                 src_offset = mat_src.offset;
1249                 localThreads[0] = 1;
1250                 localThreads[1] = 256;
1251                 globalThreads[0] = left_col + right_col;
1252                 globalThreads[1] = mat_src.rows;
1253
1254                 args.clear();
1255                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
1256                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
1257                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset));
1258                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_sub_hist.data));
1259                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&left_col));
1260                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
1261                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
1262                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&hist_step));
1263
1264                 openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calc_sub_hist_border", globalThreads, localThreads, args, -1, depth);
1265             }
1266         }
1267
1268         static void merge_sub_hist(const oclMat &sub_hist, oclMat &mat_hist)
1269         {
1270             using namespace histograms;
1271
1272             size_t localThreads[3]  = { 256, 1, 1 };
1273             size_t globalThreads[3] = { HISTOGRAM256_BIN_COUNT *localThreads[0], 1, 1};
1274             int src_step = sub_hist.step >> 2;
1275
1276             std::vector<std::pair<size_t , const void *> > args;
1277             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&sub_hist.data));
1278             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1279             args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step));
1280
1281             openCLExecuteKernel(sub_hist.clCxt, &imgproc_histogram, "merge_hist", globalThreads, localThreads, args, -1, -1);
1282         }
1283
1284         void calcHist(const oclMat &mat_src, oclMat &mat_hist)
1285         {
1286             using namespace histograms;
1287             CV_Assert(mat_src.type() == CV_8UC1);
1288             mat_hist.create(1, 256, CV_32SC1);
1289
1290             oclMat buf(PARTIAL_HISTOGRAM256_COUNT, HISTOGRAM256_BIN_COUNT, CV_32SC1);
1291             buf.setTo(0);
1292
1293             calc_sub_hist(mat_src, buf);
1294             merge_sub_hist(buf, mat_hist);
1295         }
1296
1297         ///////////////////////////////////equalizeHist/////////////////////////////////////////////////////
1298         void equalizeHist(const oclMat &mat_src, oclMat &mat_dst)
1299         {
1300             mat_dst.create(mat_src.rows, mat_src.cols, CV_8UC1);
1301
1302             oclMat mat_hist(1, 256, CV_32SC1);
1303
1304             calcHist(mat_src, mat_hist);
1305
1306             size_t localThreads[3] = { 256, 1, 1};
1307             size_t globalThreads[3] = { 256, 1, 1};
1308             oclMat lut(1, 256, CV_8UC1);
1309             int total = mat_src.rows * mat_src.cols;
1310
1311             std::vector<std::pair<size_t , const void *> > args;
1312             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data));
1313             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_hist.data));
1314             args.push_back( std::make_pair( sizeof(int), (void *)&total));
1315
1316             openCLExecuteKernel(mat_src.clCxt, &imgproc_histogram, "calLUT", globalThreads, localThreads, args, -1, -1);
1317             LUT(mat_src, lut, mat_dst);
1318         }
1319
1320         ////////////////////////////////////////////////////////////////////////
1321         // CLAHE
1322         namespace clahe
1323         {
1324             static void calcLut(const oclMat &src, oclMat &dst,
1325                 const int tilesX, const int tilesY, const cv::Size tileSize,
1326                 const int clipLimit, const float lutScale)
1327             {
1328                 cl_int2 tile_size;
1329                 tile_size.s[0] = tileSize.width;
1330                 tile_size.s[1] = tileSize.height;
1331
1332                 std::vector<std::pair<size_t , const void *> > args;
1333                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1334                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1335                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1336                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1337                 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1338                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1339                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&clipLimit ));
1340                 args.push_back( std::make_pair( sizeof(cl_float), (void *)&lutScale ));
1341                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1342                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1343
1344                 String kernelName = "calcLut";
1345                 size_t localThreads[3]  = { 32, 8, 1 };
1346                 size_t globalThreads[3] = { tilesX * localThreads[0], tilesY * localThreads[1], 1 };
1347                 bool is_cpu = isCpuDevice();
1348                 if (is_cpu)
1349                     openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, (char*)"-D CPU");
1350                 else
1351                 {
1352                     cl_kernel kernel = openCLGetKernelFromSource(Context::getContext(), &imgproc_clahe, kernelName);
1353                     int wave_size = (int)queryWaveFrontSize(kernel);
1354                     openCLSafeCall(clReleaseKernel(kernel));
1355
1356                     std::string opt = format("-D WAVE_SIZE=%d", wave_size);
1357                     openCLExecuteKernel(Context::getContext(), &imgproc_clahe, kernelName, globalThreads, localThreads, args, -1, -1, opt.c_str());
1358                 }
1359             }
1360
1361             static void transform(const oclMat &src, oclMat &dst, const oclMat &lut,
1362                 const int tilesX, const int tilesY, const Size & tileSize)
1363             {
1364                 cl_int2 tile_size;
1365                 tile_size.s[0] = tileSize.width;
1366                 tile_size.s[1] = tileSize.height;
1367
1368                 std::vector<std::pair<size_t , const void *> > args;
1369                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1370                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1371                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&lut.data ));
1372                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step ));
1373                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.step ));
1374                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.step ));
1375                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1376                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1377                 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&tile_size ));
1378                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesX ));
1379                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&tilesY ));
1380                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.offset ));
1381                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.offset ));
1382                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&lut.offset ));
1383
1384                 size_t localThreads[3]  = { 32, 8, 1 };
1385                 size_t globalThreads[3] = { src.cols, src.rows, 1 };
1386
1387                 openCLExecuteKernel(Context::getContext(), &imgproc_clahe, "transform", globalThreads, localThreads, args, -1, -1);
1388             }
1389         }
1390
1391         namespace
1392         {
1393             class CLAHE_Impl : public cv::CLAHE
1394             {
1395             public:
1396                 CLAHE_Impl(double clipLimit = 40.0, int tilesX = 8, int tilesY = 8);
1397
1398                 cv::AlgorithmInfo* info() const;
1399
1400                 void apply(cv::InputArray src, cv::OutputArray dst);
1401
1402                 void setClipLimit(double clipLimit);
1403                 double getClipLimit() const;
1404
1405                 void setTilesGridSize(cv::Size tileGridSize);
1406                 cv::Size getTilesGridSize() const;
1407
1408                 void collectGarbage();
1409
1410             private:
1411                 double clipLimit_;
1412                 int tilesX_;
1413                 int tilesY_;
1414
1415                 oclMat srcExt_;
1416                 oclMat lut_;
1417             };
1418
1419             CLAHE_Impl::CLAHE_Impl(double clipLimit, int tilesX, int tilesY) :
1420                 clipLimit_(clipLimit), tilesX_(tilesX), tilesY_(tilesY)
1421             {
1422             }
1423
1424             CV_INIT_ALGORITHM(CLAHE_Impl, "CLAHE_OCL",
1425                 obj.info()->addParam(obj, "clipLimit", obj.clipLimit_);
1426                 obj.info()->addParam(obj, "tilesX", obj.tilesX_);
1427                 obj.info()->addParam(obj, "tilesY", obj.tilesY_))
1428
1429             void CLAHE_Impl::apply(cv::InputArray src_raw, cv::OutputArray dst_raw)
1430             {
1431                 oclMat& src = getOclMatRef(src_raw);
1432                 oclMat& dst = getOclMatRef(dst_raw);
1433                 CV_Assert( src.type() == CV_8UC1 );
1434
1435                 dst.create( src.size(), src.type() );
1436
1437                 const int histSize = 256;
1438
1439                 ensureSizeIsEnough(tilesX_ * tilesY_, histSize, CV_8UC1, lut_);
1440
1441                 cv::Size tileSize;
1442                 oclMat srcForLut;
1443
1444                 if (src.cols % tilesX_ == 0 && src.rows % tilesY_ == 0)
1445                 {
1446                     tileSize = cv::Size(src.cols / tilesX_, src.rows / tilesY_);
1447                     srcForLut = src;
1448                 }
1449                 else
1450                 {
1451                     ocl::copyMakeBorder(src, srcExt_, 0, tilesY_ - (src.rows % tilesY_), 0,
1452                                             tilesX_ - (src.cols % tilesX_), BORDER_REFLECT_101, Scalar::all(0));
1453
1454                     tileSize = Size(srcExt_.cols / tilesX_, srcExt_.rows / tilesY_);
1455                     srcForLut = srcExt_;
1456                 }
1457
1458                 const int tileSizeTotal = tileSize.area();
1459                 const float lutScale = static_cast<float>(histSize - 1) / tileSizeTotal;
1460
1461                 int clipLimit = 0;
1462                 if (clipLimit_ > 0.0)
1463                 {
1464                     clipLimit = static_cast<int>(clipLimit_ * tileSizeTotal / histSize);
1465                     clipLimit = std::max(clipLimit, 1);
1466                 }
1467
1468                 clahe::calcLut(srcForLut, lut_, tilesX_, tilesY_, tileSize, clipLimit, lutScale);
1469                 clahe::transform(src, dst, lut_, tilesX_, tilesY_, tileSize);
1470             }
1471
1472             void CLAHE_Impl::setClipLimit(double clipLimit)
1473             {
1474                 clipLimit_ = clipLimit;
1475             }
1476
1477             double CLAHE_Impl::getClipLimit() const
1478             {
1479                 return clipLimit_;
1480             }
1481
1482             void CLAHE_Impl::setTilesGridSize(cv::Size tileGridSize)
1483             {
1484                 tilesX_ = tileGridSize.width;
1485                 tilesY_ = tileGridSize.height;
1486             }
1487
1488             cv::Size CLAHE_Impl::getTilesGridSize() const
1489             {
1490                 return cv::Size(tilesX_, tilesY_);
1491             }
1492
1493             void CLAHE_Impl::collectGarbage()
1494             {
1495                 srcExt_.release();
1496                 lut_.release();
1497             }
1498         }
1499
1500         cv::Ptr<cv::CLAHE> createCLAHE(double clipLimit, cv::Size tileGridSize)
1501         {
1502             return makePtr<CLAHE_Impl>(clipLimit, tileGridSize.width, tileGridSize.height);
1503         }
1504
1505         //////////////////////////////////bilateralFilter////////////////////////////////////////////////////
1506
1507         static void oclbilateralFilter_8u( const oclMat &src, oclMat &dst, int d,
1508                                double sigma_color, double sigma_space,
1509                                int borderType )
1510         {
1511             int cn = src.channels();
1512             int i, j, maxk, radius;
1513
1514             CV_Assert( (src.channels() == 1 || src.channels() == 3) &&
1515                        src.type() == dst.type() && src.size() == dst.size() &&
1516                        src.data != dst.data );
1517
1518             if ( sigma_color <= 0 )
1519                 sigma_color = 1;
1520             if ( sigma_space <= 0 )
1521                 sigma_space = 1;
1522
1523             double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
1524             double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
1525
1526             if ( d <= 0 )
1527                 radius = cvRound(sigma_space * 1.5);
1528             else
1529                 radius = d / 2;
1530             radius = MAX(radius, 1);
1531             d = radius * 2 + 1;
1532
1533             oclMat temp;
1534             copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
1535
1536             std::vector<float> _color_weight(cn * 256);
1537             std::vector<float> _space_weight(d * d);
1538             std::vector<int> _space_ofs(d * d);
1539             float *color_weight = &_color_weight[0];
1540             float *space_weight = &_space_weight[0];
1541             int *space_ofs = &_space_ofs[0];
1542
1543             int dst_step_in_pixel = dst.step / dst.elemSize();
1544             int dst_offset_in_pixel = dst.offset / dst.elemSize();
1545             int temp_step_in_pixel = temp.step / temp.elemSize();
1546
1547             // initialize color-related bilateral filter coefficients
1548             for( i = 0; i < 256 * cn; i++ )
1549                 color_weight[i] = (float)std::exp(i * i * gauss_color_coeff);
1550
1551             // initialize space-related bilateral filter coefficients
1552             for( i = -radius, maxk = 0; i <= radius; i++ )
1553                 for( j = -radius; j <= radius; j++ )
1554                 {
1555                     double r = std::sqrt((double)i * i + (double)j * j);
1556                     if ( r > radius )
1557                         continue;
1558                     space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
1559                     space_ofs[maxk++] = (int)(i * temp_step_in_pixel + j);
1560                 }
1561
1562             oclMat oclcolor_weight(1, cn * 256, CV_32FC1, color_weight);
1563             oclMat oclspace_weight(1, d * d, CV_32FC1, space_weight);
1564             oclMat oclspace_ofs(1, d * d, CV_32SC1, space_ofs);
1565
1566             String kernelName = "bilateral";
1567             size_t localThreads[3]  = { 16, 16, 1 };
1568             size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1569
1570             if ((dst.type() == CV_8UC1) && ((dst.offset & 3) == 0) && ((dst.cols & 3) == 0))
1571             {
1572                 kernelName = "bilateral2";
1573                 globalThreads[0] = dst.cols >> 2;
1574             }
1575
1576             std::vector<std::pair<size_t , const void *> > args;
1577             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1578             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp.data ));
1579             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.rows ));
1580             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst.cols ));
1581             args.push_back( std::make_pair( sizeof(cl_int), (void *)&maxk ));
1582             args.push_back( std::make_pair( sizeof(cl_int), (void *)&radius ));
1583             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step_in_pixel ));
1584             args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset_in_pixel ));
1585             args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp_step_in_pixel ));
1586             args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.rows ));
1587             args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp.cols ));
1588             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclcolor_weight.data ));
1589             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_weight.data ));
1590             args.push_back( std::make_pair( sizeof(cl_mem), (void *)&oclspace_ofs.data ));
1591
1592             openCLExecuteKernel(src.clCxt, &imgproc_bilateral, kernelName, globalThreads, localThreads, args, dst.oclchannels(), dst.depth());
1593         }
1594
1595         void bilateralFilter(const oclMat &src, oclMat &dst, int radius, double sigmaclr, double sigmaspc, int borderType)
1596         {
1597             dst.create( src.size(), src.type() );
1598             if ( src.depth() == CV_8U )
1599                 oclbilateralFilter_8u( src, dst, radius, sigmaclr, sigmaspc, borderType );
1600             else
1601                 CV_Error(Error::StsUnsupportedFormat, "Bilateral filtering is only implemented for CV_8U images");
1602         }
1603
1604     }
1605 }
1606 //////////////////////////////////mulSpectrums////////////////////////////////////////////////////
1607 void cv::ocl::mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int /*flags*/, float scale, bool conjB)
1608 {
1609     CV_Assert(a.type() == CV_32FC2);
1610     CV_Assert(b.type() == CV_32FC2);
1611
1612     c.create(a.size(), CV_32FC2);
1613
1614     size_t lt[3]  = { 16, 16, 1 };
1615     size_t gt[3]  = { a.cols, a.rows, 1 };
1616
1617     String kernelName = conjB ? "mulAndScaleSpectrumsKernel_CONJ":"mulAndScaleSpectrumsKernel";
1618
1619     std::vector<std::pair<size_t , const void *> > args;
1620     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&a.data ));
1621     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&b.data ));
1622     args.push_back( std::make_pair( sizeof(cl_float), (void *)&scale));
1623     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&c.data ));
1624     args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.cols ));
1625     args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.rows));
1626     args.push_back( std::make_pair( sizeof(cl_int), (void *)&a.step ));
1627
1628     Context *clCxt = Context::getContext();
1629     openCLExecuteKernel(clCxt, &imgproc_mulAndScaleSpectrums, kernelName, gt, lt, args, -1, -1);
1630 }
1631 //////////////////////////////////convolve////////////////////////////////////////////////////
1632 // ported from CUDA module
1633 void cv::ocl::ConvolveBuf::create(Size image_size, Size templ_size)
1634 {
1635     result_size = Size(image_size.width - templ_size.width + 1,
1636                        image_size.height - templ_size.height + 1);
1637
1638     block_size = user_block_size;
1639     if (user_block_size.width == 0 || user_block_size.height == 0)
1640         block_size = estimateBlockSize(result_size, templ_size);
1641
1642     dft_size.width  = 1 << int(ceil(std::log(block_size.width + templ_size.width - 1.) / std::log(2.)));
1643     dft_size.height = 1 << int(ceil(std::log(block_size.height + templ_size.height - 1.) / std::log(2.)));
1644
1645     // CUFFT has hard-coded kernels for power-of-2 sizes (up to 8192),
1646     // see CUDA Toolkit 4.1 CUFFT Library Programming Guide
1647     //if (dft_size.width > 8192)
1648     dft_size.width = getOptimalDFTSize(block_size.width + templ_size.width - 1.);
1649     //if (dft_size.height > 8192)
1650     dft_size.height = getOptimalDFTSize(block_size.height + templ_size.height - 1.);
1651
1652     // To avoid wasting time doing small DFTs
1653     dft_size.width = std::max(dft_size.width, 512);
1654     dft_size.height = std::max(dft_size.height, 512);
1655
1656     image_block.create(dft_size, CV_32F);
1657     templ_block.create(dft_size, CV_32F);
1658     result_data.create(dft_size, CV_32F);
1659
1660     //spect_len = dft_size.height * (dft_size.width / 2 + 1);
1661     image_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1662     templ_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1663     result_spect.create(dft_size.height, dft_size.width / 2 + 1, CV_32FC2);
1664
1665     // Use maximum result matrix block size for the estimated DFT block size
1666     block_size.width = std::min(dft_size.width - templ_size.width + 1, result_size.width);
1667     block_size.height = std::min(dft_size.height - templ_size.height + 1, result_size.height);
1668 }
1669
1670 Size cv::ocl::ConvolveBuf::estimateBlockSize(Size result_size, Size /*templ_size*/)
1671 {
1672     int width = (result_size.width + 2) / 3;
1673     int height = (result_size.height + 2) / 3;
1674     width = std::min(width, result_size.width);
1675     height = std::min(height, result_size.height);
1676     return Size(width, height);
1677 }
1678
1679 static void convolve_run_fft(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1680 {
1681 #if defined HAVE_CLAMDFFT
1682     CV_Assert(image.type() == CV_32F);
1683     CV_Assert(templ.type() == CV_32F);
1684
1685     buf.create(image.size(), templ.size());
1686     result.create(buf.result_size, CV_32F);
1687
1688     Size& block_size = buf.block_size;
1689     Size& dft_size = buf.dft_size;
1690
1691     oclMat& image_block = buf.image_block;
1692     oclMat& templ_block = buf.templ_block;
1693     oclMat& result_data = buf.result_data;
1694
1695     oclMat& image_spect = buf.image_spect;
1696     oclMat& templ_spect = buf.templ_spect;
1697     oclMat& result_spect = buf.result_spect;
1698
1699     oclMat templ_roi = templ;
1700     copyMakeBorder(templ_roi, templ_block, 0, templ_block.rows - templ_roi.rows, 0,
1701                    templ_block.cols - templ_roi.cols, 0, Scalar());
1702
1703     cv::ocl::dft(templ_block, templ_spect, dft_size);
1704
1705     // Process all blocks of the result matrix
1706     for (int y = 0; y < result.rows; y += block_size.height)
1707     {
1708         for (int x = 0; x < result.cols; x += block_size.width)
1709         {
1710             Size image_roi_size(std::min(x + dft_size.width, image.cols) - x,
1711                                 std::min(y + dft_size.height, image.rows) - y);
1712             Rect roi0(x, y, image_roi_size.width, image_roi_size.height);
1713
1714             oclMat image_roi(image, roi0);
1715
1716             copyMakeBorder(image_roi, image_block, 0, image_block.rows - image_roi.rows,
1717                            0, image_block.cols - image_roi.cols, 0, Scalar());
1718
1719             cv::ocl::dft(image_block, image_spect, dft_size);
1720
1721             mulSpectrums(image_spect, templ_spect, result_spect, 0,
1722                                  1.f / dft_size.area(), ccorr);
1723
1724             cv::ocl::dft(result_spect, result_data, dft_size, cv::DFT_INVERSE | cv::DFT_REAL_OUTPUT);
1725
1726             Size result_roi_size(std::min(x + block_size.width, result.cols) - x,
1727                                  std::min(y + block_size.height, result.rows) - y);
1728
1729             Rect roi1(x, y, result_roi_size.width, result_roi_size.height);
1730             Rect roi2(0, 0, result_roi_size.width, result_roi_size.height);
1731
1732             oclMat result_roi(result, roi1);
1733             oclMat result_block(result_data, roi2);
1734
1735             result_block.copyTo(result_roi);
1736         }
1737     }
1738
1739 #else
1740     CV_Error(Error::OpenCLNoAMDBlasFft, "OpenCL DFT is not implemented");
1741 #define UNUSED(x) (void)(x);
1742     UNUSED(image) UNUSED(templ) UNUSED(result) UNUSED(ccorr) UNUSED(buf)
1743 #undef UNUSED
1744 #endif
1745 }
1746
1747 static void convolve_run(const oclMat &src, const oclMat &temp1, oclMat &dst, String kernelName, const cv::ocl::ProgramEntry* source)
1748 {
1749     CV_Assert(src.depth() == CV_32FC1);
1750     CV_Assert(temp1.depth() == CV_32F);
1751     CV_Assert(temp1.cols <= 17 && temp1.rows <= 17);
1752
1753     dst.create(src.size(), src.type());
1754
1755     CV_Assert(src.cols == dst.cols && src.rows == dst.rows);
1756     CV_Assert(src.type() == dst.type());
1757
1758     size_t localThreads[3]  = { 16, 16, 1 };
1759     size_t globalThreads[3] = { dst.cols, dst.rows, 1 };
1760
1761     int src_step = src.step / src.elemSize(), src_offset = src.offset / src.elemSize();
1762     int dst_step = dst.step / dst.elemSize(), dst_offset = dst.offset / dst.elemSize();
1763     int temp1_step = temp1.step / temp1.elemSize(), temp1_offset = temp1.offset / temp1.elemSize();
1764
1765     std::vector<std::pair<size_t , const void *> > args;
1766     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data ));
1767     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&temp1.data ));
1768     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst.data ));
1769     args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.rows ));
1770     args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.cols ));
1771     args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_step ));
1772     args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step ));
1773     args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_step ));
1774     args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.rows ));
1775     args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1.cols ));
1776     args.push_back( std::make_pair( sizeof(cl_int), (void *)&src_offset ));
1777     args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_offset ));
1778     args.push_back( std::make_pair( sizeof(cl_int), (void *)&temp1_offset ));
1779
1780     openCLExecuteKernel(src.clCxt, source, kernelName, globalThreads, localThreads, args, -1, dst.depth());
1781 }
1782
1783 void cv::ocl::convolve(const oclMat &x, const oclMat &t, oclMat &y, bool ccorr)
1784 {
1785     CV_Assert(x.depth() == CV_32F);
1786     CV_Assert(t.depth() == CV_32F);
1787     y.create(x.size(), x.type());
1788     String kernelName = "convolve";
1789     if(t.cols > 17 || t.rows > 17)
1790     {
1791         ConvolveBuf buf;
1792         convolve_run_fft(x, t, y, ccorr, buf);
1793     }
1794     else
1795     {
1796         CV_Assert(ccorr == false);
1797         convolve_run(x, t, y, kernelName, &imgproc_convolve);
1798     }
1799 }
1800 void cv::ocl::convolve(const oclMat &image, const oclMat &templ, oclMat &result, bool ccorr, ConvolveBuf& buf)
1801 {
1802     result.create(image.size(), image.type());
1803     convolve_run_fft(image, templ, result, ccorr, buf);
1804 }