diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 5f4055a936..1d40163d10 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -12,6 +12,8 @@ using Xunit; namespace ImageSharp.Tests { + using ImageSharp.Tests.TestUtilities.ImageComparison; + public class PngDecoderTests { private const PixelTypes PixelTypes = Tests.PixelTypes.Rgba32 | Tests.PixelTypes.RgbaVector | Tests.PixelTypes.Argb32; @@ -19,17 +21,19 @@ namespace ImageSharp.Tests public static readonly string[] TestFiles = { TestImages.Png.Splash, TestImages.Png.Indexed, TestImages.Png.Interlaced, TestImages.Png.FilterVar, - TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2, TestImages.Png.Rgb48Bpp, TestImages.Png.Rgb48BppInterlaced + TestImages.Png.Bad.ChunkLength1, TestImages.Png.Bad.ChunkLength2, TestImages.Png.Rgb48Bpp, + TestImages.Png.Rgb48BppInterlaced, TestImages.Png.SnakeGame }; [Theory] [WithFileCollection(nameof(TestFiles), PixelTypes)] - public void Decode(TestImageProvider imageProvider) + public void Decode(TestImageProvider provider) where TPixel : struct, IPixel { - using (Image image = imageProvider.GetImage()) + using (Image image = provider.GetImage(new PngDecoder())) { - image.DebugSave(imageProvider); + image.DebugSave(provider); + image.CompareToOriginal(provider, ImageComparer.Exact); } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 527b78ae6b..66ca93bd23 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -31,6 +31,7 @@ namespace ImageSharp.Tests public const string Bike = "Png/Bike.png"; public const string BikeGrayscale = "Png/BikeGrayscale.png"; public const string Rgb48BppInterlaced = "Png/rgb-48bpp-interlaced.png"; + public const string SnakeGame = "Png/SnakeGame.png"; // Filtered test images from http://www.schaik.com/pngsuite/pngsuite_fil_png.html public const string Filter0 = "Png/filter0.png"; diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs index b13905ef57..80102e6010 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Tests.TestUtilities.ImageComparison public abstract class ImageComparer { - public static ImageComparer Exact { get; } = ExactImageComparer.Instance; + public static ImageComparer Exact { get; } = Tolerant(0, 0); public static ImageComparer Tolerant( float imageThreshold = TolerantImageComparer.DefaultImageThreshold, diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs index 23e5761806..0351b00200 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs @@ -66,7 +66,7 @@ for (int x = 0; x < width; x++) { - int d = GetDifferenceInPixelByteSum(ref aBuffer[x], ref bBuffer[x]); + int d = GetHammingDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); if (d > this.PixelThresholdInPixelByteSum) { @@ -92,7 +92,7 @@ } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int GetDifferenceInPixelByteSum(ref Rgba32 a, ref Rgba32 b) + private static int GetHammingDistanceInRgbaSpace(ref Rgba32 a, ref Rgba32 b) { return Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A); } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs index 7cfc5c6e40..9b2f7c479e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.cs @@ -119,7 +119,7 @@ namespace ImageSharp.Tests return Configuration.FindEncoder(format); } - private static IImageFormat GetImageFormat(string filePath) + internal static IImageFormat GetImageFormat(string filePath) { string extension = Path.GetExtension(filePath).ToLower(); if (extension[0] == '.') extension = extension.Substring(1); diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index e4563df4fd..17d75ff3f2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -7,7 +7,9 @@ namespace ImageSharp.Tests { using System; using System.IO; + using System.Linq; + using ImageSharp.Formats; using ImageSharp.PixelFormats; using ImageSharp.Tests.TestUtilities.ImageComparison; using ImageSharp.Tests.TestUtilities.ReferenceCodecs; @@ -137,9 +139,17 @@ namespace ImageSharp.Tests var testFile = TestFile.Create(path); - using (var original = Image.Load(testFile.Bytes, SystemDrawingReferenceDecoder.Instance)) + IImageDecoder referenceDecoder = TestEnvironment.GetReferenceDecoder(path); + IImageFormat format = TestEnvironment.GetImageFormat(path); + IImageDecoder defaultDecoder = Configuration.Default.FindDecoder(format); + + if (referenceDecoder.GetType() == defaultDecoder.GetType()) + { + throw new InvalidOperationException($"Can't use CompareToOriginal(): no actual reference decoder registered for {format.Name}"); + } + + using (var original = Image.Load(testFile.Bytes, referenceDecoder)) { - //original.DebugSave(provider, "__SYSTEMDRAWING__"); comparer.VerifySimilarity(original, image); } diff --git a/tests/Images/Input/Png/SnakeGame.png b/tests/Images/Input/Png/SnakeGame.png new file mode 100644 index 0000000000..96d72b38aa Binary files /dev/null and b/tests/Images/Input/Png/SnakeGame.png differ