[{"id":31209,"web_url":"https://patchwork.libcamera.org/comment/31209/","msgid":"<172622237516.3474483.7148290298257272825@ping.linuxembedded.co.uk>","date":"2024-09-13T10:12:55","subject":"Re: [PATCH v2 7/9] ipa: libipa: Add lsc polynomial class","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Stefan Klug (2024-09-13 08:57:25)\n> Add a basic class to represent polynomials as specified in the DNG spec\n> for vignetting correction.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> ---\n>  src/ipa/libipa/lsc_polynomial.cpp |  81 +++++++++++++++++++++++\n>  src/ipa/libipa/lsc_polynomial.h   | 104 ++++++++++++++++++++++++++++++\n>  src/ipa/libipa/meson.build        |   2 +\n>  3 files changed, 187 insertions(+)\n>  create mode 100644 src/ipa/libipa/lsc_polynomial.cpp\n>  create mode 100644 src/ipa/libipa/lsc_polynomial.h\n> \n> diff --git a/src/ipa/libipa/lsc_polynomial.cpp b/src/ipa/libipa/lsc_polynomial.cpp\n> new file mode 100644\n> index 000000000000..abbbcb6a724c\n> --- /dev/null\n> +++ b/src/ipa/libipa/lsc_polynomial.cpp\n> @@ -0,0 +1,81 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Ideas On Board\n> + *\n> + * Polynomal class to represent lens shading correction\n> + */\n> +\n> +#include \"lsc_polynomial.h\"\n> +\n> +#include <libcamera/base/log.h>\n> +\n> +/**\n> + * \\file lsc_polynomial.h\n> + * \\brief LscPolynomial class\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(LscPolynomial)\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\class LscPolynomial\n> + * \\brief Class for handling even polynomial used in lens shading correction\n\n'an even polynomial used in...'\n\nor\n\n'even polynomials used in...'\n\n> + *\n> + * Shading artifacts of camera lenses can be modeled using even radial\n> + * polynomials. This class implements a polynomial with 5 coefficients which\n> + * follows the definition of the FixVignetteRadial opcode in the Adobe DNG\n> + * specification.\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::LscPolynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0,\n> +                     double k1 = 0.0, double k2 = 0.0, double k3 = 0.0,\n> +                     double k4 = 0.0)\n> + * \\brief Construct a polynomial using the given coefficients\n> + * \\param cx Center-x relative to the image in normalized coordinates (0..1)\n> + * \\param cy Center-y relative to the image in normalized coordinates (0..1)\n> + * \\param k0 Coefficient of the polynomial\n> + * \\param k1 Coefficient of the polynomial\n> + * \\param k2 Coefficient of the polynomial\n> + * \\param k3 Coefficient of the polynomial\n> + * \\param k4 Coefficient of the polynomial\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::sampleAtNormalizedPixelPos(double x, double y)\n\nOhh it's Pwl::PointF all over again ;-)\n\n\n\n> + * \\brief Sample the polynomial at the given normalized pixel position\n> + *\n> + * This functions samples the polynomial at the given pixel position divided by\n> + * the value returned by getM().\n> + *\n> + * \\param x x position in normalized coordinates\n> + * \\param y y position in normalized coordinates\n> + * \\return The sampled value\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::getM()\n> + * \\brief Get the value m as described in the dng specification\n> + *\n> + * Returns m according to dng spec. m represents the Euclidean distance\n> + * (in pixels) from the optical center to the farthest pixel in the\n> + * image.\n> + *\n> + * \\return The sampled value\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::setReferenceImageSize(const Size &size)\n> + * \\brief Set the reference image size\n> + *\n> + * Set the reference image size that is used for subsequent calls to getM() and\n> + * sampleAtNormalizedPixelPos()\n> + *\n> + * \\param size THe size of the reference image\n\n/THe/The/\n\n\nThat's all I've got so :\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> + */\n> +\n> +} // namespace ipa\n> +} // namespace libcamera\n> diff --git a/src/ipa/libipa/lsc_polynomial.h b/src/ipa/libipa/lsc_polynomial.h\n> new file mode 100644\n> index 000000000000..890199017590\n> --- /dev/null\n> +++ b/src/ipa/libipa/lsc_polynomial.h\n> @@ -0,0 +1,104 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Ideas On Board\n> + *\n> + * Helper for radial polynomial used in lens shading correction.\n> + */\n> +#pragma once\n> +\n> +#include <algorithm>\n> +#include <array>\n> +#include <assert.h>\n> +#include <cmath>\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(LscPolynomial)\n> +\n> +namespace ipa {\n> +\n> +class LscPolynomial\n> +{\n> +public:\n> +       LscPolynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0,\n> +                     double k1 = 0.0, double k2 = 0.0, double k3 = 0.0,\n> +                     double k4 = 0.0)\n> +               : cx_(cx), cy_(cy),\n> +                 coefficients_({ k0, k1, k2, k3, k4 })\n> +       {\n> +       }\n> +\n> +       double sampleAtNormalizedPixelPos(double x, double y) const\n> +       {\n> +               double dx = x - cnx_;\n> +               double dy = y - cny_;\n> +               double r = sqrt(dx * dx + dy * dy);\n> +               double res = 1.0;\n> +               for (unsigned int i = 0; i < coefficients_.size(); i++) {\n> +                       res += coefficients_[i] * std::pow(r, (i + 1) * 2);\n> +               }\n> +               return res;\n> +       }\n> +\n> +       double getM() const\n> +       {\n> +               double cpx = imageSize_.width * cx_;\n> +               double cpy = imageSize_.height * cy_;\n> +               double mx = std::max(cpx, std::fabs(imageSize_.width - cpx));\n> +               double my = std::max(cpy, std::fabs(imageSize_.height - cpy));\n> +\n> +               return sqrt(mx * mx + my * my);\n> +       }\n> +\n> +       void setReferenceImageSize(const Size &size)\n> +       {\n> +               imageSize_ = size;\n> +\n> +               /* Calculate normalized centers */\n> +               double m = getM();\n> +               cnx_ = (size.width * cx_) / m;\n> +               cny_ = (size.height * cy_) / m;\n> +       }\n> +\n> +private:\n> +       double cx_;\n> +       double cy_;\n> +       double cnx_;\n> +       double cny_;\n> +       std::array<double, 5> coefficients_;\n> +\n> +       Size imageSize_;\n> +};\n> +\n> +} /* namespace ipa */\n> +\n> +#ifndef __DOXYGEN__\n> +\n> +template<>\n> +struct YamlObject::Getter<ipa::LscPolynomial> {\n> +       std::optional<ipa::LscPolynomial> get(const YamlObject &obj) const\n> +       {\n> +               std::optional<double> cx = obj[\"cx\"].get<double>();\n> +               std::optional<double> cy = obj[\"cy\"].get<double>();\n> +               std::optional<double> k0 = obj[\"k0\"].get<double>();\n> +               std::optional<double> k1 = obj[\"k1\"].get<double>();\n> +               std::optional<double> k2 = obj[\"k2\"].get<double>();\n> +               std::optional<double> k3 = obj[\"k3\"].get<double>();\n> +               std::optional<double> k4 = obj[\"k4\"].get<double>();\n> +\n> +               if (!(cx && cy && k0 && k1 && k2 && k3 && k4))\n> +                       LOG(LscPolynomial, Error)\n> +                               << \"Polynomial is missing a parameter\";\n> +\n> +               return ipa::LscPolynomial(*cx, *cy, *k0, *k1, *k2, *k3, *k4);\n> +       }\n> +};\n> +\n> +#endif\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index 3740178b2909..e78cbcd63633 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>      'interpolator.h',\n> +    'lsc_polynomial.h',\n>      'matrix.h',\n>      'module.h',\n>      'pwl.h',\n> @@ -22,6 +23,7 @@ libipa_sources = files([\n>      'fc_queue.cpp',\n>      'histogram.cpp',\n>      'interpolator.cpp',\n> +    'lsc_polynomial.cpp',\n>      'matrix.cpp',\n>      'module.cpp',\n>      'pwl.cpp',\n> -- \n> 2.43.0\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 DB3CBC3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 13 Sep 2024 10:13:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 557B4634F8;\n\tFri, 13 Sep 2024 12:13:01 +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 9DE96634E3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 13 Sep 2024 12:12:59 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 04A601083;\n\tFri, 13 Sep 2024 12:11:39 +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=\"izo5W7Zh\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726222300;\n\tbh=Qew10QDFJ7CkLGMmJ5bHljOG403Y55bvWKUj8fMd/MU=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=izo5W7Zhrys41DSkgTWF96WbJbU2nljn21gHkXUC31XssqPZQCc/xGNpGqSGdaf2C\n\t1X7NpylXrezwxVBR7wzwscDKkHdhHg426dKQCarRVZglPEgXs5xhvLcAL+a1C9soef\n\tAB4f975zhFwEEzc9gj2lYJawWJrl8awiq69KhLBU=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240913075750.35115-8-stefan.klug@ideasonboard.com>","References":"<20240913075750.35115-1-stefan.klug@ideasonboard.com>\n\t<20240913075750.35115-8-stefan.klug@ideasonboard.com>","Subject":"Re: [PATCH v2 7/9] ipa: libipa: Add lsc polynomial class","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 13 Sep 2024 11:12:55 +0100","Message-ID":"<172622237516.3474483.7148290298257272825@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}},{"id":31218,"web_url":"https://patchwork.libcamera.org/comment/31218/","msgid":"<ZuQc2CE8vYb54deZ@pyrite.rasen.tech>","date":"2024-09-13T11:07:04","subject":"Re: [PATCH v2 7/9] ipa: libipa: Add lsc polynomial class","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"On Fri, Sep 13, 2024 at 09:57:25AM +0200, Stefan Klug wrote:\n> Add a basic class to represent polynomials as specified in the DNG spec\n> for vignetting correction.\n> \n> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> ---\n>  src/ipa/libipa/lsc_polynomial.cpp |  81 +++++++++++++++++++++++\n>  src/ipa/libipa/lsc_polynomial.h   | 104 ++++++++++++++++++++++++++++++\n>  src/ipa/libipa/meson.build        |   2 +\n>  3 files changed, 187 insertions(+)\n>  create mode 100644 src/ipa/libipa/lsc_polynomial.cpp\n>  create mode 100644 src/ipa/libipa/lsc_polynomial.h\n> \n> diff --git a/src/ipa/libipa/lsc_polynomial.cpp b/src/ipa/libipa/lsc_polynomial.cpp\n> new file mode 100644\n> index 000000000000..abbbcb6a724c\n> --- /dev/null\n> +++ b/src/ipa/libipa/lsc_polynomial.cpp\n> @@ -0,0 +1,81 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Ideas On Board\n> + *\n> + * Polynomal class to represent lens shading correction\n\ns/Polynomal/Polynomial/\n\n> + */\n> +\n> +#include \"lsc_polynomial.h\"\n> +\n> +#include <libcamera/base/log.h>\n> +\n> +/**\n> + * \\file lsc_polynomial.h\n> + * \\brief LscPolynomial class\n> + */\n> +\n> +namespace libcamera {\n> +\n> +LOG_DEFINE_CATEGORY(LscPolynomial)\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\class LscPolynomial\n> + * \\brief Class for handling even polynomial used in lens shading correction\n> + *\n> + * Shading artifacts of camera lenses can be modeled using even radial\n> + * polynomials. This class implements a polynomial with 5 coefficients which\n> + * follows the definition of the FixVignetteRadial opcode in the Adobe DNG\n> + * specification.\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::LscPolynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0,\n> +\t\t      double k1 = 0.0, double k2 = 0.0, double k3 = 0.0,\n> +\t\t      double k4 = 0.0)\n> + * \\brief Construct a polynomial using the given coefficients\n> + * \\param cx Center-x relative to the image in normalized coordinates (0..1)\n> + * \\param cy Center-y relative to the image in normalized coordinates (0..1)\n> + * \\param k0 Coefficient of the polynomial\n> + * \\param k1 Coefficient of the polynomial\n> + * \\param k2 Coefficient of the polynomial\n> + * \\param k3 Coefficient of the polynomial\n> + * \\param k4 Coefficient of the polynomial\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::sampleAtNormalizedPixelPos(double x, double y)\n> + * \\brief Sample the polynomial at the given normalized pixel position\n> + *\n> + * This functions samples the polynomial at the given pixel position divided by\n> + * the value returned by getM().\n> + *\n> + * \\param x x position in normalized coordinates\n> + * \\param y y position in normalized coordinates\n> + * \\return The sampled value\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::getM()\n> + * \\brief Get the value m as described in the dng specification\n> + *\n> + * Returns m according to dng spec. m represents the Euclidean distance\n> + * (in pixels) from the optical center to the farthest pixel in the\n> + * image.\n> + *\n> + * \\return The sampled value\n> + */\n> +\n> +/**\n> + * \\fn LscPolynomial::setReferenceImageSize(const Size &size)\n> + * \\brief Set the reference image size\n> + *\n> + * Set the reference image size that is used for subsequent calls to getM() and\n> + * sampleAtNormalizedPixelPos()\n> + *\n> + * \\param size THe size of the reference image\n> + */\n> +\n> +} // namespace ipa\n> +} // namespace libcamera\n> diff --git a/src/ipa/libipa/lsc_polynomial.h b/src/ipa/libipa/lsc_polynomial.h\n> new file mode 100644\n> index 000000000000..890199017590\n> --- /dev/null\n> +++ b/src/ipa/libipa/lsc_polynomial.h\n> @@ -0,0 +1,104 @@\n> +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> +/*\n> + * Copyright (C) 2024, Ideas On Board\n> + *\n> + * Helper for radial polynomial used in lens shading correction.\n> + */\n> +#pragma once\n> +\n> +#include <algorithm>\n> +#include <array>\n> +#include <assert.h>\n> +#include <cmath>\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(LscPolynomial)\n> +\n> +namespace ipa {\n> +\n> +class LscPolynomial\n> +{\n> +public:\n> +\tLscPolynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0,\n> +\t\t      double k1 = 0.0, double k2 = 0.0, double k3 = 0.0,\n> +\t\t      double k4 = 0.0)\n> +\t\t: cx_(cx), cy_(cy),\n> +\t\t  coefficients_({ k0, k1, k2, k3, k4 })\n> +\t{\n> +\t}\n> +\n> +\tdouble sampleAtNormalizedPixelPos(double x, double y) const\n> +\t{\n> +\t\tdouble dx = x - cnx_;\n> +\t\tdouble dy = y - cny_;\n\nShould cn{x,y}_ be initialized above?\n\n\nOtherwise it looks fine.\n\nPaul\n\n> +\t\tdouble r = sqrt(dx * dx + dy * dy);\n> +\t\tdouble res = 1.0;\n> +\t\tfor (unsigned int i = 0; i < coefficients_.size(); i++) {\n> +\t\t\tres += coefficients_[i] * std::pow(r, (i + 1) * 2);\n> +\t\t}\n> +\t\treturn res;\n> +\t}\n> +\n> +\tdouble getM() const\n> +\t{\n> +\t\tdouble cpx = imageSize_.width * cx_;\n> +\t\tdouble cpy = imageSize_.height * cy_;\n> +\t\tdouble mx = std::max(cpx, std::fabs(imageSize_.width - cpx));\n> +\t\tdouble my = std::max(cpy, std::fabs(imageSize_.height - cpy));\n> +\n> +\t\treturn sqrt(mx * mx + my * my);\n> +\t}\n> +\n> +\tvoid setReferenceImageSize(const Size &size)\n> +\t{\n> +\t\timageSize_ = size;\n> +\n> +\t\t/* Calculate normalized centers */\n> +\t\tdouble m = getM();\n> +\t\tcnx_ = (size.width * cx_) / m;\n> +\t\tcny_ = (size.height * cy_) / m;\n> +\t}\n> +\n> +private:\n> +\tdouble cx_;\n> +\tdouble cy_;\n> +\tdouble cnx_;\n> +\tdouble cny_;\n> +\tstd::array<double, 5> coefficients_;\n> +\n> +\tSize imageSize_;\n> +};\n> +\n> +} /* namespace ipa */\n> +\n> +#ifndef __DOXYGEN__\n> +\n> +template<>\n> +struct YamlObject::Getter<ipa::LscPolynomial> {\n> +\tstd::optional<ipa::LscPolynomial> get(const YamlObject &obj) const\n> +\t{\n> +\t\tstd::optional<double> cx = obj[\"cx\"].get<double>();\n> +\t\tstd::optional<double> cy = obj[\"cy\"].get<double>();\n> +\t\tstd::optional<double> k0 = obj[\"k0\"].get<double>();\n> +\t\tstd::optional<double> k1 = obj[\"k1\"].get<double>();\n> +\t\tstd::optional<double> k2 = obj[\"k2\"].get<double>();\n> +\t\tstd::optional<double> k3 = obj[\"k3\"].get<double>();\n> +\t\tstd::optional<double> k4 = obj[\"k4\"].get<double>();\n> +\n> +\t\tif (!(cx && cy && k0 && k1 && k2 && k3 && k4))\n> +\t\t\tLOG(LscPolynomial, Error)\n> +\t\t\t\t<< \"Polynomial is missing a parameter\";\n> +\n> +\t\treturn ipa::LscPolynomial(*cx, *cy, *k0, *k1, *k2, *k3, *k4);\n> +\t}\n> +};\n> +\n> +#endif\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index 3740178b2909..e78cbcd63633 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>      'interpolator.h',\n> +    'lsc_polynomial.h',\n>      'matrix.h',\n>      'module.h',\n>      'pwl.h',\n> @@ -22,6 +23,7 @@ libipa_sources = files([\n>      'fc_queue.cpp',\n>      'histogram.cpp',\n>      'interpolator.cpp',\n> +    'lsc_polynomial.cpp',\n>      'matrix.cpp',\n>      'module.cpp',\n>      'pwl.cpp',\n> -- \n> 2.43.0\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 6BB26C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 13 Sep 2024 11:07:12 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3D3A2634E3;\n\tFri, 13 Sep 2024 13:07:11 +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 347FD634E3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 13 Sep 2024 13:07:09 +0200 (CEST)","from pyrite.rasen.tech (213-229-8-243.static.upcbusiness.at\n\t[213.229.8.243])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3DEC23D5;\n\tFri, 13 Sep 2024 13:05:50 +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=\"LwndwkxC\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726225550;\n\tbh=sbafbvMkddgRQWTQiirB+dP9RB+99V28cDVlyy+9McE=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=LwndwkxCBsKE7FOpSIw2F2uvonsRRdd7W3BlvPKNJjN5W9y4YgZd+ZR0yRHOpvhBw\n\t6Q3lh21Ed+N/qdykEIsx8ododJJOg92dkt8lZBgBYiTFSAk7/pXxSal8/XP31lK/QT\n\tFvneLG7jSmbExlJEXiJuefAsBGBSUrzw7eXdT0iE=","Date":"Fri, 13 Sep 2024 13:07:04 +0200","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [PATCH v2 7/9] ipa: libipa: Add lsc polynomial class","Message-ID":"<ZuQc2CE8vYb54deZ@pyrite.rasen.tech>","References":"<20240913075750.35115-1-stefan.klug@ideasonboard.com>\n\t<20240913075750.35115-8-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20240913075750.35115-8-stefan.klug@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>"}},{"id":31249,"web_url":"https://patchwork.libcamera.org/comment/31249/","msgid":"<vfpvzrjn7mtxgvyri66rl7dfqjt2x4ucz7euhksxbwpczxkatr@efcehrrjykhi>","date":"2024-09-16T15:38:22","subject":"Re: [PATCH v2 7/9] ipa: libipa: Add lsc polynomial class","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the review. \n\nOn Fri, Sep 13, 2024 at 01:07:04PM +0200, Paul Elder wrote:\n> On Fri, Sep 13, 2024 at 09:57:25AM +0200, Stefan Klug wrote:\n> > Add a basic class to represent polynomials as specified in the DNG spec\n> > for vignetting correction.\n> > \n> > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n> > ---\n> >  src/ipa/libipa/lsc_polynomial.cpp |  81 +++++++++++++++++++++++\n> >  src/ipa/libipa/lsc_polynomial.h   | 104 ++++++++++++++++++++++++++++++\n> >  src/ipa/libipa/meson.build        |   2 +\n> >  3 files changed, 187 insertions(+)\n> >  create mode 100644 src/ipa/libipa/lsc_polynomial.cpp\n> >  create mode 100644 src/ipa/libipa/lsc_polynomial.h\n> > \n> > diff --git a/src/ipa/libipa/lsc_polynomial.cpp b/src/ipa/libipa/lsc_polynomial.cpp\n> > new file mode 100644\n> > index 000000000000..abbbcb6a724c\n> > --- /dev/null\n> > +++ b/src/ipa/libipa/lsc_polynomial.cpp\n> > @@ -0,0 +1,81 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2024, Ideas On Board\n> > + *\n> > + * Polynomal class to represent lens shading correction\n> \n> s/Polynomal/Polynomial/\n> \n> > + */\n> > +\n> > +#include \"lsc_polynomial.h\"\n> > +\n> > +#include <libcamera/base/log.h>\n> > +\n> > +/**\n> > + * \\file lsc_polynomial.h\n> > + * \\brief LscPolynomial class\n> > + */\n> > +\n> > +namespace libcamera {\n> > +\n> > +LOG_DEFINE_CATEGORY(LscPolynomial)\n> > +\n> > +namespace ipa {\n> > +\n> > +/**\n> > + * \\class LscPolynomial\n> > + * \\brief Class for handling even polynomial used in lens shading correction\n> > + *\n> > + * Shading artifacts of camera lenses can be modeled using even radial\n> > + * polynomials. This class implements a polynomial with 5 coefficients which\n> > + * follows the definition of the FixVignetteRadial opcode in the Adobe DNG\n> > + * specification.\n> > + */\n> > +\n> > +/**\n> > + * \\fn LscPolynomial::LscPolynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0,\n> > +\t\t      double k1 = 0.0, double k2 = 0.0, double k3 = 0.0,\n> > +\t\t      double k4 = 0.0)\n> > + * \\brief Construct a polynomial using the given coefficients\n> > + * \\param cx Center-x relative to the image in normalized coordinates (0..1)\n> > + * \\param cy Center-y relative to the image in normalized coordinates (0..1)\n> > + * \\param k0 Coefficient of the polynomial\n> > + * \\param k1 Coefficient of the polynomial\n> > + * \\param k2 Coefficient of the polynomial\n> > + * \\param k3 Coefficient of the polynomial\n> > + * \\param k4 Coefficient of the polynomial\n> > + */\n> > +\n> > +/**\n> > + * \\fn LscPolynomial::sampleAtNormalizedPixelPos(double x, double y)\n> > + * \\brief Sample the polynomial at the given normalized pixel position\n> > + *\n> > + * This functions samples the polynomial at the given pixel position divided by\n> > + * the value returned by getM().\n> > + *\n> > + * \\param x x position in normalized coordinates\n> > + * \\param y y position in normalized coordinates\n> > + * \\return The sampled value\n> > + */\n> > +\n> > +/**\n> > + * \\fn LscPolynomial::getM()\n> > + * \\brief Get the value m as described in the dng specification\n> > + *\n> > + * Returns m according to dng spec. m represents the Euclidean distance\n> > + * (in pixels) from the optical center to the farthest pixel in the\n> > + * image.\n> > + *\n> > + * \\return The sampled value\n> > + */\n> > +\n> > +/**\n> > + * \\fn LscPolynomial::setReferenceImageSize(const Size &size)\n> > + * \\brief Set the reference image size\n> > + *\n> > + * Set the reference image size that is used for subsequent calls to getM() and\n> > + * sampleAtNormalizedPixelPos()\n> > + *\n> > + * \\param size THe size of the reference image\n> > + */\n> > +\n> > +} // namespace ipa\n> > +} // namespace libcamera\n> > diff --git a/src/ipa/libipa/lsc_polynomial.h b/src/ipa/libipa/lsc_polynomial.h\n> > new file mode 100644\n> > index 000000000000..890199017590\n> > --- /dev/null\n> > +++ b/src/ipa/libipa/lsc_polynomial.h\n> > @@ -0,0 +1,104 @@\n> > +/* SPDX-License-Identifier: LGPL-2.1-or-later */\n> > +/*\n> > + * Copyright (C) 2024, Ideas On Board\n> > + *\n> > + * Helper for radial polynomial used in lens shading correction.\n> > + */\n> > +#pragma once\n> > +\n> > +#include <algorithm>\n> > +#include <array>\n> > +#include <assert.h>\n> > +#include <cmath>\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(LscPolynomial)\n> > +\n> > +namespace ipa {\n> > +\n> > +class LscPolynomial\n> > +{\n> > +public:\n> > +\tLscPolynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0,\n> > +\t\t      double k1 = 0.0, double k2 = 0.0, double k3 = 0.0,\n> > +\t\t      double k4 = 0.0)\n> > +\t\t: cx_(cx), cy_(cy),\n> > +\t\t  coefficients_({ k0, k1, k2, k3, k4 })\n> > +\t{\n> > +\t}\n> > +\n> > +\tdouble sampleAtNormalizedPixelPos(double x, double y) const\n> > +\t{\n> > +\t\tdouble dx = x - cnx_;\n> > +\t\tdouble dy = y - cny_;\n> \n> Should cn{x,y}_ be initialized above?\n\nYes, you're right. I'll initialize them in the constructor.\n\nCheers,\nStefan\n\n> \n> \n> Otherwise it looks fine.\n> \n> Paul\n> \n> > +\t\tdouble r = sqrt(dx * dx + dy * dy);\n> > +\t\tdouble res = 1.0;\n> > +\t\tfor (unsigned int i = 0; i < coefficients_.size(); i++) {\n> > +\t\t\tres += coefficients_[i] * std::pow(r, (i + 1) * 2);\n> > +\t\t}\n> > +\t\treturn res;\n> > +\t}\n> > +\n> > +\tdouble getM() const\n> > +\t{\n> > +\t\tdouble cpx = imageSize_.width * cx_;\n> > +\t\tdouble cpy = imageSize_.height * cy_;\n> > +\t\tdouble mx = std::max(cpx, std::fabs(imageSize_.width - cpx));\n> > +\t\tdouble my = std::max(cpy, std::fabs(imageSize_.height - cpy));\n> > +\n> > +\t\treturn sqrt(mx * mx + my * my);\n> > +\t}\n> > +\n> > +\tvoid setReferenceImageSize(const Size &size)\n> > +\t{\n> > +\t\timageSize_ = size;\n> > +\n> > +\t\t/* Calculate normalized centers */\n> > +\t\tdouble m = getM();\n> > +\t\tcnx_ = (size.width * cx_) / m;\n> > +\t\tcny_ = (size.height * cy_) / m;\n> > +\t}\n> > +\n> > +private:\n> > +\tdouble cx_;\n> > +\tdouble cy_;\n> > +\tdouble cnx_;\n> > +\tdouble cny_;\n> > +\tstd::array<double, 5> coefficients_;\n> > +\n> > +\tSize imageSize_;\n> > +};\n> > +\n> > +} /* namespace ipa */\n> > +\n> > +#ifndef __DOXYGEN__\n> > +\n> > +template<>\n> > +struct YamlObject::Getter<ipa::LscPolynomial> {\n> > +\tstd::optional<ipa::LscPolynomial> get(const YamlObject &obj) const\n> > +\t{\n> > +\t\tstd::optional<double> cx = obj[\"cx\"].get<double>();\n> > +\t\tstd::optional<double> cy = obj[\"cy\"].get<double>();\n> > +\t\tstd::optional<double> k0 = obj[\"k0\"].get<double>();\n> > +\t\tstd::optional<double> k1 = obj[\"k1\"].get<double>();\n> > +\t\tstd::optional<double> k2 = obj[\"k2\"].get<double>();\n> > +\t\tstd::optional<double> k3 = obj[\"k3\"].get<double>();\n> > +\t\tstd::optional<double> k4 = obj[\"k4\"].get<double>();\n> > +\n> > +\t\tif (!(cx && cy && k0 && k1 && k2 && k3 && k4))\n> > +\t\t\tLOG(LscPolynomial, Error)\n> > +\t\t\t\t<< \"Polynomial is missing a parameter\";\n> > +\n> > +\t\treturn ipa::LscPolynomial(*cx, *cy, *k0, *k1, *k2, *k3, *k4);\n> > +\t}\n> > +};\n> > +\n> > +#endif\n> > +\n> > +} /* namespace libcamera */\n> > diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> > index 3740178b2909..e78cbcd63633 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> >      'interpolator.h',\n> > +    'lsc_polynomial.h',\n> >      'matrix.h',\n> >      'module.h',\n> >      'pwl.h',\n> > @@ -22,6 +23,7 @@ libipa_sources = files([\n> >      'fc_queue.cpp',\n> >      'histogram.cpp',\n> >      'interpolator.cpp',\n> > +    'lsc_polynomial.cpp',\n> >      'matrix.cpp',\n> >      'module.cpp',\n> >      'pwl.cpp',\n> > -- \n> > 2.43.0\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 2011BC3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 16 Sep 2024 15:38:30 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 01D0A634FD;\n\tMon, 16 Sep 2024 17:38:29 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 40D46634F5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Sep 2024 17:38:27 +0200 (CEST)","from ideasonboard.com (unknown\n\t[IPv6:2001:4bc9:a45:b0af:8993:ee4e:ea06:9114])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 996F1480;\n\tMon, 16 Sep 2024 17:37:05 +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=\"mSVMYs9O\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726501026;\n\tbh=DSRqIVUL5FMfdXbaRSAYQJ72Z1YrnhpTHYC6V0RGLe8=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=mSVMYs9OuhfLgdmZA4mHqYU9ZC6EVIyqRgIGdSHzr+GB96XbBsmS2B3oqZ2Y76Vcy\n\t/SwkPYsjcUXdMzwXojUGFlcuE6MDNyZ1Cx8rL/IWft4UPGabkV4SZIeOqWpatPLcHO\n\tTdEpmKhGZ45dOn8dX+AePKxKXQVFPPkHABEOwuBY=","Date":"Mon, 16 Sep 2024 17:38:22 +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 7/9] ipa: libipa: Add lsc polynomial class","Message-ID":"<vfpvzrjn7mtxgvyri66rl7dfqjt2x4ucz7euhksxbwpczxkatr@efcehrrjykhi>","References":"<20240913075750.35115-1-stefan.klug@ideasonboard.com>\n\t<20240913075750.35115-8-stefan.klug@ideasonboard.com>\n\t<ZuQc2CE8vYb54deZ@pyrite.rasen.tech>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<ZuQc2CE8vYb54deZ@pyrite.rasen.tech>","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>"}}]