using Febe.Core.Codecs.Images;
using static Febe.Tests.Imaging.TestImages;
namespace Febe.Tests.Imaging;
/// The in-house GIF decoder on files built from GIF89a: palettes, transparency, clipping, damage.
[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
{
["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(() => GifDecoder.Decode(bytes), name);
}
}
[TestMethod]
public void ACanvasBeyondTheLimitsIsRefusedButStillIdentified()
{
var gif = Gif(60000, 60000, Palette4, GifFrame(0, 0, 1, 1, [1], 2));
Assert.ThrowsExactly(() => GifDecoder.Decode(gif));
Assert.AreEqual((60000, 60000), (GifDecoder.Identify(gif).Width, GifDecoder.Identify(gif).Height));
}
}