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
qcolortransfertable_p.h
Go to the documentation of this file.
1// Copyright (C) 2018 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 QCOLORTRANSFERTABLE_P_H
6#define QCOLORTRANSFERTABLE_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>
21
22#include <QList>
23
24#include <algorithm>
25#include <cmath>
26
28
29// Defines either an ICC TRC 'curve' or a lut8/lut16 A or B table
31{
32public:
33 enum Type : uint8_t {
34 TwoWay = 0,
35 OneWay,
36 };
37 QColorTransferTable() noexcept = default;
38 QColorTransferTable(uint32_t size, const QList<uint8_t> &table, Type type = TwoWay) noexcept
39 : m_type(type), m_tableSize(size), m_table8(table)
40 {
41 Q_ASSERT(qsizetype(size) <= table.size());
42 }
43 QColorTransferTable(uint32_t size, const QList<uint16_t> &table, Type type = TwoWay) noexcept
44 : m_type(type), m_tableSize(size), m_table16(table)
45 {
46 Q_ASSERT(qsizetype(size) <= table.size());
47 }
48
49 bool isEmpty() const noexcept
50 {
51 return m_tableSize == 0;
52 }
53
54 bool isIdentity() const
55 {
56 if (isEmpty())
57 return true;
58 if (m_tableSize != 2)
59 return false;
60 if (!m_table8.isEmpty())
61 return m_table8[0] == 0 && m_table8[1] == 255;
62 return m_table16[0] == 0 && m_table16[1] == 65535;
63 }
64
65 bool checkValidity() const
66 {
67 if (isEmpty())
68 return true;
69 // Only one table can be set
70 if (!m_table8.isEmpty() && !m_table16.isEmpty())
71 return false;
72 // At least 2 elements
73 if (m_tableSize < 2)
74 return false;
75 return (m_type == OneWay) || checkInvertibility();
76 }
77 bool checkInvertibility() const
78 {
79 // The two-way tables must describe an injective curve:
80 if (!m_table8.isEmpty()) {
81 uint8_t val = 0;
82 for (uint i = 0; i < m_tableSize; ++i) {
83 if (m_table8[i] < val)
84 return false;
85 val = m_table8[i];
86 }
87 }
88 if (!m_table16.isEmpty()) {
89 uint16_t val = 0;
90 for (uint i = 0; i < m_tableSize; ++i) {
91 if (m_table16[i] < val)
92 return false;
93 val = m_table16[i];
94 }
95 }
96 return true;
97 }
98
99 float apply(float x) const
100 {
101 if (isEmpty())
102 return x;
103 x = std::clamp(x, 0.0f, 1.0f);
104 x *= m_tableSize - 1;
105 const uint32_t lo = static_cast<uint32_t>(x);
106 const uint32_t hi = std::min(lo + 1, m_tableSize - 1);
107 const float frac = x - lo;
108 if (!m_table16.isEmpty())
109 return (m_table16[lo] + (m_table16[hi] - m_table16[lo]) * frac) * (1.0f/65535.0f);
110 if (!m_table8.isEmpty())
111 return (m_table8[lo] + (m_table8[hi] - m_table8[lo]) * frac) * (1.0f/255.0f);
112 return x;
113 }
114
115 // Apply inverse, optimized by giving a previous result for a value < x.
116 float applyInverse(float x, float resultLargerThan = 0.0f) const
117 {
118 Q_ASSERT(resultLargerThan >= 0.0f && resultLargerThan <= 1.0f);
119 Q_ASSERT(m_type == TwoWay);
120 if (x <= 0.0f)
121 return 0.0f;
122 if (x >= 1.0f)
123 return 1.0f;
124 if (!m_table16.isEmpty())
125 return inverseLookup(x * 65535.0f, resultLargerThan, m_table16, m_tableSize - 1);
126 if (!m_table8.isEmpty())
127 return inverseLookup(x * 255.0f, resultLargerThan, m_table8, m_tableSize - 1);
128 return x;
129 }
130
131 bool asColorTransferFunction(QColorTransferFunction *transferFn)
132 {
133 Q_ASSERT(transferFn);
134 if (isEmpty()) {
135 *transferFn = QColorTransferFunction();
136 return true;
137 }
138 if (m_tableSize < 2)
139 return false;
140 if (!m_table8.isEmpty() && (m_table8[0] != 0 || m_table8[m_tableSize - 1] != 255))
141 return false;
142 if (!m_table16.isEmpty() && (m_table16[0] != 0 || m_table16[m_tableSize - 1] != 65535))
143 return false;
144 if (m_tableSize == 2) {
145 *transferFn = QColorTransferFunction(); // Linear
146 return true;
147 }
148 // The following heuristics are based on those from Skia:
149 if (m_tableSize == 26 && !m_table16.isEmpty()) {
150 // code.facebook.com/posts/411525055626587/under-the-hood-improving-facebook-photos
151 if (m_table16[6] != 3062)
152 return false;
153 if (m_table16[12] != 12824)
154 return false;
155 if (m_table16[18] != 31237)
156 return false;
157 *transferFn = QColorTransferFunction::fromSRgb();
158 return true;
159 }
160 if (m_tableSize == 1024 && !m_table16.isEmpty()) {
161 // HP and Canon sRGB gamma tables:
162 if (m_table16[257] != 3366)
163 return false;
164 if (m_table16[513] != 14116)
165 return false;
166 if (m_table16[768] != 34318)
167 return false;
168 *transferFn = QColorTransferFunction::fromSRgb();
169 return true;
170 }
171 if (m_tableSize == 4096 && !m_table16.isEmpty()) {
172 // Nikon, Epson, and lcms2 sRGB gamma tables:
173 if (m_table16[515] != 960)
174 return false;
175 if (m_table16[1025] != 3342)
176 return false;
177 if (m_table16[2051] != 14079)
178 return false;
179 *transferFn = QColorTransferFunction::fromSRgb();
180 return true;
181 }
182 return false;
183 }
184 friend inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2);
185 friend inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2);
186
187 Type m_type = TwoWay;
188 uint32_t m_tableSize = 0;
189 QList<uint8_t> m_table8;
190 QList<uint16_t> m_table16;
191private:
192 template<typename T>
193 static float inverseLookup(float needle, float resultLargerThan, const QList<T> &table, quint32 tableMax)
194 {
195 uint32_t i = qMax(static_cast<uint32_t>(resultLargerThan * tableMax), 1U) - 1;
196 auto it = std::lower_bound(table.cbegin() + i, table.cend(), needle);
197 i = it - table.cbegin();
198 if (i == 0)
199 return 0.0f;
200 if (i >= tableMax)
201 return 1.0f;
202 const float y1 = table[i - 1];
203 const float y2 = table[i];
204 Q_ASSERT(needle >= y1 && needle <= y2);
205 const float fr = (needle - y1) / (y2 - y1);
206 return (i + fr) * (1.0f / tableMax);
207 }
208
209};
210
211inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2)
212{
213 if (t1.m_tableSize != t2.m_tableSize)
214 return true;
215 if (t1.m_type != t2.m_type)
216 return true;
217 if (t1.m_table8.isEmpty() != t2.m_table8.isEmpty())
218 return true;
219 if (t1.m_table16.isEmpty() != t2.m_table16.isEmpty())
220 return true;
221 if (!t1.m_table8.isEmpty()) {
222 for (uint32_t i = 0; i < t1.m_tableSize; ++i) {
223 if (t1.m_table8[i] != t2.m_table8[i])
224 return true;
225 }
226 }
227 if (!t1.m_table16.isEmpty()) {
228 for (uint32_t i = 0; i < t1.m_tableSize; ++i) {
229 if (t1.m_table16[i] != t2.m_table16[i])
230 return true;
231 }
232 }
233 return false;
234}
235
236inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2)
237{
238 return !(t1 != t2);
239}
240
241QT_END_NAMESPACE
242
243#endif // QCOLORTRANSFERTABLE_P_H
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:807
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:818
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]