gta5view/SnapmaticPicture.cpp

973 lines
29 KiB
C++
Raw Normal View History

2016-02-26 22:22:34 +01:00
/*****************************************************************************
2017-06-05 18:16:17 +02:00
* gta5sync-spv Grand Theft Auto Snapmatic Picture Viewer
2017-02-11 04:21:55 +01:00
* Copyright (C) 2016-2017 Syping
2016-02-26 22:22:34 +01:00
*
2016-03-21 07:54:14 +01:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
2016-02-26 22:22:34 +01:00
*
2016-03-21 07:54:14 +01:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2016-02-26 22:22:34 +01:00
*
2016-03-21 07:54:14 +01:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-02-26 22:22:34 +01:00
*****************************************************************************/
2016-03-20 21:57:18 +01:00
#include "SnapmaticPicture.h"
2016-03-31 23:41:53 +02:00
#include "StringParser.h"
2017-08-26 14:13:35 +02:00
#include <QStringBuilder>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStringList>
#include <QVariantMap>
#include <QJsonArray>
2017-01-27 04:33:27 +01:00
#include <QFileInfo>
2017-02-17 12:07:37 +01:00
#include <QPainter>
#include <QString>
2016-07-22 16:06:36 +02:00
#include <QBuffer>
#include <QDebug>
#include <QImage>
2017-03-01 15:19:40 +01:00
#include <QSize>
#include <QFile>
2016-02-26 22:22:34 +01:00
2017-02-25 06:30:37 +01:00
// PARSER ALLOCATIONS
#define snapmaticHeaderLength 278
#define snapmaticUsefulLength 260
#define snapmaticFileMaxSize 528192
#define jpegHeaderLineDifStr 2
#define jpegPreHeaderLength 14
#define jpegPicStreamLength 524288
#define jsonStreamLength 3076
#define tideStreamLength 260
// EDITOR ALLOCATIONS
#define jpegStreamEditorBegin 292
#define jsonStreamEditorBegin 524588
#define jsonStreamEditorLength 3072
#define titlStreamEditorBegin 527668
#define titlStreamEditorLength 256
#define titlStreamCharacterMax 39
2017-03-01 15:19:40 +01:00
// IMAGES VALUES
#define snapmaticResolutionW 960
#define snapmaticResolutionH 536
#define snapmaticResolution QSize(snapmaticResolutionW, snapmaticResolutionH)
2017-02-11 04:21:55 +01:00
SnapmaticPicture::SnapmaticPicture(const QString &fileName, QObject *parent) : QObject(parent), picFilePath(fileName)
2016-02-26 22:22:34 +01:00
{
2017-02-11 04:21:55 +01:00
reset();
}
SnapmaticPicture::~SnapmaticPicture()
{
}
void SnapmaticPicture::reset()
{
2016-03-21 21:46:36 +01:00
// INIT PIC
2017-02-25 06:30:37 +01:00
rawPicContent = "";
2017-03-01 15:19:40 +01:00
cachePicture = QImage();
2017-03-30 03:44:48 +02:00
jpegRawContentSizeE = 0;
2017-02-11 04:21:55 +01:00
jpegRawContentSize = 0;
picExportFileName = "";
2017-02-11 04:21:55 +01:00
isCustomFormat = 0;
2017-08-26 14:13:35 +02:00
isLoadedInRAM = 0;
2017-02-11 04:21:55 +01:00
pictureHead = "";
pictureStr = "";
2017-06-05 18:16:17 +02:00
lowRamMode = 0;
lastStep = "";
sortStr = "";
titlStr = "";
descStr = "";
2016-03-21 22:29:57 +01:00
picOk = 0;
2016-03-21 21:46:36 +01:00
// INIT JSON
2016-03-21 22:29:57 +01:00
jsonOk = 0;
jsonStr = "";
2017-02-17 12:07:37 +01:00
// SNAPMATIC PROPERTIES
localSpJson = {};
}
2017-08-26 14:13:35 +02:00
bool SnapmaticPicture::preloadFile()
{
2017-02-11 04:21:55 +01:00
QFile *picFile = new QFile(picFilePath);
picFileName = QFileInfo(picFilePath).fileName();
if (!picFile->open(QFile::ReadOnly))
{
2017-08-26 14:13:35 +02:00
lastStep = "1;/1,OpenFile," % StringParser::convertDrawStringForLog(picFilePath);
delete picFile;
return false;
}
2017-08-26 14:13:35 +02:00
if (picFilePath.right(4) != QLatin1String(".g5e"))
2017-01-27 04:33:27 +01:00
{
rawPicContent = picFile->read(snapmaticFileMaxSize);
picFile->close();
delete picFile;
2017-02-11 04:21:55 +01:00
2017-08-26 14:13:35 +02:00
// Setting is values
2017-02-11 04:21:55 +01:00
isCustomFormat = false;
2017-08-26 14:13:35 +02:00
isLoadedInRAM = true;
2017-01-27 04:33:27 +01:00
}
else
{
QByteArray g5eContent = picFile->read(snapmaticFileMaxSize + 1024);
picFile->close();
delete picFile;
2017-02-11 04:21:55 +01:00
// Set Custom Format
isCustomFormat = true;
2017-01-27 04:33:27 +01:00
// Reading g5e Content
g5eContent.remove(0, 1);
2017-08-26 14:13:35 +02:00
if (g5eContent.left(3) == QByteArray("G5E"))
2017-01-27 04:33:27 +01:00
{
g5eContent.remove(0, 3);
2017-08-26 14:13:35 +02:00
if (g5eContent.left(2).toHex() == QByteArray("1000"))
2017-01-27 04:33:27 +01:00
{
g5eContent.remove(0, 2);
2017-08-26 14:13:35 +02:00
if (g5eContent.left(3) == QByteArray("LEN"))
2017-01-27 04:33:27 +01:00
{
g5eContent.remove(0, 3);
int fileNameLength = g5eContent.left(1).toHex().toInt();
g5eContent.remove(0, 1);
2017-08-26 14:13:35 +02:00
if (g5eContent.left(3) == QByteArray("FIL"))
2017-01-27 04:33:27 +01:00
{
g5eContent.remove(0, 3);
picFileName = g5eContent.left(fileNameLength);
g5eContent.remove(0, fileNameLength);
2017-08-26 14:13:35 +02:00
if (g5eContent.left(3) == QByteArray("COM"))
2017-01-27 04:33:27 +01:00
{
g5eContent.remove(0, 3);
rawPicContent = qUncompress(g5eContent);
2017-08-26 14:13:35 +02:00
// Setting is values
isLoadedInRAM = true;
2017-01-27 04:33:27 +01:00
}
else
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",4,G5E_FORMATERROR";
2017-01-27 04:33:27 +01:00
return false;
}
}
else
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",3,G5E_FORMATERROR";
2017-01-27 04:33:27 +01:00
return false;
}
}
else
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",2,G5E_FORMATERROR";
2017-01-27 04:33:27 +01:00
return false;
}
}
else
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",1,G5E_NOTCOMPATIBLE";
2017-01-27 04:33:27 +01:00
return false;
}
}
else
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",1,G5E_FORMATERROR";
2017-01-27 04:33:27 +01:00
return false;
}
}
2017-08-26 14:13:35 +02:00
emit preloaded();
return true;
}
bool SnapmaticPicture::readingPicture(bool writeEnabled_, bool cacheEnabled_, bool fastLoad, bool lowRamMode_)
{
// Start opening file
// lastStep is like currentStep
// Set boolean values
writeEnabled = writeEnabled_;
cacheEnabled = cacheEnabled_;
lowRamMode = lowRamMode_;
if (!writeEnabled) { lowRamMode = false; } // Low RAM Mode only works when writeEnabled is true
QIODevice *picStream;
if (!isLoadedInRAM) { preloadFile(); }
2016-07-22 16:06:36 +02:00
picStream = new QBuffer(&rawPicContent);
picStream->open(QIODevice::ReadWrite);
// Reading Snapmatic Header
2016-07-22 16:06:36 +02:00
if (!picStream->isReadable())
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",1,NOHEADER";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
return false;
}
2016-07-22 16:06:36 +02:00
QByteArray snapmaticHeaderLine = picStream->read(snapmaticHeaderLength);
2017-02-11 04:21:55 +01:00
pictureHead = getSnapmaticHeaderString(snapmaticHeaderLine);
// Reading JPEG Header Line
2016-07-22 16:06:36 +02:00
if (!picStream->isReadable())
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",2,NOHEADER";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
return false;
}
2016-07-22 16:06:36 +02:00
QByteArray jpegHeaderLine = picStream->read(jpegPreHeaderLength);
// Checking for JPEG
2016-03-21 21:46:36 +01:00
jpegHeaderLine.remove(0, jpegHeaderLineDifStr);
2017-08-26 14:13:35 +02:00
if (jpegHeaderLine.left(4) != QByteArray("JPEG"))
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",2,NOJPEG";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
return false;
}
2016-03-21 07:54:14 +01:00
// Read JPEG Stream
2016-07-22 16:06:36 +02:00
if (!picStream->isReadable())
2016-03-21 07:54:14 +01:00
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",2,NOPIC";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2016-03-21 07:54:14 +01:00
return false;
}
2016-07-22 16:06:36 +02:00
QByteArray jpegRawContent = picStream->read(jpegPicStreamLength);
2017-06-05 18:16:17 +02:00
if (jpegRawContent.contains("\xFF\xD9"))
2017-02-11 04:21:55 +01:00
{
2017-06-05 18:16:17 +02:00
int jpegRawContentSizeT = jpegRawContent.indexOf("\xFF\xD9") + 2;
2017-03-30 03:44:48 +02:00
jpegRawContentSizeE = jpegRawContentSizeT;
2017-02-11 04:21:55 +01:00
jpegRawContentSize = jpegRawContentSizeT;
2017-06-05 18:16:17 +02:00
if (jpegRawContent.contains("\xFF\x45\x4F\x49"))
2017-02-11 04:21:55 +01:00
{
2017-06-05 18:16:17 +02:00
jpegRawContentSizeT = jpegRawContent.indexOf("\xFF\x45\x4F\x49");
2017-02-11 04:21:55 +01:00
}
jpegRawContent = jpegRawContent.left(jpegRawContentSize);
jpegRawContentSize = jpegRawContentSizeT;
}
if (cacheEnabled) picOk = cachePicture.loadFromData(jpegRawContent, "JPEG");
if (!cacheEnabled)
{
QImage tempPicture;
picOk = tempPicture.loadFromData(jpegRawContent, "JPEG");
}
2017-02-17 12:07:37 +01:00
else if (!fastLoad)
{
QImage tempPicture = QImage(snapmaticResolution, QImage::Format_RGB888);
QPainter tempPainter(&tempPicture);
if (cachePicture.size() == snapmaticResolution)
{
tempPainter.drawImage(0, 0, cachePicture);
}
else
{
tempPainter.drawImage(0, 0, cachePicture.scaled(snapmaticResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
tempPainter.end();
cachePicture = tempPicture;
}
2016-03-21 07:54:14 +01:00
// Read JSON Stream
2016-07-22 16:06:36 +02:00
if (!picStream->isReadable())
2016-03-21 07:54:14 +01:00
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",3,NOJSON";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2017-02-11 04:21:55 +01:00
return false;
2016-03-21 07:54:14 +01:00
}
2017-08-26 14:13:35 +02:00
else if (picStream->read(4) != QByteArray("JSON"))
2016-03-21 07:54:14 +01:00
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",3,CTJSON";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2017-02-11 04:21:55 +01:00
return false;
2016-03-21 07:54:14 +01:00
}
2016-07-22 16:06:36 +02:00
QByteArray jsonRawContent = picStream->read(jsonStreamLength);
2016-03-21 07:54:14 +01:00
jsonStr = getSnapmaticJSONString(jsonRawContent);
parseJsonContent(); // JSON parsing is own function
2016-03-21 07:54:14 +01:00
2016-07-22 16:06:36 +02:00
if (!picStream->isReadable())
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",4,NOTITL";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2017-02-11 04:21:55 +01:00
return false;
}
2017-08-26 14:13:35 +02:00
else if (picStream->read(4) != QByteArray("TITL"))
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",4,CTTITL";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2017-02-11 04:21:55 +01:00
return false;
}
2016-07-22 16:06:36 +02:00
QByteArray titlRawContent = picStream->read(tideStreamLength);
titlStr = getSnapmaticTIDEString(titlRawContent);
2016-07-22 16:06:36 +02:00
if (!picStream->isReadable())
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",5,NODESC";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
return picOk;
}
2017-08-26 14:13:35 +02:00
else if (picStream->read(4) != QByteArray("DESC"))
{
2017-08-26 14:13:35 +02:00
lastStep = "2;/3,ReadingFile," % StringParser::convertDrawStringForLog(picFilePath) % ",5,CTDESC";
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2017-02-11 04:21:55 +01:00
return false;
}
2016-07-22 16:06:36 +02:00
QByteArray descRawContent = picStream->read(tideStreamLength);
descStr = getSnapmaticTIDEString(descRawContent);
2017-02-11 04:21:55 +01:00
updateStrings();
2016-07-22 16:06:36 +02:00
picStream->close();
delete picStream;
2017-08-26 14:13:35 +02:00
2016-07-22 16:06:36 +02:00
if (!writeEnabled) { rawPicContent.clear(); }
2017-06-05 18:16:17 +02:00
else if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
2017-08-26 14:13:35 +02:00
emit loaded();
2016-03-21 22:29:57 +01:00
return picOk;
}
2017-02-11 04:21:55 +01:00
QString SnapmaticPicture::getSnapmaticHeaderString(const QByteArray &snapmaticHeader)
{
2016-03-31 23:41:53 +02:00
QByteArray snapmaticBytes = snapmaticHeader.left(snapmaticUsefulLength);
2017-06-05 18:16:17 +02:00
QList<QByteArray> snapmaticBytesList = snapmaticBytes.split('\x01');
2016-03-31 23:41:53 +02:00
snapmaticBytes = snapmaticBytesList.at(1);
snapmaticBytesList.clear();
return StringParser::parseTitleString(snapmaticBytes, snapmaticBytes.length());
}
QString SnapmaticPicture::getSnapmaticJSONString(const QByteArray &jsonBytes)
2016-03-21 07:54:14 +01:00
{
QByteArray jsonUsefulBytes = jsonBytes;
2017-06-05 18:16:17 +02:00
jsonUsefulBytes.replace('\x00', "");
jsonUsefulBytes.replace('\x0c', "");
return QString::fromUtf8(jsonUsefulBytes).trimmed();
2016-03-21 07:54:14 +01:00
}
QString SnapmaticPicture::getSnapmaticTIDEString(const QByteArray &tideBytes)
{
QByteArray tideUsefulBytes = tideBytes;
tideUsefulBytes.remove(0,4);
2017-06-05 18:16:17 +02:00
QList<QByteArray> tideUsefulBytesList = tideUsefulBytes.split('\x00');
return QString::fromUtf8(tideUsefulBytesList.at(0)).trimmed();
}
2017-02-11 04:21:55 +01:00
void SnapmaticPicture::updateStrings()
{
2017-02-11 04:21:55 +01:00
QString cmpPicTitl = titlStr;
2017-08-26 14:13:35 +02:00
cmpPicTitl.replace('\"', "''");
cmpPicTitl.replace(' ', '_');
cmpPicTitl.replace(':', '-');
cmpPicTitl.remove('\\');
cmpPicTitl.remove('{');
cmpPicTitl.remove('}');
cmpPicTitl.remove('/');
cmpPicTitl.remove('<');
cmpPicTitl.remove('>');
cmpPicTitl.remove('*');
cmpPicTitl.remove('?');
cmpPicTitl.remove('.');
2017-02-11 04:21:55 +01:00
pictureStr = tr("PHOTO - %1").arg(localSpJson.createdDateTime.toString("MM/dd/yy HH:mm:ss"));
2017-08-26 14:13:35 +02:00
sortStr = localSpJson.createdDateTime.toString("yyMMddHHmmss") % QString::number(localSpJson.uid);
picExportFileName = sortStr % "_" % cmpPicTitl;
}
2017-06-05 18:16:17 +02:00
bool SnapmaticPicture::readingPictureFromFile(const QString &fileName, bool writeEnabled_, bool cacheEnabled_, bool fastLoad, bool lowRamMode_)
{
2017-08-26 14:13:35 +02:00
if (!fileName.isEmpty())
{
2017-02-11 04:21:55 +01:00
picFilePath = fileName;
2017-06-05 18:16:17 +02:00
return readingPicture(writeEnabled_, cacheEnabled_, fastLoad, lowRamMode_);
}
else
{
return false;
}
}
2017-02-11 04:21:55 +01:00
bool SnapmaticPicture::setImage(const QImage &picture) // dirty method
{
if (writeEnabled)
{
QByteArray picByteArray;
2017-02-11 04:21:55 +01:00
int comLvl = 100;
bool saveSuccess = false;
while (comLvl != 0 && !saveSuccess)
{
2017-02-11 04:21:55 +01:00
QByteArray picByteArrayT;
QBuffer picStreamT(&picByteArrayT);
picStreamT.open(QIODevice::WriteOnly);
saveSuccess = picture.save(&picStreamT, "JPEG", comLvl);
picStreamT.close();
if (saveSuccess)
{
2017-02-11 04:21:55 +01:00
if (picByteArrayT.length() > jpegRawContentSize)
{
2017-02-11 04:21:55 +01:00
comLvl--;
saveSuccess = false;
}
else
{
picByteArray = picByteArrayT;
}
}
2016-12-10 05:56:09 +01:00
}
2017-02-11 04:21:55 +01:00
if (saveSuccess) return setPictureStream(picByteArray);
}
return false;
}
bool SnapmaticPicture::setPictureStream(const QByteArray &picByteArray_) // clean method
{
if (writeEnabled)
{
2017-06-05 18:16:17 +02:00
bool customEOI = false;
2017-02-11 04:21:55 +01:00
QByteArray picByteArray = picByteArray_;
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
2017-02-11 04:21:55 +01:00
QBuffer snapmaticStream(&rawPicContent);
snapmaticStream.open(QIODevice::ReadWrite);
if (!snapmaticStream.seek(jpegStreamEditorBegin)) return false;
if (picByteArray.length() > jpegPicStreamLength) return false;
if (picByteArray.length() < jpegRawContentSize && jpegRawContentSize + 4 < jpegPicStreamLength)
{
2017-06-05 18:16:17 +02:00
customEOI = true;
2017-02-11 04:21:55 +01:00
}
2016-12-10 05:56:09 +01:00
while (picByteArray.length() != jpegPicStreamLength)
{
2017-06-05 18:16:17 +02:00
picByteArray += '\x00';
}
2017-06-05 18:16:17 +02:00
if (customEOI)
2017-02-11 04:21:55 +01:00
{
2017-06-05 18:16:17 +02:00
picByteArray.replace(jpegRawContentSize, 4, "\xFF\x45\x4F\x49");
2017-02-11 04:21:55 +01:00
}
int result = snapmaticStream.write(picByteArray);
2017-06-05 18:16:17 +02:00
snapmaticStream.close();
if (result != 0)
{
if (cacheEnabled)
{
2017-02-11 04:21:55 +01:00
QImage replacedPicture;
replacedPicture.loadFromData(picByteArray);
cachePicture = replacedPicture;
}
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
return true;
}
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
return false;
}
return false;
}
2017-02-11 04:21:55 +01:00
bool SnapmaticPicture::setPictureTitl(const QString &newTitle_)
{
if (writeEnabled)
{
QString newTitle = newTitle_;
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
2017-02-11 04:21:55 +01:00
QBuffer snapmaticStream(&rawPicContent);
snapmaticStream.open(QIODevice::ReadWrite);
if (!snapmaticStream.seek(titlStreamEditorBegin)) return false;
if (newTitle.length() > titlStreamCharacterMax)
{
newTitle = newTitle.left(titlStreamCharacterMax);
}
QByteArray newTitleArray = newTitle.toUtf8();
while (newTitleArray.length() != titlStreamEditorLength)
{
2017-06-05 18:16:17 +02:00
newTitleArray += '\x00';
2017-02-11 04:21:55 +01:00
}
int result = snapmaticStream.write(newTitleArray);
2017-06-05 18:16:17 +02:00
snapmaticStream.close();
2017-02-11 04:21:55 +01:00
if (result != 0)
{
titlStr = newTitle;
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
2017-02-11 04:21:55 +01:00
return true;
}
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
2017-02-11 04:21:55 +01:00
return false;
}
return false;
}
QString SnapmaticPicture::getExportPictureFileName()
{
return picExportFileName;
}
2016-04-03 10:17:01 +02:00
QString SnapmaticPicture::getPictureFileName()
{
return picFileName;
}
2017-02-11 04:21:55 +01:00
QString SnapmaticPicture::getPictureFilePath()
{
return picFilePath;
}
QString SnapmaticPicture::getPictureSortStr()
{
return sortStr;
}
QString SnapmaticPicture::getPictureDesc()
{
return descStr;
}
QString SnapmaticPicture::getPictureTitl()
{
return titlStr;
}
2017-02-11 04:21:55 +01:00
QString SnapmaticPicture::getPictureHead()
{
return pictureHead;
}
QString SnapmaticPicture::getPictureStr()
{
return pictureStr;
}
QString SnapmaticPicture::getLastStep()
{
return lastStep;
}
2017-02-11 04:21:55 +01:00
QImage SnapmaticPicture::getImage()
{
if (cacheEnabled)
{
return cachePicture;
}
else if (writeEnabled)
{
2016-12-14 00:43:31 +01:00
bool returnOk = 0;
2017-02-17 12:07:37 +01:00
QImage tempPicture;
QImage returnPicture(snapmaticResolution, QImage::Format_RGB888);
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
QBuffer snapmaticStream(&rawPicContent);
snapmaticStream.open(QIODevice::ReadOnly);
if (snapmaticStream.seek(jpegStreamEditorBegin))
{
QByteArray jpegRawContent = snapmaticStream.read(jpegPicStreamLength);
2017-02-17 12:07:37 +01:00
returnOk = tempPicture.loadFromData(jpegRawContent, "JPEG");
}
snapmaticStream.close();
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
if (returnOk)
{
2017-02-17 12:07:37 +01:00
QPainter returnPainter(&returnPicture);
if (tempPicture.size() == snapmaticResolution)
{
returnPainter.drawImage(0, 0, tempPicture);
}
else
{
returnPainter.drawImage(0, 0, tempPicture.scaled(snapmaticResolution, Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}
returnPainter.end();
return returnPicture;
}
}
else
{
2016-12-14 00:43:31 +01:00
bool returnOk = 0;
QImage returnPicture;
QIODevice *picStream;
2017-02-11 04:21:55 +01:00
QFile *picFile = new QFile(picFilePath);
if (!picFile->open(QFile::ReadOnly))
{
2017-08-26 14:13:35 +02:00
lastStep = "1;/1,OpenFile," % StringParser::convertDrawStringForLog(picFilePath);
delete picFile;
2017-02-17 12:07:37 +01:00
return QImage(0, 0, QImage::Format_RGB888);
}
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;
}
}
2017-02-17 12:07:37 +01:00
return QImage(0, 0, QImage::Format_RGB888);
}
2017-02-11 04:21:55 +01:00
int SnapmaticPicture::getContentMaxLength()
{
return jpegRawContentSize;
}
2016-03-21 22:29:57 +01:00
bool SnapmaticPicture::isPicOk()
{
return picOk;
}
2017-03-01 15:19:40 +01:00
void SnapmaticPicture::clearCache()
2016-04-13 00:32:14 +02:00
{
2017-03-01 15:19:40 +01:00
cacheEnabled = false;
cachePicture = QImage();
2016-04-13 00:32:14 +02:00
}
2017-03-01 15:19:40 +01:00
void SnapmaticPicture::emitUpdate()
2017-02-11 04:21:55 +01:00
{
2017-03-01 15:19:40 +01:00
emit updated();
2017-02-11 04:21:55 +01:00
}
2017-03-01 15:19:40 +01:00
// JSON part
bool SnapmaticPicture::isJsonOk()
{
2017-03-01 15:19:40 +01:00
return jsonOk;
}
2017-03-01 15:19:40 +01:00
QString SnapmaticPicture::getJsonStr()
{
return jsonStr;
}
SnapmaticProperties SnapmaticPicture::getSnapmaticProperties()
{
return localSpJson;
}
void SnapmaticPicture::parseJsonContent()
{
2016-12-10 05:56:09 +01:00
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toUtf8());
QJsonObject jsonObject = jsonDocument.object();
2016-12-14 00:43:31 +01:00
QVariantMap jsonMap = jsonObject.toVariantMap(); // backward compatibility
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("loc"))
{
QJsonObject locObject = jsonObject["loc"].toObject();
2016-12-10 04:35:24 +01:00
if (locObject.contains("x")) { localSpJson.location.x = locObject["x"].toDouble(); }
if (locObject.contains("y")) { localSpJson.location.y = locObject["y"].toDouble(); }
if (locObject.contains("z")) { localSpJson.location.z = locObject["z"].toDouble(); }
}
2017-02-11 04:21:55 +01:00
if (jsonObject.contains("uid"))
{
localSpJson.uid = jsonObject["uid"].toInt();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("area"))
{
2017-02-11 04:21:55 +01:00
localSpJson.location.area = jsonObject["area"].toString();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("crewid"))
2016-03-21 21:46:36 +01:00
{
2016-12-10 04:35:24 +01:00
localSpJson.crewID = jsonObject["crewid"].toInt();
2016-03-21 21:46:36 +01:00
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("creat"))
2016-05-08 19:07:55 +02:00
{
QDateTime createdTimestamp;
2016-12-14 00:43:31 +01:00
localSpJson.createdTimestamp = jsonMap["creat"].toUInt();
2016-12-10 04:35:24 +01:00
createdTimestamp.setTime_t(localSpJson.createdTimestamp);
localSpJson.createdDateTime = createdTimestamp;
2016-05-08 19:07:55 +02:00
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("plyrs"))
{
2016-12-14 00:43:31 +01:00
localSpJson.playersList = jsonMap["plyrs"].toStringList();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("meme"))
{
2016-12-10 04:35:24 +01:00
localSpJson.isMeme = jsonObject["meme"].toBool();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("mug"))
{
2016-12-10 04:35:24 +01:00
localSpJson.isMug = jsonObject["mug"].toBool();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("slf"))
{
2016-12-10 04:35:24 +01:00
localSpJson.isSelfie = jsonObject["slf"].toBool();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("drctr"))
{
2016-12-10 04:35:24 +01:00
localSpJson.isFromDirector = jsonObject["drctr"].toBool();
}
2016-12-10 04:35:24 +01:00
if (jsonObject.contains("rsedtr"))
{
2016-12-10 04:35:24 +01:00
localSpJson.isFromRSEditor = jsonObject["rsedtr"].toBool();
}
2016-03-21 22:29:57 +01:00
jsonOk = true;
}
2016-12-10 05:56:09 +01:00
bool SnapmaticPicture::setSnapmaticProperties(SnapmaticProperties newSpJson)
{
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonStr.toUtf8());
QJsonObject jsonObject = jsonDocument.object();
QJsonObject locObject;
locObject["x"] = newSpJson.location.x;
locObject["y"] = newSpJson.location.y;
locObject["z"] = newSpJson.location.z;
jsonObject["loc"] = locObject;
2017-02-11 04:21:55 +01:00
jsonObject["uid"] = newSpJson.uid;
jsonObject["area"] = newSpJson.location.area;
2016-12-10 05:56:09 +01:00
jsonObject["crewid"] = newSpJson.crewID;
jsonObject["creat"] = QJsonValue::fromVariant(newSpJson.createdTimestamp);
jsonObject["plyrs"] = QJsonValue::fromVariant(newSpJson.playersList);
jsonObject["meme"] = newSpJson.isMeme;
jsonObject["mug"] = newSpJson.isMug;
jsonObject["slf"] = newSpJson.isSelfie;
jsonObject["drctr"] = newSpJson.isFromDirector;
jsonObject["rsedtr"] = newSpJson.isFromRSEditor;
jsonDocument.setObject(jsonObject);
QString newJsonStr = QString::fromUtf8(jsonDocument.toJson(QJsonDocument::Compact));
if (newJsonStr.length() < jsonStreamEditorLength)
{
if (writeEnabled)
{
QByteArray jsonByteArray = newJsonStr.toUtf8();
while (jsonByteArray.length() != jsonStreamEditorLength)
{
2017-06-05 18:16:17 +02:00
jsonByteArray += '\x00';
2016-12-10 05:56:09 +01:00
}
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qUncompress(rawPicContent); }
2016-12-10 05:56:09 +01:00
QBuffer snapmaticStream(&rawPicContent);
snapmaticStream.open(QIODevice::ReadWrite);
if (!snapmaticStream.seek(jsonStreamEditorBegin))
{
snapmaticStream.close();
return false;
}
int result = snapmaticStream.write(jsonByteArray);
snapmaticStream.close();
if (result != 0)
{
localSpJson = newSpJson;
2016-12-10 05:56:09 +01:00
jsonStr = newJsonStr;
2017-06-05 18:16:17 +02:00
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
2016-12-10 05:56:09 +01:00
return true;
}
2017-06-05 18:16:17 +02:00
else
{
if (lowRamMode) { rawPicContent = qCompress(rawPicContent, 9); }
return false;
}
2016-12-10 05:56:09 +01:00
}
else
{
return false;
}
}
else
{
return false;
}
return true;
}
2017-03-01 15:19:40 +01:00
// FILE MANAGEMENT
2017-03-30 03:44:48 +02:00
bool SnapmaticPicture::exportPicture(const QString &fileName, const QString format)
2017-03-01 15:19:40 +01:00
{
QFile *picFile = new QFile(fileName);
if (picFile->open(QIODevice::WriteOnly))
{
2017-08-26 14:13:35 +02:00
if (format == QLatin1String("G5E"))
2017-03-01 15:19:40 +01:00
{
// Modern compressed export
QByteArray stockFileNameUTF8 = picFileName.toUtf8();
QByteArray numberLength = QByteArray::number(stockFileNameUTF8.length());
if (numberLength.length() == 1)
{
numberLength.insert(0, "0");
}
else if (numberLength.length() != 2)
{
numberLength = "00";
}
2017-06-05 18:16:17 +02:00
QByteArray g5eHeader;
2017-08-26 14:13:35 +02:00
g5eHeader.reserve(stockFileNameUTF8.length() + 16);
2017-06-05 18:16:17 +02:00
g5eHeader += '\x00'; // First Null Byte
2017-08-26 14:13:35 +02:00
g5eHeader += QByteArray("G5E"); // GTA 5 Export
2017-06-05 18:16:17 +02:00
g5eHeader += '\x10'; g5eHeader += '\x00'; // 2 byte GTA 5 Export Version
2017-08-26 14:13:35 +02:00
g5eHeader += QByteArray("LEN"); // Before Length
2017-06-05 18:16:17 +02:00
g5eHeader += QByteArray::fromHex(numberLength); // Length in HEX before Compressed
2017-08-26 14:13:35 +02:00
g5eHeader += QByteArray("FIL"); // Before File Name
2017-06-05 18:16:17 +02:00
g5eHeader += stockFileNameUTF8; // File Name
2017-08-26 14:13:35 +02:00
g5eHeader += QByteArray("COM"); // Before Compressed
2017-06-05 18:16:17 +02:00
picFile->write(g5eHeader);
if (!lowRamMode)
{
picFile->write(qCompress(rawPicContent, 9)); // Compressed Snapmatic
}
else
{
picFile->write(rawPicContent);
}
2017-03-01 15:19:40 +01:00
picFile->close();
2017-03-30 03:44:48 +02:00
delete picFile;
}
2017-08-26 14:13:35 +02:00
else if (format == QLatin1String("JPG"))
2017-03-30 03:44:48 +02:00
{
// JPEG export
QBuffer snapmaticStream(&rawPicContent);
snapmaticStream.open(QIODevice::ReadOnly);
if (snapmaticStream.seek(jpegStreamEditorBegin))
{
QByteArray jpegRawContent = snapmaticStream.read(jpegPicStreamLength);
if (jpegRawContentSizeE != 0)
{
jpegRawContent = jpegRawContent.left(jpegRawContentSizeE);
}
picFile->write(jpegRawContent);
}
picFile->close();
delete picFile;
}
else
{
// Classic straight export
2017-06-05 18:16:17 +02:00
if (!lowRamMode)
{
picFile->write(rawPicContent);
}
else
{
picFile->write(qUncompress(rawPicContent));
}
2017-03-30 03:44:48 +02:00
picFile->close();
delete picFile;
2017-03-01 15:19:40 +01:00
}
return true;
}
else
{
delete picFile;
2017-03-01 15:19:40 +01:00
return false;
}
}
void SnapmaticPicture::setPicFileName(const QString &picFileName_)
{
picFileName = picFileName_;
}
void SnapmaticPicture::setPicFilePath(const QString &picFilePath_)
{
picFilePath = picFilePath_;
}
bool SnapmaticPicture::deletePicFile()
{
if (!QFile::exists(picFilePath)) return true;
if (QFile::remove(picFilePath)) return true;
return false;
}
// VISIBILITY
bool SnapmaticPicture::isHidden()
{
2017-08-26 14:13:35 +02:00
if (picFilePath.right(7) == QLatin1String(".hidden"))
{
return true;
}
return false;
}
bool SnapmaticPicture::setPictureHidden()
{
2017-02-11 04:21:55 +01:00
if (isCustomFormat)
{
return false;
}
if (!isHidden())
{
2017-08-26 14:13:35 +02:00
QString newPicFilePath = QString(picFilePath % ".hidden");
2017-02-11 04:21:55 +01:00
if (QFile::rename(picFilePath, newPicFilePath))
{
2017-02-11 04:21:55 +01:00
picFilePath = newPicFilePath;
return true;
}
return false;
}
return true;
}
bool SnapmaticPicture::setPictureVisible()
{
2017-02-11 04:21:55 +01:00
if (isCustomFormat)
{
return false;
}
if (isHidden())
{
2017-02-11 04:21:55 +01:00
QString newPicFilePath = QString(picFilePath).remove(picFilePath.length() - 7, 7);
if (QFile::rename(picFilePath, newPicFilePath))
{
2017-02-11 04:21:55 +01:00
picFilePath = newPicFilePath;
return true;
}
return false;
}
return true;
}
2017-02-17 12:07:37 +01:00
// PREDEFINED PROPERTIES
QSize SnapmaticPicture::getSnapmaticResolution()
{
return snapmaticResolution;
}
2017-02-25 06:30:37 +01:00
// VERIFY CONTENT
bool SnapmaticPicture::verifyTitle(const QString &title)
{
// VERIFY TITLE FOR BE A VALID SNAPMATIC TITLE
if (title.length() <= titlStreamCharacterMax)
{
foreach(const QChar &titleChar, title)
{
if (!verifyTitleChar(titleChar)) return false;
}
return true;
}
return false;
}
bool SnapmaticPicture::verifyTitleChar(const QChar &titleChar)
{
// VERIFY CHAR FOR BE A VALID SNAPMATIC CHARACTER
if (titleChar.isLetterOrNumber() || titleChar.isPrint())
{
if (titleChar == '<' || titleChar == '>' || titleChar == '\\') return false;
return true;
}
return false;
}