[{"id":37884,"web_url":"https://patchwork.libcamera.org/comment/37884/","msgid":"<176909985596.302817.17753496451309574776@localhost>","date":"2026-01-22T16:37:35","subject":"Re: [PATCH v6 02/16] test: libipa: Add tests for Quantized types","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"content":"Hi Kiearan,\n\nQuoting Kieran Bingham (2026-01-21 18:37:21)\n> Provide use case tests for the Quantized types to ensure construction\n> and usages are consistent and work as expected.\n> \n> Reviewed-by: Isaac Scott <isaac.scott@ideasonboard.com>\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> \n> ---\n> v3:\n> - Rename quantized_type to QuantizedType\n> \n> v5:\n> - use static asserts for constructible failure tests\n> - Remove constexpr from lround users (only possible in C++23)\n> - Remove move tests\n> - Fix up inequality test\n> \n> v6:\n> - Remove static casts on fromFloat conversions\n> - Use int8_t(64) instead of (int8_t)64\n> - Add new test from different floats to the same quantized value\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nDouble signed off? Will be cut anyways...\n\nLooks good to me.\n\nReviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>\n\nRegards,\nStefan\n\n> ---\n>  test/ipa/libipa/meson.build   |   1 +\n>  test/ipa/libipa/quantized.cpp | 148 ++++++++++++++++++++++++++++++++++\n>  2 files changed, 149 insertions(+)\n>  create mode 100644 test/ipa/libipa/quantized.cpp\n> \n> diff --git a/test/ipa/libipa/meson.build b/test/ipa/libipa/meson.build\n> index 2070bed70222..c3e255871f4f 100644\n> --- a/test/ipa/libipa/meson.build\n> +++ b/test/ipa/libipa/meson.build\n> @@ -5,6 +5,7 @@ libipa_test = [\n>      {'name': 'histogram', 'sources': ['histogram.cpp']},\n>      {'name': 'interpolator', 'sources': ['interpolator.cpp']},\n>      {'name': 'pwl', 'sources': ['pwl.cpp'] },\n> +    {'name': 'quantized', 'sources': ['quantized.cpp']},\n>  ]\n>  \n>  foreach test : libipa_test\n> diff --git a/test/ipa/libipa/quantized.cpp b/test/ipa/libipa/quantized.cpp\n> new file mode 100644\n> index 000000000000..860d54d2dd18\n> --- /dev/null\n> +++ b/test/ipa/libipa/quantized.cpp\n> @@ -0,0 +1,148 @@\n> +/* SPDX-License-Identifier: GPL-2.0-or-later */\n> +/*\n> + * Copyright (C) 2025, Ideas on Board\n> + *\n> + * Dual Type and Quantizer tests\n> + */\n> +\n> +#include \"../src/ipa/libipa/quantized.h\"\n> +\n> +#include <algorithm>\n> +#include <cmath>\n> +#include <iostream>\n> +#include <map>\n> +#include <stdint.h>\n> +\n> +#include \"test.h\"\n> +\n> +using namespace std;\n> +using namespace libcamera;\n> +using namespace ipa;\n> +\n> +struct BrightnessHueTraits {\n> +       using QuantizedType = int8_t;\n> +       static QuantizedType fromFloat(float v)\n> +       {\n> +               int quantized = std::lround(v * 128.0f);\n> +               return std::clamp<int>(quantized, -128, 127);\n> +       }\n> +       static float toFloat(QuantizedType v)\n> +       {\n> +               return static_cast<float>(v) / 128.0f;\n> +       }\n> +};\n> +\n> +using BrightnessHueQuantizer = Quantized<BrightnessHueTraits>;\n> +\n> +struct ContrastSaturationTraits {\n> +       using QuantizedType = uint8_t;\n> +       static QuantizedType fromFloat(float v)\n> +       {\n> +               int quantized = std::lround(v * 128.0f);\n> +               return std::clamp<int>(quantized, 0, 255);\n> +       }\n> +       static float toFloat(QuantizedType v)\n> +       {\n> +               return static_cast<float>(v) / 128.0f;\n> +       }\n> +};\n> +\n> +using ContrastSaturationQuantizer = Quantized<ContrastSaturationTraits>;\n> +\n> +using BrightnessQ = BrightnessHueQuantizer;\n> +using HueQ = BrightnessHueQuantizer;\n> +using ContrastQ = ContrastSaturationQuantizer;\n> +using SaturationQ = ContrastSaturationQuantizer;\n> +\n> +class QuantizedTest : public Test\n> +{\n> +protected:\n> +       int run()\n> +       {\n> +               /* Test construction from float */\n> +               {\n> +                       BrightnessQ b(0.5f);\n> +                       if (b.quantized() != 64 || std::abs(b.value() - 0.5f) > 0.01f)\n> +                               return TestFail;\n> +               }\n> +\n> +               /* Test construction from T */\n> +               {\n> +                       ContrastQ c(uint8_t(128));\n> +                       if (c.quantized() != 128 || std::abs(c.value() - 1.0f) > 0.01f)\n> +                               return TestFail;\n> +               }\n> +\n> +               /*\n> +                * Only construction from the exact storage type or a float\n> +                * is permitted.\n> +                */\n> +               static_assert(!std::is_constructible_v<BrightnessQ, unsigned int>);\n> +               static_assert(!std::is_constructible_v<BrightnessQ, uint8_t>);\n> +               static_assert(!std::is_constructible_v<BrightnessQ, double>);\n> +               static_assert(!std::is_constructible_v<ContrastQ, int>);\n> +               static_assert(!std::is_constructible_v<ContrastQ, int8_t>);\n> +               static_assert(!std::is_constructible_v<ContrastQ, unsigned int>);\n> +\n> +               /* Test equality */\n> +               {\n> +                       BrightnessQ b1(0.5f), b2(int8_t(64));\n> +                       if (!(b1 == b2))\n> +                               return TestFail;\n> +               }\n> +\n> +               /* Test inequality */\n> +               {\n> +                       BrightnessQ b1(0.5f), b2(-0.5f);\n> +                       if (!(b1 != b2))\n> +                               return TestFail;\n> +               }\n> +\n> +               /* Test copying */\n> +               {\n> +                       BrightnessQ b1(0.25f);\n> +                       BrightnessQ b2 = b1;\n> +                       if (!(b1 == b2))\n> +                               return TestFail;\n> +               }\n> +\n> +               /* Test assignment */\n> +               {\n> +                       ContrastQ c1(1.5f);\n> +                       ContrastQ c2(0.0f);\n> +                       c2 = c1;\n> +                       if (!(c1 == c2))\n> +                               return TestFail;\n> +               }\n> +\n> +               /*\n> +                * Test construction from different floats mapping to same\n> +                * quantized value\n> +                */\n> +               {\n> +                       /* Two floats that have the same quantized value. */\n> +                       const float f1 = 1.007f;\n> +                       const float f2 = 1.008f;\n> +\n> +                       ContrastQ c1(f1);\n> +                       ContrastQ c2(f2);\n> +\n> +                       /* Quantized values must match */\n> +                       if (!(c1.quantized() == c2.quantized()))\n> +                               return TestFail;\n> +\n> +                       /* Float values must now match */\n> +                       if (!(c1.value() == c2.value()))\n> +                               return TestFail;\n> +\n> +                       if (!(c1 == c2))\n> +                               return TestFail;\n> +               }\n> +\n> +               std::cout << \"Quantised tests passed successfully.\" << std::endl;\n> +\n> +               return TestPass;\n> +       }\n> +};\n> +\n> +TEST_REGISTER(QuantizedTest)\n> -- \n> 2.52.0\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 CDB72BDCBF\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 22 Jan 2026 16:37:41 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id ECF0461FC4;\n\tThu, 22 Jan 2026 17:37:40 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id ACD0361F84\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 22 Jan 2026 17:37:39 +0100 (CET)","from ideasonboard.com (unknown\n\t[IPv6:2a00:6020:448c:6c00:bc8e:214:d514:699a])\n\tby perceval.ideasonboard.com (Postfix) with UTF8SMTPSA id B9312324;\n\tThu, 22 Jan 2026 17:37:06 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"GZ0W0ymc\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1769099826;\n\tbh=gyJR46LouYknLOQ8hhEK0zKKSMIMUw2NgL5HbEJPwyQ=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=GZ0W0ymcg8fEeUzWvQNNujD3oKGFXVk8lwzAuHrN0sd8NLZQW7Mn5BGGOX2vTf+w0\n\t4e9eyFLwlkb2DhSH7PCFrEsj/AK7fp1LCf3/Ce4bMPQg5a6J9CZyOuDB1SH1YLMcZ/\n\t86FJaER/Se1eRyghIltF/x9mR7piPgd0MTbDL5Rc=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20260121173737.376113-3-kieran.bingham@ideasonboard.com>","References":"<20260121173737.376113-1-kieran.bingham@ideasonboard.com>\n\t<20260121173737.376113-3-kieran.bingham@ideasonboard.com>","Subject":"Re: [PATCH v6 02/16] test: libipa: Add tests for Quantized types","From":"Stefan Klug <stefan.klug@ideasonboard.com>","Cc":"Kieran Bingham <kieran.bingham@ideasonboard.com>,\n\tIsaac Scott <isaac.scott@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>,\n\tlibcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Thu, 22 Jan 2026 17:37:35 +0100","Message-ID":"<176909985596.302817.17753496451309574776@localhost>","User-Agent":"alot/0.12.dev8+g2c003385c862.d20250602","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>"}}]