[PATCH v3 01/11] ipa: helpers: Add Unsigned Q(m, n) helpers
Daniel Scally
dan.scally at ideasonboard.com
Thu Nov 7 12:48:09 CET 2024
Add functions to convert a double to or from an unsigned Q(m,n)
format. As the format is unsigned this most resembles the UQ variant
of the TI implementation detailed on Wikipedia:
https://en.wikipedia.org/wiki/Q_(number_format)
Signed-off-by: Daniel Scally <dan.scally at ideasonboard.com>
---
Changes in v3:
- New patch
src/ipa/libipa/helpers.cpp | 29 +++++++++++++++++++++++++++++
src/ipa/libipa/helpers.h | 2 ++
2 files changed, 31 insertions(+)
diff --git a/src/ipa/libipa/helpers.cpp b/src/ipa/libipa/helpers.cpp
index 6c038895..908c6e93 100644
--- a/src/ipa/libipa/helpers.cpp
+++ b/src/ipa/libipa/helpers.cpp
@@ -72,6 +72,35 @@ uint32_t estimateCCT(double red, double green, double blue)
return 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;
}
+/**
+ * \brief Convert double to Q(m.n) format
+ * \param[in] value The value to convert
+ * \param[in] m The number of bits used to represent the integer part
+ * \param[in] n The number of bits used to represent the fraction part
+ *
+ * This function converts a double into an unsigned fractional Q format,
+ * clamping at the largest possible value given m and n. As these formats are
+ * unsigned the m figure does not include a sign bit.
+ */
+unsigned int toUnsignedQFormat(double value, unsigned int m, unsigned int n)
+{
+ double maxVal = (std::pow(2, m + n) - 1) / std::pow(2, n);
+
+ return std::clamp(value, 0.0, maxVal) * std::pow(2, n);
+}
+
+/**
+ * \brief Convert Q(m.n) formatted number to double
+ * \param[in] value The value to convert
+ * \param[in] n The number of bits used to represent the fraction part
+ *
+ * This function converts an unsigned Q formatted value into a double.
+ */
+double fromUnsignedQFormat(unsigned int value, unsigned int n)
+{
+ return value / std::pow(2, n);
+}
+
} /* namespace ipa */
} /* namespace libcamera */
diff --git a/src/ipa/libipa/helpers.h b/src/ipa/libipa/helpers.h
index 51c74a36..1f26e768 100644
--- a/src/ipa/libipa/helpers.h
+++ b/src/ipa/libipa/helpers.h
@@ -15,6 +15,8 @@ namespace ipa {
double rec601LuminanceFromRGB(unsigned int r, unsigned int g, unsigned int b);
uint32_t estimateCCT(double red, double green, double blue);
+unsigned int toUnsignedQFormat(double value, unsigned int m, unsigned int n);
+double fromUnsignedQFormat(unsigned int value, unsigned int n);
} /* namespace ipa */
--
2.30.2
More information about the libcamera-devel
mailing list