{"id":14112,"url":"https://patchwork.libcamera.org/api/1.1/patches/14112/?format=json","web_url":"https://patchwork.libcamera.org/patch/14112/","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":"<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>","date":"2021-10-13T01:26:29","name":"[libcamera-devel,1/2] libcamera: geometry: Add Size members to grown or shrink by a margin","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"73890afd0ce7613a8e8ebd8166d1a14e576b5a21","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14112/mbox/","series":[{"id":2620,"url":"https://patchwork.libcamera.org/api/1.1/series/2620/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2620","date":"2021-10-13T01:26:28","name":"libcamera: Add new helpers to the Size class","version":1,"mbox":"https://patchwork.libcamera.org/series/2620/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14112/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14112/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 50032BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 13 Oct 2021 01:26:52 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 263D96012B;\n\tWed, 13 Oct 2021 03:26:51 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BC8E868F4D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Oct 2021 03:26:48 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 62D9D8F0;\n\tWed, 13 Oct 2021 03:26:48 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"AsUldwv1\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634088408;\n\tbh=lrNq7oXuiyVOnMpdmw1njm0ldBKxEgVVmlI+eRKsR+8=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=AsUldwv1iSXFmiHMfYaqHYbq+e0TVPo0E3Zg9CnOuz/ptoTCTRbvosE2z0vKAws0o\n\thyLfAavMo2qVO9Nttr3rZxjmusNG4dtcm6mRatJXQJmVYblFdWtPtvkgeDxXn+DS/P\n\tnU27BheOnCPkiJruWVvI3MqEjriTJ9O+/YnUzekE=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed, 13 Oct 2021 04:26:29 +0300","Message-Id":"<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20211013012630.18877-1-laurent.pinchart@ideasonboard.com>","References":"<20211013012630.18877-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size members\n\tto grown or shrink by a margin","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":"Add four new member functions to the Size class (two in-place and two\nconst) to grow and shrink a Size by given margins.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/geometry.h | 30 ++++++++++++++++++++++\n src/libcamera/geometry.cpp   | 42 +++++++++++++++++++++++++++++++\n test/geometry.cpp            | 48 ++++++++++++++++++++++++++++++++----\n 3 files changed, 115 insertions(+), 5 deletions(-)","diff":"diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\nindex fdd1b4678074..fa7ae7bce8db 100644\n--- a/include/libcamera/geometry.h\n+++ b/include/libcamera/geometry.h\n@@ -94,6 +94,20 @@ public:\n \t\treturn *this;\n \t}\n \n+\tSize &growBy(const Size &margins)\n+\t{\n+\t\twidth += margins.width;\n+\t\theight += margins.height;\n+\t\treturn *this;\n+\t}\n+\n+\tSize &shrinkBy(const Size &margins)\n+\t{\n+\t\twidth = width > margins.width ? width - margins.width : 0;\n+\t\theight = height > margins.height ? height - margins.height : 0;\n+\t\treturn *this;\n+\t}\n+\n \t__nodiscard constexpr Size alignedDownTo(unsigned int hAlignment,\n \t\t\t\t\t\t unsigned int vAlignment) const\n \t{\n@@ -128,6 +142,22 @@ public:\n \t\t};\n \t}\n \n+\t__nodiscard constexpr Size grownBy(const Size &margins) const\n+\t{\n+\t\treturn {\n+\t\t\twidth + margins.width,\n+\t\t\theight + margins.height\n+\t\t};\n+\t}\n+\n+\t__nodiscard constexpr Size shrunkBy(const Size &margins) const\n+\t{\n+\t\treturn {\n+\t\t\twidth > margins.width ? width - margins.width : 0,\n+\t\t\theight > margins.height ? height - margins.height : 0\n+\t\t};\n+\t}\n+\n \t__nodiscard Size boundedToAspectRatio(const Size &ratio) const;\n \t__nodiscard Size expandedToAspectRatio(const Size &ratio) const;\n \ndiff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\nindex b014180e9ba5..cb3c2de18d5e 100644\n--- a/src/libcamera/geometry.cpp\n+++ b/src/libcamera/geometry.cpp\n@@ -173,6 +173,28 @@ const std::string Size::toString() const\n  * \\return A reference to this object\n  */\n \n+/**\n+ * \\fn Size::growBy(const Size &margins)\n+ * \\brief Grow the size by \\a margins in place\n+ * \\param[in] margins The margins to add to the size\n+ *\n+ * This function adds the width and height of the \\a margin size to this size.\n+ *\n+ * \\return A reference to this object\n+ */\n+\n+/**\n+ * \\fn Size::shrinkBy(const Size &margins)\n+ * \\brief Shrink the size by \\a margins in place\n+ * \\param[in] margins The margins to subtract to the size\n+ *\n+ * This function subtracts the width and height of the \\a margin size from this\n+ * size. If the width or height of the size are smaller than those of \\a\n+ * margins, the result is clamped to 0.\n+ *\n+ * \\return A reference to this object\n+ */\n+\n /**\n  * \\fn Size::alignedDownTo(unsigned int hAlignment, unsigned int vAlignment)\n  * \\brief Align the size down horizontally and vertically\n@@ -209,6 +231,26 @@ const std::string Size::toString() const\n  * height of this size and the \\a expand size\n  */\n \n+/**\n+ * \\fn Size::grownBy(const Size &margins)\n+ * \\brief Grow the size by \\a margins\n+ * \\param[in] margins The margins to add to the size\n+ * \\return A Size whose width and height are the sum of the width and height of\n+ * this size and the \\a margins size\n+ */\n+\n+/**\n+ * \\fn Size::shrunkBy(const Size &margins)\n+ * \\brief Shrink the size by \\a margins\n+ * \\param[in] margins The margins to subtract to the size\n+ *\n+ * If the width or height of the size are smaller than those of \\a margins, the\n+ * resulting size has its width or height clamped to 0.\n+ *\n+ * \\return A Size whose width and height are the difference of the width and\n+ * height of this size and the \\a margins size, clamped to 0\n+ */\n+\n /**\n  * \\brief Bound the size down to match the aspect ratio given by \\a ratio\n  * \\param[in] ratio The size whose aspect ratio must be matched\ndiff --git a/test/geometry.cpp b/test/geometry.cpp\nindex d13c88bf2e40..5125692496b3 100644\n--- a/test/geometry.cpp\n+++ b/test/geometry.cpp\n@@ -104,7 +104,10 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\t/* Test alignDownTo(), alignUpTo(), boundTo() and expandTo() */\n+\t\t/*\n+\t\t * Test alignDownTo(), alignUpTo(), boundTo(), expandTo(),\n+\t\t * growBy() and shrinkBy()\n+\t\t */\n \t\tSize s(50, 50);\n \n \t\ts.alignDownTo(16, 16);\n@@ -131,14 +134,36 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\ts.alignDownTo(16, 16).alignUpTo(32, 32)\n-\t\t .boundTo({ 40, 80 }).expandTo({ 16, 80 });\n-\t\tif (s != Size(40, 80)) {\n+\t\ts.growBy({ 10, 20 });\n+\t\tif (s != Size(60, 70)) {\n+\t\t\tcout << \"Size::growBy() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\ts.shrinkBy({ 20, 10 });\n+\t\tif (s != Size(40, 60)) {\n+\t\t\tcout << \"Size::shrinkBy() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\ts.shrinkBy({ 100, 100 });\n+\t\tif (s != Size(0, 0)) {\n+\t\t\tcout << \"Size::shrinkBy() clamp test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\ts = Size(50,50).alignDownTo(16, 16).alignUpTo(32, 32)\n+\t\t  .boundTo({ 40, 80 }).expandTo({ 16, 80 })\n+\t\t  .growBy({ 4, 4 }).shrinkBy({ 10, 20 });\n+\t\tif (s != Size(34, 64)) {\n \t\t\tcout << \"Size chained in-place modifiers test failed\" << endl;\n \t\t\treturn TestFail;\n \t\t}\n \n-\t\t/* Test alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() */\n+\t\t/*\n+\t\t * Test alignedDownTo(), alignedUpTo(), boundedTo(),\n+\t\t * expandedTo(), grownBy() and shrunkBy()\n+\t\t */\n \t\tif (Size(0, 0).alignedDownTo(16, 8) != Size(0, 0) ||\n \t\t    Size(1, 1).alignedDownTo(16, 8) != Size(0, 0) ||\n \t\t    Size(16, 8).alignedDownTo(16, 8) != Size(16, 8)) {\n@@ -167,6 +192,19 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\tif (Size(0, 0).grownBy({ 10, 20 }) != Size(10, 20) ||\n+\t\t    Size(200, 50).grownBy({ 10, 20 }) != Size(210, 70)) {\n+\t\t\tcout << \"Size::grownBy() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tif (Size(200, 50).shrunkBy({ 10, 20 }) != Size(190, 30) ||\n+\t\t    Size(200, 50).shrunkBy({ 10, 100 }) != Size(190, 0) ||\n+\t\t    Size(200, 50).shrunkBy({ 300, 20 }) != Size(0, 30)) {\n+\t\t\tcout << \"Size::shrunkBy() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\t/* Aspect ratio tests */\n \t\tif (Size(0, 0).boundedToAspectRatio(Size(4, 3)) != Size(0, 0) ||\n \t\t    Size(1920, 1440).boundedToAspectRatio(Size(16, 9)) != Size(1920, 1080) ||\n","prefixes":["libcamera-devel","1/2"]}