{"id":22195,"url":"https://patchwork.libcamera.org/api/1.1/patches/22195/?format=json","web_url":"https://patchwork.libcamera.org/patch/22195/","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":"<20241206101344.767170-4-stefan.klug@ideasonboard.com>","date":"2024-12-06T10:13:25","name":"[v3,03/17] libcamera: geometry: Add Rectangle::transformedBetween()","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"04a7c1234fba91dc8e2b5a8cdfc0bb9d94b49f4c","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/1.1/people/184/?format=json","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/22195/mbox/","series":[{"id":4854,"url":"https://patchwork.libcamera.org/api/1.1/series/4854/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4854","date":"2024-12-06T10:13:22","name":"rkisp1: Fix aspect ratio and ScalerCrop","version":3,"mbox":"https://patchwork.libcamera.org/series/4854/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/22195/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/22195/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 7CE02BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  6 Dec 2024 10:14:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 291DB66139;\n\tFri,  6 Dec 2024 11:14:00 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0B8C166135\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Dec 2024 11:13:57 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:3543:aebe:e043:ef86])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id AB52174C;\n\tFri,  6 Dec 2024 11:13:27 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"YMWTmiWA\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1733480007;\n\tbh=LnhbRJbhNTkk7xb9hRaSCNoSo/GtePhhHMNJe9ULUfM=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=YMWTmiWApO5HxpzofpDKynWGDCCd9yA2/Xg5x8jVQQeWOrlB49aZugbvqgURpXVaZ\n\tvvmN+hBl48bL0k4wE9a7zHwec6zdPv2IV5nh+mBw4037TizHEQCqinaJClV4WQ9e1O\n\tgTtjoUTZ2DPlpky7XdRVjjuOTV9AW09Cyqsojml0=","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>,\n\tPaul Elder <paul.elder@ideasonboard.com>,\n\tJacopo Mondi <jacopo.mondi@ideasonboard.com>","Subject":"[PATCH v3 03/17] libcamera: geometry: Add\n\tRectangle::transformedBetween()","Date":"Fri,  6 Dec 2024 11:13:25 +0100","Message-ID":"<20241206101344.767170-4-stefan.klug@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20241206101344.767170-1-stefan.klug@ideasonboard.com>","References":"<20241206101344.767170-1-stefan.klug@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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":"Handling cropping and scaling within a complicated pipeline involves\ntransformations of rectangles between different coordinate systems. For\nexample the full input of the dewarper (0,0)/1920x1080 might correspond\nto the rectangle (0, 243)/2592x1458 in sensor coordinates (of a\n2592x1944 sensor). Add a function that allows the transformation of a\nrectangle defined in one reference frame (dewarper) into the coordinates\nof a second reference frame (sensor).\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\nReviewed-by: Paul Elder <paul.elder@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n\n---\nChanges in v3:\n- Improved documentation\n- Added testcase\n\nChanges in v2:\n- Renamed mappedBetween() to transformedBetween()\n- Improved documentation\n- Collected tags\n---\n include/libcamera/geometry.h |  3 +++\n src/libcamera/geometry.cpp   | 49 ++++++++++++++++++++++++++++++++++++\n test/geometry.cpp            | 11 ++++++++\n 3 files changed, 63 insertions(+)","diff":"diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\nindex 9ca5865a3d0d..e5f0a843d314 100644\n--- a/include/libcamera/geometry.h\n+++ b/include/libcamera/geometry.h\n@@ -299,6 +299,9 @@ public:\n \t__nodiscard Rectangle scaledBy(const Size &numerator,\n \t\t\t\t       const Size &denominator) const;\n \t__nodiscard Rectangle translatedBy(const Point &point) const;\n+\n+\tRectangle transformedBetween(const Rectangle &source,\n+\t\t\t\t     const Rectangle &target) const;\n };\n \n bool operator==(const Rectangle &lhs, const Rectangle &rhs);\ndiff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\nindex 90ccf8c19f97..81cc8cd538a0 100644\n--- a/src/libcamera/geometry.cpp\n+++ b/src/libcamera/geometry.cpp\n@@ -837,6 +837,55 @@ Rectangle Rectangle::translatedBy(const Point &point) const\n \treturn { x + point.x, y + point.y, width, height };\n }\n \n+/**\n+ * \\brief Transform a Rectangle from one reference rectangle to another\n+ * \\param[in] source The \\a source reference rectangle\n+ * \\param[in] destination The \\a destination reference rectangle\n+ *\n+ * The \\a source and \\a destination parameters describe two rectangles defined\n+ * in different reference systems. The Rectangle is translated from the source\n+ * reference system into the destination reference system.\n+ *\n+ * The typical use case for this function is to translate a selection rectangle\n+ * specified in a reference system, in example the sensor's pixel array, into\n+ * the same rectangle re-scaled and translated into a different reference\n+ * system, in example the output frame on which the selection rectangle is\n+ * applied to.\n+ *\n+ * For example, consider a sensor with a resolution of 4040x2360 pixels and a\n+ * assume a rectangle of (100, 100)/3840x2160 (sensorFrame) in sensor\n+ * coordinates is mapped to a rectangle (0,0)/(1920,1080) (displayFrame) in\n+ * display coordinates. This function can be used to transform an arbitrary\n+ * rectangle from display coordinates to sensor coordinates or vice versa:\n+ *\n+ * \\code{.cpp}\n+ * Rectangle sensorReference(100, 100, 3840, 2160);\n+ * Rectangle displayReference(0, 0, 1920, 1080);\n+ *\n+ * // Bottom right quarter in sensor coordinates\n+ * Rectangle sensorRect(2020, 100, 1920, 1080);\n+ * displayRect = sensorRect.transformedBetween(sensorReference, displayReference);\n+ * // displayRect is now (960, 540)/960x540\n+ *\n+ * // Transformation back to sensor coordinates\n+ * sensorRect = displayRect.transformedBetween(displayReference, sensorReference);\n+ * \\endcode\n+ */\n+Rectangle Rectangle::transformedBetween(const Rectangle &source,\n+\t\t\t\t\tconst Rectangle &destination) const\n+{\n+\tRectangle r;\n+\tdouble sx = static_cast<double>(destination.width) / source.width;\n+\tdouble sy = static_cast<double>(destination.height) / source.height;\n+\n+\tr.x = static_cast<int>((x - source.x) * sx) + destination.x;\n+\tr.y = static_cast<int>((y - source.y) * sy) + destination.y;\n+\tr.width = static_cast<int>(width * sx);\n+\tr.height = static_cast<int>(height * sy);\n+\n+\treturn r;\n+}\n+\n /**\n  * \\brief Compare rectangles for equality\n  * \\return True if the two rectangles are equal, false otherwise\ndiff --git a/test/geometry.cpp b/test/geometry.cpp\nindex 5760fa3c885a..11df043b733b 100644\n--- a/test/geometry.cpp\n+++ b/test/geometry.cpp\n@@ -495,6 +495,17 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\tRectangle f1 = Rectangle(100, 200, 3000, 2000);\n+\t\tRectangle f2 = Rectangle(200, 300, 1500, 1000);\n+\t\t/* Bottom right quarter of the corresponding frames. */\n+\t\tRectangle r1 = Rectangle(100 + 1500, 200 + 1000, 1500, 1000);\n+\t\tRectangle r2 = Rectangle(200 + 750, 300 + 500, 750, 500);\n+\t\tif (r1.transformedBetween(f1, f2) != r2 ||\n+\t\t    r2.transformedBetween(f2, f1) != r1) {\n+\t\t\tcout << \"Rectangle::transformedBetween() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\treturn TestPass;\n \t}\n };\n","prefixes":["v3","03/17"]}