1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #include "precomp.hpp"
48 #include "opencv2/core/hal/intrin.hpp"
49 #include "opencl_kernels_imgproc.hpp"
51 #include "opencv2/core/openvx/ovx_defs.hpp"
55 #include "fixedpoint.inl.hpp"
57 * This file includes the code, contributed by Simon Perreault
58 * (the function icvMedianBlur_8u_O1)
60 * Constant-time median filtering -- http://nomis80.org/ctmf.html
61 * Copyright (C) 2006 Simon Perreault
64 * Laboratoire de vision et systemes numeriques
65 * Pavillon Adrien-Pouliot
67 * Sainte-Foy, Quebec, Canada
70 * perreaul@gel.ulaval.ca
76 /****************************************************************************************\
78 \****************************************************************************************/
80 template<typename T, typename ST>
84 RowSum( int _ksize, int _anchor ) :
91 virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
93 const T* S = (const T*)src;
95 int i = 0, k, ksz_cn = ksize*cn;
97 width = (width - 1)*cn;
100 for( i = 0; i < width + cn; i++ )
102 D[i] = (ST)S[i] + (ST)S[i+cn] + (ST)S[i+cn*2];
105 else if( ksize == 5 )
107 for( i = 0; i < width + cn; i++ )
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];
115 for( i = 0; i < ksz_cn; i++ )
118 for( i = 0; i < width; i++ )
120 s += (ST)S[i + ksz_cn] - (ST)S[i];
126 ST s0 = 0, s1 = 0, s2 = 0;
127 for( i = 0; i < ksz_cn; i += 3 )
136 for( i = 0; i < width; i += 3 )
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];
148 ST s0 = 0, s1 = 0, s2 = 0, s3 = 0;
149 for( i = 0; i < ksz_cn; i += 4 )
160 for( i = 0; i < width; i += 4 )
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];
173 for( k = 0; k < cn; k++, S++, D++ )
176 for( i = 0; i < ksz_cn; i += cn )
179 for( i = 0; i < width; i += cn )
181 s += (ST)S[i + ksz_cn] - (ST)S[i];
189 template<typename ST, typename T>
191 public BaseColumnFilter
193 ColumnSum( int _ksize, int _anchor, double _scale ) :
202 virtual void reset() CV_OVERRIDE { sumCount = 0; }
204 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
208 bool haveScale = scale != 1;
209 double _scale = scale;
211 if( width != (int)sum.size() )
220 memset((void*)SUM, 0, width*sizeof(ST));
222 for( ; sumCount < ksize - 1; sumCount++, src++ )
224 const ST* Sp = (const ST*)src[0];
226 for( i = 0; i < width; i++ )
232 CV_Assert( sumCount == ksize-1 );
236 for( ; count--; src++ )
238 const ST* Sp = (const ST*)src[0];
239 const ST* Sm = (const ST*)src[1-ksize];
243 for( i = 0; i <= width - 2; i += 2 )
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;
252 for( ; i < width; i++ )
254 ST s0 = SUM[i] + Sp[i];
255 D[i] = saturate_cast<T>(s0*_scale);
261 for( i = 0; i <= width - 2; i += 2 )
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;
270 for( ; i < width; i++ )
272 ST s0 = SUM[i] + Sp[i];
273 D[i] = saturate_cast<T>(s0);
288 struct ColumnSum<int, uchar> :
289 public BaseColumnFilter
291 ColumnSum( int _ksize, int _anchor, double _scale ) :
300 virtual void reset() CV_OVERRIDE { sumCount = 0; }
302 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
305 bool haveScale = scale != 1;
306 double _scale = scale;
309 bool haveSIMD128 = hasSIMD128();
312 if( width != (int)sum.size() )
321 memset((void*)SUM, 0, width*sizeof(int));
322 for( ; sumCount < ksize - 1; sumCount++, src++ )
324 const int* Sp = (const int*)src[0];
329 for (; i <= width - 4; i += 4)
331 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
335 for( ; i < width; i++ )
341 CV_Assert( sumCount == ksize-1 );
345 for( ; count--; src++ )
347 const int* Sp = (const int*)src[0];
348 const int* Sm = (const int*)src[1-ksize];
349 uchar* D = (uchar*)dst;
357 v_float32x4 v_scale = v_setall_f32((float)_scale);
358 for( ; i <= width-8; i+=8 )
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);
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));
366 v_uint16x8 v_dst = v_pack(v_s0d, v_s01d);
367 v_pack_store(D + i, v_dst);
369 v_store(SUM + i, v_s0 - v_load(Sm + i));
370 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
374 for( ; i < width; i++ )
376 int s0 = SUM[i] + Sp[i];
377 D[i] = saturate_cast<uchar>(s0*_scale);
387 for( ; i <= width-8; i+=8 )
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);
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);
395 v_store(SUM + i, v_s0 - v_load(Sm + i));
396 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
401 for( ; i < width; i++ )
403 int s0 = SUM[i] + Sp[i];
404 D[i] = saturate_cast<uchar>(s0);
414 std::vector<int> sum;
419 struct ColumnSum<ushort, uchar> :
420 public BaseColumnFilter
424 ColumnSum( int _ksize, int _anchor, double _scale ) :
435 int d = cvRound(1./scale);
436 double scalef = ((double)(1 << SHIFT))/d;
437 divScale = cvFloor(scalef);
447 virtual void reset() CV_OVERRIDE { sumCount = 0; }
449 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
451 const int ds = divScale;
452 const int dd = divDelta;
454 const bool haveScale = scale != 1;
457 bool haveSIMD128 = hasSIMD128();
460 if( width != (int)sum.size() )
469 memset((void*)SUM, 0, width*sizeof(SUM[0]));
470 for( ; sumCount < ksize - 1; sumCount++, src++ )
472 const ushort* Sp = (const ushort*)src[0];
477 for( ; i <= width - 8; i += 8 )
479 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
483 for( ; i < width; i++ )
489 CV_Assert( sumCount == ksize-1 );
493 for( ; count--; src++ )
495 const ushort* Sp = (const ushort*)src[0];
496 const ushort* Sm = (const ushort*)src[1-ksize];
497 uchar* D = (uchar*)dst;
502 v_uint32x4 ds4 = v_setall_u32((unsigned)ds);
503 v_uint16x8 dd8 = v_setall_u16((ushort)dd);
505 for( ; i <= width-16; i+=16 )
507 v_uint16x8 _sm0 = v_load(Sm + i);
508 v_uint16x8 _sm1 = v_load(Sm + i + 8);
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));
513 v_uint32x4 _s00, _s01, _s10, _s11;
515 v_expand(_s0 + dd8, _s00, _s01);
516 v_expand(_s1 + dd8, _s10, _s11);
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);
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));
526 _s0 = v_sub_wrap(_s0, _sm0);
527 _s1 = v_sub_wrap(_s1, _sm1);
529 v_store(D + i, v_pack_u(r0, r1));
530 v_store(SUM + i, _s0);
531 v_store(SUM + i + 8, _s1);
534 for( ; i < width; i++ )
536 int s0 = SUM[i] + Sp[i];
537 D[i] = (uchar)((s0 + dd)*ds >> SHIFT);
538 SUM[i] = (ushort)(s0 - Sm[i]);
544 for( ; i < width; i++ )
546 int s0 = SUM[i] + Sp[i];
547 D[i] = saturate_cast<uchar>(s0);
548 SUM[i] = (ushort)(s0 - Sm[i]);
559 std::vector<ushort> sum;
564 struct ColumnSum<int, short> :
565 public BaseColumnFilter
567 ColumnSum( int _ksize, int _anchor, double _scale ) :
576 virtual void reset() CV_OVERRIDE { sumCount = 0; }
578 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
582 bool haveScale = scale != 1;
583 double _scale = scale;
586 bool haveSIMD128 = hasSIMD128();
589 if( width != (int)sum.size() )
598 memset((void*)SUM, 0, width*sizeof(int));
599 for( ; sumCount < ksize - 1; sumCount++, src++ )
601 const int* Sp = (const int*)src[0];
606 for( ; i <= width - 4; i+=4 )
608 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
612 for( ; i < width; i++ )
618 CV_Assert( sumCount == ksize-1 );
622 for( ; count--; src++ )
624 const int* Sp = (const int*)src[0];
625 const int* Sm = (const int*)src[1-ksize];
626 short* D = (short*)dst;
633 v_float32x4 v_scale = v_setall_f32((float)_scale);
634 for( ; i <= width-8; i+=8 )
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);
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));
643 v_store(SUM + i, v_s0 - v_load(Sm + i));
644 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
648 for( ; i < width; i++ )
650 int s0 = SUM[i] + Sp[i];
651 D[i] = saturate_cast<short>(s0*_scale);
661 for( ; i <= width-8; i+=8 )
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);
666 v_store(D + i, v_pack(v_s0, v_s01));
668 v_store(SUM + i, v_s0 - v_load(Sm + i));
669 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
674 for( ; i < width; i++ )
676 int s0 = SUM[i] + Sp[i];
677 D[i] = saturate_cast<short>(s0);
687 std::vector<int> sum;
692 struct ColumnSum<int, ushort> :
693 public BaseColumnFilter
695 ColumnSum( int _ksize, int _anchor, double _scale ) :
704 virtual void reset() CV_OVERRIDE { sumCount = 0; }
706 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
709 bool haveScale = scale != 1;
710 double _scale = scale;
713 bool haveSIMD128 = hasSIMD128();
716 if( width != (int)sum.size() )
725 memset((void*)SUM, 0, width*sizeof(int));
726 for( ; sumCount < ksize - 1; sumCount++, src++ )
728 const int* Sp = (const int*)src[0];
733 for (; i <= width - 4; i += 4)
735 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
739 for( ; i < width; i++ )
745 CV_Assert( sumCount == ksize-1 );
749 for( ; count--; src++ )
751 const int* Sp = (const int*)src[0];
752 const int* Sm = (const int*)src[1-ksize];
753 ushort* D = (ushort*)dst;
760 v_float32x4 v_scale = v_setall_f32((float)_scale);
761 for( ; i <= width-8; i+=8 )
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);
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));
770 v_store(SUM + i, v_s0 - v_load(Sm + i));
771 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
775 for( ; i < width; i++ )
777 int s0 = SUM[i] + Sp[i];
778 D[i] = saturate_cast<ushort>(s0*_scale);
788 for( ; i <= width-8; i+=8 )
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);
793 v_store(D + i, v_pack(v_reinterpret_as_u32(v_s0), v_reinterpret_as_u32(v_s01)));
795 v_store(SUM + i, v_s0 - v_load(Sm + i));
796 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
800 for( ; i < width; i++ )
802 int s0 = SUM[i] + Sp[i];
803 D[i] = saturate_cast<ushort>(s0);
813 std::vector<int> sum;
817 struct ColumnSum<int, int> :
818 public BaseColumnFilter
820 ColumnSum( int _ksize, int _anchor, double _scale ) :
829 virtual void reset() CV_OVERRIDE { sumCount = 0; }
831 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
834 bool haveScale = scale != 1;
835 double _scale = scale;
838 bool haveSIMD128 = hasSIMD128();
841 if( width != (int)sum.size() )
850 memset((void*)SUM, 0, width*sizeof(int));
851 for( ; sumCount < ksize - 1; sumCount++, src++ )
853 const int* Sp = (const int*)src[0];
858 for( ; i <= width - 4; i+=4 )
860 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
864 for( ; i < width; i++ )
870 CV_Assert( sumCount == ksize-1 );
874 for( ; count--; src++ )
876 const int* Sp = (const int*)src[0];
877 const int* Sm = (const int*)src[1-ksize];
885 v_float32x4 v_scale = v_setall_f32((float)_scale);
886 for( ; i <= width-4; i+=4 )
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);
891 v_store(D + i, v_s0d);
892 v_store(SUM + i, v_s0 - v_load(Sm + i));
896 for( ; i < width; i++ )
898 int s0 = SUM[i] + Sp[i];
899 D[i] = saturate_cast<int>(s0*_scale);
909 for( ; i <= width-4; i+=4 )
911 v_int32x4 v_s0 = v_load(SUM + i) + v_load(Sp + i);
913 v_store(D + i, v_s0);
914 v_store(SUM + i, v_s0 - v_load(Sm + i));
918 for( ; i < width; i++ )
920 int s0 = SUM[i] + Sp[i];
931 std::vector<int> sum;
936 struct ColumnSum<int, float> :
937 public BaseColumnFilter
939 ColumnSum( int _ksize, int _anchor, double _scale ) :
948 virtual void reset() CV_OVERRIDE { sumCount = 0; }
950 virtual void operator()(const uchar** src, uchar* dst, int dststep, int count, int width) CV_OVERRIDE
953 bool haveScale = scale != 1;
954 double _scale = scale;
957 bool haveSIMD128 = hasSIMD128();
960 if( width != (int)sum.size() )
969 memset((void*)SUM, 0, width*sizeof(int));
970 for( ; sumCount < ksize - 1; sumCount++, src++ )
972 const int* Sp = (const int*)src[0];
977 for( ; i <= width - 4; i+=4 )
979 v_store(SUM + i, v_load(SUM + i) + v_load(Sp + i));
984 for( ; i < width; i++ )
990 CV_Assert( sumCount == ksize-1 );
994 for( ; count--; src++ )
996 const int * Sp = (const int*)src[0];
997 const int * Sm = (const int*)src[1-ksize];
998 float* D = (float*)dst;
1006 v_float32x4 v_scale = v_setall_f32((float)_scale);
1007 for (; i <= width - 8; i += 8)
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);
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);
1015 v_store(SUM + i, v_s0 - v_load(Sm + i));
1016 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
1020 for( ; i < width; i++ )
1022 int s0 = SUM[i] + Sp[i];
1023 D[i] = (float)(s0*_scale);
1024 SUM[i] = s0 - Sm[i];
1034 for( ; i <= width-8; i+=8 )
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);
1039 v_store(D + i, v_cvt_f32(v_s0));
1040 v_store(D + i + 4, v_cvt_f32(v_s01));
1042 v_store(SUM + i, v_s0 - v_load(Sm + i));
1043 v_store(SUM + i + 4, v_s01 - v_load(Sm + i + 4));
1047 for( ; i < width; i++ )
1049 int s0 = SUM[i] + Sp[i];
1051 SUM[i] = s0 - Sm[i];
1060 std::vector<int> sum;
1065 static bool ocl_boxFilter3x3_8UC1( InputArray _src, OutputArray _dst, int ddepth,
1066 Size ksize, Point anchor, int borderType, bool normalize )
1068 const ocl::Device & dev = ocl::Device::getDefault();
1069 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
1075 anchor.x = ksize.width / 2;
1077 anchor.y = ksize.height / 2;
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)) )
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" };
1092 globalsize[0] = size.width / 16;
1093 globalsize[1] = size.height / 2;
1095 char build_opts[1024];
1096 sprintf(build_opts, "-D %s %s", borderMap[borderType], normalize ? "-D NORMALIZE" : "");
1098 ocl::Kernel kernel("boxFilter3x3_8UC1_cols16_rows2", cv::ocl::imgproc::boxFilter3x3_oclsrc, build_opts);
1102 UMat src = _src.getUMat();
1103 _dst.create(size, CV_MAKETYPE(ddepth, cn));
1104 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
1106 UMat dst = _dst.getUMat();
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);
1115 idxArg = kernel.set(idxArg, (float)alpha);
1117 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
1120 #define DIVUP(total, grain) ((total + grain - 1) / (grain))
1121 #define ROUNDUP(sz, n) ((sz) + (n) - 1 - (((sz) + (n) - 1) % (n)))
1123 static bool ocl_boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1124 Size ksize, Point anchor, int borderType, bool normalize, bool sqr = false )
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;
1133 if (cn > 4 || (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F)) ||
1134 _src.offset() % esz != 0 || _src.step() % esz != 0)
1138 anchor.x = ksize.width / 2;
1140 anchor.y = ksize.height / 2;
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);
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;
1154 UMat src = _src.getUMat();
1158 src.locateROI(wholeSize, ofs);
1161 int h = isolated ? size.height : wholeSize.height;
1162 int w = isolated ? size.width : wholeSize.width;
1164 size_t maxWorkItemSizes[32];
1165 ocl::Device::getDefault().maxWorkItemSizes(maxWorkItemSizes);
1166 int tryWorkItems = (int)maxWorkItemSizes[0];
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)))
1174 if (w < ksize.width || h < ksize.height)
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;
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)
1186 pxPerWorkItemX = size.width % 8 ? size.width % 4 ? size.width % 2 ? 1 : 2 : 4 : 8;
1187 pxPerWorkItemY = size.height % 2 ? 1 : 2;
1189 else if (cn < 4 || (ksize.width <= 4 && ksize.height <= 4))
1191 pxPerWorkItemX = size.width % 2 ? 1 : 2;
1192 pxPerWorkItemY = size.height % 2 ? 1 : 2;
1194 globalsize[0] = size.width / pxPerWorkItemX;
1195 globalsize[1] = size.height / pxPerWorkItemY;
1197 // Need some padding in the private array for pixels
1198 int privDataWidth = ROUNDUP(pxPerWorkItemX + ksize.width - 1, pxLoadNumPixels);
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);
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
1227 if (!kernel.create("filterSmall", cv::ocl::imgproc::filterSmall_oclsrc, build_options))
1232 localsize = localsize_general;
1235 int BLOCK_SIZE_X = tryWorkItems, BLOCK_SIZE_Y = std::min(ksize.height * 10, size.height);
1237 while (BLOCK_SIZE_X > 32 && BLOCK_SIZE_X >= ksize.width * 2 && BLOCK_SIZE_X > size.width * 2)
1239 while (BLOCK_SIZE_Y < BLOCK_SIZE_X / 8 && BLOCK_SIZE_Y * computeUnits * 32 < size.height)
1242 if (ksize.width > BLOCK_SIZE_X || w < ksize.width || h < ksize.height)
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);
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);
1262 kernel.create("boxFilter", cv::ocl::imgproc::boxFilter_oclsrc, opts);
1266 size_t kernelWorkGroupSize = kernel.workGroupSize();
1267 if (localsize[0] <= kernelWorkGroupSize)
1269 if (BLOCK_SIZE_X < (int)kernelWorkGroupSize)
1272 tryWorkItems = (int)kernelWorkGroupSize;
1276 _dst.create(size, CV_MAKETYPE(ddepth, cn));
1277 UMat dst = _dst.getUMat();
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));
1291 idxArg = kernel.set(idxArg, (float)alpha);
1293 return kernel.run(2, globalsize, localsize, false);
1303 cv::Ptr<cv::BaseRowFilter> cv::getRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1305 int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1306 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
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);
1332 CV_Error_( CV_StsNotImplemented,
1333 ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1336 return Ptr<BaseRowFilter>();
1340 cv::Ptr<cv::BaseColumnFilter> cv::getColumnSumFilter(int sumType, int dstType, int ksize,
1341 int anchor, double scale)
1343 int sdepth = CV_MAT_DEPTH(sumType), ddepth = CV_MAT_DEPTH(dstType);
1344 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(dstType) );
1349 if( ddepth == CV_8U && sdepth == CV_32S )
1350 return makePtr<ColumnSum<int, uchar> >(ksize, anchor, scale);
1351 if( ddepth == CV_8U && sdepth == CV_16U )
1352 return makePtr<ColumnSum<ushort, uchar> >(ksize, anchor, scale);
1353 if( ddepth == CV_8U && sdepth == CV_64F )
1354 return makePtr<ColumnSum<double, uchar> >(ksize, anchor, scale);
1355 if( ddepth == CV_16U && sdepth == CV_32S )
1356 return makePtr<ColumnSum<int, ushort> >(ksize, anchor, scale);
1357 if( ddepth == CV_16U && sdepth == CV_64F )
1358 return makePtr<ColumnSum<double, ushort> >(ksize, anchor, scale);
1359 if( ddepth == CV_16S && sdepth == CV_32S )
1360 return makePtr<ColumnSum<int, short> >(ksize, anchor, scale);
1361 if( ddepth == CV_16S && sdepth == CV_64F )
1362 return makePtr<ColumnSum<double, short> >(ksize, anchor, scale);
1363 if( ddepth == CV_32S && sdepth == CV_32S )
1364 return makePtr<ColumnSum<int, int> >(ksize, anchor, scale);
1365 if( ddepth == CV_32F && sdepth == CV_32S )
1366 return makePtr<ColumnSum<int, float> >(ksize, anchor, scale);
1367 if( ddepth == CV_32F && sdepth == CV_64F )
1368 return makePtr<ColumnSum<double, float> >(ksize, anchor, scale);
1369 if( ddepth == CV_64F && sdepth == CV_32S )
1370 return makePtr<ColumnSum<int, double> >(ksize, anchor, scale);
1371 if( ddepth == CV_64F && sdepth == CV_64F )
1372 return makePtr<ColumnSum<double, double> >(ksize, anchor, scale);
1374 CV_Error_( CV_StsNotImplemented,
1375 ("Unsupported combination of sum format (=%d), and destination format (=%d)",
1378 return Ptr<BaseColumnFilter>();
1382 cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize,
1383 Point anchor, bool normalize, int borderType )
1385 int sdepth = CV_MAT_DEPTH(srcType);
1386 int cn = CV_MAT_CN(srcType), sumType = CV_64F;
1387 if( sdepth == CV_8U && CV_MAT_DEPTH(dstType) == CV_8U &&
1388 ksize.width*ksize.height <= 256 )
1390 else if( sdepth <= CV_32S && (!normalize ||
1391 ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) :
1392 sdepth == CV_16U ? (1 << 15) : (1 << 16))) )
1394 sumType = CV_MAKETYPE( sumType, cn );
1396 Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1397 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1398 dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1);
1400 return makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1401 srcType, dstType, sumType, borderType );
1408 template <> inline bool skipSmallImages<VX_KERNEL_BOX_3x3>(int w, int h) { return w*h < 640 * 480; }
1410 static bool openvx_boxfilter(InputArray _src, OutputArray _dst, int ddepth,
1411 Size ksize, Point anchor,
1412 bool normalize, int borderType)
1416 if (_src.type() != CV_8UC1 || ddepth != CV_8U || !normalize ||
1417 _src.cols() < 3 || _src.rows() < 3 ||
1418 ksize.width != 3 || ksize.height != 3 ||
1419 (anchor.x >= 0 && anchor.x != 1) ||
1420 (anchor.y >= 0 && anchor.y != 1) ||
1421 ovx::skipSmallImages<VX_KERNEL_BOX_3x3>(_src.cols(), _src.rows()))
1424 Mat src = _src.getMat();
1426 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
1427 return false; //Process isolated borders only
1429 switch (borderType & ~BORDER_ISOLATED)
1431 case BORDER_CONSTANT:
1432 border = VX_BORDER_CONSTANT;
1434 case BORDER_REPLICATE:
1435 border = VX_BORDER_REPLICATE;
1441 _dst.create(src.size(), CV_8UC1);
1442 Mat dst = _dst.getMat();
1446 ivx::Context ctx = ovx::getOpenVXContext();
1449 if (dst.data != src.data)
1455 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1456 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
1457 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1458 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
1460 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
1461 //since OpenVX standard says nothing about thread-safety for now
1462 ivx::border_t prevBorder = ctx.immediateBorder();
1463 ctx.setImmediateBorder(border, (vx_uint8)(0));
1464 ivx::IVX_CHECK_STATUS(vxuBox3x3(ctx, ia, ib));
1465 ctx.setImmediateBorder(prevBorder);
1467 catch (ivx::RuntimeError & e)
1469 VX_DbgThrow(e.what());
1471 catch (ivx::WrapperError & e)
1473 VX_DbgThrow(e.what());
1481 #if defined(HAVE_IPP)
1484 static bool ipp_boxfilter(Mat &src, Mat &dst, Size ksize, Point anchor, bool normalize, int borderType)
1487 CV_INSTRUMENT_REGION_IPP()
1489 #if IPP_VERSION_X100 < 201801
1490 // Problem with SSE42 optimization for 16s and some 8u modes
1491 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))))
1494 // Other optimizations has some degradations too
1495 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))))
1502 if(!ippiCheckAnchor(anchor, ksize))
1507 ::ipp::IwiImage iwSrc = ippiGetImage(src);
1508 ::ipp::IwiImage iwDst = ippiGetImage(dst);
1509 ::ipp::IwiSize iwKSize = ippiGetSize(ksize);
1510 ::ipp::IwiBorderSize borderSize(iwKSize);
1511 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
1515 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBox, iwSrc, iwDst, iwKSize, ::ipp::IwDefault(), ippBorder);
1517 catch (::ipp::IwException)
1524 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(ksize); CV_UNUSED(anchor); CV_UNUSED(normalize); CV_UNUSED(borderType);
1532 void cv::boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1533 Size ksize, Point anchor,
1534 bool normalize, int borderType )
1536 CV_INSTRUMENT_REGION()
1538 CV_OCL_RUN(_dst.isUMat() &&
1539 (borderType == BORDER_REPLICATE || borderType == BORDER_CONSTANT ||
1540 borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101),
1541 ocl_boxFilter3x3_8UC1(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1543 CV_OCL_RUN(_dst.isUMat(), ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1545 Mat src = _src.getMat();
1546 int stype = src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
1549 _dst.create( src.size(), CV_MAKETYPE(ddepth, cn) );
1550 Mat dst = _dst.getMat();
1551 if( borderType != BORDER_CONSTANT && normalize && (borderType & BORDER_ISOLATED) != 0 )
1560 Size wsz(src.cols, src.rows);
1561 if(!(borderType&BORDER_ISOLATED))
1562 src.locateROI( wsz, ofs );
1564 CALL_HAL(boxFilter, cv_hal_boxFilter, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
1565 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
1566 anchor.x, anchor.y, normalize, borderType&~BORDER_ISOLATED);
1569 openvx_boxfilter(src, dst, ddepth, ksize, anchor, normalize, borderType))
1571 CV_IPP_RUN_FAST(ipp_boxfilter(src, dst, ksize, anchor, normalize, borderType));
1573 borderType = (borderType&~BORDER_ISOLATED);
1575 Ptr<FilterEngine> f = createBoxFilter( src.type(), dst.type(),
1576 ksize, anchor, normalize, borderType );
1578 f->apply( src, dst, wsz, ofs );
1582 void cv::blur( InputArray src, OutputArray dst,
1583 Size ksize, Point anchor, int borderType )
1585 CV_INSTRUMENT_REGION()
1587 boxFilter( src, dst, -1, ksize, anchor, true, borderType );
1591 /****************************************************************************************\
1593 \****************************************************************************************/
1598 template<typename T, typename ST>
1600 public BaseRowFilter
1602 SqrRowSum( int _ksize, int _anchor ) :
1609 virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
1611 const T* S = (const T*)src;
1613 int i = 0, k, ksz_cn = ksize*cn;
1615 width = (width - 1)*cn;
1616 for( k = 0; k < cn; k++, S++, D++ )
1619 for( i = 0; i < ksz_cn; i += cn )
1625 for( i = 0; i < width; i += cn )
1627 ST val0 = (ST)S[i], val1 = (ST)S[i + ksz_cn];
1628 s += val1*val1 - val0*val0;
1635 static Ptr<BaseRowFilter> getSqrRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1637 int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1638 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
1643 if( sdepth == CV_8U && ddepth == CV_32S )
1644 return makePtr<SqrRowSum<uchar, int> >(ksize, anchor);
1645 if( sdepth == CV_8U && ddepth == CV_64F )
1646 return makePtr<SqrRowSum<uchar, double> >(ksize, anchor);
1647 if( sdepth == CV_16U && ddepth == CV_64F )
1648 return makePtr<SqrRowSum<ushort, double> >(ksize, anchor);
1649 if( sdepth == CV_16S && ddepth == CV_64F )
1650 return makePtr<SqrRowSum<short, double> >(ksize, anchor);
1651 if( sdepth == CV_32F && ddepth == CV_64F )
1652 return makePtr<SqrRowSum<float, double> >(ksize, anchor);
1653 if( sdepth == CV_64F && ddepth == CV_64F )
1654 return makePtr<SqrRowSum<double, double> >(ksize, anchor);
1656 CV_Error_( CV_StsNotImplemented,
1657 ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1660 return Ptr<BaseRowFilter>();
1665 void cv::sqrBoxFilter( InputArray _src, OutputArray _dst, int ddepth,
1666 Size ksize, Point anchor,
1667 bool normalize, int borderType )
1669 CV_INSTRUMENT_REGION()
1671 int srcType = _src.type(), sdepth = CV_MAT_DEPTH(srcType), cn = CV_MAT_CN(srcType);
1672 Size size = _src.size();
1675 ddepth = sdepth < CV_32F ? CV_32F : CV_64F;
1677 if( borderType != BORDER_CONSTANT && normalize )
1679 if( size.height == 1 )
1681 if( size.width == 1 )
1685 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
1686 ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize, true))
1688 int sumDepth = CV_64F;
1689 if( sdepth == CV_8U )
1691 int sumType = CV_MAKETYPE( sumDepth, cn ), dstType = CV_MAKETYPE(ddepth, cn);
1693 Mat src = _src.getMat();
1694 _dst.create( size, dstType );
1695 Mat dst = _dst.getMat();
1697 Ptr<BaseRowFilter> rowFilter = getSqrRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1698 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1699 dstType, ksize.height, anchor.y,
1700 normalize ? 1./(ksize.width*ksize.height) : 1);
1702 Ptr<FilterEngine> f = makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1703 srcType, dstType, sumType, borderType );
1705 Size wsz(src.cols, src.rows);
1706 src.locateROI( wsz, ofs );
1708 f->apply( src, dst, wsz, ofs );
1712 /****************************************************************************************\
1714 \****************************************************************************************/
1716 cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
1718 const int SMALL_GAUSSIAN_SIZE = 7;
1719 static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
1722 {0.25f, 0.5f, 0.25f},
1723 {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
1724 {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
1727 const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
1728 small_gaussian_tab[n>>1] : 0;
1730 CV_Assert( ktype == CV_32F || ktype == CV_64F );
1731 Mat kernel(n, 1, ktype);
1732 float* cf = kernel.ptr<float>();
1733 double* cd = kernel.ptr<double>();
1735 double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
1736 double scale2X = -0.5/(sigmaX*sigmaX);
1740 for( i = 0; i < n; i++ )
1742 double x = i - (n-1)*0.5;
1743 double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
1744 if( ktype == CV_32F )
1757 for( i = 0; i < n; i++ )
1759 if( ktype == CV_32F )
1760 cf[i] = (float)(cf[i]*sum);
1770 template <typename T>
1771 static std::vector<T> getFixedpointGaussianKernel( int n, double sigma )
1776 return std::vector<T>(1, softdouble(1.0));
1779 T v3[] = { softdouble(0.25), softdouble(0.5), softdouble(0.25) };
1780 return std::vector<T>(v3, v3 + 3);
1784 T v5[] = { softdouble(0.0625), softdouble(0.25), softdouble(0.375), softdouble(0.25), softdouble(0.0625) };
1785 return std::vector<T>(v5, v5 + 5);
1789 T v7[] = { softdouble(0.03125), softdouble(0.109375), softdouble(0.21875), softdouble(0.28125), softdouble(0.21875), softdouble(0.109375), softdouble(0.03125) };
1790 return std::vector<T>(v7, v7 + 7);
1795 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)
1796 softdouble scale2X = softdouble(-0.5*0.25)/(sigmaX*sigmaX);
1797 std::vector<softdouble> values(n);
1799 for(int i = 0, x = 1 - n; i < n; i++, x+=2 )
1801 // x = i - (n - 1)*0.5
1802 // t = std::exp(scale2X*x*x)
1803 values[i] = exp(softdouble(x*x)*scale2X);
1806 sum = softdouble::one()/sum;
1808 std::vector<T> kernel(n);
1809 for(int i = 0; i < n; i++ )
1811 kernel[i] = values[i] * sum;
1817 template <typename ET, typename FT>
1818 void hlineSmooth1N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int)
1820 for (int i = 0; i < len*cn; i++, src++, dst++)
1821 *dst = (*m) * (*src);
1824 void hlineSmooth1N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int)
1827 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
1829 for (; i <= lencn - 16; i += 16)
1831 v_uint8x16 v_src = v_load(src + i);
1832 v_uint16x8 v_tmp0, v_tmp1;
1833 v_expand(v_src, v_tmp0, v_tmp1);
1834 v_store((uint16_t*)dst + i, v_mul*v_tmp0);
1835 v_store((uint16_t*)dst + i + 8, v_mul*v_tmp1);
1839 v_uint16x8 v_src = v_load_expand(src + i);
1840 v_store((uint16_t*)dst + i, v_mul*v_src);
1843 for (; i < lencn; i++)
1844 dst[i] = m[0] * src[i];
1846 template <typename ET, typename FT>
1847 void hlineSmooth1N1(const ET* src, int cn, const FT*, int, FT* dst, int len, int)
1849 for (int i = 0; i < len*cn; i++, src++, dst++)
1853 void hlineSmooth1N1<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int)
1857 for (; i <= lencn - 16; i += 16)
1859 v_uint8x16 v_src = v_load(src + i);
1860 v_uint16x8 v_tmp0, v_tmp1;
1861 v_expand(v_src, v_tmp0, v_tmp1);
1862 v_store((uint16_t*)dst + i, v_shl<8>(v_tmp0));
1863 v_store((uint16_t*)dst + i + 8, v_shl<8>(v_tmp1));
1867 v_uint16x8 v_src = v_load_expand(src + i);
1868 v_store((uint16_t*)dst + i, v_shl<8>(v_src));
1871 for (; i < lencn; i++)
1874 template <typename ET, typename FT>
1875 void hlineSmooth3N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
1879 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1880 for (int k = 0; k < cn; k++)
1881 dst[k] = msum * src[k];
1885 // Point that fall left from border
1886 for (int k = 0; k < cn; k++)
1887 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1888 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1890 int src_idx = borderInterpolate(-1, len, borderType);
1891 for (int k = 0; k < cn; k++)
1892 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1895 src += cn; dst += cn;
1896 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1897 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1899 // Point that fall right from border
1900 for (int k = 0; k < cn; k++)
1901 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1902 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1904 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1905 for (int k = 0; k < cn; k++)
1906 dst[k] = dst[k] + m[2] * src[src_idx + k];
1911 void hlineSmooth3N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
1915 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1916 for (int k = 0; k < cn; k++)
1917 dst[k] = msum * src[k];
1921 // Point that fall left from border
1922 for (int k = 0; k < cn; k++)
1923 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1924 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1926 int src_idx = borderInterpolate(-1, len, borderType);
1927 for (int k = 0; k < cn; k++)
1928 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1931 src += cn; dst += cn;
1932 int i = cn, lencn = (len - 1)*cn;
1933 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
1934 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
1935 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
1936 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
1938 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
1939 v_expand(v_load(src - cn), v_src00, v_src01);
1940 v_expand(v_load(src), v_src10, v_src11);
1941 v_expand(v_load(src + cn), v_src20, v_src21);
1942 v_store((uint16_t*)dst, v_src00 * v_mul0 + v_src10 * v_mul1 + v_src20 * v_mul2);
1943 v_store((uint16_t*)dst + 8, v_src01 * v_mul0 + v_src11 * v_mul1 + v_src21 * v_mul2);
1945 for (; i < lencn; i++, src++, dst++)
1946 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1948 // Point that fall right from border
1949 for (int k = 0; k < cn; k++)
1950 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1951 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1953 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1954 for (int k = 0; k < cn; k++)
1955 dst[k] = dst[k] + m[2] * src[src_idx + k];
1959 template <typename ET, typename FT>
1960 void hlineSmooth3N121(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
1964 if(borderType != BORDER_CONSTANT)
1965 for (int k = 0; k < cn; k++)
1966 dst[k] = FT(src[k]);
1968 for (int k = 0; k < cn; k++)
1969 dst[k] = FT(src[k])>>1;
1973 // Point that fall left from border
1974 for (int k = 0; k < cn; k++)
1975 dst[k] = (FT(src[k])>>1) + (FT(src[cn + k])>>2);
1976 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1978 int src_idx = borderInterpolate(-1, len, borderType);
1979 for (int k = 0; k < cn; k++)
1980 dst[k] = dst[k] + (FT(src[src_idx*cn + k])>>2);
1983 src += cn; dst += cn;
1984 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1985 *dst = (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[0])>>1);
1987 // Point that fall right from border
1988 for (int k = 0; k < cn; k++)
1989 dst[k] = (FT(src[k - cn])>>2) + (FT(src[k])>>1);
1990 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1992 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1993 for (int k = 0; k < cn; k++)
1994 dst[k] = dst[k] + (FT(src[src_idx + k])>>2);
1999 void hlineSmooth3N121<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2003 if (borderType != BORDER_CONSTANT)
2004 for (int k = 0; k < cn; k++)
2005 dst[k] = ufixedpoint16(src[k]);
2007 for (int k = 0; k < cn; k++)
2008 dst[k] = ufixedpoint16(src[k]) >> 1;
2012 // Point that fall left from border
2013 for (int k = 0; k < cn; k++)
2014 dst[k] = (ufixedpoint16(src[k])>>1) + (ufixedpoint16(src[cn + k])>>2);
2015 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2017 int src_idx = borderInterpolate(-1, len, borderType);
2018 for (int k = 0; k < cn; k++)
2019 dst[k] = dst[k] + (ufixedpoint16(src[src_idx*cn + k])>>2);
2022 src += cn; dst += cn;
2023 int i = cn, lencn = (len - 1)*cn;
2024 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2026 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2027 v_expand(v_load(src - cn), v_src00, v_src01);
2028 v_expand(v_load(src), v_src10, v_src11);
2029 v_expand(v_load(src + cn), v_src20, v_src21);
2030 v_store((uint16_t*)dst, (v_src00 + v_src20 + (v_src10 << 1)) << 6);
2031 v_store((uint16_t*)dst + 8, (v_src01 + v_src21 + (v_src11 << 1)) << 6);
2033 for (; i < lencn; i++, src++, dst++)
2034 *((uint16_t*)dst) = (uint16_t(src[-cn]) + uint16_t(src[cn]) + (uint16_t(src[0]) << 1)) << 6;
2036 // Point that fall right from border
2037 for (int k = 0; k < cn; k++)
2038 dst[k] = (ufixedpoint16(src[k - cn])>>2) + (ufixedpoint16(src[k])>>1);
2039 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2041 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2042 for (int k = 0; k < cn; k++)
2043 dst[k] = dst[k] + (ufixedpoint16(src[src_idx + k])>>2);
2047 template <typename ET, typename FT>
2048 void hlineSmooth3Naba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2052 FT msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2053 for (int k = 0; k < cn; k++)
2054 dst[k] = msum * src[k];
2058 // Point that fall left from border
2059 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2061 int src_idx = borderInterpolate(-1, len, borderType);
2062 for (int k = 0; k < cn; k++)
2063 dst[k] = m[1] * src[k] + m[0] * src[cn + k] + m[0] * src[src_idx*cn + k];
2067 for (int k = 0; k < cn; k++)
2068 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2071 src += cn; dst += cn;
2072 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
2073 *dst = m[1] * src[0] + m[0] * src[-cn] + m[0] * src[cn];
2075 // Point that fall right from border
2076 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2078 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2079 for (int k = 0; k < cn; k++)
2080 dst[k] = m[1] * src[k] + m[0] * src[k - cn] + m[0] * src[src_idx + k];
2084 for (int k = 0; k < cn; k++)
2085 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2090 void hlineSmooth3Naba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2094 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2095 for (int k = 0; k < cn; k++)
2096 dst[k] = msum * src[k];
2100 // Point that fall left from border
2101 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2103 int src_idx = borderInterpolate(-1, len, borderType);
2104 for (int k = 0; k < cn; k++)
2105 ((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]));
2109 for (int k = 0; k < cn; k++)
2110 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2113 src += cn; dst += cn;
2114 int i = cn, lencn = (len - 1)*cn;
2115 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2116 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)m+1));
2117 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2119 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2120 v_expand(v_load(src - cn), v_src00, v_src01);
2121 v_expand(v_load(src), v_src10, v_src11);
2122 v_expand(v_load(src + cn), v_src20, v_src21);
2123 v_store((uint16_t*)dst, (v_src00 + v_src20) * v_mul0 + v_src10 * v_mul1);
2124 v_store((uint16_t*)dst + 8, (v_src01 + v_src21) * v_mul0 + v_src11 * v_mul1);
2126 for (; i < lencn; i++, src++, dst++)
2127 *((uint16_t*)dst) = ((uint16_t*)m)[1] * src[0] + ((uint16_t*)m)[0] * ((uint16_t)(src[-cn]) + (uint16_t)(src[cn]));
2129 // Point that fall right from border
2130 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2132 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2133 for (int k = 0; k < cn; k++)
2134 ((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]));
2138 for (int k = 0; k < cn; k++)
2139 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2143 template <typename ET, typename FT>
2144 void hlineSmooth5N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2148 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2149 for (int k = 0; k < cn; k++)
2150 dst[k] = msum * src[k];
2154 if (borderType == BORDER_CONSTANT)
2155 for (int k = 0; k < cn; k++)
2157 dst[k ] = m[2] * src[k] + m[3] * src[k+cn];
2158 dst[k+cn] = m[1] * src[k] + m[2] * src[k+cn];
2162 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2163 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2164 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2165 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2166 for (int k = 0; k < cn; k++)
2168 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];
2169 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];
2175 if (borderType == BORDER_CONSTANT)
2176 for (int k = 0; k < cn; k++)
2178 dst[k ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn];
2179 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn];
2180 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn];
2184 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2185 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2186 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2187 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2188 for (int k = 0; k < cn; k++)
2190 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];
2191 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];
2192 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];
2198 // Points that fall left from border
2199 for (int k = 0; k < cn; k++)
2201 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2*cn + k];
2202 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2*cn + k] + m[4] * src[3*cn + k];
2204 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2206 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2207 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2208 for (int k = 0; k < cn; k++)
2210 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2211 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2215 src += 2*cn; dst += 2*cn;
2216 for (int i = 2*cn; i < (len - 2)*cn; i++, src++, dst++)
2217 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2219 // Points that fall right from border
2220 for (int k = 0; k < cn; k++)
2222 dst[k] = m[0] * src[k - 2*cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2223 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2225 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2227 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2228 int idxp2 = (borderInterpolate(len+1, len, borderType) - (len - 2))*cn;
2229 for (int k = 0; k < cn; k++)
2231 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2232 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2238 void hlineSmooth5N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2242 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2243 for (int k = 0; k < cn; k++)
2244 dst[k] = msum * src[k];
2248 if (borderType == BORDER_CONSTANT)
2249 for (int k = 0; k < cn; k++)
2251 dst[k] = m[2] * src[k] + m[3] * src[k + cn];
2252 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2256 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2257 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2258 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2259 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2260 for (int k = 0; k < cn; k++)
2262 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];
2263 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];
2269 if (borderType == BORDER_CONSTANT)
2270 for (int k = 0; k < cn; k++)
2272 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn];
2273 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn];
2274 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2278 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2279 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2280 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2281 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2282 for (int k = 0; k < cn; k++)
2284 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];
2285 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];
2286 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];
2292 // Points that fall left from border
2293 for (int k = 0; k < cn; k++)
2295 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2 * cn + k];
2296 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2 * cn + k] + m[4] * src[3 * cn + k];
2298 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2300 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2301 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2302 for (int k = 0; k < cn; k++)
2304 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2305 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2309 src += 2 * cn; dst += 2 * cn;
2310 int i = 2*cn, lencn = (len - 2)*cn;
2311 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2312 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2313 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2314 v_uint16x8 v_mul3 = v_setall_u16(*((uint16_t*)(m + 3)));
2315 v_uint16x8 v_mul4 = v_setall_u16(*((uint16_t*)(m + 4)));
2316 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2318 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2319 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2320 v_expand(v_load(src - cn), v_src10, v_src11);
2321 v_expand(v_load(src), v_src20, v_src21);
2322 v_expand(v_load(src + cn), v_src30, v_src31);
2323 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2324 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);
2325 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);
2327 for (; i < lencn; i++, src++, dst++)
2328 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2330 // Points that fall right from border
2331 for (int k = 0; k < cn; k++)
2333 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2334 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2336 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2338 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2339 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2340 for (int k = 0; k < cn; k++)
2342 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2343 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2348 template <typename ET, typename FT>
2349 void hlineSmooth5N14641(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
2353 if (borderType == BORDER_CONSTANT)
2354 for (int k = 0; k < cn; k++)
2355 dst[k] = (FT(src[k])>>3)*(uint8_t)3;
2357 for (int k = 0; k < cn; k++)
2362 if (borderType == BORDER_CONSTANT)
2363 for (int k = 0; k < cn; k++)
2365 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2);
2366 dst[k + cn] = (FT(src[k]) >> 2) + (FT(src[k + cn])>>4)*(uint8_t)6;
2370 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2371 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2372 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2373 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2374 for (int k = 0; k < cn; k++)
2376 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);
2377 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);
2383 if (borderType == BORDER_CONSTANT)
2384 for (int k = 0; k < cn; k++)
2386 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + 2 * cn])>>4);
2387 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2);
2388 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k])>>4);
2392 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2393 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2394 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2395 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2396 for (int k = 0; k < cn; k++)
2398 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);
2399 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);
2400 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);
2406 // Points that fall left from border
2407 for (int k = 0; k < cn; k++)
2409 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[cn + k])>>2) + (FT(src[2 * cn + k])>>4);
2410 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);
2412 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2414 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2415 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2416 for (int k = 0; k < cn; k++)
2418 dst[k] = dst[k] + (FT(src[idxm2 + k])>>4) + (FT(src[idxm1 + k])>>2);
2419 dst[k + cn] = dst[k + cn] + (FT(src[idxm1 + k])>>4);
2423 src += 2 * cn; dst += 2 * cn;
2424 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2425 *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);
2427 // Points that fall right from border
2428 for (int k = 0; k < cn; k++)
2430 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);
2431 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k - cn])>>4);
2433 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2435 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2436 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2437 for (int k = 0; k < cn; k++)
2439 dst[k] = dst[k] + (FT(src[idxp1 + k])>>4);
2440 dst[k + cn] = dst[k + cn] + (FT(src[idxp1 + k])>>2) + (FT(src[idxp2 + k])>>4);
2446 void hlineSmooth5N14641<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2450 if (borderType == BORDER_CONSTANT)
2451 for (int k = 0; k < cn; k++)
2452 dst[k] = (ufixedpoint16(src[k])>>3) * (uint8_t)3;
2455 for (int k = 0; k < cn; k++)
2461 if (borderType == BORDER_CONSTANT)
2462 for (int k = 0; k < cn; k++)
2464 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2);
2465 dst[k + cn] = (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6;
2469 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2470 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2471 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2472 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2473 for (int k = 0; k < cn; k++)
2475 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);
2476 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);
2482 if (borderType == BORDER_CONSTANT)
2483 for (int k = 0; k < cn; k++)
2485 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4);
2486 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2);
2487 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k]) >> 4);
2491 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2492 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2493 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2494 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2495 for (int k = 0; k < cn; k++)
2497 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);
2498 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);
2499 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);
2505 // Points that fall left from border
2506 for (int k = 0; k < cn; k++)
2508 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[cn + k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 4);
2509 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);
2511 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2513 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2514 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2515 for (int k = 0; k < cn; k++)
2517 dst[k] = dst[k] + (ufixedpoint16(src[idxm2 + k]) >> 4) + (ufixedpoint16(src[idxm1 + k]) >> 2);
2518 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxm1 + k]) >> 4);
2522 src += 2 * cn; dst += 2 * cn;
2523 int i = 2 * cn, lencn = (len - 2)*cn;
2524 v_uint16x8 v_6 = v_setall_u16(6);
2525 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2527 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2528 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2529 v_expand(v_load(src - cn), v_src10, v_src11);
2530 v_expand(v_load(src), v_src20, v_src21);
2531 v_expand(v_load(src + cn), v_src30, v_src31);
2532 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2533 v_store((uint16_t*)dst, (v_src20 * v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40) << 4);
2534 v_store((uint16_t*)dst + 8, (v_src21 * v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41) << 4);
2536 for (; i < lencn; i++, src++, dst++)
2537 *((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;
2539 // Points that fall right from border
2540 for (int k = 0; k < cn; k++)
2542 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);
2543 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k - cn]) >> 4);
2545 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2547 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2548 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2549 for (int k = 0; k < cn; k++)
2551 dst[k] = dst[k] + (ufixedpoint16(src[idxp1 + k]) >> 4);
2552 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxp1 + k]) >> 2) + (ufixedpoint16(src[idxp2 + k]) >> 4);
2557 template <typename ET, typename FT>
2558 void hlineSmooth5Nabcba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2562 FT msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1])<<1) + m[2] : m[2];
2563 for (int k = 0; k < cn; k++)
2564 dst[k] = msum * src[k];
2568 if (borderType == BORDER_CONSTANT)
2569 for (int k = 0; k < cn; k++)
2571 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2572 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2576 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2577 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2578 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2579 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2580 for (int k = 0; k < cn; k++)
2582 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];
2583 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];
2589 if (borderType == BORDER_CONSTANT)
2590 for (int k = 0; k < cn; k++)
2592 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2593 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + 2 * cn];
2594 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2598 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2599 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2600 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2601 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2602 for (int k = 0; k < cn; k++)
2604 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];
2605 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];
2606 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];
2612 // Points that fall left from border
2613 for (int k = 0; k < cn; k++)
2615 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2616 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[1] * src[2 * cn + k] + m[0] * src[3 * cn + k];
2618 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2620 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2621 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2622 for (int k = 0; k < cn; k++)
2624 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2625 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2629 src += 2 * cn; dst += 2 * cn;
2630 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2631 *dst = m[0] * src[-2 * cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2 * cn];
2633 // Points that fall right from border
2634 for (int k = 0; k < cn; k++)
2636 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2637 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2639 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2641 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2642 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2643 for (int k = 0; k < cn; k++)
2645 dst[k] = dst[k] + m[0] * src[idxp1 + k];
2646 dst[k + cn] = dst[k + cn] + m[1] * src[idxp1 + k] + m[0] * src[idxp2 + k];
2652 void hlineSmooth5Nabcba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2656 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1]) << 1) + m[2] : m[2];
2657 for (int k = 0; k < cn; k++)
2658 dst[k] = msum * src[k];
2662 if (borderType == BORDER_CONSTANT)
2663 for (int k = 0; k < cn; k++)
2665 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2666 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2670 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2671 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2672 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2673 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2674 for (int k = 0; k < cn; k++)
2676 ((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]));
2677 ((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];
2683 if (borderType == BORDER_CONSTANT)
2684 for (int k = 0; k < cn; k++)
2686 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2687 ((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];
2688 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2692 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2693 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2694 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2695 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2696 for (int k = 0; k < cn; k++)
2698 ((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]));
2699 ((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]));
2700 ((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];
2706 // Points that fall left from border
2707 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2709 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2710 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2711 for (int k = 0; k < cn; k++)
2713 ((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]));
2714 ((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]));
2719 for (int k = 0; k < cn; k++)
2721 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2722 ((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];
2726 src += 2 * cn; dst += 2 * cn;
2727 int i = 2 * cn, lencn = (len - 2)*cn;
2728 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2729 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2730 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2731 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2733 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2734 v_expand(v_load(src - 2 * cn), v_src00, v_src01);
2735 v_expand(v_load(src - cn), v_src10, v_src11);
2736 v_expand(v_load(src), v_src20, v_src21);
2737 v_expand(v_load(src + cn), v_src30, v_src31);
2738 v_expand(v_load(src + 2 * cn), v_src40, v_src41);
2739 v_store((uint16_t*)dst, (v_src00 + v_src40) * v_mul0 + (v_src10 + v_src30)* v_mul1 + v_src20 * v_mul2);
2740 v_store((uint16_t*)dst + 8, (v_src01 + v_src41) * v_mul0 + (v_src11 + v_src31) * v_mul1 + v_src21 * v_mul2);
2742 for (; i < lencn; i++, src++, dst++)
2743 *((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];
2745 // Points that fall right from border
2746 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2748 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2749 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2750 for (int k = 0; k < cn; k++)
2752 ((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];
2753 ((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];
2758 for (int k = 0; k < cn; k++)
2760 ((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];
2761 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2766 template <typename ET, typename FT>
2767 void hlineSmooth(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2769 int pre_shift = n / 2;
2770 int post_shift = n - pre_shift;
2772 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2774 for (int k = 0; k < cn; k++)
2775 dst[k] = m[pre_shift-i] * src[k];
2776 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2777 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2779 int src_idx = borderInterpolate(j, len, borderType);
2780 for (int k = 0; k < cn; k++)
2781 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2784 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2785 for (int k = 0; k < cn; k++)
2786 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2787 if (borderType != BORDER_CONSTANT)
2788 for (; j < i + post_shift; j++, mid++)
2790 int src_idx = borderInterpolate(j, len, borderType);
2791 for (int k = 0; k < cn; k++)
2792 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2796 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2798 *dst = m[0] * src[0];
2799 for (int j = 1; j < n; j++)
2800 *dst = *dst + m[j] * src[j*cn];
2803 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2805 for (int k = 0; k < cn; k++)
2806 dst[k] = m[0] * src[k];
2808 for (; j < len - i; j++)
2809 for (int k = 0; k < cn; k++)
2810 dst[k] = dst[k] + m[j] * src[j*cn + k];
2811 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2814 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2815 for (int k = 0; k < cn; k++)
2816 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2821 void hlineSmooth<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2823 int pre_shift = n / 2;
2824 int post_shift = n - pre_shift;
2826 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2828 for (int k = 0; k < cn; k++)
2829 dst[k] = m[pre_shift - i] * src[k];
2830 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2831 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2833 int src_idx = borderInterpolate(j, len, borderType);
2834 for (int k = 0; k < cn; k++)
2835 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2838 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2839 for (int k = 0; k < cn; k++)
2840 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2841 if (borderType != BORDER_CONSTANT)
2842 for (; j < i + post_shift; j++, mid++)
2844 int src_idx = borderInterpolate(j, len, borderType);
2845 for (int k = 0; k < cn; k++)
2846 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2850 int lencn = (len - post_shift + 1)*cn;
2851 for (; i <= lencn - 16; i+=16, src+=16, dst+=16)
2853 v_uint16x8 v_src0, v_src1;
2854 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
2855 v_expand(v_load(src), v_src0, v_src1);
2856 v_uint16x8 v_res0 = v_src0 * v_mul;
2857 v_uint16x8 v_res1 = v_src1 * v_mul;
2858 for (int j = 1; j < n; j++)
2860 v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2861 v_expand(v_load(src + j * cn), v_src0, v_src1);
2862 v_res0 += v_src0 * v_mul;
2863 v_res1 += v_src1 * v_mul;
2865 v_store((uint16_t*)dst, v_res0);
2866 v_store((uint16_t*)dst+8, v_res1);
2868 for (; i < lencn; i++, src++, dst++)
2870 *dst = m[0] * src[0];
2871 for (int j = 1; j < n; j++)
2872 *dst = *dst + m[j] * src[j*cn];
2875 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2877 for (int k = 0; k < cn; k++)
2878 dst[k] = m[0] * src[k];
2880 for (; j < len - i; j++)
2881 for (int k = 0; k < cn; k++)
2882 dst[k] = dst[k] + m[j] * src[j*cn + k];
2883 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2886 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2887 for (int k = 0; k < cn; k++)
2888 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2892 template <typename ET, typename FT>
2893 void hlineSmoothONa_yzy_a(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2895 int pre_shift = n / 2;
2896 int post_shift = n - pre_shift;
2898 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2900 for (int k = 0; k < cn; k++)
2901 dst[k] = m[pre_shift - i] * src[k];
2902 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2903 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2905 int src_idx = borderInterpolate(j, len, borderType);
2906 for (int k = 0; k < cn; k++)
2907 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2910 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2911 for (int k = 0; k < cn; k++)
2912 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2913 if (borderType != BORDER_CONSTANT)
2914 for (; j < i + post_shift; j++, mid++)
2916 int src_idx = borderInterpolate(j, len, borderType);
2917 for (int k = 0; k < cn; k++)
2918 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2922 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2924 *dst = m[pre_shift] * src[pre_shift*cn];
2925 for (int j = 0; j < pre_shift; j++)
2926 *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n-1-j)*cn];
2929 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2931 for (int k = 0; k < cn; k++)
2932 dst[k] = m[0] * src[k];
2934 for (; j < len - i; j++)
2935 for (int k = 0; k < cn; k++)
2936 dst[k] = dst[k] + m[j] * src[j*cn + k];
2937 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2940 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2941 for (int k = 0; k < cn; k++)
2942 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2947 void hlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2949 int pre_shift = n / 2;
2950 int post_shift = n - pre_shift;
2952 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2954 for (int k = 0; k < cn; k++)
2955 dst[k] = m[pre_shift - i] * src[k];
2956 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2957 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2959 int src_idx = borderInterpolate(j, len, borderType);
2960 for (int k = 0; k < cn; k++)
2961 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2964 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2965 for (int k = 0; k < cn; k++)
2966 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2967 if (borderType != BORDER_CONSTANT)
2968 for (; j < i + post_shift; j++, mid++)
2970 int src_idx = borderInterpolate(j, len, borderType);
2971 for (int k = 0; k < cn; k++)
2972 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2976 int lencn = (len - post_shift + 1)*cn;
2977 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2979 v_uint16x8 v_src00, v_src01, v_srcN00, v_srcN01;
2981 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)(m + pre_shift)));
2982 v_expand(v_load(src + pre_shift * cn), v_src00, v_src01);
2983 v_uint16x8 v_res0 = v_src00 * v_mul;
2984 v_uint16x8 v_res1 = v_src01 * v_mul;
2985 for (int j = 0; j < pre_shift; j ++)
2987 v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2988 v_expand(v_load(src + j * cn), v_src00, v_src01);
2989 v_expand(v_load(src + (n - 1 - j)*cn), v_srcN00, v_srcN01);
2990 v_res0 += (v_src00 + v_srcN00) * v_mul;
2991 v_res1 += (v_src01 + v_srcN01) * v_mul;
2994 v_store((uint16_t*)dst, v_res0);
2995 v_store((uint16_t*)dst + 8, v_res1);
2997 for (; i < lencn; i++, src++, dst++)
2999 *dst = m[pre_shift] * src[pre_shift*cn];
3000 for (int j = 0; j < pre_shift; j++)
3001 *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n - 1 - j)*cn];
3004 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
3006 for (int k = 0; k < cn; k++)
3007 dst[k] = m[0] * src[k];
3009 for (; j < len - i; j++)
3010 for (int k = 0; k < cn; k++)
3011 dst[k] = dst[k] + m[j] * src[j*cn + k];
3012 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3015 int src_idx = borderInterpolate(i + j, len, borderType) - i;
3016 for (int k = 0; k < cn; k++)
3017 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
3021 template <typename ET, typename FT>
3022 void vlineSmooth1N(const FT* const * src, const FT* m, int, ET* dst, int len)
3024 const FT* src0 = src[0];
3025 for (int i = 0; i < len; i++)
3026 dst[i] = *m * src0[i];
3029 void vlineSmooth1N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3031 const ufixedpoint16* src0 = src[0];
3032 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
3034 v_uint16x8 v_1 = v_setall_u16(1);
3038 for (; i <= len - 16; i += 16)
3040 v_uint16x8 v_src0 = v_load((uint16_t*)src0 + i);
3041 v_uint16x8 v_src1 = v_load((uint16_t*)src0 + i + 8);
3044 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),
3045 _mm_srli_epi16(_mm_add_epi16(v_1.val, _mm_mulhi_epu16(v_src1.val, v_mul.val)),1));
3047 v_uint32x4 v_res0, v_res1, v_res2, v_res3;
3048 v_mul_expand(v_src0, v_mul, v_res0, v_res1);
3049 v_mul_expand(v_src1, v_mul, v_res2, v_res3);
3050 v_res = v_pack(v_rshr_pack<16>(v_res0, v_res1), v_rshr_pack<16>(v_res2, v_res3));
3052 v_store(dst + i, v_res);
3054 for (; i < len; i++)
3055 dst[i] = m[0] * src0[i];
3057 template <typename ET, typename FT>
3058 void vlineSmooth1N1(const FT* const * src, const FT*, int, ET* dst, int len)
3060 const FT* src0 = src[0];
3061 for (int i = 0; i < len; i++)
3065 void vlineSmooth1N1<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3067 const ufixedpoint16* src0 = src[0];
3069 for (; i <= len - 8; i += 8)
3070 v_rshr_pack_store<8>(dst + i, v_load((uint16_t*)(src0 + i)));
3071 for (; i < len; i++)
3074 template <typename ET, typename FT>
3075 void vlineSmooth3N(const FT* const * src, const FT* m, int, ET* dst, int len)
3077 for (int i = 0; i < len; i++)
3078 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3081 void vlineSmooth3N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3084 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3085 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3088 ufixedpoint32 val[] = { (m[0] + m[1] + m[2]) * ufixedpoint16((uint8_t)128) };
3089 v_128_4 = v_setall_s32(*((int32_t*)val));
3091 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3092 v_int16x8 v_mul2 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 2))));
3093 for (; i <= len - 32; i += 32)
3095 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3096 v_int16x8 v_tmp0, v_tmp1;
3098 v_src00 = v_load((int16_t*)(src[0]) + i);
3099 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3100 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3101 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3102 v_src10 = v_load((int16_t*)(src[1]) + i);
3103 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3104 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3105 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3106 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3107 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3108 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3109 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3110 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3111 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3112 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3113 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3114 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3115 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3116 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3117 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3119 v_int32x4 v_resj0, v_resj1;
3120 v_src00 = v_load((int16_t*)(src[2]) + i);
3121 v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3122 v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3123 v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3124 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul2, v_resj0, v_resj1);
3127 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul2, v_resj0, v_resj1);
3130 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul2, v_resj0, v_resj1);
3133 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul2, v_resj0, v_resj1);
3146 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3147 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3148 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3149 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3151 for (; i < len; i++)
3152 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3154 template <typename ET, typename FT>
3155 void vlineSmooth3N121(const FT* const * src, const FT*, int, ET* dst, int len)
3157 for (int i = 0; i < len; i++)
3158 dst[i] = (FT::WT(src[0][i]) >> 2) + (FT::WT(src[2][i]) >> 2) + (FT::WT(src[1][i]) >> 1);
3161 void vlineSmooth3N121<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3164 for (; i <= len - 16; i += 16)
3166 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;
3167 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3168 v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3169 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3170 v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3171 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3172 v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3173 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)),
3174 v_rshr_pack<10>(v_src02 + v_src22 + (v_src12 + v_src12), v_src03 + v_src23 + (v_src13 + v_src13))));
3176 for (; i < len; i++)
3177 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;
3179 template <typename ET, typename FT>
3180 void vlineSmooth5N(const FT* const * src, const FT* m, int, ET* dst, int len)
3182 for (int i = 0; i < len; i++)
3183 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];
3186 void vlineSmooth5N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3189 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3190 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3193 ufixedpoint32 val[] = { (m[0] + m[1] + m[2] + m[3] + m[4]) * ufixedpoint16((uint8_t)128) };
3194 v_128_4 = v_setall_s32(*((int32_t*)val));
3196 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3197 v_int16x8 v_mul23 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + 2))));
3198 v_int16x8 v_mul4 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 4))));
3199 for (; i <= len - 32; i += 32)
3201 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3202 v_int16x8 v_tmp0, v_tmp1;
3204 v_src00 = v_load((int16_t*)(src[0]) + i);
3205 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3206 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3207 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3208 v_src10 = v_load((int16_t*)(src[1]) + i);
3209 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3210 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3211 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3212 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3213 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3214 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3215 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3216 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3217 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3218 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3219 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3220 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3221 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3222 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3223 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3225 v_src00 = v_load((int16_t*)(src[2]) + i);
3226 v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3227 v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3228 v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3229 v_src10 = v_load((int16_t*)(src[3]) + i);
3230 v_src11 = v_load((int16_t*)(src[3]) + i + 8);
3231 v_src12 = v_load((int16_t*)(src[3]) + i + 16);
3232 v_src13 = v_load((int16_t*)(src[3]) + i + 24);
3233 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3234 v_res0 += v_dotprod(v_tmp0, v_mul23);
3235 v_res1 += v_dotprod(v_tmp1, v_mul23);
3236 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3237 v_res2 += v_dotprod(v_tmp0, v_mul23);
3238 v_res3 += v_dotprod(v_tmp1, v_mul23);
3239 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3240 v_res4 += v_dotprod(v_tmp0, v_mul23);
3241 v_res5 += v_dotprod(v_tmp1, v_mul23);
3242 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3243 v_res6 += v_dotprod(v_tmp0, v_mul23);
3244 v_res7 += v_dotprod(v_tmp1, v_mul23);
3246 v_int32x4 v_resj0, v_resj1;
3247 v_src00 = v_load((int16_t*)(src[4]) + i);
3248 v_src01 = v_load((int16_t*)(src[4]) + i + 8);
3249 v_src02 = v_load((int16_t*)(src[4]) + i + 16);
3250 v_src03 = v_load((int16_t*)(src[4]) + i + 24);
3251 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul4, v_resj0, v_resj1);
3254 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul4, v_resj0, v_resj1);
3257 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul4, v_resj0, v_resj1);
3260 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul4, v_resj0, v_resj1);
3273 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3274 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3275 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3276 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3278 for (; i < len; i++)
3279 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];
3281 template <typename ET, typename FT>
3282 void vlineSmooth5N14641(const FT* const * src, const FT*, int, ET* dst, int len)
3284 for (int i = 0; i < len; i++)
3285 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;
3288 void vlineSmooth5N14641<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3291 v_uint32x4 v_6 = v_setall_u32(6);
3292 for (; i <= len - 16; i += 16)
3294 v_uint32x4 v_src00, v_src10, v_src20, v_src30, v_src40;
3295 v_uint32x4 v_src01, v_src11, v_src21, v_src31, v_src41;
3296 v_uint32x4 v_src02, v_src12, v_src22, v_src32, v_src42;
3297 v_uint32x4 v_src03, v_src13, v_src23, v_src33, v_src43;
3298 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3299 v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3300 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3301 v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3302 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3303 v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3304 v_expand(v_load((uint16_t*)(src[3]) + i), v_src30, v_src31);
3305 v_expand(v_load((uint16_t*)(src[3]) + i + 8), v_src32, v_src33);
3306 v_expand(v_load((uint16_t*)(src[4]) + i), v_src40, v_src41);
3307 v_expand(v_load((uint16_t*)(src[4]) + i + 8), v_src42, v_src43);
3308 v_store(dst + i, v_pack(v_rshr_pack<12>(v_src20*v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40,
3309 v_src21*v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41),
3310 v_rshr_pack<12>(v_src22*v_6 + ((v_src12 + v_src32) << 2) + v_src02 + v_src42,
3311 v_src23*v_6 + ((v_src13 + v_src33) << 2) + v_src03 + v_src43)));
3313 for (; i < len; i++)
3314 dst[i] = ((uint32_t)(((uint16_t*)(src[2]))[i]) * 6 +
3315 (((uint32_t)(((uint16_t*)(src[1]))[i]) + (uint32_t)(((uint16_t*)(src[3]))[i])) << 2) +
3316 (uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[4]))[i]) + (1 << 11)) >> 12;
3318 template <typename ET, typename FT>
3319 void vlineSmooth(const FT* const * src, const FT* m, int n, ET* dst, int len)
3321 for (int i = 0; i < len; i++)
3323 typename FT::WT val = m[0] * src[0][i];
3324 for (int j = 1; j < n; j++)
3325 val = val + m[j] * src[j][i];
3330 void vlineSmooth<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3333 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3334 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3337 ufixedpoint16 msum = m[0] + m[1];
3338 for (int j = 2; j < n; j++)
3340 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3341 v_128_4 = v_setall_s32(*((int32_t*)val));
3343 for (; i <= len - 32; i += 32)
3345 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3346 v_int16x8 v_tmp0, v_tmp1;
3348 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3350 v_src00 = v_load((int16_t*)(src[0]) + i);
3351 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3352 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3353 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3354 v_src10 = v_load((int16_t*)(src[1]) + i);
3355 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3356 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3357 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3358 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3359 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul);
3360 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul);
3361 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3362 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul);
3363 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul);
3364 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3365 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul);
3366 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul);
3367 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3368 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul);
3369 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul);
3372 for (; j < n - 1; j+=2)
3374 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m+j))));
3376 v_src00 = v_load((int16_t*)(src[j]) + i);
3377 v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3378 v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3379 v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3380 v_src10 = v_load((int16_t*)(src[j+1]) + i);
3381 v_src11 = v_load((int16_t*)(src[j+1]) + i + 8);
3382 v_src12 = v_load((int16_t*)(src[j+1]) + i + 16);
3383 v_src13 = v_load((int16_t*)(src[j+1]) + i + 24);
3384 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3385 v_res0 += v_dotprod(v_tmp0, v_mul);
3386 v_res1 += v_dotprod(v_tmp1, v_mul);
3387 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3388 v_res2 += v_dotprod(v_tmp0, v_mul);
3389 v_res3 += v_dotprod(v_tmp1, v_mul);
3390 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3391 v_res4 += v_dotprod(v_tmp0, v_mul);
3392 v_res5 += v_dotprod(v_tmp1, v_mul);
3393 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3394 v_res6 += v_dotprod(v_tmp0, v_mul);
3395 v_res7 += v_dotprod(v_tmp1, v_mul);
3399 v_int32x4 v_resj0, v_resj1;
3400 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3401 v_src00 = v_load((int16_t*)(src[j]) + i);
3402 v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3403 v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3404 v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3405 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_resj0, v_resj1);
3408 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul, v_resj0, v_resj1);
3411 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul, v_resj0, v_resj1);
3414 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul, v_resj0, v_resj1);
3427 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3428 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3429 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3430 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3432 for (; i < len; i++)
3434 ufixedpoint32 val = m[0] * src[0][i];
3435 for (int j = 1; j < n; j++)
3437 val = val + m[j] * src[j][i];
3442 template <typename ET, typename FT>
3443 void vlineSmoothONa_yzy_a(const FT* const * src, const FT* m, int n, ET* dst, int len)
3445 int pre_shift = n / 2;
3446 for (int i = 0; i < len; i++)
3448 typename FT::WT val = m[pre_shift] * src[pre_shift][i];
3449 for (int j = 0; j < pre_shift; j++)
3450 val = val + m[j] * src[j][i] + m[j] * src[(n - 1 - j)][i];
3455 void vlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3457 int pre_shift = n / 2;
3459 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3460 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3463 ufixedpoint16 msum = m[0] + m[pre_shift] + m[n - 1];
3464 for (int j = 1; j < pre_shift; j++)
3465 msum = msum + m[j] + m[n - 1 - j];
3466 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3467 v_128_4 = v_setall_s32(*((int32_t*)val));
3469 for (; i <= len - 32; i += 32)
3471 v_int16x8 v_src00, v_src10, v_src20, v_src30, v_src01, v_src11, v_src21, v_src31;
3472 v_int32x4 v_res0, v_res1, v_res2, v_res3, v_res4, v_res5, v_res6, v_res7;
3473 v_int16x8 v_tmp0, v_tmp1, v_tmp2, v_tmp3, v_tmp4, v_tmp5, v_tmp6, v_tmp7;
3475 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + pre_shift))));
3476 v_src00 = v_load((int16_t*)(src[pre_shift]) + i);
3477 v_src10 = v_load((int16_t*)(src[pre_shift]) + i + 8);
3478 v_src20 = v_load((int16_t*)(src[pre_shift]) + i + 16);
3479 v_src30 = v_load((int16_t*)(src[pre_shift]) + i + 24);
3480 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_res0, v_res1);
3481 v_mul_expand(v_add_wrap(v_src10, v_128), v_mul, v_res2, v_res3);
3482 v_mul_expand(v_add_wrap(v_src20, v_128), v_mul, v_res4, v_res5);
3483 v_mul_expand(v_add_wrap(v_src30, v_128), v_mul, v_res6, v_res7);
3486 for (; j < pre_shift; j++)
3488 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3490 v_src00 = v_load((int16_t*)(src[j]) + i);
3491 v_src10 = v_load((int16_t*)(src[j]) + i + 8);
3492 v_src20 = v_load((int16_t*)(src[j]) + i + 16);
3493 v_src30 = v_load((int16_t*)(src[j]) + i + 24);
3494 v_src01 = v_load((int16_t*)(src[n - 1 - j]) + i);
3495 v_src11 = v_load((int16_t*)(src[n - 1 - j]) + i + 8);
3496 v_src21 = v_load((int16_t*)(src[n - 1 - j]) + i + 16);
3497 v_src31 = v_load((int16_t*)(src[n - 1 - j]) + i + 24);
3498 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src01, v_128), v_tmp0, v_tmp1);
3499 v_res0 += v_dotprod(v_tmp0, v_mul);
3500 v_res1 += v_dotprod(v_tmp1, v_mul);
3501 v_zip(v_add_wrap(v_src10, v_128), v_add_wrap(v_src11, v_128), v_tmp2, v_tmp3);
3502 v_res2 += v_dotprod(v_tmp2, v_mul);
3503 v_res3 += v_dotprod(v_tmp3, v_mul);
3504 v_zip(v_add_wrap(v_src20, v_128), v_add_wrap(v_src21, v_128), v_tmp4, v_tmp5);
3505 v_res4 += v_dotprod(v_tmp4, v_mul);
3506 v_res5 += v_dotprod(v_tmp5, v_mul);
3507 v_zip(v_add_wrap(v_src30, v_128), v_add_wrap(v_src31, v_128), v_tmp6, v_tmp7);
3508 v_res6 += v_dotprod(v_tmp6, v_mul);
3509 v_res7 += v_dotprod(v_tmp7, v_mul);
3521 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3522 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3523 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3524 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3526 for (; i < len; i++)
3528 ufixedpoint32 val = m[0] * src[0][i];
3529 for (int j = 1; j < n; j++)
3531 val = val + m[j] * src[j][i];
3536 template <typename ET, typename FT>
3537 class fixedSmoothInvoker : public ParallelLoopBody
3540 fixedSmoothInvoker(const ET* _src, size_t _src_stride, ET* _dst, size_t _dst_stride,
3541 int _width, int _height, int _cn, const FT* _kx, int _kxlen, const FT* _ky, int _kylen, int _borderType) : ParallelLoopBody(),
3542 src(_src), dst(_dst), src_stride(_src_stride), dst_stride(_dst_stride),
3543 width(_width), height(_height), cn(_cn), kx(_kx), ky(_ky), kxlen(_kxlen), kylen(_kylen), borderType(_borderType)
3547 if (kx[0] == FT::one())
3548 hlineSmoothFunc = hlineSmooth1N1;
3550 hlineSmoothFunc = hlineSmooth1N;
3552 else if (kxlen == 3)
3554 if (kx[0] == (FT::one()>>2)&&kx[1] == (FT::one()>>1)&&kx[2] == (FT::one()>>2))
3555 hlineSmoothFunc = hlineSmooth3N121;
3556 else if ((kx[0] - kx[2]).isZero())
3557 hlineSmoothFunc = hlineSmooth3Naba;
3559 hlineSmoothFunc = hlineSmooth3N;
3561 else if (kxlen == 5)
3563 if (kx[2] == (FT::one()*(uint8_t)3>>3) &&
3564 kx[1] == (FT::one()>>2) && kx[3] == (FT::one()>>2) &&
3565 kx[0] == (FT::one()>>4) && kx[4] == (FT::one()>>4))
3566 hlineSmoothFunc = hlineSmooth5N14641;
3567 else if (kx[0] == kx[4] && kx[1] == kx[3])
3568 hlineSmoothFunc = hlineSmooth5Nabcba;
3570 hlineSmoothFunc = hlineSmooth5N;
3572 else if (kxlen % 2 == 1)
3574 hlineSmoothFunc = hlineSmoothONa_yzy_a;
3575 for (int i = 0; i < kxlen / 2; i++)
3576 if (!(kx[i] == kx[kxlen - 1 - i]))
3578 hlineSmoothFunc = hlineSmooth;
3583 hlineSmoothFunc = hlineSmooth;
3586 if (ky[0] == FT::one())
3587 vlineSmoothFunc = vlineSmooth1N1;
3589 vlineSmoothFunc = vlineSmooth1N;
3591 else if (kylen == 3)
3593 if (ky[0] == (FT::one() >> 2) && ky[1] == (FT::one() >> 1) && ky[2] == (FT::one() >> 2))
3594 vlineSmoothFunc = vlineSmooth3N121;
3596 vlineSmoothFunc = vlineSmooth3N;
3598 else if (kylen == 5)
3600 if (ky[2] == (FT::one() * (uint8_t)3 >> 3) &&
3601 ky[1] == (FT::one() >> 2) && ky[3] == (FT::one() >> 2) &&
3602 ky[0] == (FT::one() >> 4) && ky[4] == (FT::one() >> 4))
3603 vlineSmoothFunc = vlineSmooth5N14641;
3605 vlineSmoothFunc = vlineSmooth5N;
3607 else if (kylen % 2 == 1)
3609 vlineSmoothFunc = vlineSmoothONa_yzy_a;
3610 for (int i = 0; i < kylen / 2; i++)
3611 if (!(ky[i] == ky[kylen - 1 - i]))
3613 vlineSmoothFunc = vlineSmooth;
3618 vlineSmoothFunc = vlineSmooth;
3620 virtual void operator() (const Range& range) const CV_OVERRIDE
3622 AutoBuffer<FT> _buf(width*cn*kylen);
3624 AutoBuffer<FT*> _ptrs(kylen*2);
3630 for (int i = range.start; i < range.end; i++)
3632 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[0], width, borderType);
3633 vlineSmoothFunc(ptrs, ky, kylen, dst + i * dst_stride, width*cn);
3636 else if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3638 int pre_shift = kylen / 2;
3639 int post_shift = kylen - pre_shift - 1;
3640 // First line evaluation
3641 int idst = range.start;
3642 int ifrom = max(0, idst - pre_shift);
3643 int ito = idst + post_shift + 1;
3646 for (; i < min(ito, height); i++, bufline++)
3648 ptrs[bufline+kylen] = ptrs[bufline] = buf + bufline * width*cn;
3649 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3651 for (; i < ito; i++, bufline++)
3653 int src_idx = borderInterpolate(i, height, borderType);
3654 if (src_idx < ifrom)
3656 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3657 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3661 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[src_idx - ifrom];
3664 for (int j = idst - pre_shift; j < 0; j++)
3666 int src_idx = borderInterpolate(j, height, borderType);
3669 ptrs[2*kylen + j] = ptrs[kylen + j] = buf + (kylen + j) * width*cn;
3670 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[kylen + j], width, borderType);
3674 ptrs[2*kylen + j] = ptrs[kylen + j] = ptrs[src_idx];
3677 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn); idst++;
3679 // border mode dependent part evaluation
3680 // i points to last src row to evaluate in convolution
3681 bufline %= kylen; ito = min(height, range.end + post_shift);
3682 for (; i < min(kylen, ito); i++, idst++)
3684 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3685 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3686 bufline = (bufline + 1) % kylen;
3687 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3689 // Points inside the border
3690 for (; i < ito; i++, idst++)
3692 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3693 bufline = (bufline + 1) % kylen;
3694 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3696 // Points that could fall below border
3697 for (; i < range.end + post_shift; i++, idst++)
3699 int src_idx = borderInterpolate(i, height, borderType);
3700 if ((i - src_idx) > kylen)
3701 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3703 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[(bufline + kylen - (i - src_idx)) % kylen];
3704 bufline = (bufline + 1) % kylen;
3705 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3710 int pre_shift = kylen / 2;
3711 int post_shift = kylen - pre_shift - 1;
3712 // First line evaluation
3713 int idst = range.start;
3714 int ifrom = idst - pre_shift;
3715 int ito = min(idst + post_shift + 1, height);
3716 int i = max(0, ifrom);
3718 for (; i < ito; i++, bufline++)
3720 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3721 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3725 vlineSmooth1N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3726 else if (bufline == 3)
3727 vlineSmooth3N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3728 else if (bufline == 5)
3729 vlineSmooth5N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3731 vlineSmooth(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3734 // border mode dependent part evaluation
3735 // i points to last src row to evaluate in convolution
3736 bufline %= kylen; ito = min(height, range.end + post_shift);
3737 for (; i < min(kylen, ito); i++, idst++)
3739 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3740 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3743 vlineSmooth3N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3744 else if (bufline == 5)
3745 vlineSmooth5N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3747 vlineSmooth(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3750 // Points inside the border
3751 if (i - max(0, ifrom) >= kylen)
3753 for (; i < ito; i++, idst++)
3755 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3756 bufline = (bufline + 1) % kylen;
3757 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3760 // Points that could fall below border
3761 // i points to first src row to evaluate in convolution
3762 bufline = (bufline + 1) % kylen;
3763 for (i = idst - pre_shift; i < range.end - pre_shift; i++, idst++, bufline++)
3764 if (height - i == 3)
3765 vlineSmooth3N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3766 else if (height - i == 5)
3767 vlineSmooth5N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3769 vlineSmooth(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3773 // i points to first src row to evaluate in convolution
3774 for (i = idst - pre_shift; i < min(range.end - pre_shift, 0); i++, idst++)
3776 vlineSmooth3N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3777 else if (height == 5)
3778 vlineSmooth5N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3780 vlineSmooth(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3781 for (; i < range.end - pre_shift; i++, idst++)
3782 if (height - i == 3)
3783 vlineSmooth3N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3784 else if (height - i == 5)
3785 vlineSmooth5N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3787 vlineSmooth(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3794 size_t src_stride, dst_stride;
3795 int width, height, cn;
3799 void(*hlineSmoothFunc)(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType);
3800 void(*vlineSmoothFunc)(const FT* const * src, const FT* m, int n, ET* dst, int len);
3802 fixedSmoothInvoker(const fixedSmoothInvoker&);
3803 fixedSmoothInvoker& operator=(const fixedSmoothInvoker&);
3806 static void getGaussianKernel(int n, double sigma, int ktype, Mat& res) { res = getGaussianKernel(n, sigma, ktype); }
3807 template <typename T> static void getGaussianKernel(int n, double sigma, int, std::vector<T>& res) { res = getFixedpointGaussianKernel<T>(n, sigma); }
3809 template <typename T>
3810 static void createGaussianKernels( T & kx, T & ky, int type, Size &ksize,
3811 double sigma1, double sigma2 )
3813 int depth = CV_MAT_DEPTH(type);
3817 // automatic detection of kernel size from sigma
3818 if( ksize.width <= 0 && sigma1 > 0 )
3819 ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3820 if( ksize.height <= 0 && sigma2 > 0 )
3821 ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3823 CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
3824 ksize.height > 0 && ksize.height % 2 == 1 );
3826 sigma1 = std::max( sigma1, 0. );
3827 sigma2 = std::max( sigma2, 0. );
3829 getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F), kx );
3830 if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
3833 getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F), ky );
3838 cv::Ptr<cv::FilterEngine> cv::createGaussianFilter( int type, Size ksize,
3839 double sigma1, double sigma2,
3843 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
3845 return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
3852 static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize, int ddepth,
3853 InputArray _kernelX, InputArray _kernelY, int borderType)
3855 const ocl::Device & dev = ocl::Device::getDefault();
3856 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
3858 if ( !(dev.isIntel() && (type == CV_8UC1) &&
3859 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
3860 ((ksize.width == 5 && (_src.cols() % 4 == 0)) ||
3861 (ksize.width == 3 && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)))) )
3864 Mat kernelX = _kernelX.getMat().reshape(1, 1);
3865 if (kernelX.cols % 2 != 1)
3867 Mat kernelY = _kernelY.getMat().reshape(1, 1);
3868 if (kernelY.cols % 2 != 1)
3874 Size size = _src.size();
3875 size_t globalsize[2] = { 0, 0 };
3876 size_t localsize[2] = { 0, 0 };
3878 if (ksize.width == 3)
3880 globalsize[0] = size.width / 16;
3881 globalsize[1] = size.height / 2;
3883 else if (ksize.width == 5)
3885 globalsize[0] = size.width / 4;
3886 globalsize[1] = size.height / 1;
3889 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
3890 char build_opts[1024];
3891 sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
3892 ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
3893 ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
3897 if (ksize.width == 3)
3898 kernel.create("gaussianBlur3x3_8UC1_cols16_rows2", cv::ocl::imgproc::gaussianBlur3x3_oclsrc, build_opts);
3899 else if (ksize.width == 5)
3900 kernel.create("gaussianBlur5x5_8UC1_cols4", cv::ocl::imgproc::gaussianBlur5x5_oclsrc, build_opts);
3905 UMat src = _src.getUMat();
3906 _dst.create(size, CV_MAKETYPE(ddepth, cn));
3907 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
3909 UMat dst = _dst.getUMat();
3911 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
3912 idxArg = kernel.set(idxArg, (int)src.step);
3913 idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
3914 idxArg = kernel.set(idxArg, (int)dst.step);
3915 idxArg = kernel.set(idxArg, (int)dst.rows);
3916 idxArg = kernel.set(idxArg, (int)dst.cols);
3918 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
3926 template <> inline bool skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(int w, int h) { return w*h < 320 * 240; }
3928 static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
3929 double sigma1, double sigma2, int borderType)
3933 // automatic detection of kernel size from sigma
3934 if (ksize.width <= 0 && sigma1 > 0)
3935 ksize.width = cvRound(sigma1*6 + 1) | 1;
3936 if (ksize.height <= 0 && sigma2 > 0)
3937 ksize.height = cvRound(sigma2*6 + 1) | 1;
3939 if (_src.type() != CV_8UC1 ||
3940 _src.cols() < 3 || _src.rows() < 3 ||
3941 ksize.width != 3 || ksize.height != 3)
3944 sigma1 = std::max(sigma1, 0.);
3945 sigma2 = std::max(sigma2, 0.);
3947 if (!(sigma1 == 0.0 || (sigma1 - 0.8) < DBL_EPSILON) || !(sigma2 == 0.0 || (sigma2 - 0.8) < DBL_EPSILON) ||
3948 ovx::skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(_src.cols(), _src.rows()))
3951 Mat src = _src.getMat();
3952 Mat dst = _dst.getMat();
3954 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
3955 return false; //Process isolated borders only
3957 switch (borderType & ~BORDER_ISOLATED)
3959 case BORDER_CONSTANT:
3960 border = VX_BORDER_CONSTANT;
3962 case BORDER_REPLICATE:
3963 border = VX_BORDER_REPLICATE;
3971 ivx::Context ctx = ovx::getOpenVXContext();
3974 if (dst.data != src.data)
3980 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3981 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
3982 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3983 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
3985 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
3986 //since OpenVX standard says nothing about thread-safety for now
3987 ivx::border_t prevBorder = ctx.immediateBorder();
3988 ctx.setImmediateBorder(border, (vx_uint8)(0));
3989 ivx::IVX_CHECK_STATUS(vxuGaussian3x3(ctx, ia, ib));
3990 ctx.setImmediateBorder(prevBorder);
3992 catch (ivx::RuntimeError & e)
3994 VX_DbgThrow(e.what());
3996 catch (ivx::WrapperError & e)
3998 VX_DbgThrow(e.what());
4006 // IW 2017u2 has bug which doesn't allow use of partial inMem with tiling
4007 #if IPP_DISABLE_GAUSSIANBLUR_PARALLEL
4008 #define IPP_GAUSSIANBLUR_PARALLEL 0
4010 #define IPP_GAUSSIANBLUR_PARALLEL 1
4015 class ipp_gaussianBlurParallel: public ParallelLoopBody
4018 ipp_gaussianBlurParallel(::ipp::IwiImage &src, ::ipp::IwiImage &dst, int kernelSize, float sigma, ::ipp::IwiBorderType &border, bool *pOk):
4019 m_src(src), m_dst(dst), m_kernelSize(kernelSize), m_sigma(sigma), m_border(border), m_pOk(pOk) {
4022 ~ipp_gaussianBlurParallel()
4026 virtual void operator() (const Range& range) const CV_OVERRIDE
4028 CV_INSTRUMENT_REGION_IPP()
4035 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, m_dst.m_size.width, range.end - range.start);
4036 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, m_src, m_dst, m_kernelSize, m_sigma, ::ipp::IwDefault(), m_border, tile);
4038 catch(::ipp::IwException e)
4045 ::ipp::IwiImage &m_src;
4046 ::ipp::IwiImage &m_dst;
4050 ::ipp::IwiBorderType &m_border;
4052 volatile bool *m_pOk;
4053 const ipp_gaussianBlurParallel& operator= (const ipp_gaussianBlurParallel&);
4058 static bool ipp_GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
4059 double sigma1, double sigma2, int borderType )
4062 CV_INSTRUMENT_REGION_IPP()
4064 #if IPP_VERSION_X100 < 201800 && ((defined _MSC_VER && defined _M_IX86) || (defined __GNUC__ && defined __i386__))
4065 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4066 return false; // bug on ia32
4068 if(sigma1 != sigma2)
4071 if(sigma1 < FLT_EPSILON)
4074 if(ksize.width != ksize.height)
4077 // Acquire data and begin processing
4080 Mat src = _src.getMat();
4081 Mat dst = _dst.getMat();
4082 ::ipp::IwiImage iwSrc = ippiGetImage(src);
4083 ::ipp::IwiImage iwDst = ippiGetImage(dst);
4084 ::ipp::IwiBorderSize borderSize = ::ipp::iwiSizeToBorderSize(ippiGetSize(ksize));
4085 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
4089 const int threads = ippiSuggestThreadsNum(iwDst, 2);
4090 if(IPP_GAUSSIANBLUR_PARALLEL && threads > 1) {
4092 ipp_gaussianBlurParallel invoker(iwSrc, iwDst, ksize.width, (float) sigma1, ippBorder, &ok);
4096 const Range range(0, (int) iwDst.m_size.height);
4097 parallel_for_(range, invoker, threads*4);
4102 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, iwSrc, iwDst, ksize.width, sigma1, ::ipp::IwDefault(), ippBorder);
4105 catch (::ipp::IwException ex)
4113 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4120 void cv::GaussianBlur( InputArray _src, OutputArray _dst, Size ksize,
4121 double sigma1, double sigma2,
4124 CV_INSTRUMENT_REGION()
4126 int type = _src.type();
4127 Size size = _src.size();
4128 _dst.create( size, type );
4130 if( (borderType & ~BORDER_ISOLATED) != BORDER_CONSTANT &&
4131 ((borderType & BORDER_ISOLATED) != 0 || !_src.getMat().isSubmatrix()) )
4133 if( size.height == 1 )
4135 if( size.width == 1 )
4139 if( ksize.width == 1 && ksize.height == 1 )
4145 bool useOpenCL = (ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
4146 ((ksize.width == 3 && ksize.height == 3) ||
4147 (ksize.width == 5 && ksize.height == 5)) &&
4148 _src.rows() > ksize.height && _src.cols() > ksize.width);
4151 int sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4153 if(sdepth == CV_8U && ((borderType & BORDER_ISOLATED) || !_src.getMat().isSubmatrix()))
4155 std::vector<ufixedpoint16> fkx, fky;
4156 createGaussianKernels(fkx, fky, type, ksize, sigma1, sigma2);
4157 Mat src = _src.getMat();
4158 Mat dst = _dst.getMat();
4159 if (src.data == dst.data)
4161 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);
4162 parallel_for_(Range(0, dst.rows), invoker, std::max(1, std::min(getNumThreads(), getNumberOfCPUs())));
4168 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
4170 CV_OCL_RUN(useOpenCL, ocl_GaussianBlur_8UC1(_src, _dst, ksize, CV_MAT_DEPTH(type), kx, ky, borderType));
4172 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
4173 ocl_sepFilter2D(_src, _dst, sdepth, kx, ky, Point(-1, -1), 0, borderType))
4175 Mat src = _src.getMat();
4176 Mat dst = _dst.getMat();
4179 Size wsz(src.cols, src.rows);
4180 if(!(borderType & BORDER_ISOLATED))
4181 src.locateROI( wsz, ofs );
4183 CALL_HAL(gaussianBlur, cv_hal_gaussianBlur, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, cn,
4184 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
4185 sigma1, sigma2, borderType&~BORDER_ISOLATED);
4188 openvx_gaussianBlur(src, dst, ksize, sigma1, sigma2, borderType))
4190 CV_IPP_RUN_FAST(ipp_GaussianBlur(src, dst, ksize, sigma1, sigma2, borderType));
4192 sepFilter2D(src, dst, sdepth, kx, ky, Point(-1, -1), 0, borderType);
4195 /****************************************************************************************\
4197 \****************************************************************************************/
4204 * This structure represents a two-tier histogram. The first tier (known as the
4205 * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
4206 * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
4207 * coarse bucket designated by the 4 MSBs of the fine bucket value.
4209 * The structure is aligned on 16 bits, which is a prerequisite for SIMD
4210 * instructions. Each bucket is 16 bit wide, which means that extra care must be
4211 * taken to prevent overflow.
4222 static inline void histogram_add_simd( const HT x[16], HT y[16] )
4224 v_store(y, v_load(x) + v_load(y));
4225 v_store(y + 8, v_load(x + 8) + v_load(y + 8));
4228 static inline void histogram_sub_simd( const HT x[16], HT y[16] )
4230 v_store(y, v_load(y) - v_load(x));
4231 v_store(y + 8, v_load(y + 8) - v_load(x + 8));
4237 static inline void histogram_add( const HT x[16], HT y[16] )
4240 for( i = 0; i < 16; ++i )
4241 y[i] = (HT)(y[i] + x[i]);
4244 static inline void histogram_sub( const HT x[16], HT y[16] )
4247 for( i = 0; i < 16; ++i )
4248 y[i] = (HT)(y[i] - x[i]);
4251 static inline void histogram_muladd( int a, const HT x[16],
4254 for( int i = 0; i < 16; ++i )
4255 y[i] = (HT)(y[i] + a * x[i]);
4259 medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
4262 * HOP is short for Histogram OPeration. This macro makes an operation \a op on
4263 * histogram \a h for pixel value \a x. It takes care of handling both levels.
4265 #define HOP(h,x,op) \
4266 h.coarse[x>>4] op, \
4267 *((HT*)h.fine + x) op
4269 #define COP(c,j,x,op) \
4270 h_coarse[ 16*(n*c+j) + (x>>4) ] op, \
4271 h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op
4273 int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2;
4274 CV_Assert(cn > 0 && cn <= 4);
4275 size_t sstep = _src.step, dstep = _dst.step;
4276 Histogram CV_DECL_ALIGNED(16) H[4];
4277 HT CV_DECL_ALIGNED(16) luc[4][16];
4279 int STRIPE_SIZE = std::min( _dst.cols, 512/cn );
4281 std::vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4282 std::vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4283 HT* h_coarse = alignPtr(&_h_coarse[0], 16);
4284 HT* h_fine = alignPtr(&_h_fine[0], 16);
4286 volatile bool useSIMD = hasSIMD128();
4289 for( int x = 0; x < _dst.cols; x += STRIPE_SIZE )
4291 int i, j, k, c, n = std::min(_dst.cols - x, STRIPE_SIZE) + r*2;
4292 const uchar* src = _src.ptr() + x*cn;
4293 uchar* dst = _dst.ptr() + (x - r)*cn;
4295 memset( h_coarse, 0, 16*n*cn*sizeof(h_coarse[0]) );
4296 memset( h_fine, 0, 16*16*n*cn*sizeof(h_fine[0]) );
4298 // First row initialization
4299 for( c = 0; c < cn; c++ )
4301 for( j = 0; j < n; j++ )
4302 COP( c, j, src[cn*j+c], += (cv::HT)(r+2) );
4304 for( i = 1; i < r; i++ )
4306 const uchar* p = src + sstep*std::min(i, m-1);
4307 for ( j = 0; j < n; j++ )
4308 COP( c, j, p[cn*j+c], ++ );
4312 for( i = 0; i < m; i++ )
4314 const uchar* p0 = src + sstep * std::max( 0, i-r-1 );
4315 const uchar* p1 = src + sstep * std::min( m-1, i+r );
4317 memset( H, 0, cn*sizeof(H[0]) );
4318 memset( luc, 0, cn*sizeof(luc[0]) );
4319 for( c = 0; c < cn; c++ )
4321 // Update column histograms for the entire row.
4322 for( j = 0; j < n; j++ )
4324 COP( c, j, p0[j*cn + c], -- );
4325 COP( c, j, p1[j*cn + c], ++ );
4328 // First column initialization
4329 for( k = 0; k < 16; ++k )
4330 histogram_muladd( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].fine[k][0] );
4335 for( j = 0; j < 2*r; ++j )
4336 histogram_add_simd( &h_coarse[16*(n*c+j)], H[c].coarse );
4338 for( j = r; j < n-r; j++ )
4340 int t = 2*r*r + 2*r, b, sum = 0;
4343 histogram_add_simd( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4345 // Find median at coarse level
4346 for ( k = 0; k < 16 ; ++k )
4348 sum += H[c].coarse[k];
4351 sum -= H[c].coarse[k];
4355 CV_Assert( k < 16 );
4357 /* Update corresponding histogram segment */
4358 if ( luc[c][k] <= j-r )
4360 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4361 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4362 histogram_add_simd( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4364 if ( luc[c][k] < j+r+1 )
4366 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4367 luc[c][k] = (HT)(j+r+1);
4372 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4374 histogram_sub_simd( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4375 histogram_add_simd( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4379 histogram_sub_simd( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4381 /* Find median in segment */
4382 segment = H[c].fine[k];
4383 for ( b = 0; b < 16 ; b++ )
4388 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4392 CV_Assert( b < 16 );
4398 for( j = 0; j < 2*r; ++j )
4399 histogram_add( &h_coarse[16*(n*c+j)], H[c].coarse );
4401 for( j = r; j < n-r; j++ )
4403 int t = 2*r*r + 2*r, b, sum = 0;
4406 histogram_add( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4408 // Find median at coarse level
4409 for ( k = 0; k < 16 ; ++k )
4411 sum += H[c].coarse[k];
4414 sum -= H[c].coarse[k];
4418 CV_Assert( k < 16 );
4420 /* Update corresponding histogram segment */
4421 if ( luc[c][k] <= j-r )
4423 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4424 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4425 histogram_add( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4427 if ( luc[c][k] < j+r+1 )
4429 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4430 luc[c][k] = (HT)(j+r+1);
4435 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4437 histogram_sub( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4438 histogram_add( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4442 histogram_sub( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4444 /* Find median in segment */
4445 segment = H[c].fine[k];
4446 for ( b = 0; b < 16 ; b++ )
4451 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4455 CV_Assert( b < 16 );
4467 medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m )
4474 Size size = _dst.size();
4475 const uchar* src = _src.ptr();
4476 uchar* dst = _dst.ptr();
4477 int src_step = (int)_src.step, dst_step = (int)_dst.step;
4478 int cn = _src.channels();
4479 const uchar* src_max = src + size.height*src_step;
4480 CV_Assert(cn > 0 && cn <= 4);
4482 #define UPDATE_ACC01( pix, cn, op ) \
4486 zone0[cn][p >> 4] op; \
4489 //CV_Assert( size.height >= nx && size.width >= nx );
4490 for( x = 0; x < size.width; x++, src += cn, dst += cn )
4492 uchar* dst_cur = dst;
4493 const uchar* src_top = src;
4494 const uchar* src_bottom = src;
4496 int src_step1 = src_step, dst_step1 = dst_step;
4500 src_bottom = src_top += src_step*(size.height-1);
4501 dst_cur += dst_step*(size.height-1);
4502 src_step1 = -src_step1;
4503 dst_step1 = -dst_step1;
4507 memset( zone0, 0, sizeof(zone0[0])*cn );
4508 memset( zone1, 0, sizeof(zone1[0])*cn );
4510 for( y = 0; y <= m/2; y++ )
4512 for( c = 0; c < cn; c++ )
4516 for( k = 0; k < m*cn; k += cn )
4517 UPDATE_ACC01( src_bottom[k+c], c, ++ );
4521 for( k = 0; k < m*cn; k += cn )
4522 UPDATE_ACC01( src_bottom[k+c], c, += m/2+1 );
4526 if( (src_step1 > 0 && y < size.height-1) ||
4527 (src_step1 < 0 && size.height-y-1 > 0) )
4528 src_bottom += src_step1;
4531 for( y = 0; y < size.height; y++, dst_cur += dst_step1 )
4534 for( c = 0; c < cn; c++ )
4539 int t = s + zone0[c][k];
4550 dst_cur[c] = (uchar)k;
4553 if( y+1 == size.height )
4558 for( k = 0; k < m; k++ )
4561 int q = src_bottom[k];
4570 for( k = 0; k < m*3; k += 3 )
4572 UPDATE_ACC01( src_top[k], 0, -- );
4573 UPDATE_ACC01( src_top[k+1], 1, -- );
4574 UPDATE_ACC01( src_top[k+2], 2, -- );
4576 UPDATE_ACC01( src_bottom[k], 0, ++ );
4577 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4578 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4584 for( k = 0; k < m*4; k += 4 )
4586 UPDATE_ACC01( src_top[k], 0, -- );
4587 UPDATE_ACC01( src_top[k+1], 1, -- );
4588 UPDATE_ACC01( src_top[k+2], 2, -- );
4589 UPDATE_ACC01( src_top[k+3], 3, -- );
4591 UPDATE_ACC01( src_bottom[k], 0, ++ );
4592 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4593 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4594 UPDATE_ACC01( src_bottom[k+3], 3, ++ );
4598 if( (src_step1 > 0 && src_bottom + src_step1 < src_max) ||
4599 (src_step1 < 0 && src_bottom + src_step1 >= src) )
4600 src_bottom += src_step1;
4603 src_top += src_step1;
4613 typedef uchar value_type;
4614 typedef int arg_type;
4616 arg_type load(const uchar* ptr) { return *ptr; }
4617 void store(uchar* ptr, arg_type val) { *ptr = (uchar)val; }
4618 void operator()(arg_type& a, arg_type& b) const
4620 int t = CV_FAST_CAST_8U(a - b);
4627 typedef ushort value_type;
4628 typedef int arg_type;
4630 arg_type load(const ushort* ptr) { return *ptr; }
4631 void store(ushort* ptr, arg_type val) { *ptr = (ushort)val; }
4632 void operator()(arg_type& a, arg_type& b) const
4642 typedef short value_type;
4643 typedef int arg_type;
4645 arg_type load(const short* ptr) { return *ptr; }
4646 void store(short* ptr, arg_type val) { *ptr = (short)val; }
4647 void operator()(arg_type& a, arg_type& b) const
4657 typedef float value_type;
4658 typedef float arg_type;
4660 arg_type load(const float* ptr) { return *ptr; }
4661 void store(float* ptr, arg_type val) { *ptr = val; }
4662 void operator()(arg_type& a, arg_type& b) const
4674 typedef uchar value_type;
4675 typedef v_uint8x16 arg_type;
4677 arg_type load(const uchar* ptr) { return v_load(ptr); }
4678 void store(uchar* ptr, const arg_type &val) { v_store(ptr, val); }
4679 void operator()(arg_type& a, arg_type& b) const
4690 typedef ushort value_type;
4691 typedef v_uint16x8 arg_type;
4693 arg_type load(const ushort* ptr) { return v_load(ptr); }
4694 void store(ushort* ptr, const arg_type &val) { v_store(ptr, val); }
4695 void operator()(arg_type& a, arg_type& b) const
4706 typedef short value_type;
4707 typedef v_int16x8 arg_type;
4709 arg_type load(const short* ptr) { return v_load(ptr); }
4710 void store(short* ptr, const arg_type &val) { v_store(ptr, val); }
4711 void operator()(arg_type& a, arg_type& b) const
4722 typedef float value_type;
4723 typedef v_float32x4 arg_type;
4725 arg_type load(const float* ptr) { return v_load(ptr); }
4726 void store(float* ptr, const arg_type &val) { v_store(ptr, val); }
4727 void operator()(arg_type& a, arg_type& b) const
4737 typedef MinMax8u MinMaxVec8u;
4738 typedef MinMax16u MinMaxVec16u;
4739 typedef MinMax16s MinMaxVec16s;
4740 typedef MinMax32f MinMaxVec32f;
4744 template<class Op, class VecOp>
4746 medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
4748 typedef typename Op::value_type T;
4749 typedef typename Op::arg_type WT;
4750 typedef typename VecOp::arg_type VT;
4752 const T* src = _src.ptr<T>();
4753 T* dst = _dst.ptr<T>();
4754 int sstep = (int)(_src.step/sizeof(T));
4755 int dstep = (int)(_dst.step/sizeof(T));
4756 Size size = _dst.size();
4757 int i, j, k, cn = _src.channels();
4760 volatile bool useSIMD = hasSIMD128();
4764 if( size.width == 1 || size.height == 1 )
4766 int len = size.width + size.height - 1;
4767 int sdelta = size.height == 1 ? cn : sstep;
4768 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4769 int ddelta = size.height == 1 ? cn : dstep;
4771 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4772 for( j = 0; j < cn; j++, src++ )
4774 WT p0 = src[i > 0 ? -sdelta : 0];
4776 WT p2 = src[i < len - 1 ? sdelta : 0];
4778 op(p0, p1); op(p1, p2); op(p0, p1);
4785 for( i = 0; i < size.height; i++, dst += dstep )
4787 const T* row0 = src + std::max(i - 1, 0)*sstep;
4788 const T* row1 = src + i*sstep;
4789 const T* row2 = src + std::min(i + 1, size.height-1)*sstep;
4790 int limit = useSIMD ? cn : size.width;
4794 for( ; j < limit; j++ )
4796 int j0 = j >= cn ? j - cn : j;
4797 int j2 = j < size.width - cn ? j + cn : j;
4798 WT p0 = row0[j0], p1 = row0[j], p2 = row0[j2];
4799 WT p3 = row1[j0], p4 = row1[j], p5 = row1[j2];
4800 WT p6 = row2[j0], p7 = row2[j], p8 = row2[j2];
4802 op(p1, p2); op(p4, p5); op(p7, p8); op(p0, p1);
4803 op(p3, p4); op(p6, p7); op(p1, p2); op(p4, p5);
4804 op(p7, p8); op(p0, p3); op(p5, p8); op(p4, p7);
4805 op(p3, p6); op(p1, p4); op(p2, p5); op(p4, p7);
4806 op(p4, p2); op(p6, p4); op(p4, p2);
4810 if( limit == size.width )
4813 for( ; j <= size.width - VecOp::SIZE - cn; j += VecOp::SIZE )
4815 VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
4816 VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
4817 VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);
4819 vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
4820 vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
4821 vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
4822 vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
4823 vop(p4, p2); vop(p6, p4); vop(p4, p2);
4824 vop.store(dst+j, p4);
4833 if( size.width == 1 || size.height == 1 )
4835 int len = size.width + size.height - 1;
4836 int sdelta = size.height == 1 ? cn : sstep;
4837 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4838 int ddelta = size.height == 1 ? cn : dstep;
4840 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4841 for( j = 0; j < cn; j++, src++ )
4843 int i1 = i > 0 ? -sdelta : 0;
4844 int i0 = i > 1 ? -sdelta*2 : i1;
4845 int i3 = i < len-1 ? sdelta : 0;
4846 int i4 = i < len-2 ? sdelta*2 : i3;
4847 WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];
4849 op(p0, p1); op(p3, p4); op(p2, p3); op(p3, p4); op(p0, p2);
4850 op(p2, p4); op(p1, p3); op(p1, p2);
4857 for( i = 0; i < size.height; i++, dst += dstep )
4860 row[0] = src + std::max(i - 2, 0)*sstep;
4861 row[1] = src + std::max(i - 1, 0)*sstep;
4862 row[2] = src + i*sstep;
4863 row[3] = src + std::min(i + 1, size.height-1)*sstep;
4864 row[4] = src + std::min(i + 2, size.height-1)*sstep;
4865 int limit = useSIMD ? cn*2 : size.width;
4869 for( ; j < limit; j++ )
4872 int j1 = j >= cn ? j - cn : j;
4873 int j0 = j >= cn*2 ? j - cn*2 : j1;
4874 int j3 = j < size.width - cn ? j + cn : j;
4875 int j4 = j < size.width - cn*2 ? j + cn*2 : j3;
4876 for( k = 0; k < 5; k++ )
4878 const T* rowk = row[k];
4879 p[k*5] = rowk[j0]; p[k*5+1] = rowk[j1];
4880 p[k*5+2] = rowk[j]; p[k*5+3] = rowk[j3];
4881 p[k*5+4] = rowk[j4];
4884 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]);
4885 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]);
4886 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]);
4887 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]);
4888 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]);
4889 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]);
4890 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]);
4891 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]);
4892 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]);
4893 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]);
4894 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]);
4895 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]);
4896 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]);
4897 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]);
4898 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]);
4899 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]);
4900 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]);
4901 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]);
4902 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]);
4903 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]);
4904 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]);
4905 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]);
4906 op(p[7], p[11]); op(p[11], p[13]); op(p[11], p[12]);
4910 if( limit == size.width )
4913 for( ; j <= size.width - VecOp::SIZE - cn*2; j += VecOp::SIZE )
4916 for( k = 0; k < 5; k++ )
4918 const T* rowk = row[k];
4919 p[k*5] = vop.load(rowk+j-cn*2); p[k*5+1] = vop.load(rowk+j-cn);
4920 p[k*5+2] = vop.load(rowk+j); p[k*5+3] = vop.load(rowk+j+cn);
4921 p[k*5+4] = vop.load(rowk+j+cn*2);
4924 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]);
4925 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]);
4926 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]);
4927 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]);
4928 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]);
4929 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]);
4930 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]);
4931 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]);
4932 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]);
4933 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]);
4934 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]);
4935 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]);
4936 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]);
4937 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]);
4938 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]);
4939 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]);
4940 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]);
4941 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]);
4942 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]);
4943 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]);
4944 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]);
4945 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]);
4946 vop(p[7], p[11]); vop(p[11], p[13]); vop(p[11], p[12]);
4947 vop.store(dst+j, p[12]);
4958 static bool ocl_medianFilter(InputArray _src, OutputArray _dst, int m)
4960 size_t localsize[2] = { 16, 16 };
4961 size_t globalsize[2];
4962 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4964 if ( !((depth == CV_8U || depth == CV_16U || depth == CV_16S || depth == CV_32F) && cn <= 4 && (m == 3 || m == 5)) )
4967 Size imgSize = _src.size();
4968 bool useOptimized = (1 == cn) &&
4969 (size_t)imgSize.width >= localsize[0] * 8 &&
4970 (size_t)imgSize.height >= localsize[1] * 8 &&
4971 imgSize.width % 4 == 0 &&
4972 imgSize.height % 4 == 0 &&
4973 (ocl::Device::getDefault().isIntel());
4975 cv::String kname = format( useOptimized ? "medianFilter%d_u" : "medianFilter%d", m) ;
4976 cv::String kdefs = useOptimized ?
4977 format("-D T=%s -D T1=%s -D T4=%s%d -D cn=%d -D USE_4OPT", ocl::typeToStr(type),
4978 ocl::typeToStr(depth), ocl::typeToStr(depth), cn*4, cn)
4980 format("-D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn) ;
4982 ocl::Kernel k(kname.c_str(), ocl::imgproc::medianFilter_oclsrc, kdefs.c_str() );
4987 UMat src = _src.getUMat();
4988 _dst.create(src.size(), type);
4989 UMat dst = _dst.getUMat();
4991 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst));
4995 globalsize[0] = DIVUP(src.cols / 4, localsize[0]) * localsize[0];
4996 globalsize[1] = DIVUP(src.rows / 4, localsize[1]) * localsize[1];
5000 globalsize[0] = (src.cols + localsize[0] + 2) / localsize[0] * localsize[0];
5001 globalsize[1] = (src.rows + localsize[1] - 1) / localsize[1] * localsize[1];
5004 return k.run(2, globalsize, localsize, false);
5015 template <> inline bool skipSmallImages<VX_KERNEL_MEDIAN_3x3>(int w, int h) { return w*h < 1280 * 720; }
5017 static bool openvx_medianFilter(InputArray _src, OutputArray _dst, int ksize)
5019 if (_src.type() != CV_8UC1 || _dst.type() != CV_8U
5020 #ifndef VX_VERSION_1_1
5026 Mat src = _src.getMat();
5027 Mat dst = _dst.getMat();
5030 #ifdef VX_VERSION_1_1
5031 ksize != 3 ? ovx::skipSmallImages<VX_KERNEL_NON_LINEAR_FILTER>(src.cols, src.rows) :
5033 ovx::skipSmallImages<VX_KERNEL_MEDIAN_3x3>(src.cols, src.rows)
5039 ivx::Context ctx = ovx::getOpenVXContext();
5040 #ifdef VX_VERSION_1_1
5041 if ((vx_size)ksize > ctx.nonlinearMaxDimension())
5046 if (dst.data != src.data)
5052 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5053 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
5054 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5055 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
5057 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
5058 //since OpenVX standard says nothing about thread-safety for now
5059 ivx::border_t prevBorder = ctx.immediateBorder();
5060 ctx.setImmediateBorder(VX_BORDER_REPLICATE);
5061 #ifdef VX_VERSION_1_1
5065 ivx::IVX_CHECK_STATUS(vxuMedian3x3(ctx, ia, ib));
5067 #ifdef VX_VERSION_1_1
5072 mtx = ivx::Matrix::createFromPattern(ctx, VX_PATTERN_BOX, ksize, ksize);
5075 vx_size supportedSize;
5076 ivx::IVX_CHECK_STATUS(vxQueryContext(ctx, VX_CONTEXT_NONLINEAR_MAX_DIMENSION, &supportedSize, sizeof(supportedSize)));
5077 if ((vx_size)ksize > supportedSize)
5079 ctx.setImmediateBorder(prevBorder);
5082 Mat mask(ksize, ksize, CV_8UC1, Scalar(255));
5083 mtx = ivx::Matrix::create(ctx, VX_TYPE_UINT8, ksize, ksize);
5086 ivx::IVX_CHECK_STATUS(vxuNonLinearFilter(ctx, VX_NONLINEAR_FILTER_MEDIAN, ia, mtx, ib));
5089 ctx.setImmediateBorder(prevBorder);
5091 catch (ivx::RuntimeError & e)
5093 VX_DbgThrow(e.what());
5095 catch (ivx::WrapperError & e)
5097 VX_DbgThrow(e.what());
5108 static bool ipp_medianFilter(Mat &src0, Mat &dst, int ksize)
5110 CV_INSTRUMENT_REGION_IPP()
5112 #if IPP_VERSION_X100 < 201801
5113 // Degradations for big kernel
5120 IppiSize dstRoiSize = ippiSize(dst.cols, dst.rows), maskSize = ippiSize(ksize, ksize);
5121 IppDataType ippType = ippiGetDataType(src0.type());
5122 int channels = src0.channels();
5123 IppAutoBuffer<Ipp8u> buffer;
5125 if(src0.isSubmatrix())
5129 if(dst.data != src0.data)
5134 if(ippiFilterMedianBorderGetBufferSize(dstRoiSize, maskSize, ippType, channels, &bufSize) < 0)
5137 buffer.allocate(bufSize);
5143 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;
5144 else if(channels == 3)
5145 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;
5146 else if(channels == 4)
5147 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;
5152 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;
5153 else if(channels == 3)
5154 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;
5155 else if(channels == 4)
5156 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;
5161 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;
5162 else if(channels == 3)
5163 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;
5164 else if(channels == 4)
5165 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;
5170 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;
5181 void cv::medianBlur( InputArray _src0, OutputArray _dst, int ksize )
5183 CV_INSTRUMENT_REGION()
5185 CV_Assert( (ksize % 2 == 1) && (_src0.dims() <= 2 ));
5187 if( ksize <= 1 || _src0.empty() )
5193 CV_OCL_RUN(_dst.isUMat(),
5194 ocl_medianFilter(_src0,_dst, ksize))
5196 Mat src0 = _src0.getMat();
5197 _dst.create( src0.size(), src0.type() );
5198 Mat dst = _dst.getMat();
5200 CALL_HAL(medianBlur, cv_hal_medianBlur, src0.data, src0.step, dst.data, dst.step, src0.cols, src0.rows, src0.depth(),
5201 src0.channels(), ksize);
5204 openvx_medianFilter(_src0, _dst, ksize))
5206 CV_IPP_RUN_FAST(ipp_medianFilter(src0, dst, ksize));
5208 bool useSortNet = ksize == 3 || (ksize == 5
5210 && ( src0.depth() > CV_8U || src0.channels() == 2 || src0.channels() > 4 )
5217 if( dst.data != src0.data )
5222 if( src.depth() == CV_8U )
5223 medianBlur_SortNet<MinMax8u, MinMaxVec8u>( src, dst, ksize );
5224 else if( src.depth() == CV_16U )
5225 medianBlur_SortNet<MinMax16u, MinMaxVec16u>( src, dst, ksize );
5226 else if( src.depth() == CV_16S )
5227 medianBlur_SortNet<MinMax16s, MinMaxVec16s>( src, dst, ksize );
5228 else if( src.depth() == CV_32F )
5229 medianBlur_SortNet<MinMax32f, MinMaxVec32f>( src, dst, ksize );
5231 CV_Error(CV_StsUnsupportedFormat, "");
5237 cv::copyMakeBorder( src0, src, 0, 0, ksize/2, ksize/2, BORDER_REPLICATE|BORDER_ISOLATED);
5239 int cn = src0.channels();
5240 CV_Assert( src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4) );
5242 double img_size_mp = (double)(src0.total())/(1 << 20);
5243 if( ksize <= 3 + (img_size_mp < 1 ? 12 : img_size_mp < 4 ? 6 : 2)*
5244 (CV_SIMD128 && hasSIMD128() ? 1 : 3))
5245 medianBlur_8u_Om( src, dst, ksize );
5247 medianBlur_8u_O1( src, dst, ksize );
5251 /****************************************************************************************\
5253 \****************************************************************************************/
5258 class BilateralFilter_8u_Invoker :
5259 public ParallelLoopBody
5262 BilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, int _radius, int _maxk,
5263 int* _space_ofs, float *_space_weight, float *_color_weight) :
5264 temp(&_temp), dest(&_dest), radius(_radius),
5265 maxk(_maxk), space_ofs(_space_ofs), space_weight(_space_weight), color_weight(_color_weight)
5269 virtual void operator() (const Range& range) const CV_OVERRIDE
5271 int i, j, cn = dest->channels(), k;
5272 Size size = dest->size();
5274 int CV_DECL_ALIGNED(16) buf[4];
5275 bool haveSIMD128 = hasSIMD128();
5278 for( i = range.start; i < range.end; i++ )
5280 const uchar* sptr = temp->ptr(i+radius) + radius*cn;
5281 uchar* dptr = dest->ptr(i);
5285 for( j = 0; j < size.width; j++ )
5287 float sum = 0, wsum = 0;
5293 v_float32x4 _val0 = v_setall_f32(static_cast<float>(val0));
5294 v_float32x4 vsumw = v_setzero_f32();
5295 v_float32x4 vsumc = v_setzero_f32();
5297 for( ; k <= maxk - 4; k += 4 )
5299 v_float32x4 _valF = v_float32x4(sptr[j + space_ofs[k]],
5300 sptr[j + space_ofs[k + 1]],
5301 sptr[j + space_ofs[k + 2]],
5302 sptr[j + space_ofs[k + 3]]);
5303 v_float32x4 _val = v_abs(_valF - _val0);
5304 v_store(buf, v_round(_val));
5306 v_float32x4 _cw = v_float32x4(color_weight[buf[0]],
5307 color_weight[buf[1]],
5308 color_weight[buf[2]],
5309 color_weight[buf[3]]);
5310 v_float32x4 _sw = v_load(space_weight+k);
5311 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5312 // details: https://github.com/opencv/opencv/issues/11004
5314 vsumc += _cw * _sw * _valF;
5316 v_float32x4 _w = _cw * _sw;
5323 float *bufFloat = (float*)buf;
5324 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumc, vsumw, vsumc);
5325 v_store(bufFloat, sum4);
5327 wsum += bufFloat[0];
5330 for( ; k < maxk; k++ )
5332 int val = sptr[j + space_ofs[k]];
5333 float w = space_weight[k]*color_weight[std::abs(val - val0)];
5337 // overflow is not possible here => there is no need to use cv::saturate_cast
5338 dptr[j] = (uchar)cvRound(sum/wsum);
5344 for( j = 0; j < size.width*3; j += 3 )
5346 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5347 int b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5352 v_float32x4 vsumw = v_setzero_f32();
5353 v_float32x4 vsumb = v_setzero_f32();
5354 v_float32x4 vsumg = v_setzero_f32();
5355 v_float32x4 vsumr = v_setzero_f32();
5356 const v_float32x4 _b0 = v_setall_f32(static_cast<float>(b0));
5357 const v_float32x4 _g0 = v_setall_f32(static_cast<float>(g0));
5358 const v_float32x4 _r0 = v_setall_f32(static_cast<float>(r0));
5360 for( ; k <= maxk - 4; k += 4 )
5362 const uchar* const sptr_k0 = sptr + j + space_ofs[k];
5363 const uchar* const sptr_k1 = sptr + j + space_ofs[k+1];
5364 const uchar* const sptr_k2 = sptr + j + space_ofs[k+2];
5365 const uchar* const sptr_k3 = sptr + j + space_ofs[k+3];
5367 v_float32x4 __b = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k0)));
5368 v_float32x4 __g = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k1)));
5369 v_float32x4 __r = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k2)));
5370 v_float32x4 __z = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k3)));
5371 v_float32x4 _b, _g, _r, _z;
5373 v_transpose4x4(__b, __g, __r, __z, _b, _g, _r, _z);
5375 v_float32x4 bt = v_abs(_b -_b0);
5376 v_float32x4 gt = v_abs(_g -_g0);
5377 v_float32x4 rt = v_abs(_r -_r0);
5380 v_store(buf, v_round(bt));
5382 v_float32x4 _w = v_float32x4(color_weight[buf[0]],color_weight[buf[1]],
5383 color_weight[buf[2]],color_weight[buf[3]]);
5384 v_float32x4 _sw = v_load(space_weight+k);
5386 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5387 // details: https://github.com/opencv/opencv/issues/11004
5389 vsumb += _w * _sw * _b;
5390 vsumg += _w * _sw * _g;
5391 vsumr += _w * _sw * _r;
5404 float *bufFloat = (float*)buf;
5405 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumb, vsumg, vsumr);
5406 v_store(bufFloat, sum4);
5407 wsum += bufFloat[0];
5408 sum_b += bufFloat[1];
5409 sum_g += bufFloat[2];
5410 sum_r += bufFloat[3];
5414 for( ; k < maxk; k++ )
5416 const uchar* sptr_k = sptr + j + space_ofs[k];
5417 int b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5418 float w = space_weight[k]*color_weight[std::abs(b - b0) +
5419 std::abs(g - g0) + std::abs(r - r0)];
5420 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5424 b0 = cvRound(sum_b*wsum);
5425 g0 = cvRound(sum_g*wsum);
5426 r0 = cvRound(sum_r*wsum);
5427 dptr[j] = (uchar)b0; dptr[j+1] = (uchar)g0; dptr[j+2] = (uchar)r0;
5436 int radius, maxk, *space_ofs;
5437 float *space_weight, *color_weight;
5442 static bool ocl_bilateralFilter_8u(InputArray _src, OutputArray _dst, int d,
5443 double sigma_color, double sigma_space,
5447 if (ocl::Device::getDefault().isNVidia())
5451 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
5452 int i, j, maxk, radius;
5454 if (depth != CV_8U || cn > 4)
5457 if (sigma_color <= 0)
5459 if (sigma_space <= 0)
5462 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
5463 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
5466 radius = cvRound(sigma_space * 1.5);
5469 radius = MAX(radius, 1);
5472 UMat src = _src.getUMat(), dst = _dst.getUMat(), temp;
5476 copyMakeBorder(src, temp, radius, radius, radius, radius, borderType);
5477 std::vector<float> _space_weight(d * d);
5478 std::vector<int> _space_ofs(d * d);
5479 float * const space_weight = &_space_weight[0];
5480 int * const space_ofs = &_space_ofs[0];
5482 // initialize space-related bilateral filter coefficients
5483 for( i = -radius, maxk = 0; i <= radius; i++ )
5484 for( j = -radius; j <= radius; j++ )
5486 double r = std::sqrt((double)i * i + (double)j * j);
5489 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
5490 space_ofs[maxk++] = (int)(i * temp.step + j * cn);
5494 String cnstr = cn > 1 ? format("%d", cn) : "";
5495 String kernelName("bilateral");
5497 if ((ocl::Device::getDefault().isIntel()) &&
5498 (ocl::Device::getDefault().type() == ocl::Device::TYPE_GPU))
5501 if (dst.cols % 4 == 0 && cn == 1) // For single channel x4 sized images.
5503 kernelName = "bilateral_float4";
5507 ocl::Kernel k(kernelName.c_str(), ocl::imgproc::bilateral_oclsrc,
5508 format("-D radius=%d -D maxk=%d -D cn=%d -D int_t=%s -D uint_t=uint%s -D convert_int_t=%s"
5509 " -D uchar_t=%s -D float_t=%s -D convert_float_t=%s -D convert_uchar_t=%s -D gauss_color_coeff=(float)%f",
5510 radius, maxk, cn, ocl::typeToStr(CV_32SC(cn)), cnstr.c_str(),
5511 ocl::convertTypeStr(CV_8U, CV_32S, cn, cvt[0]),
5512 ocl::typeToStr(type), ocl::typeToStr(CV_32FC(cn)),
5513 ocl::convertTypeStr(CV_32S, CV_32F, cn, cvt[1]),
5514 ocl::convertTypeStr(CV_32F, CV_8U, cn, cvt[2]), gauss_color_coeff));
5518 Mat mspace_weight(1, d * d, CV_32FC1, space_weight);
5519 Mat mspace_ofs(1, d * d, CV_32SC1, space_ofs);
5520 UMat ucolor_weight, uspace_weight, uspace_ofs;
5522 mspace_weight.copyTo(uspace_weight);
5523 mspace_ofs.copyTo(uspace_ofs);
5525 k.args(ocl::KernelArg::ReadOnlyNoSize(temp), ocl::KernelArg::WriteOnly(dst),
5526 ocl::KernelArg::PtrReadOnly(uspace_weight),
5527 ocl::KernelArg::PtrReadOnly(uspace_ofs));
5529 size_t globalsize[2] = { (size_t)dst.cols / sizeDiv, (size_t)dst.rows };
5530 return k.run(2, globalsize, NULL, false);
5535 bilateralFilter_8u( const Mat& src, Mat& dst, int d,
5536 double sigma_color, double sigma_space,
5539 int cn = src.channels();
5540 int i, j, maxk, radius;
5541 Size size = src.size();
5543 CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data );
5545 if( sigma_color <= 0 )
5547 if( sigma_space <= 0 )
5550 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5551 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5554 radius = cvRound(sigma_space*1.5);
5557 radius = MAX(radius, 1);
5561 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5563 std::vector<float> _color_weight(cn*256);
5564 std::vector<float> _space_weight(d*d);
5565 std::vector<int> _space_ofs(d*d);
5566 float* color_weight = &_color_weight[0];
5567 float* space_weight = &_space_weight[0];
5568 int* space_ofs = &_space_ofs[0];
5570 // initialize color-related bilateral filter coefficients
5572 for( i = 0; i < 256*cn; i++ )
5573 color_weight[i] = (float)std::exp(i*i*gauss_color_coeff);
5575 // initialize space-related bilateral filter coefficients
5576 for( i = -radius, maxk = 0; i <= radius; i++ )
5580 for( ; j <= radius; j++ )
5582 double r = std::sqrt((double)i*i + (double)j*j);
5585 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5586 space_ofs[maxk++] = (int)(i*temp.step + j*cn);
5590 BilateralFilter_8u_Invoker body(dst, temp, radius, maxk, space_ofs, space_weight, color_weight);
5591 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5595 class BilateralFilter_32f_Invoker :
5596 public ParallelLoopBody
5600 BilateralFilter_32f_Invoker(int _cn, int _radius, int _maxk, int *_space_ofs,
5601 const Mat& _temp, Mat& _dest, float _scale_index, float *_space_weight, float *_expLUT) :
5602 cn(_cn), radius(_radius), maxk(_maxk), space_ofs(_space_ofs),
5603 temp(&_temp), dest(&_dest), scale_index(_scale_index), space_weight(_space_weight), expLUT(_expLUT)
5607 virtual void operator() (const Range& range) const CV_OVERRIDE
5610 Size size = dest->size();
5612 int CV_DECL_ALIGNED(16) idxBuf[4];
5613 bool haveSIMD128 = hasSIMD128();
5616 for( i = range.start; i < range.end; i++ )
5618 const float* sptr = temp->ptr<float>(i+radius) + radius*cn;
5619 float* dptr = dest->ptr<float>(i);
5623 for( j = 0; j < size.width; j++ )
5625 float sum = 0, wsum = 0;
5626 float val0 = sptr[j];
5631 v_float32x4 vecwsum = v_setzero_f32();
5632 v_float32x4 vecvsum = v_setzero_f32();
5633 const v_float32x4 _val0 = v_setall_f32(sptr[j]);
5634 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5636 for (; k <= maxk - 4; k += 4)
5638 v_float32x4 _sw = v_load(space_weight + k);
5639 v_float32x4 _val = v_float32x4(sptr[j + space_ofs[k]],
5640 sptr[j + space_ofs[k + 1]],
5641 sptr[j + space_ofs[k + 2]],
5642 sptr[j + space_ofs[k + 3]]);
5643 v_float32x4 _alpha = v_abs(_val - _val0) * _scale_index;
5645 v_int32x4 _idx = v_round(_alpha);
5646 v_store(idxBuf, _idx);
5647 _alpha -= v_cvt_f32(_idx);
5649 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5653 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5654 expLUT[idxBuf[1] + 1],
5655 expLUT[idxBuf[2] + 1],
5656 expLUT[idxBuf[3] + 1]);
5658 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5664 float *bufFloat = (float*)idxBuf;
5665 v_float32x4 sum4 = v_reduce_sum4(vecwsum, vecvsum, vecwsum, vecvsum);
5666 v_store(bufFloat, sum4);
5668 wsum += bufFloat[0];
5672 for( ; k < maxk; k++ )
5674 float val = sptr[j + space_ofs[k]];
5675 float alpha = (float)(std::abs(val - val0)*scale_index);
5676 int idx = cvFloor(alpha);
5678 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5682 dptr[j] = (float)(sum/wsum);
5687 CV_Assert( cn == 3 );
5688 for( j = 0; j < size.width*3; j += 3 )
5690 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5691 float b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5696 v_float32x4 sumw = v_setzero_f32();
5697 v_float32x4 sumb = v_setzero_f32();
5698 v_float32x4 sumg = v_setzero_f32();
5699 v_float32x4 sumr = v_setzero_f32();
5700 const v_float32x4 _b0 = v_setall_f32(b0);
5701 const v_float32x4 _g0 = v_setall_f32(g0);
5702 const v_float32x4 _r0 = v_setall_f32(r0);
5703 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5705 for( ; k <= maxk-4; k += 4 )
5707 v_float32x4 _sw = v_load(space_weight + k);
5709 const float* const sptr_k0 = sptr + j + space_ofs[k];
5710 const float* const sptr_k1 = sptr + j + space_ofs[k+1];
5711 const float* const sptr_k2 = sptr + j + space_ofs[k+2];
5712 const float* const sptr_k3 = sptr + j + space_ofs[k+3];
5714 v_float32x4 _v0 = v_load(sptr_k0);
5715 v_float32x4 _v1 = v_load(sptr_k1);
5716 v_float32x4 _v2 = v_load(sptr_k2);
5717 v_float32x4 _v3 = v_load(sptr_k3);
5718 v_float32x4 _b, _g, _r, _dummy;
5720 v_transpose4x4(_v0, _v1, _v2, _v3, _b, _g, _r, _dummy);
5722 v_float32x4 _bt = v_abs(_b - _b0);
5723 v_float32x4 _gt = v_abs(_g - _g0);
5724 v_float32x4 _rt = v_abs(_r - _r0);
5725 v_float32x4 _alpha = _scale_index * (_bt + _gt + _rt);
5727 v_int32x4 _idx = v_round(_alpha);
5728 v_store((int*)idxBuf, _idx);
5729 _alpha -= v_cvt_f32(_idx);
5731 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5735 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5736 expLUT[idxBuf[1] + 1],
5737 expLUT[idxBuf[2] + 1],
5738 expLUT[idxBuf[3] + 1]);
5740 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5750 v_float32x4 sum4 = v_reduce_sum4(sumw, sumb, sumg, sumr);
5751 float *bufFloat = (float*)idxBuf;
5752 v_store(bufFloat, sum4);
5753 wsum += bufFloat[0];
5754 sum_b += bufFloat[1];
5755 sum_g += bufFloat[2];
5756 sum_r += bufFloat[3];
5760 for(; k < maxk; k++ )
5762 const float* sptr_k = sptr + j + space_ofs[k];
5763 float b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5764 float alpha = (float)((std::abs(b - b0) +
5765 std::abs(g - g0) + std::abs(r - r0))*scale_index);
5766 int idx = cvFloor(alpha);
5768 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5769 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5776 dptr[j] = b0; dptr[j+1] = g0; dptr[j+2] = r0;
5783 int cn, radius, maxk, *space_ofs;
5786 float scale_index, *space_weight, *expLUT;
5791 bilateralFilter_32f( const Mat& src, Mat& dst, int d,
5792 double sigma_color, double sigma_space,
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();
5804 CV_Assert( (src.type() == CV_32FC1 || src.type() == CV_32FC3) && src.data != dst.data );
5806 if( sigma_color <= 0 )
5808 if( sigma_space <= 0 )
5811 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5812 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5815 radius = cvRound(sigma_space*1.5);
5818 radius = MAX(radius, 1);
5820 // compute the min/max range for the input image (even if multichannel)
5822 minMaxLoc( src.reshape(1), &minValSrc, &maxValSrc );
5823 if(std::abs(minValSrc - maxValSrc) < FLT_EPSILON)
5829 // temporary copy of the image with borders for easy processing
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];
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];
5847 scale_index = kExpNumBins/len;
5849 // initialize the exp LUT
5850 for( i = 0; i < kExpNumBins+2; i++ )
5852 if( lastExpVal > 0.f )
5854 double val = i / scale_index;
5855 expLUT[i] = (float)std::exp(val * val * gauss_color_coeff);
5856 lastExpVal = expLUT[i];
5862 // initialize space-related bilateral filter coefficients
5863 for( i = -radius, maxk = 0; i <= radius; i++ )
5864 for( j = -radius; j <= radius; j++ )
5866 double r = std::sqrt((double)i*i + (double)j*j);
5869 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5870 space_ofs[maxk++] = (int)(i*(temp.step/sizeof(float)) + j*cn);
5873 // parallel_for usage
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));
5880 #define IPP_BILATERAL_PARALLEL 1
5883 class ipp_bilateralFilterParallel: public ParallelLoopBody
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)
5892 valSquareSigma = _valSquareSigma;
5893 posSquareSigma = _posSquareSigma;
5894 borderType = _borderType;
5898 ~ipp_bilateralFilterParallel() {}
5900 virtual void operator() (const Range& range) const CV_OVERRIDE
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);
5910 catch(::ipp::IwException)
5917 ::ipp::IwiImage &src;
5918 ::ipp::IwiImage &dst;
5921 Ipp32f valSquareSigma;
5922 Ipp32f posSquareSigma;
5923 ::ipp::IwiBorderType borderType;
5926 const ipp_bilateralFilterParallel& operator= (const ipp_bilateralFilterParallel&);
5930 static bool ipp_bilateralFilter(Mat &src, Mat &dst, int d, double sigmaColor, double sigmaSpace, int borderType)
5933 CV_INSTRUMENT_REGION_IPP()
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);
5939 // Acquire data and begin processing
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));
5949 const int threads = ippiSuggestThreadsNum(iwDst, 2);
5950 if(IPP_BILATERAL_PARALLEL && threads > 1) {
5952 Range range(0, (int)iwDst.m_size.height);
5953 ipp_bilateralFilterParallel invoker(iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ippBorder, &ok);
5957 parallel_for_(range, invoker, threads*4);
5962 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), ippBorder);
5965 catch (::ipp::IwException)
5971 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(d); CV_UNUSED(sigmaColor); CV_UNUSED(sigmaSpace); CV_UNUSED(borderType);
5979 void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
5980 double sigmaColor, double sigmaSpace,
5983 CV_INSTRUMENT_REGION()
5985 _dst.create( _src.size(), _src.type() );
5987 CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
5988 ocl_bilateralFilter_8u(_src, _dst, d, sigmaColor, sigmaSpace, borderType))
5990 Mat src = _src.getMat(), dst = _dst.getMat();
5992 CV_IPP_RUN_FAST(ipp_bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType));
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 );
5999 CV_Error( CV_StsUnsupportedFormat,
6000 "Bilateral filtering is only implemented for 8u and 32f images" );
6003 //////////////////////////////////////////////////////////////////////////////////////////
6006 cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
6007 int param1, int param2, double param3, double param4 )
6009 cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
6011 CV_Assert( dst.size() == src.size() &&
6012 (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
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 );
6025 cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
6027 if( dst.data != dst0.data )
6028 CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );