using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; namespace Titano.Core.Project; public enum TimingMode { /// Every source frame lasts one output frame. Constant, /// Output time follows capture time (deliberate intervalometer pauses are kept short, gaps preserved). Adaptive, /// Missing frames in sparse gaps are synthesised from motion. Interpolated, /// A monotone curve maps output time to source position (speed ramps). Remapped, } public enum RegionMode { Off, Auto, Horizon, Luminance, } public enum FlowQualitySetting { Draft, Balanced, Best, } public enum StackSetting { None, Median, Mean, StarTrails, } /// A control point of the time-remap curve: output fraction → source fraction (both 0..1). public sealed record CurvePoint(double X, double Y); /// Virtual-camera keyframe in normalised coordinates. public sealed record CameraKey(int Frame, double CenterX, double CenterY, double Zoom, double Rotation, string Easing = "EaseInOut"); public sealed record TimingSettings { public Auto Mode { get; init; } = new(); /// Output frame rate as "num/den" or an integer (e.g. "24000/1001", "30"). public Auto FrameRate { get; init; } = new(); /// When set, the plan is scaled so the video lasts this many seconds. public double? FitToSeconds { get; init; } public IReadOnlyList RemapCurve { get; init; } = [new(0, 0), new(1, 1)]; } public sealed record ExposureSettings { public Auto Deflicker { get; init; } = new(); /// Local-regression window in frames (odd). public Auto DeflickerWindow { get; init; } = new(); /// 0..1: share of the flicker removed. public Auto DeflickerStrength { get; init; } = new(); public Auto Regions { get; init; } = new(); public Auto HolyGrail { get; init; } = new(); /// Transition length in frames for redistributed exposure steps. public Auto TransitionLength { get; init; } = new(); public Auto WhiteBalanceSmoothing { get; init; } = new(); /// Global exposure offset in EV applied after correction. public double ExposureOffsetEv { get; init; } /// Soft highlight knee (linear), null = hard clip at the encoder. public double? HighlightKnee { get; init; } = 0.9; } public sealed record MotionSettings { public Auto MotionBlur { get; init; } = new(); /// Target shutter angle in degrees (180 = film look). public Auto TargetShutterAngle { get; init; } = new(); public Auto FlowQuality { get; init; } = new(); public Auto Stabilize { get; init; } = new(); /// Path smoothing in frames (Gaussian sigma). public Auto StabilizeSmoothing { get; init; } = new(); } public sealed record CameraSettings { /// Normalised crop (0..1) or null for the full frame. public double[]? Crop { get; init; } /// "source", "16:9", "21:9", "4:3", "3:2", "1:1", "9:16", "4:5" or "w:h". public Auto Aspect { get; init; } = new(); public double OffsetX { get; init; } public double OffsetY { get; init; } public IReadOnlyList Keyframes { get; init; } = []; } public sealed record StackSettings { public StackSetting Mode { get; init; } = StackSetting.None; /// Window (median/mean) or trail length (star trails) in frames. public int Length { get; init; } = 9; } /// Values of an export; null fields take the preset's value (BRIEF §11.4: editing any value makes it "Custom"). public sealed record ExportOverrides { public string? Codec { get; init; } public string? Container { get; init; } public string? Profile { get; init; } public int? BitDepth { get; init; } public int? Width { get; init; } public int? Height { get; init; } public string? FrameRate { get; init; } public long? BitRate { get; init; } public int? GopFrames { get; init; } public int? BFrames { get; init; } public int? Quality { get; init; } public string? RateControl { get; init; } public string? Encoder { get; init; } } public sealed record ExportSettings { /// Id of the selected preset (built-in or user); "custom" when values differ from every preset. public Auto Preset { get; init; } = new(); public ExportOverrides Overrides { get; init; } = new(); /// Output file name template (tokens: date, title, preset, width, height, fps, codec). public string NameTemplate { get; init; } = "{date} {title} - {preset}"; /// Output folder; null = the sequence's exports folder. public string? Folder { get; init; } /// Presets queued together by "Render another preset" / batch. public IReadOnlyList Batch { get; init; } = []; } /// /// The "project" of a sequence (.titano/project.json, BRIEF §7): every render decision. Originals are /// never modified; all edits live here. /// public sealed record ProjectSettings { public const int CurrentVersion = 1; public int Version { get; init; } = CurrentVersion; public TimingSettings Timing { get; init; } = new(); public ExposureSettings Exposure { get; init; } = new(); public MotionSettings Motion { get; init; } = new(); public CameraSettings Camera { get; init; } = new(); public StackSettings Stack { get; init; } = new(); public ExportSettings Export { get; init; } = new(); /// Frame indices excluded from renders. public IReadOnlyList ExcludedFrames { get; init; } = []; /// Index of the hero frame (sequence thumbnail). public int? HeroFrame { get; init; } public string ToJson() => JsonSerializer.Serialize(this, ProjectJson.Default.ProjectSettings); /// /// Reads a project; members missing from (an API client's partial settings, an older file) /// keep their defaults. The JSON is laid over the default project first, because generated deserialisation leaves /// missing init-only members null or false rather than at their initial values. /// /// The text is not a JSON object. /// The project was written by a newer Titano. public static ProjectSettings FromJson(string json) { var options = new JsonNodeOptions { PropertyNameCaseInsensitive = true }; var merged = JsonNode.Parse(new ProjectSettings().ToJson(), options)!.AsObject(); switch (JsonNode.Parse(json, options)) { case JsonObject given: Overlay(merged, given); break; case not null: throw new JsonException("A project must be a JSON object."); } var settings = JsonSerializer.Deserialize(merged, ProjectJson.Default.ProjectSettings) ?? new ProjectSettings(); if (settings.Version > CurrentVersion) throw new InvalidDataException($"project.json version {settings.Version} is newer than this Titano ({CurrentVersion})."); return settings; } /// Copies into , merging nested objects; a null never replaces an object. private static void Overlay(JsonObject target, JsonObject source) { foreach (var (name, value) in source) { if (value is JsonObject child && target[name] is JsonObject existing) Overlay(existing, child); else if (value is not null || target[name] is not JsonObject) target[name] = value?.DeepClone(); } } } [JsonSourceGenerationOptions( WriteIndented = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, UseStringEnumConverter = true, PropertyNameCaseInsensitive = true)] [JsonSerializable(typeof(ProjectSettings))] internal sealed partial class ProjectJson : JsonSerializerContext;