using Titano.Core.Imaging;
using Titano.Core.Windows.Wic;
namespace Titano.Core.Windows.Codecs;
/// How one image format decodes through WIC on this machine.
/// The format.
/// The WIC decoder that handles its extensions, if any (it may only reach previews, e.g. the in-box DNG decoder).
/// True when WIC delivers a full native decode ().
/// Store extensions whose installation would make the format native.
public sealed record FormatSupport(ImageFileFormat Format, WicDecoderInfo? Decoder, bool IsNative, IReadOnlyList MissingExtensions);
///
/// Which WIC decoders exist on this machine and what they mean per (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
/// again (with refresh) after the user installs an extension.
///
public sealed class CodecInventory
{
/// The Raw Image Extension reuses the CLSID of the older in-box RAW codec, so its package identifies it.
internal const string RawImageExtensionPackageFamily = "Microsoft.RawImageExtension_8wekyb3d8bbwe";
private readonly Dictionary _formats;
internal CodecInventory(IReadOnlyList 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();
}
/// Installed WIC decoders, in WIC's enumeration order.
public IReadOnlyList Decoders { get; }
/// An HEVC decoder MFT is present (HEVC Video Extensions or a vendor hardware decoder).
public bool HasHevcDecoder { get; }
/// An AV1 decoder MFT is present (AV1 Video Extension or a vendor hardware decoder).
public bool HasAv1Decoder { get; }
/// One entry per known format, in order.
public IReadOnlyList Formats { get; }
/// Distinct Store extensions that would unlock at least one format, for the "Install" notice.
public IReadOnlyList MissingExtensions { get; }
/// Enumerates WIC decoders and probes the HEVC/AV1 decoders. Call from any thread.
/// Make WIC re-read codec registrations (after an extension was installed).
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;
/// Which Microsoft Store extension installed a decoder, judged by its CLSID (and vendor for AVIF).
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(requirements.Length);
foreach (var (isMissing, kind) in requirements)
{
if (isMissing) missing.Add(StoreExtension.Get(kind));
}
return [.. missing];
}
}