Commit 4bb1d28a authored by Peng Liu's avatar Peng Liu
Browse files

Removed the QLists for determining image format. Instead, using QImageReader,...

Removed the QLists for determining image format. Instead, using QImageReader, both when downloading/saving and when loading, to determine the correct format (Cockatrice now supports all QImageReader-supported formats).
Image loading still uses one for loop to iterate through the QList of paths to folders in which to search for images.
parent 8587b8c3
...@@ -13,13 +13,10 @@ ...@@ -13,13 +13,10 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QDebug> #include <QDebug>
#include <QImageReader>
const int CardDatabase::versionNeeded = 3; const int CardDatabase::versionNeeded = 3;
//Specifies image formats and their associated signature bytes (expected first x bytes, in hex)
const QList<QString> PictureLoader::imgFormats = QList<QString>() << ".png";
const QList<QByteArray> PictureLoader::imgSignatures = QList<QByteArray>() << "89504E470D0A1A0A";
static QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardSet *set) static QXmlStreamWriter &operator<<(QXmlStreamWriter &xml, const CardSet *set)
{ {
xml.writeStartElement("set"); xml.writeStartElement("set");
...@@ -135,22 +132,17 @@ void PictureLoader::processLoadQueue() ...@@ -135,22 +132,17 @@ void PictureLoader::processLoadQueue()
<< _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full"; << _picsPath + "/downloadedPics/" + ptl.getSetName() + "/" + ptl.getCard()->getCorrectedName() + ".full";
QImage image; QImage image;
QString extension; bool found = false;
bool found;
int i, j;
//Iterates through the list of paths, iterating through imgFormats in each to find the image in any supported format; default is JPG //Iterates through the list of paths, searching for images with the desired name with any QImageReader-supported extension
for (found = false, i = 0; i < picsPaths.length() && !found; i ++) for (int i = 0; i < picsPaths.length() && !found; i ++) {
for (extension = ".jpg", j = 0; !found; j ++) { QImageReader imgReader;
//qDebug() << picsPaths.at(i) + extension; imgReader.setFileName(picsPaths.at(i));
if (image.load(picsPaths.at(i) + extension)) { if (imgReader.read(&image)) {
emit imageLoaded(ptl.getCard(), image); emit imageLoaded(ptl.getCard(), image);
found = true; found = true;
}
if (j >= imgFormats.length())
break;
extension = imgFormats.at(j);
} }
}
if (!found) { if (!found) {
if (picDownload) { if (picDownload) {
...@@ -225,32 +217,26 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply) ...@@ -225,32 +217,26 @@ void PictureLoader::picDownloadFinished(QNetworkReply *reply)
qDebug() << "Download failed:" << reply->errorString(); qDebug() << "Download failed:" << reply->errorString();
} }
const QByteArray &picData = reply->readAll(); const QByteArray &picData = reply->peek(reply->size()); //peek is used to keep the data in the buffer for use by QImageReader
QImage testImage; QImage testImage;
if (testImage.loadFromData(picData)) {
if (!QDir(QString(picsPath + "/downloadedPics/")).exists()) { QImageReader imgReader;
QDir dir(picsPath); imgReader.setDecideFormatFromContent(true);
if (!dir.exists()) imgReader.setDevice(reply);
return; QString extension = "." + imgReader.format(); //the format is determined prior to reading the QImageReader data into a QImage object, as that wipes the QImageReader buffer
dir.mkdir("downloadedPics"); if (extension == ".jpeg")
} extension = ".jpg";
if (!QDir(QString(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())).exists()) {
QDir dir(QString(picsPath + "/downloadedPics")); if (imgReader.read(&testImage)) {
dir.mkdir(cardBeingDownloaded.getSetName()); if (!QDir().mkpath(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName())) {
qDebug() << picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + " could not be created.";
return;
} }
QString suffix; QString suffix;
if (!cardBeingDownloaded.getStripped()) if (!cardBeingDownloaded.getStripped())
suffix = ".full"; suffix = ".full";
//Supports JPG and formats specified in imgFormats; default is JPG
QString extension = ".jpg";
for (int i = 0; i < imgFormats.length(); i ++)
if (picData.left(QByteArray::fromHex(imgSignatures.at(i)).length()) == QByteArray::fromHex(imgSignatures.at(i))) {
extension = imgFormats.at(i);
break;
}
QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension); QFile newPic(picsPath + "/downloadedPics/" + cardBeingDownloaded.getSetName() + "/" + cardBeingDownloaded.getCard()->getCorrectedName() + suffix + extension);
if (!newPic.open(QIODevice::WriteOnly)) if (!newPic.open(QIODevice::WriteOnly))
return; return;
......
...@@ -73,8 +73,6 @@ private: ...@@ -73,8 +73,6 @@ private:
bool picDownload, picDownloadHq, downloadRunning, loadQueueRunning; bool picDownload, picDownloadHq, downloadRunning, loadQueueRunning;
void startNextPicDownload(); void startNextPicDownload();
QString getPicUrl(CardInfo* card); QString getPicUrl(CardInfo* card);
static const QList<QString> imgFormats;
static const QList<QByteArray> imgSignatures;
public: public:
PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent = 0); PictureLoader(const QString &__picsPath, bool _picDownload, bool _picDownloadHq, QObject *parent = 0);
~PictureLoader(); ~PictureLoader();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment