📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

54 lines
1.8 KiB

namespace ImageSharp.Tests.TestUtilities.ImageComparison
{
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 ImageSimilarityReport CompareImagesOrFrames<TPixelA, TPixelB>(ImageBase<TPixelA> expected, ImageBase<TPixelB> actual)
{
if (expected.Size() != actual.Size())
{
throw new InvalidOperationException("Calling ImageComparer is invalid when dimensions mismatch!");
}
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<PixelDifference>();
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++)
{
Rgba32 aPixel = aBuffer[x];
Rgba32 bPixel = bBuffer[x];
if (aPixel != bPixel)
{
var diff = new PixelDifference(new Point(x, y), aPixel, bPixel);
differences.Add(diff);
}
}
}
return new ImageSimilarityReport(expected, actual, differences);
}
}
}