From 05c552524b1862c4b64c40ab14c8e7001932c1de Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 27 Feb 2025 20:48:31 +1000 Subject: [PATCH] Threshold on the way out also. --- .../Quantization/WuQuantizer{TPixel}.cs | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index e637c2cf04..db6490259f 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -74,7 +74,8 @@ internal struct WuQuantizer : IQuantizer private readonly IMemoryOwner paletteOwner; private ReadOnlyMemory palette; private int maxColors; - private short transparencyThreshold; + private readonly float transparencyThreshold; + private readonly short transparencyThreshold255; private readonly Box[] colorCube; private EuclideanPixelMap? pixelMap; private readonly bool isDithering; @@ -102,8 +103,9 @@ internal struct WuQuantizer : IQuantizer this.isDisposed = false; this.pixelMap = default; this.palette = default; - this.isDithering = this.isDithering = this.Options.Dither is not null; - this.transparencyThreshold = (short)(this.Options.TransparencyThreshold * 255); + this.isDithering = this.Options.Dither is not null; + this.transparencyThreshold = this.Options.TransparencyThreshold; + this.transparencyThreshold255 = (short)(this.Options.TransparencyThreshold * 255); } /// @@ -152,7 +154,13 @@ internal struct WuQuantizer : IQuantizer Moment moment = Volume(ref this.colorCube[k], momentsSpan); if (moment.Weight > 0) { - paletteSpan[k] = TPixel.FromScaledVector4(moment.Normalize()); + Vector4 normalized = moment.Normalize(); + if (normalized.W < this.transparencyThreshold) + { + normalized = Vector4.Zero; + } + + paletteSpan[k] = TPixel.FromScaledVector4(normalized); } } @@ -179,11 +187,11 @@ internal struct WuQuantizer : IQuantizer // In this case, we must use the pixel map to get the closest color. if (this.isDithering) { - return (byte)this.pixelMap!.GetClosestColor(color, out match, this.transparencyThreshold); + return (byte)this.pixelMap!.GetClosestColor(color, out match, this.transparencyThreshold255); } Rgba32 rgba = color.ToRgba32(); - if (rgba.A < this.transparencyThreshold) + if (rgba.A < this.transparencyThreshold255) { rgba = default; }