Files
Chronos/tests/Titano.Core.Tests/Metadata/ImageSignatureTests.cs
T
Claude f68eae03c5 test(metadata): cover signatures, Exif, XMP and TIFF RAWs
Signature table for every format and tie-break; every FrameMetadata
field from JPEG in both byte orders; timestamp fallbacks (digitized,
XMP, GPS, file time), sub-seconds and offsets, orientation, ISO and
APEX rules, GPS validation; XMP attribute/element forms, priority, DTD
refusal, size cap and extended-XMP reassembly; CR2/NEF/DNG/ARW/ORF/RW2
main-image selection, previews with real sizes and MakerNote fallbacks.

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

113 lines
5.7 KiB
C#

using Titano.Core.Imaging;
using Titano.Core.Metadata;
using Titano.Core.Metadata.Tiff;
using Titano.Core.Tests.Metadata.Builders;
using Titano.Testing;
namespace Titano.Core.Tests.Metadata;
public sealed class ImageSignatureTests
{
[Theory]
[Case("jpeg", "a.jpg", ImageFileFormat.Jpeg)]
[Case("jpeg", "a.png", ImageFileFormat.Jpeg, Name = "extension never overrides the signature")]
[Case("png", null, ImageFileFormat.Png)]
[Case("webp", null, ImageFileFormat.WebP)]
[Case("raf", "a.jpg", ImageFileFormat.Raf)]
[Case("cr2", "a.tif", ImageFileFormat.Cr2, Name = "CR marker beats .tif")]
[Case("cr3", null, ImageFileFormat.Cr3)]
[Case("heic", null, ImageFileFormat.Heif)]
[Case("avif", null, ImageFileFormat.Avif)]
[Case("nef", null, ImageFileFormat.Nef)]
[Case("nef", "x.NRW", ImageFileFormat.Nrw)]
[Case("nef", "x.tif", ImageFileFormat.Tiff, Name = "Nikon-made TIFF")]
[Case("arw", "x.arw", ImageFileFormat.Arw)]
[Case("arw", "x.sr2", ImageFileFormat.Sr2)]
[Case("arw", "x.srf", ImageFileFormat.Srf)]
[Case("dng", "x.dng", ImageFileFormat.Dng)]
[Case("dng", "x.gpr", ImageFileFormat.Gpr)]
[Case("dng", "x.tif", ImageFileFormat.Dng, Name = "DNGVersion beats .tif")]
[Case("orf", null, ImageFileFormat.Orf)]
[Case("rw2", null, ImageFileFormat.Rw2)]
[Case("rw2", "x.rwl", ImageFileFormat.Rwl)]
[Case("rw2", "x.raw", ImageFileFormat.Raw)]
public void DetectsFixtures(string fixture, string? fileName, ImageFileFormat expected) =>
Assert.Equal(expected, ImageSignature.Detect(new MemoryStream(Fixtures.ByName(fixture)), fileName));
[Theory]
[Case("PENTAX Corporation", null, ImageFileFormat.Pef)]
[Case("RICOH IMAGING COMPANY, LTD.", null, ImageFileFormat.Pef)]
[Case("SAMSUNG", null, ImageFileFormat.Srw)]
[Case("Hasselblad", null, ImageFileFormat.ThreeFr)]
[Case("Hasselblad", "x.fff", ImageFileFormat.Fff)]
[Case("Phase One", null, ImageFileFormat.Iiq)]
[Case("Leaf", null, ImageFileFormat.Iiq)]
[Case("Canon", "x.cr2", ImageFileFormat.Cr2, Name = "Canon without CR marker, .cr2")]
[Case("Canon", null, ImageFileFormat.Tiff)]
[Case("OLYMPUS", "x.orf", ImageFileFormat.Orf)]
[Case("Scanner Inc", "x.pef", ImageFileFormat.Pef, Name = "unknown make, raw extension breaks the tie")]
[Case("Scanner Inc", null, ImageFileFormat.Tiff)]
[Case("SAMSUNG", "x.tiff", ImageFileFormat.Tiff, Name = ".tiff keeps a camera-made TIFF a TIFF")]
public void RefinesTiffByMake(string make, string? fileName, ImageFileFormat expected)
{
var tiff = TiffFile.Build(new IfdSpec().Ascii(TiffTags.Make, make).Long(TiffTags.ImageWidth, 8), littleEndian: false);
Assert.Equal(expected, ImageSignature.Detect(new MemoryStream(tiff), fileName));
}
[Theory]
[Case("LEICA CAMERA AG", null, ImageFileFormat.Rwl)]
[Case("Panasonic", null, ImageFileFormat.Rw2)]
public void PanasonicHeaderUsesMake(string make, string? fileName, ImageFileFormat expected)
{
var rw = TiffFile.Build(new IfdSpec().Ascii(TiffTags.Make, make), magic: "IIU\0");
Assert.Equal(expected, ImageSignature.Detect(new MemoryStream(rw), fileName));
}
[Theory]
[Case("mif1", "heic", ImageFileFormat.Heif)]
[Case("mif1", "avif", ImageFileFormat.Avif)]
[Case("msf1", "hevc", ImageFileFormat.Heif)]
[Case("heix", "mif1", ImageFileFormat.Heif)]
[Case("avis", "msf1", ImageFileFormat.Avif)]
[Case("qt ", "qt ", ImageFileFormat.Unknown, Name = "QuickTime movie")]
[Case("isom", "mp41", ImageFileFormat.Unknown, Name = "MP4 video")]
public void ClassifiesFtypBrands(string major, string compatible, ImageFileFormat expected)
{
var ftyp = Box.Make("ftyp", Box.Ascii(major), Box.U32(0), Box.Ascii(compatible));
Assert.Equal(expected, ImageSignature.Detect(new MemoryStream([.. ftyp, .. Box.Make("free")]), null));
}
[Theory]
[Case(new byte[] { (byte)'G', (byte)'I', (byte)'F', (byte)'8', (byte)'9', (byte)'a', 1, 0, 1, 0 }, ImageFileFormat.Gif)]
[Case(new byte[] { (byte)'G', (byte)'I', (byte)'F', (byte)'8', (byte)'7', (byte)'a', 1, 0, 1, 0 }, ImageFileFormat.Gif)]
[Case(new byte[] { 0xFF, 0x0A, 0x7F, 0 }, ImageFileFormat.JpegXl)]
[Case(new byte[] { 0, 0, 0, 0x0C, (byte)'J', (byte)'X', (byte)'L', (byte)' ', 0x0D, 0x0A, 0x87, 0x0A }, ImageFileFormat.JpegXl)]
[Case(new byte[] { (byte)'I', (byte)'I', 0xBC, 0x01, 8, 0, 0, 0 }, ImageFileFormat.JpegXr)]
[Case(new byte[] { (byte)'I', (byte)'I', 0x1A, 0, 0, 0, (byte)'H', (byte)'E', (byte)'A', (byte)'P', (byte)'C', (byte)'C', (byte)'D', (byte)'R' }, ImageFileFormat.Crw)]
[Case(new byte[] { (byte)'M', (byte)'M', (byte)'O', (byte)'R', 0, 0, 0, 8 }, ImageFileFormat.Orf)]
[Case(new byte[] { 0xFF, 0xD8, 0xFF, 0xE1 }, ImageFileFormat.Jpeg)]
[Case(new byte[] { 0xFF, 0xD8 }, ImageFileFormat.Unknown, Name = "two bytes of SOI only")]
[Case(new byte[] { (byte)'B', (byte)'M', 0, 0 }, ImageFileFormat.Unknown, Name = "BM without a DIB header")]
[Case(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }, ImageFileFormat.Unknown)]
[Case(new byte[0], ImageFileFormat.Unknown)]
public void DetectsMagicBytes(byte[] head, ImageFileFormat expected) =>
Assert.Equal(expected, ImageSignature.Detect(new MemoryStream(head), "file.jpg"));
[Test]
public void DetectsBmpWithInfoHeader()
{
var bmp = new byte[54];
"BM"u8.CopyTo(bmp);
bmp[14] = 40;
Assert.Equal(ImageFileFormat.Bmp, ImageSignature.Detect(new MemoryStream(bmp), null));
}
[Test]
public void RestoresStreamPosition()
{
var stream = new MemoryStream(Fixtures.Nef()) { Position = 5 };
ImageSignature.Detect(stream, null);
Assert.Equal(5L, stream.Position);
}
}