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)",
1338 cv::Ptr<cv::BaseColumnFilter> cv::getColumnSumFilter(int sumType, int dstType, int ksize,
1339 int anchor, double scale)
1341 int sdepth = CV_MAT_DEPTH(sumType), ddepth = CV_MAT_DEPTH(dstType);
1342 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(dstType) );
1347 if( ddepth == CV_8U && sdepth == CV_32S )
1348 return makePtr<ColumnSum<int, uchar> >(ksize, anchor, scale);
1349 if( ddepth == CV_8U && sdepth == CV_16U )
1350 return makePtr<ColumnSum<ushort, uchar> >(ksize, anchor, scale);
1351 if( ddepth == CV_8U && sdepth == CV_64F )
1352 return makePtr<ColumnSum<double, uchar> >(ksize, anchor, scale);
1353 if( ddepth == CV_16U && sdepth == CV_32S )
1354 return makePtr<ColumnSum<int, ushort> >(ksize, anchor, scale);
1355 if( ddepth == CV_16U && sdepth == CV_64F )
1356 return makePtr<ColumnSum<double, ushort> >(ksize, anchor, scale);
1357 if( ddepth == CV_16S && sdepth == CV_32S )
1358 return makePtr<ColumnSum<int, short> >(ksize, anchor, scale);
1359 if( ddepth == CV_16S && sdepth == CV_64F )
1360 return makePtr<ColumnSum<double, short> >(ksize, anchor, scale);
1361 if( ddepth == CV_32S && sdepth == CV_32S )
1362 return makePtr<ColumnSum<int, int> >(ksize, anchor, scale);
1363 if( ddepth == CV_32F && sdepth == CV_32S )
1364 return makePtr<ColumnSum<int, float> >(ksize, anchor, scale);
1365 if( ddepth == CV_32F && sdepth == CV_64F )
1366 return makePtr<ColumnSum<double, float> >(ksize, anchor, scale);
1367 if( ddepth == CV_64F && sdepth == CV_32S )
1368 return makePtr<ColumnSum<int, double> >(ksize, anchor, scale);
1369 if( ddepth == CV_64F && sdepth == CV_64F )
1370 return makePtr<ColumnSum<double, double> >(ksize, anchor, scale);
1372 CV_Error_( CV_StsNotImplemented,
1373 ("Unsupported combination of sum format (=%d), and destination format (=%d)",
1378 cv::Ptr<cv::FilterEngine> cv::createBoxFilter( int srcType, int dstType, Size ksize,
1379 Point anchor, bool normalize, int borderType )
1381 int sdepth = CV_MAT_DEPTH(srcType);
1382 int cn = CV_MAT_CN(srcType), sumType = CV_64F;
1383 if( sdepth == CV_8U && CV_MAT_DEPTH(dstType) == CV_8U &&
1384 ksize.width*ksize.height <= 256 )
1386 else if( sdepth <= CV_32S && (!normalize ||
1387 ksize.width*ksize.height <= (sdepth == CV_8U ? (1<<23) :
1388 sdepth == CV_16U ? (1 << 15) : (1 << 16))) )
1390 sumType = CV_MAKETYPE( sumType, cn );
1392 Ptr<BaseRowFilter> rowFilter = getRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1393 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1394 dstType, ksize.height, anchor.y, normalize ? 1./(ksize.width*ksize.height) : 1);
1396 return makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1397 srcType, dstType, sumType, borderType );
1404 template <> inline bool skipSmallImages<VX_KERNEL_BOX_3x3>(int w, int h) { return w*h < 640 * 480; }
1406 static bool openvx_boxfilter(InputArray _src, OutputArray _dst, int ddepth,
1407 Size ksize, Point anchor,
1408 bool normalize, int borderType)
1412 if (_src.type() != CV_8UC1 || ddepth != CV_8U || !normalize ||
1413 _src.cols() < 3 || _src.rows() < 3 ||
1414 ksize.width != 3 || ksize.height != 3 ||
1415 (anchor.x >= 0 && anchor.x != 1) ||
1416 (anchor.y >= 0 && anchor.y != 1) ||
1417 ovx::skipSmallImages<VX_KERNEL_BOX_3x3>(_src.cols(), _src.rows()))
1420 Mat src = _src.getMat();
1422 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
1423 return false; //Process isolated borders only
1425 switch (borderType & ~BORDER_ISOLATED)
1427 case BORDER_CONSTANT:
1428 border = VX_BORDER_CONSTANT;
1430 case BORDER_REPLICATE:
1431 border = VX_BORDER_REPLICATE;
1437 _dst.create(src.size(), CV_8UC1);
1438 Mat dst = _dst.getMat();
1442 ivx::Context ctx = ovx::getOpenVXContext();
1445 if (dst.data != src.data)
1451 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1452 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
1453 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
1454 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
1456 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
1457 //since OpenVX standard says nothing about thread-safety for now
1458 ivx::border_t prevBorder = ctx.immediateBorder();
1459 ctx.setImmediateBorder(border, (vx_uint8)(0));
1460 ivx::IVX_CHECK_STATUS(vxuBox3x3(ctx, ia, ib));
1461 ctx.setImmediateBorder(prevBorder);
1463 catch (ivx::RuntimeError & e)
1465 VX_DbgThrow(e.what());
1467 catch (ivx::WrapperError & e)
1469 VX_DbgThrow(e.what());
1477 #if defined(HAVE_IPP)
1480 static bool ipp_boxfilter(Mat &src, Mat &dst, Size ksize, Point anchor, bool normalize, int borderType)
1483 CV_INSTRUMENT_REGION_IPP()
1485 #if IPP_VERSION_X100 < 201801
1486 // Problem with SSE42 optimization for 16s and some 8u modes
1487 if(ipp::getIppTopFeatures() == ippCPUID_SSE42 && (((src.depth() == CV_16S || src.depth() == CV_16U) && (src.channels() == 3 || src.channels() == 4)) || (src.depth() == CV_8U && src.channels() == 3 && (ksize.width > 5 || ksize.height > 5))))
1490 // Other optimizations has some degradations too
1491 if((((src.depth() == CV_16S || src.depth() == CV_16U) && (src.channels() == 4)) || (src.depth() == CV_8U && src.channels() == 1 && (ksize.width > 5 || ksize.height > 5))))
1498 if(!ippiCheckAnchor(anchor, ksize))
1503 ::ipp::IwiImage iwSrc = ippiGetImage(src);
1504 ::ipp::IwiImage iwDst = ippiGetImage(dst);
1505 ::ipp::IwiSize iwKSize = ippiGetSize(ksize);
1506 ::ipp::IwiBorderSize borderSize(iwKSize);
1507 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
1511 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBox, iwSrc, iwDst, iwKSize, ::ipp::IwDefault(), ippBorder);
1513 catch (::ipp::IwException)
1520 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(ksize); CV_UNUSED(anchor); CV_UNUSED(normalize); CV_UNUSED(borderType);
1528 void cv::boxFilter( InputArray _src, OutputArray _dst, int ddepth,
1529 Size ksize, Point anchor,
1530 bool normalize, int borderType )
1532 CV_INSTRUMENT_REGION()
1534 CV_OCL_RUN(_dst.isUMat() &&
1535 (borderType == BORDER_REPLICATE || borderType == BORDER_CONSTANT ||
1536 borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101),
1537 ocl_boxFilter3x3_8UC1(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1539 CV_OCL_RUN(_dst.isUMat(), ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize))
1541 Mat src = _src.getMat();
1542 int stype = src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype);
1545 _dst.create( src.size(), CV_MAKETYPE(ddepth, cn) );
1546 Mat dst = _dst.getMat();
1547 if( borderType != BORDER_CONSTANT && normalize && (borderType & BORDER_ISOLATED) != 0 )
1556 Size wsz(src.cols, src.rows);
1557 if(!(borderType&BORDER_ISOLATED))
1558 src.locateROI( wsz, ofs );
1560 CALL_HAL(boxFilter, cv_hal_boxFilter, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, ddepth, cn,
1561 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
1562 anchor.x, anchor.y, normalize, borderType&~BORDER_ISOLATED);
1565 openvx_boxfilter(src, dst, ddepth, ksize, anchor, normalize, borderType))
1567 CV_IPP_RUN_FAST(ipp_boxfilter(src, dst, ksize, anchor, normalize, borderType));
1569 borderType = (borderType&~BORDER_ISOLATED);
1571 Ptr<FilterEngine> f = createBoxFilter( src.type(), dst.type(),
1572 ksize, anchor, normalize, borderType );
1574 f->apply( src, dst, wsz, ofs );
1578 void cv::blur( InputArray src, OutputArray dst,
1579 Size ksize, Point anchor, int borderType )
1581 CV_INSTRUMENT_REGION()
1583 boxFilter( src, dst, -1, ksize, anchor, true, borderType );
1587 /****************************************************************************************\
1589 \****************************************************************************************/
1594 template<typename T, typename ST>
1596 public BaseRowFilter
1598 SqrRowSum( int _ksize, int _anchor ) :
1605 virtual void operator()(const uchar* src, uchar* dst, int width, int cn) CV_OVERRIDE
1607 const T* S = (const T*)src;
1609 int i = 0, k, ksz_cn = ksize*cn;
1611 width = (width - 1)*cn;
1612 for( k = 0; k < cn; k++, S++, D++ )
1615 for( i = 0; i < ksz_cn; i += cn )
1621 for( i = 0; i < width; i += cn )
1623 ST val0 = (ST)S[i], val1 = (ST)S[i + ksz_cn];
1624 s += val1*val1 - val0*val0;
1631 static Ptr<BaseRowFilter> getSqrRowSumFilter(int srcType, int sumType, int ksize, int anchor)
1633 int sdepth = CV_MAT_DEPTH(srcType), ddepth = CV_MAT_DEPTH(sumType);
1634 CV_Assert( CV_MAT_CN(sumType) == CV_MAT_CN(srcType) );
1639 if( sdepth == CV_8U && ddepth == CV_32S )
1640 return makePtr<SqrRowSum<uchar, int> >(ksize, anchor);
1641 if( sdepth == CV_8U && ddepth == CV_64F )
1642 return makePtr<SqrRowSum<uchar, double> >(ksize, anchor);
1643 if( sdepth == CV_16U && ddepth == CV_64F )
1644 return makePtr<SqrRowSum<ushort, double> >(ksize, anchor);
1645 if( sdepth == CV_16S && ddepth == CV_64F )
1646 return makePtr<SqrRowSum<short, double> >(ksize, anchor);
1647 if( sdepth == CV_32F && ddepth == CV_64F )
1648 return makePtr<SqrRowSum<float, double> >(ksize, anchor);
1649 if( sdepth == CV_64F && ddepth == CV_64F )
1650 return makePtr<SqrRowSum<double, double> >(ksize, anchor);
1652 CV_Error_( CV_StsNotImplemented,
1653 ("Unsupported combination of source format (=%d), and buffer format (=%d)",
1659 void cv::sqrBoxFilter( InputArray _src, OutputArray _dst, int ddepth,
1660 Size ksize, Point anchor,
1661 bool normalize, int borderType )
1663 CV_INSTRUMENT_REGION()
1665 int srcType = _src.type(), sdepth = CV_MAT_DEPTH(srcType), cn = CV_MAT_CN(srcType);
1666 Size size = _src.size();
1669 ddepth = sdepth < CV_32F ? CV_32F : CV_64F;
1671 if( borderType != BORDER_CONSTANT && normalize )
1673 if( size.height == 1 )
1675 if( size.width == 1 )
1679 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
1680 ocl_boxFilter(_src, _dst, ddepth, ksize, anchor, borderType, normalize, true))
1682 int sumDepth = CV_64F;
1683 if( sdepth == CV_8U )
1685 int sumType = CV_MAKETYPE( sumDepth, cn ), dstType = CV_MAKETYPE(ddepth, cn);
1687 Mat src = _src.getMat();
1688 _dst.create( size, dstType );
1689 Mat dst = _dst.getMat();
1691 Ptr<BaseRowFilter> rowFilter = getSqrRowSumFilter(srcType, sumType, ksize.width, anchor.x );
1692 Ptr<BaseColumnFilter> columnFilter = getColumnSumFilter(sumType,
1693 dstType, ksize.height, anchor.y,
1694 normalize ? 1./(ksize.width*ksize.height) : 1);
1696 Ptr<FilterEngine> f = makePtr<FilterEngine>(Ptr<BaseFilter>(), rowFilter, columnFilter,
1697 srcType, dstType, sumType, borderType );
1699 Size wsz(src.cols, src.rows);
1700 src.locateROI( wsz, ofs );
1702 f->apply( src, dst, wsz, ofs );
1706 /****************************************************************************************\
1708 \****************************************************************************************/
1710 cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype )
1712 const int SMALL_GAUSSIAN_SIZE = 7;
1713 static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
1716 {0.25f, 0.5f, 0.25f},
1717 {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
1718 {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
1721 const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
1722 small_gaussian_tab[n>>1] : 0;
1724 CV_Assert( ktype == CV_32F || ktype == CV_64F );
1725 Mat kernel(n, 1, ktype);
1726 float* cf = kernel.ptr<float>();
1727 double* cd = kernel.ptr<double>();
1729 double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
1730 double scale2X = -0.5/(sigmaX*sigmaX);
1734 for( i = 0; i < n; i++ )
1736 double x = i - (n-1)*0.5;
1737 double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
1738 if( ktype == CV_32F )
1751 for( i = 0; i < n; i++ )
1753 if( ktype == CV_32F )
1754 cf[i] = (float)(cf[i]*sum);
1764 template <typename T>
1765 static std::vector<T> getFixedpointGaussianKernel( int n, double sigma )
1770 return std::vector<T>(1, softdouble(1.0));
1773 T v3[] = { softdouble(0.25), softdouble(0.5), softdouble(0.25) };
1774 return std::vector<T>(v3, v3 + 3);
1778 T v5[] = { softdouble(0.0625), softdouble(0.25), softdouble(0.375), softdouble(0.25), softdouble(0.0625) };
1779 return std::vector<T>(v5, v5 + 5);
1783 T v7[] = { softdouble(0.03125), softdouble(0.109375), softdouble(0.21875), softdouble(0.28125), softdouble(0.21875), softdouble(0.109375), softdouble(0.03125) };
1784 return std::vector<T>(v7, v7 + 7);
1789 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)
1790 softdouble scale2X = softdouble(-0.5*0.25)/(sigmaX*sigmaX);
1791 std::vector<softdouble> values(n);
1793 for(int i = 0, x = 1 - n; i < n; i++, x+=2 )
1795 // x = i - (n - 1)*0.5
1796 // t = std::exp(scale2X*x*x)
1797 values[i] = exp(softdouble(x*x)*scale2X);
1800 sum = softdouble::one()/sum;
1802 std::vector<T> kernel(n);
1803 for(int i = 0; i < n; i++ )
1805 kernel[i] = values[i] * sum;
1811 template <typename ET, typename FT>
1812 void hlineSmooth1N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int)
1814 for (int i = 0; i < len*cn; i++, src++, dst++)
1815 *dst = (*m) * (*src);
1818 void hlineSmooth1N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int)
1821 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
1823 for (; i <= lencn - 16; i += 16)
1825 v_uint8x16 v_src = v_load(src + i);
1826 v_uint16x8 v_tmp0, v_tmp1;
1827 v_expand(v_src, v_tmp0, v_tmp1);
1828 v_store((uint16_t*)dst + i, v_mul*v_tmp0);
1829 v_store((uint16_t*)dst + i + 8, v_mul*v_tmp1);
1833 v_uint16x8 v_src = v_load_expand(src + i);
1834 v_store((uint16_t*)dst + i, v_mul*v_src);
1837 for (; i < lencn; i++)
1838 dst[i] = m[0] * src[i];
1840 template <typename ET, typename FT>
1841 void hlineSmooth1N1(const ET* src, int cn, const FT*, int, FT* dst, int len, int)
1843 for (int i = 0; i < len*cn; i++, src++, dst++)
1847 void hlineSmooth1N1<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int)
1851 for (; i <= lencn - 16; i += 16)
1853 v_uint8x16 v_src = v_load(src + i);
1854 v_uint16x8 v_tmp0, v_tmp1;
1855 v_expand(v_src, v_tmp0, v_tmp1);
1856 v_store((uint16_t*)dst + i, v_shl<8>(v_tmp0));
1857 v_store((uint16_t*)dst + i + 8, v_shl<8>(v_tmp1));
1861 v_uint16x8 v_src = v_load_expand(src + i);
1862 v_store((uint16_t*)dst + i, v_shl<8>(v_src));
1865 for (; i < lencn; i++)
1868 template <typename ET, typename FT>
1869 void hlineSmooth3N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
1873 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1874 for (int k = 0; k < cn; k++)
1875 dst[k] = msum * src[k];
1879 // Point that fall left from border
1880 for (int k = 0; k < cn; k++)
1881 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1882 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1884 int src_idx = borderInterpolate(-1, len, borderType);
1885 for (int k = 0; k < cn; k++)
1886 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1889 src += cn; dst += cn;
1890 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1891 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1893 // Point that fall right from border
1894 for (int k = 0; k < cn; k++)
1895 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1896 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1898 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1899 for (int k = 0; k < cn; k++)
1900 dst[k] = dst[k] + m[2] * src[src_idx + k];
1905 void hlineSmooth3N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
1909 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1910 for (int k = 0; k < cn; k++)
1911 dst[k] = msum * src[k];
1915 // Point that fall left from border
1916 for (int k = 0; k < cn; k++)
1917 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1918 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1920 int src_idx = borderInterpolate(-1, len, borderType);
1921 for (int k = 0; k < cn; k++)
1922 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1925 src += cn; dst += cn;
1926 int i = cn, lencn = (len - 1)*cn;
1927 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
1928 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
1929 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
1930 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
1932 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
1933 v_expand(v_load(src - cn), v_src00, v_src01);
1934 v_expand(v_load(src), v_src10, v_src11);
1935 v_expand(v_load(src + cn), v_src20, v_src21);
1936 v_store((uint16_t*)dst, v_src00 * v_mul0 + v_src10 * v_mul1 + v_src20 * v_mul2);
1937 v_store((uint16_t*)dst + 8, v_src01 * v_mul0 + v_src11 * v_mul1 + v_src21 * v_mul2);
1939 for (; i < lencn; i++, src++, dst++)
1940 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1942 // Point that fall right from border
1943 for (int k = 0; k < cn; k++)
1944 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1945 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1947 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1948 for (int k = 0; k < cn; k++)
1949 dst[k] = dst[k] + m[2] * src[src_idx + k];
1953 template <typename ET, typename FT>
1954 void hlineSmooth3N121(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
1958 if(borderType != BORDER_CONSTANT)
1959 for (int k = 0; k < cn; k++)
1960 dst[k] = FT(src[k]);
1962 for (int k = 0; k < cn; k++)
1963 dst[k] = FT(src[k])>>1;
1967 // Point that fall left from border
1968 for (int k = 0; k < cn; k++)
1969 dst[k] = (FT(src[k])>>1) + (FT(src[cn + k])>>2);
1970 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1972 int src_idx = borderInterpolate(-1, len, borderType);
1973 for (int k = 0; k < cn; k++)
1974 dst[k] = dst[k] + (FT(src[src_idx*cn + k])>>2);
1977 src += cn; dst += cn;
1978 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1979 *dst = (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[0])>>1);
1981 // Point that fall right from border
1982 for (int k = 0; k < cn; k++)
1983 dst[k] = (FT(src[k - cn])>>2) + (FT(src[k])>>1);
1984 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1986 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1987 for (int k = 0; k < cn; k++)
1988 dst[k] = dst[k] + (FT(src[src_idx + k])>>2);
1993 void hlineSmooth3N121<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
1997 if (borderType != BORDER_CONSTANT)
1998 for (int k = 0; k < cn; k++)
1999 dst[k] = ufixedpoint16(src[k]);
2001 for (int k = 0; k < cn; k++)
2002 dst[k] = ufixedpoint16(src[k]) >> 1;
2006 // Point that fall left from border
2007 for (int k = 0; k < cn; k++)
2008 dst[k] = (ufixedpoint16(src[k])>>1) + (ufixedpoint16(src[cn + k])>>2);
2009 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2011 int src_idx = borderInterpolate(-1, len, borderType);
2012 for (int k = 0; k < cn; k++)
2013 dst[k] = dst[k] + (ufixedpoint16(src[src_idx*cn + k])>>2);
2016 src += cn; dst += cn;
2017 int i = cn, lencn = (len - 1)*cn;
2018 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2020 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2021 v_expand(v_load(src - cn), v_src00, v_src01);
2022 v_expand(v_load(src), v_src10, v_src11);
2023 v_expand(v_load(src + cn), v_src20, v_src21);
2024 v_store((uint16_t*)dst, (v_src00 + v_src20 + (v_src10 << 1)) << 6);
2025 v_store((uint16_t*)dst + 8, (v_src01 + v_src21 + (v_src11 << 1)) << 6);
2027 for (; i < lencn; i++, src++, dst++)
2028 *((uint16_t*)dst) = (uint16_t(src[-cn]) + uint16_t(src[cn]) + (uint16_t(src[0]) << 1)) << 6;
2030 // Point that fall right from border
2031 for (int k = 0; k < cn; k++)
2032 dst[k] = (ufixedpoint16(src[k - cn])>>2) + (ufixedpoint16(src[k])>>1);
2033 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2035 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2036 for (int k = 0; k < cn; k++)
2037 dst[k] = dst[k] + (ufixedpoint16(src[src_idx + k])>>2);
2041 template <typename ET, typename FT>
2042 void hlineSmooth3Naba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2046 FT msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2047 for (int k = 0; k < cn; k++)
2048 dst[k] = msum * src[k];
2052 // Point that fall left from border
2053 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2055 int src_idx = borderInterpolate(-1, len, borderType);
2056 for (int k = 0; k < cn; k++)
2057 dst[k] = m[1] * src[k] + m[0] * src[cn + k] + m[0] * src[src_idx*cn + k];
2061 for (int k = 0; k < cn; k++)
2062 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2065 src += cn; dst += cn;
2066 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
2067 *dst = m[1] * src[0] + m[0] * src[-cn] + m[0] * src[cn];
2069 // Point that fall right from border
2070 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2072 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2073 for (int k = 0; k < cn; k++)
2074 dst[k] = m[1] * src[k] + m[0] * src[k - cn] + m[0] * src[src_idx + k];
2078 for (int k = 0; k < cn; k++)
2079 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2084 void hlineSmooth3Naba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2088 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2089 for (int k = 0; k < cn; k++)
2090 dst[k] = msum * src[k];
2094 // Point that fall left from border
2095 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2097 int src_idx = borderInterpolate(-1, len, borderType);
2098 for (int k = 0; k < cn; k++)
2099 ((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]));
2103 for (int k = 0; k < cn; k++)
2104 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2107 src += cn; dst += cn;
2108 int i = cn, lencn = (len - 1)*cn;
2109 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2110 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)m+1));
2111 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2113 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2114 v_expand(v_load(src - cn), v_src00, v_src01);
2115 v_expand(v_load(src), v_src10, v_src11);
2116 v_expand(v_load(src + cn), v_src20, v_src21);
2117 v_store((uint16_t*)dst, (v_src00 + v_src20) * v_mul0 + v_src10 * v_mul1);
2118 v_store((uint16_t*)dst + 8, (v_src01 + v_src21) * v_mul0 + v_src11 * v_mul1);
2120 for (; i < lencn; i++, src++, dst++)
2121 *((uint16_t*)dst) = ((uint16_t*)m)[1] * src[0] + ((uint16_t*)m)[0] * ((uint16_t)(src[-cn]) + (uint16_t)(src[cn]));
2123 // Point that fall right from border
2124 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2126 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2127 for (int k = 0; k < cn; k++)
2128 ((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]));
2132 for (int k = 0; k < cn; k++)
2133 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2137 template <typename ET, typename FT>
2138 void hlineSmooth5N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2142 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2143 for (int k = 0; k < cn; k++)
2144 dst[k] = msum * src[k];
2148 if (borderType == BORDER_CONSTANT)
2149 for (int k = 0; k < cn; k++)
2151 dst[k ] = m[2] * src[k] + m[3] * src[k+cn];
2152 dst[k+cn] = m[1] * src[k] + m[2] * src[k+cn];
2156 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2157 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2158 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2159 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2160 for (int k = 0; k < cn; k++)
2162 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];
2163 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];
2169 if (borderType == BORDER_CONSTANT)
2170 for (int k = 0; k < cn; k++)
2172 dst[k ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn];
2173 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn];
2174 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn];
2178 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2179 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2180 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2181 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2182 for (int k = 0; k < cn; k++)
2184 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];
2185 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];
2186 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];
2192 // Points that fall left from border
2193 for (int k = 0; k < cn; k++)
2195 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2*cn + k];
2196 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2*cn + k] + m[4] * src[3*cn + k];
2198 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2200 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2201 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2202 for (int k = 0; k < cn; k++)
2204 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2205 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2209 src += 2*cn; dst += 2*cn;
2210 for (int i = 2*cn; i < (len - 2)*cn; i++, src++, dst++)
2211 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2213 // Points that fall right from border
2214 for (int k = 0; k < cn; k++)
2216 dst[k] = m[0] * src[k - 2*cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2217 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2219 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2221 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2222 int idxp2 = (borderInterpolate(len+1, len, borderType) - (len - 2))*cn;
2223 for (int k = 0; k < cn; k++)
2225 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2226 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2232 void hlineSmooth5N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2236 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2237 for (int k = 0; k < cn; k++)
2238 dst[k] = msum * src[k];
2242 if (borderType == BORDER_CONSTANT)
2243 for (int k = 0; k < cn; k++)
2245 dst[k] = m[2] * src[k] + m[3] * src[k + cn];
2246 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2250 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2251 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2252 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2253 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2254 for (int k = 0; k < cn; k++)
2256 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];
2257 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];
2263 if (borderType == BORDER_CONSTANT)
2264 for (int k = 0; k < cn; k++)
2266 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn];
2267 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn];
2268 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2272 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2273 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2274 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2275 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2276 for (int k = 0; k < cn; k++)
2278 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];
2279 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];
2280 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];
2286 // Points that fall left from border
2287 for (int k = 0; k < cn; k++)
2289 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2 * cn + k];
2290 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2 * cn + k] + m[4] * src[3 * cn + k];
2292 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2294 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2295 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2296 for (int k = 0; k < cn; k++)
2298 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2299 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2303 src += 2 * cn; dst += 2 * cn;
2304 int i = 2*cn, lencn = (len - 2)*cn;
2305 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2306 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2307 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2308 v_uint16x8 v_mul3 = v_setall_u16(*((uint16_t*)(m + 3)));
2309 v_uint16x8 v_mul4 = v_setall_u16(*((uint16_t*)(m + 4)));
2310 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2312 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2313 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2314 v_expand(v_load(src - cn), v_src10, v_src11);
2315 v_expand(v_load(src), v_src20, v_src21);
2316 v_expand(v_load(src + cn), v_src30, v_src31);
2317 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2318 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);
2319 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);
2321 for (; i < lencn; i++, src++, dst++)
2322 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2324 // Points that fall right from border
2325 for (int k = 0; k < cn; k++)
2327 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2328 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2330 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2332 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2333 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2334 for (int k = 0; k < cn; k++)
2336 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2337 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2342 template <typename ET, typename FT>
2343 void hlineSmooth5N14641(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
2347 if (borderType == BORDER_CONSTANT)
2348 for (int k = 0; k < cn; k++)
2349 dst[k] = (FT(src[k])>>3)*(uint8_t)3;
2351 for (int k = 0; k < cn; k++)
2356 if (borderType == BORDER_CONSTANT)
2357 for (int k = 0; k < cn; k++)
2359 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2);
2360 dst[k + cn] = (FT(src[k]) >> 2) + (FT(src[k + cn])>>4)*(uint8_t)6;
2364 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2365 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2366 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2367 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2368 for (int k = 0; k < cn; k++)
2370 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);
2371 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);
2377 if (borderType == BORDER_CONSTANT)
2378 for (int k = 0; k < cn; k++)
2380 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + 2 * cn])>>4);
2381 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2);
2382 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k])>>4);
2386 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2387 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2388 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2389 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2390 for (int k = 0; k < cn; k++)
2392 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);
2393 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);
2394 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);
2400 // Points that fall left from border
2401 for (int k = 0; k < cn; k++)
2403 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[cn + k])>>2) + (FT(src[2 * cn + k])>>4);
2404 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);
2406 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2408 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2409 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2410 for (int k = 0; k < cn; k++)
2412 dst[k] = dst[k] + (FT(src[idxm2 + k])>>4) + (FT(src[idxm1 + k])>>2);
2413 dst[k + cn] = dst[k + cn] + (FT(src[idxm1 + k])>>4);
2417 src += 2 * cn; dst += 2 * cn;
2418 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2419 *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);
2421 // Points that fall right from border
2422 for (int k = 0; k < cn; k++)
2424 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);
2425 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k - cn])>>4);
2427 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2429 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2430 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2431 for (int k = 0; k < cn; k++)
2433 dst[k] = dst[k] + (FT(src[idxp1 + k])>>4);
2434 dst[k + cn] = dst[k + cn] + (FT(src[idxp1 + k])>>2) + (FT(src[idxp2 + k])>>4);
2440 void hlineSmooth5N14641<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2444 if (borderType == BORDER_CONSTANT)
2445 for (int k = 0; k < cn; k++)
2446 dst[k] = (ufixedpoint16(src[k])>>3) * (uint8_t)3;
2449 for (int k = 0; k < cn; k++)
2455 if (borderType == BORDER_CONSTANT)
2456 for (int k = 0; k < cn; k++)
2458 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2);
2459 dst[k + cn] = (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6;
2463 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2464 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2465 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2466 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2467 for (int k = 0; k < cn; k++)
2469 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);
2470 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);
2476 if (borderType == BORDER_CONSTANT)
2477 for (int k = 0; k < cn; k++)
2479 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4);
2480 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2);
2481 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k]) >> 4);
2485 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2486 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2487 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2488 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2489 for (int k = 0; k < cn; k++)
2491 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);
2492 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);
2493 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);
2499 // Points that fall left from border
2500 for (int k = 0; k < cn; k++)
2502 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[cn + k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 4);
2503 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);
2505 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2507 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2508 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2509 for (int k = 0; k < cn; k++)
2511 dst[k] = dst[k] + (ufixedpoint16(src[idxm2 + k]) >> 4) + (ufixedpoint16(src[idxm1 + k]) >> 2);
2512 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxm1 + k]) >> 4);
2516 src += 2 * cn; dst += 2 * cn;
2517 int i = 2 * cn, lencn = (len - 2)*cn;
2518 v_uint16x8 v_6 = v_setall_u16(6);
2519 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2521 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2522 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2523 v_expand(v_load(src - cn), v_src10, v_src11);
2524 v_expand(v_load(src), v_src20, v_src21);
2525 v_expand(v_load(src + cn), v_src30, v_src31);
2526 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2527 v_store((uint16_t*)dst, (v_src20 * v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40) << 4);
2528 v_store((uint16_t*)dst + 8, (v_src21 * v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41) << 4);
2530 for (; i < lencn; i++, src++, dst++)
2531 *((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;
2533 // Points that fall right from border
2534 for (int k = 0; k < cn; k++)
2536 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);
2537 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k - cn]) >> 4);
2539 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2541 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2542 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2543 for (int k = 0; k < cn; k++)
2545 dst[k] = dst[k] + (ufixedpoint16(src[idxp1 + k]) >> 4);
2546 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxp1 + k]) >> 2) + (ufixedpoint16(src[idxp2 + k]) >> 4);
2551 template <typename ET, typename FT>
2552 void hlineSmooth5Nabcba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2556 FT msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1])<<1) + m[2] : m[2];
2557 for (int k = 0; k < cn; k++)
2558 dst[k] = msum * src[k];
2562 if (borderType == BORDER_CONSTANT)
2563 for (int k = 0; k < cn; k++)
2565 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2566 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2570 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2571 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2572 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2573 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2574 for (int k = 0; k < cn; k++)
2576 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];
2577 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];
2583 if (borderType == BORDER_CONSTANT)
2584 for (int k = 0; k < cn; k++)
2586 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2587 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + 2 * cn];
2588 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2592 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2593 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2594 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2595 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2596 for (int k = 0; k < cn; k++)
2598 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];
2599 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];
2600 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];
2606 // Points that fall left from border
2607 for (int k = 0; k < cn; k++)
2609 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2610 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[1] * src[2 * cn + k] + m[0] * src[3 * cn + k];
2612 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2614 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2615 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2616 for (int k = 0; k < cn; k++)
2618 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2619 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2623 src += 2 * cn; dst += 2 * cn;
2624 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2625 *dst = m[0] * src[-2 * cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2 * cn];
2627 // Points that fall right from border
2628 for (int k = 0; k < cn; k++)
2630 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2631 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2633 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2635 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2636 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2637 for (int k = 0; k < cn; k++)
2639 dst[k] = dst[k] + m[0] * src[idxp1 + k];
2640 dst[k + cn] = dst[k + cn] + m[1] * src[idxp1 + k] + m[0] * src[idxp2 + k];
2646 void hlineSmooth5Nabcba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2650 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1]) << 1) + m[2] : m[2];
2651 for (int k = 0; k < cn; k++)
2652 dst[k] = msum * src[k];
2656 if (borderType == BORDER_CONSTANT)
2657 for (int k = 0; k < cn; k++)
2659 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2660 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2664 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2665 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2666 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2667 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2668 for (int k = 0; k < cn; k++)
2670 ((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]));
2671 ((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];
2677 if (borderType == BORDER_CONSTANT)
2678 for (int k = 0; k < cn; k++)
2680 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2681 ((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];
2682 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2686 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2687 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2688 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2689 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2690 for (int k = 0; k < cn; k++)
2692 ((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]));
2693 ((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]));
2694 ((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];
2700 // Points that fall left from border
2701 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2703 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2704 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2705 for (int k = 0; k < cn; k++)
2707 ((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]));
2708 ((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]));
2713 for (int k = 0; k < cn; k++)
2715 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2716 ((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];
2720 src += 2 * cn; dst += 2 * cn;
2721 int i = 2 * cn, lencn = (len - 2)*cn;
2722 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2723 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2724 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2725 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2727 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2728 v_expand(v_load(src - 2 * cn), v_src00, v_src01);
2729 v_expand(v_load(src - cn), v_src10, v_src11);
2730 v_expand(v_load(src), v_src20, v_src21);
2731 v_expand(v_load(src + cn), v_src30, v_src31);
2732 v_expand(v_load(src + 2 * cn), v_src40, v_src41);
2733 v_store((uint16_t*)dst, (v_src00 + v_src40) * v_mul0 + (v_src10 + v_src30)* v_mul1 + v_src20 * v_mul2);
2734 v_store((uint16_t*)dst + 8, (v_src01 + v_src41) * v_mul0 + (v_src11 + v_src31) * v_mul1 + v_src21 * v_mul2);
2736 for (; i < lencn; i++, src++, dst++)
2737 *((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];
2739 // Points that fall right from border
2740 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2742 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2743 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2744 for (int k = 0; k < cn; k++)
2746 ((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];
2747 ((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];
2752 for (int k = 0; k < cn; k++)
2754 ((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];
2755 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2760 template <typename ET, typename FT>
2761 void hlineSmooth(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2763 int pre_shift = n / 2;
2764 int post_shift = n - pre_shift;
2766 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2768 for (int k = 0; k < cn; k++)
2769 dst[k] = m[pre_shift-i] * src[k];
2770 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2771 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2773 int src_idx = borderInterpolate(j, len, borderType);
2774 for (int k = 0; k < cn; k++)
2775 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2778 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2779 for (int k = 0; k < cn; k++)
2780 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2781 if (borderType != BORDER_CONSTANT)
2782 for (; j < i + post_shift; j++, mid++)
2784 int src_idx = borderInterpolate(j, len, borderType);
2785 for (int k = 0; k < cn; k++)
2786 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2790 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2792 *dst = m[0] * src[0];
2793 for (int j = 1; j < n; j++)
2794 *dst = *dst + m[j] * src[j*cn];
2797 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2799 for (int k = 0; k < cn; k++)
2800 dst[k] = m[0] * src[k];
2802 for (; j < len - i; j++)
2803 for (int k = 0; k < cn; k++)
2804 dst[k] = dst[k] + m[j] * src[j*cn + k];
2805 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2808 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2809 for (int k = 0; k < cn; k++)
2810 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2815 void hlineSmooth<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2817 int pre_shift = n / 2;
2818 int post_shift = n - pre_shift;
2820 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2822 for (int k = 0; k < cn; k++)
2823 dst[k] = m[pre_shift - i] * src[k];
2824 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2825 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2827 int src_idx = borderInterpolate(j, len, borderType);
2828 for (int k = 0; k < cn; k++)
2829 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2832 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2833 for (int k = 0; k < cn; k++)
2834 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2835 if (borderType != BORDER_CONSTANT)
2836 for (; j < i + post_shift; j++, mid++)
2838 int src_idx = borderInterpolate(j, len, borderType);
2839 for (int k = 0; k < cn; k++)
2840 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2844 int lencn = (len - post_shift + 1)*cn;
2845 for (; i <= lencn - 16; i+=16, src+=16, dst+=16)
2847 v_uint16x8 v_src0, v_src1;
2848 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
2849 v_expand(v_load(src), v_src0, v_src1);
2850 v_uint16x8 v_res0 = v_src0 * v_mul;
2851 v_uint16x8 v_res1 = v_src1 * v_mul;
2852 for (int j = 1; j < n; j++)
2854 v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2855 v_expand(v_load(src + j * cn), v_src0, v_src1);
2856 v_res0 += v_src0 * v_mul;
2857 v_res1 += v_src1 * v_mul;
2859 v_store((uint16_t*)dst, v_res0);
2860 v_store((uint16_t*)dst+8, v_res1);
2862 for (; i < lencn; i++, src++, dst++)
2864 *dst = m[0] * src[0];
2865 for (int j = 1; j < n; j++)
2866 *dst = *dst + m[j] * src[j*cn];
2869 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2871 for (int k = 0; k < cn; k++)
2872 dst[k] = m[0] * src[k];
2874 for (; j < len - i; j++)
2875 for (int k = 0; k < cn; k++)
2876 dst[k] = dst[k] + m[j] * src[j*cn + k];
2877 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2880 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2881 for (int k = 0; k < cn; k++)
2882 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2886 template <typename ET, typename FT>
2887 void hlineSmoothONa_yzy_a(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2889 int pre_shift = n / 2;
2890 int post_shift = n - pre_shift;
2892 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2894 for (int k = 0; k < cn; k++)
2895 dst[k] = m[pre_shift - i] * src[k];
2896 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2897 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2899 int src_idx = borderInterpolate(j, len, borderType);
2900 for (int k = 0; k < cn; k++)
2901 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2904 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2905 for (int k = 0; k < cn; k++)
2906 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2907 if (borderType != BORDER_CONSTANT)
2908 for (; j < i + post_shift; j++, mid++)
2910 int src_idx = borderInterpolate(j, len, borderType);
2911 for (int k = 0; k < cn; k++)
2912 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2916 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2918 *dst = m[pre_shift] * src[pre_shift*cn];
2919 for (int j = 0; j < pre_shift; j++)
2920 *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n-1-j)*cn];
2923 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2925 for (int k = 0; k < cn; k++)
2926 dst[k] = m[0] * src[k];
2928 for (; j < len - i; j++)
2929 for (int k = 0; k < cn; k++)
2930 dst[k] = dst[k] + m[j] * src[j*cn + k];
2931 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2934 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2935 for (int k = 0; k < cn; k++)
2936 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2941 void hlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2943 int pre_shift = n / 2;
2944 int post_shift = n - pre_shift;
2946 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2948 for (int k = 0; k < cn; k++)
2949 dst[k] = m[pre_shift - i] * src[k];
2950 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2951 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2953 int src_idx = borderInterpolate(j, len, borderType);
2954 for (int k = 0; k < cn; k++)
2955 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2958 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2959 for (int k = 0; k < cn; k++)
2960 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2961 if (borderType != BORDER_CONSTANT)
2962 for (; j < i + post_shift; j++, mid++)
2964 int src_idx = borderInterpolate(j, len, borderType);
2965 for (int k = 0; k < cn; k++)
2966 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2970 int lencn = (len - post_shift + 1)*cn;
2971 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2973 v_uint16x8 v_src00, v_src01, v_srcN00, v_srcN01;
2975 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)(m + pre_shift)));
2976 v_expand(v_load(src + pre_shift * cn), v_src00, v_src01);
2977 v_uint16x8 v_res0 = v_src00 * v_mul;
2978 v_uint16x8 v_res1 = v_src01 * v_mul;
2979 for (int j = 0; j < pre_shift; j ++)
2981 v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2982 v_expand(v_load(src + j * cn), v_src00, v_src01);
2983 v_expand(v_load(src + (n - 1 - j)*cn), v_srcN00, v_srcN01);
2984 v_res0 += (v_src00 + v_srcN00) * v_mul;
2985 v_res1 += (v_src01 + v_srcN01) * v_mul;
2988 v_store((uint16_t*)dst, v_res0);
2989 v_store((uint16_t*)dst + 8, v_res1);
2991 for (; i < lencn; i++, src++, dst++)
2993 *dst = m[pre_shift] * src[pre_shift*cn];
2994 for (int j = 0; j < pre_shift; j++)
2995 *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n - 1 - j)*cn];
2998 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
3000 for (int k = 0; k < cn; k++)
3001 dst[k] = m[0] * src[k];
3003 for (; j < len - i; j++)
3004 for (int k = 0; k < cn; k++)
3005 dst[k] = dst[k] + m[j] * src[j*cn + k];
3006 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3009 int src_idx = borderInterpolate(i + j, len, borderType) - i;
3010 for (int k = 0; k < cn; k++)
3011 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
3015 template <typename ET, typename FT>
3016 void vlineSmooth1N(const FT* const * src, const FT* m, int, ET* dst, int len)
3018 const FT* src0 = src[0];
3019 for (int i = 0; i < len; i++)
3020 dst[i] = *m * src0[i];
3023 void vlineSmooth1N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3025 const ufixedpoint16* src0 = src[0];
3026 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
3028 v_uint16x8 v_1 = v_setall_u16(1);
3032 for (; i <= len - 16; i += 16)
3034 v_uint16x8 v_src0 = v_load((uint16_t*)src0 + i);
3035 v_uint16x8 v_src1 = v_load((uint16_t*)src0 + i + 8);
3038 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),
3039 _mm_srli_epi16(_mm_add_epi16(v_1.val, _mm_mulhi_epu16(v_src1.val, v_mul.val)),1));
3041 v_uint32x4 v_res0, v_res1, v_res2, v_res3;
3042 v_mul_expand(v_src0, v_mul, v_res0, v_res1);
3043 v_mul_expand(v_src1, v_mul, v_res2, v_res3);
3044 v_res = v_pack(v_rshr_pack<16>(v_res0, v_res1), v_rshr_pack<16>(v_res2, v_res3));
3046 v_store(dst + i, v_res);
3048 for (; i < len; i++)
3049 dst[i] = m[0] * src0[i];
3051 template <typename ET, typename FT>
3052 void vlineSmooth1N1(const FT* const * src, const FT*, int, ET* dst, int len)
3054 const FT* src0 = src[0];
3055 for (int i = 0; i < len; i++)
3059 void vlineSmooth1N1<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3061 const ufixedpoint16* src0 = src[0];
3063 for (; i <= len - 8; i += 8)
3064 v_rshr_pack_store<8>(dst + i, v_load((uint16_t*)(src0 + i)));
3065 for (; i < len; i++)
3068 template <typename ET, typename FT>
3069 void vlineSmooth3N(const FT* const * src, const FT* m, int, ET* dst, int len)
3071 for (int i = 0; i < len; i++)
3072 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3075 void vlineSmooth3N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3078 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3079 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3082 ufixedpoint32 val[] = { (m[0] + m[1] + m[2]) * ufixedpoint16((uint8_t)128) };
3083 v_128_4 = v_setall_s32(*((int32_t*)val));
3085 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3086 v_int16x8 v_mul2 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 2))));
3087 for (; i <= len - 32; i += 32)
3089 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3090 v_int16x8 v_tmp0, v_tmp1;
3092 v_src00 = v_load((int16_t*)(src[0]) + i);
3093 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3094 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3095 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3096 v_src10 = v_load((int16_t*)(src[1]) + i);
3097 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3098 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3099 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3100 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3101 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3102 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3103 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3104 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3105 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3106 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3107 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3108 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3109 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3110 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3111 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3113 v_int32x4 v_resj0, v_resj1;
3114 v_src00 = v_load((int16_t*)(src[2]) + i);
3115 v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3116 v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3117 v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3118 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul2, v_resj0, v_resj1);
3121 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul2, v_resj0, v_resj1);
3124 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul2, v_resj0, v_resj1);
3127 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul2, v_resj0, v_resj1);
3140 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3141 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3142 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3143 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3145 for (; i < len; i++)
3146 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3148 template <typename ET, typename FT>
3149 void vlineSmooth3N121(const FT* const * src, const FT*, int, ET* dst, int len)
3151 for (int i = 0; i < len; i++)
3152 dst[i] = (FT::WT(src[0][i]) >> 2) + (FT::WT(src[2][i]) >> 2) + (FT::WT(src[1][i]) >> 1);
3155 void vlineSmooth3N121<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3158 for (; i <= len - 16; i += 16)
3160 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;
3161 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3162 v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3163 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3164 v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3165 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3166 v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3167 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)),
3168 v_rshr_pack<10>(v_src02 + v_src22 + (v_src12 + v_src12), v_src03 + v_src23 + (v_src13 + v_src13))));
3170 for (; i < len; i++)
3171 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;
3173 template <typename ET, typename FT>
3174 void vlineSmooth5N(const FT* const * src, const FT* m, int, ET* dst, int len)
3176 for (int i = 0; i < len; i++)
3177 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
3180 void vlineSmooth5N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3183 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3184 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3187 ufixedpoint32 val[] = { (m[0] + m[1] + m[2] + m[3] + m[4]) * ufixedpoint16((uint8_t)128) };
3188 v_128_4 = v_setall_s32(*((int32_t*)val));
3190 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3191 v_int16x8 v_mul23 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + 2))));
3192 v_int16x8 v_mul4 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 4))));
3193 for (; i <= len - 32; i += 32)
3195 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3196 v_int16x8 v_tmp0, v_tmp1;
3198 v_src00 = v_load((int16_t*)(src[0]) + i);
3199 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3200 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3201 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3202 v_src10 = v_load((int16_t*)(src[1]) + i);
3203 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3204 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3205 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3206 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3207 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3208 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3209 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3210 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3211 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3212 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3213 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3214 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3215 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3216 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3217 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3219 v_src00 = v_load((int16_t*)(src[2]) + i);
3220 v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3221 v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3222 v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3223 v_src10 = v_load((int16_t*)(src[3]) + i);
3224 v_src11 = v_load((int16_t*)(src[3]) + i + 8);
3225 v_src12 = v_load((int16_t*)(src[3]) + i + 16);
3226 v_src13 = v_load((int16_t*)(src[3]) + i + 24);
3227 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3228 v_res0 += v_dotprod(v_tmp0, v_mul23);
3229 v_res1 += v_dotprod(v_tmp1, v_mul23);
3230 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3231 v_res2 += v_dotprod(v_tmp0, v_mul23);
3232 v_res3 += v_dotprod(v_tmp1, v_mul23);
3233 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3234 v_res4 += v_dotprod(v_tmp0, v_mul23);
3235 v_res5 += v_dotprod(v_tmp1, v_mul23);
3236 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3237 v_res6 += v_dotprod(v_tmp0, v_mul23);
3238 v_res7 += v_dotprod(v_tmp1, v_mul23);
3240 v_int32x4 v_resj0, v_resj1;
3241 v_src00 = v_load((int16_t*)(src[4]) + i);
3242 v_src01 = v_load((int16_t*)(src[4]) + i + 8);
3243 v_src02 = v_load((int16_t*)(src[4]) + i + 16);
3244 v_src03 = v_load((int16_t*)(src[4]) + i + 24);
3245 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul4, v_resj0, v_resj1);
3248 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul4, v_resj0, v_resj1);
3251 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul4, v_resj0, v_resj1);
3254 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul4, v_resj0, v_resj1);
3267 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3268 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3269 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3270 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3272 for (; i < len; i++)
3273 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];
3275 template <typename ET, typename FT>
3276 void vlineSmooth5N14641(const FT* const * src, const FT*, int, ET* dst, int len)
3278 for (int i = 0; i < len; i++)
3279 dst[i] = (FT::WT(src[2][i])*(uint8_t)6 + ((FT::WT(src[1][i]) + FT::WT(src[3][i]))<<2) + FT::WT(src[0][i]) + FT::WT(src[4][i])) >> 4;
3282 void vlineSmooth5N14641<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3285 v_uint32x4 v_6 = v_setall_u32(6);
3286 for (; i <= len - 16; i += 16)
3288 v_uint32x4 v_src00, v_src10, v_src20, v_src30, v_src40;
3289 v_uint32x4 v_src01, v_src11, v_src21, v_src31, v_src41;
3290 v_uint32x4 v_src02, v_src12, v_src22, v_src32, v_src42;
3291 v_uint32x4 v_src03, v_src13, v_src23, v_src33, v_src43;
3292 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3293 v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3294 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3295 v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3296 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3297 v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3298 v_expand(v_load((uint16_t*)(src[3]) + i), v_src30, v_src31);
3299 v_expand(v_load((uint16_t*)(src[3]) + i + 8), v_src32, v_src33);
3300 v_expand(v_load((uint16_t*)(src[4]) + i), v_src40, v_src41);
3301 v_expand(v_load((uint16_t*)(src[4]) + i + 8), v_src42, v_src43);
3302 v_store(dst + i, v_pack(v_rshr_pack<12>(v_src20*v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40,
3303 v_src21*v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41),
3304 v_rshr_pack<12>(v_src22*v_6 + ((v_src12 + v_src32) << 2) + v_src02 + v_src42,
3305 v_src23*v_6 + ((v_src13 + v_src33) << 2) + v_src03 + v_src43)));
3307 for (; i < len; i++)
3308 dst[i] = ((uint32_t)(((uint16_t*)(src[2]))[i]) * 6 +
3309 (((uint32_t)(((uint16_t*)(src[1]))[i]) + (uint32_t)(((uint16_t*)(src[3]))[i])) << 2) +
3310 (uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[4]))[i]) + (1 << 11)) >> 12;
3312 template <typename ET, typename FT>
3313 void vlineSmooth(const FT* const * src, const FT* m, int n, ET* dst, int len)
3315 for (int i = 0; i < len; i++)
3317 typename FT::WT val = m[0] * src[0][i];
3318 for (int j = 1; j < n; j++)
3319 val = val + m[j] * src[j][i];
3324 void vlineSmooth<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3327 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3328 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3331 ufixedpoint16 msum = m[0] + m[1];
3332 for (int j = 2; j < n; j++)
3334 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3335 v_128_4 = v_setall_s32(*((int32_t*)val));
3337 for (; i <= len - 32; i += 32)
3339 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3340 v_int16x8 v_tmp0, v_tmp1;
3342 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3344 v_src00 = v_load((int16_t*)(src[0]) + i);
3345 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3346 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3347 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3348 v_src10 = v_load((int16_t*)(src[1]) + i);
3349 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3350 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3351 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3352 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3353 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul);
3354 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul);
3355 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3356 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul);
3357 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul);
3358 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3359 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul);
3360 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul);
3361 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3362 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul);
3363 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul);
3366 for (; j < n - 1; j+=2)
3368 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m+j))));
3370 v_src00 = v_load((int16_t*)(src[j]) + i);
3371 v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3372 v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3373 v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3374 v_src10 = v_load((int16_t*)(src[j+1]) + i);
3375 v_src11 = v_load((int16_t*)(src[j+1]) + i + 8);
3376 v_src12 = v_load((int16_t*)(src[j+1]) + i + 16);
3377 v_src13 = v_load((int16_t*)(src[j+1]) + i + 24);
3378 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3379 v_res0 += v_dotprod(v_tmp0, v_mul);
3380 v_res1 += v_dotprod(v_tmp1, v_mul);
3381 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3382 v_res2 += v_dotprod(v_tmp0, v_mul);
3383 v_res3 += v_dotprod(v_tmp1, v_mul);
3384 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3385 v_res4 += v_dotprod(v_tmp0, v_mul);
3386 v_res5 += v_dotprod(v_tmp1, v_mul);
3387 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3388 v_res6 += v_dotprod(v_tmp0, v_mul);
3389 v_res7 += v_dotprod(v_tmp1, v_mul);
3393 v_int32x4 v_resj0, v_resj1;
3394 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3395 v_src00 = v_load((int16_t*)(src[j]) + i);
3396 v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3397 v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3398 v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3399 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_resj0, v_resj1);
3402 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul, v_resj0, v_resj1);
3405 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul, v_resj0, v_resj1);
3408 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul, v_resj0, v_resj1);
3421 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3422 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3423 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3424 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3426 for (; i < len; i++)
3428 ufixedpoint32 val = m[0] * src[0][i];
3429 for (int j = 1; j < n; j++)
3431 val = val + m[j] * src[j][i];
3436 template <typename ET, typename FT>
3437 void vlineSmoothONa_yzy_a(const FT* const * src, const FT* m, int n, ET* dst, int len)
3439 int pre_shift = n / 2;
3440 for (int i = 0; i < len; i++)
3442 typename FT::WT val = m[pre_shift] * src[pre_shift][i];
3443 for (int j = 0; j < pre_shift; j++)
3444 val = val + m[j] * src[j][i] + m[j] * src[(n - 1 - j)][i];
3449 void vlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3451 int pre_shift = n / 2;
3453 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3454 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3457 ufixedpoint16 msum = m[0] + m[pre_shift] + m[n - 1];
3458 for (int j = 1; j < pre_shift; j++)
3459 msum = msum + m[j] + m[n - 1 - j];
3460 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3461 v_128_4 = v_setall_s32(*((int32_t*)val));
3463 for (; i <= len - 32; i += 32)
3465 v_int16x8 v_src00, v_src10, v_src20, v_src30, v_src01, v_src11, v_src21, v_src31;
3466 v_int32x4 v_res0, v_res1, v_res2, v_res3, v_res4, v_res5, v_res6, v_res7;
3467 v_int16x8 v_tmp0, v_tmp1, v_tmp2, v_tmp3, v_tmp4, v_tmp5, v_tmp6, v_tmp7;
3469 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + pre_shift))));
3470 v_src00 = v_load((int16_t*)(src[pre_shift]) + i);
3471 v_src10 = v_load((int16_t*)(src[pre_shift]) + i + 8);
3472 v_src20 = v_load((int16_t*)(src[pre_shift]) + i + 16);
3473 v_src30 = v_load((int16_t*)(src[pre_shift]) + i + 24);
3474 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_res0, v_res1);
3475 v_mul_expand(v_add_wrap(v_src10, v_128), v_mul, v_res2, v_res3);
3476 v_mul_expand(v_add_wrap(v_src20, v_128), v_mul, v_res4, v_res5);
3477 v_mul_expand(v_add_wrap(v_src30, v_128), v_mul, v_res6, v_res7);
3480 for (; j < pre_shift; j++)
3482 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3484 v_src00 = v_load((int16_t*)(src[j]) + i);
3485 v_src10 = v_load((int16_t*)(src[j]) + i + 8);
3486 v_src20 = v_load((int16_t*)(src[j]) + i + 16);
3487 v_src30 = v_load((int16_t*)(src[j]) + i + 24);
3488 v_src01 = v_load((int16_t*)(src[n - 1 - j]) + i);
3489 v_src11 = v_load((int16_t*)(src[n - 1 - j]) + i + 8);
3490 v_src21 = v_load((int16_t*)(src[n - 1 - j]) + i + 16);
3491 v_src31 = v_load((int16_t*)(src[n - 1 - j]) + i + 24);
3492 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src01, v_128), v_tmp0, v_tmp1);
3493 v_res0 += v_dotprod(v_tmp0, v_mul);
3494 v_res1 += v_dotprod(v_tmp1, v_mul);
3495 v_zip(v_add_wrap(v_src10, v_128), v_add_wrap(v_src11, v_128), v_tmp2, v_tmp3);
3496 v_res2 += v_dotprod(v_tmp2, v_mul);
3497 v_res3 += v_dotprod(v_tmp3, v_mul);
3498 v_zip(v_add_wrap(v_src20, v_128), v_add_wrap(v_src21, v_128), v_tmp4, v_tmp5);
3499 v_res4 += v_dotprod(v_tmp4, v_mul);
3500 v_res5 += v_dotprod(v_tmp5, v_mul);
3501 v_zip(v_add_wrap(v_src30, v_128), v_add_wrap(v_src31, v_128), v_tmp6, v_tmp7);
3502 v_res6 += v_dotprod(v_tmp6, v_mul);
3503 v_res7 += v_dotprod(v_tmp7, v_mul);
3515 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3516 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3517 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3518 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3520 for (; i < len; i++)
3522 ufixedpoint32 val = m[0] * src[0][i];
3523 for (int j = 1; j < n; j++)
3525 val = val + m[j] * src[j][i];
3530 template <typename ET, typename FT>
3531 class fixedSmoothInvoker : public ParallelLoopBody
3534 fixedSmoothInvoker(const ET* _src, size_t _src_stride, ET* _dst, size_t _dst_stride,
3535 int _width, int _height, int _cn, const FT* _kx, int _kxlen, const FT* _ky, int _kylen, int _borderType) : ParallelLoopBody(),
3536 src(_src), dst(_dst), src_stride(_src_stride), dst_stride(_dst_stride),
3537 width(_width), height(_height), cn(_cn), kx(_kx), ky(_ky), kxlen(_kxlen), kylen(_kylen), borderType(_borderType)
3541 if (kx[0] == FT::one())
3542 hlineSmoothFunc = hlineSmooth1N1;
3544 hlineSmoothFunc = hlineSmooth1N;
3546 else if (kxlen == 3)
3548 if (kx[0] == (FT::one()>>2)&&kx[1] == (FT::one()>>1)&&kx[2] == (FT::one()>>2))
3549 hlineSmoothFunc = hlineSmooth3N121;
3550 else if ((kx[0] - kx[2]).isZero())
3551 hlineSmoothFunc = hlineSmooth3Naba;
3553 hlineSmoothFunc = hlineSmooth3N;
3555 else if (kxlen == 5)
3557 if (kx[2] == (FT::one()*(uint8_t)3>>3) &&
3558 kx[1] == (FT::one()>>2) && kx[3] == (FT::one()>>2) &&
3559 kx[0] == (FT::one()>>4) && kx[4] == (FT::one()>>4))
3560 hlineSmoothFunc = hlineSmooth5N14641;
3561 else if (kx[0] == kx[4] && kx[1] == kx[3])
3562 hlineSmoothFunc = hlineSmooth5Nabcba;
3564 hlineSmoothFunc = hlineSmooth5N;
3566 else if (kxlen % 2 == 1)
3568 hlineSmoothFunc = hlineSmoothONa_yzy_a;
3569 for (int i = 0; i < kxlen / 2; i++)
3570 if (!(kx[i] == kx[kxlen - 1 - i]))
3572 hlineSmoothFunc = hlineSmooth;
3577 hlineSmoothFunc = hlineSmooth;
3580 if (ky[0] == FT::one())
3581 vlineSmoothFunc = vlineSmooth1N1;
3583 vlineSmoothFunc = vlineSmooth1N;
3585 else if (kylen == 3)
3587 if (ky[0] == (FT::one() >> 2) && ky[1] == (FT::one() >> 1) && ky[2] == (FT::one() >> 2))
3588 vlineSmoothFunc = vlineSmooth3N121;
3590 vlineSmoothFunc = vlineSmooth3N;
3592 else if (kylen == 5)
3594 if (ky[2] == (FT::one() * (uint8_t)3 >> 3) &&
3595 ky[1] == (FT::one() >> 2) && ky[3] == (FT::one() >> 2) &&
3596 ky[0] == (FT::one() >> 4) && ky[4] == (FT::one() >> 4))
3597 vlineSmoothFunc = vlineSmooth5N14641;
3599 vlineSmoothFunc = vlineSmooth5N;
3601 else if (kylen % 2 == 1)
3603 vlineSmoothFunc = vlineSmoothONa_yzy_a;
3604 for (int i = 0; i < kylen / 2; i++)
3605 if (!(ky[i] == ky[kylen - 1 - i]))
3607 vlineSmoothFunc = vlineSmooth;
3612 vlineSmoothFunc = vlineSmooth;
3614 virtual void operator() (const Range& range) const CV_OVERRIDE
3616 AutoBuffer<FT> _buf(width*cn*kylen);
3617 FT* buf = _buf.data();
3618 AutoBuffer<FT*> _ptrs(kylen*2);
3619 FT** ptrs = _ptrs.data();
3624 for (int i = range.start; i < range.end; i++)
3626 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[0], width, borderType);
3627 vlineSmoothFunc(ptrs, ky, kylen, dst + i * dst_stride, width*cn);
3630 else if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3632 int pre_shift = kylen / 2;
3633 int post_shift = kylen - pre_shift - 1;
3634 // First line evaluation
3635 int idst = range.start;
3636 int ifrom = max(0, idst - pre_shift);
3637 int ito = idst + post_shift + 1;
3640 for (; i < min(ito, height); i++, bufline++)
3642 ptrs[bufline+kylen] = ptrs[bufline] = buf + bufline * width*cn;
3643 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3645 for (; i < ito; i++, bufline++)
3647 int src_idx = borderInterpolate(i, height, borderType);
3648 if (src_idx < ifrom)
3650 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3651 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3655 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[src_idx - ifrom];
3658 for (int j = idst - pre_shift; j < 0; j++)
3660 int src_idx = borderInterpolate(j, height, borderType);
3663 ptrs[2*kylen + j] = ptrs[kylen + j] = buf + (kylen + j) * width*cn;
3664 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[kylen + j], width, borderType);
3668 ptrs[2*kylen + j] = ptrs[kylen + j] = ptrs[src_idx];
3671 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn); idst++;
3673 // border mode dependent part evaluation
3674 // i points to last src row to evaluate in convolution
3675 bufline %= kylen; ito = min(height, range.end + post_shift);
3676 for (; i < min(kylen, ito); i++, idst++)
3678 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3679 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3680 bufline = (bufline + 1) % kylen;
3681 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3683 // Points inside the border
3684 for (; i < ito; i++, idst++)
3686 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3687 bufline = (bufline + 1) % kylen;
3688 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3690 // Points that could fall below border
3691 for (; i < range.end + post_shift; i++, idst++)
3693 int src_idx = borderInterpolate(i, height, borderType);
3694 if ((i - src_idx) > kylen)
3695 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3697 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[(bufline + kylen - (i - src_idx)) % kylen];
3698 bufline = (bufline + 1) % kylen;
3699 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3704 int pre_shift = kylen / 2;
3705 int post_shift = kylen - pre_shift - 1;
3706 // First line evaluation
3707 int idst = range.start;
3708 int ifrom = idst - pre_shift;
3709 int ito = min(idst + post_shift + 1, height);
3710 int i = max(0, ifrom);
3712 for (; i < ito; i++, bufline++)
3714 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3715 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3719 vlineSmooth1N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3720 else if (bufline == 3)
3721 vlineSmooth3N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3722 else if (bufline == 5)
3723 vlineSmooth5N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3725 vlineSmooth(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3728 // border mode dependent part evaluation
3729 // i points to last src row to evaluate in convolution
3730 bufline %= kylen; ito = min(height, range.end + post_shift);
3731 for (; i < min(kylen, ito); i++, idst++)
3733 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3734 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3737 vlineSmooth3N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3738 else if (bufline == 5)
3739 vlineSmooth5N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3741 vlineSmooth(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3744 // Points inside the border
3745 if (i - max(0, ifrom) >= kylen)
3747 for (; i < ito; i++, idst++)
3749 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3750 bufline = (bufline + 1) % kylen;
3751 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3754 // Points that could fall below border
3755 // i points to first src row to evaluate in convolution
3756 bufline = (bufline + 1) % kylen;
3757 for (i = idst - pre_shift; i < range.end - pre_shift; i++, idst++, bufline++)
3758 if (height - i == 3)
3759 vlineSmooth3N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3760 else if (height - i == 5)
3761 vlineSmooth5N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3763 vlineSmooth(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3767 // i points to first src row to evaluate in convolution
3768 for (i = idst - pre_shift; i < min(range.end - pre_shift, 0); i++, idst++)
3770 vlineSmooth3N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3771 else if (height == 5)
3772 vlineSmooth5N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3774 vlineSmooth(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3775 for (; i < range.end - pre_shift; i++, idst++)
3776 if (height - i == 3)
3777 vlineSmooth3N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3778 else if (height - i == 5)
3779 vlineSmooth5N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3781 vlineSmooth(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3788 size_t src_stride, dst_stride;
3789 int width, height, cn;
3793 void(*hlineSmoothFunc)(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType);
3794 void(*vlineSmoothFunc)(const FT* const * src, const FT* m, int n, ET* dst, int len);
3796 fixedSmoothInvoker(const fixedSmoothInvoker&);
3797 fixedSmoothInvoker& operator=(const fixedSmoothInvoker&);
3800 static void getGaussianKernel(int n, double sigma, int ktype, Mat& res) { res = getGaussianKernel(n, sigma, ktype); }
3801 template <typename T> static void getGaussianKernel(int n, double sigma, int, std::vector<T>& res) { res = getFixedpointGaussianKernel<T>(n, sigma); }
3803 template <typename T>
3804 static void createGaussianKernels( T & kx, T & ky, int type, Size &ksize,
3805 double sigma1, double sigma2 )
3807 int depth = CV_MAT_DEPTH(type);
3811 // automatic detection of kernel size from sigma
3812 if( ksize.width <= 0 && sigma1 > 0 )
3813 ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3814 if( ksize.height <= 0 && sigma2 > 0 )
3815 ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3817 CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
3818 ksize.height > 0 && ksize.height % 2 == 1 );
3820 sigma1 = std::max( sigma1, 0. );
3821 sigma2 = std::max( sigma2, 0. );
3823 getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F), kx );
3824 if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
3827 getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F), ky );
3832 cv::Ptr<cv::FilterEngine> cv::createGaussianFilter( int type, Size ksize,
3833 double sigma1, double sigma2,
3837 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
3839 return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
3846 static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize, int ddepth,
3847 InputArray _kernelX, InputArray _kernelY, int borderType)
3849 const ocl::Device & dev = ocl::Device::getDefault();
3850 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
3852 if ( !(dev.isIntel() && (type == CV_8UC1) &&
3853 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
3854 ((ksize.width == 5 && (_src.cols() % 4 == 0)) ||
3855 (ksize.width == 3 && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)))) )
3858 Mat kernelX = _kernelX.getMat().reshape(1, 1);
3859 if (kernelX.cols % 2 != 1)
3861 Mat kernelY = _kernelY.getMat().reshape(1, 1);
3862 if (kernelY.cols % 2 != 1)
3868 Size size = _src.size();
3869 size_t globalsize[2] = { 0, 0 };
3870 size_t localsize[2] = { 0, 0 };
3872 if (ksize.width == 3)
3874 globalsize[0] = size.width / 16;
3875 globalsize[1] = size.height / 2;
3877 else if (ksize.width == 5)
3879 globalsize[0] = size.width / 4;
3880 globalsize[1] = size.height / 1;
3883 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
3884 char build_opts[1024];
3885 sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
3886 ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
3887 ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
3891 if (ksize.width == 3)
3892 kernel.create("gaussianBlur3x3_8UC1_cols16_rows2", cv::ocl::imgproc::gaussianBlur3x3_oclsrc, build_opts);
3893 else if (ksize.width == 5)
3894 kernel.create("gaussianBlur5x5_8UC1_cols4", cv::ocl::imgproc::gaussianBlur5x5_oclsrc, build_opts);
3899 UMat src = _src.getUMat();
3900 _dst.create(size, CV_MAKETYPE(ddepth, cn));
3901 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
3903 UMat dst = _dst.getUMat();
3905 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
3906 idxArg = kernel.set(idxArg, (int)src.step);
3907 idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
3908 idxArg = kernel.set(idxArg, (int)dst.step);
3909 idxArg = kernel.set(idxArg, (int)dst.rows);
3910 idxArg = kernel.set(idxArg, (int)dst.cols);
3912 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
3920 template <> inline bool skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(int w, int h) { return w*h < 320 * 240; }
3922 static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
3923 double sigma1, double sigma2, int borderType)
3927 // automatic detection of kernel size from sigma
3928 if (ksize.width <= 0 && sigma1 > 0)
3929 ksize.width = cvRound(sigma1*6 + 1) | 1;
3930 if (ksize.height <= 0 && sigma2 > 0)
3931 ksize.height = cvRound(sigma2*6 + 1) | 1;
3933 if (_src.type() != CV_8UC1 ||
3934 _src.cols() < 3 || _src.rows() < 3 ||
3935 ksize.width != 3 || ksize.height != 3)
3938 sigma1 = std::max(sigma1, 0.);
3939 sigma2 = std::max(sigma2, 0.);
3941 if (!(sigma1 == 0.0 || (sigma1 - 0.8) < DBL_EPSILON) || !(sigma2 == 0.0 || (sigma2 - 0.8) < DBL_EPSILON) ||
3942 ovx::skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(_src.cols(), _src.rows()))
3945 Mat src = _src.getMat();
3946 Mat dst = _dst.getMat();
3948 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
3949 return false; //Process isolated borders only
3951 switch (borderType & ~BORDER_ISOLATED)
3953 case BORDER_CONSTANT:
3954 border = VX_BORDER_CONSTANT;
3956 case BORDER_REPLICATE:
3957 border = VX_BORDER_REPLICATE;
3965 ivx::Context ctx = ovx::getOpenVXContext();
3968 if (dst.data != src.data)
3974 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3975 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
3976 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3977 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
3979 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
3980 //since OpenVX standard says nothing about thread-safety for now
3981 ivx::border_t prevBorder = ctx.immediateBorder();
3982 ctx.setImmediateBorder(border, (vx_uint8)(0));
3983 ivx::IVX_CHECK_STATUS(vxuGaussian3x3(ctx, ia, ib));
3984 ctx.setImmediateBorder(prevBorder);
3986 catch (ivx::RuntimeError & e)
3988 VX_DbgThrow(e.what());
3990 catch (ivx::WrapperError & e)
3992 VX_DbgThrow(e.what());
4000 // IW 2017u2 has bug which doesn't allow use of partial inMem with tiling
4001 #if IPP_DISABLE_GAUSSIANBLUR_PARALLEL
4002 #define IPP_GAUSSIANBLUR_PARALLEL 0
4004 #define IPP_GAUSSIANBLUR_PARALLEL 1
4009 class ipp_gaussianBlurParallel: public ParallelLoopBody
4012 ipp_gaussianBlurParallel(::ipp::IwiImage &src, ::ipp::IwiImage &dst, int kernelSize, float sigma, ::ipp::IwiBorderType &border, bool *pOk):
4013 m_src(src), m_dst(dst), m_kernelSize(kernelSize), m_sigma(sigma), m_border(border), m_pOk(pOk) {
4016 ~ipp_gaussianBlurParallel()
4020 virtual void operator() (const Range& range) const CV_OVERRIDE
4022 CV_INSTRUMENT_REGION_IPP()
4029 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, m_dst.m_size.width, range.end - range.start);
4030 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, m_src, m_dst, m_kernelSize, m_sigma, ::ipp::IwDefault(), m_border, tile);
4032 catch(::ipp::IwException e)
4039 ::ipp::IwiImage &m_src;
4040 ::ipp::IwiImage &m_dst;
4044 ::ipp::IwiBorderType &m_border;
4046 volatile bool *m_pOk;
4047 const ipp_gaussianBlurParallel& operator= (const ipp_gaussianBlurParallel&);
4052 static bool ipp_GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
4053 double sigma1, double sigma2, int borderType )
4056 CV_INSTRUMENT_REGION_IPP()
4058 #if IPP_VERSION_X100 < 201800 && ((defined _MSC_VER && defined _M_IX86) || (defined __GNUC__ && defined __i386__))
4059 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4060 return false; // bug on ia32
4062 if(sigma1 != sigma2)
4065 if(sigma1 < FLT_EPSILON)
4068 if(ksize.width != ksize.height)
4071 // Acquire data and begin processing
4074 Mat src = _src.getMat();
4075 Mat dst = _dst.getMat();
4076 ::ipp::IwiImage iwSrc = ippiGetImage(src);
4077 ::ipp::IwiImage iwDst = ippiGetImage(dst);
4078 ::ipp::IwiBorderSize borderSize = ::ipp::iwiSizeToBorderSize(ippiGetSize(ksize));
4079 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
4083 const int threads = ippiSuggestThreadsNum(iwDst, 2);
4084 if(IPP_GAUSSIANBLUR_PARALLEL && threads > 1) {
4086 ipp_gaussianBlurParallel invoker(iwSrc, iwDst, ksize.width, (float) sigma1, ippBorder, &ok);
4090 const Range range(0, (int) iwDst.m_size.height);
4091 parallel_for_(range, invoker, threads*4);
4096 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, iwSrc, iwDst, ksize.width, sigma1, ::ipp::IwDefault(), ippBorder);
4099 catch (::ipp::IwException ex)
4107 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4114 void cv::GaussianBlur( InputArray _src, OutputArray _dst, Size ksize,
4115 double sigma1, double sigma2,
4118 CV_INSTRUMENT_REGION()
4120 int type = _src.type();
4121 Size size = _src.size();
4122 _dst.create( size, type );
4124 if( (borderType & ~BORDER_ISOLATED) != BORDER_CONSTANT &&
4125 ((borderType & BORDER_ISOLATED) != 0 || !_src.getMat().isSubmatrix()) )
4127 if( size.height == 1 )
4129 if( size.width == 1 )
4133 if( ksize.width == 1 && ksize.height == 1 )
4139 bool useOpenCL = (ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
4140 ((ksize.width == 3 && ksize.height == 3) ||
4141 (ksize.width == 5 && ksize.height == 5)) &&
4142 _src.rows() > ksize.height && _src.cols() > ksize.width);
4145 int sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4147 if(sdepth == CV_8U && ((borderType & BORDER_ISOLATED) || !_src.getMat().isSubmatrix()))
4149 std::vector<ufixedpoint16> fkx, fky;
4150 createGaussianKernels(fkx, fky, type, ksize, sigma1, sigma2);
4151 Mat src = _src.getMat();
4152 Mat dst = _dst.getMat();
4153 if (src.data == dst.data)
4155 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);
4156 parallel_for_(Range(0, dst.rows), invoker, std::max(1, std::min(getNumThreads(), getNumberOfCPUs())));
4162 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
4164 CV_OCL_RUN(useOpenCL, ocl_GaussianBlur_8UC1(_src, _dst, ksize, CV_MAT_DEPTH(type), kx, ky, borderType));
4166 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
4167 ocl_sepFilter2D(_src, _dst, sdepth, kx, ky, Point(-1, -1), 0, borderType))
4169 Mat src = _src.getMat();
4170 Mat dst = _dst.getMat();
4173 Size wsz(src.cols, src.rows);
4174 if(!(borderType & BORDER_ISOLATED))
4175 src.locateROI( wsz, ofs );
4177 CALL_HAL(gaussianBlur, cv_hal_gaussianBlur, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, cn,
4178 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
4179 sigma1, sigma2, borderType&~BORDER_ISOLATED);
4182 openvx_gaussianBlur(src, dst, ksize, sigma1, sigma2, borderType))
4184 CV_IPP_RUN_FAST(ipp_GaussianBlur(src, dst, ksize, sigma1, sigma2, borderType));
4186 sepFilter2D(src, dst, sdepth, kx, ky, Point(-1, -1), 0, borderType);
4189 /****************************************************************************************\
4191 \****************************************************************************************/
4198 * This structure represents a two-tier histogram. The first tier (known as the
4199 * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
4200 * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
4201 * coarse bucket designated by the 4 MSBs of the fine bucket value.
4203 * The structure is aligned on 16 bits, which is a prerequisite for SIMD
4204 * instructions. Each bucket is 16 bit wide, which means that extra care must be
4205 * taken to prevent overflow.
4216 static inline void histogram_add_simd( const HT x[16], HT y[16] )
4218 v_store(y, v_load(x) + v_load(y));
4219 v_store(y + 8, v_load(x + 8) + v_load(y + 8));
4222 static inline void histogram_sub_simd( const HT x[16], HT y[16] )
4224 v_store(y, v_load(y) - v_load(x));
4225 v_store(y + 8, v_load(y + 8) - v_load(x + 8));
4231 static inline void histogram_add( const HT x[16], HT y[16] )
4234 for( i = 0; i < 16; ++i )
4235 y[i] = (HT)(y[i] + x[i]);
4238 static inline void histogram_sub( const HT x[16], HT y[16] )
4241 for( i = 0; i < 16; ++i )
4242 y[i] = (HT)(y[i] - x[i]);
4245 static inline void histogram_muladd( int a, const HT x[16],
4248 for( int i = 0; i < 16; ++i )
4249 y[i] = (HT)(y[i] + a * x[i]);
4253 medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
4256 * HOP is short for Histogram OPeration. This macro makes an operation \a op on
4257 * histogram \a h for pixel value \a x. It takes care of handling both levels.
4259 #define HOP(h,x,op) \
4260 h.coarse[x>>4] op, \
4261 *((HT*)h.fine + x) op
4263 #define COP(c,j,x,op) \
4264 h_coarse[ 16*(n*c+j) + (x>>4) ] op, \
4265 h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op
4267 int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2;
4268 CV_Assert(cn > 0 && cn <= 4);
4269 size_t sstep = _src.step, dstep = _dst.step;
4270 Histogram CV_DECL_ALIGNED(16) H[4];
4271 HT CV_DECL_ALIGNED(16) luc[4][16];
4273 int STRIPE_SIZE = std::min( _dst.cols, 512/cn );
4275 std::vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4276 std::vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4277 HT* h_coarse = alignPtr(&_h_coarse[0], 16);
4278 HT* h_fine = alignPtr(&_h_fine[0], 16);
4280 volatile bool useSIMD = hasSIMD128();
4283 for( int x = 0; x < _dst.cols; x += STRIPE_SIZE )
4285 int i, j, k, c, n = std::min(_dst.cols - x, STRIPE_SIZE) + r*2;
4286 const uchar* src = _src.ptr() + x*cn;
4287 uchar* dst = _dst.ptr() + (x - r)*cn;
4289 memset( h_coarse, 0, 16*n*cn*sizeof(h_coarse[0]) );
4290 memset( h_fine, 0, 16*16*n*cn*sizeof(h_fine[0]) );
4292 // First row initialization
4293 for( c = 0; c < cn; c++ )
4295 for( j = 0; j < n; j++ )
4296 COP( c, j, src[cn*j+c], += (cv::HT)(r+2) );
4298 for( i = 1; i < r; i++ )
4300 const uchar* p = src + sstep*std::min(i, m-1);
4301 for ( j = 0; j < n; j++ )
4302 COP( c, j, p[cn*j+c], ++ );
4306 for( i = 0; i < m; i++ )
4308 const uchar* p0 = src + sstep * std::max( 0, i-r-1 );
4309 const uchar* p1 = src + sstep * std::min( m-1, i+r );
4311 memset( H, 0, cn*sizeof(H[0]) );
4312 memset( luc, 0, cn*sizeof(luc[0]) );
4313 for( c = 0; c < cn; c++ )
4315 // Update column histograms for the entire row.
4316 for( j = 0; j < n; j++ )
4318 COP( c, j, p0[j*cn + c], -- );
4319 COP( c, j, p1[j*cn + c], ++ );
4322 // First column initialization
4323 for( k = 0; k < 16; ++k )
4324 histogram_muladd( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].fine[k][0] );
4329 for( j = 0; j < 2*r; ++j )
4330 histogram_add_simd( &h_coarse[16*(n*c+j)], H[c].coarse );
4332 for( j = r; j < n-r; j++ )
4334 int t = 2*r*r + 2*r, b, sum = 0;
4337 histogram_add_simd( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4339 // Find median at coarse level
4340 for ( k = 0; k < 16 ; ++k )
4342 sum += H[c].coarse[k];
4345 sum -= H[c].coarse[k];
4349 CV_Assert( k < 16 );
4351 /* Update corresponding histogram segment */
4352 if ( luc[c][k] <= j-r )
4354 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4355 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4356 histogram_add_simd( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4358 if ( luc[c][k] < j+r+1 )
4360 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4361 luc[c][k] = (HT)(j+r+1);
4366 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4368 histogram_sub_simd( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4369 histogram_add_simd( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4373 histogram_sub_simd( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4375 /* Find median in segment */
4376 segment = H[c].fine[k];
4377 for ( b = 0; b < 16 ; b++ )
4382 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4386 CV_Assert( b < 16 );
4392 for( j = 0; j < 2*r; ++j )
4393 histogram_add( &h_coarse[16*(n*c+j)], H[c].coarse );
4395 for( j = r; j < n-r; j++ )
4397 int t = 2*r*r + 2*r, b, sum = 0;
4400 histogram_add( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4402 // Find median at coarse level
4403 for ( k = 0; k < 16 ; ++k )
4405 sum += H[c].coarse[k];
4408 sum -= H[c].coarse[k];
4412 CV_Assert( k < 16 );
4414 /* Update corresponding histogram segment */
4415 if ( luc[c][k] <= j-r )
4417 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4418 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4419 histogram_add( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4421 if ( luc[c][k] < j+r+1 )
4423 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4424 luc[c][k] = (HT)(j+r+1);
4429 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4431 histogram_sub( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4432 histogram_add( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4436 histogram_sub( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4438 /* Find median in segment */
4439 segment = H[c].fine[k];
4440 for ( b = 0; b < 16 ; b++ )
4445 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4449 CV_Assert( b < 16 );
4461 medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m )
4468 Size size = _dst.size();
4469 const uchar* src = _src.ptr();
4470 uchar* dst = _dst.ptr();
4471 int src_step = (int)_src.step, dst_step = (int)_dst.step;
4472 int cn = _src.channels();
4473 const uchar* src_max = src + size.height*src_step;
4474 CV_Assert(cn > 0 && cn <= 4);
4476 #define UPDATE_ACC01( pix, cn, op ) \
4480 zone0[cn][p >> 4] op; \
4483 //CV_Assert( size.height >= nx && size.width >= nx );
4484 for( x = 0; x < size.width; x++, src += cn, dst += cn )
4486 uchar* dst_cur = dst;
4487 const uchar* src_top = src;
4488 const uchar* src_bottom = src;
4490 int src_step1 = src_step, dst_step1 = dst_step;
4494 src_bottom = src_top += src_step*(size.height-1);
4495 dst_cur += dst_step*(size.height-1);
4496 src_step1 = -src_step1;
4497 dst_step1 = -dst_step1;
4501 memset( zone0, 0, sizeof(zone0[0])*cn );
4502 memset( zone1, 0, sizeof(zone1[0])*cn );
4504 for( y = 0; y <= m/2; y++ )
4506 for( c = 0; c < cn; c++ )
4510 for( k = 0; k < m*cn; k += cn )
4511 UPDATE_ACC01( src_bottom[k+c], c, ++ );
4515 for( k = 0; k < m*cn; k += cn )
4516 UPDATE_ACC01( src_bottom[k+c], c, += m/2+1 );
4520 if( (src_step1 > 0 && y < size.height-1) ||
4521 (src_step1 < 0 && size.height-y-1 > 0) )
4522 src_bottom += src_step1;
4525 for( y = 0; y < size.height; y++, dst_cur += dst_step1 )
4528 for( c = 0; c < cn; c++ )
4533 int t = s + zone0[c][k];
4544 dst_cur[c] = (uchar)k;
4547 if( y+1 == size.height )
4552 for( k = 0; k < m; k++ )
4555 int q = src_bottom[k];
4564 for( k = 0; k < m*3; k += 3 )
4566 UPDATE_ACC01( src_top[k], 0, -- );
4567 UPDATE_ACC01( src_top[k+1], 1, -- );
4568 UPDATE_ACC01( src_top[k+2], 2, -- );
4570 UPDATE_ACC01( src_bottom[k], 0, ++ );
4571 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4572 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4578 for( k = 0; k < m*4; k += 4 )
4580 UPDATE_ACC01( src_top[k], 0, -- );
4581 UPDATE_ACC01( src_top[k+1], 1, -- );
4582 UPDATE_ACC01( src_top[k+2], 2, -- );
4583 UPDATE_ACC01( src_top[k+3], 3, -- );
4585 UPDATE_ACC01( src_bottom[k], 0, ++ );
4586 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4587 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4588 UPDATE_ACC01( src_bottom[k+3], 3, ++ );
4592 if( (src_step1 > 0 && src_bottom + src_step1 < src_max) ||
4593 (src_step1 < 0 && src_bottom + src_step1 >= src) )
4594 src_bottom += src_step1;
4597 src_top += src_step1;
4607 typedef uchar value_type;
4608 typedef int arg_type;
4610 arg_type load(const uchar* ptr) { return *ptr; }
4611 void store(uchar* ptr, arg_type val) { *ptr = (uchar)val; }
4612 void operator()(arg_type& a, arg_type& b) const
4614 int t = CV_FAST_CAST_8U(a - b);
4621 typedef ushort value_type;
4622 typedef int arg_type;
4624 arg_type load(const ushort* ptr) { return *ptr; }
4625 void store(ushort* ptr, arg_type val) { *ptr = (ushort)val; }
4626 void operator()(arg_type& a, arg_type& b) const
4636 typedef short value_type;
4637 typedef int arg_type;
4639 arg_type load(const short* ptr) { return *ptr; }
4640 void store(short* ptr, arg_type val) { *ptr = (short)val; }
4641 void operator()(arg_type& a, arg_type& b) const
4651 typedef float value_type;
4652 typedef float arg_type;
4654 arg_type load(const float* ptr) { return *ptr; }
4655 void store(float* ptr, arg_type val) { *ptr = val; }
4656 void operator()(arg_type& a, arg_type& b) const
4668 typedef uchar value_type;
4669 typedef v_uint8x16 arg_type;
4671 arg_type load(const uchar* ptr) { return v_load(ptr); }
4672 void store(uchar* ptr, const arg_type &val) { v_store(ptr, val); }
4673 void operator()(arg_type& a, arg_type& b) const
4684 typedef ushort value_type;
4685 typedef v_uint16x8 arg_type;
4687 arg_type load(const ushort* ptr) { return v_load(ptr); }
4688 void store(ushort* ptr, const arg_type &val) { v_store(ptr, val); }
4689 void operator()(arg_type& a, arg_type& b) const
4700 typedef short value_type;
4701 typedef v_int16x8 arg_type;
4703 arg_type load(const short* ptr) { return v_load(ptr); }
4704 void store(short* ptr, const arg_type &val) { v_store(ptr, val); }
4705 void operator()(arg_type& a, arg_type& b) const
4716 typedef float value_type;
4717 typedef v_float32x4 arg_type;
4719 arg_type load(const float* ptr) { return v_load(ptr); }
4720 void store(float* ptr, const arg_type &val) { v_store(ptr, val); }
4721 void operator()(arg_type& a, arg_type& b) const
4731 typedef MinMax8u MinMaxVec8u;
4732 typedef MinMax16u MinMaxVec16u;
4733 typedef MinMax16s MinMaxVec16s;
4734 typedef MinMax32f MinMaxVec32f;
4738 template<class Op, class VecOp>
4740 medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
4742 typedef typename Op::value_type T;
4743 typedef typename Op::arg_type WT;
4744 typedef typename VecOp::arg_type VT;
4746 const T* src = _src.ptr<T>();
4747 T* dst = _dst.ptr<T>();
4748 int sstep = (int)(_src.step/sizeof(T));
4749 int dstep = (int)(_dst.step/sizeof(T));
4750 Size size = _dst.size();
4751 int i, j, k, cn = _src.channels();
4754 volatile bool useSIMD = hasSIMD128();
4758 if( size.width == 1 || size.height == 1 )
4760 int len = size.width + size.height - 1;
4761 int sdelta = size.height == 1 ? cn : sstep;
4762 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4763 int ddelta = size.height == 1 ? cn : dstep;
4765 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4766 for( j = 0; j < cn; j++, src++ )
4768 WT p0 = src[i > 0 ? -sdelta : 0];
4770 WT p2 = src[i < len - 1 ? sdelta : 0];
4772 op(p0, p1); op(p1, p2); op(p0, p1);
4779 for( i = 0; i < size.height; i++, dst += dstep )
4781 const T* row0 = src + std::max(i - 1, 0)*sstep;
4782 const T* row1 = src + i*sstep;
4783 const T* row2 = src + std::min(i + 1, size.height-1)*sstep;
4784 int limit = useSIMD ? cn : size.width;
4788 for( ; j < limit; j++ )
4790 int j0 = j >= cn ? j - cn : j;
4791 int j2 = j < size.width - cn ? j + cn : j;
4792 WT p0 = row0[j0], p1 = row0[j], p2 = row0[j2];
4793 WT p3 = row1[j0], p4 = row1[j], p5 = row1[j2];
4794 WT p6 = row2[j0], p7 = row2[j], p8 = row2[j2];
4796 op(p1, p2); op(p4, p5); op(p7, p8); op(p0, p1);
4797 op(p3, p4); op(p6, p7); op(p1, p2); op(p4, p5);
4798 op(p7, p8); op(p0, p3); op(p5, p8); op(p4, p7);
4799 op(p3, p6); op(p1, p4); op(p2, p5); op(p4, p7);
4800 op(p4, p2); op(p6, p4); op(p4, p2);
4804 if( limit == size.width )
4807 for( ; j <= size.width - VecOp::SIZE - cn; j += VecOp::SIZE )
4809 VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
4810 VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
4811 VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);
4813 vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
4814 vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
4815 vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
4816 vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
4817 vop(p4, p2); vop(p6, p4); vop(p4, p2);
4818 vop.store(dst+j, p4);
4827 if( size.width == 1 || size.height == 1 )
4829 int len = size.width + size.height - 1;
4830 int sdelta = size.height == 1 ? cn : sstep;
4831 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4832 int ddelta = size.height == 1 ? cn : dstep;
4834 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4835 for( j = 0; j < cn; j++, src++ )
4837 int i1 = i > 0 ? -sdelta : 0;
4838 int i0 = i > 1 ? -sdelta*2 : i1;
4839 int i3 = i < len-1 ? sdelta : 0;
4840 int i4 = i < len-2 ? sdelta*2 : i3;
4841 WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];
4843 op(p0, p1); op(p3, p4); op(p2, p3); op(p3, p4); op(p0, p2);
4844 op(p2, p4); op(p1, p3); op(p1, p2);
4851 for( i = 0; i < size.height; i++, dst += dstep )
4854 row[0] = src + std::max(i - 2, 0)*sstep;
4855 row[1] = src + std::max(i - 1, 0)*sstep;
4856 row[2] = src + i*sstep;
4857 row[3] = src + std::min(i + 1, size.height-1)*sstep;
4858 row[4] = src + std::min(i + 2, size.height-1)*sstep;
4859 int limit = useSIMD ? cn*2 : size.width;
4863 for( ; j < limit; j++ )
4866 int j1 = j >= cn ? j - cn : j;
4867 int j0 = j >= cn*2 ? j - cn*2 : j1;
4868 int j3 = j < size.width - cn ? j + cn : j;
4869 int j4 = j < size.width - cn*2 ? j + cn*2 : j3;
4870 for( k = 0; k < 5; k++ )
4872 const T* rowk = row[k];
4873 p[k*5] = rowk[j0]; p[k*5+1] = rowk[j1];
4874 p[k*5+2] = rowk[j]; p[k*5+3] = rowk[j3];
4875 p[k*5+4] = rowk[j4];
4878 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]);
4879 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]);
4880 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]);
4881 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]);
4882 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]);
4883 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]);
4884 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]);
4885 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]);
4886 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]);
4887 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]);
4888 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]);
4889 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]);
4890 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]);
4891 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]);
4892 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]);
4893 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]);
4894 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]);
4895 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]);
4896 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]);
4897 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]);
4898 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]);
4899 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]);
4900 op(p[7], p[11]); op(p[11], p[13]); op(p[11], p[12]);
4904 if( limit == size.width )
4907 for( ; j <= size.width - VecOp::SIZE - cn*2; j += VecOp::SIZE )
4910 for( k = 0; k < 5; k++ )
4912 const T* rowk = row[k];
4913 p[k*5] = vop.load(rowk+j-cn*2); p[k*5+1] = vop.load(rowk+j-cn);
4914 p[k*5+2] = vop.load(rowk+j); p[k*5+3] = vop.load(rowk+j+cn);
4915 p[k*5+4] = vop.load(rowk+j+cn*2);
4918 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]);
4919 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]);
4920 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]);
4921 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]);
4922 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]);
4923 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]);
4924 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]);
4925 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]);
4926 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]);
4927 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]);
4928 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]);
4929 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]);
4930 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]);
4931 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]);
4932 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]);
4933 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]);
4934 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]);
4935 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]);
4936 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]);
4937 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]);
4938 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]);
4939 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]);
4940 vop(p[7], p[11]); vop(p[11], p[13]); vop(p[11], p[12]);
4941 vop.store(dst+j, p[12]);
4952 static bool ocl_medianFilter(InputArray _src, OutputArray _dst, int m)
4954 size_t localsize[2] = { 16, 16 };
4955 size_t globalsize[2];
4956 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4958 if ( !((depth == CV_8U || depth == CV_16U || depth == CV_16S || depth == CV_32F) && cn <= 4 && (m == 3 || m == 5)) )
4961 Size imgSize = _src.size();
4962 bool useOptimized = (1 == cn) &&
4963 (size_t)imgSize.width >= localsize[0] * 8 &&
4964 (size_t)imgSize.height >= localsize[1] * 8 &&
4965 imgSize.width % 4 == 0 &&
4966 imgSize.height % 4 == 0 &&
4967 (ocl::Device::getDefault().isIntel());
4969 cv::String kname = format( useOptimized ? "medianFilter%d_u" : "medianFilter%d", m) ;
4970 cv::String kdefs = useOptimized ?
4971 format("-D T=%s -D T1=%s -D T4=%s%d -D cn=%d -D USE_4OPT", ocl::typeToStr(type),
4972 ocl::typeToStr(depth), ocl::typeToStr(depth), cn*4, cn)
4974 format("-D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn) ;
4976 ocl::Kernel k(kname.c_str(), ocl::imgproc::medianFilter_oclsrc, kdefs.c_str() );
4981 UMat src = _src.getUMat();
4982 _dst.create(src.size(), type);
4983 UMat dst = _dst.getUMat();
4985 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst));
4989 globalsize[0] = DIVUP(src.cols / 4, localsize[0]) * localsize[0];
4990 globalsize[1] = DIVUP(src.rows / 4, localsize[1]) * localsize[1];
4994 globalsize[0] = (src.cols + localsize[0] + 2) / localsize[0] * localsize[0];
4995 globalsize[1] = (src.rows + localsize[1] - 1) / localsize[1] * localsize[1];
4998 return k.run(2, globalsize, localsize, false);
5009 template <> inline bool skipSmallImages<VX_KERNEL_MEDIAN_3x3>(int w, int h) { return w*h < 1280 * 720; }
5011 static bool openvx_medianFilter(InputArray _src, OutputArray _dst, int ksize)
5013 if (_src.type() != CV_8UC1 || _dst.type() != CV_8U
5014 #ifndef VX_VERSION_1_1
5020 Mat src = _src.getMat();
5021 Mat dst = _dst.getMat();
5024 #ifdef VX_VERSION_1_1
5025 ksize != 3 ? ovx::skipSmallImages<VX_KERNEL_NON_LINEAR_FILTER>(src.cols, src.rows) :
5027 ovx::skipSmallImages<VX_KERNEL_MEDIAN_3x3>(src.cols, src.rows)
5033 ivx::Context ctx = ovx::getOpenVXContext();
5034 #ifdef VX_VERSION_1_1
5035 if ((vx_size)ksize > ctx.nonlinearMaxDimension())
5040 if (dst.data != src.data)
5046 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5047 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
5048 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5049 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
5051 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
5052 //since OpenVX standard says nothing about thread-safety for now
5053 ivx::border_t prevBorder = ctx.immediateBorder();
5054 ctx.setImmediateBorder(VX_BORDER_REPLICATE);
5055 #ifdef VX_VERSION_1_1
5059 ivx::IVX_CHECK_STATUS(vxuMedian3x3(ctx, ia, ib));
5061 #ifdef VX_VERSION_1_1
5066 mtx = ivx::Matrix::createFromPattern(ctx, VX_PATTERN_BOX, ksize, ksize);
5069 vx_size supportedSize;
5070 ivx::IVX_CHECK_STATUS(vxQueryContext(ctx, VX_CONTEXT_NONLINEAR_MAX_DIMENSION, &supportedSize, sizeof(supportedSize)));
5071 if ((vx_size)ksize > supportedSize)
5073 ctx.setImmediateBorder(prevBorder);
5076 Mat mask(ksize, ksize, CV_8UC1, Scalar(255));
5077 mtx = ivx::Matrix::create(ctx, VX_TYPE_UINT8, ksize, ksize);
5080 ivx::IVX_CHECK_STATUS(vxuNonLinearFilter(ctx, VX_NONLINEAR_FILTER_MEDIAN, ia, mtx, ib));
5083 ctx.setImmediateBorder(prevBorder);
5085 catch (ivx::RuntimeError & e)
5087 VX_DbgThrow(e.what());
5089 catch (ivx::WrapperError & e)
5091 VX_DbgThrow(e.what());
5102 static bool ipp_medianFilter(Mat &src0, Mat &dst, int ksize)
5104 CV_INSTRUMENT_REGION_IPP()
5106 #if IPP_VERSION_X100 < 201801
5107 // Degradations for big kernel
5114 IppiSize dstRoiSize = ippiSize(dst.cols, dst.rows), maskSize = ippiSize(ksize, ksize);
5115 IppDataType ippType = ippiGetDataType(src0.type());
5116 int channels = src0.channels();
5117 IppAutoBuffer<Ipp8u> buffer;
5119 if(src0.isSubmatrix())
5123 if(dst.data != src0.data)
5128 if(ippiFilterMedianBorderGetBufferSize(dstRoiSize, maskSize, ippType, channels, &bufSize) < 0)
5131 buffer.allocate(bufSize);
5137 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;
5138 else if(channels == 3)
5139 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;
5140 else if(channels == 4)
5141 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;
5146 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;
5147 else if(channels == 3)
5148 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;
5149 else if(channels == 4)
5150 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;
5155 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;
5156 else if(channels == 3)
5157 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;
5158 else if(channels == 4)
5159 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;
5164 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;
5175 void cv::medianBlur( InputArray _src0, OutputArray _dst, int ksize )
5177 CV_INSTRUMENT_REGION()
5179 CV_Assert( (ksize % 2 == 1) && (_src0.dims() <= 2 ));
5181 if( ksize <= 1 || _src0.empty() )
5187 CV_OCL_RUN(_dst.isUMat(),
5188 ocl_medianFilter(_src0,_dst, ksize))
5190 Mat src0 = _src0.getMat();
5191 _dst.create( src0.size(), src0.type() );
5192 Mat dst = _dst.getMat();
5194 CALL_HAL(medianBlur, cv_hal_medianBlur, src0.data, src0.step, dst.data, dst.step, src0.cols, src0.rows, src0.depth(),
5195 src0.channels(), ksize);
5198 openvx_medianFilter(_src0, _dst, ksize))
5200 CV_IPP_RUN_FAST(ipp_medianFilter(src0, dst, ksize));
5202 bool useSortNet = ksize == 3 || (ksize == 5
5204 && ( src0.depth() > CV_8U || src0.channels() == 2 || src0.channels() > 4 )
5211 if( dst.data != src0.data )
5216 if( src.depth() == CV_8U )
5217 medianBlur_SortNet<MinMax8u, MinMaxVec8u>( src, dst, ksize );
5218 else if( src.depth() == CV_16U )
5219 medianBlur_SortNet<MinMax16u, MinMaxVec16u>( src, dst, ksize );
5220 else if( src.depth() == CV_16S )
5221 medianBlur_SortNet<MinMax16s, MinMaxVec16s>( src, dst, ksize );
5222 else if( src.depth() == CV_32F )
5223 medianBlur_SortNet<MinMax32f, MinMaxVec32f>( src, dst, ksize );
5225 CV_Error(CV_StsUnsupportedFormat, "");
5231 cv::copyMakeBorder( src0, src, 0, 0, ksize/2, ksize/2, BORDER_REPLICATE|BORDER_ISOLATED);
5233 int cn = src0.channels();
5234 CV_Assert( src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4) );
5236 double img_size_mp = (double)(src0.total())/(1 << 20);
5237 if( ksize <= 3 + (img_size_mp < 1 ? 12 : img_size_mp < 4 ? 6 : 2)*
5238 (CV_SIMD128 && hasSIMD128() ? 1 : 3))
5239 medianBlur_8u_Om( src, dst, ksize );
5241 medianBlur_8u_O1( src, dst, ksize );
5245 /****************************************************************************************\
5247 \****************************************************************************************/
5252 class BilateralFilter_8u_Invoker :
5253 public ParallelLoopBody
5256 BilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, int _radius, int _maxk,
5257 int* _space_ofs, float *_space_weight, float *_color_weight) :
5258 temp(&_temp), dest(&_dest), radius(_radius),
5259 maxk(_maxk), space_ofs(_space_ofs), space_weight(_space_weight), color_weight(_color_weight)
5263 virtual void operator() (const Range& range) const CV_OVERRIDE
5265 int i, j, cn = dest->channels(), k;
5266 Size size = dest->size();
5268 int CV_DECL_ALIGNED(16) buf[4];
5269 bool haveSIMD128 = hasSIMD128();
5272 for( i = range.start; i < range.end; i++ )
5274 const uchar* sptr = temp->ptr(i+radius) + radius*cn;
5275 uchar* dptr = dest->ptr(i);
5279 for( j = 0; j < size.width; j++ )
5281 float sum = 0, wsum = 0;
5287 v_float32x4 _val0 = v_setall_f32(static_cast<float>(val0));
5288 v_float32x4 vsumw = v_setzero_f32();
5289 v_float32x4 vsumc = v_setzero_f32();
5291 for( ; k <= maxk - 4; k += 4 )
5293 v_float32x4 _valF = v_float32x4(sptr[j + space_ofs[k]],
5294 sptr[j + space_ofs[k + 1]],
5295 sptr[j + space_ofs[k + 2]],
5296 sptr[j + space_ofs[k + 3]]);
5297 v_float32x4 _val = v_abs(_valF - _val0);
5298 v_store(buf, v_round(_val));
5300 v_float32x4 _cw = v_float32x4(color_weight[buf[0]],
5301 color_weight[buf[1]],
5302 color_weight[buf[2]],
5303 color_weight[buf[3]]);
5304 v_float32x4 _sw = v_load(space_weight+k);
5305 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5306 // details: https://github.com/opencv/opencv/issues/11004
5308 vsumc += _cw * _sw * _valF;
5310 v_float32x4 _w = _cw * _sw;
5317 float *bufFloat = (float*)buf;
5318 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumc, vsumw, vsumc);
5319 v_store(bufFloat, sum4);
5321 wsum += bufFloat[0];
5324 for( ; k < maxk; k++ )
5326 int val = sptr[j + space_ofs[k]];
5327 float w = space_weight[k]*color_weight[std::abs(val - val0)];
5331 // overflow is not possible here => there is no need to use cv::saturate_cast
5332 dptr[j] = (uchar)cvRound(sum/wsum);
5338 for( j = 0; j < size.width*3; j += 3 )
5340 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5341 int b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5346 v_float32x4 vsumw = v_setzero_f32();
5347 v_float32x4 vsumb = v_setzero_f32();
5348 v_float32x4 vsumg = v_setzero_f32();
5349 v_float32x4 vsumr = v_setzero_f32();
5350 const v_float32x4 _b0 = v_setall_f32(static_cast<float>(b0));
5351 const v_float32x4 _g0 = v_setall_f32(static_cast<float>(g0));
5352 const v_float32x4 _r0 = v_setall_f32(static_cast<float>(r0));
5354 for( ; k <= maxk - 4; k += 4 )
5356 const uchar* const sptr_k0 = sptr + j + space_ofs[k];
5357 const uchar* const sptr_k1 = sptr + j + space_ofs[k+1];
5358 const uchar* const sptr_k2 = sptr + j + space_ofs[k+2];
5359 const uchar* const sptr_k3 = sptr + j + space_ofs[k+3];
5361 v_float32x4 __b = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k0)));
5362 v_float32x4 __g = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k1)));
5363 v_float32x4 __r = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k2)));
5364 v_float32x4 __z = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k3)));
5365 v_float32x4 _b, _g, _r, _z;
5367 v_transpose4x4(__b, __g, __r, __z, _b, _g, _r, _z);
5369 v_float32x4 bt = v_abs(_b -_b0);
5370 v_float32x4 gt = v_abs(_g -_g0);
5371 v_float32x4 rt = v_abs(_r -_r0);
5374 v_store(buf, v_round(bt));
5376 v_float32x4 _w = v_float32x4(color_weight[buf[0]],color_weight[buf[1]],
5377 color_weight[buf[2]],color_weight[buf[3]]);
5378 v_float32x4 _sw = v_load(space_weight+k);
5380 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5381 // details: https://github.com/opencv/opencv/issues/11004
5383 vsumb += _w * _sw * _b;
5384 vsumg += _w * _sw * _g;
5385 vsumr += _w * _sw * _r;
5398 float *bufFloat = (float*)buf;
5399 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumb, vsumg, vsumr);
5400 v_store(bufFloat, sum4);
5401 wsum += bufFloat[0];
5402 sum_b += bufFloat[1];
5403 sum_g += bufFloat[2];
5404 sum_r += bufFloat[3];
5408 for( ; k < maxk; k++ )
5410 const uchar* sptr_k = sptr + j + space_ofs[k];
5411 int b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5412 float w = space_weight[k]*color_weight[std::abs(b - b0) +
5413 std::abs(g - g0) + std::abs(r - r0)];
5414 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5418 b0 = cvRound(sum_b*wsum);
5419 g0 = cvRound(sum_g*wsum);
5420 r0 = cvRound(sum_r*wsum);
5421 dptr[j] = (uchar)b0; dptr[j+1] = (uchar)g0; dptr[j+2] = (uchar)r0;
5430 int radius, maxk, *space_ofs;
5431 float *space_weight, *color_weight;
5436 static bool ocl_bilateralFilter_8u(InputArray _src, OutputArray _dst, int d,
5437 double sigma_color, double sigma_space,
5441 if (ocl::Device::getDefault().isNVidia())
5445 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
5446 int i, j, maxk, radius;
5448 if (depth != CV_8U || cn > 4)
5451 if (sigma_color <= 0)
5453 if (sigma_space <= 0)
5456 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
5457 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
5460 radius = cvRound(sigma_space * 1.5);
5463 radius = MAX(radius, 1);
5466 UMat src = _src.getUMat(), dst = _dst.getUMat(), temp;
5470 copyMakeBorder(src, temp, radius, radius, radius, radius, borderType);
5471 std::vector<float> _space_weight(d * d);
5472 std::vector<int> _space_ofs(d * d);
5473 float * const space_weight = &_space_weight[0];
5474 int * const space_ofs = &_space_ofs[0];
5476 // initialize space-related bilateral filter coefficients
5477 for( i = -radius, maxk = 0; i <= radius; i++ )
5478 for( j = -radius; j <= radius; j++ )
5480 double r = std::sqrt((double)i * i + (double)j * j);
5483 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
5484 space_ofs[maxk++] = (int)(i * temp.step + j * cn);
5488 String cnstr = cn > 1 ? format("%d", cn) : "";
5489 String kernelName("bilateral");
5491 if ((ocl::Device::getDefault().isIntel()) &&
5492 (ocl::Device::getDefault().type() == ocl::Device::TYPE_GPU))
5495 if (dst.cols % 4 == 0 && cn == 1) // For single channel x4 sized images.
5497 kernelName = "bilateral_float4";
5501 ocl::Kernel k(kernelName.c_str(), ocl::imgproc::bilateral_oclsrc,
5502 format("-D radius=%d -D maxk=%d -D cn=%d -D int_t=%s -D uint_t=uint%s -D convert_int_t=%s"
5503 " -D uchar_t=%s -D float_t=%s -D convert_float_t=%s -D convert_uchar_t=%s -D gauss_color_coeff=(float)%f",
5504 radius, maxk, cn, ocl::typeToStr(CV_32SC(cn)), cnstr.c_str(),
5505 ocl::convertTypeStr(CV_8U, CV_32S, cn, cvt[0]),
5506 ocl::typeToStr(type), ocl::typeToStr(CV_32FC(cn)),
5507 ocl::convertTypeStr(CV_32S, CV_32F, cn, cvt[1]),
5508 ocl::convertTypeStr(CV_32F, CV_8U, cn, cvt[2]), gauss_color_coeff));
5512 Mat mspace_weight(1, d * d, CV_32FC1, space_weight);
5513 Mat mspace_ofs(1, d * d, CV_32SC1, space_ofs);
5514 UMat ucolor_weight, uspace_weight, uspace_ofs;
5516 mspace_weight.copyTo(uspace_weight);
5517 mspace_ofs.copyTo(uspace_ofs);
5519 k.args(ocl::KernelArg::ReadOnlyNoSize(temp), ocl::KernelArg::WriteOnly(dst),
5520 ocl::KernelArg::PtrReadOnly(uspace_weight),
5521 ocl::KernelArg::PtrReadOnly(uspace_ofs));
5523 size_t globalsize[2] = { (size_t)dst.cols / sizeDiv, (size_t)dst.rows };
5524 return k.run(2, globalsize, NULL, false);
5529 bilateralFilter_8u( const Mat& src, Mat& dst, int d,
5530 double sigma_color, double sigma_space,
5533 int cn = src.channels();
5534 int i, j, maxk, radius;
5535 Size size = src.size();
5537 CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data );
5539 if( sigma_color <= 0 )
5541 if( sigma_space <= 0 )
5544 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5545 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5548 radius = cvRound(sigma_space*1.5);
5551 radius = MAX(radius, 1);
5555 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5557 std::vector<float> _color_weight(cn*256);
5558 std::vector<float> _space_weight(d*d);
5559 std::vector<int> _space_ofs(d*d);
5560 float* color_weight = &_color_weight[0];
5561 float* space_weight = &_space_weight[0];
5562 int* space_ofs = &_space_ofs[0];
5564 // initialize color-related bilateral filter coefficients
5566 for( i = 0; i < 256*cn; i++ )
5567 color_weight[i] = (float)std::exp(i*i*gauss_color_coeff);
5569 // initialize space-related bilateral filter coefficients
5570 for( i = -radius, maxk = 0; i <= radius; i++ )
5574 for( ; j <= radius; j++ )
5576 double r = std::sqrt((double)i*i + (double)j*j);
5579 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5580 space_ofs[maxk++] = (int)(i*temp.step + j*cn);
5584 BilateralFilter_8u_Invoker body(dst, temp, radius, maxk, space_ofs, space_weight, color_weight);
5585 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5589 class BilateralFilter_32f_Invoker :
5590 public ParallelLoopBody
5594 BilateralFilter_32f_Invoker(int _cn, int _radius, int _maxk, int *_space_ofs,
5595 const Mat& _temp, Mat& _dest, float _scale_index, float *_space_weight, float *_expLUT) :
5596 cn(_cn), radius(_radius), maxk(_maxk), space_ofs(_space_ofs),
5597 temp(&_temp), dest(&_dest), scale_index(_scale_index), space_weight(_space_weight), expLUT(_expLUT)
5601 virtual void operator() (const Range& range) const CV_OVERRIDE
5604 Size size = dest->size();
5606 int CV_DECL_ALIGNED(16) idxBuf[4];
5607 bool haveSIMD128 = hasSIMD128();
5610 for( i = range.start; i < range.end; i++ )
5612 const float* sptr = temp->ptr<float>(i+radius) + radius*cn;
5613 float* dptr = dest->ptr<float>(i);
5617 for( j = 0; j < size.width; j++ )
5619 float sum = 0, wsum = 0;
5620 float val0 = sptr[j];
5625 v_float32x4 vecwsum = v_setzero_f32();
5626 v_float32x4 vecvsum = v_setzero_f32();
5627 const v_float32x4 _val0 = v_setall_f32(sptr[j]);
5628 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5630 for (; k <= maxk - 4; k += 4)
5632 v_float32x4 _sw = v_load(space_weight + k);
5633 v_float32x4 _val = v_float32x4(sptr[j + space_ofs[k]],
5634 sptr[j + space_ofs[k + 1]],
5635 sptr[j + space_ofs[k + 2]],
5636 sptr[j + space_ofs[k + 3]]);
5637 v_float32x4 _alpha = v_abs(_val - _val0) * _scale_index;
5639 v_int32x4 _idx = v_round(_alpha);
5640 v_store(idxBuf, _idx);
5641 _alpha -= v_cvt_f32(_idx);
5643 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5647 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5648 expLUT[idxBuf[1] + 1],
5649 expLUT[idxBuf[2] + 1],
5650 expLUT[idxBuf[3] + 1]);
5652 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5658 float *bufFloat = (float*)idxBuf;
5659 v_float32x4 sum4 = v_reduce_sum4(vecwsum, vecvsum, vecwsum, vecvsum);
5660 v_store(bufFloat, sum4);
5662 wsum += bufFloat[0];
5666 for( ; k < maxk; k++ )
5668 float val = sptr[j + space_ofs[k]];
5669 float alpha = (float)(std::abs(val - val0)*scale_index);
5670 int idx = cvFloor(alpha);
5672 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5676 dptr[j] = (float)(sum/wsum);
5681 CV_Assert( cn == 3 );
5682 for( j = 0; j < size.width*3; j += 3 )
5684 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5685 float b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5690 v_float32x4 sumw = v_setzero_f32();
5691 v_float32x4 sumb = v_setzero_f32();
5692 v_float32x4 sumg = v_setzero_f32();
5693 v_float32x4 sumr = v_setzero_f32();
5694 const v_float32x4 _b0 = v_setall_f32(b0);
5695 const v_float32x4 _g0 = v_setall_f32(g0);
5696 const v_float32x4 _r0 = v_setall_f32(r0);
5697 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5699 for( ; k <= maxk-4; k += 4 )
5701 v_float32x4 _sw = v_load(space_weight + k);
5703 const float* const sptr_k0 = sptr + j + space_ofs[k];
5704 const float* const sptr_k1 = sptr + j + space_ofs[k+1];
5705 const float* const sptr_k2 = sptr + j + space_ofs[k+2];
5706 const float* const sptr_k3 = sptr + j + space_ofs[k+3];
5708 v_float32x4 _v0 = v_load(sptr_k0);
5709 v_float32x4 _v1 = v_load(sptr_k1);
5710 v_float32x4 _v2 = v_load(sptr_k2);
5711 v_float32x4 _v3 = v_load(sptr_k3);
5712 v_float32x4 _b, _g, _r, _dummy;
5714 v_transpose4x4(_v0, _v1, _v2, _v3, _b, _g, _r, _dummy);
5716 v_float32x4 _bt = v_abs(_b - _b0);
5717 v_float32x4 _gt = v_abs(_g - _g0);
5718 v_float32x4 _rt = v_abs(_r - _r0);
5719 v_float32x4 _alpha = _scale_index * (_bt + _gt + _rt);
5721 v_int32x4 _idx = v_round(_alpha);
5722 v_store((int*)idxBuf, _idx);
5723 _alpha -= v_cvt_f32(_idx);
5725 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5729 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5730 expLUT[idxBuf[1] + 1],
5731 expLUT[idxBuf[2] + 1],
5732 expLUT[idxBuf[3] + 1]);
5734 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5744 v_float32x4 sum4 = v_reduce_sum4(sumw, sumb, sumg, sumr);
5745 float *bufFloat = (float*)idxBuf;
5746 v_store(bufFloat, sum4);
5747 wsum += bufFloat[0];
5748 sum_b += bufFloat[1];
5749 sum_g += bufFloat[2];
5750 sum_r += bufFloat[3];
5754 for(; k < maxk; k++ )
5756 const float* sptr_k = sptr + j + space_ofs[k];
5757 float b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5758 float alpha = (float)((std::abs(b - b0) +
5759 std::abs(g - g0) + std::abs(r - r0))*scale_index);
5760 int idx = cvFloor(alpha);
5762 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5763 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5770 dptr[j] = b0; dptr[j+1] = g0; dptr[j+2] = r0;
5777 int cn, radius, maxk, *space_ofs;
5780 float scale_index, *space_weight, *expLUT;
5785 bilateralFilter_32f( const Mat& src, Mat& dst, int d,
5786 double sigma_color, double sigma_space,
5789 int cn = src.channels();
5790 int i, j, maxk, radius;
5791 double minValSrc=-1, maxValSrc=1;
5792 const int kExpNumBinsPerChannel = 1 << 12;
5793 int kExpNumBins = 0;
5794 float lastExpVal = 1.f;
5795 float len, scale_index;
5796 Size size = src.size();
5798 CV_Assert( (src.type() == CV_32FC1 || src.type() == CV_32FC3) && src.data != dst.data );
5800 if( sigma_color <= 0 )
5802 if( sigma_space <= 0 )
5805 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5806 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5809 radius = cvRound(sigma_space*1.5);
5812 radius = MAX(radius, 1);
5814 // compute the min/max range for the input image (even if multichannel)
5816 minMaxLoc( src.reshape(1), &minValSrc, &maxValSrc );
5817 if(std::abs(minValSrc - maxValSrc) < FLT_EPSILON)
5823 // temporary copy of the image with borders for easy processing
5825 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5826 const double insteadNaNValue = -5. * sigma_color;
5827 patchNaNs( temp, insteadNaNValue ); // this replacement of NaNs makes the assumption that depth values are nonnegative
5828 // TODO: make insteadNaNValue avalible in the outside function interface to control the cases breaking the assumption
5829 // allocate lookup tables
5830 std::vector<float> _space_weight(d*d);
5831 std::vector<int> _space_ofs(d*d);
5832 float* space_weight = &_space_weight[0];
5833 int* space_ofs = &_space_ofs[0];
5835 // assign a length which is slightly more than needed
5836 len = (float)(maxValSrc - minValSrc) * cn;
5837 kExpNumBins = kExpNumBinsPerChannel * cn;
5838 std::vector<float> _expLUT(kExpNumBins+2);
5839 float* expLUT = &_expLUT[0];
5841 scale_index = kExpNumBins/len;
5843 // initialize the exp LUT
5844 for( i = 0; i < kExpNumBins+2; i++ )
5846 if( lastExpVal > 0.f )
5848 double val = i / scale_index;
5849 expLUT[i] = (float)std::exp(val * val * gauss_color_coeff);
5850 lastExpVal = expLUT[i];
5856 // initialize space-related bilateral filter coefficients
5857 for( i = -radius, maxk = 0; i <= radius; i++ )
5858 for( j = -radius; j <= radius; j++ )
5860 double r = std::sqrt((double)i*i + (double)j*j);
5863 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5864 space_ofs[maxk++] = (int)(i*(temp.step/sizeof(float)) + j*cn);
5867 // parallel_for usage
5869 BilateralFilter_32f_Invoker body(cn, radius, maxk, space_ofs, temp, dst, scale_index, space_weight, expLUT);
5870 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5874 #define IPP_BILATERAL_PARALLEL 1
5877 class ipp_bilateralFilterParallel: public ParallelLoopBody
5880 ipp_bilateralFilterParallel(::ipp::IwiImage &_src, ::ipp::IwiImage &_dst, int _radius, Ipp32f _valSquareSigma, Ipp32f _posSquareSigma, ::ipp::IwiBorderType _borderType, bool *_ok):
5881 src(_src), dst(_dst)
5886 valSquareSigma = _valSquareSigma;
5887 posSquareSigma = _posSquareSigma;
5888 borderType = _borderType;
5892 ~ipp_bilateralFilterParallel() {}
5894 virtual void operator() (const Range& range) const CV_OVERRIDE
5901 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, dst.m_size.width, range.end - range.start);
5902 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, src, dst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), borderType, tile);
5904 catch(::ipp::IwException)
5911 ::ipp::IwiImage &src;
5912 ::ipp::IwiImage &dst;
5915 Ipp32f valSquareSigma;
5916 Ipp32f posSquareSigma;
5917 ::ipp::IwiBorderType borderType;
5920 const ipp_bilateralFilterParallel& operator= (const ipp_bilateralFilterParallel&);
5924 static bool ipp_bilateralFilter(Mat &src, Mat &dst, int d, double sigmaColor, double sigmaSpace, int borderType)
5927 CV_INSTRUMENT_REGION_IPP()
5929 int radius = IPP_MAX(((d <= 0)?cvRound(sigmaSpace*1.5):d/2), 1);
5930 Ipp32f valSquareSigma = (Ipp32f)((sigmaColor <= 0)?1:sigmaColor*sigmaColor);
5931 Ipp32f posSquareSigma = (Ipp32f)((sigmaSpace <= 0)?1:sigmaSpace*sigmaSpace);
5933 // Acquire data and begin processing
5936 ::ipp::IwiImage iwSrc = ippiGetImage(src);
5937 ::ipp::IwiImage iwDst = ippiGetImage(dst);
5938 ::ipp::IwiBorderSize borderSize(radius);
5939 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
5943 const int threads = ippiSuggestThreadsNum(iwDst, 2);
5944 if(IPP_BILATERAL_PARALLEL && threads > 1) {
5946 Range range(0, (int)iwDst.m_size.height);
5947 ipp_bilateralFilterParallel invoker(iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ippBorder, &ok);
5951 parallel_for_(range, invoker, threads*4);
5956 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), ippBorder);
5959 catch (::ipp::IwException)
5965 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(d); CV_UNUSED(sigmaColor); CV_UNUSED(sigmaSpace); CV_UNUSED(borderType);
5973 void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
5974 double sigmaColor, double sigmaSpace,
5977 CV_INSTRUMENT_REGION()
5979 _dst.create( _src.size(), _src.type() );
5981 CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
5982 ocl_bilateralFilter_8u(_src, _dst, d, sigmaColor, sigmaSpace, borderType))
5984 Mat src = _src.getMat(), dst = _dst.getMat();
5986 CV_IPP_RUN_FAST(ipp_bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType));
5988 if( src.depth() == CV_8U )
5989 bilateralFilter_8u( src, dst, d, sigmaColor, sigmaSpace, borderType );
5990 else if( src.depth() == CV_32F )
5991 bilateralFilter_32f( src, dst, d, sigmaColor, sigmaSpace, borderType );
5993 CV_Error( CV_StsUnsupportedFormat,
5994 "Bilateral filtering is only implemented for 8u and 32f images" );
5997 //////////////////////////////////////////////////////////////////////////////////////////
6000 cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
6001 int param1, int param2, double param3, double param4 )
6003 cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
6005 CV_Assert( dst.size() == src.size() &&
6006 (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
6011 if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE )
6012 cv::boxFilter( src, dst, dst.depth(), cv::Size(param1, param2), cv::Point(-1,-1),
6013 smooth_type == CV_BLUR, cv::BORDER_REPLICATE );
6014 else if( smooth_type == CV_GAUSSIAN )
6015 cv::GaussianBlur( src, dst, cv::Size(param1, param2), param3, param4, cv::BORDER_REPLICATE );
6016 else if( smooth_type == CV_MEDIAN )
6017 cv::medianBlur( src, dst, param1 );
6019 cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
6021 if( dst.data != dst0.data )
6022 CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );