Browse Source

replaced old comparer everywhere

pull/298/head
Anton Firszov 9 years ago
parent
commit
f9cd6b6364
  1. 3
      tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs
  2. 2
      tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryThresholdTest.cs
  3. 4
      tests/ImageSharp.Tests/Processing/Processors/Binarization/DitherTest.cs
  4. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/BlackWhiteTest.cs
  5. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/ColorBlindnessTest.cs
  6. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/GrayscaleTest.cs
  7. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/HueTest.cs
  8. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/KodachromeTest.cs
  9. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/LomographTest.cs
  10. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/PolaroidTest.cs
  11. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/SaturationTest.cs
  12. 2
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/SepiaTest.cs
  13. 2
      tests/ImageSharp.Tests/Processing/Processors/Convolution/BoxBlurTest.cs
  14. 2
      tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs
  15. 2
      tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs
  16. 2
      tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs
  17. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/AlphaTest.cs
  18. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/BackgroundColorTest.cs
  19. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/BrightnessTest.cs
  20. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/ContrastTest.cs
  21. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/InvertTest.cs
  22. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/OilPaintTest.cs
  23. 2
      tests/ImageSharp.Tests/Processing/Processors/Effects/PixelateTest.cs
  24. 2
      tests/ImageSharp.Tests/Processing/Processors/Overlays/GlowTest.cs
  25. 2
      tests/ImageSharp.Tests/Processing/Processors/Overlays/VignetteTest.cs
  26. 29
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs
  27. 224
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/PercentageImageComparer_Old.cs
  28. 6
      tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs

3
tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs

