QImageReader: Check for existing handler in initHandler

Some calling code did this beforehand, other didn't. Now, the function
itself checks for an existing handler before doing anything else.

Change-Id: I8fc43fb8788c9dfe825b15ffa2fa69ee43915cd6
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
Robert Löhning 2022-01-12 23:19:16 +01:00
parent 961f5a6fa2
commit 27b560373d
2 changed files with 32 additions and 3 deletions

View File

@ -529,6 +529,9 @@ QImageReaderPrivate::~QImageReaderPrivate()
*/
bool QImageReaderPrivate::initHandler()
{
if (handler)
return true;
// check some preconditions
if (!device || (!deleteDevice && !device->isOpen() && !device->open(QIODevice::ReadOnly))) {
imageReaderError = QImageReader::DeviceError;
@ -575,7 +578,7 @@ bool QImageReaderPrivate::initHandler()
}
// assign a handler
if (!handler && (handler = createReadHandlerHelper(device, format, autoDetectImageFormat, ignoresFormatAndExtension)) == nullptr) {
if ((handler = createReadHandlerHelper(device, format, autoDetectImageFormat, ignoresFormatAndExtension)) == nullptr) {
imageReaderError = QImageReader::UnsupportedFormatError;
errorString = QImageReader::tr("Unsupported image format");
return false;
@ -588,7 +591,7 @@ bool QImageReaderPrivate::initHandler()
*/
void QImageReaderPrivate::getText()
{
if (text.isEmpty() && (handler || initHandler()) && handler->supportsOption(QImageIOHandler::Description))
if (text.isEmpty() && initHandler() && handler->supportsOption(QImageIOHandler::Description))
text = qt_getImageTextFromDescription(handler->option(QImageIOHandler::Description).toString());
}
@ -1223,7 +1226,7 @@ bool QImageReader::read(QImage *image)
return false;
}
if (!d->handler && !d->initHandler())
if (!d->initHandler())
return false;
// set the handler specific options.

View File

@ -94,6 +94,8 @@ private slots:
void setScaledClipRect_data();
void setScaledClipRect();
void setFormat();
void imageFormat_data();
void imageFormat();
@ -513,6 +515,30 @@ void tst_QImageReader::setScaledClipRect()
QCOMPARE(originalImage.copy(newRect), image);
}
void tst_QImageReader::setFormat()
{
QByteArray ppmImage = "P1 2 2\n1 0\n0 1";
QBuffer buf(&ppmImage);
QImageReader reader(&buf);
// read image in autodetected format
QCOMPARE(reader.size(), QSize(2,2));
buf.close();
// try reading with non-matching format, must not succeed
reader.setDecideFormatFromContent(false);
reader.setFormat("bmp");
reader.setDevice(&buf);
QCOMPARE(reader.size(), QSize());
buf.close();
// read with manually set matching format
reader.setFormat("ppm");
reader.setDevice(&buf);
QCOMPARE(reader.size(), QSize(2,2));
buf.close();
}
void tst_QImageReader::imageFormat_data()
{
QTest::addColumn<QString>("fileName");