refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
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
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>
This commit was merged in pull request #6.
This commit is contained in:
1 parent
c99842cc86
commit
f2b58522b9
1801 files changed
+11804
-10676
No files matched your search
@@ -0,0 +1,68 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace Chronos.Core.Tests.Metadata.Builders;
|
||||
|
||||
/// <summary>ISO-BMFF / RIFF / PNG byte helpers for synthetic containers.</summary>
|
||||
internal static class Box
|
||||
{
|
||||
public static byte[] Make(string type, params byte[][] content)
|
||||
{
|
||||
var size = 8 + content.Sum(c => c.Length);
|
||||
return [.. U32((uint)size), .. Encoding.Latin1.GetBytes(type), .. content.SelectMany(c => c)];
|
||||
}
|
||||
|
||||
public static byte[] Full(string type, byte version, uint flags, params byte[][] content) =>
|
||||
Make(type, [[version, (byte)(flags >> 16), (byte)(flags >> 8), (byte)flags], .. content]);
|
||||
|
||||
public static byte[] Uuid(Guid userType, params byte[][] content) =>
|
||||
Make("uuid", [userType.ToByteArray(bigEndian: true), .. content]);
|
||||
|
||||
public static byte[] U8(byte v) => [v];
|
||||
|
||||
public static byte[] U16(ushort v)
|
||||
{
|
||||
var b = new byte[2];
|
||||
BinaryPrimitives.WriteUInt16BigEndian(b, v);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static byte[] U32(uint v)
|
||||
{
|
||||
var b = new byte[4];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(b, v);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static byte[] U64(ulong v)
|
||||
{
|
||||
var b = new byte[8];
|
||||
BinaryPrimitives.WriteUInt64BigEndian(b, v);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static byte[] Ascii(string s) => Encoding.Latin1.GetBytes(s);
|
||||
|
||||
public static byte[] Zeros(int n) => new byte[n];
|
||||
|
||||
/// <summary>RIFF chunk: FourCC, little-endian size, data, pad byte to even length.</summary>
|
||||
public static byte[] Riff(string fourCC, byte[] data)
|
||||
{
|
||||
var b = new byte[8 + data.Length + (data.Length & 1)];
|
||||
Encoding.Latin1.GetBytes(fourCC).CopyTo(b, 0);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(b.AsSpan(4), (uint)data.Length);
|
||||
data.CopyTo(b, 8);
|
||||
return b;
|
||||
}
|
||||
|
||||
/// <summary>PNG chunk with a real CRC.</summary>
|
||||
public static byte[] Png(string type, byte[] data)
|
||||
{
|
||||
var b = new byte[12 + data.Length];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(b, (uint)data.Length);
|
||||
Encoding.Latin1.GetBytes(type).CopyTo(b, 4);
|
||||
data.CopyTo(b, 8);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(b.AsSpan(8 + data.Length), Core.Hashing.Crc32.Compute(b.AsSpan(4, 4 + data.Length)));
|
||||
return b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,417 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
using Chronos.Core.Metadata.Tiff;
|
||||
|
||||
namespace Chronos.Core.Tests.Metadata.Builders;
|
||||
|
||||
/// <summary>Synthetic files for every container Chronos reads. The canonical camera values below are what the tests
|
||||
/// expect back; each RAW-like fixture also exercises its vendor MakerNote and preview layout.</summary>
|
||||
internal static class Fixtures
|
||||
{
|
||||
public const string Make = "TestCam";
|
||||
public const string Model = "TC-1";
|
||||
public const string Xmp = """
|
||||
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
|
||||
<x:xmpmeta xmlns:x="adobe:ns:meta/">
|
||||
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
|
||||
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmp:Rating="4">
|
||||
<xmp:Label>Red</xmp:Label>
|
||||
</rdf:Description>
|
||||
</rdf:RDF>
|
||||
</x:xmpmeta>
|
||||
<?xpacket end="w"?>
|
||||
""";
|
||||
|
||||
public static readonly Guid CanonUuid = new("85c0b687-820f-11e0-8111-f4ce462b6a48");
|
||||
public static readonly Guid PreviewUuid = new("eaf42b5e-1c98-4b88-b9fb-b7dc406e4d16");
|
||||
|
||||
/// <summary>12:34:56.123 +02:00 on 2024-05-01.</summary>
|
||||
public static readonly DateTime Captured = new DateTime(2024, 5, 1, 12, 34, 56, DateTimeKind.Unspecified).AddTicks(1_230_000);
|
||||
|
||||
public static IfdSpec Ifd0(string make = Make, string model = Model, ushort orientation = 6) => new IfdSpec()
|
||||
.Ascii(TiffTags.Make, make)
|
||||
.Ascii(TiffTags.Model, model)
|
||||
.Short(TiffTags.Orientation, orientation)
|
||||
.Ascii(TiffTags.Software, "fw 1.0")
|
||||
.Ascii(TiffTags.Artist, "Ada")
|
||||
.Ascii(TiffTags.Copyright, "(c) Ada")
|
||||
.Short(TiffTags.Rating, 3);
|
||||
|
||||
/// <summary>Every standard Exif field Chronos reads; <paramref name="identity"/> adds body serial and lens model.</summary>
|
||||
public static IfdSpec ExifIfd(bool identity = true, ushort iso = 400)
|
||||
{
|
||||
var exif = new IfdSpec()
|
||||
.Rational(TiffTags.ExposureTime, (1, 250))
|
||||
.Rational(TiffTags.FNumber, (56, 10))
|
||||
.Ascii(TiffTags.DateTimeOriginal, "2024:05:01 12:34:56")
|
||||
.Ascii(TiffTags.SubSecTimeOriginal, "123")
|
||||
.Ascii(TiffTags.OffsetTimeOriginal, "+02:00")
|
||||
.Ascii(TiffTags.DateTimeDigitized, "2024:05:01 12:34:57")
|
||||
.Rational(TiffTags.FocalLength, (35, 1))
|
||||
.Short(TiffTags.FocalLengthIn35mmFilm, 52)
|
||||
.SRational(TiffTags.ExposureBiasValue, -2, 3)
|
||||
.Short(TiffTags.ExposureProgram, 3)
|
||||
.Short(TiffTags.MeteringMode, 5)
|
||||
.Short(TiffTags.WhiteBalance, 1)
|
||||
.Short(TiffTags.Flash, 16)
|
||||
.Long(TiffTags.PixelXDimension, 4000)
|
||||
.Long(TiffTags.PixelYDimension, 3000);
|
||||
if (iso > 0) exif.Short(TiffTags.IsoSpeedRatings, iso);
|
||||
if (identity) exif.Ascii(TiffTags.BodySerialNumber, "SN123").Ascii(TiffTags.LensModel, "TestLens 35mm");
|
||||
return exif;
|
||||
}
|
||||
|
||||
/// <summary>45.5° N, 9.26° W, 120.5 m, 2024-05-01 10:34:56 UTC.</summary>
|
||||
public static IfdSpec GpsIfd() => new IfdSpec()
|
||||
.Bytes(TiffTags.GpsVersionId, 2, 3, 0, 0)
|
||||
.Ascii(TiffTags.GpsLatitudeRef, "N")
|
||||
.Rational(TiffTags.GpsLatitude, (45, 1), (30, 1), (0, 1))
|
||||
.Ascii(TiffTags.GpsLongitudeRef, "W")
|
||||
.Rational(TiffTags.GpsLongitude, (9, 1), (15, 1), (3600, 100))
|
||||
.Bytes(TiffTags.GpsAltitudeRef, 0)
|
||||
.Rational(TiffTags.GpsAltitude, (1205, 10))
|
||||
.Rational(TiffTags.GpsTimeStamp, (10, 1), (34, 1), (56, 1))
|
||||
.Ascii(TiffTags.GpsDateStamp, "2024:05:01");
|
||||
|
||||
/// <summary>Canonical Exif TIFF block; IFD1 carries a 160×120 JPEG thumbnail.</summary>
|
||||
public static byte[] ExifTiff(bool littleEndian = true, IfdSpec? exif = null, IfdSpec? ifd0 = null)
|
||||
{
|
||||
var root = (ifd0 ?? Ifd0()).Child(TiffTags.ExifIfd, exif ?? ExifIfd()).Child(TiffTags.GpsIfd, GpsIfd());
|
||||
root.Next = new IfdSpec()
|
||||
.Short(TiffTags.Compression, 6)
|
||||
.Blob(TiffTags.JpegInterchangeFormat, TiffTags.JpegInterchangeFormatLength, JpegBuilder.Minimal(160, 120));
|
||||
return TiffFile.Build(root, littleEndian);
|
||||
}
|
||||
|
||||
/// <summary>4000×3000 JPEG: Exif (+thumbnail), XMP, MPF with a 1920×1440 large thumbnail and a disparity image
|
||||
/// (not a preview).</summary>
|
||||
public static byte[] Jpeg(bool littleEndian = true) => new JpegBuilder()
|
||||
.Segment(0xE0, [.. "JFIF\0"u8, 1, 2, 0, 0, 1, 0, 1, 0, 0])
|
||||
.Exif(ExifTiff(littleEndian))
|
||||
.Xmp(Xmp)
|
||||
.Mpf((JpegBuilder.Minimal(1920, 1440), 0x00010002), (JpegBuilder.Minimal(640, 480), 0x00020002))
|
||||
.Build(4000, 3000);
|
||||
|
||||
/// <summary>CR2: "CR" marker, IFD0 full-size JPEG strip (6720×4480), IFD1 thumbnail, IFD2 RGB, IFD3 lossless
|
||||
/// raw (SOF3, 14-bit); Canon MakerNote supplies serial, lens, IS and colour temperature.</summary>
|
||||
public static byte[] Cr2()
|
||||
{
|
||||
var makerNote = new IfdSpec()
|
||||
.Short(0x0001, [.. Enumerable.Range(0, 40).Select(i => (ushort)(i == 34 ? 1 : 0))])
|
||||
.Long(0x000C, 123456)
|
||||
.Long(0x0010, 0x80000349)
|
||||
.Ascii(0x0095, "EF24-70mm f/2.8L II USM")
|
||||
.Short(0x00AE, 5200);
|
||||
var exif = ExifIfd(identity: false).Embedded(TiffTags.MakerNote, [], makerNote, relativeToTiff: true, littleEndian: true);
|
||||
var ifd0 = Ifd0("Canon", "Canon EOS 5D Mark IV", 1)
|
||||
.Long(TiffTags.ImageWidth, 6720).Long(TiffTags.ImageLength, 4480)
|
||||
.Short(TiffTags.BitsPerSample, 8, 8, 8)
|
||||
.Short(TiffTags.Compression, 6)
|
||||
.Strip(JpegBuilder.Minimal(6720, 4480))
|
||||
.Child(TiffTags.ExifIfd, exif);
|
||||
var ifd1 = new IfdSpec().Short(TiffTags.Compression, 6)
|
||||
.Blob(TiffTags.JpegInterchangeFormat, TiffTags.JpegInterchangeFormatLength, JpegBuilder.Minimal(160, 120));
|
||||
var ifd2 = new IfdSpec().Long(TiffTags.ImageWidth, 592).Long(TiffTags.ImageLength, 395)
|
||||
.Short(TiffTags.BitsPerSample, 16, 16, 16).Short(TiffTags.Compression, 1).Strip(new byte[64]);
|
||||
var ifd3 = new IfdSpec().Short(TiffTags.Compression, 6).Strip(JpegBuilder.Minimal(2784, 3708, 0xC3, 14));
|
||||
ifd0.Next = ifd1;
|
||||
ifd1.Next = ifd2;
|
||||
ifd2.Next = ifd3;
|
||||
return TiffFile.Build(ifd0, true, headerExtra: [(byte)'C', (byte)'R', 2, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
||||
/// <summary>Big-endian NEF: IFD0 RGB thumbnail; SubIFDs JpgFromRaw (8256×5504) and 14-bit CFA raw (8288×5520);
|
||||
/// Nikon type-3 MakerNote (serial, shutter count, lens, VR, ISO used because Exif ISO is absent).</summary>
|
||||
public static byte[] Nef()
|
||||
{
|
||||
var nikon = new IfdSpec()
|
||||
.Short(0x0002, 0, 64)
|
||||
.Ascii(0x001D, "3001234")
|
||||
.Undefined(0x001F, [.. "0100"u8, 1, 0, 0, 0])
|
||||
.Rational(0x0084, (24, 1), (70, 1), (28, 10), (28, 10))
|
||||
.Long(0x00A7, 12345);
|
||||
var exif = ExifIfd(identity: false, iso: 0)
|
||||
.Undefined(TiffTags.MakerNote, [.. "Nikon\0"u8, 2, 0x11, 0, 0, .. TiffFile.Build(nikon, littleEndian: false)]);
|
||||
var jpgFromRaw = new IfdSpec().Long(TiffTags.NewSubfileType, 1).Short(TiffTags.Compression, 6)
|
||||
.Blob(TiffTags.JpegInterchangeFormat, TiffTags.JpegInterchangeFormatLength, JpegBuilder.Minimal(8256, 5504));
|
||||
var raw = new IfdSpec().Long(TiffTags.NewSubfileType, 0)
|
||||
.Long(TiffTags.ImageWidth, 8288).Long(TiffTags.ImageLength, 5520)
|
||||
.Short(TiffTags.BitsPerSample, 14).Short(TiffTags.Compression, 34713)
|
||||
.Short(TiffTags.PhotometricInterpretation, 32803).Strip(new byte[128]);
|
||||
var ifd0 = Ifd0("NIKON CORPORATION", "NIKON D850", 1)
|
||||
.Long(TiffTags.NewSubfileType, 1)
|
||||
.Long(TiffTags.ImageWidth, 160).Long(TiffTags.ImageLength, 120)
|
||||
.Short(TiffTags.BitsPerSample, 8, 8, 8).Short(TiffTags.Compression, 1).Strip(new byte[96])
|
||||
.Child(TiffTags.SubIfds, jpgFromRaw, raw)
|
||||
.Child(TiffTags.ExifIfd, exif);
|
||||
return TiffFile.Build(ifd0, littleEndian: false);
|
||||
}
|
||||
|
||||
/// <summary>DNG: IFD0 thumbnail with CameraSerialNumber; SubIFDs 16-bit CFA raw (4032×3024, lossless JPEG strip,
|
||||
/// excluded as CFA) and a 1024×768 YCbCr JPEG preview (NewSubfileType 1).</summary>
|
||||
public static byte[] Dng()
|
||||
{
|
||||
var raw = new IfdSpec().Long(TiffTags.NewSubfileType, 0)
|
||||
.Long(TiffTags.ImageWidth, 4032).Long(TiffTags.ImageLength, 3024)
|
||||
.Short(TiffTags.BitsPerSample, 16).Short(TiffTags.Compression, 7)
|
||||
.Short(TiffTags.PhotometricInterpretation, 32803).Strip(JpegBuilder.Minimal(2016, 3024, 0xC3, 12));
|
||||
var preview = new IfdSpec().Long(TiffTags.NewSubfileType, 1)
|
||||
.Long(TiffTags.ImageWidth, 1024).Long(TiffTags.ImageLength, 768)
|
||||
.Short(TiffTags.BitsPerSample, 8, 8, 8).Short(TiffTags.Compression, 7)
|
||||
.Short(TiffTags.PhotometricInterpretation, 6).Strip(JpegBuilder.Minimal(1024, 768));
|
||||
var ifd0 = Ifd0("Google", "Pixel 8", 1)
|
||||
.Long(TiffTags.NewSubfileType, 1)
|
||||
.Long(TiffTags.ImageWidth, 256).Long(TiffTags.ImageLength, 192)
|
||||
.Short(TiffTags.BitsPerSample, 8, 8, 8).Short(TiffTags.Compression, 1).Short(TiffTags.PhotometricInterpretation, 2)
|
||||
.Strip(new byte[48])
|
||||
.Bytes(TiffTags.DngVersion, 1, 4, 0, 0)
|
||||
.Ascii(TiffTags.CameraSerialNumber, "DNGSER1")
|
||||
.Child(TiffTags.SubIfds, raw, preview)
|
||||
.Child(TiffTags.ExifIfd, ExifIfd(identity: false));
|
||||
return TiffFile.Build(ifd0);
|
||||
}
|
||||
|
||||
/// <summary>ARW: IFD0 is a 1616×1080 JPEG preview, SubIFD the 12-bit raw; "SONY DSC" MakerNote with LensSpec.</summary>
|
||||
public static byte[] Arw()
|
||||
{
|
||||
var sony = new IfdSpec()
|
||||
.Long(0xB021, 5600)
|
||||
.Long(0xB026, 1)
|
||||
.Undefined(0xB02A, [0x00, 0x00, 0x24, 0x00, 0x70, 0x28, 0x28, 0x00]);
|
||||
var exif = ExifIfd(identity: false).Embedded(TiffTags.MakerNote, [.. "SONY DSC \0\0\0"u8], sony, relativeToTiff: true, littleEndian: true);
|
||||
var raw = new IfdSpec().Long(TiffTags.NewSubfileType, 0)
|
||||
.Long(TiffTags.ImageWidth, 6048).Long(TiffTags.ImageLength, 4024)
|
||||
.Short(TiffTags.BitsPerSample, 12).Short(TiffTags.Compression, 32767)
|
||||
.Short(TiffTags.PhotometricInterpretation, 32803).Strip(new byte[64]);
|
||||
var ifd0 = Ifd0("SONY", "ILCE-7M3", 1)
|
||||
.Long(TiffTags.NewSubfileType, 1).Short(TiffTags.Compression, 6)
|
||||
.Blob(TiffTags.JpegInterchangeFormat, TiffTags.JpegInterchangeFormatLength, JpegBuilder.Minimal(1616, 1080))
|
||||
.Child(TiffTags.SubIfds, raw)
|
||||
.Child(TiffTags.ExifIfd, exif);
|
||||
return TiffFile.Build(ifd0);
|
||||
}
|
||||
|
||||
/// <summary>ORF ("IIRO"): raw in IFD0; "OM SYSTEM" MakerNote with Equipment (serial, lens) and CameraSettings
|
||||
/// (3200×2400 preview at a MakerNote-relative offset, IS on).</summary>
|
||||
public static byte[] Orf()
|
||||
{
|
||||
var equipment = new IfdSpec().Ascii(0x0101, "BHX123").Ascii(0x0203, "M.Zuiko 12-40mm F2.8");
|
||||
var settings = new IfdSpec().Long(0x0100, 1).Blob(0x0101, 0x0102, JpegBuilder.Minimal(3200, 2400)).Long(0x0604, 1);
|
||||
var olympus = new IfdSpec().Child(0x2010, equipment).Child(0x2020, settings);
|
||||
var exif = ExifIfd(identity: false)
|
||||
.Embedded(TiffTags.MakerNote, [.. "OM SYSTEM\0\0\0II"u8, 4, 0], olympus, relativeToTiff: false, littleEndian: true);
|
||||
var ifd0 = Ifd0("OM Digital Solutions", "OM-1", 1)
|
||||
.Long(TiffTags.ImageWidth, 5240).Long(TiffTags.ImageLength, 3912)
|
||||
.Short(TiffTags.BitsPerSample, 12).Short(TiffTags.Compression, 1).Strip(new byte[64])
|
||||
.Child(TiffTags.ExifIfd, exif);
|
||||
return TiffFile.Build(ifd0, magic: "IIRO");
|
||||
}
|
||||
|
||||
/// <summary>RW2 ("IIU\0"): sensor borders give 5776×4344, 12-bit, ISO 800; JpgFromRaw (1920×1440) carries the Exif
|
||||
/// block with a Panasonic MakerNote (internal serial, lens type, IS, colour temperature).</summary>
|
||||
public static byte[] Rw2()
|
||||
{
|
||||
var panasonic = new IfdSpec()
|
||||
.Short(0x001A, 2)
|
||||
.Undefined(0x0025, [.. "F541504040123"u8, 0, 0, 0])
|
||||
.Short(0x0044, 5000)
|
||||
.Ascii(0x0051, "LUMIX G VARIO 12-60mm");
|
||||
var exif = ExifIfd(identity: false, iso: 800)
|
||||
.Embedded(TiffTags.MakerNote, [.. "Panasonic\0\0\0"u8], panasonic, relativeToTiff: true, littleEndian: true);
|
||||
var jpeg = new JpegBuilder().Exif(ExifTiff(true, exif, Ifd0("Panasonic", "DC-GH6", 1))).Build(1920, 1440);
|
||||
var ifd0 = new IfdSpec()
|
||||
.Short(0x0002, 5800).Short(0x0003, 4360)
|
||||
.Short(0x0004, 8).Short(0x0005, 12).Short(0x0006, 4352).Short(0x0007, 5788)
|
||||
.Short(0x000A, 12).Short(0x0017, 800)
|
||||
.Undefined(0x002E, jpeg)
|
||||
.Ascii(TiffTags.Make, "Panasonic").Ascii(TiffTags.Model, "DC-GH6").Short(TiffTags.Orientation, 1);
|
||||
return TiffFile.Build(ifd0, magic: "IIU\0");
|
||||
}
|
||||
|
||||
/// <summary>CR3: CMT1–4 + THMB in the Canon uuid, PRVW in the preview uuid, track 1 full-size JPEG (8192×5464),
|
||||
/// track 2 raw with CMP1 (8352×5586, 14-bit); <c>mdat</c> comes last.</summary>
|
||||
public static byte[] Cr3() => Cr3(out _);
|
||||
|
||||
public static byte[] Cr3(out int mdatStart)
|
||||
{
|
||||
var full = JpegBuilder.Minimal(8192, 5464);
|
||||
var canonMakerNote = TiffFile.Build(new IfdSpec().Long(0x000C, 98765).Ascii(0x0095, "RF24-105mm F4 L IS USM"));
|
||||
var cmt2 = new IfdSpec()
|
||||
.Rational(TiffTags.ExposureTime, (1, 100)).Rational(TiffTags.FNumber, (4, 1)).Short(TiffTags.IsoSpeedRatings, 100)
|
||||
.Ascii(TiffTags.DateTimeOriginal, "2024:06:01 08:00:00").Ascii(TiffTags.SubSecTimeOriginal, "50")
|
||||
.Ascii(TiffTags.OffsetTimeOriginal, "-04:00");
|
||||
var thumb = JpegBuilder.Minimal(160, 120);
|
||||
var canon = Box.Uuid(CanonUuid,
|
||||
Box.Make("CNCV", Box.Ascii("CanonCR3_001/00.09.00/00.00.00")),
|
||||
Box.Make("CMT1", TiffFile.Build(Ifd0("Canon", "Canon EOS R5", 8).Long(TiffTags.ImageWidth, 8192).Long(TiffTags.ImageLength, 5464))),
|
||||
Box.Make("CMT2", TiffFile.Build(cmt2)),
|
||||
Box.Make("CMT3", canonMakerNote),
|
||||
Box.Make("CMT4", TiffFile.Build(GpsIfd())),
|
||||
Box.Make("THMB", [0, 0, 0, 0], Box.U16(160), Box.U16(120), Box.U32((uint)thumb.Length), Box.U16(0xFF), Box.U16(0xFF), thumb));
|
||||
var prvwJpeg = JpegBuilder.Minimal(1620, 1080);
|
||||
var preview = Box.Uuid(PreviewUuid, Box.U64(0),
|
||||
Box.Make("PRVW", Box.U32(0), Box.U16(1), Box.U16(1620), Box.U16(1080), Box.U16(1), Box.U32((uint)prvwJpeg.Length), prvwJpeg));
|
||||
|
||||
byte[] Build(uint jpegOffset)
|
||||
{
|
||||
var moov = Box.Make("moov", canon, Box.Make("mvhd", Box.Zeros(100)),
|
||||
Track(Box.Make("JPEG", Box.Zeros(4)), Box.U32(0), Box.U32(1), Box.U32((uint)full.Length), jpegOffset, 8192, 5464),
|
||||
Track(Box.Make("CMP1", Box.U16(0), Box.U16(0x30), Box.U32(0x100), Box.U32(8352), Box.U32(5586), Box.U32(512), Box.U32(512), [14, 0x11, 0, 0]),
|
||||
Box.U32(0), Box.U32(1), Box.U32(1000), jpegOffset + (uint)full.Length, 8352, 5586));
|
||||
return [.. Box.Make("ftyp", Box.Ascii("crx "), Box.U32(1), Box.Ascii("crx isom")), .. moov, .. preview];
|
||||
}
|
||||
|
||||
var head = Build(0);
|
||||
var offset = (uint)(head.Length + 8);
|
||||
head = Build(offset);
|
||||
mdatStart = head.Length;
|
||||
var mdat = new byte[8 + full.Length + 1000];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(mdat, (uint)mdat.Length);
|
||||
"mdat"u8.CopyTo(mdat.AsSpan(4));
|
||||
full.CopyTo(mdat, 8);
|
||||
return [.. head, .. mdat];
|
||||
|
||||
static byte[] Track(byte[] entryChild, byte[] sampleSize, byte[] sampleCount, byte[] firstSize, uint chunkOffset, ushort width, ushort height)
|
||||
{
|
||||
var craw = Box.Make("CRAW", Box.Zeros(6), Box.U16(1), Box.Zeros(16), Box.U16(width), Box.U16(height),
|
||||
Box.U32(0x00480000), Box.U32(0x00480000), Box.U32(0), Box.U16(1), Box.Zeros(32), Box.U16(24), Box.U16(0xFFFF), entryChild);
|
||||
var stbl = Box.Make("stbl",
|
||||
Box.Full("stsd", 0, 0, Box.U32(1), craw),
|
||||
Box.Full("stsz", 0, 0, sampleSize, sampleCount, firstSize),
|
||||
Box.Full("stco", 0, 0, Box.U32(1), Box.U32(chunkOffset)));
|
||||
return Box.Make("trak", Box.Make("mdia", Box.Make("minf", stbl)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>HEIC: primary hvc1 item 4032×3024 (ispe), 10-bit pixi, irot 1 (+ imir axis 1 when <paramref name="mirror"/>),
|
||||
/// Exif item (orientation 6, ignored when transforms exist) and a two-extent XMP item.</summary>
|
||||
public static byte[] Heic(bool rotate = true, bool mirror = false, string major = "heic")
|
||||
{
|
||||
var exifPayload = (byte[])[.. Box.U32(6), .. "Exif\0\0"u8, .. ExifTiff()];
|
||||
var xmp = Encoding.UTF8.GetBytes(Xmp);
|
||||
var split = xmp.Length / 2;
|
||||
var codec = major == "avif" ? "av01" : "hvc1";
|
||||
|
||||
byte[] Build(uint mdatData)
|
||||
{
|
||||
var exifAt = mdatData + 64;
|
||||
var xmpAt = exifAt + (uint)exifPayload.Length;
|
||||
var properties = new List<byte[]> { Box.Full("ispe", 0, 0, Box.U32(4032), Box.U32(3024)), Box.Full("pixi", 0, 0, [3, 10, 10, 10]) };
|
||||
if (rotate) properties.Add(Box.Make("irot", [1]));
|
||||
if (mirror) properties.Add(Box.Make("imir", [1]));
|
||||
var associations = Enumerable.Range(1, properties.Count).Select(i => (byte)(0x80 | i)).ToArray();
|
||||
var meta = Box.Full("meta", 0, 0,
|
||||
Box.Full("hdlr", 0, 0, Box.U32(0), Box.Ascii("pict"), Box.Zeros(12), [0]),
|
||||
Box.Full("pitm", 0, 0, Box.U16(1)),
|
||||
Box.Full("iinf", 0, 0, Box.U16(3),
|
||||
Box.Full("infe", 2, 0, Box.U16(1), Box.U16(0), Box.Ascii(codec), [0]),
|
||||
Box.Full("infe", 2, 0, Box.U16(2), Box.U16(0), Box.Ascii("Exif"), [0]),
|
||||
Box.Full("infe", 2, 0, Box.U16(3), Box.U16(0), Box.Ascii("mime"), [0], Box.Ascii("application/rdf+xml"), [0])),
|
||||
Box.Full("iloc", 1, 0, [0x44, 0x00], Box.U16(3),
|
||||
Box.U16(1), Box.U16(0), Box.U16(0), Box.U16(1), Box.U32(mdatData), Box.U32(64),
|
||||
Box.U16(2), Box.U16(0), Box.U16(0), Box.U16(1), Box.U32(exifAt), Box.U32((uint)exifPayload.Length),
|
||||
Box.U16(3), Box.U16(0), Box.U16(0), Box.U16(2), Box.U32(xmpAt), Box.U32((uint)split), Box.U32(xmpAt + (uint)split), Box.U32((uint)(xmp.Length - split))),
|
||||
Box.Make("iprp",
|
||||
Box.Make("ipco", [.. properties.SelectMany(p => p)]),
|
||||
Box.Full("ipma", 0, 0, Box.U32(1), Box.U16(1), [(byte)associations.Length], associations)));
|
||||
return [.. Box.Make("ftyp", Box.Ascii(major), Box.U32(0), Box.Ascii("mif1"), Box.Ascii(major)), .. meta];
|
||||
}
|
||||
|
||||
var head = Build(0);
|
||||
var data = (uint)(head.Length + 8);
|
||||
head = Build(data);
|
||||
return [.. head, .. Box.Make("mdat", Box.Zeros(64), exifPayload, xmp)];
|
||||
}
|
||||
|
||||
/// <summary>RAF: big-endian directory; embedded 1920×1280 JPEG with Exif and a Fujifilm MakerNote; RAF records give
|
||||
/// 7752×5178 (cropped) and the FujiIFD 14-bit.</summary>
|
||||
public static byte[] Raf()
|
||||
{
|
||||
var fuji = new IfdSpec().Ascii(0x0010, "FF02B1234567").Short(0x1005, 6100).Short(0x1422, 2, 1, 0);
|
||||
var exif = ExifIfd(identity: false)
|
||||
.Embedded(TiffTags.MakerNote, [.. "FUJIFILM"u8, 12, 0, 0, 0], fuji, relativeToTiff: false, littleEndian: true);
|
||||
var jpeg = new JpegBuilder().Exif(ExifTiff(true, exif, Ifd0("FUJIFILM", "X-T5", 1))).Build(1920, 1280);
|
||||
var records = (byte[])[.. Box.U32(2), .. Box.U16(0x0100), .. Box.U16(4), .. Box.U16(5280), .. Box.U16(7920),
|
||||
.. Box.U16(0x0111), .. Box.U16(4), .. Box.U16(5178), .. Box.U16(7752)];
|
||||
var fujiIfd = TiffFile.Build(new IfdSpec().Child(0xF000, new IfdSpec().Long(0xF001, 7920).Long(0xF002, 5280).Long(0xF003, 14)), littleEndian: false);
|
||||
var jpegOffset = 0x6C;
|
||||
var recordsOffset = jpegOffset + jpeg.Length;
|
||||
var cfaOffset = recordsOffset + records.Length;
|
||||
var header = new byte[0x6C];
|
||||
"FUJIFILMCCD-RAW 0201FF129502"u8.CopyTo(header);
|
||||
"X-T5"u8.CopyTo(header.AsSpan(0x1C));
|
||||
"0100"u8.CopyTo(header.AsSpan(0x3C));
|
||||
uint[] directory = [(uint)jpegOffset, (uint)jpeg.Length, (uint)recordsOffset, (uint)records.Length, (uint)cfaOffset, (uint)fujiIfd.Length];
|
||||
for (var i = 0; i < directory.Length; i++) BinaryPrimitives.WriteUInt32BigEndian(header.AsSpan(0x54 + 4 * i), directory[i]);
|
||||
return [.. header, .. jpeg, .. records, .. fujiIfd];
|
||||
}
|
||||
|
||||
/// <summary>WebP with VP8X canvas 3000×2000, a VP8L bitstream, EXIF (no prefix) and XMP chunks.</summary>
|
||||
public static byte[] WebP()
|
||||
{
|
||||
byte[] vp8x = [0x0C, 0, 0, 0, .. UInt24(2999), .. UInt24(1999)];
|
||||
var body = (byte[])[.. "WEBP"u8, .. Box.Riff("VP8X", vp8x), .. Box.Riff("VP8L", Vp8L(3000, 2000)),
|
||||
.. Box.Riff("EXIF", ExifTiff(false)), .. Box.Riff("XMP ", Encoding.UTF8.GetBytes(Xmp))];
|
||||
return [.. "RIFF"u8, .. LittleEndian32(body.Length), .. body];
|
||||
}
|
||||
|
||||
/// <summary>Simple lossy WebP (VP8 key frame) without metadata.</summary>
|
||||
public static byte[] WebPLossy(int width, int height)
|
||||
{
|
||||
byte[] frame = [0x10, 0x02, 0x00, 0x9D, 0x01, 0x2A, (byte)width, (byte)(width >> 8), (byte)height, (byte)(height >> 8), 0, 0];
|
||||
var body = (byte[])[.. "WEBP"u8, .. Box.Riff("VP8 ", frame)];
|
||||
return [.. "RIFF"u8, .. LittleEndian32(body.Length), .. body];
|
||||
}
|
||||
|
||||
public static byte[] Vp8L(int width, int height)
|
||||
{
|
||||
var bits = (uint)(width - 1) | (uint)(height - 1) << 14;
|
||||
return [0x2F, .. LittleEndian32((int)bits), 0];
|
||||
}
|
||||
|
||||
/// <summary>PNG 2048×1536 16-bit RGB with eXIf (big-endian TIFF), iTXt XMP (zlib-compressed when asked), IDAT, IEND.</summary>
|
||||
public static byte[] Png(bool compressedXmp = false)
|
||||
{
|
||||
var ihdr = (byte[])[.. Box.U32(2048), .. Box.U32(1536), 16, 2, 0, 0, 0];
|
||||
var xmp = Encoding.UTF8.GetBytes(Xmp);
|
||||
if (compressedXmp)
|
||||
{
|
||||
using var output = new MemoryStream();
|
||||
using (var z = new System.IO.Compression.ZLibStream(output, System.IO.Compression.CompressionLevel.Optimal, leaveOpen: true)) z.Write(xmp);
|
||||
xmp = output.ToArray();
|
||||
}
|
||||
var itxt = (byte[])[.. "XML:com.adobe.xmp\0"u8, compressedXmp ? (byte)1 : (byte)0, 0, 0, 0, .. xmp];
|
||||
return [0x89, .. "PNG\r\n\u001a\n"u8, .. Box.Png("IHDR", ihdr), .. Box.Png("eXIf", ExifTiff(false)),
|
||||
.. Box.Png("iTXt", itxt), .. Box.Png("IDAT", new byte[32]), .. Box.Png("IEND", [])];
|
||||
}
|
||||
|
||||
/// <summary>Every fixture by name, for table-driven and fuzz tests.</summary>
|
||||
public static byte[] ByName(string name) => name switch
|
||||
{
|
||||
"jpeg" => Jpeg(),
|
||||
"jpeg-be" => Jpeg(false),
|
||||
"cr2" => Cr2(),
|
||||
"nef" => Nef(),
|
||||
"dng" => Dng(),
|
||||
"arw" => Arw(),
|
||||
"orf" => Orf(),
|
||||
"rw2" => Rw2(),
|
||||
"cr3" => Cr3(),
|
||||
"heic" => Heic(),
|
||||
"avif" => Heic(major: "avif"),
|
||||
"raf" => Raf(),
|
||||
"webp" => WebP(),
|
||||
"png" => Png(),
|
||||
"png-z" => Png(compressedXmp: true),
|
||||
_ => throw new ArgumentException(name),
|
||||
};
|
||||
|
||||
private static byte[] UInt24(int v) => [(byte)v, (byte)(v >> 8), (byte)(v >> 16)];
|
||||
|
||||
private static byte[] LittleEndian32(int v)
|
||||
{
|
||||
var b = new byte[4];
|
||||
BinaryPrimitives.WriteInt32LittleEndian(b, v);
|
||||
return b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
using Chronos.Core.Metadata.Tiff;
|
||||
|
||||
namespace Chronos.Core.Tests.Metadata.Builders;
|
||||
|
||||
/// <summary>Declarative IFD for synthetic test files: typed values, child-IFD pointers, data blobs and embedded
|
||||
/// (MakerNote-style) IFDs. Serialised by <see cref="TiffFile"/> in either byte order.</summary>
|
||||
internal sealed class IfdSpec
|
||||
{
|
||||
internal abstract record Entry(ushort Tag);
|
||||
|
||||
internal sealed record ValueEntry(ushort Tag, TiffType Type, object Data) : Entry(Tag);
|
||||
|
||||
internal sealed record ChildrenEntry(ushort Tag, IfdSpec[] Children) : Entry(Tag);
|
||||
|
||||
internal sealed record BlobEntry(ushort Tag, byte[] Data) : Entry(Tag);
|
||||
|
||||
internal sealed record EmbeddedEntry(ushort Tag, byte[] Prefix, IfdSpec Ifd, bool RelativeToTiff, bool LittleEndian) : Entry(Tag);
|
||||
|
||||
public List<Entry> Entries { get; } = [];
|
||||
|
||||
/// <summary>Next IFD in the chain (IFD1 after IFD0…).</summary>
|
||||
public IfdSpec? Next { get; set; }
|
||||
|
||||
public IfdSpec Ascii(ushort tag, string value) => Add(new ValueEntry(tag, TiffType.Ascii, value));
|
||||
|
||||
public IfdSpec Short(ushort tag, params ushort[] values) => Add(new ValueEntry(tag, TiffType.Short, values));
|
||||
|
||||
public IfdSpec Long(ushort tag, params uint[] values) => Add(new ValueEntry(tag, TiffType.Long, values));
|
||||
|
||||
public IfdSpec Rational(ushort tag, params (uint, uint)[] values) => Add(new ValueEntry(tag, TiffType.Rational, values));
|
||||
|
||||
public IfdSpec SRational(ushort tag, int numerator, int denominator) => Add(new ValueEntry(tag, TiffType.SRational, new[] { (numerator, denominator) }));
|
||||
|
||||
public IfdSpec Undefined(ushort tag, byte[] data) => Add(new ValueEntry(tag, TiffType.Undefined, data));
|
||||
|
||||
public IfdSpec Bytes(ushort tag, params byte[] data) => Add(new ValueEntry(tag, TiffType.Byte, data));
|
||||
|
||||
/// <summary>LONG pointer(s) to child IFDs (Exif, GPS, SubIFDs, MakerNote sub-directories).</summary>
|
||||
public IfdSpec Child(ushort tag, params IfdSpec[] children) => Add(new ChildrenEntry(tag, children));
|
||||
|
||||
/// <summary>LONG offset to <paramref name="data"/> (placed after all IFDs) plus an optional LONG length tag.</summary>
|
||||
public IfdSpec Blob(ushort offsetTag, ushort? lengthTag, byte[] data)
|
||||
{
|
||||
Add(new BlobEntry(offsetTag, data));
|
||||
return lengthTag is { } t ? Long(t, (uint)data.Length) : this;
|
||||
}
|
||||
|
||||
public IfdSpec Strip(byte[] data) => Blob(TiffTags.StripOffsets, TiffTags.StripByteCounts, data);
|
||||
|
||||
/// <summary>UNDEFINED value made of <paramref name="prefix"/> and an IFD whose offsets are relative to the TIFF base
|
||||
/// (<paramref name="relativeToTiff"/>) or to the start of the value itself.</summary>
|
||||
public IfdSpec Embedded(ushort tag, byte[] prefix, IfdSpec ifd, bool relativeToTiff, bool littleEndian) =>
|
||||
Add(new EmbeddedEntry(tag, prefix, ifd, relativeToTiff, littleEndian));
|
||||
|
||||
public IfdSpec Without(params ushort[] tags)
|
||||
{
|
||||
Entries.RemoveAll(e => tags.Contains(e.Tag));
|
||||
return this;
|
||||
}
|
||||
|
||||
private IfdSpec Add(Entry entry)
|
||||
{
|
||||
Entries.RemoveAll(e => e.Tag == entry.Tag);
|
||||
Entries.Add(entry);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Serialises <see cref="IfdSpec"/> trees: IFDs in depth-first order, each followed by its values, blobs last.</summary>
|
||||
internal static class TiffFile
|
||||
{
|
||||
/// <summary>A complete TIFF file/block with header (<paramref name="magic"/> overrides "II*\0"/"MM\0*", e.g. "IIRO");
|
||||
/// <paramref name="headerExtra"/> follows the 8-byte header (the CR2 "CR" marker block).</summary>
|
||||
public static byte[] Build(IfdSpec ifd0, bool littleEndian = true, string? magic = null, byte[]? headerExtra = null)
|
||||
{
|
||||
var w = new IfdWriter(littleEndian, 0);
|
||||
w.Raw(Encoding.Latin1.GetBytes(magic ?? (littleEndian ? "II*\0" : "MM\0*")));
|
||||
w.Raw(new byte[4]);
|
||||
if (headerExtra is not null) w.Raw(headerExtra);
|
||||
w.PatchU32(4, (uint)w.WriteChain(ifd0));
|
||||
w.FlushBlobs();
|
||||
return w.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>An IFD and its values laid out as if placed at <paramref name="origin"/> relative to the offset base.</summary>
|
||||
public static byte[] Block(IfdSpec ifd, bool littleEndian, int origin)
|
||||
{
|
||||
var w = new IfdWriter(littleEndian, origin);
|
||||
w.WriteChain(ifd);
|
||||
w.FlushBlobs();
|
||||
return w.ToArray();
|
||||
}
|
||||
|
||||
private sealed class IfdWriter(bool le, int origin)
|
||||
{
|
||||
private readonly List<byte> _b = [];
|
||||
private readonly List<(int Patch, byte[] Data)> _blobs = [];
|
||||
|
||||
public byte[] ToArray() => _b.ToArray();
|
||||
|
||||
public void Raw(byte[] bytes) => _b.AddRange(bytes);
|
||||
|
||||
public int WriteChain(IfdSpec spec)
|
||||
{
|
||||
var position = WriteIfd(spec, out var nextPatch);
|
||||
if (spec.Next is { } next) PatchU32(nextPatch, (uint)WriteChain(next));
|
||||
return position;
|
||||
}
|
||||
|
||||
public void FlushBlobs()
|
||||
{
|
||||
foreach (var (patch, data) in _blobs)
|
||||
{
|
||||
Align();
|
||||
PatchU32(patch, (uint)(origin + _b.Count));
|
||||
_b.AddRange(data);
|
||||
}
|
||||
_blobs.Clear();
|
||||
}
|
||||
|
||||
public void PatchU32(int at, uint value)
|
||||
{
|
||||
Span<byte> s = stackalloc byte[4];
|
||||
if (le) BinaryPrimitives.WriteUInt32LittleEndian(s, value);
|
||||
else BinaryPrimitives.WriteUInt32BigEndian(s, value);
|
||||
for (var i = 0; i < 4; i++) _b[at + i] = s[i];
|
||||
}
|
||||
|
||||
private void PatchU16(int at, ushort value)
|
||||
{
|
||||
Span<byte> s = stackalloc byte[2];
|
||||
if (le) BinaryPrimitives.WriteUInt16LittleEndian(s, value);
|
||||
else BinaryPrimitives.WriteUInt16BigEndian(s, value);
|
||||
_b[at] = s[0];
|
||||
_b[at + 1] = s[1];
|
||||
}
|
||||
|
||||
private void Align()
|
||||
{
|
||||
if ((origin + _b.Count) % 2 != 0) _b.Add(0);
|
||||
}
|
||||
|
||||
private int WriteIfd(IfdSpec spec, out int nextPatch)
|
||||
{
|
||||
Align();
|
||||
var start = _b.Count;
|
||||
var entries = spec.Entries.OrderBy(e => e.Tag).ToList();
|
||||
_b.AddRange(new byte[2 + 12 * entries.Count + 4]);
|
||||
PatchU16(start, (ushort)entries.Count);
|
||||
nextPatch = start + 2 + 12 * entries.Count;
|
||||
var children = new List<(int Patch, IfdSpec Child)>();
|
||||
for (var i = 0; i < entries.Count; i++)
|
||||
{
|
||||
var at = start + 2 + 12 * i;
|
||||
var e = entries[i];
|
||||
PatchU16(at, e.Tag);
|
||||
switch (e)
|
||||
{
|
||||
case IfdSpec.ValueEntry v:
|
||||
var (bytes, count) = Encode(v);
|
||||
PutValue(at, v.Type, count, bytes);
|
||||
break;
|
||||
case IfdSpec.ChildrenEntry c:
|
||||
PatchU16(at + 2, (ushort)TiffType.Long);
|
||||
PatchU32(at + 4, (uint)c.Children.Length);
|
||||
if (c.Children.Length == 1)
|
||||
{
|
||||
children.Add((at + 8, c.Children[0]));
|
||||
}
|
||||
else
|
||||
{
|
||||
Align();
|
||||
PatchU32(at + 8, (uint)(origin + _b.Count));
|
||||
for (var k = 0; k < c.Children.Length; k++)
|
||||
{
|
||||
children.Add((_b.Count, c.Children[k]));
|
||||
_b.AddRange(new byte[4]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case IfdSpec.BlobEntry blob:
|
||||
PatchU16(at + 2, (ushort)TiffType.Long);
|
||||
PatchU32(at + 4, 1);
|
||||
_blobs.Add((at + 8, blob.Data));
|
||||
break;
|
||||
case IfdSpec.EmbeddedEntry m:
|
||||
Align();
|
||||
var position = origin + _b.Count;
|
||||
var block = Block(m.Ifd, m.LittleEndian, m.RelativeToTiff ? position + m.Prefix.Length : m.Prefix.Length);
|
||||
PatchU16(at + 2, (ushort)TiffType.Undefined);
|
||||
PatchU32(at + 4, (uint)(m.Prefix.Length + block.Length));
|
||||
PatchU32(at + 8, (uint)position);
|
||||
_b.AddRange(m.Prefix);
|
||||
_b.AddRange(block);
|
||||
break;
|
||||
}
|
||||
}
|
||||
foreach (var (patch, child) in children) PatchU32(patch, (uint)WriteChain(child));
|
||||
return origin + start;
|
||||
}
|
||||
|
||||
private void PutValue(int at, TiffType type, uint count, byte[] bytes)
|
||||
{
|
||||
PatchU16(at + 2, (ushort)type);
|
||||
PatchU32(at + 4, count);
|
||||
if (bytes.Length <= 4)
|
||||
{
|
||||
for (var i = 0; i < bytes.Length; i++) _b[at + 8 + i] = bytes[i];
|
||||
return;
|
||||
}
|
||||
Align();
|
||||
PatchU32(at + 8, (uint)(origin + _b.Count));
|
||||
_b.AddRange(bytes);
|
||||
}
|
||||
|
||||
private (byte[] Bytes, uint Count) Encode(IfdSpec.ValueEntry v)
|
||||
{
|
||||
switch (v.Data)
|
||||
{
|
||||
case string s:
|
||||
var ascii = Encoding.UTF8.GetBytes(s + "\0");
|
||||
return (ascii, (uint)ascii.Length);
|
||||
case byte[] raw:
|
||||
return (raw, (uint)raw.Length);
|
||||
case ushort[] shorts:
|
||||
{
|
||||
var b = new byte[2 * shorts.Length];
|
||||
for (var i = 0; i < shorts.Length; i++) Write16(b.AsSpan(2 * i), shorts[i]);
|
||||
return (b, (uint)shorts.Length);
|
||||
}
|
||||
case uint[] longs:
|
||||
{
|
||||
var b = new byte[4 * longs.Length];
|
||||
for (var i = 0; i < longs.Length; i++) Write32(b.AsSpan(4 * i), longs[i]);
|
||||
return (b, (uint)longs.Length);
|
||||
}
|
||||
case (uint, uint)[] rationals:
|
||||
{
|
||||
var b = new byte[8 * rationals.Length];
|
||||
for (var i = 0; i < rationals.Length; i++)
|
||||
{
|
||||
Write32(b.AsSpan(8 * i), rationals[i].Item1);
|
||||
Write32(b.AsSpan(8 * i + 4), rationals[i].Item2);
|
||||
}
|
||||
return (b, (uint)rationals.Length);
|
||||
}
|
||||
case (int, int)[] signed:
|
||||
{
|
||||
var b = new byte[8 * signed.Length];
|
||||
for (var i = 0; i < signed.Length; i++)
|
||||
{
|
||||
Write32(b.AsSpan(8 * i), (uint)signed[i].Item1);
|
||||
Write32(b.AsSpan(8 * i + 4), (uint)signed[i].Item2);
|
||||
}
|
||||
return (b, (uint)signed.Length);
|
||||
}
|
||||
default:
|
||||
throw new InvalidOperationException("Unsupported test value " + v.Data.GetType());
|
||||
}
|
||||
}
|
||||
|
||||
private void Write16(Span<byte> s, ushort v)
|
||||
{
|
||||
if (le) BinaryPrimitives.WriteUInt16LittleEndian(s, v);
|
||||
else BinaryPrimitives.WriteUInt16BigEndian(s, v);
|
||||
}
|
||||
|
||||
private void Write32(Span<byte> s, uint v)
|
||||
{
|
||||
if (le) BinaryPrimitives.WriteUInt32LittleEndian(s, v);
|
||||
else BinaryPrimitives.WriteUInt32BigEndian(s, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
|
||||
namespace Chronos.Core.Tests.Metadata.Builders;
|
||||
|
||||
/// <summary>Synthetic JPEG: SOI, arbitrary segments/raw bytes, an optional APP2 MPF index of appended secondary
|
||||
/// images, SOFn, SOS with a fake scan, EOI. No real entropy data: parsers stop at SOS.</summary>
|
||||
internal sealed class JpegBuilder
|
||||
{
|
||||
public const string XmpSignature = "http://ns.adobe.com/xap/1.0/\0";
|
||||
public const string ExtendedXmpSignature = "http://ns.adobe.com/xmp/extension/\0";
|
||||
|
||||
private readonly List<byte[]> _parts = [];
|
||||
private readonly List<(byte[] Jpeg, uint Type)> _secondary = [];
|
||||
private int _mpfPart = -1;
|
||||
|
||||
/// <summary>A minimal JPEG with just a frame header (used as embedded previews).</summary>
|
||||
public static byte[] Minimal(int width, int height, byte sofMarker = 0xC0, int precision = 8, int scanBytes = 32) =>
|
||||
new JpegBuilder().Build(width, height, sofMarker, precision, scanBytes);
|
||||
|
||||
public JpegBuilder Segment(byte marker, byte[] payload)
|
||||
{
|
||||
var s = new byte[4 + payload.Length];
|
||||
s[0] = 0xFF;
|
||||
s[1] = marker;
|
||||
BinaryPrimitives.WriteUInt16BigEndian(s.AsSpan(2), (ushort)(payload.Length + 2));
|
||||
payload.CopyTo(s, 4);
|
||||
_parts.Add(s);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Bytes inserted verbatim (fill bytes, garbage).</summary>
|
||||
public JpegBuilder Raw(params byte[] bytes)
|
||||
{
|
||||
_parts.Add(bytes);
|
||||
return this;
|
||||
}
|
||||
|
||||
public JpegBuilder Exif(byte[] tiff) => Segment(0xE1, [.. "Exif\0\0"u8, .. tiff]);
|
||||
|
||||
public JpegBuilder Xmp(string packet) => Segment(0xE1, [.. Encoding.ASCII.GetBytes(XmpSignature), .. Encoding.UTF8.GetBytes(packet)]);
|
||||
|
||||
/// <summary>Extended XMP split into <paramref name="chunk"/>-byte APP1 segments (written in reverse order to prove
|
||||
/// reassembly uses offsets).</summary>
|
||||
public JpegBuilder ExtendedXmp(string guid, string packet, int chunk)
|
||||
{
|
||||
var data = Encoding.UTF8.GetBytes(packet);
|
||||
var segments = new List<byte[]>();
|
||||
for (var offset = 0; offset < data.Length; offset += chunk)
|
||||
{
|
||||
var piece = data.AsSpan(offset, Math.Min(chunk, data.Length - offset));
|
||||
var payload = new byte[ExtendedXmpSignature.Length + 32 + 8 + piece.Length];
|
||||
Encoding.ASCII.GetBytes(ExtendedXmpSignature).CopyTo(payload, 0);
|
||||
Encoding.ASCII.GetBytes(guid).CopyTo(payload, ExtendedXmpSignature.Length);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(payload.AsSpan(ExtendedXmpSignature.Length + 32), (uint)data.Length);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(payload.AsSpan(ExtendedXmpSignature.Length + 36), (uint)offset);
|
||||
piece.CopyTo(payload.AsSpan(ExtendedXmpSignature.Length + 40));
|
||||
segments.Add(payload);
|
||||
}
|
||||
segments.Reverse();
|
||||
foreach (var s in segments) Segment(0xE1, s);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>APP2 MPF index; each secondary JPEG is appended after the primary's EOI with the given MP type code.</summary>
|
||||
public JpegBuilder Mpf(params (byte[] Jpeg, uint Type)[] secondary)
|
||||
{
|
||||
_secondary.AddRange(secondary);
|
||||
_mpfPart = _parts.Count;
|
||||
_parts.Add([]);
|
||||
return this;
|
||||
}
|
||||
|
||||
public byte[] Build(int width, int height, byte sofMarker = 0xC0, int precision = 8, int scanBytes = 32)
|
||||
{
|
||||
var tail = new List<byte>();
|
||||
tail.AddRange(SegmentBytes(sofMarker, [(byte)precision, (byte)(height >> 8), (byte)height, (byte)(width >> 8), (byte)width, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1]));
|
||||
tail.AddRange(SegmentBytes(0xDA, [3, 1, 0, 2, 0x11, 3, 0x11, 0, 0x3F, 0]));
|
||||
for (var i = 0; i < scanBytes; i++) tail.Add((byte)(i * 37 % 255));
|
||||
tail.AddRange([0xFF, 0xD9]);
|
||||
|
||||
if (_mpfPart >= 0) _parts[_mpfPart] = SegmentBytes(0xE2, MpfPayload(new uint[_secondary.Count], 0));
|
||||
var headLength = 2 + _parts.Sum(p => p.Length);
|
||||
var primaryLength = headLength + tail.Count;
|
||||
if (_mpfPart >= 0)
|
||||
{
|
||||
var mpHeader = 2 + _parts.Take(_mpfPart).Sum(p => p.Length) + 4 + 4;
|
||||
var offsets = new uint[_secondary.Count];
|
||||
var at = primaryLength;
|
||||
for (var i = 0; i < offsets.Length; i++)
|
||||
{
|
||||
offsets[i] = (uint)(at - mpHeader);
|
||||
at += _secondary[i].Jpeg.Length;
|
||||
}
|
||||
_parts[_mpfPart] = SegmentBytes(0xE2, MpfPayload(offsets, primaryLength));
|
||||
}
|
||||
|
||||
var file = new List<byte>(primaryLength);
|
||||
file.AddRange([0xFF, 0xD8]);
|
||||
foreach (var p in _parts) file.AddRange(p);
|
||||
file.AddRange(tail);
|
||||
foreach (var (jpeg, _) in _secondary) file.AddRange(jpeg);
|
||||
return file.ToArray();
|
||||
|
||||
byte[] MpfPayload(uint[] offsets, int primarySize)
|
||||
{
|
||||
// "MPF\0", big-endian MP header, index IFD (version, count, entries), then 16-byte MP entries.
|
||||
var n = offsets.Length + 1;
|
||||
var b = new byte[4 + 8 + 2 + 3 * 12 + 4 + 16 * n];
|
||||
var s = b.AsSpan();
|
||||
"MPF\0MM\0*"u8.CopyTo(s);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(s[8..], 8);
|
||||
var ifd = s[12..];
|
||||
BinaryPrimitives.WriteUInt16BigEndian(ifd, 3);
|
||||
WriteEntry(ifd[2..], 0xB000, 7, 4, BinaryPrimitives.ReadUInt32BigEndian("0100"u8));
|
||||
WriteEntry(ifd[14..], 0xB001, 4, 1, (uint)n);
|
||||
WriteEntry(ifd[26..], 0xB002, 7, (uint)(16 * n), 8 + 2 + 36 + 4);
|
||||
var entries = s[(4 + 8 + 2 + 36 + 4)..];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(entries, 0x20030000);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(entries[4..], (uint)primarySize);
|
||||
for (var i = 0; i < offsets.Length; i++)
|
||||
{
|
||||
var e = entries[(16 * (i + 1))..];
|
||||
BinaryPrimitives.WriteUInt32BigEndian(e, _secondary[i].Type);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(e[4..], (uint)_secondary[i].Jpeg.Length);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(e[8..], offsets[i]);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteEntry(Span<byte> s, ushort tag, ushort type, uint count, uint value)
|
||||
{
|
||||
BinaryPrimitives.WriteUInt16BigEndian(s, tag);
|
||||
BinaryPrimitives.WriteUInt16BigEndian(s[2..], type);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(s[4..], count);
|
||||
BinaryPrimitives.WriteUInt32BigEndian(s[8..], value);
|
||||
}
|
||||
|
||||
private static byte[] SegmentBytes(byte marker, byte[] payload)
|
||||
{
|
||||
var s = new byte[4 + payload.Length];
|
||||
s[0] = 0xFF;
|
||||
s[1] = marker;
|
||||
BinaryPrimitives.WriteUInt16BigEndian(s.AsSpan(2), (ushort)(payload.Length + 2));
|
||||
payload.CopyTo(s, 4);
|
||||
return s;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
namespace Chronos.Core.Tests.Metadata.Builders;
|
||||
|
||||
/// <summary>Read-only seekable stream: <c>prefix</c> bytes followed by zeros up to <c>length</c>. Simulates a large
|
||||
/// RAW (image data never materialised) for I/O-bound tests.</summary>
|
||||
internal sealed class SparseStream(byte[] prefix, long length) : Stream
|
||||
{
|
||||
private long _position;
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => true;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => _position;
|
||||
set => _position = value;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));
|
||||
|
||||
public override int Read(Span<byte> buffer)
|
||||
{
|
||||
var n = (int)Math.Max(0, Math.Min(buffer.Length, length - _position));
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
var p = _position + i;
|
||||
buffer[i] = p < prefix.Length ? prefix[p] : (byte)0;
|
||||
}
|
||||
_position += n;
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) => _position = origin switch
|
||||
{
|
||||
SeekOrigin.Begin => offset,
|
||||
SeekOrigin.Current => _position + offset,
|
||||
_ => length + offset,
|
||||
};
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
||||
}
|
||||
|
||||
/// <summary>Counts the bytes returned by <see cref="Read(Span{byte})"/> of the wrapped stream.</summary>
|
||||
internal sealed class CountingStream(Stream inner) : Stream
|
||||
{
|
||||
public long BytesRead { get; private set; }
|
||||
public int ReadCalls { get; private set; }
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => true;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => inner.Length;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => inner.Position;
|
||||
set => inner.Position = value;
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count) => Read(buffer.AsSpan(offset, count));
|
||||
|
||||
public override int Read(Span<byte> buffer)
|
||||
{
|
||||
var n = inner.Read(buffer);
|
||||
BytesRead += n;
|
||||
ReadCalls++;
|
||||
return n;
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin) => inner.Seek(offset, origin);
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
}
|
||||
|
||||
public override void SetLength(long value) => throw new NotSupportedException();
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
||||
}
|
||||
Reference in new issue
Block a user