Files
Febe/tests/Febe.Tests/Imaging/BmpDecoderTests.cs
Alby96andClaude Opus 5.5 7e8296e790 Correzioni della revisione dei codec di casa (C1)
Correggo i difetti confermati dalla revisione di funzione/codec-immagini e
aggiungo le prove che li avrebbero trovati.

- JPEG: una tabella di Huffman con troppi codici corti dava
  ArgumentOutOfRangeException invece di ImageDecodeException, e un file
  rovinato faceva fallire importazione e ricostruzione; ora il controllo si
  fa prima di scrivere le voci. La prova che diceva di coprirlo cadeva invece
  su «tabella troncata»: ora tiene il totale giusto e controlla il messaggio.
- JPEG: il troncamento delle scansioni di Huffman si vede a ogni riga di MCU,
  non dopo l'ultimo MCU (un file tagliato dopo le intestazioni costava 6,7 s).
- JPEG: IDCT ridotta a 1/2 e 1/4 calcolata dai coefficienti; miniatura di una
  foto 4000x3000 da 126 a 59 ms in Release. Il predittore senza perdita non
  cerca più l'indice della componente a ogni campione.
- WebP: con livello del filtro del fotogramma 0 il filtro è spento, come nei
  decodificatori di riferimento; VP8L costruisce solo i gruppi nominati dai
  meta codici (prima fino a 335 MB di tabelle da 160 KB di file).
- BMP OS/2: altezza senza segno e sempre dal basso.
- Array di lavoro dei codec oltre 16 MiB fuori dal pool condiviso, che li
  avrebbe tenuti dopo l'uso.
- Conversione: profilo ICC copiato solo se RGB, profondità oltre 8 bit e
  blocchi gAMA/cHRM/sRGB dichiarati in NotCarried, Lossy vero quando si perde
  precisione.
- Impronta e misure di una foto da una sola lettura e decodifica; ReadSize
  legge solo l'inizio di JPEG, GIF e BMP.
- docs: decisioni (righe aggiunte, con la correzione dei 2,0 MB del set di
  riferimento) e windows.md (Febe.exe rimisurato: 85,6 MB, a capo sistemato).

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

190 lines
8.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 ACoreHeaderHeightIsUnsignedAndBottomUp()
{
// bcHeight 40000 is 0x9C40, which a signed read takes for −25536 (top-down, wrong size). One-bit pixels,
// rows of 4 bytes: the first row in the file is the bottom of the picture and the only one with index 1.
const int height = 40_000;
byte[] palette = [10, 20, 30, 40, 50, 60];
var pixels = new byte[height * 4];
pixels[0] = 0b1000_0000;
var bmp = Bmp(12, 1, height, 1, BiRgb, palette, pixels);
Assert.AreEqual((1, height), (BmpDecoder.Identify(bmp).Width, BmpDecoder.Identify(bmp).Height));
using var image = BmpDecoder.Decode(bmp);
Assert.AreEqual((1, height), (image.Width, image.Height));
Assert.AreEqual((60, 50, 40, 255), image.GetPixel(0, height - 1), "la prima riga del file è in fondo");
Assert.AreEqual((30, 20, 10, 255), image.GetPixel(0, 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;
}
}