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:
1 parent
e3cbe30b29
commit
5b5acaf307
15 files changed
+1160
-78
No files matched your search
@@ -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)]
|
||||
|
||||
Reference in new issue
Block a user