using System.Globalization;
using Febe.Core.Codecs.Images;
namespace Febe.Tests.Imaging;
/// How close a lossy decode is to its reference: PSNR per channel and the share of pixels far off.
/// PSNR of the red channel, dB (infinity when identical).
/// PSNR of the green channel, dB.
/// PSNR of the blue channel, dB.
/// PSNR of the alpha channel, dB.
/// Share (0-1) of pixels with some channel off by more than .
/// Largest difference of any channel.
internal sealed record ImageQualityResult(double PsnrRed, double PsnrGreen, double PsnrBlue, double PsnrAlpha, double FarOff, int MaxDifference)
{
/// The smallest PSNR of the colour channels.
public double MinPsnr => Math.Min(PsnrRed, Math.Min(PsnrGreen, PsnrBlue));
public override string ToString() => string.Create(
CultureInfo.InvariantCulture,
$"PSNR R {PsnrRed:F2} G {PsnrGreen:F2} B {PsnrBlue:F2} A {PsnrAlpha:F2} dB, oltre {ImageQuality.FarThreshold}: {FarOff * 100:F3}%, scarto massimo {MaxDifference}");
}
/// Measures and orientation helpers for comparing decoded images.
internal static class ImageQuality
{
/// A pixel counts as far off when one of its channels differs by more than this.
public const int FarThreshold = 16;
/// Lossy decodes must reach this PSNR on every colour channel (SPECIFICA ยง14.1, proposal of C1).
public const double MinimumPsnr = 40;
/// Lossy decodes may have at most this share of pixels far off.
public const double MaximumFarOff = 0.001;
public static ImageQualityResult Compare(RgbaImage expected, RgbaImage actual)
{
Assert.AreEqual((expected.Width, expected.Height), (actual.Width, actual.Height), "misura diversa");
var e = expected.Pixels;
var a = actual.Pixels;
Span squares = stackalloc double[4];
var far = 0;
var max = 0;
for (var i = 0; i < e.Length; i += 4)
{
var isFar = false;
for (var ch = 0; ch < 4; ch++)
{
var d = Math.Abs(e[i + ch] - a[i + ch]);
squares[ch] += d * d;
max = Math.Max(max, d);
isFar |= d > FarThreshold;
}
if (isFar)
{
far++;
}
}
var pixels = e.Length / 4;
return new ImageQualityResult(Psnr(squares[0], pixels), Psnr(squares[1], pixels), Psnr(squares[2], pixels), Psnr(squares[3], pixels), (double)far / pixels, max);
}
/// Asserts the thresholds of a lossy decode.
public static void AssertClose(ImageQualityResult result, string what)
{
Assert.IsGreaterThanOrEqualTo(MinimumPsnr, result.MinPsnr, $"{what}: {result}");
Assert.IsLessThanOrEqualTo(MaximumFarOff, result.FarOff, $"{what}: {result}");
}
private static double Psnr(double squares, int pixels) =>
squares == 0 ? double.PositiveInfinity : 10 * Math.Log10(255.0 * 255.0 / (squares / pixels));
///
/// The image as it should be shown for EXIF orientation (1-8): mirrored, rotated
/// or transposed from the stored pixels.
///
public static RgbaImage Orient(RgbaImage stored, int orientation)
{
var w = stored.Width;
var h = stored.Height;
var swap = orientation >= 5;
var result = new RgbaImage(swap ? h : w, swap ? w : h);
for (var dy = 0; dy < result.Height; dy++)
{
var row = result.GetRow(dy);
for (var dx = 0; dx < result.Width; dx++)
{
var (sx, sy) = orientation switch
{
2 => (w - 1 - dx, dy),
3 => (w - 1 - dx, h - 1 - dy),
4 => (dx, h - 1 - dy),
5 => (dy, dx),
6 => (dy, h - 1 - dx),
7 => (w - 1 - dy, h - 1 - dx),
8 => (w - 1 - dy, dx),
_ => (dx, dy),
};
stored.GetRow(sy).Slice(sx * 4, 4).CopyTo(row[(dx * 4)..]);
}
}
return result;
}
}