Merge branch 'or1p4-p2' into or1-p4
This commit is contained in:
commit
d067219764
4 files changed
+537
-6
No files matched your search
@@ -1,3 +1,6 @@
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Chronos.Core.Imaging.Color;
|
||||
|
||||
/// <summary>
|
||||
@@ -6,26 +9,212 @@ namespace Chronos.Core.Imaging.Color;
|
||||
/// strength; values are never clamped (the highlight knee that follows compresses any overshoot). Applied to output
|
||||
/// frames only (<see cref="Chronos.Core.Render.FrameFinish"/>), never seen by measuring, flow or stacking.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Per pixel, with c the linear RGB value and s the strength: m = M·c; p = 0.18·(m / 0.18)^γ for each channel above 0
|
||||
/// (others kept as they are); q = lift + (1 − lift)·p; result c + s·(q − c). The matrices are built from the Rec.709
|
||||
/// luminance (<see cref="ColorSpace"/>) and keep it: a saturation k mixes the luminance and the colour as
|
||||
/// (1 − k)·L + k·I, and a tint scales the rows by gains whose luminance is 1. Mid grey (0.18) is the contrast's pivot.
|
||||
/// </remarks>
|
||||
public static class LookFilter
|
||||
{
|
||||
/// <summary>The identity look's name.</summary>
|
||||
public const string None = "none";
|
||||
|
||||
/// <summary>The contrast power's pivot: mid grey in linear light, kept by every contrast.</summary>
|
||||
public const double Pivot = 0.18;
|
||||
|
||||
private const int ParallelThreshold = 1 << 20;
|
||||
private const int Chunk = 1 << 16;
|
||||
|
||||
/// <summary>Every look this Chronos renders, in the order the editor lists them; names are lower case.</summary>
|
||||
public static IReadOnlyList<string> Names { get; } = [None, "mono", "sepia", "warm", "cool", "vivid", "muted", "fade"];
|
||||
|
||||
private static readonly LookDefinition[] Definitions =
|
||||
[
|
||||
Saturated("mono", 0, 1, 0),
|
||||
Tinted("sepia", 1.30, 0.97, 0.62, 0, 1.05, 0),
|
||||
Tinted("warm", 1.10, 1.00, 0.86, 1.05, 1, 0),
|
||||
Tinted("cool", 0.90, 1.00, 1.14, 1, 1, 0),
|
||||
Saturated("vivid", 1.35, 1.12, 0),
|
||||
Saturated("muted", 0.6, 0.92, 0),
|
||||
Saturated("fade", 0.8, 0.85, 0.025),
|
||||
];
|
||||
|
||||
/// <summary>A name this Chronos renders: null (none) or one of <see cref="Names"/> (ordinal, case-sensitive).</summary>
|
||||
public static bool IsKnown(string? name) => name is null || Names.Contains(name, StringComparer.Ordinal);
|
||||
|
||||
/// <summary>
|
||||
/// Applies the look <paramref name="filter"/> at <paramref name="strength"/> (0..1) to the colour planes of a
|
||||
/// linear-light <paramref name="image"/> in place; a fourth (alpha) channel is left untouched. None, an unknown name and
|
||||
/// a strength of 0 leave the image as it is.
|
||||
/// Applies the look <paramref name="filter"/> at <paramref name="strength"/> (0..1, above 1 read as 1) to the colour
|
||||
/// planes of a linear-light <paramref name="image"/> in place; a fourth (alpha) channel is left untouched, and so is a
|
||||
/// single-channel image. None, an unknown name and a strength of 0 (or not a number) leave the image as it is.
|
||||
/// Vectorised with <see cref="Vector{T}"/> (the last pixels too, padded, so a pixel's result never depends on where it
|
||||
/// lies); large images are split across cores.
|
||||
/// </summary>
|
||||
public static void Apply(ImageBuffer image, string? filter, double strength)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(image);
|
||||
if (filter is null or None || !(strength > 0) || !IsKnown(filter)) return;
|
||||
// Every look renders as the identity until the looks are implemented.
|
||||
if (filter is null or None || !(strength > 0) || image.Channels < 3 || Definition(filter) is not { } look) return;
|
||||
var kernel = new Kernel(look, Math.Min(strength, 1));
|
||||
ForChunks(image.PlaneLength, (start, length) =>
|
||||
kernel.Run(image.Plane(0).Slice(start, length), image.Plane(1).Slice(start, length), image.Plane(2).Slice(start, length)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scalar reference of <see cref="Apply(ImageBuffer, string?, double)"/> for one linear RGB value, worked out in double
|
||||
/// precision; none, an unknown name and a strength of 0 return the value as it is.
|
||||
/// </summary>
|
||||
public static (float R, float G, float B) Apply(string? filter, double strength, float r, float g, float b)
|
||||
{
|
||||
if (filter is null or None || !(strength > 0) || Definition(filter) is not { } look) return (r, g, b);
|
||||
var s = Math.Min(strength, 1);
|
||||
var m = look.Matrix;
|
||||
double[] c = [r, g, b];
|
||||
var result = new float[3];
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
var v = m[3 * i] * c[0] + m[3 * i + 1] * c[1] + m[3 * i + 2] * c[2];
|
||||
if (v > 0 && look.Contrast != 1) v = Pivot * Math.Pow(v / Pivot, look.Contrast);
|
||||
v = look.Lift + (1 - look.Lift) * v;
|
||||
result[i] = (float)(c[i] + s * (v - c[i]));
|
||||
}
|
||||
return (result[0], result[1], result[2]);
|
||||
}
|
||||
|
||||
/// <summary>The parameters of a look this Chronos renders other than none; null for none or an unknown name.</summary>
|
||||
internal static LookDefinition? Definition(string? name)
|
||||
{
|
||||
foreach (var look in Definitions)
|
||||
{
|
||||
if (string.Equals(look.Name, name, StringComparison.Ordinal)) return look;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>A look that changes the saturation by <paramref name="saturation"/> (0 = grey, 1 = as it is).</summary>
|
||||
private static LookDefinition Saturated(string name, double saturation, double contrast, double lift) =>
|
||||
Tinted(name, 1, 1, 1, saturation, contrast, lift);
|
||||
|
||||
/// <summary>A saturation followed by channel gains, scaled so that their luminance is 1 (the look keeps luminance).</summary>
|
||||
private static LookDefinition Tinted(string name, double red, double green, double blue, double saturation, double contrast, double lift)
|
||||
{
|
||||
double[] luma = [ColorSpace.LumaR, ColorSpace.LumaG, ColorSpace.LumaB];
|
||||
var norm = luma[0] * red + luma[1] * green + luma[2] * blue;
|
||||
double[] gains = [red / norm, green / norm, blue / norm];
|
||||
var matrix = new double[9];
|
||||
for (var i = 0; i < 3; i++)
|
||||
{
|
||||
for (var j = 0; j < 3; j++) matrix[3 * i + j] = gains[i] * ((1 - saturation) * luma[j] + (i == j ? saturation : 0));
|
||||
}
|
||||
return new LookDefinition(name, matrix, contrast, lift);
|
||||
}
|
||||
|
||||
private static void ForChunks(int length, Action<int, int> body)
|
||||
{
|
||||
if (length < ParallelThreshold)
|
||||
{
|
||||
body(0, length);
|
||||
return;
|
||||
}
|
||||
var chunks = (length + Chunk - 1) / Chunk;
|
||||
Parallel.For(0, chunks, i =>
|
||||
{
|
||||
var start = i * Chunk;
|
||||
body(start, Math.Min(Chunk, length - start));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>A look's coefficients in single precision, broadcast for the vector kernel.</summary>
|
||||
private sealed class Kernel
|
||||
{
|
||||
private readonly Vector<float>[] _matrix = new Vector<float>[9];
|
||||
private readonly bool _contrast;
|
||||
private readonly Vector<float> _gamma;
|
||||
private readonly Vector<float> _offset;
|
||||
private readonly bool _lift;
|
||||
private readonly Vector<float> _liftScale;
|
||||
private readonly Vector<float> _liftOffset;
|
||||
private readonly bool _blend;
|
||||
private readonly Vector<float> _strength;
|
||||
|
||||
public Kernel(LookDefinition look, double strength)
|
||||
{
|
||||
for (var i = 0; i < 9; i++) _matrix[i] = new Vector<float>((float)look.Matrix[i]);
|
||||
_contrast = look.Contrast != 1;
|
||||
// 0.18·(v / 0.18)^γ = exp(γ·ln v + (1 − γ)·ln 0.18).
|
||||
_gamma = new Vector<float>((float)look.Contrast);
|
||||
_offset = new Vector<float>((float)((1 - look.Contrast) * Math.Log(Pivot)));
|
||||
_lift = look.Lift != 0;
|
||||
_liftScale = new Vector<float>((float)(1 - look.Lift));
|
||||
_liftOffset = new Vector<float>((float)look.Lift);
|
||||
_blend = strength < 1;
|
||||
_strength = new Vector<float>((float)strength);
|
||||
}
|
||||
|
||||
public void Run(Span<float> r, Span<float> g, Span<float> b)
|
||||
{
|
||||
var vr = MemoryMarshal.Cast<float, Vector<float>>(r);
|
||||
var vg = MemoryMarshal.Cast<float, Vector<float>>(g);
|
||||
var vb = MemoryMarshal.Cast<float, Vector<float>>(b);
|
||||
for (var i = 0; i < vr.Length; i++) Map(ref vr[i], ref vg[i], ref vb[i]);
|
||||
var done = vr.Length * Vector<float>.Count;
|
||||
var rest = r.Length - done;
|
||||
if (rest == 0) return;
|
||||
// The last pixels go through the same kernel, padded with zeros, so they match the vectorised ones exactly.
|
||||
var count = Vector<float>.Count;
|
||||
Span<float> pad = stackalloc float[3 * count];
|
||||
pad.Clear();
|
||||
r[done..].CopyTo(pad);
|
||||
g[done..].CopyTo(pad[count..]);
|
||||
b[done..].CopyTo(pad[(2 * count)..]);
|
||||
var tr = new Vector<float>(pad[..count]);
|
||||
var tg = new Vector<float>(pad.Slice(count, count));
|
||||
var tb = new Vector<float>(pad.Slice(2 * count, count));
|
||||
Map(ref tr, ref tg, ref tb);
|
||||
tr.CopyTo(pad);
|
||||
tg.CopyTo(pad[count..]);
|
||||
tb.CopyTo(pad[(2 * count)..]);
|
||||
pad[..rest].CopyTo(r[done..]);
|
||||
pad.Slice(count, rest).CopyTo(g[done..]);
|
||||
pad.Slice(2 * count, rest).CopyTo(b[done..]);
|
||||
}
|
||||
|
||||
private void Map(ref Vector<float> r, ref Vector<float> g, ref Vector<float> b)
|
||||
{
|
||||
var m = _matrix;
|
||||
var nr = m[0] * r + m[1] * g + m[2] * b;
|
||||
var ng = m[3] * r + m[4] * g + m[5] * b;
|
||||
var nb = m[6] * r + m[7] * g + m[8] * b;
|
||||
if (_contrast)
|
||||
{
|
||||
nr = Power(nr);
|
||||
ng = Power(ng);
|
||||
nb = Power(nb);
|
||||
}
|
||||
if (_lift)
|
||||
{
|
||||
nr = nr * _liftScale + _liftOffset;
|
||||
ng = ng * _liftScale + _liftOffset;
|
||||
nb = nb * _liftScale + _liftOffset;
|
||||
}
|
||||
if (_blend)
|
||||
{
|
||||
nr = r + _strength * (nr - r);
|
||||
ng = g + _strength * (ng - g);
|
||||
nb = b + _strength * (nb - b);
|
||||
}
|
||||
r = nr;
|
||||
g = ng;
|
||||
b = nb;
|
||||
}
|
||||
|
||||
/// <summary>The contrast power for values above 0; zero, negative and NaN values are kept as they are.</summary>
|
||||
private Vector<float> Power(Vector<float> v) =>
|
||||
Vector.ConditionalSelect(Vector.GreaterThan(v, Vector<float>.Zero), Vector.Exp(_gamma * Vector.Log(v) + _offset), v);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One built-in look (<see cref="LookFilter"/>): a row-major linear-light 3×3 <paramref name="Matrix"/>, the contrast power
|
||||
/// <paramref name="Contrast"/> about <see cref="LookFilter.Pivot"/> (1 = none) and the <paramref name="Lift"/> of black.
|
||||
/// </summary>
|
||||
internal sealed record LookDefinition(string Name, IReadOnlyList<double> Matrix, double Contrast, double Lift);
|
||||
@@ -1,7 +1,37 @@
|
||||
using Chronos.Core.Imaging;
|
||||
using Chronos.Core.Project;
|
||||
|
||||
namespace Chronos.Core.Render.Diagnostics;
|
||||
|
||||
/// <summary>Colour look checks (owner review 1, R6).</summary>
|
||||
public static partial class SelfTest
|
||||
{
|
||||
private static IEnumerable<SelfTestCheck> ColourChecks() => [];
|
||||
private static IEnumerable<SelfTestCheck> ColourChecks()
|
||||
{
|
||||
yield return Check("proc.look", "The mono look renders grey at the picture's luminance and an unknown look renders nothing", async context =>
|
||||
{
|
||||
var source = Synthetic(2);
|
||||
var plain = await RenderDecodedAsync(context, Input(source), name: "plain").ConfigureAwait(false);
|
||||
var mono = await RenderDecodedAsync(context, Input(source, Plain with { Look = new LookSettings { Filter = "mono" } }), name: "mono").ConfigureAwait(false);
|
||||
double grey = 0, luminance = 0;
|
||||
var compared = 0;
|
||||
for (var i = 0; i < plain[1].PlaneLength; i++)
|
||||
{
|
||||
var (r, g, b) = (mono[1].Plane(0)[i], mono[1].Plane(1)[i], mono[1].Plane(2)[i]);
|
||||
grey = double.Max(grey, double.Max(double.Abs(r - g), double.Abs(g - b)));
|
||||
var (pr, pg, pb) = (plain[1].Plane(0)[i], plain[1].Plane(1)[i], plain[1].Plane(2)[i]);
|
||||
// Clipped highlights lose the luminance the look kept: only values the PNG holds are compared.
|
||||
if (float.Max(pr, float.Max(pg, pb)) > 0.95f) continue;
|
||||
luminance = double.Max(luminance, double.Abs(ColorSpace.Luminance(pr, pg, pb) - r));
|
||||
compared++;
|
||||
}
|
||||
DisposeAll(plain);
|
||||
DisposeAll(mono);
|
||||
var warnings = new List<RenderWarning>();
|
||||
var unknown = FrameFinish.Look(new LookSettings { Filter = "technicolor" }, warnings);
|
||||
Require(grey == 0 && compared > 0 && luminance < 1e-3, Invariant($"mono channel spread {grey:0.######}, luminance error {luminance:0.######} over {compared} pixels"));
|
||||
Require(unknown is null && warnings.Count == 1 && warnings[0].Key == "Export.Render.LookIgnored", "an unknown look is not ignored with a warning");
|
||||
return Invariant($"luminance error {luminance:0.######}");
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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