RAM usage reduced (tested 107 pics 220MB to 68MB)

This commit is contained in:
Rafael 2016-12-09 23:13:21 +01:00
parent 5af3dabcb8
commit 3be2e49d5c
4 changed files with 128 additions and 22 deletions

View file

@ -105,6 +105,5 @@ void PictureWidget::updateWindowSize(int screenID)
this->resize(desktopRect.width(), desktopRect.height());
this->showFullScreen();
pictureLabel->setPixmap(QPixmap::fromImage(image.scaled(desktopRect.width(), desktopRect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation)));
}
}

View file

@ -67,15 +67,25 @@ SnapmaticPicture::SnapmaticPicture(const QString &fileName, QObject *parent) : Q
jsonArea = "";
jsonCreatedTimestamp = 0;
jsonPlyrsList = QStringList();
jsonMeme = 0;
jsonMug = 0;
jsonSelfie = 0;
jsonDirector = 0;
jsonRockstarEditor = 0;
}
bool SnapmaticPicture::readingPicture(bool writeEnabled_)
SnapmaticPicture::~SnapmaticPicture()
{
}
bool SnapmaticPicture::readingPicture(bool writeEnabled_, bool cacheEnabled_)
{
// Start opening file
// lastStep is like currentStep
// Set boolean values
writeEnabled = writeEnabled_;
cacheEnabled = cacheEnabled_;
QFile *picFile = new QFile(picFileName);
QIODevice *picStream;
@ -88,6 +98,9 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
return false;
}
rawPicContent = picFile->read(snapmaticFileMaxSize);
picFile->close();
delete picFile;
picStream = new QBuffer(&rawPicContent);
picStream->open(QIODevice::ReadWrite);
@ -97,7 +110,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",1,NOHEADER";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return false;
}
QByteArray snapmaticHeaderLine = picStream->read(snapmaticHeaderLength);
@ -109,7 +122,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOHEADER";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return false;
}
QByteArray jpegHeaderLine = picStream->read(jpegPreHeaderLength);
@ -121,7 +134,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOJPEG";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return false;
}
@ -131,11 +144,16 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",2,NOPIC";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return false;
}
QByteArray jpegRawContent = picStream->read(jpegPicStreamLength);
picOk = cachePicture.loadFromData(jpegRawContent, "JPEG");
if (cacheEnabled) picOk = cachePicture.loadFromData(jpegRawContent, "JPEG");
if (!cacheEnabled)
{
QImage tempPicture;
picOk = tempPicture.loadFromData(jpegRawContent, "JPEG");
}
// Read JSON Stream
if (!picStream->isReadable())
@ -143,7 +161,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",3,NOJSON";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return picOk;
}
else if (picStream->read(4) != "JSON")
@ -151,7 +169,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",3,CTJSON";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return picOk;
}
QByteArray jsonRawContent = picStream->read(jsonStreamLength);
@ -163,7 +181,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",4,NOTITL";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return picOk;
}
else if (picStream->read(4) != "TITL")
@ -171,7 +189,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",4,CTTITL";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return picOk;
}
QByteArray titlRawContent = picStream->read(tideStreamLength);
@ -182,7 +200,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",5,NODESC";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return picOk;
}
else if (picStream->read(4) != "DESC")
@ -190,7 +208,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
lastStep = "2;/3,ReadingFile," + StringParser::convertDrawStringForLog(picFileName) + ",5,CTDESC";
picStream->close();
picStream->deleteLater();
delete picFile;
delete picStream;
return picOk;
}
QByteArray descRawContent = picStream->read(tideStreamLength);
@ -200,9 +218,7 @@ bool SnapmaticPicture::readingPicture(bool writeEnabled_)
picStream->close();
picStream->deleteLater();
picFile->deleteLater();
delete picStream;
delete picFile;
if (!writeEnabled) { rawPicContent.clear(); }
return picOk;
}
@ -271,12 +287,12 @@ void SnapmaticPicture::parseSnapmaticExportAndSortString()
}
}
bool SnapmaticPicture::readingPictureFromFile(const QString &fileName, bool writeEnabled_)
bool SnapmaticPicture::readingPictureFromFile(const QString &fileName, bool writeEnabled_, bool cacheEnabled_)
{
if (fileName != "")
{
picFileName = fileName;
return readingPicture(writeEnabled_);
return readingPicture(writeEnabled_, cacheEnabled_);
}
else
{
@ -383,9 +399,65 @@ QString SnapmaticPicture::getLastStep()
}
QImage SnapmaticPicture::getPicture()
{
if (cacheEnabled)
{
return cachePicture;
}
else if (writeEnabled)
{
bool returnOk;
QImage returnPicture;
QBuffer snapmaticStream(&rawPicContent);
snapmaticStream.open(QIODevice::ReadOnly);
if (snapmaticStream.seek(jpegStreamEditorBegin))
{
QByteArray jpegRawContent = snapmaticStream.read(jpegPicStreamLength);
returnOk = returnPicture.loadFromData(jpegRawContent, "JPEG");
}
snapmaticStream.close();
if (returnOk)
{
return returnPicture;
}
}
else
{
bool returnOk;
QImage returnPicture;
QIODevice *picStream;
QFile *picFile = new QFile(picFileName);
if (!picFile->open(QFile::ReadOnly))
{
lastStep = "1;/1,OpenFile," + StringParser::convertDrawStringForLog(picFileName);
picFile->deleteLater();
delete picFile;
return QImage(0, 0, QImage::Format_RGB32);
}
rawPicContent = picFile->read(snapmaticFileMaxSize);
picFile->close();
delete picFile;
picStream = new QBuffer(&rawPicContent);
picStream->open(QIODevice::ReadWrite);
if (picStream->seek(jpegStreamEditorBegin))
{
QByteArray jpegRawContent = picStream->read(jpegPicStreamLength);
returnOk = returnPicture.loadFromData(jpegRawContent, "JPEG");
}
picStream->close();
delete picStream;
if (returnOk)
{
return returnPicture;
}
}
return QImage(0, 0, QImage::Format_RGB32);
}
bool SnapmaticPicture::isPicOk()
{
@ -397,6 +469,12 @@ void SnapmaticPicture::setPicFileName(QString picFileName_)
picFileName = picFileName_;
}
void SnapmaticPicture::clearCache()
{
cacheEnabled = false;
cachePicture = QImage(0, 0, QImage::Format_RGB32);
}
// JSON part
void SnapmaticPicture::parseJsonContent()
@ -432,6 +510,26 @@ void SnapmaticPicture::parseJsonContent()
{
jsonPlyrsList = jsonMap["plyrs"].toStringList();
}
if (jsonMap.contains("meme"))
{
jsonMeme = jsonMap["meme"].toBool();
}
if (jsonMap.contains("mug"))
{
jsonMug = jsonMap["mug"].toBool();
}
if (jsonMap.contains("slf"))
{
jsonSelfie = jsonMap["slf"].toBool();
}
if (jsonMap.contains("drctr"))
{
jsonDirector = jsonMap["drctr"].toBool();
}
if (jsonMap.contains("rsedtr"))
{
jsonRockstarEditor = jsonMap["rsedtr"].toBool();
}
jsonOk = true;
}

