Lettori PNG, GIF e BMP e scrittore PNG di casa (C1, passo 2)

Prima parte dei codec d'immagine scritti in casa per togliere SkiaSharp (decisione di Alberto del 18 novembre 2026: niente componenti esterni). In Febe.Core/Codecs/Images: la base comune (RgbaImage RGBA a 8 bit non premoltiplicata, anche dal pool; ImageLimits controllati prima di ogni allocazione; ImageDecodeException e sottotipi per limiti e formati non letti; riconoscimento del formato dai primi byte; facciata ImageDecoder), il lettore PNG (tutti i tipi di colore e le profondità, tavolozza e tRNS, Adam7, i cinque filtri, CRC di ogni blocco, metadati eXIf, iCCP, tEXt, zTXt, iTXt e XMP, numero dei fotogrammi APNG), lo scrittore PNG (RGB/RGBA a 8 bit, filtro adattivo per riga, eXIf, iCCP, XMP), il lettore GIF (LZW, primo fotogramma, trasparenza, interlacciamento, tavolozze, numero dei fotogrammi) e il lettore BMP (1-32 bit, BI_RGB, BITFIELDS, RLE8, RLE4, PNG dentro, dal basso e dall'alto). CRC-32 e LZW sono nostri, zlib è quello della BCL.

Le prove leggono i 37 riferimenti PNG, GIF e BMP e li trovano uguali byte per byte alle immagini attese, lette a loro volta con il nostro lettore PNG verificato contro un lettore minimo indipendente; poi andate e ritorno dello scrittore, metadati, file danneggiati e tagliati, limiti, e modifiche casuali a seme fisso che devono dare un'immagine o un'ImageDecodeException e nient'altro. Una riga in decisioni.md dice le scelte di lettura.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-18 13:26:58 +01:00
1 parent 3e4608c62d
commit 8bf76f18ff
23 files changed
+4382 -4

No files matched your search

