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