Skip to content

Commit cb71ae2

Browse files
Complete Rgb conversion
1 parent 66bbc6a commit cb71ae2

11 files changed

Lines changed: 202 additions & 43 deletions

src/ImageSharp/ColorProfiles/ColorProfileConverterExtensionsCieLabCieXyz.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static TTo Convert<TFrom, TTo>(this ColorProfileConverter converter, TFro
2121
CieXyz pcsTo = pcsFrom.ToProfileConnectingSpace(options);
2222

2323
// Adapt to target white point
24-
VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsTo);
24+
pcsTo = VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsTo);
2525

2626
// Convert to output from PCS
2727
return TTo.FromProfileConnectingSpace(options, pcsTo);

src/ImageSharp/ColorProfiles/ColorProfileConverterExtensionsCieXyzCieLab.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static TTo Convert<TFrom, TTo>(this ColorProfileConverter converter, TFro
1818
CieXyz pcsFrom = source.ToProfileConnectingSpace(options);
1919

2020
// Adapt to target white point
21-
VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsFrom);
21+
pcsFrom = VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsFrom);
2222

2323
// Convert between PCS
2424
CieLab pcsTo = CieLab.FromProfileConnectingSpace(options, in pcsFrom);

src/ImageSharp/ColorProfiles/ColorProfileConverterExtensionsCieXyzCieXyz.cs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ public static TTo Convert<TFrom, TTo>(this ColorProfileConverter converter, TFro
1818
CieXyz pcsFrom = source.ToProfileConnectingSpace(options);
1919

2020
// Adapt to target white point
21-
VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsFrom);
22-
23-
// Convert between PCS
24-
CieXyz pcsTo = CieXyz.FromProfileConnectingSpace(options, in pcsFrom);
21+
pcsFrom = VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, in pcsFrom);
2522

2623
// Convert to output from PCS
27-
return TTo.FromProfileConnectingSpace(options, pcsTo);
24+
return TTo.FromProfileConnectingSpace(options, pcsFrom);
2825
}
2926

3027
public static void Convert<TFrom, TTo>(this ColorProfileConverter converter, ReadOnlySpan<TFrom> source, Span<TTo> destination)
@@ -41,12 +38,7 @@ public static void Convert<TFrom, TTo>(this ColorProfileConverter converter, Rea
4138
// Adapt to target white point
4239
VonKriesChromaticAdaptation.Transform<TFrom, TTo>(options, pcsFrom, pcsFrom);
4340

44-
// Convert between PCS.
45-
using IMemoryOwner<CieXyz> pcsToOwner = options.MemoryAllocator.Allocate<CieXyz>(source.Length);
46-
Span<CieXyz> pcsTo = pcsToOwner.GetSpan();
47-
CieXyz.FromProfileConnectionSpace(options, pcsFrom, pcsTo);
48-
4941
// Convert to output from PCS
50-
TTo.FromProfileConnectionSpace(options, pcsTo, destination);
42+
TTo.FromProfileConnectionSpace(options, pcsFrom, destination);
5143
}
5244
}

