Files
Chronos/tests/Chronos.Core.Tests/Codecs/TiffTests.cs
T
Alby96 5b5acaf307 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)
2026-11-02 10:33:33 +01:00

402 lines
18 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Buffers.Binary;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Imaging;
using Chronos.Core.Metadata.Tiff;
using Chronos.Testing;
using static Chronos.Core.Tests.Codecs.CodecTestData;
namespace Chronos.Core.Tests.Codecs;
public sealed class TiffTests(TestContext context)
{
private static TiffRaster Raster(byte[] tiff, TiffDecodeOptions? options = null)
{
var reader = TiffReader.Open(new MemoryStream(tiff)) ?? throw new InvalidDataException("not a TIFF");
return TiffImageReader.ReadRaster(reader, reader.ReadChain()[0], options);
}
[Theory]
[Case("tiff/lzw-pred.tif")]
[Case("tiff/packbits.tif")]
[Case("tiff/none-strips.tif")]
[Case("tiff/lzw-tiled.tif")]
[Case("tiff/planar-zip.tif")]
public void EightBitLayoutsMatchSource(string name)
{
var raster = Raster(Fixture(name));
Assert.Empty(raster.Warnings);
Assert.Equal(3, raster.SamplesPerPixel);
Assert.SequenceEqual(ReadPnm(Fixture("src.ppm")).Words, raster.Samples!);
}
[Test]
public void SixteenBitDeflatePredictorMatchesSource()
{
var raster = Raster(Fixture("tiff/deflate16-pred.tif"));
Assert.Equal(16, raster.BitsPerSample);
Assert.SequenceEqual(ReadPnm(Fixture("src16.ppm")).Words, raster.Samples!);
}
[Test]
public void FloatingPointPredictorMatchesSource()
{
var raster = Raster(Fixture("tiff/float32-pred3.tif"));
Assert.Equal(TiffSampleFormat.Float, raster.SampleFormat);
var source = ReadPnm(Fixture("src16.ppm")).Samples;
for (var i = 0; i < source.Length; i++) Assert.Approximately(source[i] / 65535.0, raster.FloatSamples![i], 1e-6);
using var image = TiffImageReader.Decode(Fixture("tiff/float32-pred3.tif"));
Assert.Approximately(source[3 * 100 + 1] / 65535.0, image[1, 100 % 61, 100 / 61], 1e-6);
}
[Theory]
[Case("tiff/jpeg-ycbcr")]
[Case("tiff/jpeg-rgb")]
public void JpegCompressedStripsMatchLibtiff(string name)
{
var raster = Raster(Fixture(name + ".tif"));
Assert.Equal(2, raster.Photometric);
Assert.Empty(raster.Warnings);
var diff = MaxAbsDiff(raster.Samples!.Select(s => (byte)s).ToArray(), ReadPnm(Fixture(name + ".ppm")).Samples);
context.Log($"{name}: max |diff| vs libtiff {diff}");
Assert.LessOrEqual(diff, 2);
}
[Test]
public void MinIsWhiteIsInverted()
{
// The raster keeps stored values; the image inverts them (0 = white), as libtiff readers do.
var file = Fixture("tiff/gray-miniswhite.tif");
var raster = Raster(file);
Assert.Equal(0, raster.Photometric);
using var image = TiffImageReader.Decode(file);
Assert.Equal(1, image.Channels);
for (var i = 0; i < image.PlaneLength; i++) Assert.Equal(ColorSpace.Linear8((byte)(255 - raster.Samples![i])), image.Plane(0)[i]);
}
[Theory]
[Case(false)]
[Case(true)]
public void LzwBothBitOrdersAndTableResets(bool oldStyle)
{
const int w = 70, h = 64;
var pixels = new byte[w * h * 3];
new Random(3).NextBytes(pixels.AsSpan(0, pixels.Length / 2));
for (var i = pixels.Length / 2; i < pixels.Length; i++) pixels[i] = (byte)(i / 50);
var strips = new List<byte[]>();
for (var y = 0; y < h; y += 32) strips.Add(TiffTestEncoders.Lzw(pixels.AsSpan(y * w * 3, Math.Min(32, h - y) * w * 3), oldStyle));
var tiff = TiffTestEncoders.Build(d => Layout(d, w, h, 3, 8, compression: 5, rowsPerStrip: 32), strips, tiled: false);
var raster = Raster(tiff);
Assert.Empty(raster.Warnings);
Assert.SequenceEqual(pixels.Select(b => (ushort)b), raster.Samples!);
}
[Test]
public void PackBitsSixteenBitPlanarTiles()
{
const int w = 40, h = 24, tile = 16;
var samples = new ushort[w * h * 3];
for (var i = 0; i < samples.Length; i++) samples[i] = (ushort)(i % 7 == 0 ? 60000 : i * 13);
var chunks = new List<byte[]>();
for (var plane = 0; plane < 3; plane++)
{
for (var ty = 0; ty < h; ty += tile)
{
for (var tx = 0; tx < w; tx += tile)
{
var raw = new byte[tile * tile * 2];
for (var y = 0; y < tile; y++)
{
for (var x = 0; x < tile; x++)
{
var v = ty + y < h && tx + x < w ? samples[((ty + y) * w + tx + x) * 3 + plane] : (ushort)0;
BinaryPrimitives.WriteUInt16LittleEndian(raw.AsSpan((y * tile + x) * 2), v);
}
}
chunks.Add(TiffTestEncoders.PackBits(raw));
}
}
}
var tiff = TiffTestEncoders.Build(d =>
{
Layout(d, w, h, 3, 16, compression: 32773);
d.Short(TiffTags.PlanarConfiguration, 2);
d.Long(TiffTags.TileWidth, tile);
d.Long(TiffTags.TileLength, tile);
}, chunks, tiled: true);
Assert.SequenceEqual(samples, Raster(tiff).Samples!);
}
[Test]
public void SingleStripLargerThanTheReaderCapDecodes()
{
// Photoshop-style single uncompressed strip of 70 MB (TiffReader reads at most 64 MB per call).
const int w = 5000, h = 7000;
var strip = new byte[w * h * 2];
for (var i = 0; i < strip.Length; i += 2) BinaryPrimitives.WriteUInt16LittleEndian(strip.AsSpan(i), (ushort)(i * 7));
var tiff = TiffTestEncoders.Build(d => Layout(d, w, h, 1, 16, compression: 1), [strip], tiled: false);
var raster = Raster(tiff);
Assert.Empty(raster.Warnings);
Assert.Equal(unchecked((ushort)((w * h - 1) * 2 * 7)), raster.Samples![^1]);
Assert.Equal(unchecked((ushort)(12345 * 2 * 7)), raster.Samples[12345]);
}
[Test]
public void PackedTwelveBitSamplesUnpack()
{
const int w = 5, h = 3;
var values = Enumerable.Range(0, w * h).Select(i => (ushort)(i * 273 % 4096)).ToArray();
var stride = (w * 12 + 7) / 8;
var raw = new byte[stride * h];
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
for (var b = 0; b < 12; b++)
{
if ((values[y * w + x] >> (11 - b) & 1) != 0) raw[y * stride + (x * 12 + b) / 8] |= (byte)(0x80 >> ((x * 12 + b) % 8));
}
}
}
var tiff = TiffTestEncoders.Build(d => Layout(d, w, h, 1, 12, compression: 1), [raw], tiled: false);
var raster = Raster(tiff);
Assert.Equal(12, raster.BitsPerSample);
Assert.SequenceEqual(values, raster.Samples!);
}
[Test]
public void DngStyleLosslessTilesMapBackFromReshapedFrames()
{
// A CFA-like 1-sample tile of 32×8 stored as a 2-component, 16-wide lossless JPEG frame (as DNG writers do).
const int w = 48, h = 8, tile = 32;
var samples = Enumerable.Range(0, w * h).Select(i => (ushort)(i * 977 % 16384)).ToArray();
var chunks = new List<byte[]>();
for (var tx = 0; tx < w; tx += tile)
{
var t = new ushort[tile * h];
for (var y = 0; y < h; y++)
{
for (var x = 0; x < tile; x++) t[y * tile + x] = tx + x < w ? samples[y * w + tx + x] : (ushort)0;
}
chunks.Add(JpegLosslessEncoder.Encode(t, tile / 2, h, 2, 14, predictor: 1));
}
var tiff = TiffTestEncoders.Build(d =>
{
Layout(d, w, h, 1, 16, compression: 7);
d.Short(TiffTags.PhotometricInterpretation, 32803);
d.Long(TiffTags.TileWidth, tile);
d.Long(TiffTags.TileLength, h);
}, chunks, tiled: true);
var raster = Raster(tiff);
Assert.Empty(raster.Warnings);
Assert.Equal(14, raster.BitsPerSample);
Assert.SequenceEqual(samples, raster.Samples!);
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)]
[Case(16, 3, TiffCompression.Deflate, true)]
[Case(16, 3, TiffCompression.Deflate, false)]
[Case(16, 4, TiffCompression.None, false)]
[Case(16, 1, TiffCompression.Deflate, true)]
public void WriterRoundTrips(int bits, int channels, TiffCompression compression, bool predictor)
{
const int w = 123, h = 77;
var samples = new ushort[w * h * channels];
var rng = new Random(bits + channels);
for (var i = 0; i < samples.Length; i++) samples[i] = (ushort)(i % 3 == 0 ? rng.Next(1 << bits) : (i / channels * 31) & ((1 << bits) - 1));
var options = new TiffWriteOptions { BitsPerSample = bits, Compression = compression, Predictor = predictor };
using var ms = new MemoryStream();
TiffWriter.Write(ms, samples, w, h, channels, options);
var raster = Raster(ms.ToArray());
Assert.Equal(channels, raster.SamplesPerPixel);
Assert.Equal(bits, raster.BitsPerSample);
Assert.SequenceEqual(samples, raster.Samples!);
}
[Test]
public void ImageBufferRoundTripAndExifEmbedding()
{
using var image = new ImageBuffer(300, 1100);
for (var c = 0; c < 3; c++)
{
var plane = image.Plane(c);
for (var i = 0; i < plane.Length; i++) plane[i] = (i * (c + 2) % 1000) / 999f;
}
var tiff = TiffWriter.Write(image, new TiffWriteOptions { Exif = ExifBlock() });
using var back = TiffImageReader.Decode(tiff);
for (var i = 0; i < image.Data.Length; i += 101) Assert.Approximately(image.Data[i], back.Data[i], 2e-4);
AssertExif(tiff);
Assert.True(Raster(tiff).Warnings.Count == 0);
}
[Theory]
[Case("tiff/lzw-pred.tif")]
[Case("tiff/lzw-tiled.tif")]
[Case("tiff/planar-zip.tif")]
[Case("tiff/jpeg-ycbcr.tif")]
[Case("tiff/float32-pred3.tif")]
public void FuzzedInputOnlyThrowsInvalidData(string name)
{
int ok = 0, rejected = 0;
var options = new TiffDecodeOptions { MaxSamples = 1 << 22 };
foreach (var input in Corruptions(Fixture(name), 120, seed: name.Length * 31))
{
try
{
using var image = TiffImageReader.Decode(input, options);
ok++;
}
catch (InvalidDataException)
{
rejected++;
}
}
context.Log($"{name}: {ok} decoded, {rejected} rejected");
}
internal static void Layout(TiffDirectory d, int w, int h, int spp, int bits, int compression, int rowsPerStrip = 0)
{
d.Long(TiffTags.ImageWidth, (uint)w);
d.Long(TiffTags.ImageLength, (uint)h);
d.Short(TiffTags.BitsPerSample, Enumerable.Repeat((ushort)bits, spp).ToArray());
d.Short(TiffTags.Compression, (ushort)compression);
d.Short(TiffTags.PhotometricInterpretation, spp >= 3 ? (ushort)2 : (ushort)1);
d.Short(TiffTags.SamplesPerPixel, (ushort)spp);
if (rowsPerStrip > 0) d.Long(TiffTags.RowsPerStrip, (uint)rowsPerStrip);
}
/// <summary>A big-endian Exif APP1 payload: IFD0 Make/Model/Orientation, Exif IFD (exposure, ISO, Interop), GPS IFD.</summary>
internal static byte[] ExifBlock()
{
var ms = new MemoryStream();
var w = new BinaryWriter(ms);
void U16(int v) => w.Write(BinaryPrimitives.ReverseEndianness((ushort)v));
void U32(uint v) => w.Write(BinaryPrimitives.ReverseEndianness(v));
void Entry(int tag, int type, uint count, uint value)
{
U16(tag);
U16(type);
U32(count);
U32(value);
}
w.Write("Exif\0\0MM\0*"u8);
U32(8);
// IFD0 at 8: 5 entries → 2 + 60 + 4 = 66 bytes → data area at 74.
U16(5);
Entry(0x010F, 2, 6, 74); // Make "Canon\0" at 74
Entry(0x0110, 2, 4, 0x45_4F_53_00); // Model "EOS\0" inline
Entry(0x0112, 3, 1, 6u << 16); // Orientation (not copied)
Entry(0x8769, 4, 1, 80); // Exif IFD at 80
Entry(0x8825, 4, 1, 148); // GPS IFD at 148
U32(0);
w.Write("Canon\0"u8);
// Exif IFD at 80: 3 entries → 2 + 36 + 4 = 42 → rational at 122.
U16(3);
Entry(0x829A, 5, 1, 122); // ExposureTime 1/250
Entry(0x8827, 3, 1, 400u << 16); // ISO 400
Entry(0xA005, 4, 1, 130); // Interop IFD at 130
U32(0);
U32(1);
U32(250);
// Interop IFD at 130: 2 + 12 + 4 = 18 bytes → GPS IFD at 148.
U16(1);
Entry(0x0001, 2, 4, 0x52_39_38_00); // InteroperabilityIndex "R98"
U32(0);
U16(1);
Entry(0x0001, 2, 2, 0x4E_00_00_00); // GPSLatitudeRef "N"
U32(0);
return ms.ToArray();
}
internal static void AssertExif(byte[] file)
{
var reader = TiffReader.Open(new MemoryStream(file))!;
var ifd0 = reader.ReadChain()[0];
Assert.Equal("Canon", reader.GetString(ifd0, TiffTags.Make));
Assert.Equal("EOS", reader.GetString(ifd0, TiffTags.Model));
Assert.False(ifd0.Has(TiffTags.Orientation) && reader.GetUInt(ifd0, TiffTags.Orientation) == 6, "orientation must not be copied");
var exif = reader.ReadIfd(reader.GetUInt(ifd0, TiffTags.ExifIfd)!.Value)!;
Assert.Approximately(1 / 250.0, reader.GetDouble(exif, TiffTags.ExposureTime)!.Value, 1e-9);
Assert.Equal(400u, reader.GetUInt(exif, TiffTags.IsoSpeedRatings));
var interop = reader.ReadIfd(reader.GetUInt(exif, TiffTags.InteropIfd)!.Value)!;
Assert.Equal("R98", reader.GetString(interop, 0x0001));
var gps = reader.ReadIfd(reader.GetUInt(ifd0, TiffTags.GpsIfd)!.Value)!;
Assert.Equal("N", reader.GetString(gps, TiffTags.GpsLatitudeRef));
}
}