From patchwork Wed Nov 20 08:57:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Klug X-Patchwork-Id: 22025 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 B1D6AC330B for ; Wed, 20 Nov 2024 08:58:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2212165F49; Wed, 20 Nov 2024 09:58:09 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ugdSNCjD"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id CD93A65F3E for ; Wed, 20 Nov 2024 09:58:06 +0100 (CET) Received: from ideasonboard.com (unknown [IPv6:2a00:6020:448c:6c00:bbd:82cc:f3f3:e12e]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id D31F2928; Wed, 20 Nov 2024 09:57:48 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1732093068; bh=ZvuOzT6tTsES0CuCVFv8GhwTaFIPAZiE0r28BpY439I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ugdSNCjDg06SVNuSJt8ddyRH4qAahxC7tZSLBpw28/cI2Sp3kxpmbJAKEMAEZcbWR wPWbD8mu14Mi1ljyepSX+C+P+ex17BtTPsVC/UHDUloPqAZq5gYpIqvawfEHxJvWQ1 gpmranSEETDb4EXg6W4FVebdJnlojNovaeMTdQ3U= From: Stefan Klug To: libcamera-devel@lists.libcamera.org Cc: Stefan Klug Subject: [PATCH 3/7] libcamera: geometry: Add Rectangle::mappedBetween() Date: Wed, 20 Nov 2024 09:57:42 +0100 Message-ID: <20241120085753.1993436-4-stefan.klug@ideasonboard.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241120085753.1993436-1-stefan.klug@ideasonboard.com> References: <20241120085753.1993436-1-stefan.klug@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" Handling cropping and scaling within a complicated pipeline involves transformations of rectangles between different coordinate systems. For example the full input of the dewarper (0,0)/1920x1080 might correspond to the rectangle (0, 243)/2592x1458 in sensor coordinates (of a 2592x1944 sensor). Add a function that allows the transformation of a rectangle defined in one reference frame (dewarper) into the coordinates of a second reference frame (sensor). Signed-off-by: Stefan Klug --- include/libcamera/geometry.h | 2 ++ src/libcamera/geometry.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 9ca5865a3d0d..8c5f34f4dec1 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -299,6 +299,8 @@ public: __nodiscard Rectangle scaledBy(const Size &numerator, const Size &denominator) const; __nodiscard Rectangle translatedBy(const Point &point) const; + + Rectangle mappedBetween(const Rectangle &source, const Rectangle &target) const; }; bool operator==(const Rectangle &lhs, const Rectangle &rhs); diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp index 90ccf8c19f97..7e29fe35801e 100644 --- a/src/libcamera/geometry.cpp +++ b/src/libcamera/geometry.cpp @@ -837,6 +837,39 @@ Rectangle Rectangle::translatedBy(const Point &point) const return { x + point.x, y + point.y, width, height }; } +/** + * \brief Maps a Rectangle from one reference frame to another + * \param[in] source The source reference frame + * \param[in] destination The destination reference frame + * + * The params source and destinations specify two reference frames for the same + * data. The rectangle is translated from the source reference frame into the + * destination reference frame. + * + * For example, consider a sensor with a resolution of 2592x1458 pixels and a + * assume a rectangle of (0, 243)/2592x1458 (sensorFrame) in sensor coordinates + * is mapped to a rectangle (0,0)/(1920,1080) (displayFrame) in display + * coordinates. This function can be used to transform an arbitrary rectangle + * from display coordinates to sensor coordinates or vice versa: + * + * \code{.cpp} + * displayRect = sensorRect.mappedBetween(sensorFrame, displayFrame); + * \endcode + */ +Rectangle Rectangle::mappedBetween(const Rectangle &source, + const Rectangle &destination) const +{ + Rectangle r; + double sx = static_cast(destination.width) / source.width; + double sy = static_cast(destination.height) / source.height; + + r.x = static_cast((x - source.x) * sx) + destination.x; + r.y = static_cast((y - source.y) * sy) + destination.y; + r.width = static_cast(width * sx); + r.height = static_cast(height * sy); + return r; +} + /** * \brief Compare rectangles for equality * \return True if the two rectangles are equal, false otherwise