1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QCOLORTRANSFERGENERIC_P_H
5#define QCOLORTRANSFERGENERIC_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtGui/private/qtguiglobal_p.h>
19
20#include <cmath>
21
22QT_BEGIN_NAMESPACE
23
24// Defines the a generic transfer function for our HDR functions
25class QColorTransferGenericFunction
26{
27public:
28 using ConverterPtr = float (*)(float);
29 constexpr QColorTransferGenericFunction(ConverterPtr toLinear = nullptr, ConverterPtr fromLinear = nullptr) noexcept
30 : m_toLinear(toLinear), m_fromLinear(fromLinear)
31 {}
32
33 static QColorTransferGenericFunction hlg()
34 {
35 return QColorTransferGenericFunction(hlgToLinear, hlgFromLinear);
36 }
37 static QColorTransferGenericFunction pq()
38 {
39 return QColorTransferGenericFunction(pqToLinear, pqFromLinear);
40 }
41
42 float apply(float x) const
43 {
44 return m_toLinear(x);
45 }
46
47 float applyInverse(float x) const
48 {
49 return m_fromLinear(x);
50 }
51
52 bool operator==(const QColorTransferGenericFunction &o) const noexcept
53 {
54 return m_toLinear == o.m_toLinear && m_fromLinear == o.m_fromLinear;
55 }
56 bool operator!=(const QColorTransferGenericFunction &o) const noexcept
57 {
58 return m_toLinear != o.m_toLinear || m_fromLinear != o.m_fromLinear;
59 }
60
61private:
62 ConverterPtr m_toLinear = nullptr;
63 ConverterPtr m_fromLinear = nullptr;
64
65 // HLG from linear [0-12] -> [0-1]
66 static float hlgFromLinear(float x)
67 {
68 x = std::clamp(val: x, lo: 0.f, hi: 12.f);
69 if (x > 1.f)
70 return m_hlg_a * std::log(x: x - m_hlg_b) + m_hlg_c;
71 return std::sqrt(x: x * 0.25f);
72 }
73
74 // HLG to linear [0-1] -> [0-12]
75 static float hlgToLinear(float x)
76 {
77 x = std::clamp(val: x, lo: 0.f, hi: 1.f);
78 if (x < 0.5f)
79 return (x * x) * 4.f;
80 return std::exp(x: (x - m_hlg_c) / m_hlg_a) + m_hlg_b;
81 }
82
83 constexpr static float m_hlg_a = 0.17883277f;
84 constexpr static float m_hlg_b = 1.f - (4.f * m_hlg_a);
85 constexpr static float m_hlg_c = 0.55991073f; // 0.5 - a * ln(4 * a)
86
87 // BT.2100-2 Reference PQ EOTF and inverse (see Table 4)
88 // PQ to linear [0-1] -> [0-64]
89 static float pqToLinear(float e)
90 {
91 e = std::clamp(val: e, lo: 0.f, hi: 1.f);
92 // m2-th root of E'
93 const float eRoot = std::pow(x: e, y: 1.f / m_pq_m2);
94 // rational transform
95 const float yBase = (std::max)(a: eRoot - m_pq_c1, b: 0.0f) / (m_pq_c2 - m_pq_c3 * eRoot);
96 // calculate Y = yBase^(1/m1)
97 const float y = std::pow(x: yBase, y: 1.f / m_pq_m1);
98 // scale Y to Fd
99 return y * m_pq_f;
100 }
101
102 // PQ from linear [0-64] -> [0-1]
103 static float pqFromLinear(float fd)
104 {
105 fd = std::clamp(val: fd, lo: 0.f, hi: 64.f);
106 // scale Fd to Y
107 const float y = fd * (1.f / m_pq_f);
108 // yRoot = Y^m1 -- "root" because m1 is <1
109 const float yRoot = std::pow(x: y, y: m_pq_m1);
110 // rational transform
111 const float eBase = (m_pq_c1 + m_pq_c2 * yRoot) / (1.f + m_pq_c3 * yRoot);
112 // calculate E' = eBase^m2
113 return std::pow(x: eBase, y: m_pq_m2);
114 }
115
116 constexpr static float m_pq_c1 = 107.f / 128.f; // c3 - c2 + 1
117 constexpr static float m_pq_c2 = 2413.f / 128.f;
118 constexpr static float m_pq_c3 = 2392.f / 128.f;
119 constexpr static float m_pq_m1 = 1305.f / 8192.f;
120 constexpr static float m_pq_m2 = 2523.f / 32.f;
121 constexpr static float m_pq_f = 64.f; // This might need to be set based on scene metadata
122};
123
124QT_END_NAMESPACE
125
126#endif // QCOLORTRANSFERGENERIC_P_H
127

source code of qtbase/src/gui/painting/qcolortransfergeneric_p.h