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

@@ -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()
{