using Chronos.Core.Imaging;
using Chronos.Core.Metadata;
using Chronos.Core.Metadata.Tiff;
using Chronos.Core.Tests.Metadata.Builders;
using Chronos.Testing;
using static Chronos.Core.Tests.Metadata.MetadataTestHelpers;
namespace Chronos.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));
}
}