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 )
1713 const int SMALL_GAUSSIAN_SIZE = 7;
1714 static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] =
1717 {0.25f, 0.5f, 0.25f},
1718 {0.0625f, 0.25f, 0.375f, 0.25f, 0.0625f},
1719 {0.03125f, 0.109375f, 0.21875f, 0.28125f, 0.21875f, 0.109375f, 0.03125f}
1722 const float* fixed_kernel = n % 2 == 1 && n <= SMALL_GAUSSIAN_SIZE && sigma <= 0 ?
1723 small_gaussian_tab[n>>1] : 0;
1725 CV_Assert( ktype == CV_32F || ktype == CV_64F );
1726 Mat kernel(n, 1, ktype);
1727 float* cf = kernel.ptr<float>();
1728 double* cd = kernel.ptr<double>();
1730 double sigmaX = sigma > 0 ? sigma : ((n-1)*0.5 - 1)*0.3 + 0.8;
1731 double scale2X = -0.5/(sigmaX*sigmaX);
1735 for( i = 0; i < n; i++ )
1737 double x = i - (n-1)*0.5;
1738 double t = fixed_kernel ? (double)fixed_kernel[i] : std::exp(scale2X*x*x);
1739 if( ktype == CV_32F )
1751 CV_DbgAssert(fabs(sum) > 0);
1753 for( i = 0; i < n; i++ )
1755 if( ktype == CV_32F )
1756 cf[i] = (float)(cf[i]*sum);
1766 template <typename T>
1767 static std::vector<T> getFixedpointGaussianKernel( int n, double sigma )
1772 return std::vector<T>(1, softdouble(1.0));
1775 T v3[] = { softdouble(0.25), softdouble(0.5), softdouble(0.25) };
1776 return std::vector<T>(v3, v3 + 3);
1780 T v5[] = { softdouble(0.0625), softdouble(0.25), softdouble(0.375), softdouble(0.25), softdouble(0.0625) };
1781 return std::vector<T>(v5, v5 + 5);
1785 T v7[] = { softdouble(0.03125), softdouble(0.109375), softdouble(0.21875), softdouble(0.28125), softdouble(0.21875), softdouble(0.109375), softdouble(0.03125) };
1786 return std::vector<T>(v7, v7 + 7);
1791 softdouble sigmaX = sigma > 0 ? softdouble(sigma) : mulAdd(softdouble(n),softdouble(0.15),softdouble(0.35));// softdouble(((n-1)*0.5 - 1)*0.3 + 0.8)
1792 softdouble scale2X = softdouble(-0.5*0.25)/(sigmaX*sigmaX);
1793 std::vector<softdouble> values(n);
1795 for(int i = 0, x = 1 - n; i < n; i++, x+=2 )
1797 // x = i - (n - 1)*0.5
1798 // t = std::exp(scale2X*x*x)
1799 values[i] = exp(softdouble(x*x)*scale2X);
1802 sum = softdouble::one()/sum;
1804 std::vector<T> kernel(n);
1805 for(int i = 0; i < n; i++ )
1807 kernel[i] = values[i] * sum;
1813 template <typename ET, typename FT>
1814 void hlineSmooth1N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int)
1816 for (int i = 0; i < len*cn; i++, src++, dst++)
1817 *dst = (*m) * (*src);
1820 void hlineSmooth1N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int)
1823 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
1825 for (; i <= lencn - 16; i += 16)
1827 v_uint8x16 v_src = v_load(src + i);
1828 v_uint16x8 v_tmp0, v_tmp1;
1829 v_expand(v_src, v_tmp0, v_tmp1);
1830 v_store((uint16_t*)dst + i, v_mul*v_tmp0);
1831 v_store((uint16_t*)dst + i + 8, v_mul*v_tmp1);
1835 v_uint16x8 v_src = v_load_expand(src + i);
1836 v_store((uint16_t*)dst + i, v_mul*v_src);
1839 for (; i < lencn; i++)
1840 dst[i] = m[0] * src[i];
1842 template <typename ET, typename FT>
1843 void hlineSmooth1N1(const ET* src, int cn, const FT*, int, FT* dst, int len, int)
1845 for (int i = 0; i < len*cn; i++, src++, dst++)
1849 void hlineSmooth1N1<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int)
1853 for (; i <= lencn - 16; i += 16)
1855 v_uint8x16 v_src = v_load(src + i);
1856 v_uint16x8 v_tmp0, v_tmp1;
1857 v_expand(v_src, v_tmp0, v_tmp1);
1858 v_store((uint16_t*)dst + i, v_shl<8>(v_tmp0));
1859 v_store((uint16_t*)dst + i + 8, v_shl<8>(v_tmp1));
1863 v_uint16x8 v_src = v_load_expand(src + i);
1864 v_store((uint16_t*)dst + i, v_shl<8>(v_src));
1867 for (; i < lencn; i++)
1870 template <typename ET, typename FT>
1871 void hlineSmooth3N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
1875 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1876 for (int k = 0; k < cn; k++)
1877 dst[k] = msum * src[k];
1881 // Point that fall left from border
1882 for (int k = 0; k < cn; k++)
1883 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1884 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1886 int src_idx = borderInterpolate(-1, len, borderType);
1887 for (int k = 0; k < cn; k++)
1888 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1891 src += cn; dst += cn;
1892 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1893 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1895 // Point that fall right from border
1896 for (int k = 0; k < cn; k++)
1897 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1898 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1900 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1901 for (int k = 0; k < cn; k++)
1902 dst[k] = dst[k] + m[2] * src[src_idx + k];
1907 void hlineSmooth3N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
1911 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] : m[1];
1912 for (int k = 0; k < cn; k++)
1913 dst[k] = msum * src[k];
1917 // Point that fall left from border
1918 for (int k = 0; k < cn; k++)
1919 dst[k] = m[1] * src[k] + m[2] * src[cn + k];
1920 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1922 int src_idx = borderInterpolate(-1, len, borderType);
1923 for (int k = 0; k < cn; k++)
1924 dst[k] = dst[k] + m[0] * src[src_idx*cn + k];
1927 src += cn; dst += cn;
1928 int i = cn, lencn = (len - 1)*cn;
1929 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
1930 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
1931 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
1932 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
1934 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
1935 v_expand(v_load(src - cn), v_src00, v_src01);
1936 v_expand(v_load(src), v_src10, v_src11);
1937 v_expand(v_load(src + cn), v_src20, v_src21);
1938 v_store((uint16_t*)dst, v_src00 * v_mul0 + v_src10 * v_mul1 + v_src20 * v_mul2);
1939 v_store((uint16_t*)dst + 8, v_src01 * v_mul0 + v_src11 * v_mul1 + v_src21 * v_mul2);
1941 for (; i < lencn; i++, src++, dst++)
1942 *dst = m[0] * src[-cn] + m[1] * src[0] + m[2] * src[cn];
1944 // Point that fall right from border
1945 for (int k = 0; k < cn; k++)
1946 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
1947 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1949 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1950 for (int k = 0; k < cn; k++)
1951 dst[k] = dst[k] + m[2] * src[src_idx + k];
1955 template <typename ET, typename FT>
1956 void hlineSmooth3N121(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
1960 if(borderType != BORDER_CONSTANT)
1961 for (int k = 0; k < cn; k++)
1962 dst[k] = FT(src[k]);
1964 for (int k = 0; k < cn; k++)
1965 dst[k] = FT(src[k])>>1;
1969 // Point that fall left from border
1970 for (int k = 0; k < cn; k++)
1971 dst[k] = (FT(src[k])>>1) + (FT(src[cn + k])>>2);
1972 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1974 int src_idx = borderInterpolate(-1, len, borderType);
1975 for (int k = 0; k < cn; k++)
1976 dst[k] = dst[k] + (FT(src[src_idx*cn + k])>>2);
1979 src += cn; dst += cn;
1980 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
1981 *dst = (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[0])>>1);
1983 // Point that fall right from border
1984 for (int k = 0; k < cn; k++)
1985 dst[k] = (FT(src[k - cn])>>2) + (FT(src[k])>>1);
1986 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
1988 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
1989 for (int k = 0; k < cn; k++)
1990 dst[k] = dst[k] + (FT(src[src_idx + k])>>2);
1995 void hlineSmooth3N121<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
1999 if (borderType != BORDER_CONSTANT)
2000 for (int k = 0; k < cn; k++)
2001 dst[k] = ufixedpoint16(src[k]);
2003 for (int k = 0; k < cn; k++)
2004 dst[k] = ufixedpoint16(src[k]) >> 1;
2008 // Point that fall left from border
2009 for (int k = 0; k < cn; k++)
2010 dst[k] = (ufixedpoint16(src[k])>>1) + (ufixedpoint16(src[cn + k])>>2);
2011 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2013 int src_idx = borderInterpolate(-1, len, borderType);
2014 for (int k = 0; k < cn; k++)
2015 dst[k] = dst[k] + (ufixedpoint16(src[src_idx*cn + k])>>2);
2018 src += cn; dst += cn;
2019 int i = cn, lencn = (len - 1)*cn;
2020 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2022 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2023 v_expand(v_load(src - cn), v_src00, v_src01);
2024 v_expand(v_load(src), v_src10, v_src11);
2025 v_expand(v_load(src + cn), v_src20, v_src21);
2026 v_store((uint16_t*)dst, (v_src00 + v_src20 + (v_src10 << 1)) << 6);
2027 v_store((uint16_t*)dst + 8, (v_src01 + v_src21 + (v_src11 << 1)) << 6);
2029 for (; i < lencn; i++, src++, dst++)
2030 *((uint16_t*)dst) = (uint16_t(src[-cn]) + uint16_t(src[cn]) + (uint16_t(src[0]) << 1)) << 6;
2032 // Point that fall right from border
2033 for (int k = 0; k < cn; k++)
2034 dst[k] = (ufixedpoint16(src[k - cn])>>2) + (ufixedpoint16(src[k])>>1);
2035 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2037 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2038 for (int k = 0; k < cn; k++)
2039 dst[k] = dst[k] + (ufixedpoint16(src[src_idx + k])>>2);
2043 template <typename ET, typename FT>
2044 void hlineSmooth3Naba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2048 FT msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2049 for (int k = 0; k < cn; k++)
2050 dst[k] = msum * src[k];
2054 // Point that fall left from border
2055 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2057 int src_idx = borderInterpolate(-1, len, borderType);
2058 for (int k = 0; k < cn; k++)
2059 dst[k] = m[1] * src[k] + m[0] * src[cn + k] + m[0] * src[src_idx*cn + k];
2063 for (int k = 0; k < cn; k++)
2064 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2067 src += cn; dst += cn;
2068 for (int i = cn; i < (len - 1)*cn; i++, src++, dst++)
2069 *dst = m[1] * src[0] + m[0] * src[-cn] + m[0] * src[cn];
2071 // Point that fall right from border
2072 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2074 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2075 for (int k = 0; k < cn; k++)
2076 dst[k] = m[1] * src[k] + m[0] * src[k - cn] + m[0] * src[src_idx + k];
2080 for (int k = 0; k < cn; k++)
2081 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2086 void hlineSmooth3Naba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2090 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? (m[0]<<1) + m[1] : m[1];
2091 for (int k = 0; k < cn; k++)
2092 dst[k] = msum * src[k];
2096 // Point that fall left from border
2097 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2099 int src_idx = borderInterpolate(-1, len, borderType);
2100 for (int k = 0; k < cn; k++)
2101 ((uint16_t*)dst)[k] = ((uint16_t*)m)[1] * src[k] + ((uint16_t*)m)[0] * ((uint16_t)(src[cn + k]) + (uint16_t)(src[src_idx*cn + k]));
2105 for (int k = 0; k < cn; k++)
2106 dst[k] = m[1] * src[k] + m[0] * src[cn + k];
2109 src += cn; dst += cn;
2110 int i = cn, lencn = (len - 1)*cn;
2111 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2112 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)m+1));
2113 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2115 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21;
2116 v_expand(v_load(src - cn), v_src00, v_src01);
2117 v_expand(v_load(src), v_src10, v_src11);
2118 v_expand(v_load(src + cn), v_src20, v_src21);
2119 v_store((uint16_t*)dst, (v_src00 + v_src20) * v_mul0 + v_src10 * v_mul1);
2120 v_store((uint16_t*)dst + 8, (v_src01 + v_src21) * v_mul0 + v_src11 * v_mul1);
2122 for (; i < lencn; i++, src++, dst++)
2123 *((uint16_t*)dst) = ((uint16_t*)m)[1] * src[0] + ((uint16_t*)m)[0] * ((uint16_t)(src[-cn]) + (uint16_t)(src[cn]));
2125 // Point that fall right from border
2126 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2128 int src_idx = (borderInterpolate(len, len, borderType) - (len - 1))*cn;
2129 for (int k = 0; k < cn; k++)
2130 ((uint16_t*)dst)[k] = ((uint16_t*)m)[1] * src[k] + ((uint16_t*)m)[0] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[src_idx + k]));
2134 for (int k = 0; k < cn; k++)
2135 dst[k] = m[0] * src[k - cn] + m[1] * src[k];
2139 template <typename ET, typename FT>
2140 void hlineSmooth5N(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2144 FT msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2145 for (int k = 0; k < cn; k++)
2146 dst[k] = msum * src[k];
2150 if (borderType == BORDER_CONSTANT)
2151 for (int k = 0; k < cn; k++)
2153 dst[k ] = m[2] * src[k] + m[3] * src[k+cn];
2154 dst[k+cn] = m[1] * src[k] + m[2] * src[k+cn];
2158 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2159 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2160 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2161 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2162 for (int k = 0; k < cn; k++)
2164 dst[k ] = m[1] * src[k + idxm1] + m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + idxp1] + m[0] * src[k + idxm2];
2165 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2171 if (borderType == BORDER_CONSTANT)
2172 for (int k = 0; k < cn; k++)
2174 dst[k ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn];
2175 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn];
2176 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn];
2180 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2181 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2182 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2183 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2184 for (int k = 0; k < cn; k++)
2186 dst[k ] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2*cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2187 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2*cn] + m[0] * src[k + idxm1] + m[4] * src[k + idxp1];
2188 dst[k + 2*cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2*cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2194 // Points that fall left from border
2195 for (int k = 0; k < cn; k++)
2197 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2*cn + k];
2198 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2*cn + k] + m[4] * src[3*cn + k];
2200 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2202 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2203 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2204 for (int k = 0; k < cn; k++)
2206 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2207 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2211 src += 2*cn; dst += 2*cn;
2212 for (int i = 2*cn; i < (len - 2)*cn; i++, src++, dst++)
2213 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2215 // Points that fall right from border
2216 for (int k = 0; k < cn; k++)
2218 dst[k] = m[0] * src[k - 2*cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2219 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2221 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2223 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2224 int idxp2 = (borderInterpolate(len+1, len, borderType) - (len - 2))*cn;
2225 for (int k = 0; k < cn; k++)
2227 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2228 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2234 void hlineSmooth5N<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2238 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? m[0] + m[1] + m[2] + m[3] + m[4] : m[2];
2239 for (int k = 0; k < cn; k++)
2240 dst[k] = msum * src[k];
2244 if (borderType == BORDER_CONSTANT)
2245 for (int k = 0; k < cn; k++)
2247 dst[k] = m[2] * src[k] + m[3] * src[k + cn];
2248 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2252 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2253 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2254 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2255 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2256 for (int k = 0; k < cn; k++)
2258 dst[k] = m[1] * src[k + idxm1] + m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + idxp1] + m[0] * src[k + idxm2];
2259 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2265 if (borderType == BORDER_CONSTANT)
2266 for (int k = 0; k < cn; k++)
2268 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn];
2269 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn];
2270 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2274 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2275 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2276 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2277 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2278 for (int k = 0; k < cn; k++)
2280 dst[k] = m[2] * src[k] + m[3] * src[k + cn] + m[4] * src[k + 2 * cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2281 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[3] * src[k + 2 * cn] + m[0] * src[k + idxm1] + m[4] * src[k + idxp1];
2282 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn] + m[3] * src[k + idxp1] + m[4] * src[k + idxp2];
2288 // Points that fall left from border
2289 for (int k = 0; k < cn; k++)
2291 dst[k] = m[2] * src[k] + m[3] * src[cn + k] + m[4] * src[2 * cn + k];
2292 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[3] * src[2 * cn + k] + m[4] * src[3 * cn + k];
2294 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2296 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2297 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2298 for (int k = 0; k < cn; k++)
2300 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2301 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2305 src += 2 * cn; dst += 2 * cn;
2306 int i = 2*cn, lencn = (len - 2)*cn;
2307 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2308 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2309 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2310 v_uint16x8 v_mul3 = v_setall_u16(*((uint16_t*)(m + 3)));
2311 v_uint16x8 v_mul4 = v_setall_u16(*((uint16_t*)(m + 4)));
2312 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2314 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2315 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2316 v_expand(v_load(src - cn), v_src10, v_src11);
2317 v_expand(v_load(src), v_src20, v_src21);
2318 v_expand(v_load(src + cn), v_src30, v_src31);
2319 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2320 v_store((uint16_t*)dst, v_src00 * v_mul0 + v_src10 * v_mul1 + v_src20 * v_mul2 + v_src30 * v_mul3 + v_src40 * v_mul4);
2321 v_store((uint16_t*)dst + 8, v_src01 * v_mul0 + v_src11 * v_mul1 + v_src21 * v_mul2 + v_src31 * v_mul3 + v_src41 * v_mul4);
2323 for (; i < lencn; i++, src++, dst++)
2324 *dst = m[0] * src[-2*cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2*cn];
2326 // Points that fall right from border
2327 for (int k = 0; k < cn; k++)
2329 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2330 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2332 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2334 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2335 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2336 for (int k = 0; k < cn; k++)
2338 dst[k] = dst[k] + m[4] * src[idxp1 + k];
2339 dst[k + cn] = dst[k + cn] + m[3] * src[idxp1 + k] + m[4] * src[idxp2 + k];
2344 template <typename ET, typename FT>
2345 void hlineSmooth5N14641(const ET* src, int cn, const FT*, int, FT* dst, int len, int borderType)
2349 if (borderType == BORDER_CONSTANT)
2350 for (int k = 0; k < cn; k++)
2351 dst[k] = (FT(src[k])>>3)*(uint8_t)3;
2353 for (int k = 0; k < cn; k++)
2358 if (borderType == BORDER_CONSTANT)
2359 for (int k = 0; k < cn; k++)
2361 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2);
2362 dst[k + cn] = (FT(src[k]) >> 2) + (FT(src[k + cn])>>4)*(uint8_t)6;
2366 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2367 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2368 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2369 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2370 for (int k = 0; k < cn; k++)
2372 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + idxm1])>>2) + (FT(src[k + cn])>>2) + (FT(src[k + idxp1])>>4) + (FT(src[k + idxm2])>>4);
2373 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + idxp1])>>2) + (FT(src[k + idxm1])>>4) + (FT(src[k + idxp2])>>4);
2379 if (borderType == BORDER_CONSTANT)
2380 for (int k = 0; k < cn; k++)
2382 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + 2 * cn])>>4);
2383 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2);
2384 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k])>>4);
2388 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2389 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2390 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2391 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2392 for (int k = 0; k < cn; k++)
2394 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + idxm1])>>2) + (FT(src[k + 2 * cn])>>4) + (FT(src[k + idxm2])>>4);
2395 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k + 2 * cn])>>2) + (FT(src[k + idxm1])>>4) + (FT(src[k + idxp1])>>4);
2396 dst[k + 2 * cn] = (FT(src[k + 2 * cn])>>4)*(uint8_t)6 + (FT(src[k + cn])>>2) + (FT(src[k + idxp1])>>2) + (FT(src[k])>>4) + (FT(src[k + idxp2])>>4);
2402 // Points that fall left from border
2403 for (int k = 0; k < cn; k++)
2405 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[cn + k])>>2) + (FT(src[2 * cn + k])>>4);
2406 dst[k + cn] = (FT(src[cn + k])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[2 * cn + k])>>2) + (FT(src[3 * cn + k])>>4);
2408 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2410 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2411 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2412 for (int k = 0; k < cn; k++)
2414 dst[k] = dst[k] + (FT(src[idxm2 + k])>>4) + (FT(src[idxm1 + k])>>2);
2415 dst[k + cn] = dst[k + cn] + (FT(src[idxm1 + k])>>4);
2419 src += 2 * cn; dst += 2 * cn;
2420 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2421 *dst = (FT(src[0])>>4)*(uint8_t)6 + (FT(src[-cn])>>2) + (FT(src[cn])>>2) + (FT(src[-2 * cn])>>4) + (FT(src[2 * cn])>>4);
2423 // Points that fall right from border
2424 for (int k = 0; k < cn; k++)
2426 dst[k] = (FT(src[k])>>4)*(uint8_t)6 + (FT(src[k - cn])>>2) + (FT(src[k + cn])>>2) + (FT(src[k - 2 * cn])>>4);
2427 dst[k + cn] = (FT(src[k + cn])>>4)*(uint8_t)6 + (FT(src[k])>>2) + (FT(src[k - cn])>>4);
2429 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2431 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2432 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2433 for (int k = 0; k < cn; k++)
2435 dst[k] = dst[k] + (FT(src[idxp1 + k])>>4);
2436 dst[k + cn] = dst[k + cn] + (FT(src[idxp1 + k])>>2) + (FT(src[idxp2 + k])>>4);
2442 void hlineSmooth5N14641<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16*, int, ufixedpoint16* dst, int len, int borderType)
2446 if (borderType == BORDER_CONSTANT)
2447 for (int k = 0; k < cn; k++)
2448 dst[k] = (ufixedpoint16(src[k])>>3) * (uint8_t)3;
2451 for (int k = 0; k < cn; k++)
2457 if (borderType == BORDER_CONSTANT)
2458 for (int k = 0; k < cn; k++)
2460 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2);
2461 dst[k + cn] = (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6;
2465 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2466 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2467 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2468 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2469 for (int k = 0; k < cn; k++)
2471 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + idxm1]) >> 2) + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 4) + (ufixedpoint16(src[k + idxm2]) >> 4);
2472 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 4) + (ufixedpoint16(src[k + idxp2]) >> 4);
2478 if (borderType == BORDER_CONSTANT)
2479 for (int k = 0; k < cn; k++)
2481 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4);
2482 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2);
2483 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k]) >> 4);
2487 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2488 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2489 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2490 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2491 for (int k = 0; k < cn; k++)
2493 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 4) + (ufixedpoint16(src[k + idxm2]) >> 4);
2494 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k + 2 * cn]) >> 2) + (ufixedpoint16(src[k + idxm1]) >> 4) + (ufixedpoint16(src[k + idxp1]) >> 4);
2495 dst[k + 2 * cn] = (ufixedpoint16(src[k + 2 * cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k + idxp1]) >> 2) + (ufixedpoint16(src[k]) >> 4) + (ufixedpoint16(src[k + idxp2]) >> 4);
2501 // Points that fall left from border
2502 for (int k = 0; k < cn; k++)
2504 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[cn + k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 4);
2505 dst[k + cn] = (ufixedpoint16(src[cn + k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[2 * cn + k]) >> 2) + (ufixedpoint16(src[3 * cn + k]) >> 4);
2507 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2509 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2510 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2511 for (int k = 0; k < cn; k++)
2513 dst[k] = dst[k] + (ufixedpoint16(src[idxm2 + k]) >> 4) + (ufixedpoint16(src[idxm1 + k]) >> 2);
2514 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxm1 + k]) >> 4);
2518 src += 2 * cn; dst += 2 * cn;
2519 int i = 2 * cn, lencn = (len - 2)*cn;
2520 v_uint16x8 v_6 = v_setall_u16(6);
2521 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2523 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2524 v_expand(v_load(src - 2*cn), v_src00, v_src01);
2525 v_expand(v_load(src - cn), v_src10, v_src11);
2526 v_expand(v_load(src), v_src20, v_src21);
2527 v_expand(v_load(src + cn), v_src30, v_src31);
2528 v_expand(v_load(src + 2*cn), v_src40, v_src41);
2529 v_store((uint16_t*)dst, (v_src20 * v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40) << 4);
2530 v_store((uint16_t*)dst + 8, (v_src21 * v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41) << 4);
2532 for (; i < lencn; i++, src++, dst++)
2533 *((uint16_t*)dst) = (uint16_t(src[0]) * 6 + ((uint16_t(src[-cn]) + uint16_t(src[cn])) << 2) + uint16_t(src[-2 * cn]) + uint16_t(src[2 * cn])) << 4;
2535 // Points that fall right from border
2536 for (int k = 0; k < cn; k++)
2538 dst[k] = (ufixedpoint16(src[k]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k - cn]) >> 2) + (ufixedpoint16(src[k + cn]) >> 2) + (ufixedpoint16(src[k - 2 * cn]) >> 4);
2539 dst[k + cn] = (ufixedpoint16(src[k + cn]) >> 4) * (uint8_t)6 + (ufixedpoint16(src[k]) >> 2) + (ufixedpoint16(src[k - cn]) >> 4);
2541 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2543 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2544 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2545 for (int k = 0; k < cn; k++)
2547 dst[k] = dst[k] + (ufixedpoint16(src[idxp1 + k]) >> 4);
2548 dst[k + cn] = dst[k + cn] + (ufixedpoint16(src[idxp1 + k]) >> 2) + (ufixedpoint16(src[idxp2 + k]) >> 4);
2553 template <typename ET, typename FT>
2554 void hlineSmooth5Nabcba(const ET* src, int cn, const FT* m, int, FT* dst, int len, int borderType)
2558 FT msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1])<<1) + m[2] : m[2];
2559 for (int k = 0; k < cn; k++)
2560 dst[k] = msum * src[k];
2564 if (borderType == BORDER_CONSTANT)
2565 for (int k = 0; k < cn; k++)
2567 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2568 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2572 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2573 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2574 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2575 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2576 for (int k = 0; k < cn; k++)
2578 dst[k] = m[1] * src[k + idxm1] + m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + idxp1] + m[0] * src[k + idxm2];
2579 dst[k + cn] = m[0] * src[k + idxm1] + m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + idxp1] + m[0] * src[k + idxp2];
2585 if (borderType == BORDER_CONSTANT)
2586 for (int k = 0; k < cn; k++)
2588 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2589 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + 2 * cn];
2590 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2594 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2595 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2596 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2597 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2598 for (int k = 0; k < cn; k++)
2600 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn] + m[0] * src[k + idxm2] + m[1] * src[k + idxm1];
2601 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn] + m[1] * src[k + 2 * cn] + m[0] * src[k + idxm1] + m[0] * src[k + idxp1];
2602 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn] + m[1] * src[k + idxp1] + m[0] * src[k + idxp2];
2608 // Points that fall left from border
2609 for (int k = 0; k < cn; k++)
2611 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2612 dst[k + cn] = m[1] * src[k] + m[2] * src[cn + k] + m[1] * src[2 * cn + k] + m[0] * src[3 * cn + k];
2614 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2616 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2617 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2618 for (int k = 0; k < cn; k++)
2620 dst[k] = dst[k] + m[0] * src[idxm2 + k] + m[1] * src[idxm1 + k];
2621 dst[k + cn] = dst[k + cn] + m[0] * src[idxm1 + k];
2625 src += 2 * cn; dst += 2 * cn;
2626 for (int i = 2 * cn; i < (len - 2)*cn; i++, src++, dst++)
2627 *dst = m[0] * src[-2 * cn] + m[1] * src[-cn] + m[2] * src[0] + m[3] * src[cn] + m[4] * src[2 * cn];
2629 // Points that fall right from border
2630 for (int k = 0; k < cn; k++)
2632 dst[k] = m[0] * src[k - 2 * cn] + m[1] * src[k - cn] + m[2] * src[k] + m[3] * src[k + cn];
2633 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2635 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2637 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2638 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2639 for (int k = 0; k < cn; k++)
2641 dst[k] = dst[k] + m[0] * src[idxp1 + k];
2642 dst[k + cn] = dst[k + cn] + m[1] * src[idxp1 + k] + m[0] * src[idxp2 + k];
2648 void hlineSmooth5Nabcba<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int, ufixedpoint16* dst, int len, int borderType)
2652 ufixedpoint16 msum = borderType != BORDER_CONSTANT ? ((m[0] + m[1]) << 1) + m[2] : m[2];
2653 for (int k = 0; k < cn; k++)
2654 dst[k] = msum * src[k];
2658 if (borderType == BORDER_CONSTANT)
2659 for (int k = 0; k < cn; k++)
2661 dst[k] = m[2] * src[k] + m[1] * src[k + cn];
2662 dst[k + cn] = m[1] * src[k] + m[2] * src[k + cn];
2666 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2667 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2668 int idxp1 = borderInterpolate(2, len, borderType)*cn;
2669 int idxp2 = borderInterpolate(3, len, borderType)*cn;
2670 for (int k = 0; k < cn; k++)
2672 ((uint16_t*)dst)[k] = ((uint16_t*)m)[1] * ((uint16_t)(src[k + idxm1]) + (uint16_t)(src[k + cn])) + ((uint16_t*)m)[2] * src[k] + ((uint16_t*)m)[0] * ((uint16_t)(src[k + idxp1]) + (uint16_t)(src[k + idxm2]));
2673 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[0] * ((uint16_t)(src[k + idxm1]) + (uint16_t)(src[k + idxp2])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[k + idxp1])) + ((uint16_t*)m)[2] * src[k + cn];
2679 if (borderType == BORDER_CONSTANT)
2680 for (int k = 0; k < cn; k++)
2682 dst[k] = m[2] * src[k] + m[1] * src[k + cn] + m[0] * src[k + 2 * cn];
2683 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[k + 2 * cn])) + ((uint16_t*)m)[2] * src[k + cn];
2684 dst[k + 2 * cn] = m[0] * src[k] + m[1] * src[k + cn] + m[2] * src[k + 2 * cn];
2688 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2689 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2690 int idxp1 = borderInterpolate(3, len, borderType)*cn;
2691 int idxp2 = borderInterpolate(4, len, borderType)*cn;
2692 for (int k = 0; k < cn; k++)
2694 ((uint16_t*)dst)[k] = ((uint16_t*)m)[2] * src[k] + ((uint16_t*)m)[1] * ((uint16_t)(src[k + cn]) + (uint16_t)(src[k + idxm1])) + ((uint16_t*)m)[0] * ((uint16_t)(src[k + 2 * cn]) + (uint16_t)(src[k + idxm2]));
2695 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[2] * src[k + cn] + ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[k + 2 * cn])) + ((uint16_t*)m)[0] * ((uint16_t)(src[k + idxm1]) + (uint16_t)(src[k + idxp1]));
2696 ((uint16_t*)dst)[k + 2 * cn] = ((uint16_t*)m)[0] * ((uint16_t)(src[k]) + (uint16_t)(src[k + idxp2])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k + cn]) + (uint16_t)(src[k + idxp1])) + ((uint16_t*)m)[2] * src[k + 2 * cn];
2702 // Points that fall left from border
2703 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2705 int idxm2 = borderInterpolate(-2, len, borderType)*cn;
2706 int idxm1 = borderInterpolate(-1, len, borderType)*cn;
2707 for (int k = 0; k < cn; k++)
2709 ((uint16_t*)dst)[k] = ((uint16_t*)m)[2] * src[k] + ((uint16_t*)m)[1] * ((uint16_t)(src[cn + k]) + (uint16_t)(src[idxm1 + k])) + ((uint16_t*)m)[0] * ((uint16_t)(src[2 * cn + k]) + (uint16_t)(src[idxm2 + k]));
2710 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[2 * cn + k])) + ((uint16_t*)m)[2] * src[cn + k] + ((uint16_t*)m)[0] * ((uint16_t)(src[3 * cn + k]) + (uint16_t)(src[idxm1 + k]));
2715 for (int k = 0; k < cn; k++)
2717 dst[k] = m[2] * src[k] + m[1] * src[cn + k] + m[0] * src[2 * cn + k];
2718 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[2 * cn + k])) + ((uint16_t*)m)[2] * src[cn + k] + ((uint16_t*)m)[0] * src[3 * cn + k];
2722 src += 2 * cn; dst += 2 * cn;
2723 int i = 2 * cn, lencn = (len - 2)*cn;
2724 v_uint16x8 v_mul0 = v_setall_u16(*((uint16_t*)m));
2725 v_uint16x8 v_mul1 = v_setall_u16(*((uint16_t*)(m + 1)));
2726 v_uint16x8 v_mul2 = v_setall_u16(*((uint16_t*)(m + 2)));
2727 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2729 v_uint16x8 v_src00, v_src01, v_src10, v_src11, v_src20, v_src21, v_src30, v_src31, v_src40, v_src41;
2730 v_expand(v_load(src - 2 * cn), v_src00, v_src01);
2731 v_expand(v_load(src - cn), v_src10, v_src11);
2732 v_expand(v_load(src), v_src20, v_src21);
2733 v_expand(v_load(src + cn), v_src30, v_src31);
2734 v_expand(v_load(src + 2 * cn), v_src40, v_src41);
2735 v_store((uint16_t*)dst, (v_src00 + v_src40) * v_mul0 + (v_src10 + v_src30)* v_mul1 + v_src20 * v_mul2);
2736 v_store((uint16_t*)dst + 8, (v_src01 + v_src41) * v_mul0 + (v_src11 + v_src31) * v_mul1 + v_src21 * v_mul2);
2738 for (; i < lencn; i++, src++, dst++)
2739 *((uint16_t*)dst) = ((uint16_t*)m)[0] * ((uint16_t)(src[-2 * cn]) + (uint16_t)(src[2 * cn])) + ((uint16_t*)m)[1] * ((uint16_t)(src[-cn]) + (uint16_t)(src[cn])) + ((uint16_t*)m)[2] * src[0];
2741 // Points that fall right from border
2742 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2744 int idxp1 = (borderInterpolate(len, len, borderType) - (len - 2))*cn;
2745 int idxp2 = (borderInterpolate(len + 1, len, borderType) - (len - 2))*cn;
2746 for (int k = 0; k < cn; k++)
2748 ((uint16_t*)dst)[k] = ((uint16_t*)m)[0] * ((uint16_t)(src[k - 2 * cn]) + (uint16_t)(src[idxp1 + k])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[k + cn])) + ((uint16_t*)m)[2] * src[k];
2749 ((uint16_t*)dst)[k + cn] = ((uint16_t*)m)[0] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[idxp2 + k])) + ((uint16_t*)m)[1] * ((uint16_t)(src[k]) + (uint16_t)(src[idxp1 + k])) + ((uint16_t*)m)[2] * src[k + cn];
2754 for (int k = 0; k < cn; k++)
2756 ((uint16_t*)dst)[k] = ((uint16_t*)m)[0] * src[k - 2 * cn] + ((uint16_t*)m)[1] * ((uint16_t)(src[k - cn]) + (uint16_t)(src[k + cn])) + ((uint16_t*)m)[2] * src[k];
2757 dst[k + cn] = m[0] * src[k - cn] + m[1] * src[k] + m[2] * src[k + cn];
2762 template <typename ET, typename FT>
2763 void hlineSmooth(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2765 int pre_shift = n / 2;
2766 int post_shift = n - pre_shift;
2768 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2770 for (int k = 0; k < cn; k++)
2771 dst[k] = m[pre_shift-i] * src[k];
2772 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2773 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2775 int src_idx = borderInterpolate(j, len, borderType);
2776 for (int k = 0; k < cn; k++)
2777 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2780 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2781 for (int k = 0; k < cn; k++)
2782 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2783 if (borderType != BORDER_CONSTANT)
2784 for (; j < i + post_shift; j++, mid++)
2786 int src_idx = borderInterpolate(j, len, borderType);
2787 for (int k = 0; k < cn; k++)
2788 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2792 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2794 *dst = m[0] * src[0];
2795 for (int j = 1; j < n; j++)
2796 *dst = *dst + m[j] * src[j*cn];
2799 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2801 for (int k = 0; k < cn; k++)
2802 dst[k] = m[0] * src[k];
2804 for (; j < len - i; j++)
2805 for (int k = 0; k < cn; k++)
2806 dst[k] = dst[k] + m[j] * src[j*cn + k];
2807 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2810 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2811 for (int k = 0; k < cn; k++)
2812 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2817 void hlineSmooth<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2819 int pre_shift = n / 2;
2820 int post_shift = n - pre_shift;
2822 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2824 for (int k = 0; k < cn; k++)
2825 dst[k] = m[pre_shift - i] * src[k];
2826 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2827 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2829 int src_idx = borderInterpolate(j, len, borderType);
2830 for (int k = 0; k < cn; k++)
2831 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2834 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2835 for (int k = 0; k < cn; k++)
2836 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2837 if (borderType != BORDER_CONSTANT)
2838 for (; j < i + post_shift; j++, mid++)
2840 int src_idx = borderInterpolate(j, len, borderType);
2841 for (int k = 0; k < cn; k++)
2842 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2846 int lencn = (len - post_shift + 1)*cn;
2847 for (; i <= lencn - 16; i+=16, src+=16, dst+=16)
2849 v_uint16x8 v_src0, v_src1;
2850 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
2851 v_expand(v_load(src), v_src0, v_src1);
2852 v_uint16x8 v_res0 = v_src0 * v_mul;
2853 v_uint16x8 v_res1 = v_src1 * v_mul;
2854 for (int j = 1; j < n; j++)
2856 v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2857 v_expand(v_load(src + j * cn), v_src0, v_src1);
2858 v_res0 += v_src0 * v_mul;
2859 v_res1 += v_src1 * v_mul;
2861 v_store((uint16_t*)dst, v_res0);
2862 v_store((uint16_t*)dst+8, v_res1);
2864 for (; i < lencn; i++, src++, dst++)
2866 *dst = m[0] * src[0];
2867 for (int j = 1; j < n; j++)
2868 *dst = *dst + m[j] * src[j*cn];
2871 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2873 for (int k = 0; k < cn; k++)
2874 dst[k] = m[0] * src[k];
2876 for (; j < len - i; j++)
2877 for (int k = 0; k < cn; k++)
2878 dst[k] = dst[k] + m[j] * src[j*cn + k];
2879 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2882 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2883 for (int k = 0; k < cn; k++)
2884 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2888 template <typename ET, typename FT>
2889 void hlineSmoothONa_yzy_a(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType)
2891 int pre_shift = n / 2;
2892 int post_shift = n - pre_shift;
2894 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2896 for (int k = 0; k < cn; k++)
2897 dst[k] = m[pre_shift - i] * src[k];
2898 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2899 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2901 int src_idx = borderInterpolate(j, len, borderType);
2902 for (int k = 0; k < cn; k++)
2903 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2906 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2907 for (int k = 0; k < cn; k++)
2908 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2909 if (borderType != BORDER_CONSTANT)
2910 for (; j < i + post_shift; j++, mid++)
2912 int src_idx = borderInterpolate(j, len, borderType);
2913 for (int k = 0; k < cn; k++)
2914 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2918 for (; i < (len - post_shift + 1)*cn; i++, src++, dst++)
2920 *dst = m[pre_shift] * src[pre_shift*cn];
2921 for (int j = 0; j < pre_shift; j++)
2922 *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n-1-j)*cn];
2925 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
2927 for (int k = 0; k < cn; k++)
2928 dst[k] = m[0] * src[k];
2930 for (; j < len - i; j++)
2931 for (int k = 0; k < cn; k++)
2932 dst[k] = dst[k] + m[j] * src[j*cn + k];
2933 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2936 int src_idx = borderInterpolate(i + j, len, borderType) - i;
2937 for (int k = 0; k < cn; k++)
2938 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
2943 void hlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const uint8_t* src, int cn, const ufixedpoint16* m, int n, ufixedpoint16* dst, int len, int borderType)
2945 int pre_shift = n / 2;
2946 int post_shift = n - pre_shift;
2948 for (; i < min(pre_shift, len); i++, dst += cn) // Points that fall left from border
2950 for (int k = 0; k < cn; k++)
2951 dst[k] = m[pre_shift - i] * src[k];
2952 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
2953 for (int j = i - pre_shift, mid = 0; j < 0; j++, mid++)
2955 int src_idx = borderInterpolate(j, len, borderType);
2956 for (int k = 0; k < cn; k++)
2957 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2960 for (j = 1, mid = pre_shift - i + 1; j < min(i + post_shift, len); j++, mid++)
2961 for (int k = 0; k < cn; k++)
2962 dst[k] = dst[k] + m[mid] * src[j*cn + k];
2963 if (borderType != BORDER_CONSTANT)
2964 for (; j < i + post_shift; j++, mid++)
2966 int src_idx = borderInterpolate(j, len, borderType);
2967 for (int k = 0; k < cn; k++)
2968 dst[k] = dst[k] + m[mid] * src[src_idx*cn + k];
2972 int lencn = (len - post_shift + 1)*cn;
2973 for (; i <= lencn - 16; i += 16, src += 16, dst += 16)
2975 v_uint16x8 v_src00, v_src01, v_srcN00, v_srcN01;
2977 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)(m + pre_shift)));
2978 v_expand(v_load(src + pre_shift * cn), v_src00, v_src01);
2979 v_uint16x8 v_res0 = v_src00 * v_mul;
2980 v_uint16x8 v_res1 = v_src01 * v_mul;
2981 for (int j = 0; j < pre_shift; j ++)
2983 v_mul = v_setall_u16(*((uint16_t*)(m + j)));
2984 v_expand(v_load(src + j * cn), v_src00, v_src01);
2985 v_expand(v_load(src + (n - 1 - j)*cn), v_srcN00, v_srcN01);
2986 v_res0 += (v_src00 + v_srcN00) * v_mul;
2987 v_res1 += (v_src01 + v_srcN01) * v_mul;
2990 v_store((uint16_t*)dst, v_res0);
2991 v_store((uint16_t*)dst + 8, v_res1);
2993 for (; i < lencn; i++, src++, dst++)
2995 *dst = m[pre_shift] * src[pre_shift*cn];
2996 for (int j = 0; j < pre_shift; j++)
2997 *dst = *dst + m[j] * src[j*cn] + m[j] * src[(n - 1 - j)*cn];
3000 for (i -= pre_shift; i < len - pre_shift; i++, src += cn, dst += cn) // Points that fall right from border
3002 for (int k = 0; k < cn; k++)
3003 dst[k] = m[0] * src[k];
3005 for (; j < len - i; j++)
3006 for (int k = 0; k < cn; k++)
3007 dst[k] = dst[k] + m[j] * src[j*cn + k];
3008 if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3011 int src_idx = borderInterpolate(i + j, len, borderType) - i;
3012 for (int k = 0; k < cn; k++)
3013 dst[k] = dst[k] + m[j] * src[src_idx*cn + k];
3017 template <typename ET, typename FT>
3018 void vlineSmooth1N(const FT* const * src, const FT* m, int, ET* dst, int len)
3020 const FT* src0 = src[0];
3021 for (int i = 0; i < len; i++)
3022 dst[i] = *m * src0[i];
3025 void vlineSmooth1N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3027 const ufixedpoint16* src0 = src[0];
3028 v_uint16x8 v_mul = v_setall_u16(*((uint16_t*)m));
3030 v_uint16x8 v_1 = v_setall_u16(1);
3034 for (; i <= len - 16; i += 16)
3036 v_uint16x8 v_src0 = v_load((uint16_t*)src0 + i);
3037 v_uint16x8 v_src1 = v_load((uint16_t*)src0 + i + 8);
3040 v_res.val = _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(v_1.val, _mm_mulhi_epu16(v_src0.val, v_mul.val)),1),
3041 _mm_srli_epi16(_mm_add_epi16(v_1.val, _mm_mulhi_epu16(v_src1.val, v_mul.val)),1));
3043 v_uint32x4 v_res0, v_res1, v_res2, v_res3;
3044 v_mul_expand(v_src0, v_mul, v_res0, v_res1);
3045 v_mul_expand(v_src1, v_mul, v_res2, v_res3);
3046 v_res = v_pack(v_rshr_pack<16>(v_res0, v_res1), v_rshr_pack<16>(v_res2, v_res3));
3048 v_store(dst + i, v_res);
3050 for (; i < len; i++)
3051 dst[i] = m[0] * src0[i];
3053 template <typename ET, typename FT>
3054 void vlineSmooth1N1(const FT* const * src, const FT*, int, ET* dst, int len)
3056 const FT* src0 = src[0];
3057 for (int i = 0; i < len; i++)
3061 void vlineSmooth1N1<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3063 const ufixedpoint16* src0 = src[0];
3065 for (; i <= len - 8; i += 8)
3066 v_rshr_pack_store<8>(dst + i, v_load((uint16_t*)(src0 + i)));
3067 for (; i < len; i++)
3070 template <typename ET, typename FT>
3071 void vlineSmooth3N(const FT* const * src, const FT* m, int, ET* dst, int len)
3073 for (int i = 0; i < len; i++)
3074 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3077 void vlineSmooth3N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3080 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3081 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3084 ufixedpoint32 val[] = { (m[0] + m[1] + m[2]) * ufixedpoint16((uint8_t)128) };
3085 v_128_4 = v_setall_s32(*((int32_t*)val));
3087 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3088 v_int16x8 v_mul2 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 2))));
3089 for (; i <= len - 32; i += 32)
3091 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3092 v_int16x8 v_tmp0, v_tmp1;
3094 v_src00 = v_load((int16_t*)(src[0]) + i);
3095 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3096 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3097 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3098 v_src10 = v_load((int16_t*)(src[1]) + i);
3099 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3100 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3101 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3102 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3103 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3104 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3105 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3106 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3107 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3108 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3109 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3110 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3111 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3112 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3113 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3115 v_int32x4 v_resj0, v_resj1;
3116 v_src00 = v_load((int16_t*)(src[2]) + i);
3117 v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3118 v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3119 v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3120 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul2, v_resj0, v_resj1);
3123 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul2, v_resj0, v_resj1);
3126 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul2, v_resj0, v_resj1);
3129 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul2, v_resj0, v_resj1);
3142 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3143 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3144 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3145 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3147 for (; i < len; i++)
3148 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i];
3150 template <typename ET, typename FT>
3151 void vlineSmooth3N121(const FT* const * src, const FT*, int, ET* dst, int len)
3153 for (int i = 0; i < len; i++)
3154 dst[i] = (FT::WT(src[0][i]) >> 2) + (FT::WT(src[2][i]) >> 2) + (FT::WT(src[1][i]) >> 1);
3157 void vlineSmooth3N121<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3160 for (; i <= len - 16; i += 16)
3162 v_uint32x4 v_src00, v_src01, v_src02, v_src03, v_src10, v_src11, v_src12, v_src13, v_src20, v_src21, v_src22, v_src23;
3163 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3164 v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3165 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3166 v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3167 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3168 v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3169 v_store(dst + i, v_pack(v_rshr_pack<10>(v_src00 + v_src20 + (v_src10 + v_src10), v_src01 + v_src21 + (v_src11 + v_src11)),
3170 v_rshr_pack<10>(v_src02 + v_src22 + (v_src12 + v_src12), v_src03 + v_src23 + (v_src13 + v_src13))));
3172 for (; i < len; i++)
3173 dst[i] = (((uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[2]))[i]) + ((uint32_t)(((uint16_t*)(src[1]))[i]) << 1)) + (1 << 9)) >> 10;
3175 template <typename ET, typename FT>
3176 void vlineSmooth5N(const FT* const * src, const FT* m, int, ET* dst, int len)
3178 for (int i = 0; i < len; i++)
3179 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
3182 void vlineSmooth5N<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int, uint8_t* dst, int len)
3185 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3186 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3189 ufixedpoint32 val[] = { (m[0] + m[1] + m[2] + m[3] + m[4]) * ufixedpoint16((uint8_t)128) };
3190 v_128_4 = v_setall_s32(*((int32_t*)val));
3192 v_int16x8 v_mul01 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3193 v_int16x8 v_mul23 = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m + 2))));
3194 v_int16x8 v_mul4 = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + 4))));
3195 for (; i <= len - 32; i += 32)
3197 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3198 v_int16x8 v_tmp0, v_tmp1;
3200 v_src00 = v_load((int16_t*)(src[0]) + i);
3201 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3202 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3203 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3204 v_src10 = v_load((int16_t*)(src[1]) + i);
3205 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3206 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3207 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3208 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3209 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul01);
3210 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul01);
3211 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3212 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul01);
3213 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul01);
3214 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3215 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul01);
3216 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul01);
3217 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3218 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul01);
3219 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul01);
3221 v_src00 = v_load((int16_t*)(src[2]) + i);
3222 v_src01 = v_load((int16_t*)(src[2]) + i + 8);
3223 v_src02 = v_load((int16_t*)(src[2]) + i + 16);
3224 v_src03 = v_load((int16_t*)(src[2]) + i + 24);
3225 v_src10 = v_load((int16_t*)(src[3]) + i);
3226 v_src11 = v_load((int16_t*)(src[3]) + i + 8);
3227 v_src12 = v_load((int16_t*)(src[3]) + i + 16);
3228 v_src13 = v_load((int16_t*)(src[3]) + i + 24);
3229 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3230 v_res0 += v_dotprod(v_tmp0, v_mul23);
3231 v_res1 += v_dotprod(v_tmp1, v_mul23);
3232 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3233 v_res2 += v_dotprod(v_tmp0, v_mul23);
3234 v_res3 += v_dotprod(v_tmp1, v_mul23);
3235 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3236 v_res4 += v_dotprod(v_tmp0, v_mul23);
3237 v_res5 += v_dotprod(v_tmp1, v_mul23);
3238 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3239 v_res6 += v_dotprod(v_tmp0, v_mul23);
3240 v_res7 += v_dotprod(v_tmp1, v_mul23);
3242 v_int32x4 v_resj0, v_resj1;
3243 v_src00 = v_load((int16_t*)(src[4]) + i);
3244 v_src01 = v_load((int16_t*)(src[4]) + i + 8);
3245 v_src02 = v_load((int16_t*)(src[4]) + i + 16);
3246 v_src03 = v_load((int16_t*)(src[4]) + i + 24);
3247 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul4, v_resj0, v_resj1);
3250 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul4, v_resj0, v_resj1);
3253 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul4, v_resj0, v_resj1);
3256 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul4, v_resj0, v_resj1);
3269 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3270 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3271 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3272 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3274 for (; i < len; i++)
3275 dst[i] = m[0] * src[0][i] + m[1] * src[1][i] + m[2] * src[2][i] + m[3] * src[3][i] + m[4] * src[4][i];
3277 template <typename ET, typename FT>
3278 void vlineSmooth5N14641(const FT* const * src, const FT*, int, ET* dst, int len)
3280 for (int i = 0; i < len; i++)
3281 dst[i] = (FT::WT(src[2][i])*(uint8_t)6 + ((FT::WT(src[1][i]) + FT::WT(src[3][i]))<<2) + FT::WT(src[0][i]) + FT::WT(src[4][i])) >> 4;
3284 void vlineSmooth5N14641<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16*, int, uint8_t* dst, int len)
3287 v_uint32x4 v_6 = v_setall_u32(6);
3288 for (; i <= len - 16; i += 16)
3290 v_uint32x4 v_src00, v_src10, v_src20, v_src30, v_src40;
3291 v_uint32x4 v_src01, v_src11, v_src21, v_src31, v_src41;
3292 v_uint32x4 v_src02, v_src12, v_src22, v_src32, v_src42;
3293 v_uint32x4 v_src03, v_src13, v_src23, v_src33, v_src43;
3294 v_expand(v_load((uint16_t*)(src[0]) + i), v_src00, v_src01);
3295 v_expand(v_load((uint16_t*)(src[0]) + i + 8), v_src02, v_src03);
3296 v_expand(v_load((uint16_t*)(src[1]) + i), v_src10, v_src11);
3297 v_expand(v_load((uint16_t*)(src[1]) + i + 8), v_src12, v_src13);
3298 v_expand(v_load((uint16_t*)(src[2]) + i), v_src20, v_src21);
3299 v_expand(v_load((uint16_t*)(src[2]) + i + 8), v_src22, v_src23);
3300 v_expand(v_load((uint16_t*)(src[3]) + i), v_src30, v_src31);
3301 v_expand(v_load((uint16_t*)(src[3]) + i + 8), v_src32, v_src33);
3302 v_expand(v_load((uint16_t*)(src[4]) + i), v_src40, v_src41);
3303 v_expand(v_load((uint16_t*)(src[4]) + i + 8), v_src42, v_src43);
3304 v_store(dst + i, v_pack(v_rshr_pack<12>(v_src20*v_6 + ((v_src10 + v_src30) << 2) + v_src00 + v_src40,
3305 v_src21*v_6 + ((v_src11 + v_src31) << 2) + v_src01 + v_src41),
3306 v_rshr_pack<12>(v_src22*v_6 + ((v_src12 + v_src32) << 2) + v_src02 + v_src42,
3307 v_src23*v_6 + ((v_src13 + v_src33) << 2) + v_src03 + v_src43)));
3309 for (; i < len; i++)
3310 dst[i] = ((uint32_t)(((uint16_t*)(src[2]))[i]) * 6 +
3311 (((uint32_t)(((uint16_t*)(src[1]))[i]) + (uint32_t)(((uint16_t*)(src[3]))[i])) << 2) +
3312 (uint32_t)(((uint16_t*)(src[0]))[i]) + (uint32_t)(((uint16_t*)(src[4]))[i]) + (1 << 11)) >> 12;
3314 template <typename ET, typename FT>
3315 void vlineSmooth(const FT* const * src, const FT* m, int n, ET* dst, int len)
3317 for (int i = 0; i < len; i++)
3319 typename FT::WT val = m[0] * src[0][i];
3320 for (int j = 1; j < n; j++)
3321 val = val + m[j] * src[j][i];
3326 void vlineSmooth<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3329 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3330 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3333 ufixedpoint16 msum = m[0] + m[1];
3334 for (int j = 2; j < n; j++)
3336 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3337 v_128_4 = v_setall_s32(*((int32_t*)val));
3339 for (; i <= len - 32; i += 32)
3341 v_int16x8 v_src00, v_src10, v_src01, v_src11, v_src02, v_src12, v_src03, v_src13;
3342 v_int16x8 v_tmp0, v_tmp1;
3344 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)m)));
3346 v_src00 = v_load((int16_t*)(src[0]) + i);
3347 v_src01 = v_load((int16_t*)(src[0]) + i + 8);
3348 v_src02 = v_load((int16_t*)(src[0]) + i + 16);
3349 v_src03 = v_load((int16_t*)(src[0]) + i + 24);
3350 v_src10 = v_load((int16_t*)(src[1]) + i);
3351 v_src11 = v_load((int16_t*)(src[1]) + i + 8);
3352 v_src12 = v_load((int16_t*)(src[1]) + i + 16);
3353 v_src13 = v_load((int16_t*)(src[1]) + i + 24);
3354 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3355 v_int32x4 v_res0 = v_dotprod(v_tmp0, v_mul);
3356 v_int32x4 v_res1 = v_dotprod(v_tmp1, v_mul);
3357 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3358 v_int32x4 v_res2 = v_dotprod(v_tmp0, v_mul);
3359 v_int32x4 v_res3 = v_dotprod(v_tmp1, v_mul);
3360 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3361 v_int32x4 v_res4 = v_dotprod(v_tmp0, v_mul);
3362 v_int32x4 v_res5 = v_dotprod(v_tmp1, v_mul);
3363 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3364 v_int32x4 v_res6 = v_dotprod(v_tmp0, v_mul);
3365 v_int32x4 v_res7 = v_dotprod(v_tmp1, v_mul);
3368 for (; j < n - 1; j+=2)
3370 v_mul = v_reinterpret_as_s16(v_setall_u32(*((uint32_t*)(m+j))));
3372 v_src00 = v_load((int16_t*)(src[j]) + i);
3373 v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3374 v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3375 v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3376 v_src10 = v_load((int16_t*)(src[j+1]) + i);
3377 v_src11 = v_load((int16_t*)(src[j+1]) + i + 8);
3378 v_src12 = v_load((int16_t*)(src[j+1]) + i + 16);
3379 v_src13 = v_load((int16_t*)(src[j+1]) + i + 24);
3380 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src10, v_128), v_tmp0, v_tmp1);
3381 v_res0 += v_dotprod(v_tmp0, v_mul);
3382 v_res1 += v_dotprod(v_tmp1, v_mul);
3383 v_zip(v_add_wrap(v_src01, v_128), v_add_wrap(v_src11, v_128), v_tmp0, v_tmp1);
3384 v_res2 += v_dotprod(v_tmp0, v_mul);
3385 v_res3 += v_dotprod(v_tmp1, v_mul);
3386 v_zip(v_add_wrap(v_src02, v_128), v_add_wrap(v_src12, v_128), v_tmp0, v_tmp1);
3387 v_res4 += v_dotprod(v_tmp0, v_mul);
3388 v_res5 += v_dotprod(v_tmp1, v_mul);
3389 v_zip(v_add_wrap(v_src03, v_128), v_add_wrap(v_src13, v_128), v_tmp0, v_tmp1);
3390 v_res6 += v_dotprod(v_tmp0, v_mul);
3391 v_res7 += v_dotprod(v_tmp1, v_mul);
3395 v_int32x4 v_resj0, v_resj1;
3396 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3397 v_src00 = v_load((int16_t*)(src[j]) + i);
3398 v_src01 = v_load((int16_t*)(src[j]) + i + 8);
3399 v_src02 = v_load((int16_t*)(src[j]) + i + 16);
3400 v_src03 = v_load((int16_t*)(src[j]) + i + 24);
3401 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_resj0, v_resj1);
3404 v_mul_expand(v_add_wrap(v_src01, v_128), v_mul, v_resj0, v_resj1);
3407 v_mul_expand(v_add_wrap(v_src02, v_128), v_mul, v_resj0, v_resj1);
3410 v_mul_expand(v_add_wrap(v_src03, v_128), v_mul, v_resj0, v_resj1);
3423 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3424 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3425 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3426 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3428 for (; i < len; i++)
3430 ufixedpoint32 val = m[0] * src[0][i];
3431 for (int j = 1; j < n; j++)
3433 val = val + m[j] * src[j][i];
3438 template <typename ET, typename FT>
3439 void vlineSmoothONa_yzy_a(const FT* const * src, const FT* m, int n, ET* dst, int len)
3441 int pre_shift = n / 2;
3442 for (int i = 0; i < len; i++)
3444 typename FT::WT val = m[pre_shift] * src[pre_shift][i];
3445 for (int j = 0; j < pre_shift; j++)
3446 val = val + m[j] * src[j][i] + m[j] * src[(n - 1 - j)][i];
3451 void vlineSmoothONa_yzy_a<uint8_t, ufixedpoint16>(const ufixedpoint16* const * src, const ufixedpoint16* m, int n, uint8_t* dst, int len)
3453 int pre_shift = n / 2;
3455 static const v_int16x8 v_128 = v_reinterpret_as_s16(v_setall_u16((uint16_t)1 << 15));
3456 v_int32x4 v_128_4 = v_setall_s32(128 << 16);
3459 ufixedpoint16 msum = m[0] + m[pre_shift] + m[n - 1];
3460 for (int j = 1; j < pre_shift; j++)
3461 msum = msum + m[j] + m[n - 1 - j];
3462 ufixedpoint32 val[] = { msum * ufixedpoint16((uint8_t)128) };
3463 v_128_4 = v_setall_s32(*((int32_t*)val));
3465 for (; i <= len - 32; i += 32)
3467 v_int16x8 v_src00, v_src10, v_src20, v_src30, v_src01, v_src11, v_src21, v_src31;
3468 v_int32x4 v_res0, v_res1, v_res2, v_res3, v_res4, v_res5, v_res6, v_res7;
3469 v_int16x8 v_tmp0, v_tmp1, v_tmp2, v_tmp3, v_tmp4, v_tmp5, v_tmp6, v_tmp7;
3471 v_int16x8 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + pre_shift))));
3472 v_src00 = v_load((int16_t*)(src[pre_shift]) + i);
3473 v_src10 = v_load((int16_t*)(src[pre_shift]) + i + 8);
3474 v_src20 = v_load((int16_t*)(src[pre_shift]) + i + 16);
3475 v_src30 = v_load((int16_t*)(src[pre_shift]) + i + 24);
3476 v_mul_expand(v_add_wrap(v_src00, v_128), v_mul, v_res0, v_res1);
3477 v_mul_expand(v_add_wrap(v_src10, v_128), v_mul, v_res2, v_res3);
3478 v_mul_expand(v_add_wrap(v_src20, v_128), v_mul, v_res4, v_res5);
3479 v_mul_expand(v_add_wrap(v_src30, v_128), v_mul, v_res6, v_res7);
3482 for (; j < pre_shift; j++)
3484 v_mul = v_reinterpret_as_s16(v_setall_u16(*((uint16_t*)(m + j))));
3486 v_src00 = v_load((int16_t*)(src[j]) + i);
3487 v_src10 = v_load((int16_t*)(src[j]) + i + 8);
3488 v_src20 = v_load((int16_t*)(src[j]) + i + 16);
3489 v_src30 = v_load((int16_t*)(src[j]) + i + 24);
3490 v_src01 = v_load((int16_t*)(src[n - 1 - j]) + i);
3491 v_src11 = v_load((int16_t*)(src[n - 1 - j]) + i + 8);
3492 v_src21 = v_load((int16_t*)(src[n - 1 - j]) + i + 16);
3493 v_src31 = v_load((int16_t*)(src[n - 1 - j]) + i + 24);
3494 v_zip(v_add_wrap(v_src00, v_128), v_add_wrap(v_src01, v_128), v_tmp0, v_tmp1);
3495 v_res0 += v_dotprod(v_tmp0, v_mul);
3496 v_res1 += v_dotprod(v_tmp1, v_mul);
3497 v_zip(v_add_wrap(v_src10, v_128), v_add_wrap(v_src11, v_128), v_tmp2, v_tmp3);
3498 v_res2 += v_dotprod(v_tmp2, v_mul);
3499 v_res3 += v_dotprod(v_tmp3, v_mul);
3500 v_zip(v_add_wrap(v_src20, v_128), v_add_wrap(v_src21, v_128), v_tmp4, v_tmp5);
3501 v_res4 += v_dotprod(v_tmp4, v_mul);
3502 v_res5 += v_dotprod(v_tmp5, v_mul);
3503 v_zip(v_add_wrap(v_src30, v_128), v_add_wrap(v_src31, v_128), v_tmp6, v_tmp7);
3504 v_res6 += v_dotprod(v_tmp6, v_mul);
3505 v_res7 += v_dotprod(v_tmp7, v_mul);
3517 v_store(dst + i , v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res0, v_res1)),
3518 v_reinterpret_as_u16(v_rshr_pack<16>(v_res2, v_res3))));
3519 v_store(dst + i + 16, v_pack(v_reinterpret_as_u16(v_rshr_pack<16>(v_res4, v_res5)),
3520 v_reinterpret_as_u16(v_rshr_pack<16>(v_res6, v_res7))));
3522 for (; i < len; i++)
3524 ufixedpoint32 val = m[0] * src[0][i];
3525 for (int j = 1; j < n; j++)
3527 val = val + m[j] * src[j][i];
3532 template <typename ET, typename FT>
3533 class fixedSmoothInvoker : public ParallelLoopBody
3536 fixedSmoothInvoker(const ET* _src, size_t _src_stride, ET* _dst, size_t _dst_stride,
3537 int _width, int _height, int _cn, const FT* _kx, int _kxlen, const FT* _ky, int _kylen, int _borderType) : ParallelLoopBody(),
3538 src(_src), dst(_dst), src_stride(_src_stride), dst_stride(_dst_stride),
3539 width(_width), height(_height), cn(_cn), kx(_kx), ky(_ky), kxlen(_kxlen), kylen(_kylen), borderType(_borderType)
3543 if (kx[0] == FT::one())
3544 hlineSmoothFunc = hlineSmooth1N1;
3546 hlineSmoothFunc = hlineSmooth1N;
3548 else if (kxlen == 3)
3550 if (kx[0] == (FT::one()>>2)&&kx[1] == (FT::one()>>1)&&kx[2] == (FT::one()>>2))
3551 hlineSmoothFunc = hlineSmooth3N121;
3552 else if ((kx[0] - kx[2]).isZero())
3553 hlineSmoothFunc = hlineSmooth3Naba;
3555 hlineSmoothFunc = hlineSmooth3N;
3557 else if (kxlen == 5)
3559 if (kx[2] == (FT::one()*(uint8_t)3>>3) &&
3560 kx[1] == (FT::one()>>2) && kx[3] == (FT::one()>>2) &&
3561 kx[0] == (FT::one()>>4) && kx[4] == (FT::one()>>4))
3562 hlineSmoothFunc = hlineSmooth5N14641;
3563 else if (kx[0] == kx[4] && kx[1] == kx[3])
3564 hlineSmoothFunc = hlineSmooth5Nabcba;
3566 hlineSmoothFunc = hlineSmooth5N;
3568 else if (kxlen % 2 == 1)
3570 hlineSmoothFunc = hlineSmoothONa_yzy_a;
3571 for (int i = 0; i < kxlen / 2; i++)
3572 if (!(kx[i] == kx[kxlen - 1 - i]))
3574 hlineSmoothFunc = hlineSmooth;
3579 hlineSmoothFunc = hlineSmooth;
3582 if (ky[0] == FT::one())
3583 vlineSmoothFunc = vlineSmooth1N1;
3585 vlineSmoothFunc = vlineSmooth1N;
3587 else if (kylen == 3)
3589 if (ky[0] == (FT::one() >> 2) && ky[1] == (FT::one() >> 1) && ky[2] == (FT::one() >> 2))
3590 vlineSmoothFunc = vlineSmooth3N121;
3592 vlineSmoothFunc = vlineSmooth3N;
3594 else if (kylen == 5)
3596 if (ky[2] == (FT::one() * (uint8_t)3 >> 3) &&
3597 ky[1] == (FT::one() >> 2) && ky[3] == (FT::one() >> 2) &&
3598 ky[0] == (FT::one() >> 4) && ky[4] == (FT::one() >> 4))
3599 vlineSmoothFunc = vlineSmooth5N14641;
3601 vlineSmoothFunc = vlineSmooth5N;
3603 else if (kylen % 2 == 1)
3605 vlineSmoothFunc = vlineSmoothONa_yzy_a;
3606 for (int i = 0; i < kylen / 2; i++)
3607 if (!(ky[i] == ky[kylen - 1 - i]))
3609 vlineSmoothFunc = vlineSmooth;
3614 vlineSmoothFunc = vlineSmooth;
3616 virtual void operator() (const Range& range) const CV_OVERRIDE
3618 AutoBuffer<FT> _buf(width*cn*kylen);
3619 FT* buf = _buf.data();
3620 AutoBuffer<FT*> _ptrs(kylen*2);
3621 FT** ptrs = _ptrs.data();
3626 for (int i = range.start; i < range.end; i++)
3628 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[0], width, borderType);
3629 vlineSmoothFunc(ptrs, ky, kylen, dst + i * dst_stride, width*cn);
3632 else if (borderType != BORDER_CONSTANT)// If BORDER_CONSTANT out of border values are equal to zero and could be skipped
3634 int pre_shift = kylen / 2;
3635 int post_shift = kylen - pre_shift - 1;
3636 // First line evaluation
3637 int idst = range.start;
3638 int ifrom = max(0, idst - pre_shift);
3639 int ito = idst + post_shift + 1;
3642 for (; i < min(ito, height); i++, bufline++)
3644 ptrs[bufline+kylen] = ptrs[bufline] = buf + bufline * width*cn;
3645 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3647 for (; i < ito; i++, bufline++)
3649 int src_idx = borderInterpolate(i, height, borderType);
3650 if (src_idx < ifrom)
3652 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3653 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3657 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[src_idx - ifrom];
3660 for (int j = idst - pre_shift; j < 0; j++)
3662 int src_idx = borderInterpolate(j, height, borderType);
3665 ptrs[2*kylen + j] = ptrs[kylen + j] = buf + (kylen + j) * width*cn;
3666 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[kylen + j], width, borderType);
3670 ptrs[2*kylen + j] = ptrs[kylen + j] = ptrs[src_idx];
3673 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn); idst++;
3675 // border mode dependent part evaluation
3676 // i points to last src row to evaluate in convolution
3677 bufline %= kylen; ito = min(height, range.end + post_shift);
3678 for (; i < min(kylen, ito); i++, idst++)
3680 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3681 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3682 bufline = (bufline + 1) % kylen;
3683 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3685 // Points inside the border
3686 for (; i < ito; i++, idst++)
3688 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3689 bufline = (bufline + 1) % kylen;
3690 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3692 // Points that could fall below border
3693 for (; i < range.end + post_shift; i++, idst++)
3695 int src_idx = borderInterpolate(i, height, borderType);
3696 if ((i - src_idx) > kylen)
3697 hlineSmoothFunc(src + src_idx * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3699 ptrs[bufline + kylen] = ptrs[bufline] = ptrs[(bufline + kylen - (i - src_idx)) % kylen];
3700 bufline = (bufline + 1) % kylen;
3701 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3706 int pre_shift = kylen / 2;
3707 int post_shift = kylen - pre_shift - 1;
3708 // First line evaluation
3709 int idst = range.start;
3710 int ifrom = idst - pre_shift;
3711 int ito = min(idst + post_shift + 1, height);
3712 int i = max(0, ifrom);
3714 for (; i < ito; i++, bufline++)
3716 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3717 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3721 vlineSmooth1N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3722 else if (bufline == 3)
3723 vlineSmooth3N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3724 else if (bufline == 5)
3725 vlineSmooth5N(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3727 vlineSmooth(ptrs, ky - min(ifrom, 0), bufline, dst + idst*dst_stride, width*cn);
3730 // border mode dependent part evaluation
3731 // i points to last src row to evaluate in convolution
3732 bufline %= kylen; ito = min(height, range.end + post_shift);
3733 for (; i < min(kylen, ito); i++, idst++)
3735 ptrs[bufline + kylen] = ptrs[bufline] = buf + bufline * width*cn;
3736 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3739 vlineSmooth3N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3740 else if (bufline == 5)
3741 vlineSmooth5N(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3743 vlineSmooth(ptrs, ky + kylen - bufline, i + 1, dst + idst*dst_stride, width*cn);
3746 // Points inside the border
3747 if (i - max(0, ifrom) >= kylen)
3749 for (; i < ito; i++, idst++)
3751 hlineSmoothFunc(src + i * src_stride, cn, kx, kxlen, ptrs[bufline], width, borderType);
3752 bufline = (bufline + 1) % kylen;
3753 vlineSmoothFunc(ptrs + bufline, ky, kylen, dst + idst*dst_stride, width*cn);
3756 // Points that could fall below border
3757 // i points to first src row to evaluate in convolution
3758 bufline = (bufline + 1) % kylen;
3759 for (i = idst - pre_shift; i < range.end - pre_shift; i++, idst++, bufline++)
3760 if (height - i == 3)
3761 vlineSmooth3N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3762 else if (height - i == 5)
3763 vlineSmooth5N(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3765 vlineSmooth(ptrs + bufline, ky, height - i, dst + idst*dst_stride, width*cn);
3769 // i points to first src row to evaluate in convolution
3770 for (i = idst - pre_shift; i < min(range.end - pre_shift, 0); i++, idst++)
3772 vlineSmooth3N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3773 else if (height == 5)
3774 vlineSmooth5N(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3776 vlineSmooth(ptrs, ky - i, height, dst + idst*dst_stride, width*cn);
3777 for (; i < range.end - pre_shift; i++, idst++)
3778 if (height - i == 3)
3779 vlineSmooth3N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3780 else if (height - i == 5)
3781 vlineSmooth5N(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3783 vlineSmooth(ptrs + i - max(0, ifrom), ky, height - i, dst + idst*dst_stride, width*cn);
3790 size_t src_stride, dst_stride;
3791 int width, height, cn;
3795 void(*hlineSmoothFunc)(const ET* src, int cn, const FT* m, int n, FT* dst, int len, int borderType);
3796 void(*vlineSmoothFunc)(const FT* const * src, const FT* m, int n, ET* dst, int len);
3798 fixedSmoothInvoker(const fixedSmoothInvoker&);
3799 fixedSmoothInvoker& operator=(const fixedSmoothInvoker&);
3802 static void getGaussianKernel(int n, double sigma, int ktype, Mat& res) { res = getGaussianKernel(n, sigma, ktype); }
3803 template <typename T> static void getGaussianKernel(int n, double sigma, int, std::vector<T>& res) { res = getFixedpointGaussianKernel<T>(n, sigma); }
3805 template <typename T>
3806 static void createGaussianKernels( T & kx, T & ky, int type, Size &ksize,
3807 double sigma1, double sigma2 )
3809 int depth = CV_MAT_DEPTH(type);
3813 // automatic detection of kernel size from sigma
3814 if( ksize.width <= 0 && sigma1 > 0 )
3815 ksize.width = cvRound(sigma1*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3816 if( ksize.height <= 0 && sigma2 > 0 )
3817 ksize.height = cvRound(sigma2*(depth == CV_8U ? 3 : 4)*2 + 1)|1;
3819 CV_Assert( ksize.width > 0 && ksize.width % 2 == 1 &&
3820 ksize.height > 0 && ksize.height % 2 == 1 );
3822 sigma1 = std::max( sigma1, 0. );
3823 sigma2 = std::max( sigma2, 0. );
3825 getGaussianKernel( ksize.width, sigma1, std::max(depth, CV_32F), kx );
3826 if( ksize.height == ksize.width && std::abs(sigma1 - sigma2) < DBL_EPSILON )
3829 getGaussianKernel( ksize.height, sigma2, std::max(depth, CV_32F), ky );
3834 cv::Ptr<cv::FilterEngine> cv::createGaussianFilter( int type, Size ksize,
3835 double sigma1, double sigma2,
3839 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
3841 return createSeparableLinearFilter( type, type, kx, ky, Point(-1,-1), 0, borderType );
3848 static bool ocl_GaussianBlur_8UC1(InputArray _src, OutputArray _dst, Size ksize, int ddepth,
3849 InputArray _kernelX, InputArray _kernelY, int borderType)
3851 const ocl::Device & dev = ocl::Device::getDefault();
3852 int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
3854 if ( !(dev.isIntel() && (type == CV_8UC1) &&
3855 (_src.offset() == 0) && (_src.step() % 4 == 0) &&
3856 ((ksize.width == 5 && (_src.cols() % 4 == 0)) ||
3857 (ksize.width == 3 && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)))) )
3860 Mat kernelX = _kernelX.getMat().reshape(1, 1);
3861 if (kernelX.cols % 2 != 1)
3863 Mat kernelY = _kernelY.getMat().reshape(1, 1);
3864 if (kernelY.cols % 2 != 1)
3870 Size size = _src.size();
3871 size_t globalsize[2] = { 0, 0 };
3872 size_t localsize[2] = { 0, 0 };
3874 if (ksize.width == 3)
3876 globalsize[0] = size.width / 16;
3877 globalsize[1] = size.height / 2;
3879 else if (ksize.width == 5)
3881 globalsize[0] = size.width / 4;
3882 globalsize[1] = size.height / 1;
3885 const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" };
3886 char build_opts[1024];
3887 sprintf(build_opts, "-D %s %s%s", borderMap[borderType & ~BORDER_ISOLATED],
3888 ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(),
3889 ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str());
3893 if (ksize.width == 3)
3894 kernel.create("gaussianBlur3x3_8UC1_cols16_rows2", cv::ocl::imgproc::gaussianBlur3x3_oclsrc, build_opts);
3895 else if (ksize.width == 5)
3896 kernel.create("gaussianBlur5x5_8UC1_cols4", cv::ocl::imgproc::gaussianBlur5x5_oclsrc, build_opts);
3901 UMat src = _src.getUMat();
3902 _dst.create(size, CV_MAKETYPE(ddepth, cn));
3903 if (!(_dst.offset() == 0 && _dst.step() % 4 == 0))
3905 UMat dst = _dst.getUMat();
3907 int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src));
3908 idxArg = kernel.set(idxArg, (int)src.step);
3909 idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst));
3910 idxArg = kernel.set(idxArg, (int)dst.step);
3911 idxArg = kernel.set(idxArg, (int)dst.rows);
3912 idxArg = kernel.set(idxArg, (int)dst.cols);
3914 return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false);
3922 template <> inline bool skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(int w, int h) { return w*h < 320 * 240; }
3924 static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
3925 double sigma1, double sigma2, int borderType)
3929 // automatic detection of kernel size from sigma
3930 if (ksize.width <= 0 && sigma1 > 0)
3931 ksize.width = cvRound(sigma1*6 + 1) | 1;
3932 if (ksize.height <= 0 && sigma2 > 0)
3933 ksize.height = cvRound(sigma2*6 + 1) | 1;
3935 if (_src.type() != CV_8UC1 ||
3936 _src.cols() < 3 || _src.rows() < 3 ||
3937 ksize.width != 3 || ksize.height != 3)
3940 sigma1 = std::max(sigma1, 0.);
3941 sigma2 = std::max(sigma2, 0.);
3943 if (!(sigma1 == 0.0 || (sigma1 - 0.8) < DBL_EPSILON) || !(sigma2 == 0.0 || (sigma2 - 0.8) < DBL_EPSILON) ||
3944 ovx::skipSmallImages<VX_KERNEL_GAUSSIAN_3x3>(_src.cols(), _src.rows()))
3947 Mat src = _src.getMat();
3948 Mat dst = _dst.getMat();
3950 if ((borderType & BORDER_ISOLATED) == 0 && src.isSubmatrix())
3951 return false; //Process isolated borders only
3953 switch (borderType & ~BORDER_ISOLATED)
3955 case BORDER_CONSTANT:
3956 border = VX_BORDER_CONSTANT;
3958 case BORDER_REPLICATE:
3959 border = VX_BORDER_REPLICATE;
3967 ivx::Context ctx = ovx::getOpenVXContext();
3970 if (dst.data != src.data)
3976 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3977 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
3978 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
3979 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
3981 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
3982 //since OpenVX standard says nothing about thread-safety for now
3983 ivx::border_t prevBorder = ctx.immediateBorder();
3984 ctx.setImmediateBorder(border, (vx_uint8)(0));
3985 ivx::IVX_CHECK_STATUS(vxuGaussian3x3(ctx, ia, ib));
3986 ctx.setImmediateBorder(prevBorder);
3988 catch (ivx::RuntimeError & e)
3990 VX_DbgThrow(e.what());
3992 catch (ivx::WrapperError & e)
3994 VX_DbgThrow(e.what());
4002 // IW 2017u2 has bug which doesn't allow use of partial inMem with tiling
4003 #if IPP_DISABLE_GAUSSIANBLUR_PARALLEL
4004 #define IPP_GAUSSIANBLUR_PARALLEL 0
4006 #define IPP_GAUSSIANBLUR_PARALLEL 1
4011 class ipp_gaussianBlurParallel: public ParallelLoopBody
4014 ipp_gaussianBlurParallel(::ipp::IwiImage &src, ::ipp::IwiImage &dst, int kernelSize, float sigma, ::ipp::IwiBorderType &border, bool *pOk):
4015 m_src(src), m_dst(dst), m_kernelSize(kernelSize), m_sigma(sigma), m_border(border), m_pOk(pOk) {
4018 ~ipp_gaussianBlurParallel()
4022 virtual void operator() (const Range& range) const CV_OVERRIDE
4024 CV_INSTRUMENT_REGION_IPP()
4031 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, m_dst.m_size.width, range.end - range.start);
4032 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, m_src, m_dst, m_kernelSize, m_sigma, ::ipp::IwDefault(), m_border, tile);
4034 catch(::ipp::IwException e)
4041 ::ipp::IwiImage &m_src;
4042 ::ipp::IwiImage &m_dst;
4046 ::ipp::IwiBorderType &m_border;
4048 volatile bool *m_pOk;
4049 const ipp_gaussianBlurParallel& operator= (const ipp_gaussianBlurParallel&);
4054 static bool ipp_GaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
4055 double sigma1, double sigma2, int borderType )
4058 CV_INSTRUMENT_REGION_IPP()
4060 #if IPP_VERSION_X100 < 201800 && ((defined _MSC_VER && defined _M_IX86) || (defined __GNUC__ && defined __i386__))
4061 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4062 return false; // bug on ia32
4064 if(sigma1 != sigma2)
4067 if(sigma1 < FLT_EPSILON)
4070 if(ksize.width != ksize.height)
4073 // Acquire data and begin processing
4076 Mat src = _src.getMat();
4077 Mat dst = _dst.getMat();
4078 ::ipp::IwiImage iwSrc = ippiGetImage(src);
4079 ::ipp::IwiImage iwDst = ippiGetImage(dst);
4080 ::ipp::IwiBorderSize borderSize = ::ipp::iwiSizeToBorderSize(ippiGetSize(ksize));
4081 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
4085 const int threads = ippiSuggestThreadsNum(iwDst, 2);
4086 if(IPP_GAUSSIANBLUR_PARALLEL && threads > 1) {
4088 ipp_gaussianBlurParallel invoker(iwSrc, iwDst, ksize.width, (float) sigma1, ippBorder, &ok);
4092 const Range range(0, (int) iwDst.m_size.height);
4093 parallel_for_(range, invoker, threads*4);
4098 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterGaussian, iwSrc, iwDst, ksize.width, sigma1, ::ipp::IwDefault(), ippBorder);
4101 catch (::ipp::IwException ex)
4109 CV_UNUSED(_src); CV_UNUSED(_dst); CV_UNUSED(ksize); CV_UNUSED(sigma1); CV_UNUSED(sigma2); CV_UNUSED(borderType);
4116 void cv::GaussianBlur( InputArray _src, OutputArray _dst, Size ksize,
4117 double sigma1, double sigma2,
4120 CV_INSTRUMENT_REGION()
4122 int type = _src.type();
4123 Size size = _src.size();
4124 _dst.create( size, type );
4126 if( (borderType & ~BORDER_ISOLATED) != BORDER_CONSTANT &&
4127 ((borderType & BORDER_ISOLATED) != 0 || !_src.getMat().isSubmatrix()) )
4129 if( size.height == 1 )
4131 if( size.width == 1 )
4135 if( ksize.width == 1 && ksize.height == 1 )
4141 bool useOpenCL = (ocl::isOpenCLActivated() && _dst.isUMat() && _src.dims() <= 2 &&
4142 ((ksize.width == 3 && ksize.height == 3) ||
4143 (ksize.width == 5 && ksize.height == 5)) &&
4144 _src.rows() > ksize.height && _src.cols() > ksize.width);
4147 int sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4149 if(sdepth == CV_8U && ((borderType & BORDER_ISOLATED) || !_src.getMat().isSubmatrix()))
4151 std::vector<ufixedpoint16> fkx, fky;
4152 createGaussianKernels(fkx, fky, type, ksize, sigma1, sigma2);
4153 Mat src = _src.getMat();
4154 Mat dst = _dst.getMat();
4155 if (src.data == dst.data)
4157 fixedSmoothInvoker<uint8_t, ufixedpoint16> invoker(src.ptr<uint8_t>(), src.step1(), dst.ptr<uint8_t>(), dst.step1(), dst.cols, dst.rows, dst.channels(), &fkx[0], (int)fkx.size(), &fky[0], (int)fky.size(), borderType & ~BORDER_ISOLATED);
4158 parallel_for_(Range(0, dst.rows), invoker, std::max(1, std::min(getNumThreads(), getNumberOfCPUs())));
4164 createGaussianKernels(kx, ky, type, ksize, sigma1, sigma2);
4166 CV_OCL_RUN(useOpenCL, ocl_GaussianBlur_8UC1(_src, _dst, ksize, CV_MAT_DEPTH(type), kx, ky, borderType));
4168 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2 && (size_t)_src.rows() > kx.total() && (size_t)_src.cols() > kx.total(),
4169 ocl_sepFilter2D(_src, _dst, sdepth, kx, ky, Point(-1, -1), 0, borderType))
4171 Mat src = _src.getMat();
4172 Mat dst = _dst.getMat();
4175 Size wsz(src.cols, src.rows);
4176 if(!(borderType & BORDER_ISOLATED))
4177 src.locateROI( wsz, ofs );
4179 CALL_HAL(gaussianBlur, cv_hal_gaussianBlur, src.ptr(), src.step, dst.ptr(), dst.step, src.cols, src.rows, sdepth, cn,
4180 ofs.x, ofs.y, wsz.width - src.cols - ofs.x, wsz.height - src.rows - ofs.y, ksize.width, ksize.height,
4181 sigma1, sigma2, borderType&~BORDER_ISOLATED);
4184 openvx_gaussianBlur(src, dst, ksize, sigma1, sigma2, borderType))
4186 CV_IPP_RUN_FAST(ipp_GaussianBlur(src, dst, ksize, sigma1, sigma2, borderType));
4188 sepFilter2D(src, dst, sdepth, kx, ky, Point(-1, -1), 0, borderType);
4191 /****************************************************************************************\
4193 \****************************************************************************************/
4200 * This structure represents a two-tier histogram. The first tier (known as the
4201 * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
4202 * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
4203 * coarse bucket designated by the 4 MSBs of the fine bucket value.
4205 * The structure is aligned on 16 bits, which is a prerequisite for SIMD
4206 * instructions. Each bucket is 16 bit wide, which means that extra care must be
4207 * taken to prevent overflow.
4218 static inline void histogram_add_simd( const HT x[16], HT y[16] )
4220 v_store(y, v_load(x) + v_load(y));
4221 v_store(y + 8, v_load(x + 8) + v_load(y + 8));
4224 static inline void histogram_sub_simd( const HT x[16], HT y[16] )
4226 v_store(y, v_load(y) - v_load(x));
4227 v_store(y + 8, v_load(y + 8) - v_load(x + 8));
4233 static inline void histogram_add( const HT x[16], HT y[16] )
4236 for( i = 0; i < 16; ++i )
4237 y[i] = (HT)(y[i] + x[i]);
4240 static inline void histogram_sub( const HT x[16], HT y[16] )
4243 for( i = 0; i < 16; ++i )
4244 y[i] = (HT)(y[i] - x[i]);
4247 static inline void histogram_muladd( int a, const HT x[16],
4250 for( int i = 0; i < 16; ++i )
4251 y[i] = (HT)(y[i] + a * x[i]);
4255 medianBlur_8u_O1( const Mat& _src, Mat& _dst, int ksize )
4258 * HOP is short for Histogram OPeration. This macro makes an operation \a op on
4259 * histogram \a h for pixel value \a x. It takes care of handling both levels.
4261 #define HOP(h,x,op) \
4262 h.coarse[x>>4] op, \
4263 *((HT*)h.fine + x) op
4265 #define COP(c,j,x,op) \
4266 h_coarse[ 16*(n*c+j) + (x>>4) ] op, \
4267 h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op
4269 int cn = _dst.channels(), m = _dst.rows, r = (ksize-1)/2;
4270 CV_Assert(cn > 0 && cn <= 4);
4271 size_t sstep = _src.step, dstep = _dst.step;
4272 Histogram CV_DECL_ALIGNED(16) H[4];
4273 HT CV_DECL_ALIGNED(16) luc[4][16];
4275 int STRIPE_SIZE = std::min( _dst.cols, 512/cn );
4277 std::vector<HT> _h_coarse(1 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4278 std::vector<HT> _h_fine(16 * 16 * (STRIPE_SIZE + 2*r) * cn + 16);
4279 HT* h_coarse = alignPtr(&_h_coarse[0], 16);
4280 HT* h_fine = alignPtr(&_h_fine[0], 16);
4282 volatile bool useSIMD = hasSIMD128();
4285 for( int x = 0; x < _dst.cols; x += STRIPE_SIZE )
4287 int i, j, k, c, n = std::min(_dst.cols - x, STRIPE_SIZE) + r*2;
4288 const uchar* src = _src.ptr() + x*cn;
4289 uchar* dst = _dst.ptr() + (x - r)*cn;
4291 memset( h_coarse, 0, 16*n*cn*sizeof(h_coarse[0]) );
4292 memset( h_fine, 0, 16*16*n*cn*sizeof(h_fine[0]) );
4294 // First row initialization
4295 for( c = 0; c < cn; c++ )
4297 for( j = 0; j < n; j++ )
4298 COP( c, j, src[cn*j+c], += (cv::HT)(r+2) );
4300 for( i = 1; i < r; i++ )
4302 const uchar* p = src + sstep*std::min(i, m-1);
4303 for ( j = 0; j < n; j++ )
4304 COP( c, j, p[cn*j+c], ++ );
4308 for( i = 0; i < m; i++ )
4310 const uchar* p0 = src + sstep * std::max( 0, i-r-1 );
4311 const uchar* p1 = src + sstep * std::min( m-1, i+r );
4313 memset( H, 0, cn*sizeof(H[0]) );
4314 memset( luc, 0, cn*sizeof(luc[0]) );
4315 for( c = 0; c < cn; c++ )
4317 // Update column histograms for the entire row.
4318 for( j = 0; j < n; j++ )
4320 COP( c, j, p0[j*cn + c], -- );
4321 COP( c, j, p1[j*cn + c], ++ );
4324 // First column initialization
4325 for( k = 0; k < 16; ++k )
4326 histogram_muladd( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].fine[k][0] );
4331 for( j = 0; j < 2*r; ++j )
4332 histogram_add_simd( &h_coarse[16*(n*c+j)], H[c].coarse );
4334 for( j = r; j < n-r; j++ )
4336 int t = 2*r*r + 2*r, b, sum = 0;
4339 histogram_add_simd( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4341 // Find median at coarse level
4342 for ( k = 0; k < 16 ; ++k )
4344 sum += H[c].coarse[k];
4347 sum -= H[c].coarse[k];
4351 CV_Assert( k < 16 );
4353 /* Update corresponding histogram segment */
4354 if ( luc[c][k] <= j-r )
4356 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4357 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4358 histogram_add_simd( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4360 if ( luc[c][k] < j+r+1 )
4362 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4363 luc[c][k] = (HT)(j+r+1);
4368 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4370 histogram_sub_simd( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4371 histogram_add_simd( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4375 histogram_sub_simd( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4377 /* Find median in segment */
4378 segment = H[c].fine[k];
4379 for ( b = 0; b < 16 ; b++ )
4384 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4388 CV_Assert( b < 16 );
4394 for( j = 0; j < 2*r; ++j )
4395 histogram_add( &h_coarse[16*(n*c+j)], H[c].coarse );
4397 for( j = r; j < n-r; j++ )
4399 int t = 2*r*r + 2*r, b, sum = 0;
4402 histogram_add( &h_coarse[16*(n*c + std::min(j+r,n-1))], H[c].coarse );
4404 // Find median at coarse level
4405 for ( k = 0; k < 16 ; ++k )
4407 sum += H[c].coarse[k];
4410 sum -= H[c].coarse[k];
4414 CV_Assert( k < 16 );
4416 /* Update corresponding histogram segment */
4417 if ( luc[c][k] <= j-r )
4419 memset( &H[c].fine[k], 0, 16 * sizeof(HT) );
4420 for ( luc[c][k] = cv::HT(j-r); luc[c][k] < MIN(j+r+1,n); ++luc[c][k] )
4421 histogram_add( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].fine[k] );
4423 if ( luc[c][k] < j+r+1 )
4425 histogram_muladd( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].fine[k][0] );
4426 luc[c][k] = (HT)(j+r+1);
4431 for ( ; luc[c][k] < j+r+1; ++luc[c][k] )
4433 histogram_sub( &h_fine[16*(n*(16*c+k)+MAX(luc[c][k]-2*r-1,0))], H[c].fine[k] );
4434 histogram_add( &h_fine[16*(n*(16*c+k)+MIN(luc[c][k],n-1))], H[c].fine[k] );
4438 histogram_sub( &h_coarse[16*(n*c+MAX(j-r,0))], H[c].coarse );
4440 /* Find median in segment */
4441 segment = H[c].fine[k];
4442 for ( b = 0; b < 16 ; b++ )
4447 dst[dstep*i+cn*j+c] = (uchar)(16*k + b);
4451 CV_Assert( b < 16 );
4463 medianBlur_8u_Om( const Mat& _src, Mat& _dst, int m )
4470 Size size = _dst.size();
4471 const uchar* src = _src.ptr();
4472 uchar* dst = _dst.ptr();
4473 int src_step = (int)_src.step, dst_step = (int)_dst.step;
4474 int cn = _src.channels();
4475 const uchar* src_max = src + size.height*src_step;
4476 CV_Assert(cn > 0 && cn <= 4);
4478 #define UPDATE_ACC01( pix, cn, op ) \
4482 zone0[cn][p >> 4] op; \
4485 //CV_Assert( size.height >= nx && size.width >= nx );
4486 for( x = 0; x < size.width; x++, src += cn, dst += cn )
4488 uchar* dst_cur = dst;
4489 const uchar* src_top = src;
4490 const uchar* src_bottom = src;
4492 int src_step1 = src_step, dst_step1 = dst_step;
4496 src_bottom = src_top += src_step*(size.height-1);
4497 dst_cur += dst_step*(size.height-1);
4498 src_step1 = -src_step1;
4499 dst_step1 = -dst_step1;
4503 memset( zone0, 0, sizeof(zone0[0])*cn );
4504 memset( zone1, 0, sizeof(zone1[0])*cn );
4506 for( y = 0; y <= m/2; y++ )
4508 for( c = 0; c < cn; c++ )
4512 for( k = 0; k < m*cn; k += cn )
4513 UPDATE_ACC01( src_bottom[k+c], c, ++ );
4517 for( k = 0; k < m*cn; k += cn )
4518 UPDATE_ACC01( src_bottom[k+c], c, += m/2+1 );
4522 if( (src_step1 > 0 && y < size.height-1) ||
4523 (src_step1 < 0 && size.height-y-1 > 0) )
4524 src_bottom += src_step1;
4527 for( y = 0; y < size.height; y++, dst_cur += dst_step1 )
4530 for( c = 0; c < cn; c++ )
4535 int t = s + zone0[c][k];
4546 dst_cur[c] = (uchar)k;
4549 if( y+1 == size.height )
4554 for( k = 0; k < m; k++ )
4557 int q = src_bottom[k];
4566 for( k = 0; k < m*3; k += 3 )
4568 UPDATE_ACC01( src_top[k], 0, -- );
4569 UPDATE_ACC01( src_top[k+1], 1, -- );
4570 UPDATE_ACC01( src_top[k+2], 2, -- );
4572 UPDATE_ACC01( src_bottom[k], 0, ++ );
4573 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4574 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4580 for( k = 0; k < m*4; k += 4 )
4582 UPDATE_ACC01( src_top[k], 0, -- );
4583 UPDATE_ACC01( src_top[k+1], 1, -- );
4584 UPDATE_ACC01( src_top[k+2], 2, -- );
4585 UPDATE_ACC01( src_top[k+3], 3, -- );
4587 UPDATE_ACC01( src_bottom[k], 0, ++ );
4588 UPDATE_ACC01( src_bottom[k+1], 1, ++ );
4589 UPDATE_ACC01( src_bottom[k+2], 2, ++ );
4590 UPDATE_ACC01( src_bottom[k+3], 3, ++ );
4594 if( (src_step1 > 0 && src_bottom + src_step1 < src_max) ||
4595 (src_step1 < 0 && src_bottom + src_step1 >= src) )
4596 src_bottom += src_step1;
4599 src_top += src_step1;
4609 typedef uchar value_type;
4610 typedef int arg_type;
4612 arg_type load(const uchar* ptr) { return *ptr; }
4613 void store(uchar* ptr, arg_type val) { *ptr = (uchar)val; }
4614 void operator()(arg_type& a, arg_type& b) const
4616 int t = CV_FAST_CAST_8U(a - b);
4623 typedef ushort value_type;
4624 typedef int arg_type;
4626 arg_type load(const ushort* ptr) { return *ptr; }
4627 void store(ushort* ptr, arg_type val) { *ptr = (ushort)val; }
4628 void operator()(arg_type& a, arg_type& b) const
4638 typedef short value_type;
4639 typedef int arg_type;
4641 arg_type load(const short* ptr) { return *ptr; }
4642 void store(short* ptr, arg_type val) { *ptr = (short)val; }
4643 void operator()(arg_type& a, arg_type& b) const
4653 typedef float value_type;
4654 typedef float arg_type;
4656 arg_type load(const float* ptr) { return *ptr; }
4657 void store(float* ptr, arg_type val) { *ptr = val; }
4658 void operator()(arg_type& a, arg_type& b) const
4670 typedef uchar value_type;
4671 typedef v_uint8x16 arg_type;
4673 arg_type load(const uchar* ptr) { return v_load(ptr); }
4674 void store(uchar* ptr, const arg_type &val) { v_store(ptr, val); }
4675 void operator()(arg_type& a, arg_type& b) const
4686 typedef ushort value_type;
4687 typedef v_uint16x8 arg_type;
4689 arg_type load(const ushort* ptr) { return v_load(ptr); }
4690 void store(ushort* ptr, const arg_type &val) { v_store(ptr, val); }
4691 void operator()(arg_type& a, arg_type& b) const
4702 typedef short value_type;
4703 typedef v_int16x8 arg_type;
4705 arg_type load(const short* ptr) { return v_load(ptr); }
4706 void store(short* ptr, const arg_type &val) { v_store(ptr, val); }
4707 void operator()(arg_type& a, arg_type& b) const
4718 typedef float value_type;
4719 typedef v_float32x4 arg_type;
4721 arg_type load(const float* ptr) { return v_load(ptr); }
4722 void store(float* ptr, const arg_type &val) { v_store(ptr, val); }
4723 void operator()(arg_type& a, arg_type& b) const
4733 typedef MinMax8u MinMaxVec8u;
4734 typedef MinMax16u MinMaxVec16u;
4735 typedef MinMax16s MinMaxVec16s;
4736 typedef MinMax32f MinMaxVec32f;
4740 template<class Op, class VecOp>
4742 medianBlur_SortNet( const Mat& _src, Mat& _dst, int m )
4744 typedef typename Op::value_type T;
4745 typedef typename Op::arg_type WT;
4746 typedef typename VecOp::arg_type VT;
4748 const T* src = _src.ptr<T>();
4749 T* dst = _dst.ptr<T>();
4750 int sstep = (int)(_src.step/sizeof(T));
4751 int dstep = (int)(_dst.step/sizeof(T));
4752 Size size = _dst.size();
4753 int i, j, k, cn = _src.channels();
4756 volatile bool useSIMD = hasSIMD128();
4760 if( size.width == 1 || size.height == 1 )
4762 int len = size.width + size.height - 1;
4763 int sdelta = size.height == 1 ? cn : sstep;
4764 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4765 int ddelta = size.height == 1 ? cn : dstep;
4767 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4768 for( j = 0; j < cn; j++, src++ )
4770 WT p0 = src[i > 0 ? -sdelta : 0];
4772 WT p2 = src[i < len - 1 ? sdelta : 0];
4774 op(p0, p1); op(p1, p2); op(p0, p1);
4781 for( i = 0; i < size.height; i++, dst += dstep )
4783 const T* row0 = src + std::max(i - 1, 0)*sstep;
4784 const T* row1 = src + i*sstep;
4785 const T* row2 = src + std::min(i + 1, size.height-1)*sstep;
4786 int limit = useSIMD ? cn : size.width;
4790 for( ; j < limit; j++ )
4792 int j0 = j >= cn ? j - cn : j;
4793 int j2 = j < size.width - cn ? j + cn : j;
4794 WT p0 = row0[j0], p1 = row0[j], p2 = row0[j2];
4795 WT p3 = row1[j0], p4 = row1[j], p5 = row1[j2];
4796 WT p6 = row2[j0], p7 = row2[j], p8 = row2[j2];
4798 op(p1, p2); op(p4, p5); op(p7, p8); op(p0, p1);
4799 op(p3, p4); op(p6, p7); op(p1, p2); op(p4, p5);
4800 op(p7, p8); op(p0, p3); op(p5, p8); op(p4, p7);
4801 op(p3, p6); op(p1, p4); op(p2, p5); op(p4, p7);
4802 op(p4, p2); op(p6, p4); op(p4, p2);
4806 if( limit == size.width )
4809 for( ; j <= size.width - VecOp::SIZE - cn; j += VecOp::SIZE )
4811 VT p0 = vop.load(row0+j-cn), p1 = vop.load(row0+j), p2 = vop.load(row0+j+cn);
4812 VT p3 = vop.load(row1+j-cn), p4 = vop.load(row1+j), p5 = vop.load(row1+j+cn);
4813 VT p6 = vop.load(row2+j-cn), p7 = vop.load(row2+j), p8 = vop.load(row2+j+cn);
4815 vop(p1, p2); vop(p4, p5); vop(p7, p8); vop(p0, p1);
4816 vop(p3, p4); vop(p6, p7); vop(p1, p2); vop(p4, p5);
4817 vop(p7, p8); vop(p0, p3); vop(p5, p8); vop(p4, p7);
4818 vop(p3, p6); vop(p1, p4); vop(p2, p5); vop(p4, p7);
4819 vop(p4, p2); vop(p6, p4); vop(p4, p2);
4820 vop.store(dst+j, p4);
4829 if( size.width == 1 || size.height == 1 )
4831 int len = size.width + size.height - 1;
4832 int sdelta = size.height == 1 ? cn : sstep;
4833 int sdelta0 = size.height == 1 ? 0 : sstep - cn;
4834 int ddelta = size.height == 1 ? cn : dstep;
4836 for( i = 0; i < len; i++, src += sdelta0, dst += ddelta )
4837 for( j = 0; j < cn; j++, src++ )
4839 int i1 = i > 0 ? -sdelta : 0;
4840 int i0 = i > 1 ? -sdelta*2 : i1;
4841 int i3 = i < len-1 ? sdelta : 0;
4842 int i4 = i < len-2 ? sdelta*2 : i3;
4843 WT p0 = src[i0], p1 = src[i1], p2 = src[0], p3 = src[i3], p4 = src[i4];
4845 op(p0, p1); op(p3, p4); op(p2, p3); op(p3, p4); op(p0, p2);
4846 op(p2, p4); op(p1, p3); op(p1, p2);
4853 for( i = 0; i < size.height; i++, dst += dstep )
4856 row[0] = src + std::max(i - 2, 0)*sstep;
4857 row[1] = src + std::max(i - 1, 0)*sstep;
4858 row[2] = src + i*sstep;
4859 row[3] = src + std::min(i + 1, size.height-1)*sstep;
4860 row[4] = src + std::min(i + 2, size.height-1)*sstep;
4861 int limit = useSIMD ? cn*2 : size.width;
4865 for( ; j < limit; j++ )
4868 int j1 = j >= cn ? j - cn : j;
4869 int j0 = j >= cn*2 ? j - cn*2 : j1;
4870 int j3 = j < size.width - cn ? j + cn : j;
4871 int j4 = j < size.width - cn*2 ? j + cn*2 : j3;
4872 for( k = 0; k < 5; k++ )
4874 const T* rowk = row[k];
4875 p[k*5] = rowk[j0]; p[k*5+1] = rowk[j1];
4876 p[k*5+2] = rowk[j]; p[k*5+3] = rowk[j3];
4877 p[k*5+4] = rowk[j4];
4880 op(p[1], p[2]); op(p[0], p[1]); op(p[1], p[2]); op(p[4], p[5]); op(p[3], p[4]);
4881 op(p[4], p[5]); op(p[0], p[3]); op(p[2], p[5]); op(p[2], p[3]); op(p[1], p[4]);
4882 op(p[1], p[2]); op(p[3], p[4]); op(p[7], p[8]); op(p[6], p[7]); op(p[7], p[8]);
4883 op(p[10], p[11]); op(p[9], p[10]); op(p[10], p[11]); op(p[6], p[9]); op(p[8], p[11]);
4884 op(p[8], p[9]); op(p[7], p[10]); op(p[7], p[8]); op(p[9], p[10]); op(p[0], p[6]);
4885 op(p[4], p[10]); op(p[4], p[6]); op(p[2], p[8]); op(p[2], p[4]); op(p[6], p[8]);
4886 op(p[1], p[7]); op(p[5], p[11]); op(p[5], p[7]); op(p[3], p[9]); op(p[3], p[5]);
4887 op(p[7], p[9]); op(p[1], p[2]); op(p[3], p[4]); op(p[5], p[6]); op(p[7], p[8]);
4888 op(p[9], p[10]); op(p[13], p[14]); op(p[12], p[13]); op(p[13], p[14]); op(p[16], p[17]);
4889 op(p[15], p[16]); op(p[16], p[17]); op(p[12], p[15]); op(p[14], p[17]); op(p[14], p[15]);
4890 op(p[13], p[16]); op(p[13], p[14]); op(p[15], p[16]); op(p[19], p[20]); op(p[18], p[19]);
4891 op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[21], p[23]); op(p[22], p[24]);
4892 op(p[22], p[23]); op(p[18], p[21]); op(p[20], p[23]); op(p[20], p[21]); op(p[19], p[22]);
4893 op(p[22], p[24]); op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[12], p[18]);
4894 op(p[16], p[22]); op(p[16], p[18]); op(p[14], p[20]); op(p[20], p[24]); op(p[14], p[16]);
4895 op(p[18], p[20]); op(p[22], p[24]); op(p[13], p[19]); op(p[17], p[23]); op(p[17], p[19]);
4896 op(p[15], p[21]); op(p[15], p[17]); op(p[19], p[21]); op(p[13], p[14]); op(p[15], p[16]);
4897 op(p[17], p[18]); op(p[19], p[20]); op(p[21], p[22]); op(p[23], p[24]); op(p[0], p[12]);
4898 op(p[8], p[20]); op(p[8], p[12]); op(p[4], p[16]); op(p[16], p[24]); op(p[12], p[16]);
4899 op(p[2], p[14]); op(p[10], p[22]); op(p[10], p[14]); op(p[6], p[18]); op(p[6], p[10]);
4900 op(p[10], p[12]); op(p[1], p[13]); op(p[9], p[21]); op(p[9], p[13]); op(p[5], p[17]);
4901 op(p[13], p[17]); op(p[3], p[15]); op(p[11], p[23]); op(p[11], p[15]); op(p[7], p[19]);
4902 op(p[7], p[11]); op(p[11], p[13]); op(p[11], p[12]);
4906 if( limit == size.width )
4909 for( ; j <= size.width - VecOp::SIZE - cn*2; j += VecOp::SIZE )
4912 for( k = 0; k < 5; k++ )
4914 const T* rowk = row[k];
4915 p[k*5] = vop.load(rowk+j-cn*2); p[k*5+1] = vop.load(rowk+j-cn);
4916 p[k*5+2] = vop.load(rowk+j); p[k*5+3] = vop.load(rowk+j+cn);
4917 p[k*5+4] = vop.load(rowk+j+cn*2);
4920 vop(p[1], p[2]); vop(p[0], p[1]); vop(p[1], p[2]); vop(p[4], p[5]); vop(p[3], p[4]);
4921 vop(p[4], p[5]); vop(p[0], p[3]); vop(p[2], p[5]); vop(p[2], p[3]); vop(p[1], p[4]);
4922 vop(p[1], p[2]); vop(p[3], p[4]); vop(p[7], p[8]); vop(p[6], p[7]); vop(p[7], p[8]);
4923 vop(p[10], p[11]); vop(p[9], p[10]); vop(p[10], p[11]); vop(p[6], p[9]); vop(p[8], p[11]);
4924 vop(p[8], p[9]); vop(p[7], p[10]); vop(p[7], p[8]); vop(p[9], p[10]); vop(p[0], p[6]);
4925 vop(p[4], p[10]); vop(p[4], p[6]); vop(p[2], p[8]); vop(p[2], p[4]); vop(p[6], p[8]);
4926 vop(p[1], p[7]); vop(p[5], p[11]); vop(p[5], p[7]); vop(p[3], p[9]); vop(p[3], p[5]);
4927 vop(p[7], p[9]); vop(p[1], p[2]); vop(p[3], p[4]); vop(p[5], p[6]); vop(p[7], p[8]);
4928 vop(p[9], p[10]); vop(p[13], p[14]); vop(p[12], p[13]); vop(p[13], p[14]); vop(p[16], p[17]);
4929 vop(p[15], p[16]); vop(p[16], p[17]); vop(p[12], p[15]); vop(p[14], p[17]); vop(p[14], p[15]);
4930 vop(p[13], p[16]); vop(p[13], p[14]); vop(p[15], p[16]); vop(p[19], p[20]); vop(p[18], p[19]);
4931 vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[21], p[23]); vop(p[22], p[24]);
4932 vop(p[22], p[23]); vop(p[18], p[21]); vop(p[20], p[23]); vop(p[20], p[21]); vop(p[19], p[22]);
4933 vop(p[22], p[24]); vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[12], p[18]);
4934 vop(p[16], p[22]); vop(p[16], p[18]); vop(p[14], p[20]); vop(p[20], p[24]); vop(p[14], p[16]);
4935 vop(p[18], p[20]); vop(p[22], p[24]); vop(p[13], p[19]); vop(p[17], p[23]); vop(p[17], p[19]);
4936 vop(p[15], p[21]); vop(p[15], p[17]); vop(p[19], p[21]); vop(p[13], p[14]); vop(p[15], p[16]);
4937 vop(p[17], p[18]); vop(p[19], p[20]); vop(p[21], p[22]); vop(p[23], p[24]); vop(p[0], p[12]);
4938 vop(p[8], p[20]); vop(p[8], p[12]); vop(p[4], p[16]); vop(p[16], p[24]); vop(p[12], p[16]);
4939 vop(p[2], p[14]); vop(p[10], p[22]); vop(p[10], p[14]); vop(p[6], p[18]); vop(p[6], p[10]);
4940 vop(p[10], p[12]); vop(p[1], p[13]); vop(p[9], p[21]); vop(p[9], p[13]); vop(p[5], p[17]);
4941 vop(p[13], p[17]); vop(p[3], p[15]); vop(p[11], p[23]); vop(p[11], p[15]); vop(p[7], p[19]);
4942 vop(p[7], p[11]); vop(p[11], p[13]); vop(p[11], p[12]);
4943 vop.store(dst+j, p[12]);
4954 static bool ocl_medianFilter(InputArray _src, OutputArray _dst, int m)
4956 size_t localsize[2] = { 16, 16 };
4957 size_t globalsize[2];
4958 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
4960 if ( !((depth == CV_8U || depth == CV_16U || depth == CV_16S || depth == CV_32F) && cn <= 4 && (m == 3 || m == 5)) )
4963 Size imgSize = _src.size();
4964 bool useOptimized = (1 == cn) &&
4965 (size_t)imgSize.width >= localsize[0] * 8 &&
4966 (size_t)imgSize.height >= localsize[1] * 8 &&
4967 imgSize.width % 4 == 0 &&
4968 imgSize.height % 4 == 0 &&
4969 (ocl::Device::getDefault().isIntel());
4971 cv::String kname = format( useOptimized ? "medianFilter%d_u" : "medianFilter%d", m) ;
4972 cv::String kdefs = useOptimized ?
4973 format("-D T=%s -D T1=%s -D T4=%s%d -D cn=%d -D USE_4OPT", ocl::typeToStr(type),
4974 ocl::typeToStr(depth), ocl::typeToStr(depth), cn*4, cn)
4976 format("-D T=%s -D T1=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn) ;
4978 ocl::Kernel k(kname.c_str(), ocl::imgproc::medianFilter_oclsrc, kdefs.c_str() );
4983 UMat src = _src.getUMat();
4984 _dst.create(src.size(), type);
4985 UMat dst = _dst.getUMat();
4987 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst));
4991 globalsize[0] = DIVUP(src.cols / 4, localsize[0]) * localsize[0];
4992 globalsize[1] = DIVUP(src.rows / 4, localsize[1]) * localsize[1];
4996 globalsize[0] = (src.cols + localsize[0] + 2) / localsize[0] * localsize[0];
4997 globalsize[1] = (src.rows + localsize[1] - 1) / localsize[1] * localsize[1];
5000 return k.run(2, globalsize, localsize, false);
5011 template <> inline bool skipSmallImages<VX_KERNEL_MEDIAN_3x3>(int w, int h) { return w*h < 1280 * 720; }
5013 static bool openvx_medianFilter(InputArray _src, OutputArray _dst, int ksize)
5015 if (_src.type() != CV_8UC1 || _dst.type() != CV_8U
5016 #ifndef VX_VERSION_1_1
5022 Mat src = _src.getMat();
5023 Mat dst = _dst.getMat();
5026 #ifdef VX_VERSION_1_1
5027 ksize != 3 ? ovx::skipSmallImages<VX_KERNEL_NON_LINEAR_FILTER>(src.cols, src.rows) :
5029 ovx::skipSmallImages<VX_KERNEL_MEDIAN_3x3>(src.cols, src.rows)
5035 ivx::Context ctx = ovx::getOpenVXContext();
5036 #ifdef VX_VERSION_1_1
5037 if ((vx_size)ksize > ctx.nonlinearMaxDimension())
5042 if (dst.data != src.data)
5048 ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5049 ivx::Image::createAddressing(a.cols, a.rows, 1, (vx_int32)(a.step)), a.data),
5050 ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
5051 ivx::Image::createAddressing(dst.cols, dst.rows, 1, (vx_int32)(dst.step)), dst.data);
5053 //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
5054 //since OpenVX standard says nothing about thread-safety for now
5055 ivx::border_t prevBorder = ctx.immediateBorder();
5056 ctx.setImmediateBorder(VX_BORDER_REPLICATE);
5057 #ifdef VX_VERSION_1_1
5061 ivx::IVX_CHECK_STATUS(vxuMedian3x3(ctx, ia, ib));
5063 #ifdef VX_VERSION_1_1
5068 mtx = ivx::Matrix::createFromPattern(ctx, VX_PATTERN_BOX, ksize, ksize);
5071 vx_size supportedSize;
5072 ivx::IVX_CHECK_STATUS(vxQueryContext(ctx, VX_CONTEXT_NONLINEAR_MAX_DIMENSION, &supportedSize, sizeof(supportedSize)));
5073 if ((vx_size)ksize > supportedSize)
5075 ctx.setImmediateBorder(prevBorder);
5078 Mat mask(ksize, ksize, CV_8UC1, Scalar(255));
5079 mtx = ivx::Matrix::create(ctx, VX_TYPE_UINT8, ksize, ksize);
5082 ivx::IVX_CHECK_STATUS(vxuNonLinearFilter(ctx, VX_NONLINEAR_FILTER_MEDIAN, ia, mtx, ib));
5085 ctx.setImmediateBorder(prevBorder);
5087 catch (ivx::RuntimeError & e)
5089 VX_DbgThrow(e.what());
5091 catch (ivx::WrapperError & e)
5093 VX_DbgThrow(e.what());
5104 static bool ipp_medianFilter(Mat &src0, Mat &dst, int ksize)
5106 CV_INSTRUMENT_REGION_IPP()
5108 #if IPP_VERSION_X100 < 201801
5109 // Degradations for big kernel
5116 IppiSize dstRoiSize = ippiSize(dst.cols, dst.rows), maskSize = ippiSize(ksize, ksize);
5117 IppDataType ippType = ippiGetDataType(src0.type());
5118 int channels = src0.channels();
5119 IppAutoBuffer<Ipp8u> buffer;
5121 if(src0.isSubmatrix())
5125 if(dst.data != src0.data)
5130 if(ippiFilterMedianBorderGetBufferSize(dstRoiSize, maskSize, ippType, channels, &bufSize) < 0)
5133 buffer.allocate(bufSize);
5139 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C1R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5140 else if(channels == 3)
5141 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C3R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5142 else if(channels == 4)
5143 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_8u_C4R, src.ptr<Ipp8u>(), (int)src.step, dst.ptr<Ipp8u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5148 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C1R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5149 else if(channels == 3)
5150 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C3R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5151 else if(channels == 4)
5152 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16u_C4R, src.ptr<Ipp16u>(), (int)src.step, dst.ptr<Ipp16u>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5157 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C1R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5158 else if(channels == 3)
5159 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C3R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5160 else if(channels == 4)
5161 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_16s_C4R, src.ptr<Ipp16s>(), (int)src.step, dst.ptr<Ipp16s>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5166 return CV_INSTRUMENT_FUN_IPP(ippiFilterMedianBorder_32f_C1R, src.ptr<Ipp32f>(), (int)src.step, dst.ptr<Ipp32f>(), (int)dst.step, dstRoiSize, maskSize, ippBorderRepl, 0, buffer) >= 0;
5177 void cv::medianBlur( InputArray _src0, OutputArray _dst, int ksize )
5179 CV_INSTRUMENT_REGION()
5181 CV_Assert( (ksize % 2 == 1) && (_src0.dims() <= 2 ));
5183 if( ksize <= 1 || _src0.empty() )
5189 CV_OCL_RUN(_dst.isUMat(),
5190 ocl_medianFilter(_src0,_dst, ksize))
5192 Mat src0 = _src0.getMat();
5193 _dst.create( src0.size(), src0.type() );
5194 Mat dst = _dst.getMat();
5196 CALL_HAL(medianBlur, cv_hal_medianBlur, src0.data, src0.step, dst.data, dst.step, src0.cols, src0.rows, src0.depth(),
5197 src0.channels(), ksize);
5200 openvx_medianFilter(_src0, _dst, ksize))
5202 CV_IPP_RUN_FAST(ipp_medianFilter(src0, dst, ksize));
5204 #ifdef HAVE_TEGRA_OPTIMIZATION
5205 if (tegra::useTegra() && tegra::medianBlur(src0, dst, ksize))
5209 bool useSortNet = ksize == 3 || (ksize == 5
5211 && ( src0.depth() > CV_8U || src0.channels() == 2 || src0.channels() > 4 )
5218 if( dst.data != src0.data )
5223 if( src.depth() == CV_8U )
5224 medianBlur_SortNet<MinMax8u, MinMaxVec8u>( src, dst, ksize );
5225 else if( src.depth() == CV_16U )
5226 medianBlur_SortNet<MinMax16u, MinMaxVec16u>( src, dst, ksize );
5227 else if( src.depth() == CV_16S )
5228 medianBlur_SortNet<MinMax16s, MinMaxVec16s>( src, dst, ksize );
5229 else if( src.depth() == CV_32F )
5230 medianBlur_SortNet<MinMax32f, MinMaxVec32f>( src, dst, ksize );
5232 CV_Error(CV_StsUnsupportedFormat, "");
5238 cv::copyMakeBorder( src0, src, 0, 0, ksize/2, ksize/2, BORDER_REPLICATE|BORDER_ISOLATED);
5240 int cn = src0.channels();
5241 CV_Assert( src.depth() == CV_8U && (cn == 1 || cn == 3 || cn == 4) );
5243 double img_size_mp = (double)(src0.total())/(1 << 20);
5244 if( ksize <= 3 + (img_size_mp < 1 ? 12 : img_size_mp < 4 ? 6 : 2)*
5245 (CV_SIMD128 && hasSIMD128() ? 1 : 3))
5246 medianBlur_8u_Om( src, dst, ksize );
5248 medianBlur_8u_O1( src, dst, ksize );
5252 /****************************************************************************************\
5254 \****************************************************************************************/
5259 class BilateralFilter_8u_Invoker :
5260 public ParallelLoopBody
5263 BilateralFilter_8u_Invoker(Mat& _dest, const Mat& _temp, int _radius, int _maxk,
5264 int* _space_ofs, float *_space_weight, float *_color_weight) :
5265 temp(&_temp), dest(&_dest), radius(_radius),
5266 maxk(_maxk), space_ofs(_space_ofs), space_weight(_space_weight), color_weight(_color_weight)
5270 virtual void operator() (const Range& range) const CV_OVERRIDE
5272 int i, j, cn = dest->channels(), k;
5273 Size size = dest->size();
5275 int CV_DECL_ALIGNED(16) buf[4];
5276 bool haveSIMD128 = hasSIMD128();
5279 for( i = range.start; i < range.end; i++ )
5281 const uchar* sptr = temp->ptr(i+radius) + radius*cn;
5282 uchar* dptr = dest->ptr(i);
5286 for( j = 0; j < size.width; j++ )
5288 float sum = 0, wsum = 0;
5294 v_float32x4 _val0 = v_setall_f32(static_cast<float>(val0));
5295 v_float32x4 vsumw = v_setzero_f32();
5296 v_float32x4 vsumc = v_setzero_f32();
5298 for( ; k <= maxk - 4; k += 4 )
5300 v_float32x4 _valF = v_float32x4(sptr[j + space_ofs[k]],
5301 sptr[j + space_ofs[k + 1]],
5302 sptr[j + space_ofs[k + 2]],
5303 sptr[j + space_ofs[k + 3]]);
5304 v_float32x4 _val = v_abs(_valF - _val0);
5305 v_store(buf, v_round(_val));
5307 v_float32x4 _cw = v_float32x4(color_weight[buf[0]],
5308 color_weight[buf[1]],
5309 color_weight[buf[2]],
5310 color_weight[buf[3]]);
5311 v_float32x4 _sw = v_load(space_weight+k);
5312 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5313 // details: https://github.com/opencv/opencv/issues/11004
5315 vsumc += _cw * _sw * _valF;
5317 v_float32x4 _w = _cw * _sw;
5324 float *bufFloat = (float*)buf;
5325 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumc, vsumw, vsumc);
5326 v_store(bufFloat, sum4);
5328 wsum += bufFloat[0];
5331 for( ; k < maxk; k++ )
5333 int val = sptr[j + space_ofs[k]];
5334 float w = space_weight[k]*color_weight[std::abs(val - val0)];
5338 // overflow is not possible here => there is no need to use cv::saturate_cast
5339 CV_DbgAssert(fabs(wsum) > 0);
5340 dptr[j] = (uchar)cvRound(sum/wsum);
5346 for( j = 0; j < size.width*3; j += 3 )
5348 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5349 int b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5354 v_float32x4 vsumw = v_setzero_f32();
5355 v_float32x4 vsumb = v_setzero_f32();
5356 v_float32x4 vsumg = v_setzero_f32();
5357 v_float32x4 vsumr = v_setzero_f32();
5358 const v_float32x4 _b0 = v_setall_f32(static_cast<float>(b0));
5359 const v_float32x4 _g0 = v_setall_f32(static_cast<float>(g0));
5360 const v_float32x4 _r0 = v_setall_f32(static_cast<float>(r0));
5362 for( ; k <= maxk - 4; k += 4 )
5364 const uchar* const sptr_k0 = sptr + j + space_ofs[k];
5365 const uchar* const sptr_k1 = sptr + j + space_ofs[k+1];
5366 const uchar* const sptr_k2 = sptr + j + space_ofs[k+2];
5367 const uchar* const sptr_k3 = sptr + j + space_ofs[k+3];
5369 v_float32x4 __b = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k0)));
5370 v_float32x4 __g = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k1)));
5371 v_float32x4 __r = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k2)));
5372 v_float32x4 __z = v_cvt_f32(v_reinterpret_as_s32(v_load_expand_q(sptr_k3)));
5373 v_float32x4 _b, _g, _r, _z;
5375 v_transpose4x4(__b, __g, __r, __z, _b, _g, _r, _z);
5377 v_float32x4 bt = v_abs(_b -_b0);
5378 v_float32x4 gt = v_abs(_g -_g0);
5379 v_float32x4 rt = v_abs(_r -_r0);
5382 v_store(buf, v_round(bt));
5384 v_float32x4 _w = v_float32x4(color_weight[buf[0]],color_weight[buf[1]],
5385 color_weight[buf[2]],color_weight[buf[3]]);
5386 v_float32x4 _sw = v_load(space_weight+k);
5388 #if defined(_MSC_VER) && _MSC_VER == 1700/* MSVS 2012 */ && CV_AVX
5389 // details: https://github.com/opencv/opencv/issues/11004
5391 vsumb += _w * _sw * _b;
5392 vsumg += _w * _sw * _g;
5393 vsumr += _w * _sw * _r;
5406 float *bufFloat = (float*)buf;
5407 v_float32x4 sum4 = v_reduce_sum4(vsumw, vsumb, vsumg, vsumr);
5408 v_store(bufFloat, sum4);
5409 wsum += bufFloat[0];
5410 sum_b += bufFloat[1];
5411 sum_g += bufFloat[2];
5412 sum_r += bufFloat[3];
5416 for( ; k < maxk; k++ )
5418 const uchar* sptr_k = sptr + j + space_ofs[k];
5419 int b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5420 float w = space_weight[k]*color_weight[std::abs(b - b0) +
5421 std::abs(g - g0) + std::abs(r - r0)];
5422 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5425 CV_DbgAssert(fabs(wsum) > 0);
5427 b0 = cvRound(sum_b*wsum);
5428 g0 = cvRound(sum_g*wsum);
5429 r0 = cvRound(sum_r*wsum);
5430 dptr[j] = (uchar)b0; dptr[j+1] = (uchar)g0; dptr[j+2] = (uchar)r0;
5439 int radius, maxk, *space_ofs;
5440 float *space_weight, *color_weight;
5445 static bool ocl_bilateralFilter_8u(InputArray _src, OutputArray _dst, int d,
5446 double sigma_color, double sigma_space,
5450 if (ocl::Device::getDefault().isNVidia())
5454 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
5455 int i, j, maxk, radius;
5457 if (depth != CV_8U || cn > 4)
5460 if (sigma_color <= 0)
5462 if (sigma_space <= 0)
5465 double gauss_color_coeff = -0.5 / (sigma_color * sigma_color);
5466 double gauss_space_coeff = -0.5 / (sigma_space * sigma_space);
5469 radius = cvRound(sigma_space * 1.5);
5472 radius = MAX(radius, 1);
5475 UMat src = _src.getUMat(), dst = _dst.getUMat(), temp;
5479 copyMakeBorder(src, temp, radius, radius, radius, radius, borderType);
5480 std::vector<float> _space_weight(d * d);
5481 std::vector<int> _space_ofs(d * d);
5482 float * const space_weight = &_space_weight[0];
5483 int * const space_ofs = &_space_ofs[0];
5485 // initialize space-related bilateral filter coefficients
5486 for( i = -radius, maxk = 0; i <= radius; i++ )
5487 for( j = -radius; j <= radius; j++ )
5489 double r = std::sqrt((double)i * i + (double)j * j);
5492 space_weight[maxk] = (float)std::exp(r * r * gauss_space_coeff);
5493 space_ofs[maxk++] = (int)(i * temp.step + j * cn);
5497 String cnstr = cn > 1 ? format("%d", cn) : "";
5498 String kernelName("bilateral");
5500 if ((ocl::Device::getDefault().isIntel()) &&
5501 (ocl::Device::getDefault().type() == ocl::Device::TYPE_GPU))
5504 if (dst.cols % 4 == 0 && cn == 1) // For single channel x4 sized images.
5506 kernelName = "bilateral_float4";
5510 ocl::Kernel k(kernelName.c_str(), ocl::imgproc::bilateral_oclsrc,
5511 format("-D radius=%d -D maxk=%d -D cn=%d -D int_t=%s -D uint_t=uint%s -D convert_int_t=%s"
5512 " -D uchar_t=%s -D float_t=%s -D convert_float_t=%s -D convert_uchar_t=%s -D gauss_color_coeff=(float)%f",
5513 radius, maxk, cn, ocl::typeToStr(CV_32SC(cn)), cnstr.c_str(),
5514 ocl::convertTypeStr(CV_8U, CV_32S, cn, cvt[0]),
5515 ocl::typeToStr(type), ocl::typeToStr(CV_32FC(cn)),
5516 ocl::convertTypeStr(CV_32S, CV_32F, cn, cvt[1]),
5517 ocl::convertTypeStr(CV_32F, CV_8U, cn, cvt[2]), gauss_color_coeff));
5521 Mat mspace_weight(1, d * d, CV_32FC1, space_weight);
5522 Mat mspace_ofs(1, d * d, CV_32SC1, space_ofs);
5523 UMat ucolor_weight, uspace_weight, uspace_ofs;
5525 mspace_weight.copyTo(uspace_weight);
5526 mspace_ofs.copyTo(uspace_ofs);
5528 k.args(ocl::KernelArg::ReadOnlyNoSize(temp), ocl::KernelArg::WriteOnly(dst),
5529 ocl::KernelArg::PtrReadOnly(uspace_weight),
5530 ocl::KernelArg::PtrReadOnly(uspace_ofs));
5532 size_t globalsize[2] = { (size_t)dst.cols / sizeDiv, (size_t)dst.rows };
5533 return k.run(2, globalsize, NULL, false);
5538 bilateralFilter_8u( const Mat& src, Mat& dst, int d,
5539 double sigma_color, double sigma_space,
5542 int cn = src.channels();
5543 int i, j, maxk, radius;
5544 Size size = src.size();
5546 CV_Assert( (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.data != dst.data );
5548 if( sigma_color <= 0 )
5550 if( sigma_space <= 0 )
5553 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5554 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5557 radius = cvRound(sigma_space*1.5);
5560 radius = MAX(radius, 1);
5564 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5566 std::vector<float> _color_weight(cn*256);
5567 std::vector<float> _space_weight(d*d);
5568 std::vector<int> _space_ofs(d*d);
5569 float* color_weight = &_color_weight[0];
5570 float* space_weight = &_space_weight[0];
5571 int* space_ofs = &_space_ofs[0];
5573 // initialize color-related bilateral filter coefficients
5575 for( i = 0; i < 256*cn; i++ )
5576 color_weight[i] = (float)std::exp(i*i*gauss_color_coeff);
5578 // initialize space-related bilateral filter coefficients
5579 for( i = -radius, maxk = 0; i <= radius; i++ )
5583 for( ; j <= radius; j++ )
5585 double r = std::sqrt((double)i*i + (double)j*j);
5588 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5589 space_ofs[maxk++] = (int)(i*temp.step + j*cn);
5593 BilateralFilter_8u_Invoker body(dst, temp, radius, maxk, space_ofs, space_weight, color_weight);
5594 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5598 class BilateralFilter_32f_Invoker :
5599 public ParallelLoopBody
5603 BilateralFilter_32f_Invoker(int _cn, int _radius, int _maxk, int *_space_ofs,
5604 const Mat& _temp, Mat& _dest, float _scale_index, float *_space_weight, float *_expLUT) :
5605 cn(_cn), radius(_radius), maxk(_maxk), space_ofs(_space_ofs),
5606 temp(&_temp), dest(&_dest), scale_index(_scale_index), space_weight(_space_weight), expLUT(_expLUT)
5610 virtual void operator() (const Range& range) const CV_OVERRIDE
5613 Size size = dest->size();
5615 int CV_DECL_ALIGNED(16) idxBuf[4];
5616 bool haveSIMD128 = hasSIMD128();
5619 for( i = range.start; i < range.end; i++ )
5621 const float* sptr = temp->ptr<float>(i+radius) + radius*cn;
5622 float* dptr = dest->ptr<float>(i);
5626 for( j = 0; j < size.width; j++ )
5628 float sum = 0, wsum = 0;
5629 float val0 = sptr[j];
5634 v_float32x4 vecwsum = v_setzero_f32();
5635 v_float32x4 vecvsum = v_setzero_f32();
5636 const v_float32x4 _val0 = v_setall_f32(sptr[j]);
5637 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5639 for (; k <= maxk - 4; k += 4)
5641 v_float32x4 _sw = v_load(space_weight + k);
5642 v_float32x4 _val = v_float32x4(sptr[j + space_ofs[k]],
5643 sptr[j + space_ofs[k + 1]],
5644 sptr[j + space_ofs[k + 2]],
5645 sptr[j + space_ofs[k + 3]]);
5646 v_float32x4 _alpha = v_abs(_val - _val0) * _scale_index;
5648 v_int32x4 _idx = v_round(_alpha);
5649 v_store(idxBuf, _idx);
5650 _alpha -= v_cvt_f32(_idx);
5652 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5656 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5657 expLUT[idxBuf[1] + 1],
5658 expLUT[idxBuf[2] + 1],
5659 expLUT[idxBuf[3] + 1]);
5661 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5667 float *bufFloat = (float*)idxBuf;
5668 v_float32x4 sum4 = v_reduce_sum4(vecwsum, vecvsum, vecwsum, vecvsum);
5669 v_store(bufFloat, sum4);
5671 wsum += bufFloat[0];
5675 for( ; k < maxk; k++ )
5677 float val = sptr[j + space_ofs[k]];
5678 float alpha = (float)(std::abs(val - val0)*scale_index);
5679 int idx = cvFloor(alpha);
5681 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5685 CV_DbgAssert(fabs(wsum) > 0);
5686 dptr[j] = (float)(sum/wsum);
5691 CV_Assert( cn == 3 );
5692 for( j = 0; j < size.width*3; j += 3 )
5694 float sum_b = 0, sum_g = 0, sum_r = 0, wsum = 0;
5695 float b0 = sptr[j], g0 = sptr[j+1], r0 = sptr[j+2];
5700 v_float32x4 sumw = v_setzero_f32();
5701 v_float32x4 sumb = v_setzero_f32();
5702 v_float32x4 sumg = v_setzero_f32();
5703 v_float32x4 sumr = v_setzero_f32();
5704 const v_float32x4 _b0 = v_setall_f32(b0);
5705 const v_float32x4 _g0 = v_setall_f32(g0);
5706 const v_float32x4 _r0 = v_setall_f32(r0);
5707 const v_float32x4 _scale_index = v_setall_f32(scale_index);
5709 for( ; k <= maxk-4; k += 4 )
5711 v_float32x4 _sw = v_load(space_weight + k);
5713 const float* const sptr_k0 = sptr + j + space_ofs[k];
5714 const float* const sptr_k1 = sptr + j + space_ofs[k+1];
5715 const float* const sptr_k2 = sptr + j + space_ofs[k+2];
5716 const float* const sptr_k3 = sptr + j + space_ofs[k+3];
5718 v_float32x4 _v0 = v_load(sptr_k0);
5719 v_float32x4 _v1 = v_load(sptr_k1);
5720 v_float32x4 _v2 = v_load(sptr_k2);
5721 v_float32x4 _v3 = v_load(sptr_k3);
5722 v_float32x4 _b, _g, _r, _dummy;
5724 v_transpose4x4(_v0, _v1, _v2, _v3, _b, _g, _r, _dummy);
5726 v_float32x4 _bt = v_abs(_b - _b0);
5727 v_float32x4 _gt = v_abs(_g - _g0);
5728 v_float32x4 _rt = v_abs(_r - _r0);
5729 v_float32x4 _alpha = _scale_index * (_bt + _gt + _rt);
5731 v_int32x4 _idx = v_round(_alpha);
5732 v_store((int*)idxBuf, _idx);
5733 _alpha -= v_cvt_f32(_idx);
5735 v_float32x4 _explut = v_float32x4(expLUT[idxBuf[0]],
5739 v_float32x4 _explut1 = v_float32x4(expLUT[idxBuf[0] + 1],
5740 expLUT[idxBuf[1] + 1],
5741 expLUT[idxBuf[2] + 1],
5742 expLUT[idxBuf[3] + 1]);
5744 v_float32x4 _w = _sw * (_explut + (_alpha * (_explut1 - _explut)));
5754 v_float32x4 sum4 = v_reduce_sum4(sumw, sumb, sumg, sumr);
5755 float *bufFloat = (float*)idxBuf;
5756 v_store(bufFloat, sum4);
5757 wsum += bufFloat[0];
5758 sum_b += bufFloat[1];
5759 sum_g += bufFloat[2];
5760 sum_r += bufFloat[3];
5764 for(; k < maxk; k++ )
5766 const float* sptr_k = sptr + j + space_ofs[k];
5767 float b = sptr_k[0], g = sptr_k[1], r = sptr_k[2];
5768 float alpha = (float)((std::abs(b - b0) +
5769 std::abs(g - g0) + std::abs(r - r0))*scale_index);
5770 int idx = cvFloor(alpha);
5772 float w = space_weight[k]*(expLUT[idx] + alpha*(expLUT[idx+1] - expLUT[idx]));
5773 sum_b += b*w; sum_g += g*w; sum_r += r*w;
5776 CV_DbgAssert(fabs(wsum) > 0);
5781 dptr[j] = b0; dptr[j+1] = g0; dptr[j+2] = r0;
5788 int cn, radius, maxk, *space_ofs;
5791 float scale_index, *space_weight, *expLUT;
5796 bilateralFilter_32f( const Mat& src, Mat& dst, int d,
5797 double sigma_color, double sigma_space,
5800 int cn = src.channels();
5801 int i, j, maxk, radius;
5802 double minValSrc=-1, maxValSrc=1;
5803 const int kExpNumBinsPerChannel = 1 << 12;
5804 int kExpNumBins = 0;
5805 float lastExpVal = 1.f;
5806 float len, scale_index;
5807 Size size = src.size();
5809 CV_Assert( (src.type() == CV_32FC1 || src.type() == CV_32FC3) && src.data != dst.data );
5811 if( sigma_color <= 0 )
5813 if( sigma_space <= 0 )
5816 double gauss_color_coeff = -0.5/(sigma_color*sigma_color);
5817 double gauss_space_coeff = -0.5/(sigma_space*sigma_space);
5820 radius = cvRound(sigma_space*1.5);
5823 radius = MAX(radius, 1);
5825 // compute the min/max range for the input image (even if multichannel)
5827 minMaxLoc( src.reshape(1), &minValSrc, &maxValSrc );
5828 if(std::abs(minValSrc - maxValSrc) < FLT_EPSILON)
5834 // temporary copy of the image with borders for easy processing
5836 copyMakeBorder( src, temp, radius, radius, radius, radius, borderType );
5837 const double insteadNaNValue = -5. * sigma_color;
5838 patchNaNs( temp, insteadNaNValue ); // this replacement of NaNs makes the assumption that depth values are nonnegative
5839 // TODO: make insteadNaNValue avalible in the outside function interface to control the cases breaking the assumption
5840 // allocate lookup tables
5841 std::vector<float> _space_weight(d*d);
5842 std::vector<int> _space_ofs(d*d);
5843 float* space_weight = &_space_weight[0];
5844 int* space_ofs = &_space_ofs[0];
5846 // assign a length which is slightly more than needed
5847 len = (float)(maxValSrc - minValSrc) * cn;
5848 kExpNumBins = kExpNumBinsPerChannel * cn;
5849 std::vector<float> _expLUT(kExpNumBins+2);
5850 float* expLUT = &_expLUT[0];
5852 scale_index = kExpNumBins/len;
5854 // initialize the exp LUT
5855 for( i = 0; i < kExpNumBins+2; i++ )
5857 if( lastExpVal > 0.f )
5859 double val = i / scale_index;
5860 expLUT[i] = (float)std::exp(val * val * gauss_color_coeff);
5861 lastExpVal = expLUT[i];
5867 // initialize space-related bilateral filter coefficients
5868 for( i = -radius, maxk = 0; i <= radius; i++ )
5869 for( j = -radius; j <= radius; j++ )
5871 double r = std::sqrt((double)i*i + (double)j*j);
5874 space_weight[maxk] = (float)std::exp(r*r*gauss_space_coeff);
5875 space_ofs[maxk++] = (int)(i*(temp.step/sizeof(float)) + j*cn);
5878 // parallel_for usage
5880 BilateralFilter_32f_Invoker body(cn, radius, maxk, space_ofs, temp, dst, scale_index, space_weight, expLUT);
5881 parallel_for_(Range(0, size.height), body, dst.total()/(double)(1<<16));
5885 #define IPP_BILATERAL_PARALLEL 1
5888 class ipp_bilateralFilterParallel: public ParallelLoopBody
5891 ipp_bilateralFilterParallel(::ipp::IwiImage &_src, ::ipp::IwiImage &_dst, int _radius, Ipp32f _valSquareSigma, Ipp32f _posSquareSigma, ::ipp::IwiBorderType _borderType, bool *_ok):
5892 src(_src), dst(_dst)
5897 valSquareSigma = _valSquareSigma;
5898 posSquareSigma = _posSquareSigma;
5899 borderType = _borderType;
5903 ~ipp_bilateralFilterParallel() {}
5905 virtual void operator() (const Range& range) const CV_OVERRIDE
5912 ::ipp::IwiTile tile = ::ipp::IwiRoi(0, range.start, dst.m_size.width, range.end - range.start);
5913 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, src, dst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), borderType, tile);
5915 catch(::ipp::IwException)
5922 ::ipp::IwiImage &src;
5923 ::ipp::IwiImage &dst;
5926 Ipp32f valSquareSigma;
5927 Ipp32f posSquareSigma;
5928 ::ipp::IwiBorderType borderType;
5931 const ipp_bilateralFilterParallel& operator= (const ipp_bilateralFilterParallel&);
5935 static bool ipp_bilateralFilter(Mat &src, Mat &dst, int d, double sigmaColor, double sigmaSpace, int borderType)
5938 CV_INSTRUMENT_REGION_IPP()
5940 int radius = IPP_MAX(((d <= 0)?cvRound(sigmaSpace*1.5):d/2), 1);
5941 Ipp32f valSquareSigma = (Ipp32f)((sigmaColor <= 0)?1:sigmaColor*sigmaColor);
5942 Ipp32f posSquareSigma = (Ipp32f)((sigmaSpace <= 0)?1:sigmaSpace*sigmaSpace);
5944 // Acquire data and begin processing
5947 ::ipp::IwiImage iwSrc = ippiGetImage(src);
5948 ::ipp::IwiImage iwDst = ippiGetImage(dst);
5949 ::ipp::IwiBorderSize borderSize(radius);
5950 ::ipp::IwiBorderType ippBorder(ippiGetBorder(iwSrc, borderType, borderSize));
5954 const int threads = ippiSuggestThreadsNum(iwDst, 2);
5955 if(IPP_BILATERAL_PARALLEL && threads > 1) {
5957 Range range(0, (int)iwDst.m_size.height);
5958 ipp_bilateralFilterParallel invoker(iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ippBorder, &ok);
5962 parallel_for_(range, invoker, threads*4);
5967 CV_INSTRUMENT_FUN_IPP(::ipp::iwiFilterBilateral, iwSrc, iwDst, radius, valSquareSigma, posSquareSigma, ::ipp::IwDefault(), ippBorder);
5970 catch (::ipp::IwException)
5976 CV_UNUSED(src); CV_UNUSED(dst); CV_UNUSED(d); CV_UNUSED(sigmaColor); CV_UNUSED(sigmaSpace); CV_UNUSED(borderType);
5984 void cv::bilateralFilter( InputArray _src, OutputArray _dst, int d,
5985 double sigmaColor, double sigmaSpace,
5988 CV_INSTRUMENT_REGION()
5990 _dst.create( _src.size(), _src.type() );
5992 CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
5993 ocl_bilateralFilter_8u(_src, _dst, d, sigmaColor, sigmaSpace, borderType))
5995 Mat src = _src.getMat(), dst = _dst.getMat();
5997 CV_IPP_RUN_FAST(ipp_bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType));
5999 if( src.depth() == CV_8U )
6000 bilateralFilter_8u( src, dst, d, sigmaColor, sigmaSpace, borderType );
6001 else if( src.depth() == CV_32F )
6002 bilateralFilter_32f( src, dst, d, sigmaColor, sigmaSpace, borderType );
6004 CV_Error( CV_StsUnsupportedFormat,
6005 "Bilateral filtering is only implemented for 8u and 32f images" );
6008 //////////////////////////////////////////////////////////////////////////////////////////
6011 cvSmooth( const void* srcarr, void* dstarr, int smooth_type,
6012 int param1, int param2, double param3, double param4 )
6014 cv::Mat src = cv::cvarrToMat(srcarr), dst0 = cv::cvarrToMat(dstarr), dst = dst0;
6016 CV_Assert( dst.size() == src.size() &&
6017 (smooth_type == CV_BLUR_NO_SCALE || dst.type() == src.type()) );
6022 if( smooth_type == CV_BLUR || smooth_type == CV_BLUR_NO_SCALE )
6023 cv::boxFilter( src, dst, dst.depth(), cv::Size(param1, param2), cv::Point(-1,-1),
6024 smooth_type == CV_BLUR, cv::BORDER_REPLICATE );
6025 else if( smooth_type == CV_GAUSSIAN )
6026 cv::GaussianBlur( src, dst, cv::Size(param1, param2), param3, param4, cv::BORDER_REPLICATE );
6027 else if( smooth_type == CV_MEDIAN )
6028 cv::medianBlur( src, dst, param1 );
6030 cv::bilateralFilter( src, dst, param1, param3, param4, cv::BORDER_REPLICATE );
6032 if( dst.data != dst0.data )
6033 CV_Error( CV_StsUnmatchedFormats, "The destination image does not have the proper type" );