feat(analysis): add frame statistics, colour temperature and exposure processor

FrameStatistics: trimmed log2-luminance mean (fine weighted histogram),
256-bin log histogram, clip fractions, channel means, CCT, Laplacian
variance sharpness, Immerkaer noise, mask-weighted region statistics,
black/white/duplicate helpers. ColorTemperature: McCamy seed refined to
the nearest Krystek Planckian-locus point with a |Duv| <= 0.02 check.
ExposureProcessor: SIMD EV/white-balance/region gains and hue-preserving
highlight soft clip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 09:51:43 +00:00
1 parent cce38c3927
commit 40870e12dc
5 files changed
+702

No files matched your search

@@ -0,0 +1,116 @@
using Titano.Core.Analysis;
using Titano.Core.Imaging;
using Titano.Testing;
namespace Titano.Core.Tests.Analysis;
public sealed class ExposureProcessorTests
{
// 37 × 5 = 185 pixels: not a multiple of any vector width, so the scalar tail runs too.
private static ImageBuffer Ramp(int width = 37, int height = 5, int channels = 3)
{
var image = new ImageBuffer(width, height, channels);
var data = image.Data;
for (var i = 0; i < data.Length; i++) data[i] = 0.001f * (i % 997);
return image;
}
[Test]
public void EvAndChannelGainsMultiply()
{
using var image = Ramp();
using var original = image.Clone();
ExposureProcessor.Apply(image, 1, new ChannelGains(1.5, 1, 0.5));
float[] factors = [3, 2, 1];
for (var c = 0; c < 3; c++)
{
for (var i = 0; i < image.PlaneLength; i++)
Assert.Approximately(factors[c] * original.Plane(c)[i], image.Plane(c)[i], 1e-6);
}
Assert.Throws<ArgumentOutOfRangeException>(() => ExposureProcessor.Apply(image, double.NaN));
Assert.Throws<ArgumentOutOfRangeException>(() => ExposureProcessor.Apply(image, 0, new ChannelGains(0, 1, 1)));
}
[Test]
public void SingleChannelAndAlphaAreHandled()
{
using var grey = Ramp(channels: 1);
using var greyOriginal = grey.Clone();
ExposureProcessor.Apply(grey, -1, new ChannelGains(4, 4, 4));
Assert.Approximately(0.5 * greyOriginal.Plane(0)[100], grey.Plane(0)[100], 1e-7);
using var rgba = Ramp(channels: 4);
using var rgbaOriginal = rgba.Clone();
ExposureProcessor.Apply(rgba, 2);
Assert.SequenceEqual(rgbaOriginal.Plane(3).ToArray(), rgba.Plane(3).ToArray());
Assert.Approximately(4 * rgbaOriginal.Plane(2)[50], rgba.Plane(2)[50], 1e-6);
}
[Test]
public void RegionGainsBlendThroughTheMask()
{
using var image = new ImageBuffer(37, 5, 3);
image.Data.Fill(0.1f);
var mask = new float[image.PlaneLength];
for (var i = 0; i < mask.Length; i++) mask[i] = (i % 3) * 0.5f;
ExposureProcessor.ApplyRegion(image, mask, skyEv: 1, landEv: -1);
for (var i = 0; i < mask.Length; i++)
{
var expected = 0.1 * (0.5 + mask[i] * (2 - 0.5));
Assert.Approximately(expected, image.Plane(1)[i], 1e-7);
}
Assert.Throws<ArgumentException>(() => ExposureProcessor.ApplyRegion(image, new float[3], 0, 0));
}
[Test]
public void SoftClipIsSmoothBoundedAndKeepsHue()
{
const float knee = 0.8f;
Assert.Equal(0.5f, ExposureProcessor.SoftClip(0.5f, knee));
Assert.Approximately(knee, ExposureProcessor.SoftClip(knee + 1e-6f, knee), 1e-5);
var values = Enumerable.Range(0, 400).Select(i => (double)ExposureProcessor.SoftClip(i * 0.05f, knee)).ToList();
Assert.Monotonic(values);
Assert.Less(values.Max(), 1);
// Slope 1 at the knee (C¹): the secant over h above it is 1 / (1 + h / (1 − knee)).
var slope = (ExposureProcessor.SoftClip(knee + 1e-3f, knee) - knee) / 1e-3f;
Assert.Approximately(1 / (1 + 1e-3 / (1 - knee)), slope, 1e-3);
using var image = new ImageBuffer(37, 5, 3);
for (var i = 0; i < image.PlaneLength; i++)
{
image.Plane(0)[i] = 0.02f * i;
image.Plane(1)[i] = 0.01f * i;
image.Plane(2)[i] = 0.005f * i;
}
ExposureProcessor.Apply(image, 0, softClipKnee: knee);
for (var i = 1; i < image.PlaneLength; i++)
{
var r = image.Plane(0)[i];
Assert.LessOrEqual(r, 1);
Assert.Approximately(2, r / image.Plane(1)[i], 1e-4);
Assert.Approximately(2, image.Plane(1)[i] / image.Plane(2)[i], 1e-4);
Assert.Approximately(ExposureProcessor.SoftClip(0.02f * i, knee), r, 1e-5);
}
}
[Test]
public void LargeImagesTakeTheParallelPathWithTheSameResult()
{
using var image = new ImageBuffer(1100, 1000, 3);
var data = image.Data;
for (var i = 0; i < data.Length; i++) data[i] = (i % 1000) * 0.0012f;
using var expected = image.Clone();
ExposureProcessor.Apply(image, 0.5, softClipKnee: 0.9);
var gain = (float)Math.Pow(2, 0.5);
foreach (var index in new[] { 0, 12345, 777_777, 1_099_999 })
{
var r = expected.Plane(0)[index] * gain;
var g = expected.Plane(1)[index] * gain;
var b = expected.Plane(2)[index] * gain;
var m = Math.Max(r, Math.Max(g, b));
var s = m > 0.9f ? ExposureProcessor.SoftClip(m, 0.9f) / m : 1;
Assert.Approximately(r * s, image.Plane(0)[index], 1e-5);
Assert.Approximately(b * s, image.Plane(2)[index], 1e-5);
}
}
}