using Chronos.Core.Imaging; using Chronos.Core.Windows.Wic; using Chronos.Testing; namespace Chronos.Core.Windows.Tests.Wic; [WindowsOnly] public sealed class WicDecoderProviderTests(TestContext context) { private static readonly Lazy 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.Native, Provider.TierFor(format), format.ToString()); Assert.Null(Provider.TierFor(ImageFileFormat.Unknown)); } /// /// A conversion on the desktop (owner review 2, R7) reads every format the managed decoders read natively through them, /// WIC after them, and a RAW for a 16-bit target never through WIC's Raw Image Extension (8 bits). /// [Test] public void AConversionReadsWithTheManagedDecodersFirstAndRawsAtSixteenBitsWithoutWic() { Assert.True(((IImageDecoderProvider)Provider).DevelopsRawAt8Bits); var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider); Assert.Equal("wic", desktop.Providers[0].Name); var jpeg = Chronos.Core.Conversion.FrameConverter.Decoders(desktop, new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg), ImageFileFormat.Cr2); Assert.SequenceEqual(["managed", "wic"], jpeg.Providers.Select(p => p.Name)); var tiff = Chronos.Core.Conversion.FrameConverter.Decoders(desktop, new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Tiff16), ImageFileFormat.Cr2); Assert.SequenceEqual(["managed"], tiff.Providers.Select(p => p.Name)); var heif = Chronos.Core.Conversion.FrameConverter.Decoders(desktop, new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Tiff16), ImageFileFormat.Heif); Assert.SequenceEqual(["managed", "wic"], heif.Providers.Select(p => p.Name)); } [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(() => 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}"); } } } }