[2/3] ipa: libipa: Add MatrixInterpolator class
diff mbox series

Message ID 20240405084050.1919105-3-paul.elder@ideasonboard.com
State Superseded
Headers show
Series
  • ipa: rkisp1: Add crosstalk algorithm
Related show

Commit Message

Paul Elder April 5, 2024, 8:40 a.m. UTC
Add a class to encapsulate the functionality of fetching a matrix based
on an integer key, and interpolating if there is no exact match. This is
expected to be used by both color correction matrices / crosstalk
correction as well as lens shading correction.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
---
 src/ipa/libipa/matrix_interpolator.cpp | 54 +++++++++++++++
 src/ipa/libipa/matrix_interpolator.h   | 94 ++++++++++++++++++++++++++
 src/ipa/libipa/meson.build             |  2 +
 3 files changed, 150 insertions(+)
 create mode 100644 src/ipa/libipa/matrix_interpolator.cpp
 create mode 100644 src/ipa/libipa/matrix_interpolator.h

Comments

Stefan Klug April 10, 2024, 9:52 p.m. UTC | #1
Hi Paul,

thank you for the patch. 

On Fri, Apr 05, 2024 at 05:40:49PM +0900, Paul Elder wrote:
> Add a class to encapsulate the functionality of fetching a matrix based
> on an integer key, and interpolating if there is no exact match. This is
> expected to be used by both color correction matrices / crosstalk
> correction as well as lens shading correction.
> 
> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
> ---
>  src/ipa/libipa/matrix_interpolator.cpp | 54 +++++++++++++++
>  src/ipa/libipa/matrix_interpolator.h   | 94 ++++++++++++++++++++++++++
>  src/ipa/libipa/meson.build             |  2 +
>  3 files changed, 150 insertions(+)
>  create mode 100644 src/ipa/libipa/matrix_interpolator.cpp
>  create mode 100644 src/ipa/libipa/matrix_interpolator.h
> 
> diff --git a/src/ipa/libipa/matrix_interpolator.cpp b/src/ipa/libipa/matrix_interpolator.cpp
> new file mode 100644
> index 00000000..394633b5
> --- /dev/null
> +++ b/src/ipa/libipa/matrix_interpolator.cpp
> @@ -0,0 +1,54 @@
> +/* SPDX-License-Identifier: BSD-2-Clause */
> +/*
> + * Copyright (C) 2019, Raspberry Pi Ltd
> + * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
> + *
> + * matrix_interpolator.cpp - Helper class for interpolating maps of matrices
> + */
> +#include "matrix_interpolator.h"
> +
> +#include <algorithm>
> +#include <string>
> +
> +#include <libcamera/base/log.h>
> +
> +#include "libcamera/internal/yaml_parser.h"
> +
> +#include "matrix.h"
> +
> +/**
> + * \file ccm.h
> + * \brief Helper class for interpolating maps of matrices
> + */
> +
> +namespace libcamera {
> +
> +LOG_DEFINE_CATEGORY(MatrixInterpolator)
> +
> +namespace ipa {
> +
> +/**
> + * \class MatrixInterpolator
> + * \brief Class for storing, retrieving, and interpolating matrices
> + */
> +
> +/**
> + * \fn int MatrixInterpolator<T, R, C>::readYaml(const libcamera::YamlObject &yaml)
> + * \brief Initialize an MatrixInterpolator instance from yaml
> + * \tparam T Type of data stored in the matrices
> + * \tparam R Number of rows of the matrices
> + * \tparam C Number of columns of the matrices
> + * \param[in] yaml The yaml object that contains the map of unsigned integers to matrices
> + * \return Zero on success, negative error code otherwise
> + */
> +
> +/**
> + * \fn Matrix<T> MatrixInterpolator<T, R, C>::get(unsigned int key)
> + * \brief Retrieve a matrix from the list of matrices, interpolating if necessary
> + * \param[in] key The unsigned integer key of the matrix to retrieve
> + * \return The matrix corresponding to the color temperature
> + */
> +
> +} /* namespace ipa */
> +
> +} /* namespace libcamera */
> diff --git a/src/ipa/libipa/matrix_interpolator.h b/src/ipa/libipa/matrix_interpolator.h
> new file mode 100644
> index 00000000..2cd4ff75
> --- /dev/null
> +++ b/src/ipa/libipa/matrix_interpolator.h
> @@ -0,0 +1,94 @@
> +/* SPDX-License-Identifier: BSD-2-Clause */
> +/*
> + * Copyright (C) 2019, Raspberry Pi Ltd
> + * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
> + *
> + * matrix_interpolator.h - Helper class for interpolating maps of matrices
> + */
> +
> +#pragma once
> +
> +#include <algorithm>
> +#include <map>
> +#include <string>
> +#include <tuple>
> +
> +#include <libcamera/base/log.h>
> +
> +#include "libcamera/internal/yaml_parser.h"
> +
> +#include "matrix.h"
> +
> +namespace libcamera {
> +
> +LOG_DECLARE_CATEGORY(MatrixInterpolator)
> +
> +namespace ipa {
> +
> +template<typename T, unsigned int R, unsigned int C>

Why does this class use template paramters for rows and cols and the
matrix class does not? I thought about suggesting these tempate params
on the matrix class, but did not due to simplicity reasons. I believe
these should be symetrical. So R and C should also be members here.

> +class MatrixInterpolator
> +{
> +public:
> +	MatrixInterpolator(){};
> +	~MatrixInterpolator(){};
> +
> +	int readYaml(const libcamera::YamlObject &yaml)
> +	{
> +		int ret;
> +
> +		if (!yaml.isDictionary()) {
> +			LOG(MatrixInterpolator, Error) << "yaml object must be a dictionary";
> +			return -EINVAL;
> +		}
> +
> +		for (const auto &[ctStr, value] : yaml.asDict()) {
> +			unsigned int ct = std::stoul(ctStr);
> +			Matrix<T> matrix;
> +			if ((ret = matrix.readYaml(R, C, value)) < 0) {
> +				LOG(MatrixInterpolator, Error) << "Failed to read matrix";
> +				return ret;
> +			}
> +
> +			matrices_[ct] = matrix;
> +
> +			LOG(MatrixInterpolator, Debug)
> +				<< "Read matrix for key " << ct << ": "
> +				<< matrices_[ct].toString();
> +		}
> +
> +		if (matrices_.size() < 1) {
> +			LOG(MatrixInterpolator, Error) << "Need at least one matrix";
> +			return -EINVAL;
> +		}
> +
> +		return 0;
> +	}
> +
> +	Matrix<T> get(unsigned int ct)
> +	{
> +		if (matrices_.size() == 1 ||
> +		    ct <= matrices_.begin()->first)
> +			return matrices_.begin()->second;
> +
> +		if (ct >= matrices_.rbegin()->first)
> +			return matrices_.rbegin()->second;
> +
> +		if (matrices_.count(ct))
> +			return matrices_[ct];
> +
> +		/* The above four guarantee that this will succeed */
> +		auto iter = matrices_.upper_bound(ct);
> +		unsigned int ctUpper = iter->first;
> +		unsigned int ctLower = (--iter)->first;
> +
> +		double lambda = (ct - ctLower) / (ctUpper - ctLower);

Both sides of the division are integers, so the division will result in
an integer. I think the denominator must be cast to double or am I
missing something here?

That would be a good case for a small unit test. Simple to write but
saves hours of debugging later.

> +		return lambda * matrices_[ctUpper] + (1.0 - lambda) * matrices_[ctLower];
> +	}
> +
> +private:
> +	std::map<unsigned int, Matrix<T>> matrices_;
> +};
> +
> +} /* namespace ipa */
> +
> +} /* namespace libcamera */
> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build
> index 5d5ba5e5..128de712 100644
> --- a/src/ipa/libipa/meson.build
> +++ b/src/ipa/libipa/meson.build
> @@ -3,6 +3,7 @@
>  libipa_headers = files([
>      'algorithm.h',
>      'camera_sensor_helper.h',
> +    'matrix_interpolator.h',

These should be sorted alphabetically.

>      'exposure_mode_helper.h',
>      'fc_queue.h',
>      'histogram.h',
> @@ -14,6 +15,7 @@ libipa_headers = files([
>  libipa_sources = files([
>      'algorithm.cpp',
>      'camera_sensor_helper.cpp',
> +    'matrix_interpolator.cpp',

likewise

>      'exposure_mode_helper.cpp',
>      'fc_queue.cpp',
>      'histogram.cpp',
> -- 
> 2.39.2
> 

Best regards,
Stefan

Patch
diff mbox series

diff --git a/src/ipa/libipa/matrix_interpolator.cpp b/src/ipa/libipa/matrix_interpolator.cpp
new file mode 100644
index 00000000..394633b5
--- /dev/null
+++ b/src/ipa/libipa/matrix_interpolator.cpp
@@ -0,0 +1,54 @@ 
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2019, Raspberry Pi Ltd
+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
+ *
+ * matrix_interpolator.cpp - Helper class for interpolating maps of matrices
+ */
+#include "matrix_interpolator.h"
+
+#include <algorithm>
+#include <string>
+
+#include <libcamera/base/log.h>
+
+#include "libcamera/internal/yaml_parser.h"
+
+#include "matrix.h"
+
+/**
+ * \file ccm.h
+ * \brief Helper class for interpolating maps of matrices
+ */
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(MatrixInterpolator)
+
+namespace ipa {
+
+/**
+ * \class MatrixInterpolator
+ * \brief Class for storing, retrieving, and interpolating matrices
+ */
+
+/**
+ * \fn int MatrixInterpolator<T, R, C>::readYaml(const libcamera::YamlObject &yaml)
+ * \brief Initialize an MatrixInterpolator instance from yaml
+ * \tparam T Type of data stored in the matrices
+ * \tparam R Number of rows of the matrices
+ * \tparam C Number of columns of the matrices
+ * \param[in] yaml The yaml object that contains the map of unsigned integers to matrices
+ * \return Zero on success, negative error code otherwise
+ */
+
+/**
+ * \fn Matrix<T> MatrixInterpolator<T, R, C>::get(unsigned int key)
+ * \brief Retrieve a matrix from the list of matrices, interpolating if necessary
+ * \param[in] key The unsigned integer key of the matrix to retrieve
+ * \return The matrix corresponding to the color temperature
+ */
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/matrix_interpolator.h b/src/ipa/libipa/matrix_interpolator.h
new file mode 100644
index 00000000..2cd4ff75
--- /dev/null
+++ b/src/ipa/libipa/matrix_interpolator.h
@@ -0,0 +1,94 @@ 
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (C) 2019, Raspberry Pi Ltd
+ * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
+ *
+ * matrix_interpolator.h - Helper class for interpolating maps of matrices
+ */
+
+#pragma once
+
+#include <algorithm>
+#include <map>
+#include <string>
+#include <tuple>
+
+#include <libcamera/base/log.h>
+
+#include "libcamera/internal/yaml_parser.h"
+
+#include "matrix.h"
+
+namespace libcamera {
+
+LOG_DECLARE_CATEGORY(MatrixInterpolator)
+
+namespace ipa {
+
+template<typename T, unsigned int R, unsigned int C>
+class MatrixInterpolator
+{
+public:
+	MatrixInterpolator(){};
+	~MatrixInterpolator(){};
+
+	int readYaml(const libcamera::YamlObject &yaml)
+	{
+		int ret;
+
+		if (!yaml.isDictionary()) {
+			LOG(MatrixInterpolator, Error) << "yaml object must be a dictionary";
+			return -EINVAL;
+		}
+
+		for (const auto &[ctStr, value] : yaml.asDict()) {
+			unsigned int ct = std::stoul(ctStr);
+			Matrix<T> matrix;
+			if ((ret = matrix.readYaml(R, C, value)) < 0) {
+				LOG(MatrixInterpolator, Error) << "Failed to read matrix";
+				return ret;
+			}
+
+			matrices_[ct] = matrix;
+
+			LOG(MatrixInterpolator, Debug)
+				<< "Read matrix for key " << ct << ": "
+				<< matrices_[ct].toString();
+		}
+
+		if (matrices_.size() < 1) {
+			LOG(MatrixInterpolator, Error) << "Need at least one matrix";
+			return -EINVAL;
+		}
+
+		return 0;
+	}
+
+	Matrix<T> get(unsigned int ct)
+	{
+		if (matrices_.size() == 1 ||
+		    ct <= matrices_.begin()->first)
+			return matrices_.begin()->second;
+
+		if (ct >= matrices_.rbegin()->first)
+			return matrices_.rbegin()->second;
+
+		if (matrices_.count(ct))
+			return matrices_[ct];
+
+		/* The above four guarantee that this will succeed */
+		auto iter = matrices_.upper_bound(ct);
+		unsigned int ctUpper = iter->first;
+		unsigned int ctLower = (--iter)->first;
+
+		double lambda = (ct - ctLower) / (ctUpper - ctLower);
+		return lambda * matrices_[ctUpper] + (1.0 - lambda) * matrices_[ctLower];
+	}
+
+private:
+	std::map<unsigned int, Matrix<T>> matrices_;
+};
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build
index 5d5ba5e5..128de712 100644
--- a/src/ipa/libipa/meson.build
+++ b/src/ipa/libipa/meson.build
@@ -3,6 +3,7 @@ 
 libipa_headers = files([
     'algorithm.h',
     'camera_sensor_helper.h',
+    'matrix_interpolator.h',
     'exposure_mode_helper.h',
     'fc_queue.h',
     'histogram.h',
@@ -14,6 +15,7 @@  libipa_headers = files([
 libipa_sources = files([
     'algorithm.cpp',
     'camera_sensor_helper.cpp',
+    'matrix_interpolator.cpp',
     'exposure_mode_helper.cpp',
     'fc_queue.cpp',
     'histogram.cpp',