Fix transparency in 16 bit and 24 bit ico files

As a result of the fix for QTBUG-75214, Qt inadvertently no longer
reads the AND mask that specifies transparency for 16-bit and 24-bit
ico files. This is because it tries to detect 32-bit icons by checking
icoAttrib.depth == 32, but icoAttrib.depth is set to the depth of the
QImage, not the depth of the icon, and 32-bit QImage is used for all of
the non-indexed cases (16-bit, 24-bit and 32-bit.)

This commit instead uses icoAttrib.nbits, which should reliably
determine whether or not the icon is 32-bit. This makes the behavior
consistent with other ico reading software, including Windows.

Also, adds a unit test that verifies correct behavior of icon masks,
checking for both QTBUG-75214 and QTBUG-113319.

Amends 1d128ed1df.

Fixes: QTBUG-113319
Change-Id: I89ac86ff16054c8925fff6afc8c530fa737f8385
Pick-to: 6.6 6.5 6.2 5.15
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
This commit is contained in:
John Chadwick 2023-07-08 02:52:03 -04:00
parent 417878904b
commit 1079b53739
6 changed files with 30 additions and 1 deletions

View File

@ -499,7 +499,7 @@ QImage ICOReader::iconAt(int index)
if (!image.isNull()) { if (!image.isNull()) {
readBMP(image); readBMP(image);
if (!image.isNull()) { if (!image.isNull()) {
if (icoAttrib.depth == 32) { if (icoAttrib.nbits == 32) {
img = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied); img = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
} else { } else {
QImage mask(image.width(), image.height(), QImage::Format_Mono); QImage mask(image.width(), image.height(), QImage::Format_Mono);

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@ -27,6 +27,8 @@ private slots:
void pngCompression(); void pngCompression();
void write_data(); void write_data();
void write(); void write();
void icoMask_data();
void icoMask();
private: private:
QString m_IconPath; QString m_IconPath;
@ -319,6 +321,33 @@ void tst_QIcoImageFormat::write()
} }
} }
void tst_QIcoImageFormat::icoMask_data()
{
QTest::addColumn<QString>("inFile");
QTest::addColumn<QString>("outFile");
QTest::newRow("24bpp") << "masked/24bpp.ico" << "masked/24bpp.png";
QTest::newRow("32bpp") << "masked/32bpp.ico" << "masked/32bpp.png";
}
void tst_QIcoImageFormat::icoMask()
{
QFETCH(QString, inFile);
QFETCH(QString, outFile);
QImage inImage;
QImageReader inReader(m_IconPath + QLatin1Char('/') + inFile);
inReader.read(&inImage);
QImage outImage;
QImageReader outReader(m_IconPath + QLatin1Char('/') + outFile);
outReader.read(&outImage);
outImage.setColorSpace(inImage.colorSpace());
outImage = outImage.convertToFormat(inImage.format());
QCOMPARE(inImage, outImage);
}
QTEST_MAIN(tst_QIcoImageFormat) QTEST_MAIN(tst_QIcoImageFormat)
#include "tst_qicoimageformat.moc" #include "tst_qicoimageformat.moc"