From patchwork Tue Nov 19 12:19:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 22012 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 D2316C32F1 for ; Tue, 19 Nov 2024 12:20:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 46BD565F3B; Tue, 19 Nov 2024 13:20:28 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="k/s2eCss"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EFB9265F38 for ; Tue, 19 Nov 2024 13:20:21 +0100 (CET) Received: from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi [81.175.209.231]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1DBB91A4A for ; Tue, 19 Nov 2024 13:20:04 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1732018804; bh=0iZYBaR+o5G70ZHH7Vi/NBicWWhhajdjTWCDkTGSmBI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=k/s2eCss3cIqK0mDBZGafHYCb2U1Wndw7mNYVxFBY40E0gbq37OssGQX3zVS2ofqN DUlEmUNNIwDh0Ufzi1b8JtXsvr5YNrAuaIGCbJ3YdC6YUmX60X7CQmkU6pixem5tNG FEMLkv8p9R09LvbF8CvXP1N8vmHxKohJLXSKwTJg= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Subject: [PATCH v4 14/17] ipa: libipa: colour: Use Vector and Matrix for linear algebra Date: Tue, 19 Nov 2024 14:19:25 +0200 Message-ID: <20241119121928.30939-15-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241119121928.30939-1-laurent.pinchart@ideasonboard.com> References: <20241119121928.30939-1-laurent.pinchart@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" Replace the manual vector and matrix calculations with usage of the Vector and Matrix classes. This simplifies the code and improves readability. Signed-off-by: Laurent Pinchart Reviewed-by: Milan Zamazal --- src/ipa/libipa/colours.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp index 6c597093ddf8..97124cf460d4 100644 --- a/src/ipa/libipa/colours.cpp +++ b/src/ipa/libipa/colours.cpp @@ -31,7 +31,11 @@ namespace ipa { */ double rec601LuminanceFromRGB(const RGB &rgb) { - return (rgb.r() * .299) + (rgb.g() * .587) + (rgb.b() * .114); + static const Vector rgb2y{{ + 0.299, 0.587, 0.114 + }}; + + return rgb.dot(rgb2y); } /** @@ -54,17 +58,21 @@ double rec601LuminanceFromRGB(const RGB &rgb) */ uint32_t estimateCCT(const RGB &rgb) { - /* Convert the RGB values to CIE tristimulus values (XYZ) */ - double X = (-0.14282) * rgb.r() + (1.54924) * rgb.g() + (-0.95641) * rgb.b(); - double Y = (-0.32466) * rgb.r() + (1.57837) * rgb.g() + (-0.73191) * rgb.b(); - double Z = (-0.68202) * rgb.r() + (0.77073) * rgb.g() + (0.56332) * rgb.b(); + /* + * Convert the RGB values to CIE tristimulus values (XYZ) and divide by + * the sum of X, Y and Z to calculate the CIE xy chromaticity. + */ + static const Matrix rgb2xyz({ + -0.14282, 1.54924, -0.95641, + -0.32466, 1.57837, -0.73191, + -0.68202, 0.77073, 0.56332 + }); - /* Calculate the normalized chromaticity values */ - double x = X / (X + Y + Z); - double y = Y / (X + Y + Z); + Vector xyz = rgb2xyz * rgb; + xyz /= xyz.sum(); /* Calculate CCT */ - double n = (x - 0.3320) / (0.1858 - y); + double n = (xyz.x() - 0.3320) / (0.1858 - xyz.y()); return 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33; }