feat: owner review 2 phase 4b: mass conversion after import (#13)
CI / Build and test (push) Failing after 3m1s
CI / PostgreSQL 16 integration (push) Skipped
CI / PostgreSQL 17 integration (push) Skipped
CI / Docker image (push) Skipped
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 10s
Release / Build, test and publish (push) Successful in 15m31s
Release / Server image (push) Successful in 1m13s

Convert photos already in the archive to JPEG, TIFF-16 or Linear DNG, from the panel's Library and the desktop's Archive and Library. Each run asks Replace (same frames, originals to the trash, recoverable) or New sequence. A dry run lists what converts, what is skipped and why, size before and after, and free space; the job converts exactly the confirmed photos, resumes, and commits all or nothing per sequence. A conversion never changes what Chronos reads from a photo. Replaced originals are erased only on the archive's own word that their conversion is gone. Desktop TIFFs that WIC showed black or as noise now go to the managed reader; the Edit picker no longer spins a core.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #13.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-11-03 10:41:50 +01:00
1 parent 7e4a3b481d
commit efa87f68ce
354 files changed
+24530 -843

No files matched your search

@@ -0,0 +1,32 @@
using Chronos.Core.Windows.Devices;
using Chronos.Testing;
namespace Chronos.Core.Windows.Tests.Devices;
/// <summary>
/// The free space of a folder's volume as a conversion checks it on the desktop (owner review 2, R7): a network share's too,
/// which <see cref="DriveInfo"/> cannot measure. Apart from <see cref="VolumeTests"/>, which enumerate every volume.
/// </summary>
public sealed class FreeSpaceTests(TestContext context)
{
[Test]
[WindowsOnly]
public void AFolderIsMeasuredOnItsVolumeAndAMissingOneIsNot()
{
var folder = context.CreateTempDirectory("free");
Assert.Greater(Assert.NotNull(VolumeInfo.FreeBytesAt(folder)), 0);
Assert.Greater(Assert.NotNull(VolumeInfo.FreeBytesAt(folder + Path.DirectorySeparatorChar)), 0);
Assert.Null(VolumeInfo.FreeBytesAt(Path.Combine(folder, "missing", "deeper")));
}
[Test]
[WindowsOnly]
public void AFolderOnAShareIsMeasuredWhereTheDriveCannotBe()
{
var folder = Path.GetFullPath(context.CreateTempDirectory("share"));
var share = $@"\\localhost\{char.ToLowerInvariant(folder[0])}$" + folder[2..];
if (!Directory.Exists(share)) TestContext.Skip("This PC has no administrative share for the test folder.");
Assert.Throws<ArgumentException>(() => new DriveInfo(Path.GetPathRoot(share)!));
Assert.Greater(Assert.NotNull(VolumeInfo.FreeBytesAt(share)), 0);
}
}
@@ -0,0 +1,169 @@
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Windows.Wic;
using Chronos.Testing;
namespace Chronos.Core.Windows.Tests.Wic;
/// <summary>
/// Conversions on the desktop (owner review 2, R7), read back through the desktop's own decoders, WIC first: what Chronos
/// shows of a photo there never changes with its conversion, whatever the photo's colour space and samples.
/// </summary>
[WindowsOnly]
public sealed class DesktopConversionTests(TestContext context)
{
private static readonly Lazy<ImageDecoder> Desktop = new(() => DecoderFactory.CreateClient(new WicDecoderProvider()));
/// <summary>
/// The desktop decodes the converted file (WIC for a JPEG or TIFF, the managed reader for a DNG) to the values it decodes
/// the original to, within what the target holds, for sRGB, Adobe RGB, ProPhoto and linear-curve sources of 8-bit,
/// 16-bit and float samples; the file carries a profile that says what its codes are. A DNG takes only values that are
/// sRGB's as the decoders read them, and refuses the others.
/// </summary>
[Test]
public void TheDesktopReadsTheConvertedFileAsItReadsTheOriginal()
{
var folder = context.CreateTempDirectory();
using var scene = WicDecoderProviderTests.Scene(48, 32);
using var grays = new ImageBuffer(48, 32, 1);
for (var i = 0; i < grays.PlaneLength; i++) grays.Plane(0)[i] = 0.002f + 0.95f * i / grays.PlaneLength;
var srgb = IccProfile.Synthesize("sRGB IEC61966-2.1", new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), IccCurve.Srgb);
var g10 = IccProfile.Synthesize("sRGB-elle-V4-g10", new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), IccCurve.Gamma(1));
var prophoto = IccProfile.Synthesize("ProPhoto RGB", new(0.7347, 0.2653), new(0.1596, 0.8404), new(0.0366, 0.0001), IccCurve.Gamma(1.8));
(string Name, byte[] File, bool DngKeeps)[] sources =
[
("untagged.jpg", Jpeg(scene, null), true),
("srgb.jpg", Jpeg(scene, srgb), true),
("gray22.jpg", JpegCarrying(grays, Gray(IccCurve.Gamma(2.2))), false),
("sgray.jpg", JpegCarrying(grays, Gray(IccCurve.Srgb)), true),
("adobe.jpg", Jpeg(scene, IccProfile.AdobeRgb), false),
("g10.jpg", Jpeg(scene, g10), false),
("prophoto16.tif", TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = prophoto.Data }), false),
("adobe16.tif", TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = IccProfile.AdobeRgb.Data }), false),
("float.tif", TestTiffs.FloatRgb(scene), true),
("float-adobe.tif", TestTiffs.FloatRgb(scene, IccProfile.AdobeRgb.Data), false),
("float-prophoto.tif", TestTiffs.FloatRgb(scene, prophoto.Data), false),
("float-g10.tif", TestTiffs.FloatRgb(scene, g10.Data), true),
("float-adobe-predicted.tif", TestTiffs.FloatRgb(scene, IccProfile.AdobeRgb.Data, predicted: true), false),
];
var desktop = Desktop.Value;
foreach (var (name, bytes, dngKeeps) in sources)
{
var path = Path.Combine(folder, name);
File.WriteAllBytes(path, bytes);
var format = name.EndsWith(".jpg", StringComparison.Ordinal) ? ImageFileFormat.Jpeg : ImageFileFormat.Tiff;
var color = SourceColor.Read(path, format);
var source = new ConvertSource(path, Frame(name, format, scene.Width, scene.Height));
using var original = Assert.NotNull(desktop.Decode(new DecodeRequest(path, AllowPreview: false)));
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target, 100);
var file = FrameConverter.Convert(source, options, desktop, Target(options), cancellationToken: context.Cancellation);
using var converted = Assert.NotNull(desktop.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
Assert.Equal("wic", converted.Decoder, name);
var (max, mean) = CodeDifference(original.Pixels, converted.Pixels);
var what = $"{name} ({original.Decoder}) → {target}";
context.Log($"{what}: max {max * 255:0.00}/255, mean {mean * 255:0.000}/255");
Assert.LessOrEqual(max, target == ConvertTarget.Jpeg ? 4 / 255.0 : 2 / 65535.0, what);
Assert.Less(mean, target == ConvertTarget.Jpeg ? 1 / 255.0 : 1 / 65535.0, what);
// What the file says its codes are: the original's profile with the original's codes, else its colorants with the sRGB curve.
var carried = SourceColor.Read(file.Path, file.Format).Profile;
if (color.Profile is not { } profile)
{
Assert.Null(carried, what);
}
else if (profile.IsGray)
{
// A gray profile's codes, kept, with its curve on three channels.
Assert.True(Assert.NotNull(carried, what).IsMatrixShaper, what);
for (var code = 0.0; code <= 1; code += 0.125) Assert.Approximately(profile.Curves![0].Evaluate(code), carried.Curves![1].Evaluate(code), 1e-3, what);
}
else if (!color.IsLinear)
{
Assert.BytesEqual(profile.Data, Assert.NotNull(carried, what).Data);
}
else
{
Assert.Equal(profile.Colorants, Assert.NotNull(carried, what).Colorants);
Assert.True(carried.Curves!.All(c => c.IsSrgb), what);
}
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
if (!dngKeeps)
{
Assert.Equal(ConversionFailure.ColorProfile,
Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, desktop, output, cancellationToken: context.Cancellation)).Reason, name);
Assert.FileMissing(output);
continue;
}
FrameConverter.Convert(source, dng, desktop, output, cancellationToken: context.Cancellation);
using var developed = Assert.NotNull(desktop.Decode(new DecodeRequest(output, AllowPreview: false)));
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) Assert.Approximately(original.Pixels.Plane(c)[i], developed.Pixels.Plane(c)[i], 3e-4, $"{name} → DNG");
}
}
}
private static byte[] Jpeg(ImageBuffer scene, IccProfile? profile) => JpegCarrying(scene, profile?.Data);
private static byte[] JpegCarrying(ImageBuffer scene, byte[]? icc) => JpegEncoder.Encode(scene, new JpegEncodeOptions
{
Quality = 98,
Subsampling = JpegSubsampling.Yuv444,
IccProfile = icc,
});
/// <summary>A gray profile (GRAY, a kTRC of <paramref name="curve"/>): a synthesized RGB one, its red curve renamed.</summary>
private static byte[] Gray(IccCurve curve)
{
var data = (byte[])IccProfile.Synthesize("Gray", new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), curve).Data.Clone();
"GRAY"u8.CopyTo(data.AsSpan(16));
var count = System.Buffers.Binary.BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(128));
for (var i = 0; i < count; i++)
{
var entry = data.AsSpan(132 + 12 * i, 4);
if (entry.SequenceEqual("rTRC"u8)) "kTRC"u8.CopyTo(entry);
}
return data;
}
private static SidecarFrame Frame(string file, ImageFileFormat format, int width, int height) => new()
{
Id = Guid.NewGuid(),
File = "originals/" + file,
Sha256 = new string('a', 64),
CapturedAt = new DateTime(2026, 9, 28, 18, 0, 5),
W = width,
H = height,
Format = format,
};
private string Target(ConvertOptions options) => Path.Combine(context.CreateTempDirectory(), "converted" + options.Extension);
/// <summary>The largest and the mean difference between two decodes, in sRGB code values (what the files hold).</summary>
private static (double Max, double Mean) CodeDifference(ImageBuffer a, ImageBuffer b)
{
Assert.Equal((a.Width, a.Height), (b.Width, b.Height));
double max = 0, sum = 0;
for (var c = 0; c < 3; c++)
{
var pa = a.Plane(c);
var pb = b.Plane(c);
for (var i = 0; i < pa.Length; i++)
{
var d = Math.Abs(ColorSpace.LinearToSrgb(pa[i]) - ColorSpace.LinearToSrgb(pb[i]));
max = Math.Max(max, d);
sum += d;
}
}
return (max, sum / (3.0 * a.PlaneLength));
}
}
@@ -0,0 +1,137 @@
using System.Buffers.Binary;
using System.IO.Compression;
using Chronos.Core.Imaging;
namespace Chronos.Core.Windows.Tests.Wic;
/// <summary>Little-endian TIFFs of one strip, built tag by tag: what WIC and the managed reader are asked about.</summary>
internal static class TestTiffs
{
private const ushort Short = 3, Long = 4, Undefined = 7;
/// <summary>
/// A TIFF of <paramref name="width"/> × <paramref name="height"/> pixels of <paramref name="bits"/>-bit samples,
/// <paramref name="spp"/> a pixel, in one strip holding <paramref name="strip"/> as it is (already compressed or predicted).
/// </summary>
public static byte[] Build(int width, int height, int spp, int bits, byte[] strip, int compression = 1, int photometric = 2, int sampleFormat = 1,
int predictor = 1, byte[]? icc = null)
{
var entries = new List<(ushort Tag, ushort Type, uint[] Values, byte[]? Bytes)>
{
(256, Long, [(uint)width], null),
(257, Long, [(uint)height], null),
(258, Short, [.. Enumerable.Repeat((uint)bits, spp)], null),
(259, Short, [(uint)compression], null),
(262, Short, [(uint)photometric], null),
(273, Long, [0], null),
(277, Short, [(uint)spp], null),
(278, Long, [(uint)height], null),
(279, Long, [(uint)strip.Length], null),
(284, Short, [1], null),
};
if (predictor != 1) entries.Add((317, Short, [(uint)predictor], null));
if (sampleFormat != 1) entries.Add((339, Short, [.. Enumerable.Repeat((uint)sampleFormat, spp)], null));
if (icc is not null) entries.Add((34675, Undefined, [], icc));
entries.Sort((a, b) => a.Tag.CompareTo(b.Tag));
const int ifd = 8;
var position = ifd + 2 + entries.Count * 12 + 4;
var outOfLine = new int[entries.Count];
for (var i = 0; i < entries.Count; i++)
{
var size = Size(entries[i]);
if (size <= 4) continue;
outOfLine[i] = position;
position += size + (size & 1);
}
var data = position;
var bytes = new byte[data + strip.Length];
"II*\0"u8.CopyTo(bytes);
BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(4), ifd);
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(ifd), (ushort)entries.Count);
for (var i = 0; i < entries.Count; i++)
{
var (tag, type, values, raw) = entries[i];
var at = ifd + 2 + 12 * i;
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(at), tag);
BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(at + 2), type);
BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at + 4), (uint)(raw?.Length ?? values.Length));
var target = outOfLine[i] > 0 ? outOfLine[i] : at + 8;
if (outOfLine[i] > 0) BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(at + 8), (uint)outOfLine[i]);
if (raw is not null)
{
raw.CopyTo(bytes, target);
continue;
}
for (var k = 0; k < values.Length; k++)
{
var value = tag == 273 ? (uint)data : values[k];
if (type == Short) BinaryPrimitives.WriteUInt16LittleEndian(bytes.AsSpan(target + 2 * k), (ushort)value);
else BinaryPrimitives.WriteUInt32LittleEndian(bytes.AsSpan(target + 4 * k), value);
}
}
strip.CopyTo(bytes, data);
return bytes;
}
private static int Size((ushort Tag, ushort Type, uint[] Values, byte[]? Bytes) entry) =>
entry.Bytes?.Length ?? (entry.Type == Short ? 2 : 4) * entry.Values.Length;
/// <summary>An 8-bit RGB TIFF of <paramref name="image"/>'s sRGB codes, uncompressed, with its strip labelled <paramref name="compression"/>.</summary>
public static byte[] Rgb8(ImageBuffer image, int compression = 1) =>
Build(image.Width, image.Height, 3, 8, PixelConvert.ToInterleaved8(image, 3), compression);
/// <summary>
/// A 32-bit float RGB TIFF of <paramref name="image"/> (linear light, as it is), uncompressed, or Deflate with the
/// floating-point predictor (3) as Photoshop writes its 32-bit files: byte planes most significant first, each byte less
/// the one a pixel before it.
/// </summary>
public static byte[] FloatRgb(ImageBuffer image, byte[]? icc = null, bool predicted = false)
{
const int spp = 3;
var count = image.Width * spp;
var strip = new byte[image.PlaneLength * spp * 4];
var row = new byte[count * 4];
for (var y = 0; y < image.Height; y++)
{
for (var x = 0; x < image.Width; x++)
{
for (var c = 0; c < spp; c++)
{
var bits = BitConverter.SingleToUInt32Bits(image[c, x, y]);
var i = x * spp + c;
if (!predicted)
{
BinaryPrimitives.WriteUInt32LittleEndian(row.AsSpan(i * 4), bits);
continue;
}
for (var k = 0; k < 4; k++) row[k * count + i] = (byte)(bits >> (8 * (3 - k)));
}
}
if (predicted)
{
for (var i = row.Length - 1; i >= spp; i--) row[i] -= row[i - spp];
}
row.CopyTo(strip, y * row.Length);
}
return predicted
? Build(image.Width, image.Height, spp, 32, Deflate(strip), compression: 8, sampleFormat: 3, predictor: 3, icc: icc)
: Build(image.Width, image.Height, spp, 32, strip, sampleFormat: 3, icc: icc);
}
/// <summary>A 32-bit unsigned integer RGB TIFF of <paramref name="image"/>'s codes, scaled to 32 bits.</summary>
public static byte[] Uint32Rgb(ImageBuffer image)
{
var codes = PixelConvert.ToInterleaved8(image, 3);
var strip = new byte[codes.Length * 4];
for (var i = 0; i < codes.Length; i++) BinaryPrimitives.WriteUInt32LittleEndian(strip.AsSpan(i * 4), codes[i] * 16843009u);
return Build(image.Width, image.Height, 3, 32, strip);
}
private static byte[] Deflate(byte[] raw)
{
using var output = new MemoryStream();
using (var z = new ZLibStream(output, CompressionLevel.Optimal, leaveOpen: true)) z.Write(raw);
return output.ToArray();
}
}
@@ -21,6 +21,242 @@ public sealed class WicDecoderProviderTests(TestContext context)
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>
/// 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.
/// </summary>
[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)));
}
/// <summary>
/// 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.
/// </summary>
[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<FileNotFoundException>(() => reading.Probe(Path.Combine(folder, "gone.png"), ImageFileFormat.Png));
using (new FileStream(png, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
Assert.Throws<IOException>(() => 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<string>(["wide.tif"]),
new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg), desktop));
Assert.Null(Chronos.Core.Conversion.ConvertPlanner.Skip(frame, archive, new HashSet<string>(["wide.tif"]),
new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.LinearDng), desktop));
}
/// <summary>A gradient with saturated patches, linear light, every value between 0 and 1.</summary>
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);
}
}
/// <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()
{