Skip to content

Commit

Permalink
Extract highest resolution from downloaded ICO files
Browse files Browse the repository at this point in the history
  • Loading branch information
phoerious authored and droidmonkey committed May 28, 2020
1 parent 19b8185 commit 0a21dc8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Binary file modified share/demo.kdbx
Binary file not shown.
33 changes: 32 additions & 1 deletion src/core/IconDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "core/NetworkManager.h"

#include <QHostInfo>
#include <QImageReader>
#include <QtNetwork>

#define MAX_REDIRECTS 5
Expand Down Expand Up @@ -188,7 +189,7 @@ void IconDownloader::fetchFinished()
}
} else {
// No redirect, and we theoretically have some icon data now.
image.loadFromData(m_bytesReceived);
image = parseImage(m_bytesReceived);
}
}

Expand All @@ -206,3 +207,33 @@ void IconDownloader::fetchFinished()
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;
}
1 change: 1 addition & 0 deletions src/core/IconDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private slots:

private:
void fetchFavicon(const QUrl& url);
QImage parseImage(QByteArray& imageBytes) const;

QString m_url;
QUrl m_fetchUrl;
Expand Down

0 comments on commit 0a21dc8

Please sign in to comment.