diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index a2f7bd8b5..9d4ffbb3a 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Tests // TODO: We should make this comparer less tolerant ... private static readonly ImageComparer VeryTolerantJpegComparer = - ImageComparer.Tolerant(0.005f, pixelThresholdHammingDistance: 4); + ImageComparer.Tolerant(0.005f, perPixelManhattanThreshold: 4); public JpegDecoderTests(ITestOutputHelper output) { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs index 1b6f7a14a..74f46a869 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs @@ -17,9 +17,9 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison public static ImageComparer Tolerant( float imageThreshold = TolerantImageComparer.DefaultImageThreshold, - int pixelThresholdHammingDistance = 0) + int perPixelManhattanThreshold = 0) { - return new TolerantImageComparer(imageThreshold, pixelThresholdHammingDistance); + return new TolerantImageComparer(imageThreshold, perPixelManhattanThreshold); } public abstract ImageSimilarityReport CompareImagesOrFrames( diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs index c74de50ce..48335945c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs @@ -12,10 +12,10 @@ { public const float DefaultImageThreshold = 1.0f / (100 * 100 * 255); - public TolerantImageComparer(float imageThreshold, int pixelThresholdHammingDistance = 0) + public TolerantImageComparer(float imageThreshold, int perPixelManhattanThreshold = 0) { this.ImageThreshold = imageThreshold; - this.PixelThresholdHammingDistance = pixelThresholdHammingDistance; + this.PerPixelManhattanThreshold = perPixelManhattanThreshold; } /// @@ -26,7 +26,10 @@ /// 3. PixelA = (255,255,255,0) PixelB =(128,128,128,128) leads to 50% difference on a single pixel /// /// The total differences is the sum of all pixel differences normalized by image dimensions! - /// + /// The individual distances are calculated using the Manhattan function: + /// + /// https://en.wikipedia.org/wiki/Taxicab_geometry + /// /// ImageThresholdInPercents = 1.0/255 means that we allow one byte difference per channel on a 1x1 image /// ImageThresholdInPercents = 1.0/(100*100*255) means that we allow only one byte difference per channel on a 100x100 image /// @@ -34,9 +37,12 @@ /// /// The threshold of the individual pixels before they acumulate towards the overall difference. - /// For an individual pixel the value it's calculated as: pixel.R + pixel.G + pixel.B + pixel.A + /// For an individual pixel pair the value is the Manhattan distance of pixels: + /// + /// https://en.wikipedia.org/wiki/Taxicab_geometry + /// /// - public int PixelThresholdHammingDistance { get; } + public int PerPixelManhattanThreshold { get; } public override ImageSimilarityReport CompareImagesOrFrames(ImageBase expected, ImageBase actual) { @@ -66,9 +72,9 @@ for (int x = 0; x < width; x++) { - int d = GetHammingDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); + int d = GetManhattanDistanceInRgbaSpace(ref aBuffer[x], ref bBuffer[x]); - if (d > this.PixelThresholdHammingDistance) + if (d > this.PerPixelManhattanThreshold) { var diff = new PixelDifference(new Point(x, y), aBuffer[x], bBuffer[x]); differences.Add(diff); @@ -92,7 +98,7 @@ } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int GetHammingDistanceInRgbaSpace(ref Rgba32 a, ref Rgba32 b) + private static int GetManhattanDistanceInRgbaSpace(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/Tests/ImageComparerTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs index 45e20f3fb..29acabdc4 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/ImageComparerTests.cs @@ -96,7 +96,7 @@ namespace SixLabors.ImageSharp.Tests ImagingTestCaseUtility.ModifyPixel(clone, 1, 0, 10); ImagingTestCaseUtility.ModifyPixel(clone, 2, 0, 10); - var comparer = ImageComparer.Tolerant(pixelThresholdHammingDistance: 42); + var comparer = ImageComparer.Tolerant(perPixelManhattanThreshold: 42); comparer.VerifySimilarity(image, clone); } }