[{"id":29301,"web_url":"https://patchwork.libcamera.org/comment/29301/","msgid":"<20240422192512.zk5hvawdad4gwgd3@jasper>","date":"2024-04-22T19:25:12","subject":"Re: [PATCH v2 2/3] ipa: libipa: Add MatrixInterpolator class","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Paul,\n\nthanks for the patch.\n\nOn Fri, Apr 19, 2024 at 04:18:18PM +0900, Paul Elder wrote:\n> Add a class to encapsulate the functionality of fetching a matrix based\n> on an integer key, and interpolating if there is no exact match. This is\n> expected to be used by both color correction matrices / crosstalk\n> correction as well as lens shading correction.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> \n> ---\n> Changes in v2:\n> - initialize to identity matrix\n> - add a function to reset to identity matrix\n> - other minor fixes\n> ---\n>  src/ipa/libipa/matrix_interpolator.cpp |  54 +++++++++++++\n>  src/ipa/libipa/matrix_interpolator.h   | 105 +++++++++++++++++++++++++\n>  src/ipa/libipa/meson.build             |   2 +\n>  3 files changed, 161 insertions(+)\n>  create mode 100644 src/ipa/libipa/matrix_interpolator.cpp\n>  create mode 100644 src/ipa/libipa/matrix_interpolator.h\n> \n> diff --git a/src/ipa/libipa/matrix_interpolator.cpp b/src/ipa/libipa/matrix_interpolator.cpp\n> new file mode 100644\n> index 00000000..fc1837e7\n> --- /dev/null\n> +++ b/src/ipa/libipa/matrix_interpolator.cpp\n> @@ -0,0 +1,54 @@\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_interpolator.cpp - Helper class for interpolating maps of matrices\n> + */\n> +#include \"matrix_interpolator.h\"\n> +\n> +#include <algorithm>\n> +#include <string>\n> +\n> +#include <libcamera/base/log.h>\n> +\n> +#include \"libcamera/internal/yaml_parser.h\"\n> +\n> +#include \"matrix.h\"\n> +\n> +/**\n> + * \\file ccm.h\n> + * \\brief Helper class for interpolating maps of matrices\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(MatrixInterpolator)\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\class MatrixInterpolator\n> + * \\brief Class for storing, retrieving, and interpolating matrices\n> + */\n> +\n> +/**\n> + * \\fn int MatrixInterpolator<T, R, C>::readYaml(const libcamera::YamlObject &yaml)\n> + * \\brief Initialize an MatrixInterpolator instance from yaml\n> + * \\tparam T Type of data stored in the matrices\n> + * \\tparam R Number of rows of the matrices\n> + * \\tparam C Number of columns of the matrices\n> + * \\param[in] yaml The yaml object that contains the map of unsigned integers to matrices\n> + * \\return Zero on success, negative error code otherwise\n> + */\n> +\n> +/**\n> + * \\fn Matrix<T, R, C> MatrixInterpolator<T, R, C>::get(unsigned int key)\n> + * \\brief Retrieve a matrix from the list of matrices, interpolating if necessary\n> + * \\param[in] key The unsigned integer key of the matrix to retrieve\n> + * \\return The matrix corresponding to the color temperature\n> + */\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/matrix_interpolator.h b/src/ipa/libipa/matrix_interpolator.h\n> new file mode 100644\n> index 00000000..ddc7b350\n> --- /dev/null\n> +++ b/src/ipa/libipa/matrix_interpolator.h\n> @@ -0,0 +1,105 @@\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_interpolator.h - Helper class for interpolating maps of matrices\n> + */\n> +\n> +#pragma once\n> +\n> +#include <algorithm>\n> +#include <map>\n> +#include <string>\n> +#include <tuple>\n> +\n> +#include <libcamera/base/log.h>\n> +\n> +#include \"libcamera/internal/yaml_parser.h\"\n> +\n> +#include \"matrix.h\"\n> +\n> +namespace libcamera {\n> +\n> +LOG_DECLARE_CATEGORY(MatrixInterpolator)\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 MatrixInterpolator\n> +{\n> +public:\n> +\tMatrixInterpolator()\n> +\t{\n> +\t\treset();\n> +\t};\n> +\n> +\t~MatrixInterpolator(){};\n> +\n> +\tvoid reset()\n> +\t{\n> +\t\tMatrix<T, R, C> unit;\n> +\t\tmatrices_[0] = unit;\n\nI thinks that stems from a my bad english :-) As Kieran wrote in another\nmail it should be called \"identity\".\n\nI wonder however if reset should actually add a matrix. It makes\nreadYaml more complicated and would be easy to catch in get().\n\n> +\t}\n> +\n> +\tint readYaml(const libcamera::YamlObject &yaml)\n> +\t{\n> +\t\tint ret;\n\nHere matrices must be erased. Otherwise he one from reset is still in\nthere (or the ones from a previous readYaml).\n\n> +\n> +\t\tif (!yaml.isDictionary()) {\n> +\t\t\tLOG(MatrixInterpolator, Error) << \"yaml object must be a dictionary\";\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\tfor (const auto &[ctStr, value] : yaml.asDict()) {\n> +\t\t\tunsigned int ct = std::stoul(ctStr);\n> +\t\t\tMatrix<T, R, C> matrix;\n> +\t\t\tif ((ret = matrix.readYaml(value)) < 0) {\n> +\t\t\t\tLOG(MatrixInterpolator, Error) << \"Failed to read matrix\";\n> +\t\t\t\treturn ret;\n> +\t\t\t}\n> +\n> +\t\t\tmatrices_[ct] = matrix;\n> +\n> +\t\t\tLOG(MatrixInterpolator, Debug)\n> +\t\t\t\t<< \"Read matrix for key \" << ct << \": \"\n> +\t\t\t\t<< matrices_[ct].toString();\n> +\t\t}\n> +\n> +\t\tif (matrices_.size() < 1) {\n> +\t\t\tLOG(MatrixInterpolator, Error) << \"Need at least one matrix\";\n> +\t\t\treturn -EINVAL;\n> +\t\t}\n> +\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tMatrix<T, R, C> get(unsigned int ct)\n> +\t{\n\nI think a check for matrices_.size() == 0 wouldn't hurt here. Or even\nbetter a assert, as there is no way to continue in a sensible way.\n\nRegards,\nStefan\n\n> +\t\tif (matrices_.size() == 1 ||\n> +\t\t    ct <= matrices_.begin()->first)\n> +\t\t\treturn matrices_.begin()->second;\n> +\n> +\t\tif (ct >= matrices_.rbegin()->first)\n> +\t\t\treturn matrices_.rbegin()->second;\n> +\n> +\t\tif (matrices_.count(ct))\n> +\t\t\treturn matrices_[ct];\n> +\n> +\t\t/* The above four guarantee that this will succeed */\n> +\t\tauto iter = matrices_.upper_bound(ct);\n> +\t\tunsigned int ctUpper = iter->first;\n> +\t\tunsigned int ctLower = (--iter)->first;\n> +\n> +\t\tdouble lambda = (ct - ctLower) / static_cast<double>(ctUpper - ctLower);\n> +\t\treturn lambda * matrices_[ctUpper] + (1.0 - lambda) * matrices_[ctLower];\n> +\t}\n> +\n> +private:\n> +\tstd::map<unsigned int, Matrix<T, R, C>> matrices_;\n> +};\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index 1e34355f..305acf64 100644\n> --- a/src/ipa/libipa/meson.build\n> +++ b/src/ipa/libipa/meson.build\n> @@ -8,6 +8,7 @@ libipa_headers = files([\n>      'fc_queue.h',\n>      'histogram.h',\n>      'matrix.h',\n> +    'matrix_interpolator.h',\n>      'module.h',\n>      'pwl.h',\n>  ])\n> @@ -20,6 +21,7 @@ libipa_sources = files([\n>      'fc_queue.cpp',\n>      'histogram.cpp',\n>      'matrix.cpp',\n> +    'matrix_interpolator.cpp',\n>      'module.cpp',\n>      'pwl.cpp'\n>  ])\n> -- \n> 2.39.2\n>","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 B9A74BE08B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 22 Apr 2024 19:25:18 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id A59A463412;\n\tMon, 22 Apr 2024 21:25:17 +0200 (CEST)","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 4DF9563408\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 22 Apr 2024 21:25:16 +0200 (CEST)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:8cc8:e8af:967f:28a5])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 5A11B8CC;\n\tMon, 22 Apr 2024 21:24:25 +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=\"Mjp2GfTW\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1713813865;\n\tbh=lzfDaqQU5PD++GJOnUjqZZleqre0bQLIhW96Bl6n998=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=Mjp2GfTWbeLEjRvascTTyC0eSTIADgGKAhof3VxfZTUgc4cy6bKce42iEv0LsoQZw\n\tlj7tpO/t4etw4WGBo0tZ6qFBPObEZbkQbq9txgW9k2b4oSZ8lVOyiegNf5gUQ0Mun0\n\tyTJcGC2nvuoDAtwVSx/jPHnRXllXvkQUYuMhDxMA=","Date":"Mon, 22 Apr 2024 21:25:12 +0200","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2 2/3] ipa: libipa: Add MatrixInterpolator class","Message-ID":"<20240422192512.zk5hvawdad4gwgd3@jasper>","References":"<20240419071819.1325791-1-paul.elder@ideasonboard.com>\n\t<20240419071819.1325791-3-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240419071819.1325791-3-paul.elder@ideasonboard.com>","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>"}}]