libcamera v0.8.0+1-3c8337a0-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 using FloatingType = typename Traits::FloatingType;
25
26 static_assert(std::is_integral_v<QuantizedType>,
27 "Quantized: QuantizedType must be integral");
28 static_assert(std::is_floating_point_v<FloatingType>,
29 "Quantized: FloatingType must be floating");
30
31 constexpr Quantized()
33
35 : Quantized(Traits::fromFloat(x))
36 {
37 }
38
40 : quantized_(x), value_(Traits::toFloat(x))
41 {
42 }
43
44 constexpr Quantized &operator=(float x)
45 {
46 *this = Quantized(x);
47 return *this;
48 }
49
51 {
52 *this = Quantized(x);
53 return *this;
54 }
55
56 constexpr float value() const { return value_; }
57 constexpr QuantizedType quantized() const { return quantized_; }
58
59 constexpr bool operator==(const Quantized &other) const
60 {
61 return quantized_ == other.quantized_;
62 }
63
64 constexpr bool operator!=(const Quantized &other) const
65 {
66 return !(*this == other);
67 }
68
69 friend std::ostream &operator<<(std::ostream &out,
70 const Quantized<Traits> &q)
71 {
72 out << "[" << utils::hex(q.quantized())
73 << ":" << q.value() << "]";
74
75 return out;
76 }
77
78private:
79 QuantizedType quantized_;
80 FloatingType value_;
81};
82
83} /* namespace ipa */
84
85} /* 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:69
constexpr float value() const
Retrieve the floating-point representation.
Definition quantized.h:56
typename Traits::FloatingType FloatingType
The floating type used for the value representation.
Definition quantized.h:24
constexpr QuantizedType quantized() const
Retrieve the quantized integer representation.
Definition quantized.h:57
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:59
constexpr Quantized(QuantizedType x)
Construct a Quantized value from an existing quantized integer.
Definition quantized.h:39
typename Traits::QuantizedType QuantizedType
The integer type used for the quantized representation.
Definition quantized.h:23
constexpr Quantized(FloatingType x)
Construct a Quantized value from a floating-point number.
Definition quantized.h:34
constexpr Quantized & operator=(QuantizedType x)
Assign a quantized integer value to the Quantized object.
Definition quantized.h:50
constexpr bool operator!=(const Quantized &other) const
Compare two Quantized objects for inequality.
Definition quantized.h:64
constexpr Quantized & operator=(float x)
Assign a floating-point value to the Quantized object.
Definition quantized.h:44
Miscellaneous utility functions.