summaryrefslogtreecommitdiff
path: root/src/ipa
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-11-18 21:07:01 +0200
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-11-26 19:05:20 +0200
commit16c15c428d22ad2a83598ee9665c58ec4ce547b9 (patch)
treef46e10a680b36b326119e236a156f26212b362b6 /src/ipa
parent29892f1c56c6657a9d650aff203912212c295bd0 (diff)
ipa: libipa: colour: Use Vector and Matrix for linear algebra
Replace the manual vector and matrix calculations with usage of the Vector and Matrix classes. This simplifies the code and improves readability. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
Diffstat (limited to 'src/ipa')
-rw-r--r--src/ipa/libipa/colours.cpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp
index 6c597093..97124cf4 100644
--- a/src/ipa/libipa/colours.cpp
+++ b/src/ipa/libipa/colours.cpp
@@ -31,7 +31,11 @@ namespace ipa {
*/
double rec601LuminanceFromRGB(const RGB<double> &rgb)
{
- return (rgb.r() * .299) + (rgb.g() * .587) + (rgb.b() * .114);
+ static const Vector<double, 3> rgb2y{{
+ 0.299, 0.587, 0.114
+ }};
+
+ return rgb.dot(rgb2y);
}
/**
@@ -54,17 +58,21 @@ double rec601LuminanceFromRGB(const RGB<double> &rgb)
*/
uint32_t estimateCCT(const RGB<double> &rgb)
{
- /* Convert the RGB values to CIE tristimulus values (XYZ) */
- double X = (-0.14282) * rgb.r() + (1.54924) * rgb.g() + (-0.95641) * rgb.b();
- double Y = (-0.32466) * rgb.r() + (1.57837) * rgb.g() + (-0.73191) * rgb.b();
- double Z = (-0.68202) * rgb.r() + (0.77073) * rgb.g() + (0.56332) * rgb.b();
+ /*
+ * Convert the RGB values to CIE tristimulus values (XYZ) and divide by
+ * the sum of X, Y and Z to calculate the CIE xy chromaticity.
+ */
+ static const Matrix<double, 3, 3> rgb2xyz({
+ -0.14282, 1.54924, -0.95641,
+ -0.32466, 1.57837, -0.73191,
+ -0.68202, 0.77073, 0.56332
+ });
- /* Calculate the normalized chromaticity values */
- double x = X / (X + Y + Z);
- double y = Y / (X + Y + Z);
+ Vector<double, 3> xyz = rgb2xyz * rgb;
+ xyz /= xyz.sum();
/* Calculate CCT */
- double n = (x - 0.3320) / (0.1858 - y);
+ double n = (xyz.x() - 0.3320) / (0.1858 - xyz.y());
return 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;
}