From 800843d2aeb7cabf14471fdf054ff5a29bd52f5f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 09:01:53 +0000 Subject: [PATCH] feat(core): add sRGB transfer and luminance helpers Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- src/Titano.Core/Imaging/ColorSpace.cs | 99 +++++++++++++++++++ .../Foundation/ColorSpaceTests.cs | 36 +++++++ 2 files changed, 135 insertions(+) create mode 100644 src/Titano.Core/Imaging/ColorSpace.cs create mode 100644 tests/Titano.Core.Tests/Foundation/ColorSpaceTests.cs diff --git a/src/Titano.Core/Imaging/ColorSpace.cs b/src/Titano.Core/Imaging/ColorSpace.cs new file mode 100644 index 00000000..bcfd7bef --- /dev/null +++ b/src/Titano.Core/Imaging/ColorSpace.cs @@ -0,0 +1,99 @@ +using System.Runtime.CompilerServices; + +namespace Titano.Core.Imaging; + +/// +/// 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. +/// +public static class ColorSpace +{ + /// Rec.709 / sRGB luminance weights for linear RGB. + 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(); + + /// Exact sRGB EOTF: encoded 0..1 → linear 0..1. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static double SrgbToLinear(double v) => v <= 0.04045 ? v / 12.92 : Math.Pow((v + 0.055) / 1.055, 2.4); + + /// Exact inverse sRGB EOTF: linear 0..1 → encoded 0..1 (values outside are clamped). + [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; + } + + /// 8-bit sRGB code value → linear (table lookup). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Linear8(byte code) => Decode8[code]; + + /// 16-bit sRGB code value → linear (table lookup). + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Linear16(ushort code) => Decode16[code]; + + /// Fast linear → encoded sRGB (0..1) with a 4096-entry table and linear interpolation; + /// max error below 1/65535 above the toe. + [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; + } + + /// Linear → 8-bit sRGB code with rounding. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static byte Encode8(float linear) => (byte)(EncodeFast(linear) * 255f + 0.5f); + + /// Linear → 16-bit sRGB code with rounding. + [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; + + /// Writes Rec.709 linear luminance of an RGB buffer into . + public static void Luminance(ImageBuffer rgb, Span 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]; + } + + /// Exposure value difference in stops between two linear luminances. + 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; + } +} diff --git a/tests/Titano.Core.Tests/Foundation/ColorSpaceTests.cs b/tests/Titano.Core.Tests/Foundation/ColorSpaceTests.cs new file mode 100644 index 00000000..2b05fdb8 --- /dev/null +++ b/tests/Titano.Core.Tests/Foundation/ColorSpaceTests.cs @@ -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); +}