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

@@ -39,6 +39,76 @@ public sealed class WicDecoderProviderTests(TestContext context)
Assert.SequenceEqual(["managed", "wic"], heif.Providers.Select(p => p.Name));
}
/// <summary>
/// An untagged CMYK TIFF, which the managed reader refuses rather than read as the gray of its first ink, is planned and
/// converted through WIC on the desktop (owner review 2, R7): its red stays red in the converted file.
/// </summary>
[Test]
public void ACmykTiffConvertsThroughWicInItsColours()
{
var path = Path.Combine(context.CreateTempDirectory(), "cmyk.tif");
File.WriteAllBytes(path, CmykTiff(6, 4, [0, 255, 255, 0]));
var desktop = Chronos.Core.Imaging.Decoders.DecoderFactory.CreateClient(Provider);
var jpeg = new Chronos.Core.Conversion.ConvertOptions(Chronos.Core.Conversion.ConvertTarget.Jpeg);
var probe = Chronos.Core.Conversion.FrameConverter.Decoders(desktop, jpeg, ImageFileFormat.Tiff).Probe(path, ImageFileFormat.Tiff);
Assert.Equal((DecodeTier.Native, "wic"), (probe.Tier, probe.Decoder));
var frame = new Chronos.Core.Archiving.SidecarFrame
{
Id = Guid.NewGuid(),
File = "originals/cmyk.tif",
Sha256 = new string('a', 64),
W = 6,
H = 4,
Format = ImageFileFormat.Tiff,
};
var target = Path.Combine(context.CreateTempDirectory(), "converted.jpg");
var file = Chronos.Core.Conversion.FrameConverter.Convert(new Chronos.Core.Conversion.ConvertSource(path, frame), jpeg, desktop, target,
cancellationToken: context.Cancellation);
using var converted = Assert.NotNull(Chronos.Core.Conversion.FrameConverter.Verifier.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
Assert.Greater(converted.Pixels[0, 3, 2], 0.5);
Assert.Less(converted.Pixels[1, 3, 2], 0.1);
Assert.Less(converted.Pixels[2, 3, 2], 0.1);
}
/// <summary>An uncompressed little-endian TIFF of <paramref name="width"/> × <paramref name="height"/> CMYK pixels, each <paramref name="inks"/>, without a profile.</summary>
private static byte[] CmykTiff(int width, int height, byte[] inks)
{
var strip = new byte[width * height * 4];
for (var i = 0; i < width * height; i++) inks.CopyTo(strip, i * 4);
const int ifd = 8;
(ushort Tag, ushort Type, uint Count, uint Value)[] entries =
[
(256, 4, 1, (uint)width),
(257, 4, 1, (uint)height),
(258, 3, 4, 0), // four 8-bit samples, stored after the directory
(259, 3, 1, 1),
(262, 3, 1, 5), // separated: CMYK
(273, 4, 1, 0), // the strip, after the samples' bits
(277, 3, 1, 4),
(278, 4, 1, (uint)height),
(279, 4, 1, (uint)strip.Length),
];
var bits = ifd + 2 + entries.Length * 12 + 4;
var data = bits + 8;
using var stream = new MemoryStream();
using var writer = new BinaryWriter(stream);
writer.Write("II*\0"u8);
writer.Write((uint)ifd);
writer.Write((ushort)entries.Length);
foreach (var (tag, type, count, value) in entries)
{
writer.Write(tag);
writer.Write(type);
writer.Write(count);
writer.Write(tag switch { 258 => (uint)bits, 273 => (uint)data, _ => value });
}
writer.Write(0u);
for (var i = 0; i < 4; i++) writer.Write((ushort)8);
writer.Write(strip);
return stream.ToArray();
}
[Test]
public void PngMidGreyDecodesToLinearLight()
{