diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 141105c48..79dd37893 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -8,12 +8,15 @@ namespace ImageSharp.Tests { using System; using System.IO; + using System.Linq; using ImageSharp.Formats; + using ImageSharp.Formats.Jpeg.PdfJsPort; using ImageSharp.PixelFormats; using ImageSharp.Tests.TestUtilities.ImageComparison; using Xunit; + using Xunit.Abstractions; public class JpegDecoderTests { @@ -29,7 +32,53 @@ namespace ImageSharp.Tests // TODO: We should make this comparer less tolerant ... private static readonly ImageComparer VeryTolerantJpegComparer = ImageComparer.Tolerant(0.005f, pixelThresholdInPixelByteSum: 4); - + + public JpegDecoderTests(ITestOutputHelper output) + { + this.Output = output; + } + + private ITestOutputHelper Output { get; } + + private float GetSimilarityPercentage(Image image, TestImageProvider provider) + where TPixel : struct, IPixel + { + var reportingComparer = ImageComparer.Tolerant(0, 0); + + ImageSimilarityReport report = image.GetReferenceOutputSimilarityReports( + provider, + reportingComparer, + appendPixelTypeToFileName: false).SingleOrDefault(); + + if (report != null && report.TotalNormalizedDifference.HasValue) + { + return report.TotalNormalizedDifference.Value * 100; + } + + return 100; + } + + [Theory] + [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32)] + public void CompareJpegDecoders(TestImageProvider provider) + where TPixel : struct, IPixel + { + this.Output.WriteLine(provider.SourceFileOrDescription); + provider.Utility.TestName = nameof(this.DecodeBaselineJpeg); + + using (Image image = provider.GetImage()) + { + double similarity = this.GetSimilarityPercentage(image, provider); + this.Output.WriteLine($"Similarity with ORIGINAL decoder: {similarity:0.0000}%"); + } + + using (Image image = provider.GetImage(new PdfJsJpegDecoder())) + { + double similarity = this.GetSimilarityPercentage(image, provider); + this.Output.WriteLine($"Similarity with PDFJS decoder: {similarity:0.0000}%"); + } + } + [Theory] [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32 | PixelTypes.Rgba32 | PixelTypes.Argb32)] public void DecodeBaselineJpeg(TestImageProvider provider) @@ -41,6 +90,20 @@ namespace ImageSharp.Tests image.CompareToReferenceOutput(provider, VeryTolerantJpegComparer, appendPixelTypeToFileName: false); } } + + [Theory] + [WithFileCollection(nameof(BaselineTestJpegs), PixelTypes.Rgba32 | PixelTypes.Rgba32 | PixelTypes.Argb32)] + public void DecodeBaselineJpeg_PdfJs(TestImageProvider provider) + where TPixel : struct, IPixel + { + using (Image image = provider.GetImage(new PdfJsJpegDecoder())) + { + image.DebugSave(provider); + + provider.Utility.TestName = nameof(this.DecodeBaselineJpeg); + image.CompareToReferenceOutput(provider, VeryTolerantJpegComparer, appendPixelTypeToFileName: false); + } + } [Theory] [WithFileCollection(nameof(ProgressiveTestJpegs), PixelTypes.Rgba32 | PixelTypes.Rgba32 | PixelTypes.Argb32)] diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs index 1cf26e49b..e36d70719 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs @@ -26,6 +26,7 @@ namespace ImageSharp.Tests.TestUtilities.ImageComparison sb.Append($"Report{i}: "); sb.Append(r); sb.Append(Environment.NewLine); + i++; } return sb.ToString(); } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs index 74995537e..313d7ffb8 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs @@ -42,7 +42,7 @@ var sb = new StringBuilder(); if (this.TotalNormalizedDifference.HasValue) { - sb.AppendLine($"Total difference: {this.TotalNormalizedDifference.Value * 100:0.00}%"); + sb.AppendLine($"Total difference: {this.TotalNormalizedDifference.Value * 100:0.0000}%"); } int max = Math.Min(5, this.Differences.Length); diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 35a7d17d4..8d9b60cc0 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -6,6 +6,7 @@ namespace ImageSharp.Tests { using System; + using System.Collections.Generic; using System.IO; using ImageSharp.PixelFormats; @@ -101,20 +102,51 @@ namespace ImageSharp.Tests bool grayscale = false, bool appendPixelTypeToFileName = true) where TPixel : struct, IPixel + { + using (Image referenceImage = GetReferenceOutputImage( + provider, + testOutputDetails, + extension, + appendPixelTypeToFileName)) + { + comparer.VerifySimilarity(referenceImage, image); + } + + return image; + } + + public static Image GetReferenceOutputImage(this ITestImageProvider provider, + object testOutputDetails = null, + string extension = "png", + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel { string referenceOutputFile = provider.Utility.GetReferenceOutputFileName(extension, testOutputDetails, appendPixelTypeToFileName); - + if (!File.Exists(referenceOutputFile)) { throw new Exception("Reference output file missing: " + referenceOutputFile); } - - using (var referenceImage = Image.Load(referenceOutputFile/*, ReferenceDecoder.Instance*/)) + + return Image.Load(referenceOutputFile); + } + + public static IEnumerable GetReferenceOutputSimilarityReports( + this Image image, + ITestImageProvider provider, + ImageComparer comparer, + object testOutputDetails = null, + string extension = "png", + bool appendPixelTypeToFileName = true) + where TPixel : struct, IPixel + { + using (Image referenceImage = provider.GetReferenceOutputImage( + testOutputDetails, + extension, + appendPixelTypeToFileName)) { - comparer.VerifySimilarity(referenceImage, image); + return comparer.CompareImages(referenceImage, image); } - - return image; } public static Image CompareToOriginal(