@ -37,7 +37,6 @@ namespace ImageSharp.Tests.Formats.Png
{
ImageComparer.Tolerant().VerifySimilarity(image, img2);
// img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder());
PercentageImageComparer_Old.VerifySimilarity(image, img2);
}
}
}
@ -123,7 +122,7 @@ namespace ImageSharp.Tests.Formats.Png
ms.Position = 0;
using (Image<Rgba32> img2 = Image.Load<Rgba32>(ms, new PngDecoder()))
{
PercentageImageComparer_Old.VerifySimilarity(image, img2);
ImageComparer.Tolerant().VerifySimilarity(image, img2);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Binarization/BinaryThresholdTest.cs

@ -46,7 +46,7 @@ namespace ImageSharp.Tests.Processing.Processors.Binarization
image.Mutate(x => x.BinaryThreshold(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

4
tests/ImageSharp.Tests/Processing/Processors/Binarization/DitherTest.cs

@ -58,7 +58,7 @@ namespace ImageSharp.Tests.Processing.Processors.Binarization
image.Mutate(x => x.Dither(ditherer, bounds));
image.DebugSave(provider, name);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
@ -87,7 +87,7 @@ namespace ImageSharp.Tests.Processing.Processors.Binarization
image.Mutate(x => x.Dither(diffuser, .5F, bounds));
image.DebugSave(provider, name);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/BlackWhiteTest.cs

@ -38,7 +38,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.BlackWhite(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/ColorBlindnessTest.cs

@ -52,7 +52,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.ColorBlindness(colorBlindness, bounds));
image.DebugSave(provider, colorBlindness.ToString());
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/GrayscaleTest.cs

@ -56,7 +56,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Grayscale(value, bounds));
image.DebugSave(provider, value.ToString());
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/HueTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Hue(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/KodachromeTest.cs

@ -38,7 +38,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Kodachrome(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/LomographTest.cs

@ -40,7 +40,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Lomograph(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/PolaroidTest.cs

@ -38,7 +38,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Polaroid(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/SaturationTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Saturation(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/SepiaTest.cs

@ -38,7 +38,7 @@ namespace ImageSharp.Tests.Processing.Processors.ColorMatrix
image.Mutate(x => x.Sepia(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Convolution/BoxBlurTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
image.Mutate(x => x.BoxBlur(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs

@ -58,7 +58,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
image.DebugSave(provider, grayscale: true);
// TODO: We don't need this any longer after switching to ReferenceImages
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianBlurTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
image.Mutate(x => x.GaussianBlur(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Convolution/GaussianSharpenTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Convolution
image.Mutate(x => x.GaussianSharpen(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/AlphaTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
image.Mutate(x => x.Alpha(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/BackgroundColorTest.cs

@ -38,7 +38,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
image.Mutate(x => x.BackgroundColor(NamedColors<TPixel>.HotPink, bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/BrightnessTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
image.Mutate(x => x.Brightness(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds); ;
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/ContrastTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
image.Mutate(x => x.Contrast(value, bounds));
image.DebugSave(provider, value);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/InvertTest.cs

@ -38,7 +38,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
image.Mutate(x => x.Invert(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/OilPaintTest.cs

@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
image.Mutate(x => x.OilPaint(levels, brushSize, bounds));
image.DebugSave(provider, string.Join("-", levels, brushSize));
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds, 0.001F);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Effects/PixelateTest.cs

@ -79,7 +79,7 @@ namespace ImageSharp.Tests.Processing.Processors.Effects
}
}
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Overlays/GlowTest.cs

@ -62,7 +62,7 @@ namespace ImageSharp.Tests.Processing.Processors.Overlays
image.Mutate(x => x.Glow(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

2
tests/ImageSharp.Tests/Processing/Processors/Overlays/VignetteTest.cs

@ -62,7 +62,7 @@ namespace ImageSharp.Tests.Processing.Processors.Overlays
image.Mutate(x => x.Vignette(bounds));
image.DebugSave(provider);
PercentageImageComparer_Old.EnsureProcessorChangesAreConstrained(source, image, bounds);
ImageComparer.Tolerant().EnsureProcessorChangesAreConstrained(source, image, bounds);
}
}
}

29
tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparer.cs

@ -13,10 +13,10 @@ namespace ImageSharp.Tests.TestUtilities.ImageComparison
public static ImageComparer Exact { get; } = ExactImageComparer.Instance;
public static ImageComparer Tolerant(
float imageThresholdInPercents = 0.01f,
float imageThreshold = TolerantImageComparer.DefaultImageThreshold,
int pixelThresholdInPixelByteSum = 0)
{
return new TolerantImageComparer(imageThresholdInPercents, pixelThresholdInPixelByteSum);
return new TolerantImageComparer(imageThreshold, pixelThresholdInPixelByteSum);
}
public abstract ImageSimilarityReport CompareImagesOrFrames<TPixelA, TPixelB>(
@ -78,5 +78,30 @@ namespace ImageSharp.Tests.TestUtilities.ImageComparison
throw new ImagePixelsAreDifferentException(reports);
}
}
/// <summary>
/// Fills the bounded area with a solid color and does a visual comparison between 2 images asserting the difference outwith
/// that area is less then a configurable threshold.
/// </summary>
/// <typeparam name="TPixelA">The color of the expected image</typeparam>
/// <typeparam name="TPixelB">The color type fo the the actual image</typeparam>
/// <param name="comparer">The <see cref="ImageComparer"/> to use</param>
/// <param name="expected">The expected image</param>
/// <param name="actual">The actual image</param>
/// <param name="bounds">The bounds within the image has been altered</param>
public static void EnsureProcessorChangesAreConstrained<TPixelA, TPixelB>(
this ImageComparer comparer,
Image<TPixelA> expected,
Image<TPixelB> actual,
Rectangle bounds)
where TPixelA : struct, IPixel<TPixelA>
where TPixelB : struct, IPixel<TPixelB>
{
// Draw identical shapes over the bounded and compare to ensure changes are constrained.
expected.Mutate(x => x.Fill(NamedColors<TPixelA>.HotPink, bounds));
actual.Mutate(x => x.Fill(NamedColors<TPixelB>.HotPink, bounds));
comparer.VerifySimilarity(expected, actual);
}
}
}

224
tests/ImageSharp.Tests/TestUtilities/ImageComparison/PercentageImageComparer_Old.cs

@ -1,224 +0,0 @@
// <copyright file="ImageComparer.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Tests.TestUtilities.ImageComparison
{
using System;
using ImageSharp.Memory;
using ImageSharp.PixelFormats;
using SixLabors.Primitives;
/// <summary>
/// Class to perform simple image comparisons.
/// </summary>
public class PercentageImageComparer_Old : ImageComparer
{
public float ImageThreshold { get; }
public byte SegmentThreshold { get; }
public int ScaleIntoSize { get; }
public PercentageImageComparer_Old(
float imageThreshold = DefaultImageThreshold,
byte segmentThreshold = DefaultSegmentThreshold,
int scaleIntoSize = DefaultScaleIntoSize)
{
this.ImageThreshold = imageThreshold;
this.SegmentThreshold = segmentThreshold;
this.ScaleIntoSize = scaleIntoSize;
}
/// <summary>
/// This is means the images get scaled into a 32x32 image to sample pixels
/// </summary>
public const int DefaultScaleIntoSize = 32;
/// <summary>
/// The greyscale difference between 2 segements my be > 3 before it influences the overall difference
/// </summary>
public const int DefaultSegmentThreshold = 3;
/// <summary>
/// After segment thresholds the images must have no differences
/// </summary>
public const float DefaultImageThreshold = 0.000F;
/// <summary>
/// Fills the bounded area with a solid color and does a visual comparison between 2 images asserting the difference outwith
/// that area is less then a configurable threshold.
/// </summary>
/// <typeparam name="TPixelA">The color of the expected image</typeparam>
/// <typeparam name="TPixelB">The color type fo the the actual image</typeparam>
/// <param name="expected">The expected image</param>
/// <param name="actual">The actual image</param>
/// <param name="bounds">The bounds within the image has been altered</param>
/// <param name="imageTheshold">
/// The threshold for the percentage difference where the images are asumed to be the same.
/// The default/undefined value is <see cref="DefaultImageThreshold"/>
/// </param>
/// <param name="segmentThreshold">
/// The threshold of the individual segments before it acumulates towards the overall difference.
/// The default undefined value is <see cref="DefaultSegmentThreshold"/>
/// </param>
/// <param name="scaleIntoSize">
/// This is a sampling factor we sample a grid of average pixels <paramref name="scaleIntoSize"/> width by <paramref name="scaleIntoSize"/> high
/// The default undefined value is <see cref="DefaultScaleIntoSize"/>
/// </param>
public static void EnsureProcessorChangesAreConstrained<TPixelA, TPixelB>(
Image<TPixelA> expected,
Image<TPixelB> actual,
Rectangle bounds,
float imageTheshold = DefaultImageThreshold,
byte segmentThreshold = DefaultSegmentThreshold,
int scaleIntoSize = DefaultScaleIntoSize)
where TPixelA : struct, IPixel<TPixelA>
where TPixelB : struct, IPixel<TPixelB>
{
// Draw identical shapes over the bounded and compare to ensure changes are constrained.
expected.Mutate(x => x.Fill(NamedColors<TPixelA>.HotPink, bounds));
actual.Mutate(x => x.Fill(NamedColors<TPixelB>.HotPink, bounds));
VerifySimilarity(expected, actual, imageTheshold, segmentThreshold, scaleIntoSize);
}
/// <summary>
/// Does a visual comparison between 2 images and then asserts the difference is less then a configurable threshold
/// </summary>
/// <typeparam name="TPixelA">The color of the expected image</typeparam>
/// <typeparam name="TPixelB">The color type fo the the actual image</typeparam>
/// <param name="expected">The expected image</param>
/// <param name="actual">The actual image</param>
/// <param name="imageTheshold">
/// The threshold for the percentage difference where the images are asumed to be the same.
/// The default/undefined value is <see cref="DefaultImageThreshold"/>
/// </param>
/// <param name="segmentThreshold">
/// The threshold of the individual segments before it acumulates towards the overall difference.
/// The default undefined value is <see cref="DefaultSegmentThreshold"/>
/// </param>
/// <param name="scaleIntoSize">
/// This is a sampling factor we sample a grid of average pixels <paramref name="scaleIntoSize"/> width by <paramref name="scaleIntoSize"/> high
/// The default undefined value is <see cref="DefaultScaleIntoSize"/>
/// </param>
public static void VerifySimilarity<TPixelA, TPixelB>(
Image<TPixelA> expected,
Image<TPixelB> actual,
float imageTheshold = DefaultImageThreshold,
byte segmentThreshold = DefaultSegmentThreshold,
int scaleIntoSize = DefaultScaleIntoSize)
where TPixelA : struct, IPixel<TPixelA> where TPixelB : struct, IPixel<TPixelB>
{
var comparer = new PercentageImageComparer_Old(imageTheshold, segmentThreshold, scaleIntoSize);
comparer.CompareImages(expected, actual);
}
/// <summary>
/// Does a visual comparison between 2 images and then and returns the percentage diffence between the 2
/// </summary>
/// <typeparam name="TPixelA">The color of the source image</typeparam>
/// <typeparam name="TPixelB">The color type for the target image</typeparam>
/// <param name="source">The source image</param>
/// <param name="target">The target image</param>
/// <param name="segmentThreshold">
/// The threshold of the individual segments before it acumulates towards the overall difference.
/// The default undefined value is <see cref="DefaultSegmentThreshold"/>
/// </param>
/// <param name="scalingFactor">
/// This is a sampling factor we sample a grid of average pixels <paramref name="scalingFactor"/> width by <paramref name="scalingFactor"/> high
/// The default undefined value is <see cref="DefaultScaleIntoSize"/>
/// </param>
/// <returns>Returns a number from 0 - 1 which represents the difference focter between the images.</returns>
public static float PercentageDifference<TPixelA, TPixelB>(
Image<TPixelA> source,
Image<TPixelB> target,
byte segmentThreshold = DefaultSegmentThreshold,
int scalingFactor = DefaultScaleIntoSize)
where TPixelA : struct, IPixel<TPixelA>
where TPixelB : struct, IPixel<TPixelB>
{
// code adapted from https://www.codeproject.com/Articles/374386/Simple-image-comparison-in-NET
Fast2DArray<byte> differences = GetDifferences(source, target, scalingFactor);
int diffPixels = 0;
foreach (byte b in differences.Data)
{
if (b > segmentThreshold) { diffPixels++; }
}
return diffPixels / (float)(scalingFactor * scalingFactor);
}
private static Fast2DArray<byte> GetDifferences<TPixelA, TPixelB>(Image<TPixelA> source, Image<TPixelB> target, int scalingFactor)
where TPixelA : struct, IPixel<TPixelA>
where TPixelB : struct, IPixel<TPixelB>
{
var differences = new Fast2DArray<byte>(scalingFactor, scalingFactor);
Fast2DArray<byte> firstGray = GetGrayScaleValues(source, scalingFactor);
Fast2DArray<byte> secondGray = GetGrayScaleValues(target, scalingFactor);
for (int y = 0; y < scalingFactor; y++)
{
for (int x = 0; x < scalingFactor; x++)
{
int diff = firstGray[x, y] - secondGray[x, y];
differences[x, y] = (byte)Math.Abs(diff);
}
}
return differences;
}
private static Fast2DArray<byte> GetGrayScaleValues<TPixelA>(Image<TPixelA> source, int scalingFactor)
where TPixelA : struct, IPixel<TPixelA>
{
byte[] buffer = new byte[3];
using (Image<TPixelA> img = source.Clone(x => x.Resize(scalingFactor, scalingFactor).Grayscale()))
{
using (PixelAccessor<TPixelA> pixels = img.Lock())
{
var grayScale = new Fast2DArray<byte>(scalingFactor, scalingFactor);
for (int y = 0; y < scalingFactor; y++)
{
for (int x = 0; x < scalingFactor; x++)
{
pixels[x, y].ToXyzBytes(buffer, 0);
grayScale[x, y] = buffer[0];
}
}
return grayScale;
}
}
}
public override ImageSimilarityReport CompareImagesOrFrames<TPixelA, TPixelB>(ImageBase<TPixelA> expected, ImageBase<TPixelB> actual)
{
throw new NotImplementedException();
}
public void CompareImages<TPixelA, TPixelB>(Image<TPixelA> expected, Image<TPixelB> actual)
where TPixelA : struct, IPixel<TPixelA>
where TPixelB : struct, IPixel<TPixelB>
{
if (expected.Size() != actual.Size())
{
throw new ImageDimensionsMismatchException(expected.Size(), actual.Size());
}
float percentage = PercentageDifference(expected, actual, this.SegmentThreshold, this.ScaleIntoSize);
if (percentage > this.ImageThreshold)
{
throw new ImagesSimilarityException(
$"The percentage difference of images {percentage} is larger than expected maximum {this.ImageThreshold}!");
}
}
}
}

6
tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs

@ -51,7 +51,7 @@
Rgba32[] aBuffer = new Rgba32[width];
Rgba32[] bBuffer = new Rgba32[width];
double totalDifference = 0.0;
float totalDifference = 0.0f;
var differences = new List<PixelDifference>();
@ -78,7 +78,9 @@
}
}
if (totalDifference > this.ImageThreshold)
float normalizedDifference = totalDifference / ((float)actual.Width * (float)actual.Height);
if (normalizedDifference > this.ImageThreshold)
{
return new ImageSimilarityReport(expected, actual, differences);
}

Loading…
Cancel
Save