Coverage Report

Created: 2026-05-16 07:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/prefix/include/QtGui/qrgba64.h
Line
Count
Source
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 QRGBA64_H
6
#define QRGBA64_H
7
8
#include <QtGui/qtguiglobal.h>
9
10
#include <QtCore/qhashfunctions.h>
11
#include <QtCore/qprocessordetection.h>
12
13
QT_BEGIN_NAMESPACE
14
15
class QRgba64 {
16
    quint64 rgba;
17
18
    // Make sure that the representation always has the order: red green blue alpha, independent
19
    // of byte order. This way, vector operations that assume 4 16-bit values see the correct ones.
20
    enum Shifts {
21
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
22
        RedShift = 48,
23
        GreenShift = 32,
24
        BlueShift = 16,
25
        AlphaShift = 0
26
#else // little endian:
27
        RedShift = 0,
28
        GreenShift = 16,
29
        BlueShift = 32,
30
        AlphaShift = 48
31
#endif
32
    };
33
34
    Q_ALWAYS_INLINE explicit constexpr QRgba64(quint64 c) : rgba(c) { }
35
public:
36
    QRgba64() = default;
37
38
    constexpr static
39
    QRgba64 fromRgba64(quint64 c)
40
    {
41
        return QRgba64(c);
42
    }
43
    constexpr static
44
    QRgba64 fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
45
    {
46
        return fromRgba64(quint64(red)   << RedShift
47
                        | quint64(green) << GreenShift
48
                        | quint64(blue)  << BlueShift
49
                        | quint64(alpha) << AlphaShift);
50
    }
51
    constexpr static QRgba64 fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
52
    {
53
        QRgba64 rgb64 = fromRgba64(red, green, blue, alpha);
54
        // Expand the range so that 0x00 maps to 0x0000 and 0xff maps to 0xffff.
55
        rgb64.rgba |= rgb64.rgba << 8;
56
        return rgb64;
57
    }
58
    constexpr static
59
    QRgba64 fromArgb32(uint rgb)
60
    {
61
        return fromRgba(quint8(rgb >> 16), quint8(rgb >> 8), quint8(rgb), quint8(rgb >> 24));
62
    }
63
64
    constexpr bool isOpaque() const
65
    {
66
        return (rgba & alphaMask()) == alphaMask();
67
    }
68
    constexpr bool isTransparent() const
69
    {
70
        return (rgba & alphaMask()) == 0;
71
    }
72
73
    constexpr quint16 red()   const { return quint16(rgba >> RedShift);   }
74
    constexpr quint16 green() const { return quint16(rgba >> GreenShift); }
75
    constexpr quint16 blue()  const { return quint16(rgba >> BlueShift);  }
76
    constexpr quint16 alpha() const { return quint16(rgba >> AlphaShift); }
77
0
    void setRed(quint16 _red)     { rgba = (rgba & ~(Q_UINT64_C(0xffff) << RedShift))   | (quint64(_red) << RedShift); }
78
0
    void setGreen(quint16 _green) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << GreenShift)) | (quint64(_green) << GreenShift); }
79
0
    void setBlue(quint16 _blue)   { rgba = (rgba & ~(Q_UINT64_C(0xffff) << BlueShift))  | (quint64(_blue) << BlueShift); }
80
    void setAlpha(quint16 _alpha) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << AlphaShift)) | (quint64(_alpha) << AlphaShift); }
81
82
    constexpr quint8 red8()   const { return div_257(red()); }
83
0
    constexpr quint8 green8() const { return div_257(green()); }
84
0
    constexpr quint8 blue8()  const { return div_257(blue()); }
85
0
    constexpr quint8 alpha8() const { return div_257(alpha()); }
86
    constexpr uint toArgb32() const
87
    {
88
        quint64 br = rgba & Q_UINT64_C(0xffff0000ffff);
89
        quint64 ag = (rgba >> 16) & Q_UINT64_C(0xffff0000ffff);
90
        br += Q_UINT64_C(0x8000000080);
91
        ag += Q_UINT64_C(0x8000000080);
92
        br = (br - ((br >> 8) & Q_UINT64_C(0xffff0000ffff))) >> 8;
93
        ag = (ag - ((ag >> 8) & Q_UINT64_C(0xffff0000ffff)));
94
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
95
        return ((br << 24) & 0xff000000)
96
             | ((ag >> 24) & 0xff0000)
97
             | ((br >> 24) & 0xff00)
98
             | ((ag >> 8)  & 0xff);
99
#else
100
        return ((ag >> 16) & 0xff000000)
101
             | ((br << 16) & 0xff0000)
102
             | (ag         & 0xff00)
103
             | ((br >> 32) & 0xff);
104
#endif
105
    }
106
    constexpr ushort toRgb16() const
107
    {
108
        return ushort((red() & 0xf800) | ((green() >> 10) << 5) | (blue() >> 11));
109
    }
