Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcolortrclut_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
5#ifndef QCOLORTRCLUT_P_H
6#define QCOLORTRCLUT_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtGui/private/qtguiglobal_p.h>
20#include <QtGui/qrgb.h>
21#include <QtGui/qrgba64.h>
22#include <QtCore/private/qsimd_p.h>
23
24#include <cmath>
25#include <memory>
26
27#if defined(__SSE2__)
28#include <emmintrin.h>
29#elif defined(__ARM_NEON__)
30#include <arm_neon.h>
31#endif
32
33QT_BEGIN_NAMESPACE
34
35class QColorTransferGenericFunction;
37class QColorTransferTable;
38class QColorTrc;
39
40class Q_GUI_EXPORT QColorTrcLut
41{
42public:
43 static constexpr uint32_t ShiftUp = 4; // Amount to shift up from 1->255
44 static constexpr uint32_t ShiftDown = (8 - ShiftUp); // Amount to shift down from 1->65280
45 static constexpr qsizetype Resolution = (1 << ShiftUp) * 255; // Number of entries in table
46
47 enum Direction {
48 ToLinear = 1,
49 FromLinear = 2,
50 BiLinear = ToLinear | FromLinear
51 };
52
53 static std::shared_ptr<QColorTrcLut> fromGamma(float gamma, Direction dir = BiLinear);
54 static std::shared_ptr<QColorTrcLut> fromTrc(const QColorTrc &trc, Direction dir = BiLinear);
55 void setFromGamma(float gamma, Direction dir = BiLinear);
56 void setFromTransferFunction(const QColorTransferFunction &transFn, Direction dir = BiLinear);
57 void setFromTransferTable(const QColorTransferTable &transTable, Direction dir = BiLinear);
58 void setFromTransferGenericFunction(const QColorTransferGenericFunction &transfn, Direction dir);
59 void setFromTrc(const QColorTrc &trc, Direction dir);
60
61 // The following methods all convert opaque or unpremultiplied colors:
62
63 QRgba64 toLinear64(QRgb rgb32) const
64 {
65#if defined(__SSE2__)
66 __m128i v = _mm_cvtsi32_si128(rgb32);
67 v = _mm_unpacklo_epi8(v, _mm_setzero_si128());
68 const __m128i vidx = _mm_slli_epi16(v, ShiftUp);
69 const int ridx = _mm_extract_epi16(vidx, 2);
70 const int gidx = _mm_extract_epi16(vidx, 1);
71 const int bidx = _mm_extract_epi16(vidx, 0);
72 v = _mm_slli_epi16(v, 8); // a * 256
73 v = _mm_insert_epi16(v, m_toLinear[ridx], 0);
74 v = _mm_insert_epi16(v, m_toLinear[gidx], 1);
75 v = _mm_insert_epi16(v, m_toLinear[bidx], 2);
76 v = _mm_add_epi16(v, _mm_srli_epi16(v, 8));
77 QRgba64 rgba64;
78 _mm_storel_epi64(reinterpret_cast<__m128i *>(&rgba64), v);
79 return rgba64;
80#elif defined(__ARM_NEON__) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
81 uint8x8_t v8 = vreinterpret_u8_u32(vmov_n_u32(rgb32));
82 uint16x4_t v16 = vget_low_u16(vmovl_u8(v8));
83 const uint16x4_t vidx = vshl_n_u16(v16, ShiftUp);
84 const int ridx = vget_lane_u16(vidx, 2);
85 const int gidx = vget_lane_u16(vidx, 1);
86 const int bidx = vget_lane_u16(vidx, 0);
87 v16 = vshl_n_u16(v16, 8); // a * 256
88 v16 = vset_lane_u16(m_toLinear[ridx], v16, 0);
89 v16 = vset_lane_u16(m_toLinear[gidx], v16, 1);
90 v16 = vset_lane_u16(m_toLinear[bidx], v16, 2);
91 v16 = vadd_u16(v16, vshr_n_u16(v16, 8));
92 return QRgba64::fromRgba64(vget_lane_u64(vreinterpret_u64_u16(v16), 0));
93#else
94 uint r = m_toLinear[qRed(rgb32) << ShiftUp];
95 uint g = m_toLinear[qGreen(rgb32) << ShiftUp];
96 uint b = m_toLinear[qBlue(rgb32) << ShiftUp];
97 r = r + (r >> 8);
98 g = g + (g >> 8);
99 b = b + (b >> 8);
100 return QRgba64::fromRgba64(r, g, b, qAlpha(rgb32) * 257);
101#endif
102 }
103 QRgba64 toLinear64(QRgba64) const = delete;
104
105 QRgb toLinear(QRgb rgb32) const
106 {
107 return convertWithTable(rgb32, m_toLinear.get());
108 }
109
110 QRgba64 toLinear(QRgba64 rgb64) const
111 {
112 return convertWithTable(rgb64, m_toLinear.get());
113 }
114
115 float u8ToLinearF32(int c) const
116 {
117 ushort v = m_toLinear[c << ShiftUp];
118 return v * (1.0f / (255*256));
119 }
120
121 float u16ToLinearF32(int c) const
122 {
123 c -= (c >> 8);
124 ushort v = m_toLinear[c >> ShiftDown];
125 return v * (1.0f / (255*256));
126 }
127
128 float toLinear(float f) const
129 {
130 ushort v = m_toLinear[(int)(f * Resolution + 0.5f)];
131 return v * (1.0f / (255*256));
132 }
133
134 QRgb fromLinear64(QRgba64 rgb64) const
135 {
136#if defined(__SSE2__)
137 __m128i v = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(&rgb64));
138 v = _mm_sub_epi16(v, _mm_srli_epi16(v, 8));
139 const __m128i vidx = _mm_srli_epi16(v, ShiftDown);
140 const int ridx = _mm_extract_epi16(vidx, 0);
141 const int gidx = _mm_extract_epi16(vidx, 1);
142 const int bidx = _mm_extract_epi16(vidx, 2);
143 v = _mm_insert_epi16(v, m_fromLinear[ridx], 2);
144 v = _mm_insert_epi16(v, m_fromLinear[gidx], 1);
145 v = _mm_insert_epi16(v, m_fromLinear[bidx], 0);
146 v = _mm_add_epi16(v, _mm_set1_epi16(0x80));
147 v = _mm_srli_epi16(v, 8);
148 v = _mm_packus_epi16(v, v);
149 return _mm_cvtsi128_si32(v);
150#elif defined(__ARM_NEON__) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
151 uint16x4_t v = vreinterpret_u16_u64(vmov_n_u64(rgb64));
152 v = vsub_u16(v, vshr_n_u16(v, 8));
153 const uint16x4_t vidx = vshr_n_u16(v, ShiftDown);
154 const int ridx = vget_lane_u16(vidx, 0);
155 const int gidx = vget_lane_u16(vidx, 1);
156 const int bidx = vget_lane_u16(vidx, 2);
157 v = vset_lane_u16(m_fromLinear[ridx], v, 2);
158 v = vset_lane_u16(m_fromLinear[gidx], v, 1);
159 v = vset_lane_u16(m_fromLinear[bidx], v, 0);
160 uint8x8_t v8 = vrshrn_n_u16(vcombine_u16(v, v), 8);
161 return vget_lane_u32(vreinterpret_u32_u8(v8), 0);
162#else
163 uint a = rgb64.alpha();
164 uint r = rgb64.red();
165 uint g = rgb64.green();
166 uint b = rgb64.blue();
167 a = a - (a >> 8);
168 r = r - (r >> 8);
169 g = g - (g >> 8);
170 b = b - (b >> 8);
171 a = (a + 0x80) >> 8;
172 r = (m_fromLinear[r >> ShiftDown] + 0x80) >> 8;
173 g = (m_fromLinear[g >> ShiftDown] + 0x80) >> 8;
174 b = (m_fromLinear[b >> ShiftDown] + 0x80) >> 8;
175 return (a << 24) | (r << 16) | (g << 8) | b;
176#endif
177 }
178
179 QRgb fromLinear(QRgb rgb32) const
180 {
181 return convertWithTable(rgb32, m_fromLinear.get());
182 }
183
184 QRgba64 fromLinear(QRgba64 rgb64) const
185 {
186 return convertWithTable(rgb64, m_fromLinear.get());
187 }
188
189 int u8FromLinearF32(float f) const
190 {
191 ushort v = m_fromLinear[(int)(f * Resolution + 0.5f)];
192 return (v + 0x80) >> 8;
193 }
194 int u16FromLinearF32(float f) const
195 {
196 ushort v = m_fromLinear[(int)(f * Resolution + 0.5f)];
197 return v + (v >> 8);
198 }
199 float fromLinear(float f) const
200 {
201 ushort v = m_fromLinear[(int)(f * Resolution + 0.5f)];
202 return v * (1.0f / (255*256));
203 }
204
205 // We translate to 0-65280 (255*256) instead to 0-65535 to make simple
206 // shifting an accurate conversion.
207 // We translate from 0->Resolution (4080 = 255*16) for the same speed up,
208 // and to keep the tables small enough to fit in most inner caches.
209 std::unique_ptr<ushort[]> m_toLinear; // [0->Resolution] -> [0-65280]
210 std::unique_ptr<ushort[]> m_fromLinear; // [0->Resolution] -> [0-65280]
211 ushort m_unclampedToLinear = Resolution;
212
213private:
214 QColorTrcLut() = default;
215
216 static std::shared_ptr<QColorTrcLut> create();
217
218 Q_ALWAYS_INLINE static QRgb convertWithTable(QRgb rgb32, const ushort *table)
219 {
220 const int r = (table[qRed(rgb32) << ShiftUp] + 0x80) >> 8;
221 const int g = (table[qGreen(rgb32) << ShiftUp] + 0x80) >> 8;
222 const int b = (table[qBlue(rgb32) << ShiftUp] + 0x80) >> 8;
223 return (rgb32 & 0xff000000) | (r << 16) | (g << 8) | b;
224 }
225 Q_ALWAYS_INLINE static QRgba64 convertWithTable(QRgba64 rgb64, const ushort *table)
226 {
227#if defined(__SSE2__)
228 __m128i v = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(&rgb64));
229 v = _mm_sub_epi16(v, _mm_srli_epi16(v, 8));
230 const __m128i vidx = _mm_srli_epi16(v, ShiftDown);
231 const int ridx = _mm_extract_epi16(vidx, 2);
232 const int gidx = _mm_extract_epi16(vidx, 1);
233 const int bidx = _mm_extract_epi16(vidx, 0);
234 v = _mm_insert_epi16(v, table[ridx], 2);
235 v = _mm_insert_epi16(v, table[gidx], 1);
236 v = _mm_insert_epi16(v, table[bidx], 0);
237 v = _mm_add_epi16(v, _mm_srli_epi16(v, 8));
238 QRgba64 rgba64;
239 _mm_storel_epi64(reinterpret_cast<__m128i *>(&rgba64), v);
240 return rgba64;
241#elif defined(__ARM_NEON__) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
242 uint16x4_t v = vreinterpret_u16_u64(vmov_n_u64(rgb64));
243 v = vsub_u16(v, vshr_n_u16(v, 8));
244 const uint16x4_t vidx = vshr_n_u16(v, ShiftDown);
245 const int ridx = vget_lane_u16(vidx, 2);
246 const int gidx = vget_lane_u16(vidx, 1);
247 const int bidx = vget_lane_u16(vidx, 0);
248 v = vset_lane_u16(table[ridx], v, 2);
249 v = vset_lane_u16(table[gidx], v, 1);
250 v = vset_lane_u16(table[bidx], v, 0);
251 v = vadd_u16(v, vshr_n_u16(v, 8));
252 return QRgba64::fromRgba64(vget_lane_u64(vreinterpret_u64_u16(v), 0));
253#else
254 ushort r = rgb64.red();
255 ushort g = rgb64.green();
256 ushort b = rgb64.blue();
257 r = r - (r >> 8);
258 g = g - (g >> 8);
259 b = b - (b >> 8);
260 r = table[r >> ShiftDown];
261 g = table[g >> ShiftDown];
262 b = table[b >> ShiftDown];
263 r = r + (r >> 8);
264 g = g + (g >> 8);
265 b = b + (b >> 8);
266 return QRgba64::fromRgba64(r, g, b, rgb64.alpha());
267#endif
268 }
269};
270
271QT_END_NAMESPACE
272
273#endif // QCOLORTRCLUT_P_H
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:807
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:818
QList< QColorVector > table
uint32_t gridPointsY
uint32_t gridPointsW
bool isEmpty() const
uint32_t gridPointsX
uint32_t gridPointsZ
QColorMatrix inverted() const
static QColorMatrix toXyzFromSRgb()
static QColorMatrix toXyzFromAdobeRgb()
bool isValid() const
friend bool comparesEqual(const QColorMatrix &lhs, const QColorMatrix &rhs) noexcept
QColorVector g
constexpr float determinant() const
static QColorMatrix identity()
QColorMatrix transposed() const
static QColorMatrix fromScale(QColorVector v)
friend constexpr QColorMatrix operator*(const QColorMatrix &a, const QColorMatrix &o)
QColorVector b
QColorVector r
bool isIdentity() const noexcept
static QColorMatrix toXyzFromDciP3D65()
static QColorMatrix chromaticAdaptation(const QColorVector &whitePoint)
static QColorMatrix toXyzFromProPhotoRgb()
QColorVector map(const QColorVector &c) const
static QColorMatrix toXyzFromBt2020()
constexpr bool isNull() const
void setTransferFunctionTables(const QList< uint16_t > &redTransferFunctionTable, const QList< uint16_t > &greenTransferFunctionTable, const QList< uint16_t > &blueTransferFunctionTable)
QColorSpacePrivate(QPointF whitePoint, const QList< uint16_t > &transferFunctionTable)
QColorVector whitePoint
QColorSpacePrivate(QPointF whitePoint, QColorSpace::TransferFunction transferFunction, float gamma)
QColorSpacePrivate(const QColorSpace::PrimaryPoints &primaries, const QList< uint16_t > &transferFunctionTable)
QColorTransform transformationToColorSpace(const QColorSpacePrivate *out) const
QList< Element > mAB
QList< Element > mBA
void clearElementListProcessingForEdit()
QColorMatrix chad
void setTransferFunctionTable(const QList< uint16_t > &transferFunctionTable)
QColorTransform transformationToXYZ() const
bool isThreeComponentMatrix() const
QColorSpacePrivate(const QColorSpace::PrimaryPoints &primaries, const QList< uint16_t > &redTransferFunctionTable, const QList< uint16_t > &greenTransferFunctionTable, const QList< uint16_t > &blueRransferFunctionTable)
QColorSpacePrivate(const QColorSpacePrivate &other)=default
static const QColorSpacePrivate * get(const QColorSpace &colorSpace)
QColorSpacePrivate(const QColorSpace::PrimaryPoints &primaries, QColorSpace::TransferFunction transferFunction, float gamma)
static QColorSpacePrivate * get(QColorSpace &colorSpace)
bool isValid() const noexcept
QColorMatrix toXyz
QColorSpacePrivate(QColorSpace::NamedColorSpace namedColorSpace)
bool equals(const QColorSpacePrivate *other) const
Q_DECLARE_FLAGS(Hints, Hint)
QColorTransferFunction inverted() const
friend bool operator!=(const QColorTransferFunction &f1, const QColorTransferFunction &f2)
QColorTransferFunction(float a, float b, float c, float d, float e, float f, float g) noexcept
static QColorTransferFunction fromGamma(float gamma)
bool matches(const QColorTransferFunction &o) const
static QColorTransferFunction fromBt2020()
friend bool operator==(const QColorTransferFunction &f1, const QColorTransferFunction &f2)
static QColorTransferFunction fromProPhotoRgb()
static QColorTransferFunction fromSRgb()
static QColorTransferGenericFunction pq()
static QColorTransferGenericFunction hlg()
bool operator==(const QColorTransferGenericFunction &o) const noexcept
bool operator!=(const QColorTransferGenericFunction &o) const noexcept
constexpr QColorTransferGenericFunction(ConverterPtr toLinear=nullptr, ConverterPtr fromLinear=nullptr) noexcept
The QColorTransform class is a transformation between color spaces.
Combined button and popup list for selecting options.
bool comparesEqual(const QColorVector &v1, const QColorVector &v2) noexcept
static void cleanupPredefinedColorspaces()
static bool compareElement(const QColorSpacePrivate::TransferElement &element, const QColorSpacePrivate::TransferElement &other)
QDebug operator<<(QDebug dbg, const QColorCLUT &)
QDebug operator<<(QDebug dbg, const QColorSpace &colorSpace)
QDebug operator<<(QDebug dbg, const QColorMatrix &)
static bool compareElement(const QColorMatrix &element, const QColorMatrix &other)
QDataStream & operator>>(QDataStream &s, QColorSpace &colorSpace)
QDataStream & operator<<(QDataStream &s, const QColorSpace &image)
QDebug operator<<(QDebug dbg, const QColorVector &)
QColorMatrix qColorSpacePrimaryPointsToXyzMatrix(const QColorSpace::PrimaryPoints &primaries)
static bool compareElements(const T &element, const QColorSpacePrivate::Element &other)
static bool compareElement(const QColorCLUT &element, const QColorCLUT &other)
static bool compareElement(const QColorVector &element, const QColorVector &other)
QDebug operator<<(QDebug dbg, const QColorSpacePrivate::TransferElement &)
QT_BEGIN_NAMESPACE bool qColorSpacePrimaryPointsAreValid(const QColorSpace::PrimaryPoints &primaries)
Q_DECLARE_OPERATORS_FOR_FLAGS(QColorTransferFunction::Hints)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
LUT(const LUT &other)
const std::shared_ptr< QColorTrcLut > & operator[](int i) const
std::shared_ptr< QColorTrcLut > & operator[](int i)
std::shared_ptr< QColorTrcLut > table[3]