using Chronos.Core.Metadata.Tiff; namespace Chronos.Core.Metadata.MakerNotes; /// Sony MakerNote ("SONY DSC/CAM" 12-byte header or headerless; offsets relative to the Exif TIFF). /// Lens: the unencrypted LensSpec (BCD focal/aperture range); numeric LensType tables are not shipped. Exposure mode /// from ExposureMode (0xB041). internal static class SonyMakerNote { private const ushort ColorTemperature = 0xB021; private const ushort ImageStabilization = 0xB026; private const ushort LensSpec = 0xB02A; private const ushort ExposureMode = 0xB041; public static void Parse(TiffReader r, Ifd ifd, MetadataBuilder m) { var v = m.MakerNote; if (ifd.Entries.TryGetValue(LensSpec, out var spec) && spec.Count == 8 && r.ReadBytes(spec) is { } bytes) v.LensModel ??= DecodeLensSpec(bytes); v.ColorTemperature ??= IfdValue.UInt(r, ifd, ColorTemperature) is { } kelvin and > 0 and < 65535 ? (int)kelvin : null; v.ImageStabilization ??= IfdValue.UInt(r, ifd, ImageStabilization) switch { 0 => false, 1 => true, _ => null, }; // 0 Program AE, 1 Portrait, 3 Sports, 5 Landscape, 7 Aperture priority, 8 Shutter priority, 15 Manual. v.ExposureProgram ??= IfdValue.UInt(r, ifd, ExposureMode) switch { 15 => 1, 0 => 2, 7 => 3, 8 => 4, 3 => 6, 1 => 7, 5 => 8, _ => null, }; } /// Bytes 1–2 short focal, 3–4 long focal (BCD mm), 5–6 apertures (BCD ×10); flags 0 and 7 are not decoded. private static string? DecodeLensSpec(ReadOnlySpan s) { if (!TryBcd(s.Slice(1, 2), out var minFocal) || !TryBcd(s.Slice(3, 2), out var maxFocal) || !TryBcd(s.Slice(5, 1), out var minAperture) || !TryBcd(s.Slice(6, 1), out var maxAperture) || minFocal == 0) return null; return MakerNoteParser.FormatLens(minFocal, maxFocal, minAperture / 10.0, maxAperture / 10.0, "F"); } private static bool TryBcd(ReadOnlySpan bytes, out int value) { value = 0; foreach (var b in bytes) { if (b >> 4 > 9 || (b & 0xF) > 9) return false; value = value * 100 + (b >> 4) * 10 + (b & 0xF); } return true; } }