Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QCOLORTRCLUT_P_H
5#define QCOLORTRCLUT_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 <QtGui/qrgb.h>
20#include <QtGui/qrgba64.h>
21
22#include <cmath>
23#include <memory>
24
25#if defined(__SSE2__)
26#include <emmintrin.h>
27#elif defined(__ARM_NEON__) || defined(__ARM_NEON)
28#include <arm_neon.h>
29#endif
30
32
35
36class Q_GUI_EXPORT QColorTrcLut
37{
38public:
39 static constexpr uint32_t ShiftUp = 4; // Amount to shift up from 1->255
40 static constexpr uint32_t ShiftDown = (8 - ShiftUp); // Amount to shift down from 1->65280
41 static constexpr qsizetype Resolution = (1 << ShiftUp) * 255; // Number of entries in table
42
43 enum Direction {
44 ToLinear = 1,
45 FromLinear = 2,
46 BiLinear = ToLinear | FromLinear
47 };
48
49 static std::shared_ptr<QColorTrcLut> fromGamma(qreal gamma, Direction dir = BiLinear);
50 static std::shared_ptr<QColorTrcLut> fromTransferFunction(const QColorTransferFunction &transFn, Direction dir = BiLinear);
51 static std::shared_ptr<QColorTrcLut> fromTransferTable(const QColorTransferTable &transTable, Direction dir = BiLinear);
52 void setFromGamma(qreal gamma, Direction dir = BiLinear);
53 void setFromTransferFunction(const QColorTransferFunction &transFn, Direction dir = BiLinear);
54 void setFromTransferTable(const QColorTransferTable &transTable, Direction dir = BiLinear);
55
56 // The following methods all convert opaque or unpremultiplied colors:
57
58 QRgba64 toLinear64(QRgb rgb32) const
59 {
60#if defined(__SSE2__)
61 __m128i v = _mm_cvtsi32_si128(rgb32);
62 v = _mm_unpacklo_epi8(v, _mm_setzero_si128());
63 const __m128i vidx = _mm_slli_epi16(v, ShiftUp);
64 const int ridx = _mm_extract_epi16(vidx, 2);
65 const int gidx = _mm_extract_epi16(vidx, 1);
66 const int bidx = _mm_extract_epi16(vidx, 0);
67 v = _mm_slli_epi16(v, 8); // a * 256
68 v = _mm_insert_epi16(v, m_toLinear[ridx], 0);
69 v = _mm_insert_epi16(v, m_toLinear[gidx], 1);
70 v = _mm_insert_epi16(v, m_toLinear[bidx], 2);
71 v = _mm_add_epi16(v, _mm_srli_epi16(v, 8));
72 QRgba64 rgba64;
73 _mm_storel_epi64(reinterpret_cast<__m128i *>(&rgba64), v);
74 return rgba64;
75#elif (defined(__ARM_NEON__) || defined(__ARM_NEON)) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
76 uint8x8_t v8 = vreinterpret_u8_u32(vmov_n_u32(rgb32));
77 uint16x4_t v16 = vget_low_u16(vmovl_u8(v8));
78 const uint16x4_t vidx = vshl_n_u16(v16, ShiftUp);
79 const int ridx = vget_lane_u16(vidx, 2);
80 const int gidx = vget_lane_u16(vidx, 1);
81 const int bidx = vget_lane_u16(vidx, 0);
82 v16 = vshl_n_u16(v16, 8); // a * 256
83 v16 = vset_lane_u16(m_toLinear[ridx], v16, 0);
84 v16 = vset_lane_u16(m_toLinear[gidx], v16, 1);
85 v16 = vset_lane_u16(m_toLinear[bidx], v16, 2);
86 v16 = vadd_u16(v16, vshr_n_u16(v16, 8));
87 return QRgba64::fromRgba64(vget_lane_u64(vreinterpret_u64_u16(v16), 0));
88#else
89 uint r = m_toLinear[qRed(rgb32) << ShiftUp];
90 uint g = m_toLinear[qGreen(rgb32) << ShiftUp];
91 uint b = m_toLinear[qBlue(rgb32) << ShiftUp];
92 r = r + (r >> 8);
93 g = g + (g >> 8);
94 b = b + (b >> 8);
95 return QRgba64::fromRgba64(r, g, b, qAlpha(rgb32) * 257);
96#endif
97 }
98 QRgba64 toLinear64(QRgba64) const = delete;
99
100 QRgb toLinear(QRgb rgb32) const
101 {
102 return convertWithTable(rgb32, m_toLinear.get());
103 }
104
106 {
107 return convertWithTable(rgb64, m_toLinear.get());
108 }
109
110 float u8ToLinearF32(int c) const
111 {
112 ushort v = m_toLinear[c << ShiftUp];
113 return v * (1.0f / (255*256));
114 }
115
116 float u16ToLinearF32(int c) const
117 {
118 c -= (c >> 8);
119 ushort v = m_toLinear[c >> ShiftDown];
120 return v * (1.0f / (255*256));
121 }
122
123 float toLinear(float f) const
124 {
125 ushort v = m_toLinear[(int)(f * Resolution + 0.5f)];
126 return v * (1.0f / (255*256));
127 }
128
130 {
131#if defined(__SSE2__)
132 __m128i v = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(&rgb64));
133 v = _mm_sub_epi16(v, _mm_srli_epi16(v, 8));
134 const __m128i vidx = _mm_srli_epi16(v, ShiftDown);
135 const int ridx = _mm_extract_epi16(vidx, 0);
136 const int gidx = _mm_extract_epi16(vidx, 1);
137 const int bidx = _mm_extract_epi16(vidx, 2);
138 v = _mm_insert_epi16(v, m_fromLinear[ridx], 2);
139 v = _mm_insert_epi16(v, m_fromLinear[gidx], 1);
140 v = _mm_insert_epi16(v, m_fromLinear[bidx], 0);
141 v = _mm_add_epi16(v, _mm_set1_epi16(0x80));
142 v = _mm_srli_epi16(v, 8);
143 v = _mm_packus_epi16(v, v);
144 return _mm_cvtsi128_si32(v);
145#elif (defined(__ARM_NEON__) || defined(__ARM_NEON)) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
146 uint16x4_t v = vreinterpret_u16_u64(vmov_n_u64(rgb64));
147 v = vsub_u16(v, vshr_n_u16(v, 8));
148 const uint16x4_t vidx = vshr_n_u16(v, ShiftDown);
149 const int ridx = vget_lane_u16(vidx, 0);
150 const int gidx = vget_lane_u16(vidx, 1);
151 const int bidx = vget_lane_u16(vidx, 2);
152 v = vset_lane_u16(m_fromLinear[ridx], v, 2);
153 v = vset_lane_u16(m_fromLinear[gidx], v, 1);
154 v = vset_lane_u16(m_fromLinear[bidx], v, 0);
155 uint8x8_t v8 = vrshrn_n_u16(vcombine_u16(v, v), 8);
156 return vget_lane_u32(vreinterpret_u32_u8(v8), 0);
157#else
158 uint a = rgb64.alpha();
159 uint r = rgb64.red();
160 uint g = rgb64.green();
161 uint b = rgb64.blue();
162 a = a - (a >> 8);
163 r = r - (r >> 8);
164 g = g - (g >> 8);
165 b = b - (b >> 8);
166 a = (a + 0x80) >> 8;
167 r = (m_fromLinear[r >> ShiftDown] + 0x80) >> 8;
168 g = (m_fromLinear[g >> ShiftDown] + 0x80) >> 8;
169 b = (m_fromLinear[b >> ShiftDown] + 0x80) >> 8;
170 return (a << 24) | (r << 16) | (g << 8) | b;
171#endif
172 }
173
174 QRgb fromLinear(QRgb rgb32) const
175 {
176 return convertWithTable(rgb32, m_fromLinear.get());
177 }
178
180 {
181 return convertWithTable(rgb64, m_fromLinear.get());
182 }
183
184 int u8FromLinearF32(float f) const
185 {
186 ushort v = m_fromLinear[(int)(f * Resolution + 0.5f)];
187 return (v + 0x80) >> 8;
188 }
189 int u16FromLinearF32(float f) const
190 {
191 ushort v = m_fromLinear[(int)(f * Resolution + 0.5f)];
192 return v + (v >> 8);
193 }
194 float fromLinear(float f) const
195 {
196 ushort v = m_fromLinear[(int)(f * Resolution + 0.5f)];
197 return v * (1.0f / (255*256));
198 }
199
200 // We translate to 0-65280 (255*256) instead to 0-65535 to make simple
201 // shifting an accurate conversion.
202 // We translate from 0->Resolution (4080 = 255*16) for the same speed up,
203 // and to keep the tables small enough to fit in most inner caches.
204 std::unique_ptr<ushort[]> m_toLinear; // [0->Resolution] -> [0-65280]
205 std::unique_ptr<ushort[]> m_fromLinear; // [0->Resolution] -> [0-65280]
206
207private:
208 QColorTrcLut() = default;
209
210 static std::shared_ptr<QColorTrcLut> create();
211
212 Q_ALWAYS_INLINE static QRgb convertWithTable(QRgb rgb32, const ushort *table)
213 {
214 const int r = (table[qRed(rgb32) << ShiftUp] + 0x80) >> 8;
215 const int g = (table[qGreen(rgb32) << ShiftUp] + 0x80) >> 8;
216 const int b = (table[qBlue(rgb32) << ShiftUp] + 0x80) >> 8;
217 return (rgb32 & 0xff000000) | (r << 16) | (g << 8) | b;
218 }
219 Q_ALWAYS_INLINE static QRgba64 convertWithTable(QRgba64 rgb64, const ushort *table)
220 {
221#if defined(__SSE2__)
222 __m128i v = _mm_loadl_epi64(reinterpret_cast<const __m128i *>(&rgb64));
223 v = _mm_sub_epi16(v, _mm_srli_epi16(v, 8));
224 const __m128i vidx = _mm_srli_epi16(v, ShiftDown);
225 const int ridx = _mm_extract_epi16(vidx, 2);
226 const int gidx = _mm_extract_epi16(vidx, 1);
227 const int bidx = _mm_extract_epi16(vidx, 0);
228 v = _mm_insert_epi16(v, table[ridx], 2);
229 v = _mm_insert_epi16(v, table[gidx], 1);
230 v = _mm_insert_epi16(v, table[bidx], 0);
231 v = _mm_add_epi16(v, _mm_srli_epi16(v, 8));
232 QRgba64 rgba64;
233 _mm_storel_epi64(reinterpret_cast<__m128i *>(&rgba64), v);
234 return rgba64;
235#elif (defined(__ARM_NEON__) || defined(__ARM_NEON)) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN
236 uint16x4_t v = vreinterpret_u16_u64(vmov_n_u64(rgb64));
237 v = vsub_u16(v, vshr_n_u16(v, 8));
238 const uint16x4_t vidx = vshr_n_u16(v, ShiftDown);
239 const int ridx = vget_lane_u16(vidx, 2);
240 const int gidx = vget_lane_u16(vidx, 1);
241 const int bidx = vget_lane_u16(vidx, 0);
242 v = vset_lane_u16(table[ridx], v, 2);
243 v = vset_lane_u16(table[gidx], v, 1);
244 v = vset_lane_u16(table[bidx], v, 0);
245 v = vadd_u16(v, vshr_n_u16(v, 8));
246 return QRgba64::fromRgba64(vget_lane_u64(vreinterpret_u64_u16(v), 0));
247#else
248 ushort r = rgb64.red();
249 ushort g = rgb64.green();
250 ushort b = rgb64.blue();
251 r = r - (r >> 8);
252 g = g - (g >> 8);
253 b = b - (b >> 8);
254 r = table[r >> ShiftDown];
255 g = table[g >> ShiftDown];
256 b = table[b >> ShiftDown];
257 r = r + (r >> 8);
258 g = g + (g >> 8);
259 b = b + (b >> 8);
260 return QRgba64::fromRgba64(r, g, b, rgb64.alpha());
261#endif
262 }
263};
264
266
267#endif // QCOLORTRCLUT_P_H
std::unique_ptr< ushort[]> m_fromLinear
int u16FromLinearF32(float f) const
int u8FromLinearF32(float f) const
float toLinear(float f) const
float u8ToLinearF32(int c) const
QRgb toLinear(QRgb rgb32) const
QRgb fromLinear(QRgb rgb32) const
std::unique_ptr< ushort[]> m_toLinear
QRgba64 toLinear(QRgba64 rgb64) const
float fromLinear(float f) const
QRgb fromLinear64(QRgba64 rgb64) const
float u16ToLinearF32(int c) const
QRgba64 fromLinear(QRgba64 rgb64) const
QRgba64 toLinear64(QRgb rgb32) const
QRgba64 toLinear64(QRgba64) const =delete
static constexpr QRgba64 fromRgba64(quint64 c)
Definition qrgba64.h:36
Combined button and popup list for selecting options.
#define Q_ALWAYS_INLINE
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLfloat GLfloat f
GLboolean GLboolean g
const GLubyte * c
GLenum GLenum GLsizei void * table
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
constexpr int qRed(QRgb rgb)
Definition qrgb.h:18
constexpr int qGreen(QRgb rgb)
Definition qrgb.h:21
constexpr int qBlue(QRgb rgb)
Definition qrgb.h:24
constexpr int qAlpha(QRgb rgb)
Definition qrgb.h:27
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
unsigned short ushort
Definition qtypes.h:33
double qreal
Definition qtypes.h:187
QString dir
[11]
view create()