Coverage Report

Created: 2026-03-31 07:41

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