feat(core): add sRGB transfer and luminance helpers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+135
No files matched your search
@@ -0,0 +1,99 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Titano.Core.Imaging;
|
||||
|
||||
/// <summary>
|
||||
/// sRGB/Rec.709 transfer functions and luminance. Titano processes in linear light with Rec.709 primaries;
|
||||
/// encoded (gamma) values appear only at codec and display edges.
|
||||
/// </summary>
|
||||
public static class ColorSpace
|
||||
{
|
||||
/// <summary>Rec.709 / sRGB luminance weights for linear RGB.</summary>
|
||||
public const float LumaR = 0.2126f, LumaG = 0.7152f, LumaB = 0.0722f;
|
||||
|
||||
private const int EncodeLutSize = 4096;
|
||||
private static readonly float[] Decode8 = BuildDecodeLut(256);
|
||||
private static readonly float[] Decode16 = BuildDecodeLut(65536);
|
||||
private static readonly float[] EncodeLut = BuildEncodeLut();
|
||||
|
||||
/// <summary>Exact sRGB EOTF: encoded 0..1 → linear 0..1.</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double SrgbToLinear(double v) => v <= 0.04045 ? v / 12.92 : Math.Pow((v + 0.055) / 1.055, 2.4);
|
||||
|
||||
/// <summary>Exact inverse sRGB EOTF: linear 0..1 → encoded 0..1 (values outside are clamped).</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double LinearToSrgb(double v)
|
||||
{
|
||||
if (v <= 0) return 0;
|
||||
if (v >= 1) return 1;
|
||||
return v <= 0.0031308 ? v * 12.92 : 1.055 * Math.Pow(v, 1 / 2.4) - 0.055;
|
||||
}
|
||||
|
||||
/// <summary>8-bit sRGB code value → linear (table lookup).</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float Linear8(byte code) => Decode8[code];
|
||||
|
||||
/// <summary>16-bit sRGB code value → linear (table lookup).</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float Linear16(ushort code) => Decode16[code];
|
||||
|
||||
/// <summary>Fast linear → encoded sRGB (0..1) with a 4096-entry table and linear interpolation;
|
||||
/// max error below 1/65535 above the toe.</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float EncodeFast(float linear)
|
||||
{
|
||||
if (!(linear > 0f)) return 0f;
|
||||
if (linear >= 1f) return 1f;
|
||||
// Index in the square-root domain spreads table entries into the shadows where sRGB is steep.
|
||||
var t = MathF.Sqrt(linear) * (EncodeLutSize - 1);
|
||||
var i = (int)t;
|
||||
var f = t - i;
|
||||
return EncodeLut[i] + (EncodeLut[i + 1] - EncodeLut[i]) * f;
|
||||
}
|
||||
|
||||
/// <summary>Linear → 8-bit sRGB code with rounding.</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte Encode8(float linear) => (byte)(EncodeFast(linear) * 255f + 0.5f);
|
||||
|
||||
/// <summary>Linear → 16-bit sRGB code with rounding.</summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ushort Encode16(float linear) => (ushort)(EncodeFast(linear) * 65535f + 0.5f);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static float Luminance(float r, float g, float b) => LumaR * r + LumaG * g + LumaB * b;
|
||||
|
||||
/// <summary>Writes Rec.709 linear luminance of an RGB buffer into <paramref name="destination"/>.</summary>
|
||||
public static void Luminance(ImageBuffer rgb, Span<float> destination)
|
||||
{
|
||||
if (rgb.Channels == 1)
|
||||
{
|
||||
rgb.Plane(0).CopyTo(destination);
|
||||
return;
|
||||
}
|
||||
var r = rgb.Plane(0);
|
||||
var g = rgb.Plane(1);
|
||||
var b = rgb.Plane(2);
|
||||
for (var i = 0; i < destination.Length; i++) destination[i] = LumaR * r[i] + LumaG * g[i] + LumaB * b[i];
|
||||
}
|
||||
|
||||
/// <summary>Exposure value difference in stops between two linear luminances.</summary>
|
||||
public static double Stops(double from, double to) => Math.Log2(Math.Max(to, 1e-12) / Math.Max(from, 1e-12));
|
||||
|
||||
private static float[] BuildDecodeLut(int size)
|
||||
{
|
||||
var lut = new float[size];
|
||||
for (var i = 0; i < size; i++) lut[i] = (float)SrgbToLinear(i / (double)(size - 1));
|
||||
return lut;
|
||||
}
|
||||
|
||||
private static float[] BuildEncodeLut()
|
||||
{
|
||||
var lut = new float[EncodeLutSize + 1];
|
||||
for (var i = 0; i <= EncodeLutSize; i++)
|
||||
{
|
||||
var s = Math.Min(1.0, i / (double)(EncodeLutSize - 1));
|
||||
lut[i] = (float)LinearToSrgb(s * s);
|
||||
}
|
||||
return lut;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using Titano.Core.Imaging;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Tests.Foundation;
|
||||
|
||||
public sealed class ColorSpaceTests
|
||||
{
|
||||
[Test]
|
||||
public void EightBitRoundTripIsExact()
|
||||
{
|
||||
for (var i = 0; i < 256; i++) Assert.Equal((byte)i, ColorSpace.Encode8(ColorSpace.Linear8((byte)i)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SixteenBitRoundTripWithinOneCode()
|
||||
{
|
||||
for (var i = 0; i < 65536; i += 7)
|
||||
{
|
||||
var back = ColorSpace.Encode16(ColorSpace.Linear16((ushort)i));
|
||||
Assert.InRange(back, i - 1, i + 1);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FastEncodeMatchesExact()
|
||||
{
|
||||
for (var i = 1; i <= 10000; i++)
|
||||
{
|
||||
var v = i / 10000f;
|
||||
Assert.Approximately(ColorSpace.LinearToSrgb(v), ColorSpace.EncodeFast(v), 2e-5);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MiddleGreyIsAbout118() => Assert.InRange(ColorSpace.Encode8(0.18f), 117, 119);
|
||||
}
|
||||
Reference in new issue
Block a user