Files
Chronos/src/Titano.Core/Imaging/Decoding.cs
T
Claude 83a54fb57b chore: bootstrap repository foundation
Solution layout, build rules with zero-package guard, in-house test
framework, localization runtime, brand mark rasteriser with ICO/PNG
generation, Core contracts (imaging, video, domain model, TIFF reader),
Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 08:59:40 +00:00

113 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Titano.Core.Imaging;
/// <summary>How a frame's pixels were obtained (BRIEF §8.2). Logged per frame.</summary>
public enum DecodeTier
{
/// <summary>Full-resolution decode of the main image (in-house or WIC).</summary>
Native = 1,
/// <summary>Largest embedded JPEG preview; output resolution is capped to its size.</summary>
EmbeddedPreview = 2,
/// <summary>No decoder could produce pixels; the frame is excluded from renders.</summary>
Unavailable = 3,
}
public enum DecodePurpose
{
Thumbnail,
Analysis,
Proxy,
Render,
}
/// <summary>A request to decode one file.</summary>
/// <param name="Path">Absolute path of the source file.</param>
/// <param name="MaxLongEdge">Maximum long edge of the result in pixels; 0 = native size.</param>
/// <param name="Purpose">Lets providers trade quality for speed (e.g. bilinear demosaic for thumbnails).</param>
/// <param name="ApplyOrientation">Rotate/flip according to the Exif orientation.</param>
/// <param name="AllowPreview">Accept the embedded-preview tier when native decode is impossible.</param>
/// <param name="ForcePreview">Use the embedded preview even when native decode is possible (fast paths).</param>
public sealed record DecodeRequest(
string Path,
int MaxLongEdge = 0,
DecodePurpose Purpose = DecodePurpose.Render,
bool ApplyOrientation = true,
bool AllowPreview = true,
bool ForcePreview = false);
/// <summary>Decoded pixels plus how they were produced. Dispose to return the buffer to the pool.</summary>
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;
/// <summary>Short provider/decoder name for logs ("wic", "managed-jpeg", "managed-dng").</summary>
public string Decoder { get; } = decoder;
/// <summary>Dimensions of the full-resolution source (before scaling), oriented.</summary>
public int SourceWidth { get; } = sourceWidth;
public int SourceHeight { get; } = sourceHeight;
public void Dispose() => Pixels.Dispose();
}
/// <summary>A platform image decoder (WIC on Windows, managed codecs everywhere).</summary>
public interface IImageDecoderProvider
{
/// <summary>Short name for logs and the Settings › Formats table.</summary>
string Name { get; }
/// <summary>The best tier this provider can deliver for the format on this machine, or null.</summary>
DecodeTier? TierFor(ImageFileFormat format);
bool CanDecode(ImageFormatInfo format) => TierFor(format.Format) is not null;
/// <summary>Decodes or returns null when this provider cannot handle the specific file.</summary>
DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default);
}
/// <summary>Tries providers in priority order and applies the tier rules of BRIEF §8.2.</summary>
public sealed class ImageDecoder(IReadOnlyList<IImageDecoderProvider> providers)
{
public IReadOnlyList<IImageDecoderProvider> Providers { get; } = providers;
/// <summary>Best tier available for <paramref name="format"/> across all providers.</summary>
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;
}
/// <summary>Decodes with the first provider that succeeds; null means <see cref="DecodeTier.Unavailable"/>.</summary>
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
var format = ImageFormatInfo.FromPath(request.Path);
List<Exception>? 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;
}
}