Browse Source

Use jagged arrays

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

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

@ -14,11 +14,11 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] AtkinsonMatrix = private static readonly byte[][] AtkinsonMatrix =
{ {
{ 0, 0, 1, 1 }, new byte[] { 0, 0, 1, 1 },
{ 1, 1, 1, 0 }, new byte[] { 1, 1, 1, 0 },
{ 0, 1, 0, 0 } new byte[] { 0, 1, 0, 0 }
}; };
/// <summary> /// <summary>

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

@ -14,10 +14,10 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] BurksMatrix = private static readonly byte[][] BurksMatrix =
{ {
{ 0, 0, 0, 8, 4 }, new byte[] { 0, 0, 0, 8, 4 },
{ 2, 4, 8, 4, 2 } new byte[] { 2, 4, 8, 4, 2 }
}; };
/// <summary> /// <summary>

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

@ -39,20 +39,20 @@ namespace ImageSharp.Dithering
/// </summary> /// </summary>
/// <param name="matrix">The dithering matrix.</param> /// <param name="matrix">The dithering matrix.</param>
/// <param name="divisor">The divisor.</param> /// <param name="divisor">The divisor.</param>
protected ErrorDiffuser(byte[,] matrix, byte divisor) protected ErrorDiffuser(byte[][] matrix, byte divisor)
{ {
Guard.NotNull(matrix, nameof(matrix)); Guard.NotNull(matrix, nameof(matrix));
Guard.MustBeGreaterThan(divisor, 0, nameof(divisor)); Guard.MustBeGreaterThan(divisor, 0, nameof(divisor));
this.Matrix = matrix; this.Matrix = matrix;
this.matrixWidth = (byte)(matrix.GetUpperBound(1) + 1); this.matrixWidth = (byte)matrix[0].Length;
this.matrixHeight = (byte)(matrix.GetUpperBound(0) + 1); this.matrixHeight = (byte)matrix.Length;
this.divisorVector = new Vector4(divisor); this.divisorVector = new Vector4(divisor);
this.startingOffset = 0; this.startingOffset = 0;
for (int i = 0; i < this.matrixWidth; i++) for (int i = 0; i < this.matrixWidth; i++)
{ {
if (matrix[0, i] != 0) if (matrix[0][i] != 0)
{ {
this.startingOffset = (byte)(i - 1); this.startingOffset = (byte)(i - 1);
break; break;
@ -61,7 +61,7 @@ namespace ImageSharp.Dithering
} }
/// <inheritdoc /> /// <inheritdoc />
public byte[,] Matrix { get; } public byte[][] Matrix { get; }
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -85,13 +85,13 @@ namespace ImageSharp.Dithering
if (matrixX > 0 && matrixX < width && matrixY > 0 && matrixY < height) if (matrixX > 0 && matrixX < width && matrixY > 0 && matrixY < height)
{ {
byte coefficient = this.Matrix[row, col]; byte coefficient = this.Matrix[row][col];
if (coefficient == 0) if (coefficient == 0)
{ {
continue; continue;
} }
Vector4 coefficientVector = new Vector4(this.Matrix[row, col]); Vector4 coefficientVector = new Vector4(this.Matrix[row][col]);
Vector4 offsetColor = pixels[matrixX, matrixY].ToVector4(); Vector4 offsetColor = pixels[matrixX, matrixY].ToVector4();
Vector4 result = ((error * coefficientVector) / this.divisorVector) + offsetColor; Vector4 result = ((error * coefficientVector) / this.divisorVector) + offsetColor;
result.W = offsetColor.W; result.W = offsetColor.W;

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

@ -14,10 +14,10 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] FloydSteinbergMatrix = private static readonly byte[][] FloydSteinbergMatrix =
{ {
{ 0, 0, 7 }, new byte[] { 0, 0, 7 },
{ 3, 5, 1 } new byte[] { 3, 5, 1 }
}; };
/// <summary> /// <summary>

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

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

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

@ -14,11 +14,11 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] JarvisJudiceNinkeMatrix = private static readonly byte[][] JarvisJudiceNinkeMatrix =
{ {
{ 0, 0, 0, 7, 5 }, new byte[] { 0, 0, 0, 7, 5 },
{ 3, 5, 7, 5, 3 }, new byte[] { 3, 5, 7, 5, 3 },
{ 1, 3, 5, 3, 1 } new byte[] { 1, 3, 5, 3, 1 }
}; };
/// <summary> /// <summary>

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

@ -14,10 +14,10 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] Sierra2Matrix = private static readonly byte[][] Sierra2Matrix =
{ {
{ 0, 0, 0, 4, 3 }, new byte[] { 0, 0, 0, 4, 3 },
{ 1, 2, 3, 2, 1 } new byte[] { 1, 2, 3, 2, 1 }
}; };
/// <summary> /// <summary>

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

@ -14,11 +14,11 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] Sierra3Matrix = private static readonly byte[][] Sierra3Matrix =
{ {
{ 0, 0, 0, 5, 3 }, new byte[] { 0, 0, 0, 5, 3 },
{ 2, 4, 5, 4, 2 }, new byte[] { 2, 4, 5, 4, 2 },
{ 0, 2, 3, 2, 0 } new byte[] { 0, 2, 3, 2, 0 }
}; };
/// <summary> /// <summary>

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

@ -14,10 +14,10 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] SierraLiteMatrix = private static readonly byte[][] SierraLiteMatrix =
{ {
{ 0, 0, 2 }, new byte[] { 0, 0, 2 },
{ 1, 1, 0 } new byte[] { 1, 1, 0 }
}; };
/// <summary> /// <summary>

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

@ -14,11 +14,11 @@ namespace ImageSharp.Dithering
/// <summary> /// <summary>
/// The diffusion matrix /// The diffusion matrix
/// </summary> /// </summary>
private static readonly byte[,] StuckiMatrix = private static readonly byte[][] StuckiMatrix =
{ {
{ 0, 0, 0, 8, 4 }, new byte[] { 0, 0, 0, 8, 4 },
{ 2, 4, 8, 4, 2 }, new byte[] { 2, 4, 8, 4, 2 },
{ 1, 2, 4, 2, 1 } new byte[] { 1, 2, 4, 2, 1 }
}; };
/// <summary> /// <summary>

14
src/ImageSharp/Dithering/Ordered/Bayer.cs

@ -17,23 +17,23 @@ namespace ImageSharp.Dithering.Ordered
/// The threshold matrix. /// The threshold matrix.
/// This is calculated by multiplying each value in the original matrix by 16 and subtracting 1 /// This is calculated by multiplying each value in the original matrix by 16 and subtracting 1
/// </summary> /// </summary>
private static readonly byte[,] ThresholdMatrix = private static readonly byte[][] ThresholdMatrix =
{ {
{ 15, 143, 47, 175 }, new byte[] { 15, 143, 47, 175 },
{ 207, 79, 239, 111 }, new byte[] { 207, 79, 239, 111 },
{ 63, 191, 31, 159 }, new byte[] { 63, 191, 31, 159 },
{ 255, 127, 223, 95 } new byte[] { 255, 127, 223, 95 }
}; };
/// <inheritdoc /> /// <inheritdoc />
public byte[,] Matrix { get; } = ThresholdMatrix; public byte[][] Matrix { get; } = ThresholdMatrix;
/// <inheritdoc /> /// <inheritdoc />
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height) public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
source.ToXyzwBytes(bytes, 0); source.ToXyzwBytes(bytes, 0);
pixels[x, y] = ThresholdMatrix[x % 3, y % 3] >= bytes[index] ? lower : upper; pixels[x, y] = ThresholdMatrix[x % 3][y % 3] >= bytes[index] ? lower : upper;
} }
} }
} }

