From aef2e7f2bce302a819edca60d094c293762a1bf9 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 16 Feb 2020 00:20:53 +1100 Subject: [PATCH] Handle transparent pixels with Octree --- .../OctreeFrameQuantizer{TPixel}.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs index b489b5e8d..643507351 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs @@ -92,7 +92,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization [MethodImpl(InliningOptions.ShortMethod)] protected override byte GetQuantizedColor(TPixel color, ReadOnlySpan palette, out TPixel match) { - if (!this.DoDither) + // Octree only maps the RGB component of a color + // so cannot tell the difference between a fully transparent + // pixel and a black one. + if (!this.DoDither && !color.Equals(default)) { var index = (byte)this.octree.GetPaletteIndex(color); match = palette[index]; @@ -110,7 +113,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization => this.palette ?? (this.palette = this.octree.Palletize(this.colors)); /// - /// Class which does the actual quantization + /// Class which does the actual quantization. /// private sealed class Octree { @@ -332,15 +335,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// Initializes a new instance of the class. /// - /// - /// The level in the tree = 0 - 7 - /// - /// - /// The number of significant color bits in the image - /// - /// - /// The tree to which this node belongs - /// + /// The level in the tree = 0 - 7. + /// The number of significant color bits in the image. + /// The tree to which this node belongs. public OctreeNode(int level, int colorBits, Octree octree) { // Construct the new node @@ -524,9 +521,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization { int shift = 7 - level; byte mask = Mask[level]; - return ((color.B & mask) >> (shift - 2)) + return ((color.R & mask) >> shift) | ((color.G & mask) >> (shift - 1)) - | ((color.R & mask) >> shift); + | ((color.B & mask) >> (shift - 2)); } ///