Files
Chronos/tests/Chronos.Core.Windows.Tests/Wic/WicDecoderProviderTests.cs
T
Alby96 5b5acaf307 fix(convert): plan, decode and colour only what a conversion really does
Round-2 fixes for the codecs part of owner review 2, phase 4b (mass
conversion), so the dry run never promises a conversion that then fails
or writes wrong pixels.

- The managed TIFF reader refuses samples it does not render (CMYK,
  CIELab, palette, LogL, RGB of fewer than three samples, float
  min-is-white) before reading pixels, instead of decoding them as the
  gray of their first plane. The conversion chain then asks WIC on a PC;
  the server leaves them to a PC. Impossible strip/tile counts are
  refused as data, not as an OverflowException that escaped the chain.
- A conversion's managed provider reads JPEG, PNG and TIFF headers in
  its probe (ReadsHeaders): what its decoders refuse there (compression,
  sample format, photometric, MaxSamples, MaxPixels, arithmetic coding)
  is not planned as convertible on the server, and is asked of WIC on a
  PC. The probe gives the real size, so TooLarge sees it.
- Float TIFFs are linear light (SourceColor.IsLinear): a JPEG or TIFF
  carrying their profile gets codes from the profile's own curves, a DNG
  gets them through the matrix only (no curve, no clip, highlights
  kept). IsSrgb is decided by the numbers for matrix/TRC profiles, so a
  linear profile named sRGB is converted, not taken as sRGB. The TIFF
  profile is read from the image the decoders read.
- TooLarge is a bound: the target's limits and what the server's
  decoders read back, which every converted file must (JPEG 2^28 pixels,
  TIFF-16 and DNG 2^29 samples), and the incompressible worst case
  against the 2 GB array. A MemoryStream overflow is TooLarge, never a
  generic file failure.
- Colorants that convert nothing (a white without positive cone
  responses, a non-finite or singular matrix) are dropped when a profile
  is parsed: the photo is carried into a JPEG or TIFF and planned out of
  a DNG as colour_profile, instead of throwing in BradfordAdaptation at
  every retry.

Tests: header probes, refused photometrics, the size bound against the
verifier's header reads, float TIFF colour, a fuzz of damaged profiles,
and a Windows test converting a CMYK TIFF through WIC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 18cf9133087aada2db06c8310d2c69ff804c0115)
2026-11-02 10:33:33 +01:00

252 lines
11 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 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<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));
}
/// <summary>
/// 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).
/// </summary>
[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));
}
/// <summary>
/// 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.
/// </summary>
[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);
}
/// <summary>An uncompressed little-endian TIFF of <paramref name="width"/> × <paramref name="height"/> CMYK pixels, each <paramref name="inks"/>, without a profile.</summary>
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<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}");
}
}
}
}