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)); } /// /// An untagged CMYK TIFF, which the managed reader refuses rather than read as the gray of its first ink, is planned and /// converted through WIC on the desktop (owner review 2, R7): its red stays red in the converted file. /// [Test] public void ACmykTiffConvertsThroughWicInItsColours() { var path = Path.Combine(context.CreateTempDirectory(), "cmyk.tif"); File.WriteAllBytes(path, CmykTiff(6, 4, [0, 255, 255, 0])); var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider); var jpeg = new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg); var probe = Chronos.Core.Conversion.FrameConverter.Decoders(desktop, jpeg, ImageFileFormat.Tiff).Probe(path, ImageFileFormat.Tiff); Assert.Equal((DecodeTier.Native, "wic"), (probe.Tier, probe.Decoder)); var frame = new Chronos.Core.Archiving.SidecarFrame { Id = Guid.NewGuid(), File = "originals/cmyk.tif", Sha256 = new string('a', 64), W = 6, H = 4, Format = ImageFileFormat.Tiff, }; var target = Path.Combine(context.CreateTempDirectory(), "converted.jpg"); var file = Chronos.Core.Conversion.FrameConverter.Convert(new Chronos.Core.Conversion.ConvertSource(path, frame), jpeg, desktop, target, cancellationToken: context.Cancellation); using var converted = Assert.NotNull(Chronos.Core.Conversion.FrameConverter.Verifier.Decode(new DecodeRequest(file.Path, AllowPreview: false))); Assert.Greater(converted.Pixels[0, 3, 2], 0.5); Assert.Less(converted.Pixels[1, 3, 2], 0.1); Assert.Less(converted.Pixels[2, 3, 2], 0.1); } /// /// A TIFF WIC would decode as black or as noise without an error (owner review 2, R7) is left to the other decoders: a /// compression it does not know (ZSTD, an unknown code), whose data it decodes as zeros; Photoshop's floating-point /// predictor, which it does not undo; 32-bit integer samples, which it takes for floats. On the desktop the managed /// reader decodes the predicted float TIFF to its values, and the others are unavailable, never black; a conversion's /// plan leaves those out and converts the float one. /// [Test] public void ATiffWicWouldDecodeBlackOrAsNoiseIsLeftToTheOtherDecoders() { var folder = context.CreateTempDirectory(); using var scene = Scene(12, 8); (string Name, byte[] File, string Why)[] refused = [ ("zstd.tif", TestTiffs.Rgb8(scene, compression: 50000), "compression 50000"), ("unknown.tif", TestTiffs.Rgb8(scene, compression: 12345), "compression 12345"), ("jpeg2000.tif", TestTiffs.Rgb8(scene, compression: 34712), "compression 34712"), ("predicted.tif", TestTiffs.FloatRgb(scene, predicted: true), "predictor 3"), ("int32.tif", TestTiffs.Uint32Rgb(scene), "32-bit integer"), ]; var reading = Provider.WithHeaderProbes(); foreach (var (name, bytes, why) in refused) { var path = Path.Combine(folder, name); File.WriteAllBytes(path, bytes); Assert.Null(Provider.Decode(new DecodeRequest(path))); var probe = reading.Probe(path, ImageFileFormat.Tiff); Assert.Equal(DecodeTier.Unavailable, probe.Tier, name); Assert.Contains(why, probe.Reason); } // What WIC decodes of the same kind it still does: the float TIFF unpredicted, the codes uncompressed. File.WriteAllBytes(Path.Combine(folder, "float.tif"), TestTiffs.FloatRgb(scene)); File.WriteAllBytes(Path.Combine(folder, "plain.tif"), TestTiffs.Rgb8(scene)); foreach (var name in (string[])["float.tif", "plain.tif"]) { using var decoded = Assert.NotNull(Provider.Decode(new DecodeRequest(Path.Combine(folder, name)))); AssertClose(scene, decoded.Pixels, name == "float.tif" ? 1e-6 : 5e-3, name); } var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider); using (var floats = Assert.NotNull(desktop.Decode(new DecodeRequest(Path.Combine(folder, "predicted.tif"))))) { Assert.Equal("managed-tiff", floats.Decoder); AssertClose(scene, floats.Pixels, 1e-6, "the predicted float TIFF"); } foreach (var name in (string[])["zstd.tif", "unknown.tif", "jpeg2000.tif", "int32.tif"]) Assert.Null(desktop.Decode(new DecodeRequest(Path.Combine(folder, name)))); var frames = refused.Select((f, i) => new Chronos.Core.Archiving.SidecarFrame { Id = Guid.NewGuid(), I = i, File = f.Name, Sha256 = new string('a', 64), W = 12, H = 8, Format = ImageFileFormat.Tiff, }).ToArray(); var archive = new Chronos.Core.Archiving.ArchivePaths(context.CreateTempDirectory()).Sequence("roof"); Directory.CreateDirectory(archive.Folder); foreach (var (name, bytes, _) in refused) File.WriteAllBytes(archive.File(name), bytes); var manifest = new Chronos.Core.Archiving.SequenceManifest { Id = Guid.NewGuid(), Frames = frames }; var plan = Chronos.Core.Conversion.ConvertPlanner.Plan(manifest, archive, [.. frames.Select(f => f.File)], new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg), desktop, "Roof"); Assert.SequenceEqual([3], plan.Frames.Select(f => f.Index)); Assert.SequenceEqual([(Chronos.Core.Conversion.ConvertSkip.NoNativeDecoder, 4)], plan.Skipped.Select(s => (s.Reason, s.Frames))); } /// /// A conversion's WIC probe reads what WIC makes of the file's headers (owner review 2, R7): native at the first frame's /// oriented size, so the plan's size bound applies to what only WIC reads; unavailable, with why, for what it does not /// decode. Other probes trust the format and open nothing. A file another program holds, or one gone, is a read error, /// never a verdict on its content. /// [Test] public void HeaderProbesSayWhatWicDecodesAtItsSize() { var folder = context.CreateTempDirectory(); var reading = Provider.WithHeaderProbes(); Assert.True(((WicDecoderProvider)reading).ReadsHeaders); Assert.Same(reading, reading.WithHeaderProbes()); Assert.False(Provider.ReadsHeaders); var rotated = Path.Combine(folder, "rotated.jpg"); WicTestImages.WriteGrayJpeg(rotated, 64, 32, (x, _) => (byte)x, exifOrientation: 6); var png = Path.Combine(folder, "frame.png"); WicTestImages.WriteRgbPng(png, 40, 30, (_, _) => (128, 128, 128)); var tiff = Path.Combine(folder, "frame.tif"); using (var scene = Scene(12, 8)) File.WriteAllBytes(tiff, Chronos.Core.Codecs.Tiff.TiffWriter.Write(scene)); foreach (var (path, format, width, height) in ((string, ImageFileFormat, int, int)[]) [(rotated, ImageFileFormat.Jpeg, 32, 64), (png, ImageFileFormat.Png, 40, 30), (tiff, ImageFileFormat.Tiff, 12, 8)]) { var probe = reading.Probe(path, format); Assert.Equal((DecodeTier.Native, "wic", width, height), (probe.Tier, probe.Decoder, probe.Width, probe.Height), Path.GetFileName(path)); } // Without header reads: the format's tier, nothing opened. var trusted = Provider.Probe(Path.Combine(folder, "none.jpg"), ImageFileFormat.Jpeg); Assert.Equal((DecodeTier.Native, 0, 0), (trusted.Tier, trusted.Width, trusted.Height)); var noise = Path.Combine(folder, "noise.png"); File.WriteAllBytes(noise, [.. Enumerable.Range(0, 4096).Select(i => (byte)(i * 37))]); Assert.Equal(DecodeTier.Unavailable, reading.Probe(noise, ImageFileFormat.Png).Tier); Assert.Throws(() => reading.Probe(Path.Combine(folder, "gone.png"), ImageFileFormat.Png)); using (new FileStream(png, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) Assert.Throws(() => reading.Probe(png, ImageFileFormat.Png)); // A CMYK strip only WIC reads, 70000 pixels wide, whose sidecar records no size: too large for a JPEG, said by the plan // rather than by a conversion failing at every retry. var archive = new Chronos.Core.Archiving.ArchivePaths(context.CreateTempDirectory()).Sequence("roof"); Directory.CreateDirectory(archive.Folder); File.WriteAllBytes(archive.File("wide.tif"), CmykTiff(70_000, 1, [0, 255, 255, 0])); var sized = reading.Probe(archive.File("wide.tif"), ImageFileFormat.Tiff); Assert.Equal((DecodeTier.Native, 70_000, 1), (sized.Tier, sized.Width, sized.Height)); var frame = new Chronos.Core.Archiving.SidecarFrame { Id = Guid.NewGuid(), File = "wide.tif", Sha256 = new string('a', 64), Format = ImageFileFormat.Tiff }; var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider); Assert.Equal(Chronos.Core.Conversion.ConvertSkip.TooLarge, Chronos.Core.Conversion.ConvertPlanner.Skip(frame, archive, new HashSet(["wide.tif"]), new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg), desktop)); Assert.Null(Chronos.Core.Conversion.ConvertPlanner.Skip(frame, archive, new HashSet(["wide.tif"]), new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.LinearDng), desktop)); } /// A gradient with saturated patches, linear light, every value between 0 and 1. internal static ImageBuffer Scene(int width, int height) { var image = new ImageBuffer(width, height); for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { image[0, x, y] = 0.02f + 0.7f * x / width; image[1, x, y] = 0.05f + 0.5f * y / height; image[2, x, y] = 0.1f + 0.3f * ((x / 4 + y / 4) % 2); } } (image[0, 0, 0], image[1, 0, 0], image[2, 0, 0]) = (0.02f, 0.9f, 0.02f); (image[0, 1, 0], image[1, 1, 0], image[2, 1, 0]) = (0.9f, 0.02f, 0.02f); (image[0, 2, 0], image[1, 2, 0], image[2, 2, 0]) = (0.001f, 0.004f, 0.0005f); return image; } private static void AssertClose(ImageBuffer expected, ImageBuffer actual, double tolerance, string what) { Assert.Equal((expected.Width, expected.Height), (actual.Width, actual.Height), what); for (var c = 0; c < 3; c++) { for (var i = 0; i < expected.PlaneLength; i++) Assert.Approximately(expected.Plane(c)[i], actual.Plane(c)[i], tolerance, what); } } /// An uncompressed little-endian TIFF of × CMYK pixels, each , without a profile. private static byte[] CmykTiff(int width, int height, byte[] inks) { var strip = new byte[width * height * 4]; for (var i = 0; i < width * height; i++) inks.CopyTo(strip, i * 4); const int ifd = 8; (ushort Tag, ushort Type, uint Count, uint Value)[] entries = [ (256, 4, 1, (uint)width), (257, 4, 1, (uint)height), (258, 3, 4, 0), // four 8-bit samples, stored after the directory (259, 3, 1, 1), (262, 3, 1, 5), // separated: CMYK (273, 4, 1, 0), // the strip, after the samples' bits (277, 3, 1, 4), (278, 4, 1, (uint)height), (279, 4, 1, (uint)strip.Length), ]; var bits = ifd + 2 + entries.Length * 12 + 4; var data = bits + 8; using var stream = new MemoryStream(); using var writer = new BinaryWriter(stream); writer.Write("II*\0"u8); writer.Write((uint)ifd); writer.Write((ushort)entries.Length); foreach (var (tag, type, count, value) in entries) { writer.Write(tag); writer.Write(type); writer.Write(count); writer.Write(tag switch { 258 => (uint)bits, 273 => (uint)data, _ => value }); } writer.Write(0u); for (var i = 0; i < 4; i++) writer.Write((ushort)8); writer.Write(strip); return stream.ToArray(); } [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}"); } } } }