Phase 4b review, codec findings. A Replace makes the converted file the photo, so a conversion must not lose what the original held, and the dry run must promise only what the job will do. - Linear DNG from a conversion is scene-referred: the pixels are stored in linear ProPhoto primaries (colours outside sRGB stay positive) and divided by the power of two that brings their peak under the white level (at most 4 stops), with BaselineExposure giving it back and ColorMatrix1/ForwardMatrix1 describing that space. Highlights a RAW develops above 1 and its wide-gamut colours now read back as written. Exports keep the clipped linear-sRGB DNG. - A conversion reads with the managed decoders first for every format they read natively (RAWs developed at float precision, as on the server) and with WIC only for what they cannot. A RAW only WIC's Raw Image Extension develops (8 bits) is refused for TIFF-16 and linear DNG and planned out as "eight_bit"; JPEG still converts it. Providers say so through IImageDecoderProvider.DevelopsRawAt8Bits. - The original's colour space is kept: a JPEG (APP2) or TIFF (tag 34675) carries its ICC profile, with the values unchanged; a linear DNG gets the values converted through a matrix/TRC profile; a gray profile converts to sRGB. Exif Adobe RGB (uncalibrated + R03) and HEIF nclx P3 get an in-house profile. A space nothing here can keep (CMYK, a lookup-table profile into a DNG, uncalibrated without R03, HDR nclx) is planned out as "color_profile". New in-house ICC parser/writer and header reader for JPEG, PNG, TIFF, HEIF/AVIF and WebP. - The planner asks every file, whatever tier was recorded (unknown before the light analysis, or by another host), with the conversion's decoders, headers only and eight at a time; a RAW the server cannot develop is "convert_on_pc" in the plan instead of failing its sequence. Photos a target cannot hold (a JPEG side above 65535, more samples than one buffer) are planned out as "too_large", and an overflow fails its photo rather than the job. - A photo that fails reads in the job log as one catalog sentence by cause (Archive.Convert.Failed.*), the file named once; the exception's English text goes to the diagnostics log only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 0ca1e91ea09946d7a55f45695ee58e1a0ff5f779)
182 lines
7.9 KiB
C#
182 lines
7.9 KiB
C#
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));
|
||
}
|
||
|
||
[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}");
|
||
}
|
||
}
|
||
}
|
||
}
|