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:
1 parent
3e4608c62d
commit
8bf76f18ff
23 files changed
+4382
-4
No files matched your search
@@ -0,0 +1,192 @@
|
||||
using Febe.Core.Codecs.Images;
|
||||
using static Febe.Tests.Imaging.TestImages;
|
||||
|
||||
namespace Febe.Tests.Imaging;
|
||||
|
||||
/// <summary>The in-house GIF decoder on files built from GIF89a: palettes, transparency, clipping, damage.</summary>
|
||||
[TestClass]
|
||||
public sealed class GifDecoderTests
|
||||
{
|
||||
// Four colours: black, red, green, blue.
|
||||
private static readonly byte[] Palette4 = [0, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255];
|
||||
|
||||
[TestMethod]
|
||||
public void AFrameOnTheWholeCanvasTakesItsColoursFromTheGlobalPalette()
|
||||
{
|
||||
byte[] indices = [0, 1, 2, 3, 3, 2, 1, 0];
|
||||
var gif = Gif(4, 2, Palette4, GifFrame(0, 0, 4, 2, indices, 2));
|
||||
using var image = GifDecoder.Decode(gif, out var info);
|
||||
Assert.AreEqual((ImageFormat.Gif, 4, 2, 1), (info.Format, info.Width, info.Height, info.FrameCount));
|
||||
Assert.AreEqual((0, 0, 0, 255), image.GetPixel(0, 0));
|
||||
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(1, 0));
|
||||
Assert.AreEqual((0, 0, 255, 255), image.GetPixel(3, 0));
|
||||
Assert.AreEqual((0, 255, 0, 255), image.GetPixel(1, 1));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TheTransparentIndexAndTheAreaOutsideTheFrameAreTransparent()
|
||||
{
|
||||
byte[] indices = [1, 2, 2, 1];
|
||||
var gif = Gif(4, 3, Palette4, GifControl(2), GifFrame(1, 1, 2, 2, indices, 2));
|
||||
using var image = GifDecoder.Decode(gif);
|
||||
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(0, 0), "fuori dal fotogramma");
|
||||
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(1, 1));
|
||||
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(2, 1), "indice trasparente");
|
||||
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(2, 2));
|
||||
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(3, 2));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TheTransparencyOfALaterControlBlockDoesNotChangeTheFirstFrame()
|
||||
{
|
||||
byte[] indices = [1, 2];
|
||||
var gif = Gif(2, 1, Palette4, GifFrame(0, 0, 2, 1, indices, 2), GifControl(1), GifFrame(0, 0, 2, 1, indices, 2));
|
||||
using var image = GifDecoder.Decode(gif, out var info);
|
||||
Assert.AreEqual(2, info.FrameCount);
|
||||
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(0, 0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ALocalPaletteWinsOverTheGlobalOne()
|
||||
{
|
||||
byte[] local = [9, 9, 9, 7, 7, 7];
|
||||
var gif = Gif(2, 1, Palette4, GifFrame(0, 0, 2, 1, [0, 1], 2, local));
|
||||
using var image = GifDecoder.Decode(gif);
|
||||
Assert.AreEqual((9, 9, 9, 255), image.GetPixel(0, 0));
|
||||
Assert.AreEqual((7, 7, 7, 255), image.GetPixel(1, 0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AFrameLargerThanTheCanvasIsClippedAndAnEmptyCanvasTakesTheFrameSize()
|
||||
{
|
||||
var indices = Enumerable.Range(0, 12).Select(i => (byte)(i % 4)).ToArray();
|
||||
using (var clipped = GifDecoder.Decode(Gif(3, 2, Palette4, GifFrame(1, 0, 4, 3, indices, 2))))
|
||||
{
|
||||
Assert.AreEqual((3, 2), (clipped.Width, clipped.Height));
|
||||
Assert.AreEqual((0, 0, 0, 0), clipped.GetPixel(0, 0));
|
||||
Assert.AreEqual((0, 0, 0, 255), clipped.GetPixel(1, 0));
|
||||
Assert.AreEqual((255, 0, 0, 255), clipped.GetPixel(2, 0));
|
||||
Assert.AreEqual((0, 0, 0, 255), clipped.GetPixel(1, 1), "riga 1 del fotogramma, colonna 0: indice 4 % 4");
|
||||
}
|
||||
|
||||
using var sized = GifDecoder.Decode(Gif(0, 0, Palette4, GifFrame(2, 1, 4, 3, indices, 2)));
|
||||
Assert.AreEqual((6, 4), (sized.Width, sized.Height));
|
||||
Assert.AreEqual((0, 0, 255, 255), sized.GetPixel(5, 1));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InterlacedRowsGoWhereTheFourPassesPutThem()
|
||||
{
|
||||
for (var height = 1; height <= 40; height++)
|
||||
{
|
||||
var rows = Enumerable.Range(0, height).Select(r => GifDecoder.InterlacedRow(r, height)).ToList();
|
||||
CollectionAssert.AreEquivalent(Enumerable.Range(0, height).ToList(), rows, $"altezza {height}");
|
||||
}
|
||||
|
||||
CollectionAssert.AreEqual(new[] { 0, 8, 4, 2, 6, 1, 3, 5, 7, 9 }, Enumerable.Range(0, 10).Select(r => GifDecoder.InterlacedRow(r, 10)).ToArray());
|
||||
|
||||
// Row r of the file holds the colour of its own image row: decoding puts each one back in place.
|
||||
const int height10 = 10;
|
||||
var indices = new byte[2 * height10];
|
||||
for (var r = 0; r < height10; r++)
|
||||
{
|
||||
indices[2 * r] = indices[(2 * r) + 1] = (byte)GifDecoder.InterlacedRow(r, height10);
|
||||
}
|
||||
|
||||
var palette = Enumerable.Range(0, 16).SelectMany(i => new[] { (byte)(i * 10), (byte)0, (byte)0 }).ToArray();
|
||||
using var image = GifDecoder.Decode(Gif(2, height10, palette, GifFrame(0, 0, 2, height10, indices, 4, interlaced: true)));
|
||||
for (var y = 0; y < height10; y++)
|
||||
{
|
||||
Assert.AreEqual((byte)(y * 10), image.GetPixel(1, y).R, $"riga {y}");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void LiteralCodesAtEveryInitialSizeDecode()
|
||||
{
|
||||
for (var minCodeSize = 2; minCodeSize <= 8; minCodeSize++)
|
||||
{
|
||||
var colours = 1 << minCodeSize;
|
||||
var random = new Random(minCodeSize);
|
||||
var indices = new byte[37 * 23];
|
||||
for (var i = 0; i < indices.Length; i++)
|
||||
{
|
||||
indices[i] = (byte)random.Next(colours);
|
||||
}
|
||||
|
||||
var output = new byte[indices.Length];
|
||||
var data = GifLiteralLzw(indices, minCodeSize);
|
||||
Assert.AreEqual(indices.Length, GifDecoder.DecodeLzw(data, 0, minCodeSize, output), $"codici da {minCodeSize + 1} bit");
|
||||
CollectionAssert.AreEqual(indices, output, $"codici da {minCodeSize + 1} bit");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DataThatStopsEarlyLeavesTheRestTransparent()
|
||||
{
|
||||
var indices = Enumerable.Repeat((byte)1, 16).ToArray();
|
||||
var full = Gif(4, 4, Palette4, GifFrame(0, 0, 4, 4, indices, 2));
|
||||
|
||||
// Cut inside the LZW data, before the end code: the first pixels are there, the others are not.
|
||||
var cut = full[..(full.Length - 6)];
|
||||
using var image = GifDecoder.Decode(cut);
|
||||
Assert.AreEqual((255, 0, 0, 255), image.GetPixel(0, 0));
|
||||
Assert.AreEqual((0, 0, 0, 0), image.GetPixel(3, 3));
|
||||
|
||||
// Every shorter file either fails cleanly or gives an image of the canvas size.
|
||||
for (var length = 0; length < full.Length; length++)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var partial = GifDecoder.Decode(full[..length]);
|
||||
Assert.AreEqual((4, 4), (partial.Width, partial.Height), $"tagliato a {length}");
|
||||
}
|
||||
catch (ImageDecodeException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CommentsBeforeTheFirstFrameAreMetadataAndOtherExtensionsAreSkipped()
|
||||
{
|
||||
byte[] comment = [0x21, 0xFE, 5, (byte)'C', (byte)'i', (byte)'a', (byte)'o', (byte)' ', 3, (byte)'G', (byte)'I', (byte)'F', 0];
|
||||
byte[] netscape = [0x21, 0xFF, 11, .. "NETSCAPE2.0"u8, 3, 1, 0, 0, 0];
|
||||
var gif = Gif(1, 1, Palette4, comment, netscape, GifFrame(0, 0, 1, 1, [3], 2));
|
||||
var info = GifDecoder.Identify(gif);
|
||||
Assert.AreEqual("Ciao GIF", info.Metadata.Texts.Single().Text);
|
||||
using var image = GifDecoder.Decode(gif);
|
||||
Assert.AreEqual((0, 0, 255, 255), image.GetPixel(0, 0));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DamagedFilesThrowAnImageDecodeException()
|
||||
{
|
||||
var frame = GifFrame(0, 0, 1, 1, [1], 2);
|
||||
var badCodeSize = frame.ToArray();
|
||||
badCodeSize[10] = 12;
|
||||
var cases = new Dictionary<string, byte[]>
|
||||
{
|
||||
["vuoto"] = [],
|
||||
["non GIF"] = "GIF90a"u8.ToArray(),
|
||||
["senza schermo logico"] = "GIF89a\x01\0"u8.ToArray(),
|
||||
["senza fotogrammi"] = Gif(1, 1, Palette4),
|
||||
["senza tavolozza"] = Gif(1, 1, null, frame),
|
||||
["codici LZW da 13 bit"] = Gif(1, 1, Palette4, badCodeSize),
|
||||
["tavolozza globale tagliata"] = Gif(1, 1, Palette4)[..20],
|
||||
};
|
||||
|
||||
foreach (var (name, bytes) in cases)
|
||||
{
|
||||
Assert.Throws<ImageDecodeException>(() => GifDecoder.Decode(bytes), name);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ACanvasBeyondTheLimitsIsRefusedButStillIdentified()
|
||||
{
|
||||
var gif = Gif(60000, 60000, Palette4, GifFrame(0, 0, 1, 1, [1], 2));
|
||||
Assert.ThrowsExactly<ImageLimitException>(() => GifDecoder.Decode(gif));
|
||||
Assert.AreEqual((60000, 60000), (GifDecoder.Identify(gif).Width, GifDecoder.Identify(gif).Height));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user