From patchwork Mon Sep 14 07:21:33 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 28239 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 9A0DCBDCB7 for ; Mon, 14 Sep 2026 07:21:42 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5FDF16868A; Mon, 14 Sep 2026 09:21:40 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EdH8YkFA"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A682068244 for ; Mon, 14 Sep 2026 09:21:37 +0200 (CEST) Received: from killaraus.ideasonboard.com (2001-14ba-70f3-e800--a06.rev.dnainternet.fi [IPv6:2001:14ba:70f3:e800::a06]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id CC807981; Mon, 14 Sep 2026 09:19:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1789370398; bh=vawFfULTOfMJcK4PEVlhojJwssVIXES+YCIFQ8JOCmg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EdH8YkFAaSG/luKEFvgjZcJ93ZiwrcGo5Tpz/gl/1iUaVjXPNBrn47KrU2wQpIEAL B0MAJWiobPwv99pJNgFYFj+eQdPbJmU73wgQxcdksKXqnn0VUdojrlGnPhHF9O8mYL i+z2tTk51AIpOquor8l9aZJMrnTa3nPh5ZWL2/DQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Cc: Robert Mader Subject: [PATCH v2 1/2] libcamera: matrix: Add a transpose() function Date: Mon, 14 Sep 2026 10:21:33 +0300 Message-ID: <20260914072134.2538724-2-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260914072134.2538724-1-laurent.pinchart@ideasonboard.com> References: <20260914072134.2538724-1-laurent.pinchart@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" The Matrix class has a member function to invert a matrix, but no function to transpose it. We already have one open-coded transpose operation in the software ISP implementation, and more would likely be added. Add a transpose() member function to the Matrix class to cover this need. Signed-off-by: Laurent Pinchart --- Changes since v1: - Add and use nonc-const data() - Add unit test --- include/libcamera/internal/matrix.h | 14 ++++++++++++ src/libcamera/matrix.cpp | 34 ++++++++++++++++++++++++++++- test/matrix.cpp | 9 ++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h index f74cda103c72..221e3b517626 100644 --- a/include/libcamera/internal/matrix.h +++ b/include/libcamera/internal/matrix.h @@ -24,6 +24,9 @@ LOG_DECLARE_CATEGORY(Matrix) template bool matrixInvert(std::span dataIn, std::span dataOut, unsigned int dim, std::span scratchBuffer, std::span swapBuffer); +template +void matrixTranspose(std::span dataIn, std::span dataOut, + unsigned int rows, unsigned int cols); #endif /* __DOXYGEN__ */ template @@ -76,6 +79,8 @@ public: constexpr std::span data() const { return data_; } + constexpr std::span data() { return data_; } + constexpr std::span operator[](size_t i) const { return std::span{ &data_.data()[i * Cols], Cols }; @@ -115,6 +120,15 @@ public: return inverse; } + Matrix transpose() const + { + Matrix transposed; + matrixTranspose(std::span(data_), + std::span(transposed.data()), + Rows, Cols); + return transposed; + } + private: /* * \todo The initializer is only necessary for the constructor to be diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp index 9cb0885b3b54..e600a598c057 100644 --- a/src/libcamera/matrix.cpp +++ b/src/libcamera/matrix.cpp @@ -69,7 +69,7 @@ LOG_DEFINE_CATEGORY(Matrix) */ /** - * \fn Matrix::data() + * \fn Matrix::data() const * \brief Access the matrix data as a linear array * * Access the contents of the matrix as a one-dimensional linear array of @@ -79,6 +79,11 @@ LOG_DEFINE_CATEGORY(Matrix) * \return A span referencing the matrix data as a linear array */ +/** + * \fn Matrix::data() + * \copydoc Matrix::data() const + */ + /** * \fn std::span Matrix::operator[](size_t i) const * \brief Index to a row in the matrix @@ -107,6 +112,16 @@ LOG_DEFINE_CATEGORY(Matrix) * \return The inverse of the matrix */ +/** + * \fn Matrix::transpose() const + * \brief Compute the transpose of the matrix + * + * This function computes the transpose of the matrix. It is only implemented + * for matrices of float and double types. + * + * \return The transpose of the matrix + */ + /** * \fn Matrix::operator[](size_t i) * \copydoc Matrix::operator[](size_t i) const @@ -309,6 +324,23 @@ template bool matrixInvert(std::span data, std::span scratchBuffer, std::span swapBuffer); +template +void matrixTranspose(std::span dataIn, std::span dataOut, + unsigned int rows, unsigned int cols) +{ + for (unsigned int row = 0; row < rows; ++row) { + for (unsigned int col = 0; col < cols; ++col) + dataOut[col * rows + row] = dataIn[row * cols + col]; + } +} + +template void matrixTranspose(std::span dataIn, + std::span dataOut, + unsigned int rows, unsigned int cols); +template void matrixTranspose(std::span dataIn, + std::span dataOut, + unsigned int rows, unsigned int cols); + /* * The value node shall be a list of numerical values. Its size shall be equal * to the product of the number of rows and columns of the matrix (Rows x diff --git a/test/matrix.cpp b/test/matrix.cpp index 4afae2da7866..4504f6c96185 100644 --- a/test/matrix.cpp +++ b/test/matrix.cpp @@ -46,6 +46,15 @@ protected: ASSERT_EQ(m5[1][0], 0.0); ASSERT_EQ(m5[1][1], 1.0); + Matrix m6({ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }); + Matrix m7 = m6.transpose(); + ASSERT_EQ(m7[0][0], 1.0); + ASSERT_EQ(m7[0][1], 4.0); + ASSERT_EQ(m7[1][0], 2.0); + ASSERT_EQ(m7[1][1], 5.0); + ASSERT_EQ(m7[2][0], 3.0); + ASSERT_EQ(m7[2][1], 6.0); + return TestPass; } };