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