fix(metadata): read exposure mode, interval shooting and RAW size from MakerNotes

BRIEF §9 lists exposure mode, intervalometer hints and sRaw flags among the MakerNote
fields; no vendor parser read them. The vendor exposure mode (Canon, Olympus,
Fujifilm, Panasonic, Sony) now fills ExposureProgram when Exif has none, Panasonic's
TimerRecording becomes FrameMetadata.IntervalShooting, and Canon's SRAWQuality and
Nikon's ImageSizeRAW / small NEF compression become FrameMetadata.RawSize. Nikon keeps
its interval-shooting state in the encrypted ShotInfo, which stays undecoded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 09:58:21 +00:00
1 parent d09b5a5cfb
commit 61e61702c6
10 files changed
+271 -7

No files matched your search

+22
View File
@@ -10,6 +10,19 @@ public enum PreviewKind
FullSize,
}
/// <summary>Resolution a camera recorded its RAW at (BRIEF §9 sRaw flags).</summary>
public enum RawSize
{
/// <summary>Full sensor resolution.</summary>
Full,
/// <summary>Canon mRAW (sRAW1), Nikon RAW M.</summary>
Medium,
/// <summary>Canon sRAW (sRAW2), Nikon RAW S and sRAW.</summary>
Small,
}
/// <summary>An embedded JPEG inside a RAW/TIFF/HEIF/JPEG(MPF) container.</summary>
public sealed record EmbeddedPreview(long Offset, long Length, int Width, int Height, PreviewKind Kind);
@@ -59,6 +72,15 @@ public sealed record FrameMetadata
public long? ShutterCount { get; init; }
public bool? ImageStabilization { get; init; }
/// <summary>
/// Intervalometer hint from the MakerNote: true when the camera's own interval-shooting or time-lapse mode took the
/// photo, false when the MakerNote names another drive mode, null when it says nothing (BRIEF §9).
/// </summary>
public bool? IntervalShooting { get; init; }
/// <summary>Resolution of a camera RAW from the MakerNote (sRAW/mRAW, RAW M/S); null when not recorded.</summary>
public RawSize? RawSize { get; init; }
/// <summary>Embedded JPEGs, largest first.</summary>
public IReadOnlyList<EmbeddedPreview> Previews { get; init; } = [];
@@ -1,9 +1,13 @@
using System.Globalization;
using Titano.Core.Imaging;
using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Metadata.MakerNotes;
/// <summary>Canon MakerNote: a bare IFD whose offsets are relative to the enclosing TIFF header (also CR3 CMT3).</summary>
/// <summary>
/// Canon MakerNote: a bare IFD whose offsets are relative to the enclosing TIFF header (also CR3 CMT3). CameraSettings
/// gives the stabiliser, the exposure mode and the sRAW size; Canon records no interval-shooting hint there.
/// </summary>
internal static class CanonMakerNote
{
private const ushort CameraSettings = 0x0001;
@@ -11,7 +15,9 @@ internal static class CanonMakerNote
private const ushort LensModel = 0x0095;
private const ushort InternalSerialNumber = 0x0096;
private const ushort ColorTemperature = 0x00AE;
private const int ExposureModeIndex = 20;
private const int ImageStabilizationIndex = 34;
private const int SRawQualityIndex = 46;
public static void Parse(TiffReader r, Ifd ifd, MetadataBuilder m)
{
@@ -25,7 +31,21 @@ internal static class CanonMakerNote
v.BodySerial ??= IfdValue.String(r, ifd, InternalSerialNumber);
v.LensModel ??= IfdValue.String(r, ifd, LensModel);
v.ColorTemperature ??= IfdValue.Int(r, ifd, ColorTemperature) is { } kelvin and > 0 and < 65535 ? kelvin : null;
if (IfdValue.UInts(r, ifd, CameraSettings) is { Length: > ImageStabilizationIndex } settings)
if (IfdValue.UInts(r, ifd, CameraSettings) is not { } settings) return;
if (settings.Length > ExposureModeIndex)
{
// CanonExposureMode: 1 Program, 2 Tv, 3 Av, 4 Manual, 5 Depth-of-field AE, 6 M-Dep, 7 Bulb, 8 Fv; 0 Easy (scene).
v.ExposureProgram ??= settings[ExposureModeIndex] switch
{
1 or 8 => 2,
2 => 4,
3 => 3,
4 or 7 => 1,
5 or 6 => 5,
_ => null,
};
}
if (settings.Length > ImageStabilizationIndex)
{
// 0 Off, 1 On, 2 Shoot only, 3 Panning, 4 Dynamic; +256 for the "(2)" variants.
v.ImageStabilization ??= (settings[ImageStabilizationIndex] & 0xFF) switch
@@ -35,5 +55,16 @@ internal static class CanonMakerNote
_ => null,
};
}
if (settings.Length > SRawQualityIndex && m.Format is ImageFileFormat.Cr2 or ImageFileFormat.Cr3)
{
// SRAWQuality: 0 full-size RAW, 1 sRAW1 (mRAW), 2 sRAW2 (sRAW).
v.RawSize ??= settings[SRawQualityIndex] switch
{
0 => RawSize.Full,
1 => RawSize.Medium,
2 => RawSize.Small,
_ => null,
};
}
}
}
@@ -9,6 +9,7 @@ internal static class FujifilmMakerNote
{
private const ushort SerialNumber = 0x0010;
private const ushort ColorTemperature = 0x1005;
private const ushort PictureMode = 0x1031;
private const ushort ImageStabilization = 0x1422;
public static void Parse(MetadataBuilder m, long start, ReadOnlySpan<byte> header)
@@ -27,5 +28,17 @@ internal static class FujifilmMakerNote
// [0] type (0 none, 1 optical, 2 sensor-shift, …), [1] mode (0 off, 1/2 on).
if (IfdValue.UInts(r, ifd, ImageStabilization) is [var type, var mode, ..])
v.ImageStabilization ??= type != 0 && mode != 0;
// PictureMode: 0x300 Manual, 6 Program AE, 0x100 Aperture priority, 0x200 Shutter priority, 1 Portrait, 2 Landscape, 4 Sports.
v.ExposureProgram ??= IfdValue.UInt(r, ifd, PictureMode) switch
{
0x300 => 1,
6 => 2,
0x100 => 3,
0x200 => 4,
4 => 6,
1 => 7,
2 => 8,
_ => null,
};
}
}
@@ -9,4 +9,14 @@ internal sealed class MakerNoteValues
public bool? ImageStabilization { get; set; }
public int? Iso { get; set; }
public int? ColorTemperature { get; set; }
/// <summary>The vendor's exposure mode as an Exif <c>ExposureProgram</c> value (1 manual, 2 program, 3 aperture
/// priority, 4 shutter priority, 5 creative, 6 action, 7 portrait, 8 landscape); scene modes without one stay null.</summary>
public int? ExposureProgram { get; set; }
/// <summary>True when the camera's own interval-shooting or time-lapse mode took the photo, false for another known mode.</summary>
public bool? IntervalShooting { get; set; }
/// <summary>Resolution of a camera RAW (Canon sRAW/mRAW, Nikon RAW M/S).</summary>
public RawSize? RawSize { get; set; }
}
@@ -3,14 +3,19 @@ using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Metadata.MakerNotes;
/// <summary>Nikon MakerNote. Type 3 ("Nikon\0" + version 2.x) embeds its own TIFF header 10 bytes in; type 1
/// ("Nikon\0" + version 1) has an IFD at +8 and headerless type 2 an IFD at +0, both relative to the Exif TIFF.</summary>
/// ("Nikon\0" + version 1) has an IFD at +8 and headerless type 2 an IFD at +0, both relative to the Exif TIFF.
/// The RAW size comes from ImageSizeRAW or the "small" NEF compression; the interval-shooting state lives only in the
/// encrypted ShotInfo, which is not decoded.</summary>
internal static class NikonMakerNote
{
private const ushort Iso = 0x0002;
private const ushort SerialNumber = 0x001D;
private const ushort VrInfo = 0x001F;
private const ushort ImageSizeRaw = 0x003E;
private const ushort Lens = 0x0084;
private const ushort NefCompression = 0x0093;
private const ushort ShutterCount = 0x00A7;
private const uint SmallNef = 8;
public static void Parse(TiffReader exif, long start, ReadOnlySpan<byte> header, MetadataBuilder m)
{
@@ -50,5 +55,13 @@ internal static class NikonMakerNote
_ => null,
};
}
// ImageSizeRAW: 1 Large, 2 Medium, 3 Small; NEFCompression 8 is the older sRAW ("Small").
v.RawSize ??= IfdValue.UInt(r, ifd, ImageSizeRaw) switch
{
1 => RawSize.Full,
2 => RawSize.Medium,
3 => RawSize.Small,
_ => IfdValue.UInt(r, ifd, NefCompression) is SmallNef ? RawSize.Small : null,
};
}
}
@@ -6,7 +6,8 @@ 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.
/// Exif TIFF. Equipment (0x2010) gives serial and lens; CameraSettings (0x2020) gives the ORF preview, the stabiliser
/// and the exposure mode.
/// </summary>
internal static class OlympusMakerNote
{
@@ -17,6 +18,7 @@ internal static class OlympusMakerNote
private const ushort PreviewImageValid = 0x0100;
private const ushort PreviewImageStart = 0x0101;
private const ushort PreviewImageLength = 0x0102;
private const ushort ExposureMode = 0x0200;
private const ushort ImageStabilization = 0x0604;
public static void Parse(TiffReader exif, long start, ReadOnlySpan<byte> header, MetadataBuilder m)
@@ -52,6 +54,15 @@ internal static class OlympusMakerNote
>= 1 and <= 4 => true,
_ => null,
};
// 1 Manual, 2 Program, 3 Aperture priority, 4 Shutter priority, 5 Program shift.
v.ExposureProgram ??= IfdValue.UInt(r, settings, ExposureMode) switch
{
1 => 1,
2 or 5 => 2,
3 => 3,
4 => 4,
_ => 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)
@@ -2,13 +2,17 @@ using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Metadata.MakerNotes;
/// <summary>Panasonic MakerNote: "Panasonic\0\0\0" then an IFD whose offsets are relative to the Exif TIFF.</summary>
/// <summary>Panasonic MakerNote: "Panasonic\0\0\0" then an IFD whose offsets are relative to the Exif TIFF. Besides
/// identity and stabiliser it gives the shooting mode and TimerRecording, which says whether the camera's time-lapse
/// mode took the photo.</summary>
internal static class PanasonicMakerNote
{
private const ushort ImageStabilization = 0x001A;
private const ushort ShootingMode = 0x001F;
private const ushort InternalSerialNumber = 0x0025;
private const ushort ColorTempKelvin = 0x0044;
private const ushort LensType = 0x0051;
private const ushort TimerRecording = 0x0096;
public static void Parse(TiffReader r, Ifd ifd, MetadataBuilder m)
{
@@ -23,5 +27,24 @@ internal static class PanasonicMakerNote
2 or (>= 4 and <= 7) or (>= 9 and <= 12) => true,
_ => null,
};
// ShootingMode: 2 Portrait, 3 Scenery, 4 Sports, 6 Program, 7 Aperture priority, 8 Shutter priority, 11 Manual.
v.ExposureProgram ??= IfdValue.UInt(r, ifd, ShootingMode) switch
{
11 => 1,
6 => 2,
7 => 3,
8 => 4,
4 => 6,
2 => 7,
3 => 8,
_ => null,
};
// TimerRecording: 0 Off, 1 Time Lapse, 2 Stop-motion Animation, 3 Focus Bracketing.
v.IntervalShooting ??= IfdValue.UInt(r, ifd, TimerRecording) switch
{
1 => true,
0 or 2 or 3 => false,
_ => null,
};
}
}
@@ -3,12 +3,14 @@ using Titano.Core.Metadata.Tiff;
namespace Titano.Core.Metadata.MakerNotes;
/// <summary>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.</summary>
/// Lens: the unencrypted LensSpec (BCD focal/aperture range); numeric LensType tables are not shipped. Exposure mode
/// from ExposureMode (0xB041).</summary>
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)
{
@@ -22,6 +24,18 @@ internal static class SonyMakerNote
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,
};
}
/// <summary>Bytes 1–2 short focal, 3–4 long focal (BCD mm), 5–6 apertures (BCD ×10); flags 0 and 7 are not decoded.</summary>
+3 -1
View File
@@ -153,7 +153,7 @@ internal sealed class MetadataBuilder
FocalLength = FocalLength,
FocalLength35 = FocalLength35,
ExposureBias = ExposureBias,
ExposureProgram = ExposureProgram,
ExposureProgram = ExposureProgram ?? MakerNote.ExposureProgram,
MeteringMode = MeteringMode,
WhiteBalanceMode = WhiteBalanceMode,
ColorTemperature = MakerNote.ColorTemperature,
@@ -167,6 +167,8 @@ internal sealed class MetadataBuilder
Label = Xmp.Label,
ShutterCount = MakerNote.ShutterCount,
ImageStabilization = MakerNote.ImageStabilization,
IntervalShooting = MakerNote.IntervalShooting,
RawSize = MakerNote.RawSize,
Previews = ResolvePreviews(main is { } m ? Math.Max(m.Width, m.Height) : 0),
Warnings = _warnings.ToArray(),
};
@@ -0,0 +1,125 @@
using Titano.Core.Imaging;
using Titano.Core.Metadata;
using Titano.Core.Metadata.Tiff;
using Titano.Core.Tests.Metadata.Builders;
using Titano.Testing;
using static Titano.Core.Tests.Metadata.MetadataTestHelpers;
namespace Titano.Core.Tests.Metadata;
/// <summary>
/// MakerNote fields of BRIEF §9 beyond identity: the vendor exposure mode (when Exif has no ExposureProgram), the
/// intervalometer hint and the reduced-size RAW flag, from synthetic vendor IFDs.
/// </summary>
public sealed class MakerNoteTests
{
/// <summary>A JPEG whose Exif lacks ExposureProgram and carries <paramref name="makerNote"/> after <paramref name="prefix"/>.</summary>
private static FrameMetadata Jpeg(string make, string model, byte[] prefix, IfdSpec makerNote, bool relativeToTiff = true, bool keepProgram = false)
{
var exif = Fixtures.ExifIfd(identity: false).Embedded(TiffTags.MakerNote, prefix, makerNote, relativeToTiff, littleEndian: true);
if (!keepProgram) exif.Without(TiffTags.ExposureProgram);
var m = Read(new JpegBuilder().Exif(TiffFile.Build(Fixtures.Ifd0(make, model, 1).Child(TiffTags.ExifIfd, exif))).Build(64, 48));
AssertNoFault(m);
return m;
}
/// <summary>A CR2 ("CR" header block) with only IFD0, the Exif IFD and a Canon MakerNote.</summary>
private static FrameMetadata Cr2(ushort[] cameraSettings)
{
var makerNote = new IfdSpec().Short(0x0001, cameraSettings);
var exif = Fixtures.ExifIfd(identity: false).Without(TiffTags.ExposureProgram)
.Embedded(TiffTags.MakerNote, [], makerNote, relativeToTiff: true, littleEndian: true);
var ifd0 = Fixtures.Ifd0("Canon", "Canon EOS 5D Mark IV", 1).Child(TiffTags.ExifIfd, exif);
var m = Read(TiffFile.Build(ifd0, true, headerExtra: [(byte)'C', (byte)'R', 2, 0, 0, 0, 0, 0]), "IMG_0001.CR2");
AssertNoFault(m);
return m;
}
private static ushort[] CanonSettings(ushort exposureMode, ushort sRawQuality) =>
[.. Enumerable.Range(0, 48).Select(i => (ushort)(i switch { 20 => exposureMode, 46 => sRawQuality, _ => 0 }))];
[Theory]
[Case(1, 2)]
[Case(2, 4)]
[Case(3, 3)]
[Case(4, 1)]
[Case(5, 5)]
[Case(7, 1)]
[Case(8, 2)]
public void CanonExposureModeFillsTheExposureProgram(int canonMode, int exposureProgram)
{
var m = Jpeg("Canon", "Canon EOS R6", [], new IfdSpec().Short(0x0001, CanonSettings((ushort)canonMode, 0)));
Assert.Equal(exposureProgram, m.ExposureProgram);
// Not a RAW: the sRAW field says nothing about a JPEG.
Assert.Null(m.RawSize);
}
[Test]
public void CanonEasyModeLeavesTheProgramUnknownAndExifWins()
{
Assert.Null(Jpeg("Canon", "Canon EOS R6", [], new IfdSpec().Short(0x0001, CanonSettings(0, 0))).ExposureProgram);
// The fixture's Exif says aperture priority (3); the MakerNote's manual mode does not override it.
Assert.Equal(3, Jpeg("Canon", "Canon EOS R6", [], new IfdSpec().Short(0x0001, CanonSettings(4, 0)), keepProgram: true).ExposureProgram);
}
[Theory]
[Case(0, RawSize.Full)]
[Case(1, RawSize.Medium)]
[Case(2, RawSize.Small)]
public void CanonSRawQualityGivesTheRawSize(int quality, RawSize expected)
{
var m = Cr2(CanonSettings(3, (ushort)quality));
Assert.Equal(ImageFileFormat.Cr2, m.Format);
Assert.Equal(expected, m.RawSize);
Assert.Equal(3, m.ExposureProgram);
// Bodies whose settings stop before the sRAW field record no size.
Assert.Null(Cr2([.. CanonSettings(3, (ushort)quality).Take(40)]).RawSize);
}
[Test]
public void NikonRawSizeComesFromImageSizeRawOrTheSmallNefCompression()
{
static FrameMetadata Nikon(IfdSpec ifd) => Jpeg("NIKON CORPORATION", "NIKON Z 8", [], ifd);
Assert.Equal(RawSize.Medium, Nikon(new IfdSpec().Short(0x003E, 2)).RawSize);
Assert.Equal(RawSize.Small, Nikon(new IfdSpec().Short(0x003E, 3)).RawSize);
Assert.Equal(RawSize.Full, Nikon(new IfdSpec().Short(0x003E, 1).Short(0x0093, 3)).RawSize);
Assert.Equal(RawSize.Small, Nikon(new IfdSpec().Short(0x0093, 8)).RawSize);
Assert.Null(Nikon(new IfdSpec().Short(0x0093, 3)).RawSize);
// Nikon keeps interval shooting in its encrypted ShotInfo only.
Assert.Null(Nikon(new IfdSpec().Short(0x003E, 1)).IntervalShooting);
}
[Test]
public void OlympusFujifilmAndSonyExposureModes()
{
var olympus = Jpeg("OLYMPUS", "E-M1MarkIII", [.. "OLYMP\0"u8, 1, 0], new IfdSpec().Child(0x2020, new IfdSpec().Short(0x0200, 4)));
Assert.Equal(4, olympus.ExposureProgram);
var fuji = Jpeg("FUJIFILM", "X-T5", [.. "FUJIFILM"u8, 12, 0, 0, 0], new IfdSpec().Short(0x1031, 0x300), relativeToTiff: false);
Assert.Equal(1, fuji.ExposureProgram);
var fujiAperture = Jpeg("FUJIFILM", "X-T5", [.. "FUJIFILM"u8, 12, 0, 0, 0], new IfdSpec().Short(0x1031, 0x100), relativeToTiff: false);
Assert.Equal(3, fujiAperture.ExposureProgram);
var sony = Jpeg("SONY", "ILCE-7M4", [.. "SONY DSC \0\0\0"u8], new IfdSpec().Short(0xB041, 8));
Assert.Equal(4, sony.ExposureProgram);
var sonyAuto = Jpeg("SONY", "ILCE-7M4", [.. "SONY DSC \0\0\0"u8], new IfdSpec().Short(0xB041, 6));
Assert.Null(sonyAuto.ExposureProgram);
}
[Theory]
[Case(1, true)]
[Case(0, false)]
[Case(2, false)]
[Case(3, false)]
public void PanasonicTimerRecordingIsTheIntervalometerHint(int timerRecording, bool expected)
{
var m = Jpeg("Panasonic", "DC-GH6", [.. "Panasonic\0\0\0"u8], new IfdSpec().Short(0x001F, 11).Short(0x0096, (ushort)timerRecording));
Assert.Equal(expected, m.IntervalShooting);
Assert.Equal(1, m.ExposureProgram);
}
[Test]
public void WithoutTheTagsNothingIsGuessed()
{
var m = Jpeg("Panasonic", "DC-GH6", [.. "Panasonic\0\0\0"u8], new IfdSpec().Short(0x001A, 2));
Assert.Equal(((bool?)null, (RawSize?)null, (int?)null), (m.IntervalShooting, m.RawSize, m.ExposureProgram));
}
}