Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color conversion with ICC profiles #1567

Draft
wants to merge 94 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
16179b3
Copy original PR changes.
JimBobSquarePants Feb 27, 2021
eeb40f5
Merge branch 'master' into icc-color-conversion
JimBobSquarePants Apr 23, 2021
8b96d37
Merge branch 'master' into icc-color-conversion
JimBobSquarePants May 22, 2021
aa2f1e9
Merge branch 'master' into icc-color-conversion
JimBobSquarePants Jul 7, 2021
3debb26
Merge branch 'master' into icc-color-conversion
brianpopow Jul 12, 2021
0812085
Fix warnings
brianpopow Jul 12, 2021
1094dc6
Add color conversion trait to be able to filter for those tests
brianpopow Jul 12, 2021
759f053
Fix failing MatrixCalculator test
brianpopow Jul 13, 2021
4cde4ef
Merge branch 'master' into icc-color-conversion
JimBobSquarePants Nov 9, 2021
a973713
Merge branch 'master' into icc-color-conversion
JimBobSquarePants Nov 11, 2021
33339d0
Merge branch 'master' into icc-color-conversion
JimBobSquarePants Jan 18, 2022
cdb495c
Merge branch 'master' into icc-color-conversion
JimBobSquarePants Jan 30, 2022
5f7acc1
Merge remote-tracking branch 'origin/main' into icc-color-conversion
brianpopow May 15, 2022
7e2bc20
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Nov 23, 2022
dc166a7
Fix build
JimBobSquarePants Nov 23, 2022
dcc8147
Cleanup icc tests
brianpopow Nov 24, 2022
73c8d8f
Fix Copyright notice
brianpopow Nov 24, 2022
d229fed
Fix icc namespaces
brianpopow Nov 24, 2022
0a08da0
Use file scoped namespaces
brianpopow Nov 27, 2022
daf366b
Cleanup and add conversion tests
JimBobSquarePants Nov 30, 2022
54856ff
Fix reader and out of range exception
JimBobSquarePants Dec 4, 2022
eee14c6
Remove invalid test.
JimBobSquarePants Dec 4, 2022
f60d4b8
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Dec 14, 2022
8de137e
Cleanup code style
JimBobSquarePants Dec 15, 2022
fb8003c
Remove double clamping
JimBobSquarePants Dec 15, 2022
9f0f9cb
Optimize matrix read/write
JimBobSquarePants Dec 15, 2022
ece11eb
Create ColorProfileHandling.cs
JimBobSquarePants Dec 17, 2022
bd7257b
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Dec 17, 2022
9a21485
Nullable disable
JimBobSquarePants Dec 17, 2022
ba76964
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Dec 18, 2022
98d1758
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jan 2, 2023
66554cb
Add ability to convert ICC profile on decode
JimBobSquarePants Jan 4, 2023
e90f165
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jan 4, 2023
8c580a7
Handle nullability in decoder base
JimBobSquarePants Jan 5, 2023
a81dac9
Add reference files.
JimBobSquarePants Jan 9, 2023
902ed99
Add tolerance for Mac
JimBobSquarePants Jan 9, 2023
e3aa452
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jan 10, 2023
0dd68fe
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jan 15, 2023
6e3dc81
Update decoder bases following merge
JimBobSquarePants Jan 15, 2023
b7833a4
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jan 16, 2023
c3984aa
Add failing tests
JimBobSquarePants Jan 16, 2023
0fff06d
Port 1d, 2d, 3d, 4d and nd interpolation from reference implementation
brianpopow Jan 22, 2023
f1c05ee
Remove not used IccClut constructors
brianpopow Jan 22, 2023
3be31c3
Preserve alpha component
JimBobSquarePants Jan 22, 2023
6c2ee90
Fix CieLab docs
JimBobSquarePants Jan 22, 2023
20e9b7f
Fix out of bounds error
brianpopow Jan 22, 2023
d6fbc01
Change clut values from jagged array to flat array
brianpopow Jan 22, 2023
67ed4ce
Fix warnings
brianpopow Jan 22, 2023
b036cc3
Fix mistake reading the clut values
brianpopow Jan 22, 2023
52f88c8
Fix oob in n-dimension calculator.
JimBobSquarePants Jan 23, 2023
ed47678
Add Lab<=>Xyz conversion
JimBobSquarePants Jan 23, 2023
10bea86
Add ICC reader tests
brianpopow Jan 23, 2023
5b131ad
Add reference output for issue-129
brianpopow Feb 3, 2023
ed8091b
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Feb 3, 2023
6225db3
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Feb 9, 2023
a65c599
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Mar 30, 2023
60f3d9d
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Sep 2, 2023
c940b86
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Sep 25, 2023
a567613
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Nov 1, 2023
3e06687
Update IccReader.cs
JimBobSquarePants Nov 1, 2023
c1ebbfe
Fix build
JimBobSquarePants Nov 1, 2023
d89d8c5
Update IccReader.cs
JimBobSquarePants Nov 1, 2023
63c89ca
Use scaled Vector4 conversion and optimize
JimBobSquarePants Nov 1, 2023
3389d7a
Add some debugging helpers to the converter
JimBobSquarePants Nov 9, 2023
7b0ff3b
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jun 11, 2024
29ed2b4
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jul 9, 2024
5f975e5
Update to latest main build
JimBobSquarePants Jul 9, 2024
79f5dfa
Update IccProfileConverterTests.cs
JimBobSquarePants Jul 9, 2024
b88b2a9
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Jul 10, 2024
bca4cad
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Aug 12, 2024
6e2c29c
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Dec 2, 2024
5b374da
Fix IccClut test setup and calculator guards
JimBobSquarePants Dec 3, 2024
441f07e
Update ColorProfileConverter to handle ICCProfiles
JimBobSquarePants Dec 3, 2024
6654218
Suppress warning
JimBobSquarePants Dec 3, 2024
21fec4e
Demonstrate ICC conversion comparison to Unicolour
waacton Dec 6, 2024
fd2e8a9
Migrate tests
JimBobSquarePants Dec 10, 2024
19ff69d
Add ICC files to LFS
JimBobSquarePants Dec 10, 2024
3cd7d67
Update TestIccProfiles.cs
JimBobSquarePants Dec 10, 2024
0327dca
Adjust PCS values for v2 profiles using perceptual intent
waacton Dec 10, 2024
9de3935
Remove TODO
waacton Dec 10, 2024
8e92f20
Fix XYZ PCS conversions
waacton Dec 10, 2024
312b55e
Cleanup
JimBobSquarePants Dec 11, 2024
44aae40
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Dec 11, 2024
df3d230
Extract conversion for v2 perceptual intent
waacton Dec 11, 2024
d20fddb
Precalculate v2 perceptual PCS adjustment
waacton Dec 12, 2024
d60ac76
Bypass PCS adjustment when not needed
waacton Dec 14, 2024
cfa2760
Add failing tests for CMYK to RGB using Matrix TRC
waacton Dec 14, 2024
369bf5f
Fix CMYK to RGB using TRCs
waacton Dec 14, 2024
7d4a742
Add RGB to CMYK tests and fix TRC calculator
waacton Dec 15, 2024
f4e9509
Handle tests in cases where PCS adjustment is bypassed
waacton Dec 16, 2024
9ceed23
Fix expected values of CLUT unit tests
waacton Dec 17, 2024
f21c0c2
Fix LUT entry calculator for XYZ PCS with non-identity matrix
waacton Dec 17, 2024
f147aad
Minor cleanup
JimBobSquarePants Dec 18, 2024
7492109
Merge branch 'main' into icc-color-conversion
JimBobSquarePants Dec 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Vector4 Calculate(Vector4 value)
{
if (this.doTransform)
{
value = Vector4.Transform(value, this.matrix);
value = Vector4.Transform(value, Matrix4x4.Transpose(this.matrix));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems Vector4.Transform was performing Vector × Matrix not Matrix × Vector? My matrix maths is rusty, but in lieu of some kind of Matrix4x4.Transform(matrix, vector) function it sounds like transposing the matrix has the same effect as changing the order of multiplication.

This issue wasn't caught yet because none of the ICC conversion tests use a profile of CMYK data ⇒ XYZ PCS. And even if it did, it would only get caught if using a non-identity matrix? As always, I'll be surprised if we can find a profile that meets these conditions, though we can include it in testing if we do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I'm not sure. @saucecontrol could I borrow your brain here please?

Copy link
Contributor

@saucecontrol saucecontrol Dec 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vector4.Transform is a dot (inner) product, which is commutative between a vector and matrix1.

I can work up some profiles to test some of these edge cases if you'd like. I swear one of these days I'm going to get around to finishing my implementation of LUT profile transforms, and I'll need them then anyway.

Footnotes

  1. Well, not in general, but in this case it's a square matrix and a vector with length equal to the length of each matrix dimension, so it is here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we want a matrix multiplication M×V which isn't commutative, but doing V⋅MT effectively calculates M×V in this particular case.

Is that an OK assumption to make given we're restricted to Matrix4x4 and Vector4? Is there a more suitable System.Numerics way to do this?

Copy link
Contributor

@saucecontrol saucecontrol Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want M×V, you want M⋅V, and I promise it's commutative.

The reason transposing gives you the correct result is that Vector4.Transform assumes the matrix is row-major, i.e. the V is a 1x4 matrix. Most of ICC docs describe the matrices as column-major, which would mean your V is a 4x1 matrix.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also see from the implementation of Vector4.Transform why it's done row-major -- that enables SIMD acceleration per row because of the memory layout. For perf reasons, you'll want to normalize to row major on the edges of the API.

Copy link
Collaborator

@waacton waacton Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't want M×V, you want M⋅V

I think some of my assumptions have muddied the waters a bit, so I'm going to try to summarise, and if I'm still making a mess I'll have to leave this to someone else.

  • Until now I've basically ignored that the vector is a 1x4 matrix, and I'm sure I've confused things by using V to mean "the values of the vector" as opposed to "a 1x4 matrix"
  • In terms of the maths I want to perform, I want to multiply a 4x4 matrix with a 4x1 matrix (what I was referring to as not commutative), and there's no in-built function for that?
  • In terms of the implementation, it sounds like we want to perform a dot product of a 4x4 matrix with a 1x4 matrix (which is commutative) for SIMD reasons
  • Since I'm stuck with a row-major 1x4 matrix, I need to make the 4x4 matrix row-major for the dot product calculation
  • I'm not experienced enough with System.Numerics to know if there's a more conventional way to "achieve the effect of matrix multiplication" than transposing the 4x4 matrix - @JimBobSquarePants perhaps any 4x4 matrices just need to be initialised row-major?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of the maths I want to perform, I want to multiply a 4x4 matrix with a 4x1 matrix (what I was referring to as not commutative), and there's no in-built function for that?

This is also commutative, as it is also a dot product. The only difference is whether you compute the dot product of the vector with each row of the matrix or with each column of the matrix.

There is no fast way to compute it column-wise, because it means gathering the non-adjacent values in memory. In fact, transposing the matrix with SIMD and then performing a row-wise dot product with SIMD is the fast way to do this, although it is much less fast than normalizing your matrix to row-major when reading the profile and skipping the transpose step during processing.

perhaps any 4x4 matrices just need to be initialised row-major?

Exactly

Copy link
Contributor

@saucecontrol saucecontrol Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure the maths explanation is clear:

M⋅V where M is 4x4 can be summarized as VMatrix1⋅VColor + VMatrix2⋅VColor + VMatrix3⋅VColor + VMatrix4⋅VColor where VMatrix represents either the rows (VColor is 1x4) or columns (VColor is 4x1) of the matrix.

That result is the same as V⋅M, which would be VColor⋅VMatrix1 + VColor⋅VMatrix2 + VColor⋅VMatrix3 + VColor⋅VMatrix4, because V⋅V is commutative.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'm referring to matrix multiplication in general.

I'm not sure how feasible initialising row-major is when we also need the inverse of the matrix for reverse transforms, will see.

}

value = CalculateLut(this.inputCurve, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles.Icc.Calculators
[Trait("Color", "Conversion")]
public class LutEntryCalculatorTests
{
[Theory(Skip = "Results do not match not expected.")]
[Theory]
[MemberData(nameof(IccConversionDataLutEntry.Lut8ConversionTestData), MemberType = typeof(IccConversionDataLutEntry))]
internal void LutEntryCalculator_WithLut8_ReturnsResult(IccLut8TagDataEntry lut, Vector4 input, Vector4 expected)
{
Expand All @@ -25,7 +25,7 @@ internal void LutEntryCalculator_WithLut8_ReturnsResult(IccLut8TagDataEntry lut,
VectorAssert.Equal(expected, result, 4);
}

[Theory(Skip = "Results do not match not expected.")]
[Theory]
[MemberData(nameof(IccConversionDataLutEntry.Lut16ConversionTestData), MemberType = typeof(IccConversionDataLutEntry))]
internal void LutEntryCalculator_WithLut16_ReturnsResult(IccLut16TagDataEntry lut, Vector4 input, Vector4 expected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ private static IccLut CreateLut(int length)

public static object[][] Lut8ConversionTestData =
{
new object[] { Lut8, new Vector4(0.2f, 0.3f, 0, 0), new Vector4(0.339762866f, 0, 0, 0) },
new object[] { Lut8Matrix, new Vector4(0.21f, 0.31f, 0.41f, 0), new Vector4(0.431305826f, 0, 0, 0) },
new object[] { Lut8, new Vector4(0.2f, 0.3f, 0, 0), new Vector4(0.4578933760499877f, 0, 0, 0) },
new object[] { Lut8Matrix, new Vector4(0.21f, 0.31f, 0.41f, 0), new Vector4(0.40099657491875312f, 0, 0, 0) },
};

public static object[][] Lut16ConversionTestData =
{
new object[] { Lut16, new Vector4(0.2f, 0.3f, 0, 0), new Vector4(0.245625019f, 0, 0, 0) },
new object[] { Lut16Matrix, new Vector4(0.21f, 0.31f, 0.41f, 0), new Vector4(0.336980581f, 0, 0, 0) },
new object[] { Lut16, new Vector4(0.2f, 0.3f, 0, 0), new Vector4(0.3543750030529918f, 0, 0, 0) },
new object[] { Lut16Matrix, new Vector4(0.21f, 0.31f, 0.41f, 0), new Vector4(0.29739562389689594f, 0, 0, 0) },
};
Copy link
Collaborator

@waacton waacton Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These test values are updated in the same way as the CLUT tests, calculated independently in Unicolour. LutEntryCalculator tests are now reenabled.

}
Loading