Show a patch.

GET /api/patches/22492/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 22492,
    "url": "https://patchwork.libcamera.org/api/patches/22492/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/22492/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20250109115412.356768-6-stefan.klug@ideasonboard.com>",
    "date": "2025-01-09T11:53:56",
    "name": "[v1,05/11] libipa: Add grey world AWB algorithm",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "65504ddbd7110f9d4e0de5f590f314c9000337e3",
    "submitter": {
        "id": 184,
        "url": "https://patchwork.libcamera.org/api/people/184/?format=api",
        "name": "Stefan Klug",
        "email": "stefan.klug@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/22492/mbox/",
    "series": [
        {
            "id": 4938,
            "url": "https://patchwork.libcamera.org/api/series/4938/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4938",
            "date": "2025-01-09T11:53:51",
            "name": "Add Bayesian AWB algorithm to libipa and rkisp1",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/4938/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/22492/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/22492/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 216C9C32EA\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  9 Jan 2025 11:55:26 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 912B668509;\n\tThu,  9 Jan 2025 12:55:25 +0100 (CET)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id AB17E684E2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  9 Jan 2025 12:55:18 +0100 (CET)",
            "from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:93b9:eca8:897d:eae6])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2B3375B3;\n\tThu,  9 Jan 2025 12:54:25 +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=\"XOR++Wqq\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1736423665;\n\tbh=PZMYoREg2rnkTargG1Vpo/vWcRdyKXgVX3J/qaM6rZw=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=XOR++Wqq2fxlIDlILb3+LM+ZFPuqhThTEgbrBI3lVjTSXGWjM1dHT0MT+RSF+djIv\n\tpA3TdZhPscNEmMkHxOn/d/2qdnqcYzMDAKFxsKkuBgkVqgvIWrgAxT5+XkxIMzoZV/\n\thTMDo7EoGPSck0G20Fkdz2Su/HtBX38O8wKB+/Fk=",
        "From": "Stefan Klug <stefan.klug@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Stefan Klug <stefan.klug@ideasonboard.com>",
        "Subject": "[PATCH v1 05/11] libipa: Add grey world AWB algorithm",
        "Date": "Thu,  9 Jan 2025 12:53:56 +0100",
        "Message-ID": "<20250109115412.356768-6-stefan.klug@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.43.0",
        "In-Reply-To": "<20250109115412.356768-1-stefan.klug@ideasonboard.com>",
        "References": "<20250109115412.356768-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>\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..192a7cf3834a\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 bayesian 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 coulours::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[0] } };\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..6eda8e5498fb\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 coulourTemperature) 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": [
        "v1",
        "05/11"
    ]
}