diff --git a/tests/ImageSharp.Tests/ExactImageComparer.cs b/tests/ImageSharp.Tests/ExactImageComparer.cs new file mode 100644 index 0000000000..f1202e27b8 --- /dev/null +++ b/tests/ImageSharp.Tests/ExactImageComparer.cs @@ -0,0 +1,53 @@ +namespace ImageSharp.Tests +{ + using System; + using System.Collections.Generic; + + using ImageSharp.PixelFormats; + + using SixLabors.Primitives; + + public class ExactImageComparer : ImageComparer + { + public static ExactImageComparer Instance { get; } = new ExactImageComparer(); + + public override void Verify(Image expected, Image actual) + { + if (expected.Size() != actual.Size()) + { + throw new ImageDimensionsMismatchException(expected.Size(), actual.Size()); + } + + int width = actual.Width; + + // TODO: Comparing through Rgba32 is not robust enough because of the existance of super high precision pixel types. + + Rgba32[] aBuffer = new Rgba32[width]; + Rgba32[] bBuffer = new Rgba32[width]; + + var differences = new List(); + + for (int y = 0; y < actual.Height; y++) + { + Span aSpan = expected.GetRowSpan(y); + Span bSpan = actual.GetRowSpan(y); + + PixelOperations.Instance.ToRgba32(aSpan, aBuffer, width); + PixelOperations.Instance.ToRgba32(bSpan, bBuffer, width); + + for (int x = 0; x < width; x++) + { + if (aBuffer[x] != bBuffer[x]) + { + differences.Add(new Point(x, y)); + } + } + } + + if (differences.Count > 0) + { + throw new ImagesAreNotEqualException(differences.ToArray()); + } + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/ImageComparer.cs b/tests/ImageSharp.Tests/ImageComparer.cs index 6994d99ed5..2469c4611e 100644 --- a/tests/ImageSharp.Tests/ImageComparer.cs +++ b/tests/ImageSharp.Tests/ImageComparer.cs @@ -1,7 +1,5 @@ namespace ImageSharp.Tests { - using System; - using ImageSharp.PixelFormats; public abstract class ImageComparer @@ -9,14 +7,4 @@ namespace ImageSharp.Tests public abstract void Verify(Image expected, Image actual) where TPixelA : struct, IPixel where TPixelB : struct, IPixel; } - - public class ExactComparer : ImageComparer - { - public static ExactComparer Instance { get; } = new ExactComparer(); - - public override void Verify(Image expected, Image actual) - { - throw new NotImplementedException(); - } - } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs index e95a084df4..db58e9ba0c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs @@ -122,7 +122,7 @@ namespace ImageSharp.Tests { using (Image clone = image.Clone()) { - ExactComparer.Instance.Verify(image, clone); + ExactImageComparer.Instance.Verify(image, clone); } } } @@ -140,7 +140,7 @@ namespace ImageSharp.Tests ImageDimensionsMismatchException ex = Assert.ThrowsAny( () => { - ExactComparer.Instance.Verify(image, clone); + ExactImageComparer.Instance.Verify(image, clone); }); this.Output.WriteLine(ex.Message); } @@ -156,13 +156,13 @@ namespace ImageSharp.Tests { using (Image clone = image.Clone()) { - ModifyPixel(clone, 42, 42, 1); + ModifyPixel(clone, 42, 24, 1); ModifyPixel(clone, 7, 93, 1); ImagesAreNotEqualException ex = Assert.ThrowsAny( () => { - ExactComparer.Instance.Verify(image, clone); + ExactImageComparer.Instance.Verify(image, clone); }); this.Output.WriteLine(ex.Message); Assert.Equal(2, ex.Differences.Length);