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>
bb10
Eirik Aavitsland 2021-05-28 16:06:58 +02:00
parent 5b6f5c6fc2
commit 2b52452843
3 changed files with 75 additions and 26 deletions

View File

@ -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,12 +3753,25 @@ 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)
@ -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,21 +3795,35 @@ 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);
return QImageReader(&b, format).read();
}
/*!
\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.
*/
/*!

View File

@ -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;

View File

@ -1269,15 +1269,36 @@ void tst_QImage::loadFromData()
QVERIFY(original.save(&buf, "BMP"));
}
QVERIFY(!ba.isEmpty());
const uchar *baPtr = reinterpret_cast<const uchar *>(ba.constData());
QImage dest;
QVERIFY(dest.loadFromData(ba, "BMP"));
QVERIFY(!dest.isNull());
{
QImage dest;
QVERIFY(dest.loadFromData(QByteArrayView(ba), "BMP"));
QCOMPARE(original, dest);
QCOMPARE(original, dest);
QVERIFY(!dest.loadFromData(QByteArrayView()));
QVERIFY(dest.isNull());
}
{
QImage dest;
QVERIFY(dest.loadFromData(ba, "BMP"));
QCOMPARE(original, dest);
QVERIFY(!dest.loadFromData(QByteArray()));
QVERIFY(dest.isNull());
QVERIFY(!dest.loadFromData(QByteArray()));
QVERIFY(dest.isNull());
}
{
QImage dest;
QVERIFY(dest.loadFromData(baPtr, int(ba.size()), "BMP"));
QCOMPARE(original, dest);
QVERIFY(!dest.loadFromData(nullptr, 0));
QVERIFY(dest.isNull());
}
QCOMPARE(original, QImage::fromData(QByteArrayView(ba), "BMP"));
QCOMPARE(original, QImage::fromData(ba, "BMP"));
QCOMPARE(original, QImage::fromData(baPtr, int(ba.size()), "BMP"));
}
#if !defined(QT_NO_DATASTREAM)