Extract highest resolution from downloaded ICO files

This commit is contained in:
Janek Bevendorff 2020-05-27 17:53:42 +02:00 committed by Jonathan White
parent b5554e05d8
commit 4de99cfe8e
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
3 changed files with 33 additions and 1 deletions

Binary file not shown.

View File

@ -20,6 +20,7 @@
#include "core/NetworkManager.h" #include "core/NetworkManager.h"
#include <QHostInfo> #include <QHostInfo>
#include <QImageReader>
#include <QtNetwork> #include <QtNetwork>
#define MAX_REDIRECTS 5 #define MAX_REDIRECTS 5
@ -188,7 +189,7 @@ void IconDownloader::fetchFinished()
} }
} else { } else {
// No redirect, and we theoretically have some icon data now. // No redirect, and we theoretically have some icon data now.
image.loadFromData(m_bytesReceived); image = parseImage(m_bytesReceived);
} }
} }
@ -206,3 +207,33 @@ void IconDownloader::fetchFinished()
emit finished(url, image); emit finished(url, image);
} }
} }
/**
* Parse fetched image bytes.
*
* Parses the given byte array into a QImage. Unlike QImage::loadFromData(), this method
* tries to extract the highest resolution image from .ICO files.
*
* @param imageBytes raw image bytes
* @return parsed image
*/
QImage IconDownloader::parseImage(QByteArray& imageBytes) const
{
QBuffer buff(&imageBytes);
buff.open(QIODevice::ReadOnly);
QImageReader reader(&buff);
if (reader.imageCount() <= 0) {
return reader.read();
}
QImage img;
for (int i = 0; i < reader.imageCount(); ++i) {
if (img.isNull() || reader.size().width() > img.size().width()) {
img = reader.read();
}
reader.jumpToNextImage();
}
return img;
}

View File

@ -50,6 +50,7 @@ private slots:
private: private:
void fetchFavicon(const QUrl& url); void fetchFavicon(const QUrl& url);
QImage parseImage(QByteArray& imageBytes) const;
QString m_url; QString m_url;
QUrl m_fetchUrl; QUrl m_fetchUrl;