aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Transfer.cppm
Go to the documentation of this file.
1
8module;
9#include <algorithm>
10#include <cmath>
11#include <concepts>
12#include <string_view>
13export module aest.colors:transfer;
14
15import :color;
16
17namespace aest::colors
18{
20constexpr float SRGB_GAMMA = 2.4f;
21
26constexpr float REC709_ALPHA = 1.09929682680944f;
27
32constexpr float REC709_BETA = 0.018053968510807f;
33
35constexpr float REC709_SLOPE = 4.5f;
36
38constexpr float REC709_EXPONENT = 0.45f;
39
41constexpr double PQ_PEAK_NITS = 10000.0;
42
44constexpr double PQ_M1 = 2610.0 / 16384.0;
45
47constexpr double PQ_M2 = 2523.0 / 4096.0 * 128.0;
48
50constexpr double PQ_C1 = 3424.0 / 4096.0;
51
53constexpr double PQ_C2 = 2413.0 / 4096.0 * 32.0;
54
56constexpr double PQ_C3 = 2392.0 / 4096.0 * 32.0;
57
59constexpr double HLG_A = 0.17883277;
60
62constexpr double HLG_B = 1 - 4 * HLG_A;
63
68constexpr double HLG_C = 0.559910729529562;
69
75constexpr double HLG_REFERENCE_WHITE = 0.26496256042100724;
76
90export template <typename T>
91concept TransferFunction = requires(const float value) {
92 { T::PREFIX } -> std::convertible_to<std::string_view>;
93 { T::encode(value) } -> std::same_as<float>;
94 { T::decode(value) } -> std::same_as<float>;
95 { T::clamp(value) } -> std::same_as<float>;
96};
97
107export template <typename T>
108concept HDRTransfer = TransferFunction<T> && requires { requires T::IS_HDR; };
109
120export inline constexpr float REFERENCE_WHITE_NITS = 203.f;
121} // namespace aest::colors
122
124export namespace aest::colors::tf
125{
131struct Linear
132{
134 static constexpr std::string_view PREFIX = "Lin";
135
141 static constexpr float encode(const float value) { return value; }
142
148 static constexpr float decode(const float value) { return value; }
149
155 static constexpr float clamp(const float value) { return non_negative(value); }
156};
157
166struct SRGB
167{
169 static constexpr std::string_view PREFIX = "";
170
177 static float encode(const float value)
178 {
179 if (value >= 1.f)
180 return 1.f;
181
182 return value <= 0.0031308f ? 12.92f * value
183 : 1.055f * std::pow(value, 1.0f / SRGB_GAMMA) - 0.055f;
184 }
185
192 static float decode(const float value)
193 {
194 if (value == 1)
195 return 1;
196
197 return value <= 0.04045f ? value / 12.92f : std::pow((value + 0.055f) / 1.055f, SRGB_GAMMA);
198 }
199
205 static constexpr float clamp(const float value) { return colors::clamp(value); }
206};
207
222struct Rec709
223{
225 static constexpr std::string_view PREFIX = "Rec709";
226
233 static float encode(const float value)
234 {
235 if (value >= 1.f)
236 return 1.f;
237
238 return value < REC709_BETA
239 ? REC709_SLOPE * value
240 : REC709_ALPHA * std::pow(value, REC709_EXPONENT) - (REC709_ALPHA - 1.f);
241 }
242
250 static float decode(const float value)
251 {
252 if (value == 1)
253 return 1;
254
255 return value < REC709_SLOPE * REC709_BETA
256 ? value / REC709_SLOPE
257 : std::pow((value + (REC709_ALPHA - 1.f)) / REC709_ALPHA, 1.f / REC709_EXPONENT);
258 }
259
265 static constexpr float clamp(const float value) { return colors::clamp(value); }
266};
267
278struct PQ
279{
281 static constexpr std::string_view PREFIX = "PQ";
282
284 static constexpr bool IS_HDR = true;
285
292 static float encode(const float value)
293 {
294 const double y = std::pow(value * REFERENCE_WHITE_NITS / PQ_PEAK_NITS, PQ_M1);
295 return static_cast<float>(std::pow((PQ_C1 + PQ_C2 * y) / (1 + PQ_C3 * y), PQ_M2));
296 }
297
304 static float decode(const float value)
305 {
306 const double power = std::pow(static_cast<double>(value), 1 / PQ_M2);
307 const double ratio = std::max(power - PQ_C1, 0.0) / (PQ_C2 - PQ_C3 * power);
308 return static_cast<float>(PQ_PEAK_NITS / REFERENCE_WHITE_NITS * std::pow(ratio, 1 / PQ_M1));
309 }
310
316 static constexpr float clamp(const float value) { return colors::clamp(value); }
317};
318
330struct HLG
331{
333 static constexpr std::string_view PREFIX = "HLG";
334
336 static constexpr bool IS_HDR = true;
337
344 static float encode(const float value)
345 {
346 const double scene = value * HLG_REFERENCE_WHITE;
347 return static_cast<float>(scene <= 1.0 / 12 ? std::sqrt(3 * scene)
348 : HLG_A * std::log(12 * scene - HLG_B) + HLG_C);
349 }
350
357 static float decode(const float value)
358 {
359 const double signal = value;
360 const double scene =
361 signal <= 0.5 ? signal * signal / 3 : (std::exp((signal - HLG_C) / HLG_A) + HLG_B) / 12;
362 return static_cast<float>(scene / HLG_REFERENCE_WHITE);
363 }
364
370 static constexpr float clamp(const float value) { return colors::clamp(value); }
371};
372} // namespace aest::colors::tf
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
Definition Color.cppm:29
constexpr float non_negative(const float value)
Clamp a floating-point value to be non-negative, leaving it unbounded above.
Definition Color.cppm:39
constexpr double PQ_C2
PQ constant c2 = 2413 / 4096 * 32, from SMPTE ST 2084.
Definition Transfer.cppm:53
constexpr float SRGB_GAMMA
Exponent of the sRGB transfer function.
Definition Transfer.cppm:20
constexpr double PQ_M1
PQ constant m1 = 2610 / 16384, from SMPTE ST 2084.
Definition Transfer.cppm:44
constexpr double PQ_C3
PQ constant c3 = 2392 / 4096 * 32, from SMPTE ST 2084.
Definition Transfer.cppm:56
constexpr double HLG_REFERENCE_WHITE
Scene light that HLG encodes to 75%, its BT.2408 reference white: (exp((0.75 - c) / a) + b) / 12.
Definition Transfer.cppm:75
constexpr float REC709_BETA
Linear light beta where the Rec. 709 OETF turns from its linear segment to its power curve; BT....
Definition Transfer.cppm:32
constexpr float REFERENCE_WHITE_NITS
Luminance of linear 1.0 in cd/m²: the HDR reference white of ITU-R BT.2408, and the scale of every li...
constexpr float REC709_ALPHA
Offset alpha of the Rec. 709 OETF, solved so that its two segments meet; ITU-R BT....
Definition Transfer.cppm:26
constexpr double PQ_PEAK_NITS
Luminance of the PQ signal 1.0 in cd/m², the peak of SMPTE ST 2084.
Definition Transfer.cppm:41
constexpr double HLG_A
HLG constant a, from ITU-R BT.2100.
Definition Transfer.cppm:59
constexpr float REC709_EXPONENT
Exponent of the power curve of the Rec. 709 OETF.
Definition Transfer.cppm:38
constexpr double PQ_C1
PQ constant c1 = 3424 / 4096, from SMPTE ST 2084.
Definition Transfer.cppm:50
constexpr double PQ_M2
PQ constant m2 = 2523 / 4096 * 128, from SMPTE ST 2084.
Definition Transfer.cppm:47
constexpr double HLG_B
HLG constant b = 1 - 4a, from ITU-R BT.2100.
Definition Transfer.cppm:62
constexpr float REC709_SLOPE
Slope of the linear segment of the Rec. 709 OETF.
Definition Transfer.cppm:35
constexpr double HLG_C
HLG constant c = 0.5 - a ln(4a), from ITU-R BT.2100.
Definition Transfer.cppm:68
A transfer function for HDR signals, such as PQ or HLG: a tag with a static IS_HDR set to true....
A transfer function tag, the second parameter of BasicRGB.
Definition Transfer.cppm:91
Transfer function tags, the second parameter of BasicRGB.
The Hybrid Log-Gamma OETF of ITU-R BT.2100 and ARIB STD-B67, the relative HDR transfer function of br...
static constexpr std::string_view PREFIX
Prefix of the color space name.
static float encode(const float value)
Encodes linear light into an HLG signal with the OETF.
static float decode(const float value)
Decodes an HLG signal to linear light with the inverse OETF.
static constexpr float clamp(const float value)
Brings a signal value into the encoded range.
static constexpr bool IS_HDR
HLG is an HDR signal.
No transfer function: channel values are proportional to light.
static constexpr float clamp(const float value)
Brings a channel value into the range of linear light.
static constexpr float decode(const float value)
Decodes a channel value to linear light, leaving it unchanged.
static constexpr float encode(const float value)
Encodes linear light, which this function leaves unchanged.
static constexpr std::string_view PREFIX
Prefix of the color space name.
The Perceptual Quantizer of SMPTE ST 2084 and ITU-R BT.2100, the absolute HDR transfer function of HD...
static constexpr bool IS_HDR
PQ is an HDR signal.
static float encode(const float value)
Encodes linear light into a PQ signal.
static float decode(const float value)
Decodes a PQ signal to linear light.
static constexpr std::string_view PREFIX
Prefix of the color space name.
static constexpr float clamp(const float value)
Brings a signal value into the encoded range.
The OETF of ITU-R BT.709, which BT.2020 shares for SDR: a linear segment near black,...
static constexpr std::string_view PREFIX
Prefix of the color space name.
static constexpr float clamp(const float value)
Brings a signal value into the encoded range.
static float encode(const float value)
Encodes linear scene light into a Rec. 709 signal with the OETF.
static float decode(const float value)
Decodes a Rec. 709 signal to linear scene light with the inverse OETF.
The sRGB transfer function of IEC 61966-2-1: a short linear segment near black, then a power curve wi...
static float decode(const float value)
Decodes an sRGB channel value to linear light.
static constexpr std::string_view PREFIX
Prefix of the color space name: none, encoded spaces go by the gamut name.
static constexpr float clamp(const float value)
Brings a channel value into the encoded range.
static float encode(const float value)
Encodes linear light into an sRGB channel value.