Browse Source

Merge HLG base/derived class into one; abbreviate transfer function class name

pull/3153/head
winscripter 4 weeks ago
parent
commit
e315e04bc5
  1. 71
      src/ImageSharp/Formats/Jxl/Cms/TransferFunctions/JxlHlgTransferFunction.cs
  2. 53
      src/ImageSharp/Formats/Jxl/Cms/TransferFunctions/JxlHybridLogGammaTransferFunction.cs

71
src/ImageSharp/Formats/Jxl/Cms/TransferFunctions/JxlHybridLogGammaTransferFunctionBase.cs → src/ImageSharp/Formats/Jxl/Cms/TransferFunctions/JxlHlgTransferFunction.cs

@ -1,33 +1,78 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Jxl.Processing;
namespace SixLabors.ImageSharp.Formats.Jxl.Cms.TransferFunctions;
/// <summary>
/// Base class for HLG transfer function.
/// Hybrid Log Gamma transfer function.
/// </summary>
internal abstract class JxlHybridLogGammaTransferFunctionBase
internal sealed class JxlHlgTransferFunction
{
// Shared constants used by transfer functions
protected const float A = 0.17883277f;
protected const float RA = 1.0f / A;
protected const float B = 1 - (4 * A);
protected const float C = 0.5599107295f;
protected const float Inverse12 = 1.0f / 12.0f;
private const float A = 0.17883277f;
private const float RA = 1.0f / A;
private const float B = 1 - (4 * A);
private const float C = 0.5599107295f;
private const float Inverse12 = 1.0f / 12.0f;
private const float HiAdd = B * Inverse12;
private const float HiMul = 0.003639807079052639f; // MathF.Exp(-C * RA) * Inverse12
private const float HiPow = 8.067285659607931f; // RA * JxlMath.InverseLog2E
/// <summary>
/// Initializes a new instance of the <see cref="JxlHlgTransferFunction"/> class.
/// </summary>
/// <remarks>
/// Use static methods. Don't instantiate this class.
/// </remarks>
private JxlHlgTransferFunction()
{
}
public static Vector<float> EncodedFromDisplay(Vector<float> x)
{
Vector<float> sign = Vector.Create(0x80000000u).As<uint, float>();
Vector<float> originalSign = x & sign;
x = Vector.AndNot(sign, x);
Vector<int> belowInverse12 = Vector.LessThan(x, Vector.Create(Inverse12));
Vector<float> lo = Vector.SquareRoot(Vector.Create(3.0f) * x);
Vector<float> hi = (Vector.Create(A * JxlMath.InverseLog2E) * Vector.Log2((Vector.Create(12f) * x) + Vector.Create(-B))) + Vector.Create(C);
Vector<float> magnitude = Vector.ConditionalSelect(belowInverse12, lo, hi);
return Vector.AndNot(sign, magnitude) | originalSign;
}
public static Vector<float> DisplayFromEncoded(Vector<float> x)
{
Vector<float> sign = Vector.Create(0x80000000u).As<uint, float>();
Vector<float> originalSign = x & sign;
x = Vector.AndNot(sign, x);
Vector<int> below05 = Vector.LessThan(x, Vector.Create(0.5f));
Vector<float> lo = x * (x * Vector.Create(1f / 3f));
Vector<float> hi = (Pow2(x * Vector.Create(HiPow)) * Vector.Create(HiMul)) + Vector.Create(HiAdd);
Vector<float> magnitude = Vector.ConditionalSelect(below05, lo, hi);
return Vector.AndNot(sign, magnitude) | originalSign;
}
private static Vector<float> Pow2(Vector<float> x) => JxlSimdUtils.FastPow2f(x);
/// <summary>
/// Converts encoded signal to display signal.
/// </summary>
/// <param name="encoded">The encoded signal</param>
/// <returns>The display signal</returns>
protected static double DisplayFromEncoded(double encoded) => Ootf(InverseOotf(encoded));
public static double DisplayFromEncoded(double encoded) => Ootf(InverseOotf(encoded));
/// <summary>
/// Converts display signal to encoded signal.
/// </summary>
/// <param name="display">The display signal</param>
/// <returns>The encoded signal</returns>
protected static double EncodedFromDisplay(double display) => Oetf(InverseOetf(display));
public static double EncodedFromDisplay(double display) => Oetf(InverseOetf(display));
/// <summary>
/// Opto-Electronic Transfer Function - converts
@ -39,7 +84,7 @@ internal abstract class JxlHybridLogGammaTransferFunctionBase
/// </remarks>
/// <param name="s">Scene light</param>
/// <returns>Digital video signal</returns>
private static double Oetf(double s)
public static double Oetf(double s)
{
if (s == 0)
{
@ -70,7 +115,7 @@ internal abstract class JxlHybridLogGammaTransferFunctionBase
/// </remarks>
/// <param name="e">Digital video signal</param>
/// <returns>Scene light</returns>
private static double InverseOetf(double e)
public static double InverseOetf(double e)
{
if (e == 0)
{
@ -100,7 +145,7 @@ internal abstract class JxlHybridLogGammaTransferFunctionBase
/// </remarks>
/// <param name="s">Input signal</param>
/// <returns>Digital video signal</returns>
private static double Ootf(double s) => s;
public static double Ootf(double s) => s;
/// <summary>
/// Inverse Opto-Optical Transfer Function - as-is.
@ -110,5 +155,5 @@ internal abstract class JxlHybridLogGammaTransferFunctionBase
/// </remarks>
/// <param name="s">Digital video signal</param>
/// <returns>Scene light</returns>
private static double InverseOotf(double s) => s;
public static double InverseOotf(double s) => s;
}

53
src/ImageSharp/Formats/Jxl/Cms/TransferFunctions/JxlHybridLogGammaTransferFunction.cs

@ -1,53 +0,0 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using SixLabors.ImageSharp.Formats.Jxl.Processing;
namespace SixLabors.ImageSharp.Formats.Jxl.Cms.TransferFunctions;
internal sealed class JxlHybridLogGammaTransferFunction : JxlHybridLogGammaTransferFunctionBase
{
private const float HiAdd = B * Inverse12;
private const float HiMul = 0.003639807079052639f; // MathF.Exp(-C * RA) * Inverse12
private const float HiPow = 8.067285659607931f; // RA * JxlMath.InverseLog2E
/// <summary>
/// Initializes a new instance of the <see cref="JxlHybridLogGammaTransferFunction"/> class.
/// </summary>
/// <remarks>
/// Use static methods. Don't instantiate this class.
/// </remarks>
private JxlHybridLogGammaTransferFunction()
{
}
public static Vector<float> EncodedFromDisplay(Vector<float> x)
{
Vector<float> sign = Vector.Create(0x80000000u).As<uint, float>();
Vector<float> originalSign = x & sign;
x = Vector.AndNot(sign, x);
Vector<int> belowInverse12 = Vector.LessThan(x, Vector.Create(Inverse12));
Vector<float> lo = Vector.SquareRoot(Vector.Create(3.0f) * x);
Vector<float> hi = (Vector.Create(A * JxlMath.InverseLog2E) * Vector.Log2((Vector.Create(12f) * x) + Vector.Create(-B))) + Vector.Create(C);
Vector<float> magnitude = Vector.ConditionalSelect(belowInverse12, lo, hi);
return Vector.AndNot(sign, magnitude) | originalSign;
}
public static Vector<float> DisplayFromEncoded(Vector<float> x)
{
Vector<float> sign = Vector.Create(0x80000000u).As<uint, float>();
Vector<float> originalSign = x & sign;
x = Vector.AndNot(sign, x);
Vector<int> below05 = Vector.LessThan(x, Vector.Create(0.5f));
Vector<float> lo = x * (x * Vector.Create(1f / 3f));
Vector<float> hi = (Pow2(x * Vector.Create(HiPow)) * Vector.Create(HiMul)) + Vector.Create(HiAdd);
Vector<float> magnitude = Vector.ConditionalSelect(below05, lo, hi);
return Vector.AndNot(sign, magnitude) | originalSign;
}
private static Vector<float> Pow2(Vector<float> x) => x * x;
}
Loading…
Cancel
Save