feat(color): implement the built-in colour looks and their self-test check
The seven looks (mono, sepia, warm, cool, vivid, muted, fade) are now real: a linear-light 3x3 matrix built from Rec.709 luminance (saturation mixed as (1 - k)L + kI, tint gains normalised to luminance 1), a contrast power about mid grey 0.18 and a lift, blended by strength, never clamped so the knee that follows compresses any overshoot. The kernel is vectorised with Vector<float> and runs the last pixels through the same kernel, padded, so a pixel's result does not depend on its position; large frames split across cores like the exposure processor. A double-precision scalar reference backs the tests. The self-test gains proc.look (mono renders grey at the picture's luminance; an unknown look is ignored with Export.Render.LookIgnored). New tests cover the closed form per look, mono luminance, the strength midpoint, identity byte-equality, look-then-knee order, the render capture against FrameFinish, a median stack with mono and PNG determinism. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e4364c4f25
commit
429dbcc3d8
4 files changed
+537
-6
No files matched your search
@@ -0,0 +1,230 @@
|
||||
using System.Numerics;
|
||||
using Chronos.Core.Analysis;
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Color;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Core.Render;
|
||||
using Chronos.Testing;
|
||||
|
||||
namespace Chronos.Core.Tests.Imaging;
|
||||
|
||||
public sealed class LookFilterTests
|
||||
{
|
||||
/// <summary>Flat linear patches: black, shadows, mid grey, white, highlights above 1, saturated colours and a negative channel.</summary>
|
||||
private static readonly (float R, float G, float B)[] Patches =
|
||||
[
|
||||
(0, 0, 0), (0.001f, 0.002f, 0.0005f), (0.05f, 0.04f, 0.03f), (0.18f, 0.18f, 0.18f), (0.5f, 0.25f, 0.1f), (1, 1, 1), (1.4f, 1.2f, 0.9f),
|
||||
(0.9f, 0.1f, 0.05f), (0.05f, 0.6f, 0.1f), (0.1f, 0.2f, 0.8f), (0.3f, -0.01f, 0.2f), (2.5f, 0.3f, 0.02f),
|
||||
];
|
||||
|
||||
/// <summary>One row of every patch, repeated so the row is not a whole number of vectors (the padded tail runs too).</summary>
|
||||
private static ImageBuffer PatchImage(int channels = 3, int repeats = 3)
|
||||
{
|
||||
var width = Patches.Length * repeats + 1;
|
||||
Assert.NotEqual(0, width % Vector<float>.Count);
|
||||
var image = new ImageBuffer(width, 2, channels);
|
||||
for (var y = 0; y < 2; y++)
|
||||
{
|
||||
for (var x = 0; x < width; x++)
|
||||
{
|
||||
var (r, g, b) = Patches[x % Patches.Length];
|
||||
(image[0, x, y], image[1, x, y], image[2, x, y]) = (r, g, b);
|
||||
if (channels == 4) image[3, x, y] = 0.25f + x * 0.01f;
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>The closed form of a look: m = M·c, p = 0.18·(m / 0.18)^γ above 0, q = lift + (1 − lift)·p, c + s·(q − c).</summary>
|
||||
private static double[] ClosedForm(string name, double strength, double r, double g, double b)
|
||||
{
|
||||
var look = LookFilter.Definition(name)!;
|
||||
var m = look.Matrix;
|
||||
double[] c = [r, g, b];
|
||||
var result = new double[3];
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var v = m[3 * i] * r + m[3 * i + 1] * g + m[3 * i + 2] * b;
|
||||
if (v > 0) v = 0.18 * Math.Pow(v / 0.18, look.Contrast);
|
||||
v = look.Lift + (1 - look.Lift) * v;
|
||||
result[i] = c[i] + strength * (v - c[i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void Near(double expected, double actual, string message) =>
|
||||
Assert.Approximately(expected, actual, 1e-6 * Math.Max(1, Math.Abs(expected)), message);
|
||||
|
||||
private static IEnumerable<string> Looks => LookFilter.Names.Where(n => n != LookFilter.None);
|
||||
|
||||
[Test]
|
||||
public void NamesAreKnownOrdinallyAndEachLookIsDefined()
|
||||
{
|
||||
Assert.Equal(8, LookFilter.Names.Count);
|
||||
Assert.True(LookFilter.IsKnown(null));
|
||||
Assert.True(LookFilter.Names.All(LookFilter.IsKnown));
|
||||
Assert.False(LookFilter.IsKnown("Mono"));
|
||||
Assert.False(LookFilter.IsKnown("technicolor"));
|
||||
Assert.Null(LookFilter.Definition(LookFilter.None));
|
||||
foreach (var name in Looks)
|
||||
{
|
||||
var look = LookFilter.Definition(name);
|
||||
Assert.NotNull(look);
|
||||
Assert.Equal(9, look!.Matrix.Count);
|
||||
Assert.True(look.Matrix.All(double.IsFinite) && look.Contrast > 0 && look.Lift is >= 0 and < 0.1, name);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheIdentityANullLookAndStrengthZeroLeaveThePictureByteEqual()
|
||||
{
|
||||
using var original = PatchImage(channels: 4);
|
||||
foreach (var (filter, strength) in new (string?, double)[] { (null, 1), (LookFilter.None, 1), ("vivid", 0), ("vivid", -1), ("vivid", double.NaN), ("technicolor", 1) })
|
||||
{
|
||||
using var image = original.Clone();
|
||||
LookFilter.Apply(image, filter, strength);
|
||||
Assert.True(image.Data.SequenceEqual(original.Data), $"{filter} at {strength}");
|
||||
Assert.Equal((0.5f, 0.25f, 0.1f), LookFilter.Apply(filter, strength, 0.5f, 0.25f, 0.1f));
|
||||
}
|
||||
LookSettings?[] identities = [null, new LookSettings(), new LookSettings { Filter = LookFilter.None }, new LookSettings { Filter = "sepia", Strength = 0 },
|
||||
new LookSettings { Filter = "technicolor" }];
|
||||
foreach (var look in identities)
|
||||
{
|
||||
using var image = original.Clone();
|
||||
FrameFinish.Apply(image, look, null);
|
||||
Assert.True(image.Data.SequenceEqual(original.Data), look?.ToString());
|
||||
Assert.True(FrameFinish.IsIdentity(look, null), look?.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[Case("mono")]
|
||||
[Case("sepia")]
|
||||
[Case("warm")]
|
||||
[Case("cool")]
|
||||
[Case("vivid")]
|
||||
[Case("muted")]
|
||||
[Case("fade")]
|
||||
public void EachLookMatchesItsClosedFormScalarAndVectorised(string name)
|
||||
{
|
||||
foreach (var strength in new[] { 1, 0.5, 0.3 })
|
||||
{
|
||||
using var image = PatchImage();
|
||||
LookFilter.Apply(image, name, strength);
|
||||
for (var x = 0; x < image.Width; x++)
|
||||
{
|
||||
var (r, g, b) = Patches[x % Patches.Length];
|
||||
var expected = ClosedForm(name, strength, r, g, b);
|
||||
var scalar = LookFilter.Apply(name, strength, r, g, b);
|
||||
double[] reference = [scalar.R, scalar.G, scalar.B];
|
||||
for (var c = 0; c < 3; c++)
|
||||
{
|
||||
var label = $"{name} at {strength}, patch {x % Patches.Length}, channel {c}";
|
||||
Near(expected[c], reference[c], "scalar " + label);
|
||||
Near(expected[c], image[c, x, 1], "vectorised " + label);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MonoIsTheRec709Luminance()
|
||||
{
|
||||
using var image = PatchImage();
|
||||
LookFilter.Apply(image, "mono", 1);
|
||||
for (var x = 0; x < image.Width; x++)
|
||||
{
|
||||
var (r, g, b) = Patches[x % Patches.Length];
|
||||
Assert.Equal(image[0, x, 0], image[1, x, 0]);
|
||||
Assert.Equal(image[1, x, 0], image[2, x, 0]);
|
||||
Near(ColorSpace.Luminance(r, g, b), image[0, x, 0], $"patch {x % Patches.Length}");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HalfStrengthIsTheMidpoint()
|
||||
{
|
||||
foreach (var name in Looks)
|
||||
{
|
||||
using var full = PatchImage();
|
||||
using var half = PatchImage();
|
||||
using var original = PatchImage();
|
||||
LookFilter.Apply(full, name, 1);
|
||||
LookFilter.Apply(half, name, 0.5);
|
||||
for (var i = 0; i < original.Data.Length; i++)
|
||||
Near((original.Data[i] + (double)full.Data[i]) / 2, half.Data[i], $"{name} sample {i}");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AStrengthAboveOneIsOne()
|
||||
{
|
||||
using var one = PatchImage();
|
||||
using var more = PatchImage();
|
||||
LookFilter.Apply(one, "fade", 1);
|
||||
LookFilter.Apply(more, "fade", 3);
|
||||
Assert.True(one.Data.SequenceEqual(more.Data));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AlphaAndSingleChannelPicturesAreLeftAlone()
|
||||
{
|
||||
using var original = PatchImage(channels: 4);
|
||||
using var image = original.Clone();
|
||||
LookFilter.Apply(image, "sepia", 1);
|
||||
Assert.True(image.Plane(3).SequenceEqual(original.Plane(3)));
|
||||
Assert.False(image.Plane(0).SequenceEqual(original.Plane(0)));
|
||||
|
||||
using var grey = new ImageBuffer(5, 1, 1);
|
||||
grey.Data.Fill(0.4f);
|
||||
LookFilter.Apply(grey, "fade", 1);
|
||||
Assert.True(grey.Data.ToArray().All(v => v == 0.4f));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ValuesAboveOneAreKeptThenTheKneeIsApplied()
|
||||
{
|
||||
using var looked = PatchImage();
|
||||
LookFilter.Apply(looked, "vivid", 1);
|
||||
Assert.Greater(looked.Data.ToArray().Max(), 1.5);
|
||||
|
||||
var look = new LookSettings { Filter = "vivid" };
|
||||
using var finished = PatchImage();
|
||||
FrameFinish.Apply(finished, look, 0.8);
|
||||
using var expected = looked.Clone();
|
||||
ExposureProcessor.Apply(expected, 0, null, 0.8);
|
||||
Assert.True(finished.Data.SequenceEqual(expected.Data));
|
||||
Assert.Less(finished.Data.ToArray().Max(), 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void APixelsResultDoesNotDependOnWhereItLies()
|
||||
{
|
||||
// Over 2^20 pixels the planes are split across cores; the row of a small picture must come out the same.
|
||||
using var small = PatchImage(repeats: 1);
|
||||
using var large = new ImageBuffer(small.Width, (1 << 20) / small.Width + 3);
|
||||
for (var c = 0; c < 3; c++)
|
||||
{
|
||||
for (var y = 0; y < large.Height; y++) small.Row(c, 0).CopyTo(large.Row(c, y));
|
||||
}
|
||||
LookFilter.Apply(small, "fade", 0.7);
|
||||
LookFilter.Apply(large, "fade", 0.7);
|
||||
for (var c = 0; c < 3; c++)
|
||||
{
|
||||
for (var y = 0; y < large.Height; y += 997) Assert.True(large.Row(c, y).SequenceEqual(small.Row(c, 0)), $"channel {c}, row {y}");
|
||||
Assert.True(large.Row(c, large.Height - 1).SequenceEqual(small.Row(c, 0)), $"channel {c}, last row");
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnUnknownLookIsIgnoredWithAWarning()
|
||||
{
|
||||
var warnings = new List<RenderWarning>();
|
||||
Assert.Null(FrameFinish.Look(new LookSettings { Filter = "technicolor", Strength = 0.5 }, warnings));
|
||||
Assert.Equal("Export.Render.LookIgnored(technicolor)", warnings.Single().ToString());
|
||||
warnings.Clear();
|
||||
Assert.Null(FrameFinish.Look(new LookSettings { Filter = LookFilter.None }, warnings));
|
||||
Assert.NotNull(FrameFinish.Look(new LookSettings { Filter = "warm" }, warnings));
|
||||
Assert.Empty(warnings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Imaging.Color;
|
||||
using Chronos.Core.Project;
|
||||
using Chronos.Core.Render;
|
||||
using Chronos.Testing;
|
||||
using static Chronos.Core.Tests.Render.RenderFixture;
|
||||
|
||||
namespace Chronos.Core.Tests.Render;
|
||||
|
||||
/// <summary>The colour look in renders (owner review 1, R6): once per output frame, after stacking, before the knee.</summary>
|
||||
public sealed class LookRenderTests(TestContext context)
|
||||
{
|
||||
private static ProjectSettings With(LookSettings? look, double? knee = null, StackSettings? stack = null) =>
|
||||
Plain with { Look = look, Exposure = new ExposureSettings { HighlightKnee = knee }, Stack = stack ?? new StackSettings() };
|
||||
|
||||
private static void SameFrames(IReadOnlyList<ImageBuffer> expected, IReadOnlyList<ImageBuffer> actual)
|
||||
{
|
||||
Assert.Equal(expected.Count, actual.Count);
|
||||
for (var i = 0; i < expected.Count; i++) Assert.True(expected[i].Data.SequenceEqual(actual[i].Data), $"frame {i}");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheIdentityLooksRenderTheSameBytesAsNoLook()
|
||||
{
|
||||
var (plain, _) = await CaptureAsync(Input(Source(4), With(null)), cancellationToken: context.Cancellation);
|
||||
foreach (var look in new[] { new LookSettings { Filter = LookFilter.None }, new LookSettings { Filter = "vivid", Strength = 0 } })
|
||||
{
|
||||
var (encode, result) = await CaptureAsync(Input(Source(4), With(look)), cancellationToken: context.Cancellation);
|
||||
SameFrames(plain.Frames, encode.Frames);
|
||||
Assert.Empty(result.Warnings);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnUnknownLookRendersAsNoneWithAWarning()
|
||||
{
|
||||
var (plain, _) = await CaptureAsync(Input(Source(4), With(null)), cancellationToken: context.Cancellation);
|
||||
var (encode, result) = await CaptureAsync(Input(Source(4), With(new LookSettings { Filter = "technicolor" })), cancellationToken: context.Cancellation);
|
||||
SameFrames(plain.Frames, encode.Frames);
|
||||
Assert.Contains(result.Warnings, w => w.Key == "Export.Render.LookIgnored" && w.Args.SequenceEqual(["technicolor"]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACaptureWithALookIsTheFinishOfThePlainCapture()
|
||||
{
|
||||
// The look comes before the knee: the plain render has no knee, so the finish adds both in its own order.
|
||||
var look = new LookSettings { Filter = "sepia", Strength = 0.7 };
|
||||
var source = Source(6, configure: o => o with { StartEv = 1, EndEv = 2 });
|
||||
var (plain, _) = await CaptureAsync(Input(source, With(null)), cancellationToken: context.Cancellation);
|
||||
var (encode, result) = await CaptureAsync(Input(source, With(look, knee: 0.85)), cancellationToken: context.Cancellation);
|
||||
Assert.Empty(result.Warnings);
|
||||
foreach (var frame in plain.Frames) FrameFinish.Apply(frame, look, 0.85);
|
||||
SameFrames(plain.Frames, encode.Frames);
|
||||
Assert.Less(encode.Frames.Max(f => f.Data.ToArray().Max()), 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AMedianStackWithMonoIsMonoOfTheMedian()
|
||||
{
|
||||
var stack = new StackSettings { Mode = StackSetting.Median, Length = 5 };
|
||||
var source = Source(9, motion: i => (0.5 * i, 0));
|
||||
var (median, _) = await CaptureAsync(Input(source, With(null, stack: stack)), cancellationToken: context.Cancellation);
|
||||
var (mono, _) = await CaptureAsync(Input(source, With(new LookSettings { Filter = "mono" }, stack: stack)), cancellationToken: context.Cancellation);
|
||||
foreach (var frame in median.Frames) LookFilter.Apply(frame, "mono", 1);
|
||||
SameFrames(median.Frames, mono.Frames);
|
||||
Assert.True(mono.Frames.All(f => f.Plane(0).SequenceEqual(f.Plane(1)) && f.Plane(1).SequenceEqual(f.Plane(2))));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PngOutputWithALookIsByteIdenticalAcrossRuns()
|
||||
{
|
||||
var dir = context.CreateTempDirectory();
|
||||
var project = With(new LookSettings { Filter = "fade", Strength = 0.8 }, knee: 0.9);
|
||||
var first = await Pipeline().RenderAsync(Input(Source(5), project), Target(dir, Preset("png-sequence") with { BitDepth = 16 }, "a"), cancellationToken: context.Cancellation);
|
||||
var second = await Pipeline().RenderAsync(Input(Source(5), project), Target(dir, Preset("png-sequence") with { BitDepth = 16 }, "b"), cancellationToken: context.Cancellation);
|
||||
var a = Directory.GetFiles(first.OutputPath).Order(StringComparer.Ordinal).ToArray();
|
||||
var b = Directory.GetFiles(second.OutputPath).Order(StringComparer.Ordinal).ToArray();
|
||||
Assert.Equal(5, a.Length);
|
||||
Assert.Equal(a.Length, b.Length);
|
||||
for (var i = 0; i < a.Length; i++) Assert.BytesEqual(File.ReadAllBytes(a[i]), File.ReadAllBytes(b[i]));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user