[{"id":29454,"web_url":"https://patchwork.libcamera.org/comment/29454/","msgid":"<20240508152318.GC19625@pendragon.ideasonboard.com>","date":"2024-05-08T15:23:18","subject":"Re: [PATCH v4] ipa: libipa: histogram: Add transform parameter to\n\tconstructor","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Paul,\n\nThank you for the patch.\n\nOn Wed, May 08, 2024 at 11:59:24PM +0900, Paul Elder wrote:\n> Add a parameter to the histogram constructor that takes a transformation\n> function to apply to all the bins upon construction.\n> \n> This is necessary notably for the rkisp1, as the values reported from\n> the hardware are 20 bits where the upper 16-bits are meaningful integer\n> values and the lower 4 bits are fractional and meant to be discarded. As\n> adding a right-shift parameter is probably too specialized, a generic\n> function is added as a parameter instead.\n> \n> Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\n> \n> ---\n> Changes in v4:\n> - fix compilation when called with no transform function\n> \n> Changes in v3:\n> - make the transform function a template parameter, and optimize the\n>   constructor\n> \n> This used to be \"ipa: libipa: histogram: Add rshift parameter to\n> constructor\"\n> \n> Changes in v2:\n> - change rshift parameter to a function parameter\n> ---\n>  src/ipa/libipa/histogram.cpp | 14 ++++++++++----\n>  src/ipa/libipa/histogram.h   | 13 ++++++++++++-\n>  2 files changed, 22 insertions(+), 5 deletions(-)\n> \n> diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp\n> index c1aac59b..212acfdf 100644\n> --- a/src/ipa/libipa/histogram.cpp\n> +++ b/src/ipa/libipa/histogram.cpp\n> @@ -43,12 +43,18 @@ namespace ipa {\n>   */\n>  Histogram::Histogram(Span<const uint32_t> data)\n>  {\n> -\tcumulative_.reserve(data.size());\n> -\tcumulative_.push_back(0);\n> -\tfor (const uint32_t &value : data)\n> -\t\tcumulative_.push_back(cumulative_.back() + value);\n> +\tcumulative_.resize(data.size() + 1);\n> +\tcumulative_[0] = 0;\n> +\tfor (const auto &[i, value] : utils::enumerate(data))\n> +\t\tcumulative_[i + 1] = cumulative_[i] + value;\n\nThis is a nice optimization. I would have split it to another patch\nthough, or at least mentioned it in the commit message.\n\n>  }\n>  \n> +/**\n> + * \\brief Create a cumulative histogram\n> + * \\param[in] data A pre-sorted histogram to be passed\n\nLooks like there's room for improvement in the existing documentation of\nthe constructor. \"pre-sorted\" doesn't make too much sense here, and\nneither does \"to be passed\".\n\n> + * \\param[in] transform The transformation function to apply to every bin\n> + */\n> +\n>  /**\n>   * \\fn Histogram::bins()\n>   * \\brief Retrieve the number of bins currently used by the Histogram\n> diff --git a/src/ipa/libipa/histogram.h b/src/ipa/libipa/histogram.h\n> index 54bb2a19..0e9bef2a 100644\n> --- a/src/ipa/libipa/histogram.h\n> +++ b/src/ipa/libipa/histogram.h\n> @@ -10,10 +10,10 @@\n>  #include <assert.h>\n>  #include <limits.h>\n>  #include <stdint.h>\n\nYou need to include <type_traits> for std::enable_if_t and\nstd::is_invocable_v.\n\nWith these small issues fixed,\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n> -\n>  #include <vector>\n>  \n>  #include <libcamera/base/span.h>\n> +#include <libcamera/base/utils.h>\n>  \n>  namespace libcamera {\n>  \n> @@ -24,6 +24,17 @@ class Histogram\n>  public:\n>  \tHistogram() { cumulative_.push_back(0); }\n>  \tHistogram(Span<const uint32_t> data);\n> +\n> +\ttemplate<typename Transform,\n> +\t\t std::enable_if_t<std::is_invocable_v<Transform, uint32_t>> * = nullptr>\n> +\tHistogram(Span<const uint32_t> data, Transform transform)\n> +\t{\n> +\t\tcumulative_.resize(data.size() + 1);\n> +\t\tcumulative_[0] = 0;\n> +\t\tfor (const auto &[i, value] : utils::enumerate(data))\n> +\t\t\tcumulative_[i + 1] = cumulative_[i] + transform(value);\n> +\t}\n> +\n>  \tsize_t bins() const { return cumulative_.size() - 1; }\n>  \tuint64_t total() const { return cumulative_[cumulative_.size() - 1]; }\n>  \tuint64_t cumulativeFrequency(double bin) const;","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 70404BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed,  8 May 2024 15:23:29 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 2DFC863437;\n\tWed,  8 May 2024 17:23:28 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4AED861A73\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  8 May 2024 17:23:27 +0200 (CEST)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E2081FD6;\n\tWed,  8 May 2024 17:23:23 +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=\"a/PYm1UB\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1715181804;\n\tbh=8sAmqAetGjWvn9Uyt1jpjKmteZsvDRPJ/p+nOoe/gWM=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=a/PYm1UBQKvkbBy+DQklghS9iC0WoVyrzPO3TssLiZozkzL7j6CWrjd5IUD/y6G5M\n\tvOQ8QchhJL4KE/AqzniFmiZA3pwC5yRj2/8QqZ1j+YZuAH+6NOTGOuqgkZ/pyE25Jb\n\t2aNtVM2AZ8LWZVL7NpKHFs5PtS7NCuvUYXuWtimA=","Date":"Wed, 8 May 2024 18:23:18 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Paul Elder <paul.elder@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tDaniel Scally <dan.scally@ideasonboard.com>","Subject":"Re: [PATCH v4] ipa: libipa: histogram: Add transform parameter to\n\tconstructor","Message-ID":"<20240508152318.GC19625@pendragon.ideasonboard.com>","References":"<20240508145924.1477599-1-paul.elder@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20240508145924.1477599-1-paul.elder@ideasonboard.com>","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>"}},{"id":29466,"web_url":"https://patchwork.libcamera.org/comment/29466/","msgid":"<ZjxqXdp1sTZ7rBjw@pyrite.rasen.tech>","date":"2024-05-09T06:17:01","subject":"Re: [PATCH v4] ipa: libipa: histogram: Add transform parameter to\n\tconstructor","submitter":{"id":17,"url":"https://patchwork.libcamera.org/api/people/17/","name":"Paul Elder","email":"paul.elder@ideasonboard.com"},"content":"On Wed, May 08, 2024 at 06:23:18PM +0300, Laurent Pinchart wrote:\n> Hi Paul,\n> \n> Thank you for the patch.\n> \n> On Wed, May 08, 2024 at 11:59:24PM +0900, Paul Elder wrote:\n> > Add a parameter to the histogram constructor that takes a transformation\n> > function to apply to all the bins upon construction.\n> > \n> > This is necessary notably for the rkisp1, as the values reported from\n> > the hardware are 20 bits where the upper 16-bits are meaningful integer\n> > values and the lower 4 bits are fractional and meant to be discarded. As\n> > adding a right-shift parameter is probably too specialized, a generic\n> > function is added as a parameter instead.\n> > \n> > Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>\n> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> > Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>\n> > \n> > ---\n> > Changes in v4:\n> > - fix compilation when called with no transform function\n> > \n> > Changes in v3:\n> > - make the transform function a template parameter, and optimize the\n> >   constructor\n> > \n> > This used to be \"ipa: libipa: histogram: Add rshift parameter to\n> > constructor\"\n> > \n> > Changes in v2:\n> > - change rshift parameter to a function parameter\n> > ---\n> >  src/ipa/libipa/histogram.cpp | 14 ++++++++++----\n> >  src/ipa/libipa/histogram.h   | 13 ++++++++++++-\n> >  2 files changed, 22 insertions(+), 5 deletions(-)\n> > \n> > diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp\n> > index c1aac59b..212acfdf 100644\n> > --- a/src/ipa/libipa/histogram.cpp\n> > +++ b/src/ipa/libipa/histogram.cpp\n> > @@ -43,12 +43,18 @@ namespace ipa {\n> >   */\n> >  Histogram::Histogram(Span<const uint32_t> data)\n> >  {\n> > -\tcumulative_.reserve(data.size());\n> > -\tcumulative_.push_back(0);\n> > -\tfor (const uint32_t &value : data)\n> > -\t\tcumulative_.push_back(cumulative_.back() + value);\n> > +\tcumulative_.resize(data.size() + 1);\n> > +\tcumulative_[0] = 0;\n> > +\tfor (const auto &[i, value] : utils::enumerate(data))\n> > +\t\tcumulative_[i + 1] = cumulative_[i] + value;\n> \n> This is a nice optimization. I would have split it to another patch\n> though, or at least mentioned it in the commit message.\n> \n\nOh yeah true.\n\n> >  }\n> >  \n> > +/**\n> > + * \\brief Create a cumulative histogram\n> > + * \\param[in] data A pre-sorted histogram to be passed\n> \n> Looks like there's room for improvement in the existing documentation of\n> the constructor. \"pre-sorted\" doesn't make too much sense here, and\n> neither does \"to be passed\".\n\n\"But it does have to be pre-sorted... oh wait, we're passing in a\nnon-cumulative histogram... so it's already sorted by definition... ok\nI see\"\n\n> \n> > + * \\param[in] transform The transformation function to apply to every bin\n> > + */\n> > +\n> >  /**\n> >   * \\fn Histogram::bins()\n> >   * \\brief Retrieve the number of bins currently used by the Histogram\n> > diff --git a/src/ipa/libipa/histogram.h b/src/ipa/libipa/histogram.h\n> > index 54bb2a19..0e9bef2a 100644\n> > --- a/src/ipa/libipa/histogram.h\n> > +++ b/src/ipa/libipa/histogram.h\n> > @@ -10,10 +10,10 @@\n> >  #include <assert.h>\n> >  #include <limits.h>\n> >  #include <stdint.h>\n> \n> You need to include <type_traits> for std::enable_if_t and\n> std::is_invocable_v.\n\nack\n\n> \n> With these small issues fixed,\n> \n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nThanks,\n\nPaul\n\n> \n> > -\n> >  #include <vector>\n> >  \n> >  #include <libcamera/base/span.h>\n> > +#include <libcamera/base/utils.h>\n> >  \n> >  namespace libcamera {\n> >  \n> > @@ -24,6 +24,17 @@ class Histogram\n> >  public:\n> >  \tHistogram() { cumulative_.push_back(0); }\n> >  \tHistogram(Span<const uint32_t> data);\n> > +\n> > +\ttemplate<typename Transform,\n> > +\t\t std::enable_if_t<std::is_invocable_v<Transform, uint32_t>> * = nullptr>\n> > +\tHistogram(Span<const uint32_t> data, Transform transform)\n> > +\t{\n> > +\t\tcumulative_.resize(data.size() + 1);\n> > +\t\tcumulative_[0] = 0;\n> > +\t\tfor (const auto &[i, value] : utils::enumerate(data))\n> > +\t\t\tcumulative_[i + 1] = cumulative_[i] + transform(value);\n> > +\t}\n> > +\n> >  \tsize_t bins() const { return cumulative_.size() - 1; }\n> >  \tuint64_t total() const { return cumulative_[cumulative_.size() - 1]; }\n> >  \tuint64_t cumulativeFrequency(double bin) const;\n> \n> -- \n> Regards,\n> \n> Laurent Pinchart","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 41E76BDE6B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu,  9 May 2024 06:17:12 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 604336345F;\n\tThu,  9 May 2024 08:17:11 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 31F6D61A6B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu,  9 May 2024 08:17:09 +0200 (CEST)","from pyrite.rasen.tech (h175-177-049-156.catv02.itscom.jp\n\t[175.177.49.156])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3615E2D54;\n\tThu,  9 May 2024 08:17:03 +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=\"R6AFcWcy\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1715235425;\n\tbh=V4sNwMGbq/Vj86Wk8ViP7VUpQ/pvsYGySYa2tddVINw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=R6AFcWcyYM/kgL0scHYAq/0DV3ga5/c7Pc6pfVyx1XWZBpSYrGpeKOY5mO6xZZw7o\n\tRJNx0PMGie8/FqbK4KmBPuwLRu7pywsmLMH5bA5eLj07jKAXYNRBdHfTGI1zBJSWrw\n\tJuhemhaPQUnkoocnho+l3WVwg5CXZlRz/AY9Bo6g=","Date":"Thu, 9 May 2024 15:17:01 +0900","From":"Paul Elder <paul.elder@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>,\n\tDaniel Scally <dan.scally@ideasonboard.com>","Subject":"Re: [PATCH v4] ipa: libipa: histogram: Add transform parameter to\n\tconstructor","Message-ID":"<ZjxqXdp1sTZ7rBjw@pyrite.rasen.tech>","References":"<20240508145924.1477599-1-paul.elder@ideasonboard.com>\n\t<20240508152318.GC19625@pendragon.ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=us-ascii","Content-Disposition":"inline","In-Reply-To":"<20240508152318.GC19625@pendragon.ideasonboard.com>","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>"}}]