Make the ICO image handler capable of reading CUR files too.

This simple patch was an unfortunate victim of the Gitorious to
Gerrit transition. (https://qt.gitorious.org/qt/qt/merge_requests/1179)

As noted in the Gitorious review, a small bug in readHeader is also fixed: ||
instead of &&.

Done-with: Pali Rohár
Task-number: QTBUG-12684
Change-Id: I1fe16359b9b68c10e518904c6a5c58b00fb7379b
Reviewed-by: Yoann Lopes <yoann.lopes@digia.com>
bb10
Robin Burchell 2014-05-31 11:51:44 +02:00 committed by The Qt Project
parent 3b00497d50
commit 9e01483cdf
5 changed files with 22 additions and 11 deletions

View File

@ -1,4 +1,4 @@
{
"Keys": [ "ico" ],
"Keys": [ "ico", "cur" ],
"MimeTypes": [ "image/vnd.microsoft.icon" ]
}

View File

@ -47,7 +47,7 @@ QT_BEGIN_NAMESPACE
QImageIOPlugin::Capabilities QICOPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "ico")
if (format == "ico" || format == "cur")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;

View File

@ -75,7 +75,7 @@ typedef struct
typedef struct
{
quint16 idReserved; // Reserved
quint16 idType; // resource type (1 for icons)
quint16 idType; // resource type (1 for icons, 2 for cursors)
quint16 idCount; // how many images?
ICONDIRENTRY idEntries[1]; // the entries for each image
} ICONDIR, *LPICONDIR;
@ -275,10 +275,10 @@ bool ICOReader::canRead(QIODevice *iodev)
readBytes += ICONDIRENTRY_SIZE;
// ICO format does not have a magic identifier, so we read 6 different values, which will hopefully be enough to identify the file.
if ( ikonDir.idReserved == 0
&& ikonDir.idType == 1
&& (ikonDir.idType == 1 || ikonDir.idType == 2)
&& ikonDir.idEntries[0].bReserved == 0
&& ikonDir.idEntries[0].wPlanes <= 1
&& ikonDir.idEntries[0].wBitCount <= 32 // Bits per pixel
&& (ikonDir.idEntries[0].wPlanes <= 1 || ikonDir.idType == 2)
&& (ikonDir.idEntries[0].wBitCount <= 32 || ikonDir.idType == 2) // Bits per pixel
&& ikonDir.idEntries[0].dwBytesInRes >= 40 // Must be over 40, since sizeof (infoheader) == 40
) {
isProbablyICO = true;
@ -339,7 +339,7 @@ bool ICOReader::readHeader()
if (iod && !headerRead) {
startpos = iod->pos();
if (readIconDir(iod, &iconDir)) {
if (iconDir.idReserved == 0 || iconDir.idType == 1)
if (iconDir.idReserved == 0 && (iconDir.idType == 1 || iconDir.idType == 2))
headerRead = true;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -111,9 +111,17 @@ void tst_QIcoImageFormat::cleanupTestCase()
void tst_QIcoImageFormat::format()
{
QImageReader reader(m_IconPath + "/valid/35FLOPPY.ICO", "ico");
QByteArray fmt = reader.format();
QCOMPARE(const_cast<const char*>(fmt.data()), "ico" );
{
QImageReader reader(m_IconPath + "/valid/35FLOPPY.ICO", "ico");
QByteArray fmt = reader.format();
QCOMPARE(const_cast<const char*>(fmt.data()), "ico" );
}
{
QImageReader reader(m_IconPath + "/valid/yellow.cur", "ico");
QByteArray fmt = reader.format();
QCOMPARE(const_cast<const char*>(fmt.data()), "ico" );
}
}
void tst_QIcoImageFormat::canRead_data()
@ -133,6 +141,7 @@ void tst_QIcoImageFormat::canRead_data()
QTest::newRow("103x16px, 24BPP") << "valid/trolltechlogo_tiny.ico" << 1;
QTest::newRow("includes 32BPP w/alpha") << "valid/semitransparent.ico" << 1;
QTest::newRow("PNG compression") << "valid/Qt.ico" << 1;
QTest::newRow("CUR file") << "valid/yellow.cur" << 1;
}
void tst_QIcoImageFormat::canRead()
@ -203,7 +212,7 @@ void tst_QIcoImageFormat::imageCount_data()
QTest::newRow("invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << 0;
QTest::newRow("includes 32BPP w/alpha") << "valid/semitransparent.ico" << 9;
QTest::newRow("PNG compression") << "valid/Qt.ico" << 4;
QTest::newRow("CUR file") << "valid/yellow.cur" << 1;
}
void tst_QIcoImageFormat::imageCount()
@ -231,6 +240,7 @@ void tst_QIcoImageFormat::jumpToNextImage_data()
QTest::newRow("16px16c, 32px32c, 32px256c 2") << "valid/WORLDH.ico" << 3;
QTest::newRow("includes 32BPP w/alpha") << "valid/semitransparent.ico" << 9;
QTest::newRow("PNG compression") << "valid/Qt.ico" << 4;
QTest::newRow("CUR file") << "valid/yellow.cur" << 1;
}
void tst_QIcoImageFormat::jumpToNextImage()
@ -281,6 +291,7 @@ void tst_QIcoImageFormat::nextImageDelay_data()
QTest::newRow("invalid floppy (first 8 bytes = 0xff)") << "invalid/35floppy.ico" << -1;
QTest::newRow("includes 32BPP w/alpha") << "valid/semitransparent.ico" << 9;
QTest::newRow("PNG compression") << "valid/Qt.ico" << 4;
QTest::newRow("CUR file") << "valid/yellow.cur" << 1;
}
void tst_QIcoImageFormat::nextImageDelay()