fix(convert): plan, decode and colour only what a conversion really does

Round-2 fixes for the codecs part of owner review 2, phase 4b (mass
conversion), so the dry run never promises a conversion that then fails
or writes wrong pixels.

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

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 18cf9133087aada2db06c8310d2c69ff804c0115)
This commit is contained in:
Alby96 committed 2026-11-02 10:33:33 +01:00
1 parent e3cbe30b29
commit 5b5acaf307
15 files changed
+1160 -78

No files matched your search

@@ -1,5 +1,6 @@
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;
@@ -8,7 +9,7 @@ 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
public sealed class IccProfileTests(TestContext context)
{
[Test]
public void TheSynthesizedProfilesAreMatrixShapersOfTheirSpace()
@@ -69,6 +70,142 @@ public sealed class IccProfileTests
}
}
/// <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, and whose conversion of a JPEG's code values or a float TIFF's linear values, wherever the treatment
/// asks for one, runs to finite values; 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, worked = 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>())
{
using var image = pixels.Clone();
var treatment = FrameConverter.Treatment(color, target);
if (treatment == ColorTreatment.Convert) FrameConverter.ToLinearRec709(image, color.Profile!, color.IsLinear, 1, CancellationToken.None);
else if (treatment == ColorTreatment.Encode) FrameConverter.EncodeWithCurves(image, color.Profile!, 1, CancellationToken.None);
else continue;
worked++;
// Code values convert within the matrix's reach; linear ones as large as the file holds may overflow.
if (treatment == ColorTreatment.Encode || !color.IsLinear)
{
foreach (var v in image.Data) Assert.True(float.IsFinite(v), $"{treatment} of {target} gives finite values");
}
}
}
}
}
context.Log($"{parsed} damaged profiles parsed, {worked} conversions run");
Assert.Greater(parsed, 1000);
Assert.Greater(worked, 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 them by its name.</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);
}
/// <summary>Curves invert: exactly for a power law and a table, within a 16-bit step for a parametric function.</summary>
[Test]
public void CurvesInvert()
{
var gamma = IccCurve.Gamma(2.2);
var table = ColorFixtures.Profile("Gamma 1.8", IccCurve.Gamma(1.8)).Curves![0];
var srgb = IccCurve.Srgb;
foreach (var curve in (IccCurve[])[gamma, table, srgb]) Assert.True(curve.IsInvertible);
for (var code = 0.0; code <= 1; code += 1 / 128.0)
{
Assert.Approximately(code, gamma.Invert(gamma.Evaluate(code)), 1e-9);
Assert.Approximately(code, table.Invert(table.Evaluate(code)), 1e-6);
Assert.Approximately(code, srgb.Invert(ColorSpace.SrgbToLinear(code)), 1 / 65536.0);
}
// Linear values beyond the curve's range take its ends.
Assert.Equal((0.0, 1.0), (gamma.Invert(-0.5), gamma.Invert(3)));
Assert.Equal((0.0, 1.0), (table.Invert(-0.5), table.Invert(3)));
// A curve that falls somewhere maps two codes to one value: no encoding undoes it.
Assert.False(IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0xFF, 0xFF, 0x80, 0])!.IsInvertible);
Assert.False(IccCurve.Parse([.. "curv"u8, 0, 0, 0, 0, 0, 0, 0, 2, 0x80, 0, 0x80, 0])!.IsInvertible);
}
/// <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()
{
@@ -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)]