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 QCOLORSPACE_P_H |
5 | #define QCOLORSPACE_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 "qcolorspace.h" |
19 | #include "qcolorclut_p.h" |
20 | #include "qcolormatrix_p.h" |
21 | #include "qcolortrc_p.h" |
22 | #include "qcolortrclut_p.h" |
23 | |
24 | #include <QtCore/qmutex.h> |
25 | #include <QtCore/qpoint.h> |
26 | #include <QtCore/qshareddata.h> |
27 | |
28 | #include <memory> |
29 | |
30 | QT_BEGIN_NAMESPACE |
31 | |
32 | class Q_AUTOTEST_EXPORT QColorSpacePrimaries |
33 | { |
34 | public: |
35 | QColorSpacePrimaries() = default; |
36 | QColorSpacePrimaries(QColorSpace::Primaries primaries); |
37 | QColorSpacePrimaries(QPointF whitePoint, |
38 | QPointF redPoint, |
39 | QPointF greenPoint, |
40 | QPointF bluePoint) |
41 | : whitePoint(whitePoint) |
42 | , redPoint(redPoint) |
43 | , greenPoint(greenPoint) |
44 | , bluePoint(bluePoint) |
45 | { } |
46 | |
47 | QColorMatrix toXyzMatrix() const; |
48 | bool areValid() const; |
49 | |
50 | QPointF whitePoint; |
51 | QPointF redPoint; |
52 | QPointF greenPoint; |
53 | QPointF bluePoint; |
54 | }; |
55 | |
56 | class QColorSpacePrivate : public QSharedData |
57 | { |
58 | public: |
59 | QColorSpacePrivate(); |
60 | QColorSpacePrivate(QColorSpace::NamedColorSpace namedColorSpace); |
61 | QColorSpacePrivate(QColorSpace::Primaries primaries, QColorSpace::TransferFunction transferFunction, float gamma); |
62 | QColorSpacePrivate(QColorSpace::Primaries primaries, const QList<uint16_t> &transferFunctionTable); |
63 | QColorSpacePrivate(const QColorSpacePrimaries &primaries, QColorSpace::TransferFunction transferFunction, float gamma); |
64 | QColorSpacePrivate(const QColorSpacePrimaries &primaries, const QList<uint16_t> &transferFunctionTable); |
65 | QColorSpacePrivate(const QColorSpacePrimaries &primaries, |
66 | const QList<uint16_t> &redTransferFunctionTable, |
67 | const QList<uint16_t> &greenTransferFunctionTable, |
68 | const QList<uint16_t> &blueRransferFunctionTable); |
69 | QColorSpacePrivate(QPointF whitePoint, QColorSpace::TransferFunction transferFunction, float gamma); |
70 | QColorSpacePrivate(QPointF whitePoint, const QList<uint16_t> &transferFunctionTable); |
71 | QColorSpacePrivate(const QColorSpacePrivate &other) = default; |
72 | |
73 | static const QColorSpacePrivate *get(const QColorSpace &colorSpace) |
74 | { |
75 | return colorSpace.d_ptr.get(); |
76 | } |
77 | |
78 | static QColorSpacePrivate *get(QColorSpace &colorSpace) |
79 | { |
80 | return colorSpace.d_ptr.get(); |
81 | } |
82 | |
83 | bool equals(const QColorSpacePrivate *other) const; |
84 | bool isValid() const noexcept; |
85 | |
86 | void initialize(); |
87 | void setToXyzMatrix(); |
88 | void setTransferFunction(); |
89 | void identifyColorSpace(); |
90 | void setTransferFunctionTable(const QList<uint16_t> &transferFunctionTable); |
91 | void setTransferFunctionTables(const QList<uint16_t> &redTransferFunctionTable, |
92 | const QList<uint16_t> &greenTransferFunctionTable, |
93 | const QList<uint16_t> &blueTransferFunctionTable); |
94 | QColorTransform transformationToColorSpace(const QColorSpacePrivate *out) const; |
95 | QColorTransform transformationToXYZ() const; |
96 | |
97 | bool isThreeComponentMatrix() const; |
98 | void clearElementListProcessingForEdit(); |
99 | |
100 | static constexpr QColorSpace::NamedColorSpace Unknown = QColorSpace::NamedColorSpace(0); |
101 | QColorSpace::NamedColorSpace namedColorSpace = Unknown; |
102 | |
103 | QColorSpace::Primaries primaries = QColorSpace::Primaries::Custom; |
104 | QColorSpace::TransferFunction transferFunction = QColorSpace::TransferFunction::Custom; |
105 | QColorSpace::TransformModel transformModel = QColorSpace::TransformModel::ThreeComponentMatrix; |
106 | QColorSpace::ColorModel colorModel = QColorSpace::ColorModel::Undefined; |
107 | float gamma = 0.0f; |
108 | QColorVector whitePoint; |
109 | |
110 | // Three component matrix data: |
111 | QColorTrc trc[3]; |
112 | QColorMatrix toXyz; |
113 | QColorMatrix chad; |
114 | |
115 | // Element list processing data: |
116 | struct TransferElement { |
117 | QColorTrc trc[4]; |
118 | }; |
119 | using Element = std::variant<TransferElement, QColorMatrix, QColorVector, QColorCLUT>; |
120 | bool isPcsLab = false; |
121 | // A = device, B = PCS |
122 | QList<Element> mAB, mBA; |
123 | |
124 | // Metadata |
125 | QString description; |
126 | QString userDescription; |
127 | QByteArray iccProfile; |
128 | |
129 | // Cached tables for three component matrix transform: |
130 | Q_CONSTINIT static QBasicMutex s_lutWriteLock; |
131 | struct LUT { |
132 | LUT() = default; |
133 | ~LUT() = default; |
134 | LUT(const LUT &other) |
135 | { |
136 | if (other.generated.loadAcquire()) { |
137 | table[0] = other.table[0]; |
138 | table[1] = other.table[1]; |
139 | table[2] = other.table[2]; |
140 | generated.storeRelaxed(newValue: 1); |
141 | } |
142 | } |
143 | std::shared_ptr<QColorTrcLut> &operator[](int i) { return table[i]; } |
144 | const std::shared_ptr<QColorTrcLut> &operator[](int i) const { return table[i]; } |
145 | std::shared_ptr<QColorTrcLut> table[3]; |
146 | QAtomicInt generated; |
147 | } mutable lut; |
148 | }; |
149 | |
150 | QT_END_NAMESPACE |
151 | |
152 | #endif // QCOLORSPACE_P_H |
153 | |