Files
Febe/tests/Febe.Tests/Desktop/IconTests.cs
Alby96andClaude Opus 5.5 d6a6a68496 Codec di casa al posto di SkiaSharp (C1, passo 5)
SkiaSharp e SkiaSharp.NativeAssets.Linux.NoDependencies escono da
Directory.Packages.props e da ogni progetto, per la regola «niente
componenti esterni». Tutto ciò che leggeva, ridimensionava o disegnava
immagini con Skia ora usa codice scritto in casa sulla BCL:

- ImageResampler: Lanczos3 separabile o media per area, alfa
  premoltiplicata, luce lineare facoltativa; ImageGray per l'impronta.
- Il lettore JPEG decodifica ridotto a 1/2, 1/4, 1/8; le miniature
  chiedono almeno il doppio del lato lungo, così l'ultimo passo resta a
  Lanczos e la miniatura resta uguale alla lettura intera.
- ImageOrientation applica l'EXIF in un punto solo (miniature, impronte,
  anteprime, dimensioni mostrate).
- ImageConverter cambia formato fra JPEG, PNG e WebP portando EXIF, ICC,
  XMP e testi, con l'etichetta d'orientamento e i pixel non ruotati;
  PngEncoder scrive anche i testi (tEXt o iTXt).
- Febe.Core.Drawing: rasterizzatore con antialiasing (tracciati,
  riempimenti, tratti, gradienti) per icona, immagini dimostrative,
  riquadro neutro dei video, foto del sito di prova e prove. Febe.ico
  rigenerata.
- ThumbnailStore legge anche i BMP a 24 bit, come quelli del decoder
  video del ramo funzione/decoder-h264.

Le impronte salvate restano confrontabili (390 su 393 campioni identiche,
le altre a distanza 2, soglia 6): nessun ricalcolo. Un PNG o JPEG
troncato ora non ha miniatura né impronta. Documenti, elenco dei
componenti della pagina «Informazioni» e decisioni aggiornati; nuove
prove per ridimensionamento, orientamento, lettura ridotta, conversione
con metadati, testi PNG e rasterizzatore.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 16:02:03 +01:00

139 lines
5.9 KiB
C#

