From 16c15c428d22ad2a83598ee9665c58ec4ce547b9 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 18 Nov 2024 21:07:01 +0200 Subject: 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 Reviewed-by: Milan Zamazal --- src/ipa/libipa/colours.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'src') 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 &rgb) { - return (rgb.r() * .299) + (rgb.g() * .587) + (rgb.b() * .114); + static const Vector rgb2y{{ + 0.299, 0.587, 0.114 + }}; + + return rgb.dot(rgb2y); } /** @@ -54,17 +58,21 @@ double rec601LuminanceFromRGB(const RGB &rgb) */ uint32_t estimateCCT(const RGB &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 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 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; } -- cgit v1.2.1