{"id":9514,"url":"https://patchwork.libcamera.org/api/patches/9514/?format=json","web_url":"https://patchwork.libcamera.org/patch/9514/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20200907164450.13082-4-david.plowman@raspberrypi.com>","date":"2020-09-07T16:44:49","name":"[libcamera-devel,RFC,3/4] libcamera: Add geometry helper functions","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"21537dcbad5a24530deb3fe67bbc4cbb42bd6be8","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/9514/mbox/","series":[{"id":1270,"url":"https://patchwork.libcamera.org/api/series/1270/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1270","date":"2020-09-07T16:44:46","name":"Digital zoom","version":1,"mbox":"https://patchwork.libcamera.org/series/1270/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/9514/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/9514/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 C9606BDB1C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  7 Sep 2020 16:45:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 932B462BBA;\n\tMon,  7 Sep 2020 18:45:00 +0200 (CEST)","from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com\n\t[IPv6:2a00:1450:4864:20::42f])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4ACE062BBA\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon,  7 Sep 2020 18:44:58 +0200 (CEST)","by mail-wr1-x42f.google.com with SMTP id z4so16450223wrr.4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 07 Sep 2020 09:44:58 -0700 (PDT)","from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72])\n\tby smtp.gmail.com with ESMTPSA id\n\tv3sm27707033wmh.6.2020.09.07.09.44.57\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 07 Sep 2020 09:44:57 -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=\"K6SyI6rP\"; 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=O61rTSPSnTdYCOC7EfprGKw/KQFBenZCvM1enIUSkgs=;\n\tb=K6SyI6rPmy3qLPoD3QNZxE0lz4hZ9G+2KZEVYueSLA6p/j/C/a0HV7F2xnFNJyskMw\n\tSCNj+fEzbXW5vuMqEJE5Z96eSaK+uJ1aCsnaNGSB19oMCes3SxVixdOULgKUrhzjSwv2\n\t1PVMsMFSNnBbSjZX0tBYzMbzRJgqaW/xCpKGynX+SKBjDwwouwixT6muxe5XXRJmsy25\n\tbQBcqme9i6myDjSG8Uc5aByEAblYn/VMtvLCMRx9pRCPtMcrdproKrBmhou9X2XEAYzV\n\tj+yiqp1cBxjITqj8vXVaJESAf32ZM652ZSqqiO3HjZG9CqaAHBfXpU/DAs0/ZFPgy/wt\n\tAWiA==","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=O61rTSPSnTdYCOC7EfprGKw/KQFBenZCvM1enIUSkgs=;\n\tb=hVzskMEbDSwR8it006ZQesOxnrZ41pnSBAXghaM6JiWHWZpNfxB56Bgz4H5Pcf3Vbq\n\tHt+KOKIl5NInlDZ4H2uq4tlpGOna5VQFwd+G+2hOsO75FTt5nAY2Q6Z13otq/T70V34h\n\t1lsMI9ENJ22aIGus+kKjVXWVcAteYL4jrw7C/tlopZcNvOryo4O1gkuasZXgWzdFrU6D\n\t7G77fqx6n0+svXhO3Sp6Oyr69fCilIZAclzG07Cy4rwEp4//BhzBGiEILSAm0CaYYMUO\n\tB0WrAfieqoQJ1YTR5XOqythcdXaFwsMY+iMuS44VNysIzqHDSXLY9UeIFjQO4m2uU4rL\n\tlgWg==","X-Gm-Message-State":"AOAM532lxz/7Oc8QGARLsRWpmRqmK5Yl4fwci3a4WvTuP9MlM0p7rXbR\n\t+moI1aDJaj3VIk1hWUfUO5o7q1KP0mWOwg==","X-Google-Smtp-Source":"ABdhPJwlMXoZSrwC01EFwkbxTQB2snDH41eXBppWiulz3jpr8cqLB/1ag3cPIopn9JfHKAWSK5beYQ==","X-Received":"by 2002:adf:ec90:: with SMTP id\n\tz16mr21613682wrn.145.1599497097680; \n\tMon, 07 Sep 2020 09:44:57 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon,  7 Sep 2020 17:44:49 +0100","Message-Id":"<20200907164450.13082-4-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20200907164450.13082-1-david.plowman@raspberrypi.com>","References":"<20200907164450.13082-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [RFC PATCH 3/4] 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---\n include/libcamera/geometry.h |  18 +++++\n src/libcamera/geometry.cpp   | 129 +++++++++++++++++++++++++++++++++++\n 2 files changed, 147 insertions(+)","diff":"diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\nindex 02fb63c..8f6a9a0 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,16 @@ public:\n \t\t\tstd::max(height, expand.height)\n \t\t};\n \t}\n+\n+\tSize aspectRatioDown(const Size &ar) const;\n+\tSize aspectRatioUp(const Size &ar) const;\n+\tRectangle centre(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 +186,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 +198,9 @@ public:\n \n \tbool isNull() const { return !width && !height; }\n \tconst std::string toString() const;\n+\n+\tSize size() const;\n+\tRectangle clamp(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..3e26167 100644\n--- a/src/libcamera/geometry.cpp\n+++ b/src/libcamera/geometry.cpp\n@@ -143,6 +143,89 @@ const std::string Size::toString() const\n  * height of this size and the \\a expand size\n  */\n \n+/**\n+ * \\brief Clip the given size to have a given aspect ratio\n+ * \\param[in] ar The aspect ratio the result is to have\n+ */\n+Size Size::aspectRatioDown(const Size &ar) const\n+{\n+\tSize result;\n+\n+\tuint64_t ratio1 = static_cast<uint64_t>(width) *\n+\t\t\t  static_cast<uint64_t>(ar.height);\n+\tuint64_t ratio2 = static_cast<uint64_t>(ar.width) *\n+\t\t\t  static_cast<uint64_t>(height);\n+\n+\tif (ratio1 > ratio2)\n+\t\tresult = Size(ratio2 / ar.height, height);\n+\telse\n+\t\tresult = Size(width, ratio1 / ar.width);\n+\n+\treturn result;\n+}\n+\n+/**\n+ * \\brief Expand the given size to have a given aspect ratio\n+ * \\param[in] ar The aspect ratio the result is to have\n+ */\n+Size Size::aspectRatioUp(const Size &ar) const\n+{\n+\tSize result;\n+\n+\tuint64_t ratio1 = static_cast<uint64_t>(width) *\n+\t\t\t  static_cast<uint64_t>(ar.height);\n+\tuint64_t ratio2 = static_cast<uint64_t>(ar.width) *\n+\t\t\t  static_cast<uint64_t>(height);\n+\n+\tif (ratio1 < ratio2)\n+\t\tresult = Size(ratio2 / ar.height, height);\n+\telse\n+\t\tresult = Size(width, ratio1 / ar.width);\n+\n+\treturn result;\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 offset within a region\n+ */\n+Rectangle Size::centre(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+ */\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+ */\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 +448,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 the zero offsets and size\n+ * \\param[in] size The size\n+ */\n+\n /**\n  * \\var Rectangle::x\n  * \\brief The horizontal coordinate of the rectangle's top-left corner\n@@ -404,6 +493,46 @@ const std::string Rectangle::toString() const\n \treturn ss.str();\n }\n \n+/**\n+ * \\brief Return the size of this rectangle\n+ */\n+Size Rectangle::size() const\n+{\n+\treturn Size(width, height);\n+}\n+\n+/**\n+ * \\brief Clamp 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 clamped 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::clamp(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","RFC","3/4"]}