libcamera v0.7.0+1595-4b6c47bd-nvm
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
quantized.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2025, Ideas On Board Oy
4 *
5 * Helper class to manage conversions between floating point types and quantized
6 * storage and representation of those values.
7 */
8
9#pragma once
10
11#include <sstream>
12#include <type_traits>
13
15
16namespace libcamera {
17
18namespace ipa {
19
20template<typename Traits>
21struct Quantized {
22 using TraitsType = Traits;
23 using QuantizedType = typename Traits::QuantizedType;
24 static_assert(std::is_arithmetic_v<QuantizedType>,
25 "Quantized: QuantizedType must be arithmetic");
26
27 constexpr Quantized()
28 : Quantized(0.0f) {}
29
30 constexpr Quantized(float x)
31 : Quantized(Traits::fromFloat(x))
32 {
33 }
34
36 : quantized_(x), value_(Traits::toFloat(x))
37 {
38 }
39
40 constexpr Quantized &operator=(float x)
41 {
42 *this = Quantized(x);
43 return *this;
44 }
45
47 {
48 *this = Quantized(x);
49 return *this;
50 }
51
52 constexpr float value() const { return value_; }
53 constexpr QuantizedType quantized() const { return quantized_; }
54
55 constexpr bool operator==(const Quantized &other) const
56 {
57 return quantized_ == other.quantized_;
58 }
59
60 constexpr bool operator!=(const Quantized &other) const
61 {
62 return !(*this == other);
63 }
64
65 friend std::ostream &operator<<(std::ostream &out,
66 const Quantized<Traits> &q)
67 {
68 out << "[" << utils::hex(q.quantized())
69 << ":" << q.value() << "]";
70
71 return out;
72 }
73
74private:
75 QuantizedType quantized_;
76 float value_;
77};
78
79} /* namespace ipa */
80
81} /* namespace libcamera */
Top-level libcamera namespace.
Definition backtrace.h:17
Wrapper that stores a value in both quantized and floating-point form.
Definition quantized.h:21
friend std::ostream & operator<<(std::ostream &out, const Quantized< Traits > &q)
Insert a text representation of a Quantized into an output stream.
Definition quantized.h:65
constexpr float value() const
Retrieve the floating-point representation.
Definition quantized.h:52
constexpr QuantizedType quantized() const
Retrieve the quantized integer representation.
Definition quantized.h:53
Traits TraitsType
The traits policy type defining the quantization behaviour.
Definition quantized.h:22
constexpr bool operator==(const Quantized &other) const
Compare two Quantized objects for equality.
Definition quantized.h:55
constexpr Quantized(QuantizedType x)
Construct a Quantized value from an existing quantized integer.
Definition quantized.h:35
typename Traits::QuantizedType QuantizedType
The integer type used for the quantized representation.
Definition quantized.h:23
constexpr Quantized(float x)
Construct a Quantized value from a floating-point number.
Definition quantized.h:30
constexpr Quantized & operator=(QuantizedType x)
Assign a quantized integer value to the Quantized object.
Definition quantized.h:46
constexpr bool operator!=(const Quantized &other) const
Compare two Quantized objects for inequality.
Definition quantized.h:60
constexpr Quantized & operator=(float x)
Assign a floating-point value to the Quantized object.
Definition quantized.h:40
Miscellaneous utility functions.