diff --git a/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs b/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs index 3bc1df0bb..3c33492b8 100644 --- a/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs +++ b/src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs @@ -78,6 +78,19 @@ namespace SixLabors.ImageSharp.Processing.Dithering.ErrorDiffusion // Calculate the error Vector4 error = source.ToVector4() - transformed.ToVector4(); + // No error? Break out as there's nothing to pass. + if (error.Equals(Vector4.Zero)) + { + return; + } + + this.DoDither(image, x, y, minX, minY, maxX, maxY, error); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private void DoDither(ImageFrame image, int x, int y, int minX, int minY, int maxX, int maxY, Vector4 error) + where TPixel : struct, IPixel + { // Loop through and distribute the error amongst neighboring pixels. for (int row = 0; row < this.matrixHeight; row++) { diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs index 0c21f6e5e..08b0cb5c3 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs @@ -125,9 +125,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers protected byte GetClosestPixel(TPixel pixel, TPixel[] colorPalette, Dictionary cache) { // Check if the color is in the lookup table - if (cache.ContainsKey(pixel)) + if (cache.TryGetValue(pixel, out byte value)) { - return cache[pixel]; + return value; } return this.GetClosestPixelSlow(pixel, colorPalette, cache); diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs index 99519031d..df5fee506 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs @@ -223,11 +223,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// private readonly OctreeNode root; - /// - /// Array of reducible nodes - /// - private readonly OctreeNode[] reducibleNodes; - /// /// Maximum number of significant bits in the image /// @@ -253,7 +248,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers { this.maxColorBits = maxColorBits; this.Leaves = 0; - this.reducibleNodes = new OctreeNode[9]; + this.ReducibleNodes = new OctreeNode[9]; this.root = new OctreeNode(0, this.maxColorBits, this); this.previousColor = default; this.previousNode = null; @@ -262,12 +257,12 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// Gets or sets the number of leaves in the tree /// - private int Leaves { get; set; } + public int Leaves { get; set; } /// /// Gets the array of reducible nodes /// - private OctreeNode[] ReducibleNodes => this.reducibleNodes; + private OctreeNode[] ReducibleNodes { get; } /// /// Add a given color value to the Octree @@ -354,14 +349,14 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers { // Find the deepest level containing at least one reducible node int index = this.maxColorBits - 1; - while ((index > 0) && (this.reducibleNodes[index] == null)) + while ((index > 0) && (this.ReducibleNodes[index] == null)) { index--; } // Reduce the node most recently added to the list at level 'index' - OctreeNode node = this.reducibleNodes[index]; - this.reducibleNodes[index] = node.NextReducible; + OctreeNode node = this.ReducibleNodes[index]; + this.ReducibleNodes[index] = node.NextReducible; // Decrement the leaf count after reducing the node this.Leaves -= node.Reduce();