Files
Chronos/src/Titano.Core.Windows/Wic/WicDecoderProvider.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

223 lines
10 KiB
C#

using System.Buffers;
using System.Runtime.InteropServices;
using Titano.Core.Imaging;
using Titano.Core.Windows.Codecs;
using Titano.Core.Windows.Interop;
namespace Titano.Core.Windows.Wic;
/// <summary>
/// Native decoding through the Windows Imaging Component (BRIEF §8.2 tier 1) for every format with an installed
/// WIC decoder (see <see cref="CodecInventory"/>). Thread-safe: each call builds its own pull chain
/// (decoder → Fant scaler → format converter) on the calling thread, which joins the MTA if it has no apartment.
/// </summary>
/// <remarks>
/// Pixels are requested as 64bppRGBA and linearised with the exact sRGB EOTF (<see cref="ColorSpace.Linear16"/>):
/// WIC integer formats are sRGB-encoded, the 16-bit request keeps 16-bit PNG/TIFF precision, and the result matches
/// the managed decoders bit for bit, whereas WIC's own integer→float conversion uses its own gamma approximation.
/// Sources whose native format is floating or fixed point (scRGB: JPEG XR, HDR) are requested as 128bppRGBAFloat
/// and copied unchanged, keeping highlights above 1. Alpha is discarded. The Exif orientation is applied while
/// de-interleaving (<see cref="OrientationMap"/>).
/// </remarks>
public sealed class WicDecoderProvider : IImageDecoderProvider
{
private const int WINCODEC_ERR_COMPONENTNOTFOUND = unchecked((int)0x88982F50);
private const int WICBitmapInterpolationModeFant = 3;
private const int WICBitmapDitherTypeNone = 0;
private const int WICBitmapPaletteTypeCustom = 0;
private const uint NumericFixedPoint = 4, NumericFloat = 5;
private const int BandBytes = 4 << 20;
/// <summary>The photo-policy query covers every WIC container; the raw paths catch decoders without the policy.</summary>
private static readonly string[] OrientationQueries = ["System.Photo.Orientation", "/app1/ifd/{ushort=274}", "/ifd/{ushort=274}"];
/// <param name="inventory">Codec snapshot deciding <see cref="TierFor"/>; detected now when omitted.</param>
public WicDecoderProvider(CodecInventory? inventory = null) => Inventory = inventory ?? CodecInventory.Detect();
public string Name => "wic";
public CodecInventory Inventory { get; }
public DecodeTier? TierFor(ImageFileFormat format) => Inventory.IsNative(format) ? DecodeTier.Native : null;
/// <summary>
/// Decodes the first frame. Returns null when WIC cannot natively decode this format here, when the request
/// forces the embedded preview, or when no installed decoder recognises the content. Corrupt files raise the
/// WIC <see cref="COMException"/>; unreadable files the matching <see cref="IOException"/>.
/// </summary>
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(request);
if (request.ForcePreview || TierFor(ImageFormatInfo.FromPath(request.Path).Format) is null) return null;
cancellationToken.ThrowIfCancellationRequested();
Win32Com.EnsureInitialized();
var factory = WicFactory.Instance;
var stream = ShellStream.OpenRead(request.Path);
IWICBitmapDecoder? decoder = null;
IWICBitmapFrameDecode? frame = null;
IWICBitmapScaler? scaler = null;
IWICFormatConverter? converter = null;
try
{
var hr = factory.CreateDecoderFromStream(stream, 0, WicDecodeOptions.MetadataCacheOnDemand, out decoder);
if (hr == WINCODEC_ERR_COMPONENTNOTFOUND) return null;
Marshal.ThrowExceptionForHR(hr);
frame = decoder!.GetFrame(0);
frame.GetSize(out var width, out var height);
var orientation = request.ApplyOrientation ? ReadOrientation(frame) : 1;
var (targetWidth, targetHeight) = TargetSize(width, height, request.MaxLongEdge);
IWICBitmapSource source = frame;
if (targetWidth != width || targetHeight != height)
{
scaler = factory.CreateBitmapScaler();
scaler.Initialize(frame, targetWidth, targetHeight, WICBitmapInterpolationModeFant);
source = scaler;
}
converter = factory.CreateFormatConverter();
var sourceFormat = source.GetPixelFormat();
var floating = IsFloatingPoint(factory, frame.GetPixelFormat()) && CanConvert(converter, sourceFormat, WicGuids.PixelFormat128bppRGBAFloat);
var targetFormat = floating ? WicGuids.PixelFormat128bppRGBAFloat : WicGuids.PixelFormat64bppRGBA;
converter.Initialize(source, ref targetFormat, WICBitmapDitherTypeNone, 0, 0.0, WICBitmapPaletteTypeCustom);
var map = OrientationMap.Create(orientation, (int)targetWidth, (int)targetHeight);
var pixels = CopyToPlanes(converter, (int)targetWidth, (int)targetHeight, floating, map, cancellationToken);
var swap = OrientationMap.SwapsAxes(orientation);
return new DecodedImage(pixels, DecodeTier.Native, Name, (int)(swap ? height : width), (int)(swap ? width : height));
}
finally
{
Win32Com.Release(converter);
Win32Com.Release(scaler);
Win32Com.Release(frame);
Win32Com.Release(decoder);
Marshal.Release(stream);
}
}
/// <summary>Size after fitting the long edge into <paramref name="maxLongEdge"/> (never upscales; 0 = native).</summary>
internal static (uint Width, uint Height) TargetSize(uint width, uint height, int maxLongEdge)
{
var longEdge = Math.Max(width, height);
if (maxLongEdge <= 0 || longEdge <= (uint)maxLongEdge) return (width, height);
var scale = (double)maxLongEdge / longEdge;
return ((uint)Math.Max(1, Math.Round(width * scale)), (uint)Math.Max(1, Math.Round(height * scale)));
}
private static int ReadOrientation(IWICBitmapFrameDecode frame)
{
if (frame.GetMetadataQueryReader(out var reader) < 0 || reader is null) return 1;
try
{
foreach (var query in OrientationQueries)
{
var value = default(PropVariant);
if (reader.GetMetadataByName(query, ref value) < 0) continue;
try
{
if (value.TryGetInt32(out var orientation) && orientation is >= 1 and <= 8) return orientation;
}
finally
{
PropVariant.Clear(ref value);
}
}
return 1;
}
finally
{
Win32Com.Release(reader);
}
}
/// <summary>True for fixed- and floating-point (scRGB) pixel formats; unknown formats count as integer.</summary>
private static bool IsFloatingPoint(IWICImagingFactory factory, Guid pixelFormat)
{
IWICComponentInfo? info = null;
try
{
info = factory.CreateComponentInfo(ref pixelFormat);
return info is IWICPixelFormatInfo2 format && format.GetNumericRepresentation() is NumericFixedPoint or NumericFloat;
}
catch (COMException)
{
return false;
}
finally
{
Win32Com.Release(info);
}
}
private static bool CanConvert(IWICFormatConverter converter, Guid from, Guid to)
{
converter.CanConvert(ref from, ref to, out var canConvert);
return canConvert != 0;
}
/// <summary>Pulls the converted pixels in bands of rows (bounded memory) and writes oriented linear planes.</summary>
private static unsafe ImageBuffer CopyToPlanes(IWICBitmapSource source, int width, int height, bool floating, OrientationMap map, CancellationToken cancellationToken)
{
var stride = checked(width * (floating ? 16 : 8));
var bandRows = Math.Clamp(BandBytes / stride, 1, height);
var image = new ImageBuffer(map.Width, map.Height, 3, clear: false);
var band = ArrayPool<byte>.Shared.Rent(checked(stride * bandRows));
try
{
var r = image.Plane(0);
var g = image.Plane(1);
var b = image.Plane(2);
fixed (byte* bandPointer = band)
{
for (var top = 0; top < height; top += bandRows)
{
cancellationToken.ThrowIfCancellationRequested();
var rows = Math.Min(bandRows, height - top);
var rect = new WicRect { X = 0, Y = top, Width = width, Height = rows };
source.CopyPixels(ref rect, (uint)stride, (uint)(stride * rows), (nint)bandPointer);
for (var row = 0; row < rows; row++)
{
var line = new ReadOnlySpan<byte>(bandPointer + (row * stride), stride);
var index = map.IndexOf(0, top + row);
if (floating) WriteFloatRow(MemoryMarshal.Cast<byte, float>(line), index, map.StepX, r, g, b);
else WriteSrgb16Row(MemoryMarshal.Cast<byte, ushort>(line), index, map.StepX, r, g, b);
}
}
}
return image;
}
catch
{
image.Dispose();
throw;
}
finally
{
ArrayPool<byte>.Shared.Return(band);
}
}
private static void WriteSrgb16Row(ReadOnlySpan<ushort> rgba, int index, int step, Span<float> r, Span<float> g, Span<float> b)
{
for (var i = 0; i < rgba.Length; i += 4, index += step)
{
r[index] = ColorSpace.Linear16(rgba[i]);
g[index] = ColorSpace.Linear16(rgba[i + 1]);
b[index] = ColorSpace.Linear16(rgba[i + 2]);
}
}
private static void WriteFloatRow(ReadOnlySpan<float> rgba, int index, int step, Span<float> r, Span<float> g, Span<float> b)
{
// scRGB can carry negative (out-of-gamut) components; the pipeline's range starts at 0.
for (var i = 0; i < rgba.Length; i += 4, index += step)
{
r[index] = MathF.Max(rgba[i], 0f);
g[index] = MathF.Max(rgba[i + 1], 0f);
b[index] = MathF.Max(rgba[i + 2], 0f);
}
}
}