Browse Source

implemented ExactImageComparer

af/merge-core
Anton Firszov 9 years ago
parent
commit
c3e2982fbe
  1. 53
      tests/ImageSharp.Tests/ExactImageComparer.cs
  2. 12
      tests/ImageSharp.Tests/ImageComparer.cs
  3. 8
      tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs

53
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<TPixelA, TPixelB>(Image<TPixelA> expected, Image<TPixelB> 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<Point>();
for (int y = 0; y < actual.Height; y++)
{
Span<TPixelA> aSpan = expected.GetRowSpan(y);
Span<TPixelB> bSpan = actual.GetRowSpan(y);
PixelOperations<TPixelA>.Instance.ToRgba32(aSpan, aBuffer, width);
PixelOperations<TPixelB>.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());
}
}
}
}

12
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<TPixelA, TPixelB>(Image<TPixelA> expected, Image<TPixelB> actual)
where TPixelA : struct, IPixel<TPixelA> where TPixelB : struct, IPixel<TPixelB>;
}
public class ExactComparer : ImageComparer
{
public static ExactComparer Instance { get; } = new ExactComparer();
public override void Verify<TPixelA, TPixelB>(Image<TPixelA> expected, Image<TPixelB> actual)
{
throw new NotImplementedException();
}
}
}

8
tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs

@ -122,7 +122,7 @@ namespace ImageSharp.Tests
{
using (Image<TPixel> clone = image.Clone())
{
ExactComparer.Instance.Verify(image, clone);
ExactImageComparer.Instance.Verify(image, clone);
}
}
}
@ -140,7 +140,7 @@ namespace ImageSharp.Tests
ImageDimensionsMismatchException ex = Assert.ThrowsAny<ImageDimensionsMismatchException>(
() =>
{
ExactComparer.Instance.Verify(image, clone);
ExactImageComparer.Instance.Verify(image, clone);
});
this.Output.WriteLine(ex.Message);
}
@ -156,13 +156,13 @@ namespace ImageSharp.Tests
{
using (Image<TPixel> clone = image.Clone())
{
ModifyPixel(clone, 42, 42, 1);
ModifyPixel(clone, 42, 24, 1);
ModifyPixel(clone, 7, 93, 1);
ImagesAreNotEqualException ex = Assert.ThrowsAny<ImagesAreNotEqualException>(
() =>
{
ExactComparer.Instance.Verify(image, clone);
ExactImageComparer.Instance.Verify(image, clone);
});
this.Output.WriteLine(ex.Message);
Assert.Equal(2, ex.Differences.Length);

Loading…
Cancel
Save