Files
Chronos/src/Titano.Core.Windows/Codecs/CodecInventory.cs
T
Claude 3cbf72610e feat(windows): add WIC decoder provider and codec inventory
WicDecoderProvider decodes through a pull chain (decoder, Fant scaler to
MaxLongEdge, format converter) read in bounded row bands. Integer sources are
requested as 64bppRGBA and linearised with the exact sRGB EOTF (Linear16);
float/fixed sources as 128bppRGBAFloat (scRGB, already linear). The Exif
orientation (photo policy, then raw IFD queries) is applied while
de-interleaving instead of through IWICBitmapFlipRotator, which Microsoft
documents as quadratic over scanline decoders.

CodecInventory enumerates installed decoders (CreateComponentEnumerator),
probes HEVC/AV1 decoders with MFTEnumEx and identifies the Raw Image
Extension by package family, since it shares the in-box RAW codec's CLSID.
It maps every ImageFileFormat to its decoder, native flag and the Store
extensions (product ids, ms-windows-store links) that would unlock it.

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

117 lines
6.4 KiB
C#

using Titano.Core.Imaging;
using Titano.Core.Windows.Wic;
namespace Titano.Core.Windows.Codecs;
/// <summary>How one image format decodes through WIC on this machine.</summary>
/// <param name="Format">The format.</param>
/// <param name="Decoder">The WIC decoder that handles its extensions, if any (it may only reach previews, e.g. the in-box DNG decoder).</param>
/// <param name="IsNative">True when WIC delivers a full native decode (<see cref="DecodeTier.Native"/>).</param>
/// <param name="MissingExtensions">Store extensions whose installation would make the format native.</param>
public sealed record FormatSupport(ImageFileFormat Format, WicDecoderInfo? Decoder, bool IsNative, IReadOnlyList<StoreExtension> MissingExtensions);
/// <summary>
/// Which WIC decoders exist on this machine and what they mean per <see cref="ImageFileFormat"/> (BRIEF §8.1):
/// JPEG/PNG/TIFF/BMP/GIF/JPEG XR are built in; DNG and camera RAW count only through the Raw Image Extension
/// (the in-box DNG decoder reads just the embedded preview); HEIF needs the HEIF decoder plus an HEVC decoder,
/// AVIF an AVIF-capable decoder plus an AV1 decoder; WebP the WebP extension. Immutable snapshot; call
/// <see cref="Detect"/> again (with refresh) after the user installs an extension.
/// </summary>
public sealed class CodecInventory
{
/// <summary>The Raw Image Extension reuses the CLSID of the older in-box RAW codec, so its package identifies it.</summary>
internal const string RawImageExtensionPackageFamily = "Microsoft.RawImageExtension_8wekyb3d8bbwe";
private readonly Dictionary<ImageFileFormat, FormatSupport> _formats;
internal CodecInventory(IReadOnlyList<WicDecoderInfo> decoders, bool hasHevcDecoder, bool hasAv1Decoder)
{
Decoders = decoders;
HasHevcDecoder = hasHevcDecoder;
HasAv1Decoder = hasAv1Decoder;
_formats = ImageFormatInfo.Known.ToDictionary(i => i.Format, Evaluate);
Formats = [.. _formats.Values];
MissingExtensions = StoreExtension.All.Where(e => Formats.Any(f => f.MissingExtensions.Contains(e))).ToArray();
}
/// <summary>Installed WIC decoders, in WIC's enumeration order.</summary>
public IReadOnlyList<WicDecoderInfo> Decoders { get; }
/// <summary>An HEVC decoder MFT is present (HEVC Video Extensions or a vendor hardware decoder).</summary>
public bool HasHevcDecoder { get; }
/// <summary>An AV1 decoder MFT is present (AV1 Video Extension or a vendor hardware decoder).</summary>
public bool HasAv1Decoder { get; }
/// <summary>One entry per known format, in <see cref="ImageFormatInfo.Known"/> order.</summary>
public IReadOnlyList<FormatSupport> Formats { get; }
/// <summary>Distinct Store extensions that would unlock at least one format, for the "Install" notice.</summary>
public IReadOnlyList<StoreExtension> MissingExtensions { get; }
/// <summary>Enumerates WIC decoders and probes the HEVC/AV1 decoders. Call from any thread.</summary>
/// <param name="refresh">Make WIC re-read codec registrations (after an extension was installed).</param>
public static CodecInventory Detect(bool refresh = false)
{
var rawExtensionInstalled = PackageProbe.IsInstalled(RawImageExtensionPackageFamily);
var decoders = WicComponents.EnumerateDecoders(refresh)
.Select(d => d with { Extension = Classify(d, rawExtensionInstalled) })
.ToArray();
return new CodecInventory(decoders, VideoDecoderProbe.HasDecoder(VideoDecoderProbe.Hevc), VideoDecoderProbe.HasDecoder(VideoDecoderProbe.Av1));
}
public FormatSupport For(ImageFileFormat format) =>
_formats.TryGetValue(format, out var support) ? support : new FormatSupport(format, null, false, []);
public bool IsNative(ImageFileFormat format) => For(format).IsNative;
/// <summary>Which Microsoft Store extension installed a decoder, judged by its CLSID (and vendor for AVIF).</summary>
internal static StoreExtensionKind? Classify(WicDecoderInfo decoder, bool rawExtensionInstalled)
{
if (decoder.Clsid == WicGuids.RawDecoder) return rawExtensionInstalled ? StoreExtensionKind.RawImage : null;
if (decoder.Clsid == WicGuids.HeifDecoder) return StoreExtensionKind.HeifImage;
if (decoder.Clsid == WicGuids.WebpDecoder) return StoreExtensionKind.WebpImage;
var microsoft = decoder.Vendor == WicGuids.VendorMicrosoft || decoder.Vendor == WicGuids.VendorMicrosoftBuiltIn;
return microsoft && decoder.Handles("avif") ? StoreExtensionKind.Av1Video : null;
}
private FormatSupport Evaluate(ImageFormatInfo info)
{
var handler = Decoders.FirstOrDefault(d => info.Extensions.Any(d.Handles));
switch (info.Format)
{
case ImageFileFormat.Heif:
return new(info.Format, handler, handler is not null && HasHevcDecoder,
Missing((handler is null, StoreExtensionKind.HeifImage), (!HasHevcDecoder, StoreExtensionKind.HevcVideo)));
case ImageFileFormat.Avif:
// AVIF is HEIF with AV1 payloads: the HEIF decoder reads it when no dedicated AVIF decoder is registered.
var avif = handler ?? Decoders.FirstOrDefault(d => d.Extension == StoreExtensionKind.HeifImage);
return new(info.Format, avif, avif is not null && HasAv1Decoder,
Missing((avif is null, StoreExtensionKind.HeifImage), (!HasAv1Decoder, StoreExtensionKind.Av1Video)));
case ImageFileFormat.WebP:
return new(info.Format, handler, handler is not null, Missing((handler is null, StoreExtensionKind.WebpImage)));
}
if (info.IsRaw)
{
var raw = Decoders.FirstOrDefault(d => d.Extension == StoreExtensionKind.RawImage && info.Extensions.Any(d.Handles));
var extensionInstalled = Decoders.Any(d => d.Extension == StoreExtensionKind.RawImage);
return new(info.Format, raw ?? handler, raw is not null, Missing((!extensionInstalled, StoreExtensionKind.RawImage)));
}
return new(info.Format, handler, handler is not null, []);
}
private static StoreExtension[] Missing(params ReadOnlySpan<(bool IsMissing, StoreExtensionKind Kind)> requirements)
{
var missing = new List<StoreExtension>(requirements.Length);
foreach (var (isMissing, kind) in requirements)
{
if (isMissing) missing.Add(StoreExtension.Get(kind));
}
return [.. missing];
}
}