From patchwork Mon Aug 26 15:22:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 21016 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D3507C324C for ; Mon, 26 Aug 2024 15:22:47 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 63F466341D; Mon, 26 Aug 2024 17:22:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="cPRy9pvF"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 85B99633D4 for ; Mon, 26 Aug 2024 17:22:45 +0200 (CEST) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:58b7:f3d:c9d4:defa]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 4207E741; Mon, 26 Aug 2024 17:21:39 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1724685699; bh=AOKaijGCWZOCM1rIBzUsWumA2GsBFuu9vxGIx69KhO4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cPRy9pvFnplQg1JUkkde5YK5iWoSmmDqB5K/+2moJ4rKEJA1BP4g7C9ssVyY1iDjs 7SJ7lJCGfiZ0FiiIDlYphjmE6YQF1wu2x5rHt9HL6kBaly3ECOshebjSECjduCTnQ6 C29QhI3vfcnTmwQe4Qp6WRH3ecGJ7GO2c6iLPvOE= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH v1 6/8] ipa: libipa: Add lsc polynomial class Date: Mon, 26 Aug 2024 17:22:04 +0200 Message-ID: <20240826152224.362773-7-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240826152224.362773-1-stefan.klug@ideasonboard.com> References: <20240826152224.362773-1-stefan.klug@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a basic class to represent polynomials as specified in the DNG spec for vignetting correction. Signed-off-by: Stefan Klug --- src/ipa/libipa/meson.build | 2 + src/ipa/libipa/polynomial.cpp | 23 ++++++++ src/ipa/libipa/polynomial.h | 107 ++++++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 src/ipa/libipa/polynomial.cpp create mode 100644 src/ipa/libipa/polynomial.h diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build index 3740178b2909..8b085750807a 100644 --- a/src/ipa/libipa/meson.build +++ b/src/ipa/libipa/meson.build @@ -10,6 +10,7 @@ libipa_headers = files([ 'interpolator.h', 'matrix.h', 'module.h', + 'polynomial.h', 'pwl.h', 'vector.h', ]) @@ -24,6 +25,7 @@ libipa_sources = files([ 'interpolator.cpp', 'matrix.cpp', 'module.cpp', + 'polynomial.cpp', 'pwl.cpp', 'vector.cpp', ]) diff --git a/src/ipa/libipa/polynomial.cpp b/src/ipa/libipa/polynomial.cpp new file mode 100644 index 000000000000..39b17b29314b --- /dev/null +++ b/src/ipa/libipa/polynomial.cpp @@ -0,0 +1,23 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Ideas On Board + * + * Polynomal class to represent lens shading correction + */ + +#include "polynomial.h" + +#include + +/** + * \file polynomial.h + * \brief Polynomial class + */ + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Polynomial) + +namespace ipa { +} +} // namespace libcamera diff --git a/src/ipa/libipa/polynomial.h b/src/ipa/libipa/polynomial.h new file mode 100644 index 000000000000..971be062297f --- /dev/null +++ b/src/ipa/libipa/polynomial.h @@ -0,0 +1,107 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Ideas On Board + * + * Vector and related operations + */ +#pragma once + +#include +#include +#include +#include + +#include +#include + +#include "libcamera/internal/yaml_parser.h" + +namespace libcamera { + +LOG_DECLARE_CATEGORY(Polynomial) + +namespace ipa { + +constexpr int kNumCoefficients = 5; +class Polynomial +{ +public: + /** + * Note: cx and cy are relative to the image size + */ + Polynomial(double cx = 0.0, double cy = 0.0, double k0 = 0.0, + double k1 = 0.0, double k2 = 0.0, double k3 = 0.0, + double k4 = 0.0) + : cx_(cx), cy_(cy), + coefficients_({ k0, k1, k2, k3, k4 }) + { + } + + double sampleAtNormalizedPixelPos(double x, double y) const + { + double dx = x - cnx_; + double dy = y - cny_; + double r = sqrt(dx * dx + dy * dy); + double res = 1.0; + for (int i = 0; i < kNumCoefficients; i++) { + res += coefficients_[i] * std::pow(r, (i + 1) * 2); + } + return res; + } + + /** + * Returns m according to dng spec. 𝑚 represents the Euclidean distance + * (in pixels) from the optical center to the farthest pixel in the + * image. + */ + double getM() const + { + double cpx = imageSize_.width * cx_; + double cpy = imageSize_.height * cy_; + double mx = std::max(cpx, std::fabs(imageSize_.width - cpx)); + double my = std::max(cpy, std::fabs(imageSize_.height - cpy)); + + return sqrt(mx * mx + my * my); + } + + void setReferenceImageSize(const Size &size) + { + imageSize_ = size; + /* Calculate normalized centers */ + double m = getM(); + cnx_ = (size.width * cx_) / m; + cny_ = (size.height * cy_) / m; + } + +private: + double cx_; + double cy_; + double cnx_; + double cny_; + std::array coefficients_; + + Size imageSize_; +}; + +} /* namespace ipa */ + +template<> +struct YamlObject::Getter { + std::optional get(const YamlObject &obj) const + { + std::optional cx = obj["cx"].get(); + std::optional cy = obj["cy"].get(); + std::optional k0 = obj["k0"].get(); + std::optional k1 = obj["k1"].get(); + std::optional k2 = obj["k2"].get(); + std::optional k3 = obj["k3"].get(); + std::optional k4 = obj["k4"].get(); + + if (!(cx && cy && k0 && k1 && k2 && k3 && k4)) + LOG(Polynomial, Error) << "Polynomial is missing parameter "; + + return ipa::Polynomial(*cx, *cy, *k0, *k1, *k2, *k3, *k4); + } +}; + +} /* namespace libcamera */