From patchwork Wed Jun 26 07:20:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 20409 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D101FBD87C for ; Wed, 26 Jun 2024 07:21:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 51458654C5; Wed, 26 Jun 2024 09:21:52 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="J4c1C0JO"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8D9B4654CC for ; Wed, 26 Jun 2024 09:21:44 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1719386503; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b+NEdK6aGpbhkihQwAGrm2tKWTaIlM7LK7uVIFk5v1E=; b=J4c1C0JOajC6hqj2M29I7aY6j/ghEz4KCQFhu3aDXIFSEvu517kgL1HZBadnMyMnEBV1wE L/15utdmKuG751C369sv9R5Gd0oKmrxuwO+IK2hLfXfsejkVIfCBoEp2/AuvdHRI+8p1Oq a4hwphhtDxa0PIkkDA7Agao0M5x529I= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-428-_lY57jnEMNu6ERzkSqH24Q-1; Wed, 26 Jun 2024 03:21:42 -0400 X-MC-Unique: _lY57jnEMNu6ERzkSqH24Q-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8113A1956060 for ; Wed, 26 Jun 2024 07:21:41 +0000 (UTC) Received: from nuthatch.brq.redhat.com (unknown [10.43.17.159]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 89D953000601; Wed, 26 Jun 2024 07:21:40 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal Subject: [PATCH 16/19] libcamera: software_isp: Use floating point for color parameters Date: Wed, 26 Jun 2024 09:20:57 +0200 Message-ID: <20240626072100.55497-17-mzamazal@redhat.com> In-Reply-To: <20240626072100.55497-1-mzamazal@redhat.com> References: <20240626072100.55497-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" It's more natural to represent color gains and black level as floating point numbers rather than using a particular pixel-related representation. double is used rather than float because it's a more common floating point type in libcamera algorithms. Otherwise there is no obvious reason to select one over the other here. The constructed color tables still use integer representation for efficiency. Signed-off-by: Milan Zamazal --- src/ipa/simple/algorithms/blc.cpp | 8 ++++---- src/ipa/simple/algorithms/colors.cpp | 26 ++++++++++++++------------ src/ipa/simple/ipa_context.h | 8 ++++---- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/ipa/simple/algorithms/blc.cpp b/src/ipa/simple/algorithms/blc.cpp index e216138d..82c89dbb 100644 --- a/src/ipa/simple/algorithms/blc.cpp +++ b/src/ipa/simple/algorithms/blc.cpp @@ -26,7 +26,7 @@ int BlackLevel::init(IPAContext &context, { context.configuration.black.set = false; context.configuration.black.changed = true; - context.configuration.black.level = 255; + context.configuration.black.level = 1.0; return 0; } @@ -51,16 +51,16 @@ void BlackLevel::process(IPAContext &context, const unsigned int total = std::accumulate(begin(histogram), end(histogram), 0); const unsigned int pixelThreshold = ignoredPercentage_ * total; - const unsigned int histogramRatio = 256 / SwIspStats::kYHistogramSize; const unsigned int currentBlackIdx = - context.configuration.black.level / histogramRatio; + context.configuration.black.level * SwIspStats::kYHistogramSize; for (unsigned int i = 0, seen = 0; i < currentBlackIdx && i < SwIspStats::kYHistogramSize; i++) { seen += histogram[i]; if (seen >= pixelThreshold) { - context.configuration.black.level = i * histogramRatio; + context.configuration.black.level = + static_cast(i) / SwIspStats::kYHistogramSize; context.configuration.black.changed = true; LOG(IPASoftBL, Debug) << "Auto-set black level: " diff --git a/src/ipa/simple/algorithms/colors.cpp b/src/ipa/simple/algorithms/colors.cpp index d4ab409c..726835d9 100644 --- a/src/ipa/simple/algorithms/colors.cpp +++ b/src/ipa/simple/algorithms/colors.cpp @@ -36,7 +36,7 @@ int Colors::init(IPAContext &context, updateGammaTable(context); auto &gains = context.activeState.gains; - gains.red = gains.green = gains.blue = 256; + gains.red = gains.green = gains.blue = 1.0; return 0; } @@ -45,7 +45,7 @@ void Colors::updateGammaTable(IPAContext &context) { auto &gammaTable = context.activeState.gammaTable; const unsigned int blackIndex = - context.configuration.black.level * IPAActiveState::kGammaLookupSize / 256; + context.configuration.black.level * IPAActiveState::kGammaLookupSize; std::fill(gammaTable.begin(), gammaTable.begin() + blackIndex, 0); const float divisor = kGammaLookupSize - blackIndex - 1.0; for (unsigned int i = blackIndex; i < kGammaLookupSize; i++) @@ -65,15 +65,18 @@ void Colors::prepare(IPAContext &context, auto &gains = context.activeState.gains; auto &gammaTable = context.activeState.gammaTable; for (unsigned int i = 0; i < DebayerParams::kRGBLookupSize; i++) { - constexpr unsigned int div = - static_cast(DebayerParams::kRGBLookupSize) * 256 / kGammaLookupSize; + constexpr double div = + static_cast(DebayerParams::kRGBLookupSize) / kGammaLookupSize; /* Apply gamma after gain! */ unsigned int idx; - idx = std::min({ i * gains.red / div, kGammaLookupSize - 1 }); + idx = std::min({ static_cast(i * gains.red / div), + kGammaLookupSize - 1 }); params->red[i] = gammaTable[idx]; - idx = std::min({ i * gains.green / div, kGammaLookupSize - 1 }); + idx = std::min({ static_cast(i * gains.green / div), + kGammaLookupSize - 1 }); params->green[i] = gammaTable[idx]; - idx = std::min({ i * gains.blue / div, kGammaLookupSize - 1 }); + idx = std::min({ static_cast(i * gains.blue / div), + kGammaLookupSize - 1 }); params->blue[i] = gammaTable[idx]; } } @@ -85,7 +88,7 @@ void Colors::process(IPAContext &context, [[maybe_unused]] ControlList &metadata) { const SwIspStats::Histogram &histogram = stats->yHistogram; - const uint8_t blackLevel = context.configuration.black.level; + const double blackLevel = context.configuration.black.level; /* * Black level must be subtracted to get the correct AWB ratios, they @@ -102,12 +105,11 @@ void Colors::process(IPAContext &context, /* * Calculate red and blue gains for AWB. * Clamp max gain at 4.0, this also avoids 0 division. - * Gain: 128 = 0.5, 256 = 1.0, 512 = 2.0, etc. */ auto &gains = context.activeState.gains; - gains.red = sumR <= sumG / 4 ? 1024 : 256 * sumG / sumR; - gains.blue = sumB <= sumG / 4 ? 1024 : 256 * sumG / sumB; - /* Green gain is fixed to 256 */ + gains.red = sumR <= sumG / 4 ? 4.0 : static_cast(sumG) / sumR; + gains.blue = sumB <= sumG / 4 ? 4.0 : static_cast(sumG) / sumB; + /* Green gain is fixed to 1.0 */ LOG(IPASoftColors, Debug) << "gain R/B " << gains.red << "/" << gains.blue; } diff --git a/src/ipa/simple/ipa_context.h b/src/ipa/simple/ipa_context.h index 991a45b1..3e0d9b51 100644 --- a/src/ipa/simple/ipa_context.h +++ b/src/ipa/simple/ipa_context.h @@ -20,7 +20,7 @@ namespace ipa::soft { struct IPASessionConfiguration { float gamma; struct { - uint8_t level; + double level; bool set; bool changed; } black; @@ -28,9 +28,9 @@ struct IPASessionConfiguration { struct IPAActiveState { struct { - unsigned int red; - unsigned int green; - unsigned int blue; + double red; + double green; + double blue; } gains; static constexpr unsigned int kGammaLookupSize = 1024; std::array gammaTable;