Febe deve leggere e scrivere JPEG senza SkiaSharp né altri componenti esterni (decisione del 18 novembre 2026), quindi il codec è scritto dalla specifica ITU-T T.81. Lettore (JpegDecoder): sequenziale di base ed esteso, progressivo (selezione spettrale e approssimazioni successive), senza perdita (predittori 1-7), ognuno con Huffman e con codifica aritmetica; 8 e 12 bit (senza perdita da 2 a 16); qualunque campionamento, ripristino, DNL; grigio, YCbCr, RGB, CMYK e YCCK con il segmento Adobe. IDCT esatta e crominanza con il filtro triangolare. EXIF, ICC a pezzi, XMP e commenti; l'orientamento EXIF va in ImageMetadata.Orientation (anche per PNG) e non si applica ai pixel. I file gerarchici e quelli a 2 componenti si rifiutano come formato non letto; i dati troncati dentro una scansione sono un file danneggiato. Scrittore (JpegEncoder): sequenziale di base, qualità 1-100 sulle tabelle dell'allegato K, 4:2:0, 4:4:4 e grigio, intervallo di ripristino, EXIF, ICC, XMP e commenti. Prove: contro le letture di SkiaSharp i JPEG dell'insieme di riferimento stanno fra 59 e 67 dB di PSNR per canale, scarto massimo 3 (soglia 40 dB e 0,1% di pixel oltre 16). Un codificatore di prova scrive gli stessi coefficienti in progressivo, aritmetico, 12 bit, con ripristino, scansioni separate e DNL, e il lettore deve ridarli identici; senza perdita si confronta campione per campione. Più andata e ritorno dello scrittore, file danneggiati e mutazioni casuali con seme fisso. La facciata ImageDecoder ora legge i JPEG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
4.3 KiB
C#
96 lines
4.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);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void TheFacadeSaysWhichFormatsItCannotReadYet()
|
|
{
|
|
var webp = ReferenceImages.Of(ReferenceFormat.WebP).First().ReadInput();
|
|
var error = Assert.ThrowsExactly<ImageFormatNotSupportedException>(() => ImageDecoder.Decode(webp));
|
|
Assert.Contains("WEBP", 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));
|
|
Assert.IsFalse(ImageDecoder.CanDecode(ImageFormat.WebP) || ImageDecoder.CanDecode(ImageFormat.Unknown));
|
|
}
|
|
}
|