Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:08:16

0001 // Copyright (C) 2021 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QRGBAFLOAT_H
0005 #define QRGBAFLOAT_H
0006 
0007 #include <QtGui/qtguiglobal.h>
0008 #include <QtCore/qfloat16.h>
0009 
0010 #include <algorithm>
0011 #include <cmath>
0012 #include <type_traits>
0013 
0014 QT_BEGIN_NAMESPACE
0015 
0016 template<typename F>
0017 class alignas(sizeof(F) * 4) QRgbaFloat
0018 {
0019     static_assert(std::is_same<F, qfloat16>::value || std::is_same<F, float>::value);
0020 public:
0021     using Type = F;
0022 #if defined(__AVX512FP16__) && QFLOAT16_IS_NATIVE
0023     // AVX512FP16 has multiplication instructions
0024     using FastType = F;
0025 #else
0026     // use FP32 for multiplications
0027     using FastType = float;
0028 #endif
0029     F r;
0030     F g;
0031     F b;
0032     F a;
0033 
0034     static constexpr
0035     QRgbaFloat fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
0036     {
0037         constexpr FastType scale = FastType(1.0f / 65535.0f);
0038         return QRgbaFloat{
0039             F(red    * scale),
0040             F(green  * scale),
0041             F(blue   * scale),
0042             F(alpha  * scale) };
0043     }
0044 
0045     static constexpr
0046     QRgbaFloat fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
0047     {
0048         constexpr FastType scale = FastType(1.0f / 255.0f);
0049         return QRgbaFloat{
0050             F(red    * scale),
0051             F(green  * scale),
0052             F(blue   * scale),
0053             F(alpha  * scale) };
0054     }
0055     static constexpr
0056     QRgbaFloat fromArgb32(uint rgb)
0057     {
0058         return fromRgba(quint8(rgb >> 16), quint8(rgb >> 8), quint8(rgb), quint8(rgb >> 24));
0059     }
0060 
0061     constexpr bool isOpaque() const { return a >= FastType(1.0f); }
0062     constexpr bool isTransparent() const { return a <= FastType(0.0f); }
0063 
0064     constexpr float red()   const { return r; }
0065     constexpr float green() const { return g; }
0066     constexpr float blue()  const { return b; }
0067     constexpr float alpha() const { return a; }
0068     void setRed(float _red)     { r = F(_red); }
0069     void setGreen(float _green) { g = F(_green); }
0070     void setBlue(float _blue)   { b = F(_blue); }
0071     void setAlpha(float _alpha) { a = F(_alpha); }
0072 
0073     constexpr float redNormalized()   const { return clamp01(r); }
0074     constexpr float greenNormalized() const { return clamp01(g); }
0075     constexpr float blueNormalized()  const { return clamp01(b); }
0076     constexpr float alphaNormalized() const { return clamp01(a); }
0077 
0078     constexpr quint8 red8()   const { return qRound(redNormalized()   * FastType(255.0f)); }
0079     constexpr quint8 green8() const { return qRound(greenNormalized() * FastType(255.0f)); }
0080     constexpr quint8 blue8()  const { return qRound(blueNormalized()  * FastType(255.0f)); }
0081     constexpr quint8 alpha8() const { return qRound(alphaNormalized() * FastType(255.0f)); }
0082     constexpr uint toArgb32() const
0083     {
0084        return uint((alpha8() << 24) | (red8() << 16) | (green8() << 8) | blue8());
0085     }
0086 
0087     constexpr quint16 red16()   const { return qRound(redNormalized()   * FastType(65535.0f)); }
0088     constexpr quint16 green16() const { return qRound(greenNormalized() * FastType(65535.0f)); }
0089     constexpr quint16 blue16()  const { return qRound(blueNormalized()  * FastType(65535.0f)); }
0090     constexpr quint16 alpha16() const { return qRound(alphaNormalized() * FastType(65535.0f)); }
0091 
0092     constexpr Q_ALWAYS_INLINE QRgbaFloat premultiplied() const
0093     {
0094         return QRgbaFloat{r * a, g * a, b * a, a};
0095     }
0096     constexpr Q_ALWAYS_INLINE QRgbaFloat unpremultiplied() const
0097     {
0098         if (a <= F{0.0f})
0099             return QRgbaFloat{};    // default-initialization: zeroes
0100         if (a >= F{1.0f})
0101             return *this;
0102         const FastType ia = 1.0f / a;
0103         return QRgbaFloat{F(r * ia), F(g * ia), F(b * ia), F(a)};
0104     }
0105     constexpr bool operator==(QRgbaFloat f) const
0106     {
0107         return r == f.r && g == f.g && b == f.b && a == f.a;
0108     }
0109     constexpr bool operator!=(QRgbaFloat f) const
0110     {
0111         return !(*this == f);
0112     }
0113 
0114 private:
0115     constexpr static FastType clamp01(Type f)
0116     {
0117         return std::clamp(FastType(f), FastType(0.0f), FastType(1.0f));
0118     }
0119 };
0120 
0121 typedef QRgbaFloat<qfloat16> QRgbaFloat16;
0122 typedef QRgbaFloat<float> QRgbaFloat32;
0123 
0124 QT_END_NAMESPACE
0125 
0126 #endif // QRGBAFLOAT_H