From patchwork Fri Sep 18 12:09:37 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 28347 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id EE75DBE175 for ; Fri, 18 Sep 2026 12:10:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A11726874F; Fri, 18 Sep 2026 14:10:08 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="nFTWmEpp"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 5204868615 for ; Fri, 18 Sep 2026 14:09:55 +0200 (CEST) Received: from pb-laptop.local (185.221.142.0.nat.pool.zt.hu [185.221.142.0]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 77CDF417F for ; Fri, 18 Sep 2026 14:08:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1789733292; bh=wcA94MG4AgqWDykoXrTDwO7mB2LkgUWDq8Vihri6vRY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=nFTWmEppYbL+SF/7Bzf/Xa682+IYxKAD9n11jc3wkfeg/mJ66gpGxOOSEpF7zrpFp ZyalplyPKEmhBwE58MOsDc6j6WlIGjF4cwA0cat3GU1vp6kf+ciu8y8c0MkanQYBYL 8uuv4zNEJx+RVj351v0gNupM1+kPkGPYO0hhgERI= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v3 09/21] ipa: libipa: gamma: Use `std::optional` for segment lengths Date: Fri, 18 Sep 2026 14:09:37 +0200 Message-ID: <20260918120949.191668-10-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260918120949.191668-1-barnabas.pocze@ideasonboard.com> References: <20260918120949.191668-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Pass the segment sizes in an optional `std::span` to `init()`. The advatange of this over an empty span is that a new `NLutNodes`-dependent `init()` can be added to `GammaAlgorithm` to enforce the size during compilation. Signed-off-by: Barnabás Pőcze --- src/ipa/libipa/gamma.cpp | 95 ++++++++++++++++++++++------------------ src/ipa/libipa/gamma.h | 13 ++++-- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/ipa/libipa/gamma.cpp b/src/ipa/libipa/gamma.cpp index 20807ac601..fa850aa281 100644 --- a/src/ipa/libipa/gamma.cpp +++ b/src/ipa/libipa/gamma.cpp @@ -69,53 +69,22 @@ 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 + * \copybrief GammaAlgorithm::init() * - * 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. + * This function provides the underlying non-templated implementation for + * GammaAlgorithm::init(). Refer to the documentation of that function + * for the details. * - * 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. - * - * 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 segments) + std::optional> segments) { /* * If the caller doesn't pass in a segment list we simply construct the * position of the knee-points assuming equally spaced segments. */ - if (segments.empty()) { + if (!segments) { for (unsigned int i = 0; i < nLutNodes_; i++) kneePoints_[i] = static_cast(i) / (nLutNodes_ - 1); } else { @@ -123,17 +92,17 @@ int GammaAlgorithmBase::init(ControlInfoMap::Map &controls, const ValueNode &tun * As segments holds the distance between the knee-points, we * expect one fewer segment entries than we have LUT nodes. */ - if (segments.size() != nLutNodes_ - 1) + if (segments->size() != nLutNodes_ - 1) return -EINVAL; - float total = std::accumulate(segments.begin(), segments.end(), 0.0f); + float total = std::accumulate(segments->begin(), segments->end(), 0.0f); float x = 0.0f; for (unsigned int i = 0; i < nLutNodes_; i++) { kneePoints_[i] = x / total; - if (i < segments.size()) - x += segments[i]; + if (i < segments->size()) + x += (*segments)[i]; } } @@ -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..946df13cc4 100644 --- a/src/ipa/libipa/gamma.h +++ b/src/ipa/libipa/gamma.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include @@ -46,15 +47,15 @@ public: { } - int init(ControlInfoMap::Map &controls, const ValueNode &tuningData, - std::span 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::optional> segmentLengths); + unsigned int nLutNodes_; float defaultGamma_; std::vector kneePoints_; @@ -71,6 +72,12 @@ public: { } + int init(ControlInfoMap::Map &controls, const ValueNode &tuningData, + std::optional> segments = {}) + { + return GammaAlgorithmBase::init(controls, tuningData, segments); + } + template void prepare(gamma::FrameContext &context, std::span lut) {