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,41 @@
|
||||
namespace Febe.Core.Codecs.Images;
|
||||
|
||||
/// <summary>
|
||||
/// Decodes an image of any format Febe reads, chosen from its first bytes. The pixels come out as stored in the file:
|
||||
/// the EXIF orientation is not applied here.
|
||||
/// </summary>
|
||||
public static class ImageDecoder
|
||||
{
|
||||
/// <summary>Whether Febe has a decoder for <paramref name="format"/>.</summary>
|
||||
public static bool CanDecode(ImageFormat format) => format is ImageFormat.Png or ImageFormat.Gif or ImageFormat.Bmp;
|
||||
|
||||
/// <summary>Size, frames and metadata of an image, without decoding the pixels.</summary>
|
||||
/// <exception cref="ImageFormatNotSupportedException">The format is not known or has no decoder yet.</exception>
|
||||
/// <exception cref="ImageDecodeException">The file is damaged.</exception>
|
||||
public static ImageInfo Identify(ReadOnlyMemory<byte> data, ImageLimits? limits = null) =>
|
||||
ImageFormatSniffer.Detect(data.Span) switch
|
||||
{
|
||||
ImageFormat.Png => PngDecoder.Identify(data, limits),
|
||||
ImageFormat.Gif => GifDecoder.Identify(data, limits),
|
||||
ImageFormat.Bmp => BmpDecoder.Identify(data, limits),
|
||||
var other => throw NotSupported(other),
|
||||
};
|
||||
|
||||
/// <summary>Decodes an image to straight RGBA8 (the first frame of an animation).</summary>
|
||||
/// <exception cref="ImageFormatNotSupportedException">The format is not known or has no decoder yet.</exception>
|
||||
/// <exception cref="ImageDecodeException">The file is damaged.</exception>
|
||||
/// <exception cref="ImageLimitException">The image is larger than <paramref name="limits"/> allow.</exception>
|
||||
public static RgbaImage Decode(ReadOnlyMemory<byte> data, ImageLimits? limits = null, bool pooled = false) =>
|
||||
ImageFormatSniffer.Detect(data.Span) switch
|
||||
{
|
||||
ImageFormat.Png => PngDecoder.Decode(data, limits, pooled),
|
||||
ImageFormat.Gif => GifDecoder.Decode(data, limits, pooled),
|
||||
ImageFormat.Bmp => BmpDecoder.Decode(data, limits, pooled),
|
||||
var other => throw NotSupported(other),
|
||||
};
|
||||
|
||||
private static ImageFormatNotSupportedException NotSupported(ImageFormat format) =>
|
||||
format == ImageFormat.Unknown
|
||||
? new ImageFormatNotSupportedException("Formato d'immagine non riconosciuto.")
|
||||
: new ImageFormatNotSupportedException($"Febe non legge ancora le immagini {format.ToString().ToUpperInvariant()}.");
|
||||
}
|
||||
Reference in new issue
Block a user