diff --git a/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/Calculators/ClutCalculator.cs b/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/Calculators/ClutCalculator.cs index a1891d127..e3742bcc3 100644 --- a/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/Calculators/ClutCalculator.cs +++ b/src/ImageSharp/ColorSpaces/Conversion/Implementation/Icc/Calculators/ClutCalculator.cs @@ -14,6 +14,7 @@ internal class ClutCalculator : IVector4Calculator private readonly byte[] gridPointCount; private readonly int[] indexFactor; private readonly int nodeCount; + private readonly float[][] nodes; public ClutCalculator(IccClut clut) { @@ -25,6 +26,7 @@ internal class ClutCalculator : IVector4Calculator this.gridPointCount = clut.GridPointCount; this.indexFactor = CalculateIndexFactor(clut.InputChannelCount, clut.GridPointCount); this.nodeCount = (int)Math.Pow(2, clut.InputChannelCount); + this.nodes = new float[this.nodeCount][]; } public unsafe Vector4 Calculate(Vector4 value) @@ -50,8 +52,7 @@ internal class ClutCalculator : IVector4Calculator private unsafe void Interpolate(float* values, int valueLength, float* result, int resultLength) { - float[][] nodes = new float[this.nodeCount][]; - for (int i = 0; i < nodes.Length; i++) + for (int i = 0; i < this.nodes.Length; i++) { int index = 0; for (int j = 0; j < valueLength; j++) @@ -61,7 +62,9 @@ internal class ClutCalculator : IVector4Calculator index += (int)((this.indexFactor[j] * (position * fraction)) + 0.5f); } - nodes[i] = this.lut[index]; + // TODO: The CMYK profile in our tests causes an out of range exception + // against 'lut'. Clamping the index leads to incorrect results. + this.nodes[i] = this.lut[index]; } Span factors = stackalloc float[this.nodeCount]; @@ -92,9 +95,9 @@ internal class ClutCalculator : IVector4Calculator for (int i = 0; i < resultLength; i++) { - for (int j = 0; j < nodes.Length; j++) + for (int j = 0; j < this.nodes.Length; j++) { - result[i] += nodes[j][i] * factors[j]; + result[i] += this.nodes[j][i] * factors[j]; } } } diff --git a/tests/ImageSharp.Tests/Colorspaces/Icc/IccProfileConverterTests.cs b/tests/ImageSharp.Tests/Colorspaces/Icc/IccProfileConverterTests.cs index a2e8cbc2d..2153f1420 100644 --- a/tests/ImageSharp.Tests/Colorspaces/Icc/IccProfileConverterTests.cs +++ b/tests/ImageSharp.Tests/Colorspaces/Icc/IccProfileConverterTests.cs @@ -20,6 +20,7 @@ public class IccProfileConverterTests [WithFile(TestImages.Jpeg.ICC.WideRGB, PixelTypes.Rgb24)] [WithFile(TestImages.Jpeg.ICC.SRgb, PixelTypes.Rgb24)] [WithFile(TestImages.Jpeg.ICC.ProPhoto, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.ICC.CMYK, PixelTypes.Rgb24)] public void CanRoundTripProfile(TestImageProvider provider) where TPixel : unmanaged, IPixel { @@ -45,6 +46,7 @@ public class IccProfileConverterTests [WithFile(TestImages.Jpeg.ICC.WideRGB, PixelTypes.Rgb24)] [WithFile(TestImages.Jpeg.ICC.SRgb, PixelTypes.Rgb24)] [WithFile(TestImages.Jpeg.ICC.ProPhoto, PixelTypes.Rgb24)] + [WithFile(TestImages.Jpeg.ICC.CMYK, PixelTypes.Rgb24)] public void CanConvertToSRGB(TestImageProvider provider) where TPixel : unmanaged, IPixel { diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index f114e862a..0129c5850 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -176,6 +176,7 @@ public static class TestImages public const string ProPhoto = "Jpg/icc-profiles/Momiji-ProPhoto-yes.jpg"; public const string WideRGB = "Jpg/icc-profiles/Momiji-WideRGB-yes.jpg"; public const string AppleRGB = "Jpg/icc-profiles/Momiji-AppleRGB-yes.jpg"; + public const string CMYK = "Jpg/icc-profiles/issue-129.jpg"; } public static class Progressive diff --git a/tests/Images/Input/Jpg/icc-profiles/issue-129.jpg b/tests/Images/Input/Jpg/icc-profiles/issue-129.jpg new file mode 100644 index 000000000..98949f43f --- /dev/null +++ b/tests/Images/Input/Jpg/icc-profiles/issue-129.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e1728dd548d862ef3f960c82528716e0ad1b8eb0119a5eed4dfde51026d7bb74 +size 2903429