ipa: libipa: vector: Add element-wise division operator
diff mbox series

Message ID 20250202235634.3776-1-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • ipa: libipa: vector: Add element-wise division operator
Related show

Commit Message

Laurent Pinchart Feb. 2, 2025, 11:56 p.m. UTC
Add an operator to divide a scaler by a vector element-wise. Use it in
the rkisp1 AWB algorithm to replace manual operation on individual RGB
components.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/ipa/libipa/vector.cpp         | 10 ++++++++++
 src/ipa/libipa/vector.h           | 11 +++++++++++
 src/ipa/rkisp1/algorithms/awb.cpp |  6 +-----
 3 files changed, 22 insertions(+), 5 deletions(-)


base-commit: 9bc8b6a573a63b8c9f5588cdea6da5ae71abd138
--
Regards,

Laurent Pinchart

Comments

Milan Zamazal Feb. 3, 2025, 10:43 a.m. UTC | #1
Hi Laurent,

thank you for the patch.

Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:

> Add an operator to divide a scaler by a vector element-wise. Use it in
> the rkisp1 AWB algorithm to replace manual operation on individual RGB
> components.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/ipa/libipa/vector.cpp         | 10 ++++++++++
>  src/ipa/libipa/vector.h           | 11 +++++++++++
>  src/ipa/rkisp1/algorithms/awb.cpp |  6 +-----
>  3 files changed, 22 insertions(+), 5 deletions(-)
>
> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> index 8019f8cfdc85..f665813c24d6 100644
> --- a/src/ipa/libipa/vector.cpp
> +++ b/src/ipa/libipa/vector.cpp
> @@ -312,6 +312,16 @@ namespace ipa {
>   * \return Product of matrix \a m and vector \a v
>   */
>
> +/**
> + * \fn Vector<T, Rows> operator/(T scalar, const Vector<T, Rows> &vector)
> + * \brief Divide a scalar by a vector element-wise
> + * \tparam T Numerical type of the contents of the vector
> + * \tparam Rows The number of rows in the vector

I'd rather see here "The number of elements ..." but not that important.

> + * \param scalar The scalar
> + * \param vector The vector
> + * \return The element-wise division of \a scaler by the \a vector
> + */
> +
>  /**
>   * \typedef RGB
>   * \brief A Vector of 3 elements representing an RGB pixel value
> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> index fe33c9d6fbd1..25882616ba8a 100644
> --- a/src/ipa/libipa/vector.h
> +++ b/src/ipa/libipa/vector.h
> @@ -308,6 +308,17 @@ Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols>
>  	return result;
>  }
>
> +template<typename T, unsigned int Rows>
> +Vector<T, Rows> operator/(T scalar, const Vector<T, Rows> &vector)
> +{
> +	Vector<T, Rows> result;
> +
> +	for (unsigned int i = 0; i < Rows; i++)
> +		result[i] = scalar / vector[i];
> +
> +	return result;
> +}
> +
>  template<typename T, unsigned int Rows>
>  bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
>  {
> diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp
> index cffaa06a22c1..319f7e0b5ef2 100644
> --- a/src/ipa/rkisp1/algorithms/awb.cpp
> +++ b/src/ipa/rkisp1/algorithms/awb.cpp
> @@ -316,11 +316,7 @@ void Awb::process(IPAContext &context,
>  	 * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the
>  	 * divisor to a minimum value of 1.0.
>  	 */
> -	RGB<double> gains({
> -		rgbMeans.g() / std::max(rgbMeans.r(), 1.0),
> -		1.0,
> -		rgbMeans.g() / std::max(rgbMeans.b(), 1.0)
> -	});
> +	RGB<double> gains = rgbMeans.g() / rgbMeans.max(1.0);

How about the corner case when rgbMeans.g() < 1.0?  Then gains.g() won't
be 1.0 anymore.  Does it matter?

>  	/*
>  	 * Clamp the gain values to the hardware, which expresses gains as Q2.8
>
> base-commit: 9bc8b6a573a63b8c9f5588cdea6da5ae71abd138
> --
> Regards,
>
> Laurent Pinchart
Laurent Pinchart Feb. 13, 2025, 9:58 p.m. UTC | #2
On Mon, Feb 03, 2025 at 11:43:39AM +0100, Milan Zamazal wrote:
> Hi Laurent,
> 
> thank you for the patch.
> 
> Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:
> 
> > Add an operator to divide a scaler by a vector element-wise. Use it in
> > the rkisp1 AWB algorithm to replace manual operation on individual RGB
> > components.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/ipa/libipa/vector.cpp         | 10 ++++++++++
> >  src/ipa/libipa/vector.h           | 11 +++++++++++
> >  src/ipa/rkisp1/algorithms/awb.cpp |  6 +-----
> >  3 files changed, 22 insertions(+), 5 deletions(-)
> >
> > diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> > index 8019f8cfdc85..f665813c24d6 100644
> > --- a/src/ipa/libipa/vector.cpp
> > +++ b/src/ipa/libipa/vector.cpp
> > @@ -312,6 +312,16 @@ namespace ipa {
> >   * \return Product of matrix \a m and vector \a v
> >   */
> >
> > +/**
> > + * \fn Vector<T, Rows> operator/(T scalar, const Vector<T, Rows> &vector)
> > + * \brief Divide a scalar by a vector element-wise
> > + * \tparam T Numerical type of the contents of the vector
> > + * \tparam Rows The number of rows in the vector
> 
> I'd rather see here "The number of elements ..." but not that important.
> 
> > + * \param scalar The scalar
> > + * \param vector The vector
> > + * \return The element-wise division of \a scaler by the \a vector
> > + */
> > +
> >  /**
> >   * \typedef RGB
> >   * \brief A Vector of 3 elements representing an RGB pixel value
> > diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> > index fe33c9d6fbd1..25882616ba8a 100644
> > --- a/src/ipa/libipa/vector.h
> > +++ b/src/ipa/libipa/vector.h
> > @@ -308,6 +308,17 @@ Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols>
> >  	return result;
> >  }
> >
> > +template<typename T, unsigned int Rows>
> > +Vector<T, Rows> operator/(T scalar, const Vector<T, Rows> &vector)
> > +{
> > +	Vector<T, Rows> result;
> > +
> > +	for (unsigned int i = 0; i < Rows; i++)
> > +		result[i] = scalar / vector[i];
> > +
> > +	return result;
> > +}
> > +
> >  template<typename T, unsigned int Rows>
> >  bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
> >  {
> > diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp
> > index cffaa06a22c1..319f7e0b5ef2 100644
> > --- a/src/ipa/rkisp1/algorithms/awb.cpp
> > +++ b/src/ipa/rkisp1/algorithms/awb.cpp
> > @@ -316,11 +316,7 @@ void Awb::process(IPAContext &context,
> >  	 * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the
> >  	 * divisor to a minimum value of 1.0.
> >  	 */
> > -	RGB<double> gains({
> > -		rgbMeans.g() / std::max(rgbMeans.r(), 1.0),
> > -		1.0,
> > -		rgbMeans.g() / std::max(rgbMeans.b(), 1.0)
> > -	});
> > +	RGB<double> gains = rgbMeans.g() / rgbMeans.max(1.0);
> 
> How about the corner case when rgbMeans.g() < 1.0?  Then gains.g() won't
> be 1.0 anymore.  Does it matter?

That's a good point. I think I'll drop this patch for now.

> >  	/*
> >  	 * Clamp the gain values to the hardware, which expresses gains as Q2.8
> >
> > base-commit: 9bc8b6a573a63b8c9f5588cdea6da5ae71abd138

Patch
diff mbox series

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index 8019f8cfdc85..f665813c24d6 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -312,6 +312,16 @@  namespace ipa {
  * \return Product of matrix \a m and vector \a v
  */

+/**
+ * \fn Vector<T, Rows> operator/(T scalar, const Vector<T, Rows> &vector)
+ * \brief Divide a scalar by a vector element-wise
+ * \tparam T Numerical type of the contents of the vector
+ * \tparam Rows The number of rows in the vector
+ * \param scalar The scalar
+ * \param vector The vector
+ * \return The element-wise division of \a scaler by the \a vector
+ */
+
 /**
  * \typedef RGB
  * \brief A Vector of 3 elements representing an RGB pixel value
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index fe33c9d6fbd1..25882616ba8a 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -308,6 +308,17 @@  Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols>
 	return result;
 }

+template<typename T, unsigned int Rows>
+Vector<T, Rows> operator/(T scalar, const Vector<T, Rows> &vector)
+{
+	Vector<T, Rows> result;
+
+	for (unsigned int i = 0; i < Rows; i++)
+		result[i] = scalar / vector[i];
+
+	return result;
+}
+
 template<typename T, unsigned int Rows>
 bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
 {
diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp
index cffaa06a22c1..319f7e0b5ef2 100644
--- a/src/ipa/rkisp1/algorithms/awb.cpp
+++ b/src/ipa/rkisp1/algorithms/awb.cpp
@@ -316,11 +316,7 @@  void Awb::process(IPAContext &context,
 	 * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the
 	 * divisor to a minimum value of 1.0.
 	 */
-	RGB<double> gains({
-		rgbMeans.g() / std::max(rgbMeans.r(), 1.0),
-		1.0,
-		rgbMeans.g() / std::max(rgbMeans.b(), 1.0)
-	});
+	RGB<double> gains = rgbMeans.g() / rgbMeans.max(1.0);

 	/*
 	 * Clamp the gain values to the hardware, which expresses gains as Q2.8