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
qrgbafloat.h
Go to the documentation of this file.
1// Copyright (C) 2021 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 QRGBAFLOAT_H
6#define QRGBAFLOAT_H
7
8#include <QtGui/qtguiglobal.h>
9#include <QtCore/qfloat16.h>
10
11#include <algorithm>
12#include <cmath>
13#include <type_traits>
14
15QT_BEGIN_NAMESPACE
16
17template<typename F>
18class alignas(sizeof(F) * 4) QRgbaFloat
19{
20 static_assert(std::is_same<F, qfloat16>::value || std::is_same<F, float>::value);
21public:
22 using Type = F;
23#if defined(__AVX512FP16__) && QFLOAT16_IS_NATIVE
24 // AVX512FP16 has multiplication instructions
25 using FastType = F;
26#else
27 // use FP32 for multiplications
28 using FastType = float;
29#endif
30 F r;
31 F g;
32 F b;
33 F a;
34
35 static constexpr
36 QRgbaFloat fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
37 {
38 constexpr FastType scale = FastType(1.0f / 65535.0f);
39 return QRgbaFloat{
40 F(red * scale),
41 F(green * scale),
42 F(blue * scale),
43 F(alpha * scale) };
44 }
45
46 static constexpr
47 QRgbaFloat fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
48 {
49 constexpr FastType scale = FastType(1.0f / 255.0f);
50 return QRgbaFloat{
51 F(red * scale),
52 F(green * scale),
53 F(blue * scale),
54 F(alpha * scale) };
55 }
56 static constexpr
57 QRgbaFloat fromArgb32(uint rgb)
58 {
59 return fromRgba(quint8(rgb >> 16), quint8(rgb >> 8), quint8(rgb), quint8(rgb >> 24));
60 }
61
62 constexpr bool isOpaque() const { return a >= FastType(1.0f); }
63 constexpr bool isTransparent() const { return a <= FastType(0.0f); }
64
65 constexpr float red() const { return r; }
66 constexpr float green() const { return g; }
67 constexpr float blue() const { return b; }
68 constexpr float alpha() const { return a; }
69 void setRed(float _red) { r = F(_red); }
70 void setGreen(float _green) { g = F(_green); }
71 void setBlue(float _blue) { b = F(_blue); }
72 void setAlpha(float _alpha) { a = F(_alpha); }
73
74 constexpr float redNormalized() const { return clamp01(r); }
75 constexpr float greenNormalized() const { return clamp01(g); }
76 constexpr float blueNormalized() const { return clamp01(b); }
77 constexpr float alphaNormalized() const { return clamp01(a); }
78
79 constexpr quint8 red8() const { return qRound(redNormalized() * FastType(255.0f)); }
80 constexpr quint8 green8() const { return qRound(greenNormalized() * FastType(255.0f)); }
81 constexpr quint8 blue8() const { return qRound(blueNormalized() * FastType(255.0f)); }
82 constexpr quint8 alpha8() const { return qRound(alphaNormalized() * FastType(255.0f)); }
83 constexpr uint toArgb32() const
84 {
85 return uint((alpha8() << 24) | (red8() << 16) | (green8() << 8) | blue8());
86 }
87
88 constexpr quint16 red16() const { return qRound(redNormalized() * FastType(65535.0f)); }
89 constexpr quint16 green16() const { return qRound(greenNormalized() * FastType(65535.0f)); }
90 constexpr quint16 blue16() const { return qRound(blueNormalized() * FastType(65535.0f)); }
91 constexpr quint16 alpha16() const { return qRound(alphaNormalized() * FastType(65535.0f)); }
92
93 Q_ALWAYS_INLINE constexpr QRgbaFloat premultiplied() const
94 {
95 return QRgbaFloat{r * a, g * a, b * a, a};
96 }
97 Q_ALWAYS_INLINE constexpr QRgbaFloat unpremultiplied() const
98 {
99 if (a <= F{0.0f})
100 return QRgbaFloat{}; // default-initialization: zeroes
101 if (a >= F{1.0f})
102 return *this;
103 const FastType ia = FastType(1.0f) / FastType(a);
104 return QRgbaFloat{F(r * ia), F(g * ia), F(b * ia), F(a)};
105 }
106 constexpr bool operator==(QRgbaFloat f) const
107 {
108 return r == f.r && g == f.g && b == f.b && a == f.a;
109 }
110 constexpr bool operator!=(QRgbaFloat f) const
111 {
112 return !(*this == f);
113 }
114
115private:
116 constexpr static FastType clamp01(Type f)
117 {
118 return std::clamp(FastType(f), FastType(0.0f), FastType(1.0f));
119 }
120};
121
124
125QT_END_NAMESPACE
126
127#endif // QRGBAFLOAT_H
The QColorTransform class is a transformation between color spaces.
QColorTransform(QColorTransform &&colorTransform)=default
friend bool operator!=(const QColorTransform &ct1, const QColorTransform &ct2)
QColorTransform & operator=(const QColorTransform &other) noexcept
QColorTransform() noexcept=default
Q_GUI_EXPORT ~QColorTransform()
friend bool operator==(const QColorTransform &ct1, const QColorTransform &ct2)
\inmodule QtCore\reentrant
Definition qdatastream.h:50
QString errorString() const
Returns a human readable description of the last error that occurred.
void setDevice(QIODevice *device)
Sets QImageReader's device to device.
int quality() const
Returns the quality setting of the image format.
QString fileName() const
If the currently assigned device is a QFile, or if setFileName() has been called, this function retur...
QImageIOHandler::Transformations transformation() const
QIODevice * device() const
Returns the device currently assigned to QImageReader, or \nullptr if no device has been assigned.
QByteArray subType() const
bool supportsOption(QImageIOHandler::ImageOption option) const
Returns true if the reader supports option; otherwise returns false.
QList< QByteArray > supportedSubTypes() const
QByteArray format() const
Returns the format QImageReader uses for reading images.
void setQuality(int quality)
Sets the quality setting of the image format to quality.
ImageReaderError error() const
Returns the type of error that occurred last.
The QImageWriter class provides a format independent interface for writing images to files or other d...
QImageWriter(const QString &fileName, const QByteArray &format=QByteArray())
Constructs a QImageWriter objects that will write to a file with the name fileName,...
QImageWriter()
Constructs an empty QImageWriter object.
void setCompression(int compression)
This is an image format specific function that set the compression of an image.
bool write(const QImage &image)
Writes the image image to the assigned device or file name.
bool canWrite() const
Returns true if QImageWriter can write the image; i.e., the image format is supported and the assigne...
void setTransformation(QImageIOHandler::Transformations orientation)
QImageWriter(QIODevice *device, const QByteArray &format)
Constructs a QImageWriter object using the device device and image format format.
static QList< QByteArray > supportedMimeTypes()
Returns the list of MIME types supported by QImageWriter.
void setSubType(const QByteArray &type)
void setFileName(const QString &fileName)
Sets the file name of QImageWriter to fileName.
bool optimizedWrite() const
bool progressiveScanWrite() const
static QList< QByteArray > imageFormatsForMimeType(const QByteArray &mimeType)
void setOptimizedWrite(bool optimize)
static QList< QByteArray > supportedImageFormats()
Returns the list of image formats supported by QImageWriter.
int compression() const
Returns the compression of the image.
void setFormat(const QByteArray &format)
Sets the format QImageWriter will use when writing images, to format.
void setProgressiveScanWrite(bool progressive)
void setText(const QString &key, const QString &text)
Sets the image text associated with the key key to text.
Definition qmap.h:190
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:21
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:57
Combined button and popup list for selecting options.
QRgbaFloat< float > QRgbaFloat32
QRgbaFloat< qfloat16 > QRgbaFloat16
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
#define IWX_MSB(b)
Definition qimage.cpp:4428
static QImage rotated90(const QImage &src)
Definition qimage.cpp:4781
static void copyMetadata(QImage *dst, const QImage &src)
Definition qimage.cpp:1186
#define QT_XFORM_TYPE_LSBFIRST
Definition qimage.cpp:51
static void copyMetadata(QImageData *dst, const QImageData *src)
Definition qimage.cpp:1177
#define QT_XFORM_TYPE_MSBFIRST
Definition qimage.cpp:50
static int next_qimage_serial_number()
Definition qimage.cpp:90
#define QIMAGE_SANITYCHECK_MEMORY(image)
Definition qimage.cpp:68
static void copyPhysicalMetadata(QImageData *dst, const QImageData *src)
Definition qimage.cpp:1170
#define IWX_LSB(b)
Definition qimage.cpp:4437
static QImage rotated270(const QImage &src)
Definition qimage.cpp:4825
QMap< QString, QString > qt_getImageText(const QImage &image, const QString &description)
Definition qimage.cpp:6535
Q_GUI_EXPORT void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient)
Definition qimage.cpp:6522
#define PIX(x, y)
static QImage rotated180(const QImage &src)
Definition qimage.cpp:4807
static Qt::Orientations toOrientations(QImageIOHandler::Transformations orient)
Definition qimage.cpp:6512
QMap< QString, QString > qt_getImageTextFromDescription(const QString &description)
Definition qimage.cpp:6546
QTransform operator*(const QTransform &a, qreal n)
Definition qtransform.h:349
Q_DECLARE_TYPEINFO(QTransform, Q_RELOCATABLE_TYPE)
QTransform operator/(const QTransform &a, qreal n)
Definition qtransform.h:351
QPoint operator*(const QPoint &p, const QTransform &m)
Definition qtransform.h:334
QTransform operator+(const QTransform &a, qreal n)
Definition qtransform.h:353
QTransform operator-(const QTransform &a, qreal n)
Definition qtransform.h:355
QT_WARNING_POP bool qFuzzyCompare(const QTransform &t1, const QTransform &t2) noexcept
Definition qtransform.h:308