SkiaSharp esce da Febe: prima di toglierlo serve un termine di confronto per i lettori PNG, JPEG, WebP, GIF e BMP scritti in casa, e dopo non si potrà più generarlo. In tests/Febe.Tests/Imaging/Riferimenti ci sono 57 casi (ingresso più .atteso.png, la lettura RGBA a 8 bit non premoltiplicata di SkiaSharp 4.153.1), generati una volta sola con il permesso di Alberto da un programma usa e getta fuori dal repository; contenuto solo sintetico. Le immagini attese sono PNG scritti a mano (filtro 0, un solo IDAT), così le leggerà il lettore PNG di casa del passo 2. LEGGIMI.md dice come sono fatte, quali casi sono costruiti a mano dalle specifiche e che cosa manca (JPEG progressivo, intervalli di ripristino, CMYK/Adobe: codificatori di prova nostri dalla T.81). ReferenceImages carica l'elenco; ReferenceSetTests controlla l'integrità dell'insieme (nessun file in più o in meno, firme, misure, orientamento EXIF, CRC dei blocchi PNG, JPEG solo base) senza leggere i pixel. Le estensioni delle immagini sono binarie in .gitattributes; una riga in decisioni.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
171 lines
7.2 KiB
C#
171 lines
7.2 KiB
C#
namespace Febe.Tests.Imaging;
|
|
|
|
/// <summary>The format of a reference case's input file, from its extension.</summary>
|
|
public enum ReferenceFormat
|
|
{
|
|
Png,
|
|
Jpeg,
|
|
WebP,
|
|
Gif,
|
|
Bmp,
|
|
}
|
|
|
|
/// <summary>
|
|
/// One case of the reference set in Imaging/Riferimenti (see LEGGIMI.md there): an input file and, beside it,
|
|
/// «<see cref="Name"/>.atteso.png», SkiaSharp's straight (unpremultiplied) RGBA8 decode of it, upright.
|
|
/// </summary>
|
|
/// <param name="Folder">The folder of the set.</param>
|
|
/// <param name="Name">The case name, as elenco.txt lists it.</param>
|
|
/// <param name="Format">The format of the input file.</param>
|
|
/// <param name="InputFileName">The input file name, inside <paramref name="Folder"/>.</param>
|
|
/// <param name="Width">Width of the expected image (after the EXIF orientation, for a JPEG that has one).</param>
|
|
/// <param name="Height">Height of the expected image (after the EXIF orientation, for a JPEG that has one).</param>
|
|
public sealed record ReferenceCase(string Folder, string Name, ReferenceFormat Format, string InputFileName, int Width, int Height)
|
|
{
|
|
/// <summary>Full path of the input file.</summary>
|
|
public string InputPath => Path.Combine(Folder, InputFileName);
|
|
|
|
/// <summary>Full path of the expected image («.atteso.png»).</summary>
|
|
public string ExpectedPath => Path.Combine(Folder, Name + ReferenceImages.ExpectedSuffix);
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public int Orientation =>
|
|
Name.StartsWith(ReferenceImages.OrientationPrefix, StringComparison.Ordinal)
|
|
? int.Parse(Name.AsSpan(ReferenceImages.OrientationPrefix.Length), System.Globalization.CultureInfo.InvariantCulture)
|
|
: 1;
|
|
|
|
/// <summary>Width of the image as stored in the input file (orientations 5-8 swap the two sides).</summary>
|
|
public int StoredWidth => Orientation >= 5 ? Height : Width;
|
|
|
|
/// <summary>Height of the image as stored in the input file (orientations 5-8 swap the two sides).</summary>
|
|
public int StoredHeight => Orientation >= 5 ? Width : Height;
|
|
|
|
/// <summary>The bytes of the input file.</summary>
|
|
public byte[] ReadInput() => File.ReadAllBytes(InputPath);
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public byte[] ReadExpectedPng() => File.ReadAllBytes(ExpectedPath);
|
|
|
|
/// <inheritdoc/>
|
|
public override string ToString() => Name;
|
|
}
|
|
|
|
/// <summary>Loads the reference set of the in-house image codecs from the test output folder.</summary>
|
|
public static class ReferenceImages
|
|
{
|
|
/// <summary>Suffix of the expected images.</summary>
|
|
public const string ExpectedSuffix = ".atteso.png";
|
|
|
|
/// <summary>The list of the cases, one «name WxH» per line.</summary>
|
|
public const string ListFileName = "elenco.txt";
|
|
|
|
/// <summary>The description of the set.</summary>
|
|
public const string ReadmeFileName = "LEGGIMI.md";
|
|
|
|
/// <summary>Name prefix of the JPEG cases with an EXIF orientation.</summary>
|
|
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),
|
|
];
|
|
|
|
/// <summary>The folder of the set in the test output (copied there by Febe.Tests.csproj).</summary>
|
|
public static string Folder { get; } = Path.Combine(AppContext.BaseDirectory, "Imaging", "Riferimenti");
|
|
|
|
private static readonly Lazy<IReadOnlyList<ReferenceCase>> Cases = new(() => Load(Folder));
|
|
|
|
/// <summary>Every case, in the order of elenco.txt.</summary>
|
|
public static IReadOnlyList<ReferenceCase> All => Cases.Value;
|
|
|
|
/// <summary>The case with that name.</summary>
|
|
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}.");
|
|
}
|
|
|
|
/// <summary>The cases whose input has that format.</summary>
|
|
public static IEnumerable<ReferenceCase> Of(ReferenceFormat format) => All.Where(c => c.Format == format);
|
|
|
|
/// <summary>
|
|
/// The case names of a format, for <c>[DynamicData]</c>: the test receives the name and calls <see cref="Get"/>,
|
|
/// so each case shows up as its own row in the results.
|
|
/// </summary>
|
|
public static IEnumerable<object[]> NamesOf(ReferenceFormat format) => Of(format).Select(c => new object[] { c.Name });
|
|
|
|
/// <summary>The input formats, with their extensions.</summary>
|
|
public static IReadOnlyList<(string Extension, ReferenceFormat Format)> Extensions => InputExtensions;
|
|
|
|
/// <summary>Reads elenco.txt in <paramref name="folder"/> and finds the input file of every case.</summary>
|
|
public static IReadOnlyList<ReferenceCase> Load(string folder)
|
|
{
|
|
var list = new List<ReferenceCase>();
|
|
var seen = new HashSet<string>(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;
|
|
}
|
|
}
|