[v4,08/21] ipa: libipa: gamma: Use `std::optional` for segment lengths
diff mbox series

Message ID 20261113164054.267164-9-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • libcamera: rcar-gen4 + rpp-x1
Related show

Commit Message

Barnabás Pőcze Nov. 13, 2026, 4:40 p.m. UTC
Add an `init()` function to `GammaAlgorithm`, and make the one in
`GammaAlgorithmBase` protected. And take the list of segment lengths
in an `std::optional`. This allows the use of a constant sized span,
and thus compile-time checking when calling `init()`.

Additionally, change "@return" to "\return" in the documentation.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 src/ipa/libipa/gamma.cpp | 83 +++++++++++++++++++++++-----------------
 src/ipa/libipa/gamma.h   | 15 ++++++--
 2 files changed, 59 insertions(+), 39 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/libipa/gamma.cpp b/src/ipa/libipa/gamma.cpp
index 20807ac601..ad34548454 100644
--- a/src/ipa/libipa/gamma.cpp
+++ b/src/ipa/libipa/gamma.cpp
@@ -69,44 +69,13 @@  const float kDefaultGamma = 2.2f;
  */
 
 /**
- * \brief Initialise the algorithm with the given tuning data
- * \param[out] controls The ControlList into which this algorithm's supported
- * controls will be emplaced.
- * \param[in] tuningData The tuning data to use with the algorithm
- * \param[in] segments A vector of segment spacings to define a custom
- * X coordinate system for the curve
- *
- * Parse \a tuningData and \a segments to initialize the gamma correction curve.
- * The tuning data may contain a default gamma value to use; otherwise the value
- * of \a kDefaultGamma will be taken as the default. The piecewise linear
- * function will be applied on a number of knots whose position is described by
- * the optional \a segments argument, which describes each segment's relative
- * length.
- *
- * For example, if the gamma correction has to be applied on 16 equally spaced
- * sampling points, a \a segments array like:
- *
- * [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
+ * \copybrief GammaAlgorithm::init()
  *
- * would result in evenly spaced knee-points along the X-axis.
- *
- * Hardware may expect the knee-points to be spaced more densely towards the
- * start of the curve and more sparsely towards the end, in which case an
- * alternative array might be:
- *
- * [1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 8, 8, 8, 8, 8]
- *
- * As the values in \a segments represent the distance between two knee-points
- * relative to the total distance between the first and last point, the length
- * of \a segments should be equal to the number of knee-points minus one.
- *;
- * If an IPA implementation doesnt't provide \a segments, the GammaAlgorithm
- * class consturcts an evenly-spaced default.
+ * This function provides the underlying non-templated implementation for
+ * GammaAlgorithm::init(). Refer to the documentation of that function
+ * for the details.
  *
- * IPA modules are expected to call this function as part of their
- * implementation of Algorithm::init()
- *
- * @return 0 on success, a negative error code otherwise
+ * \sa GammaAlgorithm::init()
  */
 int GammaAlgorithmBase::init(ControlInfoMap::Map &controls, const ValueNode &tuningData,
 			     std::span<const unsigned int> segments)
@@ -247,6 +216,48 @@  void GammaAlgorithmBase::process(gamma::FrameContext &context, ControlList &meta
  * - https://en.wikipedia.org/wiki/SRGB
  */
 
+/**
+ * \fn GammaAlgorithm::init()
+ * \brief Initialise the algorithm with the given tuning data
+ * \param[out] controls The ControlList into which this algorithm's supported
+ * controls will be emplaced.
+ * \param[in] tuningData The tuning data to use with the algorithm
+ * \param[in] segments A vector of segment spacings to define a custom
+ * X coordinate system for the curve
+ *
+ * Parse \a tuningData and \a segments to initialize the gamma correction curve.
+ * The tuning data may contain a default gamma value to use; otherwise the value
+ * of \a kDefaultGamma will be taken as the default. The piecewise linear
+ * function will be applied on a number of knots whose position is described by
+ * the optional \a segments argument, which describes each segment's relative
+ * length.
+ *
+ * For example, if the gamma correction has to be applied on 16 equally spaced
+ * sampling points, a \a segments array like:
+ *
+ * [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
+ *
+ * would result in evenly spaced knee-points along the X-axis.
+ *
+ * Hardware may expect the knee-points to be spaced more densely towards the
+ * start of the curve and more sparsely towards the end, in which case an
+ * alternative array might be:
+ *
+ * [1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 8, 8, 8, 8, 8]
+ *
+ * As the values in \a segments represent the distance between two knee-points
+ * relative to the total distance between the first and last point, the length
+ * of \a segments should be equal to the number of knee-points minus one.
+ *;
+ * If an IPA implementation doesn't provide \a segments, the GammaAlgorithm
+ * class constructs an evenly-spaced default.
+ *
+ * IPA modules are expected to call this function as part of their
+ * implementation of Algorithm::init()
+ *
+ * \return 0 on success, a negative error code otherwise
+ */
+
 /**
  * \fn GammaAlgorithm::prepare()
  * \tparam T The type of data expected by the hardware's look-up table
diff --git a/src/ipa/libipa/gamma.h b/src/ipa/libipa/gamma.h
index a8872a6c75..bf71423848 100644
--- a/src/ipa/libipa/gamma.h
+++ b/src/ipa/libipa/gamma.h
@@ -8,6 +8,7 @@ 
 #pragma once
 
 #include <cmath>
+#include <optional>
 #include <span>
 #include <vector>
 
@@ -46,15 +47,15 @@  public:
 	{
 	}
 
-	int init(ControlInfoMap::Map &controls, const ValueNode &tuningData,
-		 std::span<const unsigned int> segments = {});
-
 	void configure(gamma::ActiveState &state);
 	void queueRequest(gamma::ActiveState &state, const uint32_t frame,
 			  gamma::FrameContext &context, const ControlList &controls);
 	void process(gamma::FrameContext &context, ControlList &metadata);
 
 protected:
+	int init(ControlInfoMap::Map &controls, const ValueNode &tuningData,
+		 std::span<const unsigned int> segments);
+
 	unsigned int nLutNodes_;
 	float defaultGamma_;
 	std::vector<float> kneePoints_;
@@ -71,6 +72,14 @@  public:
 	{
 	}
 
+	int init(ControlInfoMap::Map &controls, const ValueNode &tuningData,
+		 std::optional<std::span<const unsigned int, NLutNodes - 1>> segments = {})
+	{
+		return GammaAlgorithmBase::init(
+			controls, tuningData,
+			segments ? *segments : std::span<const unsigned int>{});
+	}
+
 	template<typename T>
 	void prepare(gamma::FrameContext &context, std::span<T, NLutNodes> lut)
 	{