using System.Buffers.Binary;
using Febe.Core.Codecs.Images;
using Febe.Desktop.Core;
using Febe.Tests.Desktop.Support;
namespace Febe.Tests.Desktop;
/// <summary>
/// The icon of the Windows application (spec §12, appendix C): the brand mark drawn with Febe's own canvas and written by the
/// hand-made ICO writer, at 16, 24, 32, 48, 64, 128 and 256 pixels. The committed <c>src/Febe.Desktop/Febe.ico</c> must be
/// what the code draws: <c>FEBE_RIGENERA_ICONA=1 dotnet test --filter IconTests</c> writes it again.
/// </summary>
[TestClass]
public sealed class IconTests
{
[TestMethod]
public void TheIconHasEverySizeDibBelow256AndPngAt256()
{
var ico = BrandMark.RenderIcon();
var entries = IcoWriter.ReadDirectory(ico);
CollectionAssert.AreEqual(new[] { 16, 24, 32, 48, 64, 128, 256 }, entries.Select(e => e.Size).ToArray());
Assert.IsTrue(entries.All(e => e.BitsPerPixel == 32));
Assert.IsTrue(entries.Where(e => e.Size < 256).All(e => !e.IsPng), "DIB below 256");
Assert.IsTrue(entries.Single(e => e.Size == 256).IsPng, "PNG at 256");
// Entries follow the directory and do not overlap.
var end = 6 + (16 * entries.Count);
foreach (var entry in entries.OrderBy(e => e.Offset))
{
Assert.AreEqual(end, entry.Offset);
end += entry.Length;
}
Assert.AreEqual(ico.Length, end);
// A DIB has the doubled height of the XOR + AND images and the right length.
foreach (var entry in entries.Where(e => !e.IsPng))
{
var header = ico.AsSpan(entry.Offset, 40);
Assert.AreEqual(40u, BinaryPrimitives.ReadUInt32LittleEndian(header));
Assert.AreEqual(entry.Size, BinaryPrimitives.ReadInt32LittleEndian(header[4..]));
Assert.AreEqual(entry.Size * 2, BinaryPrimitives.ReadInt32LittleEndian(header[8..]));
var maskStride = ((entry.Size + 31) / 32) * 4;
Assert.AreEqual(40 + (entry.Size * entry.Size * 4) + (maskStride * entry.Size), entry.Length);
}
}
[TestMethod]
public void EveryImageShowsTheMarkTransparentCornersAccentSquareWhiteMoon()
{
var ico = BrandMark.RenderIcon();
foreach (var entry in IcoWriter.ReadDirectory(ico))
{
using var image = IcoWriter.Decode(ico, entry);
var s = entry.Size;
Assert.AreEqual(0, image.GetPixel(0, 0).A, $"{s}: transparent corner");
Assert.AreEqual(0, image.GetPixel(s - 1, s - 1).A, $"{s}: transparent corner");
// Inside the square, away from ring and moon (x=4/32, y=28/32): the accent colour #0F5C8C.
var accent = image.GetPixel(Scale(4, s), Scale(28, s));
if (s >= 24)
{
AssertClose(Accent, accent, 6, $"{s}: accent");
}
// The centre of the moon (23.2, 8.8) is white.
var moon = image.GetPixel(Scale(23.2, s), Scale(8.8, s));
if (s >= 32)
{
AssertClose(White, moon, 10, $"{s}: moon");
}
// The centre of the ring (14.5, 17.5) is the accent, not white: it is a ring, not a disc.
var hole = image.GetPixel(Scale(14.5, s), Scale(17.5, s));
if (s >= 32)
{
AssertClose(Accent, hole, 6, $"{s}: ring hole");
}
}
}
[TestMethod]
public void TheWriterRefusesImagesThatAreNotSquareOrTooLarge()
{
using var wide = new RgbaImage(32, 16);
using var huge = new RgbaImage(512, 512);
Assert.ThrowsExactly<ArgumentException>(() => IcoWriter.Write([wide]));
Assert.ThrowsExactly<ArgumentException>(() => IcoWriter.Write([huge]));
Assert.ThrowsExactly<ArgumentException>(() => IcoWriter.Write([]));
Assert.ThrowsExactly<InvalidDataException>(() => IcoWriter.ReadDirectory([1, 2, 3, 4, 5, 6]));
}
[TestMethod]
public void TheCommittedIconIsWhatTheCodeDraws()
{
var path = Repository.PathOf("src", "Febe.Desktop", "Febe.ico");
var fresh = BrandMark.RenderIcon();
if (Environment.GetEnvironmentVariable("FEBE_RIGENERA_ICONA") == "1")
{
File.WriteAllBytes(path, fresh);
}
Assert.IsTrue(File.Exists(path), "src/Febe.Desktop/Febe.ico manca: FEBE_RIGENERA_ICONA=1 lo genera.");
var committed = File.ReadAllBytes(path);
var a = IcoWriter.ReadDirectory(committed);
var b = IcoWriter.ReadDirectory(fresh);
CollectionAssert.AreEqual(b.Select(e => e.Size).ToArray(), a.Select(e => e.Size).ToArray());
// Pixel by pixel, with a small tolerance (the drawing is in floating point).
for (var i = 0; i < a.Count; i++)
{
using var left = IcoWriter.Decode(committed, a[i]);
using var right = IcoWriter.Decode(fresh, b[i]);
for (var y = 0; y < left.Height; y++)
{
for (var x = 0; x < left.Width; x++)
{
AssertClose(right.GetPixel(x, y), left.GetPixel(x, y), 3, $"{a[i].Size} px ({x},{y})");
}
}
}
}
private static int Scale(double coordinate, int size) => Math.Clamp((int)Math.Floor(coordinate * size / 32), 0, size - 1);
private static readonly (byte R, byte G, byte B, byte A) Accent = (0x0F, 0x5C, 0x8C, 0xFF);
private static readonly (byte R, byte G, byte B, byte A) White = (0xFF, 0xFF, 0xFF, 0xFF);
private static void AssertClose((byte R, byte G, byte B, byte A) expected, (byte R, byte G, byte B, byte A) actual, int tolerance, string what)
{
var ok = Math.Abs(expected.R - actual.R) <= tolerance
&& Math.Abs(expected.G - actual.G) <= tolerance
&& Math.Abs(expected.B - actual.B) <= tolerance
&& Math.Abs(expected.A - actual.A) <= tolerance;
Assert.IsTrue(ok, $"{what}: atteso {expected}, trovato {actual}");
}
}