f4077521e15703da7ec264bdd81273a7c07fe03e
[platform/upstream/opencv.git] / modules / imgproc / src / smooth.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) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2014-2015, Itseez Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #include "precomp.hpp"
45
46 #include <vector>
47
48 #include "opencv2/core/hal/intrin.hpp"
49 #include "opencl_kernels_imgproc.hpp"
50
51 #include "opencv2/core/openvx/ovx_defs.hpp"
52
53 #include "filter.hpp"
54
55 #include "fixedpoint.inl.hpp"
56 /*
57  * This file includes the code, contributed by Simon Perreault
58  * (the function icvMedianBlur_8u_O1)
59  *
60  * Constant-time median filtering -- http://nomis80.org/ctmf.html
61  * Copyright (C) 2006 Simon Perreault
62  *
63  * Contact:
64  *  Laboratoire de vision et systemes numeriques
65  *  Pavillon Adrien-Pouliot
66  *  Universite Laval
67  *  Sainte-Foy, Quebec, Canada
68  *  G1K 7P4
69  *
70  *  perreaul@gel.ulaval.ca
71  */
72
73 namespace cv
74 {
75
76 /****************************************************************************************\
77                                          Box Filter
78 \****************************************************************************************/
79
80 template<typename T, typename ST>
81 struct RowSum :
82         public BaseRowFilter
83 {
84     RowSum( int _ksize, int _anchor ) :
85         BaseRowFilter()
86     {
87         ksize = _ksize;
88         anchor = _anchor;
89     }
90
91     virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
92     {
93         const T* S = (const T*)src;
94         ST* D = (ST*)dst;
95         int i = 0, k, ksz_cn = ksize*cn;
96
97         width = (width - 1)*cn;
98         if( ksize == 3 )
99         {
100             for( i = 0; i < width + cn; i++ )
101             {
102                 D[i] = (ST)S[i] + (ST)S[i+cn] + (ST)S[i+cn*2];
103             }
104         }
105         else if( ksize == 5 )
106         {
107             for( i = 0; i < width + cn; i++ )
108             {
109                 D[i] = (ST)S[i] + (ST)S[i+cn] + (ST)S[i+cn*2] + (ST)S[i + cn*3] + (ST)S[i + cn*4];
110             }
111         }
112         else if( cn == 1 )
113         {
114             ST s = 0;
115             for( i = 0; i < ksz_cn; i++ )
116                 s += (ST)S[i];
117             D[0] = s;
118             for( i = 0; i < width; i++ )
119             {
120                 s += (ST)S[i + ksz_cn] - (ST)S[i];
121                 D[i+1] = s;
122             }
123         }
124         else if( cn == 3 )
125         {
126             ST s0 = 0, s1 = 0, s2 = 0;
127             for( i = 0; i < ksz_cn; i += 3 )
128             {
129                 s0 += (ST)S[i];
130                 s1 += (ST)S[i+1];
131                 s2 += (ST)S[i+2];
132             }
133             D[0] = s0;
134             D[1] = s1;
135             D[2] = s2;
136             for( i = 0; i < width; i += 3 )
137             {
138                 s0 += (ST)S[i + ksz_cn] - (ST)S[i];
139                 s1 += (ST)S[i + ksz_cn + 1] - (ST)S[i + 1];
140                 s2 += (ST)S[i + ksz_cn + 2] - (ST)S[i + 2];
141                 D[i+3] = s0;
142                 D[i+4] = s1;
143                 D[i+5] = s2;
144             }
145         }
146         else if( cn == 4 )
147         {
148             ST s0 = 0, s1 = 0, s2 = 0, s3 = 0;
149             for( i = 0; i < ksz_cn; i += 4 )
150             {
151                 s0 += (ST)S[i];
152                 s1 += (ST)S[i+1];
153                 s2 += (ST)S[i+2];
154                 s3 += (ST)S[i+3];
155             }
156             D[0] = s0;
157             D[1] = s1;
158             D[2] = s2;
159             D[3] = s3;
160             for( i = 0; i < width; i += 4 )
161             {
162                 s0 += (ST)S[i + ksz_cn] - (ST)S[i];
163                 s1 += (ST)S[i + ksz_cn + 1] - (ST)S[i + 1];
164                 s2 += (ST)S[i + ksz_cn + 2] - (ST)S[i + 2];
165                 s3 += (ST)S[i + ksz_cn + 3] - (ST)S[i + 3];
166                 D[i+4] = s0;
167                 D[i+5] = s1;
168                 D[i+6] = s2;
169                 D[i+7] = s3;
170             }
171         }
172         else
173             for( k = 0; k < cn; k++, S++, D++ )
174             {
175                 ST s = 0;
176                 for( i = 0; i < ksz_cn; i += cn )
177                     s += (ST)S[i];
178                 D[0] = s;
179                 for( i = 0; i < width; i += cn )
180                 {
181                     s += (ST)S[i + ksz_cn] - (ST)S[i];
182                     D[i+cn] = s;
183                 }
184             }
185     }
186 };
187
188
189 template<typename ST, typename T>
190 struct ColumnSum :
191         public BaseColumnFilter
192 {
193     ColumnSum( int _ksize, int _anchor, double _scale ) :
194         BaseColumnFilter()
195     {
196         ksize = _ksize;
197         anchor = _anchor;
198         scale = _scale;
199         sumCount = 0;
200     }
201
202     virtual void reset() CV_OVERRIDE { sumCount = 0; }
203
204     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
205     {
206         int i;
207         ST* SUM;
208         bool haveScale = scale != 1;
209         double _scale = scale;
210
211         if( width != (int)sum.size() )
212         {
213             sum.resize(width);
214             sumCount = 0;
215         }
216
217         SUM = &sum[0];
218         if( sumCount == 0 )
219         {
220             memset((void*)SUM, 0, width*sizeof(ST));
221
222             for( ; sumCount < ksize - 1; sumCount++, src++ )
223             {
224                 const ST* Sp = (const ST*)src[0];
225
226                 for( i = 0; i < width; i++ )
227                     SUM[i] += Sp[i];
228             }
229         }
230         else
231         {
232             CV_Assert( sumCount == ksize-1 );
233             src += ksize-1;
234         }
235
236         for( ; count--; src++ )
237         {
238             const ST* Sp = (const ST*)src[0];
239             const ST* Sm = (const ST*)src[1-ksize];
240             T* D = (T*)dst;
241             if( haveScale )
242             {
243                 for( i = 0; i <= width - 2; i += 2 )
244                 {
245                     ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
246                     D[i] = saturate_cast<T>(s0*_scale);
247                     D[i+1] = saturate_cast<T>(s1*_scale);
248                     s0 -= Sm[i]; s1 -= Sm[i+1];
249                     SUM[i] = s0; SUM[i+1] = s1;
250                 }
251
252                 for( ; i < width; i++ )
253                 {
254                     ST s0 = SUM[i] + Sp[i];
255                     D[i] = saturate_cast<T>(s0*_scale);
256                     SUM[i] = s0 - Sm[i];
257                 }
258             }
259             else
260             {
261                 for( i = 0; i <= width - 2; i += 2 )
262                 {
263                     ST s0 = SUM[i] + Sp[i], s1 = SUM[i+1] + Sp[i+1];
264                     D[i] = saturate_cast<T>(s0);
265                     D[i+1] = saturate_cast<T>(s1);
266                     s0 -= Sm[i]; s1 -= Sm[i+1];
267                     SUM[i] = s0; SUM[i+1] = s1;
268                 }
269
270                 for( ; i < width; i++ )
271                 {
272                     ST s0 = SUM[i] + Sp[i];
273                     D[i] = saturate_cast<T>(s0);
274                     SUM[i] = s0 - Sm[i];
275                 }
276             }
277             dst += dststep;
278         }
279     }
280
281     double scale;
282     int sumCount;
283     std::vector<ST> sum;
284 };
285
286
287 template<>
288 struct ColumnSum<int, uchar> :
289         public BaseColumnFilter
290 {
291     ColumnSum( int _ksize, int _anchor, double _scale ) :
292         BaseColumnFilter()
293     {
294         ksize = _ksize;
295         anchor = _anchor;
296         scale = _scale;
297         sumCount = 0;
298     }
299
300     virtual void reset() CV_OVERRIDE { sumCount = 0; }
301
302     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
303     {
304         int* SUM;
305         bool haveScale = scale != 1;
306         double _scale = scale;
307
308 #if CV_SIMD128
309         bool haveSIMD128 = hasSIMD128();
310 #endif
311
312         if( width != (int)sum.size() )
313         {
314             sum.resize(width);
315             sumCount = 0;
316         }
317
318         SUM = &sum[0];
319         if( sumCount == 0 )
320         {
321             memset((void*)SUM, 0, width*sizeof(int));
322             for( ; sumCount < ksize - 1; sumCount++, src++ )
323             {
324                 const int* Sp = (const int*)src[0];
325                 int i = 0;
326 #if CV_SIMD128
327                 if( haveSIMD128 )
328                 {
329                     for (; i <= width - 4; i += 4)
330                     {
331                         v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
332                     }
333                 }
334 #endif
335                 for( ; i < width; i++ )
336                     SUM[i] += Sp[i];
337             }
338         }
339         else
340         {
341             CV_Assert( sumCount == ksize-1 );
342             src += ksize-1;
343         }
344
345         for( ; count--; src++ )
346         {
347             const int* Sp = (const int*)src[0];
348             const int* Sm = (const int*)src[1-ksize];
349             uchar* D = (uchar*)dst;
350             if( haveScale )
351             {
352                 int i = 0;
353 #if CV_SIMD128
354                 if( haveSIMD128 )
355                 {
356
357                     v_float32x4 v_scale = v_setall_f32((float)_scale);
358                     for( ; i <= width-8; i+=8 )
359                     {
360                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
361                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
362
363                         v_uint32x4 v_s0d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s0) * v_scale));
364                         v_uint32x4 v_s01d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s01) * v_scale));
365
366                         v_uint16x8 v_dst = v_pack(v_s0d, v_s01d);
367                         v_pack_store(D + i, v_dst);
368
369                         v_store(SUM + i, v_s0 - v_load(Sm + i));
370                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
371                     }
372                 }
373 #endif
374                 for( ; i < width; i++ )
375                 {
376                     int s0 = SUM[i] + Sp[i];
377                     D[i] = saturate_cast<uchar>(s0*_scale);
378                     SUM[i] = s0 - Sm[i];
379                 }
380             }
381             else
382             {
383                 int i = 0;
384 #if CV_SIMD128
385                 if( haveSIMD128 )
386                 {
387                     for( ; i <= width-8; i+=8 )
388                     {
389                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
390                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
391
392                         v_uint16x8 v_dst = v_pack(v_reinterpret_as_u32(v_s0), v_reinterpret_as_u32(v_s01));
393                         v_pack_store(D + i, v_dst);
394
395                         v_store(SUM + i, v_s0 - v_load(Sm + i));
396                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
397                     }
398                 }
399 #endif
400
401                 for( ; i < width; i++ )
402                 {
403                     int s0 = SUM[i] + Sp[i];
404                     D[i] = saturate_cast<uchar>(s0);
405                     SUM[i] = s0 - Sm[i];
406                 }
407             }
408             dst += dststep;
409         }
410     }
411
412     double scale;
413     int sumCount;
414     std::vector<int> sum;
415 };
416
417
418 template<>
419 struct ColumnSum<ushort, uchar> :
420 public BaseColumnFilter
421 {
422     enum { SHIFT = 23 };
423
424     ColumnSum( int _ksize, int _anchor, double _scale ) :
425     BaseColumnFilter()
426     {
427         ksize = _ksize;
428         anchor = _anchor;
429         scale = _scale;
430         sumCount = 0;
431         divDelta = 0;
432         divScale = 1;
433         if( scale != 1 )
434         {
435             int d = cvRound(1./scale);
436             double scalef = ((double)(1 << SHIFT))/d;
437             divScale = cvFloor(scalef);
438             scalef -= divScale;
439             divDelta = d/2;
440             if( scalef < 0.5 )
441                 divDelta++;
442             else
443                 divScale++;
444         }
445     }
446
447     virtual void reset() CV_OVERRIDE { sumCount = 0; }
448
449     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
450     {
451         const int ds = divScale;
452         const int dd = divDelta;
453         ushort* SUM;
454         const bool haveScale = scale != 1;
455
456 #if CV_SIMD128
457         bool haveSIMD128 = hasSIMD128();
458 #endif
459
460         if( width != (int)sum.size() )
461         {
462             sum.resize(width);
463             sumCount = 0;
464         }
465
466         SUM = &sum[0];
467         if( sumCount == 0 )
468         {
469             memset((void*)SUM, 0, width*sizeof(SUM[0]));
470             for( ; sumCount < ksize - 1; sumCount++, src++ )
471             {
472                 const ushort* Sp = (const ushort*)src[0];
473                 int i = 0;
474 #if CV_SIMD128
475                 if( haveSIMD128 )
476                 {
477                     for( ; i <= width - 8; i += 8 )
478                     {
479                         v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
480                     }
481                 }
482 #endif
483                 for( ; i < width; i++ )
484                     SUM[i] += Sp[i];
485             }
486         }
487         else
488         {
489             CV_Assert( sumCount == ksize-1 );
490             src += ksize-1;
491         }
492
493         for( ; count--; src++ )
494         {
495             const ushort* Sp = (const ushort*)src[0];
496             const ushort* Sm = (const ushort*)src[1-ksize];
497             uchar* D = (uchar*)dst;
498             if( haveScale )
499             {
500                 int i = 0;
501             #if CV_SIMD128
502                 v_uint32x4 ds4 = v_setall_u32((unsigned)ds);
503                 v_uint16x8 dd8 = v_setall_u16((ushort)dd);
504
505                 for( ; i <= width-16; i+=16 )
506                 {
507                     v_uint16x8 _sm0 = v_load(Sm + i);
508                     v_uint16x8 _sm1 = v_load(Sm + i + 8);
509
510                     v_uint16x8 _s0 = v_add_wrap(v_load(SUM + i), v_load(Sp + i));
511                     v_uint16x8 _s1 = v_add_wrap(v_load(SUM + i + 8), v_load(Sp + i + 8));
512
513                     v_uint32x4 _s00, _s01, _s10, _s11;
514
515                     v_expand(_s0 + dd8, _s00, _s01);
516                     v_expand(_s1 + dd8, _s10, _s11);
517
518                     _s00 = v_shr<SHIFT>(_s00*ds4);
519                     _s01 = v_shr<SHIFT>(_s01*ds4);
520                     _s10 = v_shr<SHIFT>(_s10*ds4);
521                     _s11 = v_shr<SHIFT>(_s11*ds4);
522
523                     v_int16x8 r0 = v_pack(v_reinterpret_as_s32(_s00), v_reinterpret_as_s32(_s01));
524                     v_int16x8 r1 = v_pack(v_reinterpret_as_s32(_s10), v_reinterpret_as_s32(_s11));
525
526                     _s0 = v_sub_wrap(_s0, _sm0);
527                     _s1 = v_sub_wrap(_s1, _sm1);
528
529                     v_store(D + i, v_pack_u(r0, r1));
530                     v_store(SUM + i, _s0);
531                     v_store(SUM + i + 8, _s1);
532                 }
533             #endif
534                 for( ; i < width; i++ )
535                 {
536                     int s0 = SUM[i] + Sp[i];
537                     D[i] = (uchar)((s0 + dd)*ds >> SHIFT);
538                     SUM[i] = (ushort)(s0 - Sm[i]);
539                 }
540             }
541             else
542             {
543                 int i = 0;
544                 for( ; i < width; i++ )
545                 {
546                     int s0 = SUM[i] + Sp[i];
547                     D[i] = saturate_cast<uchar>(s0);
548                     SUM[i] = (ushort)(s0 - Sm[i]);
549                 }
550             }
551             dst += dststep;
552         }
553     }
554
555     double scale;
556     int sumCount;
557     int divDelta;
558     int divScale;
559     std::vector<ushort> sum;
560 };
561
562
563 template<>
564 struct ColumnSum<int, short> :
565         public BaseColumnFilter
566 {
567     ColumnSum( int _ksize, int _anchor, double _scale ) :
568         BaseColumnFilter()
569     {
570         ksize = _ksize;
571         anchor = _anchor;
572         scale = _scale;
573         sumCount = 0;
574     }
575
576     virtual void reset() CV_OVERRIDE { sumCount = 0; }
577
578     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
579     {
580         int i;
581         int* SUM;
582         bool haveScale = scale != 1;
583         double _scale = scale;
584
585 #if CV_SIMD128
586         bool haveSIMD128 = hasSIMD128();
587 #endif
588
589         if( width != (int)sum.size() )
590         {
591             sum.resize(width);
592             sumCount = 0;
593         }
594
595         SUM = &sum[0];
596         if( sumCount == 0 )
597         {
598             memset((void*)SUM, 0, width*sizeof(int));
599             for( ; sumCount < ksize - 1; sumCount++, src++ )
600             {
601                 const int* Sp = (const int*)src[0];
602                 i = 0;
603 #if CV_SIMD128
604                 if( haveSIMD128 )
605                 {
606                     for( ; i <= width - 4; i+=4 )
607                     {
608                         v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
609                     }
610                 }
611                 #endif
612                 for( ; i < width; i++ )
613                     SUM[i] += Sp[i];
614             }
615         }
616         else
617         {
618             CV_Assert( sumCount == ksize-1 );
619             src += ksize-1;
620         }
621
622         for( ; count--; src++ )
623         {
624             const int* Sp = (const int*)src[0];
625             const int* Sm = (const int*)src[1-ksize];
626             short* D = (short*)dst;
627             if( haveScale )
628             {
629                 i = 0;
630 #if CV_SIMD128
631                 if( haveSIMD128 )
632                 {
633                     v_float32x4 v_scale = v_setall_f32((float)_scale);
634                     for( ; i <= width-8; i+=8 )
635                     {
636                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
637                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
638
639                         v_int32x4 v_s0d =  v_round(v_cvt_f32(v_s0) * v_scale);
640                         v_int32x4 v_s01d = v_round(v_cvt_f32(v_s01) * v_scale);
641                         v_store(D + i, v_pack(v_s0d, v_s01d));
642
643                         v_store(SUM + i, v_s0 - v_load(Sm + i));
644                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
645                     }
646                 }
647 #endif
648                 for( ; i < width; i++ )
649                 {
650                     int s0 = SUM[i] + Sp[i];
651                     D[i] = saturate_cast<short>(s0*_scale);
652                     SUM[i] = s0 - Sm[i];
653                 }
654             }
655             else
656             {
657                 i = 0;
658 #if CV_SIMD128
659                 if( haveSIMD128 )
660                 {
661                     for( ; i <= width-8; i+=8 )
662                     {
663                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
664                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
665
666                         v_store(D + i, v_pack(v_s0, v_s01));
667
668                         v_store(SUM + i, v_s0 - v_load(Sm + i));
669                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
670                     }
671                 }
672 #endif
673
674                 for( ; i < width; i++ )
675                 {
676                     int s0 = SUM[i] + Sp[i];
677                     D[i] = saturate_cast<short>(s0);
678                     SUM[i] = s0 - Sm[i];
679                 }
680             }
681             dst += dststep;
682         }
683     }
684
685     double scale;
686     int sumCount;
687     std::vector<int> sum;
688 };
689
690
691 template<>
692 struct ColumnSum<int, ushort> :
693         public BaseColumnFilter
694 {
695     ColumnSum( int _ksize, int _anchor, double _scale ) :
696         BaseColumnFilter()
697     {
698         ksize = _ksize;
699         anchor = _anchor;
700         scale = _scale;
701         sumCount = 0;
702     }
703
704     virtual void reset() CV_OVERRIDE { sumCount = 0; }
705
706     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
707     {
708         int* SUM;
709         bool haveScale = scale != 1;
710         double _scale = scale;
711
712 #if CV_SIMD128
713         bool haveSIMD128 = hasSIMD128();
714 #endif
715
716         if( width != (int)sum.size() )
717         {
718             sum.resize(width);
719             sumCount = 0;
720         }
721
722         SUM = &sum[0];
723         if( sumCount == 0 )
724         {
725             memset((void*)SUM, 0, width*sizeof(int));
726             for( ; sumCount < ksize - 1; sumCount++, src++ )
727             {
728                 const int* Sp = (const int*)src[0];
729                 int i = 0;
730 #if CV_SIMD128
731                 if( haveSIMD128 )
732                 {
733                     for (; i <= width - 4; i += 4)
734                     {
735                         v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
736                     }
737                 }
738 #endif
739                 for( ; i < width; i++ )
740                     SUM[i] += Sp[i];
741             }
742         }
743         else
744         {
745             CV_Assert( sumCount == ksize-1 );
746             src += ksize-1;
747         }
748
749         for( ; count--; src++ )
750         {
751             const int* Sp = (const int*)src[0];
752             const int* Sm = (const int*)src[1-ksize];
753             ushort* D = (ushort*)dst;
754             if( haveScale )
755             {
756                 int i = 0;
757 #if CV_SIMD128
758                 if( haveSIMD128 )
759                 {
760                     v_float32x4 v_scale = v_setall_f32((float)_scale);
761                     for( ; i <= width-8; i+=8 )
762                     {
763                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
764                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
765
766                         v_uint32x4 v_s0d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s0) * v_scale));
767                         v_uint32x4 v_s01d = v_reinterpret_as_u32(v_round(v_cvt_f32(v_s01) * v_scale));
768                         v_store(D + i, v_pack(v_s0d, v_s01d));
769
770                         v_store(SUM + i, v_s0 - v_load(Sm + i));
771                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
772                     }
773                 }
774 #endif
775                 for( ; i < width; i++ )
776                 {
777                     int s0 = SUM[i] + Sp[i];
778                     D[i] = saturate_cast<ushort>(s0*_scale);
779                     SUM[i] = s0 - Sm[i];
780                 }
781             }
782             else
783             {
784                 int i = 0;
785 #if CV_SIMD128
786                 if( haveSIMD128 )
787                 {
788                     for( ; i <= width-8; i+=8 )
789                     {
790                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
791                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
792
793                         v_store(D + i, v_pack(v_reinterpret_as_u32(v_s0), v_reinterpret_as_u32(v_s01)));
794
795                         v_store(SUM + i, v_s0 - v_load(Sm + i));
796                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
797                     }
798                 }
799 #endif
800                 for( ; i < width; i++ )
801                 {
802                     int s0 = SUM[i] + Sp[i];
803                     D[i] = saturate_cast<ushort>(s0);
804                     SUM[i] = s0 - Sm[i];
805                 }
806             }
807             dst += dststep;
808         }
809     }
810
811     double scale;
812     int sumCount;
813     std::vector<int> sum;
814 };
815
816 template<>
817 struct ColumnSum<int, int> :
818         public BaseColumnFilter
819 {
820     ColumnSum( int _ksize, int _anchor, double _scale ) :
821         BaseColumnFilter()
822     {
823         ksize = _ksize;
824         anchor = _anchor;
825         scale = _scale;
826         sumCount = 0;
827     }
828
829     virtual void reset() CV_OVERRIDE { sumCount = 0; }
830
831     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
832     {
833         int* SUM;
834         bool haveScale = scale != 1;
835         double _scale = scale;
836
837 #if CV_SIMD128
838         bool haveSIMD128 = hasSIMD128();
839 #endif
840
841         if( width != (int)sum.size() )
842         {
843             sum.resize(width);
844             sumCount = 0;
845         }
846
847         SUM = &sum[0];
848         if( sumCount == 0 )
849         {
850             memset((void*)SUM, 0, width*sizeof(int));
851             for( ; sumCount < ksize - 1; sumCount++, src++ )
852             {
853                 const int* Sp = (const int*)src[0];
854                 int i = 0;
855 #if CV_SIMD128
856                 if( haveSIMD128 )
857                 {
858                     for( ; i <= width - 4; i+=4 )
859                     {
860                         v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
861                     }
862                 }
863 #endif
864                 for( ; i < width; i++ )
865                     SUM[i] += Sp[i];
866             }
867         }
868         else
869         {
870             CV_Assert( sumCount == ksize-1 );
871             src += ksize-1;
872         }
873
874         for( ; count--; src++ )
875         {
876             const int* Sp = (const int*)src[0];
877             const int* Sm = (const int*)src[1-ksize];
878             int* D = (int*)dst;
879             if( haveScale )
880             {
881                 int i = 0;
882 #if CV_SIMD128
883                 if( haveSIMD128 )
884                 {
885                     v_float32x4 v_scale = v_setall_f32((float)_scale);
886                     for( ; i <= width-4; i+=4 )
887                     {
888                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
889                         v_int32x4 v_s0d = v_round(v_cvt_f32(v_s0) * v_scale);
890
891                         v_store(D + i, v_s0d);
892                         v_store(SUM + i, v_s0 - v_load(Sm + i));
893                     }
894                 }
895 #endif
896                 for( ; i < width; i++ )
897                 {
898                     int s0 = SUM[i] + Sp[i];
899                     D[i] = saturate_cast<int>(s0*_scale);
900                     SUM[i] = s0 - Sm[i];
901                 }
902             }
903             else
904             {
905                 int i = 0;
906 #if CV_SIMD128
907                 if( haveSIMD128 )
908                 {
909                     for( ; i <= width-4; i+=4 )
910                     {
911                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
912
913                         v_store(D + i, v_s0);
914                         v_store(SUM + i, v_s0 - v_load(Sm + i));
915                     }
916                 }
917 #endif
918                 for( ; i < width; i++ )
919                 {
920                     int s0 = SUM[i] + Sp[i];
921                     D[i] = s0;
922                     SUM[i] = s0 - Sm[i];
923                 }
924             }
925             dst += dststep;
926         }
927     }
928
929     double scale;
930     int sumCount;
931     std::vector<int> sum;
932 };
933
934
935 template<>
936 struct ColumnSum<int, float> :
937         public BaseColumnFilter
938 {
939     ColumnSum( int _ksize, int _anchor, double _scale ) :
940         BaseColumnFilter()
941     {
942         ksize = _ksize;
943         anchor = _anchor;
944         scale = _scale;
945         sumCount = 0;
946     }
947
948     virtual void reset() CV_OVERRIDE { sumCount = 0; }
949
950     virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
951     {
952         int* SUM;
953         bool haveScale = scale != 1;
954         double _scale = scale;
955
956 #if CV_SIMD128
957         bool haveSIMD128 = hasSIMD128();
958 #endif
959
960         if( width != (int)sum.size() )
961         {
962             sum.resize(width);
963             sumCount = 0;
964         }
965
966         SUM = &sum[0];
967         if( sumCount == 0 )
968         {
969             memset((void*)SUM, 0, width*sizeof(int));
970             for( ; sumCount < ksize - 1; sumCount++, src++ )
971             {
972                 const int* Sp = (const int*)src[0];
973                 int i = 0;
974 #if CV_SIMD128
975                 if( haveSIMD128 )
976                 {
977                     for( ; i <= width - 4; i+=4 )
978                     {
979                         v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
980                     }
981                 }
982 #endif
983
984                 for( ; i < width; i++ )
985                     SUM[i] += Sp[i];
986             }
987         }
988         else
989         {
990             CV_Assert( sumCount == ksize-1 );
991             src += ksize-1;
992         }
993
994         for( ; count--; src++ )
995         {
996             const int * Sp = (const int*)src[0];
997             const int * Sm = (const int*)src[1-ksize];
998             float* D = (float*)dst;
999             if( haveScale )
1000             {
1001                 int i = 0;
1002
1003 #if CV_SIMD128
1004                 if( haveSIMD128 )
1005                 {
1006                     v_float32x4 v_scale = v_setall_f32((float)_scale);
1007                     for (; i <= width - 8; i += 8)
1008                     {
1009                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
1010                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
1011
1012                         v_store(D + i, v_cvt_f32(v_s0) * v_scale);
1013                         v_store(D + i + 4, v_cvt_f32(v_s01) * v_scale);
1014
1015                         v_store(SUM + i, v_s0 - v_load(Sm + i));
1016                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
1017                     }
1018                 }
1019 #endif
1020                 for( ; i < width; i++ )
1021                 {
1022                     int s0 = SUM[i] + Sp[i];
1023                     D[i] = (float)(s0*_scale);
1024                     SUM[i] = s0 - Sm[i];
1025                 }
1026             }
1027             else
1028             {
1029                 int i = 0;
1030
1031 #if CV_SIMD128
1032                 if( haveSIMD128 )
1033                 {
1034                     for( ; i <= width-8; i+=8 )
1035                     {
1036                         v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
1037                         v_int32x4 v_s01 = v_load(SUM + i + 4) + v_load(Sp + i + 4);
1038
1039                         v_store(D + i, v_cvt_f32(v_s0));
1040                         v_store(D + i + 4, v_cvt_f32(v_s01));
1041
1042                         v_store(SUM + i, v_s0 - v_load(Sm + i));
1043                         v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
1044                     }
1045                 }
1046 #endif
1047                 for( ; i < width; i++ )
1048                 {
1049                     int s0 = SUM[i] + Sp[i];
1050                     D[i] = (float)(s0);
1051                     SUM[i] = s0 - Sm[i];
1052                 }
1053             }
1054             dst += dststep;
1055         }
1056     }
1057
1058     double scale;
1059     int sumCount;
1060     std::vector<int> sum;
1061 };
1062
1063 #ifdef HAVE_OPENCL
1064
1065 static bool ocl_boxFilter3x3_8UC1( InputArray _src, OutputArray _dst, int ddepth,
1066                                    Size ksize, Point anchor, int borderType, bool normalize )
1067 {
1068     const ocl::Device & dev = ocl::Device::getDefault();
1069     int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
1070
1071     if (ddepth < 0)
1072         ddepth = sdepth;
1073
1074     if (anchor.x < 0)
1075         anchor.x = ksize.width / 2;
1076     if (anchor.y < 0)
1077         anchor.y = ksize.height / 2;
1078
1079     if ( !(dev.isIntel() && (type == CV_8UC1) &&
1080          (_src.offset() == 0) && (_src.step() % 4 == 0) &&
1081          (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0) &&
1082          (anchor.x == 1) && (anchor.y == 1) &&
1083          (ksize.width == 3) && (ksize.height == 3)) )
1084         return false;
1085
1086     float alpha = 1.0f / (ksize.height * ksize.width);
1087     Size size = _src.size();
1088     size_t globalsize[2] = { 0, 0 };
1089     size_t localsize[2] = { 0, 0 };
1090     const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
1091
1092     globalsize[0] = size.width / 16;
1093     globalsize[1] = size.height / 2;
1094
1095     char build_opts[1024];
1096     sprintf(build_opts, "-D %s %s", borderMap[borderType], normalize ? "-D NORMALIZE" : "");
1097
1098     ocl::Kernel kernel("boxFilter3x3_8UC1_cols16_rows2", cv::ocl::imgproc::boxFilter3x3_oclsrc, build_opts);
1099     if (kernel.empty())
1100         return false;
1101
1102     UMat src = _src.getUMat();
1103     _dst.create(size, CV_MAKETYPE(ddepth, cn));
1104     if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
1105         return false;
1106     UMat dst = _dst.getUMat();
1107
1108     int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
1109     idxArg = kernel.set(idxArg, (int)src.step);
1110     idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
1111     idxArg = kernel.set(idxArg, (int)dst.step);
1112     idxArg = kernel.set(idxArg, (int)dst.rows);
1113     idxArg = kernel.set(idxArg, (int)dst.cols);
1114     if (normalize)
1115         idxArg = kernel.set(idxArg, (float)alpha);
1116
1117     return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
1118 }
1119
1120 #define DIVUP(total, grain) ((total + grain - 1) / (grain))
1121 #define ROUNDUP(sz, n)      ((sz) + (n) - 1 - (((sz) + (n) - 1) % (n)))
1122
1123 static bool ocl_boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1124                            Size ksize, Point anchor, int borderType, bool normalize, bool sqr = false )
1125 {
1126     const ocl::Device & dev = ocl::Device::getDefault();
1127     int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type), esz = CV_ELEM_SIZE(type);
1128     bool doubleSupport = dev.doubleFPConfig() > 0;
1129
1130     if (ddepth < 0)
1131         ddepth = sdepth;
1132
1133     if (cn > 4 || (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F)) ||
1134         _src.offset() % esz != 0 || _src.step() % esz != 0)
1135         return false;
1136
1137     if (anchor.x < 0)
1138         anchor.x = ksize.width / 2;
1139     if (anchor.y < 0)
1140         anchor.y = ksize.height / 2;
1141
1142     int computeUnits = ocl::Device::getDefault().maxComputeUnits();
1143     float alpha = 1.0f / (ksize.height * ksize.width);
1144     Size size = _src.size(), wholeSize;
1145     bool isolated = (borderType & BORDER_ISOLATED) != 0;
1146     borderType &= ~BORDER_ISOLATED;
1147     int wdepth = std::max(CV_32F, std::max(ddepth, sdepth)),
1148         wtype = CV_MAKE_TYPE(wdepth, cn), dtype = CV_MAKE_TYPE(ddepth, cn);
1149
1150     const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
1151     size_t globalsize[2] = { (size_t)size.width, (size_t)size.height };
1152     size_t localsize_general[2] = { 0, 1 }, * localsize = NULL;
1153
1154     UMat src = _src.getUMat();
1155     if (!isolated)
1156     {
1157         Point ofs;
1158         src.locateROI(wholeSize, ofs);
1159     }
1160
1161     int h = isolated ? size.height : wholeSize.height;
1162     int w = isolated ? size.width : wholeSize.width;
1163
1164     size_t maxWorkItemSizes[32];
1165     ocl::Device::getDefault().maxWorkItemSizes(maxWorkItemSizes);
1166     int tryWorkItems = (int)maxWorkItemSizes[0];
1167
1168     ocl::Kernel kernel;
1169
1170     if (dev.isIntel() && !(dev.type() & ocl::Device::TYPE_CPU) &&
1171         ((ksize.width < 5 && ksize.height < 5 && esz <= 4) ||
1172          (ksize.width == 5 && ksize.height == 5 && cn == 1)))
1173     {
1174         if (w < ksize.width || h < ksize.height)
1175             return false;
1176
1177         // Figure out what vector size to use for loading the pixels.
1178         int pxLoadNumPixels = cn != 1 || size.width % 4 ? 1 : 4;
1179         int pxLoadVecSize = cn * pxLoadNumPixels;
1180
1181         // Figure out how many pixels per work item to compute in X and Y
1182         // directions.  Too many and we run out of registers.
1183         int pxPerWorkItemX = 1, pxPerWorkItemY = 1;
1184         if (cn <= 2 && ksize.width <= 4 && ksize.height <= 4)
1185         {
1186             pxPerWorkItemX = size.width % 8 ? size.width % 4 ? size.width % 2 ? 1 : 2 : 4 : 8;
1187             pxPerWorkItemY = size.height % 2 ? 1 : 2;
1188         }
1189         else if (cn < 4 || (ksize.width <= 4 && ksize.height <= 4))
1190         {
1191             pxPerWorkItemX = size.width % 2 ? 1 : 2;
1192             pxPerWorkItemY = size.height % 2 ? 1 : 2;
1193         }
1194         globalsize[0] = size.width / pxPerWorkItemX;
1195         globalsize[1] = size.height / pxPerWorkItemY;
1196
1197         // Need some padding in the private array for pixels
1198         int privDataWidth = ROUNDUP(pxPerWorkItemX + ksize.width - 1, pxLoadNumPixels);
1199
1200         // Make the global size a nice round number so the runtime can pick
1201         // from reasonable choices for the workgroup size
1202         const int wgRound = 256;
1203         globalsize[0] = ROUNDUP(globalsize[0], wgRound);
1204
1205         char build_options[1024], cvt[2][40];
1206         sprintf(build_options, "-D cn=%d "
1207                 "-D ANCHOR_X=%d -D ANCHOR_Y=%d -D KERNEL_SIZE_X=%d -D KERNEL_SIZE_Y=%d "
1208                 "-D PX_LOAD_VEC_SIZE=%d -D PX_LOAD_NUM_PX=%d "
1209                 "-D PX_PER_WI_X=%d -D PX_PER_WI_Y=%d -D PRIV_DATA_WIDTH=%d -D %s -D %s "
1210                 "-D PX_LOAD_X_ITERATIONS=%d -D PX_LOAD_Y_ITERATIONS=%d "
1211                 "-D srcT=%s -D srcT1=%s -D dstT=%s -D dstT1=%s -D WT=%s -D WT1=%s "
1212                 "-D convertToWT=%s -D convertToDstT=%s%s%s -D PX_LOAD_FLOAT_VEC_CONV=convert_%s -D OP_BOX_FILTER",
1213                 cn, anchor.x, anchor.y, ksize.width, ksize.height,
1214                 pxLoadVecSize, pxLoadNumPixels,
1215                 pxPerWorkItemX, pxPerWorkItemY, privDataWidth, borderMap[borderType],
1216                 isolated ? "BORDER_ISOLATED" : "NO_BORDER_ISOLATED",
1217                 privDataWidth / pxLoadNumPixels, pxPerWorkItemY + ksize.height - 1,
1218                 ocl::typeToStr(type), ocl::typeToStr(sdepth), ocl::typeToStr(dtype),
1219                 ocl::typeToStr(ddepth), ocl::typeToStr(wtype), ocl::typeToStr(wdepth),
1220                 ocl::convertTypeStr(sdepth, wdepth, cn, cvt[0]),
1221                 ocl::convertTypeStr(wdepth, ddepth, cn, cvt[1]),
1222                 normalize ? " -D NORMALIZE" : "", sqr ? " -D SQR" : "",
1223                 ocl::typeToStr(CV_MAKE_TYPE(wdepth, pxLoadVecSize)) //PX_LOAD_FLOAT_VEC_CONV
1224                 );
1225
1226
1227         if (!kernel.create("filterSmall", cv::ocl::imgproc::filterSmall_oclsrc, build_options))
1228             return false;
1229     }
1230     else
1231     {
1232         localsize = localsize_general;
1233         for ( ; ; )
1234         {
1235             int BLOCK_SIZE_X = tryWorkItems, BLOCK_SIZE_Y = std::min(ksize.height * 10, size.height);
1236
1237             while (BLOCK_SIZE_X > 32 && BLOCK_SIZE_X >= ksize.width * 2 && BLOCK_SIZE_X > size.width * 2)
1238                 BLOCK_SIZE_X /= 2;
1239             while (BLOCK_SIZE_Y < BLOCK_SIZE_X / 8 && BLOCK_SIZE_Y * computeUnits * 32 < size.height)
1240                 BLOCK_SIZE_Y *= 2;
1241
1242             if (ksize.width > BLOCK_SIZE_X || w < ksize.width || h < ksize.height)
1243                 return false;
1244
1245             char cvt[2][50];
1246             String opts = format("-D LOCAL_SIZE_X=%d -D BLOCK_SIZE_Y=%d -D ST=%s -D DT=%s -D WT=%s -D convertToDT=%s -D convertToWT=%s"
1247                                  " -D ANCHOR_X=%d -D ANCHOR_Y=%d -D KERNEL_SIZE_X=%d -D KERNEL_SIZE_Y=%d -D %s%s%s%s%s"
1248                                  " -D ST1=%s -D DT1=%s -D cn=%d",
1249                                  BLOCK_SIZE_X, BLOCK_SIZE_Y, ocl::typeToStr(type), ocl::typeToStr(CV_MAKE_TYPE(ddepth, cn)),
1250                                  ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)),
1251                                  ocl::convertTypeStr(wdepth, ddepth, cn, cvt[0]),
1252                                  ocl::convertTypeStr(sdepth, wdepth, cn, cvt[1]),
1253                                  anchor.x, anchor.y, ksize.width, ksize.height, borderMap[borderType],
1254                                  isolated ? " -D BORDER_ISOLATED" : "", doubleSupport ? " -D DOUBLE_SUPPORT" : "",
1255                                  normalize ? " -D NORMALIZE" : "", sqr ? " -D SQR" : "",
1256                                  ocl::typeToStr(sdepth), ocl::typeToStr(ddepth), cn);
1257
1258             localsize[0] = BLOCK_SIZE_X;
1259             globalsize[0] = DIVUP(size.width, BLOCK_SIZE_X - (ksize.width - 1)) * BLOCK_SIZE_X;
1260             globalsize[1] = DIVUP(size.height, BLOCK_SIZE_Y);
1261
1262             kernel.create("boxFilter", cv::ocl::imgproc::boxFilter_oclsrc, opts);
1263             if (kernel.empty())
1264                 return false;
1265
1266             size_t kernelWorkGroupSize = kernel.workGroupSize();
1267             if (localsize[0] <= kernelWorkGroupSize)
1268                 break;
1269             if (BLOCK_SIZE_X < (int)kernelWorkGroupSize)
1270                 return false;
1271
1272             tryWorkItems = (int)kernelWorkGroupSize;
1273         }
1274     }
1275
1276     _dst.create(size, CV_MAKETYPE(ddepth, cn));
1277     UMat dst = _dst.getUMat();
1278
1279     int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
1280     idxArg = kernel.set(idxArg, (int)src.step);
1281     int srcOffsetX = (int)((src.offset % src.step) / src.elemSize());
1282     int srcOffsetY = (int)(src.offset / src.step);
1283     int srcEndX = isolated ? srcOffsetX + size.width : wholeSize.width;
1284     int srcEndY = isolated ? srcOffsetY + size.height : wholeSize.height;
1285     idxArg = kernel.set(idxArg, srcOffsetX);
1286     idxArg = kernel.set(idxArg, srcOffsetY);
1287     idxArg = kernel.set(idxArg, srcEndX);
1288     idxArg = kernel.set(idxArg, srcEndY);
1289     idxArg = kernel.set(idxArg, ocl::KernelArg::WriteOnly(dst));
1290     if (normalize)
1291         idxArg = kernel.set(idxArg, (float)alpha);
1292
1293     return kernel.run(2, globalsize, localsize, false);
1294 }
1295
1296 #undef ROUNDUP
1297
1298 #endif
1299
1300 }
1301
1302
1303 cv::Ptr<cv::BaseRowFilter> cv::getRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1304 {
1305     int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1306     CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
1307
1308     if( anchor < 0 )
1309         anchor = ksize/2;
1310
1311     if( sdepth == CV_8U && ddepth == CV_32S )
1312         return makePtr<RowSum<uchar, int> >(ksize, anchor);
1313     if( sdepth == CV_8U && ddepth == CV_16U )
1314         return makePtr<RowSum<uchar, ushort> >(ksize, anchor);
1315     if( sdepth == CV_8U && ddepth == CV_64F )
1316         return makePtr<RowSum<uchar, double> >(ksize, anchor);
1317     if( sdepth == CV_16U && ddepth == CV_32S )
1318         return makePtr<RowSum<ushort, int> >(ksize, anchor);
1319     if( sdepth == CV_16U && ddepth == CV_64F )
1320         return makePtr<RowSum<ushort, double> >(ksize, anchor);
1321     if( sdepth == CV_16S && ddepth == CV_32S )
1322         return makePtr<RowSum<short, int> >(ksize, anchor);
1323     if( sdepth == CV_32S && ddepth == CV_32S )
1324         return makePtr<RowSum<int, int> >(ksize, anchor);
1325     if( sdepth == CV_16S && ddepth == CV_64F )
1326         return makePtr<RowSum<short, double> >(ksize, anchor);
1327     if( sdepth == CV_32F && ddepth == CV_64F )
1328         return makePtr<RowSum<float, double> >(ksize, anchor);
1329     if( sdepth == CV_64F && ddepth == CV_64F )
1330         return makePtr<RowSum<double, double> >(ksize, anchor);
1331
1332     CV_Error_( CV_StsNotImplemented,
1333         ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1334         srcType, sumType));
1335 }
1336
1337
1338 cv::Ptr<cv::BaseColumnFilter> cv::getColumnSumFilter(int sumType, int dstType, int ksize,
1339                                                      int anchor, double scale)
1340 {
1341     int sdepth = CV_MAT_DEPTH(sumType), ddepth = CV_MAT_DEPTH(dstType);
1342     CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(dstType) );
1343
1344     if( anchor < 0 )
1345         anchor = ksize/2;
1346
1347     if( ddepth == CV_8U && sdepth == CV_32S )
1348         return makePtr<ColumnSum<int, uchar> >(ksize, anchor, scale);
1349     if( ddepth == CV_8U && sdepth == CV_16U )
1350         return makePtr<ColumnSum<ushort, uchar> >(ksize, anchor, scale);
1351     if( ddepth == CV_8U && sdepth == CV_64F )
1352         return makePtr<ColumnSum<double, uchar> >(ksize, anchor, scale);
1353     if( ddepth == CV_16U && sdepth == CV_32S )
1354         return makePtr<ColumnSum<int, ushort> >(ksize, anchor, scale);
1355     if( ddepth == CV_16U && sdepth == CV_64F )
1356         return makePtr<ColumnSum<double, ushort> >(ksize, anchor, scale);
1357     if( ddepth == CV_16S && sdepth == CV_32S )
1358         return makePtr<ColumnSum<int, short> >(ksize, anchor, scale);
1359     if( ddepth == CV_16S && sdepth == CV_64F )
1360         return makePtr<ColumnSum<double, short> >(ksize, anchor, scale);
1361     if( ddepth == CV_32S && sdepth == CV_32S )
1362         return makePtr<ColumnSum<int, int> >(ksize, anchor, scale);
1363     if( ddepth == CV_32F && sdepth == CV_32S )
1364         return makePtr<ColumnSum<int, float> >(ksize, anchor, scale);
1365     if( ddepth == CV_32F && sdepth == CV_64F )
1366         return makePtr<ColumnSum<double, float> >(ksize, anchor, scale);
1367     if( ddepth == CV_64F && sdepth == CV_32S )
1368         return makePtr<ColumnSum<int, double> >(ksize, anchor, scale);
1369     if( ddepth == CV_64F && sdepth == CV_64F )
1370         return makePtr<ColumnSum<double, double> >(ksize, anchor, scale);
1371
1372     CV_Error_( CV_StsNotImplemented,
1373         ("Unsupported combination of sum format (=%d), and destination format (=%d)",
1374         sumType, dstType));
1375 }
1376
1377
1378 cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize,
1379                     Point anchor, bool normalize, int borderType )
1380 {
1381     int sdepth = CV_MAT_DEPTH(srcType);
1382     int cn = CV_MAT_CN(srcType), sumType = CV_64F;
1383     if( sdepth == CV_8U && CV_MAT_DEPTH(dstType) == CV_8U &&
1384         ksize.width*ksize.height <= 256 )
1385         sumType = CV_16U;
1386     else if( sdepth <= CV_32S && (!normalize ||
1387         ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) :
1388             sdepth == CV_16U ? (1 << 15) : (1 << 16))) )
1389         sumType = CV_32S;
1390     sumType = CV_MAKETYPE( sumType, cn );
1391
1392     Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1393     Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1394         dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1);
1395
1396     return makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1397            srcType, dstType, sumType, borderType );
1398 }
1399
1400 #ifdef HAVE_OPENVX
1401 namespace cv
1402 {
1403     namespace ovx {
1404         template <> inline bool skipSmallImages<VX_KERNEL_BOX_3x3>(int w, int h) { return w*h < 640 * 480; }
1405     }
1406     static bool openvx_boxfilter(InputArray _src, OutputArray _dst, int ddepth,
1407                                  Size ksize, Point anchor,
1408                                  bool normalize, int borderType)
1409     {
1410         if (ddepth < 0)
1411             ddepth = CV_8UC1;
1412         if (_src.type() != CV_8UC1 || ddepth != CV_8U || !normalize ||
1413             _src.cols() < 3 || _src.rows() < 3 ||
1414             ksize.width != 3 || ksize.height != 3 ||
1415             (anchor.x >= 0 && anchor.x != 1) ||
1416             (anchor.y >= 0 && anchor.y != 1) ||
1417             ovx::skipSmallImages<VX_KERNEL_BOX_3x3>(_src.cols(), _src.rows()))
1418             return false;
1419
1420         Mat src = _src.getMat();
1421
1422         if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
1423             return false; //Process isolated borders only
1424         vx_enum border;
1425         switch (borderType & ~BORDER_ISOLATED)
1426         {
1427         case BORDER_CONSTANT:
1428             border = VX_BORDER_CONSTANT;
1429             break;
1430         case BORDER_REPLICATE:
1431             border = VX_BORDER_REPLICATE;
1432             break;
1433         default:
1434             return false;
1435         }
1436
1437         _dst.create(src.size(), CV_8UC1);
1438         Mat dst = _dst.getMat();
1439
1440         try
1441         {
1442             ivx::Context ctx = ovx::getOpenVXContext();
1443
1444             Mat a;
1445             if (dst.data != src.data)
1446                 a = src;
1447             else
1448                 src.copyTo(a);
1449
1450             ivx::Image
1451                 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1452                                                   ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
1453                 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1454                                                   ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
1455
1456             //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
1457             //since OpenVX standard says nothing about thread-safety for now
1458             ivx::border_t prevBorder = ctx.immediateBorder();
1459             ctx.setImmediateBorder(border, (vx_uint8)(0));
1460             ivx::IVX_CHECK_STATUS(vxuBox3x3(ctx, ia, ib));
1461             ctx.setImmediateBorder(prevBorder);
1462         }
1463         catch (ivx::RuntimeError & e)
1464         {
1465             VX_DbgThrow(e.what());
1466         }
1467         catch (ivx::WrapperError & e)
1468         {
1469             VX_DbgThrow(e.what());
1470         }
1471
1472         return true;
1473     }
1474 }
1475 #endif
1476
1477 #if defined(HAVE_IPP)
1478 namespace cv
1479 {
1480 static bool ipp_boxfilter(Mat &src, Mat &dst, Size ksize, Point anchor, bool normalize, int borderType)
1481 {
1482 #ifdef HAVE_IPP_IW
1483     CV_INSTRUMENT_REGION_IPP()
1484
1485 #if IPP_VERSION_X100 < 201801
1486     // Problem with SSE42 optimization for 16s and some 8u modes
1487     if(ipp::getIppTopFeatures() == ippCPUID_SSE42 && (((src.depth() == CV_16S || src.depth() == CV_16U) && (src.channels() == 3 || src.channels() == 4)) || (src.depth() == CV_8U && src.channels() == 3 && (ksize.width > 5 || ksize.height > 5))))
1488         return false;
1489
1490     // Other optimizations has some degradations too
1491     if((((src.depth() == CV_16S || src.depth() == CV_16U) && (src.channels() == 4)) || (src.depth() == CV_8U && src.channels() == 1 && (ksize.width > 5 || ksize.height > 5))))
1492         return false;
1493 #endif
1494
1495     if(!normalize)
1496         return false;
1497
1498     if(!ippiCheckAnchor(anchor, ksize))
1499         return false;
1500
1501     try
1502     {
1503         ::ipp::IwiImage       iwSrc      = ippiGetImage(src);
1504         ::ipp::IwiImage       iwDst      = ippiGetImage(dst);
1505         ::ipp::IwiSize        iwKSize    = ippiGetSize(ksize);
1506         ::ipp::IwiBorderSize  borderSize(iwKSize);
1507         ::ipp::IwiBorderType  ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
1508         if(!ippBorder)
1509             return false;
1510
1511         CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBox, iwSrc, iwDst, iwKSize, ::ipp::IwDefault(), ippBorder);
1512     }
1513     catch (::ipp::IwException)
1514     {
1515         return false;
1516     }
1517
1518     return true;
1519 #else
1520     CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(ksize); CV_UNUSED(anchor); CV_UNUSED(normalize); CV_UNUSED(borderType);
1521     return false;
1522 #endif
1523 }
1524 }
1525 #endif
1526
1527
1528 void cv::boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1529                 Size ksize, Point anchor,
1530                 bool normalize, int borderType )
1531 {
1532     CV_INSTRUMENT_REGION()
1533
1534     CV_OCL_RUN(_dst.isUMat() &&
1535                (borderType == BORDER_REPLICATE || borderType == BORDER_CONSTANT ||
1536                 borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101),
1537                ocl_boxFilter3x3_8UC1(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1538
1539     CV_OCL_RUN(_dst.isUMat(), ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1540
1541     Mat src = _src.getMat();
1542     int stype = src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
1543     if( ddepth < 0 )
1544         ddepth = sdepth;
1545     _dst.create( src.size(), CV_MAKETYPE(ddepth, cn) );
1546     Mat dst = _dst.getMat();
1547     if( borderType != BORDER_CONSTANT && normalize && (borderType & BORDER_ISOLATED) != 0 )
1548     {
1549         if( src.rows == 1 )
1550             ksize.height = 1;
1551         if( src.cols == 1 )
1552             ksize.width = 1;
1553     }
1554
1555     Point ofs;
1556     Size wsz(src.cols, src.rows);
1557     if(!(borderType&BORDER_ISOLATED))
1558         src.locateROI( wsz, ofs );
1559
1560     CALL_HAL(boxFilter, cv_hal_boxFilter, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
1561              ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
1562              anchor.x, anchor.y, normalize, borderType&~BORDER_ISOLATED);
1563
1564     CV_OVX_RUN(true,
1565                openvx_boxfilter(src, dst, ddepth, ksize, anchor, normalize, borderType))
1566
1567     CV_IPP_RUN_FAST(ipp_boxfilter(src, dst, ksize, anchor, normalize, borderType));
1568
1569     borderType = (borderType&~BORDER_ISOLATED);
1570
1571     Ptr<FilterEngine> f = createBoxFilter( src.type(), dst.type(),
1572                         ksize, anchor, normalize, borderType );
1573
1574     f->apply( src, dst, wsz, ofs );
1575 }
1576
1577
1578 void cv::blur( InputArray src, OutputArray dst,
1579            Size ksize, Point anchor, int borderType )
1580 {
1581     CV_INSTRUMENT_REGION()
1582
1583     boxFilter( src, dst, -1, ksize, anchor, true, borderType );
1584 }
1585
1586
1587 /****************************************************************************************\
1588                                     Squared Box Filter
1589 \****************************************************************************************/
1590
1591 namespace cv
1592 {
1593
1594 template<typename T, typename ST>
1595 struct SqrRowSum :
1596         public BaseRowFilter
1597 {
1598     SqrRowSum( int _ksize, int _anchor ) :
1599         BaseRowFilter()
1600     {
1601         ksize = _ksize;
1602         anchor = _anchor;
1603     }
1604
1605     virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
1606     {
1607         const T* S = (const T*)src;
1608         ST* D = (ST*)dst;
1609         int i = 0, k, ksz_cn = ksize*cn;
1610
1611         width = (width - 1)*cn;
1612         for( k = 0; k < cn; k++, S++, D++ )
1613         {
1614             ST s = 0;
1615             for( i = 0; i < ksz_cn; i += cn )
1616             {
1617                 ST val = (ST)S[i];
1618                 s += val*val;
1619             }
1620             D[0] = s;
1621             for( i = 0; i < width; i += cn )
1622             {
1623                 ST val0 = (ST)S[i], val1 = (ST)S[i + ksz_cn];
1624                 s += val1*val1 - val0*val0;
1625                 D[i+cn] = s;
1626             }
1627         }
1628     }
1629 };
1630
1631 static Ptr<BaseRowFilter> getSqrRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1632 {
1633     int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1634     CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
1635
1636     if( anchor < 0 )
1637         anchor = ksize/2;
1638
1639     if( sdepth == CV_8U && ddepth == CV_32S )
1640         return makePtr<SqrRowSum<uchar, int> >(ksize, anchor);
1641     if( sdepth == CV_8U && ddepth == CV_64F )
1642         return makePtr<SqrRowSum<uchar, double> >(ksize, anchor);
1643     if( sdepth == CV_16U && ddepth == CV_64F )
1644         return makePtr<SqrRowSum<ushort, double> >(ksize, anchor);
1645     if( sdepth == CV_16S && ddepth == CV_64F )
1646         return makePtr<SqrRowSum<short, double> >(ksize, anchor);
1647     if( sdepth == CV_32F && ddepth == CV_64F )
1648         return makePtr<SqrRowSum<float, double> >(ksize, anchor);
1649     if( sdepth == CV_64F && ddepth == CV_64F )
1650         return makePtr<SqrRowSum<double, double> >(ksize, anchor);
1651
1652     CV_Error_( CV_StsNotImplemented,
1653               ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1654                srcType, sumType));
1655 }
1656
1657 }
1658
1659 void cv::sqrBoxFilter( InputArray _src, OutputArray _dst, int ddepth,
1660                        Size ksize, Point anchor,
1661                        bool normalize, int borderType )
1662 {
1663     CV_INSTRUMENT_REGION()
1664
1665     int srcType = _src.type(), sdepth = CV_MAT_DEPTH(srcType), cn = CV_MAT_CN(srcType);
1666     Size size = _src.size();
1667
1668     if( ddepth < 0 )
1669         ddepth = sdepth < CV_32F ? CV_32F : CV_64F;
1670
1671     if( borderType != BORDER_CONSTANT && normalize )
1672     {
1673         if( size.height == 1 )
1674             ksize.height = 1;
1675         if( size.width == 1 )
1676             ksize.width = 1;
1677     }
1678
1679     CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
1680                ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize, true))
1681
1682     int sumDepth = CV_64F;
1683     if( sdepth == CV_8U )
1684         sumDepth = CV_32S;
1685     int sumType = CV_MAKETYPE( sumDepth, cn ), dstType = CV_MAKETYPE(ddepth, cn);
1686
1687     Mat src = _src.getMat();
1688     _dst.create( size, dstType );
1689     Mat dst = _dst.getMat();
1690
1691     Ptr<BaseRowFilter> rowFilter = getSqrRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1692     Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1693                                                             dstType, ksize.height, anchor.y,
1694                                                             normalize ? 1./(ksize.width*ksize.height) : 1);
1695
1696     Ptr<FilterEngine> f = makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1697                                                 srcType, dstType, sumType, borderType );
1698     Point ofs;
1699     Size wsz(src.cols, src.rows);
1700     src.locateROI( wsz, ofs );
1701
1702     f->apply( src, dst, wsz, ofs );
1703 }
1704
1705
1706 /****************************************************************************************\
1707                                      Gaussian Blur
1708 \****************************************************************************************/
1709
1710 cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
1711 {
1712     CV_Assert(n > 0);
1713     const int SMALL_GAUSSIAN_SIZE = 7;
1714     static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
1715     {
1716         {1.f},
1717         {0.25f, 0.5f, 0.25f},
1718         {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
1719         {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
1720     };
1721
1722     const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
1723         small_gaussian_tab[n>>1] : 0;
1724
1725     CV_Assert( ktype == CV_32F || ktype == CV_64F );
1726     Mat kernel(n, 1, ktype);
1727     float* cf = kernel.ptr<float>();
1728     double* cd = kernel.ptr<double>();
1729
1730     double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
1731     double scale2X = -0.5/(sigmaX*sigmaX);
1732     double sum = 0;
1733
1734     int i;
1735     for( i = 0; i < n; i++ )
1736     {
1737         double x = i - (n-1)*0.5;
1738         double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
1739         if( ktype == CV_32F )
1740         {
1741             cf[i] = (float)t;
1742             sum += cf[i];
1743         }
1744         else
1745         {
1746             cd[i] = t;
1747             sum += cd[i];
1748         }
1749     }
1750
1751     CV_DbgAssert(fabs(sum) > 0);
1752     sum = 1./sum;
1753     for( i = 0; i < n; i++ )
1754     {
1755         if( ktype == CV_32F )
1756             cf[i] = (float)(cf[i]*sum);
1757         else
1758             cd[i] *= sum;
1759     }
1760
1761     return kernel;
1762 }
1763
1764 namespace cv {
1765
1766 template <typename T>
1767 static std::vector<T> getFixedpointGaussianKernel( int n, double sigma )
1768 {
1769     if (sigma <= 0)
1770     {
1771         if(n == 1)
1772             return std::vector<T>(1, softdouble(1.0));
1773         else if(n == 3)
1774         {
1775             T v3[] = { softdouble(0.25), softdouble(0.5), softdouble(0.25) };
1776             return std::vector<T>(v3, v3 + 3);
1777         }
1778         else if(n == 5)
1779         {
1780             T v5[] = { softdouble(0.0625), softdouble(0.25), softdouble(0.375), softdouble(0.25), softdouble(0.0625) };
1781             return std::vector<T>(v5, v5 + 5);
1782         }
1783         else if(n == 7)
1784         {
1785             T v7[] = { softdouble(0.03125), softdouble(0.109375), softdouble(0.21875), softdouble(0.28125), softdouble(0.21875), softdouble(0.109375), softdouble(0.03125) };
1786             return std::vector<T>(v7, v7 + 7);
1787         }
1788     }
1789
1790
1791     softdouble sigmaX = sigma > 0 ? softdouble(sigma) : mulAdd(softdouble(n),softdouble(0.15),softdouble(0.35));// softdouble(((n-1)*0.5 - 1)*0.3 + 0.8)
1792     softdouble scale2X = softdouble(-0.5*0.25)/(sigmaX*sigmaX);
1793     std::vector<softdouble> values(n);
1794     softdouble sum(0.);
1795     for(int i = 0, x = 1 - n; i < n; i++, x+=2 )
1796     {
1797         // x = i - (n - 1)*0.5
1798         // t = std::exp(scale2X*x*x)
1799         values[i] = exp(softdouble(x*x)*scale2X);
1800         sum += values[i];
1801     }
1802     sum = softdouble::one()/sum;
1803
1804     std::vector<T> kernel(n);
1805     for(int i = 0; i < n; i++ )
1806     {
1807         kernel[i] = values[i] * sum;
1808     }
1809
1810     return kernel;
1811 };
1812
1813 template <typename ET, typename FT>
1814 void hlineSmooth1N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int)
1815 {
1816     for (int i = 0; i < len*cn; i++, src++, dst++)
1817         *dst = (*m) * (*src);
1818 }
1819 template <>
1820 void hlineSmooth1N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int)
1821 {
1822     int lencn = len*cn;
1823     v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
1824     int i = 0;
1825     for (; i <= lencn - 16; i += 16)
1826     {
1827         v_uint8x16 v_src = v_load(src + i);
1828         v_uint16x8 v_tmp0, v_tmp1;
1829         v_expand(v_src, v_tmp0, v_tmp1);
1830         v_store((uint16_t*)dst + i, v_mul*v_tmp0);
1831         v_store((uint16_t*)dst + i + 8, v_mul*v_tmp1);
1832     }
1833     if (i <= lencn - 8)
1834     {
1835         v_uint16x8 v_src = v_load_expand(src + i);
1836         v_store((uint16_t*)dst + i, v_mul*v_src);
1837         i += 8;
1838     }
1839     for (; i < lencn; i++)
1840         dst[i] = m[0] * src[i];
1841 }
1842 template <typename ET, typename FT>
1843 void hlineSmooth1N1(const ET* src, int cn, const FT*, int, FT* dst, int len, int)
1844 {
1845     for (int i = 0; i < len*cn; i++, src++, dst++)
1846         *dst = *src;
1847 }
1848 template <>
1849 void hlineSmooth1N1<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int)
1850 {
1851     int lencn = len*cn;
1852     int i = 0;
1853     for (; i <= lencn - 16; i += 16)
1854     {
1855         v_uint8x16 v_src = v_load(src + i);
1856         v_uint16x8 v_tmp0, v_tmp1;
1857         v_expand(v_src, v_tmp0, v_tmp1);
1858         v_store((uint16_t*)dst + i, v_shl<8>(v_tmp0));
1859         v_store((uint16_t*)dst + i + 8, v_shl<8>(v_tmp1));
1860     }
1861     if (i <= lencn - 8)
1862     {
1863         v_uint16x8 v_src = v_load_expand(src + i);
1864         v_store((uint16_t*)dst + i, v_shl<8>(v_src));
1865         i += 8;
1866     }
1867     for (; i < lencn; i++)
1868         dst[i] = src[i];
1869 }
1870 template <typename ET, typename FT>
1871 void hlineSmooth3N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
1872 {
1873     if (len == 1)
1874     {
1875         FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1876         for (int k = 0; k < cn; k++)
1877             dst[k] = msum * src[k];
1878     }
1879     else
1880     {
1881         // Point that fall left from border
1882         for (int k = 0; k < cn; k++)
1883             dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1884         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1885         {
1886             int src_idx = borderInterpolate(-1, len, borderType);
1887             for (int k = 0; k < cn; k++)
1888                 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1889         }
1890
1891         src += cn; dst += cn;
1892         for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1893             *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1894
1895         // Point that fall right from border
1896         for (int k = 0; k < cn; k++)
1897             dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1898         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1899         {
1900             int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1901             for (int k = 0; k < cn; k++)
1902                 dst[k] = dst[k] + m[2] * src[src_idx + k];
1903         }
1904     }
1905 }
1906 template <>
1907 void hlineSmooth3N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
1908 {
1909     if (len == 1)
1910     {
1911         ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1912         for (int k = 0; k < cn; k++)
1913             dst[k] = msum * src[k];
1914     }
1915     else
1916     {
1917         // Point that fall left from border
1918         for (int k = 0; k < cn; k++)
1919             dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1920         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1921         {
1922             int src_idx = borderInterpolate(-1, len, borderType);
1923             for (int k = 0; k < cn; k++)
1924                 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1925         }
1926
1927         src += cn; dst += cn;
1928         int i = cn, lencn = (len - 1)*cn;
1929         v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
1930         v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
1931         v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
1932         for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
1933         {
1934             v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
1935             v_expand(v_load(src - cn), v_src00, v_src01);
1936             v_expand(v_load(src), v_src10, v_src11);
1937             v_expand(v_load(src + cn), v_src20, v_src21);
1938             v_store((uint16_t*)dst, v_src00 * v_mul0 + v_src10 * v_mul1 + v_src20 * v_mul2);
1939             v_store((uint16_t*)dst + 8, v_src01 * v_mul0 + v_src11 * v_mul1 + v_src21 * v_mul2);
1940         }
1941         for (; i < lencn; i++, src++, dst++)
1942             *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1943
1944         // Point that fall right from border
1945         for (int k = 0; k < cn; k++)
1946             dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1947         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1948         {
1949             int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1950             for (int k = 0; k < cn; k++)
1951                 dst[k] = dst[k] + m[2] * src[src_idx + k];
1952         }
1953     }
1954 }
1955 template <typename ET, typename FT>
1956 void hlineSmooth3N121(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
1957 {
1958     if (len == 1)
1959     {
1960         if(borderType != BORDER_CONSTANT)
1961             for (int k = 0; k < cn; k++)
1962                 dst[k] = FT(src[k]);
1963         else
1964             for (int k = 0; k < cn; k++)
1965                 dst[k] = FT(src[k])>>1;
1966     }
1967     else
1968     {
1969         // Point that fall left from border
1970         for (int k = 0; k < cn; k++)
1971             dst[k] = (FT(src[k])>>1) + (FT(src[cn + k])>>2);
1972         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1973         {
1974             int src_idx = borderInterpolate(-1, len, borderType);
1975             for (int k = 0; k < cn; k++)
1976                 dst[k] = dst[k] + (FT(src[src_idx*cn + k])>>2);
1977         }
1978
1979         src += cn; dst += cn;
1980         for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1981             *dst = (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[0])>>1);
1982
1983         // Point that fall right from border
1984         for (int k = 0; k < cn; k++)
1985             dst[k] = (FT(src[k - cn])>>2) + (FT(src[k])>>1);
1986         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1987         {
1988             int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1989             for (int k = 0; k < cn; k++)
1990                 dst[k] = dst[k] + (FT(src[src_idx + k])>>2);
1991         }
1992     }
1993 }
1994 template <>
1995 void hlineSmooth3N121<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
1996 {
1997     if (len == 1)
1998     {
1999         if (borderType != BORDER_CONSTANT)
2000             for (int k = 0; k < cn; k++)
2001                 dst[k] = ufixedpoint16(src[k]);
2002         else
2003             for (int k = 0; k < cn; k++)
2004                 dst[k] = ufixedpoint16(src[k]) >> 1;
2005     }
2006     else
2007     {
2008         // Point that fall left from border
2009         for (int k = 0; k < cn; k++)
2010             dst[k] = (ufixedpoint16(src[k])>>1) + (ufixedpoint16(src[cn + k])>>2);
2011         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2012         {
2013             int src_idx = borderInterpolate(-1, len, borderType);
2014             for (int k = 0; k < cn; k++)
2015                 dst[k] = dst[k] + (ufixedpoint16(src[src_idx*cn + k])>>2);
2016         }
2017
2018         src += cn; dst += cn;
2019         int i = cn, lencn = (len - 1)*cn;
2020         for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2021         {
2022             v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2023             v_expand(v_load(src - cn), v_src00, v_src01);
2024             v_expand(v_load(src), v_src10, v_src11);
2025             v_expand(v_load(src + cn), v_src20, v_src21);
2026             v_store((uint16_t*)dst, (v_src00 + v_src20 + (v_src10 << 1)) << 6);
2027             v_store((uint16_t*)dst + 8, (v_src01 + v_src21 + (v_src11 << 1)) << 6);
2028         }
2029         for (; i < lencn; i++, src++, dst++)
2030             *((uint16_t*)dst) = (uint16_t(src[-cn]) + uint16_t(src[cn]) + (uint16_t(src[0]) << 1)) << 6;
2031
2032         // Point that fall right from border
2033         for (int k = 0; k < cn; k++)
2034             dst[k] = (ufixedpoint16(src[k - cn])>>2) + (ufixedpoint16(src[k])>>1);
2035         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2036         {
2037             int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2038             for (int k = 0; k < cn; k++)
2039                 dst[k] = dst[k] + (ufixedpoint16(src[src_idx + k])>>2);
2040         }
2041     }
2042 }
2043 template <typename ET, typename FT>
2044 void hlineSmooth3Naba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2045 {
2046     if (len == 1)
2047     {
2048         FT msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2049         for (int k = 0; k < cn; k++)
2050             dst[k] = msum * src[k];
2051     }
2052     else
2053     {
2054         // Point that fall left from border
2055         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2056         {
2057             int src_idx = borderInterpolate(-1, len, borderType);
2058             for (int k = 0; k < cn; k++)
2059                 dst[k] = m[1] * src[k] + m[0] * src[cn + k] + m[0] * src[src_idx*cn + k];
2060         }
2061         else
2062         {
2063             for (int k = 0; k < cn; k++)
2064                 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2065         }
2066
2067         src += cn; dst += cn;
2068         for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
2069             *dst = m[1] * src[0] + m[0] * src[-cn] + m[0] * src[cn];
2070
2071         // Point that fall right from border
2072         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2073         {
2074             int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2075             for (int k = 0; k < cn; k++)
2076                 dst[k] = m[1] * src[k] + m[0] * src[k - cn] + m[0] * src[src_idx + k];
2077         }
2078         else
2079         {
2080             for (int k = 0; k < cn; k++)
2081                 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2082         }
2083     }
2084 }
2085 template <>
2086 void hlineSmooth3Naba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2087 {
2088     if (len == 1)
2089     {
2090         ufixedpoint16 msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2091         for (int k = 0; k < cn; k++)
2092             dst[k] = msum * src[k];
2093     }
2094     else
2095     {
2096         // Point that fall left from border
2097         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2098         {
2099             int src_idx = borderInterpolate(-1, len, borderType);
2100             for (int k = 0; k < cn; k++)
2101                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[1] * src[k] + ((uint16_t*)m)[0] * ((uint16_t)(src[cn + k]) + (uint16_t)(src[src_idx*cn + k]));
2102         }
2103         else
2104         {
2105             for (int k = 0; k < cn; k++)
2106                 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2107         }
2108
2109         src += cn; dst += cn;
2110         int i = cn, lencn = (len - 1)*cn;
2111         v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2112         v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)m+1));
2113         for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2114         {
2115             v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2116             v_expand(v_load(src - cn), v_src00, v_src01);
2117             v_expand(v_load(src), v_src10, v_src11);
2118             v_expand(v_load(src + cn), v_src20, v_src21);
2119             v_store((uint16_t*)dst, (v_src00 + v_src20) * v_mul0 + v_src10 * v_mul1);
2120             v_store((uint16_t*)dst + 8, (v_src01 + v_src21) * v_mul0 + v_src11 * v_mul1);
2121         }
2122         for (; i < lencn; i++, src++, dst++)
2123             *((uint16_t*)dst) = ((uint16_t*)m)[1] * src[0] + ((uint16_t*)m)[0] * ((uint16_t)(src[-cn]) + (uint16_t)(src[cn]));
2124
2125         // Point that fall right from border
2126         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2127         {
2128             int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2129             for (int k = 0; k < cn; k++)
2130                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[1] * src[k] + ((uint16_t*)m)[0] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[src_idx + k]));
2131         }
2132         else
2133         {
2134             for (int k = 0; k < cn; k++)
2135                 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2136         }
2137     }
2138 }
2139 template <typename ET, typename FT>
2140 void hlineSmooth5N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2141 {
2142     if (len == 1)
2143     {
2144         FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2145         for (int k = 0; k < cn; k++)
2146             dst[k] = msum * src[k];
2147     }
2148     else if (len == 2)
2149     {
2150         if (borderType == BORDER_CONSTANT)
2151             for (int k = 0; k < cn; k++)
2152             {
2153                 dst[k   ] = m[2] * src[k] + m[3] * src[k+cn];
2154                 dst[k+cn] = m[1] * src[k] + m[2] * src[k+cn];
2155             }
2156         else
2157         {
2158             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2159             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2160             int idxp1 = borderInterpolate(2, len, borderType)*cn;
2161             int idxp2 = borderInterpolate(3, len, borderType)*cn;
2162             for (int k = 0; k < cn; k++)
2163             {
2164                 dst[k     ] = m[1] * src[k + idxm1] + m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + idxp1] + m[0] * src[k + idxm2];
2165                 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2166             }
2167         }
2168     }
2169     else if (len == 3)
2170     {
2171         if (borderType == BORDER_CONSTANT)
2172             for (int k = 0; k < cn; k++)
2173             {
2174                 dst[k       ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn];
2175                 dst[k +   cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn];
2176                 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn];
2177             }
2178         else
2179         {
2180             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2181             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2182             int idxp1 = borderInterpolate(3, len, borderType)*cn;
2183             int idxp2 = borderInterpolate(4, len, borderType)*cn;
2184             for (int k = 0; k < cn; k++)
2185             {
2186                 dst[k       ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2187                 dst[k +   cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn] + m[0] * src[k + idxm1] + m[4] * src[k + idxp1];
2188                 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2189             }
2190         }
2191     }
2192     else
2193     {
2194         // Points that fall left from border
2195         for (int k = 0; k < cn; k++)
2196         {
2197             dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2*cn + k];
2198             dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2*cn + k] + m[4] * src[3*cn + k];
2199         }
2200         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2201         {
2202             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2203             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2204             for (int k = 0; k < cn; k++)
2205             {
2206                 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2207                 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2208             }
2209         }
2210
2211         src += 2*cn; dst += 2*cn;
2212         for (int i = 2*cn; i < (len - 2)*cn; i++, src++, dst++)
2213             *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2214
2215         // Points that fall right from border
2216         for (int k = 0; k < cn; k++)
2217         {
2218             dst[k] = m[0] * src[k - 2*cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2219             dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2220         }
2221         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2222         {
2223             int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2224             int idxp2 = (borderInterpolate(len+1, len, borderType) - (len - 2))*cn;
2225             for (int k = 0; k < cn; k++)
2226             {
2227                 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2228                 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2229             }
2230         }
2231     }
2232 }
2233 template <>
2234 void hlineSmooth5N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2235 {
2236     if (len == 1)
2237     {
2238         ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2239         for (int k = 0; k < cn; k++)
2240             dst[k] = msum * src[k];
2241     }
2242     else if (len == 2)
2243     {
2244         if (borderType == BORDER_CONSTANT)
2245             for (int k = 0; k < cn; k++)
2246             {
2247                 dst[k] = m[2] * src[k] + m[3] * src[k + cn];
2248                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2249             }
2250         else
2251         {
2252             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2253             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2254             int idxp1 = borderInterpolate(2, len, borderType)*cn;
2255             int idxp2 = borderInterpolate(3, len, borderType)*cn;
2256             for (int k = 0; k < cn; k++)
2257             {
2258                 dst[k] = m[1] * src[k + idxm1] + m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + idxp1] + m[0] * src[k + idxm2];
2259                 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2260             }
2261         }
2262     }
2263     else if (len == 3)
2264     {
2265         if (borderType == BORDER_CONSTANT)
2266             for (int k = 0; k < cn; k++)
2267             {
2268                 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn];
2269                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn];
2270                 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2271             }
2272         else
2273         {
2274             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2275             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2276             int idxp1 = borderInterpolate(3, len, borderType)*cn;
2277             int idxp2 = borderInterpolate(4, len, borderType)*cn;
2278             for (int k = 0; k < cn; k++)
2279             {
2280                 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2281                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn] + m[0] * src[k + idxm1] + m[4] * src[k + idxp1];
2282                 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2283             }
2284         }
2285     }
2286     else
2287     {
2288         // Points that fall left from border
2289         for (int k = 0; k < cn; k++)
2290         {
2291             dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2 * cn + k];
2292             dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2 * cn + k] + m[4] * src[3 * cn + k];
2293         }
2294         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2295         {
2296             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2297             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2298             for (int k = 0; k < cn; k++)
2299             {
2300                 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2301                 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2302             }
2303         }
2304
2305         src += 2 * cn; dst += 2 * cn;
2306         int i = 2*cn, lencn = (len - 2)*cn;
2307         v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2308         v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2309         v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2310         v_uint16x8 v_mul3 = v_setall_u16(*((uint16_t*)(m + 3)));
2311         v_uint16x8 v_mul4 = v_setall_u16(*((uint16_t*)(m + 4)));
2312         for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2313         {
2314             v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2315             v_expand(v_load(src - 2*cn), v_src00, v_src01);
2316             v_expand(v_load(src - cn), v_src10, v_src11);
2317             v_expand(v_load(src), v_src20, v_src21);
2318             v_expand(v_load(src + cn), v_src30, v_src31);
2319             v_expand(v_load(src + 2*cn), v_src40, v_src41);
2320             v_store((uint16_t*)dst, v_src00 * v_mul0 + v_src10 * v_mul1 + v_src20 * v_mul2 + v_src30 * v_mul3 + v_src40 * v_mul4);
2321             v_store((uint16_t*)dst + 8, v_src01 * v_mul0 + v_src11 * v_mul1 + v_src21 * v_mul2 + v_src31 * v_mul3 + v_src41 * v_mul4);
2322         }
2323         for (; i < lencn; i++, src++, dst++)
2324             *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2325
2326         // Points that fall right from border
2327         for (int k = 0; k < cn; k++)
2328         {
2329             dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2330             dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2331         }
2332         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2333         {
2334             int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2335             int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2336             for (int k = 0; k < cn; k++)
2337             {
2338                 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2339                 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2340             }
2341         }
2342     }
2343 }
2344 template <typename ET, typename FT>
2345 void hlineSmooth5N14641(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
2346 {
2347     if (len == 1)
2348     {
2349         if (borderType == BORDER_CONSTANT)
2350             for (int k = 0; k < cn; k++)
2351                 dst[k] = (FT(src[k])>>3)*(uint8_t)3;
2352         else
2353             for (int k = 0; k < cn; k++)
2354                 dst[k] = src[k];
2355     }
2356     else if (len == 2)
2357     {
2358         if (borderType == BORDER_CONSTANT)
2359             for (int k = 0; k < cn; k++)
2360             {
2361                 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2);
2362                 dst[k + cn] = (FT(src[k]) >> 2) + (FT(src[k + cn])>>4)*(uint8_t)6;
2363             }
2364         else
2365         {
2366             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2367             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2368             int idxp1 = borderInterpolate(2, len, borderType)*cn;
2369             int idxp2 = borderInterpolate(3, len, borderType)*cn;
2370             for (int k = 0; k < cn; k++)
2371             {
2372                 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + idxm1])>>2) + (FT(src[k + cn])>>2) + (FT(src[k + idxp1])>>4) + (FT(src[k + idxm2])>>4);
2373                 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + idxp1])>>2) + (FT(src[k + idxm1])>>4) + (FT(src[k + idxp2])>>4);
2374             }
2375         }
2376     }
2377     else if (len == 3)
2378     {
2379         if (borderType == BORDER_CONSTANT)
2380             for (int k = 0; k < cn; k++)
2381             {
2382                 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + 2 * cn])>>4);
2383                 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2);
2384                 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k])>>4);
2385             }
2386         else
2387         {
2388             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2389             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2390             int idxp1 = borderInterpolate(3, len, borderType)*cn;
2391             int idxp2 = borderInterpolate(4, len, borderType)*cn;
2392             for (int k = 0; k < cn; k++)
2393             {
2394                 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + idxm1])>>2) + (FT(src[k + 2 * cn])>>4) + (FT(src[k + idxm2])>>4);
2395                 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2) + (FT(src[k + idxm1])>>4) + (FT(src[k + idxp1])>>4);
2396                 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + idxp1])>>2) + (FT(src[k])>>4) + (FT(src[k + idxp2])>>4);
2397             }
2398         }
2399     }
2400     else
2401     {
2402         // Points that fall left from border
2403         for (int k = 0; k < cn; k++)
2404         {
2405             dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[cn + k])>>2) + (FT(src[2 * cn + k])>>4);
2406             dst[k + cn] = (FT(src[cn + k])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[2 * cn + k])>>2) + (FT(src[3 * cn + k])>>4);
2407         }
2408         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2409         {
2410             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2411             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2412             for (int k = 0; k < cn; k++)
2413             {
2414                 dst[k] = dst[k] + (FT(src[idxm2 + k])>>4) + (FT(src[idxm1 + k])>>2);
2415                 dst[k + cn] = dst[k + cn] + (FT(src[idxm1 + k])>>4);
2416             }
2417         }
2418
2419         src += 2 * cn; dst += 2 * cn;
2420         for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2421             *dst = (FT(src[0])>>4)*(uint8_t)6 + (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[-2 * cn])>>4) + (FT(src[2 * cn])>>4);
2422
2423         // Points that fall right from border
2424         for (int k = 0; k < cn; k++)
2425         {
2426             dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k - cn])>>2) + (FT(src[k + cn])>>2) + (FT(src[k - 2 * cn])>>4);
2427             dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k - cn])>>4);
2428         }
2429         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2430         {
2431             int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2432             int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2433             for (int k = 0; k < cn; k++)
2434             {
2435                 dst[k] = dst[k] + (FT(src[idxp1 + k])>>4);
2436                 dst[k + cn] = dst[k + cn] + (FT(src[idxp1 + k])>>2) + (FT(src[idxp2 + k])>>4);
2437             }
2438         }
2439     }
2440 }
2441 template <>
2442 void hlineSmooth5N14641<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2443 {
2444     if (len == 1)
2445     {
2446         if (borderType == BORDER_CONSTANT)
2447             for (int k = 0; k < cn; k++)
2448                 dst[k] = (ufixedpoint16(src[k])>>3) * (uint8_t)3;
2449         else
2450         {
2451             for (int k = 0; k < cn; k++)
2452                 dst[k] = src[k];
2453         }
2454     }
2455     else if (len == 2)
2456     {
2457         if (borderType == BORDER_CONSTANT)
2458             for (int k = 0; k < cn; k++)
2459             {
2460                 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2);
2461                 dst[k + cn] = (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6;
2462             }
2463         else
2464         {
2465             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2466             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2467             int idxp1 = borderInterpolate(2, len, borderType)*cn;
2468             int idxp2 = borderInterpolate(3, len, borderType)*cn;
2469             for (int k = 0; k < cn; k++)
2470             {
2471                 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + idxm1]) >> 2) + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 4) + (ufixedpoint16(src[k + idxm2]) >> 4);
2472                 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 4) + (ufixedpoint16(src[k + idxp2]) >> 4);
2473             }
2474         }
2475     }
2476     else if (len == 3)
2477     {
2478         if (borderType == BORDER_CONSTANT)
2479             for (int k = 0; k < cn; k++)
2480             {
2481                 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4);
2482                 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2);
2483                 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k]) >> 4);
2484             }
2485         else
2486         {
2487             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2488             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2489             int idxp1 = borderInterpolate(3, len, borderType)*cn;
2490             int idxp2 = borderInterpolate(4, len, borderType)*cn;
2491             for (int k = 0; k < cn; k++)
2492             {
2493                 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4) + (ufixedpoint16(src[k + idxm2]) >> 4);
2494                 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 4) + (ufixedpoint16(src[k + idxp1]) >> 4);
2495                 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 2) + (ufixedpoint16(src[k]) >> 4) + (ufixedpoint16(src[k + idxp2]) >> 4);
2496             }
2497         }
2498     }
2499     else
2500     {
2501         // Points that fall left from border
2502         for (int k = 0; k < cn; k++)
2503         {
2504             dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[cn + k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 4);
2505             dst[k + cn] = (ufixedpoint16(src[cn + k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 2) + (ufixedpoint16(src[3 * cn + k]) >> 4);
2506         }
2507         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2508         {
2509             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2510             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2511             for (int k = 0; k < cn; k++)
2512             {
2513                 dst[k] = dst[k] + (ufixedpoint16(src[idxm2 + k]) >> 4) + (ufixedpoint16(src[idxm1 + k]) >> 2);
2514                 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxm1 + k]) >> 4);
2515             }
2516         }
2517
2518         src += 2 * cn; dst += 2 * cn;
2519         int i = 2 * cn, lencn = (len - 2)*cn;
2520         v_uint16x8 v_6 = v_setall_u16(6);
2521         for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2522         {
2523             v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2524             v_expand(v_load(src - 2*cn), v_src00, v_src01);
2525             v_expand(v_load(src - cn), v_src10, v_src11);
2526             v_expand(v_load(src), v_src20, v_src21);
2527             v_expand(v_load(src + cn), v_src30, v_src31);
2528             v_expand(v_load(src + 2*cn), v_src40, v_src41);
2529             v_store((uint16_t*)dst, (v_src20 * v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40) << 4);
2530             v_store((uint16_t*)dst + 8, (v_src21 * v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41) << 4);
2531         }
2532         for (; i < lencn; i++, src++, dst++)
2533             *((uint16_t*)dst) = (uint16_t(src[0]) * 6 + ((uint16_t(src[-cn]) + uint16_t(src[cn])) << 2) + uint16_t(src[-2 * cn]) + uint16_t(src[2 * cn])) << 4;
2534
2535         // Points that fall right from border
2536         for (int k = 0; k < cn; k++)
2537         {
2538             dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k - cn]) >> 2) + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k - 2 * cn]) >> 4);
2539             dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k - cn]) >> 4);
2540         }
2541         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2542         {
2543             int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2544             int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2545             for (int k = 0; k < cn; k++)
2546             {
2547                 dst[k] = dst[k] + (ufixedpoint16(src[idxp1 + k]) >> 4);
2548                 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxp1 + k]) >> 2) + (ufixedpoint16(src[idxp2 + k]) >> 4);
2549             }
2550         }
2551     }
2552 }
2553 template <typename ET, typename FT>
2554 void hlineSmooth5Nabcba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2555 {
2556     if (len == 1)
2557     {
2558         FT msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1])<<1) + m[2] : m[2];
2559         for (int k = 0; k < cn; k++)
2560             dst[k] = msum * src[k];
2561     }
2562     else if (len == 2)
2563     {
2564         if (borderType == BORDER_CONSTANT)
2565             for (int k = 0; k < cn; k++)
2566             {
2567                 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2568                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2569             }
2570         else
2571         {
2572             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2573             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2574             int idxp1 = borderInterpolate(2, len, borderType)*cn;
2575             int idxp2 = borderInterpolate(3, len, borderType)*cn;
2576             for (int k = 0; k < cn; k++)
2577             {
2578                 dst[k] = m[1] * src[k + idxm1] + m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + idxp1] + m[0] * src[k + idxm2];
2579                 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + idxp1] + m[0] * src[k + idxp2];
2580             }
2581         }
2582     }
2583     else if (len == 3)
2584     {
2585         if (borderType == BORDER_CONSTANT)
2586             for (int k = 0; k < cn; k++)
2587             {
2588                 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2589                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + 2 * cn];
2590                 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2591             }
2592         else
2593         {
2594             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2595             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2596             int idxp1 = borderInterpolate(3, len, borderType)*cn;
2597             int idxp2 = borderInterpolate(4, len, borderType)*cn;
2598             for (int k = 0; k < cn; k++)
2599             {
2600                 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2601                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + 2 * cn] + m[0] * src[k + idxm1] + m[0] * src[k + idxp1];
2602                 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn] + m[1] * src[k + idxp1] + m[0] * src[k + idxp2];
2603             }
2604         }
2605     }
2606     else
2607     {
2608         // Points that fall left from border
2609         for (int k = 0; k < cn; k++)
2610         {
2611             dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2612             dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[1] * src[2 * cn + k] + m[0] * src[3 * cn + k];
2613         }
2614         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2615         {
2616             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2617             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2618             for (int k = 0; k < cn; k++)
2619             {
2620                 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2621                 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2622             }
2623         }
2624
2625         src += 2 * cn; dst += 2 * cn;
2626         for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2627             *dst = m[0] * src[-2 * cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2 * cn];
2628
2629         // Points that fall right from border
2630         for (int k = 0; k < cn; k++)
2631         {
2632             dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2633             dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2634         }
2635         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2636         {
2637             int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2638             int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2639             for (int k = 0; k < cn; k++)
2640             {
2641                 dst[k] = dst[k] + m[0] * src[idxp1 + k];
2642                 dst[k + cn] = dst[k + cn] + m[1] * src[idxp1 + k] + m[0] * src[idxp2 + k];
2643             }
2644         }
2645     }
2646 }
2647 template <>
2648 void hlineSmooth5Nabcba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2649 {
2650     if (len == 1)
2651     {
2652         ufixedpoint16 msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1]) << 1) + m[2] : m[2];
2653         for (int k = 0; k < cn; k++)
2654             dst[k] = msum * src[k];
2655     }
2656     else if (len == 2)
2657     {
2658         if (borderType == BORDER_CONSTANT)
2659             for (int k = 0; k < cn; k++)
2660             {
2661                 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2662                 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2663             }
2664         else
2665         {
2666             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2667             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2668             int idxp1 = borderInterpolate(2, len, borderType)*cn;
2669             int idxp2 = borderInterpolate(3, len, borderType)*cn;
2670             for (int k = 0; k < cn; k++)
2671             {
2672                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[1] * ((uint16_t)(src[k + idxm1]) + (uint16_t)(src[k + cn])) + ((uint16_t*)m)[2] * src[k] + ((uint16_t*)m)[0] * ((uint16_t)(src[k + idxp1]) + (uint16_t)(src[k + idxm2]));
2673                 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[0] * ((uint16_t)(src[k + idxm1]) + (uint16_t)(src[k + idxp2])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[k + idxp1])) + ((uint16_t*)m)[2] * src[k + cn];
2674             }
2675         }
2676     }
2677     else if (len == 3)
2678     {
2679         if (borderType == BORDER_CONSTANT)
2680             for (int k = 0; k < cn; k++)
2681             {
2682                 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2683                 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[k + 2 * cn])) + ((uint16_t*)m)[2] * src[k + cn];
2684                 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2685             }
2686         else
2687         {
2688             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2689             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2690             int idxp1 = borderInterpolate(3, len, borderType)*cn;
2691             int idxp2 = borderInterpolate(4, len, borderType)*cn;
2692             for (int k = 0; k < cn; k++)
2693             {
2694                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[2] * src[k] + ((uint16_t*)m)[1] * ((uint16_t)(src[k + cn]) + (uint16_t)(src[k + idxm1])) + ((uint16_t*)m)[0] * ((uint16_t)(src[k + 2 * cn]) + (uint16_t)(src[k + idxm2]));
2695                 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[2] * src[k + cn] + ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[k + 2 * cn])) + ((uint16_t*)m)[0] * ((uint16_t)(src[k + idxm1]) + (uint16_t)(src[k + idxp1]));
2696                 ((uint16_t*)dst)[k + 2 * cn] = ((uint16_t*)m)[0] * ((uint16_t)(src[k]) + (uint16_t)(src[k + idxp2])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k + cn]) + (uint16_t)(src[k + idxp1])) + ((uint16_t*)m)[2] * src[k + 2 * cn];
2697             }
2698         }
2699     }
2700     else
2701     {
2702         // Points that fall left from border
2703         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2704         {
2705             int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2706             int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2707             for (int k = 0; k < cn; k++)
2708             {
2709                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[2] * src[k] + ((uint16_t*)m)[1] * ((uint16_t)(src[cn + k]) + (uint16_t)(src[idxm1 + k])) + ((uint16_t*)m)[0] * ((uint16_t)(src[2 * cn + k]) + (uint16_t)(src[idxm2 + k]));
2710                 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[2 * cn + k])) + ((uint16_t*)m)[2] * src[cn + k] + ((uint16_t*)m)[0] * ((uint16_t)(src[3 * cn + k]) + (uint16_t)(src[idxm1 + k]));
2711             }
2712         }
2713         else
2714         {
2715             for (int k = 0; k < cn; k++)
2716             {
2717                 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2718                 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[2 * cn + k])) + ((uint16_t*)m)[2] * src[cn + k] + ((uint16_t*)m)[0] * src[3 * cn + k];
2719             }
2720         }
2721
2722         src += 2 * cn; dst += 2 * cn;
2723         int i = 2 * cn, lencn = (len - 2)*cn;
2724         v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2725         v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2726         v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2727         for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2728         {
2729             v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2730             v_expand(v_load(src - 2 * cn), v_src00, v_src01);
2731             v_expand(v_load(src - cn), v_src10, v_src11);
2732             v_expand(v_load(src), v_src20, v_src21);
2733             v_expand(v_load(src + cn), v_src30, v_src31);
2734             v_expand(v_load(src + 2 * cn), v_src40, v_src41);
2735             v_store((uint16_t*)dst, (v_src00 + v_src40) * v_mul0 + (v_src10 + v_src30)* v_mul1 + v_src20 * v_mul2);
2736             v_store((uint16_t*)dst + 8, (v_src01 + v_src41) * v_mul0 + (v_src11 + v_src31) * v_mul1 + v_src21 * v_mul2);
2737         }
2738         for (; i < lencn; i++, src++, dst++)
2739             *((uint16_t*)dst) = ((uint16_t*)m)[0] * ((uint16_t)(src[-2 * cn]) + (uint16_t)(src[2 * cn])) + ((uint16_t*)m)[1] * ((uint16_t)(src[-cn]) + (uint16_t)(src[cn])) + ((uint16_t*)m)[2] * src[0];
2740
2741         // Points that fall right from border
2742         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2743         {
2744             int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2745             int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2746             for (int k = 0; k < cn; k++)
2747             {
2748                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[0] * ((uint16_t)(src[k - 2 * cn]) + (uint16_t)(src[idxp1 + k])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[k + cn])) + ((uint16_t*)m)[2] * src[k];
2749                 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[0] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[idxp2 + k])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[idxp1 + k])) + ((uint16_t*)m)[2] * src[k + cn];
2750             }
2751         }
2752         else
2753         {
2754             for (int k = 0; k < cn; k++)
2755             {
2756                 ((uint16_t*)dst)[k] = ((uint16_t*)m)[0] * src[k - 2 * cn] + ((uint16_t*)m)[1] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[k + cn])) + ((uint16_t*)m)[2] * src[k];
2757                 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2758             }
2759         }
2760     }
2761 }
2762 template <typename ET, typename FT>
2763 void hlineSmooth(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2764 {
2765     int pre_shift = n / 2;
2766     int post_shift = n - pre_shift;
2767     int i = 0;
2768     for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2769     {
2770         for (int k = 0; k < cn; k++)
2771             dst[k] = m[pre_shift-i] * src[k];
2772         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2773             for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2774             {
2775                 int src_idx = borderInterpolate(j, len, borderType);
2776                 for (int k = 0; k < cn; k++)
2777                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2778             }
2779         int j, mid;
2780         for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2781             for (int k = 0; k < cn; k++)
2782                 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2783         if (borderType != BORDER_CONSTANT)
2784             for (; j < i + post_shift; j++, mid++)
2785             {
2786                 int src_idx = borderInterpolate(j, len, borderType);
2787                 for (int k = 0; k < cn; k++)
2788                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2789             }
2790     }
2791     i *= cn;
2792     for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2793     {
2794         *dst = m[0] * src[0];
2795         for (int j = 1; j < n; j++)
2796             *dst = *dst + m[j] * src[j*cn];
2797     }
2798     i /= cn;
2799     for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2800     {
2801         for (int k = 0; k < cn; k++)
2802             dst[k] = m[0] * src[k];
2803         int j = 1;
2804         for (; j < len - i; j++)
2805             for (int k = 0; k < cn; k++)
2806                 dst[k] = dst[k] + m[j] * src[j*cn + k];
2807         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2808             for (; j < n; j++)
2809             {
2810                 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2811                 for (int k = 0; k < cn; k++)
2812                     dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2813             }
2814     }
2815 }
2816 template <>
2817 void hlineSmooth<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2818 {
2819     int pre_shift = n / 2;
2820     int post_shift = n - pre_shift;
2821     int i = 0;
2822     for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2823     {
2824         for (int k = 0; k < cn; k++)
2825             dst[k] = m[pre_shift - i] * src[k];
2826         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2827             for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2828             {
2829                 int src_idx = borderInterpolate(j, len, borderType);
2830                 for (int k = 0; k < cn; k++)
2831                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2832             }
2833         int j, mid;
2834         for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2835             for (int k = 0; k < cn; k++)
2836                 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2837         if (borderType != BORDER_CONSTANT)
2838             for (; j < i + post_shift; j++, mid++)
2839             {
2840                 int src_idx = borderInterpolate(j, len, borderType);
2841                 for (int k = 0; k < cn; k++)
2842                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2843             }
2844     }
2845     i *= cn;
2846     int lencn = (len - post_shift + 1)*cn;
2847     for (; i <= lencn - 16; i+=16, src+=16, dst+=16)
2848     {
2849         v_uint16x8 v_src0, v_src1;
2850         v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
2851         v_expand(v_load(src), v_src0, v_src1);
2852         v_uint16x8 v_res0 = v_src0 * v_mul;
2853         v_uint16x8 v_res1 = v_src1 * v_mul;
2854         for (int j = 1; j < n; j++)
2855         {
2856             v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2857             v_expand(v_load(src + j * cn), v_src0, v_src1);
2858             v_res0 += v_src0 * v_mul;
2859             v_res1 += v_src1 * v_mul;
2860         }
2861         v_store((uint16_t*)dst, v_res0);
2862         v_store((uint16_t*)dst+8, v_res1);
2863     }
2864     for (; i < lencn; i++, src++, dst++)
2865     {
2866             *dst = m[0] * src[0];
2867             for (int j = 1; j < n; j++)
2868                 *dst = *dst + m[j] * src[j*cn];
2869     }
2870     i /= cn;
2871     for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2872     {
2873         for (int k = 0; k < cn; k++)
2874             dst[k] = m[0] * src[k];
2875         int j = 1;
2876         for (; j < len - i; j++)
2877             for (int k = 0; k < cn; k++)
2878                 dst[k] = dst[k] + m[j] * src[j*cn + k];
2879         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2880             for (; j < n; j++)
2881             {
2882                 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2883                 for (int k = 0; k < cn; k++)
2884                     dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2885             }
2886     }
2887 }
2888 template <typename ET, typename FT>
2889 void hlineSmoothONa_yzy_a(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2890 {
2891     int pre_shift = n / 2;
2892     int post_shift = n - pre_shift;
2893     int i = 0;
2894     for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2895     {
2896         for (int k = 0; k < cn; k++)
2897             dst[k] = m[pre_shift - i] * src[k];
2898         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2899             for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2900             {
2901                 int src_idx = borderInterpolate(j, len, borderType);
2902                 for (int k = 0; k < cn; k++)
2903                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2904             }
2905         int j, mid;
2906         for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2907             for (int k = 0; k < cn; k++)
2908                 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2909         if (borderType != BORDER_CONSTANT)
2910             for (; j < i + post_shift; j++, mid++)
2911             {
2912                 int src_idx = borderInterpolate(j, len, borderType);
2913                 for (int k = 0; k < cn; k++)
2914                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2915             }
2916     }
2917     i *= cn;
2918     for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2919     {
2920         *dst = m[pre_shift] * src[pre_shift*cn];
2921         for (int j = 0; j < pre_shift; j++)
2922             *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n-1-j)*cn];
2923     }
2924     i /= cn;
2925     for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2926     {
2927         for (int k = 0; k < cn; k++)
2928             dst[k] = m[0] * src[k];
2929         int j = 1;
2930         for (; j < len - i; j++)
2931             for (int k = 0; k < cn; k++)
2932                 dst[k] = dst[k] + m[j] * src[j*cn + k];
2933         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2934             for (; j < n; j++)
2935             {
2936                 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2937                 for (int k = 0; k < cn; k++)
2938                     dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2939             }
2940     }
2941 }
2942 template <>
2943 void hlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2944 {
2945     int pre_shift = n / 2;
2946     int post_shift = n - pre_shift;
2947     int i = 0;
2948     for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2949     {
2950         for (int k = 0; k < cn; k++)
2951             dst[k] = m[pre_shift - i] * src[k];
2952         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2953             for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2954             {
2955                 int src_idx = borderInterpolate(j, len, borderType);
2956                 for (int k = 0; k < cn; k++)
2957                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2958             }
2959         int j, mid;
2960         for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2961             for (int k = 0; k < cn; k++)
2962                 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2963         if (borderType != BORDER_CONSTANT)
2964             for (; j < i + post_shift; j++, mid++)
2965             {
2966                 int src_idx = borderInterpolate(j, len, borderType);
2967                 for (int k = 0; k < cn; k++)
2968                     dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2969             }
2970     }
2971     i *= cn;
2972     int lencn = (len - post_shift + 1)*cn;
2973     for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2974     {
2975         v_uint16x8 v_src00, v_src01, v_srcN00, v_srcN01;
2976
2977         v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)(m + pre_shift)));
2978         v_expand(v_load(src + pre_shift * cn), v_src00, v_src01);
2979         v_uint16x8 v_res0 = v_src00 * v_mul;
2980         v_uint16x8 v_res1 = v_src01 * v_mul;
2981         for (int j = 0; j < pre_shift; j ++)
2982         {
2983             v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2984             v_expand(v_load(src + j * cn), v_src00, v_src01);
2985             v_expand(v_load(src + (n - 1 - j)*cn), v_srcN00, v_srcN01);
2986             v_res0 += (v_src00 + v_srcN00) * v_mul;
2987             v_res1 += (v_src01 + v_srcN01) * v_mul;
2988         }
2989
2990         v_store((uint16_t*)dst, v_res0);
2991         v_store((uint16_t*)dst + 8, v_res1);
2992     }
2993     for (; i < lencn; i++, src++, dst++)
2994     {
2995         *dst = m[pre_shift] * src[pre_shift*cn];
2996         for (int j = 0; j < pre_shift; j++)
2997             *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n - 1 - j)*cn];
2998     }
2999     i /= cn;
3000     for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
3001     {
3002         for (int k = 0; k < cn; k++)
3003             dst[k] = m[0] * src[k];
3004         int j = 1;
3005         for (; j < len - i; j++)
3006             for (int k = 0; k < cn; k++)
3007                 dst[k] = dst[k] + m[j] * src[j*cn + k];
3008         if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3009             for (; j < n; j++)
3010             {
3011                 int src_idx = borderInterpolate(i + j, len, borderType) - i;
3012                 for (int k = 0; k < cn; k++)
3013                     dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
3014             }
3015     }
3016 }
3017 template <typename ET, typename FT>
3018 void vlineSmooth1N(const FT* const * src, const FT* m, int, ET* dst, int len)
3019 {
3020     const FT* src0 = src[0];
3021     for (int i = 0; i < len; i++)
3022         dst[i] = *m * src0[i];
3023 }
3024 template <>
3025 void vlineSmooth1N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3026 {
3027     const ufixedpoint16* src0 = src[0];
3028     v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
3029 #if CV_SSE2
3030     v_uint16x8 v_1 = v_setall_u16(1);
3031     v_mul += v_mul;
3032 #endif
3033     int i = 0;
3034     for (; i <= len - 16; i += 16)
3035     {
3036         v_uint16x8 v_src0 = v_load((uint16_t*)src0 + i);
3037         v_uint16x8 v_src1 = v_load((uint16_t*)src0 + i + 8);
3038         v_uint8x16 v_res;
3039 #if CV_SSE2
3040         v_res.val = _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(v_1.val, _mm_mulhi_epu16(v_src0.val, v_mul.val)),1),
3041                                      _mm_srli_epi16(_mm_add_epi16(v_1.val, _mm_mulhi_epu16(v_src1.val, v_mul.val)),1));
3042 #else
3043         v_uint32x4 v_res0, v_res1, v_res2, v_res3;
3044         v_mul_expand(v_src0, v_mul, v_res0, v_res1);
3045         v_mul_expand(v_src1, v_mul, v_res2, v_res3);
3046         v_res = v_pack(v_rshr_pack<16>(v_res0, v_res1), v_rshr_pack<16>(v_res2, v_res3));
3047 #endif
3048         v_store(dst + i, v_res);
3049     }
3050     for (; i < len; i++)
3051         dst[i] = m[0] * src0[i];
3052 }
3053 template <typename ET, typename FT>
3054 void vlineSmooth1N1(const FT* const * src, const FT*, int, ET* dst, int len)
3055 {
3056     const FT* src0 = src[0];
3057     for (int i = 0; i < len; i++)
3058         dst[i] = src0[i];
3059 }
3060 template <>
3061 void vlineSmooth1N1<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3062 {
3063     const ufixedpoint16* src0 = src[0];
3064     int i = 0;
3065     for (; i <= len - 8; i += 8)
3066         v_rshr_pack_store<8>(dst + i, v_load((uint16_t*)(src0 + i)));
3067     for (; i < len; i++)
3068         dst[i] = src0[i];
3069 }
3070 template <typename ET, typename FT>
3071 void vlineSmooth3N(const FT* const * src, const FT* m, int, ET* dst, int len)
3072 {
3073     for (int i = 0; i < len; i++)
3074         dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3075 }
3076 template <>
3077 void vlineSmooth3N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3078 {
3079     int i = 0;
3080     static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3081     v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3082     if (len > 7)
3083     {
3084         ufixedpoint32 val[] = { (m[0] + m[1] + m[2]) * ufixedpoint16((uint8_t)128) };
3085         v_128_4 = v_setall_s32(*((int32_t*)val));
3086     }
3087     v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3088     v_int16x8 v_mul2 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 2))));
3089     for (; i <= len - 32; i += 32)
3090     {
3091         v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3092         v_int16x8 v_tmp0, v_tmp1;
3093
3094         v_src00 = v_load((int16_t*)(src[0]) + i);
3095         v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3096         v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3097         v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3098         v_src10 = v_load((int16_t*)(src[1]) + i);
3099         v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3100         v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3101         v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3102         v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3103         v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3104         v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3105         v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3106         v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3107         v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3108         v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3109         v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3110         v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3111         v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3112         v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3113         v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3114
3115         v_int32x4 v_resj0, v_resj1;
3116         v_src00 = v_load((int16_t*)(src[2]) + i);
3117         v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3118         v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3119         v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3120         v_mul_expand(v_add_wrap(v_src00, v_128), v_mul2, v_resj0, v_resj1);
3121         v_res0 += v_resj0;
3122         v_res1 += v_resj1;
3123         v_mul_expand(v_add_wrap(v_src01, v_128), v_mul2, v_resj0, v_resj1);
3124         v_res2 += v_resj0;
3125         v_res3 += v_resj1;
3126         v_mul_expand(v_add_wrap(v_src02, v_128), v_mul2, v_resj0, v_resj1);
3127         v_res4 += v_resj0;
3128         v_res5 += v_resj1;
3129         v_mul_expand(v_add_wrap(v_src03, v_128), v_mul2, v_resj0, v_resj1);
3130         v_res6 += v_resj0;
3131         v_res7 += v_resj1;
3132
3133         v_res0 += v_128_4;
3134         v_res1 += v_128_4;
3135         v_res2 += v_128_4;
3136         v_res3 += v_128_4;
3137         v_res4 += v_128_4;
3138         v_res5 += v_128_4;
3139         v_res6 += v_128_4;
3140         v_res7 += v_128_4;
3141
3142         v_store(dst + i     , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3143                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3144         v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3145                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3146     }
3147     for (; i < len; i++)
3148         dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3149 }
3150 template <typename ET, typename FT>
3151 void vlineSmooth3N121(const FT* const * src, const FT*, int, ET* dst, int len)
3152 {
3153     for (int i = 0; i < len; i++)
3154         dst[i] = (FT::WT(src[0][i]) >> 2) + (FT::WT(src[2][i]) >> 2) + (FT::WT(src[1][i]) >> 1);
3155 }
3156 template <>
3157 void vlineSmooth3N121<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3158 {
3159     int i = 0;
3160     for (; i <= len - 16; i += 16)
3161     {
3162         v_uint32x4 v_src00, v_src01, v_src02, v_src03, v_src10, v_src11, v_src12, v_src13, v_src20, v_src21, v_src22, v_src23;
3163         v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3164         v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3165         v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3166         v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3167         v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3168         v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3169         v_store(dst + i, v_pack(v_rshr_pack<10>(v_src00 + v_src20 + (v_src10 + v_src10), v_src01 + v_src21 + (v_src11 + v_src11)),
3170                                 v_rshr_pack<10>(v_src02 + v_src22 + (v_src12 + v_src12), v_src03 + v_src23 + (v_src13 + v_src13))));
3171     }
3172     for (; i < len; i++)
3173         dst[i] = (((uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[2]))[i]) + ((uint32_t)(((uint16_t*)(src[1]))[i]) << 1)) + (1 << 9)) >> 10;
3174 }
3175 template <typename ET, typename FT>
3176 void vlineSmooth5N(const FT* const * src, const FT* m, int, ET* dst, int len)
3177 {
3178     for (int i = 0; i < len; i++)
3179         dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
3180 }
3181 template <>
3182 void vlineSmooth5N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3183 {
3184     int i = 0;
3185     static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3186     v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3187     if (len > 7)
3188     {
3189         ufixedpoint32 val[] = { (m[0] + m[1] + m[2] + m[3] + m[4]) * ufixedpoint16((uint8_t)128) };
3190         v_128_4 = v_setall_s32(*((int32_t*)val));
3191     }
3192     v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3193     v_int16x8 v_mul23 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + 2))));
3194     v_int16x8 v_mul4 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 4))));
3195     for (; i <= len - 32; i += 32)
3196     {
3197         v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3198         v_int16x8 v_tmp0, v_tmp1;
3199
3200         v_src00 = v_load((int16_t*)(src[0]) + i);
3201         v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3202         v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3203         v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3204         v_src10 = v_load((int16_t*)(src[1]) + i);
3205         v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3206         v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3207         v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3208         v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3209         v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3210         v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3211         v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3212         v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3213         v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3214         v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3215         v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3216         v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3217         v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3218         v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3219         v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3220
3221         v_src00 = v_load((int16_t*)(src[2]) + i);
3222         v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3223         v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3224         v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3225         v_src10 = v_load((int16_t*)(src[3]) + i);
3226         v_src11 = v_load((int16_t*)(src[3]) + i + 8);
3227         v_src12 = v_load((int16_t*)(src[3]) + i + 16);
3228         v_src13 = v_load((int16_t*)(src[3]) + i + 24);
3229         v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3230         v_res0 += v_dotprod(v_tmp0, v_mul23);
3231         v_res1 += v_dotprod(v_tmp1, v_mul23);
3232         v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3233         v_res2 += v_dotprod(v_tmp0, v_mul23);
3234         v_res3 += v_dotprod(v_tmp1, v_mul23);
3235         v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3236         v_res4 += v_dotprod(v_tmp0, v_mul23);
3237         v_res5 += v_dotprod(v_tmp1, v_mul23);
3238         v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3239         v_res6 += v_dotprod(v_tmp0, v_mul23);
3240         v_res7 += v_dotprod(v_tmp1, v_mul23);
3241
3242         v_int32x4 v_resj0, v_resj1;
3243         v_src00 = v_load((int16_t*)(src[4]) + i);
3244         v_src01 = v_load((int16_t*)(src[4]) + i + 8);
3245         v_src02 = v_load((int16_t*)(src[4]) + i + 16);
3246         v_src03 = v_load((int16_t*)(src[4]) + i + 24);
3247         v_mul_expand(v_add_wrap(v_src00, v_128), v_mul4, v_resj0, v_resj1);
3248         v_res0 += v_resj0;
3249         v_res1 += v_resj1;
3250         v_mul_expand(v_add_wrap(v_src01, v_128), v_mul4, v_resj0, v_resj1);
3251         v_res2 += v_resj0;
3252         v_res3 += v_resj1;
3253         v_mul_expand(v_add_wrap(v_src02, v_128), v_mul4, v_resj0, v_resj1);
3254         v_res4 += v_resj0;
3255         v_res5 += v_resj1;
3256         v_mul_expand(v_add_wrap(v_src03, v_128), v_mul4, v_resj0, v_resj1);
3257         v_res6 += v_resj0;
3258         v_res7 += v_resj1;
3259
3260         v_res0 += v_128_4;
3261         v_res1 += v_128_4;
3262         v_res2 += v_128_4;
3263         v_res3 += v_128_4;
3264         v_res4 += v_128_4;
3265         v_res5 += v_128_4;
3266         v_res6 += v_128_4;
3267         v_res7 += v_128_4;
3268
3269         v_store(dst + i     , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3270                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3271         v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3272                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3273     }
3274     for (; i < len; i++)
3275         dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
3276 }
3277 template <typename ET, typename FT>
3278 void vlineSmooth5N14641(const FT* const * src, const FT*, int, ET* dst, int len)
3279 {
3280     for (int i = 0; i < len; i++)
3281         dst[i] = (FT::WT(src[2][i])*(uint8_t)6 + ((FT::WT(src[1][i]) + FT::WT(src[3][i]))<<2) + FT::WT(src[0][i]) + FT::WT(src[4][i])) >> 4;
3282 }
3283 template <>
3284 void vlineSmooth5N14641<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3285 {
3286     int i = 0;
3287     v_uint32x4 v_6 = v_setall_u32(6);
3288     for (; i <= len - 16; i += 16)
3289     {
3290         v_uint32x4 v_src00, v_src10, v_src20, v_src30, v_src40;
3291         v_uint32x4 v_src01, v_src11, v_src21, v_src31, v_src41;
3292         v_uint32x4 v_src02, v_src12, v_src22, v_src32, v_src42;
3293         v_uint32x4 v_src03, v_src13, v_src23, v_src33, v_src43;
3294         v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3295         v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3296         v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3297         v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3298         v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3299         v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3300         v_expand(v_load((uint16_t*)(src[3]) + i), v_src30, v_src31);
3301         v_expand(v_load((uint16_t*)(src[3]) + i + 8), v_src32, v_src33);
3302         v_expand(v_load((uint16_t*)(src[4]) + i), v_src40, v_src41);
3303         v_expand(v_load((uint16_t*)(src[4]) + i + 8), v_src42, v_src43);
3304         v_store(dst + i, v_pack(v_rshr_pack<12>(v_src20*v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40,
3305                                                 v_src21*v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41),
3306                                 v_rshr_pack<12>(v_src22*v_6 + ((v_src12 + v_src32) << 2) + v_src02 + v_src42,
3307                                                 v_src23*v_6 + ((v_src13 + v_src33) << 2) + v_src03 + v_src43)));
3308     }
3309     for (; i < len; i++)
3310         dst[i] = ((uint32_t)(((uint16_t*)(src[2]))[i]) * 6 +
3311                   (((uint32_t)(((uint16_t*)(src[1]))[i]) + (uint32_t)(((uint16_t*)(src[3]))[i])) << 2) +
3312                   (uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[4]))[i]) + (1 << 11)) >> 12;
3313 }
3314 template <typename ET, typename FT>
3315 void vlineSmooth(const FT* const * src, const FT* m, int n, ET* dst, int len)
3316 {
3317     for (int i = 0; i < len; i++)
3318     {
3319         typename FT::WT val = m[0] * src[0][i];
3320         for (int j = 1; j < n; j++)
3321             val = val + m[j] * src[j][i];
3322         dst[i] = val;
3323     }
3324 }
3325 template <>
3326 void vlineSmooth<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3327 {
3328     int i = 0;
3329     static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3330     v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3331     if (len > 7)
3332     {
3333         ufixedpoint16 msum = m[0] + m[1];
3334         for (int j = 2; j < n; j++)
3335             msum = msum + m[j];
3336         ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3337         v_128_4 = v_setall_s32(*((int32_t*)val));
3338     }
3339     for (; i <= len - 32; i += 32)
3340     {
3341         v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3342         v_int16x8 v_tmp0, v_tmp1;
3343
3344         v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3345
3346         v_src00 = v_load((int16_t*)(src[0]) + i);
3347         v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3348         v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3349         v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3350         v_src10 = v_load((int16_t*)(src[1]) + i);
3351         v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3352         v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3353         v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3354         v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3355         v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul);
3356         v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul);
3357         v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3358         v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul);
3359         v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul);
3360         v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3361         v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul);
3362         v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul);
3363         v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3364         v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul);
3365         v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul);
3366
3367         int j = 2;
3368         for (; j < n - 1; j+=2)
3369         {
3370             v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m+j))));
3371
3372             v_src00 = v_load((int16_t*)(src[j]) + i);
3373             v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3374             v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3375             v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3376             v_src10 = v_load((int16_t*)(src[j+1]) + i);
3377             v_src11 = v_load((int16_t*)(src[j+1]) + i + 8);
3378             v_src12 = v_load((int16_t*)(src[j+1]) + i + 16);
3379             v_src13 = v_load((int16_t*)(src[j+1]) + i + 24);
3380             v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3381             v_res0 += v_dotprod(v_tmp0, v_mul);
3382             v_res1 += v_dotprod(v_tmp1, v_mul);
3383             v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3384             v_res2 += v_dotprod(v_tmp0, v_mul);
3385             v_res3 += v_dotprod(v_tmp1, v_mul);
3386             v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3387             v_res4 += v_dotprod(v_tmp0, v_mul);
3388             v_res5 += v_dotprod(v_tmp1, v_mul);
3389             v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3390             v_res6 += v_dotprod(v_tmp0, v_mul);
3391             v_res7 += v_dotprod(v_tmp1, v_mul);
3392         }
3393         if(j < n)
3394         {
3395             v_int32x4 v_resj0, v_resj1;
3396             v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3397             v_src00 = v_load((int16_t*)(src[j]) + i);
3398             v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3399             v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3400             v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3401             v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_resj0, v_resj1);
3402             v_res0 += v_resj0;
3403             v_res1 += v_resj1;
3404             v_mul_expand(v_add_wrap(v_src01, v_128), v_mul, v_resj0, v_resj1);
3405             v_res2 += v_resj0;
3406             v_res3 += v_resj1;
3407             v_mul_expand(v_add_wrap(v_src02, v_128), v_mul, v_resj0, v_resj1);
3408             v_res4 += v_resj0;
3409             v_res5 += v_resj1;
3410             v_mul_expand(v_add_wrap(v_src03, v_128), v_mul, v_resj0, v_resj1);
3411             v_res6 += v_resj0;
3412             v_res7 += v_resj1;
3413         }
3414         v_res0 += v_128_4;
3415         v_res1 += v_128_4;
3416         v_res2 += v_128_4;
3417         v_res3 += v_128_4;
3418         v_res4 += v_128_4;
3419         v_res5 += v_128_4;
3420         v_res6 += v_128_4;
3421         v_res7 += v_128_4;
3422
3423         v_store(dst + i     , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3424                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3425         v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3426                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3427     }
3428     for (; i < len; i++)
3429     {
3430         ufixedpoint32 val = m[0] * src[0][i];
3431         for (int j = 1; j < n; j++)
3432         {
3433             val = val + m[j] * src[j][i];
3434         }
3435         dst[i] = val;
3436     }
3437 }
3438 template <typename ET, typename FT>
3439 void vlineSmoothONa_yzy_a(const FT* const * src, const FT* m, int n, ET* dst, int len)
3440 {
3441     int pre_shift = n / 2;
3442     for (int i = 0; i < len; i++)
3443     {
3444         typename FT::WT val = m[pre_shift] * src[pre_shift][i];
3445         for (int j = 0; j < pre_shift; j++)
3446             val = val + m[j] * src[j][i] + m[j] * src[(n - 1 - j)][i];
3447         dst[i] = val;
3448     }
3449 }
3450 template <>
3451 void vlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3452 {
3453     int pre_shift = n / 2;
3454     int i = 0;
3455     static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3456     v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3457     if (len > 7)
3458     {
3459         ufixedpoint16 msum = m[0] + m[pre_shift] + m[n - 1];
3460         for (int j = 1; j < pre_shift; j++)
3461             msum = msum + m[j] + m[n - 1 - j];
3462         ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3463         v_128_4 = v_setall_s32(*((int32_t*)val));
3464     }
3465     for (; i <= len - 32; i += 32)
3466     {
3467         v_int16x8 v_src00, v_src10, v_src20, v_src30, v_src01, v_src11, v_src21, v_src31;
3468         v_int32x4 v_res0, v_res1, v_res2, v_res3, v_res4, v_res5, v_res6, v_res7;
3469         v_int16x8 v_tmp0, v_tmp1, v_tmp2, v_tmp3, v_tmp4, v_tmp5, v_tmp6, v_tmp7;
3470
3471         v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + pre_shift))));
3472         v_src00 = v_load((int16_t*)(src[pre_shift]) + i);
3473         v_src10 = v_load((int16_t*)(src[pre_shift]) + i + 8);
3474         v_src20 = v_load((int16_t*)(src[pre_shift]) + i + 16);
3475         v_src30 = v_load((int16_t*)(src[pre_shift]) + i + 24);
3476         v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_res0, v_res1);
3477         v_mul_expand(v_add_wrap(v_src10, v_128), v_mul, v_res2, v_res3);
3478         v_mul_expand(v_add_wrap(v_src20, v_128), v_mul, v_res4, v_res5);
3479         v_mul_expand(v_add_wrap(v_src30, v_128), v_mul, v_res6, v_res7);
3480
3481         int j = 0;
3482         for (; j < pre_shift; j++)
3483         {
3484             v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3485
3486             v_src00 = v_load((int16_t*)(src[j]) + i);
3487             v_src10 = v_load((int16_t*)(src[j]) + i + 8);
3488             v_src20 = v_load((int16_t*)(src[j]) + i + 16);
3489             v_src30 = v_load((int16_t*)(src[j]) + i + 24);
3490             v_src01 = v_load((int16_t*)(src[n - 1 - j]) + i);
3491             v_src11 = v_load((int16_t*)(src[n - 1 - j]) + i + 8);
3492             v_src21 = v_load((int16_t*)(src[n - 1 - j]) + i + 16);
3493             v_src31 = v_load((int16_t*)(src[n - 1 - j]) + i + 24);
3494             v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src01, v_128), v_tmp0, v_tmp1);
3495             v_res0 += v_dotprod(v_tmp0, v_mul);
3496             v_res1 += v_dotprod(v_tmp1, v_mul);
3497             v_zip(v_add_wrap(v_src10, v_128), v_add_wrap(v_src11, v_128), v_tmp2, v_tmp3);
3498             v_res2 += v_dotprod(v_tmp2, v_mul);
3499             v_res3 += v_dotprod(v_tmp3, v_mul);
3500             v_zip(v_add_wrap(v_src20, v_128), v_add_wrap(v_src21, v_128), v_tmp4, v_tmp5);
3501             v_res4 += v_dotprod(v_tmp4, v_mul);
3502             v_res5 += v_dotprod(v_tmp5, v_mul);
3503             v_zip(v_add_wrap(v_src30, v_128), v_add_wrap(v_src31, v_128), v_tmp6, v_tmp7);
3504             v_res6 += v_dotprod(v_tmp6, v_mul);
3505             v_res7 += v_dotprod(v_tmp7, v_mul);
3506         }
3507
3508         v_res0 += v_128_4;
3509         v_res1 += v_128_4;
3510         v_res2 += v_128_4;
3511         v_res3 += v_128_4;
3512         v_res4 += v_128_4;
3513         v_res5 += v_128_4;
3514         v_res6 += v_128_4;
3515         v_res7 += v_128_4;
3516
3517         v_store(dst + i     , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3518                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3519         v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3520                                      v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3521     }
3522     for (; i < len; i++)
3523     {
3524         ufixedpoint32 val = m[0] * src[0][i];
3525         for (int j = 1; j < n; j++)
3526         {
3527             val = val + m[j] * src[j][i];
3528         }
3529         dst[i] = val;
3530     }
3531 }
3532 template <typename ET, typename FT>
3533 class fixedSmoothInvoker : public ParallelLoopBody
3534 {
3535 public:
3536     fixedSmoothInvoker(const ET* _src, size_t _src_stride, ET* _dst, size_t _dst_stride,
3537                        int _width, int _height, int _cn, const FT* _kx, int _kxlen, const FT* _ky, int _kylen, int _borderType) : ParallelLoopBody(),
3538                        src(_src), dst(_dst), src_stride(_src_stride), dst_stride(_dst_stride),
3539                        width(_width), height(_height), cn(_cn), kx(_kx), ky(_ky), kxlen(_kxlen), kylen(_kylen), borderType(_borderType)
3540     {
3541         if (kxlen == 1)
3542         {
3543             if (kx[0] == FT::one())
3544                 hlineSmoothFunc = hlineSmooth1N1;
3545             else
3546                 hlineSmoothFunc = hlineSmooth1N;
3547         }
3548         else if (kxlen == 3)
3549         {
3550             if (kx[0] == (FT::one()>>2)&&kx[1] == (FT::one()>>1)&&kx[2] == (FT::one()>>2))
3551                 hlineSmoothFunc = hlineSmooth3N121;
3552             else if ((kx[0] - kx[2]).isZero())
3553                     hlineSmoothFunc = hlineSmooth3Naba;
3554             else
3555                 hlineSmoothFunc = hlineSmooth3N;
3556         }
3557         else if (kxlen == 5)
3558         {
3559             if (kx[2] == (FT::one()*(uint8_t)3>>3) &&
3560                 kx[1] == (FT::one()>>2) && kx[3] == (FT::one()>>2) &&
3561                 kx[0] == (FT::one()>>4) && kx[4] == (FT::one()>>4))
3562                 hlineSmoothFunc = hlineSmooth5N14641;
3563             else if (kx[0] == kx[4] && kx[1] == kx[3])
3564                 hlineSmoothFunc = hlineSmooth5Nabcba;
3565             else
3566                 hlineSmoothFunc = hlineSmooth5N;
3567         }
3568         else if (kxlen % 2 == 1)
3569         {
3570             hlineSmoothFunc = hlineSmoothONa_yzy_a;
3571             for (int i = 0; i < kxlen / 2; i++)
3572                 if (!(kx[i] == kx[kxlen - 1 - i]))
3573                 {
3574                     hlineSmoothFunc = hlineSmooth;
3575                     break;
3576                 }
3577         }
3578         else
3579             hlineSmoothFunc = hlineSmooth;
3580         if (kylen == 1)
3581         {
3582             if (ky[0] == FT::one())
3583                 vlineSmoothFunc = vlineSmooth1N1;
3584             else
3585                 vlineSmoothFunc = vlineSmooth1N;
3586         }
3587         else if (kylen == 3)
3588         {
3589             if (ky[0] == (FT::one() >> 2) && ky[1] == (FT::one() >> 1) && ky[2] == (FT::one() >> 2))
3590                 vlineSmoothFunc = vlineSmooth3N121;
3591             else
3592                 vlineSmoothFunc = vlineSmooth3N;
3593         }
3594         else if (kylen == 5)
3595         {
3596             if (ky[2] == (FT::one() * (uint8_t)3 >> 3) &&
3597                 ky[1] == (FT::one() >> 2) && ky[3] == (FT::one() >> 2) &&
3598                 ky[0] == (FT::one() >> 4) && ky[4] == (FT::one() >> 4))
3599                 vlineSmoothFunc = vlineSmooth5N14641;
3600             else
3601                 vlineSmoothFunc = vlineSmooth5N;
3602         }
3603         else if (kylen % 2 == 1)
3604         {
3605             vlineSmoothFunc = vlineSmoothONa_yzy_a;
3606             for (int i = 0; i < kylen / 2; i++)
3607                 if (!(ky[i] == ky[kylen - 1 - i]))
3608                 {
3609                     vlineSmoothFunc = vlineSmooth;
3610                     break;
3611                 }
3612         }
3613         else
3614             vlineSmoothFunc = vlineSmooth;
3615     }
3616     virtual void operator() (const Range& range) const CV_OVERRIDE
3617     {
3618         AutoBuffer<FT> _buf(width*cn*kylen);
3619         FT* buf = _buf.data();
3620         AutoBuffer<FT*> _ptrs(kylen*2);
3621         FT** ptrs = _ptrs.data();
3622
3623         if (kylen == 1)
3624         {
3625             ptrs[0] = buf;
3626             for (int i = range.start; i < range.end; i++)
3627             {
3628                 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[0], width, borderType);
3629                 vlineSmoothFunc(ptrs, ky, kylen, dst + i * dst_stride, width*cn);
3630             }
3631         }
3632         else if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3633         {
3634             int pre_shift = kylen / 2;
3635             int post_shift = kylen - pre_shift - 1;
3636             // First line evaluation
3637             int idst = range.start;
3638             int ifrom = max(0, idst - pre_shift);
3639             int ito = idst + post_shift + 1;
3640             int i = ifrom;
3641             int bufline = 0;
3642             for (; i < min(ito, height); i++, bufline++)
3643             {
3644                 ptrs[bufline+kylen] = ptrs[bufline] = buf + bufline * width*cn;
3645                 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3646             }
3647             for (; i < ito; i++, bufline++)
3648             {
3649                 int src_idx = borderInterpolate(i, height, borderType);
3650                 if (src_idx < ifrom)
3651                 {
3652                     ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3653                     hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3654                 }
3655                 else
3656                 {
3657                     ptrs[bufline + kylen] = ptrs[bufline] = ptrs[src_idx - ifrom];
3658                 }
3659             }
3660             for (int j = idst - pre_shift; j < 0; j++)
3661             {
3662                 int src_idx = borderInterpolate(j, height, borderType);
3663                 if (src_idx >= ito)
3664                 {
3665                     ptrs[2*kylen + j] = ptrs[kylen + j] = buf + (kylen + j) * width*cn;
3666                     hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[kylen + j], width, borderType);
3667                 }
3668                 else
3669                 {
3670                     ptrs[2*kylen + j] = ptrs[kylen + j] = ptrs[src_idx];
3671                 }
3672             }
3673             vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn); idst++;
3674
3675             // border mode dependent part evaluation
3676             // i points to last src row to evaluate in convolution
3677             bufline %= kylen; ito = min(height, range.end + post_shift);
3678             for (; i < min(kylen, ito); i++, idst++)
3679             {
3680                 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3681                 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3682                 bufline = (bufline + 1) % kylen;
3683                 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3684             }
3685             // Points inside the border
3686             for (; i < ito; i++, idst++)
3687             {
3688                 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3689                 bufline = (bufline + 1) % kylen;
3690                 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3691             }
3692             // Points that could fall below border
3693             for (; i < range.end + post_shift; i++, idst++)
3694             {
3695                 int src_idx = borderInterpolate(i, height, borderType);
3696                 if ((i - src_idx) > kylen)
3697                     hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3698                 else
3699                     ptrs[bufline + kylen] = ptrs[bufline] = ptrs[(bufline + kylen - (i - src_idx)) % kylen];
3700                 bufline = (bufline + 1) % kylen;
3701                 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3702             }
3703         }
3704         else
3705         {
3706             int pre_shift = kylen / 2;
3707             int post_shift = kylen - pre_shift - 1;
3708             // First line evaluation
3709             int idst = range.start;
3710             int ifrom = idst - pre_shift;
3711             int ito = min(idst + post_shift + 1, height);
3712             int i = max(0, ifrom);
3713             int bufline = 0;
3714             for (; i < ito; i++, bufline++)
3715             {
3716                 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3717                 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3718             }
3719
3720             if (bufline == 1)
3721                 vlineSmooth1N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3722             else if (bufline == 3)
3723                 vlineSmooth3N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3724             else if (bufline == 5)
3725                 vlineSmooth5N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3726             else
3727                 vlineSmooth(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3728             idst++;
3729
3730             // border mode dependent part evaluation
3731             // i points to last src row to evaluate in convolution
3732             bufline %= kylen; ito = min(height, range.end + post_shift);
3733             for (; i < min(kylen, ito); i++, idst++)
3734             {
3735                 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3736                 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3737                 bufline++;
3738                 if (bufline == 3)
3739                     vlineSmooth3N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3740                 else if (bufline == 5)
3741                     vlineSmooth5N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3742                 else
3743                     vlineSmooth(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3744                 bufline %= kylen;
3745             }
3746             // Points inside the border
3747             if (i - max(0, ifrom) >= kylen)
3748             {
3749                 for (; i < ito; i++, idst++)
3750                 {
3751                     hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3752                     bufline = (bufline + 1) % kylen;
3753                     vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3754                 }
3755
3756                 // Points that could fall below border
3757                 // i points to first src row to evaluate in convolution
3758                 bufline = (bufline + 1) % kylen;
3759                 for (i = idst - pre_shift; i < range.end - pre_shift; i++, idst++, bufline++)
3760                     if (height - i == 3)
3761                         vlineSmooth3N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3762                     else if (height - i == 5)
3763                         vlineSmooth5N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3764                     else
3765                         vlineSmooth(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3766             }
3767             else
3768             {
3769                 // i points to first src row to evaluate in convolution
3770                 for (i = idst - pre_shift; i < min(range.end - pre_shift, 0); i++, idst++)
3771                     if (height == 3)
3772                         vlineSmooth3N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3773                     else if (height == 5)
3774                         vlineSmooth5N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3775                     else
3776                         vlineSmooth(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3777                 for (; i < range.end - pre_shift; i++, idst++)
3778                     if (height - i == 3)
3779                         vlineSmooth3N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3780                     else if (height - i == 5)
3781                         vlineSmooth5N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3782                     else
3783                         vlineSmooth(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3784             }
3785         }
3786     }
3787 private:
3788     const ET* src;
3789     ET* dst;
3790     size_t src_stride, dst_stride;
3791     int width, height, cn;
3792     const FT *kx, *ky;
3793     int kxlen, kylen;
3794     int borderType;
3795     void(*hlineSmoothFunc)(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType);
3796     void(*vlineSmoothFunc)(const FT* const * src, const FT* m, int n, ET* dst, int len);
3797
3798     fixedSmoothInvoker(const fixedSmoothInvoker&);
3799     fixedSmoothInvoker& operator=(const fixedSmoothInvoker&);
3800 };
3801
3802 static void getGaussianKernel(int n, double sigma, int ktype, Mat& res) { res = getGaussianKernel(n, sigma, ktype); }
3803 template <typename T> static void getGaussianKernel(int n, double sigma, int, std::vector<T>& res) { res = getFixedpointGaussianKernel<T>(n, sigma); }
3804
3805 template <typename T>
3806 static void createGaussianKernels( T & kx, T & ky, int type, Size &ksize,
3807                                    double sigma1, double sigma2 )
3808 {
3809     int depth = CV_MAT_DEPTH(type);
3810     if( sigma2 <= 0 )
3811         sigma2 = sigma1;
3812
3813     // automatic detection of kernel size from sigma
3814     if( ksize.width <= 0 && sigma1 > 0 )
3815         ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3816     if( ksize.height <= 0 && sigma2 > 0 )
3817         ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3818
3819     CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
3820         ksize.height > 0 && ksize.height % 2 == 1 );
3821
3822     sigma1 = std::max( sigma1, 0. );
3823     sigma2 = std::max( sigma2, 0. );
3824
3825     getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F), kx );
3826     if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
3827         ky = kx;
3828     else
3829         getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F), ky );
3830 }
3831
3832 }
3833
3834 cv::Ptr<cv::FilterEngine> cv::createGaussianFilter( int type, Size ksize,
3835                                         double sigma1, double sigma2,
3836                                         int borderType )
3837 {
3838     Mat kx, ky;
3839     createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
3840
3841     return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
3842 }
3843
3844 namespace cv
3845 {
3846 #ifdef HAVE_OPENCL
3847
3848 static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize, int ddepth,
3849                                   InputArray _kernelX, InputArray _kernelY, int borderType)
3850 {
3851     const ocl::Device & dev = ocl::Device::getDefault();
3852     int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
3853
3854     if ( !(dev.isIntel() && (type == CV_8UC1) &&
3855          (_src.offset() == 0) && (_src.step() % 4 == 0) &&
3856          ((ksize.width == 5 && (_src.cols() % 4 == 0)) ||
3857          (ksize.width == 3 && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)))) )
3858         return false;
3859
3860     Mat kernelX = _kernelX.getMat().reshape(1, 1);
3861     if (kernelX.cols % 2 != 1)
3862         return false;
3863     Mat kernelY = _kernelY.getMat().reshape(1, 1);
3864     if (kernelY.cols % 2 != 1)
3865         return false;
3866
3867     if (ddepth < 0)
3868         ddepth = sdepth;
3869
3870     Size size = _src.size();
3871     size_t globalsize[2] = { 0, 0 };
3872     size_t localsize[2] = { 0, 0 };
3873
3874     if (ksize.width == 3)
3875     {
3876         globalsize[0] = size.width / 16;
3877         globalsize[1] = size.height / 2;
3878     }
3879     else if (ksize.width == 5)
3880     {
3881         globalsize[0] = size.width / 4;
3882         globalsize[1] = size.height / 1;
3883     }
3884
3885     const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
3886     char build_opts[1024];
3887     sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
3888             ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
3889             ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
3890
3891     ocl::Kernel kernel;
3892
3893     if (ksize.width == 3)
3894         kernel.create("gaussianBlur3x3_8UC1_cols16_rows2", cv::ocl::imgproc::gaussianBlur3x3_oclsrc, build_opts);
3895     else if (ksize.width == 5)
3896         kernel.create("gaussianBlur5x5_8UC1_cols4", cv::ocl::imgproc::gaussianBlur5x5_oclsrc, build_opts);
3897
3898     if (kernel.empty())
3899         return false;
3900
3901     UMat src = _src.getUMat();
3902     _dst.create(size, CV_MAKETYPE(ddepth, cn));
3903     if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
3904         return false;
3905     UMat dst = _dst.getUMat();
3906
3907     int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
3908     idxArg = kernel.set(idxArg, (int)src.step);
3909     idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
3910     idxArg = kernel.set(idxArg, (int)dst.step);
3911     idxArg = kernel.set(idxArg, (int)dst.rows);
3912     idxArg = kernel.set(idxArg, (int)dst.cols);
3913
3914     return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
3915 }
3916
3917 #endif
3918
3919 #ifdef HAVE_OPENVX
3920
3921 namespace ovx {
3922     template <> inline bool skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(int w, int h) { return w*h < 320 * 240; }
3923 }
3924 static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
3925                                 double sigma1, double sigma2, int borderType)
3926 {
3927     if (sigma2 <= 0)
3928         sigma2 = sigma1;
3929     // automatic detection of kernel size from sigma
3930     if (ksize.width <= 0 && sigma1 > 0)
3931         ksize.width = cvRound(sigma1*6 + 1) | 1;
3932     if (ksize.height <= 0 && sigma2 > 0)
3933         ksize.height = cvRound(sigma2*6 + 1) | 1;
3934
3935     if (_src.type() != CV_8UC1 ||
3936         _src.cols() < 3 || _src.rows() < 3 ||
3937         ksize.width != 3 || ksize.height != 3)
3938         return false;
3939
3940     sigma1 = std::max(sigma1, 0.);
3941     sigma2 = std::max(sigma2, 0.);
3942
3943     if (!(sigma1 == 0.0 || (sigma1 - 0.8) < DBL_EPSILON) || !(sigma2 == 0.0 || (sigma2 - 0.8) < DBL_EPSILON) ||
3944         ovx::skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(_src.cols(), _src.rows()))
3945         return false;
3946
3947     Mat src = _src.getMat();
3948     Mat dst = _dst.getMat();
3949
3950     if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
3951         return false; //Process isolated borders only
3952     vx_enum border;
3953     switch (borderType & ~BORDER_ISOLATED)
3954     {
3955     case BORDER_CONSTANT:
3956         border = VX_BORDER_CONSTANT;
3957         break;
3958     case BORDER_REPLICATE:
3959         border = VX_BORDER_REPLICATE;
3960         break;
3961     default:
3962         return false;
3963     }
3964
3965     try
3966     {
3967         ivx::Context ctx = ovx::getOpenVXContext();
3968
3969         Mat a;
3970         if (dst.data != src.data)
3971             a = src;
3972         else
3973             src.copyTo(a);
3974
3975         ivx::Image
3976             ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3977                 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
3978             ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3979                 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
3980
3981         //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
3982         //since OpenVX standard says nothing about thread-safety for now
3983         ivx::border_t prevBorder = ctx.immediateBorder();
3984         ctx.setImmediateBorder(border, (vx_uint8)(0));
3985         ivx::IVX_CHECK_STATUS(vxuGaussian3x3(ctx, ia, ib));
3986         ctx.setImmediateBorder(prevBorder);
3987     }
3988     catch (ivx::RuntimeError & e)
3989     {
3990         VX_DbgThrow(e.what());
3991     }
3992     catch (ivx::WrapperError & e)
3993     {
3994         VX_DbgThrow(e.what());
3995     }
3996     return true;
3997 }
3998
3999 #endif
4000
4001 #ifdef HAVE_IPP
4002 // IW 2017u2 has bug which doesn't allow use of partial inMem with tiling
4003 #if IPP_DISABLE_GAUSSIANBLUR_PARALLEL
4004 #define IPP_GAUSSIANBLUR_PARALLEL 0
4005 #else
4006 #define IPP_GAUSSIANBLUR_PARALLEL 1
4007 #endif
4008
4009 #ifdef HAVE_IPP_IW
4010
4011 class ipp_gaussianBlurParallel: public ParallelLoopBody
4012 {
4013 public:
4014     ipp_gaussianBlurParallel(::ipp::IwiImage &src, ::ipp::IwiImage &dst, int kernelSize, float sigma, ::ipp::IwiBorderType &border, bool *pOk):
4015         m_src(src), m_dst(dst), m_kernelSize(kernelSize), m_sigma(sigma), m_border(border), m_pOk(pOk) {
4016         *m_pOk = true;
4017     }
4018     ~ipp_gaussianBlurParallel()
4019     {
4020     }
4021
4022     virtual void operator() (const Range& range) const CV_OVERRIDE
4023     {
4024         CV_INSTRUMENT_REGION_IPP()
4025
4026         if(!*m_pOk)
4027             return;
4028
4029         try
4030         {
4031             ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, m_dst.m_size.width, range.end - range.start);
4032             CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, m_src, m_dst, m_kernelSize, m_sigma, ::ipp::IwDefault(), m_border, tile);
4033         }
4034         catch(::ipp::IwException e)
4035         {
4036             *m_pOk = false;
4037             return;
4038         }
4039     }
4040 private:
4041     ::ipp::IwiImage &m_src;
4042     ::ipp::IwiImage &m_dst;
4043
4044     int m_kernelSize;
4045     float m_sigma;
4046     ::ipp::IwiBorderType &m_border;
4047
4048     volatile bool *m_pOk;
4049     const ipp_gaussianBlurParallel& operator= (const ipp_gaussianBlurParallel&);
4050 };
4051
4052 #endif
4053
4054 static bool ipp_GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
4055                    double sigma1, double sigma2, int borderType )
4056 {
4057 #ifdef HAVE_IPP_IW
4058     CV_INSTRUMENT_REGION_IPP()
4059
4060 #if IPP_VERSION_X100 < 201800 && ((defined _MSC_VER && defined _M_IX86) || (defined __GNUC__ && defined __i386__))
4061     CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4062     return false; // bug on ia32
4063 #else
4064     if(sigma1 != sigma2)
4065         return false;
4066
4067     if(sigma1 < FLT_EPSILON)
4068         return false;
4069
4070     if(ksize.width != ksize.height)
4071         return false;
4072
4073     // Acquire data and begin processing
4074     try
4075     {
4076         Mat src = _src.getMat();
4077         Mat dst = _dst.getMat();
4078         ::ipp::IwiImage       iwSrc      = ippiGetImage(src);
4079         ::ipp::IwiImage       iwDst      = ippiGetImage(dst);
4080         ::ipp::IwiBorderSize  borderSize = ::ipp::iwiSizeToBorderSize(ippiGetSize(ksize));
4081         ::ipp::IwiBorderType  ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
4082         if(!ippBorder)
4083             return false;
4084
4085         const int threads = ippiSuggestThreadsNum(iwDst, 2);
4086         if(IPP_GAUSSIANBLUR_PARALLEL && threads > 1) {
4087             bool ok;
4088             ipp_gaussianBlurParallel invoker(iwSrc, iwDst, ksize.width, (float) sigma1, ippBorder, &ok);
4089
4090             if(!ok)
4091                 return false;
4092             const Range range(0, (int) iwDst.m_size.height);
4093             parallel_for_(range, invoker, threads*4);
4094
4095             if(!ok)
4096                 return false;
4097         } else {
4098             CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, iwSrc, iwDst, ksize.width, sigma1, ::ipp::IwDefault(), ippBorder);
4099         }
4100     }
4101     catch (::ipp::IwException ex)
4102     {
4103         return false;
4104     }
4105
4106     return true;
4107 #endif
4108 #else
4109     CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4110     return false;
4111 #endif
4112 }
4113 #endif
4114 }
4115
4116 void cv::GaussianBlur( InputArray _src, OutputArray _dst, Size ksize,
4117                    double sigma1, double sigma2,
4118                    int borderType )
4119 {
4120     CV_INSTRUMENT_REGION()
4121
4122     int type = _src.type();
4123     Size size = _src.size();
4124     _dst.create( size, type );
4125
4126     if( (borderType & ~BORDER_ISOLATED) != BORDER_CONSTANT &&
4127         ((borderType & BORDER_ISOLATED) != 0 || !_src.getMat().isSubmatrix()) )
4128     {
4129         if( size.height == 1 )
4130             ksize.height = 1;
4131         if( size.width == 1 )
4132             ksize.width = 1;
4133     }
4134
4135     if( ksize.width == 1 && ksize.height == 1 )
4136     {
4137         _src.copyTo(_dst);
4138         return;
4139     }
4140
4141     bool useOpenCL = (ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
4142                ((ksize.width == 3 && ksize.height == 3) ||
4143                (ksize.width == 5 && ksize.height == 5)) &&
4144                _src.rows() > ksize.height && _src.cols() > ksize.width);
4145     (void)useOpenCL;
4146
4147     int sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4148
4149     if(sdepth == CV_8U && ((borderType & BORDER_ISOLATED) || !_src.getMat().isSubmatrix()))
4150     {
4151         std::vector<ufixedpoint16> fkx, fky;
4152         createGaussianKernels(fkx, fky, type, ksize, sigma1, sigma2);
4153         Mat src = _src.getMat();
4154         Mat dst = _dst.getMat();
4155         if (src.data == dst.data)
4156             src = src.clone();
4157         fixedSmoothInvoker<uint8_t, ufixedpoint16> invoker(src.ptr<uint8_t>(), src.step1(), dst.ptr<uint8_t>(), dst.step1(), dst.cols, dst.rows, dst.channels(), &fkx[0], (int)fkx.size(), &fky[0], (int)fky.size(), borderType & ~BORDER_ISOLATED);
4158         parallel_for_(Range(0, dst.rows), invoker, std::max(1, std::min(getNumThreads(), getNumberOfCPUs())));
4159         return;
4160     }
4161
4162
4163     Mat kx, ky;
4164     createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
4165
4166     CV_OCL_RUN(useOpenCL, ocl_GaussianBlur_8UC1(_src, _dst, ksize, CV_MAT_DEPTH(type), kx, ky, borderType));
4167
4168     CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
4169                ocl_sepFilter2D(_src, _dst, sdepth, kx, ky, Point(-1, -1), 0, borderType))
4170
4171     Mat src = _src.getMat();
4172     Mat dst = _dst.getMat();
4173
4174     Point ofs;
4175     Size wsz(src.cols, src.rows);
4176     if(!(borderType & BORDER_ISOLATED))
4177         src.locateROI( wsz, ofs );
4178
4179     CALL_HAL(gaussianBlur, cv_hal_gaussianBlur, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, cn,
4180              ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
4181              sigma1, sigma2, borderType&~BORDER_ISOLATED);
4182
4183     CV_OVX_RUN(true,
4184                openvx_gaussianBlur(src, dst, ksize, sigma1, sigma2, borderType))
4185
4186     CV_IPP_RUN_FAST(ipp_GaussianBlur(src, dst, ksize, sigma1, sigma2, borderType));
4187
4188     sepFilter2D(src, dst, sdepth, kx, ky, Point(-1, -1), 0, borderType);
4189 }
4190
4191 /****************************************************************************************\
4192                                       Median Filter
4193 \****************************************************************************************/
4194
4195 namespace cv
4196 {
4197 typedef ushort HT;
4198
4199 /**
4200  * This structure represents a two-tier histogram. The first tier (known as the
4201  * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
4202  * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
4203  * coarse bucket designated by the 4 MSBs of the fine bucket value.
4204  *
4205  * The structure is aligned on 16 bits, which is a prerequisite for SIMD
4206  * instructions. Each bucket is 16 bit wide, which means that extra care must be
4207  * taken to prevent overflow.
4208  */
4209 typedef struct
4210 {
4211     HT coarse[16];
4212     HT fine[16][16];
4213 } Histogram;
4214
4215
4216 #if CV_SIMD128
4217
4218 static inline void histogram_add_simd( const HT x[16], HT y[16] )
4219 {
4220     v_store(y, v_load(x) + v_load(y));
4221     v_store(y + 8, v_load(x + 8) + v_load(y + 8));
4222 }
4223
4224 static inline void histogram_sub_simd( const HT x[16], HT y[16] )
4225 {
4226     v_store(y, v_load(y) - v_load(x));
4227     v_store(y + 8, v_load(y + 8) - v_load(x + 8));
4228 }
4229
4230 #endif
4231
4232
4233 static inline void histogram_add( const HT x[16], HT y[16] )
4234 {
4235     int i;
4236     for( i = 0; i < 16; ++i )
4237         y[i] = (HT)(y[i] + x[i]);
4238 }
4239
4240 static inline void histogram_sub( const HT x[16], HT y[16] )
4241 {
4242     int i;
4243     for( i = 0; i < 16; ++i )
4244         y[i] = (HT)(y[i] - x[i]);
4245 }
4246
4247 static inline void histogram_muladd( int a, const HT x[16],
4248         HT y[16] )
4249 {
4250     for( int i = 0; i < 16; ++i )
4251         y[i] = (HT)(y[i] + a * x[i]);
4252 }
4253
4254 static void
4255 medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
4256 {
4257 /**
4258  * HOP is short for Histogram OPeration. This macro makes an operation \a op on
4259  * histogram \a h for pixel value \a x. It takes care of handling both levels.
4260  */
4261 #define HOP(h,x,op) \
4262     h.coarse[x>>4] op, \
4263     *((HT*)h.fine + x) op
4264
4265 #define COP(c,j,x,op) \
4266     h_coarse[ 16*(n*c+j) + (x>>4) ] op, \
4267     h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op
4268
4269     int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2;
4270     CV_Assert(cn > 0 && cn <= 4);
4271     size_t sstep = _src.step, dstep = _dst.step;
4272     Histogram CV_DECL_ALIGNED(16) H[4];
4273     HT CV_DECL_ALIGNED(16) luc[4][16];
4274
4275     int STRIPE_SIZE = std::min( _dst.cols, 512/cn );
4276
4277     std::vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4278     std::vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4279     HT* h_coarse = alignPtr(&_h_coarse[0], 16);
4280     HT* h_fine = alignPtr(&_h_fine[0], 16);
4281 #if CV_SIMD128
4282     volatile bool useSIMD = hasSIMD128();
4283 #endif
4284
4285     for( int x = 0; x < _dst.cols; x += STRIPE_SIZE )
4286     {
4287         int i, j, k, c, n = std::min(_dst.cols - x, STRIPE_SIZE) + r*2;
4288         const uchar* src = _src.ptr() + x*cn;
4289         uchar* dst = _dst.ptr() + (x - r)*cn;
4290
4291         memset( h_coarse, 0, 16*n*cn*sizeof(h_coarse[0]) );
4292         memset( h_fine, 0, 16*16*n*cn*sizeof(h_fine[0]) );
4293
4294         // First row initialization
4295         for( c = 0; c < cn; c++ )
4296         {
4297             for( j = 0; j < n; j++ )
4298                 COP( c, j, src[cn*j+c], += (cv::HT)(r+2) );
4299
4300             for( i = 1; i < r; i++ )
4301             {
4302                 const uchar* p = src + sstep*std::min(i, m-1);
4303                 for ( j = 0; j < n; j++ )
4304                     COP( c, j, p[cn*j+c], ++ );
4305             }
4306         }
4307
4308         for( i = 0; i < m; i++ )
4309         {
4310             const uchar* p0 = src + sstep * std::max( 0, i-r-1 );
4311             const uchar* p1 = src + sstep * std::min( m-1, i+r );
4312
4313             memset( H, 0, cn*sizeof(H[0]) );
4314             memset( luc, 0, cn*sizeof(luc[0]) );
4315             for( c = 0; c < cn; c++ )
4316             {
4317                 // Update column histograms for the entire row.
4318                 for( j = 0; j < n; j++ )
4319                 {
4320                     COP( c, j, p0[j*cn + c], -- );
4321                     COP( c, j, p1[j*cn + c], ++ );
4322                 }
4323
4324                 // First column initialization
4325                 for( k = 0; k < 16; ++k )
4326                     histogram_muladd( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].fine[k][0] );
4327
4328 #if CV_SIMD128
4329                 if( useSIMD )
4330                 {
4331                     for( j = 0; j < 2*r; ++j )
4332                         histogram_add_simd( &h_coarse[16*(n*c+j)], H[c].coarse );
4333
4334                     for( j = r; j < n-r; j++ )
4335                     {
4336                         int t = 2*r*r + 2*r, b, sum = 0;
4337                         HT* segment;
4338
4339                         histogram_add_simd( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4340
4341                         // Find median at coarse level
4342                         for ( k = 0; k < 16 ; ++k )
4343                         {
4344                             sum += H[c].coarse[k];
4345                             if ( sum > t )
4346                             {
4347                                 sum -= H[c].coarse[k];
4348                                 break;
4349                             }
4350                         }
4351                         CV_Assert( k < 16 );
4352
4353                         /* Update corresponding histogram segment */
4354                         if ( luc[c][k] <= j-r )
4355                         {
4356                             memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4357                             for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4358                                 histogram_add_simd( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4359
4360                             if ( luc[c][k] < j+r+1 )
4361                             {
4362                                 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4363                                 luc[c][k] = (HT)(j+r+1);
4364                             }
4365                         }
4366                         else
4367                         {
4368                             for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4369                             {
4370                                 histogram_sub_simd( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4371                                 histogram_add_simd( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4372                             }
4373                         }
4374
4375                         histogram_sub_simd( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4376
4377                         /* Find median in segment */
4378                         segment = H[c].fine[k];
4379                         for ( b = 0; b < 16 ; b++ )
4380                         {
4381                             sum += segment[b];
4382                             if ( sum > t )
4383                             {
4384                                 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4385                                 break;
4386                             }
4387                         }
4388                         CV_Assert( b < 16 );
4389                     }
4390                 }
4391                 else
4392 #endif
4393                 {
4394                     for( j = 0; j < 2*r; ++j )
4395                         histogram_add( &h_coarse[16*(n*c+j)], H[c].coarse );
4396
4397                     for( j = r; j < n-r; j++ )
4398                     {
4399                         int t = 2*r*r + 2*r, b, sum = 0;
4400                         HT* segment;
4401
4402                         histogram_add( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4403
4404                         // Find median at coarse level
4405                         for ( k = 0; k < 16 ; ++k )
4406                         {
4407                             sum += H[c].coarse[k];
4408                             if ( sum > t )
4409                             {
4410                                 sum -= H[c].coarse[k];
4411                                 break;
4412                             }
4413                         }
4414                         CV_Assert( k < 16 );
4415
4416                         /* Update corresponding histogram segment */
4417                         if ( luc[c][k] <= j-r )
4418                         {
4419                             memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4420                             for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4421                                 histogram_add( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4422
4423                             if ( luc[c][k] < j+r+1 )
4424                             {
4425                                 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4426                                 luc[c][k] = (HT)(j+r+1);
4427                             }
4428                         }
4429                         else
4430                         {
4431                             for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4432                             {
4433                                 histogram_sub( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4434                                 histogram_add( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4435                             }
4436                         }
4437
4438                         histogram_sub( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4439
4440                         /* Find median in segment */
4441                         segment = H[c].fine[k];
4442                         for ( b = 0; b < 16 ; b++ )
4443                         {
4444                             sum += segment[b];
4445                             if ( sum > t )
4446                             {
4447                                 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4448                                 break;
4449                             }
4450                         }
4451                         CV_Assert( b < 16 );
4452                     }
4453                 }
4454             }
4455         }
4456     }
4457
4458 #undef HOP
4459 #undef COP
4460 }
4461
4462 static void
4463 medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m )
4464 {
4465     #define N  16
4466     int     zone0[4][N];
4467     int     zone1[4][N*N];
4468     int     x, y;
4469     int     n2 = m*m/2;
4470     Size    size = _dst.size();
4471     const uchar* src = _src.ptr();
4472     uchar*  dst = _dst.ptr();
4473     int     src_step = (int)_src.step, dst_step = (int)_dst.step;
4474     int     cn = _src.channels();
4475     const uchar*  src_max = src + size.height*src_step;
4476     CV_Assert(cn > 0 && cn <= 4);
4477
4478     #define UPDATE_ACC01( pix, cn, op ) \
4479     {                                   \
4480         int p = (pix);                  \
4481         zone1[cn][p] op;                \
4482         zone0[cn][p >> 4] op;           \
4483     }
4484
4485     //CV_Assert( size.height >= nx && size.width >= nx );
4486     for( x = 0; x < size.width; x++, src += cn, dst += cn )
4487     {
4488         uchar* dst_cur = dst;
4489         const uchar* src_top = src;
4490         const uchar* src_bottom = src;
4491         int k, c;
4492         int src_step1 = src_step, dst_step1 = dst_step;
4493
4494         if( x % 2 != 0 )
4495         {
4496             src_bottom = src_top += src_step*(size.height-1);
4497             dst_cur += dst_step*(size.height-1);
4498             src_step1 = -src_step1;
4499             dst_step1 = -dst_step1;
4500         }
4501
4502         // init accumulator
4503         memset( zone0, 0, sizeof(zone0[0])*cn );
4504         memset( zone1, 0, sizeof(zone1[0])*cn );
4505
4506         for( y = 0; y <= m/2; y++ )
4507         {
4508             for( c = 0; c < cn; c++ )
4509             {
4510                 if( y > 0 )
4511                 {
4512                     for( k = 0; k < m*cn; k += cn )
4513                         UPDATE_ACC01( src_bottom[k+c], c, ++ );
4514                 }
4515                 else
4516                 {
4517                     for( k = 0; k < m*cn; k += cn )
4518                         UPDATE_ACC01( src_bottom[k+c], c, += m/2+1 );
4519                 }
4520             }
4521
4522             if( (src_step1 > 0 && y < size.height-1) ||
4523                 (src_step1 < 0 && size.height-y-1 > 0) )
4524                 src_bottom += src_step1;
4525         }
4526
4527         for( y = 0; y < size.height; y++, dst_cur += dst_step1 )
4528         {
4529             // find median
4530             for( c = 0; c < cn; c++ )
4531             {
4532                 int s = 0;
4533                 for( k = 0; ; k++ )
4534                 {
4535                     int t = s + zone0[c][k];
4536                     if( t > n2 ) break;
4537                     s = t;
4538                 }
4539
4540                 for( k *= N; ;k++ )
4541                 {
4542                     s += zone1[c][k];
4543                     if( s > n2 ) break;
4544                 }
4545
4546                 dst_cur[c] = (uchar)k;
4547             }
4548
4549             if( y+1 == size.height )
4550                 break;
4551
4552             if( cn == 1 )
4553             {
4554                 for( k = 0; k < m; k++ )
4555                 {
4556                     int p = src_top[k];
4557                     int q = src_bottom[k];
4558                     zone1[0][p]--;
4559                     zone0[0][p>>4]--;
4560                     zone1[0][q]++;
4561                     zone0[0][q>>4]++;
4562                 }
4563             }
4564             else if( cn == 3 )
4565             {
4566                 for( k = 0; k < m*3; k += 3 )
4567                 {
4568                     UPDATE_ACC01( src_top[k], 0, -- );
4569                     UPDATE_ACC01( src_top[k+1], 1, -- );
4570                     UPDATE_ACC01( src_top[k+2], 2, -- );
4571
4572                     UPDATE_ACC01( src_bottom[k], 0, ++ );
4573                     UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4574                     UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4575                 }
4576             }
4577             else
4578             {
4579                 assert( cn == 4 );
4580                 for( k = 0; k < m*4; k += 4 )
4581                 {
4582                     UPDATE_ACC01( src_top[k], 0, -- );
4583                     UPDATE_ACC01( src_top[k+1], 1, -- );
4584                     UPDATE_ACC01( src_top[k+2], 2, -- );
4585                     UPDATE_ACC01( src_top[k+3], 3, -- );
4586
4587                     UPDATE_ACC01( src_bottom[k], 0, ++ );
4588                     UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4589                     UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4590                     UPDATE_ACC01( src_bottom[k+3], 3, ++ );
4591                 }
4592             }
4593
4594             if( (src_step1 > 0 && src_bottom + src_step1 < src_max) ||
4595                 (src_step1 < 0 && src_bottom + src_step1 >= src) )
4596                 src_bottom += src_step1;
4597
4598             if( y >= m/2 )
4599                 src_top += src_step1;
4600         }
4601     }
4602 #undef N
4603 #undef UPDATE_ACC
4604 }
4605
4606
4607 struct MinMax8u
4608 {
4609     typedef uchar value_type;
4610     typedef int arg_type;
4611     enum { SIZE = 1 };
4612     arg_type load(const uchar* ptr) { return *ptr; }
4613     void store(uchar* ptr, arg_type val) { *ptr = (uchar)val; }
4614     void operator()(arg_type& a, arg_type& b) const
4615     {
4616         int t = CV_FAST_CAST_8U(a - b);
4617         b += t; a -= t;
4618     }
4619 };
4620
4621 struct MinMax16u
4622 {
4623     typedef ushort value_type;
4624     typedef int arg_type;
4625     enum { SIZE = 1 };
4626     arg_type load(const ushort* ptr) { return *ptr; }
4627     void store(ushort* ptr, arg_type val) { *ptr = (ushort)val; }
4628     void operator()(arg_type& a, arg_type& b) const
4629     {
4630         arg_type t = a;
4631         a = std::min(a, b);
4632         b = std::max(b, t);
4633     }
4634 };
4635
4636 struct MinMax16s
4637 {
4638     typedef short value_type;
4639     typedef int arg_type;
4640     enum { SIZE = 1 };
4641     arg_type load(const short* ptr) { return *ptr; }
4642     void store(short* ptr, arg_type val) { *ptr = (short)val; }
4643     void operator()(arg_type& a, arg_type& b) const
4644     {
4645         arg_type t = a;
4646         a = std::min(a, b);
4647         b = std::max(b, t);
4648     }
4649 };
4650
4651 struct MinMax32f
4652 {
4653     typedef float value_type;
4654     typedef float arg_type;
4655     enum { SIZE = 1 };
4656     arg_type load(const float* ptr) { return *ptr; }
4657     void store(float* ptr, arg_type val) { *ptr = val; }
4658     void operator()(arg_type& a, arg_type& b) const
4659     {
4660         arg_type t = a;
4661         a = std::min(a, b);
4662         b = std::max(b, t);
4663     }
4664 };
4665
4666 #if CV_SIMD128
4667
4668 struct MinMaxVec8u
4669 {
4670     typedef uchar value_type;
4671     typedef v_uint8x16 arg_type;
4672     enum { SIZE = 16 };
4673     arg_type load(const uchar* ptr) { return v_load(ptr); }
4674     void store(uchar* ptr, const arg_type &val) { v_store(ptr, val); }
4675     void operator()(arg_type& a, arg_type& b) const
4676     {
4677         arg_type t = a;
4678         a = v_min(a, b);
4679         b = v_max(b, t);
4680     }
4681 };
4682
4683
4684 struct MinMaxVec16u
4685 {
4686     typedef ushort value_type;
4687     typedef v_uint16x8 arg_type;
4688     enum { SIZE = 8 };
4689     arg_type load(const ushort* ptr) { return v_load(ptr); }
4690     void store(ushort* ptr, const arg_type &val) { v_store(ptr, val); }
4691     void operator()(arg_type& a, arg_type& b) const
4692     {
4693         arg_type t = a;
4694         a = v_min(a, b);
4695         b = v_max(b, t);
4696     }
4697 };
4698
4699
4700 struct MinMaxVec16s
4701 {
4702     typedef short value_type;
4703     typedef v_int16x8 arg_type;
4704     enum { SIZE = 8 };
4705     arg_type load(const short* ptr) { return v_load(ptr); }
4706     void store(short* ptr, const arg_type &val) { v_store(ptr, val); }
4707     void operator()(arg_type& a, arg_type& b) const
4708     {
4709         arg_type t = a;
4710         a = v_min(a, b);
4711         b = v_max(b, t);
4712     }
4713 };
4714
4715
4716 struct MinMaxVec32f
4717 {
4718     typedef float value_type;
4719     typedef v_float32x4 arg_type;
4720     enum { SIZE = 4 };
4721     arg_type load(const float* ptr) { return v_load(ptr); }
4722     void store(float* ptr, const arg_type &val) { v_store(ptr, val); }
4723     void operator()(arg_type& a, arg_type& b) const
4724     {
4725         arg_type t = a;
4726         a = v_min(a, b);
4727         b = v_max(b, t);
4728     }
4729 };
4730
4731 #else
4732
4733 typedef MinMax8u MinMaxVec8u;
4734 typedef MinMax16u MinMaxVec16u;
4735 typedef MinMax16s MinMaxVec16s;
4736 typedef MinMax32f MinMaxVec32f;
4737
4738 #endif
4739
4740 template<class Op, class VecOp>
4741 static void
4742 medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
4743 {
4744     typedef typename Op::value_type T;
4745     typedef typename Op::arg_type WT;
4746     typedef typename VecOp::arg_type VT;
4747
4748     const T* src = _src.ptr<T>();
4749     T* dst = _dst.ptr<T>();
4750     int sstep = (int)(_src.step/sizeof(T));
4751     int dstep = (int)(_dst.step/sizeof(T));
4752     Size size = _dst.size();
4753     int i, j, k, cn = _src.channels();
4754     Op op;
4755     VecOp vop;
4756     volatile bool useSIMD = hasSIMD128();
4757
4758     if( m == 3 )
4759     {
4760         if( size.width == 1 || size.height == 1 )
4761         {
4762             int len = size.width + size.height - 1;
4763             int sdelta = size.height == 1 ? cn : sstep;
4764             int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4765             int ddelta = size.height == 1 ? cn : dstep;
4766
4767             for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4768                 for( j = 0; j < cn; j++, src++ )
4769                 {
4770                     WT p0 = src[i > 0 ? -sdelta : 0];
4771                     WT p1 = src[0];
4772                     WT p2 = src[i < len - 1 ? sdelta : 0];
4773
4774                     op(p0, p1); op(p1, p2); op(p0, p1);
4775                     dst[j] = (T)p1;
4776                 }
4777             return;
4778         }
4779
4780         size.width *= cn;
4781         for( i = 0; i < size.height; i++, dst += dstep )
4782         {
4783             const T* row0 = src + std::max(i - 1, 0)*sstep;
4784             const T* row1 = src + i*sstep;
4785             const T* row2 = src + std::min(i + 1, size.height-1)*sstep;
4786             int limit = useSIMD ? cn : size.width;
4787
4788             for(j = 0;; )
4789             {
4790                 for( ; j < limit; j++ )
4791                 {
4792                     int j0 = j >= cn ? j - cn : j;
4793                     int j2 = j < size.width - cn ? j + cn : j;
4794                     WT p0 = row0[j0], p1 = row0[j], p2 = row0[j2];
4795                     WT p3 = row1[j0], p4 = row1[j], p5 = row1[j2];
4796                     WT p6 = row2[j0], p7 = row2[j], p8 = row2[j2];
4797
4798                     op(p1, p2); op(p4, p5); op(p7, p8); op(p0, p1);
4799                     op(p3, p4); op(p6, p7); op(p1, p2); op(p4, p5);
4800                     op(p7, p8); op(p0, p3); op(p5, p8); op(p4, p7);
4801                     op(p3, p6); op(p1, p4); op(p2, p5); op(p4, p7);
4802                     op(p4, p2); op(p6, p4); op(p4, p2);
4803                     dst[j] = (T)p4;
4804                 }
4805
4806                 if( limit == size.width )
4807                     break;
4808
4809                 for( ; j <= size.width - VecOp::SIZE - cn; j += VecOp::SIZE )
4810                 {
4811                     VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
4812                     VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
4813                     VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);
4814
4815                     vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
4816                     vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
4817                     vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
4818                     vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
4819                     vop(p4, p2); vop(p6, p4); vop(p4, p2);
4820                     vop.store(dst+j, p4);
4821                 }
4822
4823                 limit = size.width;
4824             }
4825         }
4826     }
4827     else if( m == 5 )
4828     {
4829         if( size.width == 1 || size.height == 1 )
4830         {
4831             int len = size.width + size.height - 1;
4832             int sdelta = size.height == 1 ? cn : sstep;
4833             int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4834             int ddelta = size.height == 1 ? cn : dstep;
4835
4836             for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4837                 for( j = 0; j < cn; j++, src++ )
4838                 {
4839                     int i1 = i > 0 ? -sdelta : 0;
4840                     int i0 = i > 1 ? -sdelta*2 : i1;
4841                     int i3 = i < len-1 ? sdelta : 0;
4842                     int i4 = i < len-2 ? sdelta*2 : i3;
4843                     WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];
4844
4845                     op(p0, p1); op(p3, p4); op(p2, p3); op(p3, p4); op(p0, p2);
4846                     op(p2, p4); op(p1, p3); op(p1, p2);
4847                     dst[j] = (T)p2;
4848                 }
4849             return;
4850         }
4851
4852         size.width *= cn;
4853         for( i = 0; i < size.height; i++, dst += dstep )
4854         {
4855             const T* row[5];
4856             row[0] = src + std::max(i - 2, 0)*sstep;
4857             row[1] = src + std::max(i - 1, 0)*sstep;
4858             row[2] = src + i*sstep;
4859             row[3] = src + std::min(i + 1, size.height-1)*sstep;
4860             row[4] = src + std::min(i + 2, size.height-1)*sstep;
4861             int limit = useSIMD ? cn*2 : size.width;
4862
4863             for(j = 0;; )
4864             {
4865                 for( ; j < limit; j++ )
4866                 {
4867                     WT p[25];
4868                     int j1 = j >= cn ? j - cn : j;
4869                     int j0 = j >= cn*2 ? j - cn*2 : j1;
4870                     int j3 = j < size.width - cn ? j + cn : j;
4871                     int j4 = j < size.width - cn*2 ? j + cn*2 : j3;
4872                     for( k = 0; k < 5; k++ )
4873                     {
4874                         const T* rowk = row[k];
4875                         p[k*5] = rowk[j0]; p[k*5+1] = rowk[j1];
4876                         p[k*5+2] = rowk[j]; p[k*5+3] = rowk[j3];
4877                         p[k*5+4] = rowk[j4];
4878                     }
4879
4880                     op(p[1], p[2]); op(p[0], p[1]); op(p[1], p[2]); op(p[4], p[5]); op(p[3], p[4]);
4881                     op(p[4], p[5]); op(p[0], p[3]); op(p[2], p[5]); op(p[2], p[3]); op(p[1], p[4]);
4882                     op(p[1], p[2]); op(p[3], p[4]); op(p[7], p[8]); op(p[6], p[7]); op(p[7], p[8]);
4883                     op(p[10], p[11]); op(p[9], p[10]); op(p[10], p[11]); op(p[6], p[9]); op(p[8], p[11]);
4884                     op(p[8], p[9]); op(p[7], p[10]); op(p[7], p[8]); op(p[9], p[10]); op(p[0], p[6]);
4885                     op(p[4], p[10]); op(p[4], p[6]); op(p[2], p[8]); op(p[2], p[4]); op(p[6], p[8]);
4886                     op(p[1], p[7]); op(p[5], p[11]); op(p[5], p[7]); op(p[3], p[9]); op(p[3], p[5]);
4887                     op(p[7], p[9]); op(p[1], p[2]); op(p[3], p[4]); op(p[5], p[6]); op(p[7], p[8]);
4888                     op(p[9], p[10]); op(p[13], p[14]); op(p[12], p[13]); op(p[13], p[14]); op(p[16], p[17]);
4889                     op(p[15], p[16]); op(p[16], p[17]); op(p[12], p[15]); op(p[14], p[17]); op(p[14], p[15]);
4890                     op(p[13], p[16]); op(p[13], p[14]); op(p[15], p[16]); op(p[19], p[20]); op(p[18], p[19]);
4891                     op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[21], p[23]); op(p[22], p[24]);
4892                     op(p[22], p[23]); op(p[18], p[21]); op(p[20], p[23]); op(p[20], p[21]); op(p[19], p[22]);
4893                     op(p[22], p[24]); op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[12], p[18]);
4894                     op(p[16], p[22]); op(p[16], p[18]); op(p[14], p[20]); op(p[20], p[24]); op(p[14], p[16]);
4895                     op(p[18], p[20]); op(p[22], p[24]); op(p[13], p[19]); op(p[17], p[23]); op(p[17], p[19]);
4896                     op(p[15], p[21]); op(p[15], p[17]); op(p[19], p[21]); op(p[13], p[14]); op(p[15], p[16]);
4897                     op(p[17], p[18]); op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[0], p[12]);
4898                     op(p[8], p[20]); op(p[8], p[12]); op(p[4], p[16]); op(p[16], p[24]); op(p[12], p[16]);
4899                     op(p[2], p[14]); op(p[10], p[22]); op(p[10], p[14]); op(p[6], p[18]); op(p[6], p[10]);
4900                     op(p[10], p[12]); op(p[1], p[13]); op(p[9], p[21]); op(p[9], p[13]); op(p[5], p[17]);
4901                     op(p[13], p[17]); op(p[3], p[15]); op(p[11], p[23]); op(p[11], p[15]); op(p[7], p[19]);
4902                     op(p[7], p[11]); op(p[11], p[13]); op(p[11], p[12]);
4903                     dst[j] = (T)p[12];
4904                 }
4905
4906                 if( limit == size.width )
4907                     break;
4908
4909                 for( ; j <= size.width - VecOp::SIZE - cn*2; j += VecOp::SIZE )
4910                 {
4911                     VT p[25];
4912                     for( k = 0; k < 5; k++ )
4913                     {
4914                         const T* rowk = row[k];
4915                         p[k*5] = vop.load(rowk+j-cn*2); p[k*5+1] = vop.load(rowk+j-cn);
4916                         p[k*5+2] = vop.load(rowk+j); p[k*5+3] = vop.load(rowk+j+cn);
4917                         p[k*5+4] = vop.load(rowk+j+cn*2);
4918                     }
4919
4920                     vop(p[1], p[2]); vop(p[0], p[1]); vop(p[1], p[2]); vop(p[4], p[5]); vop(p[3], p[4]);
4921                     vop(p[4], p[5]); vop(p[0], p[3]); vop(p[2], p[5]); vop(p[2], p[3]); vop(p[1], p[4]);
4922                     vop(p[1], p[2]); vop(p[3], p[4]); vop(p[7], p[8]); vop(p[6], p[7]); vop(p[7], p[8]);
4923                     vop(p[10], p[11]); vop(p[9], p[10]); vop(p[10], p[11]); vop(p[6], p[9]); vop(p[8], p[11]);
4924                     vop(p[8], p[9]); vop(p[7], p[10]); vop(p[7], p[8]); vop(p[9], p[10]); vop(p[0], p[6]);
4925                     vop(p[4], p[10]); vop(p[4], p[6]); vop(p[2], p[8]); vop(p[2], p[4]); vop(p[6], p[8]);
4926                     vop(p[1], p[7]); vop(p[5], p[11]); vop(p[5], p[7]); vop(p[3], p[9]); vop(p[3], p[5]);
4927                     vop(p[7], p[9]); vop(p[1], p[2]); vop(p[3], p[4]); vop(p[5], p[6]); vop(p[7], p[8]);
4928                     vop(p[9], p[10]); vop(p[13], p[14]); vop(p[12], p[13]); vop(p[13], p[14]); vop(p[16], p[17]);
4929                     vop(p[15], p[16]); vop(p[16], p[17]); vop(p[12], p[15]); vop(p[14], p[17]); vop(p[14], p[15]);
4930                     vop(p[13], p[16]); vop(p[13], p[14]); vop(p[15], p[16]); vop(p[19], p[20]); vop(p[18], p[19]);
4931                     vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[21], p[23]); vop(p[22], p[24]);
4932                     vop(p[22], p[23]); vop(p[18], p[21]); vop(p[20], p[23]); vop(p[20], p[21]); vop(p[19], p[22]);
4933                     vop(p[22], p[24]); vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[12], p[18]);
4934                     vop(p[16], p[22]); vop(p[16], p[18]); vop(p[14], p[20]); vop(p[20], p[24]); vop(p[14], p[16]);
4935                     vop(p[18], p[20]); vop(p[22], p[24]); vop(p[13], p[19]); vop(p[17], p[23]); vop(p[17], p[19]);
4936                     vop(p[15], p[21]); vop(p[15], p[17]); vop(p[19], p[21]); vop(p[13], p[14]); vop(p[15], p[16]);
4937                     vop(p[17], p[18]); vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[0], p[12]);
4938                     vop(p[8], p[20]); vop(p[8], p[12]); vop(p[4], p[16]); vop(p[16], p[24]); vop(p[12], p[16]);
4939                     vop(p[2], p[14]); vop(p[10], p[22]); vop(p[10], p[14]); vop(p[6], p[18]); vop(p[6], p[10]);
4940                     vop(p[10], p[12]); vop(p[1], p[13]); vop(p[9], p[21]); vop(p[9], p[13]); vop(p[5], p[17]);
4941                     vop(p[13], p[17]); vop(p[3], p[15]); vop(p[11], p[23]); vop(p[11], p[15]); vop(p[7], p[19]);
4942                     vop(p[7], p[11]); vop(p[11], p[13]); vop(p[11], p[12]);
4943                     vop.store(dst+j, p[12]);
4944                 }
4945
4946                 limit = size.width;
4947             }
4948         }
4949     }
4950 }
4951
4952 #ifdef HAVE_OPENCL
4953
4954 static bool ocl_medianFilter(InputArray _src, OutputArray _dst, int m)
4955 {
4956     size_t localsize[2] = { 16, 16 };
4957     size_t globalsize[2];
4958     int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4959
4960     if ( !((depth == CV_8U || depth == CV_16U || depth == CV_16S || depth == CV_32F) && cn <= 4 && (m == 3 || m == 5)) )
4961         return false;
4962
4963     Size imgSize = _src.size();
4964     bool useOptimized = (1 == cn) &&
4965                         (size_t)imgSize.width >= localsize[0] * 8  &&
4966                         (size_t)imgSize.height >= localsize[1] * 8 &&
4967                         imgSize.width % 4 == 0 &&
4968                         imgSize.height % 4 == 0 &&
4969                         (ocl::Device::getDefault().isIntel());
4970
4971     cv::String kname = format( useOptimized ? "medianFilter%d_u" : "medianFilter%d", m) ;
4972     cv::String kdefs = useOptimized ?
4973                          format("-D T=%s -D T1=%s -D T4=%s%d -D cn=%d -D USE_4OPT", ocl::typeToStr(type),
4974                          ocl::typeToStr(depth), ocl::typeToStr(depth), cn*4, cn)
4975                          :
4976                          format("-D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn) ;
4977
4978     ocl::Kernel k(kname.c_str(), ocl::imgproc::medianFilter_oclsrc, kdefs.c_str() );
4979
4980     if (k.empty())
4981         return false;
4982
4983     UMat src = _src.getUMat();
4984     _dst.create(src.size(), type);
4985     UMat dst = _dst.getUMat();
4986
4987     k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst));
4988
4989     if( useOptimized )
4990     {
4991         globalsize[0] = DIVUP(src.cols / 4, localsize[0]) * localsize[0];
4992         globalsize[1] = DIVUP(src.rows / 4, localsize[1]) * localsize[1];
4993     }
4994     else
4995     {
4996         globalsize[0] = (src.cols + localsize[0] + 2) / localsize[0] * localsize[0];
4997         globalsize[1] = (src.rows + localsize[1] - 1) / localsize[1] * localsize[1];
4998     }
4999
5000     return k.run(2, globalsize, localsize, false);
5001 }
5002
5003 #endif
5004
5005 }
5006
5007 #ifdef HAVE_OPENVX
5008 namespace cv
5009 {
5010     namespace ovx {
5011         template <> inline bool skipSmallImages<VX_KERNEL_MEDIAN_3x3>(int w, int h) { return w*h < 1280 * 720; }
5012     }
5013     static bool openvx_medianFilter(InputArray _src, OutputArray _dst, int ksize)
5014     {
5015         if (_src.type() != CV_8UC1 || _dst.type() != CV_8U
5016 #ifndef VX_VERSION_1_1
5017             || ksize != 3
5018 #endif
5019             )
5020             return false;
5021
5022         Mat src = _src.getMat();
5023         Mat dst = _dst.getMat();
5024
5025         if (
5026 #ifdef VX_VERSION_1_1
5027              ksize != 3 ? ovx::skipSmallImages<VX_KERNEL_NON_LINEAR_FILTER>(src.cols, src.rows) :
5028 #endif
5029              ovx::skipSmallImages<VX_KERNEL_MEDIAN_3x3>(src.cols, src.rows)
5030            )
5031             return false;
5032
5033         try
5034         {
5035             ivx::Context ctx = ovx::getOpenVXContext();
5036 #ifdef VX_VERSION_1_1
5037             if ((vx_size)ksize > ctx.nonlinearMaxDimension())
5038                 return false;
5039 #endif
5040
5041             Mat a;
5042             if (dst.data != src.data)
5043                 a = src;
5044             else
5045                 src.copyTo(a);
5046
5047             ivx::Image
5048                 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5049                     ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
5050                 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5051                     ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
5052
5053             //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
5054             //since OpenVX standard says nothing about thread-safety for now
5055             ivx::border_t prevBorder = ctx.immediateBorder();
5056             ctx.setImmediateBorder(VX_BORDER_REPLICATE);
5057 #ifdef VX_VERSION_1_1
5058             if (ksize == 3)
5059 #endif
5060             {
5061                 ivx::IVX_CHECK_STATUS(vxuMedian3x3(ctx, ia, ib));
5062             }
5063 #ifdef VX_VERSION_1_1
5064             else
5065             {
5066                 ivx::Matrix mtx;
5067                 if(ksize == 5)
5068                     mtx = ivx::Matrix::createFromPattern(ctx, VX_PATTERN_BOX, ksize, ksize);
5069                 else
5070                 {
5071                     vx_size supportedSize;
5072                     ivx::IVX_CHECK_STATUS(vxQueryContext(ctx, VX_CONTEXT_NONLINEAR_MAX_DIMENSION, &supportedSize, sizeof(supportedSize)));
5073                     if ((vx_size)ksize > supportedSize)
5074                     {
5075                         ctx.setImmediateBorder(prevBorder);
5076                         return false;
5077                     }
5078                     Mat mask(ksize, ksize, CV_8UC1, Scalar(255));
5079                     mtx = ivx::Matrix::create(ctx, VX_TYPE_UINT8, ksize, ksize);
5080                     mtx.copyFrom(mask);
5081                 }
5082                 ivx::IVX_CHECK_STATUS(vxuNonLinearFilter(ctx, VX_NONLINEAR_FILTER_MEDIAN, ia, mtx, ib));
5083             }
5084 #endif
5085             ctx.setImmediateBorder(prevBorder);
5086         }
5087         catch (ivx::RuntimeError & e)
5088         {
5089             VX_DbgThrow(e.what());
5090         }
5091         catch (ivx::WrapperError & e)
5092         {
5093             VX_DbgThrow(e.what());
5094         }
5095
5096         return true;
5097     }
5098 }
5099 #endif
5100
5101 #ifdef HAVE_IPP
5102 namespace cv
5103 {
5104 static bool ipp_medianFilter(Mat &src0, Mat &dst, int ksize)
5105 {
5106     CV_INSTRUMENT_REGION_IPP()
5107
5108 #if IPP_VERSION_X100 < 201801
5109     // Degradations for big kernel
5110     if(ksize > 7)
5111         return false;
5112 #endif
5113
5114     {
5115         int         bufSize;
5116         IppiSize    dstRoiSize = ippiSize(dst.cols, dst.rows), maskSize = ippiSize(ksize, ksize);
5117         IppDataType ippType = ippiGetDataType(src0.type());
5118         int         channels = src0.channels();
5119         IppAutoBuffer<Ipp8u> buffer;
5120
5121         if(src0.isSubmatrix())
5122             return false;
5123
5124         Mat src;
5125         if(dst.data != src0.data)
5126             src = src0;
5127         else
5128             src0.copyTo(src);
5129
5130         if(ippiFilterMedianBorderGetBufferSize(dstRoiSize, maskSize, ippType, channels, &bufSize) < 0)
5131             return false;
5132
5133         buffer.allocate(bufSize);
5134
5135         switch(ippType)
5136         {
5137         case ipp8u:
5138             if(channels == 1)
5139                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C1R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5140             else if(channels == 3)
5141                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C3R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5142             else if(channels == 4)
5143                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C4R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5144             else
5145                 return false;
5146         case ipp16u:
5147             if(channels == 1)
5148                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C1R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5149             else if(channels == 3)
5150                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C3R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5151             else if(channels == 4)
5152                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C4R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5153             else
5154                 return false;
5155         case ipp16s:
5156             if(channels == 1)
5157                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C1R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5158             else if(channels == 3)
5159                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C3R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5160             else if(channels == 4)
5161                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C4R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5162             else
5163                 return false;
5164         case ipp32f:
5165             if(channels == 1)
5166                 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_32f_C1R, src.ptr<Ipp32f>(), (int)src.step, dst.ptr<Ipp32f>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5167             else
5168                 return false;
5169         default:
5170             return false;
5171         }
5172     }
5173 }
5174 }
5175 #endif
5176
5177 void cv::medianBlur( InputArray _src0, OutputArray _dst, int ksize )
5178 {
5179     CV_INSTRUMENT_REGION()
5180
5181     CV_Assert( (ksize % 2 == 1) && (_src0.dims() <= 2 ));
5182
5183     if( ksize <= 1 || _src0.empty() )
5184     {
5185         _src0.copyTo(_dst);
5186         return;
5187     }
5188
5189     CV_OCL_RUN(_dst.isUMat(),
5190                ocl_medianFilter(_src0,_dst, ksize))
5191
5192     Mat src0 = _src0.getMat();
5193     _dst.create( src0.size(), src0.type() );
5194     Mat dst = _dst.getMat();
5195
5196     CALL_HAL(medianBlur, cv_hal_medianBlur, src0.data, src0.step, dst.data, dst.step, src0.cols, src0.rows, src0.depth(),
5197              src0.channels(), ksize);
5198
5199     CV_OVX_RUN(true,
5200                openvx_medianFilter(_src0, _dst, ksize))
5201
5202     CV_IPP_RUN_FAST(ipp_medianFilter(src0, dst, ksize));
5203
5204     bool useSortNet = ksize == 3 || (ksize == 5
5205 #if !(CV_SIMD128)
5206             && ( src0.depth() > CV_8U || src0.channels() == 2 || src0.channels() > 4 )
5207 #endif
5208         );
5209
5210     Mat src;
5211     if( useSortNet )
5212     {
5213         if( dst.data != src0.data )
5214             src = src0;
5215         else
5216             src0.copyTo(src);
5217
5218         if( src.depth() == CV_8U )
5219             medianBlur_SortNet<MinMax8u, MinMaxVec8u>( src, dst, ksize );
5220         else if( src.depth() == CV_16U )
5221             medianBlur_SortNet<MinMax16u, MinMaxVec16u>( src, dst, ksize );
5222         else if( src.depth() == CV_16S )
5223             medianBlur_SortNet<MinMax16s, MinMaxVec16s>( src, dst, ksize );
5224         else if( src.depth() == CV_32F )
5225             medianBlur_SortNet<MinMax32f, MinMaxVec32f>( src, dst, ksize );
5226         else
5227             CV_Error(CV_StsUnsupportedFormat, "");
5228
5229         return;
5230     }
5231     else
5232     {
5233         cv::copyMakeBorder( src0, src, 0, 0, ksize/2, ksize/2, BORDER_REPLICATE|BORDER_ISOLATED);
5234
5235         int cn = src0.channels();
5236         CV_Assert( src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4) );
5237
5238         double img_size_mp = (double)(src0.total())/(1 << 20);
5239         if( ksize <= 3 + (img_size_mp < 1 ? 12 : img_size_mp < 4 ? 6 : 2)*
5240             (CV_SIMD128 && hasSIMD128() ? 1 : 3))
5241             medianBlur_8u_Om( src, dst, ksize );
5242         else
5243             medianBlur_8u_O1( src, dst, ksize );
5244     }
5245 }
5246
5247 /****************************************************************************************\
5248                                    Bilateral Filtering
5249 \****************************************************************************************/
5250
5251 namespace cv
5252 {
5253
5254 class BilateralFilter_8u_Invoker :
5255     public ParallelLoopBody
5256 {
5257 public:
5258     BilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, int _radius, int _maxk,
5259         int* _space_ofs, float *_space_weight, float *_color_weight) :
5260         temp(&_temp), dest(&_dest), radius(_radius),
5261         maxk(_maxk), space_ofs(_space_ofs), space_weight(_space_weight), color_weight(_color_weight)
5262     {
5263     }
5264
5265     virtual void operator() (const Range& range) const CV_OVERRIDE
5266     {
5267         int i, j, cn = dest->channels(), k;
5268         Size size = dest->size();
5269 #if CV_SIMD128
5270         int CV_DECL_ALIGNED(16) buf[4];
5271         bool haveSIMD128 = hasSIMD128();
5272 #endif
5273
5274         for( i = range.start; i < range.end; i++ )
5275         {
5276             const uchar* sptr = temp->ptr(i+radius) + radius*cn;
5277             uchar* dptr = dest->ptr(i);
5278
5279             if( cn == 1 )
5280             {
5281                 for( j = 0; j < size.width; j++ )
5282                 {
5283                     float sum = 0, wsum = 0;
5284                     int val0 = sptr[j];
5285                     k = 0;
5286 #if CV_SIMD128
5287                     if( haveSIMD128 )
5288                     {
5289                         v_float32x4 _val0 = v_setall_f32(static_cast<float>(val0));
5290                         v_float32x4 vsumw = v_setzero_f32();
5291                         v_float32x4 vsumc = v_setzero_f32();
5292
5293                         for( ; k <= maxk - 4; k += 4 )
5294                         {
5295                             v_float32x4 _valF = v_float32x4(sptr[j + space_ofs[k]],
5296                                 sptr[j + space_ofs[k + 1]],
5297                                 sptr[j + space_ofs[k + 2]],
5298                                 sptr[j + space_ofs[k + 3]]);
5299                             v_float32x4 _val = v_abs(_valF - _val0);
5300                             v_store(buf, v_round(_val));
5301
5302                             v_float32x4 _cw = v_float32x4(color_weight[buf[0]],
5303                                 color_weight[buf[1]],
5304                                 color_weight[buf[2]],
5305                                 color_weight[buf[3]]);
5306                             v_float32x4 _sw = v_load(space_weight+k);
5307 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5308                             // details: https://github.com/opencv/opencv/issues/11004
5309                             vsumw += _cw * _sw;
5310                             vsumc += _cw * _sw * _valF;
5311 #else
5312                             v_float32x4 _w = _cw * _sw;
5313                             _cw = _w * _valF;
5314
5315                             vsumw += _w;
5316                             vsumc += _cw;
5317 #endif
5318                         }
5319                         float *bufFloat = (float*)buf;
5320                         v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumc, vsumw, vsumc);
5321                         v_store(bufFloat, sum4);
5322                         sum += bufFloat[1];
5323                         wsum += bufFloat[0];
5324                     }
5325 #endif
5326                     for( ; k < maxk; k++ )
5327                     {
5328                         int val = sptr[j + space_ofs[k]];
5329                         float w = space_weight[k]*color_weight[std::abs(val - val0)];
5330                         sum += val*w;
5331                         wsum += w;
5332                     }
5333                     // overflow is not possible here => there is no need to use cv::saturate_cast
5334                     CV_DbgAssert(fabs(wsum) > 0);
5335                     dptr[j] = (uchar)cvRound(sum/wsum);
5336                 }
5337             }
5338             else
5339             {
5340                 assert( cn == 3 );
5341                 for( j = 0; j < size.width*3; j += 3 )
5342                 {
5343                     float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5344                     int b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5345                     k = 0;
5346 #if CV_SIMD128
5347                     if( haveSIMD128 )
5348                     {
5349                         v_float32x4 vsumw = v_setzero_f32();
5350                         v_float32x4 vsumb = v_setzero_f32();
5351                         v_float32x4 vsumg = v_setzero_f32();
5352                         v_float32x4 vsumr = v_setzero_f32();
5353                         const v_float32x4 _b0 = v_setall_f32(static_cast<float>(b0));
5354                         const v_float32x4 _g0 = v_setall_f32(static_cast<float>(g0));
5355                         const v_float32x4 _r0 = v_setall_f32(static_cast<float>(r0));
5356
5357                         for( ; k <= maxk - 4; k += 4 )
5358                         {
5359                             const uchar* const sptr_k0  = sptr + j + space_ofs[k];
5360                             const uchar* const sptr_k1  = sptr + j + space_ofs[k+1];
5361                             const uchar* const sptr_k2  = sptr + j + space_ofs[k+2];
5362                             const uchar* const sptr_k3  = sptr + j + space_ofs[k+3];
5363
5364                             v_float32x4 __b = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k0)));
5365                             v_float32x4 __g = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k1)));
5366                             v_float32x4 __r = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k2)));
5367                             v_float32x4 __z = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k3)));
5368                             v_float32x4 _b, _g, _r, _z;
5369
5370                             v_transpose4x4(__b, __g, __r, __z, _b, _g, _r, _z);
5371
5372                             v_float32x4 bt = v_abs(_b -_b0);
5373                             v_float32x4 gt = v_abs(_g -_g0);
5374                             v_float32x4 rt = v_abs(_r -_r0);
5375
5376                             bt = rt + bt + gt;
5377                             v_store(buf, v_round(bt));
5378
5379                             v_float32x4 _w  = v_float32x4(color_weight[buf[0]],color_weight[buf[1]],
5380                                                     color_weight[buf[2]],color_weight[buf[3]]);
5381                             v_float32x4 _sw = v_load(space_weight+k);
5382
5383 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5384                             // details: https://github.com/opencv/opencv/issues/11004
5385                             vsumw += _w * _sw;
5386                             vsumb += _w * _sw * _b;
5387                             vsumg += _w * _sw * _g;
5388                             vsumr += _w * _sw * _r;
5389 #else
5390                             _w *= _sw;
5391                             _b *=  _w;
5392                             _g *=  _w;
5393                             _r *=  _w;
5394
5395                             vsumw += _w;
5396                             vsumb += _b;
5397                             vsumg += _g;
5398                             vsumr += _r;
5399 #endif
5400                         }
5401                         float *bufFloat = (float*)buf;
5402                         v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumb, vsumg, vsumr);
5403                         v_store(bufFloat, sum4);
5404                         wsum += bufFloat[0];
5405                         sum_b += bufFloat[1];
5406                         sum_g += bufFloat[2];
5407                         sum_r += bufFloat[3];
5408                     }
5409 #endif
5410
5411                     for( ; k < maxk; k++ )
5412                     {
5413                         const uchar* sptr_k = sptr + j + space_ofs[k];
5414                         int b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5415                         float w = space_weight[k]*color_weight[std::abs(b - b0) +
5416                                                                std::abs(g - g0) + std::abs(r - r0)];
5417                         sum_b += b*w; sum_g += g*w; sum_r += r*w;
5418                         wsum += w;
5419                     }
5420                     CV_DbgAssert(fabs(wsum) > 0);
5421                     wsum = 1.f/wsum;
5422                     b0 = cvRound(sum_b*wsum);
5423                     g0 = cvRound(sum_g*wsum);
5424                     r0 = cvRound(sum_r*wsum);
5425                     dptr[j] = (uchar)b0; dptr[j+1] = (uchar)g0; dptr[j+2] = (uchar)r0;
5426                 }
5427             }
5428         }
5429     }
5430
5431 private:
5432     const Mat *temp;
5433     Mat *dest;
5434     int radius, maxk, *space_ofs;
5435     float *space_weight, *color_weight;
5436 };
5437
5438 #ifdef HAVE_OPENCL
5439
5440 static bool ocl_bilateralFilter_8u(InputArray _src, OutputArray _dst, int d,
5441                                    double sigma_color, double sigma_space,
5442                                    int borderType)
5443 {
5444 #ifdef __ANDROID__
5445     if (ocl::Device::getDefault().isNVidia())
5446         return false;
5447 #endif
5448
5449     int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
5450     int i, j, maxk, radius;
5451
5452     if (depth != CV_8U || cn > 4)
5453         return false;
5454
5455     if (sigma_color <= 0)
5456         sigma_color = 1;
5457     if (sigma_space <= 0)
5458         sigma_space = 1;
5459
5460     double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
5461     double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
5462
5463     if ( d <= 0 )
5464         radius = cvRound(sigma_space * 1.5);
5465     else
5466         radius = d / 2;
5467     radius = MAX(radius, 1);
5468     d = radius * 2 + 1;
5469
5470     UMat src = _src.getUMat(), dst = _dst.getUMat(), temp;
5471     if (src.u == dst.u)
5472         return false;
5473
5474     copyMakeBorder(src, temp, radius, radius, radius, radius, borderType);
5475     std::vector<float> _space_weight(d * d);
5476     std::vector<int> _space_ofs(d * d);
5477     float * const space_weight = &_space_weight[0];
5478     int * const space_ofs = &_space_ofs[0];
5479
5480     // initialize space-related bilateral filter coefficients
5481     for( i = -radius, maxk = 0; i <= radius; i++ )
5482         for( j = -radius; j <= radius; j++ )
5483         {
5484             double r = std::sqrt((double)i * i + (double)j * j);
5485             if ( r > radius )
5486                 continue;
5487             space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
5488             space_ofs[maxk++] = (int)(i * temp.step + j * cn);
5489         }
5490
5491     char cvt[3][40];
5492     String cnstr = cn > 1 ? format("%d", cn) : "";
5493     String kernelName("bilateral");
5494     size_t sizeDiv = 1;
5495     if ((ocl::Device::getDefault().isIntel()) &&
5496         (ocl::Device::getDefault().type() == ocl::Device::TYPE_GPU))
5497     {
5498             //Intel GPU
5499             if (dst.cols % 4 == 0 && cn == 1) // For single channel x4 sized images.
5500             {
5501                 kernelName = "bilateral_float4";
5502                 sizeDiv = 4;
5503             }
5504      }
5505      ocl::Kernel k(kernelName.c_str(), ocl::imgproc::bilateral_oclsrc,
5506             format("-D radius=%d -D maxk=%d -D cn=%d -D int_t=%s -D uint_t=uint%s -D convert_int_t=%s"
5507             " -D uchar_t=%s -D float_t=%s -D convert_float_t=%s -D convert_uchar_t=%s -D gauss_color_coeff=(float)%f",
5508             radius, maxk, cn, ocl::typeToStr(CV_32SC(cn)), cnstr.c_str(),
5509             ocl::convertTypeStr(CV_8U, CV_32S, cn, cvt[0]),
5510             ocl::typeToStr(type), ocl::typeToStr(CV_32FC(cn)),
5511             ocl::convertTypeStr(CV_32S, CV_32F, cn, cvt[1]),
5512             ocl::convertTypeStr(CV_32F, CV_8U, cn, cvt[2]), gauss_color_coeff));
5513     if (k.empty())
5514         return false;
5515
5516     Mat mspace_weight(1, d * d, CV_32FC1, space_weight);
5517     Mat mspace_ofs(1, d * d, CV_32SC1, space_ofs);
5518     UMat ucolor_weight, uspace_weight, uspace_ofs;
5519
5520     mspace_weight.copyTo(uspace_weight);
5521     mspace_ofs.copyTo(uspace_ofs);
5522
5523     k.args(ocl::KernelArg::ReadOnlyNoSize(temp), ocl::KernelArg::WriteOnly(dst),
5524            ocl::KernelArg::PtrReadOnly(uspace_weight),
5525            ocl::KernelArg::PtrReadOnly(uspace_ofs));
5526
5527     size_t globalsize[2] = { (size_t)dst.cols / sizeDiv, (size_t)dst.rows };
5528     return k.run(2, globalsize, NULL, false);
5529 }
5530
5531 #endif
5532 static void
5533 bilateralFilter_8u( const Mat& src, Mat& dst, int d,
5534     double sigma_color, double sigma_space,
5535     int borderType )
5536 {
5537     int cn = src.channels();
5538     int i, j, maxk, radius;
5539     Size size = src.size();
5540
5541     CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data );
5542
5543     if( sigma_color <= 0 )
5544         sigma_color = 1;
5545     if( sigma_space <= 0 )
5546         sigma_space = 1;
5547
5548     double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5549     double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5550
5551     if( d <= 0 )
5552         radius = cvRound(sigma_space*1.5);
5553     else
5554         radius = d/2;
5555     radius = MAX(radius, 1);
5556     d = radius*2 + 1;
5557
5558     Mat temp;
5559     copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5560
5561     std::vector<float> _color_weight(cn*256);
5562     std::vector<float> _space_weight(d*d);
5563     std::vector<int> _space_ofs(d*d);
5564     float* color_weight = &_color_weight[0];
5565     float* space_weight = &_space_weight[0];
5566     int* space_ofs = &_space_ofs[0];
5567
5568     // initialize color-related bilateral filter coefficients
5569
5570     for( i = 0; i < 256*cn; i++ )
5571         color_weight[i] = (float)std::exp(i*i*gauss_color_coeff);
5572
5573     // initialize space-related bilateral filter coefficients
5574     for( i = -radius, maxk = 0; i <= radius; i++ )
5575     {
5576         j = -radius;
5577
5578         for( ; j <= radius; j++ )
5579         {
5580             double r = std::sqrt((double)i*i + (double)j*j);
5581             if( r > radius )
5582                 continue;
5583             space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5584             space_ofs[maxk++] = (int)(i*temp.step + j*cn);
5585         }
5586     }
5587
5588     BilateralFilter_8u_Invoker body(dst, temp, radius, maxk, space_ofs, space_weight, color_weight);
5589     parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5590 }
5591
5592
5593 class BilateralFilter_32f_Invoker :
5594     public ParallelLoopBody
5595 {
5596 public:
5597
5598     BilateralFilter_32f_Invoker(int _cn, int _radius, int _maxk, int *_space_ofs,
5599         const Mat& _temp, Mat& _dest, float _scale_index, float *_space_weight, float *_expLUT) :
5600         cn(_cn), radius(_radius), maxk(_maxk), space_ofs(_space_ofs),
5601         temp(&_temp), dest(&_dest), scale_index(_scale_index), space_weight(_space_weight), expLUT(_expLUT)
5602     {
5603     }
5604
5605     virtual void operator() (const Range& range) const CV_OVERRIDE
5606     {
5607         int i, j, k;
5608         Size size = dest->size();
5609 #if CV_SIMD128
5610         int CV_DECL_ALIGNED(16) idxBuf[4];
5611         bool haveSIMD128 = hasSIMD128();
5612 #endif
5613
5614         for( i = range.start; i < range.end; i++ )
5615         {
5616             const float* sptr = temp->ptr<float>(i+radius) + radius*cn;
5617             float* dptr = dest->ptr<float>(i);
5618
5619             if( cn == 1 )
5620             {
5621                 for( j = 0; j < size.width; j++ )
5622                 {
5623                     float sum = 0, wsum = 0;
5624                     float val0 = sptr[j];
5625                     k = 0;
5626 #if CV_SIMD128
5627                     if( haveSIMD128 )
5628                     {
5629                         v_float32x4 vecwsum = v_setzero_f32();
5630                         v_float32x4 vecvsum = v_setzero_f32();
5631                         const v_float32x4 _val0 = v_setall_f32(sptr[j]);
5632                         const v_float32x4 _scale_index = v_setall_f32(scale_index);
5633
5634                         for (; k <= maxk - 4; k += 4)
5635                         {
5636                             v_float32x4 _sw = v_load(space_weight + k);
5637                             v_float32x4 _val = v_float32x4(sptr[j + space_ofs[k]],
5638                                 sptr[j + space_ofs[k + 1]],
5639                                 sptr[j + space_ofs[k + 2]],
5640                                 sptr[j + space_ofs[k + 3]]);
5641                             v_float32x4 _alpha = v_abs(_val - _val0) * _scale_index;
5642
5643                             v_int32x4 _idx = v_round(_alpha);
5644                             v_store(idxBuf, _idx);
5645                             _alpha -= v_cvt_f32(_idx);
5646
5647                             v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5648                                 expLUT[idxBuf[1]],
5649                                 expLUT[idxBuf[2]],
5650                                 expLUT[idxBuf[3]]);
5651                             v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5652                                 expLUT[idxBuf[1] + 1],
5653                                 expLUT[idxBuf[2] + 1],
5654                                 expLUT[idxBuf[3] + 1]);
5655
5656                             v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5657                             _val *= _w;
5658
5659                             vecwsum += _w;
5660                             vecvsum += _val;
5661                         }
5662                         float *bufFloat = (float*)idxBuf;
5663                         v_float32x4 sum4 = v_reduce_sum4(vecwsum, vecvsum, vecwsum, vecvsum);
5664                         v_store(bufFloat, sum4);
5665                         sum += bufFloat[1];
5666                         wsum += bufFloat[0];
5667                     }
5668 #endif
5669
5670                     for( ; k < maxk; k++ )
5671                     {
5672                         float val = sptr[j + space_ofs[k]];
5673                         float alpha = (float)(std::abs(val - val0)*scale_index);
5674                         int idx = cvFloor(alpha);
5675                         alpha -= idx;
5676                         float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5677                         sum += val*w;
5678                         wsum += w;
5679                     }
5680                     CV_DbgAssert(fabs(wsum) > 0);
5681                     dptr[j] = (float)(sum/wsum);
5682                 }
5683             }
5684             else
5685             {
5686                 CV_Assert( cn == 3 );
5687                 for( j = 0; j < size.width*3; j += 3 )
5688                 {
5689                     float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5690                     float b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5691                     k = 0;
5692 #if CV_SIMD128
5693                     if( haveSIMD128 )
5694                     {
5695                         v_float32x4 sumw = v_setzero_f32();
5696                         v_float32x4 sumb = v_setzero_f32();
5697                         v_float32x4 sumg = v_setzero_f32();
5698                         v_float32x4 sumr = v_setzero_f32();
5699                         const v_float32x4 _b0 = v_setall_f32(b0);
5700                         const v_float32x4 _g0 = v_setall_f32(g0);
5701                         const v_float32x4 _r0 = v_setall_f32(r0);
5702                         const v_float32x4 _scale_index = v_setall_f32(scale_index);
5703
5704                         for( ; k <= maxk-4; k += 4 )
5705                         {
5706                             v_float32x4 _sw = v_load(space_weight + k);
5707
5708                             const float* const sptr_k0 = sptr + j + space_ofs[k];
5709                             const float* const sptr_k1 = sptr + j + space_ofs[k+1];
5710                             const float* const sptr_k2 = sptr + j + space_ofs[k+2];
5711                             const float* const sptr_k3 = sptr + j + space_ofs[k+3];
5712
5713                             v_float32x4 _v0 = v_load(sptr_k0);
5714                             v_float32x4 _v1 = v_load(sptr_k1);
5715                             v_float32x4 _v2 = v_load(sptr_k2);
5716                             v_float32x4 _v3 = v_load(sptr_k3);
5717                             v_float32x4 _b, _g, _r, _dummy;
5718
5719                             v_transpose4x4(_v0, _v1, _v2, _v3, _b, _g, _r, _dummy);
5720
5721                             v_float32x4 _bt = v_abs(_b - _b0);
5722                             v_float32x4 _gt = v_abs(_g - _g0);
5723                             v_float32x4 _rt = v_abs(_r - _r0);
5724                             v_float32x4 _alpha = _scale_index * (_bt + _gt + _rt);
5725
5726                             v_int32x4 _idx = v_round(_alpha);
5727                             v_store((int*)idxBuf, _idx);
5728                             _alpha -= v_cvt_f32(_idx);
5729
5730                             v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5731                                 expLUT[idxBuf[1]],
5732                                 expLUT[idxBuf[2]],
5733                                 expLUT[idxBuf[3]]);
5734                             v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5735                                 expLUT[idxBuf[1] + 1],
5736                                 expLUT[idxBuf[2] + 1],
5737                                 expLUT[idxBuf[3] + 1]);
5738
5739                             v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5740
5741                             _b *=  _w;
5742                             _g *=  _w;
5743                             _r *=  _w;
5744                             sumw += _w;
5745                             sumb += _b;
5746                             sumg += _g;
5747                             sumr += _r;
5748                         }
5749                         v_float32x4 sum4 = v_reduce_sum4(sumw, sumb, sumg, sumr);
5750                         float *bufFloat = (float*)idxBuf;
5751                         v_store(bufFloat, sum4);
5752                         wsum += bufFloat[0];
5753                         sum_b += bufFloat[1];
5754                         sum_g += bufFloat[2];
5755                         sum_r += bufFloat[3];
5756                     }
5757 #endif
5758
5759                     for(; k < maxk; k++ )
5760                     {
5761                         const float* sptr_k = sptr + j + space_ofs[k];
5762                         float b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5763                         float alpha = (float)((std::abs(b - b0) +
5764                             std::abs(g - g0) + std::abs(r - r0))*scale_index);
5765                         int idx = cvFloor(alpha);
5766                         alpha -= idx;
5767                         float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5768                         sum_b += b*w; sum_g += g*w; sum_r += r*w;
5769                         wsum += w;
5770                     }
5771                     CV_DbgAssert(fabs(wsum) > 0);
5772                     wsum = 1.f/wsum;
5773                     b0 = sum_b*wsum;
5774                     g0 = sum_g*wsum;
5775                     r0 = sum_r*wsum;
5776                     dptr[j] = b0; dptr[j+1] = g0; dptr[j+2] = r0;
5777                 }
5778             }
5779         }
5780     }
5781
5782 private:
5783     int cn, radius, maxk, *space_ofs;
5784     const Mat* temp;
5785     Mat *dest;
5786     float scale_index, *space_weight, *expLUT;
5787 };
5788
5789
5790 static void
5791 bilateralFilter_32f( const Mat& src, Mat& dst, int d,
5792                      double sigma_color, double sigma_space,
5793                      int borderType )
5794 {
5795     int cn = src.channels();
5796     int i, j, maxk, radius;
5797     double minValSrc=-1, maxValSrc=1;
5798     const int kExpNumBinsPerChannel = 1 << 12;
5799     int kExpNumBins = 0;
5800     float lastExpVal = 1.f;
5801     float len, scale_index;
5802     Size size = src.size();
5803
5804     CV_Assert( (src.type() == CV_32FC1 || src.type() == CV_32FC3) && src.data != dst.data );
5805
5806     if( sigma_color <= 0 )
5807         sigma_color = 1;
5808     if( sigma_space <= 0 )
5809         sigma_space = 1;
5810
5811     double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5812     double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5813
5814     if( d <= 0 )
5815         radius = cvRound(sigma_space*1.5);
5816     else
5817         radius = d/2;
5818     radius = MAX(radius, 1);
5819     d = radius*2 + 1;
5820     // compute the min/max range for the input image (even if multichannel)
5821
5822     minMaxLoc( src.reshape(1), &minValSrc, &maxValSrc );
5823     if(std::abs(minValSrc - maxValSrc) < FLT_EPSILON)
5824     {
5825         src.copyTo(dst);
5826         return;
5827     }
5828
5829     // temporary copy of the image with borders for easy processing
5830     Mat temp;
5831     copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5832     const double insteadNaNValue = -5. * sigma_color;
5833     patchNaNs( temp, insteadNaNValue ); // this replacement of NaNs makes the assumption that depth values are nonnegative
5834                                         // TODO: make insteadNaNValue avalible in the outside function interface to control the cases breaking the assumption
5835     // allocate lookup tables
5836     std::vector<float> _space_weight(d*d);
5837     std::vector<int> _space_ofs(d*d);
5838     float* space_weight = &_space_weight[0];
5839     int* space_ofs = &_space_ofs[0];
5840
5841     // assign a length which is slightly more than needed
5842     len = (float)(maxValSrc - minValSrc) * cn;
5843     kExpNumBins = kExpNumBinsPerChannel * cn;
5844     std::vector<float> _expLUT(kExpNumBins+2);
5845     float* expLUT = &_expLUT[0];
5846
5847     scale_index = kExpNumBins/len;
5848
5849     // initialize the exp LUT
5850     for( i = 0; i < kExpNumBins+2; i++ )
5851     {
5852         if( lastExpVal > 0.f )
5853         {
5854             double val =  i / scale_index;
5855             expLUT[i] = (float)std::exp(val * val * gauss_color_coeff);
5856             lastExpVal = expLUT[i];
5857         }
5858         else
5859             expLUT[i] = 0.f;
5860     }
5861
5862     // initialize space-related bilateral filter coefficients
5863     for( i = -radius, maxk = 0; i <= radius; i++ )
5864         for( j = -radius; j <= radius; j++ )
5865         {
5866             double r = std::sqrt((double)i*i + (double)j*j);
5867             if( r > radius )
5868                 continue;
5869             space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5870             space_ofs[maxk++] = (int)(i*(temp.step/sizeof(float)) + j*cn);
5871         }
5872
5873     // parallel_for usage
5874
5875     BilateralFilter_32f_Invoker body(cn, radius, maxk, space_ofs, temp, dst, scale_index, space_weight, expLUT);
5876     parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5877 }
5878
5879 #ifdef HAVE_IPP
5880 #define IPP_BILATERAL_PARALLEL 1
5881
5882 #ifdef HAVE_IPP_IW
5883 class ipp_bilateralFilterParallel: public ParallelLoopBody
5884 {
5885 public:
5886     ipp_bilateralFilterParallel(::ipp::IwiImage &_src, ::ipp::IwiImage &_dst, int _radius, Ipp32f _valSquareSigma, Ipp32f _posSquareSigma, ::ipp::IwiBorderType _borderType, bool *_ok):
5887         src(_src), dst(_dst)
5888     {
5889         pOk = _ok;
5890
5891         radius          = _radius;
5892         valSquareSigma  = _valSquareSigma;
5893         posSquareSigma  = _posSquareSigma;
5894         borderType      = _borderType;
5895
5896         *pOk = true;
5897     }
5898     ~ipp_bilateralFilterParallel() {}
5899
5900     virtual void operator() (const Range& range) const CV_OVERRIDE
5901     {
5902         if(*pOk == false)
5903             return;
5904
5905         try
5906         {
5907             ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, dst.m_size.width, range.end - range.start);
5908             CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, src, dst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), borderType, tile);
5909         }
5910         catch(::ipp::IwException)
5911         {
5912             *pOk = false;
5913             return;
5914         }
5915     }
5916 private:
5917     ::ipp::IwiImage &src;
5918     ::ipp::IwiImage &dst;
5919
5920     int                  radius;
5921     Ipp32f               valSquareSigma;
5922     Ipp32f               posSquareSigma;
5923     ::ipp::IwiBorderType borderType;
5924
5925     bool  *pOk;
5926     const ipp_bilateralFilterParallel& operator= (const ipp_bilateralFilterParallel&);
5927 };
5928 #endif
5929
5930 static bool ipp_bilateralFilter(Mat &src, Mat &dst, int d, double sigmaColor, double sigmaSpace, int borderType)
5931 {
5932 #ifdef HAVE_IPP_IW
5933     CV_INSTRUMENT_REGION_IPP()
5934
5935     int         radius         = IPP_MAX(((d <= 0)?cvRound(sigmaSpace*1.5):d/2), 1);
5936     Ipp32f      valSquareSigma = (Ipp32f)((sigmaColor <= 0)?1:sigmaColor*sigmaColor);
5937     Ipp32f      posSquareSigma = (Ipp32f)((sigmaSpace <= 0)?1:sigmaSpace*sigmaSpace);
5938
5939     // Acquire data and begin processing
5940     try
5941     {
5942         ::ipp::IwiImage      iwSrc = ippiGetImage(src);
5943         ::ipp::IwiImage      iwDst = ippiGetImage(dst);
5944         ::ipp::IwiBorderSize borderSize(radius);
5945         ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
5946         if(!ippBorder)
5947             return false;
5948
5949         const int threads = ippiSuggestThreadsNum(iwDst, 2);
5950         if(IPP_BILATERAL_PARALLEL && threads > 1) {
5951             bool  ok      = true;
5952             Range range(0, (int)iwDst.m_size.height);
5953             ipp_bilateralFilterParallel invoker(iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ippBorder, &ok);
5954             if(!ok)
5955                 return false;
5956
5957             parallel_for_(range, invoker, threads*4);
5958
5959             if(!ok)
5960                 return false;
5961         } else {
5962             CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), ippBorder);
5963         }
5964     }
5965     catch (::ipp::IwException)
5966     {
5967         return false;
5968     }
5969     return true;
5970 #else
5971     CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(d); CV_UNUSED(sigmaColor); CV_UNUSED(sigmaSpace); CV_UNUSED(borderType);
5972     return false;
5973 #endif
5974 }
5975 #endif
5976
5977 }
5978
5979 void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
5980                       double sigmaColor, double sigmaSpace,
5981                       int borderType )
5982 {
5983     CV_INSTRUMENT_REGION()
5984
5985     _dst.create( _src.size(), _src.type() );
5986
5987     CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
5988                ocl_bilateralFilter_8u(_src, _dst, d, sigmaColor, sigmaSpace, borderType))
5989
5990     Mat src = _src.getMat(), dst = _dst.getMat();
5991
5992     CV_IPP_RUN_FAST(ipp_bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType));
5993
5994     if( src.depth() == CV_8U )
5995         bilateralFilter_8u( src, dst, d, sigmaColor, sigmaSpace, borderType );
5996     else if( src.depth() == CV_32F )
5997         bilateralFilter_32f( src, dst, d, sigmaColor, sigmaSpace, borderType );
5998     else
5999         CV_Error( CV_StsUnsupportedFormat,
6000         "Bilateral filtering is only implemented for 8u and 32f images" );
6001 }
6002
6003 //////////////////////////////////////////////////////////////////////////////////////////
6004
6005 CV_IMPL void
6006 cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
6007           int param1, int param2, double param3, double param4 )
6008 {
6009     cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
6010
6011     CV_Assert( dst.size() == src.size() &&
6012         (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
6013
6014     if( param2 <= 0 )
6015         param2 = param1;
6016
6017     if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE )
6018         cv::boxFilter( src, dst, dst.depth(), cv::Size(param1, param2), cv::Point(-1,-1),
6019             smooth_type == CV_BLUR, cv::BORDER_REPLICATE );
6020     else if( smooth_type == CV_GAUSSIAN )
6021         cv::GaussianBlur( src, dst, cv::Size(param1, param2), param3, param4, cv::BORDER_REPLICATE );
6022     else if( smooth_type == CV_MEDIAN )
6023         cv::medianBlur( src, dst, param1 );
6024     else
6025         cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
6026
6027     if( dst.data != dst0.data )
6028         CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );
6029 }
6030
6031 /* End of file. */