Files
Febe/tests/Febe.Tests/Imaging/ImagingBaseTests.cs
T
Alby96andClaude Opus 5.5 7e5eccd1ec Prove adattate all'unione: HEIC come RgbaImage, facciata dei codec che legge HEIF
Dopo l'unione le foto HEIC escono dalla facciata dei codec di C1 come
RgbaImage (pixel R, G, B, A) e non più come bitmap di SkiaSharp: la prova
dell'angolo ruotato legge R, G, B. La facciata ora decodifica HEIF, quindi
la prova dei formati lo mette fra quelli letti e tiene AVIF fra quelli no.
Una prova nuova guarda la facciata su un HEIC (formato, orientamento da
irot, pixel, limite di misura) e la misura di un HEIC più grande dei byte
che ImageDecoding legge per primi, presa dai soli box. Un HEIC senza
immagine non si dice danneggiato: il suo decodificatore non distingue un
file rovinato da uno che non gestisce (commento e messaggio corretti).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:36:39 +01:00

133 lines
6.4 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.IsTrue(ImageDecoder.CanDecode(ImageFormat.Heif), "HEIC, con il decodificatore HEIF sull'HEVC");
Assert.IsFalse(ImageDecoder.CanDecode(ImageFormat.Tiff) || ImageDecoder.CanDecode(ImageFormat.Avif) || ImageDecoder.CanDecode(ImageFormat.Unknown));
}
}