diff --git a/src/Titano.Core/Metadata/FrameMetadata.cs b/src/Titano.Core/Metadata/FrameMetadata.cs index e38d2714..2fca42c5 100644 --- a/src/Titano.Core/Metadata/FrameMetadata.cs +++ b/src/Titano.Core/Metadata/FrameMetadata.cs @@ -10,6 +10,19 @@ public enum PreviewKind FullSize, } +/// Resolution a camera recorded its RAW at (BRIEF §9 sRaw flags). +public enum RawSize +{ + /// Full sensor resolution. + Full, + + /// Canon mRAW (sRAW1), Nikon RAW M. + Medium, + + /// Canon sRAW (sRAW2), Nikon RAW S and sRAW. + Small, +} + /// An embedded JPEG inside a RAW/TIFF/HEIF/JPEG(MPF) container. 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; } + /// + /// 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). + /// + public bool? IntervalShooting { get; init; } + + /// Resolution of a camera RAW from the MakerNote (sRAW/mRAW, RAW M/S); null when not recorded. + public RawSize? RawSize { get; init; } + /// Embedded JPEGs, largest first. public IReadOnlyList Previews { get; init; } = []; diff --git a/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs index 6c60030e..59eda5e0 100644 --- a/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/CanonMakerNote.cs @@ -1,9 +1,13 @@ using System.Globalization; +using Titano.Core.Imaging; using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; -/// Canon MakerNote: a bare IFD whose offsets are relative to the enclosing TIFF header (also CR3 CMT3). +/// +/// 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. +/// 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, + }; + } } } diff --git a/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs index 1b5f93e6..911ba340 100644 --- a/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/FujifilmMakerNote.cs @@ -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 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, + }; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs b/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs index 9d2625b9..97da31b3 100644 --- a/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs +++ b/src/Titano.Core/Metadata/MakerNotes/MakerNoteValues.cs @@ -9,4 +9,14 @@ internal sealed class MakerNoteValues public bool? ImageStabilization { get; set; } public int? Iso { get; set; } public int? ColorTemperature { get; set; } + + /// The vendor's exposure mode as an Exif ExposureProgram 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. + public int? ExposureProgram { get; set; } + + /// True when the camera's own interval-shooting or time-lapse mode took the photo, false for another known mode. + public bool? IntervalShooting { get; set; } + + /// Resolution of a camera RAW (Canon sRAW/mRAW, Nikon RAW M/S). + public RawSize? RawSize { get; set; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs index a7e50bb7..c53a86fe 100644 --- a/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/NikonMakerNote.cs @@ -3,14 +3,19 @@ using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; /// 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. +/// ("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. 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 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, + }; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs index e00e90b5..66455c9d 100644 --- a/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/OlympusMakerNote.cs @@ -6,7 +6,8 @@ namespace Titano.Core.Metadata.MakerNotes; /// /// 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. /// 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 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) diff --git a/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs index 9709903c..6217e5e7 100644 --- a/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/PanasonicMakerNote.cs @@ -2,13 +2,17 @@ using Titano.Core.Metadata.Tiff; namespace Titano.Core.Metadata.MakerNotes; -/// Panasonic MakerNote: "Panasonic\0\0\0" then an IFD whose offsets are relative to the Exif TIFF. +/// 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. 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, + }; } } diff --git a/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs b/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs index 692a129b..4280f7a2 100644 --- a/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs +++ b/src/Titano.Core/Metadata/MakerNotes/SonyMakerNote.cs @@ -3,12 +3,14 @@ using Titano.Core.Metadata.Tiff; namespace Titano.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. +/// 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) { @@ -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, + }; } /// Bytes 1–2 short focal, 3–4 long focal (BCD mm), 5–6 apertures (BCD ×10); flags 0 and 7 are not decoded. diff --git a/src/Titano.Core/Metadata/MetadataBuilder.cs b/src/Titano.Core/Metadata/MetadataBuilder.cs index f55fe60e..e1673585 100644 --- a/src/Titano.Core/Metadata/MetadataBuilder.cs +++ b/src/Titano.Core/Metadata/MetadataBuilder.cs @@ -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(), }; diff --git a/tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs b/tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs new file mode 100644 index 00000000..cceede71 --- /dev/null +++ b/tests/Titano.Core.Tests/Metadata/MakerNoteTests.cs @@ -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; + +/// +/// 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. +/// +public sealed class MakerNoteTests +{ + /// A JPEG whose Exif lacks ExposureProgram and carries after . + 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; + } + + /// A CR2 ("CR" header block) with only IFD0, the Exif IFD and a Canon MakerNote. + 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)); + } +}