Skip to content

Commit 07e354d

Browse files
Add YCbCr conversion
1 parent 80dc3f5 commit 07e354d

6 files changed

Lines changed: 348 additions & 2 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Buffers;
5+
using SixLabors.ImageSharp.Memory;
6+
7+
namespace SixLabors.ImageSharp.ColorProfiles;
8+
9+
internal static class ColorProfileConverterExtensionsCieXyzRgb
10+
{
11+
public static TTo Convert<TFrom, TTo>(this ColorProfileConverter converter, TFrom source)
12+
where TFrom : struct, IColorProfile<TFrom, CieXyz>
13+
where TTo : struct, IColorProfile<TTo, Rgb>
14+
{
15+
ColorConversionOptions options = converter.Options;
16+
17+
// Convert to input PCS
18+
CieXyz pcsFrom = source.ToProfileConnectingSpace(options);
19+
20+
// Adapt to target white point
21+
pcsFrom = VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsFrom);
22+
23+
// Convert between PCS
24+
Rgb pcsTo = Rgb.FromProfileConnectingSpace(options, in pcsFrom);
25+
26+
// Convert to output from PCS
27+
return TTo.FromProfileConnectingSpace(options, pcsTo);
28+
}
29+
30+
public static void Convert<TFrom, TTo>(this ColorProfileConverter converter, ReadOnlySpan<TFrom> source, Span<TTo> destination)
31+
where TFrom : struct, IColorProfile<TFrom, CieXyz>
32+
where TTo : struct, IColorProfile<TTo, Rgb>
33+
{
34+
ColorConversionOptions options = converter.Options;
35+
36+
// Convert to input PCS.
37+
using IMemoryOwner<CieXyz> pcsFromOwner = options.MemoryAllocator.Allocate<CieXyz>(source.Length);
38+
Span<CieXyz> pcsFrom = pcsFromOwner.GetSpan();
39+
TFrom.ToProfileConnectionSpace(options, source, pcsFrom);
40+
41+
// Adapt to target white point
42+
VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, pcsFrom, pcsFrom);
43+
44+
// Convert between PCS.
45+
using IMemoryOwner<Rgb> pcsToOwner = options.MemoryAllocator.Allocate<Rgb>(source.Length);
46+
Span<Rgb> pcsTo = pcsToOwner.GetSpan();
47+
Rgb.FromProfileConnectionSpace(options, pcsFrom, pcsTo);
48+
49+
// Convert to output from PCS
50+
TTo.FromProfileConnectionSpace(options, pcsTo, destination);
51+
}
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Buffers;
5+
using SixLabors.ImageSharp.Memory;
6+
7+
namespace SixLabors.ImageSharp.ColorProfiles;
8+
9+
internal static class ColorProfileConverterExtensionsRgbCieXyz
10+
{
11+
public static TTo Convert<TFrom, TTo>(this ColorProfileConverter converter, TFrom source)
12+
where TFrom : struct, IColorProfile<TFrom, Rgb>
13+
where TTo : struct, IColorProfile<TTo, CieXyz>
14+
{
15+
ColorConversionOptions options = converter.Options;
16+
17+
// Convert to input PCS
18+
Rgb pcsFrom = source.ToProfileConnectingSpace(options);
19+
20+
// Convert between PCS
21+
CieXyz pcsTo = pcsFrom.ToProfileConnectingSpace(options);
22+
23+
// Adapt to target white point
24+
pcsTo = VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsTo);
25+
26+
// Convert to output from PCS
27+
return TTo.FromProfileConnectingSpace(options, pcsTo);
28+
}
29+
30+
public static void Convert<TFrom, TTo>(this ColorProfileConverter converter, ReadOnlySpan<TFrom> source, Span<TTo> destination)
31+
where TFrom : struct, IColorProfile<TFrom, Rgb>
32+
where TTo : struct, IColorProfile<TTo, CieXyz>
33+
{
34+
ColorConversionOptions options = converter.Options;
35+
36+
// Convert to input PCS.
37+
using IMemoryOwner<Rgb> pcsFromOwner = options.MemoryAllocator.Allocate<Rgb>(source.Length);
38+
Span<Rgb> pcsFrom = pcsFromOwner.GetSpan();
39+
TFrom.ToProfileConnectionSpace(options, source, pcsFrom);
40+
41+
// Convert between PCS.
42+
using IMemoryOwner<CieXyz> pcsToOwner = options.MemoryAllocator.Allocate<CieXyz>(source.Length);
43+
Span<CieXyz> pcsTo = pcsToOwner.GetSpan();
44+
Rgb.ToProfileConnectionSpace(options, pcsFrom, pcsTo);
45+
46+
// Adapt to target white point
47+
VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, pcsTo, pcsTo);
48+
49+
// Convert to output from PCS
50+
TTo.FromProfileConnectionSpace(options, pcsTo, destination);
51+
}
52+
}

