[5/5] libcamera: simple: Prevent division by zero in BLC
diff mbox series

Message ID 20250821134141.83236-6-mzamazal@redhat.com
State Superseded
Headers show
Series
  • Fix stats related problems in software ISP
Related show

Commit Message

Milan Zamazal Aug. 21, 2025, 1:41 p.m. UTC
When there are no values in the histogram, BLC simple IPA can crash on
division by zero.  We cannot get anything meaningful in such a case
anyway, let's simply return from `process()' then.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 src/ipa/simple/algorithms/blc.cpp | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Barnabás Pőcze Sept. 10, 2025, 12:05 p.m. UTC | #1
Hi


Maybe the title should be something like "ipa: simple: blc: ..."

2025. 08. 21. 15:41 keltezéssel, Milan Zamazal írta:
> When there are no values in the histogram, BLC simple IPA can crash on
> division by zero.  We cannot get anything meaningful in such a case
> anyway, let's simply return from `process()' then.
> 
> Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
> ---
>   src/ipa/simple/algorithms/blc.cpp | 5 +++++
>   1 file changed, 5 insertions(+)
> 
> diff --git a/src/ipa/simple/algorithms/blc.cpp b/src/ipa/simple/algorithms/blc.cpp
> index 8c1e9ed08..71731c6f6 100644
> --- a/src/ipa/simple/algorithms/blc.cpp
> +++ b/src/ipa/simple/algorithms/blc.cpp
> @@ -77,6 +77,11 @@ void BlackLevel::process(IPAContext &context,
>   	constexpr float ignoredPercentage = 0.02;
>   	const unsigned int total =
>   		std::accumulate(begin(histogram), end(histogram), 0);
> +	if (total == 0) {
> +		LOG(IPASoftBL, Debug) << "Not guessing black level, histogram is empty";
> +		return;
> +	};
          ^-- no need

Tested-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>


> +
>   	const unsigned int pixelThreshold = ignoredPercentage * total;
>   	const unsigned int histogramRatio = 256 / SwIspStats::kYHistogramSize;
>   	const unsigned int currentBlackIdx =

Patch
diff mbox series

diff --git a/src/ipa/simple/algorithms/blc.cpp b/src/ipa/simple/algorithms/blc.cpp
index 8c1e9ed08..71731c6f6 100644
--- a/src/ipa/simple/algorithms/blc.cpp
+++ b/src/ipa/simple/algorithms/blc.cpp
@@ -77,6 +77,11 @@  void BlackLevel::process(IPAContext &context,
 	constexpr float ignoredPercentage = 0.02;
 	const unsigned int total =
 		std::accumulate(begin(histogram), end(histogram), 0);
+	if (total == 0) {
+		LOG(IPASoftBL, Debug) << "Not guessing black level, histogram is empty";
+		return;
+	};
+
 	const unsigned int pixelThreshold = ignoredPercentage * total;
 	const unsigned int histogramRatio = 256 / SwIspStats::kYHistogramSize;
 	const unsigned int currentBlackIdx =