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
qcolorclut_p.h
Go to the documentation of this file.
1// Copyright (C) 2024 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 QCOLORCLUT_H
5#define QCOLORCLUT_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 <QtCore/qlist.h>
19#include <QtCore/qsimd.h>
20#include <QtGui/private/qcolormatrix_p.h>
21#if defined(__SSE2__)
22#include <immintrin.h>
23#endif
24
26
27// A 3/4-dimensional lookup table compatible with ICC lut8, lut16, mAB, and mBA formats.
29{
30 inline static QColorVector interpolate(const QColorVector &a, const QColorVector &b, float t)
31 {
32 return a + (b - a) * t; // faster than std::lerp by assuming no super large or non-number floats
33 }
34 inline static void interpolateIn(QColorVector &a, const QColorVector &b, float t)
35 {
36 a += (b - a) * t;
37 }
38public:
44
45 bool isEmpty() const { return table.isEmpty(); }
46
47 QColorVector apply(const QColorVector &v) const
48 {
49 Q_ASSERT(table.size() == qsizetype(gridPointsX * gridPointsY * gridPointsZ * gridPointsW));
50 QColorVector frac;
51#if defined(__SSE2__)
52 const __m128 minV = _mm_setzero_ps();
53 const __m128 maxV = _mm_set1_ps(1.0f);
54 const __m128i gridPointsInt = _mm_loadu_si128(reinterpret_cast<const __m128i *>(&gridPointsX));
55 const __m128 gridPointsV = _mm_cvtepi32_ps(_mm_add_epi32(gridPointsInt, _mm_set1_epi32(-1)));
56 __m128 c = _mm_loadu_ps(&v.x);
57 c = _mm_max_ps(c, minV);
58 c = _mm_min_ps(c, maxV);
59 c = _mm_mul_ps(c, gridPointsV);
60#if !defined(__SSE4_1__)
61 const __m128 clo = _mm_cvtepi32_ps(_mm_cvttps_epi32(c)); // truncation == floor for x >= 0
62#else
63 const __m128 clo = _mm_floor_ps(c);
64#endif
65 __m128 chi = _mm_add_ps(clo, maxV);
66 chi = _mm_min_ps(chi, gridPointsV);
67 _mm_storeu_ps(reinterpret_cast<float *>(&frac), _mm_sub_ps(c, clo));
68 const __m128i ilo = _mm_cvtps_epi32(clo);
69 const __m128i ihi = _mm_cvtps_epi32(chi);
70 const uint32_t lox = _mm_cvtsi128_si32(ilo);
71 const uint32_t hix = _mm_cvtsi128_si32(ihi);
72#if !defined(__SSE4_1__)
73 const uint32_t loy = _mm_cvtsi128_si32(_mm_shuffle_epi32(ilo, _MM_SHUFFLE(1, 1, 1, 1)));
74 const uint32_t loz = _mm_cvtsi128_si32(_mm_unpackhi_epi32(ilo, ilo));
75 const uint32_t low = _mm_cvtsi128_si32(_mm_shuffle_epi32(ilo, _MM_SHUFFLE(3, 3, 3, 3)));
76 const uint32_t hiy = _mm_cvtsi128_si32(_mm_shuffle_epi32(ihi, _MM_SHUFFLE(1, 1, 1, 1)));
77 const uint32_t hiz = _mm_cvtsi128_si32(_mm_unpackhi_epi32(ihi, ihi));
78 const uint32_t hiw = _mm_cvtsi128_si32(_mm_shuffle_epi32(ihi, _MM_SHUFFLE(3, 3, 3, 3)));
79#else
80 const uint32_t loy = _mm_extract_epi32(ilo, 1);
81 const uint32_t loz = _mm_extract_epi32(ilo, 2);
82 const uint32_t low = _mm_extract_epi32(ilo, 3);
83 const uint32_t hiy = _mm_extract_epi32(ihi, 1);
84 const uint32_t hiz = _mm_extract_epi32(ihi, 2);
85 const uint32_t hiw = _mm_extract_epi32(ihi, 3);
86#endif
87#else
88 const float x = std::clamp(v.x, 0.0f, 1.0f) * (gridPointsX - 1);
89 const float y = std::clamp(v.y, 0.0f, 1.0f) * (gridPointsY - 1);
90 const float z = std::clamp(v.z, 0.0f, 1.0f) * (gridPointsZ - 1);
91 const float w = std::clamp(v.w, 0.0f, 1.0f) * (gridPointsW - 1);
92 const uint32_t lox = static_cast<uint32_t>(std::floor(x));
93 const uint32_t hix = std::min(lox + 1, gridPointsX - 1);
94 const uint32_t loy = static_cast<uint32_t>(std::floor(y));
95 const uint32_t hiy = std::min(loy + 1, gridPointsY - 1);
96 const uint32_t loz = static_cast<uint32_t>(std::floor(z));
97 const uint32_t hiz = std::min(loz + 1, gridPointsZ - 1);
98 const uint32_t low = static_cast<uint32_t>(std::floor(w));
99 const uint32_t hiw = std::min(low + 1, gridPointsW - 1);
100 frac.x = x - static_cast<float>(lox);
101 frac.y = y - static_cast<float>(loy);
102 frac.z = z - static_cast<float>(loz);
103 frac.w = w - static_cast<float>(low);
104#endif
105 if (gridPointsW > 1) {
106 auto index = [&](qsizetype x, qsizetype y, qsizetype z, qsizetype w) -> qsizetype {
107 return x * gridPointsW * gridPointsZ * gridPointsY
108 + y * gridPointsW * gridPointsZ
109 + z * gridPointsW
110 + w;
111 };
112 QColorVector tmp[8];
113 // interpolate over w
114 tmp[0] = interpolate(table[index(lox, loy, loz, low)],
115 table[index(lox, loy, loz, hiw)], frac.w);
116 tmp[1] = interpolate(table[index(lox, loy, hiz, low)],
117 table[index(lox, loy, hiz, hiw)], frac.w);
118 tmp[2] = interpolate(table[index(lox, hiy, loz, low)],
119 table[index(lox, hiy, loz, hiw)], frac.w);
120 tmp[3] = interpolate(table[index(lox, hiy, hiz, low)],
121 table[index(lox, hiy, hiz, hiw)], frac.w);
122 tmp[4] = interpolate(table[index(hix, loy, loz, low)],
123 table[index(hix, loy, loz, hiw)], frac.w);
124 tmp[5] = interpolate(table[index(hix, loy, hiz, low)],
125 table[index(hix, loy, hiz, hiw)], frac.w);
126 tmp[6] = interpolate(table[index(hix, hiy, loz, low)],
127 table[index(hix, hiy, loz, hiw)], frac.w);
128 tmp[7] = interpolate(table[index(hix, hiy, hiz, low)],
129 table[index(hix, hiy, hiz, hiw)], frac.w);
130 // interpolate over z
131 for (int i = 0; i < 4; ++i)
132 interpolateIn(tmp[i * 2], tmp[i * 2 + 1], frac.z);
133 // interpolate over y
134 for (int i = 0; i < 2; ++i)
135 interpolateIn(tmp[i * 4], tmp[i * 4 + 2], frac.y);
136 // interpolate over x
137 interpolateIn(tmp[0], tmp[4], frac.x);
138 return tmp[0];
139 }
140 auto index = [&](qsizetype x, qsizetype y, qsizetype z) -> qsizetype {
141 return x * gridPointsZ * gridPointsY
142 + y * gridPointsZ
143 + z;
144 };
145 QColorVector tmp[8] = {
146 table[index(lox, loy, loz)],
147 table[index(lox, loy, hiz)],
148 table[index(lox, hiy, loz)],
149 table[index(lox, hiy, hiz)],
150 table[index(hix, loy, loz)],
151 table[index(hix, loy, hiz)],
152 table[index(hix, hiy, loz)],
153 table[index(hix, hiy, hiz)]
154 };
155 // interpolate over z
156 for (int i = 0; i < 4; ++i)
157 interpolateIn(tmp[i * 2], tmp[i * 2 + 1], frac.z);
158 // interpolate over y
159 for (int i = 0; i < 2; ++i)
160 interpolateIn(tmp[i * 4], tmp[i * 4 + 2], frac.y);
161 // interpolate over x
162 interpolateIn(tmp[0], tmp[4], frac.x);
163 return tmp[0];
164 }
165};
166
167QT_END_NAMESPACE
168
169#endif // QCOLORCLUT_H
QColorVector apply(const QColorVector &v) const
QList< QColorVector > table
uint32_t gridPointsY
uint32_t gridPointsW
bool isEmpty() const
uint32_t gridPointsX
uint32_t gridPointsZ
QColorMatrix inverted() const
static QColorMatrix identity()
static QColorMatrix fromScale(QColorVector v)
friend constexpr QColorMatrix operator*(const QColorMatrix &a, const QColorMatrix &o)
QColorVector map(const QColorVector &c) 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
The QColorTransform class is a transformation between color spaces.
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)
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
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]