using System.Buffers.Binary;
using System.Text;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Tests.Metadata.Builders;
/// Synthetic files for every container Titano reads. The canonical camera values below are what the tests
/// expect back; each RAW-like fixture also exercises its vendor MakerNote and preview layout.
internal static class Fixtures
{
public const string Make = "TestCam";
public const string Model = "TC-1";
public const string Xmp = """
Red
""";
public static readonly Guid CanonUuid = new("85c0b687-820f-11e0-8111-f4ce462b6a48");
public static readonly Guid PreviewUuid = new("eaf42b5e-1c98-4b88-b9fb-b7dc406e4d16");
/// 12:34:56.123 +02:00 on 2024-05-01.
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);
/// Every standard Exif field Titano reads; adds body serial and lens model.
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;
}
/// 45.5° N, 9.26° W, 120.5 m, 2024-05-01 10:34:56 UTC.
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");
/// Canonical Exif TIFF block; IFD1 carries a 160×120 JPEG thumbnail.
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);
}
/// 4000×3000 JPEG: Exif (+thumbnail), XMP, MPF with a 1920×1440 large thumbnail and a disparity image
/// (not a preview).
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);
/// 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.
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]);
}
/// 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).
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);
}
/// 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).
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);
}
/// ARW: IFD0 is a 1616×1080 JPEG preview, SubIFD the 12-bit raw; "SONY DSC" MakerNote with LensSpec.
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);
}
/// ORF ("IIRO"): raw in IFD0; "OM SYSTEM" MakerNote with Equipment (serial, lens) and CameraSettings
/// (3200×2400 preview at a MakerNote-relative offset, IS on).
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");
}
/// 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).
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");
}
/// 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); mdat comes last.
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)));
}
}
/// HEIC: primary hvc1 item 4032×3024 (ispe), 10-bit pixi, irot 1 (+ imir axis 1 when ),
/// Exif item (orientation 6, ignored when transforms exist) and a two-extent XMP item.
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 { 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)];
}
/// 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.
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];
}
/// WebP with VP8X canvas 3000×2000, a VP8L bitstream, EXIF (no prefix) and XMP chunks.
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];
}
/// Simple lossy WebP (VP8 key frame) without metadata.
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];
}
/// PNG 2048×1536 16-bit RGB with eXIf (big-endian TIFF), iTXt XMP (zlib-compressed when asked), IDAT, IEND.
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", [])];
}
/// Every fixture by name, for table-driven and fuzz tests.
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;
}
}