{"id":8749,"url":"https://patchwork.libcamera.org/api/1.1/patches/8749/?format=json","web_url":"https://patchwork.libcamera.org/patch/8749/","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":"<20200712150537.22946-1-laurent.pinchart@ideasonboard.com>","date":"2020-07-12T15:05:37","name":"[libcamera-devel,v2] libcamera: geometry: Add helper functions to the Size class","commit_ref":"6c0afb8b33a1586b2e5bb5543edca5103769af0e","pull_url":null,"state":"accepted","archived":false,"hash":"91372a1809245e6e1e1689817ab3b3c8b1582d01","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/8749/mbox/","series":[{"id":1099,"url":"https://patchwork.libcamera.org/api/1.1/series/1099/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1099","date":"2020-07-12T15:05:37","name":"[libcamera-devel,v2] libcamera: geometry: Add helper functions to the Size class","version":2,"mbox":"https://patchwork.libcamera.org/series/1099/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8749/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8749/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 4C381BD790\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 12 Jul 2020 15:05:53 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DDA606055B;\n\tSun, 12 Jul 2020 17:05:52 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2614D6055B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 12 Jul 2020 17:05:51 +0200 (CEST)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A981E514\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 12 Jul 2020 17:05:50 +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=\"wQdwdXva\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1594566350;\n\tbh=4XTJECuyet9NaSWIgW3XbnsRPgIK0OLdmdghsPtN/OM=;\n\th=From:To:Subject:Date:From;\n\tb=wQdwdXvaDr6tuM9HOzT2UCOWdL56Hv9V/YyHGXCSk1yKDGoGmKW+q5E/3Uaj+VVNb\n\t91gStpVedkQH7rlv30y8oOUy0FW/y8b35CvKVJ0iJ2wNKifSr67o+/BPdJO6HuJzw5\n\t76vSG9OLXpO7tdlm1LRvmGCp5YNo6eIZ7gIPfUe8=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun, 12 Jul 2020 18:05:37 +0300","Message-Id":"<20200712150537.22946-1-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v2] libcamera: geometry: Add helper\n\tfunctions to the Size class","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":"Pipeline handlers commonly have to calculate the minimum or maximum of\nmultiple sizes, or align a size's width and height. Add helper functions\nto the Size class to perform those tasks.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\nChanges since v1:\n\n- Rename aligneTo() to alignedUpTo()\n- Add alignDownTo()\n---\n include/libcamera/geometry.h | 33 +++++++++++++++++++++++++++++++++\n src/libcamera/geometry.cpp   | 36 ++++++++++++++++++++++++++++++++++++\n test/geometry.cpp            | 29 +++++++++++++++++++++++++++++\n 3 files changed, 98 insertions(+)","diff":"diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\nindex 7d4b8bcfe3d8..d217cfd50c86 100644\n--- a/include/libcamera/geometry.h\n+++ b/include/libcamera/geometry.h\n@@ -8,6 +8,7 @@\n #ifndef __LIBCAMERA_GEOMETRY_H__\n #define __LIBCAMERA_GEOMETRY_H__\n \n+#include <algorithm>\n #include <string>\n \n namespace libcamera {\n@@ -43,6 +44,38 @@ struct Size {\n \n \tbool isNull() const { return !width && !height; }\n \tconst std::string toString() const;\n+\n+\tSize alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const\n+\t{\n+\t\treturn {\n+\t\t\twidth / hAlignment * hAlignment,\n+\t\t\theight / vAlignment * vAlignment\n+\t\t};\n+\t}\n+\n+\tSize alignedUpTo(unsigned int hAlignment, unsigned int vAlignment) const\n+\t{\n+\t\treturn {\n+\t\t\t(width + hAlignment - 1) / hAlignment * hAlignment,\n+\t\t\t(height + vAlignment - 1) / vAlignment * vAlignment\n+\t\t};\n+\t}\n+\n+\tSize boundedTo(const Size &bound) const\n+\t{\n+\t\treturn {\n+\t\t\tstd::min(width, bound.width),\n+\t\t\tstd::min(height, bound.height)\n+\t\t};\n+\t}\n+\n+\tSize expandedTo(const Size &expand) const\n+\t{\n+\t\treturn {\n+\t\t\tstd::max(width, expand.width),\n+\t\t\tstd::max(height, expand.height)\n+\t\t};\n+\t}\n };\n \n bool operator==(const Size &lhs, const Size &rhs);\ndiff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\nindex 24c44fb43acf..4594f9ff435f 100644\n--- a/src/libcamera/geometry.cpp\n+++ b/src/libcamera/geometry.cpp\n@@ -122,6 +122,42 @@ const std::string Size::toString() const\n \treturn std::to_string(width) + \"x\" + std::to_string(height);\n }\n \n+/**\n+ * \\fn Size::alignedDownTo(unsigned int hAlignment, unsigned int vAlignment)\n+ * \\brief Align the size down horizontally and vertically\n+ * \\param[in] hAlignment Horizontal alignment\n+ * \\param[in] vAlignment Vertical alignment\n+ * \\return A Size whose width and height are equal to the width and height of\n+ * this size rounded down to the nearest multiple of \\a hAlignment and\n+ * \\a vAlignment respectively\n+ */\n+\n+/**\n+ * \\fn Size::alignedUpTo(unsigned int hAlignment, unsigned int vAlignment)\n+ * \\brief Align the size up horizontally and vertically\n+ * \\param[in] hAlignment Horizontal alignment\n+ * \\param[in] vAlignment Vertical alignment\n+ * \\return A Size whose width and height are equal to the width and height of\n+ * this size rounded up to the nearest multiple of \\a hAlignment and\n+ * \\a vAlignment respectively\n+ */\n+\n+/**\n+ * \\fn Size::boundedTo(const Size &bound)\n+ * \\brief Bound the size to \\a bound\n+ * \\param[in] bound The maximum size\n+ * \\return A Size whose width and height are the minimum of the width and\n+ * height of this size and the \\a bound size\n+ */\n+\n+/**\n+ * \\fn Size::expandedTo(const Size &expand)\n+ * \\brief Expand the size to \\a expand\n+ * \\param[in] expand The minimum size\n+ * \\return A Size whose width and height are the maximum of the width and\n+ * height of this size and the \\a expand size\n+ */\n+\n /**\n  * \\brief Compare sizes for equality\n  * \\return True if the two sizes are equal, false otherwise\ndiff --git a/test/geometry.cpp b/test/geometry.cpp\nindex 904ad92c9448..fd0132c03b02 100644\n--- a/test/geometry.cpp\n+++ b/test/geometry.cpp\n@@ -46,6 +46,35 @@ protected:\n \t\t\treturn TestFail;\n \t\t}\n \n+\t\t/* Test alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() */\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+\t\t\tcout << \"Size::alignedDownTo() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tif (Size(0, 0).alignedUpTo(16, 8) != Size(0, 0) ||\n+\t\t    Size(1, 1).alignedUpTo(16, 8) != Size(16, 8) ||\n+\t\t    Size(16, 8).alignedUpTo(16, 8) != Size(16, 8)) {\n+\t\t\tcout << \"Size::alignedUpTo() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tif (Size(0, 0).boundedTo({ 100, 100 }) != Size(0, 0) ||\n+\t\t    Size(200, 50).boundedTo({ 100, 100 }) != Size(100, 50) ||\n+\t\t    Size(50, 200).boundedTo({ 100, 100 }) != Size(50, 100)) {\n+\t\t\tcout << \"Size::boundedTo() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n+\t\tif (Size(0, 0).expandedTo({ 100, 100 }) != Size(100, 100) ||\n+\t\t    Size(200, 50).expandedTo({ 100, 100 }) != Size(200, 100) ||\n+\t\t    Size(50, 200).expandedTo({ 100, 100 }) != Size(100, 200)) {\n+\t\t\tcout << \"Size::expandedTo() test failed\" << endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\t/* Test Size equality and inequality. */\n \t\tif (!compare(Size(100, 100), Size(100, 100), &operator==, \"==\", true))\n \t\t\treturn TestFail;\n","prefixes":["libcamera-devel","v2"]}