src/ImageSharp/ColorProfiles/Rgb.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
namespace SixLabors.ImageSharp.ColorProfiles;
99

10-
internal readonly struct Rgb : IProfileConnectingSpace<Rgb, CieXyz>
10+
/// <summary>
11+
/// Represents an RGB (red, green, blue) color profile.
12+
/// </summary>
13+
public readonly struct Rgb : IProfileConnectingSpace<Rgb, CieXyz>
1114
{
1215
/// <summary>
1316
/// Initializes a new instance of the <see cref="Rgb"/> struct.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Numerics;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace SixLabors.ImageSharp.ColorProfiles;
8+
9+
/// <summary>
10+
/// Represents an YCbCr (luminance, blue chroma, red chroma) color as defined in the ITU-T T.871 specification for the JFIF use with Jpeg.
11+
/// <see href="http://en.wikipedia.org/wiki/YCbCr"/>
12+
/// <see href="http://www.ijg.org/files/T-REC-T.871-201105-I!!PDF-E.pdf"/>
13+
/// </summary>
14+
public readonly struct YCbCr : IColorProfile<YCbCr, Rgb>
15+
{
16+
private static readonly Vector3 Min = Vector3.Zero;
17+
private static readonly Vector3 Max = new(255);
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="YCbCr"/> struct.
21+
/// </summary>
22+
/// <param name="y">The y luminance component.</param>
23+
/// <param name="cb">The cb chroma component.</param>
24+
/// <param name="cr">The cr chroma component.</param>
25+
[MethodImpl(InliningOptions.ShortMethod)]
26+
public YCbCr(float y, float cb, float cr)
27+
: this(new Vector3(y, cb, cr))
28+
{
29+
}
30+
31+
/// <summary>
32+
/// Initializes a new instance of the <see cref="YCbCr"/> struct.
33+
/// </summary>
34+
/// <param name="vector">The vector representing the y, cb, cr components.</param>
35+
[MethodImpl(InliningOptions.ShortMethod)]
36+
public YCbCr(Vector3 vector)
37+
{
38+
vector = Vector3.Clamp(vector, Min, Max);
39+
this.Y = vector.X;
40+
this.Cb = vector.Y;
41+
this.Cr = vector.Z;
42+
}
43+
44+
/// <summary>
45+
/// Gets the Y luminance component.
46+
/// <remarks>A value ranging between 0 and 255.</remarks>
47+
/// </summary>
48+
public readonly float Y { get; }
49+
50+
/// <summary>
51+
/// Gets the Cb chroma component.
52+
/// <remarks>A value ranging between 0 and 255.</remarks>
53+
/// </summary>
54+
public readonly float Cb { get; }
55+
56+
/// <summary>
57+
/// Gets the Cr chroma component.
58+
/// <remarks>A value ranging between 0 and 255.</remarks>
59+
/// </summary>
60+
public readonly float Cr { get; }
61+
62+
/// <summary>
63+
/// Compares two <see cref="YCbCr"/> objects for equality.
64+
/// </summary>
65+
/// <param name="left">The <see cref="YCbCr"/> on the left side of the operand.</param>
66+
/// <param name="right">The <see cref="YCbCr"/> on the right side of the operand.</param>
67+
/// <returns>
68+
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
69+
/// </returns>
70+
public static bool operator ==(YCbCr left, YCbCr right) => left.Equals(right);
71+
72+
/// <summary>
73+
/// Compares two <see cref="YCbCr"/> objects for inequality.
74+
/// </summary>
75+
/// <param name="left">The <see cref="YCbCr"/> on the left side of the operand.</param>
76+
/// <param name="right">The <see cref="YCbCr"/> on the right side of the operand.</param>
77+
/// <returns>
78+
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
79+
/// </returns>
80+
[MethodImpl(InliningOptions.ShortMethod)]
81+
public static bool operator !=(YCbCr left, YCbCr right) => !left.Equals(right);
82+
83+
/// <inheritdoc/>
84+
public static YCbCr FromProfileConnectingSpace(ColorConversionOptions options, in Rgb source)
85+
{
86+
Vector3 rgb = source.ToScaledVector3() * Max;
87+
float r = rgb.X;
88+
float g = rgb.Y;
89+
float b = rgb.Z;
90+
91+
float y = (0.299F * r) + (0.587F * g) + (0.114F * b);
92+
float cb = 128F + ((-0.168736F * r) - (0.331264F * g) + (0.5F * b));
93+
float cr = 128F + ((0.5F * r) - (0.418688F * g) - (0.081312F * b));
94+
95+
return new YCbCr(y, cb, cr);
96+
}
97+
98+
/// <inheritdoc/>
99+
public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Rgb> source, Span<YCbCr> destination)
100+
{
101+
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
102+
103+
// TODO: We can optimize this by using SIMD
104+
for (int i = 0; i < source.Length; i++)
105+
{
106+
Rgb rgb = source[i];
107+
destination[i] = FromProfileConnectingSpace(options, in rgb);
108+
}
109+
}
110+
111+
/// <inheritdoc/>
112+
public Rgb ToProfileConnectingSpace(ColorConversionOptions options)
113+
{
114+
float y = this.Y;
115+
float cb = this.Cb - 128F;
116+
float cr = this.Cr - 128F;
117+
118+
float r = MathF.Round(y + (1.402F * cr), MidpointRounding.AwayFromZero);
119+
float g = MathF.Round(y - (0.344136F * cb) - (0.714136F * cr), MidpointRounding.AwayFromZero);
120+
float b = MathF.Round(y + (1.772F * cb), MidpointRounding.AwayFromZero);
121+
122+
return Rgb.FromScaledVector3(new Vector3(r, g, b) / Max);
123+
}
124+
125+
/// <inheritdoc/>
126+
public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<YCbCr> source, Span<Rgb> destination)
127+
{
128+
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
129+
130+
// TODO: We can optimize this by using SIMD
131+
for (int i = 0; i < source.Length; i++)
132+
{
133+
YCbCr ycbcr = source[i];
134+
destination[i] = ycbcr.ToProfileConnectingSpace(options);
135+
}
136+
}
137+
138+
/// <inheritdoc/>
139+
public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource()
140+
=> ChromaticAdaptionWhitePointSource.RgbWorkingSpace;
141+
142+
/// <inheritdoc/>
143+
[MethodImpl(InliningOptions.ShortMethod)]
144+
public override int GetHashCode() => HashCode.Combine(this.Y, this.Cb, this.Cr);
145+
146+
/// <inheritdoc/>
147+
public override string ToString() => FormattableString.Invariant($"YCbCr({this.Y}, {this.Cb}, {this.Cr})");
148+
149+
/// <inheritdoc/>
150+
public override bool Equals(object? obj) => obj is YCbCr other && this.Equals(other);
151+
152+
/// <inheritdoc/>
153+
[MethodImpl(InliningOptions.ShortMethod)]
154+
public bool Equals(YCbCr other)
155+
=> this.Y.Equals(other.Y)
156+
&& this.Cb.Equals(other.Cb)
157+
&& this.Cr.Equals(other.Cr);
158+
}

