Files
Chronos/tests/Titano.Core.Tests/Codecs/TiffTests.cs
T
Claude 78dad093ab fix(codecs): read TIFF strips larger than the 64 MB per-read cap
Single-strip uncompressed files (Photoshop's default) exceed
TiffReader's bounded read; such strips are now read in pieces, the
uncompressed path skips a copy, and hostile strip/tile counts are
rejected up front.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 10:12:49 +00:00

330 lines
14 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 Titano.Core.Codecs.Jpeg;
using Titano.Core.Codecs.Tiff;
using Titano.Core.Imaging;
using Titano.Core.Metadata.Tiff;
using Titano.Testing;
using static Titano.Core.Tests.Codecs.CodecTestData;
namespace Titano.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));
}
[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));
}
}