Browse Source

Minor improvements to diffusion

af/merge-core
James Jackson-South 8 years ago
parent
commit
fef5fa0681
  1. 13
      src/ImageSharp/Processing/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs
  2. 4
      src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs
  3. 17
      src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs

13
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<TPixel>(ImageFrame<TPixel> image, int x, int y, int minX, int minY, int maxX, int maxY, Vector4 error)
where TPixel : struct, IPixel<TPixel>
{
// Loop through and distribute the error amongst neighboring pixels.
for (int row = 0; row < this.matrixHeight; row++)
{

4
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<TPixel, byte> 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);

17
src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs

@ -223,11 +223,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
/// </summary>
private readonly OctreeNode root;
/// <summary>
/// Array of reducible nodes
/// </summary>
private readonly OctreeNode[] reducibleNodes;
/// <summary>
/// Maximum number of significant bits in the image
/// </summary>
@ -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
/// <summary>
/// Gets or sets the number of leaves in the tree
/// </summary>
private int Leaves { get; set; }
public int Leaves { get; set; }
/// <summary>
/// Gets the array of reducible nodes
/// </summary>
private OctreeNode[] ReducibleNodes => this.reducibleNodes;
private OctreeNode[] ReducibleNodes { get; }
/// <summary>
/// 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();

Loading…
Cancel
Save