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,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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user