using System.Text;
using Febe.Core.Codecs.Images;
namespace Febe.Tests.Imaging;
/// The common pieces of the in-house image codecs: format recognition, the RGBA image, the limits.
[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(""), 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(() => image.GetRow(2).Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Assert.ThrowsExactly(() => image.GetPixel(3, 0));
Assert.ThrowsExactly(() => new RgbaImage(0, 1));
Assert.ThrowsExactly(() => new RgbaImage(50_000, 50_000));
Assert.ThrowsExactly(() => 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(() => 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(() => 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(1000);
Assert.AreEqual(1024, small.Length);
Assert.IsTrue(ImageBuffers.IsPooled(small));
ImageBuffers.Return(small);
var limit = ImageBuffers.Rent(ImageBuffers.PooledBytes);
Assert.AreEqual(ImageBuffers.PooledBytes, limit.Length);
Assert.IsTrue(ImageBuffers.IsPooled(limit));
ImageBuffers.Return(limit);
var large = ImageBuffers.Rent(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((ImageBuffers.PooledBytes / 2) + 1);
Assert.AreEqual((ImageBuffers.PooledBytes / 2) + 1, shorts.Length);
ImageBuffers.Return(shorts);
var words = ImageBuffers.Rent(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(() => ImageDecoder.Decode(tiff));
Assert.Contains("TIFF", error.Message);
Assert.ThrowsExactly(() => 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));
}
}