110
111
    constexpr QRgba64 premultiplied() const
112
    {
113
        if (isOpaque())
114
            return *this;
115
        if (isTransparent())
116
            return QRgba64::fromRgba64(0);
117
        const quint64 a = alpha();
118
        quint64 br = (rgba & Q_UINT64_C(0xffff0000ffff)) * a;
119
        quint64 ag = ((rgba >> 16) & Q_UINT64_C(0xffff0000ffff)) * a;
120
        br = (br + ((br >> 16) & Q_UINT64_C(0xffff0000ffff)) + Q_UINT64_C(0x800000008000));
121
        ag = (ag + ((ag >> 16) & Q_UINT64_C(0xffff0000ffff)) + Q_UINT64_C(0x800000008000));
122
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
123
        ag = ag & Q_UINT64_C(0xffff0000ffff0000);
124
        br = (br >> 16) & Q_UINT64_C(0xffff00000000);
125
        return fromRgba64(a | br | ag);
126
#else
127
        br = (br >> 16) & Q_UINT64_C(0xffff0000ffff);
128
        ag = ag & Q_UINT64_C(0xffff0000);
129
        return fromRgba64((a << 48) | br | ag);
130
#endif
131
    }
132
133
    constexpr QRgba64 unpremultiplied() const
134
    {
135
#if Q_PROCESSOR_WORDSIZE < 8
136
        return unpremultiplied_32bit();
137
#else
138
        return unpremultiplied_64bit();
139
#endif
140
    }
141
142
    constexpr operator quint64() const
143
    {
144
        return rgba;
145
    }
146
147
    QRgba64 &operator=(quint64 _rgba) noexcept
148
    {
149
        rgba = _rgba;
150
        return *this;
151
    }
152
153
private:
154
    friend constexpr size_t qHash(QRgba64 key, size_t seed = 0) noexcept
155
0
    { return QHashPrivate::ex1to2arg(key.rgba, seed); }
156
157
    Q_ALWAYS_INLINE static constexpr quint64 alphaMask() { return Q_UINT64_C(0xffff) << AlphaShift; }
158
159
    Q_ALWAYS_INLINE static constexpr quint8 div_257_floor(uint x) { return quint8((x - (x >> 8)) >> 8); }
160
    Q_ALWAYS_INLINE static constexpr quint8 div_257(quint16 x) { return div_257_floor(x + 128U); }
161
    Q_ALWAYS_INLINE constexpr QRgba64 unpremultiplied_32bit() const
162
0
    {
163
0
        if (isOpaque() || isTransparent())
164
0
            return *this;
165
0
        const quint32 a = alpha();
166
0
        const quint16 r = quint16((red()   * 0xffff + a/2) / a);
167
0
        const quint16 g = quint16((green() * 0xffff + a/2) / a);
168
0
        const quint16 b = quint16((blue()  * 0xffff + a/2) / a);
169
0
        return fromRgba64(r, g, b, quint16(a));
170
0
    }
171
    Q_ALWAYS_INLINE constexpr QRgba64 unpremultiplied_64bit() const
172
    {
173
        if (isOpaque() || isTransparent())
174
            return *this;
175
        const quint64 a = alpha();
176
        const quint64 fa = (Q_UINT64_C(0xffff00008000) + a/2) / a;
177
        const quint16 r = quint16((red()   * fa + 0x80000000) >> 32);
178
        const quint16 g = quint16((green() * fa + 0x80000000) >> 32);
179
        const quint16 b = quint16((blue()  * fa + 0x80000000) >> 32);
180
        return fromRgba64(r, g, b, quint16(a));
181
    }
182
};
183
184
Q_DECLARE_TYPEINFO(QRgba64, Q_PRIMITIVE_TYPE);
185
186
constexpr inline QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
187
0
{
188
0
    return QRgba64::fromRgba64(r, g, b, a);
189
0
}
190
191
constexpr inline QRgba64 qRgba64(quint64 c)
192
0
{
193
0
    return QRgba64::fromRgba64(c);
194
0
}
195
196
constexpr inline QRgba64 qPremultiply(QRgba64 c)
197
0
{
198
0
    return c.premultiplied();
199
0
}
200
201
constexpr inline QRgba64 qUnpremultiply(QRgba64 c)
202
0
{
203
0
    return c.unpremultiplied();
204
0
}
205
206
inline constexpr uint qRed(QRgba64 rgb)
207
0
{ return rgb.red8(); }
208
209
inline constexpr uint qGreen(QRgba64 rgb)
210
0
{ return rgb.green8(); }
211
212
inline constexpr uint qBlue(QRgba64 rgb)
213
0
{ return rgb.blue8(); }
214
215
inline constexpr uint qAlpha(QRgba64 rgb)
216
0
{ return rgb.alpha8(); }
217
218
QT_END_NAMESPACE
219
220
#endif // QRGBA64_H