src/ImageSharp/ColorProfiles/Companding/CompandingUtilities.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,28 @@ public static class CompandingUtilities
1717
{
1818
private const int Length = Scale + 2; // 256kb @ 16bit precision.
1919
private const int Scale = (1 << 16) - 1;
20-
private static readonly ConcurrentDictionary<(Type, double), Lazy<float[]>> LookupTables = new();
20+
private static readonly ConcurrentDictionary<(Type, double), float[]> CompressLookupTables = new();
21+
private static readonly ConcurrentDictionary<(Type, double), float[]> ExpandLookupTables = new();
2122

2223
/// <summary>
23-
/// Lazily creates and stores a companding lookup table using the given function and modifier.
24+
/// Lazily creates and stores a companding compression lookup table using the given function and modifier.
2425
/// </summary>
2526
/// <typeparam name="T">The type of companding function.</typeparam>
2627
/// <param name="compandingFunction">The companding function.</param>
2728
/// <param name="modifier">A modifier to pass to the function.</param>
2829
/// <returns>The <see cref="float"/> array.</returns>
29-
public static Lazy<float[]> GetLookupTable<T>(Func<double, double, double> compandingFunction, double modifier = 0)
30-
=> LookupTables.GetOrAdd((typeof(T), modifier), args => new(() => CreateLookupTableImpl(compandingFunction, args.Item2), true));
30+
public static float[] GetCompressLookupTable<T>(Func<double, double, double> compandingFunction, double modifier = 0)
31+
=> CompressLookupTables.GetOrAdd((typeof(T), modifier), args => CreateLookupTableImpl(compandingFunction, args.Item2));
32+
33+
/// <summary>
34+
/// Lazily creates and stores a companding expanding lookup table using the given function and modifier.
35+
/// </summary>
36+
/// <typeparam name="T">The type of companding function.</typeparam>
37+
/// <param name="compandingFunction">The companding function.</param>
38+
/// <param name="modifier">A modifier to pass to the function.</param>
39+
/// <returns>The <see cref="float"/> array.</returns>
40+
public static float[] GetExpandLookupTable<T>(Func<double, double, double> compandingFunction, double modifier = 0)
41+
=> ExpandLookupTables.GetOrAdd((typeof(T), modifier), args => CreateLookupTableImpl(compandingFunction, args.Item2));
3142

3243
/// <summary>
3344
/// Creates a companding lookup table using the given function.

src/ImageSharp/ColorProfiles/Companding/GammaCompanding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public static class GammaCompanding
2424
/// <param name="vectors">The span of vectors.</param>
2525
/// <param name="gamma">The gamma value.</param>
2626
public static void Compress(Span<Vector4> vectors, double gamma)
27-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<GammaCompandingKey>(CompressFunction, gamma).Value);
27+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetCompressLookupTable<GammaCompandingKey>(CompressFunction, gamma));
2828

2929
/// <summary>
3030
/// Expands the nonlinear vectors to their linear equivalents with respect to the energy.
3131
/// </summary>
3232
/// <param name="vectors">The span of vectors.</param>
3333
/// <param name="gamma">The gamma value.</param>
3434
public static void Expand(Span<Vector4> vectors, double gamma)
35-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<GammaCompandingKey>(ExpandFunction, gamma).Value);
35+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetExpandLookupTable<GammaCompandingKey>(ExpandFunction, gamma));
3636

3737
/// <summary>
3838
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
@@ -41,16 +41,16 @@ public static void Expand(Span<Vector4> vectors, double gamma)
4141
/// <param name="gamma">The gamma value.</param>
4242
/// <returns>The <see cref="Vector4"/>.</returns>
4343
public static Vector4 Compress(Vector4 vector, double gamma)
44-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<GammaCompandingKey>(CompressFunction, gamma).Value);
44+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetCompressLookupTable<GammaCompandingKey>(CompressFunction, gamma));
4545

4646
/// <summary>
47-
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
47+
/// Expands the nonlinear vector to its linear equivalent with respect to the energy.
4848
/// </summary>
4949
/// <param name="vector">The vector.</param>
5050
/// <param name="gamma">The gamma value.</param>
5151
/// <returns>The <see cref="Vector4"/>.</returns>
5252
public static Vector4 Expand(Vector4 vector, double gamma)
53-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<GammaCompandingKey>(ExpandFunction, gamma).Value);
53+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetExpandLookupTable<GammaCompandingKey>(ExpandFunction, gamma));
5454

5555
private class GammaCompandingKey;
5656
}

