From 3389d7af8f8c15ece19dba56b05945b8716c735a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 9 Nov 2023 21:21:08 +1000 Subject: [PATCH] Add some debugging helpers to the converter --- .../Implementation/Icc/IccProfileConverter.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/IccProfileConverter.cs b/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/IccProfileConverter.cs index 2c11b0bc43..671e9aa4c0 100644 --- a/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/IccProfileConverter.cs +++ b/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/IccProfileConverter.cs @@ -47,8 +47,15 @@ internal static class IccProfileConverter WhitePoint = new(illuminant), }); + // TODO: Our Xxy/Lab conversion are dependent on the version number. We are applying the conversion using V4 + // but we should use the correct algorithm per version. This includes Lab/Lab Xyz/Xyz. using IMemoryOwner vectors = configuration.MemoryAllocator.Allocate(accessor.Width); Span vectorsSpan = vectors.GetSpan(); + + // TODO: For debugging - remove. + // It appears we have a scaling problem. The pcs values differ by on average 0.000001. + Span temp = new Vector4[vectorsSpan.Length]; + for (int y = 0; y < accessor.Height; y++) { Span row = accessor.GetRowSpan(y); @@ -60,9 +67,10 @@ internal static class IccProfileConverter for (int x = 0; x < vectorsSpan.Length; x++) { Vector4 pcs = converterDataToPcs.Calculate(vectorsSpan[x]); + temp[x] = pcs; pcs = PcsToLab(pcs); CieXyz xyz = converter.ToCieXyz(new CieLab(pcs.X, pcs.Y, pcs.Z, new CieXyz(inputIccProfile.Header.PcsIlluminant))); - pcs = new Vector4(xyz.X, xyz.Y, xyz.Z, pcs.W); + pcs = XyzToPcs(pcs, xyz); vectorsSpan[x] = converterPcsToData.Calculate(pcs); } @@ -97,7 +105,7 @@ internal static class IccProfileConverter private static unsafe Vector4 PcsToLab(Vector4 input) { Vector3* v = (Vector3*)&input; - v[0] *= 100F; + v[0] *= new Vector3(100f, 255, 255); v[0] -= new Vector3(0, 128F, 128F); return input; } @@ -110,6 +118,13 @@ internal static class IccProfileConverter return input; } + private static unsafe Vector4 XyzToPcs(Vector4 input, CieXyz xyz) + { + Vector3* v = (Vector3*)&input; + v[0] *= 32768 / 65535f; + return input; + } + private readonly struct IccProfileConverterVisitor : IImageVisitor { private readonly IccProfile inputIccProfile;