Files
Chronos/tests/Titano.Core.Windows.Tests/Wic/WicDecoderProviderTests.cs
T
Claude 3cbf72610e feat(windows): add WIC decoder provider and codec inventory
WicDecoderProvider decodes through a pull chain (decoder, Fant scaler to
MaxLongEdge, format converter) read in bounded row bands. Integer sources are
requested as 64bppRGBA and linearised with the exact sRGB EOTF (Linear16);
float/fixed sources as 128bppRGBAFloat (scRGB, already linear). The Exif
orientation (photo policy, then raw IFD queries) is applied while
de-interleaving instead of through IWICBitmapFlipRotator, which Microsoft
documents as quadratic over scanline decoders.

CodecInventory enumerates installed decoders (CreateComponentEnumerator),
probes HEVC/AV1 decoders with MFTEnumEx and identifies the Raw Image
Extension by package family, since it shares the in-box RAW codec's CLSID.
It maps every ImageFileFormat to its decoder, native flag and the Store
extensions (product ids, ms-windows-store links) that would unlock it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 09:46:19 +00:00

164 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}");
}
}
}
}