From d5446e9f327ab4eaef38249b0907f11fbf52be45 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 15 Jul 2020 12:55:17 +0300 Subject: libcamera: geometry: Provide in-place versions of the Size helpers 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 --- include/libcamera/geometry.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'include/libcamera/geometry.h') diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 30aaa7a3..d858f85c 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 { -- cgit v1.2.1