mirror of https://github.com/SixLabors/ImageSharp
49 changed files with 518 additions and 335 deletions
@ -1,35 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Pair of companding functions for <see cref="RgbWorkingSpace"/>.
|
|
||||
/// Used for conversion to <see cref="CieXyz"/> and backwards.
|
|
||||
/// See also: <seealso cref="RgbWorkingSpace.Companding"/>
|
|
||||
/// </summary>
|
|
||||
public interface ICompanding |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Expands a companded channel to its linear equivalent with respect to the energy.
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// For more info see:
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
|
||||
/// </remarks>
|
|
||||
/// <param name="channel">The channel value</param>
|
|
||||
/// <returns>The linear channel value</returns>
|
|
||||
float Expand(float channel); |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent (depends on the RGB color system).
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// For more info see:
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
|
||||
/// </remarks>
|
|
||||
/// <param name="channel">The channel value</param>
|
|
||||
/// <returns>The nonlinear channel value</returns>
|
|
||||
float Compress(float channel); |
|
||||
} |
|
||||
} |
|
||||
@ -1,37 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Implements gamma companding
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
|
||||
/// </remarks>
|
|
||||
public sealed class GammaCompanding : ICompanding |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="GammaCompanding"/> class.
|
|
||||
/// </summary>
|
|
||||
/// <param name="gamma">The gamma value.</param>
|
|
||||
public GammaCompanding(float gamma) => this.Gamma = gamma; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the gamma value
|
|
||||
/// </summary>
|
|
||||
public float Gamma { get; } |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(InliningOptions.ShortMethod)] |
|
||||
public float Expand(float channel) => MathF.Pow(channel, this.Gamma); |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(InliningOptions.ShortMethod)] |
|
||||
public float Compress(float channel) => MathF.Pow(channel, 1 / this.Gamma); |
|
||||
} |
|
||||
} |
|
||||
@ -1,33 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Implements L* companding
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// For more info see:
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
|
||||
/// </remarks>
|
|
||||
public sealed class LCompanding : ICompanding |
|
||||
{ |
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(InliningOptions.ShortMethod)] |
|
||||
public float Expand(float channel) |
|
||||
=> channel <= 0.08 ? 100 * channel / CieConstants.Kappa : ImageMaths.Pow3((channel + 0.16F) / 1.16F); |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(InliningOptions.ShortMethod)] |
|
||||
public float Compress(float channel) |
|
||||
{ |
|
||||
return channel <= CieConstants.Epsilon |
|
||||
? channel * CieConstants.Kappa / 100F |
|
||||
: MathF.Pow(1.16F * channel, 0.3333333F) - 0.16F; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,27 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Implements the Rec. 709 companding function
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// http://en.wikipedia.org/wiki/Rec._709
|
|
||||
/// </remarks>
|
|
||||
public sealed class Rec709Companding : ICompanding |
|
||||
{ |
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(InliningOptions.ShortMethod)] |
|
||||
public float Expand(float channel) |
|
||||
=> channel < 0.081F ? channel / 4.5F : MathF.Pow((channel + 0.099F) / 1.099F, 2.222222F); |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(InliningOptions.ShortMethod)] |
|
||||
public float Compress(float channel) |
|
||||
=> channel < 0.018F ? 4500F * channel : (1.099F * channel) - 0.099F; |
|
||||
} |
|
||||
} |
|
||||
@ -1,90 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Trivial implementation of <see cref="RgbWorkingSpace"/>
|
|
||||
/// </summary>
|
|
||||
public class RgbWorkingSpace : IEquatable<RgbWorkingSpace> |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="RgbWorkingSpace"/> class.
|
|
||||
/// </summary>
|
|
||||
/// <param name="referenceWhite">The reference white point.</param>
|
|
||||
/// <param name="companding">The function pair for converting to <see cref="CieXyz"/> and back.</param>
|
|
||||
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
|
||||
public RgbWorkingSpace(CieXyz referenceWhite, ICompanding companding, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
|
||||
{ |
|
||||
this.WhitePoint = referenceWhite; |
|
||||
this.Companding = companding; |
|
||||
this.ChromaticityCoordinates = chromaticityCoordinates; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the reference white point
|
|
||||
/// </summary>
|
|
||||
public CieXyz WhitePoint { get; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the function pair for converting to <see cref="CieXyz"/> and back.
|
|
||||
/// </summary>
|
|
||||
public ICompanding Companding { get; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Gets the chromaticity of the rgb primaries.
|
|
||||
/// </summary>
|
|
||||
public RgbPrimariesChromaticityCoordinates ChromaticityCoordinates { get; } |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Compares two <see cref="RgbWorkingSpace"/> objects for equality.
|
|
||||
/// </summary>
|
|
||||
/// <param name="left">The <see cref="RgbWorkingSpace"/> on the left side of the operand.</param>
|
|
||||
/// <param name="right">The <see cref="RgbWorkingSpace"/> on the right side of the operand.</param>
|
|
||||
/// <returns>
|
|
||||
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
|
|
||||
/// </returns>
|
|
||||
public static bool operator ==(RgbWorkingSpace left, RgbWorkingSpace right) |
|
||||
{ |
|
||||
return Equals(left, right); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Compares two <see cref="RgbWorkingSpace"/> objects for inequality
|
|
||||
/// </summary>
|
|
||||
/// <param name="left">The <see cref="RgbWorkingSpace"/> on the left side of the operand.</param>
|
|
||||
/// <param name="right">The <see cref="RgbWorkingSpace"/> on the right side of the operand.</param>
|
|
||||
/// <returns>
|
|
||||
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
|
|
||||
/// </returns>
|
|
||||
public static bool operator !=(RgbWorkingSpace left, RgbWorkingSpace right) |
|
||||
{ |
|
||||
return !Equals(left, right); |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override bool Equals(object obj) |
|
||||
{ |
|
||||
return obj is RgbWorkingSpace other && this.Equals(other); |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public bool Equals(RgbWorkingSpace other) |
|
||||
{ |
|
||||
// Object.Equals for ICompanding compares the reference only.
|
|
||||
return this.WhitePoint.Equals(other.WhitePoint) |
|
||||
&& this.ChromaticityCoordinates.Equals(other.ChromaticityCoordinates) |
|
||||
&& Equals(this.Companding, other.Companding); |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
public override int GetHashCode() |
|
||||
{ |
|
||||
int hash = this.WhitePoint.GetHashCode(); |
|
||||
hash = HashHelpers.Combine(hash, this.ChromaticityCoordinates.GetHashCode()); |
|
||||
return HashHelpers.Combine(hash, this.Companding?.GetHashCode() ?? 0); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,33 +0,0 @@ |
|||||
// Copyright (c) Six Labors and contributors.
|
|
||||
// Licensed under the Apache License, Version 2.0.
|
|
||||
|
|
||||
using System; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Implements sRGB companding
|
|
||||
/// </summary>
|
|
||||
/// <remarks>
|
|
||||
/// For more info see:
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
|
||||
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
|
||||
/// </remarks>
|
|
||||
public sealed class SRgbCompanding : ICompanding |
|
||||
{ |
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public float Expand(float channel) |
|
||||
{ |
|
||||
return channel <= 0.04045F ? channel / 12.92F : MathF.Pow((channel + 0.055F) / 1.055F, 2.4F); |
|
||||
} |
|
||||
|
|
||||
/// <inheritdoc/>
|
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public float Compress(float channel) |
|
||||
{ |
|
||||
return channel <= 0.0031308F ? 12.92F * channel : (1.055F * MathF.Pow(channel, 0.416666666666667F)) - 0.055F; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,36 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements gamma companding
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
||||
|
/// </remarks>
|
||||
|
public static class GammaCompanding |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Expands a companded channel to its linear equivalent with respect to the energy.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <param name="gamma">The gamma value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Expand(float channel, float gamma) => MathF.Pow(channel, gamma); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <param name="gamma">The gamma value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Compress(float channel, float gamma) => MathF.Pow(channel, 1 / gamma); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The gamma working space.
|
||||
|
/// </summary>
|
||||
|
public class GammaWorkingSpace : RgbWorkingSpaceBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="GammaWorkingSpace" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="gamma">The gamma value.</param>
|
||||
|
/// <param name="referenceWhite">The reference white point.</param>
|
||||
|
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
||||
|
public GammaWorkingSpace(float gamma, CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
||||
|
: base(referenceWhite, chromaticityCoordinates) => this.Gamma = gamma; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the gamma value.
|
||||
|
/// </summary>
|
||||
|
public float Gamma { get; } |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Compress(float channel) => GammaCompanding.Compress(channel, this.Gamma); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Expand(float channel) => GammaCompanding.Expand(channel, this.Gamma); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override bool Equals(object obj) |
||||
|
{ |
||||
|
if (obj is null) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (ReferenceEquals(this, obj)) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
if (obj is GammaWorkingSpace other) |
||||
|
{ |
||||
|
return this.Gamma.Equals(other.Gamma) |
||||
|
&& this.WhitePoint.Equals(other.WhitePoint) |
||||
|
&& this.ChromaticityCoordinates.Equals(other.ChromaticityCoordinates); |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override int GetHashCode() |
||||
|
{ |
||||
|
int hash = base.GetHashCode(); |
||||
|
return HashHelpers.Combine(hash, this.Gamma.GetHashCode()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements L* companding
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// For more info see:
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
||||
|
/// </remarks>
|
||||
|
public static class LCompanding |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Expands a companded channel to its linear equivalent with respect to the energy.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Expand(float channel) |
||||
|
=> channel <= 0.08 ? 100 * channel / CieConstants.Kappa : ImageMaths.Pow3((channel + 0.16F) / 1.16F); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Compress(float channel) |
||||
|
=> channel <= CieConstants.Epsilon ? channel * CieConstants.Kappa / 100F : MathF.Pow(1.16F * channel, 0.3333333F) - 0.16F; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// L* working space.
|
||||
|
/// </summary>
|
||||
|
public sealed class LWorkingSpace : RgbWorkingSpaceBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="LWorkingSpace" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="referenceWhite">The reference white point.</param>
|
||||
|
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
||||
|
public LWorkingSpace(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
||||
|
: base(referenceWhite, chromaticityCoordinates) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Compress(float channel) => LCompanding.Compress(channel); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Expand(float channel) => LCompanding.Expand(channel); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Rec. 2020 (ITU-R Recommendation BT.2020F) working space.
|
||||
|
/// </summary>
|
||||
|
public sealed class Rec2020WorkingSpace : RgbWorkingSpaceBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rec2020WorkingSpace" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="referenceWhite">The reference white point.</param>
|
||||
|
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
||||
|
public Rec2020WorkingSpace(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
||||
|
: base(referenceWhite, chromaticityCoordinates) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Compress(float channel) => Rec2020Companding.Compress(channel); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Expand(float channel) => Rec2020Companding.Expand(channel); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements the Rec. 709 companding function.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// http://en.wikipedia.org/wiki/Rec._709
|
||||
|
/// </remarks>
|
||||
|
public static class Rec709Companding |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Expands a companded channel to its linear equivalent with respect to the energy.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Expand(float channel) |
||||
|
=> channel < 0.081F ? channel / 4.5F : MathF.Pow((channel + 0.099F) / 1.099F, 2.222222F); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Compress(float channel) |
||||
|
=> channel < 0.018F ? 4500F * channel : (1.099F * channel) - 0.099F; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Rec. 709 (ITU-R Recommendation BT.709) working space.
|
||||
|
/// </summary>
|
||||
|
public sealed class Rec709WorkingSpace : RgbWorkingSpaceBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="Rec709WorkingSpace" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="referenceWhite">The reference white point.</param>
|
||||
|
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
||||
|
public Rec709WorkingSpace(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
||||
|
: base(referenceWhite, chromaticityCoordinates) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Compress(float channel) => Rec709Companding.Compress(channel); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Expand(float channel) => Rec709Companding.Expand(channel); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,83 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Base class for all implementations of <see cref="RgbWorkingSpaceBase"/>.
|
||||
|
/// </summary>
|
||||
|
public abstract class RgbWorkingSpaceBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="RgbWorkingSpaceBase"/> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="referenceWhite">The reference white point.</param>
|
||||
|
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
||||
|
protected RgbWorkingSpaceBase(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
||||
|
{ |
||||
|
this.WhitePoint = referenceWhite; |
||||
|
this.ChromaticityCoordinates = chromaticityCoordinates; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the reference white point
|
||||
|
/// </summary>
|
||||
|
public CieXyz WhitePoint { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the chromaticity of the rgb primaries.
|
||||
|
/// </summary>
|
||||
|
public RgbPrimariesChromaticityCoordinates ChromaticityCoordinates { get; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Expands a companded channel to its linear equivalent with respect to the energy.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// For more info see:
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
||||
|
/// </remarks>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
|
||||
|
public abstract float Expand(float channel); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent (depends on the RGB color system).
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// For more info see:
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
||||
|
/// </remarks>
|
||||
|
/// <param name="channel">The channel value.</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
|
||||
|
public abstract float Compress(float channel); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override bool Equals(object obj) |
||||
|
{ |
||||
|
if (obj is null) |
||||
|
{ |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
if (ReferenceEquals(this, obj)) |
||||
|
{ |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
if (obj is RgbWorkingSpaceBase other) |
||||
|
{ |
||||
|
return this.WhitePoint.Equals(other.WhitePoint) |
||||
|
&& this.ChromaticityCoordinates.Equals(other.ChromaticityCoordinates); |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
public override int GetHashCode() |
||||
|
{ |
||||
|
int hash = this.WhitePoint.GetHashCode(); |
||||
|
return HashHelpers.Combine(hash, this.ChromaticityCoordinates.GetHashCode()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Implements sRGB companding
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// For more info see:
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_RGB_to_XYZ.html"/>
|
||||
|
/// <see href="http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html"/>
|
||||
|
/// </remarks>
|
||||
|
public static class SRgbCompanding |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Expands a companded channel to its linear equivalent with respect to the energy.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the linear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Expand(float channel) => channel <= 0.04045F ? channel / 12.92F : MathF.Pow((channel + 0.055F) / 1.055F, 2.4F); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Compresses an uncompanded channel (linear) to its nonlinear equivalent.
|
||||
|
/// </summary>
|
||||
|
/// <param name="channel">The channel value</param>
|
||||
|
/// <returns>The <see cref="float"/> representing the nonlinear channel value.</returns>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public static float Compress(float channel) => channel <= 0.0031308F ? 12.92F * channel : (1.055F * MathF.Pow(channel, 0.416666666666667F)) - 0.055F; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
// Copyright (c) Six Labors and contributors.
|
||||
|
// Licensed under the Apache License, Version 2.0.
|
||||
|
|
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
namespace SixLabors.ImageSharp.ColorSpaces.Conversion.Implementation |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The sRgb working space.
|
||||
|
/// </summary>
|
||||
|
public sealed class SRgbWorkingSpace : RgbWorkingSpaceBase |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Initializes a new instance of the <see cref="SRgbWorkingSpace" /> class.
|
||||
|
/// </summary>
|
||||
|
/// <param name="referenceWhite">The reference white point.</param>
|
||||
|
/// <param name="chromaticityCoordinates">The chromaticity of the rgb primaries.</param>
|
||||
|
public SRgbWorkingSpace(CieXyz referenceWhite, RgbPrimariesChromaticityCoordinates chromaticityCoordinates) |
||||
|
: base(referenceWhite, chromaticityCoordinates) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Compress(float channel) => SRgbCompanding.Compress(channel); |
||||
|
|
||||
|
/// <inheritdoc/>
|
||||
|
[MethodImpl(InliningOptions.ShortMethod)] |
||||
|
public override float Expand(float channel) => SRgbCompanding.Expand(channel); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue