Files
Chronos/tests/Chronos.Core.Tests/Conversion/FrameConverterTests.cs
T
Alby96 341fb77507 fix: conversions name, lock and settle as phase 4a's writers do
Phase 4b was built on phase 4a before its review fixes. Integrated on the reviewed 4a, the
conversion now follows the same rules as filing and the other writers.

- Names: a converted file is named as filing names a photo of its sequence (FrameNaming: the
  archive's file template, the frame's context in the sequence with its camera, lens, number
  and title, {name} the card's, {ext} the target's, a long name keeping its extension).
  Replace through ConvertOptions.FileName; New sequence through the ingest itself
  (StagedFrame.NamedAs), which numbers and names the copies in the new sequence. Names stay
  unique against every file present, those leaving included, so a Replace never moves onto a
  file; a test pins a stray file that keeps its bytes.
- Leases: stage A starts from the sidecar settled under the sequence's lease where the index
  has it then (SequenceWriter.ReadLocked), and FrameTrash.ReplaceAsync reads the row again
  under the lease, so a folder a Reorganize moved meanwhile is followed, never made again.
  A Reorganize the archive waits for keeps the conversion off its sequences (pinned).
- Journals: the replace journal is settled with the others. MassRenamer steps, restores into
  a sequence and sequence deletions wait for an unsettled replacement as they wait for a
  restore, and so do photo deletions, whose frames version the recovery reads
  (FrameTrash.EnsureReplaced); a scan settles it and reconciles nothing meanwhile; a restore
  settles its sequence's replace journal first, at the folder the index has under the lease.
- The convert job tells each sequence converted or made as it completes, as a Reorganize
  tells each step, so an editor open on it reads it again while the rest of the job runs.
- Desktop: JobKind.Convert gets its row in the settings' job priority list. A click on Apply
  that commits the quality no longer starts the same dry run again under the click (as 4a
  fixed for Reorganize).
- Panel: a dry run answered during the confirmation with the same plan no longer drops the
  confirmed Apply; a different plan is shown with Admin.Convert.PlanChanged (en, it).
- Tests: TrashDialogTests.EmptyQueuesTheJobOfTheConfirmedPlanOnly waits for the command to
  end before running it again (a running AsyncRelayCommand ignored the second Execute).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 2e0d1d6dcc10575734970bb9c77749f00777c859)
2026-10-27 22:03:07 +01:00

311 lines
16 KiB
C#

