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#include <QtCore/qprocessordetection.h>
10
12
13class 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) { }
33public:
34 QRgba64() = default;
35
36 constexpr static
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
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 void setRed(quint16 _red) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << RedShift)) | (quint64(_red) << RedShift); }
76 void setGreen(quint16 _green) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << GreenShift)) | (quint64(_green) << GreenShift); }
77 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 constexpr quint8 green8() const { return div_257(green()); }
82 constexpr quint8 blue8() const { return div_257(blue()); }
83 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
146 {
147 rgba = _rgba;
148 return *this;
149 }
150
151private:
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); }
157 {
158 if (isOpaque() || isTransparent())
159 return *this;
160 const quint32 a = alpha();
161 const quint16 r = quint16((red() * 0xffff + a/2) / a);
162 const quint16 g = quint16((green() * 0xffff + a/2) / a);
163 const quint16 b = quint16((blue() * 0xffff + a/2) / a);
164 return fromRgba64(r, g, b, quint16(a));
165 }
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
180
181constexpr inline QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
182{
183 return QRgba64::fromRgba64(r, g, b, a);
184}
185
186constexpr inline QRgba64 qRgba64(quint64 c)
187{
188 return QRgba64::fromRgba64(c);
189}
190
191constexpr inline QRgba64 qPremultiply(QRgba64 c)
192{
193 return c.premultiplied();
194}
195
196constexpr inline QRgba64 qUnpremultiply(QRgba64 c)
197{
198 return c.unpremultiplied();
199}
200
201inline constexpr uint qRed(QRgba64 rgb)
202{ return rgb.red8(); }
203
204inline constexpr uint qGreen(QRgba64 rgb)
205{ return rgb.green8(); }
206
207inline constexpr uint qBlue(QRgba64 rgb)
208{ return rgb.blue8(); }
209
210inline constexpr uint qAlpha(QRgba64 rgb)
211{ return rgb.alpha8(); }
212
213QT_END_NAMESPACE
214
215#endif // QRGBA64_H
\inmodule QtGui
Definition qimage.h:37
constexpr ushort toRgb16() const
Definition qrgba64.h:104
static constexpr QRgba64 fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
Definition qrgba64.h:42
constexpr quint16 red() const
Definition qrgba64.h:71
constexpr quint8 blue8() const
Definition qrgba64.h:82
constexpr quint16 alpha() const
Definition qrgba64.h:74
constexpr QRgba64 unpremultiplied() const
Definition qrgba64.h:131
constexpr quint16 green() const
Definition qrgba64.h:72
constexpr quint16 blue() const
Definition qrgba64.h:73
static constexpr QRgba64 fromRgba64(quint64 c)
Definition qrgba64.h:37
void setAlpha(quint16 _alpha)
Definition qrgba64.h:78
constexpr uint toArgb32() const
Definition qrgba64.h:84
static constexpr QRgba64 fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
Definition qrgba64.h:49
constexpr quint8 red8() const
Definition qrgba64.h:80
constexpr bool isOpaque() const
Definition qrgba64.h:62
constexpr quint8 green8() const
Definition qrgba64.h:81
constexpr quint8 alpha8() const
Definition qrgba64.h:83
void setGreen(quint16 _green)
Definition qrgba64.h:76
void setBlue(quint16 _blue)
Definition qrgba64.h:77
static constexpr QRgba64 fromArgb32(uint rgb)
Definition qrgba64.h:57
void setRed(quint16 _red)
Definition qrgba64.h:75
constexpr bool isTransparent() const
Definition qrgba64.h:66
QRgba64()=default
constexpr QRgba64 premultiplied() const
Definition qrgba64.h:109
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:207
constexpr QRgba64 qRgba64(quint64 c)
Definition qrgba64.h:186
constexpr QRgba64 qUnpremultiply(QRgba64 c)
Definition qrgba64.h:196
constexpr QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
Definition qrgba64.h:181
constexpr uint qAlpha(QRgba64 rgb)
Definition qrgba64.h:210
constexpr QRgba64 qPremultiply(QRgba64 c)
Definition qrgba64.h:191
constexpr uint qRed(QRgba64 rgb)
Definition qrgba64.h:201
constexpr uint qGreen(QRgba64 rgb)
Definition qrgba64.h:204
Q_DECLARE_TYPEINFO(QRgba64, Q_PRIMITIVE_TYPE)