src/ImageSharp/ColorProfiles/Companding/LCompanding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ private static Func<double, double, double> ExpandFunction
4242
/// </summary>
4343
/// <param name="vectors">The span of vectors.</param>
4444
public static void Compress(Span<Vector4> vectors)
45-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<LCompandingKey>(CompressFunction).Value);
45+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetCompressLookupTable<LCompandingKey>(CompressFunction));
4646

4747
/// <summary>
4848
/// Expands the nonlinear vectors to their linear equivalents with respect to the energy.
4949
/// </summary>
5050
/// <param name="vectors">The span of vectors.</param>
5151
public static void Expand(Span<Vector4> vectors)
52-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<LCompandingKey>(ExpandFunction).Value);
52+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetExpandLookupTable<LCompandingKey>(ExpandFunction));
5353

5454
/// <summary>
5555
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
5656
/// </summary>
5757
/// <param name="vector">The vector.</param>
5858
/// <returns>The <see cref="Vector4"/>.</returns>
5959
public static Vector4 Compress(Vector4 vector)
60-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<LCompandingKey>(CompressFunction).Value);
60+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetCompressLookupTable<LCompandingKey>(CompressFunction));
6161

6262
/// <summary>
63-
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
63+
/// Expands the nonlinear vector to its linear equivalent with respect to the energy.
6464
/// </summary>
6565
/// <param name="vector">The vector.</param>
6666
/// <returns>The <see cref="Vector4"/>.</returns>
6767
public static Vector4 Expand(Vector4 vector)
68-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<LCompandingKey>(ExpandFunction).Value);
68+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetExpandLookupTable<LCompandingKey>(ExpandFunction));
6969

7070
private class LCompandingKey;
7171
}

src/ImageSharp/ColorProfiles/Companding/Rec2020Companding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,30 @@ private static Func<double, double, double> ExpandFunction
4646
/// </summary>
4747
/// <param name="vectors">The span of vectors.</param>
4848
public static void Compress(Span<Vector4> vectors)
49-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(CompressFunction).Value);
49+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetCompressLookupTable<Rec2020CompandingKey>(CompressFunction));
5050

5151
/// <summary>
5252
/// Expands the nonlinear vectors to their linear equivalents with respect to the energy.
5353
/// </summary>
5454
/// <param name="vectors">The span of vectors.</param>
5555
public static void Expand(Span<Vector4> vectors)
56-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(ExpandFunction).Value);
56+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetExpandLookupTable<Rec2020CompandingKey>(ExpandFunction));
5757

5858
/// <summary>
5959
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
6060
/// </summary>
6161
/// <param name="vector">The vector.</param>
6262
/// <returns>The <see cref="Vector4"/>.</returns>
6363
public static Vector4 Compress(Vector4 vector)
64-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(CompressFunction).Value);
64+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetCompressLookupTable<Rec2020CompandingKey>(CompressFunction));
6565

6666
/// <summary>
67-
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
67+
/// Expands the nonlinear vector to its linear equivalent with respect to the energy.
6868
/// </summary>
6969
/// <param name="vector">The vector.</param>
7070
/// <returns>The <see cref="Vector4"/>.</returns>
7171
public static Vector4 Expand(Vector4 vector)
72-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(ExpandFunction).Value);
72+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetExpandLookupTable<Rec2020CompandingKey>(ExpandFunction));
7373

7474
private class Rec2020CompandingKey;
7575
}

src/ImageSharp/ColorProfiles/Companding/Rec709Companding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ private static Func<double, double, double> ExpandFunction
4242
/// </summary>
4343
/// <param name="vectors">The span of vectors.</param>
4444
public static void Compress(Span<Vector4> vectors)
45-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(CompressFunction).Value);
45+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetCompressLookupTable<Rec2020CompandingKey>(CompressFunction));
4646

4747
/// <summary>
4848
/// Expands the nonlinear vectors to their linear equivalents with respect to the energy.
4949
/// </summary>
5050
/// <param name="vectors">The span of vectors.</param>
5151
public static void Expand(Span<Vector4> vectors)
52-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(ExpandFunction).Value);
52+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetExpandLookupTable<Rec2020CompandingKey>(ExpandFunction));
5353

