diff --git a/src/Titano.Core/Project/FramePhotos.cs b/src/Titano.Core/Project/FramePhotos.cs index 880b2169..7eca054d 100644 --- a/src/Titano.Core/Project/FramePhotos.cs +++ b/src/Titano.Core/Project/FramePhotos.cs @@ -26,12 +26,13 @@ public sealed record FramePhoto(string? Sha256, long Size, DateTime? CapturedAt) /// /// The photos of one copy's frames by frame index (positions in capture order, BRIEF §5), and where a photo is among -/// them. Immutable once built, so parallel workers may share it. +/// them. A copy may hold one photo more than once (a folder adopted with 'IMG_0001.JPG' and 'IMG_0001 (1).JPG'); those +/// frames are told apart by their order (). Immutable once built, so parallel workers may share it. /// public sealed class FramePhotos { private readonly FramePhoto?[] _byIndex; - private readonly Dictionary _bySha = new(StringComparer.Ordinal); + private readonly Dictionary> _bySha = new(StringComparer.Ordinal); private readonly Dictionary<(long Size, long Time), List> _bySizeAndTime = []; /// Each frame's index and photo, in any order. @@ -44,7 +45,12 @@ public sealed class FramePhotos for (var i = 0; i < _byIndex.Length; i++) { if (_byIndex[i] is not { } photo) continue; - if (photo.IsHashed) _bySha.TryAdd(photo.Sha256!.ToLowerInvariant(), i); + if (photo.IsHashed) + { + var sha = photo.Sha256!.ToLowerInvariant(); + if (!_bySha.TryGetValue(sha, out var copies)) _bySha[sha] = copies = []; + copies.Add(i); + } if (photo.CapturedAt is not { } time) continue; var key = (photo.Size, FramePhoto.Millisecond(time)); if (!_bySizeAndTime.TryGetValue(key, out var same)) _bySizeAndTime[key] = same = []; @@ -60,17 +66,38 @@ public sealed class FramePhotos /// Some frame is not hashed yet: matching it needs capture times. public bool HasUnhashed => _byIndex.Any(p => p is { IsHashed: false }); - /// The index of the first frame holding (); null when none does. - public int? IndexOf(FramePhoto photo) + /// + /// The index of the first frame holding (), or with + /// n of the (n+1)-th in index order; null when fewer frames hold it. + /// + public int? IndexOf(FramePhoto photo, int occurrence = 0) { ArgumentNullException.ThrowIfNull(photo); - if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) return hashed; - if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return null; + ArgumentOutOfRangeException.ThrowIfNegative(occurrence); + var holding = Holding(photo); + return occurrence < holding.Count ? holding[occurrence] : null; + } + + /// + /// Which of the frames holding the photo at it is, in index order: 0 for the first (and for + /// a photo held once), 1 for the second, … A position translated to another copy names the same one there. + /// + public int Occurrence(int index) => this[index] is { } photo ? Math.Max(Holding(photo).BinarySearch(index), 0) : 0; + + /// The indices of the frames holding , ascending. + private List Holding(FramePhoto photo) + { + var holding = new List(1); + if (photo.IsHashed && _bySha.TryGetValue(photo.Sha256!.ToLowerInvariant(), out var hashed)) holding.AddRange(hashed); + if (photo.CapturedAt is not { } time || !_bySizeAndTime.TryGetValue((photo.Size, FramePhoto.Millisecond(time)), out var candidates)) return holding; + // Frames not hashed on one side match by size and capture time; a hashed photo's hashed frames are listed already. foreach (var index in candidates) { - if (_byIndex[index]!.Matches(photo)) return index; + var frame = _byIndex[index]!; + if (!(photo.IsHashed && frame.IsHashed) && frame.Matches(photo)) holding.Add(index); } - return null; + if (holding.Count > 1) holding.Sort(); + return holding; } /// Both lists hold the same photos at the same indices: a position names the same photo on both. diff --git a/src/Titano.Core/Project/ProjectSettings.cs b/src/Titano.Core/Project/ProjectSettings.cs index 6e5402e2..1494e670 100644 --- a/src/Titano.Core/Project/ProjectSettings.cs +++ b/src/Titano.Core/Project/ProjectSettings.cs @@ -59,6 +59,13 @@ public sealed record PendingFrame public long Size { get; init; } public DateTime? CapturedAt { get; init; } + /// + /// Which of the frames holding the photo it names, in capture order, when a copy holds the photo more than once + /// (): 0, the first, is not written. + /// + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public int Occurrence { get; init; } + /// The photo is excluded from renders. public bool Excluded { get; init; } @@ -347,7 +354,9 @@ public sealed record ProjectSettings /// Translates the frame positions of a project written against the frames of another copy of the sequence /// (; a server sequence and a PC's copy hold the same photos, though not always all of them, /// BRIEF §14.5) to the frames of , by photo: each exclusion, the hero and each camera keyframe - /// moves to the frame of holding its photo. One whose photo lacks becomes + /// moves to the frame of holding its photo, and a position on the n-th frame holding a photo + /// holds more than once to the n-th such frame (), so + /// duplicates stay apart. One whose photo (or that copy of it) lacks becomes /// a , and pending frames whose photo holds become positions again; a /// pending hero only while no other hero is chosen (a hero chosen since one was left pending is newer, so that one no /// longer waits even when the chosen one must wait in turn), a pending keyframe only where no keyframe is. With @@ -370,7 +379,7 @@ public sealed record ProjectSettings foreach (var node in stored) { // An entry naming no photo is not this Titano's to place: it stays as it is. - if (node is JsonObject entry && PhotoOf(entry) is { } photo) pending.Add(new Parked(photo, entry.DeepClone().AsObject())); + if (node is JsonObject entry && PhotoOf(entry) is { } photo) pending.Add(new Parked(photo, OccurrenceOf(entry), entry.DeepClone().AsObject())); else unreadable.Add(node?.DeepClone()); } } @@ -409,14 +418,14 @@ public sealed record ProjectSettings { if (!IsPosition(node, out var at)) kept.Add(node?.DeepClone()); else if (from[at] is not { } photo) changed = true; - else if (to.IndexOf(photo) is { } now) + else if (to.IndexOf(photo, from.Occurrence(at)) is { } now) { kept.Add(JsonValue.Create(now)); changed |= now != at; } else { - Park(pending, photo).Entry["excluded"] = true; + Park(pending, photo, from.Occurrence(at)).Entry["excluded"] = true; changed = true; } } @@ -425,8 +434,8 @@ public sealed record ProjectSettings if (project["heroFrame"] is JsonValue hero && hero.TryGetValue(out var heroAt)) { var photo = from[heroAt]; - var now = photo is null ? null : to.IndexOf(photo); - if (photo is not null && now is null) Park(pending, photo).Entry["hero"] = true; + var now = photo is null ? null : to.IndexOf(photo, from.Occurrence(heroAt)); + if (photo is not null && now is null) Park(pending, photo, from.Occurrence(heroAt)).Entry["hero"] = true; if (now != heroAt) { project["heroFrame"] = now; @@ -448,7 +457,7 @@ public sealed record ProjectSettings { changed = true; } - else if (to.IndexOf(photo) is { } now) + else if (to.IndexOf(photo, from.Occurrence(at)) is { } now) { copy["frame"] = now; kept.Add(copy); @@ -456,7 +465,7 @@ public sealed record ProjectSettings } else { - var parked = Park(pending, photo).Entry; + var parked = Park(pending, photo, from.Occurrence(at)).Entry; if (parked["keyframes"] is not JsonArray keys) parked["keyframes"] = keys = new JsonArray(); keys.Add(copy); changed = true; @@ -473,7 +482,7 @@ public sealed record ProjectSettings var changed = false; foreach (var parked in pending) { - if (to.IndexOf(parked.Photo) is not { } at) continue; + if (to.IndexOf(parked.Photo, parked.Occurrence) is not { } at) continue; parked.Placed = changed = true; if (Flag(parked.Entry, "excluded")) { @@ -523,18 +532,19 @@ public sealed record ProjectSettings } } - /// The pending entry of , added when there is none. - private static Parked Park(List pending, FramePhoto photo) + /// The pending entry of (the copy of it names), added when there is none. + private static Parked Park(List pending, FramePhoto photo, int occurrence) { - if (pending.FirstOrDefault(p => p.Photo.Matches(photo)) is { } known) return known; + if (pending.FirstOrDefault(p => p.Occurrence == occurrence && p.Photo.Matches(photo)) is { } known) return known; var entry = new JsonObject(); if (photo.IsHashed) entry["sha256"] = photo.Sha256!.ToLowerInvariant(); entry["size"] = photo.Size; if (photo.CapturedAt is { } captured) entry["capturedAt"] = captured; + if (occurrence > 0) entry["occurrence"] = occurrence; entry["excluded"] = false; entry["hero"] = false; entry["keyframes"] = new JsonArray(); - var parked = new Parked(photo, entry); + var parked = new Parked(photo, occurrence, entry); pending.Add(parked); return parked; } @@ -548,6 +558,9 @@ public sealed record ProjectSettings return string.IsNullOrEmpty(sha) && (captured is null || size < 0) ? null : new FramePhoto(sha, size, captured); } + /// Which copy of its photo a pending entry names; the first when it says nothing (or nothing readable). + private static int OccurrenceOf(JsonObject entry) => entry["occurrence"] is JsonValue o && o.TryGetValue(out var occurrence) && occurrence > 0 ? occurrence : 0; + private static bool Flag(JsonObject entry, string name) => entry[name] is JsonValue value && value.TryGetValue(out var set) && set; /// A pending entry still has a position to give its photo. @@ -566,10 +579,11 @@ public sealed record ProjectSettings foreach (var node in copy) array.Add(node); } - /// A pending entry being edited: its photo, its JSON, and whether its photo took its place. - private sealed class Parked(FramePhoto photo, JsonObject entry) + /// A pending entry being edited: its photo (and which copy of it), its JSON, and whether its photo took its place. + private sealed class Parked(FramePhoto photo, int occurrence, JsonObject entry) { public FramePhoto Photo { get; } = photo; + public int Occurrence { get; } = occurrence; public JsonObject Entry { get; } = entry; public bool Placed { get; set; } } diff --git a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs index 08943aa5..33e3c525 100644 --- a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs +++ b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs @@ -283,6 +283,69 @@ public sealed class ProjectSettingsTests Assert.Null(cleared.PendingFrames); } + [Test] + public void APhotoHeldTwiceKeepsEachCopysPositions() + { + // The server adopted a folder holding photo 1 twice ('IMG_0001.JPG', 'IMG_0001 (1).JPG'); this PC keeps it once. + var photos = Enumerable.Range(0, 3).Select(Photo).ToList(); + var server = Frames([photos[0], photos[1], photos[1], photos[2]]); + var here = Frames(photos); + var project = new ProjectSettings + { + ExcludedFrames = [2], + HeroFrame = 2, + Camera = new CameraSettings { Keyframes = [new CameraKey(1, 0.1, 0.5, 1, 0), new CameraKey(2, 0.2, 0.5, 1, 0)] }, + }; + + // The second copy, which the deep analysis flags as a duplicate, is excluded there: this PC still renders its only copy. + var taken = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(project.ToJson(), server, here))); + Assert.Empty(taken.ExcludedFrames); + Assert.Null(taken.HeroFrame); + Assert.SequenceEqual([1], taken.Camera.Keyframes.Select(k => k.Frame)); + Assert.Equal(0.1, taken.Camera.Keyframes[0].CenterX); + var waiting = Assert.NotNull(taken.PendingFrames); + Assert.Count(1, waiting); + Assert.True(waiting[0] is { Excluded: true, Hero: true, Occurrence: 1 } && waiting[0].Sha256 == photos[1].Sha256); + Assert.Count(1, waiting[0].Keyframes); + Assert.Equal(0.2, waiting[0].Keyframes[0].CenterX); + Assert.Contains("\"occurrence\": 1", taken.ToJson()); + + // Sent back after an edit of something else, every position lands on the copy it named. + var edited = taken with { Exposure = new ExposureSettings { ExposureOffsetEv = 0.3 } }; + var back = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(edited.ToJson(), here, server))); + Assert.SequenceEqual([2], back.ExcludedFrames); + Assert.Equal(2, back.HeroFrame); + Assert.SequenceEqual([1, 2], back.Camera.Keyframes.Select(k => k.Frame)); + Assert.SequenceEqual([0.1, 0.2], back.Camera.Keyframes.Select(k => k.CenterX)); + Assert.Null(back.PendingFrames); + + // Both copies excluded there: this PC excludes its copy, and the server gets both back. + var both = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(new ProjectSettings { ExcludedFrames = [1, 2] }.ToJson(), server, here))); + Assert.SequenceEqual([1], both.ExcludedFrames); + var excluded = Assert.NotNull(both.PendingFrames); + Assert.Count(1, excluded); + Assert.True(excluded[0] is { Excluded: true, Hero: false, Occurrence: 1 }); + var returned = ProjectSettings.FromJson(Assert.NotNull(ProjectSettings.TranslateFramePositions(both.ToJson(), here, server))); + Assert.SequenceEqual([1, 2], returned.ExcludedFrames); + Assert.Null(returned.PendingFrames); + } + + [Test] + public void TheCopiesOfAPhotoAreToldApartByTheirOrder() + { + var time = new DateTime(2026, 9, 28, 18, 0, 0); + var hashed = new FramePhoto(new string('a', 64), 100, time); + var other = new FramePhoto(new string('b', 64), 200, time); + // The copy at 3 has not been hashed yet: it is the same photo by size and capture time. + var frames = Frames([other, hashed, other, new FramePhoto(null, 100, time), hashed]); + + Assert.SequenceEqual([1, 3, 4], new[] { 0, 1, 2 }.Select(n => frames.IndexOf(hashed, n)).OfType()); + Assert.Null(frames.IndexOf(hashed, 3)); + Assert.SequenceEqual([0, 0, 1, 1, 2], Enumerable.Range(0, 5).Select(frames.Occurrence)); + Assert.Equal(0, frames.Occurrence(9)); + Assert.Equal(2, frames.IndexOf(new FramePhoto(new string('B', 64), 0, null), 1)); + } + [Test] public void AProjectNamingNoFrameCrossesAsItIs() {