namespace Febe.Core.Codecs.Images; /// /// 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. /// public static class ImageDecoder { /// Whether Febe has a decoder for . public static bool CanDecode(ImageFormat format) => format is ImageFormat.Png or ImageFormat.Jpeg or ImageFormat.Gif or ImageFormat.Bmp or ImageFormat.WebP; /// Size, frames and metadata of an image, without decoding the pixels. /// The format is not known or has no decoder yet. /// The file is damaged. public static ImageInfo Identify(ReadOnlyMemory 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), }; /// Decodes an image to straight RGBA8 (the first frame of an animation). /// The format is not known or has no decoder yet. /// The file is damaged. /// The image is larger than allow. public static RgbaImage Decode(ReadOnlyMemory 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), }; /// Decodes an image to straight RGBA8 and returns what returns as well. /// The format is not known or has no decoder yet. /// The file is damaged. /// The image is larger than allow. public static RgbaImage Decode(ReadOnlyMemory 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); } } /// /// Decodes an image no smaller than 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 returns (the full size). The result may still need resizing. /// public static RgbaImage DecodeAtLeast(ReadOnlyMemory 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()}."); }