Message ID | 20250403154925.382973-14-stefan.klug@ideasonboard.com |
---|---|
State | Accepted |
Headers | show |
Series |
|
Related | show |
On Thu, Apr 03, 2025 at 05:49:18PM +0200, Stefan Klug wrote: > In the grey world AWB case, if no colour gains are contained in the > tuning file, the colour gains get reset to 1 when the colour temperature > is set manually. This is unexpected and undesirable. Allow the > gainsFromColourTemp() function to return a std::nullopt to handle that > case. > > While at it, remove an unnecessary import from rkisp1/algorithms/awb.h. > > Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com> > > --- > > Changes in v2: > - Added this patch > > Changes in v3: > - Added #include <optional> to libipa/awb.h > - Removed stray include from rkisp1/awb.h > - Reordered colour temperature assignment for better clarity > --- > src/ipa/libipa/awb.cpp | 2 +- > src/ipa/libipa/awb.h | 3 ++- > src/ipa/libipa/awb_bayes.cpp | 4 ++-- > src/ipa/libipa/awb_bayes.h | 2 +- > src/ipa/libipa/awb_grey.cpp | 6 +++--- > src/ipa/libipa/awb_grey.h | 2 +- > src/ipa/rkisp1/algorithms/awb.cpp | 18 ++++++++++++------ > src/ipa/rkisp1/algorithms/awb.h | 2 -- > 8 files changed, 22 insertions(+), 17 deletions(-) > > diff --git a/src/ipa/libipa/awb.cpp b/src/ipa/libipa/awb.cpp > index 925fac232709..214bac8b56a6 100644 > --- a/src/ipa/libipa/awb.cpp > +++ b/src/ipa/libipa/awb.cpp > @@ -114,7 +114,7 @@ namespace ipa { > * 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 > + * \return The colour gains or std::nullopt if the conversion is not possible > */ > > /** > diff --git a/src/ipa/libipa/awb.h b/src/ipa/libipa/awb.h > index 4bab7a451ce5..f4a86038635f 100644 > --- a/src/ipa/libipa/awb.h > +++ b/src/ipa/libipa/awb.h > @@ -8,6 +8,7 @@ > #pragma once > > #include <map> > +#include <optional> > > #include <libcamera/control_ids.h> > #include <libcamera/controls.h> > @@ -39,7 +40,7 @@ public: > > virtual int init(const YamlObject &tuningData) = 0; > virtual AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) = 0; > - virtual RGB<double> gainsFromColourTemperature(double colourTemperature) = 0; > + virtual std::optional<RGB<double>> gainsFromColourTemperature(double colourTemperature) = 0; > > const ControlInfoMap::Map &controls() const > { > diff --git a/src/ipa/libipa/awb_bayes.cpp b/src/ipa/libipa/awb_bayes.cpp > index d1d0eaf0e68f..d2bcbd83d7f8 100644 > --- a/src/ipa/libipa/awb_bayes.cpp > +++ b/src/ipa/libipa/awb_bayes.cpp > @@ -270,7 +270,7 @@ void AwbBayes::handleControls(const ControlList &controls) > } > } > > -RGB<double> AwbBayes::gainsFromColourTemperature(double colourTemperature) > +std::optional<RGB<double>> AwbBayes::gainsFromColourTemperature(double colourTemperature) > { > /* > * \todo In the RaspberryPi code, the ct curve was interpolated in > @@ -278,7 +278,7 @@ RGB<double> AwbBayes::gainsFromColourTemperature(double colourTemperature) > * 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] } }; > + return RGB<double>{ { gains[0], 1.0, gains[1] } }; > } > > AwbResult AwbBayes::calculateAwb(const AwbStats &stats, unsigned int lux) > diff --git a/src/ipa/libipa/awb_bayes.h b/src/ipa/libipa/awb_bayes.h > index bb933038d6d2..47ef3cce4d58 100644 > --- a/src/ipa/libipa/awb_bayes.h > +++ b/src/ipa/libipa/awb_bayes.h > @@ -27,7 +27,7 @@ public: > > int init(const YamlObject &tuningData) override; > AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) override; > - RGB<double> gainsFromColourTemperature(double temperatureK) override; > + std::optional<RGB<double>> gainsFromColourTemperature(double temperatureK) override; > void handleControls(const ControlList &controls) override; > > private: > diff --git a/src/ipa/libipa/awb_grey.cpp b/src/ipa/libipa/awb_grey.cpp > index d3d2132b8869..d252edb2b6c6 100644 > --- a/src/ipa/libipa/awb_grey.cpp > +++ b/src/ipa/libipa/awb_grey.cpp > @@ -98,15 +98,15 @@ AwbResult AwbGrey::calculateAwb(const AwbStats &stats, [[maybe_unused]] unsigned > * \return The colour gains if a colour temperature curve is available, > * [1, 1, 1] otherwise. > */ > -RGB<double> AwbGrey::gainsFromColourTemperature(double colourTemperature) > +std::optional<RGB<double>> AwbGrey::gainsFromColourTemperature(double colourTemperature) > { > if (!colourGainCurve_) { > LOG(Awb, Error) << "No gains defined"; > - return RGB<double>({ 1.0, 1.0, 1.0 }); > + return std::nullopt; > } > > auto gains = colourGainCurve_->getInterpolated(colourTemperature); > - return { { gains[0], 1.0, gains[1] } }; > + return RGB<double>{ { gains[0], 1.0, gains[1] } }; > } > > } /* namespace ipa */ > diff --git a/src/ipa/libipa/awb_grey.h b/src/ipa/libipa/awb_grey.h > index 7ec7bfa5da9a..f82a368d11cc 100644 > --- a/src/ipa/libipa/awb_grey.h > +++ b/src/ipa/libipa/awb_grey.h > @@ -25,7 +25,7 @@ public: > > int init(const YamlObject &tuningData) override; > AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) override; > - RGB<double> gainsFromColourTemperature(double colourTemperature) override; > + std::optional<RGB<double>> gainsFromColourTemperature(double colourTemperature) override; > > private: > std::optional<Interpolator<Vector<double, 2>>> colourGainCurve_; > diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp > index 0795b8e5b1e1..f5415a0bb99d 100644 > --- a/src/ipa/rkisp1/algorithms/awb.cpp > +++ b/src/ipa/rkisp1/algorithms/awb.cpp > @@ -125,8 +125,12 @@ int Awb::configure(IPAContext &context, > const IPACameraSensorInfo &configInfo) > { > context.activeState.awb.manual.gains = RGB<double>{ 1.0 }; > - context.activeState.awb.automatic.gains = > - awbAlgo_->gainsFromColourTemperature(kDefaultColourTemperature); > + auto gains = awbAlgo_->gainsFromColourTemperature(kDefaultColourTemperature); > + if (gains) > + context.activeState.awb.automatic.gains = *gains; > + else > + context.activeState.awb.automatic.gains = RGB<double>{ 1.0 }; > + > context.activeState.awb.autoEnabled = true; > context.activeState.awb.manual.temperatureK = kDefaultColourTemperature; > context.activeState.awb.automatic.temperatureK = kDefaultColourTemperature; > @@ -183,11 +187,13 @@ void Awb::queueRequest(IPAContext &context, > */ > update = true; > } else if (colourTemperature) { > - const auto &gains = awbAlgo_->gainsFromColourTemperature(*colourTemperature); > - awb.manual.gains.r() = gains.r(); > - awb.manual.gains.b() = gains.b(); > awb.manual.temperatureK = *colourTemperature; > - update = true; > + const auto &gains = awbAlgo_->gainsFromColourTemperature(*colourTemperature); > + if (gains) { > + awb.manual.gains.r() = gains->r(); > + awb.manual.gains.b() = gains->b(); > + update = true; > + } > } > > if (update) > diff --git a/src/ipa/rkisp1/algorithms/awb.h b/src/ipa/rkisp1/algorithms/awb.h > index 7e6c3862e699..02651cc732bf 100644 > --- a/src/ipa/rkisp1/algorithms/awb.h > +++ b/src/ipa/rkisp1/algorithms/awb.h > @@ -7,8 +7,6 @@ > > #pragma once > > -#include <optional> > - > #include "libcamera/internal/vector.h" > > #include "libipa/awb.h" > -- > 2.43.0 >
diff --git a/src/ipa/libipa/awb.cpp b/src/ipa/libipa/awb.cpp index 925fac232709..214bac8b56a6 100644 --- a/src/ipa/libipa/awb.cpp +++ b/src/ipa/libipa/awb.cpp @@ -114,7 +114,7 @@ namespace ipa { * 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 + * \return The colour gains or std::nullopt if the conversion is not possible */ /** diff --git a/src/ipa/libipa/awb.h b/src/ipa/libipa/awb.h index 4bab7a451ce5..f4a86038635f 100644 --- a/src/ipa/libipa/awb.h +++ b/src/ipa/libipa/awb.h @@ -8,6 +8,7 @@ #pragma once #include <map> +#include <optional> #include <libcamera/control_ids.h> #include <libcamera/controls.h> @@ -39,7 +40,7 @@ public: virtual int init(const YamlObject &tuningData) = 0; virtual AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) = 0; - virtual RGB<double> gainsFromColourTemperature(double colourTemperature) = 0; + virtual std::optional<RGB<double>> gainsFromColourTemperature(double colourTemperature) = 0; const ControlInfoMap::Map &controls() const { diff --git a/src/ipa/libipa/awb_bayes.cpp b/src/ipa/libipa/awb_bayes.cpp index d1d0eaf0e68f..d2bcbd83d7f8 100644 --- a/src/ipa/libipa/awb_bayes.cpp +++ b/src/ipa/libipa/awb_bayes.cpp @@ -270,7 +270,7 @@ void AwbBayes::handleControls(const ControlList &controls) } } -RGB<double> AwbBayes::gainsFromColourTemperature(double colourTemperature) +std::optional<RGB<double>> AwbBayes::gainsFromColourTemperature(double colourTemperature) { /* * \todo In the RaspberryPi code, the ct curve was interpolated in @@ -278,7 +278,7 @@ RGB<double> AwbBayes::gainsFromColourTemperature(double colourTemperature) * 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] } }; + return RGB<double>{ { gains[0], 1.0, gains[1] } }; } AwbResult AwbBayes::calculateAwb(const AwbStats &stats, unsigned int lux) diff --git a/src/ipa/libipa/awb_bayes.h b/src/ipa/libipa/awb_bayes.h index bb933038d6d2..47ef3cce4d58 100644 --- a/src/ipa/libipa/awb_bayes.h +++ b/src/ipa/libipa/awb_bayes.h @@ -27,7 +27,7 @@ public: int init(const YamlObject &tuningData) override; AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) override; - RGB<double> gainsFromColourTemperature(double temperatureK) override; + std::optional<RGB<double>> gainsFromColourTemperature(double temperatureK) override; void handleControls(const ControlList &controls) override; private: diff --git a/src/ipa/libipa/awb_grey.cpp b/src/ipa/libipa/awb_grey.cpp index d3d2132b8869..d252edb2b6c6 100644 --- a/src/ipa/libipa/awb_grey.cpp +++ b/src/ipa/libipa/awb_grey.cpp @@ -98,15 +98,15 @@ AwbResult AwbGrey::calculateAwb(const AwbStats &stats, [[maybe_unused]] unsigned * \return The colour gains if a colour temperature curve is available, * [1, 1, 1] otherwise. */ -RGB<double> AwbGrey::gainsFromColourTemperature(double colourTemperature) +std::optional<RGB<double>> AwbGrey::gainsFromColourTemperature(double colourTemperature) { if (!colourGainCurve_) { LOG(Awb, Error) << "No gains defined"; - return RGB<double>({ 1.0, 1.0, 1.0 }); + return std::nullopt; } auto gains = colourGainCurve_->getInterpolated(colourTemperature); - return { { gains[0], 1.0, gains[1] } }; + return RGB<double>{ { gains[0], 1.0, gains[1] } }; } } /* namespace ipa */ diff --git a/src/ipa/libipa/awb_grey.h b/src/ipa/libipa/awb_grey.h index 7ec7bfa5da9a..f82a368d11cc 100644 --- a/src/ipa/libipa/awb_grey.h +++ b/src/ipa/libipa/awb_grey.h @@ -25,7 +25,7 @@ public: int init(const YamlObject &tuningData) override; AwbResult calculateAwb(const AwbStats &stats, unsigned int lux) override; - RGB<double> gainsFromColourTemperature(double colourTemperature) override; + std::optional<RGB<double>> gainsFromColourTemperature(double colourTemperature) override; private: std::optional<Interpolator<Vector<double, 2>>> colourGainCurve_; diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp index 0795b8e5b1e1..f5415a0bb99d 100644 --- a/src/ipa/rkisp1/algorithms/awb.cpp +++ b/src/ipa/rkisp1/algorithms/awb.cpp @@ -125,8 +125,12 @@ int Awb::configure(IPAContext &context, const IPACameraSensorInfo &configInfo) { context.activeState.awb.manual.gains = RGB<double>{ 1.0 }; - context.activeState.awb.automatic.gains = - awbAlgo_->gainsFromColourTemperature(kDefaultColourTemperature); + auto gains = awbAlgo_->gainsFromColourTemperature(kDefaultColourTemperature); + if (gains) + context.activeState.awb.automatic.gains = *gains; + else + context.activeState.awb.automatic.gains = RGB<double>{ 1.0 }; + context.activeState.awb.autoEnabled = true; context.activeState.awb.manual.temperatureK = kDefaultColourTemperature; context.activeState.awb.automatic.temperatureK = kDefaultColourTemperature; @@ -183,11 +187,13 @@ void Awb::queueRequest(IPAContext &context, */ update = true; } else if (colourTemperature) { - const auto &gains = awbAlgo_->gainsFromColourTemperature(*colourTemperature); - awb.manual.gains.r() = gains.r(); - awb.manual.gains.b() = gains.b(); awb.manual.temperatureK = *colourTemperature; - update = true; + const auto &gains = awbAlgo_->gainsFromColourTemperature(*colourTemperature); + if (gains) { + awb.manual.gains.r() = gains->r(); + awb.manual.gains.b() = gains->b(); + update = true; + } } if (update) diff --git a/src/ipa/rkisp1/algorithms/awb.h b/src/ipa/rkisp1/algorithms/awb.h index 7e6c3862e699..02651cc732bf 100644 --- a/src/ipa/rkisp1/algorithms/awb.h +++ b/src/ipa/rkisp1/algorithms/awb.h @@ -7,8 +7,6 @@ #pragma once -#include <optional> - #include "libcamera/internal/vector.h" #include "libipa/awb.h"