gta5view-cmd/main.cpp

464 lines
20 KiB
C++
Raw Normal View History

2017-03-04 19:36:47 +01:00
/*****************************************************************************
* gta5view-cmd - gta5view Command Line
* Copyright (C) 2017 Syping
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "SnapmaticPicture.h"
#include <QCoreApplication>
2018-02-03 00:59:27 +01:00
#include <QImageReader>
2017-08-29 15:23:14 +02:00
#include <QDateTime>
#include <QPainter>
#include <QImage>
2018-02-01 18:09:11 +01:00
#include <QMap>
2017-03-04 19:36:47 +01:00
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QStringList args = a.arguments();
2017-08-29 15:23:14 +02:00
if (args.length() >= 3)
2017-03-04 19:36:47 +01:00
{
2018-02-03 03:00:27 +01:00
QString format = "jpg";
2018-02-01 02:05:26 +01:00
bool isDefault = true;
bool avatarMode = false;
2018-02-03 03:00:27 +01:00
bool convertToGTA = false;
2017-08-29 15:23:14 +02:00
bool customFormat = false;
2018-02-03 07:41:24 +01:00
bool formatSwitch = false;
bool keepAspectRatio = true;
2018-02-01 18:09:11 +01:00
QMap<QString,QString> flags;
if (args.length() >= 4)
2017-03-04 19:36:47 +01:00
{
2017-08-29 15:23:14 +02:00
if (args.at(3) == "-pgta")
2017-03-04 19:36:47 +01:00
{
2017-08-29 15:23:14 +02:00
convertToGTA = true;
customFormat = false;
}
else if (args.at(3) == "-g5e")
{
convertToGTA = true;
customFormat = true;
}
2018-02-03 03:00:27 +01:00
else if (args.at(3) == "-jpg")
{
format = "jpg";
convertToGTA = false;
}
2017-08-29 15:23:14 +02:00
}
if (args.length() >= 5)
{
if (args.at(4) == "-a")
{
2018-02-01 02:05:26 +01:00
isDefault = false;
avatarMode = true;
}
else if (args.at(4) == "-p")
{
2018-02-01 02:05:26 +01:00
isDefault = false;
avatarMode = false;
}
2018-02-01 02:05:26 +01:00
else if (args.at(4) == "-d")
{
isDefault = true;
}
else if (args.at(4) == "-aiar")
{
2018-02-01 02:05:26 +01:00
isDefault = false;
avatarMode = true;
keepAspectRatio = false;
}
else if (args.at(4) == "-akar")
{
2018-02-01 02:05:26 +01:00
isDefault = false;
avatarMode = true;
keepAspectRatio = true;
}
else if (args.at(4) == "-piar")
{
2018-02-01 02:05:26 +01:00
isDefault = false;
avatarMode = false;
keepAspectRatio = false;
}
else if (args.at(4) == "-pkar")
{
2018-02-01 02:05:26 +01:00
isDefault = false;
avatarMode = false;
keepAspectRatio = true;
}
else if (args.at(4) == "-diar")
{
2018-02-01 02:05:26 +01:00
isDefault = true;
keepAspectRatio = false;
}
else if (args.at(4) == "-dkar")
{
2018-02-01 02:05:26 +01:00
isDefault = true;
keepAspectRatio = true;
}
2018-02-03 07:41:24 +01:00
else if (args.at(4) == "-fs")
{
convertToGTA = false;
formatSwitch = true;
}
}
2018-02-01 18:09:11 +01:00
if (args.length() >= 6)
{
QStringList flagArgs(args);
for (int argId = 0; argId == 5; argId++)
{
flagArgs.removeAt(argId);
}
for (QString flagArg : flagArgs)
{
QStringList currentFlagArg = flagArg.split("=");
QString currentFlag = currentFlagArg.at(0).toLower();
if (currentFlag.left(1) == "-")
{
currentFlag.remove(0, 1);
currentFlagArg.removeAt(0);
QString currentValue = currentFlagArg.join(QString());
flags[currentFlag] = currentValue;
}
}
}
2017-08-29 15:23:14 +02:00
if (!convertToGTA)
{
SnapmaticPicture picture(args.at(1));
if (picture.readingPicture(true, false, true))
{
2017-10-11 11:38:16 +02:00
QString filePath = args.at(2);
2018-02-03 07:41:24 +01:00
if (!formatSwitch)
2017-08-29 15:23:14 +02:00
{
2018-02-03 07:41:24 +01:00
filePath.replace("<autodef>", picture.getExportPictureFileName());
filePath.replace("<autoext>", ".jpg");
if (!picture.exportPicture(filePath, SnapmaticFormat::JPEG_Format))
{
cout << "gta5view-cmd: Exporting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is failed!" << endl;
return 1;
}
else
{
if (flags.value("output", QString()).toLower() == "exported_filename")
{
cout << filePath.toStdString().c_str() << endl;
}
else
{
cout << "gta5view-cmd: Exporting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is successful!" << endl;
}
}
2017-08-29 15:23:14 +02:00
}
2018-02-03 03:05:11 +01:00
else
{
2018-02-03 07:41:24 +01:00
filePath.replace("<autodef>", picture.getPictureFileName());
if (customFormat)
2018-02-03 03:05:11 +01:00
{
2018-02-03 07:41:24 +01:00
filePath.replace("<autoext>", ".g5e");
if (!picture.exportPicture(filePath, SnapmaticFormat::G5E_Format))
{
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is failed!" << endl;
return 1;
}
else
{
if (flags.value("output", QString()).toLower() == "exported_filename")
{
cout << filePath.toStdString().c_str() << endl;
}
else
{
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is successful!" << endl;
}
}
2018-02-03 03:05:11 +01:00
}
else
{
2018-02-03 07:41:24 +01:00
filePath.replace("<autoext>", "");
if (!picture.exportPicture(filePath, SnapmaticFormat::PGTA_Format))
{
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is failed!" << endl;
return 1;
}
else
{
if (flags.value("output", QString()).toLower() == "exported_filename")
{
cout << filePath.toStdString().c_str() << endl;
}
else
{
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is successful!" << endl;
}
}
2018-02-03 03:05:11 +01:00
}
}
2017-08-29 15:23:14 +02:00
}
else
{
2018-02-03 02:16:24 +01:00
cout << "gta5view-cmd: Reading of " << args.at(1).toStdString().c_str() << " is failed!" << endl;
2017-03-04 19:36:47 +01:00
return 1;
}
}
else
{
2017-08-29 15:23:14 +02:00
SnapmaticPicture picture(":/template.g5e");
if (picture.readingPicture(true, false, true))
{
2018-02-03 00:59:27 +01:00
QImage image;
QFile imageFile(args.at(1));
if (!imageFile.open(QFile::ReadOnly))
{
2018-02-03 02:16:24 +01:00
cout << "gta5view-cmd: Reading of " << args.at(1).toStdString().c_str() << " is failed!" << endl;
2018-02-03 00:59:27 +01:00
return 1;
}
QImageReader imageReader;
imageReader.setDecideFormatFromContent(true);
imageReader.setDevice(&imageFile);
if (!imageReader.read(&image))
{
2018-02-03 02:16:24 +01:00
cout << "gta5view-cmd: Parsing of " << args.at(1).toStdString().c_str() << " is failed!" << endl;
2018-02-03 00:59:27 +01:00
return 1;
}
2017-08-29 15:23:14 +02:00
if (!image.isNull())
{
QSize snapmaticRes(960, 536);
QSize avatarRes(470, 470);
2018-02-01 01:53:44 +01:00
int avatarZoneX = 145;
int avatarZoneY = 66;
2018-02-01 02:05:26 +01:00
if (isDefault)
{
if (image.width() == image.height())
{
avatarMode = true;
}
else
{
avatarMode = false;
}
}
if (!avatarMode)
{
QImage snapmaticImage(snapmaticRes, QImage::Format_RGB888);
snapmaticImage.fill(Qt::black);
QPainter snapmaticPainter(&snapmaticImage);
// Picture mode
int diffWidth = 0;
int diffHeight = 0;
if (keepAspectRatio)
{
image = image.scaled(snapmaticRes, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (image.width() != snapmaticRes.width())
{
diffWidth = snapmaticRes.width() - image.width();
diffWidth = diffWidth / 2;
}
else if (image.height() != snapmaticRes.height())
{
diffHeight = snapmaticRes.height() - image.height();
diffHeight = diffHeight / 2;
}
}
else
{
image = image.scaled(snapmaticRes.width(), snapmaticRes.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
snapmaticPainter.drawImage(0 + diffWidth, 0 + diffHeight, image);
snapmaticPainter.end();
image = snapmaticImage;
}
else
{
QImage snapmaticImage(snapmaticRes, QImage::Format_RGB888);
snapmaticImage.fill(Qt::black);
QPainter snapmaticPainter(&snapmaticImage);
// Scale to Avatar Resolution if needed
if (image.width() != avatarRes.width())
{
image.scaled(avatarRes, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
// Avatar mode
int diffWidth = 0;
int diffHeight = 0;
if (keepAspectRatio)
{
image = image.scaled(avatarRes, Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (image.width() > image.height())
{
diffHeight = avatarRes.height() - image.height();
diffHeight = diffHeight / 2;
}
else if (image.width() < image.height())
{
diffWidth = avatarRes.width() - image.width();
diffWidth = diffWidth / 2;
}
}
else
{
image = image.scaled(avatarRes, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
}
2018-02-01 01:53:44 +01:00
snapmaticPainter.drawImage(avatarZoneX + diffWidth, avatarZoneY + diffHeight, image);
snapmaticPainter.end();
image = snapmaticImage;
}
2017-08-29 15:23:14 +02:00
if (picture.setImage(image))
{
2018-02-01 18:09:11 +01:00
int crew = 0;
QStringList players;
if (flags.contains("crew"))
{
bool crewValid;
int crewId = flags["crew"].toInt(&crewValid);
if (crewValid)
{
crew = crewId;
}
}
if (flags.contains("title") && SnapmaticPicture::verifyTitle(flags["title"]))
{
picture.setPictureTitle(flags["title"]);
}
else
{
avatarMode ? picture.setPictureTitle("Converted Avatar") : picture.setPictureTitle("Converted Picture");
}
if (flags.contains("players"))
{
bool flagValid = true;
QString playerFlag = flags["players"];
if (playerFlag.left(1) == "[" && playerFlag.right(1) == "]")
{
playerFlag.remove(0, 1);
playerFlag.remove(playerFlag.length() - 1, 1);
}
const QStringList playersList = playerFlag.split(",");
for (QString player : playersList)
{
if (player.left(1) == "\"" && player.right(1) == "\"")
{
player.remove(0, 1);
player.remove(player.length() - 1, 1);
}
bool playerValid;
player.toInt(&playerValid);
if (!playerValid)
{
flagValid = false;
}
}
if (flagValid)
{
players = playersList;
}
}
2017-08-29 15:23:14 +02:00
SnapmaticProperties pictureSP = picture.getSnapmaticProperties();
pictureSP.uid = QString(QTime::currentTime().toString("HHmmss") +
2017-10-11 11:34:38 +02:00
QString::number(QDate::currentDate().dayOfYear())).toInt();
2017-08-29 15:23:14 +02:00
pictureSP.createdDateTime = QDateTime::currentDateTime();
pictureSP.createdTimestamp = pictureSP.createdDateTime.toTime_t();
2018-02-01 18:09:11 +01:00
pictureSP.crewID = crew;
pictureSP.playersList = players;
2017-08-29 15:23:14 +02:00
picture.setSnapmaticProperties(pictureSP);
picture.setPictureFileName(QString("PGTA5%1").arg(QString::number(pictureSP.uid)));
2017-08-29 15:23:14 +02:00
QString filePath = args.at(2);
filePath.replace("<autodef>", picture.getPictureFileName());
if (!customFormat)
{
2017-10-11 11:34:38 +02:00
filePath.replace("<autoext>", "");
if (!picture.exportPicture(filePath, SnapmaticFormat::PGTA_Format))
2017-08-29 15:23:14 +02:00
{
2018-02-03 02:16:24 +01:00
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is failed!" << endl;
2017-08-29 15:23:14 +02:00
return 1;
}
2017-10-11 11:34:38 +02:00
else
{
2018-02-03 02:28:14 +01:00
if (flags.value("output", QString()).toLower() == "exported_filename")
2018-02-03 02:23:51 +01:00
{
cout << filePath.toStdString().c_str() << endl;
}
else
{
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is successful!" << endl;
}
2017-10-11 11:34:38 +02:00
}
2017-08-29 15:23:14 +02:00
}
else
{
2017-10-11 11:34:38 +02:00
filePath.replace("<autoext>", ".g5e");
if (!picture.exportPicture(filePath, SnapmaticFormat::G5E_Format))
2017-08-29 15:23:14 +02:00
{
2018-02-03 02:16:24 +01:00
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is failed!" << endl;
2017-08-29 15:23:14 +02:00
return 1;
}
2017-10-11 11:34:38 +02:00
else
{
2018-02-03 02:28:14 +01:00
if (flags.value("output", QString()).toLower() == "exported_filename")
2018-02-03 02:23:51 +01:00
{
cout << filePath.toStdString().c_str() << endl;
}
else
{
cout << "gta5view-cmd: Converting of " << args.at(1).toStdString().c_str() << " to " << filePath.toStdString().c_str() << " is successful!" << endl;
}
2017-10-11 11:34:38 +02:00
}
2017-08-29 15:23:14 +02:00
}
}
else
{
cout << "gta5view-cmd: Editing of Snapmatic Image Stream failed!" << endl;
return 1;
}
}
else
{
2018-02-03 00:59:27 +01:00
cout << "gta5view-cmd: " << args.at(1).toStdString().c_str() << " is invalid image!" << endl;
2017-08-29 15:23:14 +02:00
return 1;
}
}
else
{
cout << "gta5view-cmd: Reading of internal template failed!" << endl;
return 1;
}
2017-03-04 19:36:47 +01:00
}
}
else
{
2017-08-29 15:23:14 +02:00
cout << "gta5view Command Line" << endl << endl;
2018-02-01 18:09:11 +01:00
cout << "Usage: " << args.at(0).toStdString().c_str() << " source target -format -mode -flag=value" << endl;
2018-02-01 18:16:04 +01:00
if (args.length() >= 2 && (args.at(1) == "--help" || args.at(1) == "-help"))
2017-08-29 15:23:14 +02:00
{
2018-02-01 02:16:33 +01:00
cout << "Convert-only: <autodef> (auto file name at convert)" << endl;
cout << "Convert-only: <autoext> (auto file extension at convert)" << endl;
cout << "Formats: jpg pgta g5e" << endl;
2018-02-03 07:42:50 +01:00
cout << "Snapmatic Modes: a p d fs aiar akar piar pkar diar dkar" << endl;
2018-02-03 07:41:24 +01:00
cout << "Snapmatic Flags: crew=int players=int[] title=string" << endl;
2018-02-03 07:42:50 +01:00
cout << "Global Flags: output=string" << endl;
2017-08-29 15:23:14 +02:00
}
2017-03-04 19:36:47 +01:00
return 255;
}
2017-03-04 19:41:31 +01:00
return 0;
2017-03-04 19:36:47 +01:00
}