Skip to content

Commit 05c5525

Browse files
Threshold on the way out also.
1 parent e733495 commit 05c5525

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ internal struct WuQuantizer<TPixel> : IQuantizer<TPixel>
7474
private readonly IMemoryOwner<TPixel> paletteOwner;
7575
private ReadOnlyMemory<TPixel> palette;
7676
private int maxColors;
77-
private short transparencyThreshold;
77+
private readonly float transparencyThreshold;
78+
private readonly short transparencyThreshold255;
7879
private readonly Box[] colorCube;
7980
private EuclideanPixelMap<TPixel>? pixelMap;
8081
private readonly bool isDithering;
@@ -102,8 +103,9 @@ public WuQuantizer(Configuration configuration, QuantizerOptions options)
102103
this.isDisposed = false;
103104
this.pixelMap = default;
104105
this.palette = default;
105-
this.isDithering = this.isDithering = this.Options.Dither is not null;
106-
this.transparencyThreshold = (short)(this.Options.TransparencyThreshold * 255);
106+
this.isDithering = this.Options.Dither is not null;
107+
this.transparencyThreshold = this.Options.TransparencyThreshold;
108+
this.transparencyThreshold255 = (short)(this.Options.TransparencyThreshold * 255);
107109
}
108110

109111
/// <inheritdoc/>
@@ -152,7 +154,13 @@ private void ResolvePalette()
152154
Moment moment = Volume(ref this.colorCube[k], momentsSpan);
153155
if (moment.Weight > 0)
154156
{
155-
paletteSpan[k] = TPixel.FromScaledVector4(moment.Normalize());
157+
Vector4 normalized = moment.Normalize();
158+
if (normalized.W < this.transparencyThreshold)
159+
{
160+
normalized = Vector4.Zero;
161+
}
162+
163+
paletteSpan[k] = TPixel.FromScaledVector4(normalized);
156164
}
157165
}
158166

@@ -179,11 +187,11 @@ public readonly byte GetQuantizedColor(TPixel color, out TPixel match)
179187
// In this case, we must use the pixel map to get the closest color.
180188
if (this.isDithering)
181189
{
182-
return (byte)this.pixelMap!.GetClosestColor(color, out match, this.transparencyThreshold);
190+
return (byte)this.pixelMap!.GetClosestColor(color, out match, this.transparencyThreshold255);
183191
}
184192

185193
Rgba32 rgba = color.ToRgba32();
186-
if (rgba.A < this.transparencyThreshold)
194+
if (rgba.A < this.transparencyThreshold255)
187195
{
188196
rgba = default;
189197
}

0 commit comments

Comments
 (0)