Browse Source

Handle transparent pixels with Octree

pull/1114/head
James Jackson-South 6 years ago
parent
commit
f921df1cb0
  1. 23
      src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs

23
src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs

@ -92,7 +92,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
protected override byte GetQuantizedColor(TPixel color, ReadOnlySpan<TPixel> palette, out TPixel match) protected override byte GetQuantizedColor(TPixel color, ReadOnlySpan<TPixel> 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); var index = (byte)this.octree.GetPaletteIndex(color);
match = palette[index]; match = palette[index];
@ -110,7 +113,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
=> this.palette ?? (this.palette = this.octree.Palletize(this.colors)); => this.palette ?? (this.palette = this.octree.Palletize(this.colors));
/// <summary> /// <summary>
/// Class which does the actual quantization /// Class which does the actual quantization.
/// </summary> /// </summary>
private sealed class Octree private sealed class Octree
{ {
@ -332,15 +335,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="OctreeNode"/> class. /// Initializes a new instance of the <see cref="OctreeNode"/> class.
/// </summary> /// </summary>
/// <param name="level"> /// <param name="level">The level in the tree = 0 - 7.</param>
/// The level in the tree = 0 - 7 /// <param name="colorBits">The number of significant color bits in the image.</param>
/// </param> /// <param name="octree">The tree to which this node belongs.</param>
/// <param name="colorBits">
/// The number of significant color bits in the image
/// </param>
/// <param name="octree">
/// The tree to which this node belongs
/// </param>
public OctreeNode(int level, int colorBits, Octree octree) public OctreeNode(int level, int colorBits, Octree octree)
{ {
// Construct the new node // Construct the new node
@ -524,9 +521,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{ {
int shift = 7 - level; int shift = 7 - level;
byte mask = Mask[level]; byte mask = Mask[level];
return ((color.B & mask) >> (shift - 2)) return ((color.R & mask) >> shift)
| ((color.G & mask) >> (shift - 1)) | ((color.G & mask) >> (shift - 1))
| ((color.R & mask) >> shift); | ((color.B & mask) >> (shift - 2));
} }
/// <summary> /// <summary>

Loading…
Cancel
Save