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
This commit is contained in:
15 files changed
+1555
No files matched your search
@@ -0,0 +1,116 @@
|
||||
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];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Titano.Core.Windows.Codecs;
|
||||
|
||||
/// <summary>Checks whether a packaged (Store) app is registered for the current user.</summary>
|
||||
internal static partial class PackageProbe
|
||||
{
|
||||
private const int ERROR_SUCCESS = 0;
|
||||
private const int ERROR_INSUFFICIENT_BUFFER = 122;
|
||||
|
||||
/// <summary>True when at least one package of <paramref name="packageFamilyName"/> is installed for this user.</summary>
|
||||
public static bool IsInstalled(string packageFamilyName)
|
||||
{
|
||||
uint count = 0, bufferLength = 0;
|
||||
// With empty buffers the call reports how many packages exist (ERROR_INSUFFICIENT_BUFFER when any do).
|
||||
var result = GetPackagesByPackageFamily(packageFamilyName, ref count, 0, ref bufferLength, 0);
|
||||
return (result is ERROR_SUCCESS or ERROR_INSUFFICIENT_BUFFER) && count > 0;
|
||||
}
|
||||
|
||||
[LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16)]
|
||||
private static partial int GetPackagesByPackageFamily(string packageFamilyName, ref uint count, nint packageFullNames, ref uint bufferLength, nint buffer);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Titano.Core.Windows.Shell;
|
||||
|
||||
namespace Titano.Core.Windows.Codecs;
|
||||
|
||||
/// <summary>Free or paid Microsoft Store codec extensions that widen what WIC can decode (BRIEF §8.1).</summary>
|
||||
public enum StoreExtensionKind
|
||||
{
|
||||
RawImage,
|
||||
HeifImage,
|
||||
HevcVideo,
|
||||
Av1Video,
|
||||
WebpImage,
|
||||
}
|
||||
|
||||
/// <summary>A Microsoft Store codec extension: product id and the URIs that open its Store page.</summary>
|
||||
public sealed record StoreExtension(StoreExtensionKind Kind, string ProductName, string ProductId)
|
||||
{
|
||||
/// <summary>Catalogue in the order the UI lists missing extensions.</summary>
|
||||
public static IReadOnlyList<StoreExtension> All { get; } =
|
||||
[
|
||||
new(StoreExtensionKind.RawImage, "Raw Image Extension", "9NCTDW2W1BH8"),
|
||||
new(StoreExtensionKind.HeifImage, "HEIF Image Extensions", "9PMMSR1CGPWG"),
|
||||
new(StoreExtensionKind.HevcVideo, "HEVC Video Extensions", "9NMZLZ57R3T7"),
|
||||
new(StoreExtensionKind.Av1Video, "AV1 Video Extension", "9MVZQVXJBQ9V"),
|
||||
new(StoreExtensionKind.WebpImage, "WebP Image Extensions", "9PG2DK419DRG"),
|
||||
];
|
||||
|
||||
/// <summary>Store app deep link (<c>ms-windows-store://pdp/?ProductId=…</c>).</summary>
|
||||
public Uri StoreUri => new($"ms-windows-store://pdp/?ProductId={ProductId}");
|
||||
|
||||
/// <summary>Web page of the same product, for systems without the Store app.</summary>
|
||||
public Uri WebUri => new($"https://apps.microsoft.com/detail/{ProductId.ToLowerInvariant()}");
|
||||
|
||||
public static StoreExtension Get(StoreExtensionKind kind) => All.First(e => e.Kind == kind);
|
||||
|
||||
/// <summary>Opens the Store page through the shell; false when the shell could not open it.</summary>
|
||||
public bool OpenInStore() => ShellLauncher.OpenUrl(StoreUri.AbsoluteUri);
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Titano.Core.Windows.Codecs;
|
||||
|
||||
/// <summary>
|
||||
/// Detects HEVC and AV1 decoders with <c>MFTEnumEx</c>, the check Microsoft documents for the codecs that HEIC and
|
||||
/// AVIF decoding through WIC depend on (HEVC / AV1 Video Extensions or a vendor hardware decoder).
|
||||
/// </summary>
|
||||
internal static unsafe partial class VideoDecoderProbe
|
||||
{
|
||||
/// <summary>MFVideoFormat_HEVC (FOURCC 'HEVC').</summary>
|
||||
public static readonly Guid Hevc = new("43564548-0000-0010-8000-00aa00389b71");
|
||||
|
||||
/// <summary>MFVideoFormat_AV1 (FOURCC 'AV01').</summary>
|
||||
public static readonly Guid Av1 = new("31305641-0000-0010-8000-00aa00389b71");
|
||||
|
||||
private static readonly Guid VideoDecoderCategory = new("d6c02d4b-6833-45b4-971a-05a4b04bab91");
|
||||
private static readonly Guid MediaTypeVideo = new("73646976-0000-0010-8000-00aa00389b71");
|
||||
|
||||
private const uint MF_VERSION = 0x00020070;
|
||||
private const uint MFSTARTUP_LITE = 0x1;
|
||||
private const uint MFT_ENUM_FLAG_SYNCMFT = 0x1, MFT_ENUM_FLAG_ASYNCMFT = 0x2, MFT_ENUM_FLAG_HARDWARE = 0x4, MFT_ENUM_FLAG_SORTANDFILTER = 0x40;
|
||||
|
||||
/// <summary>True when at least one decoder MFT accepts the video <paramref name="subtype"/>.</summary>
|
||||
public static bool HasDecoder(Guid subtype)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (MFStartup(MF_VERSION, MFSTARTUP_LITE) < 0) return false;
|
||||
try
|
||||
{
|
||||
var input = new MftRegisterTypeInfo { MajorType = MediaTypeVideo, Subtype = subtype };
|
||||
const uint flags = MFT_ENUM_FLAG_SYNCMFT | MFT_ENUM_FLAG_ASYNCMFT | MFT_ENUM_FLAG_HARDWARE | MFT_ENUM_FLAG_SORTANDFILTER;
|
||||
if (MFTEnumEx(VideoDecoderCategory, flags, &input, null, out var activates, out var count) < 0) return false;
|
||||
for (var i = 0; i < count; i++) Marshal.Release(((nint*)activates)[i]);
|
||||
Marshal.FreeCoTaskMem(activates);
|
||||
return count > 0;
|
||||
}
|
||||
finally
|
||||
{
|
||||
MFShutdown();
|
||||
}
|
||||
}
|
||||
catch (DllNotFoundException)
|
||||
{
|
||||
// Windows "N" editions ship without Media Foundation unless the Media Feature Pack is installed.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private struct MftRegisterTypeInfo
|
||||
{
|
||||
public Guid MajorType;
|
||||
public Guid Subtype;
|
||||
}
|
||||
|
||||
[LibraryImport("mfplat.dll")]
|
||||
private static partial int MFStartup(uint version, uint flags);
|
||||
|
||||
[LibraryImport("mfplat.dll")]
|
||||
private static partial int MFShutdown();
|
||||
|
||||
[LibraryImport("mfplat.dll")]
|
||||
private static partial int MFTEnumEx(Guid category, uint flags, MftRegisterTypeInfo* inputType, MftRegisterTypeInfo* outputType, out nint activates, out uint count);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
namespace Titano.Core.Windows.Codecs;
|
||||
|
||||
/// <summary>One installed WIC decoder as registered on this machine.</summary>
|
||||
/// <param name="FriendlyName">Registered display name, e.g. "JPEG Decoder".</param>
|
||||
/// <param name="Clsid">Decoder class id.</param>
|
||||
/// <param name="ContainerFormat">WIC container format GUID.</param>
|
||||
/// <param name="Vendor">Vendor GUID (Microsoft built-in, Microsoft, or a third party).</param>
|
||||
/// <param name="Version">Registered version string.</param>
|
||||
/// <param name="FileExtensions">Lower-case extensions without the dot.</param>
|
||||
/// <param name="Extension">The Microsoft Store extension that installed it, if any.</param>
|
||||
public sealed record WicDecoderInfo(
|
||||
string FriendlyName,
|
||||
Guid Clsid,
|
||||
Guid ContainerFormat,
|
||||
Guid Vendor,
|
||||
string Version,
|
||||
IReadOnlyList<string> FileExtensions,
|
||||
StoreExtensionKind? Extension)
|
||||
{
|
||||
/// <summary>True when the decoder registers <paramref name="extension"/> (with or without the dot).</summary>
|
||||
public bool Handles(string extension)
|
||||
{
|
||||
var bare = extension.TrimStart('.');
|
||||
foreach (var e in FileExtensions)
|
||||
{
|
||||
if (string.Equals(e, bare, StringComparison.OrdinalIgnoreCase)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>Parses WIC's comma-separated extension list (".jpg,.jpeg") into bare lower-case names.</summary>
|
||||
internal static IReadOnlyList<string> ParseExtensions(string list) =>
|
||||
list.Split([',', ';', ' '], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
|
||||
.Select(e => e.TrimStart('.').ToLowerInvariant())
|
||||
.Where(e => e.Length > 0)
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace Titano.Core.Windows.Wic;
|
||||
|
||||
/// <summary>
|
||||
/// Where each stored pixel (x, y) of a <c>width</c>×<c>height</c> image lands once Exif orientation 1–8 is applied,
|
||||
/// as a linear index <c>Origin + x·StepX + y·StepY</c> into the row-major oriented image. Applying the orientation
|
||||
/// while de-interleaving costs nothing extra, unlike a WIC flip-rotator over a scanline decoder.
|
||||
/// </summary>
|
||||
internal readonly record struct OrientationMap(int Width, int Height, int Origin, int StepX, int StepY)
|
||||
{
|
||||
/// <summary>Orientations 5–8 swap width and height.</summary>
|
||||
public static bool SwapsAxes(int orientation) => orientation is >= 5 and <= 8;
|
||||
|
||||
/// <summary>Map for a stored <paramref name="width"/>×<paramref name="height"/> image; unknown values act as 1.</summary>
|
||||
public static OrientationMap Create(int orientation, int width, int height) => orientation switch
|
||||
{
|
||||
2 => new(width, height, width - 1, -1, width), // mirror horizontal
|
||||
3 => new(width, height, (height * width) - 1, -1, -width), // rotate 180°
|
||||
4 => new(width, height, (height - 1) * width, 1, -width), // mirror vertical
|
||||
5 => new(height, width, 0, height, 1), // transpose
|
||||
6 => new(height, width, height - 1, height, -1), // rotate 90° clockwise
|
||||
7 => new(height, width, (width * height) - 1, -height, -1), // transverse
|
||||
8 => new(height, width, (width - 1) * height, -height, 1), // rotate 90° counter-clockwise
|
||||
_ => new(width, height, 0, 1, width),
|
||||
};
|
||||
|
||||
public int IndexOf(int x, int y) => Origin + (x * StepX) + (y * StepY);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Titano.Core.Diagnostics;
|
||||
using Titano.Core.Windows.Codecs;
|
||||
using Titano.Core.Windows.Interop;
|
||||
|
||||
namespace Titano.Core.Windows.Wic;
|
||||
|
||||
/// <summary>Enumerates installed WIC decoders through <c>IWICImagingFactory::CreateComponentEnumerator</c>.</summary>
|
||||
internal static class WicComponents
|
||||
{
|
||||
private const uint ComponentDecoder = 0x1;
|
||||
private const uint EnumerateRefresh = 0x1;
|
||||
private const uint EnumerateUnsigned = 0x40000000;
|
||||
|
||||
/// <summary>
|
||||
/// Every enabled decoder, signed or not (third-party camera codecs are often unsigned and WIC still uses them).
|
||||
/// <paramref name="refresh"/> makes WIC re-read the registrations, e.g. after a Store extension was installed.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<WicDecoderInfo> EnumerateDecoders(bool refresh)
|
||||
{
|
||||
Win32Com.EnsureInitialized();
|
||||
var decoders = new List<WicDecoderInfo>();
|
||||
var enumerator = WicFactory.Instance.CreateComponentEnumerator(ComponentDecoder, EnumerateUnsigned | (refresh ? EnumerateRefresh : 0));
|
||||
try
|
||||
{
|
||||
while (enumerator.Next(1, out var unknown, out var fetched) == 0 && fetched == 1)
|
||||
{
|
||||
object? component = null;
|
||||
try
|
||||
{
|
||||
component = Marshal.GetObjectForIUnknown(unknown);
|
||||
if (component is IWICBitmapDecoderInfo info) decoders.Add(Read(info));
|
||||
}
|
||||
catch (COMException ex)
|
||||
{
|
||||
Log.Warning("wic", "Skipped a WIC decoder whose registration could not be read", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.Release(unknown);
|
||||
Win32Com.Release(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Win32Com.Release(enumerator);
|
||||
}
|
||||
return decoders;
|
||||
}
|
||||
|
||||
/// <summary>Reads a component-info string through the "ask for the length, then fill" protocol.</summary>
|
||||
public static unsafe string ReadString(WicStringGetter getter)
|
||||
{
|
||||
if (getter(0, null, out var length) < 0 || length <= 1) return string.Empty;
|
||||
var buffer = new char[length];
|
||||
int hr;
|
||||
fixed (char* p = buffer)
|
||||
{
|
||||
hr = getter(length, p, out length);
|
||||
}
|
||||
if (hr < 0) return string.Empty;
|
||||
var text = buffer.AsSpan(0, (int)Math.Min(length, (uint)buffer.Length));
|
||||
var end = text.IndexOf('\0');
|
||||
return new string(end >= 0 ? text[..end] : text);
|
||||
}
|
||||
|
||||
// Lambdas call through the interface normally rather than binding delegates to COM methods.
|
||||
private static unsafe WicDecoderInfo Read(IWICBitmapDecoderInfo info) => new(
|
||||
FriendlyName: ReadString((uint capacity, char* buffer, out uint length) => info.GetFriendlyName(capacity, buffer, out length)),
|
||||
Clsid: info.GetCLSID(),
|
||||
ContainerFormat: info.GetContainerFormat(),
|
||||
Vendor: info.GetVendorGUID(),
|
||||
Version: ReadString((uint capacity, char* buffer, out uint length) => info.GetVersion(capacity, buffer, out length)),
|
||||
FileExtensions: WicDecoderInfo.ParseExtensions(
|
||||
ReadString((uint capacity, char* buffer, out uint length) => info.GetFileExtensions(capacity, buffer, out length))),
|
||||
Extension: null);
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Titano.Core.Windows.Interop;
|
||||
|
||||
namespace Titano.Core.Windows.Wic;
|
||||
|
||||
/// <summary>Process-wide WIC imaging factory (free-threaded; safe to call from any thread).</summary>
|
||||
internal static class WicFactory
|
||||
{
|
||||
private static readonly Lazy<IWICImagingFactory> Shared = new(Create);
|
||||
|
||||
public static IWICImagingFactory Instance => Shared.Value;
|
||||
|
||||
private static IWICImagingFactory Create()
|
||||
{
|
||||
// Create it in the MTA: a factory born on a UI thread's STA could make worker threads marshal every
|
||||
// call into that apartment.
|
||||
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
|
||||
return Task.Run(Create).GetAwaiter().GetResult();
|
||||
|
||||
var iid = typeof(IWICImagingFactory).GUID;
|
||||
var hr = Win32Com.TryCreateInstance(WicGuids.ImagingFactory2, iid, out var instance);
|
||||
// WICImagingFactory2 is missing only on Windows 7 without the platform update.
|
||||
if (hr == Win32Com.REGDB_E_CLASSNOTREG) hr = Win32Com.TryCreateInstance(WicGuids.ImagingFactory1, iid, out instance);
|
||||
Marshal.ThrowExceptionForHR(hr);
|
||||
return (IWICImagingFactory)instance!;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Titano.Core.Windows.Wic;
|
||||
|
||||
/// <summary>Class, vendor and pixel-format identifiers from wincodec.h.</summary>
|
||||
internal static class WicGuids
|
||||
{
|
||||
public static readonly Guid ImagingFactory1 = new("cacaf262-9370-4615-a13b-9f5539da4c0a");
|
||||
public static readonly Guid ImagingFactory2 = new("317d06e8-5f24-433d-bdf7-79ce68d8abc2");
|
||||
|
||||
/// <summary>CLSID_WICRAWDecoder: the camera RAW decoder (Raw Image Extension; also the CLSID of the older in-box codec).</summary>
|
||||
public static readonly Guid RawDecoder = new("41945702-8302-44a6-9445-ac98e8afa086");
|
||||
|
||||
/// <summary>CLSID_WICHeifDecoder (HEIF Image Extensions; decodes HEVC- and AV1-coded HEIF).</summary>
|
||||
public static readonly Guid HeifDecoder = new("e9a4a80a-44fe-4de4-8971-7150b10a5199");
|
||||
|
||||
/// <summary>CLSID_WICWebpDecoder (WebP Image Extensions).</summary>
|
||||
public static readonly Guid WebpDecoder = new("7693e886-51c9-4070-8419-9f70738ec8fa");
|
||||
|
||||
public static readonly Guid VendorMicrosoft = new("f0e749ca-edef-4589-a73a-ee0e626a2a2b");
|
||||
public static readonly Guid VendorMicrosoftBuiltIn = new("257a30fd-06b6-462b-aea4-63f70b86e533");
|
||||
|
||||
/// <summary>16-bit unsigned RGBA, sRGB-encoded like every WIC integer format.</summary>
|
||||
public static readonly Guid PixelFormat64bppRGBA = new("6fddc324-4e03-4bfe-b185-3d77768dc916");
|
||||
|
||||
/// <summary>32-bit float RGBA, scRGB (linear light, Rec.709 primaries) like every WIC float format.</summary>
|
||||
public static readonly Guid PixelFormat128bppRGBAFloat = new("6fddc324-4e03-4bfe-b185-3d77768dc919");
|
||||
}
|
||||
@@ -0,0 +1,354 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Titano.Core.Windows.Interop;
|
||||
|
||||
namespace Titano.Core.Windows.Wic;
|
||||
|
||||
// Windows Imaging Component interfaces, vtable order as in wincodec.h / objidl.h.
|
||||
// [ComImport] interfaces do not inherit vtable slots from their C# base, so every derived interface
|
||||
// redeclares the base methods (with `new`) in their native order before its own. Interfaces are declared
|
||||
// up to the last slot Titano calls (a vtable prefix is valid); intermediate slots Titano never calls keep
|
||||
// their native shape with pointer-sized placeholders for interfaces it does not model. Methods whose
|
||||
// failure is an expected outcome use [PreserveSig]; the others throw COMException on a failed HRESULT.
|
||||
|
||||
/// <summary>WICDecodeOptions.</summary>
|
||||
internal enum WicDecodeOptions
|
||||
{
|
||||
MetadataCacheOnDemand = 0,
|
||||
MetadataCacheOnLoad = 1,
|
||||
}
|
||||
|
||||
/// <summary>WICRect.</summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct WicRect
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
public int Width;
|
||||
public int Height;
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("ec5ec8a9-c395-4314-9c77-54d7a935ff70")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICImagingFactory
|
||||
{
|
||||
[PreserveSig]
|
||||
int CreateDecoderFromFilename(nint fileName, nint vendor, uint desiredAccess, WicDecodeOptions options, out nint decoder);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateDecoderFromStream(nint stream, nint vendor, WicDecodeOptions options, out IWICBitmapDecoder? decoder);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateDecoderFromFileHandle(nint file, nint vendor, WicDecodeOptions options, out nint decoder);
|
||||
|
||||
IWICComponentInfo CreateComponentInfo(ref Guid clsid);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateDecoder(ref Guid containerFormat, nint vendor, out nint decoder);
|
||||
|
||||
/// <summary>Returns an owned IWICBitmapEncoder pointer (used by the tests to synthesise JPEG fixtures).</summary>
|
||||
[PreserveSig]
|
||||
int CreateEncoder(ref Guid containerFormat, nint vendor, out nint encoder);
|
||||
|
||||
[PreserveSig]
|
||||
int CreatePalette(out nint palette);
|
||||
|
||||
IWICFormatConverter CreateFormatConverter();
|
||||
|
||||
IWICBitmapScaler CreateBitmapScaler();
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapClipper(out nint clipper);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapFlipRotator(out nint flipRotator);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateStream(out nint stream);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateColorContext(out nint colorContext);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateColorTransformer(out nint colorTransform);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmap(uint width, uint height, ref Guid pixelFormat, int cacheOption, out nint bitmap);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapFromSource(nint source, int cacheOption, out nint bitmap);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapFromSourceRect(nint source, uint x, uint y, uint width, uint height, out nint bitmap);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapFromMemory(uint width, uint height, ref Guid pixelFormat, uint stride, uint bufferSize, nint buffer, out nint bitmap);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapFromHBITMAP(nint bitmapHandle, nint paletteHandle, int alphaOption, out nint bitmap);
|
||||
|
||||
[PreserveSig]
|
||||
int CreateBitmapFromHICON(nint iconHandle, out nint bitmap);
|
||||
|
||||
IEnumUnknown CreateComponentEnumerator(uint componentTypes, uint options);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("00000100-0000-0000-c000-000000000046")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IEnumUnknown
|
||||
{
|
||||
/// <summary>S_OK when <paramref name="count"/> items were fetched, S_FALSE at the end.</summary>
|
||||
[PreserveSig]
|
||||
int Next(uint count, out nint element, out uint fetched);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("9edde9e7-8dee-47ea-99df-e6faf2ed44bf")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICBitmapDecoder
|
||||
{
|
||||
[PreserveSig]
|
||||
int QueryCapability(nint stream, out uint capability);
|
||||
|
||||
[PreserveSig]
|
||||
int Initialize(nint stream, WicDecodeOptions options);
|
||||
|
||||
Guid GetContainerFormat();
|
||||
|
||||
[PreserveSig]
|
||||
int GetDecoderInfo(out nint decoderInfo);
|
||||
|
||||
[PreserveSig]
|
||||
int CopyPalette(nint palette);
|
||||
|
||||
[PreserveSig]
|
||||
int GetMetadataQueryReader(out nint reader);
|
||||
|
||||
[PreserveSig]
|
||||
int GetPreview(out nint preview);
|
||||
|
||||
[PreserveSig]
|
||||
int GetColorContexts(uint count, nint colorContexts, out uint actualCount);
|
||||
|
||||
[PreserveSig]
|
||||
int GetThumbnail(out nint thumbnail);
|
||||
|
||||
uint GetFrameCount();
|
||||
|
||||
IWICBitmapFrameDecode GetFrame(uint index);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("00000120-a8f2-4877-ba0a-fd2b6645fb94")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICBitmapSource
|
||||
{
|
||||
void GetSize(out uint width, out uint height);
|
||||
|
||||
Guid GetPixelFormat();
|
||||
|
||||
void GetResolution(out double dpiX, out double dpiY);
|
||||
|
||||
[PreserveSig]
|
||||
int CopyPalette(nint palette);
|
||||
|
||||
void CopyPixels(ref WicRect rect, uint stride, uint bufferSize, nint buffer);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("3b16811b-6a43-4ec9-a813-3d930c13b940")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICBitmapFrameDecode : IWICBitmapSource
|
||||
{
|
||||
new void GetSize(out uint width, out uint height);
|
||||
|
||||
new Guid GetPixelFormat();
|
||||
|
||||
new void GetResolution(out double dpiX, out double dpiY);
|
||||
|
||||
[PreserveSig]
|
||||
new int CopyPalette(nint palette);
|
||||
|
||||
new void CopyPixels(ref WicRect rect, uint stride, uint bufferSize, nint buffer);
|
||||
|
||||
/// <summary>Fails (e.g. WINCODEC_ERR_UNSUPPORTEDOPERATION) for formats without metadata.</summary>
|
||||
[PreserveSig]
|
||||
int GetMetadataQueryReader(out IWICMetadataQueryReader? reader);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("00000301-a8f2-4877-ba0a-fd2b6645fb94")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICFormatConverter : IWICBitmapSource
|
||||
{
|
||||
new void GetSize(out uint width, out uint height);
|
||||
|
||||
new Guid GetPixelFormat();
|
||||
|
||||
new void GetResolution(out double dpiX, out double dpiY);
|
||||
|
||||
[PreserveSig]
|
||||
new int CopyPalette(nint palette);
|
||||
|
||||
new void CopyPixels(ref WicRect rect, uint stride, uint bufferSize, nint buffer);
|
||||
|
||||
void Initialize(IWICBitmapSource source, ref Guid destinationFormat, int dither, nint palette, double alphaThresholdPercent, int paletteTranslate);
|
||||
|
||||
void CanConvert(ref Guid sourceFormat, ref Guid destinationFormat, out int canConvert);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("00000302-a8f2-4877-ba0a-fd2b6645fb94")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICBitmapScaler : IWICBitmapSource
|
||||
{
|
||||
new void GetSize(out uint width, out uint height);
|
||||
|
||||
new Guid GetPixelFormat();
|
||||
|
||||
new void GetResolution(out double dpiX, out double dpiY);
|
||||
|
||||
[PreserveSig]
|
||||
new int CopyPalette(nint palette);
|
||||
|
||||
new void CopyPixels(ref WicRect rect, uint stride, uint bufferSize, nint buffer);
|
||||
|
||||
void Initialize(IWICBitmapSource source, uint width, uint height, int interpolationMode);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("30989668-e1c9-4597-b395-458eedb808df")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICMetadataQueryReader
|
||||
{
|
||||
Guid GetContainerFormat();
|
||||
|
||||
[PreserveSig]
|
||||
int GetLocation(uint capacity, nint buffer, out uint length);
|
||||
|
||||
/// <summary>Fails with WINCODEC_ERR_PROPERTYNOTFOUND when the query matches nothing.</summary>
|
||||
[PreserveSig]
|
||||
int GetMetadataByName([MarshalAs(UnmanagedType.LPWStr)] string name, ref PropVariant value);
|
||||
}
|
||||
|
||||
/// <summary>String getters of the component-info family: capacity 0 + null buffer returns the required length.</summary>
|
||||
internal unsafe delegate int WicStringGetter(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[ComImport]
|
||||
[Guid("23bc3f0a-698b-4357-886b-f24d50671334")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal unsafe interface IWICComponentInfo
|
||||
{
|
||||
uint GetComponentType();
|
||||
|
||||
Guid GetCLSID();
|
||||
|
||||
uint GetSigningStatus();
|
||||
|
||||
[PreserveSig]
|
||||
int GetAuthor(uint capacity, char* buffer, out uint length);
|
||||
|
||||
Guid GetVendorGUID();
|
||||
|
||||
[PreserveSig]
|
||||
int GetVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
int GetSpecVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
int GetFriendlyName(uint capacity, char* buffer, out uint length);
|
||||
}
|
||||
|
||||
/// <summary>IWICPixelFormatInfo2 (IWICComponentInfo → IWICPixelFormatInfo → IWICPixelFormatInfo2).</summary>
|
||||
[ComImport]
|
||||
[Guid("a9db33a2-af5f-43c7-b679-74f5984b5aa4")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal unsafe interface IWICPixelFormatInfo2 : IWICComponentInfo
|
||||
{
|
||||
new uint GetComponentType();
|
||||
|
||||
new Guid GetCLSID();
|
||||
|
||||
new uint GetSigningStatus();
|
||||
|
||||
[PreserveSig]
|
||||
new int GetAuthor(uint capacity, char* buffer, out uint length);
|
||||
|
||||
new Guid GetVendorGUID();
|
||||
|
||||
[PreserveSig]
|
||||
new int GetVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
new int GetSpecVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
new int GetFriendlyName(uint capacity, char* buffer, out uint length);
|
||||
|
||||
Guid GetFormatGUID();
|
||||
|
||||
[PreserveSig]
|
||||
int GetColorContext(out nint colorContext);
|
||||
|
||||
uint GetBitsPerPixel();
|
||||
|
||||
uint GetChannelCount();
|
||||
|
||||
[PreserveSig]
|
||||
int GetChannelMask(uint channel, uint maskSize, nint mask, out uint actualSize);
|
||||
|
||||
void SupportsTransparency(out int supportsTransparency);
|
||||
|
||||
/// <summary>WICPixelFormatNumericRepresentation: 2 unsigned integer, 4 fixed point, 5 float.</summary>
|
||||
uint GetNumericRepresentation();
|
||||
}
|
||||
|
||||
/// <summary>IWICBitmapDecoderInfo (IWICComponentInfo → IWICBitmapCodecInfo → IWICBitmapDecoderInfo).</summary>
|
||||
[ComImport]
|
||||
[Guid("d8cd007f-d08f-4191-9bfc-236ea7f0e4b5")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal unsafe interface IWICBitmapDecoderInfo : IWICComponentInfo
|
||||
{
|
||||
new uint GetComponentType();
|
||||
|
||||
new Guid GetCLSID();
|
||||
|
||||
new uint GetSigningStatus();
|
||||
|
||||
[PreserveSig]
|
||||
new int GetAuthor(uint capacity, char* buffer, out uint length);
|
||||
|
||||
new Guid GetVendorGUID();
|
||||
|
||||
[PreserveSig]
|
||||
new int GetVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
new int GetSpecVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
new int GetFriendlyName(uint capacity, char* buffer, out uint length);
|
||||
|
||||
Guid GetContainerFormat();
|
||||
|
||||
[PreserveSig]
|
||||
int GetPixelFormats(uint count, nint pixelFormats, out uint actualCount);
|
||||
|
||||
[PreserveSig]
|
||||
int GetColorManagementVersion(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
int GetDeviceManufacturer(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
int GetDeviceModels(uint capacity, char* buffer, out uint length);
|
||||
|
||||
[PreserveSig]
|
||||
int GetMimeTypes(uint capacity, char* buffer, out uint length);
|
||||
|
||||
/// <summary>Comma-separated list such as ".jpg,.jpeg,.jpe,.jfif".</summary>
|
||||
[PreserveSig]
|
||||
int GetFileExtensions(uint capacity, char* buffer, out uint length);
|
||||
}
|
||||
Reference in new issue
Block a user