{"id":25975,"url":"https://patchwork.libcamera.org/api/1.1/patches/25975/?format=json","web_url":"https://patchwork.libcamera.org/patch/25975/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260127123728.70513-1-mzamazal@redhat.com>","date":"2026-01-27T12:37:28","name":"libcamera: simple: Fix black level offsets in AWB","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"99d33a66f250b30195be592b2cf10bee83c876e8","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/1.1/people/177/?format=json","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/25975/mbox/","series":[{"id":5742,"url":"https://patchwork.libcamera.org/api/1.1/series/5742/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5742","date":"2026-01-27T12:37:28","name":"libcamera: simple: Fix black level offsets in AWB","version":1,"mbox":"https://patchwork.libcamera.org/series/5742/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/25975/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/25975/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id D8EDCC3220\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 Jan 2026 12:37:39 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 00A4661FC8;\n\tTue, 27 Jan 2026 13:37:38 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 906F761FBF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Jan 2026 13:37:37 +0100 (CET)","from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com\n\t(ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-355-krBOJFbPOM26pODRmibDdw-1;\n\tTue, 27 Jan 2026 07:37:34 -0500","from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com\n\t(mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com\n\t[10.30.177.17])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTPS\n\tid F41A31800624 for <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Jan 2026 12:37:33 +0000 (UTC)","from mzamazal-thinkpadp1gen7.tpbc.com (unknown [10.44.34.174])\n\tby mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix)\n\twith ESMTP id BEFD61956095; Tue, 27 Jan 2026 12:37:32 +0000 (UTC)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"GQCy6v0E\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1769517456;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding;\n\tbh=t6Z+J8sVeD8URo7A7kyv63jK28bZIrhzKlJ56AnsIQU=;\n\tb=GQCy6v0EOzruv4hu8W86IXHfsICwFOeVv9Vv2vtzYhRaigmmjqNy78l7gDbX1qPkTCn8gg\n\td5FqSV21gzksrGxp4oT8XwlDQcWuKyKPtiR/3DvMc9IBD9nZiDlfv4uPkrB2uVOCBIBCTX\n\t1WrvMIVVsv3GKarjE4VlzdTeA7FOs3Y=","X-MC-Unique":"krBOJFbPOM26pODRmibDdw-1","X-Mimecast-MFC-AGG-ID":"krBOJFbPOM26pODRmibDdw_1769517454","From":"Milan Zamazal <mzamazal@redhat.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Milan Zamazal <mzamazal@redhat.com>","Subject":"[PATCH] libcamera: simple: Fix black level offsets in AWB","Date":"Tue, 27 Jan 2026 13:37:28 +0100","Message-ID":"<20260127123728.70513-1-mzamazal@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.0 on 10.30.177.17","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"Phc-8iQdk99i1J1PsyRNRMIXdzmCNsWE2s5NQ4-rqmA_1769517454","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","content-type":"text/plain; charset=\"US-ASCII\"; x-default=true","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The black level offset subtracted in AWB is wrong.  It assumes that the\nstats contain sums of the individual colour pixels.  But they actually\ncontain sums of the colour channels of larger \"superpixels\" consisting\nof the individual colour pixels.  Each of the RGB colour values and the\ncomputed luminosity (a histogram entry) are added once to the stats per\nsuch a superpixel.  This means the offset computed from the black level\nand the number of pixels should be used as it is, not divided.\n\nThe patch fixes the subtracted offset.  Since it is the same for all the\nthree colours, a SwIspStats helper method returning an RGB instance is\nintroduced.  The individual colour variables are still retained in\nSwIspStats for maximum efficiency when gathering the stats.  SwIspStats\ndocstrings are changed to avoid the original confusion.\n\nFixes: 4e13c6f55bd667 (\"Honor black level in AWB\")\nSigned-off-by: Milan Zamazal <mzamazal@redhat.com>\n---\n .../internal/software_isp/swisp_stats.h         | 17 +++++++++++++----\n src/ipa/simple/algorithms/awb.cpp               | 10 ++++------\n 2 files changed, 17 insertions(+), 10 deletions(-)","diff":"diff --git a/include/libcamera/internal/software_isp/swisp_stats.h b/include/libcamera/internal/software_isp/swisp_stats.h\nindex 3c9860185..f8816209f 100644\n--- a/include/libcamera/internal/software_isp/swisp_stats.h\n+++ b/include/libcamera/internal/software_isp/swisp_stats.h\n@@ -10,6 +10,8 @@\n #include <array>\n #include <stdint.h>\n \n+#include \"libcamera/internal/vector.h\"\n+\n namespace libcamera {\n \n /**\n@@ -26,17 +28,24 @@ struct SwIspStats {\n \t */\n \tbool valid;\n \t/**\n-\t * \\brief Holds the sum of all sampled red pixels\n+\t * \\brief Holds the sum of red channels of all the sampled pixels\n \t */\n \tuint64_t sumR_;\n \t/**\n-\t * \\brief Holds the sum of all sampled green pixels\n+\t * \\brief Holds the sum of green channels of all the sampled pixels\n \t */\n \tuint64_t sumG_;\n \t/**\n-\t * \\brief Holds the sum of all sampled blue pixels\n+\t * \\brief Holds the sum of blue channels of all the sampled pixels\n \t */\n \tuint64_t sumB_;\n+\t/**\n+\t * \\brief Return the sums of colour channels of all the sampled pixels\n+\t */\n+\tRGB<uint64_t> rgbSum() const\n+\t{\n+\t\treturn RGB<uint64_t>({ sumR_, sumG_, sumB_ });\n+\t}\n \t/**\n \t * \\brief Number of bins in the yHistogram\n \t */\n@@ -46,7 +55,7 @@ struct SwIspStats {\n \t */\n \tusing Histogram = std::array<uint32_t, kYHistogramSize>;\n \t/**\n-\t * \\brief A histogram of luminance values\n+\t * \\brief A histogram of luminance values of all the sampled pixels\n \t */\n \tHistogram yHistogram;\n };\ndiff --git a/src/ipa/simple/algorithms/awb.cpp b/src/ipa/simple/algorithms/awb.cpp\nindex 0080865aa..4c8bfd2de 100644\n--- a/src/ipa/simple/algorithms/awb.cpp\n+++ b/src/ipa/simple/algorithms/awb.cpp\n@@ -1,6 +1,6 @@\n /* SPDX-License-Identifier: LGPL-2.1-or-later */\n /*\n- * Copyright (C) 2024, Red Hat Inc.\n+ * Copyright (C) 2024-2026 Red Hat Inc.\n  *\n  * Auto white balance\n  */\n@@ -73,9 +73,7 @@ void Awb::process(IPAContext &context,\n \t\thistogram.begin(), histogram.end(), uint64_t(0));\n \tconst uint64_t offset = blackLevel * nPixels;\n \tconst uint64_t minValid = 1;\n-\tconst uint64_t sumR = stats->sumR_ > offset / 4 ? stats->sumR_ - offset / 4 : minValid;\n-\tconst uint64_t sumG = stats->sumG_ > offset / 2 ? stats->sumG_ - offset / 2 : minValid;\n-\tconst uint64_t sumB = stats->sumB_ > offset / 4 ? stats->sumB_ - offset / 4 : minValid;\n+\tconst RGB<uint64_t> sum = (stats->rgbSum() - offset).max(minValid);\n \n \t/*\n \t * Calculate red and blue gains for AWB.\n@@ -83,9 +81,9 @@ void Awb::process(IPAContext &context,\n \t */\n \tauto &gains = context.activeState.awb.gains;\n \tgains = { {\n-\t\tsumR <= sumG / 4 ? 4.0f : static_cast<float>(sumG) / sumR,\n+\t\tsum.r() <= sum.g() / 4 ? 4.0f : static_cast<float>(sum.g()) / sum.r(),\n \t\t1.0,\n-\t\tsumB <= sumG / 4 ? 4.0f : static_cast<float>(sumG) / sumB,\n+\t\tsum.b() <= sum.g() / 4 ? 4.0f : static_cast<float>(sum.g()) / sum.b(),\n \t} };\n \n \tRGB<double> rgbGains{ { 1 / gains.r(), 1 / gains.g(), 1 / gains.b() } };\n","prefixes":[]}