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,291 @@
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Mathematics;
using Chronos.Core.Tests.Conversion;
using Chronos.Testing;
namespace Chronos.Core.Tests.Codecs;
/// <summary>ICC profiles (owner review 2, R7): parsed, synthesized for the spaces a marker names, and read from each container.</summary>
public sealed class IccProfileTests(TestContext context)
{
[Test]
public void TheSynthesizedProfilesAreMatrixShapersOfTheirSpace()
{
var adobe = IccProfile.AdobeRgb;
Assert.True(adobe.IsMatrixShaper && adobe.IsRgb && !adobe.IsSrgb);
Assert.Equal("Compatible with Adobe RGB (1998)", adobe.Description);
// Colorants add up to the D50 PCS white.
var white = adobe.Colorants!.Value * new Vector3D(1, 1, 1);
Assert.Approximately(0.9642, white.X, 2e-4);
Assert.Approximately(1.0, white.Y, 2e-4);
Assert.Approximately(0.8249, white.Z, 2e-4);
// Adobe RGB → linear sRGB: red reaches 1.398 with green −0.398, blue stays put but borrows from green.
var m = adobe.ToLinearRec709();
Assert.Approximately(1.3982, m.M11, 2e-3);
Assert.Approximately(-0.3982, m.M12, 2e-3);
Assert.Approximately(1.0, m.M22, 2e-3);
Assert.Approximately(-0.0429, m.M32, 2e-3);
Assert.Approximately(1.0429, m.M33, 2e-3);
var neutral = m * new Vector3D(1, 1, 1);
Assert.Approximately(1, neutral.X, 1e-9);
Assert.Approximately(1, neutral.Y, 1e-9);
Assert.Approximately(1, neutral.Z, 1e-9);
// Its gamma 563/256, written as a u8Fixed8 curve.
Assert.Approximately(Math.Pow(0.5, 563 / 256.0), adobe.Curves![0].Evaluate(0.5), 1e-12);
var p3 = IccProfile.DisplayP3;
Assert.True(p3.IsMatrixShaper);
// The sRGB curve, tabulated in 1024 entries.
for (var v = 0.0; v <= 1; v += 0.05) Assert.Approximately(ColorSpace.SrgbToLinear(v), p3.Curves![1].Evaluate(v), 2e-5);
// P3's own green is outside sRGB: negative red and blue in Rec.709.
var green = p3.ToLinearRec709() * new Vector3D(0, 1, 0);
Assert.Approximately(-0.2246, green.X, 3e-3);
Assert.Approximately(1.0421, green.Y, 3e-3);
Assert.Approximately(-0.0786, green.Z, 3e-3);
Assert.Null(IccProfile.Parse(new byte[200]));
Assert.Null(IccProfile.Parse(adobe.Data[..131]));
}
[Test]
public void ParametricCurvesFollowTheirFunctions()
{
// para function 3 with the sRGB parameters (as Apple's Display P3 profile writes it).
byte[] tag = [.. "para"u8, 0, 0, 0, 0, 0, 3, 0, 0, .. Fixed(2.4), .. Fixed(1 / 1.055), .. Fixed(0.055 / 1.055), .. Fixed(1 / 12.92), .. Fixed(0.04045)];
var curve = IccCurve.Parse(tag)!;
foreach (var v in new[] { 0.0, 0.02, 0.04045, 0.3, 0.75, 1.0 }) Assert.Approximately(ColorSpace.SrgbToLinear(v), curve.Evaluate(v), 2e-5);
Assert.Approximately(Math.Pow(0.5, 1.8), IccCurve.Parse([.. "para"u8, 0, 0, 0, 0, 0, 0, 0, 0, .. Fixed(1.8)])!.Evaluate(0.5), 1e-4);
Assert.Approximately(0.25, IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 0])!.Evaluate(0.25), 1e-12);
Assert.Approximately(0.5, IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0x80, 0, 0xFF, 0xFF])!.Evaluate(0.5), 1e-4);
Assert.Null(IccCurve.Parse([.. "para"u8, 0, 0, 0, 0, 0, 9, 0, 0]));
static byte[] Fixed(double v)
{
var b = new byte[4];
System.Buffers.Binary.BinaryPrimitives.WriteInt32BigEndian(b, (int)Math.Round(v * 65536));
return b;
}
}
/// <summary>
/// Colorants that convert nothing (their white has a cone response that is not positive, as a damaged profile's may) are
/// dropped when the profile is parsed: it stays an RGB profile to carry, never one to convert through, and asking its
/// matrix fails as for any profile without one, not inside the chromatic adaptation.
/// </summary>
[Test]
public void ColorantsThatConvertNothingAreDroppedWhenParsed()
{
var degenerate = IccProfile.Parse(ColorFixtures.Degenerate())!;
Assert.True(degenerate.IsRgb);
Assert.Null(degenerate.Colorants);
Assert.False(degenerate.IsMatrixShaper);
Assert.False(degenerate.IsSrgb);
Assert.Throws<InvalidOperationException>(() => degenerate.ToLinearRec709());
// The same colorants inside a white with positive cone responses convert.
Assert.NotNull(IccProfile.AdobeRgb.Colorants);
}
/// <summary>
/// A damaged profile never fails a conversion's colour work midway (owner review 2, R7): bytes flipped in matrix/TRC
/// profiles, as in a damaged file, give a profile that does not parse, one only to carry, or one whose matrix is finite
/// and invertible; wherever the treatment asks for a profile made for the codes written (a float TIFF's linear values
/// into a JPEG or TIFF), that profile is made and parses, its matrix finite and invertible; a DNG takes the values as
/// they are or refuses them. Anything else the treatment refuses before any pixel is decoded.
/// </summary>
[Test]
public void DamagedProfilesConvertOrAreRefusedNeverFailMidway()
{
var random = new Random(27);
using var pixels = new ImageBuffer(4, 2);
float[] values = [0, 0.18f, 0.5f, 1, 3, -0.2f, 0.02f, 0.9f];
var data = pixels.Data;
for (var i = 0; i < data.Length; i++) data[i] = values[i % values.Length];
int parsed = 0, described = 0;
foreach (var original in (byte[][])[IccProfile.AdobeRgb.Data, IccProfile.DisplayP3.Data, ColorFixtures.Profile("Wide gamma 1.8", IccCurve.Gamma(1.8), wide: true).Data])
{
for (var n = 0; n < 1000; n++)
{
var damaged = (byte[])original.Clone();
// The tag table and the tags: colorants, curves, description.
for (var k = random.Next(1, 4); k > 0; k--) damaged[random.Next(128, damaged.Length)] ^= (byte)random.Next(1, 256);
if (IccProfile.Parse(damaged) is not { } profile) continue;
parsed++;
if (profile.IsMatrixShaper)
{
var m = profile.ToLinearRec709();
Assert.True(Finite(m) && m.TryInvert(out var inverse) && Finite(inverse), "a matrix to convert through is finite and invertible");
}
SourceColor[] carriers =
[
SourceColor.Read(new MemoryStream(JpegCarrying(damaged)), ImageFileFormat.Jpeg),
SourceColor.Read(new MemoryStream(ColorFixtures.FloatTiff(pixels, damaged)), ImageFileFormat.Tiff),
];
foreach (var color in carriers)
{
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var treatment = FrameConverter.Treatment(color, target);
if (target == ConvertTarget.LinearDng) Assert.True(treatment is ColorTreatment.None or ColorTreatment.Refuse, $"a DNG of {treatment}");
if (treatment != ColorTreatment.Describe) continue;
var made = IccProfile.Parse(FrameConverter.Described(color.Profile!).Data);
Assert.True(made is { IsMatrixShaper: true }, "the profile made for the codes parses as a matrix/TRC one");
var m = made!.ToLinearRec709();
Assert.True(Finite(m) && m.TryInvert(out var inverse) && Finite(inverse), "and converts with finite numbers");
described++;
}
}
}
}
context.Log($"{parsed} damaged profiles parsed, {described} described");
Assert.Greater(parsed, 1000);
Assert.Greater(described, 1000);
static bool Finite(Matrix3 m) => new[] { m.M11, m.M12, m.M13, m.M21, m.M22, m.M23, m.M31, m.M32, m.M33 }.All(double.IsFinite);
// The markers of a JPEG carrying the profile in one APP2 chunk, no image: all its colour is read from.
static byte[] JpegCarrying(byte[] icc)
{
var length = 2 + 14 + icc.Length;
return [0xFF, 0xD8, 0xFF, 0xE2, (byte)(length >> 8), (byte)length, .. "ICC_PROFILE\0"u8, 1, 1, .. icc, 0xFF, 0xD9];
}
}
/// <summary>
/// A matrix/TRC profile is sRGB when its numbers are sRGB's, whatever its name says; one without colorant tags at all (a
/// lookup-table profile) by its name. One whose colorants convert nothing, or whose curves are missing, is not, whatever
/// its name (owner review 2, R7).
/// </summary>
[Test]
public void SrgbIsWhatTheNumbersSay()
{
Assert.True(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb).IsSrgb);
Assert.True(ColorFixtures.Profile("Camera RGB", IccCurve.Srgb).IsSrgb);
// sRGB's primaries with a linear curve, or a gamma 2.2 one, named sRGB.
Assert.False(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)).IsSrgb);
Assert.False(ColorFixtures.Profile("sRGB gamma 2.2", IccCurve.Gamma(2.2)).IsSrgb);
Assert.False(ColorFixtures.Profile("sRGB wide", IccCurve.Srgb, wide: true).IsSrgb);
Assert.False(IccProfile.DisplayP3.IsSrgb);
// Without colorant tags, the name decides; with colorants that convert nothing, or no curves, nothing says sRGB.
Assert.True(IccProfile.Parse(ColorFixtures.WithoutColorants(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Gamma(1)).Data))!.IsSrgb);
Assert.False(IccProfile.Parse(ColorFixtures.WithoutColorants(IccProfile.AdobeRgb.Data))!.IsSrgb);
var degenerate = IccProfile.Parse(ColorFixtures.Degenerate(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Srgb).Data))!;
Assert.True(degenerate is { IsRgb: true, Colorants: null, IsSrgb: false });
var curveless = ColorFixtures.Renamed(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb).Data, "rTRC", "xTRC");
Assert.True(IccProfile.Parse(curveless) is { IsMatrixShaper: false, IsSrgb: false });
// The curves themselves.
Assert.True(IccCurve.Srgb.IsSrgb && IccProfile.DisplayP3.Curves![0].IsSrgb);
Assert.False(IccCurve.Gamma(2.2).IsSrgb || IccCurve.Gamma(1).IsSrgb);
}
/// <summary>
/// Values are sRGB's as Chronos's decoders take them (owner review 2, R7) when they are in sRGB's primaries and, for code
/// values, through sRGB's curve: a linear-curve profile in sRGB's primaries holds sRGB's linear values but not its code
/// values; a gray profile's linear values are linear gray, its code values need the sRGB curve.
/// </summary>
[Test]
public void ValuesAreSrgbsWhereThePrimariesAndTheCurveSaySo()
{
(IccProfile Profile, bool Linear, bool Codes)[] cases =
[
(ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb), true, true),
(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)), true, false),
(ColorFixtures.Profile("sRGB gamma 2.2", IccCurve.Gamma(2.2)), true, false),
(IccProfile.AdobeRgb, false, false),
(IccProfile.DisplayP3, false, false),
(ColorFixtures.ProPhoto(), false, false),
(IccProfile.Parse(ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2)))!, true, false),
(IccProfile.Parse(ColorFixtures.Gray("sGray", IccCurve.Srgb))!, true, true),
(IccProfile.Parse(ColorFixtures.Cmyk())!, false, false),
(IccProfile.Parse(ColorFixtures.Degenerate())!, false, false),
];
foreach (var (profile, linear, codes) in cases)
Assert.Equal((linear, codes), (profile.IsSrgbFor(linear: true), profile.IsSrgbFor(linear: false)), $"{profile.Description} ({profile.ColorSpace:X8})");
}
/// <summary>
/// The profiles a converted file carries for codes that are not the original's (owner review 2, R7): a matrix/TRC
/// profile's colorants and media white with the sRGB curve, to the bit; a gray profile's curve on three channels whose
/// equal values are neutral.
/// </summary>
[Test]
public void ProfilesMadeForTheCodesKeepWhatTheyDescribe()
{
foreach (var source in (IccProfile[])[IccProfile.AdobeRgb, ColorFixtures.ProPhoto(), ColorFixtures.Profile("Wide gamma 1.0", IccCurve.Gamma(1), wide: true)])
{
var made = IccProfile.Parse(source.WithSrgbCurve().Data)!;
Assert.True(made.IsMatrixShaper && made.IsRgb, source.Description);
Assert.Equal(source.Colorants, made.Colorants);
Assert.Equal(source.MediaWhite, made.MediaWhite);
Assert.True(made.Curves!.All(c => c.IsSrgb), source.Description);
Assert.Equal($"{source.Description} (sRGB curve)", made.Description);
}
var gray = IccProfile.Parse(ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2)))!;
var rgb = IccProfile.Parse(gray.GrayAsRgb().Data)!;
Assert.True(rgb is { IsMatrixShaper: true, IsRgb: true });
Assert.Equal("Gray Gamma 2.2 (RGB)", rgb.Description);
for (var code = 0.0; code <= 1; code += 0.125)
{
var light = rgb.ToLinearRec709() * new Vector3D(rgb.Curves![0].Evaluate(code), rgb.Curves[1].Evaluate(code), rgb.Curves[2].Evaluate(code));
var expected = gray.Curves![0].Evaluate(code);
Assert.Approximately(expected, light.X, 1e-3);
Assert.Approximately(expected, light.Y, 1e-3);
Assert.Approximately(expected, light.Z, 1e-3);
}
Assert.Throws<InvalidOperationException>(() => gray.WithSrgbCurve());
Assert.Throws<InvalidOperationException>(() => IccProfile.AdobeRgb.GrayAsRgb());
// A damaged profile's endless description is cut.
var long1 = IccProfile.Synthesize(new string('x', 5000), new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), IccCurve.Srgb);
Assert.Less(long1.Description!.Length, 300);
}
/// <summary>A TIFF of floating-point samples or linear RAW holds linear values; integer samples are code values.</summary>
[Test]
public void ATiffSaysWhetherItsSamplesAreLinear()
{
using var scene = ColorFixtures.Scene();
SourceColor Read(byte[] file) => SourceColor.Read(new MemoryStream(file), ImageFileFormat.Tiff);
var adobe = IccProfile.AdobeRgb.Data;
var linear = Read(ColorFixtures.FloatTiff(scene, adobe));
Assert.True(linear.IsLinear);
Assert.BytesEqual(adobe, linear.Profile!.Data);
Assert.True(Read(ColorFixtures.FloatTiff(scene)) is { IsLinear: true, Profile: null, IsUnknown: false });
Assert.True(Read(Chronos.Core.Codecs.Dng.LinearDngWriter.Write(scene)).IsLinear);
Assert.False(Read(TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = adobe })).IsLinear);
Assert.False(Read(TiffWriter.Write(scene)).IsLinear);
}
[Test]
public void EachContainerSaysItsColourSpace()
{
using var scene = ColorFixtures.Scene();
var p3 = IccProfile.DisplayP3.Data;
SourceColor Read(byte[] file, ImageFileFormat format) => SourceColor.Read(new MemoryStream(file), format);
// Untagged and sRGB files are sRGB.
Assert.Same(SourceColor.Untagged, Read(ColorFixtures.Jpeg(scene), ImageFileFormat.Jpeg));
Assert.Same(SourceColor.Untagged, Read(ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(1, "R98")), ImageFileFormat.Jpeg));
// An embedded profile, byte for byte: JPEG APP2 (one chunk, or several), PNG iCCP, TIFF tag 34675, HEIF prof.
Assert.BytesEqual(p3, Read(ColorFixtures.Jpeg(scene, p3), ImageFileFormat.Jpeg).Profile!.Data);
var large = ColorFixtures.Large();
Assert.BytesEqual(large, Read(ColorFixtures.Jpeg(scene, large), ImageFileFormat.Jpeg).Profile!.Data);
Assert.BytesEqual(p3, Read(ColorFixtures.Png(scene, p3), ImageFileFormat.Png).Profile!.Data);
Assert.BytesEqual(p3, Read(TiffWriter.Write(scene, new TiffWriteOptions { IccProfile = p3 }), ImageFileFormat.Tiff).Profile!.Data);
Assert.BytesEqual(p3, Read(ColorFixtures.Heif(ColorFixtures.Nclx(1, 13), ColorFixtures.Prof(p3)), ImageFileFormat.Heif).Profile!.Data);
// A grid whose tiles carry the profile, not the grid item.
Assert.BytesEqual(p3, Read(ColorFixtures.Heif(2, ColorFixtures.Prof(p3)), ImageFileFormat.Heif).Profile!.Data);
// A camera's Adobe RGB marker (uncalibrated, Interop R03) names Adobe RGB, in a JPEG and in a TIFF.
Assert.Same(IccProfile.AdobeRgb, Read(ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF, "R03")), ImageFileFormat.Jpeg).Profile);
// nclx: BT.709 is sRGB, P3 primaries Display P3, BT.2020 or PQ unknown.
Assert.Same(SourceColor.Untagged, Read(ColorFixtures.Heif(ColorFixtures.Nclx(1, 13)), ImageFileFormat.Heif));
Assert.Same(IccProfile.DisplayP3, Read(ColorFixtures.Heif(ColorFixtures.Nclx(12, 13)), ImageFileFormat.Heif).Profile);
Assert.True(Read(ColorFixtures.Heif(ColorFixtures.Nclx(9, 16)), ImageFileFormat.Heif).IsUnknown);
// Uncalibrated without R03, or a profile that does not parse: a space nothing here describes.
Assert.True(Read(ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)), ImageFileFormat.Jpeg).IsUnknown);
Assert.True(Read(ColorFixtures.Jpeg(scene, new byte[300]), ImageFileFormat.Jpeg).IsUnknown);
// RAWs say nothing here: their decoders give Rec.709.
Assert.Same(SourceColor.Untagged, Read([1, 2, 3], ImageFileFormat.Cr2));
}
}
@@ -1,4 +1,5 @@
using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Raw;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
@@ -87,6 +88,67 @@ public sealed class LinearDngWriterTests
for (var row = 0; row < 3; row++) Assert.Approximately(d50[row], fm[row * 3] + fm[row * 3 + 1] + fm[row * 3 + 2], 3e-4);
}
/// <summary>
/// A conversion's archival DNG (owner review 2, R7): highlights above white and colours outside sRGB (negative in
/// Rec.709) read back through the in-house DNG reader and developer as they were written, to the 16-bit step.
/// </summary>
[Test]
public void ASceneReferredDngKeepsHighlightsAndColoursOutsideSrgb()
{
using var image = new ImageBuffer(64, 48);
for (var y = 0; y < 48; y++)
{
for (var x = 0; x < 64; x++)
{
image[0, x, y] = 0.02f + 0.9f * x / 63;
image[1, x, y] = 0.05f + 0.7f * y / 47;
image[2, x, y] = 0.1f + 0.3f * ((x + y) % 7) / 6;
}
}
(int X, int Y, float R, float G, float B)[] special =
[
(1, 1, 2.6f, 2.4f, 2.2f), // a highlight above white, as a RAW's reconstruction gives it
(2, 1, 3.5f, 1.0f, 0.5f), // a bright saturated sunset
(3, 1, -0.2246f, 1.0421f, -0.0786f), // Display P3 green, outside sRGB
(4, 1, -0.15f, 0.8f, 0.9f), // a cyan outside sRGB
(5, 1, 0.6f, -0.05f, 0.7f), // a magenta outside sRGB
];
foreach (var (x, y, r, g, b) in special) (image[0, x, y], image[1, x, y], image[2, x, y]) = (r, g, b);
var dng = LinearDngWriter.Write(image, new LinearDngOptions { SceneReferred = true, TileSize = 32 });
var reader = TiffReader.Open(new MemoryStream(dng))!;
var ifd = reader.ReadChain()[0];
// The peak, about 2.5 in ProPhoto, fits under white two stops down.
Assert.Equal(2.0, reader.GetDouble(ifd, TiffTags.BaselineExposure));
var fm = reader.GetDoubles(ifd, TiffTags.ForwardMatrix1)!;
double[] d50 = [0.3457 / 0.3585, 1.0, (1 - 0.3457 - 0.3585) / 0.3585];
for (var row = 0; row < 3; row++) Assert.Approximately(d50[row], fm[row * 3] + fm[row * 3 + 1] + fm[row * 3 + 2], 3e-4);
using var developed = RawDecoder.Decode(new MemoryStream(dng), "scene.dng", new RawDecodeOptions { ApplyOrientation = false });
var back = developed.Image;
Assert.Equal((64, 48, 3), (back.Width, back.Height, back.Channels));
var worst = 0.0;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < image.PlaneLength; i++) worst = Math.Max(worst, Math.Abs(image.Plane(c)[i] - back.Plane(c)[i]));
}
Assert.Less(worst, 4e-4, "largest difference after the round trip");
foreach (var (x, y, r, g, b) in special)
{
Assert.Approximately(r, back[0, x, y], 4e-4);
Assert.Approximately(g, back[1, x, y], 4e-4);
Assert.Approximately(b, back[2, x, y], 4e-4);
}
// The export mode is unchanged: linear sRGB primaries, clipped to 0..1, no headroom.
var export = LinearDngWriter.Write(image, new LinearDngOptions { TileSize = 32 });
using var clipped = RawDecoder.Decode(new MemoryStream(export), "export.dng", new RawDecodeOptions { ApplyOrientation = false });
Assert.Less(clipped.Image[0, 2, 1], 1.01, "the export clips the highlight");
Assert.Greater(clipped.Image[0, 3, 1], -0.01, "the export clips the colour outside sRGB");
var exportReader = TiffReader.Open(new MemoryStream(export))!;
Assert.Equal(0.0, exportReader.GetDouble(exportReader.ReadChain()[0], TiffTags.BaselineExposure));
}
[Test]
public void GrayInputExifAndNonSeekableOutput()
{
@@ -194,6 +194,78 @@ public sealed class TiffTests(TestContext context)
Assert.Throws<InvalidDataException>(() => TiffImageReader.ToImageBuffer(raster));
}
/// <summary>
/// Samples that are not gray, RGB or linear RAW are refused before their pixels are read, from the headers alone too,
/// never decoded as gray from their first plane (a CMYK red read as black, owner review 2, R7).
/// </summary>
[Theory]
[Case(5, 4)] // CMYK
[Case(8, 3)] // CIELab
[Case(3, 1)] // palette
[Case(2, 1)] // RGB of one sample
[Case(32844, 1)] // LogL
public void SamplesItDoesNotRenderAreRefusedNotReadAsGray(int photometric, int samples)
{
// A CMYK red: no cyan, full magenta and yellow, no black.
var pixel = Enumerable.Range(0, samples).Select(c => (byte)(c is 1 or 2 ? 255 : 0)).ToArray();
var tiff = Conversion.ColorFixtures.Tiff(6, 4, (ushort)photometric, pixel);
Assert.Throws<InvalidDataException>(() => TiffImageReader.Decode(tiff));
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(tiff)));
Assert.Throws<InvalidDataException>(() => TiffImageReader.ToImageBuffer(Raster(tiff)));
}
/// <summary>The headers say what <see cref="TiffImageReader.Decode(Stream, TiffDecodeOptions?)"/> reads, and refuse what it refuses.</summary>
[Test]
public void ReadInfoSaysWhatDecodeReads()
{
foreach (var name in (string[])["tiff/lzw-pred.tif", "tiff/lzw-tiled.tif", "tiff/planar-zip.tif", "tiff/jpeg-ycbcr.tif", "tiff/float32-pred3.tif", "tiff/gray-miniswhite.tif"])
{
using var image = TiffImageReader.Decode(Fixture(name));
Assert.Equal((image.Width, image.Height), TiffImageReader.ReadInfo(new MemoryStream(Fixture(name))), name);
}
var rgb = Conversion.ColorFixtures.Tiff(6, 4, 2, [10, 20, 30]);
Assert.Equal((6, 4), TiffImageReader.ReadInfo(new MemoryStream(rgb)));
// More samples than the decoder takes, a compression it lacks (ZSTD), float samples whose white is 0, no strips.
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(rgb), new TiffDecodeOptions { MaxSamples = 6 * 4 * 3 - 1 }));
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(Conversion.ColorFixtures.Tiff(6, 4, 2, [1, 2, 3], d => d.Short(TiffTags.Compression, 50000)))));
var floatWhiteIsZero = TiffTestEncoders.Build(d =>
{
Layout(d, 2, 2, 1, 32, compression: 1);
d.Short(TiffTags.PhotometricInterpretation, 0);
d.Short(TiffTags.SampleFormat, 3);
}, [new byte[16]], tiled: false);
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(floatWhiteIsZero)));
Assert.Throws<InvalidDataException>(() => TiffImageReader.Decode(floatWhiteIsZero));
using var noStrips = new MemoryStream();
noStrips.Write("II*\0\0\0\0\0"u8);
var directory = new TiffDirectory();
Layout(directory, 6, 4, 3, 8, compression: 1);
TiffWriter.WriteDirectory(noStrips, 0, directory);
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(noStrips.ToArray())));
// Tiles that no count holds are a malformed file, not an arithmetic error, to a decode as to the headers: a chain
// that finds them refused asks its next decoder.
var tiles = Conversion.ColorFixtures.Tiff(6, 4, 2, [1, 2, 3], d =>
{
d.Long(TiffTags.ImageWidth, int.MaxValue);
d.Long(TiffTags.ImageLength, int.MaxValue);
d.Long(TiffTags.TileWidth, 1);
d.Long(TiffTags.TileLength, 1);
d.Long(TiffTags.TileOffsets, 8);
});
var unbounded = new TiffDecodeOptions { MaxSamples = long.MaxValue };
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(tiles), unbounded));
Assert.Throws<InvalidDataException>(() => TiffImageReader.Decode(tiles, unbounded));
// More strips than the reader takes (one row each), refused before any is read.
var strips = Conversion.ColorFixtures.Tiff(6, 4, 1, [1], d =>
{
d.Long(TiffTags.ImageWidth, 1);
d.Long(TiffTags.ImageLength, (1 << 24) + 1);
d.Long(TiffTags.RowsPerStrip, 1);
});
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(strips)));
Assert.Throws<InvalidDataException>(() => TiffImageReader.Decode(strips));
}
[Theory]
[Case(8, 1, TiffCompression.None, false)]
[Case(8, 3, TiffCompression.Deflate, true)]
@@ -0,0 +1,243 @@
using System.Buffers.Binary;
using System.IO.Compression;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Png;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Tests.Codecs;
using Chronos.Core.Tests.Metadata.Builders;
namespace Chronos.Core.Tests.Conversion;
/// <summary>Photos that record their colour space: an embedded ICC profile in each container, or an Exif colour space.</summary>
internal static class ColorFixtures
{
/// <summary>Saturated colours (a P3 photo's greens and reds fall outside sRGB once converted), plus a gradient.</summary>
public static ImageBuffer Scene(int width = 48, int height = 32)
{
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.05f + 0.6f * x / width;
image[1, x, y] = 0.1f + 0.5f * y / height;
image[2, x, y] = 0.15f + 0.2f * ((x / 4 + y / 4) % 2);
}
}
for (var y = 0; y < 8; y++)
{
for (var x = 0; x < 8; x++)
{
(image[0, x, y], image[1, x, y], image[2, x, y]) = (0.02f, 0.9f, 0.02f); // a saturated green
(image[0, x + 8, y], image[1, x + 8, y], image[2, x + 8, y]) = (0.9f, 0.02f, 0.02f); // a saturated red
}
}
return image;
}
/// <summary>A JPEG whose values are <paramref name="scene"/> in sRGB, with an Exif block and/or an ICC profile.</summary>
public static byte[] Jpeg(ImageBuffer scene, byte[]? icc = null, byte[]? exifTiff = null) => JpegEncoder.Encode(scene, new JpegEncodeOptions
{
Quality = 98,
Subsampling = JpegSubsampling.Yuv444,
App1Segments = exifTiff is null ? null : [[.. "Exif\0\0"u8, .. exifTiff]],
IccProfile = icc,
});
/// <summary>An Exif block recording <paramref name="colorSpace"/> (1 sRGB, 0xFFFF uncalibrated) and an Interop index.</summary>
public static byte[] Exif(ushort colorSpace, string? interop = null)
{
var exif = new IfdSpec().Short(TiffTags.ColorSpace, colorSpace);
if (interop is not null) exif.Child(TiffTags.InteropIfd, new IfdSpec().Ascii(TiffTags.InteropIndex, interop));
return TiffFile.Build(new IfdSpec().Ascii(TiffTags.Make, "Canon").Child(TiffTags.ExifIfd, exif));
}
/// <summary>A PNG of <paramref name="scene"/> with an iCCP chunk after its header.</summary>
public static byte[] Png(ImageBuffer scene, byte[] icc)
{
var png = PngEncoder.Encode(scene);
using var compressed = new MemoryStream();
using (var z = new ZLibStream(compressed, CompressionLevel.Optimal, leaveOpen: true)) z.Write(icc);
var chunk = Box.Png("iCCP", [.. "Display P3\0"u8, 0, .. compressed.ToArray()]);
// Signature (8) and IHDR (25) come first.
return [.. png.AsSpan(0, 33), .. chunk, .. png.AsSpan(33)];
}
/// <summary>A HEIF whose primary item has the colr boxes given (no image data: only its colour is read).</summary>
public static byte[] Heif(params byte[][] colr) => Heif(1, colr);
/// <summary>A HEIF whose item <paramref name="item"/> has the colr boxes given; item 1 is the primary (a grid of tiles 2…).</summary>
public static byte[] Heif(ushort item, params byte[][] colr)
{
var associations = Enumerable.Range(1, colr.Length).Select(i => (byte)(0x80 | i)).ToArray();
return
[
.. Box.Make("ftyp", Box.Ascii("heic"), Box.U32(0), Box.Ascii("mif1"), Box.Ascii("heic")),
.. Box.Full("meta", 0, 0,
Box.Full("hdlr", 0, 0, Box.U32(0), Box.Ascii("pict"), Box.Zeros(12), [0]),
Box.Full("pitm", 0, 0, Box.U16(1)),
Box.Make("iprp",
Box.Make("ipco", [.. colr.SelectMany(c => c)]),
Box.Full("ipma", 0, 0, Box.U32(1), Box.U16(item), [(byte)associations.Length], associations))),
];
}
public static byte[] Prof(byte[] icc) => Box.Make("colr", Box.Ascii("prof"), icc);
public static byte[] Nclx(ushort primaries, ushort transfer) => Box.Make("colr", Box.Ascii("nclx"), Box.U16(primaries), Box.U16(transfer), Box.U16(1), [0x80]);
/// <summary>A profile that parses but describes CMYK, which no RGB file carries.</summary>
public static byte[] Cmyk()
{
var data = (byte[])IccProfile.AdobeRgb.Data.Clone();
"CMYK"u8.CopyTo(data.AsSpan(16));
return data;
}
/// <summary>A matrix/TRC profile of <paramref name="description"/>, with sRGB's primaries or Adobe RGB's (D65) and <paramref name="curve"/>.</summary>
public static IccProfile Profile(string description, IccCurve curve, bool wide = false) => wide
? IccProfile.Synthesize(description, new(0.64, 0.33), new(0.21, 0.71), new(0.15, 0.06), curve)
: IccProfile.Synthesize(description, new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), curve);
/// <summary>ProPhoto (ROMM) primaries with its gamma 1.8 (and the D65 white the synthesized profiles have).</summary>
public static IccProfile ProPhoto() =>
IccProfile.Synthesize("ProPhoto RGB", new(0.7347, 0.2653), new(0.1596, 0.8404), new(0.0366, 0.0001), IccCurve.Gamma(1.8));
/// <summary>A gray profile (GRAY, a kTRC of <paramref name="curve"/>) named <paramref name="description"/>.</summary>
public static byte[] Gray(string description, IccCurve curve)
{
var data = Renamed(Profile(description, curve).Data, "rTRC", "kTRC");
"GRAY"u8.CopyTo(data.AsSpan(16));
return data;
}
/// <summary><paramref name="profile"/> with its rXYZ, gXYZ and bXYZ tags renamed: a profile without colorant tags, as a lookup-table one is.</summary>
public static byte[] WithoutColorants(byte[] profile) => Renamed(Renamed(Renamed(profile, "rXYZ", "rLUT"), "gXYZ", "gLUT"), "bXYZ", "bLUT");
/// <summary>A copy of <paramref name="profile"/> whose tag <paramref name="from"/> is named <paramref name="to"/>: a reader no longer finds it.</summary>
public static byte[] Renamed(byte[] profile, string from, string to)
{
var data = (byte[])profile.Clone();
var count = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(128));
for (var i = 0; i < count; i++)
{
var entry = data.AsSpan(132 + 12 * i, 12);
if (entry[..4].SequenceEqual(System.Text.Encoding.ASCII.GetBytes(from))) System.Text.Encoding.ASCII.GetBytes(to).CopyTo(entry);
}
return data;
}
/// <summary>A gray JPEG whose codes are the sRGB encoding of <paramref name="gray"/> (one channel), with an ICC profile.</summary>
public static byte[] GrayJpeg(ImageBuffer gray, byte[]? icc = null) => JpegEncoder.Encode(gray, new JpegEncodeOptions { Quality = 98, IccProfile = icc });
/// <summary>A 16-bit TIFF whose codes are the sRGB encoding of <paramref name="scene"/>, with an ICC profile.</summary>
public static byte[] Tiff16(ImageBuffer scene, byte[]? icc = null) => TiffWriter.Write(scene, new TiffWriteOptions { BitsPerSample = 16, IccProfile = icc });
/// <summary>
/// <paramref name="profile"/> (the Adobe RGB one by default) with colorants that convert nothing: they add up to a white
/// whose cone responses are not all positive, although the matrix they make can be inverted (as a damaged profile's can).
/// </summary>
public static byte[] Degenerate(byte[]? profile = null)
{
var data = (byte[])(profile ?? IccProfile.AdobeRgb.Data).Clone();
var count = BinaryPrimitives.ReadInt32BigEndian(data.AsSpan(128));
(string Tag, double X, double Y, double Z)[] colorants = [("rXYZ", -1.0, 0.2, 0.1), ("gXYZ", 0.1, 0.5, 0.1), ("bXYZ", 0.1, 0.3, 0.6)];
for (var i = 0; i < count; i++)
{
var entry = data.AsSpan(132 + 12 * i, 12);
foreach (var (tag, x, y, z) in colorants)
{
if (!entry[..4].SequenceEqual(System.Text.Encoding.ASCII.GetBytes(tag))) continue;
var at = BinaryPrimitives.ReadInt32BigEndian(entry[4..]) + 8;
BinaryPrimitives.WriteInt32BigEndian(data.AsSpan(at), (int)Math.Round(x * 65536));
BinaryPrimitives.WriteInt32BigEndian(data.AsSpan(at + 4), (int)Math.Round(y * 65536));
BinaryPrimitives.WriteInt32BigEndian(data.AsSpan(at + 8), (int)Math.Round(z * 65536));
}
}
return data;
}
/// <summary>
/// An uncompressed TIFF of <paramref name="width"/> × <paramref name="height"/> 8-bit samples, <paramref name="samples"/>
/// per pixel in <paramref name="photometric"/> interpretation, each pixel <paramref name="pixel"/>.
/// </summary>
public static byte[] Tiff(int width, int height, ushort photometric, byte[] pixel, Action<TiffDirectory>? tags = null)
{
var strip = new byte[width * height * pixel.Length];
for (var i = 0; i < width * height; i++) pixel.CopyTo(strip, i * pixel.Length);
return TiffTestEncoders.Build(d =>
{
TiffTests.Layout(d, width, height, pixel.Length, 8, compression: 1);
d.Short(TiffTags.PhotometricInterpretation, photometric);
tags?.Invoke(d);
}, [strip], tiled: false);
}
/// <summary>
/// The headers of a TIFF of <paramref name="width"/> × <paramref name="height"/> pixels of three 8-bit samples in
/// <paramref name="photometric"/> interpretation, in strips of 16 rows, of which only the first is there: what a
/// header read takes, without its pixels.
/// </summary>
public static byte[] TiffHeader(int width, int height, ushort photometric = 2) => Tiff(6, 4, photometric, [1, 2, 3], d =>
{
d.Long(TiffTags.ImageWidth, (uint)width);
d.Long(TiffTags.ImageLength, (uint)height);
d.Long(TiffTags.RowsPerStrip, 16);
});
/// <summary>A PNG signature and header chunk of <paramref name="width"/> × <paramref name="height"/> 8-bit RGB, nothing after it.</summary>
public static byte[] PngHeader(int width, int height)
{
var ihdr = new byte[13];
BinaryPrimitives.WriteInt32BigEndian(ihdr, width);
BinaryPrimitives.WriteInt32BigEndian(ihdr.AsSpan(4), height);
(ihdr[8], ihdr[9]) = (8, 2);
return [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, .. Box.Png("IHDR", ihdr)];
}
/// <summary>
/// The markers of a JPEG of <paramref name="width"/> × <paramref name="height"/> three-component pixels coded by
/// <paramref name="sof"/> (0xC0 baseline, 0xC9 arithmetic…), up to its first scan.
/// </summary>
public static byte[] JpegHeader(int width, int height, byte sof = 0xC0) =>
[
0xFF, 0xD8,
0xFF, sof, 0x00, 0x11, 0x08, (byte)(height >> 8), (byte)height, (byte)(width >> 8), (byte)width, 0x03, 0x01, 0x11, 0x00, 0x02, 0x11, 0x00, 0x03, 0x11, 0x00,
0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x3F, 0x00,
0x12, 0x34, 0xFF, 0xD9,
];
/// <summary>
/// An uncompressed TIFF of 32-bit floating-point RGB, or gray for a one-channel image (linear light), with an ICC
/// profile when one is given.
/// </summary>
public static byte[] FloatTiff(ImageBuffer image, byte[]? icc = null)
{
var channels = image.Channels == 1 ? 1 : 3;
var strip = new byte[image.PlaneLength * channels * 4];
for (var i = 0; i < image.PlaneLength; i++)
{
for (var c = 0; c < channels; c++) BinaryPrimitives.WriteSingleLittleEndian(strip.AsSpan((i * channels + c) * 4), image.Plane(c)[i]);
}
return TiffTestEncoders.Build(d =>
{
TiffTests.Layout(d, image.Width, image.Height, channels, 32, compression: 1);
d.Short(TiffTags.SampleFormat, [.. Enumerable.Repeat((ushort)3, channels)]);
if (icc is not null) d.Add(TiffTags.InterColorProfile, TiffType.Undefined, icc.Length, icc);
}, [strip], tiled: false);
}
/// <summary>A valid profile larger than one JPEG APP2 chunk holds (the Adobe RGB one padded with a private tag's room).</summary>
public static byte[] Large()
{
var small = IccProfile.AdobeRgb.Data;
var data = new byte[small.Length + 150_000];
small.CopyTo(data, 0);
System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(data, (uint)data.Length);
for (var i = small.Length; i < data.Length; i++) data[i] = (byte)(i * 31);
return data;
}
}
@@ -0,0 +1,515 @@
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Raw;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Export;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Model;
using Chronos.Core.Tests.Raw;
using Chronos.Testing;
namespace Chronos.Core.Tests.Conversion;
/// <summary>The dry run of a conversion (owner review 2, R7): which frames convert, which stay and why, the sizes, the space and the fingerprint.</summary>
[Category("conversion")]
public sealed class ConvertPlannerTests(TestContext context)
{
private static SidecarFrame Frame(int i, string file, ImageFileFormat format, string? sha = "s", FrameFlags flags = FrameFlags.None,
DecodeTier tier = default) => new()
{
Id = Guid.NewGuid(),
I = i,
File = "originals/" + file,
Size = 1000 + i,
Sha256 = sha is null ? null : sha + i,
Format = format,
W = 6000,
H = 4000,
Flags = flags,
Tier = tier,
};
[Test]
public void EachFrameThatCannotConvertSaysWhy()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("2026/roof");
SidecarFrame[] frames =
[
Frame(0, "IMG_0001.CR2", ImageFileFormat.Cr2),
Frame(1, "IMG_0002.DNG", ImageFileFormat.Dng),
Frame(2, "IMG_0003.CR2", ImageFileFormat.Cr2),
Frame(3, "IMG_0004.CR2", ImageFileFormat.Cr2, sha: null),
Frame(4, "IMG_0005.CR2", ImageFileFormat.Cr2, flags: FrameFlags.Undecodable),
Frame(5, "IMG_0006.CR3", ImageFileFormat.Cr3),
Frame(6, "IMG_0007.HEIC", ImageFileFormat.Heif),
Frame(7, "IMG_0008.JPG", ImageFileFormat.Jpeg, flags: FrameFlags.Missing),
Frame(8, "IMG_0009.CR2", ImageFileFormat.Cr2, flags: FrameFlags.PreviewOnly | FrameFlags.Excluded, tier: DecodeTier.EmbeddedPreview),
];
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames, Versions = new ManifestVersions { Frames = 7 } };
// The third photo's file is gone; the preview-only one probes as preview here, the first one native.
var present = frames.Where(f => f.I != 2).Select(f => f.File).ToList();
var decoder = new ImageDecoder([new ProbeAs(DecodeTier.EmbeddedPreview, path => path.EndsWith("IMG_0009.CR2", StringComparison.Ordinal))]);
var plan = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.LinearDng), decoder, "Roof");
Assert.Equal((manifest.Id, "Roof", "2026/roof", 7), (plan.SequenceId, plan.Title, plan.Folder, plan.FramesVersion));
Assert.SequenceEqual([0], plan.Frames.Select(f => f.Index));
Assert.Equal("s0", plan.Frames.Single().Sha256);
Assert.SequenceEqual(
[(ConvertSkip.SameFormat, 1), (ConvertSkip.NoNativeDecoder, 2), (ConvertSkip.PreviewOnly, 1), (ConvertSkip.Undecodable, 1), (ConvertSkip.Missing, 2),
(ConvertSkip.Unhashed, 1)],
plan.Skipped.Select(s => (s.Reason, s.Frames)));
Assert.True(plan.Converts);
Assert.Equal(1000, plan.BytesBefore);
Assert.Equal(ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.LinearDng), 6000, 4000), plan.BytesAfter);
Assert.Equal(8, plan.SkippedFrames);
}
[Test]
public void APhotoRecordedAsPreviewOnlyElsewhereConvertsWhereItDecodes()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
var frame = Frame(0, "IMG_0001.CR2", ImageFileFormat.Cr2, flags: FrameFlags.PreviewOnly, tier: DecodeTier.EmbeddedPreview);
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = [frame] };
var plan = ConvertPlanner.Plan(manifest, folder, [frame.File], new ConvertOptions(ConvertTarget.Tiff16), new ImageDecoder([new ProbeAs(DecodeTier.Native)]), "Roof");
Assert.Count(1, plan.Frames);
Assert.Empty(plan.Skipped);
}
/// <summary>
/// A frame whose tier is not known yet (imported, not analysed) or was recorded native by another host with other
/// decoders is asked again: the dry run promises only what the file says here now. A file the decoders here do not read
/// at all, although they read its format, is left to a PC; only a frame flagged unreadable is unreadable.
/// </summary>
[Test]
public void AFrameIsAskedWhateverTierWasRecorded()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
SidecarFrame[] frames = [Frame(0, "IMG_0001.CR2", ImageFileFormat.Cr2), Frame(1, "IMG_0002.CR2", ImageFileFormat.Cr2, tier: DecodeTier.Native)];
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var present = frames.Select(f => f.File).ToList();
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var preview = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ProbeAs(DecodeTier.EmbeddedPreview)]), "Roof");
Assert.Empty(preview.Frames);
Assert.SequenceEqual([(ConvertSkip.PreviewOnly, 2)], preview.Skipped.Select(s => (s.Reason, s.Frames)));
var unreadable = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ProbeAs(DecodeTier.Unavailable)]), "Roof");
Assert.SequenceEqual([(ConvertSkip.NoNativeDecoder, 2)], unreadable.Skipped.Select(s => (s.Reason, s.Frames)));
var native = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ProbeAs(DecodeTier.Native)]), "Roof");
Assert.SequenceEqual([0, 1], native.Frames.Select(f => f.Index));
}
/// <summary>
/// A file the plan cannot read is not called missing unless it is gone: one another program holds, or that access is
/// denied to, could not be opened now (a plan made once it is free converts it), and one shorter than its own headers
/// cannot be decoded.
/// </summary>
[Test]
public void AFileThatCannotBeReadNowSaysWhy()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
Exception[] failures =
[
new FileNotFoundException("gone"), new DirectoryNotFoundException("gone"), new IOException("held by another process"),
new UnauthorizedAccessException("denied"), new EndOfStreamException("short"),
];
SidecarFrame[] frames = [.. failures.Select((_, i) => Frame(i, $"IMG_{i:0000}.CR2", ImageFileFormat.Cr2))];
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
// IMG_0000 fails as the first, IMG_0004 as the last.
var decoder = new ImageDecoder([new ProbeFails(path => failures[Path.GetFileNameWithoutExtension(path)[^1] - '0'])]);
var plan = ConvertPlanner.Plan(manifest, folder, [.. frames.Select(f => f.File)], new ConvertOptions(ConvertTarget.LinearDng), decoder, "Roof");
Assert.Empty(plan.Frames);
Assert.SequenceEqual([(ConvertSkip.Undecodable, 1), (ConvertSkip.Missing, 2), (ConvertSkip.Inaccessible, 2)], plan.Skipped.Select(s => (s.Reason, s.Frames)));
}
/// <summary>A RAW decoder whose probe of a file fails as <paramref name="failure"/> says.</summary>
private sealed class ProbeFails(Func<string, Exception> failure) : IImageDecoderProvider
{
public string Name => "fails";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Cr2 ? DecodeTier.Native : null;
public FileTier Probe(string path, ImageFileFormat format) => throw failure(path);
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
}
/// <summary>
/// The managed decoder develops every RAW it reads natively (at float precision) even where WIC could; a RAW only WIC's
/// Raw Image Extension develops, at 8 bits, converts to JPEG but not to TIFF-16 or linear DNG.
/// </summary>
[Test]
public void ARawDevelopedAtEightBitsOnlyConvertsToJpegOnly()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
using var scene = SyntheticRaw.Smooth(96, 64);
var active = new RawRect(24, 4, 24 + 96, 4 + 64);
var sensor = NativeRawWriters.Sensor(scene, "Canon EOS 5D Mark III", CfaPattern.Bayer("RGGB"), 128, 72, active.Left, active.Top, 2048, 16383);
SidecarFrame[] frames =
[
Frame(0, "IMG_0001.CR2", ImageFileFormat.Cr2, tier: DecodeTier.Native),
Frame(1, "IMG_0002.CR2", ImageFileFormat.Cr2, tier: DecodeTier.Native),
Frame(2, "IMG_0003.CR3", ImageFileFormat.Cr3, tier: DecodeTier.Native),
];
File.WriteAllBytes(folder.File(frames[0].File), NativeRawWriters.Cr2(sensor, active));
// A body the managed decoder has no matrix for: only the 8-bit development reads it.
File.WriteAllBytes(folder.File(frames[1].File), NativeRawWriters.Cr2(sensor, active, model: "Canon EOS R99"));
File.WriteAllBytes(folder.File(frames[2].File), [0]);
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var present = frames.Select(f => f.File).ToList();
var desktop = new ImageDecoder([new EightBitRaw(), new ManagedDecoderProvider()]);
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
{
var plan = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(target), desktop, "Roof");
Assert.SequenceEqual([0], plan.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.EightBit, 2)], plan.Skipped.Select(s => (s.Reason, s.Frames)));
}
var jpeg = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.Jpeg), desktop, "Roof");
Assert.SequenceEqual([0, 1, 2], jpeg.Frames.Select(f => f.Index));
// The server, without WIC: the unsupported body and the CR3 are to convert on a PC.
var server = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.Tiff16), DecoderFactory.CreateServer(), "Roof");
Assert.SequenceEqual([0], server.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.NoNativeDecoder, 1), (ConvertSkip.PreviewOnly, 1)], server.Skipped.Select(s => (s.Reason, s.Frames)));
}
/// <summary>A photo whose colour space no target can keep, or which a target cannot hold, is planned out with why.</summary>
[Test]
public void ColourAndSizeTheTargetCannotHoldArePlannedOut()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
using var scene = ColorFixtures.Scene();
SidecarFrame[] frames =
[
Frame(0, "plain.jpg", ImageFileFormat.Jpeg),
Frame(1, "p3.jpg", ImageFileFormat.Jpeg),
Frame(2, "unknown.jpg", ImageFileFormat.Jpeg),
Frame(3, "cmyk.jpg", ImageFileFormat.Jpeg),
Frame(4, "panorama.png", ImageFileFormat.Png) with { W = 70_000, H = 2_500 },
Frame(5, "huge.png", ImageFileFormat.Png) with { W = 70_000, H = 12_000 },
];
File.WriteAllBytes(folder.File(frames[0].File), ColorFixtures.Jpeg(scene));
File.WriteAllBytes(folder.File(frames[1].File), ColorFixtures.Jpeg(scene, IccProfile.DisplayP3.Data));
File.WriteAllBytes(folder.File(frames[2].File), ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)));
File.WriteAllBytes(folder.File(frames[3].File), ColorFixtures.Jpeg(scene, ColorFixtures.Cmyk()));
// A panorama's header (the dry run reads no further), and a file the sidecar's size already rules out.
File.WriteAllBytes(folder.File(frames[4].File), ColorFixtures.PngHeader(70_000, 2_500));
File.WriteAllBytes(folder.File(frames[5].File), [0]);
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var present = frames.Select(f => f.File).ToList();
var decoder = DecoderFactory.CreateServer();
// A TIFF carries the P3 photo's profile; a DNG, which holds sRGB as Chronos reads it, does not take it. Neither takes
// the unknown and CMYK ones, nor 2.5 G samples.
var tiff = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.Tiff16), decoder, "Roof");
Assert.SequenceEqual([0, 1, 4], tiff.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 2), (ConvertSkip.TooLarge, 1)], tiff.Skipped.Select(s => (s.Reason, s.Frames)));
var dng = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.LinearDng), decoder, "Roof");
Assert.SequenceEqual([0, 4], dng.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 3), (ConvertSkip.TooLarge, 1)], dng.Skipped.Select(s => (s.Reason, s.Frames)));
// A JPEG side holds 65535 pixels.
var jpeg = ConvertPlanner.Plan(manifest, folder, present, new ConvertOptions(ConvertTarget.Jpeg), decoder, "Roof");
Assert.SequenceEqual([(ConvertSkip.SameFormat, 4), (ConvertSkip.TooLarge, 2)], jpeg.Skipped.Select(s => (s.Reason, s.Frames)));
Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(ConvertTarget.Jpeg), 65_536, 10));
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(ConvertTarget.Jpeg), 65_535, 10));
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(ConvertTarget.Tiff16), 0, 0));
}
/// <summary>
/// The size bound is what the server's decoders read back, which every converted file must (owner review 2, R7), never
/// an estimate: a JPEG of 2^28 pixels, a TIFF-16 or DNG (whose raster the same TIFF reader reads) of 2^29 samples, whose
/// headers are read while one row more is refused. A file that size stays within the array it is made in (2 GB) even
/// when its pixels do not compress at all (16-bit noise takes 48 bits a pixel, up to 51 as lossless JPEG codes them),
/// and noise encodes within that bound in every target.
/// </summary>
[Test]
public void TheSizeBoundIsWhatTheServersDecodersReadBack()
{
var jpeg = new ConvertOptions(ConvertTarget.Jpeg, 100);
Assert.False(ConvertPlanner.TooLarge(jpeg, 16_384, 16_384));
Assert.True(ConvertPlanner.TooLarge(jpeg, 16_384, 16_385));
Assert.False(ConvertPlanner.TooLarge(jpeg, 4_096, 65_535));
Assert.True(ConvertPlanner.TooLarge(jpeg, 4_097, 65_535));
var largest = JpegDecoder.ReadInfo(ColorFixtures.JpegHeader(16_384, 16_384));
Assert.Equal((16_384, 16_384), (largest.Width, largest.Height));
Assert.Throws<InvalidDataException>(() => JpegDecoder.ReadInfo(ColorFixtures.JpegHeader(16_384, 16_385)));
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
{
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(target), 13_377, 13_377), $"{target} of 2^29 samples at most");
Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(target), 13_377, 13_378), $"{target} of one row more");
}
foreach (ushort photometric in (ushort[])[2, 34892])
{
Assert.Equal((13_377, 13_377), TiffImageReader.ReadInfo(new MemoryStream(ColorFixtures.TiffHeader(13_377, 13_377, photometric))));
Assert.Throws<InvalidDataException>(() => TiffImageReader.ReadInfo(new MemoryStream(ColorFixtures.TiffHeader(13_377, 13_378, photometric))));
}
Assert.LessOrEqual(ConvertPlanner.WorstBytes(16_384, 16_384), Array.MaxLength);
Assert.LessOrEqual(ConvertPlanner.WorstBytes(13_377, 13_377), Array.MaxLength);
// 342 MP: its estimate (30 or 26 bits a pixel) would fit, the worst case would not, and nothing reads it back.
foreach (var target in Enum.GetValues<ConvertTarget>())
{
Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(target, 100), 18_500, 18_500), $"{target} at 342 MP");
Assert.Less(ConvertPlanner.EstimateBytes(new ConvertOptions(target, 100), 18_500, 18_500), Array.MaxLength);
}
Assert.Greater(ConvertPlanner.WorstBytes(18_500, 18_500), Array.MaxLength);
using var noise = new ImageBuffer(512, 512);
var random = new Random(7);
foreach (ref var v in noise.Data) v = random.NextSingle();
var metadata = new Chronos.Core.Metadata.FrameMetadata { CapturedAt = new DateTime(2026, 10, 27, 12, 0, 0) };
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var bytes = FrameConverter.Encode(noise, metadata, new ConvertOptions(target, 100));
var bits = bytes.Length * 8.0 / noise.PlaneLength;
context.Log($"{target}: {bits:0.00} bits a pixel of noise");
Assert.LessOrEqual(bits, ConvertPlanner.WorstBitsPerPixel, $"{target} encodes noise within the bound, its headers included");
Assert.LessOrEqual(bytes.Length, ConvertPlanner.WorstBytes(512, 512));
}
}
/// <summary>
/// Any size is answered, a damaged header's included (owner review 2, R7): sides up to <see cref="int.MaxValue"/> are too
/// large for every target, never let through by a product that wrapped around, and the worst case saturates instead of
/// turning negative.
/// </summary>
[Test]
public void AnySizeIsAnsweredWithoutOverflow()
{
(int W, int H)[] absurd =
[
(int.MaxValue, int.MaxValue), (2_000_000_000, 2_000_000_000), (1_800_000_000, 1_800_000_000), (1_000_000_000, 1_000_000_000),
(int.MaxValue, 1), (1, int.MaxValue),
];
foreach (var target in Enum.GetValues<ConvertTarget>())
{
foreach (var (w, h) in absurd) Assert.True(ConvertPlanner.TooLarge(new ConvertOptions(target), w, h), $"{target} at {w}×{h}");
Assert.Greater(ConvertPlanner.EstimateBytes(new ConvertOptions(target), int.MaxValue, int.MaxValue), 0L);
}
Assert.Equal(long.MaxValue, ConvertPlanner.WorstBytes(int.MaxValue, int.MaxValue));
Assert.Greater(ConvertPlanner.WorstBytes(1_800_000_000, 1_800_000_000), Array.MaxLength);
Assert.Greater(ConvertPlanner.WorstBytes(65_535, 65_535), 0L);
Assert.False(ConvertPlanner.TooLarge(new ConvertOptions(ConvertTarget.Tiff16), -5, 10));
}
/// <summary>
/// A frame flagged unreadable by the host that analysed it (the server, whose decoders refuse a CMYK TIFF) is asked again
/// where the conversion's decoders have others for its format (WIC on a PC), with a small decode (owner review 2, R7):
/// converted where they read it, unreadable where they do not. A corrupt frame stays unreadable, and so does a flagged one
/// where only the managed decoders read the format. A file that cannot be read while it is asked is inaccessible, nothing
/// concluded of its content.
/// </summary>
[Test]
public void AFrameFlaggedUnreadableElsewhereIsAskedWhereOtherDecodersReadIt()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
SidecarFrame[] frames =
[
Frame(0, "cmyk.tif", ImageFileFormat.Tiff, flags: FrameFlags.Undecodable) with { W = 6, H = 4 },
Frame(1, "corrupt.tif", ImageFileFormat.Tiff, flags: FrameFlags.Corrupt) with { W = 6, H = 4 },
];
File.WriteAllBytes(folder.File(frames[0].File), ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
File.WriteAllBytes(folder.File(frames[1].File), ColorFixtures.Tiff(6, 4, 2, [1, 2, 3]));
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var present = frames.Select(f => f.File).ToList();
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
IEnumerable<(ConvertSkip, int)> Skipped(ConvertSequencePlan plan) => plan.Skipped.Select(s => (s.Reason, s.Frames));
var server = ConvertPlanner.Plan(manifest, folder, present, jpeg, DecoderFactory.CreateServer(), "Roof");
Assert.Empty(server.Frames);
Assert.SequenceEqual([(ConvertSkip.Undecodable, 2)], Skipped(server));
var reads = ConvertPlanner.Plan(manifest, folder, present, jpeg, new ImageDecoder([new DecodesTiffs(decodes: true), new ManagedDecoderProvider()]), "Roof");
Assert.SequenceEqual([0], reads.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.Undecodable, 1)], Skipped(reads));
var fails = ConvertPlanner.Plan(manifest, folder, present, jpeg, new ImageDecoder([new DecodesTiffs(decodes: false), new ManagedDecoderProvider()]), "Roof");
Assert.SequenceEqual([(ConvertSkip.Undecodable, 2)], Skipped(fails));
var held = ConvertPlanner.Plan(manifest, folder, present, jpeg,
new ImageDecoder([new DecodesTiffs(decodes: true, failure: new IOException("held by another process")), new ManagedDecoderProvider()]), "Roof");
Assert.Empty(held.Frames);
Assert.SequenceEqual([(ConvertSkip.Undecodable, 1), (ConvertSkip.Inaccessible, 1)], Skipped(held));
}
/// <summary>
/// WIC as a PC has it, for TIFFs: native by format (its probe trusts it), and a decode that reads a CMYK TIFF, finds
/// nothing in it, or fails as <paramref name="failure"/> says.
/// </summary>
private sealed class DecodesTiffs(bool decodes, Exception? failure = null) : IImageDecoderProvider
{
public string Name => "wic";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Tiff ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
if (failure is not null) throw failure;
if (!decodes) return null;
var red = new ImageBuffer(6, 4);
red.Plane(0).Fill(0.6f);
return new DecodedImage(red, DecodeTier.Native, Name, 6, 4);
}
}
/// <summary>
/// The dry run reads the headers of TIFF, PNG and JPEG files before it promises them (owner review 2, R7): on the server,
/// a CMYK, CIELab, palette or ZSTD TIFF, one with more samples than its decoder takes and an arithmetic-coded JPEG are
/// left to a PC, never planned to fail at every retry; where another decoder reads them (WIC on a PC), they convert. A
/// photo larger than a converted file may be stays as it is everywhere.
/// </summary>
[Test]
public void WhatTheServersDecodersRefuseIsLeftToAPc()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
(string File, ImageFileFormat Format, byte[] Bytes, int W, int H)[] files =
[
("rgb.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 2, [200, 30, 10]), 6, 4),
("cmyk.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]), 6, 4),
("lab.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 8, [50, 60, 70]), 6, 4),
("palette.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 3, [1]), 6, 4),
("zstd.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 2, [1, 2, 3], d => d.Short(TiffTags.Compression, 50000)), 6, 4),
// RGBA of 576 M samples: beyond the managed reader, while its DNG (432 M) is not.
("stitched.tif", ImageFileFormat.Tiff, ColorFixtures.Tiff(6, 4, 2, [1, 2, 3, 4], d =>
{
d.Long(TiffTags.ImageWidth, 12_000);
d.Long(TiffTags.ImageLength, 12_000);
}), 12_000, 12_000),
("arithmetic.jpg", ImageFileFormat.Jpeg, ColorFixtures.JpegHeader(6, 4, sof: 0xC9), 6, 4),
("stitched.png", ImageFileFormat.Png, ColorFixtures.PngHeader(17_000, 17_000), 17_000, 17_000),
];
var frames = files.Select((f, i) => Frame(i, f.File, f.Format) with { W = f.W, H = f.H }).ToArray();
for (var i = 0; i < files.Length; i++) File.WriteAllBytes(folder.File(frames[i].File), files[i].Bytes);
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = frames };
var present = frames.Select(f => f.File).ToList();
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var server = ConvertPlanner.Plan(manifest, folder, present, dng, DecoderFactory.CreateServer(), "Roof");
Assert.SequenceEqual([0], server.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.NoNativeDecoder, 6), (ConvertSkip.TooLarge, 1)], server.Skipped.Select(s => (s.Reason, s.Frames)));
// A PC whose WIC reads them: the managed decoders first, WIC for what they refuse.
var desktop = ConvertPlanner.Plan(manifest, folder, present, dng, new ImageDecoder([new ReadsEverything(), new ManagedDecoderProvider()]), "Roof");
Assert.SequenceEqual(Enumerable.Range(0, 7), desktop.Frames.Select(f => f.Index));
Assert.SequenceEqual([(ConvertSkip.TooLarge, 1)], desktop.Skipped.Select(s => (s.Reason, s.Frames)));
// The managed provider of a conversion reads headers; the import's does not open the files.
Assert.True(FrameConverter.Decoders(DecoderFactory.CreateServer(), dng, ImageFileFormat.Tiff).Providers.OfType<ManagedDecoderProvider>().Single().ReadsHeaders);
Assert.Equal(DecodeTier.Native, DecoderFactory.CreateServer().Probe(folder.File(frames[1].File), ImageFileFormat.Tiff).Tier);
}
/// <summary>A photo whose profile's colorants convert nothing is planned out of a DNG, which converts through them, and carried into a TIFF.</summary>
[Test]
public void AProfileThatConvertsNothingKeepsItsPhotoOutOfADng()
{
var folder = new ArchivePaths(context.CreateTempDirectory("archive")).Sequence("roof");
System.IO.Directory.CreateDirectory(Path.GetDirectoryName(folder.File("originals/x"))!);
using var scene = ColorFixtures.Scene();
var frame = Frame(0, "odd.jpg", ImageFileFormat.Jpeg);
File.WriteAllBytes(folder.File(frame.File), ColorFixtures.Jpeg(scene, ColorFixtures.Degenerate()));
var manifest = new SequenceManifest { Id = Guid.NewGuid(), Frames = [frame] };
var dng = ConvertPlanner.Plan(manifest, folder, [frame.File], new ConvertOptions(ConvertTarget.LinearDng), DecoderFactory.CreateServer(), "Roof");
Assert.SequenceEqual([(ConvertSkip.ColorProfile, 1)], dng.Skipped.Select(s => (s.Reason, s.Frames)));
var tiff = ConvertPlanner.Plan(manifest, folder, [frame.File], new ConvertOptions(ConvertTarget.Tiff16), DecoderFactory.CreateServer(), "Roof");
Assert.Count(1, tiff.Frames);
}
[Test]
public void SizesAreEstimatedFromThePixels()
{
const long pixels = 6000L * 4000;
Assert.Equal((long)Math.Ceiling(pixels * 30 / 8.0), ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.Tiff16), 6000, 4000));
Assert.Equal((long)Math.Ceiling(pixels * 26 / 8.0), ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.LinearDng), 6000, 4000));
Assert.Equal((long)Math.Ceiling(pixels * ExportEstimator.JpegBitsPerPixel(95) * 1.25 / 8),
ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.Jpeg), 6000, 4000));
Assert.Less(ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.Jpeg, 50), 6000, 4000),
ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.Jpeg, 95), 6000, 4000));
// Unknown size: a 24 MP frame.
Assert.Equal(ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.Tiff16), 6000, 4000), ConvertPlanner.EstimateBytes(new ConvertOptions(ConvertTarget.Tiff16), 0, 0));
Assert.Equal((long)Math.Ceiling(1_000_000_000 * 1.15) + (512L << 20), ConvertPlanner.Required(1_000_000_000));
Assert.True(new ConvertSpace(100, null).Enough);
Assert.True(new ConvertSpace(100, 100).Enough);
Assert.False(new ConvertSpace(100, 99).Enough);
}
[Test]
public void TheFingerprintFollowsWhatThePlanConverts()
{
var id = Guid.NewGuid();
ConvertSequencePlan Sequence(int version, int frames, params ConvertBlock[] blocked) =>
new(id, "Roof", "roof", version, [.. Enumerable.Range(0, frames).Select(i => new ConvertFramePlan(Guid.Empty, i, $"f{i}", ImageFileFormat.Cr2, 10, 20))],
[], blocked);
ConvertPlan Plan(ConvertOptions options, ConvertMode mode, ConvertSequencePlan sequence) => new(Guid.Empty, options, mode, [sequence], new ConvertSpace(0, null));
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var reference = Plan(dng, ConvertMode.Replace, Sequence(3, 2)).Fingerprint();
Assert.Equal(64, reference.Length);
Assert.Equal(reference, Plan(dng with { Quality = 40 }, ConvertMode.Replace, Sequence(3, 2)).Fingerprint());
Assert.NotEqual(reference, Plan(dng, ConvertMode.NewSequence, Sequence(3, 2)).Fingerprint());
Assert.NotEqual(reference, Plan(new ConvertOptions(ConvertTarget.Jpeg), ConvertMode.Replace, Sequence(3, 2)).Fingerprint());
Assert.NotEqual(reference, Plan(dng, ConvertMode.Replace, Sequence(4, 2)).Fingerprint());
Assert.NotEqual(reference, Plan(dng, ConvertMode.Replace, Sequence(3, 1)).Fingerprint());
var busy = Plan(dng, ConvertMode.Replace, Sequence(3, 2, ConvertBlock.Busy));
Assert.NotEqual(reference, busy.Fingerprint());
Assert.Equal((0, 0L), (busy.Frames, busy.BytesAfter));
Assert.NotEqual(Plan(new ConvertOptions(ConvertTarget.Jpeg, 90), ConvertMode.Replace, Sequence(3, 2)).Fingerprint(),
Plan(new ConvertOptions(ConvertTarget.Jpeg, 80), ConvertMode.Replace, Sequence(3, 2)).Fingerprint());
}
[Test]
public void TheDigestFollowsThePhotosConvertedAndTheirContent()
{
var a = Guid.NewGuid();
var b = Guid.NewGuid();
ConvertSequencePlan Sequence(int version, params (Guid Id, string Sha)[] frames) =>
new(Guid.Empty, "Roof", "roof", version, [.. frames.Select((f, i) => new ConvertFramePlan(f.Id, i, $"f{i}", ImageFileFormat.Cr2, 10, 20, f.Sha))], [], []);
var reference = Sequence(3, (a, "aa"), (b, "bb")).Digest();
Assert.Equal(64, reference.Length);
// The frames version and the case of a hash say nothing of what converts.
Assert.Equal(reference, Sequence(4, (a, "AA"), (b, "bb")).Digest());
Assert.Equal(reference, ConvertSequencePlan.DigestOf([(a, "aa"), (b, "bb")]));
// Another content, a photo fewer or more (verified since the plan), the same photos in another order: another digest.
Assert.NotEqual(reference, Sequence(3, (a, "aa"), (b, "cc")).Digest());
Assert.NotEqual(reference, Sequence(3, (a, "aa")).Digest());
Assert.NotEqual(reference, Sequence(3, (a, "aa"), (b, "bb"), (Guid.NewGuid(), "dd")).Digest());
Assert.NotEqual(reference, Sequence(3, (b, "bb"), (a, "aa")).Digest());
// The plan's fingerprint pins the content too: as many photos of the same size holding other content is another plan.
ConvertPlan Plan(ConvertSequencePlan sequence) => new(Guid.Empty, new ConvertOptions(ConvertTarget.LinearDng), ConvertMode.Replace, [sequence], new ConvertSpace(0, null));
Assert.NotEqual(Plan(Sequence(3, (a, "aa"), (b, "bb"))).Fingerprint(), Plan(Sequence(3, (a, "aa"), (b, "cc"))).Fingerprint());
}
/// <summary>A provider that reads CR2 natively by format, and probes the files <paramref name="applies"/> picks (all
/// by default) at a fixed tier, the others native.</summary>
private sealed class ProbeAs(DecodeTier tier, Func<string, bool>? applies = null) : IImageDecoderProvider
{
public string Name => "probe";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Cr2 ? DecodeTier.Native : null;
public FileTier Probe(string path, ImageFileFormat format) => new(format, applies?.Invoke(path) == false ? DecodeTier.Native : tier, Name, null, 0, 0);
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
}
/// <summary>WIC as a PC with every codec has it: JPEG, PNG and TIFF native, whatever they hold.</summary>
private sealed class ReadsEverything : IImageDecoderProvider
{
public string Name => "wic";
public DecodeTier? TierFor(ImageFileFormat format) => format is ImageFileFormat.Jpeg or ImageFileFormat.Png or ImageFileFormat.Tiff ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
}
/// <summary>WIC with the Raw Image Extension, as a conversion sees it: every camera RAW native, developed at 8 bits.</summary>
internal sealed class EightBitRaw : IImageDecoderProvider
{
public string Name => "wic";
public bool DevelopsRawAt8Bits => true;
public DecodeTier? TierFor(ImageFileFormat format) => ImageFormatInfo.Of(format).Family == ImageFamily.CameraRaw ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null;
}
}
@@ -0,0 +1,866 @@
using System.Globalization;
using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Icc;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Png;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Localization;
using Chronos.Core.Mathematics;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Testing;
namespace Chronos.Core.Tests.Conversion;
/// <summary>
/// One photo converted (owner review 2, R7): to JPEG, TIFF-16 and linear DNG, decoded at full resolution only, with the
/// sidecar's capture facts to the tick and the original's camera, lens, modes and GPS, verified (read back, metadata, the
/// server's decoders) before it counts.
/// </summary>
[Category("conversion")]
public sealed class FrameConverterTests(TestContext context)
{
private static readonly DateTime Captured = new DateTime(2026, 9, 28, 18, 0, 5).AddTicks(1_234_567);
/// <summary>The camera's file says one second earlier, without sub-seconds: the sidecar wins.</summary>
private static readonly FrameMetadata Camera = new()
{
Make = "Canon",
Model = "Canon EOS R5",
BodySerial = "032021001234",
LensModel = "RF15-35mm F2.8 L IS USM",
CapturedAt = new DateTime(2026, 9, 28, 18, 0, 4),
UtcOffsetMinutes = 60,
ExposureTime = 1 / 250.0,
FNumber = 8,
Iso = 100,
FocalLength = 15,
FocalLength35 = 15,
ExposureBias = -2 / 3.0,
ExposureProgram = 3,
MeteringMode = 5,
Flash = 16,
WhiteBalanceMode = 1,
Artist = "Alberto",
Gps = new GpsPosition(45.4642035, 9.1899817, 122.5, null),
};
private static SidecarFrame Frame(string file, ImageFileFormat format, int width, int height) => new()
{
Id = Guid.NewGuid(),
File = "originals/" + file,
Original = file,
Sha256 = new string('a', 64),
CapturedAt = Captured,
UtcOffset = 120,
TimeSource = TimestampSource.ExifOriginal,
Exposure = 1 / 125.0,
FNumber = 5.6,
Iso = 200,
Focal = 24,
Bias = -1,
W = width,
H = height,
Format = format,
Flags = FrameFlags.Intervalometer,
};
/// <summary>A gradient with some texture, so every encoder has real work and errors show.</summary>
private 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.05f + 0.6f * x / width;
image[1, x, y] = 0.1f + 0.5f * y / height;
image[2, x, y] = 0.2f + 0.2f * ((x / 8 + y / 8) % 2);
}
}
return image;
}
private string Source(int width = 96, int height = 64, int orientation = 1)
{
var path = Path.Combine(context.CreateTempDirectory("source"), "IMG_0001.JPG");
using var image = Scene(width, height);
var exif = ExifWriter.BuildJpegApp1(Camera with { Orientation = orientation });
File.WriteAllBytes(path, JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 97, Subsampling = JpegSubsampling.Yuv444, App1Segments = [exif[4..]] }));
return path;
}
private string Target(ConvertOptions options) => Path.Combine(context.CreateTempDirectory("staged"), "converted" + options.Extension);
[Theory]
[Case(ConvertTarget.Jpeg)]
[Case(ConvertTarget.Tiff16)]
[Case(ConvertTarget.LinearDng)]
public void TheSidecarsCaptureFactsAndTheCamerasMetadataRoundTrip(ConvertTarget target)
{
var options = new ConvertOptions(target);
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
Assert.Equal((options.Format, 96, 64), (file.Format, file.Width, file.Height));
Assert.Equal(new FileInfo(file.Path).Length, file.Size);
Assert.Equal(file.Sha256, ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult());
var read = MetadataReader.Read(file.Path);
Assert.Equal(options.Format, read.Format);
// The sidecar wins: the capture time to the tick, its offset, exposure, aperture, ISO, focal length and bias.
Assert.Equal(Captured, read.CapturedAt);
Assert.Equal(120, read.UtcOffsetMinutes);
Assert.Equal(TimestampSource.ExifOriginal, read.TimestampSource);
Assert.Approximately(1 / 125.0, read.ExposureTime!.Value, 1e-12);
Assert.Approximately(5.6, read.FNumber!.Value, 1e-9);
Assert.Equal(200, read.Iso);
Assert.Approximately(24, read.FocalLength!.Value, 1e-9);
Assert.Approximately(-1, read.ExposureBias!.Value, 1e-4);
// The original's own: camera, serial, lens, 35 mm focal length, modes, GPS, artist.
Assert.Equal(("Canon", "Canon EOS R5", "032021001234", "RF15-35mm F2.8 L IS USM"), (read.Make, read.Model, read.BodySerial, read.LensModel));
Assert.Approximately(15, read.FocalLength35!.Value, 1e-9);
Assert.Equal((3, 5, 16, 1), (read.ExposureProgram, read.MeteringMode, read.Flash, read.WhiteBalanceMode));
var gps = Assert.NotNull(read.Gps);
Assert.Approximately(45.4642035, gps.Latitude, 1e-6);
Assert.Approximately(9.1899817, gps.Longitude, 1e-6);
Assert.Equal(1, read.Orientation);
Assert.Equal((96, 64), read.OrientedSize);
}
[Theory]
[Case(ConvertTarget.Jpeg)]
[Case(ConvertTarget.Tiff16)]
[Case(ConvertTarget.LinearDng)]
public void TheConvertedFileDecodesAtFullResolutionWithTheServersDecoders(ConvertTarget target)
{
var options = new ConvertOptions(target);
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var server = DecoderFactory.CreateServer();
Assert.Equal(DecodeTier.Native, server.Probe(file.Path, file.Format).Tier);
using var original = server.Decode(new DecodeRequest(source, AllowPreview: false))!;
using var converted = Assert.NotNull(server.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
Assert.Equal(DecodeTier.Native, converted.Tier);
Assert.Equal((96, 64), (converted.Pixels.Width, converted.Pixels.Height));
// The same picture: TIFF-16 and DNG keep it to 16 bits, the JPEG within its quantisation.
var tolerance = target == ConvertTarget.Jpeg ? 0.03 : 0.01;
var error = 0.0;
for (var c = 0; c < 3; c++)
{
var a = original.Pixels.Plane(c);
var b = converted.Pixels.Plane(c);
for (var i = 0; i < a.Length; i++) error += Math.Abs(a[i] - b[i]);
}
Assert.Less(error / (3.0 * 96 * 64), tolerance, $"mean error of {target}");
}
[Test]
public void TheJpegQualityAndFullChromaAreTheOnesAsked()
{
var source = Source(160, 120);
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 160, 120);
var decoder = DecoderFactory.CreateServer();
var high = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Jpeg, 95), decoder,
Target(new ConvertOptions(ConvertTarget.Jpeg)), cancellationToken: context.Cancellation);
var low = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Jpeg, 20), decoder,
Target(new ConvertOptions(ConvertTarget.Jpeg)), cancellationToken: context.Cancellation);
Assert.Less(low.Size, high.Size);
var info = JpegDecoder.ReadInfo(File.ReadAllBytes(high.Path));
Assert.Equal("4:4:4", info.SamplingNotation);
Assert.Equal(ConvertOptions.DefaultQuality, new ConvertOptions(ConvertTarget.Jpeg).Quality);
}
[Test]
public void TiffAndLinearDngAreSixteenBitsAndReadByTheInHouseReaders()
{
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var decoder = DecoderFactory.CreateServer();
var tiff = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Tiff16), decoder,
Target(new ConvertOptions(ConvertTarget.Tiff16)), cancellationToken: context.Cancellation);
var dng = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.LinearDng), decoder,
Target(new ConvertOptions(ConvertTarget.LinearDng)), cancellationToken: context.Cancellation);
Assert.Equal(16, MetadataReader.Read(tiff.Path).BitsPerSample);
using (var back = TiffImageReader.Decode(File.ReadAllBytes(tiff.Path)))
Assert.Equal((96, 64, 3), (back.Width, back.Height, back.Channels));
Assert.Equal(ImageFileFormat.Dng, MetadataReader.Read(dng.Path).Format);
using (var back = TiffImageReader.Decode(File.ReadAllBytes(dng.Path)))
Assert.Equal((96, 64, 3), (back.Width, back.Height, back.Channels));
}
[Test]
public void APortraitOriginalIsConvertedUpright()
{
var source = Source(96, 64, orientation: 6);
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64) with { Orientation = 6 };
var options = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
Assert.Equal((64, 96), (file.Width, file.Height));
var read = MetadataReader.Read(file.Path);
Assert.Equal((1, 64, 96), (read.Orientation, read.Width, read.Height));
}
[Test]
public void ACameraAndLensTheFileLacksComeFromTheSequence()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.png");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, Chronos.Core.Codecs.Png.PngEncoder.Encode(image));
var frame = Frame("frame.png", ImageFileFormat.Png, 32, 24);
var options = new ConvertOptions(ConvertTarget.Tiff16);
var file = FrameConverter.Convert(new ConvertSource(path, frame, new SidecarCamera { Make = "Sony", Model = "ILCE-7RM5", Serial = "77" }, "FE 24mm F1.4 GM"),
options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var read = MetadataReader.Read(file.Path);
Assert.Equal(("Sony", "ILCE-7RM5", "77", "FE 24mm F1.4 GM"), (read.Make, read.Model, read.BodySerial, read.LensModel));
Assert.Equal(Captured, read.CapturedAt);
}
[Test]
public void APhotoReadOnlyFromItsPreviewIsNotConverted()
{
// A DNG the managed decoder reads natively, behind a chain that offers only a preview.
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.dng");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, LinearDngWriter.Write(image));
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var target = Target(options);
var preview = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([new PreviewOnlyProvider()]), target, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.PreviewOnly, preview.Reason);
Assert.FileMissing(target);
var none = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([]), target, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.NoDecoder, none.Reason);
}
[Test]
public void AFileThatDoesNotReadBackIsRefused()
{
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var written = FrameConverter.Metadata(MetadataReader.Read(source), new ConvertSource(source, frame));
Assert.DoesNotThrow(() => FrameConverter.Verify(file, written));
// A bit flipped on the way to the disk.
var bytes = File.ReadAllBytes(file.Path);
bytes[^10] ^= 0x40;
File.WriteAllBytes(file.Path, bytes);
Assert.Equal(ConversionFailure.NotVerified, Assert.Throws<ConversionException>(() => FrameConverter.Verify(file, written)).Reason);
// A capture time that is not the one written.
File.WriteAllBytes(file.Path, File.ReadAllBytes(source));
Assert.Throws<ConversionException>(() => FrameConverter.Verify(file with { Sha256 = ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult(), Size = new FileInfo(file.Path).Length }, written));
}
[Test]
public void ConvertedNamesAreTheNamesFilingGives()
{
var dng = new ConvertOptions(ConvertTarget.LinearDng);
Assert.Equal("originals/IMG_0001.dng", dng.WithExtension("originals/IMG_0001.CR2"));
Assert.Equal("frame.jpg", new ConvertOptions(ConvertTarget.Jpeg).WithExtension("frame"));
// The second frame of a sequence filed by a Canon, named with the sequence's camera, its number, the title and the
// name it had on the card, as an upload or a Reorganize names it; {ext} is the target's.
var first = Frame("2026-09-28_0001.CR3", ImageFileFormat.Cr3, 96, 64) with { I = 0, Original = "IMG_0001.CR3" };
var second = Frame("2026-09-28_0002.CR3", ImageFileFormat.Cr3, 96, 64) with { I = 1, Original = "IMG_0002.CR3", CapturedAt = Captured.AddSeconds(10) };
var sequence = new SequenceManifest
{
Id = Guid.NewGuid(),
Title = "Harbour",
Camera = new SidecarCamera { Make = "Canon", Model = "Canon EOS R5" },
Frames = [first, second],
};
Assert.Equal("originals/Harbour_Canon EOS R5_0002_IMG_0002.dng",
dng.FileName(PathTemplate.Parse("{title}_{camera}_{n}_{name}.{ext}"), second, sequence));
Assert.Equal("originals/2026-09-28_0002.tif",
new ConvertOptions(ConvertTarget.Tiff16).FileName(PathTemplate.Parse("{date}_{n}"), second, sequence));
Assert.Equal(PathTemplate.Parse("{date}_{n}").ExpandFile(FrameNaming.Context(second with { File = "originals/x.dng" }, sequence, "Harbour")),
Path.GetFileName(dng.FileName(PathTemplate.Parse("{date}_{n}"), second, sequence)));
// A frame adopted in place stays in its folder; a long name keeps the target's extension whole.
var adopted = second with { File = "IMG_0002.CR3", Original = new string('x', 300) + ".CR3" };
var named = dng.FileName(PathTemplate.Parse("{name}.{ext}"), adopted, sequence);
Assert.False(named.Contains('/'));
Assert.True(named.EndsWith(".dng", StringComparison.Ordinal) && named.Length <= PathTemplate.MaxSegmentLength, named);
Assert.Equal(ConvertTarget.LinearDng, ConvertOptions.ArchivalTarget);
Assert.False(new ConvertOptions(ConvertTarget.Jpeg, 0).IsValid);
Assert.False(new ConvertOptions(ConvertTarget.Jpeg, 101).IsValid);
Assert.Equal(new ConvertOptions(ConvertTarget.LinearDng, 10).Key, new ConvertOptions(ConvertTarget.LinearDng, 90).Key);
}
/// <summary>
/// The managed decoder develops a RAW wherever it reads it natively, ahead of WIC (owner review 2, R7): a 16-bit file is
/// never filled from WIC's 8-bit RAW development, which a JPEG may still use.
/// </summary>
[Test]
public void ASixteenBitFileNeverComesFromAnEightBitRawDevelopment()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.dng");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, LinearDngWriter.Write(image));
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var wic = new EightBitRaw();
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng, ConvertTarget.Jpeg])
{
var options = new ConvertOptions(target);
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([wic, new ManagedDecoderProvider()]), Target(options),
cancellationToken: context.Cancellation);
}
Assert.Equal(0, wic.Calls);
// Where only the 8-bit development reads it: refused for 16 bits, converted to JPEG.
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.LinearDng])
{
var options = new ConvertOptions(target);
var refused = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([wic]), Target(options), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.EightBit, refused.Reason);
}
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
FrameConverter.Convert(new ConvertSource(path, frame), jpeg, new ImageDecoder([wic]), Target(jpeg), cancellationToken: context.Cancellation);
Assert.Equal(1, wic.Calls);
}
/// <summary>A RAW developed above white and outside sRGB converts to a linear DNG that reads back the same.</summary>
[Test]
public void ALinearDngKeepsWhatTheRawHasAboveWhiteAndOutsideSrgb()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "IMG_0001.CR2");
using (var image = Scene(40, 30)) File.WriteAllBytes(path, JpegEncoder.Encode(image));
var developed = Scene(40, 30);
(developed[0, 1, 1], developed[1, 1, 1], developed[2, 1, 1]) = (2.4f, 1.9f, 1.2f);
(developed[0, 2, 1], developed[1, 2, 1], developed[2, 2, 1]) = (-0.2f, 1.0f, -0.07f);
var options = new ConvertOptions(ConvertTarget.LinearDng);
var file = FrameConverter.Convert(new ConvertSource(path, Frame("IMG_0001.CR2", ImageFileFormat.Cr2, 40, 30)), options,
new ImageDecoder([new Developed(developed)]), Target(options), cancellationToken: context.Cancellation);
using var back = Assert.NotNull(DecoderFactory.CreateServer().Decode(new DecodeRequest(file.Path, AllowPreview: false)));
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < developed.PlaneLength; i++) Assert.Approximately(developed.Plane(c)[i], back.Pixels.Plane(c)[i], 3e-4);
}
developed.Dispose();
}
/// <summary>
/// The colour space the original's values are in is kept: a JPEG or TIFF carries its ICC profile (or the Adobe RGB one
/// its Exif names) with the values unchanged. A linear DNG, whose matrices Chronos develops it with, cannot hold P3 codes
/// as Chronos reads them and say they are P3: it refuses the photo, which converting would show otherwise.
/// </summary>
[Test]
public void TheOriginalsColourSpaceIsKept()
{
var folder = context.CreateTempDirectory("source");
var p3 = IccProfile.DisplayP3;
using var scene = ColorFixtures.Scene();
var jpegPath = Path.Combine(folder, "p3.jpg");
File.WriteAllBytes(jpegPath, ColorFixtures.Jpeg(scene, p3.Data, ExifWriter.BuildTiff(Camera)));
var source = new ConvertSource(jpegPath, Frame("p3.jpg", ImageFileFormat.Jpeg, 48, 32));
var server = DecoderFactory.CreateServer();
using var original = server.Decode(new DecodeRequest(jpegPath, AllowPreview: false))!;
foreach (var target in (ConvertTarget[])[ConvertTarget.Jpeg, ConvertTarget.Tiff16])
{
var options = new ConvertOptions(target);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
Assert.BytesEqual(p3.Data, SourceColor.Read(file.Path, file.Format).Profile!.Data);
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var error = 0.0;
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < scene.PlaneLength; i++) error += Math.Abs(original.Pixels.Plane(c)[i] - converted.Pixels.Plane(c)[i]);
}
Assert.Less(error / (3.0 * scene.PlaneLength), target == ConvertTarget.Jpeg ? 0.02 : 1e-3, $"the values of {target} pass unchanged");
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
// A camera JPEG in Adobe RGB (Exif uncalibrated, Interop R03) carries the Adobe RGB profile; a PNG its iCCP.
var adobePath = Path.Combine(folder, "adobe.jpg");
File.WriteAllBytes(adobePath, ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF, "R03")));
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
var adobe = FrameConverter.Convert(new ConvertSource(adobePath, Frame("adobe.jpg", ImageFileFormat.Jpeg, 48, 32)), jpeg, server, Target(jpeg),
cancellationToken: context.Cancellation);
Assert.Equal("Compatible with Adobe RGB (1998)", SourceColor.Read(adobe.Path, adobe.Format).Profile!.Description);
var pngPath = Path.Combine(folder, "p3.png");
File.WriteAllBytes(pngPath, ColorFixtures.Png(scene, p3.Data));
var tiff = new ConvertOptions(ConvertTarget.Tiff16);
var fromPng = FrameConverter.Convert(new ConvertSource(pngPath, Frame("p3.png", ImageFileFormat.Png, 48, 32)), tiff, server, Target(tiff),
cancellationToken: context.Cancellation);
Assert.BytesEqual(p3.Data, SourceColor.Read(fromPng.Path, fromPng.Format).Profile!.Data);
}
/// <summary>A colour space nothing here can keep (uncalibrated without R03, a CMYK profile) refuses the photo, whatever the target.</summary>
[Test]
public void APhotoWhoseColourSpaceCannotBeKeptIsRefused()
{
var folder = context.CreateTempDirectory("source");
using var scene = ColorFixtures.Scene();
var unknown = Path.Combine(folder, "unknown.jpg");
File.WriteAllBytes(unknown, ColorFixtures.Jpeg(scene, exifTiff: ColorFixtures.Exif(0xFFFF)));
var cmyk = Path.Combine(folder, "cmyk.jpg");
File.WriteAllBytes(cmyk, ColorFixtures.Jpeg(scene, ColorFixtures.Cmyk()));
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
foreach (var path in (string[])[unknown, cmyk])
{
var output = Target(options);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, Frame(Path.GetFileName(path), ImageFileFormat.Jpeg, 48, 32)),
options, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
}
}
}
/// <summary>
/// A CMYK TIFF without a profile is decoded by what reads CMYK (WIC on a PC), never by the managed reader as the gray of
/// its first ink; where nothing reads it (the server), it is not converted, and nothing is written.
/// </summary>
[Test]
public void ACmykTiffIsDecodedByWhatReadsItNeverAsGray()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "cmyk.tif");
File.WriteAllBytes(path, ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
var frame = Frame("cmyk.tif", ImageFileFormat.Tiff, 6, 4);
using var red = new ImageBuffer(6, 4);
red.Plane(0).Fill(0.6f);
red.Plane(1).Fill(0.003f);
red.Plane(2).Fill(0.002f);
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(path, frame), jpeg, new ImageDecoder([new Developed(red, ImageFileFormat.Tiff), new ManagedDecoderProvider()]),
Target(jpeg), cancellationToken: context.Cancellation);
using var converted = DecoderFactory.CreateServer().Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
Assert.Greater(converted.Pixels[0, 3, 2], 0.5);
Assert.Less(converted.Pixels[1, 3, 2], 0.02);
var output = Target(jpeg);
var refused = Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), jpeg, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.NoDecoder, refused.Reason);
Assert.FileMissing(output);
}
/// <summary>
/// What Chronos reads from a photo never changes with its conversion (owner review 2, R7): Chronos's decoders, which
/// never read a profile, read the converted file as they read the original, within what the target holds, whatever the
/// original's colour space (sRGB, Adobe RGB, ProPhoto, a linear-curve profile, gray) and samples (8-bit, 16-bit, float).
/// The file says truthfully what the values are: the original's profile where its codes are the original's, else one
/// made for them (the original's colorants with the sRGB curve, or a gray curve as RGB). A DNG takes only values that are
/// sRGB's as the decoders read them, and reads back the same; it refuses the others, which its matrices would show
/// otherwise.
/// </summary>
[Test]
public void WhatChronosReadsIsWhatTheConvertedFileReadsBack()
{
var folder = context.CreateTempDirectory("source");
using var scene = ColorFixtures.Scene();
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 = ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb);
var g10 = ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1));
var wideG10 = ColorFixtures.Profile("Wide gamma 1.0", IccCurve.Gamma(1), wide: true);
var prophoto = ColorFixtures.ProPhoto();
var gamma22 = ColorFixtures.Gray("Gray Gamma 2.2", IccCurve.Gamma(2.2));
var sGray = ColorFixtures.Gray("sGray", IccCurve.Srgb);
(string Name, byte[] File, IccProfile? Profile, bool DngKeeps)[] sources =
[
("untagged.jpg", ColorFixtures.Jpeg(scene), null, true),
("srgb.jpg", ColorFixtures.Jpeg(scene, srgb.Data), srgb, true),
("adobe.jpg", ColorFixtures.Jpeg(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("g10.jpg", ColorFixtures.Jpeg(scene, g10.Data), g10, false),
("adobe16.tif", ColorFixtures.Tiff16(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("prophoto16.tif", ColorFixtures.Tiff16(scene, prophoto.Data), prophoto, false),
("float.tif", ColorFixtures.FloatTiff(scene), null, true),
("float-srgb.tif", ColorFixtures.FloatTiff(scene, srgb.Data), srgb, true),
("float-adobe.tif", ColorFixtures.FloatTiff(scene, IccProfile.AdobeRgb.Data), IccProfile.AdobeRgb, false),
("float-prophoto.tif", ColorFixtures.FloatTiff(scene, prophoto.Data), prophoto, false),
("float-g10.tif", ColorFixtures.FloatTiff(scene, g10.Data), g10, true),
("float-wide-g10.tif", ColorFixtures.FloatTiff(scene, wideG10.Data), wideG10, false),
("gray22.jpg", ColorFixtures.GrayJpeg(grays, gamma22), IccProfile.Parse(gamma22), false),
("sgray.jpg", ColorFixtures.GrayJpeg(grays, sGray), IccProfile.Parse(sGray), true),
("float-gray22.tif", ColorFixtures.FloatTiff(grays, gamma22), IccProfile.Parse(gamma22), true),
];
var server = DecoderFactory.CreateServer();
foreach (var (name, bytes, profile, 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, 48, 32));
using var original = server.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, server, Target(options), cancellationToken: context.Cancellation);
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var (max, mean) = CodeDifference(original.Pixels, converted.Pixels);
var what = $"{name} → {target}";
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);
AssertTruthful(color, SourceColor.Read(file.Path, file.Format), original.Pixels, converted.Pixels, target == ConvertTarget.Jpeg ? 0.05 : 2e-4, what);
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
Assert.Equal(dngKeeps ? ColorTreatment.None : ColorTreatment.Refuse, FrameConverter.Treatment(color, ConvertTarget.LinearDng), name);
if (!dngKeeps)
{
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason, name);
Assert.FileMissing(output);
continue;
}
FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation);
using var developed = server.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");
}
}
}
/// <summary>The largest and the mean difference between two decodes of the same size, 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, a.Channels), (b.Width, b.Height, b.Channels));
double max = 0, sum = 0;
for (var c = 0; c < a.Channels; 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 / ((double)a.Channels * a.PlaneLength));
}
/// <summary>
/// The converted file's profile says what its values are: read through it, its codes give the light the original's
/// profile gives the original's values (linear Rec.709, to <paramref name="tolerance"/>); no profile where the original
/// had none, or where its values are linear gray.
/// </summary>
private static void AssertTruthful(SourceColor original, SourceColor converted, ImageBuffer originalPixels, ImageBuffer convertedPixels, double tolerance,
string what)
{
Assert.False(converted.IsLinear || converted.IsUnknown, what);
if (original.Profile is not { } source || (source.IsGray && original.IsLinear))
{
Assert.Null(converted.Profile, what);
return;
}
var carried = Assert.NotNull(converted.Profile, what);
if (!original.IsLinear && source.IsRgb)
{
Assert.BytesEqual(source.Data, carried.Data);
return;
}
Assert.True(carried.IsMatrixShaper, what);
var to709 = carried.ToLinearRec709();
for (var i = 0; i < originalPixels.PlaneLength; i += 11)
{
var (x, y) = (i % originalPixels.Width, i / originalPixels.Width);
Vector3D light;
if (source.IsGray)
{
// A gray profile's code: its curve's light, neutral.
var gray = source.Curves![0].Evaluate(ColorSpace.LinearToSrgb(originalPixels[0, x, y]));
light = new Vector3D(gray, gray, gray);
}
else
{
light = source.ToLinearRec709() * new Vector3D(originalPixels[0, x, y], originalPixels[1, x, y], originalPixels[2, x, y]);
}
var read = to709 * new Vector3D(
carried.Curves![0].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[0, x, y])),
carried.Curves[1].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[1, x, y])),
carried.Curves[2].Evaluate(ColorSpace.LinearToSrgb(convertedPixels[2, x, y])));
Assert.Approximately(light.X, read.X, tolerance, what);
Assert.Approximately(light.Y, read.Y, tolerance, what);
Assert.Approximately(light.Z, read.Z, tolerance, what);
}
}
/// <summary>
/// A float TIFF's values are linear light in its profile's primaries (owner review 2, R7): a JPEG or TIFF holds them as
/// the decoders read them back, sRGB codes, and carries the profile's colorants with the sRGB curve, so colour-managed
/// programs read the same light; above white is clipped there, as in any file of code values. A DNG keeps above white,
/// for values in sRGB's primaries only: in other primaries it refuses them.
/// </summary>
[Theory]
[Case(1.0)]
[Case(1.8)]
public void AFloatTiffKeepsItsLinearValuesInItsColourSpace(double gamma)
{
foreach (var wide in (bool[])[true, false])
{
var profile = ColorFixtures.Profile($"Gamma {gamma}", IccCurve.Gamma(gamma), wide);
using var scene = new ImageBuffer(8, 4);
scene.Data.Fill(0.18f);
(scene[0, 1, 1], scene[1, 1, 1], scene[2, 1, 1]) = (0.5f, 0.1f, 0.05f);
(scene[0, 2, 1], scene[1, 2, 1], scene[2, 2, 1]) = (3f, 3f, 3f);
(scene[0, 3, 1], scene[1, 3, 1], scene[2, 3, 1]) = (0.001f, 0.01f, 0.0005f);
var path = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(path, ColorFixtures.FloatTiff(scene, profile.Data));
var source = new ConvertSource(path, Frame("stack.tif", ImageFileFormat.Tiff, 8, 4));
var server = DecoderFactory.CreateServer();
Assert.Equal(ColorTreatment.Describe, FrameConverter.Treatment(SourceColor.Read(path, ImageFileFormat.Tiff), ConvertTarget.Tiff16));
foreach (var target in (ConvertTarget[])[ConvertTarget.Tiff16, ConvertTarget.Jpeg])
{
var options = new ConvertOptions(target, 100);
var file = FrameConverter.Convert(source, options, server, Target(options), cancellationToken: context.Cancellation);
var carried = SourceColor.Read(file.Path, file.Format).Profile!;
Assert.Equal(profile.Colorants, carried.Colorants);
Assert.True(carried.Curves!.All(c => c.IsSrgb), "the codes are sRGB's");
using var converted = server.Decode(new DecodeRequest(file.Path, AllowPreview: false))!;
var tolerance = target == ConvertTarget.Jpeg ? 0.012 : 1e-4;
foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (3, 1)])
{
for (var c = 0; c < 3; c++)
Assert.Approximately(ColorSpace.LinearToSrgb(scene[c, x, y]), ColorSpace.LinearToSrgb(converted.Pixels[c, x, y]), tolerance, $"{target}, gamma {gamma}");
}
Assert.Approximately(1, converted.Pixels[0, 2, 1], target == ConvertTarget.Jpeg ? 0.02 : 1e-3, "above white is clipped");
}
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
if (wide)
{
Assert.Equal(ConversionFailure.ColorProfile,
Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation)).Reason);
continue;
}
FrameConverter.Convert(source, dng, server, output, cancellationToken: context.Cancellation);
using var developed = server.Decode(new DecodeRequest(output, AllowPreview: false))!;
foreach (var (x, y) in ((int, int)[])[(5, 2), (1, 1), (2, 1), (3, 1)])
{
for (var c = 0; c < 3; c++) Assert.Approximately(scene[c, x, y], developed.Pixels[c, x, y], 2e-3 * Math.Max(1, scene[c, x, y]));
}
Assert.Greater(developed.Pixels[1, 2, 1], 2.9);
}
}
/// <summary>
/// A DNG takes values only where they are sRGB's as the decoders read them, whatever the profile's name: codes in one
/// named sRGB with a linear curve are not (the decoders read them through the sRGB curve), the same profile's linear
/// values are (sRGB's primaries), a real sRGB profile's codes are.
/// </summary>
[Test]
public void ADngTakesOnlyValuesThatAreSrgbsAsTheDecodersReadThem()
{
using var scene = ColorFixtures.Scene();
var linearCurve = ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1));
var real = ColorFixtures.Profile("sRGB IEC61966-2.1", IccCurve.Srgb);
ColorTreatment Dng(byte[] file, ImageFileFormat format) => FrameConverter.Treatment(SourceColor.Read(new MemoryStream(file), format), ConvertTarget.LinearDng);
Assert.Equal(ColorTreatment.Refuse, Dng(ColorFixtures.Jpeg(scene, linearCurve.Data), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.FloatTiff(scene, linearCurve.Data), ImageFileFormat.Tiff));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.Jpeg(scene, real.Data), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.None, Dng(ColorFixtures.Jpeg(scene), ImageFileFormat.Jpeg));
Assert.Equal(ColorTreatment.Refuse, Dng(ColorFixtures.Jpeg(scene, IccProfile.AdobeRgb.Data), ImageFileFormat.Jpeg));
// Into a JPEG or TIFF the same codes are carried with their profile.
Assert.Equal(ColorTreatment.Carry, FrameConverter.Treatment(SourceColor.Read(new MemoryStream(ColorFixtures.Jpeg(scene, linearCurve.Data)), ImageFileFormat.Jpeg), ConvertTarget.Tiff16));
}
/// <summary>
/// A profile whose colorants convert nothing (a damaged one) refuses its photo for a DNG, at once and as a colour
/// profile, and is carried unchanged into a JPEG or TIFF with the codes it came with. One named sRGB is no sRGB: a float
/// TIFF's linear values in it, which a JPEG or TIFF would need its colorants to describe, are refused there too.
/// </summary>
[Test]
public void AProfileThatConvertsNothingIsRefusedForADngAndCarriedElsewhere()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "odd.jpg");
using var scene = ColorFixtures.Scene();
var degenerate = ColorFixtures.Degenerate();
File.WriteAllBytes(path, ColorFixtures.Jpeg(scene, degenerate));
var source = new ConvertSource(path, Frame("odd.jpg", ImageFileFormat.Jpeg, 48, 32));
var dng = new ConvertOptions(ConvertTarget.LinearDng);
var output = Target(dng);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(source, dng, DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.ColorProfile, refused.Reason);
Assert.FileMissing(output);
var tiff = new ConvertOptions(ConvertTarget.Tiff16);
var carried = FrameConverter.Convert(source, tiff, DecoderFactory.CreateServer(), Target(tiff), cancellationToken: context.Cancellation);
Assert.BytesEqual(degenerate, SourceColor.Read(carried.Path, carried.Format).Profile!.Data);
var namedSrgb = ColorFixtures.Degenerate(ColorFixtures.Profile("sRGB-elle-V4-g10", IccCurve.Gamma(1)).Data);
var floatPath = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(floatPath, ColorFixtures.FloatTiff(scene, namedSrgb));
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
var floatOutput = Target(options);
Assert.Equal(ConversionFailure.ColorProfile, Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(floatPath, Frame("stack.tif", ImageFileFormat.Tiff, 48, 32)), options, DecoderFactory.CreateServer(), floatOutput,
cancellationToken: context.Cancellation)).Reason, target.ToString());
Assert.FileMissing(floatOutput);
}
}
/// <summary>
/// A float file whose samples are not numbers (NaN, ±infinity: damaged data) fails its conversion as damaged in every
/// target, nothing written, rather than written black (with a DNG's headroom spent on it).
/// </summary>
[Test]
public void SamplesThatAreNotNumbersFailAsDamagedData()
{
foreach (var bad in (float[])[float.NaN, float.PositiveInfinity, float.NegativeInfinity])
{
using var scene = new ImageBuffer(8, 4);
scene.Data.Fill(0.18f);
scene[1, 6, 3] = bad;
var path = Path.Combine(context.CreateTempDirectory("source"), "stack.tif");
File.WriteAllBytes(path, ColorFixtures.FloatTiff(scene, IccProfile.AdobeRgb.Data));
File.WriteAllBytes(Path.ChangeExtension(path, ".untagged.tif"), ColorFixtures.FloatTiff(scene));
foreach (var file in (string[])[path, Path.ChangeExtension(path, ".untagged.tif")])
{
foreach (var target in Enum.GetValues<ConvertTarget>())
{
var options = new ConvertOptions(target);
if (FrameConverter.Treatment(SourceColor.Read(file, ImageFileFormat.Tiff), target) == ColorTreatment.Refuse) continue;
var output = Target(options);
var failed = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(file, Frame("stack.tif", ImageFileFormat.Tiff, 8, 4)), options,
DecoderFactory.CreateServer(), output, cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.Damaged, failed.Reason, $"{bad} → {target}");
Assert.FileMissing(output);
}
}
}
}
/// <summary>Every NaN and infinity is found, wherever it is (in a vector or in the tail), and finite values pass, however large.</summary>
[Test]
public void NumbersAreToldFromWhatIsNot()
{
foreach (var length in (int[])[0, 1, 7, 8, 9, 31, 64, 1001])
{
var values = new float[length];
for (var i = 0; i < length; i++) values[i] = (i % 3 - 1) * (i % 2 == 0 ? float.MaxValue : float.Epsilon);
Assert.True(FrameConverter.AllFinite(values), $"{length} finite values");
for (var at = 0; at < length; at += Math.Max(1, length / 5))
{
foreach (var bad in (float[])[float.NaN, float.PositiveInfinity, float.NegativeInfinity])
{
var copy = (float[])values.Clone();
copy[at] = bad;
Assert.False(FrameConverter.AllFinite(copy), $"{bad} at {at} of {length}");
}
}
}
}
/// <summary>A photo larger than the target or one buffer holds fails as too large, not with the encoder's own exception.</summary>
[Test]
public void APhotoTooLargeForTheTargetFailsAsTooLarge()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "panorama.png");
using (var image = Scene(8, 8)) File.WriteAllBytes(path, PngEncoder.Encode(image));
var frame = Frame("panorama.png", ImageFileFormat.Png, 70_000, 1);
var jpeg = new ConvertOptions(ConvertTarget.Jpeg);
using var wide = new ImageBuffer(65_536, 1);
var refused = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, frame), jpeg,
new ImageDecoder([new Developed(wide, ImageFileFormat.Png)]), Target(jpeg), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.TooLarge, refused.Reason);
var overflow = Assert.Throws<ConversionException>(() => FrameConverter.Convert(new ConvertSource(path, frame), new ConvertOptions(ConvertTarget.Tiff16),
new ImageDecoder([new Overflowing()]), Target(jpeg), cancellationToken: context.Cancellation));
Assert.Equal(ConversionFailure.TooLarge, overflow.Reason);
}
/// <summary>Why a photo failed reads as one sentence by cause in the job log, never the exception's English text.</summary>
[Test]
public void FailuresReadAsOneSentenceByCause()
{
foreach (var reason in Enum.GetValues<ConversionFailure>())
{
var text = FrameConverter.FailureText(new ConversionException(reason, "technical detail"));
Assert.Equal(Loc.L("Archive.Convert.Failed." + reason), text);
Assert.NotNull(Loc.Get("Archive.Convert.Failed." + reason, CultureInfo.GetCultureInfo("it")));
Assert.False(text.Contains("technical", StringComparison.Ordinal), text);
}
Assert.Equal(Loc.L("Archive.Convert.Failed.Full"), FrameConverter.FailureText(new IOException("full", unchecked((int)0x80070070))));
Assert.Equal(Loc.L("Archive.Failed.Access"), FrameConverter.FailureText(new UnauthorizedAccessException("denied")));
Assert.Equal(Loc.L("Archive.Convert.Failed.Unreadable"), FrameConverter.FailureText(new InvalidDataException("JPEG: bad Huffman")));
Assert.Equal(Loc.L("Archive.Failed.Files"), FrameConverter.FailureText(new IOException("network")));
Assert.Equal(Loc.L("Archive.Convert.Failed.Other"), FrameConverter.FailureText(new ArgumentOutOfRangeException("width")));
}
/// <summary>WIC with the Raw Image Extension, as a conversion sees it: every RAW native, developed at 8 bits (counted).</summary>
private sealed class EightBitRaw : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public int Calls { get; private set; }
public string Name => "wic";
public bool DevelopsRawAt8Bits => true;
public DecodeTier? TierFor(ImageFileFormat format) => ImageFormatInfo.Of(format).IsRaw ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
Calls++;
var decoded = _managed.Decode(request, cancellationToken)!;
foreach (ref var v in decoded.Pixels.Data) v = ColorSpace.Linear8(ColorSpace.Encode8(v));
return new DecodedImage(decoded.Pixels, DecodeTier.Native, Name, decoded.SourceWidth, decoded.SourceHeight);
}
}
/// <summary>A RAW decoder that develops every file of <paramref name="format"/> into a copy of <paramref name="image"/>.</summary>
private sealed class Developed(ImageBuffer image, ImageFileFormat format = ImageFileFormat.Cr2) : IImageDecoderProvider
{
public string Name => "developed";
public DecodeTier? TierFor(ImageFileFormat f) => f == format ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) =>
new(image.Clone(), DecodeTier.Native, Name, image.Width, image.Height);
}
/// <summary>A decoder whose image is too large for one buffer.</summary>
private sealed class Overflowing : IImageDecoderProvider
{
public string Name => "overflowing";
public DecodeTier? TierFor(ImageFileFormat format) => DecodeTier.Native;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => throw new OverflowException();
}
/// <summary>A provider that claims DNG natively but only ever gives the embedded-preview tier.</summary>
private sealed class PreviewOnlyProvider : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public string Name => "preview-only";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Dng ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
var decoded = _managed.Decode(request with { AllowPreview = true }, cancellationToken);
if (decoded is null) return null;
var copy = decoded.Pixels.Clone();
decoded.Dispose();
return new DecodedImage(copy, DecodeTier.EmbeddedPreview, Name, copy.Width, copy.Height);
}
}
}
@@ -232,6 +232,44 @@ public sealed class ManagedDecoderProviderTests(TestContext context)
Assert.Equal(DecodeTier.Unavailable, client.Probe(Write("y.webp", Fixtures.WebP()), ImageFileFormat.WebP).Tier);
}
/// <summary>
/// A provider that reads headers (a conversion's) finds a JPEG, PNG or TIFF native, at its oriented size, only when its
/// decoder takes what the headers describe; the others probe as before. Damage past the headers shows only when decoding.
/// </summary>
[Test]
public void HeaderProbesSayWhatTheDecodersTake()
{
var headers = new ManagedDecoderProvider { ReadsHeaders = true };
using var scene = SyntheticRaw.Smooth(W, H);
var jpeg = headers.Probe(Write("portrait.jpg", JpegFile(W, H, 6)), ImageFileFormat.Jpeg);
Assert.Equal((DecodeTier.Native, "managed-jpeg", H, W), (jpeg.Tier, jpeg.Decoder, jpeg.Width, jpeg.Height));
var png = headers.Probe(Write("a.png", PngEncoder.Encode(scene)), ImageFileFormat.Png);
Assert.Equal((DecodeTier.Native, "managed-png", W, H), (png.Tier, png.Decoder, png.Width, png.Height));
var tiff = headers.Probe(Write("a.tif", TiffWriter.Write(scene)), ImageFileFormat.Tiff);
Assert.Equal((DecodeTier.Native, "managed-tiff", W, H), (tiff.Tier, tiff.Decoder, tiff.Width, tiff.Height));
Assert.Equal(DecodeTier.Native, headers.Probe(Write("IMG_5.CR2", Cr2File("Canon EOS 5D Mark III")), ImageFileFormat.Cr2).Tier);
// Headers alone: what follows them is not read.
var header = headers.Probe(Write("header.jpg", Conversion.ColorFixtures.JpegHeader(4_000, 3_000)), ImageFileFormat.Jpeg);
Assert.Equal((DecodeTier.Native, 4_000, 3_000), (header.Tier, header.Width, header.Height));
// A CMYK TIFF, a JPEG cut before its first scan, a file that is not what its format says, an arithmetic-coded JPEG,
// and a JPEG, PNG or TIFF larger than its decoder takes: refused, like a decode.
var cmyk = Write("cmyk.tif", Conversion.ColorFixtures.Tiff(6, 4, 5, [0, 255, 255, 0]));
Assert.Throws<InvalidDataException>(() => headers.Probe(cmyk, ImageFileFormat.Tiff));
Assert.Throws<InvalidDataException>(() => headers.Probe(Write("cut.jpg", JpegFile(W, H, 1)[..200]), ImageFileFormat.Jpeg));
Assert.Throws<InvalidDataException>(() => headers.Probe(Write("short.png", [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), ImageFileFormat.Png));
Assert.Throws<InvalidDataException>(() => headers.Probe(Write("arithmetic.jpg", Conversion.ColorFixtures.JpegHeader(6, 4, sof: 0xC9)), ImageFileFormat.Jpeg));
Assert.Throws<InvalidDataException>(() => headers.Probe(Write("huge.jpg", Conversion.ColorFixtures.JpegHeader(16_384, 16_385)), ImageFileFormat.Jpeg));
Assert.Throws<InvalidDataException>(() => headers.Probe(Write("huge.png", Conversion.ColorFixtures.PngHeader(16_384, 16_385)), ImageFileFormat.Png));
Assert.Throws<InvalidDataException>(() => headers.Probe(Write("huge.tif", Conversion.ColorFixtures.TiffHeader(13_377, 13_378)), ImageFileFormat.Tiff));
// Through a chain: unavailable with the decoder's reason; a provider without header reads still finds it native.
var chain = new ImageDecoder([headers]).Probe(cmyk, ImageFileFormat.Tiff);
Assert.Equal(DecodeTier.Unavailable, chain.Tier);
Assert.Contains("photometric 5", chain.Reason);
Assert.Equal(DecodeTier.Native, new ManagedDecoderProvider().Probe(cmyk, ImageFileFormat.Tiff).Tier);
Assert.Throws<InvalidDataException>(() => new ManagedDecoderProvider().Decode(new DecodeRequest(cmyk)));
}
[Test]
public void GoProRawDecodesThroughTheJpegBesideIt()
{