{"id":9825,"url":"https://patchwork.libcamera.org/api/1.1/patches/9825/?format=json","web_url":"https://patchwork.libcamera.org/patch/9825/","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":"<20200925085127.17214-5-david.plowman@raspberrypi.com>","date":"2020-09-25T08:51:25","name":"[libcamera-devel,v2,4/6] libcamera: Add geometry helper functions","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"b5d62aeb55f5eb3d3d2c5e19596877e8f3050fa7","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/1.1/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9825/mbox/","series":[{"id":1326,"url":"https://patchwork.libcamera.org/api/1.1/series/1326/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1326","date":"2020-09-25T08:51:21","name":"Digital zoom","version":2,"mbox":"https://patchwork.libcamera.org/series/1326/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9825/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9825/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 55298C3B5C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 25 Sep 2020 08:51:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0713362FD8;\n\tFri, 25 Sep 2020 10:51:38 +0200 (CEST)","from mail-wm1-x334.google.com (mail-wm1-x334.google.com\n\t[IPv6:2a00:1450:4864:20::334])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id C985862FD8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 25 Sep 2020 10:51:35 +0200 (CEST)","by mail-wm1-x334.google.com with SMTP id e11so1394270wme.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 25 Sep 2020 01:51:35 -0700 (PDT)","from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72])\n\tby smtp.gmail.com with ESMTPSA id\n\tn4sm2001383wrp.61.2020.09.25.01.51.34\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 25 Sep 2020 01:51:34 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"pR7lKQXk\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=Y6wMl7PkxTQWHlM083BAuuViyy3KDW3mwMnkJU5FjHI=;\n\tb=pR7lKQXkmrpnjt5hG8XmndvMwo6j4jKz6DfI9JQwEcQGYF3seTB+0odR8r0wxp2VhQ\n\twMKk3/1wbH3ly8i6ZLRC6FwGmDK6SQuKlAU7hF+c466C4ds8RjOVHzRfS4u63l/ilohf\n\thyXiCdLmZZjARuGSns/QvNVFC9SFIbiBngZO/1PZjWpSX/hWTkr4WTuRSZv/8fARIGRL\n\ttAsdjpoTBlhviZixMN79aGEB6d1zEH0JTkwGbmqzeWSGaNcyaLiLeFPIk+YVck9MaDQp\n\tp4C76uzjESp9IpAmdd1Li1a1zUEgfGT9BylsqSPSPP7Vg9d77EvTqKHyVaMDXUo4tejt\n\tc1NA==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=Y6wMl7PkxTQWHlM083BAuuViyy3KDW3mwMnkJU5FjHI=;\n\tb=Bhxy87jPKalnr5p15SSYLHVsGrYLMLgeeuC4wlPP8LLtlyYO6fXd1q5HMRBoMoxQbd\n\t99SNwLkOf/1eLc5mtKVYpaw5XakpZcr9bPU6nf9ayZizFxZHYw8eVWB6l1Y9kk/RhzWc\n\trWmlHbfewVKrdVbxQEokutTyB+nBZno+9DyTRSedFb+juPsg+uX0x4sk5MZvrj/qwFZ2\n\tMjb2zKqqJlMFrgX9/aPI17ZzShb1HkgjzqohM/EfybXv2mYwhnYaTg4AUtrTAp9H68lM\n\tDpbFhvqQVYsrHCtNvkP4i/Iu/2xc3wO7RXfJcC5q30LlKeECubDf2hH+gcFHoFxBc9Tb\n\tzjvg==","X-Gm-Message-State":"AOAM5336q2WE7H97vCpJwdy0d5wUOt8L27evTSyVZ4+dMtwJ/tE5TeiP\n\tlNC5irAyn75G4P0KxjVT64A+vq3eGfI6Tw==","X-Google-Smtp-Source":"ABdhPJxTu+I8IJazWdKRopqd9K61Vb3wJdMyIURa9xGgWPULq/6UWkRvgo2kdyV6x6Vaoe1zYuBAyA==","X-Received":"by 2002:a1c:8115:: with SMTP id\n\tc21mr1962124wmd.153.1601023895198; \n\tFri, 25 Sep 2020 01:51:35 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 25 Sep 2020 09:51:25 +0100","Message-Id":"<20200925085127.17214-5-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20200925085127.17214-1-david.plowman@raspberrypi.com>","References":"<20200925085127.17214-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v2 4/6] libcamera: Add geometry helper\n\tfunctions","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"These functions are aimed at making it easier to calculate cropping\nrectangles, particularly in order to implement digital zoom.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n include/libcamera/geometry.h |  20 ++++++\n src/libcamera/geometry.cpp   | 129 +++++++++++++++++++++++++++++++++++\n 2 files changed, 149 insertions(+)","diff":"diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\nindex 02fb63c..0447ee3 100644\n--- a/include/libcamera/geometry.h\n+++ b/include/libcamera/geometry.h\n@@ -13,6 +13,8 @@\n \n namespace libcamera {\n \n+class Rectangle;\n+\n class Size\n {\n public:\n@@ -93,8 +95,17 @@ public:\n \t\t\tstd::max(height, expand.height)\n \t\t};\n \t}\n+\n+\tSize alignedDownToAspectRatio(const Size &ratio) const;\n+\tSize alignedUpToAspectRatio(const Size &ratio) const;\n+\n+\tRectangle centredTo(const Rectangle &region,\n+\t\t\t    int offsetX = 0, int offsetY = 0) const;\n };\n \n+Size operator*(const Size &size, float f);\n+Size operator/(const Size &size, float f);\n+\n bool operator==(const Size &lhs, const Size &rhs);\n bool operator<(const Size &lhs, const Size &rhs);\n \n@@ -176,6 +187,11 @@ public:\n \t{\n \t}\n \n+\tconstexpr explicit Rectangle(const Size &size)\n+\t\t: x(0), y(0), width(size.width), height(size.height)\n+\t{\n+\t}\n+\n \tint x;\n \tint y;\n \tunsigned int width;\n@@ -183,6 +199,10 @@ public:\n \n \tbool isNull() const { return !width && !height; }\n \tconst std::string toString() const;\n+\n+\tSize size() const;\n+\n+\tRectangle boundedTo(const Rectangle &boundary) 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 b12e1a6..649ee17 100644\n--- a/src/libcamera/geometry.cpp\n+++ b/src/libcamera/geometry.cpp\n@@ -143,6 +143,88 @@ const std::string Size::toString() const\n  * height of this size and the \\a expand size\n  */\n \n+/**\n+ * \\brief Align down to the aspect ratio given by \\a ratio\n+ * \\param[in] ratio The size whose aspect ratio to align down to\n+ * \\return A Size whose width and height are equal to the width and height\n+ * of this Size aligned down to the aspect ratio of \\a ratio\n+ */\n+Size Size::alignedDownToAspectRatio(const Size &ratio) const\n+{\n+\tuint64_t ratio1 = static_cast<uint64_t>(width) *\n+\t\t\t  static_cast<uint64_t>(ratio.height);\n+\tuint64_t ratio2 = static_cast<uint64_t>(ratio.width) *\n+\t\t\t  static_cast<uint64_t>(height);\n+\n+\tif (ratio1 > ratio2)\n+\t\treturn { static_cast<unsigned int>(ratio2 / ratio.height), height };\n+\telse\n+\t\treturn { width, static_cast<unsigned int>(ratio1 / ratio.width) };\n+}\n+\n+/**\n+ * \\brief Align up to the aspect ratio given by \\a ratio\n+ * \\param[in] ratio The size whose aspect ratio to align up to\n+ * \\return A Size whose width and height are equal to the width and height\n+ * of this Size aligned up to the aspect ratio of \\a ratio\n+ */\n+Size Size::alignedUpToAspectRatio(const Size &ratio) const\n+{\n+\tuint64_t ratio1 = static_cast<uint64_t>(width) *\n+\t\t\t  static_cast<uint64_t>(ratio.height);\n+\tuint64_t ratio2 = static_cast<uint64_t>(ratio.width) *\n+\t\t\t  static_cast<uint64_t>(height);\n+\n+\tif (ratio1 < ratio2)\n+\t\treturn { static_cast<unsigned int>(ratio2 / ratio.height), height };\n+\telse\n+\t\treturn { width, static_cast<unsigned int>(ratio1 / ratio.width) };\n+}\n+\n+/**\n+ * \\brief Centre a rectangle of this size within another rectangular region,\n+ * with optional offsets\n+ * \\param[in] region The rectangular region relative to which the returned\n+ * rectangle can be position\n+ * \\param[in] offsetX The X offset of the mid-point of the returned rectangle\n+ * relative to the mid-point of the region\n+ * \\param[in] offsetY The Y offset of the mid-point of the returned rectangle\n+ * relative to the mid-point of the region\n+ *\n+ * A Rectangle of this object's size is positioned within the Rectangle\n+ * given by \\a region. It is positioned so that its mid-point coincides\n+ * with the mid-point of \\a region, and is then further offset by the\n+ * values \\a offsetX and \\a offsetY.\n+ *\n+ * \\return A Rectangle with the horizontal and vertical sizes of\n+ * this Size instance, centred with offsets within a region\n+ */\n+Rectangle Size::centredTo(const Rectangle &region, int offsetX, int offsetY) const\n+{\n+\tint x = (region.width - width) / 2 + region.x + offsetX;\n+\tint y = (region.height - height) / 2 + region.y + offsetY;\n+\n+\treturn Rectangle(x, y, width, height);\n+}\n+\n+/**\n+ * \\brief Scale size up by the given factor\n+ * \\return The scaled Size\n+ */\n+Size operator*(const Size &size, float f)\n+{\n+\treturn Size(size.width * f, size.height * f);\n+}\n+\n+/**\n+ * \\brief Scale size down by the given factor\n+ * \\return The scaled Size\n+ */\n+Size operator/(const Size &size, float f)\n+{\n+\treturn Size(size.width / f, size.height / f);\n+}\n+\n /**\n  * \\brief Compare sizes for equality\n  * \\return True if the two sizes are equal, false otherwise\n@@ -365,6 +447,12 @@ bool operator==(const SizeRange &lhs, const SizeRange &rhs)\n  * \\param[in] height The height\n  */\n \n+/**\n+ * \\fn Rectangle::Rectangle(const Size &size)\n+ * \\brief Construct a Rectangle with zero offsets and the given \\a size\n+ * \\param[in] size The desired Rectangle size\n+ */\n+\n /**\n  * \\var Rectangle::x\n  * \\brief The horizontal coordinate of the rectangle's top-left corner\n@@ -404,6 +492,47 @@ const std::string Rectangle::toString() const\n \treturn ss.str();\n }\n \n+/**\n+ * \\brief Retrieve the size of this rectangle\n+ * \\return A Size reporting the Rectangle horizontal and vertical sizes\n+ */\n+Size Rectangle::size() const\n+{\n+\treturn Size(width, height);\n+}\n+\n+/**\n+ * \\brief Bound a Rectangle so as not to exceeed another Rectangle\n+ * \\param[in] boundary The limit that the returned Rectangle will not exceed\n+ *\n+ * The Rectangle is bounded so that it does not exceeed the given \\a boundary.\n+ * This process involves translating the Rectangle if any of its edges\n+ * lie beyond \\a boundary, so that those edges then lie along the boundary\n+ * instead.\n+ *\n+ * If either width or height are larger than \\a bounary, then the returned\n+ * Rectangle is clipped to be no larger. But other than this, the\n+ * Rectangle is not clipped or reduced in size, merely translated.\n+ *\n+ * We note that this is not a conventional Rectangle intersection function.\n+ *\n+ * \\return A Rectangle that does not extend beyond a boundary Rectangle\n+ */\n+Rectangle Rectangle::boundedTo(const Rectangle &boundary) const\n+{\n+\tRectangle result(*this);\n+\n+\tresult.width = std::min(result.width, boundary.width);\n+\tresult.x = std::clamp<int>(result.x, boundary.x,\n+\t\t\t\t   boundary.x + boundary.width - result.width);\n+\n+\tresult.height = std::min(result.height, boundary.height);\n+\tresult.y = std::clamp<int>(result.y, boundary.y,\n+\t\t\t\t   boundary.y + boundary.height - result.height);\n+\n+\treturn result;\n+}\n+\n /**\n  * \\brief Compare rectangles for equality\n  * \\return True if the two rectangles are equal, false otherwise\n","prefixes":["libcamera-devel","v2","4/6"]}