From 928d8b5e36af7d73fbdfc6261971e8d960f254d2 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 31 Jan 2018 12:33:49 +1100 Subject: [PATCH] Fix and optimize error diffusion --- .../Dithering/ErrorDiffusion/ErrorDiffuserBase.cs | 8 +++----- src/ImageSharp/Formats/Png/PngEncoder.cs | 2 +- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 2 +- src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs | 2 +- src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs | 4 ++-- 5 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs index 510a097ea..46bafcc0c 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuserBase.cs @@ -58,7 +58,7 @@ namespace SixLabors.ImageSharp.Dithering.Base this.startingOffset = 0; for (int i = 0; i < this.matrixWidth; i++) { - // Good to disable here as we are not comparing matematical output. + // Good to disable here as we are not comparing mathematical output. // ReSharper disable once CompareOfFloatsByEqualityOperator if (matrix[0, i] != 0) { @@ -90,7 +90,7 @@ namespace SixLabors.ImageSharp.Dithering.Base // Calculate the error Vector4 error = source.ToVector4() - transformed.ToVector4(); - // Loop through and distribute the error amongst neighbouring pixels. + // Loop through and distribute the error amongst neighboring pixels. for (int row = 0; row < this.matrixHeight; row++) { int matrixY = y + row; @@ -115,10 +115,8 @@ namespace SixLabors.ImageSharp.Dithering.Base ref TPixel pixel = ref rowSpan[matrixX]; var offsetColor = pixel.ToVector4(); - var coefficientVector = new Vector4(coefficient); - Vector4 result = ((error * coefficientVector) / this.divisorVector) + offsetColor; - result.W = offsetColor.W; + Vector4 result = ((error * coefficient) / this.divisorVector) + offsetColor; pixel.PackFromVector4(result); } } diff --git a/src/ImageSharp/Formats/Png/PngEncoder.cs b/src/ImageSharp/Formats/Png/PngEncoder.cs index 2fc6911f0..4abafc9e8 100644 --- a/src/ImageSharp/Formats/Png/PngEncoder.cs +++ b/src/ImageSharp/Formats/Png/PngEncoder.cs @@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Formats.Png public bool IgnoreMetadata { get; set; } /// - /// Gets or sets the size of the color palette to use. Set to zero to leav png encoding to use pixel data. + /// Gets or sets the size of the color palette to use. Set to zero to leave png encoding to use pixel data. /// public int PaletteSize { get; set; } = 0; diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 0efd46ec7..6dbf2eeb8 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -148,7 +148,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// /// Initializes a new instance of the class. /// - /// The options for influancing the encoder + /// The options for influencing the encoder public PngEncoderCore(IPngEncoderOptions options) { this.ignoreMetadata = options.IgnoreMetadata; diff --git a/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs b/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs index d57865c97..20ba2e637 100644 --- a/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs +++ b/src/ImageSharp/Quantizers/QuantizerBase{TPixel}.cs @@ -40,7 +40,7 @@ namespace SixLabors.ImageSharp.Quantizers.Base } /// - public bool Dither { get; set; } = false; + public bool Dither { get; set; } = true; /// public IErrorDiffuser DitherType { get; set; } = new FloydSteinbergDiffuser(); diff --git a/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs b/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs index cb9eb9b0e..f08114574 100644 --- a/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs @@ -281,7 +281,7 @@ namespace SixLabors.ImageSharp.Quantizers } /// - /// Gets the index index of the given color in the palette. + /// Gets the index of the given color in the palette. /// /// The red value. /// The green value. @@ -827,7 +827,7 @@ namespace SixLabors.ImageSharp.Quantizers { if (this.Dither) { - // The colors have changed so we need to use Euclidean distance caclulation to find the closest value. + // The colors have changed so we need to use Euclidean distance calculation to find the closest value. // This palette can never be null here. return this.GetClosestPixel(pixel, this.palette, this.colorMap); }