From df55141aa260c8a10c06aa990b6f20d6fb4718f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Tue, 15 Jun 2010 13:06:19 +0200 Subject: Fixed QImageReader::format() inconsistency. If state is set to ReadHeader before canRead() then setFormat("png") never gets called. We should make sure that setFormat() gets set whenever canRead() returns true. Task-number: QTBUG-2263 Reviewed-by: Kent Hansen --- src/gui/image/qbmphandler.cpp | 10 +++++---- src/gui/image/qpnghandler.cpp | 10 +++++---- src/gui/image/qppmhandler.cpp | 10 +++++---- src/gui/image/qxbmhandler.cpp | 10 +++++---- src/gui/image/qxpmhandler.cpp | 8 ++++++-- src/plugins/imageformats/gif/qgifhandler.cpp | 4 ++-- src/plugins/imageformats/jpeg/qjpeghandler.cpp | 10 +++++---- src/plugins/imageformats/mng/qmnghandler.cpp | 8 ++++---- tests/auto/qimagereader/tst_qimagereader.cpp | 28 ++++++++++++++++++++++++++ 9 files changed, 70 insertions(+), 28 deletions(-) diff --git a/src/gui/image/qbmphandler.cpp b/src/gui/image/qbmphandler.cpp index 42e19b8..074b8f0 100644 --- a/src/gui/image/qbmphandler.cpp +++ b/src/gui/image/qbmphandler.cpp @@ -674,13 +674,15 @@ bool QBmpHandler::readHeader() bool QBmpHandler::canRead() const { - if (state == Ready) { - if (!canRead(device())) - return false; + if (state == Ready && !canRead(device())) + return false; + + if (state != Error) { setFormat("bmp"); return true; } - return state != Error; + + return false; } bool QBmpHandler::canRead(QIODevice *device) diff --git a/src/gui/image/qpnghandler.cpp b/src/gui/image/qpnghandler.cpp index dd31834..2cf8222 100644 --- a/src/gui/image/qpnghandler.cpp +++ b/src/gui/image/qpnghandler.cpp @@ -892,13 +892,15 @@ QPngHandler::~QPngHandler() bool QPngHandler::canRead() const { - if (d->state == QPngHandlerPrivate::Ready) { - if (!canRead(device())) - return false; + if (d->state == QPngHandlerPrivate::Ready && !canRead(device())) + return false; + + if (d->state != QPngHandlerPrivate::Error) { setFormat("png"); return true; } - return d->state != QPngHandlerPrivate::Error; + + return false; } bool QPngHandler::canRead(QIODevice *device) diff --git a/src/gui/image/qppmhandler.cpp b/src/gui/image/qppmhandler.cpp index cbbbef4..a9e796c 100644 --- a/src/gui/image/qppmhandler.cpp +++ b/src/gui/image/qppmhandler.cpp @@ -409,13 +409,15 @@ bool QPpmHandler::readHeader() bool QPpmHandler::canRead() const { - if (state == Ready) { - if (!canRead(device(), &subType)) - return false; + if (state == Ready && !canRead(device(), &subType)) + return false; + + if (state != Error) { setFormat(subType); return true; } - return state != Error; + + return false; } bool QPpmHandler::canRead(QIODevice *device, QByteArray *subType) diff --git a/src/gui/image/qxbmhandler.cpp b/src/gui/image/qxbmhandler.cpp index 385340a..0dd4e99 100644 --- a/src/gui/image/qxbmhandler.cpp +++ b/src/gui/image/qxbmhandler.cpp @@ -261,13 +261,15 @@ bool QXbmHandler::readHeader() bool QXbmHandler::canRead() const { - if (state == Ready) { - if (!canRead(device())) - return false; + if (state == Ready && !canRead(device())) + return false; + + if (state != Error) { setFormat("xbm"); return true; } - return state != Error; + + return false; } bool QXbmHandler::canRead(QIODevice *device) diff --git a/src/gui/image/qxpmhandler.cpp b/src/gui/image/qxpmhandler.cpp index a475cd0..b97afd3 100644 --- a/src/gui/image/qxpmhandler.cpp +++ b/src/gui/image/qxpmhandler.cpp @@ -1225,11 +1225,15 @@ bool QXpmHandler::readImage(QImage *image) bool QXpmHandler::canRead() const { - if (state == Ready && canRead(device())) { + if (state == Ready && !canRead(device())) + return false; + + if (state != Error) { setFormat("xpm"); return true; } - return state != Error; + + return false; } bool QXpmHandler::canRead(QIODevice *device) diff --git a/src/plugins/imageformats/gif/qgifhandler.cpp b/src/plugins/imageformats/gif/qgifhandler.cpp index 8abc2d1..5e2157e 100644 --- a/src/plugins/imageformats/gif/qgifhandler.cpp +++ b/src/plugins/imageformats/gif/qgifhandler.cpp @@ -1061,12 +1061,12 @@ bool QGifHandler::imageIsComing() const bool QGifHandler::canRead() const { - if (!nextDelay && canRead(device())) { + if (!nextDelay && canRead(device()) || imageIsComing()) { setFormat("gif"); return true; } - return imageIsComing(); + return false; } bool QGifHandler::canRead(QIODevice *device) diff --git a/src/plugins/imageformats/jpeg/qjpeghandler.cpp b/src/plugins/imageformats/jpeg/qjpeghandler.cpp index 72dde15..60e7cce 100644 --- a/src/plugins/imageformats/jpeg/qjpeghandler.cpp +++ b/src/plugins/imageformats/jpeg/qjpeghandler.cpp @@ -802,13 +802,15 @@ QJpegHandler::~QJpegHandler() bool QJpegHandler::canRead() const { - if(d->state == QJpegHandlerPrivate::Ready) { - if (!canRead(device())) - return false; + if(d->state == QJpegHandlerPrivate::Ready && !canRead(device())) + return false; + + if (d->state != QJpegHandlerPrivate::Error) { setFormat("jpeg"); return true; } - return d->state != QJpegHandlerPrivate::Error; + + return false; } bool QJpegHandler::canRead(QIODevice *device) diff --git a/src/plugins/imageformats/mng/qmnghandler.cpp b/src/plugins/imageformats/mng/qmnghandler.cpp index d408e6c..c134fd6 100644 --- a/src/plugins/imageformats/mng/qmnghandler.cpp +++ b/src/plugins/imageformats/mng/qmnghandler.cpp @@ -381,10 +381,10 @@ QMngHandler::~QMngHandler() bool QMngHandler::canRead() const { Q_D(const QMngHandler); - if (!d->haveReadNone) - return (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount))); - - if (canRead(device())) { + if ((!d->haveReadNone + && (!d->haveReadAll || (d->haveReadAll && (d->nextIndex < d->frameCount)))) + || canRead(device())) + { setFormat("mng"); return true; } diff --git a/tests/auto/qimagereader/tst_qimagereader.cpp b/tests/auto/qimagereader/tst_qimagereader.cpp index aadee5b..7e57973 100644 --- a/tests/auto/qimagereader/tst_qimagereader.cpp +++ b/tests/auto/qimagereader/tst_qimagereader.cpp @@ -138,6 +138,9 @@ private slots: void sizeBeforeRead_data(); void sizeBeforeRead(); + void sizeBeforeFormat_data(); + void sizeBeforeFormat(); + void imageFormatBeforeRead_data(); void imageFormatBeforeRead(); @@ -706,6 +709,31 @@ void tst_QImageReader::sizeBeforeRead() QCOMPARE(size, image.size()); } +void tst_QImageReader::sizeBeforeFormat_data() +{ + imageFormat_data(); +} + +void tst_QImageReader::sizeBeforeFormat() +{ + QFETCH(QString, fileName); + + QByteArray formatA, formatB; + + { + QImageReader reader(prefix + fileName); + formatA = reader.format(); + } + + { + QImageReader reader(prefix + fileName); + QSize size = reader.size(); + formatB = reader.format(); + } + + QCOMPARE(formatA, formatB); +} + void tst_QImageReader::imageFormatBeforeRead_data() { imageFormat_data(); -- cgit v0.12