qrgba64.h 8.3 KB

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