SkiaSharp e SkiaSharp.NativeAssets.Linux.NoDependencies escono da Directory.Packages.props e da ogni progetto, per la regola «niente componenti esterni». Tutto ciò che leggeva, ridimensionava o disegnava immagini con Skia ora usa codice scritto in casa sulla BCL: - ImageResampler: Lanczos3 separabile o media per area, alfa premoltiplicata, luce lineare facoltativa; ImageGray per l'impronta. - Il lettore JPEG decodifica ridotto a 1/2, 1/4, 1/8; le miniature chiedono almeno il doppio del lato lungo, così l'ultimo passo resta a Lanczos e la miniatura resta uguale alla lettura intera. - ImageOrientation applica l'EXIF in un punto solo (miniature, impronte, anteprime, dimensioni mostrate). - ImageConverter cambia formato fra JPEG, PNG e WebP portando EXIF, ICC, XMP e testi, con l'etichetta d'orientamento e i pixel non ruotati; PngEncoder scrive anche i testi (tEXt o iTXt). - Febe.Core.Drawing: rasterizzatore con antialiasing (tracciati, riempimenti, tratti, gradienti) per icona, immagini dimostrative, riquadro neutro dei video, foto del sito di prova e prove. Febe.ico rigenerata. - ThumbnailStore legge anche i BMP a 24 bit, come quelli del decoder video del ramo funzione/decoder-h264. Le impronte salvate restano confrontabili (390 su 393 campioni identiche, le altre a distanza 2, soglia 6): nessun ricalcolo. Un PNG o JPEG troncato ora non ha miniatura né impronta. Documenti, elenco dei componenti della pagina «Informazioni» e decisioni aggiornati; nuove prove per ridimensionamento, orientamento, lettura ridotta, conversione con metadati, testi PNG e rasterizzatore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
5.0 KiB
C#
93 lines
5.0 KiB
C#
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.Jpeg or ImageFormat.Gif or ImageFormat.Bmp or ImageFormat.WebP;
|
|
|
|
/// <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.Jpeg => JpegDecoder.Identify(data, limits),
|
|
ImageFormat.Gif => GifDecoder.Identify(data, limits),
|
|
ImageFormat.Bmp => BmpDecoder.Identify(data, limits),
|
|
ImageFormat.WebP => WebPDecoder.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.Jpeg => JpegDecoder.Decode(data, limits, pooled),
|
|
ImageFormat.Gif => GifDecoder.Decode(data, limits, pooled),
|
|
ImageFormat.Bmp => BmpDecoder.Decode(data, limits, pooled),
|
|
ImageFormat.WebP => WebPDecoder.Decode(data, limits, pooled),
|
|
var other => throw NotSupported(other),
|
|
};
|
|
|
|
/// <summary>Decodes an image to straight RGBA8 and returns what <see cref="Identify"/> returns as well.</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, out ImageInfo info, ImageLimits? limits = null, bool pooled = false)
|
|
{
|
|
switch (ImageFormatSniffer.Detect(data.Span))
|
|
{
|
|
case ImageFormat.Png:
|
|
return PngDecoder.Decode(data, out info, limits, pooled);
|
|
case ImageFormat.Jpeg:
|
|
return JpegDecoder.Decode(data, out info, limits, pooled);
|
|
case ImageFormat.Gif:
|
|
return GifDecoder.Decode(data, out info, limits, pooled);
|
|
case ImageFormat.Bmp:
|
|
return BmpDecoder.Decode(data, out info, limits, pooled);
|
|
case ImageFormat.WebP:
|
|
return WebPDecoder.Decode(data, out info, limits, pooled);
|
|
case var other:
|
|
throw NotSupported(other);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decodes an image no smaller than <paramref name="minLongSide"/> on its long side where the format allows a
|
|
/// cheaper reduced decode (JPEG: a half, a quarter or an eighth, the smallest that keeps that side), at full size
|
|
/// otherwise; with what <see cref="Identify"/> returns (the full size). The result may still need resizing.
|
|
/// </summary>
|
|
public static RgbaImage DecodeAtLeast(ReadOnlyMemory<byte> data, int minLongSide, out ImageInfo info, ImageLimits? limits = null, bool pooled = false)
|
|
{
|
|
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(minLongSide);
|
|
if (ImageFormatSniffer.Detect(data.Span) != ImageFormat.Jpeg)
|
|
{
|
|
return Decode(data, out info, limits, pooled);
|
|
}
|
|
|
|
var header = JpegDecoder.Identify(data, limits);
|
|
var longSide = Math.Max(header.Width, header.Height);
|
|
var reduction = 0;
|
|
while (reduction < 3 && ((longSide + (2 << reduction) - 1) >> (reduction + 1)) >= minLongSide)
|
|
{
|
|
reduction++;
|
|
}
|
|
|
|
return JpegDecoder.DecodeReduced(data, reduction, out info, limits, pooled);
|
|
}
|
|
|
|
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()}.");
|
|
}
|