using Titano.Core.Imaging; using Titano.Core.Metadata; using Titano.Core.Model; namespace Titano.Core.Archiving; /// Edits of the frame list of a ; frames stay in capture order with gap-free indexes. public static class SequenceFrames { /// Capture time (unknown last), then file name. public static readonly IComparer CaptureOrder = Comparer.Create((a, b) => { var byTime = (a.CapturedAt ?? DateTime.MaxValue).CompareTo(b.CapturedAt ?? DateTime.MaxValue); return byTime != 0 ? byTime : string.CompareOrdinal(a.File, b.File); }); /// A frame record from light metadata (with when the MakerNote says so). /// Path relative to the sequence folder. /// Used when the file records no capture time of its own (only a file-system date). public static SidecarFrame FromMetadata(Guid id, string file, string? original, long size, string? sha256, FrameMetadata metadata, DateTime? fallbackCapture = null) { var useFallback = fallbackCapture is not null && metadata.TimestampSource is TimestampSource.FileSystem or TimestampSource.Unknown; return new SidecarFrame { Id = id, File = file, Original = original, Size = size, Sha256 = sha256?.ToLowerInvariant(), CapturedAt = useFallback ? DateTime.SpecifyKind(fallbackCapture!.Value, DateTimeKind.Unspecified) : metadata.CapturedAt, UtcOffset = useFallback ? null : metadata.UtcOffsetMinutes, TimeSource = useFallback ? TimestampSource.Unknown : metadata.TimestampSource, Exposure = metadata.ExposureTime, FNumber = metadata.FNumber, Iso = metadata.Iso, Focal = metadata.FocalLength, Bias = metadata.ExposureBias, Wb = metadata.WhiteBalanceMode, Orientation = metadata.Orientation, W = metadata.Width, H = metadata.Height, Format = metadata.Format, PreviewOffset = metadata.LargestPreview?.Offset, PreviewLength = metadata.LargestPreview?.Length, PreviewW = Positive(metadata.LargestPreview?.Width), PreviewH = Positive(metadata.LargestPreview?.Height), Flags = metadata.IntervalShooting == true ? FrameFlags.Intervalometer : FrameFlags.None, }; } /// /// The frame with its decode tier (BRIEF ยง8.2) and the flags that follow it: for /// the embedded-preview tier, for none. 's size, oriented /// like every , replaces the stored preview size when it is a preview's. /// /// For the unavailable tier: whether that proves the file unreadable. False when the host has no /// native decoder for the format, so a client or render node with one (or a proxy) may still produce its pixels: the /// tier is kept and is left as it was. public static SidecarFrame WithTier(SidecarFrame frame, FileTier tier, bool damaged = true) { var flags = frame.Flags & ~(FrameFlags.PreviewOnly | FrameFlags.Undecodable); if (tier.Tier == DecodeTier.EmbeddedPreview) flags |= FrameFlags.PreviewOnly; else if (tier.Tier == DecodeTier.Unavailable) flags |= damaged ? FrameFlags.Undecodable : frame.Flags & FrameFlags.Undecodable; var (w, h) = tier.Tier == DecodeTier.EmbeddedPreview && tier.Width > 0 && tier.Height > 0 ? PixelConvert.OrientedSize(tier.Width, tier.Height, frame.Orientation) : (frame.PreviewW ?? 0, frame.PreviewH ?? 0); return frame with { Tier = tier.Tier, Flags = flags, PreviewW = Positive(w), PreviewH = Positive(h) }; } private static int? Positive(int? value) => value > 0 ? value : null; /// /// Merges into the frame list by capture time (existing frames keep their relative order), /// renumbers indexes and bumps the frames version. /// public static SequenceManifest Insert(SequenceManifest manifest, IReadOnlyList added) { if (added.Count == 0) return manifest; var incoming = added.Order(CaptureOrder).ToList(); var merged = new List(manifest.Frames.Count + incoming.Count); var next = 0; foreach (var frame in manifest.Frames) { while (next < incoming.Count && CaptureOrder.Compare(incoming[next], frame) < 0) merged.Add(incoming[next++]); merged.Add(frame); } while (next < incoming.Count) merged.Add(incoming[next++]); return WithFrames(manifest, merged); } /// Replaces the frame list (indexes renumbered) and bumps the frames version. public static SequenceManifest WithFrames(SequenceManifest manifest, IReadOnlyList frames) { var renumbered = new SidecarFrame[frames.Count]; for (var i = 0; i < frames.Count; i++) renumbered[i] = frames[i].I == i ? frames[i] : frames[i] with { I = i }; return manifest with { Frames = renumbered, Versions = manifest.Versions with { Frames = manifest.Versions.Frames + 1 }, UpdatedAt = DateTimeOffset.UtcNow, }; } /// Applies per-frame changes by id (flags, hashes); frames not in are kept. public static SequenceManifest Update(SequenceManifest manifest, IReadOnlyDictionary> changes) { var frames = manifest.Frames.Select(f => changes.TryGetValue(f.Id, out var change) ? change(f) : f).ToList(); return manifest with { Frames = frames, UpdatedAt = DateTimeOffset.UtcNow }; } }