feat(metadata): parse RAF, PNG, WebP and minimal formats
RAF: embedded JPEG Exif and preview, size from the RAF directory (0x0111 cropped, else 0x0100), bit depth from the FujiIFD. PNG: IHDR, eXIf and iTXt/tEXt XMP (zlib supported), seeking over image data. WebP: VP8X canvas or VP8/VP8L headers, EXIF and XMP chunks. GIF and BMP sizes, the JPEG XR IFD and the JPEG XL size header plus container Exif/XML boxes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+374
No files matched your search
@@ -0,0 +1,119 @@
|
||||
using System.Buffers.Binary;
|
||||
using Titano.Core.Metadata.Bmff;
|
||||
using Titano.Core.Metadata.Tiff;
|
||||
using Titano.Core.Metadata.Xmp;
|
||||
|
||||
namespace Titano.Core.Metadata;
|
||||
|
||||
/// <summary>Formats with "minimal" metadata (BRIEF §8.1): GIF and BMP sizes; JPEG XR IFD (size, Exif, GPS, XMP);
|
||||
/// JPEG XL size header plus the container's Exif and XML boxes.</summary>
|
||||
internal static class MinimalFormatParser
|
||||
{
|
||||
private const ushort JxrImageWidth = 0xBC80;
|
||||
private const ushort JxrImageHeight = 0xBC81;
|
||||
|
||||
public static void ParseGif(MetadataBuilder m)
|
||||
{
|
||||
Span<byte> h = stackalloc byte[10];
|
||||
if (m.Stream.TryReadAt(0, h)) m.SetContainerSize(BinaryPrimitives.ReadUInt16LittleEndian(h[6..]), BinaryPrimitives.ReadUInt16LittleEndian(h[8..]));
|
||||
else m.Warn("GIF header truncated");
|
||||
}
|
||||
|
||||
/// <summary>BITMAPCOREHEADER (16-bit sizes) or BITMAPINFOHEADER and later (signed 32-bit; negative height = top-down).</summary>
|
||||
public static void ParseBmp(MetadataBuilder m)
|
||||
{
|
||||
Span<byte> h = stackalloc byte[26];
|
||||
if (!m.Stream.TryReadAt(0, h))
|
||||
{
|
||||
m.Warn("BMP header truncated");
|
||||
return;
|
||||
}
|
||||
if (BinaryPrimitives.ReadUInt32LittleEndian(h[14..]) == 12)
|
||||
{
|
||||
m.SetContainerSize(BinaryPrimitives.ReadUInt16LittleEndian(h[18..]), BinaryPrimitives.ReadUInt16LittleEndian(h[20..]));
|
||||
return;
|
||||
}
|
||||
var width = BinaryPrimitives.ReadInt32LittleEndian(h[18..]);
|
||||
var height = BinaryPrimitives.ReadInt32LittleEndian(h[22..]);
|
||||
if (height != int.MinValue) m.SetContainerSize(width, Math.Abs(height));
|
||||
}
|
||||
|
||||
/// <summary>JPEG XR: a little-endian TIFF-like IFD (magic 0x01BC) with standard Make/Exif/GPS/XMP tags.</summary>
|
||||
public static void ParseJpegXr(MetadataBuilder m)
|
||||
{
|
||||
Span<byte> h = stackalloc byte[8];
|
||||
if (!m.Stream.TryReadAt(0, h))
|
||||
{
|
||||
m.Warn("JPEG XR header truncated");
|
||||
return;
|
||||
}
|
||||
var r = TiffReader.ForEmbedded(m.Stream, 0, littleEndian: true);
|
||||
if (r.ReadIfd(BinaryPrimitives.ReadUInt32LittleEndian(h[4..])) is { } ifd)
|
||||
{
|
||||
ExifParser.ReadTree(r, ifd, m, [ifd.Position]);
|
||||
m.SetContainerSize(IfdValue.Int(r, ifd, JxrImageWidth) ?? 0, IfdValue.Int(r, ifd, JxrImageHeight) ?? 0);
|
||||
}
|
||||
m.AddWarnings(r.Warnings);
|
||||
}
|
||||
|
||||
/// <summary>JPEG XL: bare codestream (FF 0A) or ISO-BMFF container with jxlc/jxlp, Exif and "xml " boxes.</summary>
|
||||
public static void ParseJpegXl(MetadataBuilder m)
|
||||
{
|
||||
Span<byte> h = stackalloc byte[2];
|
||||
if (m.Stream.TryReadAt(0, h) && h[0] == 0xFF && h[1] == 0x0A)
|
||||
{
|
||||
ReadSizeHeader(m, 0);
|
||||
return;
|
||||
}
|
||||
var codestream = false;
|
||||
foreach (var box in BmffReader.ReadChildren(m.Stream, 0, m.Length))
|
||||
{
|
||||
m.ThrowIfCancelled();
|
||||
if (box.Is("Exif")) ExifParser.ParseOffsetPrefixed(m, box.PayloadStart, box.PayloadLength);
|
||||
else if (box.Is("xml ")) m.AddXmp(m.Stream.ReadAt(box.PayloadStart, box.PayloadLength, XmpParser.MaxPacketBytes));
|
||||
else if (!codestream && box.Is("jxlc")) codestream = ReadSizeHeader(m, box.PayloadStart);
|
||||
else if (!codestream && box.Is("jxlp")) codestream = ReadSizeHeader(m, box.PayloadStart + 4);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Codestream SizeHeader (ISO/IEC 18181-1 §D.2): small sizes in multiples of 8, else U32 distributions;
|
||||
/// width either explicit or derived from a fixed aspect ratio.</summary>
|
||||
private static bool ReadSizeHeader(MetadataBuilder m, long position)
|
||||
{
|
||||
Span<byte> b = stackalloc byte[18];
|
||||
var n = m.Stream.ReadUpTo(position, b);
|
||||
if (n < 3 || b[0] != 0xFF || b[1] != 0x0A)
|
||||
{
|
||||
m.Warn("JPEG XL codestream signature missing");
|
||||
return false;
|
||||
}
|
||||
b[n..].Clear();
|
||||
var bits = new BitReader(BinaryPrimitives.ReadUInt128LittleEndian(b[2..]));
|
||||
var small = bits.Read(1) == 1;
|
||||
var height = small ? (bits.Read(5) + 1) * 8 : Distribution(ref bits) + 1;
|
||||
var ratio = (int)bits.Read(3);
|
||||
var width = ratio != 0
|
||||
? height * (ulong)RatioNumerator[ratio] / (ulong)RatioDenominator[ratio]
|
||||
: small ? (bits.Read(5) + 1) * 8 : Distribution(ref bits) + 1;
|
||||
if (width <= int.MaxValue && height <= int.MaxValue) m.SetContainerSize((int)width, (int)height);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static ReadOnlySpan<int> RatioNumerator => [0, 1, 12, 4, 3, 16, 5, 2];
|
||||
private static ReadOnlySpan<int> RatioDenominator => [1, 1, 10, 3, 2, 9, 4, 1];
|
||||
|
||||
private static ulong Distribution(ref BitReader bits) => bits.Read(bits.Read(2) switch { 0 => 9, 1 => 13, 2 => 18, _ => 30 });
|
||||
|
||||
/// <summary>LSB-first bit reader over the 128 bits following the signature (a SizeHeader uses at most 68).</summary>
|
||||
private struct BitReader(UInt128 value)
|
||||
{
|
||||
private UInt128 _value = value;
|
||||
|
||||
public ulong Read(int count)
|
||||
{
|
||||
var v = (ulong)(_value & ((UInt128.One << count) - 1));
|
||||
_value >>= count;
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.IO.Compression;
|
||||
using Titano.Core.Metadata.Tiff;
|
||||
using Titano.Core.Metadata.Xmp;
|
||||
|
||||
namespace Titano.Core.Metadata.Png;
|
||||
|
||||
/// <summary>PNG chunks: IHDR (size, bit depth), eXIf (TIFF), iTXt/tEXt "XML:com.adobe.xmp" (XMP, zlib-compressed iTXt
|
||||
/// supported). Image data chunks are skipped by seeking; CRCs are not verified.</summary>
|
||||
internal static class PngParser
|
||||
{
|
||||
private const int MaxChunks = 4096;
|
||||
private const int KeywordProbeBytes = 80;
|
||||
private static ReadOnlySpan<byte> XmpKeyword => "XML:com.adobe.xmp\0"u8;
|
||||
|
||||
public static void Parse(MetadataBuilder m)
|
||||
{
|
||||
var s = m.Stream;
|
||||
Span<byte> head = stackalloc byte[8 + 13];
|
||||
long pos = 8;
|
||||
for (var i = 0; i < MaxChunks; i++)
|
||||
{
|
||||
m.ThrowIfCancelled();
|
||||
var n = s.ReadUpTo(pos, head);
|
||||
if (n < 8)
|
||||
{
|
||||
m.Warn("PNG truncated before IEND");
|
||||
return;
|
||||
}
|
||||
var length = BinaryPrimitives.ReadUInt32BigEndian(head);
|
||||
var type = head.Slice(4, 4);
|
||||
var data = pos + 8;
|
||||
if (length > int.MaxValue || data > m.Length - length)
|
||||
{
|
||||
m.Warn("PNG chunk runs past the end of the file");
|
||||
return;
|
||||
}
|
||||
if (type.SequenceEqual("IHDR"u8) && length >= 13 && n == head.Length)
|
||||
{
|
||||
var width = BinaryPrimitives.ReadUInt32BigEndian(head[8..]);
|
||||
var height = BinaryPrimitives.ReadUInt32BigEndian(head[12..]);
|
||||
if (width <= int.MaxValue && height <= int.MaxValue) m.SetContainerSize((int)width, (int)height, head[16]);
|
||||
}
|
||||
else if (type.SequenceEqual("eXIf"u8) && length >= 8)
|
||||
{
|
||||
var exif = n >= 14 && head[8..].StartsWith("Exif\0\0"u8) ? data + 6 : data;
|
||||
ExifParser.ParseEmbedded(m, exif);
|
||||
}
|
||||
else if (type.SequenceEqual("iTXt"u8) || type.SequenceEqual("tEXt"u8))
|
||||
{
|
||||
ReadText(m, data, (int)length, international: type[0] == 'i');
|
||||
}
|
||||
else if (type.SequenceEqual("IEND"u8))
|
||||
{
|
||||
return;
|
||||
}
|
||||
pos = data + length + 4;
|
||||
}
|
||||
m.Warn("PNG has too many chunks; rest ignored");
|
||||
}
|
||||
|
||||
/// <summary>XMP text chunk: keyword "XML:com.adobe.xmp"; iTXt adds compression flag/method, language and translated keyword.</summary>
|
||||
private static void ReadText(MetadataBuilder m, long data, int length, bool international)
|
||||
{
|
||||
Span<byte> probe = stackalloc byte[XmpKeyword.Length];
|
||||
if (length < probe.Length || !m.Stream.TryReadAt(data, probe) || !probe.SequenceEqual(XmpKeyword)) return;
|
||||
if (m.Stream.ReadAt(data, length, XmpParser.MaxPacketBytes + KeywordProbeBytes) is not { } chunk)
|
||||
{
|
||||
m.Warn("PNG XMP chunk too large");
|
||||
return;
|
||||
}
|
||||
var r = new SpanReader(chunk);
|
||||
r.Skip(XmpKeyword.Length);
|
||||
var compressed = false;
|
||||
if (international)
|
||||
{
|
||||
compressed = r.U8() == 1;
|
||||
r.Skip(1);
|
||||
r.CString();
|
||||
r.CString();
|
||||
}
|
||||
if (!r.Ok) return;
|
||||
var text = chunk.AsMemory(r.Position);
|
||||
if (!compressed)
|
||||
{
|
||||
m.AddXmp(text.ToArray());
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
using var z = new ZLibStream(new MemoryStream(chunk, r.Position, text.Length, writable: false), CompressionMode.Decompress);
|
||||
using var packet = new MemoryStream();
|
||||
var buffer = new byte[16 * 1024];
|
||||
int read;
|
||||
while ((read = z.Read(buffer)) > 0)
|
||||
{
|
||||
if (packet.Length + read > XmpParser.MaxPacketBytes)
|
||||
{
|
||||
m.Warn("PNG XMP larger than 4 MB ignored");
|
||||
return;
|
||||
}
|
||||
packet.Write(buffer, 0, read);
|
||||
}
|
||||
m.AddXmp(packet.ToArray());
|
||||
}
|
||||
catch (InvalidDataException)
|
||||
{
|
||||
m.Warn("PNG compressed XMP is corrupt");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using System.Buffers.Binary;
|
||||
using System.Text;
|
||||
using Titano.Core.Metadata.Jpeg;
|
||||
using Titano.Core.Metadata.Tiff;
|
||||
|
||||
namespace Titano.Core.Metadata.Raf;
|
||||
|
||||
/// <summary>
|
||||
/// Fujifilm RAF: "FUJIFILMCCD-RAW " header with a big-endian directory (0x54 JPEG offset/length, 0x5C RAF-directory
|
||||
/// offset/length, 0x64 CFA offset/length). Camera fields come from the embedded JPEG's Exif, which is also the
|
||||
/// preview; the main size from the RAF directory records (0x0111 cropped, else 0x0100 full), bit depth from the
|
||||
/// FujiIFD at the CFA offset when that is a TIFF block.
|
||||
/// </summary>
|
||||
internal static class RafParser
|
||||
{
|
||||
private const int HeaderLength = 0x6C;
|
||||
private const int MaxDirectoryBytes = 64 * 1024;
|
||||
private const int MaxRecords = 512;
|
||||
private const ushort RawImageFullSize = 0x0100;
|
||||
private const ushort RawImageCroppedSize = 0x0111;
|
||||
private const ushort FujiIfd = 0xF000;
|
||||
private const ushort FujiRawWidth = 0xF001;
|
||||
private const ushort FujiRawHeight = 0xF002;
|
||||
private const ushort FujiBitsPerSample = 0xF003;
|
||||
|
||||
public static void Parse(MetadataBuilder m)
|
||||
{
|
||||
var header = m.Stream.ReadAt(0, HeaderLength, HeaderLength);
|
||||
if (header is null)
|
||||
{
|
||||
m.Warn("RAF header truncated");
|
||||
return;
|
||||
}
|
||||
var h = header.AsSpan();
|
||||
var jpegOffset = BinaryPrimitives.ReadUInt32BigEndian(h[0x54..]);
|
||||
var jpegLength = BinaryPrimitives.ReadUInt32BigEndian(h[0x58..]);
|
||||
if (jpegOffset >= HeaderLength && jpegLength > 0 && jpegOffset <= m.Length - jpegLength)
|
||||
JpegParser.Parse(m, jpegOffset, (long)jpegOffset + jpegLength, primary: false);
|
||||
else
|
||||
m.Warn("RAF embedded JPEG missing");
|
||||
ReadDirectory(m, BinaryPrimitives.ReadUInt32BigEndian(h[0x5C..]), BinaryPrimitives.ReadUInt32BigEndian(h[0x60..]));
|
||||
ReadFujiIfd(m, BinaryPrimitives.ReadUInt32BigEndian(h[0x64..]));
|
||||
m.Make ??= "FUJIFILM";
|
||||
m.Model ??= Ascii(h.Slice(0x1C, 32));
|
||||
}
|
||||
|
||||
/// <summary>RAF directory: u32 count, then (u16 tag, u16 size, data) records; sizes are (height, width) pairs.</summary>
|
||||
private static void ReadDirectory(MetadataBuilder m, uint offset, uint length)
|
||||
{
|
||||
if (offset == 0 || m.Stream.ReadAt(offset, Math.Min(length, MaxDirectoryBytes), MaxDirectoryBytes) is not { } data) return;
|
||||
var r = new SpanReader(data);
|
||||
var count = Math.Min(r.U32(), MaxRecords);
|
||||
(int Width, int Height) full = default, cropped = default;
|
||||
for (var i = 0; i < count && r.Ok; i++)
|
||||
{
|
||||
var tag = r.U16();
|
||||
var record = r.Take(r.U16());
|
||||
if (!r.Ok || record.Length < 4) continue;
|
||||
var size = (BinaryPrimitives.ReadUInt16BigEndian(record[2..]), BinaryPrimitives.ReadUInt16BigEndian(record));
|
||||
if (tag == RawImageFullSize) full = size;
|
||||
else if (tag == RawImageCroppedSize) cropped = size;
|
||||
}
|
||||
var best = cropped.Width > 0 && cropped.Height > 0 ? cropped : full;
|
||||
m.SetContainerSize(best.Width, best.Height);
|
||||
}
|
||||
|
||||
private static void ReadFujiIfd(MetadataBuilder m, uint offset)
|
||||
{
|
||||
if (offset == 0 || TiffReader.Open(m.Stream, offset) is not { } r) return;
|
||||
if (r.ReadIfd(r.FirstIfdOffset) is not { } ifd0 || IfdValue.UInt(r, ifd0, FujiIfd) is not { } sub || r.ReadIfd(sub) is not { } fuji) return;
|
||||
m.CodedBitsPerSample ??= IfdValue.Int(r, fuji, FujiBitsPerSample);
|
||||
m.AddRaster(IfdValue.Int(r, fuji, FujiRawWidth) ?? 0, IfdValue.Int(r, fuji, FujiRawHeight) ?? 0, null, true);
|
||||
}
|
||||
|
||||
private static string? Ascii(ReadOnlySpan<byte> field)
|
||||
{
|
||||
var end = field.IndexOf((byte)0);
|
||||
var s = Encoding.ASCII.GetString(end < 0 ? field : field[..end]).Trim();
|
||||
return s.Length > 0 && !s.Any(char.IsControl) ? s : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Buffers.Binary;
|
||||
using Titano.Core.Metadata.Tiff;
|
||||
using Titano.Core.Metadata.Xmp;
|
||||
|
||||
namespace Titano.Core.Metadata.Riff;
|
||||
|
||||
/// <summary>RIFF/WebP: canvas size from VP8X, else the VP8 key-frame or VP8L header; Exif from "EXIF" (with or without
|
||||
/// an "Exif\0\0" prefix) and XMP from "XMP ". Image data is skipped by seeking.</summary>
|
||||
internal static class WebPParser
|
||||
{
|
||||
private const int MaxChunks = 1024;
|
||||
|
||||
public static void Parse(MetadataBuilder m)
|
||||
{
|
||||
var s = m.Stream;
|
||||
Span<byte> h = stackalloc byte[8 + 10];
|
||||
if (!s.TryReadAt(0, h[..12]))
|
||||
{
|
||||
m.Warn("WebP header truncated");
|
||||
return;
|
||||
}
|
||||
var end = Math.Min(m.Length, 8 + (long)BinaryPrimitives.ReadUInt32LittleEndian(h[4..]));
|
||||
long pos = 12;
|
||||
for (var i = 0; i < MaxChunks && pos <= end - 8; i++)
|
||||
{
|
||||
m.ThrowIfCancelled();
|
||||
var n = s.ReadUpTo(pos, h);
|
||||
var size = BinaryPrimitives.ReadUInt32LittleEndian(h[4..]);
|
||||
var data = pos + 8;
|
||||
var payload = h[8..n];
|
||||
if (size > end - data)
|
||||
{
|
||||
m.Warn("WebP chunk runs past the end of the file");
|
||||
size = (uint)(end - data);
|
||||
}
|
||||
var fourCC = h[..4];
|
||||
if (fourCC.SequenceEqual("VP8X"u8) && payload.Length >= 10)
|
||||
{
|
||||
m.SetContainerSize(1 + UInt24(payload[4..]), 1 + UInt24(payload[7..]), 8);
|
||||
}
|
||||
else if (fourCC.SequenceEqual("VP8 "u8) && payload.Length >= 10 && payload[3..6].SequenceEqual((ReadOnlySpan<byte>)[0x9D, 0x01, 0x2A]))
|
||||
{
|
||||
m.SetContainerSize(BinaryPrimitives.ReadUInt16LittleEndian(payload[6..]) & 0x3FFF, BinaryPrimitives.ReadUInt16LittleEndian(payload[8..]) & 0x3FFF, 8);
|
||||
}
|
||||
else if (fourCC.SequenceEqual("VP8L"u8) && payload.Length >= 5 && payload[0] == 0x2F)
|
||||
{
|
||||
var bits = BinaryPrimitives.ReadUInt32LittleEndian(payload[1..]);
|
||||
m.SetContainerSize((int)(bits & 0x3FFF) + 1, (int)((bits >> 14) & 0x3FFF) + 1, 8);
|
||||
}
|
||||
else if (fourCC.SequenceEqual("EXIF"u8) && size >= 8)
|
||||
{
|
||||
ExifParser.ParseEmbedded(m, payload.StartsWith("Exif\0\0"u8) ? data + 6 : data);
|
||||
}
|
||||
else if (fourCC.SequenceEqual("XMP "u8))
|
||||
{
|
||||
m.AddXmp(s.ReadAt(data, size, XmpParser.MaxPacketBytes));
|
||||
}
|
||||
pos = data + size + (size & 1);
|
||||
}
|
||||
}
|
||||
|
||||
private static int UInt24(ReadOnlySpan<byte> b) => b[0] | b[1] << 8 | b[2] << 16;
|
||||
}
|
||||
Reference in new issue
Block a user