summaryrefslogtreecommitdiff
path: root/src/ipa/libipa
diff options
context:
space:
mode:
Diffstat (limited to 'src/ipa/libipa')
-rw-r--r--src/ipa/libipa/awb.cpp265
-rw-r--r--src/ipa/libipa/awb.h66
-rw-r--r--src/ipa/libipa/awb_bayes.cpp505
-rw-r--r--src/ipa/libipa/awb_bayes.h59
-rw-r--r--src/ipa/libipa/awb_grey.cpp114
-rw-r--r--src/ipa/libipa/awb_grey.h36
-rw-r--r--src/ipa/libipa/colours.h2
-rw-r--r--src/ipa/libipa/exposure_mode_helper.cpp4
-rw-r--r--src/ipa/libipa/interpolator.cpp10
-rw-r--r--src/ipa/libipa/interpolator.h5
-rw-r--r--src/ipa/libipa/lux.cpp20
-rw-r--r--src/ipa/libipa/lux.h3
-rw-r--r--src/ipa/libipa/meson.build8
-rw-r--r--src/ipa/libipa/pwl.cpp5
-rw-r--r--src/ipa/libipa/pwl.h3
-rw-r--r--src/ipa/libipa/vector.cpp351
-rw-r--r--src/ipa/libipa/vector.h370
17 files changed, 1082 insertions, 744 deletions
diff --git a/src/ipa/libipa/awb.cpp b/src/ipa/libipa/awb.cpp
new file mode 100644
index 00000000..925fac23
--- /dev/null
+++ b/src/ipa/libipa/awb.cpp
@@ -0,0 +1,265 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024 Ideas on Board Oy
+ *
+ * Generic AWB algorithms
+ */
+
+#include "awb.h"
+
+#include <libcamera/base/log.h>
+
+#include <libcamera/control_ids.h>
+
+/**
+ * \file awb.h
+ * \brief Base classes for AWB algorithms
+ */
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(Awb)
+
+namespace ipa {
+
+/**
+ * \class AwbResult
+ * \brief The result of an AWB calculation
+ *
+ * This class holds the result of an auto white balance calculation.
+ */
+
+/**
+ * \var AwbResult::gains
+ * \brief The calculated white balance gains
+ */
+
+/**
+ * \var AwbResult::colourTemperature
+ * \brief The calculated colour temperature in Kelvin
+ */
+
+/**
+ * \class AwbStats
+ * \brief An abstraction class wrapping hardware-specific AWB statistics
+ *
+ * IPA modules using an AWB algorithm based on the AwbAlgorithm class need to
+ * implement this class to give the algorithm access to the hardware-specific
+ * statistics data.
+ */
+
+/**
+ * \fn AwbStats::computeColourError()
+ * \brief Compute an error value for when the given gains would be applied
+ * \param[in] gains The gains to apply
+ *
+ * Compute an error value (non-greyness) assuming the given \a gains would be
+ * applied. To keep the actual implementations computationally inexpensive,
+ * the squared colour error shall be returned.
+ *
+ * If the AWB statistics provide multiple zones, the average of the individual
+ * squared errors shall be returned. Averaging/normalizing is necessary so that
+ * the numeric dimensions are the same on all hardware platforms.
+ *
+ * \return The computed error value
+ */
+
+/**
+ * \fn AwbStats::rgbMeans()
+ * \brief Get RGB means of the statistics
+ *
+ * Fetch the RGB means from the statistics. The values of each channel are
+ * dimensionless and only the ratios are used for further calculations. This is
+ * used by the simple grey world model to calculate the gains to apply.
+ *
+ * \return The RGB means
+ */
+
+/**
+ * \class AwbAlgorithm
+ * \brief A base class for auto white balance algorithms
+ *
+ * This class is a base class for auto white balance algorithms. It defines an
+ * interface for the algorithms to implement, and is used by the IPAs to
+ * interact with the concrete implementation.
+ */
+
+/**
+ * \fn AwbAlgorithm::init()
+ * \brief Initialize the algorithm with the given tuning data
+ * \param[in] tuningData The tuning data to use for the algorithm
+ *
+ * \return 0 on success, a negative error code otherwise
+ */
+
+/**
+ * \fn AwbAlgorithm::calculateAwb()
+ * \brief Calculate AWB data from the given statistics
+ * \param[in] stats The statistics to use for the calculation
+ * \param[in] lux The lux value of the scene
+ *
+ * Calculate an AwbResult object from the given statistics and lux value. A \a
+ * lux value of 0 means it is unknown or invalid and the algorithm shall ignore
+ * it.
+ *
+ * \return The AWB result
+ */
+
+/**
+ * \fn AwbAlgorithm::gainsFromColourTemperature()
+ * \brief Compute white balance gains from a colour temperature
+ * \param[in] colourTemperature The colour temperature in Kelvin
+ *
+ * Compute the white balance gains from a \a colourTemperature. This function
+ * does not take any statistics into account. It is used to compute the colour
+ * gains when the user manually specifies a colour temperature.
+ *
+ * \return The colour gains
+ */
+
+/**
+ * \fn AwbAlgorithm::controls()
+ * \brief Get the controls info map for this algorithm
+ *
+ * \return The controls info map
+ */
+
+/**
+ * \fn AwbAlgorithm::handleControls()
+ * \param[in] controls The controls to handle
+ * \brief Handle the controls supplied in a request
+ */
+
+/**
+ * \brief Parse the mode configurations from the tuning data
+ * \param[in] tuningData the YamlObject representing the tuning data
+ * \param[in] def The default value for the AwbMode control
+ *
+ * Utility function to parse the tuning data for an AwbMode entry and read all
+ * provided modes. It adds controls::AwbMode to AwbAlgorithm::controls_ and
+ * populates AwbAlgorithm::modes_. For a list of possible modes see \ref
+ * controls::AwbModeEnum.
+ *
+ * Each mode entry must contain a "lo" and "hi" key to specify the lower and
+ * upper colour temperature of that mode. For example:
+ *
+ * \code{.unparsed}
+ * algorithms:
+ * - Awb:
+ * AwbMode:
+ * AwbAuto:
+ * lo: 2500
+ * hi: 8000
+ * AwbIncandescent:
+ * lo: 2500
+ * hi: 3000
+ * ...
+ * \endcode
+ *
+ * If \a def is supplied but not contained in the the \a tuningData, -EINVAL is
+ * returned.
+ *
+ * \sa controls::AwbModeEnum
+ * \return Zero on success, negative error code otherwise
+ */
+int AwbAlgorithm::parseModeConfigs(const YamlObject &tuningData,
+ const ControlValue &def)
+{
+ std::vector<ControlValue> availableModes;
+
+ const YamlObject &yamlModes = tuningData[controls::AwbMode.name()];
+ if (!yamlModes.isDictionary()) {
+ LOG(Awb, Error)
+ << "AwbModes must be a dictionary.";
+ return -EINVAL;
+ }
+
+ for (const auto &[modeName, modeDict] : yamlModes.asDict()) {
+ if (controls::AwbModeNameValueMap.find(modeName) ==
+ controls::AwbModeNameValueMap.end()) {
+ LOG(Awb, Warning)
+ << "Skipping unknown AWB mode '"
+ << modeName << "'";
+ continue;
+ }
+
+ if (!modeDict.isDictionary()) {
+ LOG(Awb, Error)
+ << "Invalid AWB mode '" << modeName << "'";
+ return -EINVAL;
+ }
+
+ const auto &modeValue = static_cast<controls::AwbModeEnum>(
+ controls::AwbModeNameValueMap.at(modeName));
+
+ ModeConfig &config = modes_[modeValue];
+
+ auto hi = modeDict["hi"].get<double>();
+ if (!hi) {
+ LOG(Awb, Error) << "Failed to read hi param of mode "
+ << modeName;
+ return -EINVAL;
+ }
+ config.ctHi = *hi;
+
+ auto lo = modeDict["lo"].get<double>();
+ if (!lo) {
+ LOG(Awb, Error) << "Failed to read low param of mode "
+ << modeName;
+ return -EINVAL;
+ }
+ config.ctLo = *lo;
+
+ availableModes.push_back(modeValue);
+ }
+
+ if (modes_.empty()) {
+ LOG(Awb, Error) << "No AWB modes configured";
+ return -EINVAL;
+ }
+
+ if (!def.isNone() &&
+ modes_.find(def.get<controls::AwbModeEnum>()) == modes_.end()) {
+ const auto &names = controls::AwbMode.enumerators();
+ LOG(Awb, Error) << names.at(def.get<controls::AwbModeEnum>())
+ << " mode is missing in the configuration.";
+ return -EINVAL;
+ }
+
+ controls_[&controls::AwbMode] = ControlInfo(availableModes, def);
+
+ return 0;
+}
+
+/**
+ * \class AwbAlgorithm::ModeConfig
+ * \brief Holds the configuration of a single AWB mode
+ *
+ * AWB modes limit the regulation of the AWB algorithm to a specific range of
+ * colour temperatures.
+ */
+
+/**
+ * \var AwbAlgorithm::ModeConfig::ctLo
+ * \brief The lowest valid colour temperature of that mode
+ */
+
+/**
+ * \var AwbAlgorithm::ModeConfig::ctHi
+ * \brief The highest valid colour temperature of that mode
+ */
+
+/**
+ * \var AwbAlgorithm::controls_
+ * \brief Controls info map for the controls provided by the algorithm
+ */
+
+/**
+ * \var AwbAlgorithm::modes_
+ * \brief Map of all configured modes
+ * \sa AwbAlgorithm::parseModeConfigs
+ */
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/awb.h b/src/ipa/libipa/awb.h
new file mode 100644
index 00000000..4bab7a45
--- /dev/null
+++ b/src/ipa/libipa/awb.h
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024 Ideas on Board Oy
+ *
+ * Generic AWB algorithms
+ */
+
+#pragma once
+
+#include <map>
+
+#include <libcamera/control_ids.h>
+#include <libcamera/controls.h>
+
+#include "libcamera/internal/vector.h"
+#include "libcamera/internal/yaml_parser.h"
+
+namespace libcamera {
+
+namespace ipa {
+
+struct AwbResult {
+ RGB<double> gains;
+ double colourTemperature;
+};
+
+struct AwbStats {
+ virtual double computeColourError(const RGB<double> &gains) const = 0;
+ virtual RGB<double> rgbMeans() const = 0;
+
+protected:
+ ~AwbStats() = default;
+};
+
+class AwbAlgorithm
+{
+public:
+ virtual ~AwbAlgorithm() = default;
+
+ virtual int init(const YamlObject &tuningData) = 0;
+ virtual AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) = 0;
+ virtual RGB<double> gainsFromColourTemperature(double colourTemperature) = 0;
+
+ const ControlInfoMap::Map &controls() const
+ {
+ return controls_;
+ }
+
+ virtual void handleControls([[maybe_unused]] const ControlList &controls) {}
+
+protected:
+ int parseModeConfigs(const YamlObject &tuningData,
+ const ControlValue &def = {});
+
+ struct ModeConfig {
+ double ctHi;
+ double ctLo;
+ };
+
+ ControlInfoMap::Map controls_;
+ std::map<controls::AwbModeEnum, AwbAlgorithm::ModeConfig> modes_;
+};
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/awb_bayes.cpp b/src/ipa/libipa/awb_bayes.cpp
new file mode 100644
index 00000000..d1d0eaf0
--- /dev/null
+++ b/src/ipa/libipa/awb_bayes.cpp
@@ -0,0 +1,505 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2019, Raspberry Pi Ltd
+ * Copyright (C) 2024 Ideas on Board Oy
+ *
+ * Implementation of a bayesian AWB algorithm
+ */
+
+#include "awb_bayes.h"
+
+#include <algorithm>
+#include <cmath>
+#include <limits>
+#include <map>
+#include <ostream>
+#include <vector>
+
+#include <libcamera/base/log.h>
+#include <libcamera/control_ids.h>
+
+#include "colours.h"
+
+/**
+ * \file awb_bayes.h
+ * \brief Implementation of bayesian auto white balance algorithm
+ *
+ * This implementation is based on the initial implementation done by
+ * RaspberryPi.
+ *
+ * \todo Documentation
+ *
+ * \todo Not all the features implemented by RaspberryPi were ported over to
+ * this algorithm because they either rely on hardware features not generally
+ * available or were considered not important enough at the moment.
+ *
+ * The following parts are not implemented:
+ *
+ * - min_pixels: minimum proportion of pixels counted within AWB region for it
+ * to be "useful"
+ * - min_g: minimum G value of those pixels, to be regarded a "useful"
+ * - min_regions: number of AWB regions that must be "useful" in order to do the
+ * AWB calculation
+ * - deltaLimit: clamp on colour error term (so as not to penalize non-grey
+ * excessively)
+ * - bias_proportion: The biasProportion parameter adds a small proportion of
+ * the counted pixels to a region biased to the biasCT colour temperature.
+ * A typical value for biasProportion would be between 0.05 to 0.1.
+ * - bias_ct: CT target for the search bias
+ * - sensitivityR: red sensitivity ratio (set to canonical sensor's R/G divided
+ * by this sensor's R/G)
+ * - sensitivityB: blue sensitivity ratio (set to canonical sensor's B/G divided
+ * by this sensor's B/G)
+ */
+
+namespace libcamera {
+
+LOG_DECLARE_CATEGORY(Awb)
+
+namespace {
+
+template<typename T>
+class LimitsRecorder
+{
+public:
+ LimitsRecorder()
+ : min_(std::numeric_limits<T>::max()),
+ max_(std::numeric_limits<T>::min())
+ {
+ }
+
+ void record(const T &value)
+ {
+ min_ = std::min(min_, value);
+ max_ = std::max(max_, value);
+ }
+
+ const T &min() const { return min_; }
+ const T &max() const { return max_; }
+
+private:
+ T min_;
+ T max_;
+};
+
+#ifndef __DOXYGEN__
+template<typename T>
+std::ostream &operator<<(std::ostream &out, const LimitsRecorder<T> &v)
+{
+ out << "[ " << v.min() << ", " << v.max() << " ]";
+ return out;
+}
+#endif
+
+} /* namespace */
+
+namespace ipa {
+
+/**
+ * \brief Step size control for CT search
+ */
+constexpr double kSearchStep = 0.2;
+
+/**
+ * \copydoc libcamera::ipa::Interpolator::interpolate()
+ */
+template<>
+void Interpolator<Pwl>::interpolate(const Pwl &a, const Pwl &b, Pwl &dest, double lambda)
+{
+ dest = Pwl::combine(a, b,
+ [=](double /*x*/, double y0, double y1) -> double {
+ return y0 * (1.0 - lambda) + y1 * lambda;
+ });
+}
+
+/**
+ * \class AwbBayes
+ * \brief Implementation of a bayesian auto white balance algorithm
+ *
+ * In a bayesian AWB algorithm the auto white balance estimation is improved by
+ * taking the likelihood of a given lightsource based on the estimated lux level
+ * into account. E.g. If it is very bright we can assume that we are outside and
+ * that colour temperatures around 6500 are preferred.
+ *
+ * The second part of this algorithm is the search for the most likely colour
+ * temperature. It is implemented in AwbBayes::coarseSearch() and in
+ * AwbBayes::fineSearch(). The search works very well without prior likelihoods
+ * and therefore the algorithm itself provides very good results even without
+ * prior likelihoods.
+ */
+
+/**
+ * \var AwbBayes::transversePos_
+ * \brief How far to wander off CT curve towards "more purple"
+ */
+
+/**
+ * \var AwbBayes::transverseNeg_
+ * \brief How far to wander off CT curve towards "more green"
+ */
+
+/**
+ * \var AwbBayes::currentMode_
+ * \brief The currently selected mode
+ */
+
+int AwbBayes::init(const YamlObject &tuningData)
+{
+ int ret = colourGainCurve_.readYaml(tuningData["colourGains"], "ct", "gains");
+ if (ret) {
+ LOG(Awb, Error)
+ << "Failed to parse 'colourGains' "
+ << "parameter from tuning file";
+ return ret;
+ }
+
+ ctR_.clear();
+ ctB_.clear();
+ for (const auto &[ct, g] : colourGainCurve_.data()) {
+ ctR_.append(ct, 1.0 / g[0]);
+ ctB_.append(ct, 1.0 / g[1]);
+ }
+
+ /* We will want the inverse functions of these too. */
+ ctRInverse_ = ctR_.inverse().first;
+ ctBInverse_ = ctB_.inverse().first;
+
+ ret = readPriors(tuningData);
+ if (ret) {
+ LOG(Awb, Error) << "Failed to read priors";
+ return ret;
+ }
+
+ ret = parseModeConfigs(tuningData, controls::AwbAuto);
+ if (ret) {
+ LOG(Awb, Error)
+ << "Failed to parse mode parameter from tuning file";
+ return ret;
+ }
+ currentMode_ = &modes_[controls::AwbAuto];
+
+ transversePos_ = tuningData["transversePos"].get<double>(0.01);
+ transverseNeg_ = tuningData["transverseNeg"].get<double>(0.01);
+ if (transversePos_ <= 0 || transverseNeg_ <= 0) {
+ LOG(Awb, Error) << "AwbConfig: transversePos/Neg must be > 0";
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+int AwbBayes::readPriors(const YamlObject &tuningData)
+{
+ const auto &priorsList = tuningData["priors"];
+ std::map<uint32_t, Pwl> priors;
+
+ if (!priorsList) {
+ LOG(Awb, Error) << "No priors specified";
+ return -EINVAL;
+ }
+
+ for (const auto &p : priorsList.asList()) {
+ if (!p.contains("lux")) {
+ LOG(Awb, Error) << "Missing lux value";
+ return -EINVAL;
+ }
+
+ uint32_t lux = p["lux"].get<uint32_t>(0);
+ if (priors.count(lux)) {
+ LOG(Awb, Error) << "Duplicate prior for lux value " << lux;
+ return -EINVAL;
+ }
+
+ std::vector<uint32_t> temperatures =
+ p["ct"].getList<uint32_t>().value_or(std::vector<uint32_t>{});
+ std::vector<double> probabilities =
+ p["probability"].getList<double>().value_or(std::vector<double>{});
+
+ if (temperatures.size() != probabilities.size()) {
+ LOG(Awb, Error)
+ << "Ct and probability array sizes are unequal";
+ return -EINVAL;
+ }
+
+ if (temperatures.empty()) {
+ LOG(Awb, Error)
+ << "Ct and probability arrays are empty";
+ return -EINVAL;
+ }
+
+ std::map<int, double> ctToProbability;
+ for (unsigned int i = 0; i < temperatures.size(); i++) {
+ int t = temperatures[i];
+ if (ctToProbability.count(t)) {
+ LOG(Awb, Error) << "Duplicate ct value";
+ return -EINVAL;
+ }
+
+ ctToProbability[t] = probabilities[i];
+ }
+
+ auto &pwl = priors[lux];
+ for (const auto &[ct, prob] : ctToProbability) {
+ if (prob < 1e-6) {
+ LOG(Awb, Error) << "Prior probability must be larger than 1e-6";
+ return -EINVAL;
+ }
+ pwl.append(ct, prob);
+ }
+ }
+
+ if (priors.empty()) {
+ LOG(Awb, Error) << "No priors specified";
+ return -EINVAL;
+ }
+
+ priors_.setData(std::move(priors));
+
+ return 0;
+}
+
+void AwbBayes::handleControls(const ControlList &controls)
+{
+ auto mode = controls.get(controls::AwbMode);
+ if (mode) {
+ auto it = modes_.find(static_cast<controls::AwbModeEnum>(*mode));
+ if (it != modes_.end())
+ currentMode_ = &it->second;
+ else
+ LOG(Awb, Error) << "Unsupported AWB mode " << *mode;
+ }
+}
+
+RGB<double> AwbBayes::gainsFromColourTemperature(double colourTemperature)
+{
+ /*
+ * \todo In the RaspberryPi code, the ct curve was interpolated in
+ * the white point space (1/x) not in gains space. This feels counter
+ * intuitive, as the gains are in linear space. But I can't prove it.
+ */
+ const auto &gains = colourGainCurve_.getInterpolated(colourTemperature);
+ return { { gains[0], 1.0, gains[1] } };
+}
+
+AwbResult AwbBayes::calculateAwb(const AwbStats &stats, unsigned int lux)
+{
+ ipa::Pwl prior;
+ if (lux > 0) {
+ prior = priors_.getInterpolated(lux);
+ prior.map([](double x, double y) {
+ LOG(Awb, Debug) << "(" << x << "," << y << ")";
+ });
+ } else {
+ prior.append(0, 1.0);
+ }
+
+ double t = coarseSearch(prior, stats);
+ double r = ctR_.eval(t);
+ double b = ctB_.eval(t);
+ LOG(Awb, Debug)
+ << "After coarse search: r " << r << " b " << b << " (gains r "
+ << 1 / r << " b " << 1 / b << ")";
+
+ /*
+ * Original comment from RaspberryPi:
+ * Not entirely sure how to handle the fine search yet. Mostly the
+ * estimated CT is already good enough, but the fine search allows us to
+ * wander transversely off the CT curve. Under some illuminants, where
+ * there may be more or less green light, this may prove beneficial,
+ * though I probably need more real datasets before deciding exactly how
+ * this should be controlled and tuned.
+ */
+ fineSearch(t, r, b, prior, stats);
+ LOG(Awb, Debug)
+ << "After fine search: r " << r << " b " << b << " (gains r "
+ << 1 / r << " b " << 1 / b << ")";
+
+ return { { { 1.0 / r, 1.0, 1.0 / b } }, t };
+}
+
+double AwbBayes::coarseSearch(const ipa::Pwl &prior, const AwbStats &stats) const
+{
+ std::vector<Pwl::Point> points;
+ size_t bestPoint = 0;
+ double t = currentMode_->ctLo;
+ int spanR = -1;
+ int spanB = -1;
+ LimitsRecorder<double> errorLimits;
+ LimitsRecorder<double> priorLogLikelihoodLimits;
+
+ /* Step down the CT curve evaluating log likelihood. */
+ while (true) {
+ double r = ctR_.eval(t, &spanR);
+ double b = ctB_.eval(t, &spanB);
+ RGB<double> gains({ 1 / r, 1.0, 1 / b });
+ double delta2Sum = stats.computeColourError(gains);
+ double priorLogLikelihood = log(prior.eval(prior.domain().clamp(t)));
+ double finalLogLikelihood = delta2Sum - priorLogLikelihood;
+
+ errorLimits.record(delta2Sum);
+ priorLogLikelihoodLimits.record(priorLogLikelihood);
+
+ points.push_back({ { t, finalLogLikelihood } });
+ if (points.back().y() < points[bestPoint].y())
+ bestPoint = points.size() - 1;
+
+ if (t == currentMode_->ctHi)
+ break;
+
+ /*
+ * Ensure even steps along the r/b curve by scaling them by the
+ * current t.
+ */
+ t = std::min(t + t / 10 * kSearchStep, currentMode_->ctHi);
+ }
+
+ t = points[bestPoint].x();
+ LOG(Awb, Debug) << "Coarse search found CT " << t
+ << " error limits:" << errorLimits
+ << " prior log likelihood limits:" << priorLogLikelihoodLimits;
+
+ /*
+ * We have the best point of the search, but refine it with a quadratic
+ * interpolation around its neighbors.
+ */
+ if (points.size() > 2) {
+ bestPoint = std::clamp(bestPoint, std::size_t{ 1 }, points.size() - 2);
+ t = interpolateQuadratic(points[bestPoint - 1],
+ points[bestPoint],
+ points[bestPoint + 1]);
+ LOG(Awb, Debug)
+ << "After quadratic refinement, coarse search has CT "
+ << t;
+ }
+
+ return t;
+}
+
+void AwbBayes::fineSearch(double &t, double &r, double &b, ipa::Pwl const &prior, const AwbStats &stats) const
+{
+ int spanR = -1;
+ int spanB = -1;
+ double step = t / 10 * kSearchStep * 0.1;
+ int nsteps = 5;
+ ctR_.eval(t, &spanR);
+ ctB_.eval(t, &spanB);
+ double rDiff = ctR_.eval(t + nsteps * step, &spanR) -
+ ctR_.eval(t - nsteps * step, &spanR);
+ double bDiff = ctB_.eval(t + nsteps * step, &spanB) -
+ ctB_.eval(t - nsteps * step, &spanB);
+ Pwl::Point transverse({ bDiff, -rDiff });
+ if (transverse.length2() < 1e-6)
+ return;
+ /*
+ * transverse is a unit vector orthogonal to the b vs. r function
+ * (pointing outwards with r and b increasing)
+ */
+ transverse = transverse / transverse.length();
+ double bestLogLikelihood = 0;
+ double bestT = 0;
+ Pwl::Point bestRB(0);
+ double transverseRange = transverseNeg_ + transversePos_;
+ const int maxNumDeltas = 12;
+ LimitsRecorder<double> errorLimits;
+ LimitsRecorder<double> priorLogLikelihoodLimits;
+
+
+ /* a transverse step approximately every 0.01 r/b units */
+ int numDeltas = floor(transverseRange * 100 + 0.5) + 1;
+ numDeltas = std::clamp(numDeltas, 3, maxNumDeltas);
+
+ /*
+ * Step down CT curve. March a bit further if the transverse range is
+ * large.
+ */
+ nsteps += numDeltas;
+ for (int i = -nsteps; i <= nsteps; i++) {
+ double tTest = t + i * step;
+ double priorLogLikelihood =
+ log(prior.eval(prior.domain().clamp(tTest)));
+ priorLogLikelihoodLimits.record(priorLogLikelihood);
+ Pwl::Point rbStart{ { ctR_.eval(tTest, &spanR),
+ ctB_.eval(tTest, &spanB) } };
+ Pwl::Point samples[maxNumDeltas];
+ int bestPoint = 0;
+
+ /*
+ * Sample numDeltas points transversely *off* the CT curve
+ * in the range [-transverseNeg, transversePos].
+ * The x values of a sample contains the distance and the y
+ * value contains the corresponding log likelihood.
+ */
+ double transverseStep = transverseRange / (numDeltas - 1);
+ for (int j = 0; j < numDeltas; j++) {
+ auto &p = samples[j];
+ p.x() = -transverseNeg_ + transverseStep * j;
+ Pwl::Point rbTest = rbStart + transverse * p.x();
+ RGB<double> gains({ 1 / rbTest[0], 1.0, 1 / rbTest[1] });
+ double delta2Sum = stats.computeColourError(gains);
+ errorLimits.record(delta2Sum);
+ p.y() = delta2Sum - priorLogLikelihood;
+
+ if (p.y() < samples[bestPoint].y())
+ bestPoint = j;
+ }
+
+ /*
+ * We have all samples transversely across the CT curve,
+ * now let's do a quadratic interpolation for the best result.
+ */
+ bestPoint = std::clamp(bestPoint, 1, numDeltas - 2);
+ double bestOffset = interpolateQuadratic(samples[bestPoint - 1],
+ samples[bestPoint],
+ samples[bestPoint + 1]);
+ Pwl::Point rbTest = rbStart + transverse * bestOffset;
+ RGB<double> gains({ 1 / rbTest[0], 1.0, 1 / rbTest[1] });
+ double delta2Sum = stats.computeColourError(gains);
+ errorLimits.record(delta2Sum);
+ double finalLogLikelihood = delta2Sum - priorLogLikelihood;
+
+ if (bestT == 0 || finalLogLikelihood < bestLogLikelihood) {
+ bestLogLikelihood = finalLogLikelihood;
+ bestT = tTest;
+ bestRB = rbTest;
+ }
+ }
+
+ t = bestT;
+ r = bestRB[0];
+ b = bestRB[1];
+ LOG(Awb, Debug)
+ << "Fine search found t " << t << " r " << r << " b " << b
+ << " error limits: " << errorLimits
+ << " prior log likelihood limits: " << priorLogLikelihoodLimits;
+}
+
+/**
+ * \brief Find extremum of function
+ * \param[in] a First point
+ * \param[in] b Second point
+ * \param[in] c Third point
+ *
+ * Given 3 points on a curve, find the extremum of the function in that interval
+ * by fitting a quadratic.
+ *
+ * \return The x value of the extremum clamped to the interval [a.x(), c.x()]
+ */
+double AwbBayes::interpolateQuadratic(Pwl::Point const &a, Pwl::Point const &b,
+ Pwl::Point const &c) const
+{
+ const double eps = 1e-3;
+ Pwl::Point ca = c - a;
+ Pwl::Point ba = b - a;
+ double denominator = 2 * (ba.y() * ca.x() - ca.y() * ba.x());
+ if (std::abs(denominator) > eps) {
+ double numerator = ba.y() * ca.x() * ca.x() - ca.y() * ba.x() * ba.x();
+ double result = numerator / denominator + a.x();
+ return std::max(a.x(), std::min(c.x(), result));
+ }
+ /* has degenerated to straight line segment */
+ return a.y() < c.y() - eps ? a.x() : (c.y() < a.y() - eps ? c.x() : b.x());
+}
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/awb_bayes.h b/src/ipa/libipa/awb_bayes.h
new file mode 100644
index 00000000..bb933038
--- /dev/null
+++ b/src/ipa/libipa/awb_bayes.h
@@ -0,0 +1,59 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024 Ideas on Board Oy
+ *
+ * Base class for bayes AWB algorithms
+ */
+
+#pragma once
+
+#include <libcamera/controls.h>
+
+#include "libcamera/internal/vector.h"
+#include "libcamera/internal/yaml_parser.h"
+
+#include "awb.h"
+#include "interpolator.h"
+#include "pwl.h"
+
+namespace libcamera {
+
+namespace ipa {
+
+class AwbBayes : public AwbAlgorithm
+{
+public:
+ AwbBayes() = default;
+
+ int init(const YamlObject &tuningData) override;
+ AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) override;
+ RGB<double> gainsFromColourTemperature(double temperatureK) override;
+ void handleControls(const ControlList &controls) override;
+
+private:
+ int readPriors(const YamlObject &tuningData);
+
+ void fineSearch(double &t, double &r, double &b, ipa::Pwl const &prior,
+ const AwbStats &stats) const;
+ double coarseSearch(const ipa::Pwl &prior, const AwbStats &stats) const;
+ double interpolateQuadratic(ipa::Pwl::Point const &a,
+ ipa::Pwl::Point const &b,
+ ipa::Pwl::Point const &c) const;
+
+ Interpolator<Pwl> priors_;
+ Interpolator<Vector<double, 2>> colourGainCurve_;
+
+ ipa::Pwl ctR_;
+ ipa::Pwl ctB_;
+ ipa::Pwl ctRInverse_;
+ ipa::Pwl ctBInverse_;
+
+ double transversePos_;
+ double transverseNeg_;
+
+ ModeConfig *currentMode_ = nullptr;
+};
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/awb_grey.cpp b/src/ipa/libipa/awb_grey.cpp
new file mode 100644
index 00000000..d3d2132b
--- /dev/null
+++ b/src/ipa/libipa/awb_grey.cpp
@@ -0,0 +1,114 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024 Ideas on Board Oy
+ *
+ * Implementation of grey world AWB algorithm
+ */
+
+#include "awb_grey.h"
+
+#include <algorithm>
+
+#include <libcamera/base/log.h>
+#include <libcamera/control_ids.h>
+
+#include "colours.h"
+
+using namespace libcamera::controls;
+
+/**
+ * \file awb_grey.h
+ * \brief Implementation of a grey world AWB algorithm
+ */
+
+namespace libcamera {
+
+LOG_DECLARE_CATEGORY(Awb)
+namespace ipa {
+
+/**
+ * \class AwbGrey
+ * \brief A Grey world auto white balance algorithm
+ */
+
+/**
+ * \brief Initialize the algorithm with the given tuning data
+ * \param[in] tuningData The tuning data for the algorithm
+ *
+ * Load the colour temperature curve from the tuning data. If there is no tuning
+ * data available, continue with a warning. Manual colour temperature will not
+ * work in that case.
+ *
+ * \return 0 on success, a negative error code otherwise
+ */
+int AwbGrey::init(const YamlObject &tuningData)
+{
+ Interpolator<Vector<double, 2>> gains;
+ int ret = gains.readYaml(tuningData["colourGains"], "ct", "gains");
+ if (ret < 0)
+ LOG(Awb, Warning)
+ << "Failed to parse 'colourGains' "
+ << "parameter from tuning file; "
+ << "manual colour temperature will not work properly";
+ else
+ colourGainCurve_ = gains;
+
+ return 0;
+}
+
+/**
+ * \brief Calculate AWB data from the given statistics
+ * \param[in] stats The statistics to use for the calculation
+ * \param[in] lux The lux value of the scene
+ *
+ * The colour temperature is estimated based on the colours::estimateCCT()
+ * function. The gains are calculated purely based on the RGB means provided by
+ * the \a stats. The colour temperature is not taken into account when
+ * calculating the gains.
+ *
+ * The \a lux parameter is not used in this algorithm.
+ *
+ * \return The AWB result
+ */
+AwbResult AwbGrey::calculateAwb(const AwbStats &stats, [[maybe_unused]] unsigned int lux)
+{
+ AwbResult result;
+ auto means = stats.rgbMeans();
+ result.colourTemperature = estimateCCT(means);
+
+ /*
+ * Estimate the red and blue gains to apply in a grey world. The green
+ * gain is hardcoded to 1.0. Avoid divisions by zero by clamping the
+ * divisor to a minimum value of 1.0.
+ */
+ result.gains.r() = means.g() / std::max(means.r(), 1.0);
+ result.gains.g() = 1.0;
+ result.gains.b() = means.g() / std::max(means.b(), 1.0);
+ return result;
+}
+
+/**
+ * \brief Compute white balance gains from a colour temperature
+ * \param[in] colourTemperature The colour temperature in Kelvin
+ *
+ * Compute the white balance gains from a \a colourTemperature. This function
+ * does not take any statistics into account. It simply interpolates the colour
+ * gains configured in the colour temperature curve.
+ *
+ * \return The colour gains if a colour temperature curve is available,
+ * [1, 1, 1] otherwise.
+ */
+RGB<double> AwbGrey::gainsFromColourTemperature(double colourTemperature)
+{
+ if (!colourGainCurve_) {
+ LOG(Awb, Error) << "No gains defined";
+ return RGB<double>({ 1.0, 1.0, 1.0 });
+ }
+
+ auto gains = colourGainCurve_->getInterpolated(colourTemperature);
+ return { { gains[0], 1.0, gains[1] } };
+}
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/awb_grey.h b/src/ipa/libipa/awb_grey.h
new file mode 100644
index 00000000..7ec7bfa5
--- /dev/null
+++ b/src/ipa/libipa/awb_grey.h
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2024 Ideas on Board Oy
+ *
+ * AWB grey world algorithm
+ */
+
+#pragma once
+
+#include <optional>
+
+#include "libcamera/internal/vector.h"
+
+#include "awb.h"
+#include "interpolator.h"
+
+namespace libcamera {
+
+namespace ipa {
+
+class AwbGrey : public AwbAlgorithm
+{
+public:
+ AwbGrey() = default;
+
+ int init(const YamlObject &tuningData) override;
+ AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) override;
+ RGB<double> gainsFromColourTemperature(double colourTemperature) override;
+
+private:
+ std::optional<Interpolator<Vector<double, 2>>> colourGainCurve_;
+};
+
+} /* namespace ipa */
+
+} /* namespace libcamera */
diff --git a/src/ipa/libipa/colours.h b/src/ipa/libipa/colours.h
index fa6a8b57..d39b2ca8 100644
--- a/src/ipa/libipa/colours.h
+++ b/src/ipa/libipa/colours.h
@@ -9,7 +9,7 @@
#include <stdint.h>
-#include "vector.h"
+#include "libcamera/internal/vector.h"
namespace libcamera {
diff --git a/src/ipa/libipa/exposure_mode_helper.cpp b/src/ipa/libipa/exposure_mode_helper.cpp
index f235316d..0c1e99e3 100644
--- a/src/ipa/libipa/exposure_mode_helper.cpp
+++ b/src/ipa/libipa/exposure_mode_helper.cpp
@@ -182,6 +182,7 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
* the stage limits are initialised.
*/
+ /* Clamp the gain to lastStageGain and regulate exposureTime. */
if (stageExposureTime * lastStageGain >= exposure) {
exposureTime = clampExposureTime(exposure / clampGain(lastStageGain));
gain = clampGain(exposure / exposureTime);
@@ -189,8 +190,9 @@ ExposureModeHelper::splitExposure(utils::Duration exposure) const
return { exposureTime, gain, exposure / (exposureTime * gain) };
}
+ /* Clamp the exposureTime to stageExposureTime and regulate gain. */
if (stageExposureTime * stageGain >= exposure) {
- exposureTime = clampExposureTime(exposure / clampGain(stageGain));
+ exposureTime = clampExposureTime(stageExposureTime);
gain = clampGain(exposure / exposureTime);
return { exposureTime, gain, exposure / (exposureTime * gain) };
diff --git a/src/ipa/libipa/interpolator.cpp b/src/ipa/libipa/interpolator.cpp
index 73e8d3b7..f901a86e 100644
--- a/src/ipa/libipa/interpolator.cpp
+++ b/src/ipa/libipa/interpolator.cpp
@@ -126,6 +126,13 @@ namespace ipa {
*/
/**
+ * \fn std::map<unsigned int, T> &Interpolator<T>::data() const
+ * \brief Access the internal map
+ *
+ * \return The internal map
+ */
+
+/**
* \fn const T& Interpolator<T>::getInterpolated()
* \brief Retrieve an interpolated value for the given key
* \param[in] key The unsigned integer key of the object to retrieve
@@ -136,8 +143,7 @@ namespace ipa {
*/
/**
- * \fn void Interpolator<T>::interpolate(const T &a, const T &b, T &dest, double
- * lambda)
+ * \fn void Interpolator<T>::interpolate(const T &a, const T &b, T &dest, double lambda)
* \brief Interpolate between two instances of T
* \param a The first value to interpolate
* \param b The second value to interpolate
diff --git a/src/ipa/libipa/interpolator.h b/src/ipa/libipa/interpolator.h
index fffce214..7880db69 100644
--- a/src/ipa/libipa/interpolator.h
+++ b/src/ipa/libipa/interpolator.h
@@ -81,6 +81,11 @@ public:
lastInterpolatedKey_.reset();
}
+ const std::map<unsigned int, T> &data() const
+ {
+ return data_;
+ }
+
const T &getInterpolated(unsigned int key, unsigned int *quantizedKey = nullptr)
{
ASSERT(data_.size() > 0);
diff --git a/src/ipa/libipa/lux.cpp b/src/ipa/libipa/lux.cpp
index 61f8fea8..899e8824 100644
--- a/src/ipa/libipa/lux.cpp
+++ b/src/ipa/libipa/lux.cpp
@@ -44,11 +44,6 @@ namespace ipa {
*/
/**
- * \var Lux::binSize_
- * \brief The maximum count of each bin
- */
-
-/**
* \var Lux::referenceExposureTime_
* \brief The exposure time of the reference image, in microseconds
*/
@@ -65,9 +60,8 @@ namespace ipa {
/**
* \var Lux::referenceY_
- * \brief The measured luminance of the reference image, out of the bin size
+ * \brief The measured luminance of the reference image, normalized to 1
*
- * \sa binSize_
*/
/**
@@ -76,11 +70,9 @@ namespace ipa {
*/
/**
- * \brief Construct the Lux helper module
- * \param[in] binSize The maximum count of each bin
- */
-Lux::Lux(unsigned int binSize)
- : binSize_(binSize)
+ * \brief Construct the Lux helper module
+ */
+Lux::Lux()
{
}
@@ -97,7 +89,7 @@ Lux::Lux(unsigned int binSize)
* referenceExposureTime: 10000
* referenceAnalogueGain: 4.0
* referenceDigitalGain: 1.0
- * referenceY: 12000
+ * referenceY: 0.1831
* referenceLux: 1000
* \endcode
*
@@ -167,7 +159,7 @@ double Lux::estimateLux(utils::Duration exposureTime,
double exposureTimeRatio = referenceExposureTime_ / exposureTime;
double aGainRatio = referenceAnalogueGain_ / aGain;
double dGainRatio = referenceDigitalGain_ / dGain;
- double yRatio = currentY * (binSize_ / yHist.bins()) / referenceY_;
+ double yRatio = (currentY / yHist.bins()) / referenceY_;
double estimatedLux = exposureTimeRatio * aGainRatio * dGainRatio *
yRatio * referenceLux_;
diff --git a/src/ipa/libipa/lux.h b/src/ipa/libipa/lux.h
index 93ca6479..d95bcdaf 100644
--- a/src/ipa/libipa/lux.h
+++ b/src/ipa/libipa/lux.h
@@ -21,7 +21,7 @@ class Histogram;
class Lux
{
public:
- Lux(unsigned int binSize);
+ Lux();
int parseTuningData(const YamlObject &tuningData);
double estimateLux(utils::Duration exposureTime,
@@ -29,7 +29,6 @@ public:
const Histogram &yHist) const;
private:
- unsigned int binSize_;
utils::Duration referenceExposureTime_;
double referenceAnalogueGain_;
double referenceDigitalGain_;
diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build
index f2b2f4be..660be940 100644
--- a/src/ipa/libipa/meson.build
+++ b/src/ipa/libipa/meson.build
@@ -3,6 +3,9 @@
libipa_headers = files([
'agc_mean_luminance.h',
'algorithm.h',
+ 'awb_bayes.h',
+ 'awb_grey.h',
+ 'awb.h',
'camera_sensor_helper.h',
'colours.h',
'exposure_mode_helper.h',
@@ -14,12 +17,14 @@ libipa_headers = files([
'lux.h',
'module.h',
'pwl.h',
- 'vector.h',
])
libipa_sources = files([
'agc_mean_luminance.cpp',
'algorithm.cpp',
+ 'awb_bayes.cpp',
+ 'awb_grey.cpp',
+ 'awb.cpp',
'camera_sensor_helper.cpp',
'colours.cpp',
'exposure_mode_helper.cpp',
@@ -31,7 +36,6 @@ libipa_sources = files([
'lux.cpp',
'module.cpp',
'pwl.cpp',
- 'vector.cpp',
])
libipa_includes = include_directories('..')
diff --git a/src/ipa/libipa/pwl.cpp b/src/ipa/libipa/pwl.cpp
index 88fe2022..3fa005ba 100644
--- a/src/ipa/libipa/pwl.cpp
+++ b/src/ipa/libipa/pwl.cpp
@@ -160,6 +160,11 @@ void Pwl::prepend(double x, double y, const double eps)
*/
/**
+ * \fn Pwl::clear()
+ * \brief Clear the piecewise linear function
+ */
+
+/**
* \fn Pwl::size() const
* \brief Retrieve the number of points in the piecewise linear function
* \return The number of points in the piecewise linear function
diff --git a/src/ipa/libipa/pwl.h b/src/ipa/libipa/pwl.h
index d4ec9f4f..c1496c30 100644
--- a/src/ipa/libipa/pwl.h
+++ b/src/ipa/libipa/pwl.h
@@ -12,7 +12,7 @@
#include <utility>
#include <vector>
-#include "vector.h"
+#include "libcamera/internal/vector.h"
namespace libcamera {
@@ -49,6 +49,7 @@ public:
void append(double x, double y, double eps = 1e-6);
bool empty() const { return points_.empty(); }
+ void clear() { points_.clear(); }
size_t size() const { return points_.size(); }
Interval domain() const;
diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
deleted file mode 100644
index 8019f8cf..00000000
--- a/src/ipa/libipa/vector.cpp
+++ /dev/null
@@ -1,351 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-/*
- * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
- *
- * Vector and related operations
- */
-
-#include "vector.h"
-
-#include <libcamera/base/log.h>
-
-/**
- * \file vector.h
- * \brief Vector class
- */
-
-namespace libcamera {
-
-LOG_DEFINE_CATEGORY(Vector)
-
-namespace ipa {
-
-/**
- * \class Vector
- * \brief Vector class
- * \tparam T Type of numerical values to be stored in the vector
- * \tparam Rows Number of dimension of the vector (= number of elements)
- */
-
-/**
- * \fn Vector::Vector()
- * \brief Construct an uninitialized vector
- */
-
-/**
- * \fn Vector::Vector(T scalar)
- * \brief Construct a vector filled with a \a scalar value
- * \param[in] scalar The scalar value
- */
-
-/**
- * \fn Vector::Vector(const std::array<T, Rows> &data)
- * \brief Construct vector from supplied data
- * \param data Data from which to construct a vector
- *
- * The size of \a data must be equal to the dimension size Rows of the vector.
- */
-
-/**
- * \fn T Vector::operator[](size_t i) const
- * \brief Index to an element in the vector
- * \param i Index of element to retrieve
- * \return Element at index \a i from the vector
- */
-
-/**
- * \fn T &Vector::operator[](size_t i)
- * \copydoc Vector::operator[](size_t i) const
- */
-
-/**
- * \fn Vector::operator-() const
- * \brief Negate a Vector by negating both all of its coordinates
- * \return The negated vector
- */
-
-/**
- * \fn Vector::operator+(Vector const &other) const
- * \brief Calculate the sum of this vector and \a other element-wise
- * \param[in] other The other vector
- * \return The element-wise sum of this vector and \a other
- */
-
-/**
- * \fn Vector::operator+(T scalar) const
- * \brief Calculate the sum of this vector and \a scalar element-wise
- * \param[in] scalar The scalar
- * \return The element-wise sum of this vector and \a other
- */
-
-/**
- * \fn Vector::operator-(Vector const &other) const
- * \brief Calculate the difference of this vector and \a other element-wise
- * \param[in] other The other vector
- * \return The element-wise subtraction of \a other from this vector
- */
-
-/**
- * \fn Vector::operator-(T scalar) const
- * \brief Calculate the difference of this vector and \a scalar element-wise
- * \param[in] scalar The scalar
- * \return The element-wise subtraction of \a scalar from this vector
- */
-
-/**
- * \fn Vector::operator*(const Vector &other) const
- * \brief Calculate the product of this vector and \a other element-wise
- * \param[in] other The other vector
- * \return The element-wise product of this vector and \a other
- */
-
-/**
- * \fn Vector::operator*(T scalar) const
- * \brief Calculate the product of this vector and \a scalar element-wise
- * \param[in] scalar The scalar
- * \return The element-wise product of this vector and \a scalar
- */
-
-/**
- * \fn Vector::operator/(const Vector &other) const
- * \brief Calculate the quotient of this vector and \a other element-wise
- * \param[in] other The other vector
- * \return The element-wise division of this vector by \a other
- */
-
-/**
- * \fn Vector::operator/(T scalar) const
- * \brief Calculate the quotient of this vector and \a scalar element-wise
- * \param[in] scalar The scalar
- * \return The element-wise division of this vector by \a scalar
- */
-
-/**
- * \fn Vector::operator+=(Vector const &other)
- * \brief Add \a other element-wise to this vector
- * \param[in] other The other vector
- * \return This vector
- */
-
-/**
- * \fn Vector::operator+=(T scalar)
- * \brief Add \a scalar element-wise to this vector
- * \param[in] scalar The scalar
- * \return This vector
- */
-
-/**
- * \fn Vector::operator-=(Vector const &other)
- * \brief Subtract \a other element-wise from this vector
- * \param[in] other The other vector
- * \return This vector
- */
-
-/**
- * \fn Vector::operator-=(T scalar)
- * \brief Subtract \a scalar element-wise from this vector
- * \param[in] scalar The scalar
- * \return This vector
- */
-
-/**
- * \fn Vector::operator*=(const Vector &other)
- * \brief Multiply this vector by \a other element-wise
- * \param[in] other The other vector
- * \return This vector
- */
-
-/**
- * \fn Vector::operator*=(T scalar)
- * \brief Multiply this vector by \a scalar element-wise
- * \param[in] scalar The scalar
- * \return This vector
- */
-
-/**
- * \fn Vector::operator/=(const Vector &other)
- * \brief Divide this vector by \a other element-wise
- * \param[in] other The other vector
- * \return This vector
- */
-
-/**
- * \fn Vector::operator/=(T scalar)
- * \brief Divide this vector by \a scalar element-wise
- * \param[in] scalar The scalar
- * \return This vector
- */
-
-/**
- * \fn Vector::min(const Vector &other) const
- * \brief Calculate the minimum of this vector and \a other element-wise
- * \param[in] other The other vector
- * \return The element-wise minimum of this vector and \a other
- */
-
-/**
- * \fn Vector::min(T scalar) const
- * \brief Calculate the minimum of this vector and \a scalar element-wise
- * \param[in] scalar The scalar
- * \return The element-wise minimum of this vector and \a scalar
- */
-
-/**
- * \fn Vector::max(const Vector &other) const
- * \brief Calculate the maximum of this vector and \a other element-wise
- * \param[in] other The other vector
- * \return The element-wise maximum of this vector and \a other
- */
-
-/**
- * \fn Vector::max(T scalar) const
- * \brief Calculate the maximum of this vector and \a scalar element-wise
- * \param[in] scalar The scalar
- * \return The element-wise maximum of this vector and \a scalar
- */
-
-/**
- * \fn Vector::dot(const Vector<T, Rows> &other) const
- * \brief Compute the dot product
- * \param[in] other The other vector
- * \return The dot product of the two vectors
- */
-
-/**
- * \fn constexpr T &Vector::x()
- * \brief Convenience function to access the first element of the vector
- * \return The first element of the vector
- */
-
-/**
- * \fn constexpr T &Vector::y()
- * \brief Convenience function to access the second element of the vector
- * \return The second element of the vector
- */
-
-/**
- * \fn constexpr T &Vector::z()
- * \brief Convenience function to access the third element of the vector
- * \return The third element of the vector
- */
-
-/**
- * \fn constexpr const T &Vector::x() const
- * \copydoc Vector::x()
- */
-
-/**
- * \fn constexpr const T &Vector::y() const
- * \copydoc Vector::y()
- */
-
-/**
- * \fn constexpr const T &Vector::z() const
- * \copydoc Vector::z()
- */
-
-/**
- * \fn constexpr T &Vector::r()
- * \brief Convenience function to access the first element of the vector
- * \return The first element of the vector
- */
-
-/**
- * \fn constexpr T &Vector::g()
- * \brief Convenience function to access the second element of the vector
- * \return The second element of the vector
- */
-
-/**
- * \fn constexpr T &Vector::b()
- * \brief Convenience function to access the third element of the vector
- * \return The third element of the vector
- */
-
-/**
- * \fn constexpr const T &Vector::r() const
- * \copydoc Vector::r()
- */
-
-/**
- * \fn constexpr const T &Vector::g() const
- * \copydoc Vector::g()
- */
-
-/**
- * \fn constexpr const T &Vector::b() const
- * \copydoc Vector::b()
- */
-
-/**
- * \fn Vector::length2()
- * \brief Get the squared length of the vector
- * \return The squared length of the vector
- */
-
-/**
- * \fn Vector::length()
- * \brief Get the length of the vector
- * \return The length of the vector
- */
-
-/**
- * \fn Vector::sum() const
- * \brief Calculate the sum of all the vector elements
- * \tparam R The type of the sum
- *
- * The type R of the sum defaults to the type T of the elements, but can be set
- * explicitly to use a different type in case the type T would risk
- * overflowing.
- *
- * \return The sum of all the vector elements
- */
-
-/**
- * \fn Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
- * \brief Multiply a matrix by a vector
- * \tparam T Numerical type of the contents of the matrix and vector
- * \tparam Rows The number of rows in the matrix
- * \tparam Cols The number of columns in the matrix (= rows in the vector)
- * \param m The matrix
- * \param v The vector
- * \return Product of matrix \a m and vector \a v
- */
-
-/**
- * \typedef RGB
- * \brief A Vector of 3 elements representing an RGB pixel value
- */
-
-/**
- * \fn bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
- * \brief Compare vectors for equality
- * \return True if the two vectors are equal, false otherwise
- */
-
-/**
- * \fn bool operator!=(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
- * \brief Compare vectors for inequality
- * \return True if the two vectors are not equal, false otherwise
- */
-
-#ifndef __DOXYGEN__
-bool vectorValidateYaml(const YamlObject &obj, unsigned int size)
-{
- if (!obj.isList())
- return false;
-
- if (obj.size() != size) {
- LOG(Vector, Error)
- << "Wrong number of values in YAML vector: expected "
- << size << ", got " << obj.size();
- return false;
- }
-
- return true;
-}
-#endif /* __DOXYGEN__ */
-
-} /* namespace ipa */
-
-} /* namespace libcamera */
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
deleted file mode 100644
index fe33c9d6..00000000
--- a/src/ipa/libipa/vector.h
+++ /dev/null
@@ -1,370 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-/*
- * Copyright (C) 2024, Paul Elder <paul.elder@ideasonboard.com>
- *
- * Vector and related operations
- */
-#pragma once
-
-#include <algorithm>
-#include <array>
-#include <cmath>
-#include <functional>
-#include <numeric>
-#include <optional>
-#include <ostream>
-
-#include <libcamera/base/log.h>
-#include <libcamera/base/span.h>
-
-#include "libcamera/internal/matrix.h"
-#include "libcamera/internal/yaml_parser.h"
-
-namespace libcamera {
-
-LOG_DECLARE_CATEGORY(Vector)
-
-namespace ipa {
-
-#ifndef __DOXYGEN__
-template<typename T, unsigned int Rows,
- std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
-#else
-template<typename T, unsigned int Rows>
-#endif /* __DOXYGEN__ */
-class Vector
-{
-public:
- constexpr Vector() = default;
-
- constexpr explicit Vector(T scalar)
- {
- data_.fill(scalar);
- }
-
- constexpr Vector(const std::array<T, Rows> &data)
- {
- for (unsigned int i = 0; i < Rows; i++)
- data_[i] = data[i];
- }
-
- const T &operator[](size_t i) const
- {
- ASSERT(i < data_.size());
- return data_[i];
- }
-
- T &operator[](size_t i)
- {
- ASSERT(i < data_.size());
- return data_[i];
- }
-
- constexpr Vector<T, Rows> operator-() const
- {
- Vector<T, Rows> ret;
- for (unsigned int i = 0; i < Rows; i++)
- ret[i] = -data_[i];
- return ret;
- }
-
- constexpr Vector operator+(const Vector &other) const
- {
- return apply(*this, other, std::plus<>{});
- }
-
- constexpr Vector operator+(T scalar) const
- {
- return apply(*this, scalar, std::plus<>{});
- }
-
- constexpr Vector operator-(const Vector &other) const
- {
- return apply(*this, other, std::minus<>{});
- }
-
- constexpr Vector operator-(T scalar) const
- {
- return apply(*this, scalar, std::minus<>{});
- }
-
- constexpr Vector operator*(const Vector &other) const
- {
- return apply(*this, other, std::multiplies<>{});
- }
-
- constexpr Vector operator*(T scalar) const
- {
- return apply(*this, scalar, std::multiplies<>{});
- }
-
- constexpr Vector operator/(const Vector &other) const
- {
- return apply(*this, other, std::divides<>{});
- }
-
- constexpr Vector operator/(T scalar) const
- {
- return apply(*this, scalar, std::divides<>{});
- }
-
- Vector &operator+=(const Vector &other)
- {
- return apply(other, [](T a, T b) { return a + b; });
- }
-
- Vector &operator+=(T scalar)
- {
- return apply(scalar, [](T a, T b) { return a + b; });
- }
-
- Vector &operator-=(const Vector &other)
- {
- return apply(other, [](T a, T b) { return a - b; });
- }
-
- Vector &operator-=(T scalar)
- {
- return apply(scalar, [](T a, T b) { return a - b; });
- }
-
- Vector &operator*=(const Vector &other)
- {
- return apply(other, [](T a, T b) { return a * b; });
- }
-
- Vector &operator*=(T scalar)
- {
- return apply(scalar, [](T a, T b) { return a * b; });
- }
-
- Vector &operator/=(const Vector &other)
- {
- return apply(other, [](T a, T b) { return a / b; });
- }
-
- Vector &operator/=(T scalar)
- {
- return apply(scalar, [](T a, T b) { return a / b; });
- }
-
- constexpr Vector min(const Vector &other) const
- {
- return apply(*this, other, [](T a, T b) { return std::min(a, b); });
- }
-
- constexpr Vector min(T scalar) const
- {
- return apply(*this, scalar, [](T a, T b) { return std::min(a, b); });
- }
-
- constexpr Vector max(const Vector &other) const
- {
- return apply(*this, other, [](T a, T b) { return std::max(a, b); });
- }
-
- constexpr Vector max(T scalar) const
- {
- return apply(*this, scalar, [](T a, T b) -> T { return std::max(a, b); });
- }
-
- constexpr T dot(const Vector<T, Rows> &other) const
- {
- T ret = 0;
- for (unsigned int i = 0; i < Rows; i++)
- ret += data_[i] * other[i];
- return ret;
- }
-
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
-#endif /* __DOXYGEN__ */
- constexpr const T &x() const { return data_[0]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
-#endif /* __DOXYGEN__ */
- constexpr const T &y() const { return data_[1]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
-#endif /* __DOXYGEN__ */
- constexpr const T &z() const { return data_[2]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
-#endif /* __DOXYGEN__ */
- constexpr T &x() { return data_[0]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
-#endif /* __DOXYGEN__ */
- constexpr T &y() { return data_[1]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
-#endif /* __DOXYGEN__ */
- constexpr T &z() { return data_[2]; }
-
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
-#endif /* __DOXYGEN__ */
- constexpr const T &r() const { return data_[0]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
-#endif /* __DOXYGEN__ */
- constexpr const T &g() const { return data_[1]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
-#endif /* __DOXYGEN__ */
- constexpr const T &b() const { return data_[2]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 1>>
-#endif /* __DOXYGEN__ */
- constexpr T &r() { return data_[0]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 2>>
-#endif /* __DOXYGEN__ */
- constexpr T &g() { return data_[1]; }
-#ifndef __DOXYGEN__
- template<bool Dependent = false, typename = std::enable_if_t<Dependent || Rows >= 3>>
-#endif /* __DOXYGEN__ */
- constexpr T &b() { return data_[2]; }
-
- constexpr double length2() const
- {
- double ret = 0;
- for (unsigned int i = 0; i < Rows; i++)
- ret += data_[i] * data_[i];
- return ret;
- }
-
- constexpr double length() const
- {
- return std::sqrt(length2());
- }
-
- template<typename R = T>
- constexpr R sum() const
- {
- return std::accumulate(data_.begin(), data_.end(), R{});
- }
-
-private:
- template<class BinaryOp>
- static constexpr Vector apply(const Vector &lhs, const Vector &rhs, BinaryOp op)
- {
- Vector result;
- std::transform(lhs.data_.begin(), lhs.data_.end(),
- rhs.data_.begin(), result.data_.begin(),
- op);
-
- return result;
- }
-
- template<class BinaryOp>
- static constexpr Vector apply(const Vector &lhs, T rhs, BinaryOp op)
- {
- Vector result;
- std::transform(lhs.data_.begin(), lhs.data_.end(),
- result.data_.begin(),
- [&op, rhs](T v) { return op(v, rhs); });
-
- return result;
- }
-
- template<class BinaryOp>
- Vector &apply(const Vector &other, BinaryOp op)
- {
- auto itOther = other.data_.begin();
- std::for_each(data_.begin(), data_.end(),
- [&op, &itOther](T &v) { v = op(v, *itOther++); });
-
- return *this;
- }
-
- template<class BinaryOp>
- Vector &apply(T scalar, BinaryOp op)
- {
- std::for_each(data_.begin(), data_.end(),
- [&op, scalar](T &v) { v = op(v, scalar); });
-
- return *this;
- }
-
- std::array<T, Rows> data_;
-};
-
-template<typename T>
-using RGB = Vector<T, 3>;
-
-template<typename T, unsigned int Rows, unsigned int Cols>
-Vector<T, Rows> operator*(const Matrix<T, Rows, Cols> &m, const Vector<T, Cols> &v)
-{
- Vector<T, Rows> result;
-
- for (unsigned int i = 0; i < Rows; i++) {
- T sum = 0;
- for (unsigned int j = 0; j < Cols; j++)
- sum += m[i][j] * v[j];
- result[i] = sum;
- }
-
- return result;
-}
-
-template<typename T, unsigned int Rows>
-bool operator==(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
-{
- for (unsigned int i = 0; i < Rows; i++) {
- if (lhs[i] != rhs[i])
- return false;
- }
-
- return true;
-}
-
-template<typename T, unsigned int Rows>
-bool operator!=(const Vector<T, Rows> &lhs, const Vector<T, Rows> &rhs)
-{
- return !(lhs == rhs);
-}
-
-#ifndef __DOXYGEN__
-bool vectorValidateYaml(const YamlObject &obj, unsigned int size);
-#endif /* __DOXYGEN__ */
-
-} /* namespace ipa */
-
-#ifndef __DOXYGEN__
-template<typename T, unsigned int Rows>
-std::ostream &operator<<(std::ostream &out, const ipa::Vector<T, Rows> &v)
-{
- out << "Vector { ";
- for (unsigned int i = 0; i < Rows; i++) {
- out << v[i];
- out << ((i + 1 < Rows) ? ", " : " ");
- }
- out << " }";
-
- return out;
-}
-
-template<typename T, unsigned int Rows>
-struct YamlObject::Getter<ipa::Vector<T, Rows>> {
- std::optional<ipa::Vector<T, Rows>> get(const YamlObject &obj) const
- {
- if (!ipa::vectorValidateYaml(obj, Rows))
- return std::nullopt;
-
- ipa::Vector<T, Rows> vector;
-
- unsigned int i = 0;
- for (const YamlObject &entry : obj.asList()) {
- const auto value = entry.get<T>();
- if (!value)
- return std::nullopt;
- vector[i++] = *value;
- }
-
- return vector;
- }
-};
-#endif /* __DOXYGEN__ */
-
-} /* namespace libcamera */