diff --git a/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs b/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs
index 3455031fd..401c83ce6 100644
--- a/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs
+++ b/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs
@@ -13,7 +13,7 @@ namespace ImageSharp
/// Provides fast access to 2D arrays.
///
/// The type of elements in the array.
- public struct Fast2DArray
+ internal struct Fast2DArray
{
///
/// The 1D representation of the 2D array.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
index 20a45d4df..cde146f1e 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
@@ -5,12 +5,11 @@
namespace ImageSharp.Dithering
{
- using System;
using System.Numerics;
using System.Runtime.CompilerServices;
///
- /// The base class for performing effor diffusion based dithering.
+ /// The base class for performing error diffusion based dithering.
///
public abstract class ErrorDiffuser : IErrorDiffuser
{
@@ -34,19 +33,24 @@ namespace ImageSharp.Dithering
///
private readonly int startingOffset;
+ ///
+ /// The diffusion matrix
+ ///
+ private readonly Fast2DArray matrix;
+
///
/// Initializes a new instance of the class.
///
/// The dithering matrix.
/// The divisor.
- protected ErrorDiffuser(Fast2DArray matrix, byte divisor)
+ internal ErrorDiffuser(Fast2DArray matrix, byte divisor)
{
Guard.NotNull(matrix, nameof(matrix));
Guard.MustBeGreaterThan(divisor, 0, nameof(divisor));
- this.Matrix = matrix;
- this.matrixWidth = this.Matrix.Width;
- this.matrixHeight = this.Matrix.Height;
+ this.matrix = matrix;
+ this.matrixWidth = this.matrix.Width;
+ this.matrixHeight = this.matrix.Height;
this.divisorVector = new Vector4(divisor);
this.startingOffset = 0;
@@ -62,9 +66,6 @@ namespace ImageSharp.Dithering
}
}
- ///
- public Fast2DArray Matrix { get; }
-
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dither(PixelAccessor pixels, TColor source, TColor transformed, int x, int y, int width, int height)
@@ -87,9 +88,9 @@ namespace ImageSharp.Dithering
if (matrixX > 0 && matrixX < width && matrixY > 0 && matrixY < height)
{
- float coefficient = this.Matrix[row, col];
+ float coefficient = this.matrix[row, col];
- // 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 (coefficient == 0)
{
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs b/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
index 4fb31c13e..18079b1fb 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
@@ -12,11 +12,6 @@ namespace ImageSharp.Dithering
///
public interface IErrorDiffuser
{
- ///
- /// Gets the dithering matrix
- ///
- Fast2DArray Matrix { get; }
-
///
/// Transforms the image applying the dither matrix. This method alters the input pixels array
///
diff --git a/src/ImageSharp/Dithering/Ordered/Bayer.cs b/src/ImageSharp/Dithering/Ordered/Bayer.cs
index 1027e51d9..3792c3c02 100644
--- a/src/ImageSharp/Dithering/Ordered/Bayer.cs
+++ b/src/ImageSharp/Dithering/Ordered/Bayer.cs
@@ -5,13 +5,11 @@
namespace ImageSharp.Dithering.Ordered
{
- using System;
-
///
/// Applies error diffusion based dithering using the 4x4 Bayer dithering matrix.
///
///
- public class Bayer : IOrderedDither
+ public sealed class Bayer : OrderedDither4x4
{
///
/// The threshold matrix.
@@ -26,15 +24,12 @@ namespace ImageSharp.Dithering.Ordered
{ 255, 127, 223, 95 }
};
- ///
- public Fast2DArray Matrix { get; } = ThresholdMatrix;
-
- ///
- public void Dither(PixelAccessor pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
- where TColor : struct, IPixel
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public Bayer()
+ : base(ThresholdMatrix)
{
- source.ToXyzwBytes(bytes, 0);
- pixels[x, y] = ThresholdMatrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
}
}
}
\ No newline at end of file
diff --git a/src/ImageSharp/Dithering/Ordered/IOrderedDither.cs b/src/ImageSharp/Dithering/Ordered/IOrderedDither.cs
index 162cdb6a1..5c9897374 100644
--- a/src/ImageSharp/Dithering/Ordered/IOrderedDither.cs
+++ b/src/ImageSharp/Dithering/Ordered/IOrderedDither.cs
@@ -5,18 +5,11 @@
namespace ImageSharp.Dithering
{
- using System;
-
///
/// Encapsulates properties and methods required to perfom ordered dithering on an image.
///
public interface IOrderedDither
{
- ///
- /// Gets the dithering matrix
- ///
- Fast2DArray Matrix { get; }
-
///
/// Transforms the image applying the dither matrix. This method alters the input pixels array
///
@@ -34,4 +27,4 @@ namespace ImageSharp.Dithering
void Dither(PixelAccessor pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPixel;
}
-}
+}
\ No newline at end of file
diff --git a/src/ImageSharp/Dithering/Ordered/Ordered.cs b/src/ImageSharp/Dithering/Ordered/Ordered.cs
index aabca31aa..ae75b87f2 100644
--- a/src/ImageSharp/Dithering/Ordered/Ordered.cs
+++ b/src/ImageSharp/Dithering/Ordered/Ordered.cs
@@ -5,13 +5,11 @@
namespace ImageSharp.Dithering.Ordered
{
- using System;
-
///
/// Applies error diffusion based dithering using the 4x4 ordered dithering matrix.
///
///
- public class Ordered : IOrderedDither
+ public sealed class Ordered : OrderedDither4x4
{
///
/// The threshold matrix.
@@ -26,15 +24,12 @@ namespace ImageSharp.Dithering.Ordered
{ 240, 112, 208, 80 }
};
- ///
- public Fast2DArray Matrix { get; } = ThresholdMatrix;
-
- ///
- public void Dither(PixelAccessor pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
- where TColor : struct, IPixel
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public Ordered()
+ : base(ThresholdMatrix)
{
- source.ToXyzwBytes(bytes, 0);
- pixels[x, y] = ThresholdMatrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
}
}
}
\ No newline at end of file
diff --git a/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs b/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs
new file mode 100644
index 000000000..c2b55d98e
--- /dev/null
+++ b/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs
@@ -0,0 +1,38 @@
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp.Dithering.Ordered
+{
+ ///
+ /// The base class for performing ordered ditheroing using a 4x4 matrix.
+ ///
+ public abstract class OrderedDither4x4 : IOrderedDither
+ {
+ ///
+ /// The dithering matrix
+ ///
+ private Fast2DArray matrix;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The thresholding matrix.
+ internal OrderedDither4x4(Fast2DArray matrix)
+ {
+ this.matrix = matrix;
+ }
+
+ ///
+ public void Dither(PixelAccessor pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
+ where TColor : struct, IPixel
+ {
+ // TODO: This doesn't really cut it for me.
+ // I'd rather be using float but we need to add some sort of movalization vector methods to all IPixel implementations
+ // before we can do that as the vectors all cover different ranges.
+ source.ToXyzwBytes(bytes, 0);
+ pixels[x, y] = this.matrix[y % 3, x % 3] >= bytes[index] ? lower : upper;
+ }
+ }
+}
\ No newline at end of file