{"id":22620,"url":"https://patchwork.libcamera.org/api/1.1/patches/22620/?format=json","web_url":"https://patchwork.libcamera.org/patch/22620/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20250123114204.79321-6-stefan.klug@ideasonboard.com>","date":"2025-01-23T11:40:55","name":"[v2,05/17] libipa: Add grey world AWB algorithm","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"0bb2fc2e3d8fce79d1897ddcf919dddd84324395","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/1.1/people/184/?format=json","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22620/mbox/","series":[{"id":4966,"url":"https://patchwork.libcamera.org/api/1.1/series/4966/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4966","date":"2025-01-23T11:40:50","name":"Add Bayesian AWB algorithm to libipa and rkisp1","version":2,"mbox":"https://patchwork.libcamera.org/series/4966/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22620/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22620/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 5BD2CC3226\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 23 Jan 2025 11:42:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id C2BDC6856A;\n\tThu, 23 Jan 2025 12:42:28 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id B205C68561\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 23 Jan 2025 12:42:25 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:c0a:33cd:b453:5d3f])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2193E89A;\n\tThu, 23 Jan 2025 12:41:22 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"s5jNvhbE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1737632482;\n\tbh=3TyEzYX4a/1SX9rS4VDetipZCDsL7Hd2nwUAQTI6z3A=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=s5jNvhbEyIvD27ilSietSKIvibLKFucPImTWaG9rWuDLvQncjUuw0EbLzVncnILrM\n\tsLmzPWCOWJghMe5jUFmtNTOVYoqs1Jn5cstJIwu5orHcigk5V/dNhz2x4erX+ianLb\n\tVUhSBLwZsnlk9agMweGDDAqCPzrUfABAestsGJkI=","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tDaniel Scally <dan.scally@ideasonboard.com>","Subject":"[PATCH v2 05/17] libipa: Add grey world AWB algorithm","Date":"Thu, 23 Jan 2025 12:40:55 +0100","Message-ID":"<20250123114204.79321-6-stefan.klug@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20250123114204.79321-1-stefan.klug@ideasonboard.com>","References":"<20250123114204.79321-1-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add the grey world algorithm that is currently used in rkisp1 to libipa.\nNo changes in functionality were made.\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\nReviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\n\n---\n\nChanges in v2:\n- Fixed some typos\n- Fixed result of gainsFromColourTemperature()\n---\n src/ipa/libipa/awb_grey.cpp | 114 ++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/awb_grey.h   |  35 +++++++++++\n src/ipa/libipa/meson.build  |   2 +\n 3 files changed, 151 insertions(+)\n create mode 100644 src/ipa/libipa/awb_grey.cpp\n create mode 100644 src/ipa/libipa/awb_grey.h","diff":"diff --git a/src/ipa/libipa/awb_grey.cpp b/src/ipa/libipa/awb_grey.cpp\nnew file mode 100644\nindex 000000000000..49448976ed26\n--- /dev/null\n+++ b/src/ipa/libipa/awb_grey.cpp\n@@ -0,0 +1,114 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024 Ideas on Board Oy\n+ *\n+ * Base class for grey world AWB algorithm\n+ */\n+\n+#include \"awb_grey.h\"\n+\n+#include <cmath>\n+\n+#include <libcamera/base/log.h>\n+#include <libcamera/control_ids.h>\n+\n+#include \"colours.h\"\n+\n+using namespace libcamera::controls;\n+\n+/**\n+ * \\file awb_grey.h\n+ * \\brief Implementation of a grey world AWB algorithm\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(Awb)\n+namespace ipa {\n+\n+/**\n+ * \\class AwbGrey\n+ * \\brief A Grey world auto white balance algorithm\n+ */\n+\n+/**\n+ * \\brief Initialize the algorithm with the given tuning data\n+ * \\param[in] tuningData The tuning data for the algorithm\n+ *\n+ * Load the colour temperature curve from the tuning data. If there is no tuning\n+ * data available, continue with a warning. Manual colour temperature will not\n+ * work in that case.\n+ *\n+ * \\return 0 on success, a negative error code otherwise\n+ */\n+int AwbGrey::init(const YamlObject &tuningData)\n+{\n+\tInterpolator<Vector<double, 2>> gains;\n+\tint ret = gains.readYaml(tuningData[\"colourGains\"], \"ct\", \"gains\");\n+\tif (ret < 0)\n+\t\tLOG(Awb, Warning)\n+\t\t\t<< \"Failed to parse 'colourGains' \"\n+\t\t\t<< \"parameter from tuning file; \"\n+\t\t\t<< \"manual colour temperature will not work properly\";\n+\telse\n+\t\tcolourGainCurve_ = gains;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Calculate awb data from the given statistics\n+ * \\param[in] stats The statistics to use for the calculation\n+ * \\param[in] lux The lux value of the scene\n+ *\n+ * Estimates the colour temperature based on the colours::estimateCCT function.\n+ * The gains are calculated purely based on the RGB means provided by the \\a\n+ * stats. The colour temperature is not taken into account when calculating the\n+ * gains.\n+ *\n+ * The \\a lux parameter is not used in this algorithm.\n+ *\n+ * \\return The awb result\n+ */\n+AwbResult AwbGrey::calculateAwb(const AwbStats &stats, [[maybe_unused]] int lux)\n+{\n+\tAwbResult result;\n+\tauto means = stats.getRGBMeans();\n+\tresult.colourTemperature = estimateCCT(means);\n+\n+\t/*\n+\t * Estimate the red and blue gains to apply in a grey world. The green\n+\t * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the\n+\t * divisor to a minimum value of 1.0.\n+\t */\n+\tresult.gains.r() = means.g() / std::max(means.r(), 1.0);\n+\tresult.gains.g() = 1.0;\n+\tresult.gains.b() = means.g() / std::max(means.b(), 1.0);\n+\treturn result;\n+}\n+\n+/**\n+ * \\brief Compute white balance gains from a colour temperature\n+ * \\param[in] colourTemperature The colour temperature in Kelvin\n+ *\n+ * Compute the white balance gains from a \\a colourTemperature. This function\n+ * does not take any statistics into account. It simply interpolates the colour\n+ * gains configured in the colour temperature curve.\n+ *\n+ * \\return The colour gains if a colour temperature curve is available,\n+ * [1, 1, 1] otherwise.\n+ */\n+RGB<double> AwbGrey::gainsFromColourTemperature(double colourTemperature)\n+{\n+\tif (!colourGainCurve_) {\n+\t\tLOG(Awb, Error) << \"No gains defined\";\n+\t\treturn RGB<double>({ 1.0, 1.0, 1.0 });\n+\t}\n+\n+\tauto gains = colourGainCurve_->getInterpolated(colourTemperature);\n+\treturn { { gains[0], 1.0, gains[1] } };\n+}\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/awb_grey.h b/src/ipa/libipa/awb_grey.h\nnew file mode 100644\nindex 000000000000..7cb5cbf86873\n--- /dev/null\n+++ b/src/ipa/libipa/awb_grey.h\n@@ -0,0 +1,35 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024 Ideas on Board Oy\n+ *\n+ * AWB grey world algorithm\n+ */\n+\n+#pragma once\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+#include \"awb.h\"\n+#include \"interpolator.h\"\n+#include \"vector.h\"\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class AwbGrey : public AwbAlgorithm\n+{\n+public:\n+\tAwbGrey() = default;\n+\n+\tint init(const YamlObject &tuningData) override;\n+\tAwbResult calculateAwb(const AwbStats &stats, int lux) override;\n+\tRGB<double> gainsFromColourTemperature(double colourTemperature) override;\n+\n+private:\n+\tstd::optional<Interpolator<Vector<double, 2>>> colourGainCurve_;\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 03e879c5834f..c550a6eb45b6 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -3,6 +3,7 @@\n libipa_headers = files([\n     'agc_mean_luminance.h',\n     'algorithm.h',\n+    'awb_grey.h',\n     'awb.h',\n     'camera_sensor_helper.h',\n     'colours.h',\n@@ -21,6 +22,7 @@ libipa_headers = files([\n libipa_sources = files([\n     'agc_mean_luminance.cpp',\n     'algorithm.cpp',\n+    'awb_grey.cpp',\n     'awb.cpp',\n     'camera_sensor_helper.cpp',\n     'colours.cpp',\n","prefixes":["v2","05/17"]}