Skip to content

Commit cb18bb7

Browse files
committed
*
1 parent 79e617b commit cb18bb7

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>2025.4</Version>
3+
<Version>2025.4.1</Version>
44
<Deterministic>true</Deterministic>
55
</PropertyGroup>
66
</Project>

Masuit.Tools.Abstractions/Media/ImageBorderRemover.cs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using SixLabors.ImageSharp;
33
using SixLabors.ImageSharp.PixelFormats;
44
using SixLabors.ImageSharp.Processing;
5+
using Color = System.Drawing.Color;
56

67
// ReSharper disable AccessToDisposedClosure
78

@@ -15,9 +16,9 @@ public class ImageBorderRemover
1516
/// <summary>
1617
/// 容差模式
1718
/// </summary>
18-
private ToleranceMode ToleranceMode { get; set; }
19+
private ToleranceMode ToleranceMode { get; }
1920

20-
private int CroppedBorderCount { get; set; }
21+
private int CroppedBorderCount { get; }
2122

2223
/// <summary>
2324
///
@@ -163,7 +164,7 @@ private bool RemoveBorders(Image<Rgba32> image, int tolerance, int maxLayers, bo
163164
/// <summary>
164165
/// 查找内容边界(支持多层边框检测)
165166
/// </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)
167168
{
168169
// 如果启用缩小采样且图像足够大
169170
Image<Rgba32> workingImage;
@@ -295,7 +296,7 @@ private static int Clamp(int value, int min, int max)
295296
/// <summary>
296297
/// 检测顶部边框层(优化版)
297298
/// </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)
299300
{
300301
int newTop = currentTop;
301302
Rgba32? detectedColor = null;
@@ -365,7 +366,7 @@ private static int DetectLayerBorderTop(Image<Rgba32> image, int currentTop, int
365366
/// <summary>
366367
/// 检测底部边框层(优化版)
367368
/// </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)
369370
{
370371
int newBottom = currentBottom;
371372
Rgba32? detectedColor = null;
@@ -432,7 +433,7 @@ private static int DetectLayerBorderBottom(Image<Rgba32> image, int currentTop,
432433
/// <summary>
433434
/// 检测左侧边框层(优化版)
434435
/// </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)
436437
{
437438
int newLeft = currentLeft;
438439
Rgba32? detectedColor = null;
@@ -499,7 +500,7 @@ private static int DetectLayerBorderLeft(Image<Rgba32> image, int currentTop, in
499500
/// <summary>
500501
/// 检测右侧边框层(优化版)
501502
/// </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)
503504
{
504505
int newRight = currentRight;
505506
Rgba32? detectedColor = null;
@@ -566,18 +567,28 @@ private static int DetectLayerBorderRight(Image<Rgba32> image, int currentTop, i
566567
/// <summary>
567568
/// 颜色相似度比较(SIMD优化)
568569
/// </summary>
569-
private static bool IsSimilarColor(Rgba32 color1, Rgba32 color2, byte tolerance)
570+
private bool IsSimilarColor(Rgba32 color1, Rgba32 color2, byte tolerance)
570571
{
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;
575583

576-
// 快速路径:如果任一通道差异超过容差
577-
if (diffR > tolerance || diffG > tolerance || diffB > tolerance)
578-
return false;
584+
// 精确比较
585+
return diffR <= tolerance && diffG <= tolerance && diffB <= tolerance;
579586

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+
}
582593
}
583594
}

0 commit comments

Comments
 (0)