feat(metadata): parse TIFF/Exif, JPEG segments and MakerNotes
ExifParser walks IFD0, Exif, GPS and SubIFDs, selects the main raster (largest full-resolution IFD), and enumerates embedded JPEG previews (JPEGInterchangeFormat, single-strip/tile Compression 6/7, lossless raw strips excluded). JpegParser scans APP1 Exif/XMP/extended XMP, APP2 MPF large thumbnails and SOFn up to SOS, tolerating fill bytes and garbage. Vendor MakerNotes (Canon, Nikon, Sony, Fujifilm, Olympus/OM, Panasonic) fill serial, lens, shutter count, stabiliser, ISO and colour temperature only when standard tags did not. TiffTags gains GPSVersionID, GPSStatus, YCbCrPositioning, StandardOutputSensitivity and ISOSpeed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
11 files changed
+918
No files matched your search
@@ -0,0 +1,66 @@
|
||||
using Titano.Core.Metadata.Jpeg;
|
||||
using Titano.Core.Metadata.Tiff;
|
||||
|
||||
namespace Titano.Core.Metadata.MakerNotes;
|
||||
|
||||
/// <summary>
|
||||
/// Olympus / OM System MakerNote. "OLYMPUS\0II\3\0" (IFD at +12) and "OM SYSTEM\0\0\0II\4\0" (IFD at +16) use offsets
|
||||
/// relative to the MakerNote start and their own byte order; legacy "OLYMP\0" has its IFD at +8 relative to the
|
||||
/// Exif TIFF. Equipment (0x2010) gives serial and lens; CameraSettings (0x2020) gives the ORF preview and stabiliser.
|
||||
/// </summary>
|
||||
internal static class OlympusMakerNote
|
||||
{
|
||||
private const ushort Equipment = 0x2010;
|
||||
private const ushort CameraSettings = 0x2020;
|
||||
private const ushort EquipmentSerialNumber = 0x0101;
|
||||
private const ushort EquipmentLensModel = 0x0203;
|
||||
private const ushort PreviewImageValid = 0x0100;
|
||||
private const ushort PreviewImageStart = 0x0101;
|
||||
private const ushort PreviewImageLength = 0x0102;
|
||||
private const ushort ImageStabilization = 0x0604;
|
||||
|
||||
public static void Parse(TiffReader exif, long start, ReadOnlySpan<byte> header, MetadataBuilder m)
|
||||
{
|
||||
if (header.StartsWith("OLYMP\0"u8))
|
||||
{
|
||||
MakerNoteParser.Vendor(exif, (uint)(start - exif.Base + 8), m, ParseIfd);
|
||||
return;
|
||||
}
|
||||
var orderAt = header.StartsWith("OM SYSTEM\0"u8) ? 12 : 8;
|
||||
if (header.Length < orderAt + 4 || header[orderAt] != header[orderAt + 1] || header[orderAt] is not ((byte)'I' or (byte)'M'))
|
||||
{
|
||||
m.Warn("Olympus MakerNote header malformed");
|
||||
return;
|
||||
}
|
||||
var reader = TiffReader.ForEmbedded(m.Stream, start, header[orderAt] == 'I');
|
||||
MakerNoteParser.Vendor(reader, (uint)(orderAt + 4), m, ParseIfd);
|
||||
m.AddWarnings(reader.Warnings);
|
||||
}
|
||||
|
||||
private static void ParseIfd(TiffReader r, Ifd ifd, MetadataBuilder m)
|
||||
{
|
||||
var v = m.MakerNote;
|
||||
if (SubIfd(r, ifd, Equipment) is { } equipment)
|
||||
{
|
||||
v.BodySerial ??= IfdValue.String(r, equipment, EquipmentSerialNumber);
|
||||
v.LensModel ??= IfdValue.String(r, equipment, EquipmentLensModel);
|
||||
}
|
||||
if (SubIfd(r, ifd, CameraSettings) is not { } settings) return;
|
||||
v.ImageStabilization ??= IfdValue.UInt(r, settings, ImageStabilization) switch
|
||||
{
|
||||
0 => false,
|
||||
>= 1 and <= 4 => true,
|
||||
_ => null,
|
||||
};
|
||||
if (IfdValue.UInt(r, settings, PreviewImageValid) != 0
|
||||
&& IfdValue.UInt(r, settings, PreviewImageStart) is { } offset and > 0
|
||||
&& IfdValue.UInt(r, settings, PreviewImageLength) is { } length and > 0)
|
||||
{
|
||||
JpegParser.AddPreview(m, r.Base + offset, length);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Sub-IFD stored either as an IFD/LONG pointer or as an UNDEFINED block; both resolve to the value offset.</summary>
|
||||
private static Ifd? SubIfd(TiffReader r, Ifd ifd, ushort tag) =>
|
||||
ifd.Entries.TryGetValue(tag, out var e) && !(e.IsInline && e.Type == TiffType.Undefined) ? r.ReadIfd(e.ValueOffset) : null;
|
||||
}
|
||||
Reference in new issue
Block a user