View file

@ -31,9 +31,11 @@ class SnapmaticPicture : public QObject
Q_OBJECT
public:
explicit SnapmaticPicture(const QString &fileName = "", QObject *parent = 0);
bool readingPictureFromFile(const QString &fileName, bool writeEnabled = false);
bool readingPicture(bool writeEnabled = false);
~SnapmaticPicture();
bool readingPictureFromFile(const QString &fileName, bool writeEnabled = true, bool cacheEnabled = true);
bool readingPicture(bool writeEnabled = true, bool cacheEnabled = true);
bool isPicOk();
void clearCache();
QImage getPicture();
QString getLastStep();
QString getPictureStr();
@ -77,6 +79,7 @@ private:
QString descStr;
bool picOk;
bool writeEnabled;
bool cacheEnabled;
// PARSE INT
int snapmaticHeaderLength;
@ -106,6 +109,11 @@ private:
QStringList jsonPlyrsList;
uint jsonCreatedTimestamp;
QDateTime jsonCreatedDateTime;
bool jsonMeme;
bool jsonMug;
bool jsonSelfie;
bool jsonDirector;
bool jsonRockstarEditor;
signals:

View file

@ -86,11 +86,12 @@ void SnapmaticWidget::setSnapmaticPicture(SnapmaticPicture *picture, QString pic
picStr = picture->getPictureStr();
picTitl = picture->getPictureTitl();
QPixmap SnapmaticPixmap = QPixmap::fromImage(picture->getPicture(), Qt::AutoColor);
SnapmaticPixmap.scaled(ui->labPicture->width(), ui->labPicture->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
QPixmap SnapmaticPixmap = QPixmap::fromImage(picture->getPicture().scaled(ui->labPicture->width(), ui->labPicture->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation), Qt::AutoColor);
ui->labPicStr->setText(picStr + "\n" + picTitl + "");
ui->labPicture->setPixmap(SnapmaticPixmap);
picture->clearCache();
adjustTextColor();
}