+172
View File
@@ -0,0 +1,172 @@
using Febe.Core.Codecs.Images;
using static Febe.Tests.Imaging.TestImages;
namespace Febe.Tests.Imaging;
/// <summary>The in-house BMP decoder on files built by hand: variants the reference set lacks, and damage.</summary>
[TestClass]
public sealed class BmpDecoderTests
{
private const uint BiRgb = 0;
private const uint BiRle8 = 1;
private const uint BiRle4 = 2;
private const uint BiBitfields = 3;
private const uint BiJpeg = 4;
private const uint BiPng = 5;
private const uint BiAlphaBitfields = 6;
// BGRx palette entries: black, red, green, blue.
private static readonly byte[] Palette4 = [0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0];
[TestMethod]
public void TwoBitsPerPixelBottomUp()
{
// 3×2, stride 4 bytes; the first row in the file is the bottom one. Top row 0 1 2, bottom row 3 2 1.
byte[] pixels = [0b11_10_01_00, 0, 0, 0, 0b00_01_10_00, 0, 0, 0];
using var image = BmpDecoder.Decode(Bmp(40, 3, 2, 2, BiRgb, Palette4, pixels));
Assert.AreEqual((0, 0, 0, 255), image.GetPixel(0, 0));
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(1, 0));
Assert.AreEqual((0, 255, 0, 255), image.GetPixel(2, 0));
Assert.AreEqual((0, 0, 255, 255), image.GetPixel(0, 1));
Assert.AreEqual((0, 255, 0, 255), image.GetPixel(1, 1));
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(2, 1));
}
[TestMethod]
public void ACoreHeaderHasThreeBytePaletteEntries()
{
byte[] palette = [10, 20, 30, 40, 50, 60];
byte[] pixels = [0b0100_0000, 0, 0, 0];
using var image = BmpDecoder.Decode(Bmp(12, 3, 1, 1, BiRgb, palette, pixels));
Assert.AreEqual((30, 20, 10, 255), image.GetPixel(0, 0));
Assert.AreEqual((60, 50, 40, 255), image.GetPixel(1, 0));
Assert.AreEqual((30, 20, 10, 255), image.GetPixel(2, 0));
}
[TestMethod]
public void SixteenBitsWithFourMasksAfterAnInformationHeader()
{
// ARGB 4444: a = 8 → 136, r = 15 → 255, g = 0, b = 15 → 255.
byte[] masks = [0x00, 0x0F, 0, 0, 0xF0, 0x00, 0, 0, 0x0F, 0x00, 0, 0, 0x00, 0xF0, 0, 0];
byte[] pixels = [0x0F, 0x8F, 0x00, 0x00];
using var image = BmpDecoder.Decode(Bmp(40, 1, 1, 16, BiAlphaBitfields, masks, pixels), out var info);
Assert.IsTrue(info.HasAlpha);
Assert.AreEqual((255, 0, 255, 136), image.GetPixel(0, 0));
}
[TestMethod]
public void ThirtyTwoBitsWithMasksInAnyOrder()
{
// V4 header with masks for R G B A in memory order: bytes are R, G, B, A.
byte[] pixels = [1, 2, 3, 4, 250, 251, 252, 253];
var bmp = Bmp(108, 2, -1, 32, BiBitfields, [], pixels, masks: [0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000]);
using var image = BmpDecoder.Decode(bmp);
Assert.AreEqual((1, 2, 3, 4), image.GetPixel(0, 0));
Assert.AreEqual((250, 251, 252, 253), image.GetPixel(1, 0));
// BI_RGB ignores the alpha byte, even with a V4 header.
using var opaque = BmpDecoder.Decode(Bmp(108, 2, -1, 32, BiRgb, [], pixels));
Assert.AreEqual((3, 2, 1, 255), opaque.GetPixel(0, 0));
}
[TestMethod]
public void RleDeltasAndLineEndsLeaveTheSkippedPixelsTransparent()
{
byte[] palette = [0, 0, 255, 0, 255, 0, 0, 0]; // BGRx: red, blue
byte[] data = [2, 1, 0, 0, 0, 2, 1, 1, 1, 0, 0, 1];
using var image = BmpDecoder.Decode(Bmp(40, 4, 3, 8, BiRle8, palette, data, colorsUsed: 2));
Assert.AreEqual((0, 0, 255, 255), image.GetPixel(0, 2), "riga in basso, indice 1");
Assert.AreEqual((0, 0, 255, 255), image.GetPixel(1, 2));
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(2, 2));
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(0, 1), "riga saltata dallo spostamento");
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(1, 0), "dopo lo spostamento (1, 1), indice 0");
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(0, 0));
}
[TestMethod]
public void AnRle4AbsoluteRunOfOddLengthIsPaddedToAWord()
{
var palette = Enumerable.Range(0, 16).SelectMany(i => new byte[] { 0, 0, (byte)(i * 16), 0 }).ToArray();
byte[] data = [0, 5, 0x12, 0x34, 0x50, 0, 2, 0x67, 0, 1];
using var image = BmpDecoder.Decode(Bmp(40, 7, 1, 4, BiRle4, palette, data));
byte[] expected = [1, 2, 3, 4, 5, 6, 7];
for (var x = 0; x < expected.Length; x++)
{
Assert.AreEqual((byte)(expected[x] * 16), image.GetPixel(x, 0).R, $"pixel {x}");
}
}
[TestMethod]
public void AnIndexBeyondAShortPaletteIsOpaqueBlack()
{
byte[] palette = [1, 2, 3, 0, 4, 5, 6, 0];
byte[] pixels = [1, 5, 0, 0];
using var image = BmpDecoder.Decode(Bmp(40, 2, 1, 8, BiRgb, palette, pixels, colorsUsed: 2));
Assert.AreEqual((6, 5, 4, 255), image.GetPixel(0, 0));
Assert.AreEqual((0, 0, 0, 255), image.GetPixel(1, 0));
}
[TestMethod]
public void AnEmbeddedPngIsDecodedByThePngDecoder()
{
using var source = TestImages.Random(5, 3, 8);
var png = PngEncoder.Encode(source);
var bmp = Bmp(40, 5, 3, 0, BiPng, [], png);
using var image = BmpDecoder.Decode(bmp);
ImageAssert.AreEqual(source, image, "PNG nel BMP");
Assert.AreEqual(ImageFormat.Bmp, BmpDecoder.Identify(bmp).Format);
}
[TestMethod]
public void DamagedOrUnsupportedFilesThrow()
{
byte[] row = [0, 0, 0, 0];
var cases = new Dictionary<string, (byte[] Bytes, bool Unsupported)>
{
["vuoto"] = ([], false),
["non BMP"] = ("BA\0\0"u8.ToArray(), false),
["solo l'intestazione del file"] = (Bmp(40, 1, 1, 24, BiRgb, [], row)[..16], false),
["intestazione di 20 byte"] = (PatchHeaderSize(Bmp(40, 1, 1, 24, BiRgb, [], row), 20), true),
["larghezza zero"] = (Bmp(40, 0, 1, 24, BiRgb, [], row), false),
["larghezza negativa"] = (Bmp(40, -3, 1, 24, BiRgb, [], row), false),
["7 bit per pixel"] = (Bmp(40, 1, 1, 7, BiRgb, [], row), false),
["64 bit per pixel"] = (Bmp(40, 1, 1, 64, BiRgb, [], new byte[8]), true),
["JPEG dentro"] = (Bmp(40, 1, 1, 0, BiJpeg, [], [0xFF, 0xD8, 0xFF]), true),
["compressione 9"] = (Bmp(40, 1, 1, 24, 9, [], row), true),
["RLE8 a 4 bit"] = (Bmp(40, 1, 1, 4, BiRle8, Palette4, [0, 1]), false),
["RLE dall'alto"] = (Bmp(40, 1, -1, 8, BiRle8, Palette4, [0, 1]), false),
["RLE tagliato"] = (Bmp(40, 4, 2, 8, BiRle8, Palette4, [2, 1, 0]), false),
["tratto assoluto tagliato"] = (Bmp(40, 4, 1, 8, BiRle8, Palette4, [0, 4, 1, 2]), false),
["pixel tagliati"] = (Bmp(40, 4, 4, 24, BiRgb, [], new byte[40]), false),
["maschere non contigue"] = (Bmp(108, 1, 1, 32, BiBitfields, [], row, masks: [0x00FF00FF, 0x0000FF00, 0, 0]), false),
["maschere oltre i 16 bit"] = (Bmp(108, 1, 1, 16, BiBitfields, [], row, masks: [0x00FF0000, 0x0000FF00, 0xFF, 0]), false),
["maschere mancanti"] = (Bmp(40, 1, 1, 32, BiBitfields, [], [])[..54], false),
};
foreach (var (name, (bytes, unsupported)) in cases)
{
if (unsupported)
{
Assert.ThrowsExactly<ImageFormatNotSupportedException>(() => BmpDecoder.Decode(bytes), name);
}
else
{
Assert.Throws<ImageDecodeException>(() => BmpDecoder.Decode(bytes), name);
}
}
}
[TestMethod]
public void AnImageBeyondTheLimitsIsRefusedButStillIdentified()
{
var bmp = Bmp(40, 100_000, 1, 24, BiRgb, [], new byte[4]);
Assert.ThrowsExactly<ImageLimitException>(() => BmpDecoder.Decode(bmp));
Assert.AreEqual(100_000, BmpDecoder.Identify(bmp).Width);
}
private static byte[] PatchHeaderSize(byte[] bmp, uint size)
{
System.Buffers.Binary.BinaryPrimitives.WriteUInt32LittleEndian(bmp.AsSpan(14), size);
return bmp;
}
}