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

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