77namespace SixLabors . ImageSharp . ColorSpaces . Companding
88{
99 /// <summary>
10- /// Implements Rec. 2020 companding function (for 12-bits) .
10+ /// Implements Rec. 2020 companding function.
1111 /// </summary>
1212 /// <remarks>
1313 /// <see href="http://en.wikipedia.org/wiki/Rec._2020"/>
14- /// For 10-bits, companding is identical to <see cref="Rec709Companding"/>
1514 /// </remarks>
1615 public static class Rec2020Companding
1716 {
17+ private const float Alpha = 1.09929682680944F ;
18+ private const float AlphaMinusOne = Alpha - 1F ;
19+ private const float Beta = 0.018053968510807F ;
20+ private const float InverseBeta = Beta * 4.5F ;
21+ private const float Epsilon = 1 / 0.45F ;
22+
1823 /// <summary>
1924 /// Expands a companded channel to its linear equivalent with respect to the energy.
2025 /// </summary>
2126 /// <param name="channel">The channel value.</param>
2227 /// <returns>The <see cref="float"/> representing the linear channel value.</returns>
2328 [ MethodImpl ( InliningOptions . ShortMethod ) ]
2429 public static float Expand ( float channel )
25- => channel < 0.08145F ? channel / 4.5F : MathF . Pow ( ( channel + 0.0993F ) / 1.0993F , 2.222222F ) ;
30+ => channel < InverseBeta ? channel / 4.5F : MathF . Pow ( ( channel + AlphaMinusOne ) / Alpha , Epsilon ) ;
2631
2732 /// <summary>
2833 /// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
@@ -31,6 +36,6 @@ public static float Expand(float channel)
3136 /// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
3237 [ MethodImpl ( InliningOptions . ShortMethod ) ]
3338 public static float Compress ( float channel )
34- => channel < 0.0181F ? 4500F * channel : ( 1.0993F * channel ) - 0.0993F ;
39+ => channel < Beta ? 4.5F * channel : ( Alpha * MathF . Pow ( channel , 0.45F ) ) - AlphaMinusOne ;
3540 }
3641}
0 commit comments