{"id":11777,"url":"https://patchwork.libcamera.org/api/patches/11777/?format=json","web_url":"https://patchwork.libcamera.org/patch/11777/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210329191826.77817-3-jeanmichel.hautbois@ideasonboard.com>","date":"2021-03-29T19:18:23","name":"[libcamera-devel,v3,2/5] ipa: ipu3: Add an histogram class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"3da7a9a29023e26241775cc3113e0e8a80fd85fc","submitter":{"id":75,"url":"https://patchwork.libcamera.org/api/people/75/?format=json","name":"Jean-Michel Hautbois","email":"jeanmichel.hautbois@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/11777/mbox/","series":[{"id":1865,"url":"https://patchwork.libcamera.org/api/series/1865/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1865","date":"2021-03-29T19:18:21","name":"Implement IPA algorithms and demo with IPU3","version":3,"mbox":"https://patchwork.libcamera.org/series/1865/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/11777/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/11777/checks/","tags":{},"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 19945C32F0\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 29 Mar 2021 19:18:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CC52168793;\n\tMon, 29 Mar 2021 21:18:40 +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 9EE0968783\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 29 Mar 2021 21:18:35 +0200 (CEST)","from localhost.localdomain (unknown\n\t[IPv6:2a01:e0a:169:7140:9714:da21:50fd:f63f])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 4AF22503;\n\tMon, 29 Mar 2021 21:18:35 +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=\"n6JDQ0q7\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1617045515;\n\tbh=4ugc+u64tJwtiF9ySV7Cyo9jk10r8fkAzax7dpg0aIE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=n6JDQ0q7rvXdNQBE0xmzZ5YTmBOQe+R4DptCJRyN5e4Iyh01fLH29/E2PF7GsAxw8\n\t8oFJ9JH4yibltB2+17bvGCWbEjIB1wboXQ88m9tifw1zydmmpCF6pPGKPCGQQuam+f\n\tbLDxTTIZqQ64JVKivqFGEt0wFGgme00JlAk4dEnQ=","From":"Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 29 Mar 2021 21:18:23 +0200","Message-Id":"<20210329191826.77817-3-jeanmichel.hautbois@ideasonboard.com>","X-Mailer":"git-send-email 2.27.0","In-Reply-To":"<20210329191826.77817-1-jeanmichel.hautbois@ideasonboard.com>","References":"<20210329191826.77817-1-jeanmichel.hautbois@ideasonboard.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v3 2/5] ipa: ipu3: Add an histogram class","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>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"This class will be used at least by AGC algorithm when quantiles are\nneeded for example.\n\nSigned-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n---\n src/ipa/libipa/histogram.cpp | 102 +++++++++++++++++++++++++++++++++++\n src/ipa/libipa/histogram.h   |  62 +++++++++++++++++++++\n src/ipa/libipa/meson.build   |   2 +\n 3 files changed, 166 insertions(+)\n create mode 100644 src/ipa/libipa/histogram.cpp\n create mode 100644 src/ipa/libipa/histogram.h","diff":"diff --git a/src/ipa/libipa/histogram.cpp b/src/ipa/libipa/histogram.cpp\nnew file mode 100644\nindex 00000000..bea52687\n--- /dev/null\n+++ b/src/ipa/libipa/histogram.cpp\n@@ -0,0 +1,102 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi (Trading) Limited\n+ *\n+ * histogram.cpp - histogram calculations\n+ */\n+#include <math.h>\n+\n+#include \"histogram.h\"\n+\n+/**\n+ * \\file histogram.h\n+ * \\brief Class to represent Histograms and manipulate them\n+ */\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+/**\n+ * \\class Histogram\n+ * \\brief The base class for creating histograms\n+ *\n+ * The Histogram class defines a standard interface for IPA algorithms. By\n+ * abstracting histograms, it makes possible the implementation of generic code\n+ * to manage algorithms regardless of their specific type.\n+ */\n+\n+/**\n+ * \\brief Cumulative frequency up to a (fractional) point in a bin.\n+ * \\param[in] bin the number of bins\n+ * \\return The number of bins cumulated\n+ */\n+uint64_t Histogram::cumulativeFreq(double bin) const\n+{\n+\tif (bin <= 0)\n+\t\treturn 0;\n+\telse if (bin >= bins())\n+\t\treturn total();\n+\tint b = (int)bin;\n+\treturn cumulative_[b] +\n+\t       (bin - b) * (cumulative_[b + 1] - cumulative_[b]);\n+}\n+\n+/**\n+ * \\brief Return the (fractional) bin of the point through the histogram\n+ * \\param[in] q the desired point (0 <= q <= 1)\n+ * \\param[in] first low limit (optionnal if -1)\n+ * \\param[in] last high limit (optionnal if -1)\n+ * \\return The fractionnal bin of the point\n+ */\n+double Histogram::quantile(double q, int first, int last) const\n+{\n+\tif (first == -1)\n+\t\tfirst = 0;\n+\tif (last == -1)\n+\t\tlast = cumulative_.size() - 2;\n+\tassert(first <= last);\n+\tuint64_t items = q * total();\n+\t/* Binary search to find the right bin */\n+\twhile (first < last) {\n+\t\tint middle = (first + last) / 2;\n+\t\t/* Is it between first and middle ? */\n+\t\tif (cumulative_[middle + 1] > items)\n+\t\t\tlast = middle;\n+\t\telse\n+\t\t\tfirst = middle + 1;\n+\t}\n+\tassert(items >= cumulative_[first] && items <= cumulative_[last + 1]);\n+\tdouble frac = cumulative_[first + 1] == cumulative_[first] ? 0\n+\t\t\t\t\t\t\t\t   : (double)(items - cumulative_[first]) /\n+\t\t\t\t\t\t\t\t\t     (cumulative_[first + 1] - cumulative_[first]);\n+\treturn first + frac;\n+}\n+\n+/**\n+ * \\brief Calculate the mean between two quantiles\n+ * \\param[in] lowQuantile low Quantile\n+ * \\param[in] highQuantile high Quantile\n+ * \\return The average histogram bin value between the two quantiles\n+ */\n+double Histogram::interQuantileMean(double lowQuantile, double highQuantile) const\n+{\n+\tassert(highQuantile > lowQuantile);\n+\tdouble lowPoint = quantile(lowQuantile);\n+\tdouble highPoint = quantile(highQuantile, (int)lowPoint);\n+\tdouble sumBinFreq = 0, cumulFreq = 0;\n+\tfor (double p_next = floor(lowPoint) + 1.0; p_next <= ceil(highPoint);\n+\t     lowPoint = p_next, p_next += 1.0) {\n+\t\tint bin = floor(lowPoint);\n+\t\tdouble freq = (cumulative_[bin + 1] - cumulative_[bin]) *\n+\t\t\t      (std::min(p_next, highPoint) - lowPoint);\n+\t\tsumBinFreq += bin * freq;\n+\t\tcumulFreq += freq;\n+\t}\n+\t/* add 0.5 to give an average for bin mid-points */\n+\treturn sumBinFreq / cumulFreq + 0.5;\n+}\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\ndiff --git a/src/ipa/libipa/histogram.h b/src/ipa/libipa/histogram.h\nnew file mode 100644\nindex 00000000..a610f675\n--- /dev/null\n+++ b/src/ipa/libipa/histogram.h\n@@ -0,0 +1,62 @@\n+/* SPDX-License-Identifier: BSD-2-Clause */\n+/*\n+ * Copyright (C) 2019, Raspberry Pi (Trading) Limited\n+ *\n+ * histogram.h - histogram calculation interface\n+ */\n+#ifndef __LIBCAMERA_IPA_LIBIPA_HISTOGRAM_H__\n+#define __LIBCAMERA_IPA_LIBIPA_HISTOGRAM_H__\n+\n+#include <assert.h>\n+#include <stdint.h>\n+#include <vector>\n+\n+// A simple histogram class, for use in particular to find \"quantiles\" and\n+// averages between \"quantiles\".\n+\n+namespace libcamera {\n+\n+namespace ipa {\n+\n+class Histogram\n+{\n+public:\n+\ttemplate<typename T>\n+\t/**\n+\t * \\brief Create a cumulative histogram with a bin number of intervals\n+\t * \\param[in] histogram a reference to the histogram\n+\t * \\param[in] num the number of bins\n+ \t*/\n+\tHistogram(T *histogram, int num)\n+\t{\n+\t\tassert(num);\n+\t\tcumulative_.reserve(num + 1);\n+\t\tcumulative_.push_back(0);\n+\t\tfor (int i = 0; i < num; i++)\n+\t\t\tcumulative_.push_back(cumulative_.back() +\n+\t\t\t\t\t      histogram[i]);\n+\t}\n+\n+\t/**\n+\t * \\brief getter for number of bins\n+\t * \\return number of bins\n+\t */\n+\tuint32_t bins() const { return cumulative_.size() - 1; }\n+\t/**\n+\t * \\brief getter for number of values\n+\t * \\return number of values\n+\t */\n+\tuint64_t total() const { return cumulative_[cumulative_.size() - 1]; }\n+\tuint64_t cumulativeFreq(double bin) const;\n+\tdouble quantile(double q, int first = -1, int last = -1) const;\n+\tdouble interQuantileMean(double lowQuantile, double hiQuantile) const;\n+\n+private:\n+\tstd::vector<uint64_t> cumulative_;\n+};\n+\n+} /* namespace ipa */\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_IPA_LIBIPA_HISTOGRAM_H__ */\ndiff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\nindex 585d02a3..53edeef9 100644\n--- a/src/ipa/libipa/meson.build\n+++ b/src/ipa/libipa/meson.build\n@@ -2,10 +2,12 @@\n \n libipa_headers = files([\n   'algorithm.h',\n+  'histogram.h'\n ])\n \n libipa_sources = files([\n     'algorithm.cpp',\n+    'histogram.cpp'\n ])\n \n libipa_includes = include_directories('..')\n","prefixes":["libcamera-devel","v3","2/5"]}