using Chronos.Core.Archiving;
using Chronos.Core.Codecs.Dng;
using Chronos.Core.Codecs.Jpeg;
using Chronos.Core.Codecs.Tiff;
using Chronos.Core.Conversion;
using Chronos.Core.Imaging;
using Chronos.Core.Imaging.Decoders;
using Chronos.Core.Metadata;
using Chronos.Core.Model;
using Chronos.Core.Naming;
using Chronos.Testing;
namespace Chronos.Core.Tests.Conversion;
/// <summary>
/// One photo converted (owner review 2, R7): to JPEG, TIFF-16 and linear DNG, decoded at full resolution only, with the
/// sidecar's capture facts to the tick and the original's camera, lens, modes and GPS, verified (read back, metadata, the
/// server's decoders) before it counts.
/// </summary>
[Category("conversion")]
public sealed class FrameConverterTests(TestContext context)
{
private static readonly DateTime Captured = new DateTime(2026, 9, 28, 18, 0, 5).AddTicks(1_234_567);
/// <summary>The camera's file says one second earlier, without sub-seconds: the sidecar wins.</summary>
private static readonly FrameMetadata Camera = new()
{
Make = "Canon",
Model = "Canon EOS R5",
BodySerial = "032021001234",
LensModel = "RF15-35mm F2.8 L IS USM",
CapturedAt = new DateTime(2026, 9, 28, 18, 0, 4),
UtcOffsetMinutes = 60,
ExposureTime = 1 / 250.0,
FNumber = 8,
Iso = 100,
FocalLength = 15,
FocalLength35 = 15,
ExposureBias = -2 / 3.0,
ExposureProgram = 3,
MeteringMode = 5,
Flash = 16,
WhiteBalanceMode = 1,
Artist = "Alberto",
Gps = new GpsPosition(45.4642035, 9.1899817, 122.5, null),
};
private static SidecarFrame Frame(string file, ImageFileFormat format, int width, int height) => new()
{
Id = Guid.NewGuid(),
File = "originals/" + file,
Original = file,
Sha256 = new string('a', 64),
CapturedAt = Captured,
UtcOffset = 120,
TimeSource = TimestampSource.ExifOriginal,
Exposure = 1 / 125.0,
FNumber = 5.6,
Iso = 200,
Focal = 24,
Bias = -1,
W = width,
H = height,
Format = format,
Flags = FrameFlags.Intervalometer,
};
/// <summary>A gradient with some texture, so every encoder has real work and errors show.</summary>
private static ImageBuffer Scene(int width, int height)
{
var image = new ImageBuffer(width, height);
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
image[0, x, y] = 0.05f + 0.6f * x / width;
image[1, x, y] = 0.1f + 0.5f * y / height;
image[2, x, y] = 0.2f + 0.2f * ((x / 8 + y / 8) % 2);
}
}
return image;
}
private string Source(int width = 96, int height = 64, int orientation = 1)
{
var path = Path.Combine(context.CreateTempDirectory("source"), "IMG_0001.JPG");
using var image = Scene(width, height);
var exif = ExifWriter.BuildJpegApp1(Camera with { Orientation = orientation });
File.WriteAllBytes(path, JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 97, Subsampling = JpegSubsampling.Yuv444, App1Segments = [exif[4..]] }));
return path;
}
private string Target(ConvertOptions options) => Path.Combine(context.CreateTempDirectory("staged"), "converted" + options.Extension);
[Theory]
[Case(ConvertTarget.Jpeg)]
[Case(ConvertTarget.Tiff16)]
[Case(ConvertTarget.LinearDng)]
public void TheSidecarsCaptureFactsAndTheCamerasMetadataRoundTrip(ConvertTarget target)
{
var options = new ConvertOptions(target);
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
Assert.Equal((options.Format, 96, 64), (file.Format, file.Width, file.Height));
Assert.Equal(new FileInfo(file.Path).Length, file.Size);
Assert.Equal(file.Sha256, ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult());
var read = MetadataReader.Read(file.Path);
Assert.Equal(options.Format, read.Format);
// The sidecar wins: the capture time to the tick, its offset, exposure, aperture, ISO, focal length and bias.
Assert.Equal(Captured, read.CapturedAt);
Assert.Equal(120, read.UtcOffsetMinutes);
Assert.Equal(TimestampSource.ExifOriginal, read.TimestampSource);
Assert.Approximately(1 / 125.0, read.ExposureTime!.Value, 1e-12);
Assert.Approximately(5.6, read.FNumber!.Value, 1e-9);
Assert.Equal(200, read.Iso);
Assert.Approximately(24, read.FocalLength!.Value, 1e-9);
Assert.Approximately(-1, read.ExposureBias!.Value, 1e-4);
// The original's own: camera, serial, lens, 35 mm focal length, modes, GPS, artist.
Assert.Equal(("Canon", "Canon EOS R5", "032021001234", "RF15-35mm F2.8 L IS USM"), (read.Make, read.Model, read.BodySerial, read.LensModel));
Assert.Approximately(15, read.FocalLength35!.Value, 1e-9);
Assert.Equal((3, 5, 16, 1), (read.ExposureProgram, read.MeteringMode, read.Flash, read.WhiteBalanceMode));
var gps = Assert.NotNull(read.Gps);
Assert.Approximately(45.4642035, gps.Latitude, 1e-6);
Assert.Approximately(9.1899817, gps.Longitude, 1e-6);
Assert.Equal(1, read.Orientation);
Assert.Equal((96, 64), read.OrientedSize);
}
[Theory]
[Case(ConvertTarget.Jpeg)]
[Case(ConvertTarget.Tiff16)]
[Case(ConvertTarget.LinearDng)]
public void TheConvertedFileDecodesAtFullResolutionWithTheServersDecoders(ConvertTarget target)
{
var options = new ConvertOptions(target);
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var server = DecoderFactory.CreateServer();
Assert.Equal(DecodeTier.Native, server.Probe(file.Path, file.Format).Tier);
using var original = server.Decode(new DecodeRequest(source, AllowPreview: false))!;
using var converted = Assert.NotNull(server.Decode(new DecodeRequest(file.Path, AllowPreview: false)));
Assert.Equal(DecodeTier.Native, converted.Tier);
Assert.Equal((96, 64), (converted.Pixels.Width, converted.Pixels.Height));
// The same picture: TIFF-16 and DNG keep it to 16 bits, the JPEG within its quantisation.
var tolerance = target == ConvertTarget.Jpeg ? 0.03 : 0.01;
var error = 0.0;
for (var c = 0; c < 3; c++)
{
var a = original.Pixels.Plane(c);
var b = converted.Pixels.Plane(c);
for (var i = 0; i < a.Length; i++) error += Math.Abs(a[i] - b[i]);
}
Assert.Less(error / (3.0 * 96 * 64), tolerance, $"mean error of {target}");
}
[Test]
public void TheJpegQualityAndFullChromaAreTheOnesAsked()
{
var source = Source(160, 120);
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 160, 120);
var decoder = DecoderFactory.CreateServer();
var high = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Jpeg, 95), decoder,
Target(new ConvertOptions(ConvertTarget.Jpeg)), cancellationToken: context.Cancellation);
var low = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Jpeg, 20), decoder,
Target(new ConvertOptions(ConvertTarget.Jpeg)), cancellationToken: context.Cancellation);
Assert.Less(low.Size, high.Size);
var info = JpegDecoder.ReadInfo(File.ReadAllBytes(high.Path));
Assert.Equal("4:4:4", info.SamplingNotation);
Assert.Equal(ConvertOptions.DefaultQuality, new ConvertOptions(ConvertTarget.Jpeg).Quality);
}
[Test]
public void TiffAndLinearDngAreSixteenBitsAndReadByTheInHouseReaders()
{
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var decoder = DecoderFactory.CreateServer();
var tiff = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.Tiff16), decoder,
Target(new ConvertOptions(ConvertTarget.Tiff16)), cancellationToken: context.Cancellation);
var dng = FrameConverter.Convert(new ConvertSource(source, frame), new ConvertOptions(ConvertTarget.LinearDng), decoder,
Target(new ConvertOptions(ConvertTarget.LinearDng)), cancellationToken: context.Cancellation);
Assert.Equal(16, MetadataReader.Read(tiff.Path).BitsPerSample);
using (var back = TiffImageReader.Decode(File.ReadAllBytes(tiff.Path)))
Assert.Equal((96, 64, 3), (back.Width, back.Height, back.Channels));
Assert.Equal(ImageFileFormat.Dng, MetadataReader.Read(dng.Path).Format);
using (var back = TiffImageReader.Decode(File.ReadAllBytes(dng.Path)))
Assert.Equal((96, 64, 3), (back.Width, back.Height, back.Channels));
}
[Test]
public void APortraitOriginalIsConvertedUpright()
{
var source = Source(96, 64, orientation: 6);
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64) with { Orientation = 6 };
var options = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
Assert.Equal((64, 96), (file.Width, file.Height));
var read = MetadataReader.Read(file.Path);
Assert.Equal((1, 64, 96), (read.Orientation, read.Width, read.Height));
}
[Test]
public void ACameraAndLensTheFileLacksComeFromTheSequence()
{
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.png");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, Chronos.Core.Codecs.Png.PngEncoder.Encode(image));
var frame = Frame("frame.png", ImageFileFormat.Png, 32, 24);
var options = new ConvertOptions(ConvertTarget.Tiff16);
var file = FrameConverter.Convert(new ConvertSource(path, frame, new SidecarCamera { Make = "Sony", Model = "ILCE-7RM5", Serial = "77" }, "FE 24mm F1.4 GM"),
options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var read = MetadataReader.Read(file.Path);
Assert.Equal(("Sony", "ILCE-7RM5", "77", "FE 24mm F1.4 GM"), (read.Make, read.Model, read.BodySerial, read.LensModel));
Assert.Equal(Captured, read.CapturedAt);
}
[Test]
public void APhotoReadOnlyFromItsPreviewIsNotConverted()
{
// A DNG the managed decoder reads natively, behind a chain that offers only a preview.
var path = Path.Combine(context.CreateTempDirectory("source"), "frame.dng");
using (var image = Scene(32, 24)) File.WriteAllBytes(path, LinearDngWriter.Write(image));
var frame = Frame("frame.dng", ImageFileFormat.Dng, 32, 24);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var target = Target(options);
Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([new PreviewOnlyProvider()]), target, cancellationToken: context.Cancellation));
Assert.FileMissing(target);
Assert.Throws<ConversionException>(() =>
FrameConverter.Convert(new ConvertSource(path, frame), options, new ImageDecoder([]), target, cancellationToken: context.Cancellation));
}
[Test]
public void AFileThatDoesNotReadBackIsRefused()
{
var source = Source();
var frame = Frame("IMG_0001.JPG", ImageFileFormat.Jpeg, 96, 64);
var options = new ConvertOptions(ConvertTarget.Jpeg);
var file = FrameConverter.Convert(new ConvertSource(source, frame), options, DecoderFactory.CreateServer(), Target(options), cancellationToken: context.Cancellation);
var written = FrameConverter.Metadata(MetadataReader.Read(source), new ConvertSource(source, frame));
Assert.DoesNotThrow(() => FrameConverter.Verify(file, written));
// A bit flipped on the way to the disk.
var bytes = File.ReadAllBytes(file.Path);
bytes[^10] ^= 0x40;
File.WriteAllBytes(file.Path, bytes);
Assert.Throws<ConversionException>(() => FrameConverter.Verify(file, written));
// A capture time that is not the one written.
File.WriteAllBytes(file.Path, File.ReadAllBytes(source));
Assert.Throws<ConversionException>(() => FrameConverter.Verify(file with { Sha256 = ContentHash.Sha256Async(file.Path).GetAwaiter().GetResult(), Size = new FileInfo(file.Path).Length }, written));
}
[Test]
public void ConvertedNamesAreTheNamesFilingGives()
{
var dng = new ConvertOptions(ConvertTarget.LinearDng);
Assert.Equal("originals/IMG_0001.dng", dng.WithExtension("originals/IMG_0001.CR2"));
Assert.Equal("frame.jpg", new ConvertOptions(ConvertTarget.Jpeg).WithExtension("frame"));
// The second frame of a sequence filed by a Canon, named with the sequence's camera, its number, the title and the
// name it had on the card, as an upload or a Reorganize names it; {ext} is the target's.
var first = Frame("2026-09-28_0001.CR3", ImageFileFormat.Cr3, 96, 64) with { I = 0, Original = "IMG_0001.CR3" };
var second = Frame("2026-09-28_0002.CR3", ImageFileFormat.Cr3, 96, 64) with { I = 1, Original = "IMG_0002.CR3", CapturedAt = Captured.AddSeconds(10) };
var sequence = new SequenceManifest
{
Id = Guid.NewGuid(),
Title = "Harbour",
Camera = new SidecarCamera { Make = "Canon", Model = "Canon EOS R5" },
Frames = [first, second],
};
Assert.Equal("originals/Harbour_Canon EOS R5_0002_IMG_0002.dng",
dng.FileName(PathTemplate.Parse("{title}_{camera}_{n}_{name}.{ext}"), second, sequence));
Assert.Equal("originals/2026-09-28_0002.tif",
new ConvertOptions(ConvertTarget.Tiff16).FileName(PathTemplate.Parse("{date}_{n}"), second, sequence));
Assert.Equal(PathTemplate.Parse("{date}_{n}").ExpandFile(FrameNaming.Context(second with { File = "originals/x.dng" }, sequence, "Harbour")),
Path.GetFileName(dng.FileName(PathTemplate.Parse("{date}_{n}"), second, sequence)));
// A frame adopted in place stays in its folder; a long name keeps the target's extension whole.
var adopted = second with { File = "IMG_0002.CR3", Original = new string('x', 300) + ".CR3" };
var named = dng.FileName(PathTemplate.Parse("{name}.{ext}"), adopted, sequence);
Assert.False(named.Contains('/'));
Assert.True(named.EndsWith(".dng", StringComparison.Ordinal) && named.Length <= PathTemplate.MaxSegmentLength, named);
Assert.Equal(ConvertTarget.LinearDng, ConvertOptions.ArchivalTarget);
Assert.False(new ConvertOptions(ConvertTarget.Jpeg, 0).IsValid);
Assert.False(new ConvertOptions(ConvertTarget.Jpeg, 101).IsValid);
Assert.Equal(new ConvertOptions(ConvertTarget.LinearDng, 10).Key, new ConvertOptions(ConvertTarget.LinearDng, 90).Key);
}
/// <summary>A provider that claims DNG natively but only ever gives the embedded-preview tier.</summary>
private sealed class PreviewOnlyProvider : IImageDecoderProvider
{
private readonly ManagedDecoderProvider _managed = new();
public string Name => "preview-only";
public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Dng ? DecodeTier.Native : null;
public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default)
{
var decoded = _managed.Decode(request with { AllowPreview = true }, cancellationToken);
if (decoded is null) return null;
var copy = decoded.Pixels.Clone();
decoded.Dispose();
return new DecodedImage(copy, DecodeTier.EmbeddedPreview, Name, copy.Width, copy.Height);
}
}
}