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);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Windows.Codecs;
|
||||
using Titano.Core.Windows.Wic;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Codecs;
|
||||
|
||||
public sealed class CodecInventoryTests
|
||||
{
|
||||
private static readonly WicDecoderInfo Jpeg = Decoder("JPEG Decoder", Guid.NewGuid(), WicGuids.VendorMicrosoftBuiltIn, ".jpeg,.jpe,.jpg,.jfif,.exif");
|
||||
private static readonly WicDecoderInfo InBoxDng = Decoder("DNG Decoder", Guid.NewGuid(), WicGuids.VendorMicrosoftBuiltIn, ".dng");
|
||||
private static readonly WicDecoderInfo Raw = Decoder("Microsoft Raw Image Decoder", WicGuids.RawDecoder, WicGuids.VendorMicrosoft, ".arw,.cr2,.cr3,.dng,.nef,.raf");
|
||||
private static readonly WicDecoderInfo Heif = Decoder("HEIF Decoder", WicGuids.HeifDecoder, WicGuids.VendorMicrosoft, ".heic,.heif,.hif");
|
||||
private static readonly WicDecoderInfo Webp = Decoder("WebP Decoder", WicGuids.WebpDecoder, WicGuids.VendorMicrosoft, ".webp");
|
||||
|
||||
[Test]
|
||||
[WindowsOnly]
|
||||
public void DetectFindsTheBuiltInDecoders()
|
||||
{
|
||||
var inventory = CodecInventory.Detect();
|
||||
Assert.NotEmpty(inventory.Decoders);
|
||||
foreach (var format in new[] { ImageFileFormat.Jpeg, ImageFileFormat.Png, ImageFileFormat.Tiff })
|
||||
{
|
||||
var support = inventory.For(format);
|
||||
Assert.True(support.IsNative, $"{format} should decode natively through WIC");
|
||||
var decoder = Assert.NotNull(support.Decoder);
|
||||
Assert.False(string.IsNullOrWhiteSpace(decoder.FriendlyName), $"{format} decoder has no name");
|
||||
Assert.NotEqual(Guid.Empty, decoder.ContainerFormat);
|
||||
Assert.Empty(support.MissingExtensions);
|
||||
}
|
||||
Assert.Equal(ImageFormatInfo.Known.Count, inventory.Formats.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RawNeedsTheRawImageExtension()
|
||||
{
|
||||
var inventory = Inventory([Jpeg, InBoxDng, Classified(Raw, rawExtensionInstalled: false)]);
|
||||
var dng = inventory.For(ImageFileFormat.Dng);
|
||||
Assert.False(dng.IsNative);
|
||||
Assert.Equal<string?>("DNG Decoder", dng.Decoder?.FriendlyName);
|
||||
Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.RawImage)], dng.MissingExtensions);
|
||||
Assert.False(inventory.IsNative(ImageFileFormat.Cr3));
|
||||
Assert.Contains(StoreExtension.Get(StoreExtensionKind.RawImage), inventory.MissingExtensions);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RawIsNativeThroughTheExtension()
|
||||
{
|
||||
var inventory = Inventory([Jpeg, InBoxDng, Classified(Raw, rawExtensionInstalled: true)]);
|
||||
Assert.True(inventory.IsNative(ImageFileFormat.Dng));
|
||||
Assert.Equal<StoreExtensionKind?>(StoreExtensionKind.RawImage, inventory.For(ImageFileFormat.Dng).Decoder?.Extension);
|
||||
Assert.True(inventory.IsNative(ImageFileFormat.Cr3));
|
||||
// Installed but without this extension registered: nothing to install, still not native.
|
||||
var gpr = inventory.For(ImageFileFormat.Gpr);
|
||||
Assert.False(gpr.IsNative);
|
||||
Assert.Empty(gpr.MissingExtensions);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HeifNeedsAnHevcDecoder()
|
||||
{
|
||||
var withoutHevc = Inventory([Jpeg, Classified(Heif)]).For(ImageFileFormat.Heif);
|
||||
Assert.False(withoutHevc.IsNative);
|
||||
Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.HevcVideo)], withoutHevc.MissingExtensions);
|
||||
|
||||
Assert.True(Inventory([Jpeg, Classified(Heif)], hevc: true).IsNative(ImageFileFormat.Heif));
|
||||
|
||||
var nothing = Inventory([Jpeg]).For(ImageFileFormat.Heif);
|
||||
Assert.SequenceEqual(
|
||||
[StoreExtension.Get(StoreExtensionKind.HeifImage), StoreExtension.Get(StoreExtensionKind.HevcVideo)],
|
||||
nothing.MissingExtensions);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AvifUsesTheHeifDecoderWithAnAv1Decoder()
|
||||
{
|
||||
var withoutAv1 = Inventory([Classified(Heif)], hevc: true).For(ImageFileFormat.Avif);
|
||||
Assert.False(withoutAv1.IsNative);
|
||||
Assert.Equal<string?>("HEIF Decoder", withoutAv1.Decoder?.FriendlyName);
|
||||
Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.Av1Video)], withoutAv1.MissingExtensions);
|
||||
|
||||
Assert.True(Inventory([Classified(Heif)], av1: true).IsNative(ImageFileFormat.Avif));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WebpNeedsTheWebpExtension()
|
||||
{
|
||||
Assert.SequenceEqual([StoreExtension.Get(StoreExtensionKind.WebpImage)], Inventory([Jpeg]).For(ImageFileFormat.WebP).MissingExtensions);
|
||||
Assert.True(Inventory([Jpeg, Classified(Webp)]).IsNative(ImageFileFormat.WebP));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BuiltInFormatsNeverAskForExtensions()
|
||||
{
|
||||
var inventory = Inventory([Jpeg]);
|
||||
Assert.True(inventory.IsNative(ImageFileFormat.Jpeg));
|
||||
Assert.False(inventory.IsNative(ImageFileFormat.Png));
|
||||
Assert.Empty(inventory.For(ImageFileFormat.Png).MissingExtensions);
|
||||
Assert.False(inventory.IsNative(ImageFileFormat.Unknown));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ClassificationUsesClsidPackageAndVendor()
|
||||
{
|
||||
Assert.Null(CodecInventory.Classify(Raw, rawExtensionInstalled: false));
|
||||
Assert.Equal<StoreExtensionKind?>(StoreExtensionKind.RawImage, CodecInventory.Classify(Raw, rawExtensionInstalled: true));
|
||||
Assert.Equal<StoreExtensionKind?>(StoreExtensionKind.HeifImage, CodecInventory.Classify(Heif, false));
|
||||
Assert.Equal<StoreExtensionKind?>(StoreExtensionKind.WebpImage, CodecInventory.Classify(Webp, false));
|
||||
var avif = Decoder("AVIF Decoder", Guid.NewGuid(), WicGuids.VendorMicrosoft, ".avif");
|
||||
Assert.Equal<StoreExtensionKind?>(StoreExtensionKind.Av1Video, CodecInventory.Classify(avif, false));
|
||||
var thirdParty = Decoder("Other AVIF", Guid.NewGuid(), Guid.NewGuid(), ".avif");
|
||||
Assert.Null(CodecInventory.Classify(thirdParty, false));
|
||||
Assert.Null(CodecInventory.Classify(Jpeg, false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StoreLinksUseTheProductIds()
|
||||
{
|
||||
Assert.Equal(5, StoreExtension.All.Count);
|
||||
var raw = StoreExtension.Get(StoreExtensionKind.RawImage);
|
||||
Assert.Equal("ms-windows-store://pdp/?ProductId=9NCTDW2W1BH8", raw.StoreUri.AbsoluteUri);
|
||||
Assert.Equal("https://apps.microsoft.com/detail/9nctdw2w1bh8", raw.WebUri.AbsoluteUri);
|
||||
Assert.Equal("9PMMSR1CGPWG", StoreExtension.Get(StoreExtensionKind.HeifImage).ProductId);
|
||||
Assert.Equal("9NMZLZ57R3T7", StoreExtension.Get(StoreExtensionKind.HevcVideo).ProductId);
|
||||
Assert.Equal("9MVZQVXJBQ9V", StoreExtension.Get(StoreExtensionKind.Av1Video).ProductId);
|
||||
Assert.Equal("9PG2DK419DRG", StoreExtension.Get(StoreExtensionKind.WebpImage).ProductId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ExtensionListsAreNormalised()
|
||||
{
|
||||
Assert.SequenceEqual(["jpg", "jpeg", "jfif"], WicDecoderInfo.ParseExtensions(".JPG, .jpeg;.jfif,,.jpg"));
|
||||
Assert.True(Jpeg.Handles(".JPG"));
|
||||
Assert.True(Jpeg.Handles("jfif"));
|
||||
Assert.False(Jpeg.Handles("png"));
|
||||
}
|
||||
|
||||
private static CodecInventory Inventory(WicDecoderInfo[] decoders, bool hevc = false, bool av1 = false) => new(decoders, hevc, av1);
|
||||
|
||||
private static WicDecoderInfo Classified(WicDecoderInfo decoder, bool rawExtensionInstalled = false) =>
|
||||
decoder with { Extension = CodecInventory.Classify(decoder, rawExtensionInstalled) };
|
||||
|
||||
private static WicDecoderInfo Decoder(string name, Guid clsid, Guid vendor, string extensions) =>
|
||||
new(name, clsid, Guid.NewGuid(), vendor, "1.0", WicDecoderInfo.ParseExtensions(extensions), null);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Titano.Core.Windows.Wic;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Wic;
|
||||
|
||||
public sealed class OrientationMapTests
|
||||
{
|
||||
[Theory]
|
||||
[Case(1)]
|
||||
[Case(2)]
|
||||
[Case(3)]
|
||||
[Case(4)]
|
||||
[Case(5)]
|
||||
[Case(6)]
|
||||
[Case(7)]
|
||||
[Case(8)]
|
||||
public void MatchesTheExifDefinition(int orientation)
|
||||
{
|
||||
const int width = 5, height = 3;
|
||||
var map = OrientationMap.Create(orientation, width, height);
|
||||
var swapped = OrientationMap.SwapsAxes(orientation);
|
||||
Assert.Equal(swapped ? height : width, map.Width);
|
||||
Assert.Equal(swapped ? width : height, map.Height);
|
||||
|
||||
var seen = new HashSet<int>();
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
var (dx, dy) = Reference(orientation, x, y, width, height);
|
||||
Assert.Equal((dy * map.Width) + dx, map.IndexOf(x, y), $"orientation {orientation}, pixel ({x},{y})");
|
||||
seen.Add(map.IndexOf(x, y));
|
||||
}
|
||||
}
|
||||
Assert.Equal(width * height, seen.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnknownValuesActAsIdentity() => Assert.Equal(OrientationMap.Create(1, 4, 2), OrientationMap.Create(0, 4, 2));
|
||||
|
||||
[Theory]
|
||||
[Case(4000u, 3000u, 1920, 1920u, 1440u)]
|
||||
[Case(400u, 100u, 100, 100u, 25u)]
|
||||
[Case(3000u, 4000u, 1000, 750u, 1000u)]
|
||||
[Case(640u, 480u, 0, 640u, 480u)]
|
||||
[Case(640u, 480u, 2000, 640u, 480u)]
|
||||
[Case(2u, 1000u, 10, 1u, 10u)]
|
||||
public void TargetSizeFitsTheLongEdge(uint width, uint height, int maxLongEdge, uint expectedWidth, uint expectedHeight)
|
||||
{
|
||||
var (w, h) = WicDecoderProvider.TargetSize(width, height, maxLongEdge);
|
||||
Assert.Equal(expectedWidth, w);
|
||||
Assert.Equal(expectedHeight, h);
|
||||
}
|
||||
|
||||
/// <summary>Displayed position of stored pixel (x, y), straight from the Exif 2.3 orientation table.</summary>
|
||||
private static (int X, int Y) Reference(int orientation, int x, int y, int w, int h) => orientation switch
|
||||
{
|
||||
2 => (w - 1 - x, y),
|
||||
3 => (w - 1 - x, h - 1 - y),
|
||||
4 => (x, h - 1 - y),
|
||||
5 => (y, x),
|
||||
6 => (h - 1 - y, x),
|
||||
7 => (h - 1 - y, w - 1 - x),
|
||||
8 => (y, w - 1 - x),
|
||||
_ => (x, y),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Core.Windows.Wic;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Wic;
|
||||
|
||||
[WindowsOnly]
|
||||
public sealed class WicDecoderProviderTests(TestContext context)
|
||||
{
|
||||
private static readonly Lazy<WicDecoderProvider> SharedProvider = new(() => new WicDecoderProvider());
|
||||
private static readonly double MidGrey = ColorSpace.SrgbToLinear(128 / 255.0);
|
||||
|
||||
private static WicDecoderProvider Provider => SharedProvider.Value;
|
||||
|
||||
[Test]
|
||||
public void BuiltInFormatsAreNative()
|
||||
{
|
||||
Assert.Equal("wic", Provider.Name);
|
||||
foreach (var format in new[] { ImageFileFormat.Jpeg, ImageFileFormat.Png, ImageFileFormat.Tiff, ImageFileFormat.Bmp, ImageFileFormat.Gif })
|
||||
Assert.Equal<DecodeTier?>(DecodeTier.Native, Provider.TierFor(format), format.ToString());
|
||||
Assert.Null(Provider.TierFor(ImageFileFormat.Unknown));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PngMidGreyDecodesToLinearLight()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "grey.png");
|
||||
WicTestImages.WriteRgbPng(path, 64, 48, (_, _) => (128, 128, 128));
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path)));
|
||||
Assert.Equal(DecodeTier.Native, image.Tier);
|
||||
Assert.Equal("wic", image.Decoder);
|
||||
Assert.Equal(64, image.Pixels.Width);
|
||||
Assert.Equal(48, image.Pixels.Height);
|
||||
Assert.Equal(3, image.Pixels.Channels);
|
||||
Assert.Equal(64, image.SourceWidth);
|
||||
Assert.Equal(48, image.SourceHeight);
|
||||
AssertUniform(image.Pixels, MidGrey, 1e-3);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PngChannelsKeepTheirOrder()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "red-blue.png");
|
||||
WicTestImages.WriteRgbPng(path, 32, 16, (x, _) => x < 16 ? ((byte)255, (byte)0, (byte)0) : ((byte)0, (byte)0, (byte)255));
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path)));
|
||||
var p = image.Pixels;
|
||||
Assert.Approximately(1.0, p[0, 4, 8], 1e-4);
|
||||
Assert.Approximately(0.0, p[1, 4, 8], 1e-4);
|
||||
Assert.Approximately(0.0, p[2, 4, 8], 1e-4);
|
||||
Assert.Approximately(0.0, p[0, 28, 8], 1e-4);
|
||||
Assert.Approximately(1.0, p[2, 28, 8], 1e-4);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SixteenBitPngKeepsPrecision()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "grey16.png");
|
||||
WicTestImages.WriteGray16Png(path, 8, 8, 0x8000);
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path)));
|
||||
AssertUniform(image.Pixels, ColorSpace.Linear16(0x8000), 1e-4);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void JpegMidGreyDecodesToLinearLight()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "grey.jpg");
|
||||
WicTestImages.WriteGrayJpeg(path, 64, 48, (_, _) => 128);
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path)));
|
||||
Assert.Equal(64, image.Pixels.Width);
|
||||
Assert.Equal(48, image.Pixels.Height);
|
||||
AssertUniform(image.Pixels, MidGrey, 1e-3);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ScalingToMaxLongEdgeKeepsAspect()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "wide.png");
|
||||
WicTestImages.WriteRgbPng(path, 400, 100, (_, _) => (128, 128, 128));
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path, MaxLongEdge: 100)));
|
||||
Assert.Equal(100, image.Pixels.Width);
|
||||
Assert.Equal(25, image.Pixels.Height);
|
||||
Assert.Equal(400, image.SourceWidth);
|
||||
Assert.Equal(100, image.SourceHeight);
|
||||
AssertUniform(image.Pixels, MidGrey, 2e-3);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Orientation6RotatesClockwise()
|
||||
{
|
||||
// Stored 64×32 with a white 16×16 block at the top-left; displayed 32×64 with the block at the top-right.
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "rotated.jpg");
|
||||
WicTestImages.WriteGrayJpeg(path, 64, 32, (x, y) => x < 16 && y < 16 ? (byte)255 : (byte)0, exifOrientation: 6);
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path)));
|
||||
Assert.Equal(32, image.Pixels.Width);
|
||||
Assert.Equal(64, image.Pixels.Height);
|
||||
Assert.Equal(32, image.SourceWidth);
|
||||
Assert.Equal(64, image.SourceHeight);
|
||||
Assert.Greater(image.Pixels[1, 24, 8], 0.8);
|
||||
Assert.Less(image.Pixels[1, 8, 8], 0.05);
|
||||
Assert.Less(image.Pixels[1, 24, 40], 0.05);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OrientationIsIgnoredWhenNotRequested()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "rotated.jpg");
|
||||
WicTestImages.WriteGrayJpeg(path, 64, 32, (x, y) => x < 16 && y < 16 ? (byte)255 : (byte)0, exifOrientation: 6);
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path, ApplyOrientation: false)));
|
||||
Assert.Equal(64, image.Pixels.Width);
|
||||
Assert.Equal(32, image.Pixels.Height);
|
||||
Assert.Greater(image.Pixels[1, 8, 8], 0.8);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DecodesAFileAnotherProcessHasOpenForWriting()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "busy.png");
|
||||
WicTestImages.WriteRgbPng(path, 16, 16, (_, _) => (128, 128, 128));
|
||||
using var writer = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
|
||||
|
||||
using var image = Assert.NotNull(Provider.Decode(new DecodeRequest(path)));
|
||||
Assert.Equal(16, image.Pixels.Width);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UnrecognisedContentReturnsNull()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "noise.png");
|
||||
File.WriteAllBytes(path, Enumerable.Range(0, 4096).Select(i => (byte)(i * 37)).ToArray());
|
||||
Assert.Null(Provider.Decode(new DecodeRequest(path)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MissingFileThrowsFileNotFound()
|
||||
{
|
||||
var path = Path.Combine(context.CreateTempDirectory(), "missing.png");
|
||||
Assert.Throws<FileNotFoundException>(() => Provider.Decode(new DecodeRequest(path)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ForcedPreviewIsLeftToOtherProviders() =>
|
||||
Assert.Null(Provider.Decode(new DecodeRequest(Path.Combine(context.CreateTempDirectory(), "any.jpg"), ForcePreview: true)));
|
||||
|
||||
private static void AssertUniform(ImageBuffer image, double expected, double tolerance)
|
||||
{
|
||||
for (var c = 0; c < image.Channels; c++)
|
||||
{
|
||||
var plane = image.Plane(c);
|
||||
for (var i = 0; i < plane.Length; i++)
|
||||
{
|
||||
if (Math.Abs(plane[i] - expected) > tolerance)
|
||||
Assert.Fail($"channel {c} pixel {i}: expected {expected:F5} ± {tolerance}, actual {plane[i]:F5}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Runtime.InteropServices;
|
||||
using Titano.Core.Codecs.Png;
|
||||
using Titano.Core.Windows.Interop;
|
||||
using Titano.Core.Windows.Wic;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Wic;
|
||||
|
||||
/// <summary>Synthetic fixtures: PNG through the managed encoder, JPEG through WIC's own encoder.</summary>
|
||||
internal static unsafe partial class WicTestImages
|
||||
{
|
||||
private static readonly Guid JpegContainer = new("19e4a5aa-5662-4fc5-a0c0-1758028e1057");
|
||||
private static readonly Guid PixelFormat8bppGray = new("6fddc324-4e03-4bfe-b185-3d77768dc908");
|
||||
|
||||
private const uint STGM_WRITE = 0x1, STGM_SHARE_EXCLUSIVE = 0x10, STGM_CREATE = 0x1000;
|
||||
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
|
||||
private const int WICBitmapEncoderNoCache = 2;
|
||||
|
||||
public static void WriteRgbPng(string path, int width, int height, Func<int, int, (byte R, byte G, byte B)> pixel)
|
||||
{
|
||||
var data = new byte[width * height * 3];
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
var (r, g, b) = pixel(x, y);
|
||||
var i = ((y * width) + x) * 3;
|
||||
data[i] = r;
|
||||
data[i + 1] = g;
|
||||
data[i + 2] = b;
|
||||
}
|
||||
}
|
||||
File.WriteAllBytes(path, PngEncoder.Encode(data, width, height, PngColorType.Rgb));
|
||||
}
|
||||
|
||||
public static void WriteGray16Png(string path, int width, int height, ushort value)
|
||||
{
|
||||
var data = new byte[width * height * 2];
|
||||
for (var i = 0; i < data.Length; i += 2) BinaryPrimitives.WriteUInt16BigEndian(data.AsSpan(i), value);
|
||||
File.WriteAllBytes(path, PngEncoder.Encode(data, width, height, PngColorType.Gray, bitDepth: 16));
|
||||
}
|
||||
|
||||
/// <summary>Grayscale baseline JPEG from WIC's encoder; optionally tagged with an Exif orientation.</summary>
|
||||
public static void WriteGrayJpeg(string path, int width, int height, Func<int, int, byte> gray, int? exifOrientation = null)
|
||||
{
|
||||
var pixels = new byte[width * height];
|
||||
for (var y = 0; y < height; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++) pixels[(y * width) + x] = gray(x, y);
|
||||
}
|
||||
|
||||
var container = JpegContainer;
|
||||
Marshal.ThrowExceptionForHR(WicFactory.Instance.CreateEncoder(ref container, 0, out var encoderPointer));
|
||||
IWICBitmapEncoder encoder;
|
||||
try
|
||||
{
|
||||
encoder = (IWICBitmapEncoder)Marshal.GetObjectForIUnknown(encoderPointer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Marshal.Release(encoderPointer);
|
||||
}
|
||||
|
||||
Marshal.ThrowExceptionForHR(SHCreateStreamOnFileEx(path, STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE, FILE_ATTRIBUTE_NORMAL, 1, 0, out var stream));
|
||||
IWICBitmapFrameEncode? frame = null;
|
||||
nint options = 0;
|
||||
try
|
||||
{
|
||||
encoder.Initialize(stream, WICBitmapEncoderNoCache);
|
||||
encoder.CreateNewFrame(out frame, out options);
|
||||
frame.Initialize(options);
|
||||
frame.SetSize((uint)width, (uint)height);
|
||||
var format = PixelFormat8bppGray;
|
||||
frame.SetPixelFormat(ref format);
|
||||
if (format != PixelFormat8bppGray) throw new InvalidOperationException("The WIC JPEG encoder did not accept 8bppGray.");
|
||||
fixed (byte* p = pixels) frame.WritePixels((uint)height, (uint)width, (uint)pixels.Length, (nint)p);
|
||||
frame.Commit();
|
||||
encoder.Commit();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Win32Com.Release(frame);
|
||||
if (options != 0) Marshal.Release(options);
|
||||
Win32Com.Release(encoder);
|
||||
Marshal.Release(stream);
|
||||
}
|
||||
|
||||
if (exifOrientation is { } orientation) InsertExifOrientation(path, orientation);
|
||||
}
|
||||
|
||||
/// <summary>Adds an APP1 Exif segment holding only IFD0 Orientation (after APP0 when present, as cameras do).</summary>
|
||||
private static void InsertExifOrientation(string path, int orientation)
|
||||
{
|
||||
var jpeg = File.ReadAllBytes(path);
|
||||
byte[] tiff =
|
||||
[
|
||||
(byte)'I', (byte)'I', 42, 0, 8, 0, 0, 0, // little-endian header, IFD0 at offset 8
|
||||
1, 0, // one entry
|
||||
0x12, 0x01, 3, 0, 1, 0, 0, 0, (byte)orientation, 0, 0, 0, // 0x0112 Orientation, SHORT, count 1
|
||||
0, 0, 0, 0, // no next IFD
|
||||
];
|
||||
var insertAt = 2;
|
||||
if (jpeg[2] == 0xFF && jpeg[3] == 0xE0) insertAt = 4 + BinaryPrimitives.ReadUInt16BigEndian(jpeg.AsSpan(4));
|
||||
var segmentLength = 2 + 6 + tiff.Length;
|
||||
using var output = new MemoryStream();
|
||||
output.Write(jpeg, 0, insertAt);
|
||||
output.Write([0xFF, 0xE1, (byte)(segmentLength >> 8), (byte)segmentLength]);
|
||||
output.Write("Exif\0\0"u8);
|
||||
output.Write(tiff);
|
||||
output.Write(jpeg, insertAt, jpeg.Length - insertAt);
|
||||
File.WriteAllBytes(path, output.ToArray());
|
||||
}
|
||||
|
||||
[LibraryImport("shlwapi.dll", StringMarshalling = StringMarshalling.Utf16)]
|
||||
private static partial int SHCreateStreamOnFileEx(string file, uint mode, uint attributes, int create, nint template, out nint stream);
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("00000103-a8f2-4877-ba0a-fd2b6645fb94")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICBitmapEncoder
|
||||
{
|
||||
void Initialize(nint stream, int cacheOption);
|
||||
|
||||
Guid GetContainerFormat();
|
||||
|
||||
void GetEncoderInfo(out nint encoderInfo);
|
||||
|
||||
void SetColorContexts(uint count, nint colorContexts);
|
||||
|
||||
void SetPalette(nint palette);
|
||||
|
||||
void SetThumbnail(nint thumbnail);
|
||||
|
||||
void SetPreview(nint preview);
|
||||
|
||||
void CreateNewFrame(out IWICBitmapFrameEncode frame, out nint encoderOptions);
|
||||
|
||||
void Commit();
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("00000105-a8f2-4877-ba0a-fd2b6645fb94")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
internal interface IWICBitmapFrameEncode
|
||||
{
|
||||
void Initialize(nint encoderOptions);
|
||||
|
||||
void SetSize(uint width, uint height);
|
||||
|
||||
void SetResolution(double dpiX, double dpiY);
|
||||
|
||||
void SetPixelFormat(ref Guid pixelFormat);
|
||||
|
||||
void SetColorContexts(uint count, nint colorContexts);
|
||||
|
||||
void SetPalette(nint palette);
|
||||
|
||||
void SetThumbnail(nint thumbnail);
|
||||
|
||||
void WritePixels(uint lineCount, uint stride, uint bufferSize, nint pixels);
|
||||
|
||||
void WriteSource(nint source, nint rect);
|
||||
|
||||
void Commit();
|
||||
}
|
||||
Reference in new issue
Block a user