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>
418 lines
23 KiB
C#
418 lines
23 KiB
C#
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;
|
||
}
|
||
}
|