From patchwork Wed Mar 27 08:59:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 19815 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 A69B8C0DA4 for ; Wed, 27 Mar 2024 08:59:53 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id DCD7063339; Wed, 27 Mar 2024 09:59:52 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OuCsZs8v"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2642762CA2 for ; Wed, 27 Mar 2024 09:59:51 +0100 (CET) Received: from pyrite.hamster-moth.ts.net (h175-177-049-156.catv02.itscom.jp [175.177.49.156]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D9A22675; Wed, 27 Mar 2024 09:59:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1711529958; bh=yoIPKnJu0YrXxqs0VarqZxoyCPZT3+RtTGgcnqXb+d0=; h=From:To:Cc:Subject:Date:From; b=OuCsZs8vV87JHBAcC2cDxXiZz1odKPcAd3S+Gguc3tIWLySYq8B3xCRC0oEeMpwXR cg2LgBt931x//7igz9Zb+oJ0xs1WbNudHRP83wS4CAW6R0HQAzjt2/6WL2ik8hgFBg dRLxU/S/Hj6jrWCMZzq3gKKN2SKmfbQTVuTS33Yw= From: Paul Elder To: libcamera-devel@lists.libcamera.org Cc: Paul Elder Subject: [PATCH] ipa: libipa: Add Matrix class Date: Wed, 27 Mar 2024 17:59:33 +0900 Message-Id: <20240327085933.281180-1-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.39.2 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 class to represent a Matrix object and its corresponding operations. Signed-off-by: Paul Elder --- src/ipa/libipa/matrix.cpp | 17 +++++ src/ipa/libipa/matrix.h | 138 +++++++++++++++++++++++++++++++++++++ src/ipa/libipa/meson.build | 2 + 3 files changed, 157 insertions(+) create mode 100644 src/ipa/libipa/matrix.cpp create mode 100644 src/ipa/libipa/matrix.h diff --git a/src/ipa/libipa/matrix.cpp b/src/ipa/libipa/matrix.cpp new file mode 100644 index 00000000..c222c3f0 --- /dev/null +++ b/src/ipa/libipa/matrix.cpp @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2019, Raspberry Pi Ltd + * Copyright (C) 2024, Paul Elder + * + * matrix.cpp - Matrix and related operations + */ + +#include "matrix.h" + +#include + +namespace libcamera { + +LOG_DEFINE_CATEGORY(Matrix) + +} /* namespace libcamera */ diff --git a/src/ipa/libipa/matrix.h b/src/ipa/libipa/matrix.h new file mode 100644 index 00000000..1fd191c7 --- /dev/null +++ b/src/ipa/libipa/matrix.h @@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * Copyright (C) 2019, Raspberry Pi Ltd + * Copyright (C) 2024, Paul Elder + * + * matrix.cpp - Matrix and related operations + */ +#pragma once + +#include +#include +#include +#include +#include + +#include + +#include "libcamera/internal/yaml_parser.h" + +namespace libcamera { + +LOG_DECLARE_CATEGORY(Matrix) + +namespace ipa { + +template +struct Matrix { +public: + Matrix(){}; + ~Matrix(){}; + + Matrix(const unsigned int &_rows, const unsigned int &_cols, + const std::vector &_data) + : rows(_rows), cols(_cols) + { + for (const T &x : _data) + data.push_back(x); + + while (rows * cols < data.size()) + data.push_back(0); + } + + int readYaml(unsigned int _rows, unsigned int _cols, + const libcamera::YamlObject &yaml) + { + rows = _rows; + cols = _cols; + + if (yaml.size() != rows * cols) { + LOG(Matrix, Error) + << "Wrong number of values in matrix: expected " + << rows * cols << ", got " << yaml.size(); + return -EINVAL; + } + + for (const auto &x : yaml.asList()) { + auto value = x.get(); + if (!value) { + LOG(Matrix, Error) << "Failed to read matrix value " << *value; + return -EINVAL; + } + data.push_back(*value); + } + + return 0; + } + + const std::string toString() const + { + std::stringstream ss; + ss << "Matrix { "; + for (unsigned int i = 0; i < rows; i++) { + ss << "[ "; + for (unsigned int j = 0; j < cols; j++) { + ss << data[i * cols + j]; + ss << ((j + 1 < cols) ? ", " : " "); + } + ss << ((i + 1 < rows) ? "], " : "]"); + } + ss << " }"; + + return ss.str(); + } + + const T &operator[](unsigned int i) const { return data.at(i); } + + unsigned int rows; + unsigned int cols; + std::vector data; +}; + +template +Matrix operator*(T d, const Matrix &m) +{ + std::vector result(m.data.size()); + + for (const T &x : m.data) + result.push_back(d * x); + + return Matrix(m.rows, m.cols, result); +} + +template +Matrix operator*(const Matrix &m1, const Matrix &m2) +{ + ASSERT(m1.cols == m2.rows); + + std::vector result(m1.rows * m2.cols); + + for (unsigned int i = 0; i < m1.rows; i++) { + T sum = 0; + + for (unsigned int j = 0; j < m2.cols; j++) + sum += m1[i * m1.cols + j] * m2[j * m2.cols + i]; + + result.push_back(sum); + } + + return Matrix(m1.rows, m2.cols, result); +} + +template +Matrix operator+(const Matrix &m1, const Matrix &m2) +{ + ASSERT(m1.cols == m2.cols && m1.rows == m2.rows); + + unsigned int len = m1.rows * m1.cols; + std::vector result(len); + + for (unsigned int i = 0; i < len; i++) + result[i] = m1[i] + m2[i]; + + return Matrix(m1.rows, m1.cols, result); +} + +} /* namespace ipa */ + +} /* namespace libcamera */ diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build index 817a09cf..5d5ba5e5 100644 --- a/src/ipa/libipa/meson.build +++ b/src/ipa/libipa/meson.build @@ -6,6 +6,7 @@ libipa_headers = files([ 'exposure_mode_helper.h', 'fc_queue.h', 'histogram.h', + 'matrix.h', 'module.h', 'pwl.h', ]) @@ -16,6 +17,7 @@ libipa_sources = files([ 'exposure_mode_helper.cpp', 'fc_queue.cpp', 'histogram.cpp', + 'matrix.cpp', 'module.cpp', 'pwl.cpp' ])