QtBase  v6.3.1
qrgba64.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QRGBA64_H
41 #define QRGBA64_H
42 
43 #include <QtGui/qtguiglobal.h>
44 #include <QtCore/qprocessordetection.h>
45 
47 
48 class QRgba64 {
49  quint64 rgba;
50 
51  // Make sure that the representation always has the order: red green blue alpha, independent
52  // of byte order. This way, vector operations that assume 4 16-bit values see the correct ones.
53  enum Shifts {
54 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
55  RedShift = 48,
56  GreenShift = 32,
57  BlueShift = 16,
58  AlphaShift = 0
59 #else // little endian:
60  RedShift = 0,
61  GreenShift = 16,
62  BlueShift = 32,
63  AlphaShift = 48
64 #endif
65  };
66 
67  explicit Q_ALWAYS_INLINE constexpr QRgba64(quint64 c) : rgba(c) { }
68 public:
69  QRgba64() = default;
70 
71  constexpr static
73  {
74  return QRgba64(c);
75  }
76  constexpr static
78  {
79  return fromRgba64(quint64(red) << RedShift
80  | quint64(green) << GreenShift
81  | quint64(blue) << BlueShift
82  | quint64(alpha) << AlphaShift);
83  }
85  {
86  QRgba64 rgb64 = fromRgba64(red, green, blue, alpha);
87  // Expand the range so that 0x00 maps to 0x0000 and 0xff maps to 0xffff.
88  rgb64.rgba |= rgb64.rgba << 8;
89  return rgb64;
90  }
91  constexpr static
93  {
94  return fromRgba(quint8(rgb >> 16), quint8(rgb >> 8), quint8(rgb), quint8(rgb >> 24));
95  }
96 
97  constexpr bool isOpaque() const
98  {
99  return (rgba & alphaMask()) == alphaMask();
100  }
101  constexpr bool isTransparent() const
102  {
103  return (rgba & alphaMask()) == 0;
104  }
105 
106  constexpr quint16 red() const { return quint16(rgba >> RedShift); }
107  constexpr quint16 green() const { return quint16(rgba >> GreenShift); }
108  constexpr quint16 blue() const { return quint16(rgba >> BlueShift); }
109  constexpr quint16 alpha() const { return quint16(rgba >> AlphaShift); }
110  void setRed(quint16 _red) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << RedShift)) | (quint64(_red) << RedShift); }
111  void setGreen(quint16 _green) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << GreenShift)) | (quint64(_green) << GreenShift); }
112  void setBlue(quint16 _blue) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << BlueShift)) | (quint64(_blue) << BlueShift); }
113  void setAlpha(quint16 _alpha) { rgba = (rgba & ~(Q_UINT64_C(0xffff) << AlphaShift)) | (quint64(_alpha) << AlphaShift); }
114 
115  constexpr quint8 red8() const { return div_257(red()); }
116  constexpr quint8 green8() const { return div_257(green()); }
117  constexpr quint8 blue8() const { return div_257(blue()); }
118  constexpr quint8 alpha8() const { return div_257(alpha()); }
119  constexpr uint toArgb32() const
120  {
121  quint64 br = rgba & Q_UINT64_C(0xffff0000ffff);
122  quint64 ag = (rgba >> 16) & Q_UINT64_C(0xffff0000ffff);
123  br += Q_UINT64_C(0x8000000080);
124  ag += Q_UINT64_C(0x8000000080);
125  br = (br - ((br >> 8) & Q_UINT64_C(0xffff0000ffff))) >> 8;
126  ag = (ag - ((ag >> 8) & Q_UINT64_C(0xffff0000ffff)));
127 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
128  return ((br << 24) & 0xff000000)
129  | ((ag >> 24) & 0xff0000)
130  | ((br >> 24) & 0xff00)
131  | ((ag >> 8) & 0xff);
132 #else
133  return ((ag >> 16) & 0xff000000)
134  | ((br << 16) & 0xff0000)
135  | (ag & 0xff00)
136  | ((br >> 32) & 0xff);
137 #endif
138  }
139  constexpr ushort toRgb16() const
140  {
141  return ushort((red() & 0xf800) | ((green() >> 10) << 5) | (blue() >> 11));
142  }
143 
144  constexpr QRgba64 premultiplied() const
145  {
146  if (isOpaque())
147  return *this;
148  if (isTransparent())
149  return QRgba64::fromRgba64(0);
150  const quint64 a = alpha();
151  quint64 br = (rgba & Q_UINT64_C(0xffff0000ffff)) * a;
152  quint64 ag = ((rgba >> 16) & Q_UINT64_C(0xffff0000ffff)) * a;
153  br = (br + ((br >> 16) & Q_UINT64_C(0xffff0000ffff)) + Q_UINT64_C(0x800000008000));
154  ag = (ag + ((ag >> 16) & Q_UINT64_C(0xffff0000ffff)) + Q_UINT64_C(0x800000008000));
155 #if Q_BYTE_ORDER == Q_BIG_ENDIAN
156  ag = ag & Q_UINT64_C(0xffff0000ffff0000);
157  br = (br >> 16) & Q_UINT64_C(0xffff00000000);
158  return fromRgba64(a | br | ag);
159 #else
160  br = (br >> 16) & Q_UINT64_C(0xffff0000ffff);
161  ag = ag & Q_UINT64_C(0xffff0000);
162  return fromRgba64((a << 48) | br | ag);
163 #endif
164  }
165 
166  constexpr QRgba64 unpremultiplied() const
167  {
168 #if Q_PROCESSOR_WORDSIZE < 8
169  return unpremultiplied_32bit();
170 #else
171  return unpremultiplied_64bit();
172 #endif
173  }
174 
175  constexpr operator quint64() const
176  {
177  return rgba;
178  }
179 
180  QRgba64 &operator=(quint64 _rgba) noexcept
181  {
182  rgba = _rgba;
183  return *this;
184  }
185 
186 private:
187  static constexpr Q_ALWAYS_INLINE quint64 alphaMask() { return Q_UINT64_C(0xffff) << AlphaShift; }
188 
189  static constexpr Q_ALWAYS_INLINE quint8 div_257_floor(uint x) { return quint8((x - (x >> 8)) >> 8); }
190  static constexpr Q_ALWAYS_INLINE quint8 div_257(quint16 x) { return div_257_floor(x + 128U); }
191  constexpr Q_ALWAYS_INLINE QRgba64 unpremultiplied_32bit() const
192  {
193  if (isOpaque() || isTransparent())
194  return *this;
195  const quint32 a = alpha();
196  const quint16 r = quint16((red() * 0xffff + a/2) / a);
197  const quint16 g = quint16((green() * 0xffff + a/2) / a);
198  const quint16 b = quint16((blue() * 0xffff + a/2) / a);
199  return fromRgba64(r, g, b, quint16(a));
200  }
201  constexpr Q_ALWAYS_INLINE QRgba64 unpremultiplied_64bit() const
202  {
203  if (isOpaque() || isTransparent())
204  return *this;
205  const quint64 a = alpha();
206  const quint64 fa = (Q_UINT64_C(0xffff00008000) + a/2) / a;
207  const quint16 r = quint16((red() * fa + 0x80000000) >> 32);
208  const quint16 g = quint16((green() * fa + 0x80000000) >> 32);
209  const quint16 b = quint16((blue() * fa + 0x80000000) >> 32);
210  return fromRgba64(r, g, b, quint16(a));
211  }
212 };
213 
215 
217 {
218  return QRgba64::fromRgba64(r, g, b, a);
219 }
220 
221 constexpr inline QRgba64 qRgba64(quint64 c)
222 {
223  return QRgba64::fromRgba64(c);
224 }
225 
226 constexpr inline QRgba64 qPremultiply(QRgba64 c)
227 {
228  return c.premultiplied();
229 }
230 
231 constexpr inline QRgba64 qUnpremultiply(QRgba64 c)
232 {
233  return c.unpremultiplied();
234 }
235 
236 inline constexpr uint qRed(QRgba64 rgb)
237 { return rgb.red8(); }
238 
239 inline constexpr uint qGreen(QRgba64 rgb)
240 { return rgb.green8(); }
241 
242 inline constexpr uint qBlue(QRgba64 rgb)
243 { return rgb.blue8(); }
244 
245 inline constexpr uint qAlpha(QRgba64 rgb)
246 { return rgb.alpha8(); }
247 
249 
250 #endif // QRGBA64_H
int qAlpha(QRgb rgba)
Definition: qrgb.h:63
QRgb qUnpremultiply(QRgb rgb)
Definition: qrgb.h:96
int qRed(QRgb rgb)
Definition: qrgb.h:54
int qGreen(QRgb rgb)
Definition: qrgb.h:57
int qBlue(QRgb rgb)
Definition: qrgb.h:60
QRgb qPremultiply(QRgb rgb)
Definition: qrgb.h:81
constexpr static QRgba64 fromArgb32(uint rgb)
Definition: qrgba64.h:92
constexpr ushort toRgb16() const
Definition: qrgba64.h:139
constexpr quint16 red() const
Definition: qrgba64.h:106
constexpr quint8 blue8() const
Definition: qrgba64.h:117
constexpr quint16 alpha() const
Definition: qrgba64.h:109
constexpr QRgba64 unpremultiplied() const
Definition: qrgba64.h:166
constexpr quint16 green() const
Definition: qrgba64.h:107
constexpr static QRgba64 fromRgba64(quint64 c)
Definition: qrgba64.h:72
constexpr quint16 blue() const
Definition: qrgba64.h:108
void setAlpha(quint16 _alpha)
Definition: qrgba64.h:113
QRgba64 & operator=(quint64 _rgba) noexcept
Definition: qrgba64.h:180
constexpr uint toArgb32() const
Definition: qrgba64.h:119
constexpr quint8 red8() const
Definition: qrgba64.h:115
constexpr static QRgba64 fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
Definition: qrgba64.h:84
constexpr bool isOpaque() const
Definition: qrgba64.h:97
constexpr quint8 green8() const
Definition: qrgba64.h:116
constexpr quint8 alpha8() const
Definition: qrgba64.h:118
void setGreen(quint16 _green)
Definition: qrgba64.h:111
void setBlue(quint16 _blue)
Definition: qrgba64.h:112
void setRed(quint16 _red)
Definition: qrgba64.h:110
constexpr static QRgba64 fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
Definition: qrgba64.h:77
constexpr bool isTransparent() const
Definition: qrgba64.h:101
QRgba64()=default
constexpr QRgba64 premultiplied() const
Definition: qrgba64.h:144
#define rgb(r, g, b)
Definition: qcolor.cpp:157
#define Q_UINT64_C(c)
Definition: qglobal.h:296
unsigned int quint32
Definition: qglobal.h:288
unsigned short quint16
Definition: qglobal.h:286
unsigned long long quint64
Definition: qglobal.h:299
unsigned int uint
Definition: qglobal.h:334
unsigned short ushort
Definition: qglobal.h:333
unsigned char quint8
Definition: qglobal.h:284
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLboolean r
[2]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean GLboolean g
GLbyte GLbyte blue
Definition: qopenglext.h:385
const GLubyte * c
Definition: qopenglext.h:12701
GLfloat GLfloat GLfloat alpha
Definition: qopenglext.h:418
GLbyte green
Definition: qopenglext.h:385
constexpr QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
Definition: qrgba64.h:216
Q_DECLARE_TYPEINFO(QRgba64, Q_PRIMITIVE_TYPE)
@ Q_PRIMITIVE_TYPE
Definition: qtypeinfo.h:155