From patchwork Thu Nov 7 11:48:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Scally X-Patchwork-Id: 21848 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 71098C323E for ; Thu, 7 Nov 2024 11:48:32 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CBA3B6547F; Thu, 7 Nov 2024 12:48:31 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="sTMxNPfj"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 847DF653F4 for ; Thu, 7 Nov 2024 12:48:27 +0100 (CET) Received: from mail.ideasonboard.com (cpc141996-chfd3-2-0-cust928.12-3.cable.virginm.net [86.13.91.161]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id C691499F; Thu, 7 Nov 2024 12:48:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1730980098; bh=1/zdVU+wNFShOtkldi4YY/AGhOETMeIlUIgRx67CmL8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sTMxNPfjKrwlyKT19R+8vd7saPPC4EueKpqjkwGinNsHJXT10yfqJsm3aWJyWPA6p LWzZxJADe+ePYWiuTK53oVil6atvfeW6W75kDJ6DATPhrJ8ZoleD6/wawSjS3xgHaw NMnIpYkCk4JUcEYr6INi8lecmGfq3WABcDAdGJ/U= From: Daniel Scally To: libcamera-devel@lists.libcamera.org Cc: Anthony.McGivern@arm.com, Daniel Scally Subject: [PATCH v3 01/11] ipa: helpers: Add Unsigned Q(m, n) helpers Date: Thu, 7 Nov 2024 11:48:09 +0000 Message-Id: <20241107114819.57599-2-dan.scally@ideasonboard.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241107114819.57599-1-dan.scally@ideasonboard.com> References: <20241107114819.57599-1-dan.scally@ideasonboard.com> MIME-Version: 1.0 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" Add functions to convert a double to or from an unsigned Q(m,n) format. As the format is unsigned this most resembles the UQ variant of the TI implementation detailed on Wikipedia: https://en.wikipedia.org/wiki/Q_(number_format) Signed-off-by: Daniel Scally --- Changes in v3: - New patch src/ipa/libipa/helpers.cpp | 29 +++++++++++++++++++++++++++++ src/ipa/libipa/helpers.h | 2 ++ 2 files changed, 31 insertions(+) diff --git a/src/ipa/libipa/helpers.cpp b/src/ipa/libipa/helpers.cpp index 6c038895..908c6e93 100644 --- a/src/ipa/libipa/helpers.cpp +++ b/src/ipa/libipa/helpers.cpp @@ -72,6 +72,35 @@ uint32_t estimateCCT(double red, double green, double blue) return 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33; } +/** + * \brief Convert double to Q(m.n) format + * \param[in] value The value to convert + * \param[in] m The number of bits used to represent the integer part + * \param[in] n The number of bits used to represent the fraction part + * + * This function converts a double into an unsigned fractional Q format, + * clamping at the largest possible value given m and n. As these formats are + * unsigned the m figure does not include a sign bit. + */ +unsigned int toUnsignedQFormat(double value, unsigned int m, unsigned int n) +{ + double maxVal = (std::pow(2, m + n) - 1) / std::pow(2, n); + + return std::clamp(value, 0.0, maxVal) * std::pow(2, n); +} + +/** + * \brief Convert Q(m.n) formatted number to double + * \param[in] value The value to convert + * \param[in] n The number of bits used to represent the fraction part + * + * This function converts an unsigned Q formatted value into a double. + */ +double fromUnsignedQFormat(unsigned int value, unsigned int n) +{ + return value / std::pow(2, n); +} + } /* namespace ipa */ } /* namespace libcamera */ diff --git a/src/ipa/libipa/helpers.h b/src/ipa/libipa/helpers.h index 51c74a36..1f26e768 100644 --- a/src/ipa/libipa/helpers.h +++ b/src/ipa/libipa/helpers.h @@ -15,6 +15,8 @@ namespace ipa { double rec601LuminanceFromRGB(unsigned int r, unsigned int g, unsigned int b); uint32_t estimateCCT(double red, double green, double blue); +unsigned int toUnsignedQFormat(double value, unsigned int m, unsigned int n); +double fromUnsignedQFormat(unsigned int value, unsigned int n); } /* namespace ipa */