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 <QVector>
58#include <cmath>
59
60QT_BEGIN_NAMESPACE
61
62// Defines either an ICC TRC 'curve' or a lut8/lut16 A or B table
63class Q_GUI_EXPORT QColorTransferTable
64{
65public:
66 QColorTransferTable() noexcept
67 : m_tableSize(0)
68 { }
69 QColorTransferTable(uint32_t size, const QVector<uint8_t> &table) noexcept
70 : m_tableSize(size)
71 , m_table8(table)
72 {
73 Q_ASSERT(size <= uint32_t(table.count()));
74 }
75 QColorTransferTable(uint32_t size, const QVector<uint16_t> &table) noexcept
76 : m_tableSize(size)
77 , m_table16(table)
78 {
79 Q_ASSERT(size <= uint32_t(table.count()));
80 }
81
82 bool isEmpty() const
83 {
84 return m_tableSize == 0;
85 }
86
87 bool checkValidity() const
88 {
89 if (isEmpty())
90 return true;
91 // Only one table can be set
92 if (!m_table8.isEmpty() && !m_table16.isEmpty())
93 return false;
94 // At least 2 elements
95 if (m_tableSize < 2)
96 return false;
97 // The table must describe an injective curve:
98 if (!m_table8.isEmpty()) {
99 uint8_t val = 0;
100 for (uint i = 0; i < m_tableSize; ++i) {
101 if (m_table8[i] < val)
102 return false;
103 val = m_table8[i];
104 }
105 }
106 if (!m_table16.isEmpty()) {
107 uint16_t val = 0;
108 for (uint i = 0; i < m_tableSize; ++i) {
109 if (m_table16[i] < val)
110 return false;
111 val = m_table16[i];
112 }
113 }
114 return true;
115 }
116
117 float apply(float x) const
118 {
119 x = std::min(a: std::max(a: x, b: 0.0f), b: 1.0f);
120 x *= m_tableSize - 1;
121 uint32_t lo = static_cast<uint32_t>(std::floor(x: x));
122 uint32_t hi = std::min(a: lo + 1, b: m_tableSize - 1);
123 float frac = x - lo;
124 if (!m_table16.isEmpty())
125 return (m_table16[lo] * (1.0f - frac) + m_table16[hi] * frac) * (1.0f/65535.0f);
126 if (!m_table8.isEmpty())
127 return (m_table8[lo] * (1.0f - frac) + m_table8[hi] * frac) * (1.0f/255.0f);
128 return x;
129 }
130
131 // Apply inverse, optimized by giving a previous result a value < x.
132 float applyInverse(float x, float resultLargerThan = 0.0f) const
133 {
134 Q_ASSERT(resultLargerThan >= 0.0f && resultLargerThan <= 1.0f);
135 if (x <= 0.0f)
136 return 0.0f;
137 if (x >= 1.0f)
138 return 1.0f;
139 if (!m_table16.isEmpty()) {
140 float v = x * 65535.0f;
141 uint32_t i = std::floor(x: resultLargerThan * (m_tableSize - 1));
142 for ( ; i < m_tableSize; ++i) {
143 if (m_table16[i] > v)
144 break;
145 }
146 if (i >= m_tableSize - 1)
147 return 1.0f;
148 float y1 = m_table16[i - 1];
149 float y2 = m_table16[i];
150 Q_ASSERT(v >= y1 && v <= y2);
151 float fr = (v - y1) / (y2 - y1);
152 return (i + fr) * (1.0f / (m_tableSize - 1));
153
154 }
155 if (!m_table8.isEmpty()) {
156 float v = x * 255.0f;
157 uint32_t i = std::floor(x: resultLargerThan * (m_tableSize - 1));
158 for ( ; i < m_tableSize; ++i) {
159 if (m_table8[i] > v)
160 break;
161 }
162 if (i >= m_tableSize - 1)
163 return 1.0f;
164 float y1 = m_table8[i - 1];
165 float y2 = m_table8[i];
166 Q_ASSERT(v >= y1 && v <= y2);
167 float fr = (v - y1) / (y2 - y1);
168 return (i + fr) * (1.0f / (m_tableSize - 1));
169 }
170 return x;
171 }
172
173 bool asColorTransferFunction(QColorTransferFunction *transferFn)
174 {
175 Q_ASSERT(transferFn);
176 if (m_tableSize < 2)
177 return false;
178 if (!m_table8.isEmpty() && (m_table8[0] != 0 || m_table8[m_tableSize - 1] != 255))
179 return false;
180 if (!m_table16.isEmpty() && (m_table16[0] != 0 || m_table16[m_tableSize - 1] != 65535))
181 return false;
182 if (m_tableSize == 2) {
183 *transferFn = QColorTransferFunction(); // Linear
184 return true;
185 }
186 // The following heuristics are based on those from Skia:
187 if (m_tableSize == 26 && !m_table16.isEmpty()) {
188 // code.facebook.com/posts/411525055626587/under-the-hood-improving-facebook-photos
189 if (m_table16[6] != 3062)
190 return false;
191 if (m_table16[12] != 12824)
192 return false;
193 if (m_table16[18] != 31237)
194 return false;
195 *transferFn = QColorTransferFunction::fromSRgb();
196 return true;
197 }
198 if (m_tableSize == 1024 && !m_table16.isEmpty()) {
199 // HP and Canon sRGB gamma tables:
200 if (m_table16[257] != 3366)
201 return false;
202 if (m_table16[513] != 14116)
203 return false;
204 if (m_table16[768] != 34318)
205 return false;
206 *transferFn = QColorTransferFunction::fromSRgb();
207 return true;
208 }
209 if (m_tableSize == 4096 && !m_table16.isEmpty()) {
210 // Nikon, Epson, and lcms2 sRGB gamma tables:
211 if (m_table16[515] != 960)
212 return false;
213 if (m_table16[1025] != 3342)
214 return false;
215 if (m_table16[2051] != 14079)
216 return false;
217 *transferFn = QColorTransferFunction::fromSRgb();
218 return true;
219 }
220 return false;
221 }
222 friend inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2);
223 friend inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2);
224
225 uint32_t m_tableSize;
226 QVector<uint8_t> m_table8;
227 QVector<uint16_t> m_table16;
228};
229
230inline bool operator!=(const QColorTransferTable &t1, const QColorTransferTable &t2)
231{
232 if (t1.m_tableSize != t2.m_tableSize)
233 return true;
234 if (t1.m_table8.isEmpty() != t2.m_table8.isEmpty())
235 return true;
236 if (t1.m_table16.isEmpty() != t2.m_table16.isEmpty())
237 return true;
238 if (!t1.m_table8.isEmpty()) {
239 for (uint32_t i = 0; i < t1.m_tableSize; ++i) {
240 if (t1.m_table8[i] != t2.m_table8[i])
241 return true;
242 }
243 }
244 if (!t1.m_table16.isEmpty()) {
245 for (uint32_t i = 0; i < t1.m_tableSize; ++i) {
246 if (t1.m_table16[i] != t2.m_table16[i])
247 return true;
248 }
249 }
250 return false;
251}
252
253inline bool operator==(const QColorTransferTable &t1, const QColorTransferTable &t2)
254{
255 return !(t1 != t2);
256}
257
258QT_END_NAMESPACE
259
260#endif // QCOLORTRANSFERTABLE_P_H
261

source code of qtbase/src/gui/painting/qcolortransfertable_p.h