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;
///
/// Native decoding through the Windows Imaging Component (BRIEF §8.2 tier 1) for every format with an installed
/// WIC decoder (see ). 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.
///
///
/// Pixels are requested as 64bppRGBA and linearised with the exact sRGB EOTF ():
/// 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 ().
///
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;
/// The photo-policy query covers every WIC container; the raw paths catch decoders without the policy.
private static readonly string[] OrientationQueries = ["System.Photo.Orientation", "/app1/ifd/{ushort=274}", "/ifd/{ushort=274}"];
/// Codec snapshot deciding ; detected now when omitted.
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;
///
/// 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 ; unreadable files the matching .
///
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);
}
}
/// Size after fitting the long edge into (never upscales; 0 = native).
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);
}
}
/// True for fixed- and floating-point (scRGB) pixel formats; unknown formats count as integer.
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;
}
/// Pulls the converted pixels in bands of rows (bounded memory) and writes oriented linear planes.
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.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(bandPointer + (row * stride), stride);
var index = map.IndexOf(0, top + row);
if (floating) WriteFloatRow(MemoryMarshal.Cast(line), index, map.StepX, r, g, b);
else WriteSrgb16Row(MemoryMarshal.Cast(line), index, map.StepX, r, g, b);
}
}
}
return image;
}
catch
{
image.Dispose();
throw;
}
finally
{
ArrayPool.Shared.Return(band);
}
}
private static void WriteSrgb16Row(ReadOnlySpan rgba, int index, int step, Span r, Span g, Span 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 rgba, int index, int step, Span r, Span g, Span 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);
}
}
}