using System.Text;
using Titano.Testing;
namespace Titano.Core.Tests.Codecs;
/// Shared fixtures, synthetic images and image metrics for codec tests.
internal static class CodecTestData
{
public static byte[] Fixture(string relativePath) =>
File.ReadAllBytes(Path.Combine(TestContext.FixturesDirectory, "codecs", relativePath));
/// Netpbm P5/P6/P7 (PAM) reader for reference images; samples are big-endian when maxval > 255.
public static Pnm ReadPnm(byte[] data)
{
var pos = 0;
string Token()
{
while (pos < data.Length && (char.IsWhiteSpace((char)data[pos]) || data[pos] == '#'))
{
if (data[pos] == '#') while (pos < data.Length && data[pos] != '\n') pos++;
else pos++;
}
var start = pos;
while (pos < data.Length && !char.IsWhiteSpace((char)data[pos])) pos++;
return Encoding.ASCII.GetString(data, start, pos - start);
}
var magic = Token();
int width, height, channels, maxVal;
if (magic == "P7")
{
width = height = channels = maxVal = 0;
for (var t = Token(); t != "ENDHDR"; t = Token())
{
switch (t)
{
case "WIDTH": width = int.Parse(Token()); break;
case "HEIGHT": height = int.Parse(Token()); break;
case "DEPTH": channels = int.Parse(Token()); break;
case "MAXVAL": maxVal = int.Parse(Token()); break;
case "TUPLTYPE": Token(); break;
}
}
}
else
{
channels = magic switch { "P5" => 1, "P6" => 3, _ => throw new InvalidDataException(magic) };
width = int.Parse(Token());
height = int.Parse(Token());
maxVal = int.Parse(Token());
}
pos++;
var samples = new int[width * height * channels];
for (var i = 0; i < samples.Length; i++)
{
samples[i] = maxVal > 255 ? data[pos + 2 * i] << 8 | data[pos + 2 * i + 1] : data[pos + i];
}
return new Pnm(width, height, channels, maxVal, samples);
}
public sealed record Pnm(int Width, int Height, int Channels, int MaxVal, int[] Samples)
{
public byte[] Bytes => Samples.Select(s => (byte)s).ToArray();
public ushort[] Words => Samples.Select(s => (ushort)s).ToArray();
}
/// Deterministic RGB test card: smooth gradients, saturated colour bars, seeded noise and
/// text-like one-pixel strokes (the four content classes codecs struggle with).
public static byte[] TestCard(int width, int height, int seed = 1)
{
var rng = new Random(seed);
var rgb = new byte[width * height * 3];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var band = y * 4 / height;
int r, g, b;
switch (band)
{
case 0:
r = x * 255 / Math.Max(1, width - 1);
g = y * 4 * 255 / Math.Max(1, height - 1) / 4 * 4 % 256;
b = 255 - r;
break;
case 1:
var bar = x * 8 / width;
r = (bar & 4) != 0 ? 230 : 25;
g = (bar & 2) != 0 ? 230 : 25;
b = (bar & 1) != 0 ? 230 : 25;
break;
case 2:
r = rng.Next(256);
g = rng.Next(256);
b = rng.Next(256);
break;
default:
var stroke = x % 7 == 0 || y % 9 == 0 || (x + y) % 13 == 0;
r = g = b = stroke ? 20 : 235;
break;
}
var i = (y * width + x) * 3;
rgb[i] = (byte)r;
rgb[i + 1] = (byte)g;
rgb[i + 2] = (byte)b;
}
}
return rgb;
}
/// Smooth photographic-like content (low-frequency gradients); compresses like real photos.
public static byte[] SmoothImage(int width, int height)
{
var rgb = new byte[width * height * 3];
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
double u = x / (double)width, v = y / (double)height;
var i = (y * width + x) * 3;
rgb[i] = (byte)(127.5 + 120 * Math.Sin(6.1 * u + 2.3 * v));
rgb[i + 1] = (byte)(127.5 + 120 * Math.Cos(4.7 * u * v + 3.1 * v));
rgb[i + 2] = (byte)(127.5 + 120 * Math.Sin(9.3 * v - 1.7 * u + 0.5));
}
}
return rgb;
}
public static double Psnr(ReadOnlySpan a, ReadOnlySpan b) => Psnr(a, b, 255);
public static double Psnr(ReadOnlySpan a, ReadOnlySpan b, double peak) where T : struct, IConvertible
{
Assert.Equal(a.Length, b.Length, "sample count");
double sum = 0;
for (var i = 0; i < a.Length; i++)
{
var d = a[i].ToDouble(null) - b[i].ToDouble(null);
sum += d * d;
}
var mse = sum / a.Length;
return mse == 0 ? 99 : 10 * Math.Log10(peak * peak / mse);
}
public static int MaxAbsDiff(ReadOnlySpan a, ReadOnlySpan b)
{
Assert.Equal(a.Length, b.Length, "sample count");
var max = 0;
for (var i = 0; i < a.Length; i++) max = Math.Max(max, Math.Abs(a[i] - b[i]));
return max;
}
/// Seeded corruption: the input truncated at points plus single-byte flips.
public static IEnumerable Corruptions(byte[] valid, int cases, int seed)
{
var rng = new Random(seed);
for (var i = 0; i < cases; i++)
{
yield return valid.AsSpan(0, rng.Next(valid.Length)).ToArray();
var flipped = (byte[])valid.Clone();
var flips = 1 + rng.Next(4);
for (var f = 0; f < flips; f++) flipped[rng.Next(flipped.Length)] ^= (byte)(1 << rng.Next(8));
yield return flipped;
var garbage = (byte[])valid.Clone();
garbage[rng.Next(garbage.Length)] = (byte)rng.Next(256);
yield return garbage;
}
}
}