using Febe.Core.Codecs.Images;
namespace Febe.Tests.Imaging;
/// Comparisons of decoded images that say where they differ.
internal static class ImageAssert
{
/// Same size and the same bytes in every pixel.
public static void AreEqual(RgbaImage expected, RgbaImage actual, string what)
{
Assert.AreEqual((expected.Width, expected.Height), (actual.Width, actual.Height), $"{what}: misura diversa.");
AreEqual(expected.Width, expected.Pixels, actual.Pixels, what);
}
/// The same RGBA bytes, for images of the given width.
public static void AreEqual(int width, ReadOnlySpan expected, ReadOnlySpan actual, string what)
{
Assert.AreEqual(expected.Length, actual.Length, $"{what}: lunghezza diversa.");
var first = -1;
var different = 0;
for (var i = 0; i < expected.Length; i += 4)
{
if (!expected.Slice(i, 4).SequenceEqual(actual.Slice(i, 4)))
{
different++;
if (first < 0)
{
first = i;
}
}
}
if (different > 0)
{
var pixel = first / 4;
Assert.Fail(
$"{what}: {different} pixel diversi; il primo in ({pixel % width}, {pixel / width}): atteso "
+ $"{Format(expected.Slice(first, 4))}, letto {Format(actual.Slice(first, 4))}.");
}
}
private static string Format(ReadOnlySpan rgba) => $"({rgba[0]}, {rgba[1]}, {rgba[2]}, {rgba[3]})";
}