namespace Titano.Core.Imaging; /// How a frame's pixels were obtained (BRIEF §8.2). Logged per frame. public enum DecodeTier { /// Full-resolution decode of the main image (in-house or WIC). Native = 1, /// Largest embedded JPEG preview; output resolution is capped to its size. EmbeddedPreview = 2, /// No decoder could produce pixels; the frame is excluded from renders. Unavailable = 3, } public enum DecodePurpose { Thumbnail, Analysis, Proxy, Render, } /// A request to decode one file. /// Absolute path of the source file. /// Maximum long edge of the result in pixels; 0 = native size. /// Lets providers trade quality for speed (e.g. bilinear demosaic for thumbnails). /// Rotate/flip according to the Exif orientation. /// Accept the embedded-preview tier when native decode is impossible. /// Use the embedded preview even when native decode is possible (fast paths). public sealed record DecodeRequest( string Path, int MaxLongEdge = 0, DecodePurpose Purpose = DecodePurpose.Render, bool ApplyOrientation = true, bool AllowPreview = true, bool ForcePreview = false); /// Decoded pixels plus how they were produced. Dispose to return the buffer to the pool. public sealed class DecodedImage(ImageBuffer pixels, DecodeTier tier, string decoder, int sourceWidth, int sourceHeight) : IDisposable { public ImageBuffer Pixels { get; } = pixels; public DecodeTier Tier { get; } = tier; /// Short provider/decoder name for logs ("wic", "managed-jpeg", "managed-dng"). public string Decoder { get; } = decoder; /// Dimensions of the full-resolution source (before scaling), oriented. public int SourceWidth { get; } = sourceWidth; public int SourceHeight { get; } = sourceHeight; public void Dispose() => Pixels.Dispose(); } /// A platform image decoder (WIC on Windows, managed codecs everywhere). public interface IImageDecoderProvider { /// Short name for logs and the Settings › Formats table. string Name { get; } /// The best tier this provider can deliver for the format on this machine, or null. DecodeTier? TierFor(ImageFileFormat format); bool CanDecode(ImageFormatInfo format) => TierFor(format.Format) is not null; /// Decodes or returns null when this provider cannot handle the specific file. DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default); } /// Tries providers in priority order and applies the tier rules of BRIEF §8.2. public sealed class ImageDecoder(IReadOnlyList providers) { public IReadOnlyList Providers { get; } = providers; /// Best tier available for across all providers. public DecodeTier TierFor(ImageFileFormat format) { var best = DecodeTier.Unavailable; foreach (var p in Providers) { if (p.TierFor(format) is { } t && t < best) best = t; } return best; } /// Decodes with the first provider that succeeds; null means . public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) { var format = ImageFormatInfo.FromPath(request.Path); List? errors = null; foreach (var provider in Providers.OrderBy(p => p.TierFor(format.Format) ?? DecodeTier.Unavailable)) { if (provider.TierFor(format.Format) is null) continue; cancellationToken.ThrowIfCancellationRequested(); try { var result = provider.Decode(request, cancellationToken); if (result is not null) return result; } catch (OperationCanceledException) { throw; } catch (Exception ex) when (ex is IOException or InvalidDataException or NotSupportedException or FormatException or InvalidOperationException or System.Runtime.InteropServices.COMException) { (errors ??= []).Add(ex); } } return null; } }