From patchwork Wed Jul 15 10:23:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 8822 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 A3CDBBD792 for ; Wed, 15 Jul 2020 10:23:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 36E6560962; Wed, 15 Jul 2020 12:23:50 +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="uxtQ/++n"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A38B0605B1 for ; Wed, 15 Jul 2020 12:23:49 +0200 (CEST) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 387AA564 for ; Wed, 15 Jul 2020 12:23:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1594808629; bh=Ht6z9Mkcwd6zF96PQLGq2D0XRlkyHqtpRbtpTFRDdLg=; h=From:To:Subject:Date:From; b=uxtQ/++ntjwjitqCGdIToZ1yqnvIvowue3QlNjUOPaP9/+y3nBsa+scgPXBl6Cgj5 kJvx9bsBe09iDkX99alpE+RYRHHHaFVNMsWeQiXkc53A8a2Zd4XrVNmIMalbfqQu9R MeMCGkQ5mynX3po9DVi0us747y5eaHgCT52w2wIQ= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 15 Jul 2020 13:23:39 +0300 Message-Id: <20200715102339.14025-1-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH] libcamera: geometry: Provide in-place versions of the Size helpers 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 alignDownTo(), alignUpTo(), boundTo() and expandTo() helper functions to the Size class. These are in-place versions of the existing alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() functions. The new helpers return a reference to the size, to allow chaining the functions. One can thus write size.alignDownTo(16, 16).alignUpTo(32, 32) .boundTo({ 40, 80 }).expandTo({ 16, 80 }); instead of size.alignDownTo(16, 16); size.alignUpTo(32, 32); size.boundTo({ 40, 80 }); size.expandTo({ 16, 80 }); Signed-off-by: Laurent Pinchart Reviewed-by: Jacopo Mondi Reviewed-by: Kieran Bingham --- This patch applies on top of the previously submitted geometry series. --- include/libcamera/geometry.h | 28 ++++++++++++++++++++++ src/libcamera/geometry.cpp | 46 ++++++++++++++++++++++++++++++++++++ test/geometry.cpp | 34 ++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 30aaa7a30fe2..d858f85cf1f0 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -32,6 +32,34 @@ public: bool isNull() const { return !width && !height; } const std::string toString() const; + Size &alignDownTo(unsigned int hAlignment, unsigned int vAlignment) + { + width = width / hAlignment * hAlignment; + height = height / vAlignment * vAlignment; + return *this; + } + + Size &alignUpTo(unsigned int hAlignment, unsigned int vAlignment) + { + width = (width + hAlignment - 1) / hAlignment * hAlignment; + height = (height + vAlignment - 1) / vAlignment * vAlignment; + return *this; + } + + Size &boundTo(const Size &bound) + { + width = std::min(width, bound.width); + height = std::min(height, bound.height); + return *this; + } + + Size &expandTo(const Size &expand) + { + width = std::max(width, expand.width); + height = std::max(height, expand.height); + return *this; + } + constexpr Size alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) const { diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp index 3a3784df39e6..23181bdec969 100644 --- a/src/libcamera/geometry.cpp +++ b/src/libcamera/geometry.cpp @@ -61,6 +61,52 @@ const std::string Size::toString() const return std::to_string(width) + "x" + std::to_string(height); } +/** + * \fn Size::alignDownTo(unsigned int hAlignment, unsigned int vAlignment) + * \brief Align the size down horizontally and vertically in place + * \param[in] hAlignment Horizontal alignment + * \param[in] vAlignment Vertical alignment + * + * This functions rounds the width and height down to the nearest multiple of + * \a hAlignment and \a vAlignment respectively. + * + * \return A reference to this object + */ + +/** + * \fn Size::alignUpTo(unsigned int hAlignment, unsigned int vAlignment) + * \brief Align the size up horizontally and vertically in place + * \param[in] hAlignment Horizontal alignment + * \param[in] vAlignment Vertical alignment + * + * This functions rounds the width and height up to the nearest multiple of + * \a hAlignment and \a vAlignment respectively. + * + * \return A reference to this object + */ + +/** + * \fn Size::boundTo(const Size &bound) + * \brief Bound the size to \a bound in place + * \param[in] bound The maximum size + * + * This function sets the width and height to the minimum of this size and the + * \a bound size. + * + * \return A reference to this object + */ + +/** + * \fn Size::expandTo(const Size &expand) + * \brief Expand the size to \a expand + * \param[in] expand The minimum size + * + * This function sets the width and height to the maximum of this size and the + * \a expand size. + * + * \return A reference to this object + */ + /** * \fn Size::alignedDownTo(unsigned int hAlignment, unsigned int vAlignment) * \brief Align the size down horizontally and vertically diff --git a/test/geometry.cpp b/test/geometry.cpp index fd0132c03b02..1a9fc1b8e3ed 100644 --- a/test/geometry.cpp +++ b/test/geometry.cpp @@ -46,6 +46,40 @@ protected: return TestFail; } + /* Test alignDownTo(), alignUpTo(), boundTo() and expandTo() */ + Size s(50, 50); + + s.alignDownTo(16, 16); + if (s != Size(48, 48)) { + cout << "Size::alignDownTo() test failed" << endl; + return TestFail; + } + + s.alignUpTo(32, 32); + if (s != Size(64, 64)) { + cout << "Size::alignUpTo() test failed" << endl; + return TestFail; + } + + s.boundTo({ 40, 40 }); + if (s != Size(40, 40)) { + cout << "Size::boundTo() test failed" << endl; + return TestFail; + } + + s.expandTo({ 50, 50 }); + if (s != Size(50, 50)) { + cout << "Size::expandTo() test failed" << endl; + return TestFail; + } + + s.alignDownTo(16, 16).alignUpTo(32, 32) + .boundTo({ 40, 80 }).expandTo({ 16, 80 }); + if (s != Size(40, 80)) { + cout << "Size chained in-place modifiers test failed" << endl; + return TestFail; + } + /* Test alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() */ if (Size(0, 0).alignedDownTo(16, 8) != Size(0, 0) || Size(1, 1).alignedDownTo(16, 8) != Size(0, 0) ||