[{"id":20165,"web_url":"https://patchwork.libcamera.org/comment/20165/","msgid":"<20211013063648.5x6cmpbhvr236cin@uno.localdomain>","date":"2021-10-13T06:36:48","subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size\n\tmembers to grown or shrink by a margin","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Laurent\n\nOn Wed, Oct 13, 2021 at 04:26:29AM +0300, Laurent Pinchart wrote:\n> Add four new member functions to the Size class (two in-place and two\n> const) to grow and shrink a Size by given margins.\n>\n> Signed-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(-)\n>\n> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\n> index 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\nI was about to suggest std::max(height - margins.height, 0u) but it\ndoesn't work well with unsigned ints.\n\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\n\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>\n> diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\n> index 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\n> diff --git a/test/geometry.cpp b/test/geometry.cpp\n> index 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> --\n> Regards,\n>\n> Laurent Pinchart\n>","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 EE716BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 13 Oct 2021 06:36:02 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 45BC368F51;\n\tWed, 13 Oct 2021 08:36:02 +0200 (CEST)","from relay12.mail.gandi.net (relay12.mail.gandi.net\n\t[217.70.178.232])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F2335604FF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Oct 2021 08:36:00 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay12.mail.gandi.net (Postfix) with ESMTPSA id 14E7F200004;\n\tWed, 13 Oct 2021 06:35:59 +0000 (UTC)"],"Date":"Wed, 13 Oct 2021 08:36:48 +0200","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Message-ID":"<20211013063648.5x6cmpbhvr236cin@uno.localdomain>","References":"<20211013012630.18877-1-laurent.pinchart@ideasonboard.com>\n\t<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size\n\tmembers to 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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":20167,"web_url":"https://patchwork.libcamera.org/comment/20167/","msgid":"<YWaSLPjwW6DlzVej@pendragon.ideasonboard.com>","date":"2021-10-13T08:00:44","subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size\n\tmembers to grown or shrink by a margin","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn Wed, Oct 13, 2021 at 08:36:48AM +0200, Jacopo Mondi wrote:\n> On Wed, Oct 13, 2021 at 04:26:29AM +0300, Laurent Pinchart wrote:\n> > Add four new member functions to the Size class (two in-place and two\n> > const) to grow and shrink a Size by given margins.\n> >\n> > Signed-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(-)\n> >\n> > diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\n> > index 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> \n> I was about to suggest std::max(height - margins.height, 0u) but it\n> doesn't work well with unsigned ints.\n\nI've thought the same and considered playing with casts, but the result\nwas worse.\n\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> \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> >\n> > diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\n> > index 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\n> > diff --git a/test/geometry.cpp b/test/geometry.cpp\n> > index 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) ||","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 B3D8EBDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 13 Oct 2021 08:01:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2AB8C68F4D;\n\tWed, 13 Oct 2021 10:01:00 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 79A1268546\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 13 Oct 2021 10:00:58 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id F07AF291;\n\tWed, 13 Oct 2021 10:00:57 +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=\"bZFkD152\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634112058;\n\tbh=p1BfGqBDEUE4DGkhP/3SSeT7flGKOnZCz7EAiqBfT4Y=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=bZFkD152yNA0jcODKvMxCxiCtK3lB2150p8Y4DSxGE3RyxOqL6VbPWlHxa5kFksLO\n\t1pEPk/Gs1x4RojZwMCz4+n4YYtx/PM1in/mQDTOilW3Ur1yw0g0/32ehqkjQEvxZ3n\n\thACBdx6ys8AMF79FnYmyL6SM33+T1g+Sjfjooajc=","Date":"Wed, 13 Oct 2021 11:00:44 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YWaSLPjwW6DlzVej@pendragon.ideasonboard.com>","References":"<20211013012630.18877-1-laurent.pinchart@ideasonboard.com>\n\t<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>\n\t<20211013063648.5x6cmpbhvr236cin@uno.localdomain>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20211013063648.5x6cmpbhvr236cin@uno.localdomain>","Subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size\n\tmembers to 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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":20248,"web_url":"https://patchwork.libcamera.org/comment/20248/","msgid":"<163433820161.456562.2569926690268813690@Monstersaurus>","date":"2021-10-15T22:50:01","subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size\n\tmembers to grown or shrink by a margin","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Laurent Pinchart (2021-10-13 02:26:29)\n> Add four new member functions to the Size class (two in-place and two\n> const) to grow and shrink a Size by given margins.\n> \n\nInteresting helpers.\nI like the use of language for grow/grown shrink/shrunk to differentiate\nthe actions.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> Signed-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(-)\n> \n> diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h\n> index fdd1b4678074..fa7ae7bce8db 100644\n> --- a/include/libcamera/geometry.h\n> +++ b/include/libcamera/geometry.h\n> @@ -94,6 +94,20 @@ public:\n>                 return *this;\n>         }\n>  \n> +       Size &growBy(const Size &margins)\n> +       {\n> +               width += margins.width;\n> +               height += margins.height;\n> +               return *this;\n> +       }\n> +\n> +       Size &shrinkBy(const Size &margins)\n> +       {\n> +               width = width > margins.width ? width - margins.width : 0;\n> +               height = height > margins.height ? height - margins.height : 0;\n> +               return *this;\n> +       }\n> +\n>         __nodiscard constexpr Size alignedDownTo(unsigned int hAlignment,\n>                                                  unsigned int vAlignment) const\n>         {\n> @@ -128,6 +142,22 @@ public:\n>                 };\n>         }\n>  \n> +       __nodiscard constexpr Size grownBy(const Size &margins) const\n> +       {\n> +               return {\n> +                       width + margins.width,\n> +                       height + margins.height\n> +               };\n> +       }\n> +\n> +       __nodiscard constexpr Size shrunkBy(const Size &margins) const\n> +       {\n> +               return {\n> +                       width > margins.width ? width - margins.width : 0,\n> +                       height > margins.height ? height - margins.height : 0\n> +               };\n> +       }\n> +\n>         __nodiscard Size boundedToAspectRatio(const Size &ratio) const;\n>         __nodiscard Size expandedToAspectRatio(const Size &ratio) const;\n>  \n> diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp\n> index 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\n> diff --git a/test/geometry.cpp b/test/geometry.cpp\n> index d13c88bf2e40..5125692496b3 100644\n> --- a/test/geometry.cpp\n> +++ b/test/geometry.cpp\n> @@ -104,7 +104,10 @@ protected:\n>                         return TestFail;\n>                 }\n>  \n> -               /* Test alignDownTo(), alignUpTo(), boundTo() and expandTo() */\n> +               /*\n> +                * Test alignDownTo(), alignUpTo(), boundTo(), expandTo(),\n> +                * growBy() and shrinkBy()\n> +                */\n>                 Size s(50, 50);\n>  \n>                 s.alignDownTo(16, 16);\n> @@ -131,14 +134,36 @@ protected:\n>                         return TestFail;\n>                 }\n>  \n> -               s.alignDownTo(16, 16).alignUpTo(32, 32)\n> -                .boundTo({ 40, 80 }).expandTo({ 16, 80 });\n> -               if (s != Size(40, 80)) {\n> +               s.growBy({ 10, 20 });\n> +               if (s != Size(60, 70)) {\n> +                       cout << \"Size::growBy() test failed\" << endl;\n> +                       return TestFail;\n> +               }\n> +\n> +               s.shrinkBy({ 20, 10 });\n> +               if (s != Size(40, 60)) {\n> +                       cout << \"Size::shrinkBy() test failed\" << endl;\n> +                       return TestFail;\n> +               }\n> +\n> +               s.shrinkBy({ 100, 100 });\n> +               if (s != Size(0, 0)) {\n> +                       cout << \"Size::shrinkBy() clamp test failed\" << endl;\n> +                       return TestFail;\n> +               }\n> +\n> +               s = Size(50,50).alignDownTo(16, 16).alignUpTo(32, 32)\n> +                 .boundTo({ 40, 80 }).expandTo({ 16, 80 })\n> +                 .growBy({ 4, 4 }).shrinkBy({ 10, 20 });\n> +               if (s != Size(34, 64)) {\n>                         cout << \"Size chained in-place modifiers test failed\" << endl;\n>                         return TestFail;\n>                 }\n>  \n> -               /* Test alignedDownTo(), alignedUpTo(), boundedTo() and expandedTo() */\n> +               /*\n> +                * Test alignedDownTo(), alignedUpTo(), boundedTo(),\n> +                * expandedTo(), grownBy() and shrunkBy()\n> +                */\n>                 if (Size(0, 0).alignedDownTo(16, 8) != Size(0, 0) ||\n>                     Size(1, 1).alignedDownTo(16, 8) != Size(0, 0) ||\n>                     Size(16, 8).alignedDownTo(16, 8) != Size(16, 8)) {\n> @@ -167,6 +192,19 @@ protected:\n>                         return TestFail;\n>                 }\n>  \n> +               if (Size(0, 0).grownBy({ 10, 20 }) != Size(10, 20) ||\n> +                   Size(200, 50).grownBy({ 10, 20 }) != Size(210, 70)) {\n> +                       cout << \"Size::grownBy() test failed\" << endl;\n> +                       return TestFail;\n> +               }\n> +\n> +               if (Size(200, 50).shrunkBy({ 10, 20 }) != Size(190, 30) ||\n> +                   Size(200, 50).shrunkBy({ 10, 100 }) != Size(190, 0) ||\n> +                   Size(200, 50).shrunkBy({ 300, 20 }) != Size(0, 30)) {\n> +                       cout << \"Size::shrunkBy() test failed\" << endl;\n> +                       return TestFail;\n> +               }\n> +\n>                 /* Aspect ratio tests */\n>                 if (Size(0, 0).boundedToAspectRatio(Size(4, 3)) != Size(0, 0) ||\n>                     Size(1920, 1440).boundedToAspectRatio(Size(16, 9)) != Size(1920, 1080) ||\n> -- \n> Regards,\n> \n> Laurent Pinchart\n>","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 89404C324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Oct 2021 22:50:11 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 0633368F4F;\n\tSat, 16 Oct 2021 00:50:11 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id BD1F168541\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 16 Oct 2021 00:50:04 +0200 (CEST)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 50E6B29B;\n\tSat, 16 Oct 2021 00:50:04 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"S1kllMN0\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1634338204;\n\tbh=KVxwxfIUxhJqbmmFieTI2DOQerI8gwApOL09yi0cBfg=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=S1kllMN06Mlysu5UeAznO9S1fiUkWdC2eXdDGEOpICimOr4NjYEAH6iRaHla43gaO\n\tKhdJexPQJXGHWrU69o74639hM79RVQRU5v90Cuhmqj3x2wwq5EZIasxfWZ8S6gS9B6\n\t6Do311mR6Iw2vGhMtd6s5/vUM2Uf3cFn4ztK/0dc=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>","References":"<20211013012630.18877-1-laurent.pinchart@ideasonboard.com>\n\t<20211013012630.18877-2-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 15 Oct 2021 23:50:01 +0100","Message-ID":"<163433820161.456562.2569926690268813690@Monstersaurus>","User-Agent":"alot/0.9.1","Subject":"Re: [libcamera-devel] [PATCH 1/2] libcamera: geometry: Add Size\n\tmembers to 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>"}}]