2
2
using SixLabors . ImageSharp ;
3
3
using SixLabors . ImageSharp . PixelFormats ;
4
4
using SixLabors . ImageSharp . Processing ;
5
+ using Color = System . Drawing . Color ;
5
6
6
7
// ReSharper disable AccessToDisposedClosure
7
8
@@ -15,9 +16,9 @@ public class ImageBorderRemover
15
16
/// <summary>
16
17
/// 容差模式
17
18
/// </summary>
18
- private ToleranceMode ToleranceMode { get ; set ; }
19
+ private ToleranceMode ToleranceMode { get ; }
19
20
20
- private int CroppedBorderCount { get ; set ; }
21
+ private int CroppedBorderCount { get ; }
21
22
22
23
/// <summary>
23
24
///
@@ -163,7 +164,7 @@ private bool RemoveBorders(Image<Rgba32> image, int tolerance, int maxLayers, bo
163
164
/// <summary>
164
165
/// 查找内容边界(支持多层边框检测)
165
166
/// </summary>
166
- private static ( int top , int bottom , int left , int right , int layers , List < Rgba32 > colors ) FindContentBordersWithLayers ( Image < Rgba32 > image , byte tolerance , int maxLayers , bool useDownscaling , int downscaleFactor )
167
+ private ( int top , int bottom , int left , int right , int layers , List < Rgba32 > colors ) FindContentBordersWithLayers ( Image < Rgba32 > image , byte tolerance , int maxLayers , bool useDownscaling , int downscaleFactor )
167
168
{
168
169
// 如果启用缩小采样且图像足够大
169
170
Image < Rgba32 > workingImage ;
@@ -295,7 +296,7 @@ private static int Clamp(int value, int min, int max)
295
296
/// <summary>
296
297
/// 检测顶部边框层(优化版)
297
298
/// </summary>
298
- private static int DetectLayerBorderTop ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
299
+ private int DetectLayerBorderTop ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
299
300
{
300
301
int newTop = currentTop ;
301
302
Rgba32 ? detectedColor = null ;
@@ -365,7 +366,7 @@ private static int DetectLayerBorderTop(Image<Rgba32> image, int currentTop, int
365
366
/// <summary>
366
367
/// 检测底部边框层(优化版)
367
368
/// </summary>
368
- private static int DetectLayerBorderBottom ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
369
+ private int DetectLayerBorderBottom ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
369
370
{
370
371
int newBottom = currentBottom ;
371
372
Rgba32 ? detectedColor = null ;
@@ -432,7 +433,7 @@ private static int DetectLayerBorderBottom(Image<Rgba32> image, int currentTop,
432
433
/// <summary>
433
434
/// 检测左侧边框层(优化版)
434
435
/// </summary>
435
- private static int DetectLayerBorderLeft ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
436
+ private int DetectLayerBorderLeft ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
436
437
{
437
438
int newLeft = currentLeft ;
438
439
Rgba32 ? detectedColor = null ;
@@ -499,7 +500,7 @@ private static int DetectLayerBorderLeft(Image<Rgba32> image, int currentTop, in
499
500
/// <summary>
500
501
/// 检测右侧边框层(优化版)
501
502
/// </summary>
502
- private static int DetectLayerBorderRight ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
503
+ private int DetectLayerBorderRight ( Image < Rgba32 > image , int currentTop , int currentBottom , int currentLeft , int currentRight , byte tolerance , ref Rgba32 ? borderColor )
503
504
{
504
505
int newRight = currentRight ;
505
506
Rgba32 ? detectedColor = null ;
@@ -566,18 +567,28 @@ private static int DetectLayerBorderRight(Image<Rgba32> image, int currentTop, i
566
567
/// <summary>
567
568
/// 颜色相似度比较(SIMD优化)
568
569
/// </summary>
569
- private static bool IsSimilarColor ( Rgba32 color1 , Rgba32 color2 , byte tolerance )
570
+ private bool IsSimilarColor ( Rgba32 color1 , Rgba32 color2 , byte tolerance )
570
571
{
571
- // 使用快速比较算法
572
- int diffR = Math . Abs ( color1 . R - color2 . R ) ;
573
- int diffG = Math . Abs ( color1 . G - color2 . G ) ;
574
- int diffB = Math . Abs ( color1 . B - color2 . B ) ;
572
+ switch ( ToleranceMode )
573
+ {
574
+ case ToleranceMode . Channel :
575
+ // 使用快速比较算法
576
+ int diffR = Math . Abs ( color1 . R - color2 . R ) ;
577
+ int diffG = Math . Abs ( color1 . G - color2 . G ) ;
578
+ int diffB = Math . Abs ( color1 . B - color2 . B ) ;
579
+
580
+ // 快速路径:如果任一通道差异超过容差
581
+ if ( diffR > tolerance || diffG > tolerance || diffB > tolerance )
582
+ return false ;
575
583
576
- // 快速路径:如果任一通道差异超过容差
577
- if ( diffR > tolerance || diffG > tolerance || diffB > tolerance )
578
- return false ;
584
+ // 精确比较
585
+ return diffR <= tolerance && diffG <= tolerance && diffB <= tolerance ;
579
586
580
- // 精确比较
581
- return diffR <= tolerance && diffG <= tolerance && diffB <= tolerance ;
587
+ case ToleranceMode . DeltaE :
588
+ return ColorDeltaE . CIE2000 ( Color . FromArgb ( color1 . A , color1 . R , color1 . G , color1 . B ) , Color . FromArgb ( color2 . A , color2 . R , color2 . G , color2 . B ) ) <= tolerance ;
589
+
590
+ default :
591
+ throw new ArgumentOutOfRangeException ( ) ;
592
+ }
582
593
}
583
594
}
0 commit comments