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
qrgba64.h
Go to the documentation of this file.
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
14
15class 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) { }
35public:
36 QRgba64() = default;
37
38 constexpr static
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
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 void setRed(quint16 _red) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << RedShift)) | (quint64(_red) << RedShift); }
78 void setGreen(quint16 _green) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << GreenShift)) | (quint64(_green) << GreenShift); }
79 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 constexpr quint8 green8() const { return div_257(green()); }
84 constexpr quint8 blue8() const { return div_257(blue()); }
85 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
148 {
149 rgba = _rgba;
150 return *this;
151 }
152
153private:
154 friend constexpr size_t qHash(QRgba64 key, size_t seed = 0) noexcept
155 { 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); }
162 {
163 if (isOpaque() || isTransparent())
164 return *this;
165 const quint32 a = alpha();
166 const quint16 r = quint16((red() * 0xffff + a/2) / a);
167 const quint16 g = quint16((green() * 0xffff + a/2) / a);
168 const quint16 b = quint16((blue() * 0xffff + a/2) / a);
169 return fromRgba64(r, g, b, quint16(a));
170 }
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
185
186constexpr inline QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
187{
188 return QRgba64::fromRgba64(r, g, b, a);
189}
190
191constexpr inline QRgba64 qRgba64(quint64 c)
192{
193 return QRgba64::fromRgba64(c);
194}
195
196constexpr inline QRgba64 qPremultiply(QRgba64 c)
197{
198 return c.premultiplied();
199}
200
201constexpr inline QRgba64 qUnpremultiply(QRgba64 c)
202{
203 return c.unpremultiplied();
204}
205
206inline constexpr uint qRed(QRgba64 rgb)
207{ return rgb.red8(); }
208
209inline constexpr uint qGreen(QRgba64 rgb)
210{ return rgb.green8(); }
211
212inline constexpr uint qBlue(QRgba64 rgb)
213{ return rgb.blue8(); }
214
215inline constexpr uint qAlpha(QRgba64 rgb)
216{ return rgb.alpha8(); }
217
218QT_END_NAMESPACE
219
220#endif // QRGBA64_H
\inmodule QtGui
Definition qimage.h:38
constexpr ushort toRgb16() const
Definition qrgba64.h:106
static constexpr QRgba64 fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
Definition qrgba64.h:44
constexpr quint16 red() const
Definition qrgba64.h:73
constexpr quint8 blue8() const
Definition qrgba64.h:84
constexpr quint16 alpha() const
Definition qrgba64.h:76
constexpr QRgba64 unpremultiplied() const
Definition qrgba64.h:133
constexpr quint16 green() const
Definition qrgba64.h:74
constexpr quint16 blue() const
Definition qrgba64.h:75
static constexpr QRgba64 fromRgba64(quint64 c)
Definition qrgba64.h:39
void setAlpha(quint16 _alpha)
Definition qrgba64.h:80
constexpr uint toArgb32() const
Definition qrgba64.h:86
static constexpr QRgba64 fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
Definition qrgba64.h:51
constexpr quint8 red8() const
Definition qrgba64.h:82
constexpr bool isOpaque() const
Definition qrgba64.h:64
constexpr quint8 green8() const
Definition qrgba64.h:83
constexpr quint8 alpha8() const
Definition qrgba64.h:85
void setGreen(quint16 _green)
Definition qrgba64.h:78
void setBlue(quint16 _blue)
Definition qrgba64.h:79
static constexpr QRgba64 fromArgb32(uint rgb)
Definition qrgba64.h:59
void setRed(quint16 _red)
Definition qrgba64.h:77
constexpr bool isTransparent() const
Definition qrgba64.h:68
QRgba64()=default
constexpr QRgba64 premultiplied() const
Definition qrgba64.h:111
Combined button and popup list for selecting options.
#define qCWarning(category,...)
static bool write_pbm_image(QIODevice *out, const QImage &sourceImage, QByteArrayView sourceFormat)
static bool read_pbm_header(QIODevice *device, char &type, int &w, int &h, quint16 &mcc)
static quint16 read_pbm_int(QIODevice *d, bool *ok, int maxDigits=-1)
static bool read_pbm_body(QIODevice *device, char type, int w, int h, quint16 mcc, QImage *outImage)
static QRgb scale_pbm_color(quint16 mx, quint16 rv, quint16 gv, quint16 bv)
static QT_BEGIN_NAMESPACE void discard_pbm_line(QIODevice *d)
constexpr uint qBlue(QRgba64 rgb)
Definition qrgba64.h:212
constexpr QRgba64 qRgba64(quint64 c)
Definition qrgba64.h:191
constexpr QRgba64 qUnpremultiply(QRgba64 c)
Definition qrgba64.h:201
constexpr QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
Definition qrgba64.h:186
constexpr uint qAlpha(QRgba64 rgb)
Definition qrgba64.h:215
constexpr QRgba64 qPremultiply(QRgba64 c)
Definition qrgba64.h:196
constexpr uint qRed(QRgba64 rgb)
Definition qrgba64.h:206
constexpr uint qGreen(QRgba64 rgb)
Definition qrgba64.h:209
Q_DECLARE_TYPEINFO(QRgba64, Q_PRIMITIVE_TYPE)