Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcolortransfergeneric_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5#ifndef QCOLORTRANSFERGENERIC_P_H
6#define QCOLORTRANSFERGENERIC_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtGui/private/qtguiglobal_p.h>
20
21#include <cmath>
22
24
25// Defines the a generic transfer function for our HDR functions
27{
28public:
29 using ConverterPtr = float (*)(float);
30 constexpr QColorTransferGenericFunction(ConverterPtr toLinear = nullptr, ConverterPtr fromLinear = nullptr) noexcept
31 : m_toLinear(toLinear), m_fromLinear(fromLinear)
32 {}
33
35 {
36 return QColorTransferGenericFunction(hlgToLinear, hlgFromLinear);
37 }
39 {
40 return QColorTransferGenericFunction(pqToLinear, pqFromLinear);
41 }
42
43 float apply(float x) const
44 {
45 return m_toLinear(x);
46 }
47
48 float applyInverse(float x) const
49 {
50 return m_fromLinear(x);
51 }
52
53 bool operator==(const QColorTransferGenericFunction &o) const noexcept
54 {
55 return m_toLinear == o.m_toLinear && m_fromLinear == o.m_fromLinear;
56 }
57 bool operator!=(const QColorTransferGenericFunction &o) const noexcept
58 {
59 return m_toLinear != o.m_toLinear || m_fromLinear != o.m_fromLinear;
60 }
61
62private:
63 ConverterPtr m_toLinear = nullptr;
64 ConverterPtr m_fromLinear = nullptr;
65
66 // HLG from linear [0-12] -> [0-1]
67 static float hlgFromLinear(float x)
68 {
69 x = std::clamp(x, 0.f, 12.f);
70 if (x > 1.f)
71 return m_hlg_a * std::log(x - m_hlg_b) + m_hlg_c;
72 return std::sqrt(x * 0.25f);
73 }
74
75 // HLG to linear [0-1] -> [0-12]
76 static float hlgToLinear(float x)
77 {
78 x = std::clamp(x, 0.f, 1.f);
79 if (x < 0.5f)
80 return (x * x) * 4.f;
81 return std::exp((x - m_hlg_c) / m_hlg_a) + m_hlg_b;
82 }
83
84 constexpr static float m_hlg_a = 0.17883277f;
85 constexpr static float m_hlg_b = 1.f - (4.f * m_hlg_a);
86 constexpr static float m_hlg_c = 0.55991073f; // 0.5 - a * ln(4 * a)
87
88 // BT.2100-2 Reference PQ EOTF and inverse (see Table 4)
89 // PQ to linear [0-1] -> [0-64]
90 static float pqToLinear(float e)
91 {
92 e = std::clamp(e, 0.f, 1.f);
93 // m2-th root of E'
94 const float eRoot = std::pow(e, 1.f / m_pq_m2);
95 // rational transform
96 const float yBase = (std::max)(eRoot - m_pq_c1, 0.0f) / (m_pq_c2 - m_pq_c3 * eRoot);
97 // calculate Y = yBase^(1/m1)
98 const float y = std::pow(yBase, 1.f / m_pq_m1);
99 // scale Y to Fd
100 return y * m_pq_f;
101 }
102
103 // PQ from linear [0-64] -> [0-1]
104 static float pqFromLinear(float fd)
105 {
106 fd = std::clamp(fd, 0.f, 64.f);
107 // scale Fd to Y
108 const float y = fd * (1.f / m_pq_f);
109 // yRoot = Y^m1 -- "root" because m1 is <1
110 const float yRoot = std::pow(y, m_pq_m1);
111 // rational transform
112 const float eBase = (m_pq_c1 + m_pq_c2 * yRoot) / (1.f + m_pq_c3 * yRoot);
113 // calculate E' = eBase^m2
114 return std::pow(eBase, m_pq_m2);
115 }
116
117 constexpr static float m_pq_c1 = 107.f / 128.f; // c3 - c2 + 1
118 constexpr static float m_pq_c2 = 2413.f / 128.f;
119 constexpr static float m_pq_c3 = 2392.f / 128.f;
120 constexpr static float m_pq_m1 = 1305.f / 8192.f;
121 constexpr static float m_pq_m2 = 2523.f / 32.f;
122 constexpr static float m_pq_f = 64.f; // This might need to be set based on scene metadata
123};
124
125QT_END_NAMESPACE
126
127#endif // QCOLORTRANSFERGENERIC_P_H
QList< QColorVector > table
uint32_t gridPointsY
uint32_t gridPointsW
bool isEmpty() const
uint32_t gridPointsX
uint32_t gridPointsZ
QColorMatrix inverted() const
static QColorMatrix toXyzFromSRgb()
static QColorMatrix toXyzFromAdobeRgb()
bool isValid() const
friend bool comparesEqual(const QColorMatrix &lhs, const QColorMatrix &rhs) noexcept
QColorVector g
constexpr float determinant() const
static QColorMatrix identity()
QColorMatrix transposed() const
static QColorMatrix fromScale(QColorVector v)
friend constexpr QColorMatrix operator*(const QColorMatrix &a, const QColorMatrix &o)
QColorVector b
QColorVector r
bool isIdentity() const noexcept
static QColorMatrix toXyzFromDciP3D65()
static QColorMatrix chromaticAdaptation(const QColorVector &whitePoint)
static QColorMatrix toXyzFromProPhotoRgb()
QColorVector map(const QColorVector &c) const
static QColorMatrix toXyzFromBt2020()
constexpr bool isNull() const
void setTransferFunctionTables(const QList< uint16_t > &redTransferFunctionTable, const QList< uint16_t > &greenTransferFunctionTable, const QList< uint16_t > &blueTransferFunctionTable)
QColorSpacePrivate(QPointF whitePoint, const QList< uint16_t > &transferFunctionTable)
QColorVector whitePoint
QColorSpacePrivate(QPointF whitePoint, QColorSpace::TransferFunction transferFunction, float gamma)
QColorSpacePrivate(const QColorSpace::PrimaryPoints &primaries, const QList< uint16_t > &transferFunctionTable)
QColorTransform transformationToColorSpace(const QColorSpacePrivate *out) const
QList< Element > mAB
QList< Element > mBA
void clearElementListProcessingForEdit()
QColorMatrix chad
void setTransferFunctionTable(const QList< uint16_t > &transferFunctionTable)
QColorTransform transformationToXYZ() const
bool isThreeComponentMatrix() const
QColorSpacePrivate(const QColorSpace::PrimaryPoints &primaries, const QList< uint16_t > &redTransferFunctionTable, const QList< uint16_t > &greenTransferFunctionTable, const QList< uint16_t > &blueRransferFunctionTable)
QColorSpacePrivate(const QColorSpacePrivate &other)=default
static const QColorSpacePrivate * get(const QColorSpace &colorSpace)
QColorSpacePrivate(const QColorSpace::PrimaryPoints &primaries, QColorSpace::TransferFunction transferFunction, float gamma)
static QColorSpacePrivate * get(QColorSpace &colorSpace)
bool isValid() const noexcept
QColorMatrix toXyz
QColorSpacePrivate(QColorSpace::NamedColorSpace namedColorSpace)
bool equals(const QColorSpacePrivate *other) const
Q_DECLARE_FLAGS(Hints, Hint)
QColorTransferFunction inverted() const
friend bool operator!=(const QColorTransferFunction &f1, const QColorTransferFunction &f2)
QColorTransferFunction(float a, float b, float c, float d, float e, float f, float g) noexcept
static QColorTransferFunction fromGamma(float gamma)
bool matches(const QColorTransferFunction &o) const
static QColorTransferFunction fromBt2020()
friend bool operator==(const QColorTransferFunction &f1, const QColorTransferFunction &f2)
static QColorTransferFunction fromProPhotoRgb()
static QColorTransferFunction fromSRgb()
static QColorTransferGenericFunction pq()
static QColorTransferGenericFunction hlg()
bool operator==(const QColorTransferGenericFunction &o) const noexcept
bool operator!=(const QColorTransferGenericFunction &o) const noexcept
constexpr QColorTransferGenericFunction(ConverterPtr toLinear=nullptr, ConverterPtr fromLinear=nullptr) noexcept
The QColorTransform class is a transformation between color spaces.
Combined button and popup list for selecting options.
bool comparesEqual(const QColorVector &v1, const QColorVector &v2) noexcept
static void cleanupPredefinedColorspaces()
static bool compareElement(const QColorSpacePrivate::TransferElement &element, const QColorSpacePrivate::TransferElement &other)
QDebug operator<<(QDebug dbg, const QColorCLUT &)
QDebug operator<<(QDebug dbg, const QColorSpace &colorSpace)
QDebug operator<<(QDebug dbg, const QColorMatrix &)
static bool compareElement(const QColorMatrix &element, const QColorMatrix &other)
QDataStream & operator>>(QDataStream &s, QColorSpace &colorSpace)
QDataStream & operator<<(QDataStream &s, const QColorSpace &image)
QDebug operator<<(QDebug dbg, const QColorVector &)
QColorMatrix qColorSpacePrimaryPointsToXyzMatrix(const QColorSpace::PrimaryPoints &primaries)
static bool compareElements(const T &element, const QColorSpacePrivate::Element &other)
static bool compareElement(const QColorCLUT &element, const QColorCLUT &other)
static bool compareElement(const QColorVector &element, const QColorVector &other)
QDebug operator<<(QDebug dbg, const QColorSpacePrivate::TransferElement &)
QT_BEGIN_NAMESPACE bool qColorSpacePrimaryPointsAreValid(const QColorSpace::PrimaryPoints &primaries)
Q_DECLARE_OPERATORS_FOR_FLAGS(QColorTransferFunction::Hints)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
LUT(const LUT &other)
const std::shared_ptr< QColorTrcLut > & operator[](int i) const
std::shared_ptr< QColorTrcLut > & operator[](int i)
std::shared_ptr< QColorTrcLut > table[3]