CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
The owner renamed the product and moved the repository to the self-hosted Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control, and asked for Actions and a first automatic release with the server package and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md). - Chronos everywhere: a byte-exact rename of contents and paths, then a per-language review of the forms the old name's ending had dictated. - Gitea instead of GitHub: the update check, licence, issue and support links read ChronosReleasesUrl; the update check takes the highest stable version, offers a pre-release build its final release and reports a repository it cannot see as a failed check. EULA, security policy and Gitea issue forms live at the repository root. - Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release, nightly image and performance gates in the .NET SDK container with git checkout and the system SQLite. The release cross-compiles the Windows programs, checks their icon, version and DPI manifest, packs installers and zips, publishes the Gitea release and pushes the server image (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows runner (WINDOWS_RUNNER). - New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the docker-exec script, which Gitea's service containers do not allow; the throw-away server runs without durability) and pe-check. - Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR, 3 on the shared runner; the weekly performance gates keep theirs). BREAKING CHANGE: programs, settings, data folders, file types, environment variables and the Docker image use the new name; nothing is migrated from Titano, which never had a release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
330 lines
14 KiB
C#
330 lines
14 KiB
C#
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));
|
||
}
|
||
|
||
[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));
|
||
}
|
||
}
|