Skip to content

Commit

Permalink
QImageReader: Check for existing handler in initHandler
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Eirik Aavitsland <[email protected]>
  • Loading branch information
rlohning committed Feb 2, 2022
1 parent 961f5a6 commit 27b5603
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/gui/image/qimagereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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());
}

Expand Down Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions tests/auto/gui/image/qimagereader/tst_qimagereader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ private slots:
void setScaledClipRect_data();
void setScaledClipRect();

void setFormat();

void imageFormat_data();
void imageFormat();

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 27b5603

Please sign in to comment.