[PATCH 01/10] libcamera: matrix: Add cast function

Stefan Klug stefan.klug at ideasonboard.com
Mon Feb 17 11:01:42 CET 2025


The libcamera code base uses matrices and vectors of type float and
double. Add a cast function to easily convert between different
underlying types.

Signed-off-by: Stefan Klug <stefan.klug at ideasonboard.com>
---
 include/libcamera/internal/matrix.h | 11 +++++++++++
 src/libcamera/matrix.cpp            | 10 ++++++++++
 2 files changed, 21 insertions(+)

diff --git a/include/libcamera/internal/matrix.h b/include/libcamera/internal/matrix.h
index a055e6926c94..ca81dcda93c4 100644
--- a/include/libcamera/internal/matrix.h
+++ b/include/libcamera/internal/matrix.h
@@ -90,6 +90,17 @@ public:
 		return *this;
 	}
 
+	template<typename T2>
+	Matrix<T2, Rows, Cols> cast() const
+	{
+		Matrix<T2, Rows, Cols> ret;
+		for (unsigned int i = 0; i < Rows; i++)
+			for (unsigned int j = 0; j < Cols; j++)
+				ret[i][j] = static_cast<T2>((*this)[i][j]);
+
+		return ret;
+	}
+
 private:
 	std::array<T, Rows * Cols> data_;
 };
diff --git a/src/libcamera/matrix.cpp b/src/libcamera/matrix.cpp
index e7e027225666..91a3f16405a3 100644
--- a/src/libcamera/matrix.cpp
+++ b/src/libcamera/matrix.cpp
@@ -77,6 +77,16 @@ LOG_DEFINE_CATEGORY(Matrix)
  * \return Row \a i from the matrix, as a Span
  */
 
+/**
+ * \fn template<typename T2> Matrix<T2, Rows, Cols> Matrix::cast() const
+ * \brief Cast the matrix to a different type
+ *
+ * This function returns a new matrix with the same size and values but a
+ * different type.
+ *
+ * \return The new matrix
+ */
+
 /**
  * \fn Matrix::operator[](size_t i)
  * \copydoc Matrix::operator[](size_t i) const
-- 
2.43.0



More information about the libcamera-devel mailing list