diff --git a/src/Titano.Core/Advice/ParameterBindings.cs b/src/Titano.Core/Advice/ParameterBindings.cs index f3566556..956dd530 100644 --- a/src/Titano.Core/Advice/ParameterBindings.cs +++ b/src/Titano.Core/Advice/ParameterBindings.cs @@ -94,10 +94,6 @@ internal static class ParameterBindings public static string Size(int width, int height) => string.Create(CultureInfo.InvariantCulture, $"{width}x{height}"); - public static bool HasAdvice(Auto auto) => typeof(T).IsValueType - ? auto.ReasonKey is not null && !auto.ReasonKey.StartsWith(AdvisorKeys.MissingPrefix, StringComparison.Ordinal) - : auto.AutoValue is not null; - private static ParameterBinding AutoField(string parameter, AdvisorSection section, bool module, Func> get, Func, ProjectSettings> set) => new() { Parameter = parameter, @@ -108,7 +104,7 @@ internal static class ParameterBindings ResetToAuto = s => set(s, get(s).Reset()), }; - private static object? EffectiveOf(Auto auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : HasAdvice(auto) ? auto.AutoValue : null; + private static object? EffectiveOf(Auto auto) => !auto.IsAuto && auto.Manual is not null ? auto.Manual : auto.HasAdvice ? auto.AutoValue : null; /// /// Stores a value with its reason. Missing data changes no value: the missing-data reason is stored only when the @@ -118,7 +114,7 @@ internal static class ParameterBindings { var args = Strings(recommendation.ReasonArgs); if (recommendation.Value is null) - return HasAdvice(auto) ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args }; + return auto.HasAdvice ? auto : auto with { ReasonKey = recommendation.ReasonKey, ReasonArgs = args }; if (recommendation.Value is not T value) throw new InvalidOperationException($"{recommendation.Parameter} expects {typeof(T).Name}, got {recommendation.Value.GetType().Name}."); return auto.WithAdvice(value, recommendation.ReasonKey, args); diff --git a/src/Titano.Core/Project/Auto.cs b/src/Titano.Core/Project/Auto.cs index 469ce46c..4a1dcfa8 100644 --- a/src/Titano.Core/Project/Auto.cs +++ b/src/Titano.Core/Project/Auto.cs @@ -9,9 +9,13 @@ public sealed record Auto { public bool IsAuto { get; init; } = true; - /// The advisor's value (null until analysis provides one). + /// The advisor's value; meaningful only when . public T? AutoValue { get; init; } + /// True once the advisor stored a value. Needed because a value type's is never + /// null: without it "no advice" and an advised 0 or false look the same. + public bool HasAdvice { get; init; } + /// The user's value when is false. public T? Manual { get; init; } @@ -21,11 +25,11 @@ public sealed record Auto public IReadOnlyList? ReasonArgs { get; init; } /// The value in force: manual when set by the user, otherwise the advisor's, otherwise . - public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : AutoValue ?? fallback; + public T Resolve(T fallback) => !IsAuto && Manual is not null ? Manual : HasAdvice && AutoValue is not null ? AutoValue : fallback; public Auto WithManual(T value) => this with { IsAuto = false, Manual = value }; public Auto Reset() => this with { IsAuto = true, Manual = default }; - public Auto WithAdvice(T value, string? reasonKey, params string[] args) => this with { AutoValue = value, ReasonKey = reasonKey, ReasonArgs = args }; + public Auto WithAdvice(T value, string? reasonKey, params string[] args) => this with { AutoValue = value, HasAdvice = true, ReasonKey = reasonKey, ReasonArgs = args }; } diff --git a/src/Titano.Core/Render/ProjectValues.cs b/src/Titano.Core/Render/ProjectValues.cs deleted file mode 100644 index 67b3f40c..00000000 --- a/src/Titano.Core/Render/ProjectValues.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Titano.Core.Project; - -namespace Titano.Core.Render; - -/// -/// Reads value-type settings. For value types is never null (an -/// unconstrained T? is T), so returns default instead of the fallback -/// when there is no advice; here a default automatic value means "no advice" and yields the fallback. -/// -internal static class ProjectValues -{ - public static T Get(Auto setting, T fallback) where T : struct - { - if (!setting.IsAuto) return setting.Manual; - return EqualityComparer.Default.Equals(setting.AutoValue, default) ? fallback : setting.AutoValue; - } -} diff --git a/src/Titano.Core/Render/RenderPipeline.cs b/src/Titano.Core/Render/RenderPipeline.cs index dd2575e6..a984dcb1 100644 --- a/src/Titano.Core/Render/RenderPipeline.cs +++ b/src/Titano.Core/Render/RenderPipeline.cs @@ -138,8 +138,8 @@ internal sealed class RenderRun : IDisposable else _warnings.Add(new RenderWarning("Export.Render.KneeIgnored", knee.ToString(CultureInfo.InvariantCulture))); } var motion = input.Project.Motion; - _targetShutter = double.Clamp(ProjectValues.Get(motion.TargetShutterAngle, 180.0), 0, 360); - _flowQuality = ProjectValues.Get(motion.FlowQuality, FlowQualitySetting.Balanced) switch + _targetShutter = double.Clamp(motion.TargetShutterAngle.Resolve(180.0), 0, 360); + _flowQuality = motion.FlowQuality.Resolve(FlowQualitySetting.Balanced) switch { FlowQualitySetting.Draft => FlowQuality.Draft, FlowQualitySetting.Best => FlowQuality.Best, diff --git a/src/Titano.Core/Render/RenderPlan.cs b/src/Titano.Core/Render/RenderPlan.cs index 6ddc6c1b..c9a4938e 100644 --- a/src/Titano.Core/Render/RenderPlan.cs +++ b/src/Titano.Core/Render/RenderPlan.cs @@ -204,7 +204,7 @@ public sealed class RenderPlan options ??= RenderPlanOptions.Default; var n = captureTimes.Count; var timing = project.Timing; - var mode = ProjectValues.Get(timing.Mode, TimingMode.Constant); + var mode = timing.Mode.Resolve(TimingMode.Constant); var warnings = new List(); var playbackText = timing.FrameRate.Resolve(string.Empty); var playback = FrameRateText.TryParse(playbackText, out var rate) ? rate.Value : frameRate.Value; diff --git a/src/Titano.Core/Render/RenderPreparation.cs b/src/Titano.Core/Render/RenderPreparation.cs index b35316d5..1892e6c2 100644 --- a/src/Titano.Core/Render/RenderPreparation.cs +++ b/src/Titano.Core/Render/RenderPreparation.cs @@ -118,12 +118,12 @@ public static class RenderPreparation Array.Fill(excluded, true); foreach (var i in included) excluded[i] = false; - var deflicker = ProjectValues.Get(exposure.Deflicker, false); - var holyGrail = ProjectValues.Get(exposure.HolyGrail, false); - var regionMode = deflicker ? ProjectValues.Get(exposure.Regions, RegionMode.Off) : RegionMode.Off; - var whiteBalance = ProjectValues.Get(exposure.WhiteBalanceSmoothing, false); - var stabilize = ProjectValues.Get(motion.Stabilize, false); - var smoothing = ProjectValues.Get(motion.StabilizeSmoothing, new StabilizerOptions().SmoothingSigma); + var deflicker = exposure.Deflicker.Resolve(false); + var holyGrail = exposure.HolyGrail.Resolve(false); + var regionMode = deflicker ? exposure.Regions.Resolve(RegionMode.Off) : RegionMode.Off; + var whiteBalance = exposure.WhiteBalanceSmoothing.Resolve(false); + var stabilize = motion.Stabilize.Resolve(false); + var smoothing = motion.StabilizeSmoothing.Resolve(new StabilizerOptions().SmoothingSigma); var luminance = deflicker || holyGrail ? Valid(cached?.LogLuminance, excluded) : null; var measureLuminance = (deflicker || holyGrail) && luminance is null; @@ -183,15 +183,15 @@ public static class RenderPreparation { var options = new DeflickerOptions { - Window = int.Max(3, ProjectValues.Get(exposure.DeflickerWindow, 0) is var w and > 0 ? w : RecommendedWindow(luminance)), - Strength = double.Clamp(ProjectValues.Get(exposure.DeflickerStrength, 1.0), 0, 1), + Window = int.Max(3, exposure.DeflickerWindow.Resolve(0) is var w and > 0 ? w : RecommendedWindow(luminance)), + Strength = double.Clamp(exposure.DeflickerStrength.Resolve(1.0), 0, 1), }; var evs = new double?[n]; // Excluded frames (test shots, bumps) must not create exposure steps. for (var i = 0; i < n; i++) evs[i] = excluded[i] ? null : input.Frames[i].ExposureValue; if (holyGrail) { - var transition = ProjectValues.Get(exposure.TransitionLength, 0); + var transition = exposure.TransitionLength.Resolve(0); hg = HolyGrailEngine.Compute(luminance, evs, new HolyGrailOptions { TransitionFrames = transition > 0 ? transition : null, Deflicker = options }); } var offsets = hg?.StepOffsets ?? DeflickerEngine.OffsetsFromExposure(evs); diff --git a/src/Titano.Core/Render/RenderSetup.cs b/src/Titano.Core/Render/RenderSetup.cs index ae6e1d2b..b58de9cf 100644 --- a/src/Titano.Core/Render/RenderSetup.cs +++ b/src/Titano.Core/Render/RenderSetup.cs @@ -47,11 +47,11 @@ public sealed record RenderSetup warnings.AddRange(plan.Warnings.Select(k => new RenderWarning(k))); warnings.AddRange(selection.Notices.Select(n => new RenderWarning(n.Key, n.Args))); var stacking = project.Stack.Mode != StackSetting.None; - var blur = ProjectValues.Get(project.Motion.MotionBlur, false); + var blur = project.Motion.MotionBlur.Resolve(false); if (blur && stacking) warnings.Add(new RenderWarning("Export.Render.BlurOffWhileStacking")); var exposure = project.Exposure; - var measuring = ProjectValues.Get(exposure.Deflicker, false) || ProjectValues.Get(exposure.HolyGrail, false) - || ProjectValues.Get(exposure.WhiteBalanceSmoothing, false) || ProjectValues.Get(project.Motion.Stabilize, false); + var measuring = exposure.Deflicker.Resolve(false) || exposure.HolyGrail.Resolve(false) + || exposure.WhiteBalanceSmoothing.Resolve(false) || project.Motion.Stabilize.Resolve(false); return new RenderSetup { Target = target, diff --git a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs index 99712dd2..c22c0835 100644 --- a/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs +++ b/tests/Titano.Core.Tests/Project/ProjectSettingsTests.cs @@ -30,6 +30,23 @@ public sealed class ProjectSettingsTests Assert.Contains("\"deflickerWindow\"", settings.ToJson()); } + [Test] + public void ValueTypesFallBackOnlyWithoutAdvice() + { + Assert.Equal(21, new Auto().Resolve(21)); + Assert.Equal(FlowQualitySetting.Balanced, new Auto().Resolve(FlowQualitySetting.Balanced)); + Assert.Equal(0, new Auto().WithAdvice(0, null).Resolve(21)); + Assert.False(new Auto().WithAdvice(false, null).Resolve(true)); + Assert.Equal(0.5, new Auto().WithAdvice(0.8, null).WithManual(0.5).Resolve(1.0)); + Assert.False(new Auto().WithManual(false).Resolve(true)); + var back = ProjectSettings.FromJson(new ProjectSettings + { + Exposure = new ExposureSettings { Deflicker = new Auto().WithAdvice(false, null) }, + }.ToJson()); + Assert.False(back.Exposure.Deflicker.Resolve(true)); + Assert.True(ProjectSettings.FromJson(new ProjectSettings().ToJson()).Exposure.Deflicker.Resolve(true)); + } + [Test] public void ResetReturnsToAdvice() { diff --git a/tests/Titano.Core.Tests/Render/ProjectValuesTests.cs b/tests/Titano.Core.Tests/Render/ProjectValuesTests.cs deleted file mode 100644 index dd550837..00000000 --- a/tests/Titano.Core.Tests/Render/ProjectValuesTests.cs +++ /dev/null @@ -1,20 +0,0 @@ -using Titano.Core.Project; -using Titano.Core.Render; -using Titano.Testing; - -namespace Titano.Core.Tests.Render; - -public sealed class ProjectValuesTests -{ - [Test] - public void ValueTypeSettingsFallBackWithoutAdvice() - { - Assert.Equal(21, ProjectValues.Get(new Auto(), 21)); - Assert.Equal(180.0, ProjectValues.Get(new Auto(), 180.0)); - Assert.Equal(FlowQualitySetting.Balanced, ProjectValues.Get(new Auto(), FlowQualitySetting.Balanced)); - Assert.Equal(41, ProjectValues.Get(new Auto().WithAdvice(41, null), 21)); - Assert.Equal(0.5, ProjectValues.Get(new Auto().WithAdvice(0.8, null).WithManual(0.5), 1.0)); - Assert.Equal(FlowQualitySetting.Draft, ProjectValues.Get(new Auto().WithManual(FlowQualitySetting.Draft), FlowQualitySetting.Balanced)); - Assert.False(ProjectValues.Get(new Auto().WithManual(false), true)); - } -}