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