tests/ImageSharp.Tests/ColorProfiles/ApproximateColorProfileComparer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
1414
IEqualityComparer<CieXyz>,
1515
IEqualityComparer<Lms>,
1616
IEqualityComparer<CieLch>,
17-
IEqualityComparer<Rgb>
17+
IEqualityComparer<Rgb>,
18+
IEqualityComparer<YCbCr>
1819
{
1920
private readonly float epsilon;
2021

@@ -34,6 +35,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
3435

3536
public bool Equals(Rgb x, Rgb y) => this.Equals(x.R, y.R) && this.Equals(x.G, y.G) && this.Equals(x.B, y.B);
3637

38+
public bool Equals(YCbCr x, YCbCr y) => this.Equals(x.Y, y.Y) && this.Equals(x.Cb, y.Cb) && this.Equals(x.Cr, y.Cr);
39+
3740
public int GetHashCode([DisallowNull] CieLab obj) => obj.GetHashCode();
3841

3942
public int GetHashCode([DisallowNull] CieXyz obj) => obj.GetHashCode();
@@ -44,6 +47,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
4447

4548
public int GetHashCode([DisallowNull] Rgb obj) => obj.GetHashCode();
4649

50+
public int GetHashCode([DisallowNull] YCbCr obj) => obj.GetHashCode();
51+
4752
private bool Equals(float x, float y)
4853
{
4954
float d = x - y;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.ColorProfiles;
5+
6+
namespace SixLabors.ImageSharp.Tests.ColorProfiles.Conversion;
7+
8+
/// <summary>
9+
/// Tests <see cref="Rgb"/>-<see cref="YCbCr"/> conversions.
10+
/// </summary>
11+
/// <remarks>
12+
/// Test data generated mathematically
13+
/// </remarks>
14+
public class RgbAndYCbCrConversionTest
15+
{
16+
private static readonly ApproximateColorProfileComparer Comparer = new(.001F);
17+
18+
[Theory]
19+
[InlineData(255, 128, 128, 1, 1, 1)]
20+
[InlineData(0, 128, 128, 0, 0, 0)]
21+
[InlineData(128, 128, 128, 0.502, 0.502, 0.502)]
22+
public void Convert_YCbCr_To_Rgb(float y, float cb, float cr, float r, float g, float b)
23+
{
24+
// Arrange
25+
YCbCr input = new(y, cb, cr);
26+
Rgb expected = new(r, g, b);
27+
ColorProfileConverter converter = new();
28+
29+
Span<YCbCr> inputSpan = new YCbCr[5];
30+
inputSpan.Fill(input);
31+
32+
Span<Rgb> actualSpan = new Rgb[5];
33+
34+
// Act
35+
Rgb actual = converter.Convert<YCbCr, Rgb>(input);
36+
converter.Convert<YCbCr, Rgb>(inputSpan, actualSpan);
37+
38+
// Assert
39+
Assert.Equal(expected, actual, Comparer);
40+
41+
for (int i = 0; i < actualSpan.Length; i++)
42+
{
43+
Assert.Equal(expected, actualSpan[i], Comparer);
44+
}
45+
}
46+
47+
[Theory]
48+
[InlineData(0, 0, 0, 0, 128, 128)]
49+
[InlineData(1, 1, 1, 255, 128, 128)]
50+
[InlineData(0.5, 0.5, 0.5, 127.5, 128, 128)]
51+
[InlineData(1, 0, 0, 76.245, 84.972, 255)]
52+
public void Convert_Rgb_To_YCbCr(float r, float g, float b, float y, float cb, float cr)
53+
{
54+
// Arrange
55+
Rgb input = new(r, g, b);
56+
YCbCr expected = new(y, cb, cr);
57+
ColorProfileConverter converter = new();
58+
59+
Span<Rgb> inputSpan = new Rgb[5];
60+
inputSpan.Fill(input);
61+
62+
Span<YCbCr> actualSpan = new YCbCr[5];
63+
64+
// Act
65+
YCbCr actual = converter.Convert<Rgb, YCbCr>(input);
66+
converter.Convert<Rgb, YCbCr>(inputSpan, actualSpan);
67+
68+
// Assert
69+
Assert.Equal(expected, actual, Comparer);
70+
71+
for (int i = 0; i < actualSpan.Length; i++)
72+
{
73+
Assert.Equal(expected, actualSpan[i], Comparer);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)