summaryrefslogtreecommitdiffstats
path: root/src/gui/image
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2021-05-28 16:06:58 +0200
committerEirik Aavitsland <eirik.aavitsland@qt.io>2021-06-02 23:02:45 +0200
commit2b524528435047fe4b47991cb57ecfb5de5645b5 (patch)
tree9b2f408c07576c63caace49500944a9c82e48a09 /src/gui/image
parent5b6f5c6fc2a269c0af7d6de9c4ebf55c54a54770 (diff)
Add load/fromdata() overloads taking QByteArrayView to QImage
Handy when one has the data buffer to be read in a QBAV. This also fixes an issue and compiler warning about passing a qsizetype data length value as an int, and makes it possible to pass a qsizetype-size length without going through QByteArray. Makes the QByteArray overload redundant. Change-Id: Iba8825cf0fd8003fb2eac5b1d30a61ec91b85ceb Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/gui/image')
-rw-r--r--src/gui/image/qimage.cpp53
-rw-r--r--src/gui/image/qimage.h15
2 files changed, 48 insertions, 20 deletions
diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp
index 4ee781a383..cb5afcbf90 100644
--- a/src/gui/image/qimage.cpp
+++ b/src/gui/image/qimage.cpp
@@ -3741,11 +3741,10 @@ bool QImage::load(QIODevice* device, const char* format)
}
/*!
- \fn bool QImage::loadFromData(const uchar *data, int len, const char *format)
+ \since 6.2
- Loads an image from the first \a len bytes of the given binary \a
- data. Returns \c true if the image was successfully loaded; otherwise
- invalidates the image and returns \c false.
+ Loads an image from the given QByteArrayView \a data. Returns \c true if the image was
+ successfully loaded; otherwise invalidates the image and returns \c false.
The loader attempts to read the image using the specified \a format, e.g.,
PNG or JPG. If \a format is not specified (which is the default), the
@@ -3754,13 +3753,26 @@ bool QImage::load(QIODevice* device, const char* format)
\sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
*/
-bool QImage::loadFromData(const uchar *data, int len, const char *format)
+bool QImage::loadFromData(QByteArrayView data, const char *format)
{
- *this = fromData(data, len, format);
+ *this = fromData(data, format);
return !isNull();
}
/*!
+ \fn bool QImage::loadFromData(const uchar *data, int len, const char *format)
+
+ \overload
+
+ Loads an image from the first \a len bytes of the given binary \a data.
+*/
+
+bool QImage::loadFromData(const uchar *buf, int len, const char *format)
+{
+ return loadFromData(QByteArrayView(buf, len), format);
+}
+
+/*!
\fn bool QImage::loadFromData(const QByteArray &data, const char *format)
\overload
@@ -3769,12 +3781,11 @@ bool QImage::loadFromData(const uchar *data, int len, const char *format)
*/
/*!
- \fn QImage QImage::fromData(const uchar *data, int size, const char *format)
+ \since 6.2
- Constructs a QImage from the first \a size bytes of the given
- binary \a data. The loader attempts to read the image using the
- specified \a format. If \a format is not specified (which is the default),
- the loader probes the data for a header to guess the file format.
+ Constructs an image from the given QByteArrayView \a data. The loader attempts to read the image
+ using the specified \a format. If \a format is not specified (which is the default), the loader
+ probes the data for a header to guess the file format.
If \a format is specified, it must be one of the values returned by
QImageReader::supportedImageFormats().
@@ -3784,9 +3795,9 @@ bool QImage::loadFromData(const uchar *data, int len, const char *format)
\sa load(), save(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
*/
-QImage QImage::fromData(const uchar *data, int size, const char *format)
+QImage QImage::fromData(QByteArrayView data, const char *format)
{
- QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size);
+ QByteArray a = QByteArray::fromRawData(data.constData(), data.size());
QBuffer b;
b.setData(a);
b.open(QIODevice::ReadOnly);
@@ -3794,11 +3805,25 @@ QImage QImage::fromData(const uchar *data, int size, const char *format)
}
/*!
+ \fn QImage QImage::fromData(const uchar *data, int size, const char *format)
+
+ \overload
+
+ Constructs a QImage from the first \a size bytes of the given binary \a data.
+*/
+
+QImage QImage::fromData(const uchar *data, int size, const char *format)
+{
+ return fromData(QByteArrayView(data, size), format);
+}
+
+/*!
\fn QImage QImage::fromData(const QByteArray &data, const char *format)
\overload
- Loads an image from the given QByteArray \a data.
+ Constructs a QImage from the given QByteArray \a data.
+
*/
/*!
diff --git a/src/gui/image/qimage.h b/src/gui/image/qimage.h
index 885c804457..6561359dae 100644
--- a/src/gui/image/qimage.h
+++ b/src/gui/image/qimage.h
@@ -47,6 +47,7 @@
#include <QtGui/qpixelformat.h>
#include <QtGui/qtransform.h>
#include <QtCore/qbytearray.h>
+#include <QtCore/qbytearrayview.h>
#include <QtCore/qrect.h>
#include <QtCore/qstring.h>
#include <QtCore/qcontainerfwd.h>
@@ -273,16 +274,18 @@ public:
bool load(QIODevice *device, const char *format);
bool load(const QString &fileName, const char *format = nullptr);
- bool loadFromData(const uchar *buf, int len, const char *format = nullptr);
- bool loadFromData(const QByteArray &data, const char *aformat = nullptr)
- { return loadFromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), aformat); }
+ bool loadFromData(QByteArrayView data, const char *format = nullptr);
+ bool loadFromData(const uchar *buf, int len, const char *format = nullptr); // ### Qt 7: qsizetype
+ bool loadFromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop
+ { return loadFromData(QByteArrayView(data), format); }
bool save(const QString &fileName, const char *format = nullptr, int quality = -1) const;
bool save(QIODevice *device, const char *format = nullptr, int quality = -1) const;
- static QImage fromData(const uchar *data, int size, const char *format = nullptr);
- static QImage fromData(const QByteArray &data, const char *format = nullptr)
- { return fromData(reinterpret_cast<const uchar *>(data.constData()), data.size(), format); }
+ static QImage fromData(QByteArrayView data, const char *format = nullptr);
+ static QImage fromData(const uchar *data, int size, const char *format = nullptr); // ### Qt 7: qsizetype
+ static QImage fromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop
+ { return fromData(QByteArrayView(data), format); }
qint64 cacheKey() const;