From patchwork Wed Oct 13 01:26:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 14112 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 50032BDC71 for ; Wed, 13 Oct 2021 01:26:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 263D96012B; Wed, 13 Oct 2021 03:26:51 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="AsUldwv1"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BC8E868F4D for ; Wed, 13 Oct 2021 03:26:48 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 62D9D8F0; Wed, 13 Oct 2021 03:26:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1634088408; bh=lrNq7oXuiyVOnMpdmw1njm0ldBKxEgVVmlI+eRKsR+8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AsUldwv1iSXFmiHMfYaqHYbq+e0TVPo0E3Zg9CnOuz/ptoTCTRbvosE2z0vKAws0o hyLfAavMo2qVO9Nttr3rZxjmusNG4dtcm6mRatJXQJmVYblFdWtPtvkgeDxXn+DS/P nU27BheOnCPkiJruWVvI3MqEjriTJ9O+/YnUzekE= From: Laurent Pinchart 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 Subject: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size members to grown or shrink by a margin X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add four new member functions to the Size class (two in-place and two const) to grow and shrink a Size by given margins. Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- include/libcamera/geometry.h | 30 ++++++++++++++++++++++ src/libcamera/geometry.cpp | 42 +++++++++++++++++++++++++++++++ test/geometry.cpp | 48 ++++++++++++++++++++++++++++++++---- 3 files changed, 115 insertions(+), 5 deletions(-) diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index fdd1b4678074..fa7ae7bce8db 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -94,6 +94,20 @@ public: return *this; } + Size &growBy(const Size &margins) + { + width += margins.width; + height += margins.height; + return *this; + } + + Size &shrinkBy(const Size &margins) + { + width = width > margins.width ? width - margins.width : 0; + height = height > margins.height ? height - margins.height : 0; + return *this; + } + __nodiscard constexpr Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const { @@ -128,6 +142,22 @@ public: }; } + __nodiscard constexpr Size grownBy(const Size &margins) const + { + return { + width + margins.width, + height + margins.height + }; + } + + __nodiscard constexpr Size shrunkBy(const Size &margins) const + { + return { + width > margins.width ? width - margins.width : 0, + height > margins.height ? height - margins.height : 0 + }; + } + __nodiscard Size boundedToAspectRatio(const Size &ratio) const; __nodiscard Size expandedToAspectRatio(const Size &ratio) const; diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp index b014180e9ba5..cb3c2de18d5e 100644 --- a/src/libcamera/geometry.cpp +++ b/src/libcamera/geometry.cpp @@ -173,6 +173,28 @@ const std::string Size::toString() const * \return A reference to this object */ +/** + * \fn Size::growBy(const Size &margins) + * \brief Grow the size by \a margins in place + * \param[in] margins The margins to add to the size + * + * This function adds the width and height of the \a margin size to this size. + * + * \return A reference to this object + */ + +/** + * \fn Size::shrinkBy(const Size &margins) + * \brief Shrink the size by \a margins in place + * \param[in] margins The margins to subtract to the size + * + * This function subtracts the width and height of the \a margin size from this + * size. If the width or height of the size are smaller than those of \a + * margins, the result is clamped to 0. + * + * \return A reference to this object + */ + /** * \fn Size::alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) * \brief Align the size down horizontally and vertically @@ -209,6 +231,26 @@ const std::string Size::toString() const * height of this size and the \a expand size */ +/** + * \fn Size::grownBy(const Size &margins) + * \brief Grow the size by \a margins + * \param[in] margins The margins to add to the size + * \return A Size whose width and height are the sum of the width and height of + * this size and the \a margins size + */ + +/** + * \fn Size::shrunkBy(const Size &margins) + * \brief Shrink the size by \a margins + * \param[in] margins The margins to subtract to the size + * + * If the width or height of the size are smaller than those of \a margins, the + * resulting size has its width or height clamped to 0. + * + * \return A Size whose width and height are the difference of the width and + * height of this size and the \a margins size, clamped to 0 + */ + /** * \brief Bound the size down to match the aspect ratio given by \a ratio * \param[in] ratio The size whose aspect ratio must be matched diff --git a/test/geometry.cpp b/test/geometry.cpp index d13c88bf2e40..5125692496b3 100644 --- a/test/geometry.cpp +++ b/test/geometry.cpp @@ -104,7 +104,10 @@ protected: return TestFail; } - /* Test alignDownTo(), alignUpTo(), boundTo() and expandTo() */ + /* + * Test alignDownTo(), alignUpTo(), boundTo(), expandTo(), + * growBy() and shrinkBy() + */ Size s(50, 50); s.alignDownTo(16, 16); @@ -131,14 +134,36 @@ protected: return TestFail; } - s.alignDownTo(16, 16).alignUpTo(32, 32) - .boundTo({ 40, 80 }).expandTo({ 16, 80 }); - if (s != Size(40, 80)) { + s.growBy({ 10, 20 }); + if (s != Size(60, 70)) { + cout << "Size::growBy() test failed" << endl; + return TestFail; + } + + s.shrinkBy({ 20, 10 }); + if (s != Size(40, 60)) { + cout << "Size::shrinkBy() test failed" << endl; + return TestFail; + } + + s.shrinkBy({ 100, 100 }); + if (s != Size(0, 0)) { + cout << "Size::shrinkBy() clamp test failed" << endl; + return TestFail; + } + + s = Size(50,50).alignDownTo(16, 16).alignUpTo(32, 32) + .boundTo({ 40, 80 }).expandTo({ 16, 80 }) + .growBy({ 4, 4 }).shrinkBy({ 10, 20 }); + if (s != Size(34, 64)) { cout << "Size chained in-place modifiers test failed" << endl; return TestFail; } - /* Test alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() */ + /* + * Test alignedDownTo(), alignedUpTo(), boundedTo(), + * expandedTo(), grownBy() and shrunkBy() + */ if (Size(0, 0).alignedDownTo(16, 8) != Size(0, 0) || Size(1, 1).alignedDownTo(16, 8) != Size(0, 0) || Size(16, 8).alignedDownTo(16, 8) != Size(16, 8)) { @@ -167,6 +192,19 @@ protected: return TestFail; } + if (Size(0, 0).grownBy({ 10, 20 }) != Size(10, 20) || + Size(200, 50).grownBy({ 10, 20 }) != Size(210, 70)) { + cout << "Size::grownBy() test failed" << endl; + return TestFail; + } + + if (Size(200, 50).shrunkBy({ 10, 20 }) != Size(190, 30) || + Size(200, 50).shrunkBy({ 10, 100 }) != Size(190, 0) || + Size(200, 50).shrunkBy({ 300, 20 }) != Size(0, 30)) { + cout << "Size::shrunkBy() test failed" << endl; + return TestFail; + } + /* Aspect ratio tests */ if (Size(0, 0).boundedToAspectRatio(Size(4, 3)) != Size(0, 0) || Size(1920, 1440).boundedToAspectRatio(Size(16, 9)) != Size(1920, 1080) ||