namespace Febe.Tests.Imaging; /// The format of a reference case's input file, from its extension. public enum ReferenceFormat { Png, Jpeg, WebP, Gif, Bmp, } /// /// One case of the reference set in Imaging/Riferimenti (see LEGGIMI.md there): an input file and, beside it, /// «.atteso.png», SkiaSharp's straight (unpremultiplied) RGBA8 decode of it, upright. /// /// The folder of the set. /// The case name, as elenco.txt lists it. /// The format of the input file. /// The input file name, inside . /// Width of the expected image (after the EXIF orientation, for a JPEG that has one). /// Height of the expected image (after the EXIF orientation, for a JPEG that has one). public sealed record ReferenceCase(string Folder, string Name, ReferenceFormat Format, string InputFileName, int Width, int Height) { /// Full path of the input file. public string InputPath => Path.Combine(Folder, InputFileName); /// Full path of the expected image («.atteso.png»). public string ExpectedPath => Path.Combine(Folder, Name + ReferenceImages.ExpectedSuffix); /// /// The EXIF orientation (1-8) the input carries by construction: only the «jpeg-orientamento-N» cases have one. /// The integrity test checks it against the file. /// public int Orientation => Name.StartsWith(ReferenceImages.OrientationPrefix, StringComparison.Ordinal) ? int.Parse(Name.AsSpan(ReferenceImages.OrientationPrefix.Length), System.Globalization.CultureInfo.InvariantCulture) : 1; /// Width of the image as stored in the input file (orientations 5-8 swap the two sides). public int StoredWidth => Orientation >= 5 ? Height : Width; /// Height of the image as stored in the input file (orientations 5-8 swap the two sides). public int StoredHeight => Orientation >= 5 ? Width : Height; /// The bytes of the input file. public byte[] ReadInput() => File.ReadAllBytes(InputPath); /// /// The bytes of the expected image: an 8-bit RGBA PNG (colour type 6), one filter-0 row after the other in a single /// zlib stream. From step 2 of track C1 Febe's own PNG decoder reads it; SkiaSharp is no longer needed for that. /// public byte[] ReadExpectedPng() => File.ReadAllBytes(ExpectedPath); /// public override string ToString() => Name; } /// Loads the reference set of the in-house image codecs from the test output folder. public static class ReferenceImages { /// Suffix of the expected images. public const string ExpectedSuffix = ".atteso.png"; /// The list of the cases, one «name WxH» per line. public const string ListFileName = "elenco.txt"; /// The description of the set. public const string ReadmeFileName = "LEGGIMI.md"; /// Name prefix of the JPEG cases with an EXIF orientation. public const string OrientationPrefix = "jpeg-orientamento-"; private static readonly (string Extension, ReferenceFormat Format)[] InputExtensions = [ (".png", ReferenceFormat.Png), (".jpg", ReferenceFormat.Jpeg), (".webp", ReferenceFormat.WebP), (".gif", ReferenceFormat.Gif), (".bmp", ReferenceFormat.Bmp), ]; /// The folder of the set in the test output (copied there by Febe.Tests.csproj). public static string Folder { get; } = Path.Combine(AppContext.BaseDirectory, "Imaging", "Riferimenti"); private static readonly Lazy> Cases = new(() => Load(Folder)); /// Every case, in the order of elenco.txt. public static IReadOnlyList All => Cases.Value; /// The case with that name. public static ReferenceCase Get(string name) { foreach (var reference in All) { if (reference.Name == name) { return reference; } } throw new KeyNotFoundException($"No reference case named '{name}' in {ListFileName}."); } /// The cases whose input has that format. public static IEnumerable Of(ReferenceFormat format) => All.Where(c => c.Format == format); /// /// The case names of a format, for [DynamicData]: the test receives the name and calls , /// so each case shows up as its own row in the results. /// public static IEnumerable NamesOf(ReferenceFormat format) => Of(format).Select(c => new object[] { c.Name }); /// The input formats, with their extensions. public static IReadOnlyList<(string Extension, ReferenceFormat Format)> Extensions => InputExtensions; /// Reads elenco.txt in and finds the input file of every case. public static IReadOnlyList Load(string folder) { var list = new List(); var seen = new HashSet(StringComparer.Ordinal); var lineNumber = 0; foreach (var rawLine in File.ReadLines(Path.Combine(folder, ListFileName))) { lineNumber++; var line = rawLine.Trim(); if (line.Length == 0) { continue; } var parts = line.Split(' '); var size = parts.Length == 2 ? parts[1].Split('x') : []; if (size.Length != 2 || !int.TryParse(size[0], System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out var width) || !int.TryParse(size[1], System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture, out var height) || width <= 0 || height <= 0) { throw new InvalidDataException($"{ListFileName}, line {lineNumber}: expected 'name WxH', found '{line}'."); } var name = parts[0]; if (!seen.Add(name)) { throw new InvalidDataException($"{ListFileName}, line {lineNumber}: '{name}' is listed twice."); } ReferenceCase? found = null; foreach (var (extension, format) in InputExtensions) { if (!File.Exists(Path.Combine(folder, name + extension))) { continue; } if (found is not null) { throw new InvalidDataException($"'{name}' has more than one input file."); } found = new ReferenceCase(folder, name, format, name + extension, width, height); } list.Add(found ?? throw new FileNotFoundException($"'{name}' has no input file in {folder}.")); } return list; } }