1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QCOLORTRANSFERTABLE_P_H |
41 | #define QCOLORTRANSFERTABLE_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtGui/private/qtguiglobal_p.h> |
55 | #include "qcolortransferfunction_p.h" |
56 | |
57 | #include <QList> |
58 | #include <cmath> |
59 | |
60 | QT_BEGIN_NAMESPACE |
61 | |
62 | // Defines either an ICC TRC 'curve' or a lut8/lut16 A or B table |
63 | class Q_GUI_EXPORT QColorTransferTable |
64 | { |
65 | public: |
66 | QColorTransferTable() noexcept |
67 | : m_tableSize(0) |
68 | { } |
69 | QColorTransferTable(uint32_t size, const QList<uint8_t> &table) noexcept |
70 | : m_tableSize(size), m_table8(table) |
71 | { |
72 | Q_ASSERT(qsizetype(size) <= table.count()); |
73 | } |
74 | QColorTransferTable(uint32_t size, const QList<uint16_t> &table) noexcept |
75 | : m_tableSize(size), m_table16(table) |
76 | { |
77 | Q_ASSERT(qsizetype(size) <= table.count()); |
78 | } |
79 | |
80 | bool isEmpty() const |
81 | { |
82 | return m_tableSize == 0; |
83 | } |
84 | |
85 | bool checkValidity() const |
86 | { |
87 | if (isEmpty()) |
88 | return true; |
89 | // Only one table can be set |
90 | if (!m_table8.isEmpty() && !m_table16.isEmpty()) |
91 | return false; |
92 | // At least 2 elements |
93 | if (m_tableSize < 2) |
94 | return false; |
95 | // The table must describe an injective curve: |
96 | if (!m_table8.isEmpty()) { |
97 | uint8_t val = 0; |
98 | for (uint i = 0; i < m_tableSize; ++i) { |
99 | if (m_table8[i] < val) |
100 | return false; |
101 | val = m_table8[i]; |
102 | } |
103 | } |
104 | if (!m_table16.isEmpty()) { |
105 | uint16_t val = 0; |
106 | for (uint i = 0; i < m_tableSize; ++i) { |
107 | if (m_table16[i] < val) |
108 | return false; |
109 | val = m_table16[i]; |
110 | } |
111 | } |
112 | return true; |
113 | } |
114 | |
115 | float apply(float x) const |
116 | { |
117 | x = std::min(std::max(x, 0.0f), 1.0f); |
118 | x *= m_tableSize - 1; |
119 | uint32_t lo = static_cast<uint32_t>(std::floor(x)); |
120 | uint32_t hi = std::min(lo + 1, m_tableSize - 1); |
121 | float frac = x - lo; |
122 | if (!m_table16.isEmpty()) |
123 | return (m_table16[lo] * (1.0f - frac) + m_table16[hi] * frac) * (1.0f/65535.0f); |
124 | if (!m_table8.isEmpty()) |
125 | return (m_table8[lo] * (1.0f - frac) + m_table8[hi] * frac) * (1.0f/255.0f); |
126 | return x; |
127 | } |
128 | |
129 | // Apply inverse, optimized by giving a previous result a value < x. |
130 | float applyInverse(float x, float resultLargerThan = 0.0f) const |
131 | { |
132 | Q_ASSERT(resultLargerThan >= 0.0f && resultLargerThan <= 1.0f); |
133 | if (x <= 0.0f) |
134 | return 0.0f; |
135 | if (x >= 1.0f) |
136 | return 1.0f; |
137 | if (!m_table16.isEmpty()) { |
138 | float v = x * 65535.0f; |
139 | uint32_t i = std::floor(resultLargerThan * (m_tableSize - 1)) + 1; |
140 | for ( ; i < m_tableSize; ++i) { |
141 | if (m_table16[i] > v) |
142 | break; |
143 | } |
144 | if (i >= m_tableSize - 1) |
145 | return 1.0f; |
146 | float y1 = m_table16[i - 1]; |
147 | float y2 = m_table16[i]; |
148 | Q_ASSERT(x >= y1 && x < y2); |
149 | float fr = (v - y1) / (y2 - y1); |
150 | return (i + fr) * (1.0f / (m_tableSize - 1)); |
151 | |
152 | } |
153 | if (!m_table8.isEmpty()) { |
154 | float v = x * 255.0f; |
155 | uint32_t i = std::floor(resultLargerThan * (m_tableSize - 1)) + 1; |
156 | for ( ; i < m_tableSize; ++i) { |
157 | if (m_table8[i] > v) |
158 | break; |
159 | } |
160 | if (i >= m_tableSize - 1) |
161 | return 1.0f; |
162 | float y1 = m_table8[i - 1]; |
163 | float y2 = m_table8[i]; |
164 | Q_ASSERT(x >= y1 && x < y2); |
165 | float fr = (v - y1) / (y2 - y1); |
166 | return (i + fr) * (1.0f / (m_tableSize - 1)); |
167 | } |
168 | return x; |
169 | } |
170 | |
171 | bool asColorTransferFunction(QColorTransferFunction *transferFn) |
172 | { |
173 | Q_ASSERT(transferFn); |
174 | if (m_tableSize < 2) |
175 | return false; |
176 | if (!m_table8.isEmpty() && (m_table8[0] != 0 || m_table8[m_tableSize - 1] != 255)) |
177 | return false; |
178 | if (!m_table16.isEmpty() && (m_table16[0] != 0 || m_table16[m_tableSize - 1] != 65535)) |
179 | return false; |
180 | if (m_tableSize == 2) { |
181 | *transferFn = QColorTransferFunction(); // Linear |
182 | return true; |
183 | } |
184 | // The following heuristics are based on those from Skia: |
185 | if (m_tableSize == 26 && !m_table16.isEmpty()) { |
186 | // code.facebook.com/posts/411525055626587/under-the-hood-improving-facebook-photos |
187 | if (m_table16[6] != 3062) |
188 | return false; |
189 | if (m_table16[12] != 12824) |
190 | return false; |
191 | if (m_table16[18] != 31237) |
192 | return false; |
193 | *transferFn = QColorTransferFunction::fromSRgb(); |
194 | return true; |
195 | } |
196 | if (m_tableSize == 1024 && !m_table16.isEmpty()) { |
197 | // HP and Canon sRGB gamma tables: |
198 | if (m_table16[257] != 3366) |
199 | return false; |
200 | if (m_table16[513] != 14116) |
201 | return false; |
202 | if (m_table16[768] != 34318) |
203 | return false; |
204 | *transferFn = QColorTransferFunction::fromSRgb(); |
205 | return true; |
206 | } |
207 | if (m_tableSize == 4096 && !m_table16.isEmpty()) { |
208 | // Nikon, Epson, and lcms2 sRGB gamma tables: |
209 | if (m_table16[515] != 960) |
210 | return false; |
211 | if (m_table16[1025] != 3342) |
212 | return false; |
213 | if (m_table16[2051] != 14079) |
214 | return false; |
215 | *transferFn = QColorTransferFunction::fromSRgb(); |
216 | return true; |
217 | } |
218 | return false; |
219 | } |
220 | friend inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2); |
221 | friend inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2); |
222 | |
223 | uint32_t m_tableSize; |
224 | QList<uint8_t> m_table8; |
225 | QList<uint16_t> m_table16; |
226 | }; |
227 | |
228 | inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2) |
229 | { |
230 | if (t1.m_tableSize != t2.m_tableSize) |
231 | return true; |
232 | if (t1.m_table8.isEmpty() != t2.m_table8.isEmpty()) |
233 | return true; |
234 | if (t1.m_table16.isEmpty() != t2.m_table16.isEmpty()) |
235 | return true; |
236 | if (!t1.m_table8.isEmpty()) { |
237 | for (uint32_t i = 0; i < t1.m_tableSize; ++i) { |
238 | if (t1.m_table8[i] != t2.m_table8[i]) |
239 | return true; |
240 | } |
241 | } |
242 | if (!t1.m_table16.isEmpty()) { |
243 | for (uint32_t i = 0; i < t1.m_tableSize; ++i) { |
244 | if (t1.m_table16[i] != t2.m_table16[i]) |
245 | return true; |
246 | } |
247 | } |
248 | return false; |
249 | } |
250 | |
251 | inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2) |
252 | { |
253 | return !(t1 != t2); |
254 | } |
255 | |
256 | QT_END_NAMESPACE |
257 | |
258 | #endif // QCOLORTRANSFERTABLE_P_H |
259 | |