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>
132 lines
6.3 KiB
C#
132 lines
6.3 KiB
C#
using System.Text;
|
|
using Febe.Core.Codecs.Images;
|
|
|
|
namespace Febe.Tests.Imaging;
|
|
|
|
/// <summary>The common pieces of the in-house image codecs: format recognition, the RGBA image, the limits.</summary>
|
|
[TestClass]
|
|
public sealed class ImagingBaseTests
|
|
{
|
|
[TestMethod]
|
|
public void FormatsAreRecognisedFromTheirFirstBytes()
|
|
{
|
|
var bmp = new byte[18];
|
|
"BM"u8.CopyTo(bmp);
|
|
bmp[14] = 40;
|
|
var cases = new (byte[] Bytes, ImageFormat Format)[]
|
|
{
|
|
([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0], ImageFormat.Png),
|
|
([0xFF, 0xD8, 0xFF, 0xE0], ImageFormat.Jpeg),
|
|
("GIF89a"u8.ToArray(), ImageFormat.Gif),
|
|
("GIF87a"u8.ToArray(), ImageFormat.Gif),
|
|
(bmp, ImageFormat.Bmp),
|
|
("RIFF\0\0\0\0WEBPVP8 "u8.ToArray(), ImageFormat.WebP),
|
|
("II*\0"u8.ToArray(), ImageFormat.Tiff),
|
|
("MM\0*"u8.ToArray(), ImageFormat.Tiff),
|
|
("\0\0\0\u0018ftypheic"u8.ToArray(), ImageFormat.Heif),
|
|
("\0\0\0\u0018ftypmif1"u8.ToArray(), ImageFormat.Heif),
|
|
("\0\0\0\u0018ftypavif"u8.ToArray(), ImageFormat.Avif),
|
|
("\0\0\0\u0018ftypisom"u8.ToArray(), ImageFormat.Unknown),
|
|
("RIFF\0\0\0\0AVI "u8.ToArray(), ImageFormat.Unknown),
|
|
("GIF89"u8.ToArray(), ImageFormat.Unknown),
|
|
("BM"u8.ToArray(), ImageFormat.Unknown),
|
|
([0x89, 0x50, 0x4E, 0x47], ImageFormat.Unknown),
|
|
([], ImageFormat.Unknown),
|
|
(Encoding.ASCII.GetBytes("<html>"), ImageFormat.Unknown),
|
|
};
|
|
|
|
foreach (var (bytes, format) in cases)
|
|
{
|
|
Assert.AreEqual(format, ImageFormatSniffer.Detect(bytes), Convert.ToHexString(bytes));
|
|
}
|
|
|
|
// A BMP whose information header has a size no version has is not taken for one.
|
|
bmp[14] = 41;
|
|
Assert.AreEqual(ImageFormat.Unknown, ImageFormatSniffer.Detect(bmp));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void AnImageHasRowsOfFourBytesPerPixel()
|
|
{
|
|
using var image = new RgbaImage(3, 2);
|
|
Assert.AreEqual((12, 24), (image.Stride, image.Length));
|
|
Assert.IsTrue(image.Pixels.ToArray().All(b => b == 0));
|
|
Assert.IsFalse(image.IsOpaque());
|
|
image.GetRow(1)[4..8].Fill(200);
|
|
Assert.AreEqual((200, 200, 200, 200), image.GetPixel(1, 1));
|
|
Assert.AreEqual(200, image.ToArray()[16]);
|
|
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => image.GetRow(2).Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => image.GetPixel(3, 0));
|
|
|
|
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RgbaImage(0, 1));
|
|
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new RgbaImage(50_000, 50_000));
|
|
Assert.ThrowsExactly<ArgumentException>(() => new RgbaImage(2, 2, new byte[15]));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ARentedImageStartsClearAndGoesBackToThePool()
|
|
{
|
|
for (var round = 0; round < 3; round++)
|
|
{
|
|
var image = RgbaImage.Rent(33, 17);
|
|
Assert.IsTrue(image.IsPooled);
|
|
Assert.IsTrue(image.Pixels.ToArray().All(b => b == 0), $"giro {round}: un array del pool arriva sporco");
|
|
image.Pixels.Fill(0xAB);
|
|
image.Dispose();
|
|
Assert.ThrowsExactly<ObjectDisposedException>(() => image.GetRow(0).Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
// Disposing an image that owns its array changes nothing.
|
|
var owned = new RgbaImage(1, 1);
|
|
owned.Dispose();
|
|
Assert.AreEqual(4, owned.Pixels.Length);
|
|
|
|
// A picture above 16 MiB is rented as a plain array, not from the pool, and still starts clear.
|
|
var large = RgbaImage.Rent(2100, 2100);
|
|
Assert.IsTrue(large.IsPooled);
|
|
Assert.IsTrue(large.Pixels.IndexOfAnyExcept((byte)0) < 0);
|
|
large.Dispose();
|
|
Assert.ThrowsExactly<ObjectDisposedException>(() => large.GetRow(0).Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void LargeWorkingArraysDoNotStayInTheSharedPool()
|
|
{
|
|
// Up to 16 MiB the shared pool, rounded up to a power of two; above, an array of exactly the size asked,
|
|
// which the pool would otherwise round to the next power of two and keep after it was given back.
|
|
var small = ImageBuffers.Rent<byte>(1000);
|
|
Assert.AreEqual(1024, small.Length);
|
|
Assert.IsTrue(ImageBuffers.IsPooled(small));
|
|
ImageBuffers.Return(small);
|
|
|
|
var limit = ImageBuffers.Rent<byte>(ImageBuffers.PooledBytes);
|
|
Assert.AreEqual(ImageBuffers.PooledBytes, limit.Length);
|
|
Assert.IsTrue(ImageBuffers.IsPooled(limit));
|
|
ImageBuffers.Return(limit);
|
|
|
|
var large = ImageBuffers.Rent<byte>(ImageBuffers.PooledBytes + 1);
|
|
Assert.AreEqual(ImageBuffers.PooledBytes + 1, large.Length, "non arrotondato a 32 MiB: non viene dal pool");
|
|
Assert.IsFalse(ImageBuffers.IsPooled(large));
|
|
ImageBuffers.Return(large); // left to the collector: the pool would refuse an array it did not give
|
|
|
|
// The threshold is in bytes, whatever the element.
|
|
var shorts = ImageBuffers.Rent<short>((ImageBuffers.PooledBytes / 2) + 1);
|
|
Assert.AreEqual((ImageBuffers.PooledBytes / 2) + 1, shorts.Length);
|
|
ImageBuffers.Return(shorts);
|
|
var words = ImageBuffers.Rent<uint>(1000);
|
|
Assert.AreEqual(1024, words.Length);
|
|
ImageBuffers.Return(words);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheFacadeSaysWhichFormatsItCannotReadYet()
|
|
{
|
|
var tiff = "II*\0\u0008\0\0\0\0\0"u8.ToArray();
|
|
var error = Assert.ThrowsExactly<ImageFormatNotSupportedException>(() => ImageDecoder.Decode(tiff));
|
|
Assert.Contains("TIFF", error.Message);
|
|
Assert.ThrowsExactly<ImageFormatNotSupportedException>(() => ImageDecoder.Identify(Encoding.ASCII.GetBytes("testo qualsiasi")));
|
|
Assert.IsTrue(ImageDecoder.CanDecode(ImageFormat.Png) && ImageDecoder.CanDecode(ImageFormat.Gif) && ImageDecoder.CanDecode(ImageFormat.Bmp) && ImageDecoder.CanDecode(ImageFormat.Jpeg) && ImageDecoder.CanDecode(ImageFormat.WebP));
|
|
Assert.IsFalse(ImageDecoder.CanDecode(ImageFormat.Tiff) || ImageDecoder.CanDecode(ImageFormat.Heif) || ImageDecoder.CanDecode(ImageFormat.Unknown));
|
|
}
|
|
}
|