Show a patch.

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

{
    "id": 20137,
    "url": "https://patchwork.libcamera.org/api/patches/20137/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/20137/",
    "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": "<20240529193708.846687-2-paul.elder@ideasonboard.com>",
    "date": "2024-05-29T19:37:06",
    "name": "[v5,1/3] ipa: libipa: Add Matrix class",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "bc360b08ed125e46bf07a3a34231a31501a76bbb",
    "submitter": {
        "id": 17,
        "url": "https://patchwork.libcamera.org/api/people/17/?format=api",
        "name": "Paul Elder",
        "email": "paul.elder@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/20137/mbox/",
    "series": [
        {
            "id": 4338,
            "url": "https://patchwork.libcamera.org/api/series/4338/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4338",
            "date": "2024-05-29T19:37:05",
            "name": "ipa: rkisp1: Add crosstalk algorithm",
            "version": 5,
            "mbox": "https://patchwork.libcamera.org/series/4338/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/20137/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/20137/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 77BA1BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 29 May 2024 19:37:27 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 19F54634BE;\n\tWed, 29 May 2024 21:37:27 +0200 (CEST)",
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BB77A634B6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 29 May 2024 21:37:23 +0200 (CEST)",
            "from neptunite.hamster-moth.ts.net\n\t(h175-177-049-156.catv02.itscom.jp [175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E4C48149B;\n\tWed, 29 May 2024 21:37:18 +0200 (CEST)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"TNg8mUh0\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1717011440;\n\tbh=T4qLOb+/vBHwaFnu7Hsv2xS7teSktx7X+bzh5C4hSB0=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=TNg8mUh0qyxH1t6FWxFhWJt1ggkX2WvYJOHi6YYjGFVDTX7VgmuvgzVnLC/UkIxgP\n\t27X7qRSDjqjxjbYcrP0k/9MAFGH8p6EQi31P7lAYNGgKjHvKxJFPo9rtHJilfyliAp\n\thUu4rIILiwpJ/iYbfq3u9LCDZug3ZxMKrLO1Rd5k=",
        "From": "Paul Elder <paul.elder@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Paul Elder <paul.elder@ideasonboard.com>,\n\tStefan Klug <stefan.klug@ideasonboard.com>",
        "Subject": "[PATCH v5 1/3] ipa: libipa: Add Matrix class",
        "Date": "Thu, 30 May 2024 04:37:06 +0900",
        "Message-Id": "<20240529193708.846687-2-paul.elder@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.39.2",
        "In-Reply-To": "<20240529193708.846687-1-paul.elder@ideasonboard.com>",
        "References": "<20240529193708.846687-1-paul.elder@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 a class to represent a Matrix object and operations for adding\nmatrices, multipling a matrix by a scalar, and multiplying two matrices.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\n---\nChanges in v5:\n- add documentation\n\nChanges in v4:\n- remove stray semicolons\n- add operator<<\n- clean up/optimize constructor\n- replace get() and set() with operator[] (and a second [] can be used\n  as operator[] returns a Span)\n\nChanges in v3:\n- fix template parameters of operator* to allow different types for the\n  scalar multiplier and the matrix's number type\n- clear data in constructors\n- fix assert in constructor\n\nChanges v2:\n- make rows and columns into template arguments\n- initialize to identity matrix on construction\n- add getter and setter\n- change from struct to class\n- fix matrix multiplication\n- clean up unused includes\n- avoid dereferencing an absent std::optional\n---\n src/ipa/libipa/matrix.cpp  |  98 ++++++++++++++++++++++\n src/ipa/libipa/matrix.h    | 161 +++++++++++++++++++++++++++++++++++++\n src/ipa/libipa/meson.build |   2 +\n 3 files changed, 261 insertions(+)\n create mode 100644 src/ipa/libipa/matrix.cpp\n create mode 100644 src/ipa/libipa/matrix.h",
    "diff": "diff --git a/src/ipa/libipa/matrix.cpp b/src/ipa/libipa/matrix.cpp\nnew file mode 100644\nindex 000000000..a7661c3cd\n--- /dev/null\n+++ b/src/ipa/libipa/matrix.cpp\n@@ -0,0 +1,98 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi Ltd\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Matrix and related operations\n+ */\n+\n+#include \"matrix.h\"\n+\n+#include <libcamera/base/log.h>\n+\n+/**\n+ * \\file matrix.h\n+ * \\brief Matrix class\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Matrix)\n+\n+namespace ipa {\n+\n+/**\n+ * \\class Matrix\n+ * \\brief Matrix class\n+ */\n+\n+/**\n+ * \\fn Matrix()\n+ * \\brief Construct an identity matrix\n+ */\n+\n+/**\n+ * \\fn Matrix(const std::vector<T> &data)\n+ * \\brief Construct matrix from supplied data\n+ * \\param data Data from which to construct a matrix\n+ *\n+ * \\a data is a one-dimensional vector and will be turned into a matrix in\n+ * row-major order. The size of \\a data must be equal to the product of the\n+ * number of rows and columns of the matrix (\\ta R x \\ta C).\n+ */\n+\n+/**\n+ * \\fn readYaml\n+ * \\brief Populate the matrix with yaml data\n+ * \\param yaml Yaml data to populate the matrix with\n+ *\n+ * Any existing data in the matrix will be overwritten. The size of the data\n+ * read from \\a yaml must be equal to the product of the number of rows and\n+ * columns of the matrix (\\ta R x \\ta C).\n+ *\n+ * The yaml data is expected to be a list with elements of type \\ta T.\n+ *\n+ * \\return 0 on success, negative error code otherwise\n+ */\n+\n+/**\n+ * \\fn operator[]\n+ * \\brief Index to a row in the matrix\n+ * \\param i Index of row to retrieve\n+ *\n+ * This operator[] returns a Span, which can then be indexed into again with\n+ * another operator[], allowing a convenient m[i][j] to access elements of the\n+ * matrix. Note that the lifetime of the Span returned by this first-level\n+ * operator[] is bound to that of the Matrix itself, so it is not recommended\n+ * to save the Span that is the result of this operator[].\n+ *\n+ * \\return Row \\a i from the matrix, as a Span\n+ */\n+\n+/**\n+ * \\fn Matrix<U, R, C> operator*(T d, const Matrix<U, R, C> &m)\n+ * \\brief Scalar product\n+ * \\param d Scalar\n+ * \\param m Matrix\n+ * \\return Product of scalar \\a d and matrix \\a m\n+ */\n+\n+/**\n+ * \\fn Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)\n+ * \\brief Matrix multiplication\n+ * \\param m1 Multiplicand matrix\n+ * \\param m2 Multiplier matrix\n+ * \\return Matrix product of matrices \\a m1 and \\a m2\n+ */\n+\n+/**\n+ * \\fn Matrix<T, R, C> operator+(const Matrix<T, R, C> &m1, const Matrix<T, R, C> &m2)\n+ * \\brief Matrix addition\n+ * \\param m1 Summand matrix\n+ * \\param m2 Summand matrix\n+ * \\return Matrix sum of matrices \\a m1 and \\a m2\n+ */\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/matrix.h b/src/ipa/libipa/matrix.h\nnew file mode 100644\nindex 000000000..2243b4f1b\n--- /dev/null\n+++ b/src/ipa/libipa/matrix.h\n@@ -0,0 +1,161 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi Ltd\n+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>\n+ *\n+ * Matrix and related operations\n+ */\n+#pragma once\n+\n+#include <algorithm>\n+#include <cmath>\n+#include <sstream>\n+#include <vector>\n+\n+#include <libcamera/base/log.h>\n+#include <libcamera/base/span.h>\n+\n+#include \"libcamera/internal/yaml_parser.h\"\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(Matrix)\n+\n+namespace ipa {\n+\n+template<typename T, unsigned int R, unsigned int C,\n+\t std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>\n+class Matrix\n+{\n+public:\n+\tMatrix()\n+\t\t: data_(R * C, static_cast<T>(false))\n+\t{\n+\t\tfor (size_t i = 0; i < std::min(R, C); i++)\n+\t\t\t(*this)[i][i] = static_cast<T>(true);\n+\t}\n+\n+\tMatrix(const std::vector<T> &data)\n+\t{\n+\t\tASSERT(data.size() == R * C);\n+\n+\t\tdata_.clear();\n+\t\tfor (const T &x : data)\n+\t\t\tdata_.push_back(x);\n+\t}\n+\n+\t~Matrix() = default;\n+\n+\tint readYaml(const libcamera::YamlObject &yaml)\n+\t{\n+\t\tif (yaml.size() != R * C) {\n+\t\t\tLOG(Matrix, Error)\n+\t\t\t\t<< \"Wrong number of values in matrix: expected \"\n+\t\t\t\t<< R * C << \", got \" << yaml.size();\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\tunsigned int i = 0;\n+\t\tfor (const auto &x : yaml.asList()) {\n+\t\t\tauto value = x.get<T>();\n+\t\t\tif (!value) {\n+\t\t\t\tLOG(Matrix, Error) << \"Failed to read matrix value\";\n+\t\t\t\treturn -EINVAL;\n+\t\t\t}\n+\n+\t\t\tdata_[i++] = *value;\n+\t\t}\n+\n+\t\treturn 0;\n+\t}\n+\n+\tconst std::string toString() const\n+\t{\n+\t\tstd::stringstream out;\n+\n+\t\tout << \"Matrix { \";\n+\t\tfor (unsigned int i = 0; i < R; i++) {\n+\t\t\tout << \"[ \";\n+\t\t\tfor (unsigned int j = 0; j < C; j++) {\n+\t\t\t\tout << (*this)[i][j];\n+\t\t\t\tout << ((j + 1 < C) ? \", \" : \" \");\n+\t\t\t}\n+\t\t\tout << ((i + 1 < R) ? \"], \" : \"]\");\n+\t\t}\n+\t\tout << \" }\";\n+\n+\t\treturn out.str();\n+\t}\n+\n+\tSpan<const T, C> operator[](size_t i) const\n+\t{\n+\t\treturn Span<const T, C>{ &data_.data()[i * C], C };\n+\t}\n+\n+\tSpan<T, C> operator[](size_t i)\n+\t{\n+\t\treturn Span<T, C>{ &data_.data()[i * C], C };\n+\t}\n+\n+private:\n+\tstd::vector<T> data_;\n+};\n+\n+template<typename T, typename U, unsigned int R, unsigned int C,\n+\t std::enable_if_t<std::is_arithmetic_v<T> && std::is_arithmetic_v<U>> * = nullptr>\n+Matrix<U, R, C> operator*(T d, const Matrix<U, R, C> &m)\n+{\n+\tMatrix<U, R, C> result;\n+\n+\tfor (unsigned int i = 0; i < R; i++)\n+\t\tfor (unsigned int j = 0; j < C; j++)\n+\t\t\tresult[i][j] = d * m[i][j];\n+\n+\treturn result;\n+}\n+\n+template<typename T,\n+\t unsigned int R1, unsigned int C1,\n+\t unsigned int R2, unsigned int C2,\n+\t std::enable_if_t<std::is_arithmetic_v<T> && C1 == R2> * = nullptr>\n+Matrix<T, R1, C2> operator*(const Matrix<T, R1, C1> &m1, const Matrix<T, R2, C2> &m2)\n+{\n+\tMatrix<T, R1, C2> result;\n+\n+\tfor (unsigned int i = 0; i < R1; i++) {\n+\t\tfor (unsigned int j = 0; j < C2; j++) {\n+\t\t\tT sum = 0;\n+\n+\t\t\tfor (unsigned int k = 0; k < C1; k++)\n+\t\t\t\tsum += m1[i][k] * m2[k][j];\n+\n+\t\t\tresult[i][j] = sum;\n+\t\t}\n+\t}\n+\n+\treturn result;\n+}\n+\n+template<typename T, unsigned int R, unsigned int C,\n+\t std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>\n+Matrix<T, R, C> operator+(const Matrix<T, R, C> &m1, const Matrix<T, R, C> &m2)\n+{\n+\tMatrix<T, R, C> result;\n+\n+\tfor (unsigned int i = 0; i < R; i++)\n+\t\tfor (unsigned int j = 0; j < C; j++)\n+\t\t\tresult[i][j] = m1[i][j] + m2[i][j];\n+\n+\treturn result;\n+}\n+\n+} /* namespace ipa */\n+\n+template<typename T, unsigned int R, unsigned int C>\n+std::ostream &operator<<(std::ostream &out, const ipa::Matrix<T, R, C> &m)\n+{\n+\tout << m.toString();\n+\treturn out;\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 1b3faf8d5..1e34355fe 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -7,6 +7,7 @@ libipa_headers = files([\n     'exposure_mode_helper.h',\n     'fc_queue.h',\n     'histogram.h',\n+    'matrix.h',\n     'module.h',\n     'pwl.h',\n ])\n@@ -18,6 +19,7 @@ libipa_sources = files([\n     'exposure_mode_helper.cpp',\n     'fc_queue.cpp',\n     'histogram.cpp',\n+    'matrix.cpp',\n     'module.cpp',\n     'pwl.cpp'\n ])\n",
    "prefixes": [
        "v5",
        "1/3"
    ]
}