Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
29class Q_GUI_EXPORT QColorTransferTable
30{
31public:
32 enum Type : uint8_t {
33 TwoWay = 0,
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 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
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 = static_cast<uint32_t>(resultLargerThan * tableMax);
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
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
236{
237 return !(t1 != t2);
238}
239
241
242#endif // QCOLORTRANSFERTABLE_P_H
static QColorTransferFunction fromSRgb()
bool isEmpty() const noexcept
float applyInverse(float x, float resultLargerThan=0.0f) const
float apply(float x) const
QColorTransferTable(uint32_t size, const QList< uint16_t > &table, Type type=TwoWay) noexcept
QColorTransferTable() noexcept=default
QList< uint16_t > m_table16
bool asColorTransferFunction(QColorTransferFunction *transferFn)
Definition qlist.h:75
const_iterator cbegin() const noexcept
Definition qset.h:138
QSet< QString >::iterator it
Combined button and popup list for selecting options.
bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2)
bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
GLint GLint GLint GLint GLint x
[0]
GLuint GLfloat GLfloat GLfloat GLfloat y1
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLenum type
GLuint GLfloat * val
GLfixed GLfixed GLfixed y2
GLenum GLenum GLsizei void * table
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define t2
unsigned int quint32
Definition qtypes.h:50
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
Definition moc.h:23