Browse Source

Use Fast2DArray for error diffusion.

af/merge-core
James Jackson-South 9 years ago
parent
commit
e06b30f044
  1. 11
      src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs
  2. 9
      src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs
  3. 23
      src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
  4. 9
      src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs
  5. 2
      src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs
  6. 11
      src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs
  7. 9
      src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs
  8. 11
      src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs
  9. 9
      src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs
  10. 11
      src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs

11
src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs

@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] AtkinsonMatrix =
private static readonly Fast2DArray<float> AtkinsonMatrix =
new Fast2DArray<float>(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 }
});
/// <summary>
/// Initializes a new instance of the <see cref="Atkinson"/> class.

9
src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs

@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] BurksMatrix =
private static readonly Fast2DArray<float> BurksMatrix =
new Fast2DArray<float>(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 }
});
/// <summary>
/// Initializes a new instance of the <see cref="Burks"/> class.

23
src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs

@ -22,12 +22,12 @@ namespace ImageSharp.Dithering
/// <summary>
/// The matrix width
/// </summary>
private readonly byte matrixHeight;
private readonly int matrixHeight;
/// <summary>
/// The matrix height
/// </summary>
private readonly byte matrixWidth;
private readonly int matrixWidth;
/// <summary>
/// The offset at which to start the dithering operation.
@ -39,20 +39,22 @@ namespace ImageSharp.Dithering
/// </summary>
/// <param name="matrix">The dithering matrix.</param>
/// <param name="divisor">The divisor.</param>
protected ErrorDiffuser(byte[][] matrix, byte divisor)
protected ErrorDiffuser(Fast2DArray<float> 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
}
/// <inheritdoc />
public byte[][] Matrix { get; }
public Fast2DArray<float> Matrix { get; }
/// <inheritdoc />
[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;

9
src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs

@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] FloydSteinbergMatrix =
private static readonly Fast2DArray<float> FloydSteinbergMatrix =
new Fast2DArray<float>(new float[,]
{
new byte[] { 0, 0, 7 },
new byte[] { 3, 5, 1 }
};
{ 0, 0, 7 },
{ 3, 5, 1 }
});
/// <summary>
/// Initializes a new instance of the <see cref="FloydSteinberg"/> class.

2
src/ImageSharp/Dithering/ErrorDiffusion/IErrorDiffuser.cs

@ -15,7 +15,7 @@ namespace ImageSharp.Dithering
/// <summary>
/// Gets the dithering matrix
/// </summary>
byte[][] Matrix { get; }
Fast2DArray<float> Matrix { get; }
/// <summary>
/// Transforms the image applying the dither matrix. This method alters the input pixels array

11
src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs

@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] JarvisJudiceNinkeMatrix =
private static readonly Fast2DArray<float> JarvisJudiceNinkeMatrix =
new Fast2DArray<float>(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 }
});
/// <summary>
/// Initializes a new instance of the <see cref="JarvisJudiceNinke"/> class.

9
src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs

@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] Sierra2Matrix =
private static readonly Fast2DArray<float> Sierra2Matrix =
new Fast2DArray<float>(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 }
});
/// <summary>
/// Initializes a new instance of the <see cref="Sierra2"/> class.

11
src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs

@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] Sierra3Matrix =
private static readonly Fast2DArray<float> Sierra3Matrix =
new Fast2DArray<float>(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 }
});
/// <summary>
/// Initializes a new instance of the <see cref="Sierra3"/> class.

9
src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs

@ -14,11 +14,12 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] SierraLiteMatrix =
private static readonly Fast2DArray<float> SierraLiteMatrix =
new Fast2DArray<float>(new float[,]
{
new byte[] { 0, 0, 2 },
new byte[] { 1, 1, 0 }
};
{ 0, 0, 2 },
{ 1, 1, 0 }
});
/// <summary>
/// Initializes a new instance of the <see cref="SierraLite"/> class.

11
src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs

@ -14,12 +14,13 @@ namespace ImageSharp.Dithering
/// <summary>
/// The diffusion matrix
/// </summary>
private static readonly byte[][] StuckiMatrix =
private static readonly Fast2DArray<float> StuckiMatrix =
new Fast2DArray<float>(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 }
});
/// <summary>
/// Initializes a new instance of the <see cref="Stucki"/> class.

Loading…
Cancel
Save