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