using Titano.Core.Archiving; using Titano.Core.Codecs.Jpeg; using Titano.Core.Imaging; using Titano.Core.Imaging.Decoders; using Titano.Core.Metadata; using Titano.Core.Model; using Titano.Data.Archiving; using Titano.Testing; using static Titano.Data.Tests.Archiving.ArchiveFixture; namespace Titano.Data.Tests.Archiving; /// Proxies of originals a host cannot decode (BRIEF §2.3, §8.5): sent by a client or render node, checked, kept, thumbnailed. [Category("archive")] public sealed class ProxyTests(TestContext context) { [Test] public void OnlyFormatsAHostCannotDecodeTakeTheirPixelsFromProxies() { var server = DecoderFactory.CreateServer(); Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.Heif), "HEIF previews are thumbnails at best"); Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.Avif)); Assert.True(ArchiveImages.NeedsProxy(server, ImageFileFormat.WebP), "no decoder at all"); Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Cr3), "a RAW's embedded full-size preview"); Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Dng)); Assert.False(ArchiveImages.NeedsProxy(new ImageDecoder([]), ImageFileFormat.Jpeg), "proxies are JPEGs"); Assert.False(ArchiveImages.NeedsProxy(server, ImageFileFormat.Unknown)); // A desktop whose decoders read HEIF makes the proxies a server asks for. Assert.False(ArchiveImages.NeedsProxy(new ImageDecoder([new NativeHeif()]), ImageFileFormat.Heif)); } [Test] public async Task AProxySentForAFrameThisHostCannotDecodeIsCheckedKeptAndThumbnailed() { await using var fx = await CreateAsync(context); var (sequence, frames) = await HeifSequenceAsync(fx); var folder = fx.Paths.Sequence(sequence.Folder); var images = fx.Service.Images; Assert.Equal(ProxyStoreResult.Invalid, images.StoreProxy(fx.Archive, sequence, frames[0], "not a photo"u8.ToArray())); Assert.Equal(ProxyStoreResult.WrongFrame, images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[1].CapturedAt!.Value, 1))); Assert.FileMissing(folder.Proxy(0)); var proxy = Proxy(frames[0].CapturedAt!.Value, 2); Assert.Equal(ProxyStoreResult.Stored, images.StoreProxy(fx.Archive, sequence, frames[0], proxy)); Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct)); // The proxy pass keeps what was sent and makes none from the originals here, even when forced. var proxies = await images.BuildProxiesAsync(fx.Archive, sequence, force: true, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); Assert.Equal((0, 1, 1), (proxies.Built, proxies.Reused, proxies.Undecodable)); Assert.SequenceEqual(proxy, await File.ReadAllBytesAsync(folder.Proxy(0), fx.Ct)); Assert.FileMissing(folder.Proxy(1)); // The thumbnail comes from the proxy; the frame without one has none yet. var thumbs = await images.BuildThumbnailsAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); Assert.Equal((1, 0, 1), (thumbs.Built, thumbs.Reused, thumbs.Undecodable)); using (var pack = ThumbPackReader.Open(folder.ThumbsPackPath)) { var entry = pack.Entries[0]; Assert.Equal((FrameImageRenderer.ThumbnailLongEdge, 240), (entry.Width, entry.Height)); } var again = await images.BuildThumbnailsAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); Assert.Equal((0, 1, 1), (again.Built, again.Reused, again.Undecodable)); } [Test] public async Task AProxyLeftByARenumberedFrameIsAskedForAgain() { await using var fx = await CreateAsync(context); var (sequence, frames) = await HeifSequenceAsync(fx); var folder = fx.Paths.Sequence(sequence.Folder); Directory.CreateDirectory(folder.Proxies); await File.WriteAllBytesAsync(folder.Proxy(1), Proxy(frames[0].CapturedAt!.Value, 3), fx.Ct); var proxies = await fx.Service.Images.BuildProxiesAsync(fx.Archive, sequence, force: false, 0, (_, _) => ValueTask.CompletedTask, fx.Ct); Assert.Equal(2, proxies.Undecodable); Assert.FileMissing(folder.Proxy(1)); Assert.False((await fx.Db.Sequences.GetAsync(sequence.Id, fx.Ct))!.Status.HasFlag(SequenceStatus.ProxiesBuilt)); } [Test] public async Task AnArchiveWithoutProxiesRefusesThem() { await using var fx = await CreateAsync(context); var (sequence, frames) = await HeifSequenceAsync(fx); var manifest = ManifestStore.ReadArchive(fx.Paths.ManifestPath); ManifestStore.WriteArchive(fx.Paths.ManifestPath, manifest with { ProxyPolicy = new ProxyPolicy { LongEdge = ProxyPolicy.Disabled } }); Assert.Equal(ProxyStoreResult.Disabled, fx.Service.Images.StoreProxy(fx.Archive, sequence, frames[0], Proxy(frames[0].CapturedAt!.Value, 4))); Assert.FileMissing(fx.Paths.Sequence(sequence.Folder).Proxy(0)); } /// Two ingested frames whose originals the archive engine takes for HEIF (its decoders read none). private static async Task<(Sequence Sequence, IReadOnlyList Frames)> HeifSequenceAsync(ArchiveFixture fx) { Guid id = default; for (var i = 0; i < 2; i++) { var staged = fx.Stage(Start.AddSeconds(10 * i), 40 + i); id = (await fx.Service.Ingest.CommitAsync(new IngestRequest(fx.Archive.Id, null, null, "heif", staged.Path, $"IMG_{i:0000}.HEIC", staged.Size, staged.Sha256, null, null), fx.Ct)).SequenceId; } var sequence = (await fx.Db.Sequences.GetAsync(id, fx.Ct))!; var folder = fx.Paths.Sequence(sequence.Folder); var sidecar = ManifestStore.ReadSequence(folder.ManifestPath); ManifestStore.WriteSequence(folder.ManifestPath, sidecar with { Frames = [.. sidecar.Frames.Select(f => f with { Format = ImageFileFormat.Heif })] }); return (sequence, await fx.Db.Frames.ListBySequenceAsync(id)); } /// A 320 × 240 JPEG proxy with in its Exif, as a client makes it. private static byte[] Proxy(DateTime captured, int seed) { using var image = new ImageBuffer(320, 240); var level = 0.2f + seed % 5 * 0.1f; for (var c = 0; c < 3; c++) image.Plane(c).Fill(level + c * 0.05f); var exif = ExifWriter.BuildJpegApp1(new FrameMetadata { CapturedAt = captured, Software = "Titano" }); return JpegEncoder.Encode(image, new JpegEncodeOptions { Quality = 85, App1Segments = [exif[4..]] }); } /// A provider that decodes HEIF natively (WIC with the HEIF extension on a desktop). private sealed class NativeHeif : IImageDecoderProvider { public string Name => "native-heif"; public DecodeTier? TierFor(ImageFileFormat format) => format == ImageFileFormat.Heif ? DecodeTier.Native : null; public DecodedImage? Decode(DecodeRequest request, CancellationToken cancellationToken = default) => null; } }