diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs
index 76a5f96ec..1fa6852c2 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs
@@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] AtkinsonMatrix =
+ private static readonly Fast2DArray AtkinsonMatrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 1, 1 },
- new byte[] { 1, 1, 1, 0 },
- new byte[] { 0, 1, 0, 0 }
- };
+ { 0, 0, 1, 1 },
+ { 1, 1, 1, 0 },
+ { 0, 1, 0, 0 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs
index 2d71b9cc3..a4adcb0a4 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs
@@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] BurksMatrix =
+ private static readonly Fast2DArray BurksMatrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 0, 8, 4 },
- new byte[] { 2, 4, 8, 4, 2 }
- };
+ { 0, 0, 0, 8, 4 },
+ { 2, 4, 8, 4, 2 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
index 0c2a1dc40..4fd5476f0 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
@@ -22,12 +22,12 @@ namespace ImageSharp.Dithering
///
/// The matrix width
///
- private readonly byte matrixHeight;
+ private readonly int matrixHeight;
///
/// The matrix height
///
- private readonly byte matrixWidth;
+ private readonly int matrixWidth;
///
/// The offset at which to start the dithering operation.
@@ -39,20 +39,22 @@ namespace ImageSharp.Dithering
///
/// The dithering matrix.
/// The divisor.
- protected ErrorDiffuser(byte[][] matrix, byte divisor)
+ protected ErrorDiffuser(Fast2DArray matrix, byte divisor)
{
Guard.NotNull(matrix, nameof(matrix));
Guard.MustBeGreaterThan(divisor, 0, nameof(divisor));
this.Matrix = matrix;
- this.matrixWidth = (byte)matrix[0].Length;
- this.matrixHeight = (byte)matrix.Length;
+ this.matrixWidth = this.Matrix.Width;
+ this.matrixHeight = this.Matrix.Height;
this.divisorVector = new Vector4(divisor);
this.startingOffset = 0;
for (int i = 0; i < this.matrixWidth; i++)
{
- if (matrix[0][i] != 0)
+ // Good to disable here as we are not comparing matematical output.
+ // ReSharper disable once CompareOfFloatsByEqualityOperator
+ if (matrix[0, i] != 0)
{
this.startingOffset = (byte)(i - 1);
break;
@@ -61,7 +63,7 @@ namespace ImageSharp.Dithering
}
///
- public byte[][] Matrix { get; }
+ public Fast2DArray Matrix { get; }
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -85,13 +87,16 @@ namespace ImageSharp.Dithering
if (matrixX > 0 && matrixX < width && matrixY > 0 && matrixY < height)
{
- byte coefficient = this.Matrix[row][col];
+ float coefficient = this.Matrix[row, col];
+
+ // Good to disable here as we are not comparing matematical output.
+ // ReSharper disable once CompareOfFloatsByEqualityOperator
if (coefficient == 0)
{
continue;
}
- Vector4 coefficientVector = new Vector4(this.Matrix[row][col]);
+ Vector4 coefficientVector = new Vector4(coefficient);
Vector4 offsetColor = pixels[matrixX, matrixY].ToVector4();
Vector4 result = ((error * coefficientVector) / this.divisorVector) + offsetColor;
result.W = offsetColor.W;
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs b/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs
index cf91bd8fa..7b67d2dd1 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs
@@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] FloydSteinbergMatrix =
+ private static readonly Fast2DArray FloydSteinbergMatrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 7 },
- new byte[] { 3, 5, 1 }
- };
+ { 0, 0, 7 },
+ { 3, 5, 1 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs b/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
index eb6c27167..fbee27088 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
@@ -15,7 +15,7 @@ namespace ImageSharp.Dithering
///
/// Gets the dithering matrix
///
- byte[][] Matrix { get; }
+ Fast2DArray Matrix { get; }
///
/// Transforms the image applying the dither matrix. This method alters the input pixels array
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs b/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs
index 5f26d8b24..32f38fbc8 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs
@@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] JarvisJudiceNinkeMatrix =
+ private static readonly Fast2DArray JarvisJudiceNinkeMatrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 0, 7, 5 },
- new byte[] { 3, 5, 7, 5, 3 },
- new byte[] { 1, 3, 5, 3, 1 }
- };
+ { 0, 0, 0, 7, 5 },
+ { 3, 5, 7, 5, 3 },
+ { 1, 3, 5, 3, 1 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs
index 44132747d..47b14944e 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs
@@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] Sierra2Matrix =
+ private static readonly Fast2DArray Sierra2Matrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 0, 4, 3 },
- new byte[] { 1, 2, 3, 2, 1 }
- };
+ { 0, 0, 0, 4, 3 },
+ { 1, 2, 3, 2, 1 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs
index 01f6b5ded..ae33954cf 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs
@@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] Sierra3Matrix =
+ private static readonly Fast2DArray Sierra3Matrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 0, 5, 3 },
- new byte[] { 2, 4, 5, 4, 2 },
- new byte[] { 0, 2, 3, 2, 0 }
- };
+ { 0, 0, 0, 5, 3 },
+ { 2, 4, 5, 4, 2 },
+ { 0, 2, 3, 2, 0 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs b/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs
index 42fa8c23d..8a1e17816 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs
@@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] SierraLiteMatrix =
+ private static readonly Fast2DArray SierraLiteMatrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 2 },
- new byte[] { 1, 1, 0 }
- };
+ { 0, 0, 2 },
+ { 1, 1, 0 }
+ });
///
/// Initializes a new instance of the class.
diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs
index 0e462630e..b5d22b259 100644
--- a/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs
+++ b/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs
@@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
///
/// The diffusion matrix
///
- private static readonly byte[][] StuckiMatrix =
+ private static readonly Fast2DArray StuckiMatrix =
+ new Fast2DArray(new float[,]
{
- new byte[] { 0, 0, 0, 8, 4 },
- new byte[] { 2, 4, 8, 4, 2 },
- new byte[] { 1, 2, 4, 2, 1 }
- };
+ { 0, 0, 0, 8, 4 },
+ { 2, 4, 8, 4, 2 },
+ { 1, 2, 4, 2, 1 }
+ });
///
/// Initializes a new instance of the class.