5454
/// <summary>
5555
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
5656
/// </summary>
5757
/// <param name="vector">The vector.</param>
5858
/// <returns>The <see cref="Vector4"/>.</returns>
5959
public static Vector4 Compress(Vector4 vector)
60-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(CompressFunction).Value);
60+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetCompressLookupTable<Rec2020CompandingKey>(CompressFunction));
6161

6262
/// <summary>
63-
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
63+
/// Expands the nonlinear vector to its linear equivalent with respect to the energy.
6464
/// </summary>
6565
/// <param name="vector">The vector.</param>
6666
/// <returns>The <see cref="Vector4"/>.</returns>
6767
public static Vector4 Expand(Vector4 vector)
68-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<Rec2020CompandingKey>(ExpandFunction).Value);
68+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetExpandLookupTable<Rec2020CompandingKey>(ExpandFunction));
6969

7070
private class Rec2020CompandingKey;
7171
}

src/ImageSharp/ColorProfiles/Companding/SRgbCompanding.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ private static Func<double, double, double> ExpandFunction
4242
/// </summary>
4343
/// <param name="vectors">The span of vectors.</param>
4444
public static void Compress(Span<Vector4> vectors)
45-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<SRgbCompandingKey>(CompressFunction).Value);
45+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetCompressLookupTable<SRgbCompandingKey>(CompressFunction));
4646

4747
/// <summary>
4848
/// Expands the nonlinear vectors to their linear equivalents with respect to the energy.
4949
/// </summary>
5050
/// <param name="vectors">The span of vectors.</param>
5151
public static void Expand(Span<Vector4> vectors)
52-
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetLookupTable<SRgbCompandingKey>(ExpandFunction).Value);
52+
=> CompandingUtilities.Compand(vectors, CompandingUtilities.GetExpandLookupTable<SRgbCompandingKey>(ExpandFunction));
5353

5454
/// <summary>
5555
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
5656
/// </summary>
5757
/// <param name="vector">The vector.</param>
5858
/// <returns>The <see cref="Vector4"/>.</returns>
5959
public static Vector4 Compress(Vector4 vector)
60-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<SRgbCompandingKey>(CompressFunction).Value);
60+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetCompressLookupTable<SRgbCompandingKey>(CompressFunction));
6161

6262
/// <summary>
63-
/// Compresses the linear vector to its nonlinear equivalent with respect to the energy.
63+
/// Expands the nonlinear vector to its linear equivalent with respect to the energy.
6464
/// </summary>
6565
/// <param name="vector">The vector.</param>
6666
/// <returns>The <see cref="Vector4"/>.</returns>
6767
public static Vector4 Expand(Vector4 vector)
68-
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetLookupTable<SRgbCompandingKey>(ExpandFunction).Value);
68+
=> CompandingUtilities.Compand(vector, CompandingUtilities.GetExpandLookupTable<SRgbCompandingKey>(ExpandFunction));
6969

7070
private class SRgbCompandingKey;
7171
}

tests/ImageSharp.Tests/ColorProfiles/ApproximateColorProfileComparer.cs

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

@@ -31,6 +32,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
3132

3233
public bool Equals(CieLch x, CieLch y) => this.Equals(x.L, y.L) && this.Equals(x.C, y.C) && this.Equals(x.H, y.H);
3334

35+
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);
36+
3437
public int GetHashCode([DisallowNull] CieLab obj) => obj.GetHashCode();
3538

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

4043
public int GetHashCode([DisallowNull] CieLch obj) => obj.GetHashCode();
4144

45+
public int GetHashCode([DisallowNull] Rgb obj) => obj.GetHashCode();
46+
4247
private bool Equals(float x, float y)
4348
{
4449
float d = x - y;

0 commit comments

Comments
 (0)