From patchwork Sun Oct 26 23:30:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 24825 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id EB980BE080 for ; Sun, 26 Oct 2025 23:31:01 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 43F4F606F5; Mon, 27 Oct 2025 00:30:58 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="rlxMtj8E"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 3EDB6606E6 for ; Mon, 27 Oct 2025 00:30:54 +0100 (CET) Received: from charm.hippo-penny.ts.net (unknown [209.38.108.23]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 821B9E9B; Mon, 27 Oct 2025 00:29:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1761521346; bh=CenjjyPxspM0Qea0KDApsn4fVOkAyGFz8PmOvbVj8Oo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rlxMtj8EstVSPg9Xwpf8lhRj7U6htgnTdEBXI+CpPgzC4Eyr2CcCvibLpzokPxamv UALgI8Ewpg54LBPu4kHX7xKmW6HOdlmWAxKW5+fWEypxP6xwpxUoP821p38BIvcrqk JwktoSgCRaaQSbf3HaN3ppz+LkE8FCuDgwMCx+MY= From: Kieran Bingham To: libcamera devel Cc: Kieran Bingham Subject: [PATCH 2/6] test: libipa: Add tests for Quantizers Date: Sun, 26 Oct 2025 23:30:39 +0000 Message-ID: <20251026233048.175689-3-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251026233048.175689-1-kieran.bingham@ideasonboard.com> References: <20251026233048.175689-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Signed-off-by: Kieran Bingham --- test/ipa/libipa/meson.build | 1 + test/ipa/libipa/quantized.cpp | 149 ++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 test/ipa/libipa/quantized.cpp diff --git a/test/ipa/libipa/meson.build b/test/ipa/libipa/meson.build index 2070bed70222..c3e255871f4f 100644 --- a/test/ipa/libipa/meson.build +++ b/test/ipa/libipa/meson.build @@ -5,6 +5,7 @@ libipa_test = [ {'name': 'histogram', 'sources': ['histogram.cpp']}, {'name': 'interpolator', 'sources': ['interpolator.cpp']}, {'name': 'pwl', 'sources': ['pwl.cpp'] }, + {'name': 'quantized', 'sources': ['quantized.cpp']}, ] foreach test : libipa_test diff --git a/test/ipa/libipa/quantized.cpp b/test/ipa/libipa/quantized.cpp new file mode 100644 index 000000000000..51db62a1b88a --- /dev/null +++ b/test/ipa/libipa/quantized.cpp @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2025, Ideas on Board + * + * Dual Type and Quantizer tests + */ + +#include "../src/ipa/libipa/quantized.h" + +#include +#include +#include +#include +#include + +#include "test.h" + +using namespace std; +using namespace libcamera; +using namespace ipa; + +class BrightnessHueQuantizer : public Quantizer +{ +public: + BrightnessHueQuantizer(float val) { set(val); } + BrightnessHueQuantizer(int8_t val) { set(val); } + + int8_t fromFloat(float v) const override + { + int quantized = std::lround(v * 128.0f); + return static_cast(std::clamp(quantized, -128, 127)); + } + + float toFloat(int8_t v) const override + { + return static_cast(v) / 128.0f; + } +}; + +class ContrastSaturationQuantizer : public Quantizer +{ +public: + ContrastSaturationQuantizer(float val) { set(val); } + ContrastSaturationQuantizer(uint8_t val) { set(val); } + + uint8_t fromFloat(float v) const override + { + int quantized = std::lround(v * 128.0f); + return static_cast(std::clamp(quantized, 0, 255)); + } + + float toFloat(uint8_t v) const override + { + return static_cast(v) / 128.0f; + } +}; + +using BrightnessQ = BrightnessHueQuantizer; +using HueQ = BrightnessHueQuantizer; +using ContrastQ = ContrastSaturationQuantizer; +using SaturationQ = ContrastSaturationQuantizer; + +class QuantizedTest : public Test +{ +protected: + int run() + { + /* Test construction from float */ + { + BrightnessQ b(0.5f); + if (b.quantized() != 64 || std::abs(b.value() - 0.5f) > 0.01f) + return TestFail; + } + + /* Test construction from T */ + { + ContrastQ c(uint8_t(128)); + if (c.quantized() != 128 || std::abs(c.value() - 1.0f) > 0.01f) + return TestFail; + } + + /* + * Test construction from non-float/non-T type (Expected Fail) + * These should be a compile-time error if uncommented: + */ + // BrightnessQ bad1(15); // overloaded ‘BrightnessHueQuantizer(int)’ is ambiguous + // BrightnessQ bad2(0xff); // overloaded ‘BrightnessHueQuantizer(int)’ is ambiguous + // ContrastQ bad3(0x33); // overloaded ‘ContrastSaturationQuantizer(int)’ is ambiguous + // ContrastQ bad4(55U); // overloaded ‘ContrastSaturationQuantizer(unsigned int)’ is ambiguous + + /* Test equality */ + { + BrightnessQ b1(0.5f), b2((int8_t)64); + if (!(b1 == b2)) + return TestFail; + } + + /* Test inequality */ + { + BrightnessQ b1(0.5f), b2(-0.5f); + if (b1 == b2) + return TestFail; + } + + /* Test copying */ + { + BrightnessQ b1(0.25f); + BrightnessQ b2 = b1; + if (!(b1 == b2)) + return TestFail; + } + + /* Test moving */ + { + BrightnessQ b1(0.25f); + BrightnessQ b2 = std::move(b1); // Allow move semantics + if (b2.value() != 0.25f) + return TestFail; + } + + /* Test assignment */ + { + ContrastQ c1(1.5f); + ContrastQ c2(0.0f); + c2 = c1; + if (!(c1 == c2)) + return TestFail; + } + + /* Test assignment / storage in generic type */ + { + ContrastQ c(1.5f); + Quantized q; + + q = c; + if (q.value() != c.value()) + return TestFail; + + if (q.quantized() != c.quantized()) + return TestFail; + } + + std::cout << "Quantised tests passed successfully." << std::endl; + + return TestPass; + } +}; + +TEST_REGISTER(QuantizedTest)