mirror of https://github.com/SixLabors/ImageSharp
20 changed files with 400 additions and 26 deletions
@ -0,0 +1,27 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Numerics; |
|||
|
|||
namespace SixLabors.ImageSharp.ColorProfiles; |
|||
|
|||
/// <summary>
|
|||
/// Provides standard Y (luma) coefficient sets for weighted RGB conversions.
|
|||
/// </summary>
|
|||
public static class KnownYCoefficients |
|||
{ |
|||
/// <summary>
|
|||
/// ITU-R BT.601 (SD video standard).
|
|||
/// </summary>
|
|||
public static readonly Vector3 BT601 = new(0.299F, 0.587F, 0.114F); |
|||
|
|||
/// <summary>
|
|||
/// ITU-R BT.709 (HD video, sRGB standard).
|
|||
/// </summary>
|
|||
public static readonly Vector3 BT709 = new(0.2126F, 0.7152F, 0.0722F); |
|||
|
|||
/// <summary>
|
|||
/// ITU-R BT.2020 (UHD/4K video standard).
|
|||
/// </summary>
|
|||
public static readonly Vector3 BT2020 = new(0.2627F, 0.6780F, 0.0593F); |
|||
} |
|||
@ -0,0 +1,136 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
namespace SixLabors.ImageSharp.ColorProfiles; |
|||
|
|||
/// <summary>
|
|||
/// Represents a Y (luminance) color.
|
|||
/// </summary>
|
|||
[StructLayout(LayoutKind.Sequential)] |
|||
public readonly struct Y : IColorProfile<Y, Rgb> |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Y"/> struct.
|
|||
/// </summary>
|
|||
/// <param name="l">The luminance component.</param>
|
|||
public Y(float l) => this.L = l; |
|||
|
|||
/// <summary>
|
|||
/// Gets the luminance component.
|
|||
/// </summary>
|
|||
/// <remarks>A value ranging between 0 and 1.</remarks>
|
|||
public float L { get; } |
|||
|
|||
/// <summary>
|
|||
/// Compares two <see cref="Y"/> objects for equality.
|
|||
/// </summary>
|
|||
/// <param name="left">The <see cref="Y"/> on the left side of the operand.</param>
|
|||
/// <param name="right">The <see cref="Y"/> 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>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static bool operator ==(Y left, Y right) => left.Equals(right); |
|||
|
|||
/// <summary>
|
|||
/// Compares two <see cref="Y"/> objects for inequality.
|
|||
/// </summary>
|
|||
/// <param name="left">The <see cref="Y"/> on the left side of the operand.</param>
|
|||
/// <param name="right">The <see cref="Y"/> 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>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static bool operator !=(Y left, Y right) => !left.Equals(right); |
|||
|
|||
/// <inheritdoc/>
|
|||
public static Y FromProfileConnectingSpace(ColorConversionOptions options, in Rgb source) |
|||
{ |
|||
Vector3 weights = options.YCoefficients; |
|||
float l = (weights.X * source.R) + (weights.Y * source.G) + (weights.Z * source.B); |
|||
return new Y(l); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Rgb> source, Span<Y> destination) |
|||
{ |
|||
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
|||
|
|||
// TODO: We can optimize this by using SIMD
|
|||
for (int i = 0; i < source.Length; i++) |
|||
{ |
|||
Rgb rgb = source[i]; |
|||
destination[i] = FromProfileConnectingSpace(options, in rgb); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public Vector4 ToScaledVector4() => new(this.L); |
|||
|
|||
/// <inheritdoc/>
|
|||
public static Y FromScaledVector4(Vector4 source) => new(source.X); |
|||
|
|||
/// <inheritdoc/>
|
|||
public static void ToScaledVector4(ReadOnlySpan<Y> source, Span<Vector4> destination) |
|||
{ |
|||
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
|||
|
|||
// TODO: Optimize via SIMD
|
|||
for (int i = 0; i < source.Length; i++) |
|||
{ |
|||
destination[i] = source[i].ToScaledVector4(); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public static void FromScaledVector4(ReadOnlySpan<Vector4> source, Span<Y> destination) |
|||
{ |
|||
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
|||
|
|||
// TODO: Optimize via SIMD
|
|||
for (int i = 0; i < source.Length; i++) |
|||
{ |
|||
destination[i] = FromScaledVector4(source[i]); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public Rgb ToProfileConnectingSpace(ColorConversionOptions options) |
|||
=> new(this.L, this.L, this.L); |
|||
|
|||
/// <inheritdoc/>
|
|||
public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Y> source, Span<Rgb> destination) |
|||
{ |
|||
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
|||
|
|||
// TODO: We can optimize this by using SIMD
|
|||
for (int i = 0; i < source.Length; i++) |
|||
{ |
|||
destination[i] = source[i].ToProfileConnectingSpace(options); |
|||
} |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource() |
|||
=> ChromaticAdaptionWhitePointSource.RgbWorkingSpace; |
|||
|
|||
/// <inheritdoc/>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public override int GetHashCode() |
|||
=> this.L.GetHashCode(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public override string ToString() |
|||
=> FormattableString.Invariant($"Y({this.L:#0.##})"); |
|||
|
|||
/// <inheritdoc/>
|
|||
public override bool Equals(object? obj) |
|||
=> obj is Y other && this.Equals(other); |
|||
|
|||
/// <inheritdoc/>
|
|||
public bool Equals(Y other) => this.L == other.L; |
|||
} |
|||
@ -0,0 +1,113 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using SixLabors.ImageSharp.ColorProfiles; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests.ColorProfiles; |
|||
|
|||
/// <summary>
|
|||
/// Tests <see cref="Rgb"/>-<see cref="Y"/> conversions.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// Test data generated mathematically
|
|||
/// </remarks>
|
|||
public class RbgAndYConversionTests |
|||
{ |
|||
private static readonly ApproximateColorProfileComparer Comparer = new(.001F); |
|||
|
|||
[Theory] |
|||
[InlineData(0F, 0F, 0F, 0F)] |
|||
[InlineData(0.5F, 0.5F, 0.5F, 0.5F)] |
|||
[InlineData(1F, 1F, 1F, 1F)] |
|||
public void Convert_Y_To_Rgb(float y, float r, float g, float b) |
|||
{ |
|||
// Arrange
|
|||
Y input = new(y); |
|||
Rgb expected = new(r, g, b); |
|||
ColorProfileConverter converter = new(); |
|||
|
|||
Span<Y> inputSpan = new Y[5]; |
|||
inputSpan.Fill(input); |
|||
|
|||
Span<Rgb> actualSpan = new Rgb[5]; |
|||
|
|||
// Act
|
|||
Rgb actual = converter.Convert<Y, Rgb>(input); |
|||
converter.Convert<Y, Rgb>(inputSpan, actualSpan); |
|||
|
|||
// Assert
|
|||
Assert.Equal(expected, actual, Comparer); |
|||
|
|||
for (int i = 0; i < actualSpan.Length; i++) |
|||
{ |
|||
Assert.Equal(expected, actualSpan[i], Comparer); |
|||
} |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(0F, 0F, 0F, 0F)] |
|||
[InlineData(0.5F, 0.5F, 0.5F, 0.5F)] |
|||
[InlineData(1F, 1F, 1F, 1F)] |
|||
public void Convert_Rgb_To_Y_BT601(float r, float g, float b, float y) |
|||
{ |
|||
ColorConversionOptions options = new() |
|||
{ |
|||
YCoefficients = KnownYCoefficients.BT601 |
|||
}; |
|||
|
|||
Convert_Rgb_To_Y_Core(r, g, b, y, options); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(0F, 0F, 0F, 0F)] |
|||
[InlineData(0.5F, 0.5F, 0.5F, 0.5F)] |
|||
[InlineData(1F, 1F, 1F, 1F)] |
|||
public void Convert_Rgb_To_Y_BT709(float r, float g, float b, float y) |
|||
{ |
|||
ColorConversionOptions options = new() |
|||
{ |
|||
YCoefficients = KnownYCoefficients.BT709 |
|||
}; |
|||
|
|||
Convert_Rgb_To_Y_Core(r, g, b, y, options); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(0F, 0F, 0F, 0F)] |
|||
[InlineData(0.5F, 0.5F, 0.5F, 0.49999997F)] |
|||
[InlineData(1F, 1F, 1F, 0.99999994F)] |
|||
public void Convert_Rgb_To_Y_BT2020(float r, float g, float b, float y) |
|||
{ |
|||
ColorConversionOptions options = new() |
|||
{ |
|||
YCoefficients = KnownYCoefficients.BT2020 |
|||
}; |
|||
|
|||
Convert_Rgb_To_Y_Core(r, g, b, y, options); |
|||
} |
|||
|
|||
private static void Convert_Rgb_To_Y_Core(float r, float g, float b, float y, ColorConversionOptions options) |
|||
{ |
|||
// Arrange
|
|||
Rgb input = new(r, g, b); |
|||
Y expected = new(y); |
|||
ColorProfileConverter converter = new(options); |
|||
|
|||
Span<Rgb> inputSpan = new Rgb[5]; |
|||
inputSpan.Fill(input); |
|||
|
|||
Span<Y> actualSpan = new Y[5]; |
|||
|
|||
// Act
|
|||
Y actual = converter.Convert<Rgb, Y>(input); |
|||
converter.Convert<Rgb, Y>(inputSpan, actualSpan); |
|||
|
|||
// Assert
|
|||
Assert.Equal(expected, actual, Comparer); |
|||
|
|||
for (int i = 0; i < actualSpan.Length; i++) |
|||
{ |
|||
Assert.Equal(expected, actualSpan[i], Comparer); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
|
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using SixLabors.ImageSharp.ColorProfiles; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests.ColorProfiles; |
|||
|
|||
/// <summary>
|
|||
/// Tests the <see cref="Y"/> struct.
|
|||
/// </summary>
|
|||
[Trait("Color", "Conversion")] |
|||
public class YTests |
|||
{ |
|||
[Fact] |
|||
public void YConstructorAssignsFields() |
|||
{ |
|||
const float y = .75F; |
|||
Y yValue = new(y); |
|||
|
|||
Assert.Equal(y, yValue.L); |
|||
} |
|||
|
|||
[Fact] |
|||
public void YEquality() |
|||
{ |
|||
Y x = default; |
|||
Y y = new(1F); |
|||
Assert.True(default == default(Y)); |
|||
Assert.False(default != default(Y)); |
|||
Assert.Equal(default, default(Y)); |
|||
Assert.Equal(new Y(1), new Y(1)); |
|||
|
|||
Assert.Equal(new Y(.5F), new Y(.5F)); |
|||
Assert.False(x.Equals(y)); |
|||
Assert.False(x.Equals((object)y)); |
|||
Assert.False(x.GetHashCode().Equals(y.GetHashCode())); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue