From 6a7fe29a1870510b134a8485eac96031e56ff689 Mon Sep 17 00:00:00 2001 From: Milan Zamazal Date: Mon, 29 Sep 2025 22:19:21 +0200 Subject: [PATCH] ipa: simple: awb: Avoid incorrect arithmetic in AWB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The R/G/B sums computed in AWB simple IPA may be zero or perhaps even negative. Let's make sure the sums are always positive, to prevent division by zero or completely nonsense results. Signed-off-by: Milan Zamazal Reviewed-by: Barnabás Pőcze Reviewed-by: Hans de Goede Tested-by: Hans de Goede Signed-off-by: Kieran Bingham --- src/ipa/simple/algorithms/awb.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp index ddd0b791..4d33b92d 100644 --- a/src/ipa/simple/algorithms/awb.cpp +++ b/src/ipa/simple/algorithms/awb.cpp @@ -7,6 +7,7 @@ #include "awb.h" +#include #include #include @@ -72,9 +73,10 @@ void Awb::process(IPAContext &context, const uint64_t nPixels = std::accumulate( histogram.begin(), histogram.end(), 0); const uint64_t offset = blackLevel * nPixels; - const uint64_t sumR = stats->sumR_ - offset / 4; - const uint64_t sumG = stats->sumG_ - offset / 2; - const uint64_t sumB = stats->sumB_ - offset / 4; + const uint64_t minValid = 1; + const uint64_t sumR = stats->sumR_ > offset / 4 ? stats->sumR_ - offset / 4 : minValid; + const uint64_t sumG = stats->sumG_ > offset / 2 ? stats->sumG_ - offset / 2 : minValid; + const uint64_t sumB = stats->sumB_ > offset / 4 ? stats->sumB_ - offset / 4 : minValid; /* * Calculate red and blue gains for AWB.