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)]