Lettori PNG, GIF e BMP e scrittore PNG di casa (C1, passo 2)
Prima parte dei codec d'immagine scritti in casa per togliere SkiaSharp (decisione di Alberto del 18 novembre 2026: niente componenti esterni). In Febe.Core/Codecs/Images: la base comune (RgbaImage RGBA a 8 bit non premoltiplicata, anche dal pool; ImageLimits controllati prima di ogni allocazione; ImageDecodeException e sottotipi per limiti e formati non letti; riconoscimento del formato dai primi byte; facciata ImageDecoder), il lettore PNG (tutti i tipi di colore e le profondità, tavolozza e tRNS, Adam7, i cinque filtri, CRC di ogni blocco, metadati eXIf, iCCP, tEXt, zTXt, iTXt e XMP, numero dei fotogrammi APNG), lo scrittore PNG (RGB/RGBA a 8 bit, filtro adattivo per riga, eXIf, iCCP, XMP), il lettore GIF (LZW, primo fotogramma, trasparenza, interlacciamento, tavolozze, numero dei fotogrammi) e il lettore BMP (1-32 bit, BI_RGB, BITFIELDS, RLE8, RLE4, PNG dentro, dal basso e dall'alto). CRC-32 e LZW sono nostri, zlib è quello della BCL. Le prove leggono i 37 riferimenti PNG, GIF e BMP e li trovano uguali byte per byte alle immagini attese, lette a loro volta con il nostro lettore PNG verificato contro un lettore minimo indipendente; poi andate e ritorno dello scrittore, metadati, file danneggiati e tagliati, limiti, e modifiche casuali a seme fisso che devono dare un'immagine o un'ImageDecodeException e nient'altro. Una riga in decisioni.md dice le scelte di lettura. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e4608c62d
commit
8bf76f18ff
23 files changed
+4382
-4
No files matched your search
@@ -0,0 +1,95 @@
|
||||
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);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TheFacadeSaysWhichFormatsItCannotReadYet()
|
||||
{
|
||||
var jpeg = ReferenceImages.Of(ReferenceFormat.Jpeg).First().ReadInput();
|
||||
var error = Assert.ThrowsExactly<ImageFormatNotSupportedException>(() => ImageDecoder.Decode(jpeg));
|
||||
Assert.Contains("JPEG", 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));
|
||||
Assert.IsFalse(ImageDecoder.CanDecode(ImageFormat.Jpeg) || ImageDecoder.CanDecode(ImageFormat.WebP) || ImageDecoder.CanDecode(ImageFormat.Unknown));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user