summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qcolorspace_p.h
blob: 4ec801b16b3de4f59151cd042a8d4c6d6d9594fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QCOLORSPACE_P_H
#define QCOLORSPACE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qcolorspace.h"
#include "qcolorclut_p.h"
#include "qcolormatrix_p.h"
#include "qcolortrc_p.h"
#include "qcolortrclut_p.h"

#include <QtCore/qmutex.h>
#include <QtCore/qpoint.h>
#include <QtCore/qshareddata.h>

#include <memory>

QT_BEGIN_NAMESPACE

class Q_AUTOTEST_EXPORT QColorSpacePrimaries
{
public:
   QColorSpacePrimaries() = default;
   QColorSpacePrimaries(QColorSpace::Primaries primaries);
   QColorSpacePrimaries(QPointF whitePoint,
                        QPointF redPoint,
                        QPointF greenPoint,
                        QPointF bluePoint)
           : whitePoint(whitePoint)
           , redPoint(redPoint)
           , greenPoint(greenPoint)
           , bluePoint(bluePoint)
   { }

   QColorMatrix toXyzMatrix() const;
   bool areValid() const;

   QPointF whitePoint;
   QPointF redPoint;
   QPointF greenPoint;
   QPointF bluePoint;
};

class QColorSpacePrivate : public QSharedData
{
public:
    QColorSpacePrivate();
    QColorSpacePrivate(QColorSpace::NamedColorSpace namedColorSpace);
    QColorSpacePrivate(QColorSpace::Primaries primaries, QColorSpace::TransferFunction transferFunction, float gamma);
    QColorSpacePrivate(QColorSpace::Primaries primaries, const QList<uint16_t> &transferFunctionTable);
    QColorSpacePrivate(const QColorSpacePrimaries &primaries, QColorSpace::TransferFunction transferFunction, float gamma);
    QColorSpacePrivate(const QColorSpacePrimaries &primaries, const QList<uint16_t> &transferFunctionTable);
    QColorSpacePrivate(const QColorSpacePrimaries &primaries,
                       const QList<uint16_t> &redTransferFunctionTable,
                       const QList<uint16_t> &greenTransferFunctionTable,
                       const QList<uint16_t> &blueRransferFunctionTable);
    QColorSpacePrivate(const QPointF &whitePoint, QColorSpace::TransferFunction transferFunction, float gamma);
    QColorSpacePrivate(const QPointF &whitePoint, const QList<uint16_t> &transferFunctionTable);
    QColorSpacePrivate(const QColorSpacePrivate &other) = default;

    static const QColorSpacePrivate *get(const QColorSpace &colorSpace)
    {
        return colorSpace.d_ptr.get();
    }

    static QColorSpacePrivate *get(QColorSpace &colorSpace)
    {
        return colorSpace.d_ptr.get();
    }

    bool equals(const QColorSpacePrivate *other) const;
    bool isValid() const noexcept;

    void initialize();
    void setToXyzMatrix();
    void setTransferFunction();
    void identifyColorSpace();
    void setTransferFunctionTable(const QList<uint16_t> &transferFunctionTable);
    void setTransferFunctionTables(const QList<uint16_t> &redTransferFunctionTable,
                                   const QList<uint16_t> &greenTransferFunctionTable,
                                   const QList<uint16_t> &blueTransferFunctionTable);
    QColorTransform transformationToColorSpace(const QColorSpacePrivate *out) const;
    QColorTransform transformationToXYZ() const;

    bool isThreeComponentMatrix() const;
    void clearElementListProcessingForEdit();

    static constexpr QColorSpace::NamedColorSpace Unknown = QColorSpace::NamedColorSpace(0);
    QColorSpace::NamedColorSpace namedColorSpace = Unknown;

    QColorSpace::Primaries primaries = QColorSpace::Primaries::Custom;
    QColorSpace::TransferFunction transferFunction = QColorSpace::TransferFunction::Custom;
    QColorSpace::TransformModel transformModel = QColorSpace::TransformModel::ThreeComponentMatrix;
    QColorSpace::ColorModel colorModel = QColorSpace::ColorModel::Undefined;
    float gamma = 0.0f;
    QColorVector whitePoint;

    // Three component matrix data:
    QColorTrc trc[3];
    QColorMatrix toXyz;
    QColorMatrix chad;

    // Element list processing data:
    struct TransferElement {
        QColorTrc trc[4];
    };
    using Element = std::variant<TransferElement, QColorMatrix, QColorVector, QColorCLUT>;
    bool isPcsLab = false;
    // A = device, B = PCS
    QList<Element> mAB, mBA;

    // Metadata
    QString description;
    QString userDescription;
    QByteArray iccProfile;

    // Cached tables for three component matrix transform:
    Q_CONSTINIT static QBasicMutex s_lutWriteLock;
    struct LUT {
        LUT() = default;
        ~LUT() = default;
        LUT(const LUT &other)
        {
            if (other.generated.loadAcquire()) {
                table[0] = other.table[0];
                table[1] = other.table[1];
                table[2] = other.table[2];
                generated.storeRelaxed(1);
            }
        }
        std::shared_ptr<QColorTrcLut> &operator[](int i) { return table[i]; }
        const std::shared_ptr<QColorTrcLut> &operator[](int i) const  { return table[i]; }
        std::shared_ptr<QColorTrcLut> table[3];
        QAtomicInt generated;
    } mutable lut;
};

QT_END_NAMESPACE

#endif // QCOLORSPACE_P_H