[v3,14/17] ipa: libipa: colour: Use Vector and Matrix for linear algebra
diff mbox series

Message ID 20241118221618.13953-15-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • Improve linear algebra helpers in libipa
Related show

Commit Message

Laurent Pinchart Nov. 18, 2024, 10:16 p.m. UTC
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>
---
 src/ipa/libipa/colours.cpp | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

Comments

Milan Zamazal Nov. 19, 2024, 10:35 a.m. UTC | #1
Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:

> 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>

> ---
>  src/ipa/libipa/colours.cpp | 26 +++++++++++++++++---------
>  1 file changed, 17 insertions(+), 9 deletions(-)
>
> diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp
> index 6c597093ddf8..97124cf460d4 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;
>  }

Patch
diff mbox series

diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp
index 6c597093ddf8..97124cf460d4 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;
 }