2
src/ImageSharp/Dithering/Ordered/IOrderedDither.cs

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

14
src/ImageSharp/Dithering/Ordered/Ordered.cs

@ -17,23 +17,23 @@ namespace ImageSharp.Dithering.Ordered
/// The threshold matrix. /// The threshold matrix.
/// This is calculated by multiplying each value in the original matrix by 16 /// This is calculated by multiplying each value in the original matrix by 16
/// </summary> /// </summary>
private static readonly byte[,] ThresholdMatrix = private static readonly byte[][] ThresholdMatrix =
{ {
{ 0, 128, 32, 160 }, new byte[] { 0, 128, 32, 160 },
{ 192, 64, 224, 96 }, new byte[] { 192, 64, 224, 96 },
{ 48, 176, 16, 144 }, new byte[] { 48, 176, 16, 144 },
{ 240, 112, 208, 80 } new byte[] { 240, 112, 208, 80 }
}; };
/// <inheritdoc /> /// <inheritdoc />
public byte[,] Matrix { get; } = ThresholdMatrix; public byte[][] Matrix { get; } = ThresholdMatrix;
/// <inheritdoc /> /// <inheritdoc />
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height) public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height)
where TColor : struct, IPackedPixel, IEquatable<TColor> where TColor : struct, IPackedPixel, IEquatable<TColor>
{ {
source.ToXyzwBytes(bytes, 0); source.ToXyzwBytes(bytes, 0);
pixels[x, y] = ThresholdMatrix[x % 3, y % 3] >= bytes[index] ? lower : upper; pixels[x, y] = ThresholdMatrix[x % 3][y % 3] >= bytes[index] ? lower : upper;
} }
} }
} }
Loading…
Cancel
Save