libcamera v0.7.2+rpt20260817
Supporting cameras in Linux since 2019
Loading...
Searching...
No Matches
awb.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2024 Ideas on Board Oy
4 *
5 * libIPA AWB algorithms
6 */
7
8#pragma once
9
10#include <array>
11#include <map>
12#include <optional>
13
15#include <libcamera/controls.h>
16
19
20namespace libcamera {
21
22namespace ipa {
23
24namespace awb {
25
26struct Context {
28 unsigned int colourTemperature;
29};
30
37
38struct FrameContext : public Context {
40};
41
42} /* namespace awb */
43
44struct AwbStats {
45 virtual double computeColourError(const RGB<double> &gains) const = 0;
46 virtual bool valid() const = 0;
47 virtual RGB<double> rgbMeans() const = 0;
48
49protected:
50 ~AwbStats() = default;
51};
52
54{
55public:
56 struct Result {
58 unsigned int colourTemperature;
59 };
60
61 virtual ~AwbImplementation() = default;
62 virtual int init(const ValueNode &tuningData) = 0;
63 virtual Result calculateAwb(const AwbStats &stats, unsigned int lux,
64 std::array<double, 2> range) = 0;
65 virtual std::optional<RGB<double>>
66 gainsFromColourTemperature(double colourTemperature) = 0;
67};
68
70{
71public:
72 int init(const ValueNode &tuningData);
73
74 int configure(awb::ActiveState &state);
75
77 const uint32_t frame,
78 awb::FrameContext &frameContext,
79 const ControlList &controls);
80
81 void prepare(awb::ActiveState &state, awb::FrameContext &frameContext);
82
83 void process(awb::ActiveState &state, awb::FrameContext &frameContext,
84 const AwbStats &stats, unsigned int lux,
85 ControlList &metadata);
86
87protected:
88 AwbAlgorithmBase() = default;
89
91 float gainMin_;
92 float gainMax_;
93
94private:
95 struct ModeConfig {
96 double ctHi;
97 double ctLo;
98 };
99
100 /* AwbGrey does not support modes; */
101 static constexpr ModeConfig AwbGreyMode = { 0.0, 0.0 };
102
103 int parseModeConfigs(const ValueNode &tuningData,
104 const ControlValue &def = {});
105
106 std::map<controls::AwbModeEnum, AwbAlgorithmBase::ModeConfig> modes_;
107 const ModeConfig *currentMode_ = nullptr;
108 std::unique_ptr<AwbImplementation> impl_;
109 bool bayes_ = false;
110};
111
112template<typename Q>
114{
115public:
116 int init(const ValueNode &tuningData, ControlInfoMap::Map &controls)
117 {
118 AwbAlgorithmBase::init(tuningData);
119
120 gainMin_ = std::max(Q::TraitsType::min, 1.0f);
121 gainMax_ = Q::TraitsType::max;
122
125 Span<const float, 2>{ { 1.0f, 1.0f } });
126
127 controls.insert(controls_.begin(), controls_.end());
128
129 return 0;
130 }
131};
132
133} /* namespace ipa */
134
135} /* namespace libcamera */
std::unordered_map< const ControlId *, ControlInfo > Map
The base std::unsorted_map<> container.
Definition controls.h:367
Describe the limits of valid values for a Control.
Definition controls.h:330
Associate a list of ControlId with their values for an object.
Definition controls.h:409
Abstract type representing the value of a control.
Definition controls.h:134
A class representing a tree structure of values.
Definition value_node.h:27
Vector class.
Definition vector.h:35
Base class for AwbAlgorithm for non-templated functions implementation.
Definition awb.h:70
void prepare(awb::ActiveState &state, awb::FrameContext &frameContext)
Set the gains and colour temperature values in frameContext.
Definition awb.cpp:350
float gainMax_
The maximum supported gain value.
Definition awb.h:92
ControlInfoMap::Map controls_
Controls info map for the controls registered by the AWB algorithm.
Definition awb.h:90
void process(awb::ActiveState &state, awb::FrameContext &frameContext, const AwbStats &stats, unsigned int lux, ControlList &metadata)
Process AWB statistics to calculate gains and populate metadata.
Definition awb.cpp:370
int init(const ValueNode &tuningData)
Initialize the algorithm with the given tuning data.
Definition awb.cpp:213
void queueRequest(awb::ActiveState &state, const uint32_t frame, awb::FrameContext &frameContext, const ControlList &controls)
Queue a Request to the AWB algorithm.
Definition awb.cpp:282
int configure(awb::ActiveState &state)
Configure the AWB algorithm.
Definition awb.cpp:250
float gainMin_
The minimum supported gain value.
Definition awb.h:91
The libipa AWB algorithm.
Definition awb.h:114
int init(const ValueNode &tuningData, ControlInfoMap::Map &controls)
Definition awb.h:116
Pure virtual base class for AWB algorithms implementations.
Definition awb.h:54
virtual int init(const ValueNode &tuningData)=0
Initialize the algorithm by parsing tuningData.
virtual Result calculateAwb(const AwbStats &stats, unsigned int lux, std::array< double, 2 > range)=0
Calculate AWB data from the given statistics.
virtual ~AwbImplementation()=default
Virtual class destructor.
virtual std::optional< RGB< double > > gainsFromColourTemperature(double colourTemperature)=0
Compute white balance gains from a colour temperature.
Camera controls identifiers.
Framework to manage controls related to an object.
const Control< Span< const float, 2 > > ColourGains
Pair of gain values for the Red and Blue colour channels, in that order.
Top-level libcamera namespace.
Definition backtrace.h:17
The result of an AWB computation.
Definition awb.h:56
RGB< double > gains
The computed white balance gains.
Definition awb.h:57
unsigned int colourTemperature
The computed colour temperature, in Kelvin.
Definition awb.h:58
An abstraction class wrapping hardware-specific AWB statistics.
Definition awb.h:44
virtual RGB< double > rgbMeans() const =0
Get RGB means of the statistics.
virtual double computeColourError(const RGB< double > &gains) const =0
Virtual class destructor.
virtual bool valid() const =0
Retrieve if the AWB statistics are valid.
Active AWB state shared across frames.
Definition awb.h:31
Context manual
The most recent manually requested AWB state.
Definition awb.h:32
bool autoEnabled
True when automatic AWB is selected.
Definition awb.h:35
Context automatic
The most recent automatically calculated AWB state.
Definition awb.h:33
AWB gains and colour temperature.
Definition awb.h:26
unsigned int colourTemperature
The colour temperature, in Kelvin.
Definition awb.h:28
RGB< double > gains
The white balance gains.
Definition awb.h:27
Per-frame AWB state.
Definition awb.h:38
bool autoEnabled
True when automatic AWB is in use.
Definition awb.h:39
Data structure to manage tree of values.
Vector class.