mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-14 22:42:14 -07:00
Extract tryLoadImageFromReply
This commit is contained in:
parent
208f8349a6
commit
e59f6d0553
2 changed files with 44 additions and 28 deletions
|
|
@ -32,8 +32,7 @@ PictureLoaderWorkerWork::PictureLoaderWorkerWork(const PictureLoaderWorker *work
|
||||||
|
|
||||||
connect(pictureLoaderThread, &QThread::started, this, &PictureLoaderWorkerWork::startNextPicDownload);
|
connect(pictureLoaderThread, &QThread::started, this, &PictureLoaderWorkerWork::startNextPicDownload);
|
||||||
|
|
||||||
// clean up worker once loading finishes
|
// clean up threads once loading finishes
|
||||||
connect(this, &PictureLoaderWorkerWork::imageLoaded, this, &QObject::deleteLater);
|
|
||||||
connect(this, &QObject::destroyed, pictureLoaderThread, &QThread::quit);
|
connect(this, &QObject::destroyed, pictureLoaderThread, &QThread::quit);
|
||||||
connect(pictureLoaderThread, &QThread::finished, pictureLoaderThread, &QObject::deleteLater);
|
connect(pictureLoaderThread, &QThread::finished, pictureLoaderThread, &QObject::deleteLater);
|
||||||
|
|
||||||
|
|
@ -75,7 +74,7 @@ void PictureLoaderWorkerWork::picDownloadFailed()
|
||||||
<< " set: " << cardToDownload.getSetName() << "]: Picture NOT found, "
|
<< " set: " << cardToDownload.getSetName() << "]: Picture NOT found, "
|
||||||
<< (picDownload ? "download failed" : "downloads disabled")
|
<< (picDownload ? "download failed" : "downloads disabled")
|
||||||
<< ", no more url combinations to try: BAILING OUT";
|
<< ", no more url combinations to try: BAILING OUT";
|
||||||
emit imageLoaded(cardToDownload.getCard(), QImage());
|
concludeImageLoad(QImage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -134,15 +133,34 @@ void PictureLoaderWorkerWork::picDownloadFinished(QNetworkReply *reply)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage testImage;
|
QImage image = tryLoadImageFromReply(reply);
|
||||||
|
|
||||||
QImageReader imgReader;
|
if (image.isNull()) {
|
||||||
imgReader.setDecideFormatFromContent(true);
|
qCDebug(PictureLoaderWorkerWorkLog).nospace()
|
||||||
imgReader.setDevice(reply);
|
<< "PictureLoader: [card: " << cardToDownload.getCard()->getName()
|
||||||
|
<< " set: " << cardToDownload.getSetName() << "]: Possible " << (isFromCache ? "cached" : "downloaded")
|
||||||
|
<< " picture at " << reply->url().toDisplayString() << " could not be loaded: " << reply->errorString();
|
||||||
|
|
||||||
bool logSuccessMessage = false;
|
reply->deleteLater();
|
||||||
|
picDownloadFailed();
|
||||||
|
} else {
|
||||||
|
qCDebug(PictureLoaderWorkerWorkLog).nospace()
|
||||||
|
<< "PictureLoader: [card: " << cardToDownload.getCard()->getName()
|
||||||
|
<< " set: " << cardToDownload.getSetName() << "]: Image successfully "
|
||||||
|
<< (isFromCache ? "loaded from cached" : "downloaded from") << " url " << reply->url().toDisplayString();
|
||||||
|
|
||||||
static const int riffHeaderSize = 12; // RIFF_HEADER_SIZE from webp/format_constants.h
|
reply->deleteLater();
|
||||||
|
concludeImageLoad(image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param reply The reply to load the image from
|
||||||
|
* @return The loaded image, or an empty QImage if loading failed
|
||||||
|
*/
|
||||||
|
QImage PictureLoaderWorkerWork::tryLoadImageFromReply(QNetworkReply *reply)
|
||||||
|
{
|
||||||
|
static constexpr int riffHeaderSize = 12; // RIFF_HEADER_SIZE from webp/format_constants.h
|
||||||
auto replyHeader = reply->peek(riffHeaderSize);
|
auto replyHeader = reply->peek(riffHeaderSize);
|
||||||
|
|
||||||
if (replyHeader.startsWith("RIFF") && replyHeader.endsWith("WEBP")) {
|
if (replyHeader.startsWith("RIFF") && replyHeader.endsWith("WEBP")) {
|
||||||
|
|
@ -153,28 +171,24 @@ void PictureLoaderWorkerWork::picDownloadFinished(QNetworkReply *reply)
|
||||||
movie.start();
|
movie.start();
|
||||||
movie.stop();
|
movie.stop();
|
||||||
|
|
||||||
emit imageLoaded(cardToDownload.getCard(), movie.currentImage());
|
return movie.currentImage();
|
||||||
logSuccessMessage = true;
|
|
||||||
} else if (imgReader.read(&testImage)) {
|
|
||||||
emit imageLoaded(cardToDownload.getCard(), testImage);
|
|
||||||
logSuccessMessage = true;
|
|
||||||
} else {
|
|
||||||
qCDebug(PictureLoaderWorkerWorkLog).nospace()
|
|
||||||
<< "PictureLoader: [card: " << cardToDownload.getCard()->getName()
|
|
||||||
<< " set: " << cardToDownload.getSetName() << "]: Possible " << (isFromCache ? "cached" : "downloaded")
|
|
||||||
<< " picture at " << reply->url().toDisplayString() << " could not be loaded: " << reply->errorString();
|
|
||||||
|
|
||||||
picDownloadFailed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logSuccessMessage) {
|
QImageReader imgReader;
|
||||||
qCDebug(PictureLoaderWorkerWorkLog).nospace()
|
imgReader.setDecideFormatFromContent(true);
|
||||||
<< "PictureLoader: [card: " << cardToDownload.getCard()->getName()
|
imgReader.setDevice(reply);
|
||||||
<< " set: " << cardToDownload.getSetName() << "]: Image successfully "
|
|
||||||
<< (isFromCache ? "loaded from cached" : "downloaded from") << " url " << reply->url().toDisplayString();
|
|
||||||
}
|
|
||||||
|
|
||||||
reply->deleteLater();
|
return imgReader.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call this method when the image has finished being loaded.
|
||||||
|
* @param image The image that was loaded. Empty QImage indicates failure.
|
||||||
|
*/
|
||||||
|
void PictureLoaderWorkerWork::concludeImageLoad(const QImage &image)
|
||||||
|
{
|
||||||
|
emit imageLoaded(cardToDownload.getCard(), image);
|
||||||
|
this->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PictureLoaderWorkerWork::picDownloadChanged()
|
void PictureLoaderWorkerWork::picDownloadChanged()
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,8 @@ private:
|
||||||
bool picDownload, downloadRunning, loadQueueRunning;
|
bool picDownload, downloadRunning, loadQueueRunning;
|
||||||
|
|
||||||
void startNextPicDownload();
|
void startNextPicDownload();
|
||||||
|
QImage tryLoadImageFromReply(QNetworkReply *reply);
|
||||||
|
void concludeImageLoad(const QImage &image);
|
||||||
bool imageIsBlackListed(const QByteArray &);
|
bool imageIsBlackListed(const QByteArray &);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue