Browse Source

comparing decoders

af/merge-core
Anton Firszov 9 years ago
parent
commit
0d2a58747a
  1. 65
      tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
  2. 1
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/Exceptions/ImagePixelsAreDifferentException.cs
  3. 2
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageSimilarityReport.cs
  4. 44
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

65
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<TPixel>(Image<TPixel> image, TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
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<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
this.Output.WriteLine(provider.SourceFileOrDescription);
provider.Utility.TestName = nameof(this.DecodeBaselineJpeg);
using (Image<TPixel> image = provider.GetImage())
{
double similarity = this.GetSimilarityPercentage(image, provider);
this.Output.WriteLine($"Similarity with ORIGINAL decoder: {similarity:0.0000}%");
}
using (Image<TPixel> 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<TPixel>(TestImageProvider<TPixel> 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<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> 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)]

1
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();
}

2
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);

44
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<TPixel>
{
using (Image<TPixel> referenceImage = GetReferenceOutputImage<TPixel>(
provider,
testOutputDetails,
extension,
appendPixelTypeToFileName))
{
comparer.VerifySimilarity(referenceImage, image);
}
return image;
}
public static Image<TPixel> GetReferenceOutputImage<TPixel>(this ITestImageProvider provider,
object testOutputDetails = null,
string extension = "png",
bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel>
{
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<Rgba32>(referenceOutputFile/*, ReferenceDecoder.Instance*/))
return Image.Load<TPixel>(referenceOutputFile);
}
public static IEnumerable<ImageSimilarityReport> GetReferenceOutputSimilarityReports<TPixel>(
this Image<TPixel> image,
ITestImageProvider provider,
ImageComparer comparer,
object testOutputDetails = null,
string extension = "png",
bool appendPixelTypeToFileName = true)
where TPixel : struct, IPixel<TPixel>
{
using (Image<TPixel> referenceImage = provider.GetReferenceOutputImage<TPixel>(
testOutputDetails,
extension,
appendPixelTypeToFileName))
{
comparer.VerifySimilarity(referenceImage, image);
return comparer.CompareImages(referenceImage, image);
}
return image;
}
public static Image<TPixel> CompareToOriginal<TPixel>(

Loading…
Cancel
Save