using Titano.Core.Analysis;
using Titano.Core.Analysis.Deep;
using Titano.Core.Diagnostics.Synthetic;
using Titano.Core.Imaging;
namespace Titano.Core.Tests.Analysis.Deep;
///
/// over a . With > 0 the sequence is
/// rendered pixels larger on every side and each frame is cropped at a seeded integer offset
/// in ±jitter, which shakes the camera by a known amount.
///
internal sealed class SyntheticFrameSource(SyntheticSequence sequence, int jitter = 0, int seed = 11) : IFrameSource
{
private readonly (int X, int Y)[] _offsets = Offsets(sequence.Count, jitter, seed);
private int _loads;
public SyntheticSequence Sequence { get; } = sequence;
public int Width => Sequence.Options.Width - 2 * jitter;
public int Height => Sequence.Options.Height - 2 * jitter;
public int Count => Sequence.Count;
/// Total calls of .
public int Loads => Volatile.Read(ref _loads);
/// Frames that "fail to decode".
public HashSet Undecodable { get; init; } = [];
/// Called on every load before decoding (may throw to simulate a crash, or cancel a token).
public Action? OnLoad { get; set; }
/// Metadata without shutter time and aperture when true.
public bool StripExposure { get; init; }
public LightFrame Metadata(int index)
{
var frame = LightFrame.From(Sequence.Metadata[index], index) with { Width = Width, Height = Height };
return StripExposure ? frame with { ExposureTime = null, FNumber = null } : frame;
}
public ValueTask LoadAsync(int index, int maxLongEdge, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Interlocked.Increment(ref _loads);
OnLoad?.Invoke(index);
if (Undecodable.Contains(index)) return ValueTask.FromResult(null);
using var full = Sequence.Render(index);
var (dx, dy) = _offsets[index];
var image = new ImageBuffer(Width, Height, 3, clear: false);
for (var c = 0; c < 3; c++)
{
for (var y = 0; y < Height; y++)
full.Row(c, y + jitter + dy).Slice(jitter + dx, Width).CopyTo(image.Row(c, y));
}
if (int.Max(Width, Height) <= maxLongEdge) return ValueTask.FromResult(image);
using (image) return ValueTask.FromResult(AnalysisImage.Downscale(image, maxLongEdge));
}
private static (int, int)[] Offsets(int count, int jitter, int seed)
{
var random = new Random(seed);
var offsets = new (int, int)[count];
for (var i = 0; i < count; i++) offsets[i] = jitter > 0 ? (random.Next(-jitter, jitter + 1), random.Next(-jitter, jitter + 1)) : (0, 0);
return offsets;
}
}
/// In-memory checkpoint that records what was persisted.
internal sealed class MemoryCheckpoint : IDeepAnalysisCheckpoint
{
private string? _fingerprint;
public List Frames { get; } = [];
public ValueTask> LoadAsync(string fingerprint, CancellationToken cancellationToken)
{
if (_fingerprint != fingerprint) Frames.Clear();
_fingerprint = fingerprint;
return ValueTask.FromResult>([.. Frames]);
}
public ValueTask AppendAsync(IReadOnlyList frames, CancellationToken cancellationToken)
{
Frames.AddRange(frames);
return ValueTask.CompletedTask;
}
}
/// The synthetic scenes the deep-analysis and advisor tests share (small frames, fast).
internal static class DeepScenes
{
/// Options for the tests: small sources are analysed at native size, flow on every 5th pair, two workers.
public static DeepAnalysisOptions Options { get; } = new() { MinAnalysisLongEdge = 64, FlowEvery = 5, MaxDegreeOfParallelism = 2, RegionSampleFrames = 9 };
/// Night sky with stars over dark land and random flicker.
public static SyntheticSequence NightSky(int frames = 90, int jitter = 0) => SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 160 + 2 * jitter,
Height = 112 + 2 * jitter,
FrameCount = frames,
IntervalSeconds = 20,
BaseExposureTime = 15,
BaseFNumber = 2.8,
BaseIso = 3200,
StarField = true,
StarCount = 160,
FlickerEv = 0.12,
Start = new DateTime(2024, 8, 12, 23, 0, 0),
});
/// Sunset: the scene darkens 6 EV while the camera opens up 1 EV every 40 frames.
public static SyntheticSequence HolyGrail(int frames = 240) => SyntheticSequence.Create(new SyntheticSequenceOptions
{
Width = 160,
Height = 112,
FrameCount = frames,
IntervalSeconds = 15,
BaseExposureTime = 1.0 / 250,
StartEv = 0.5,
EndEv = -5.5,
FlickerEv = 0.03,
ExposureSteps = [new(40, -1), new(80, -1), new(120, -1), new(160, -1), new(200, -1)],
});
}