diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp index e7b809007..633dbd052 100644 --- a/src/ipa/libipa/histogram.cpp +++ b/src/ipa/libipa/histogram.cpp @@ -39,16 +39,22 @@ namespace ipa { /** * \brief Create a cumulative histogram - * \param[in] data A pre-sorted histogram to be passed + * \param[in] data A (non-cumulative) histogram */ Histogram::Histogram(Span data) { - cumulative_.reserve(data.size()); - cumulative_.push_back(0); - for (const uint32_t &value : data) - cumulative_.push_back(cumulative_.back() + value); + cumulative_.resize(data.size() + 1); + cumulative_[0] = 0; + for (const auto &[i, value] : utils::enumerate(data)) + cumulative_[i + 1] = cumulative_[i] + value; } +/** + * \brief Create a cumulative histogram + * \param[in] data A (non-cumulative) histogram + * \param[in] transform The transformation function to apply to every bin + */ + /** * \fn Histogram::bins() * \brief Retrieve the number of bins currently used by the Histogram diff --git a/src/ipa/libipa/histogram.h b/src/ipa/libipa/histogram.h index 0379ab536..032adca05 100644 --- a/src/ipa/libipa/histogram.h +++ b/src/ipa/libipa/histogram.h @@ -10,10 +10,11 @@ #include #include #include - +#include #include #include +#include namespace libcamera { @@ -24,6 +25,17 @@ class Histogram public: Histogram() { cumulative_.push_back(0); } Histogram(Span data); + + template> * = nullptr> + Histogram(Span data, Transform transform) + { + cumulative_.resize(data.size() + 1); + cumulative_[0] = 0; + for (const auto &[i, value] : utils::enumerate(data)) + cumulative_[i + 1] = cumulative_[i] + transform(value); + } + size_t bins() const { return cumulative_.size() - 1; } uint64_t total() const { return cumulative_[cumulative_.size() - 1]; } uint64_t cumulativeFrequency(double bin) const;