2020-05-01 16:02:29 +03:00
|
|
|
#include "convertworker.h"
|
2020-05-01 21:54:33 +03:00
|
|
|
#include <sailfishapp.h>
|
2020-05-16 16:17:42 +03:00
|
|
|
#include "papersizes.h"
|
2020-06-01 22:12:29 +03:00
|
|
|
#include <QImage>
|
|
|
|
#include <QMatrix>
|
|
|
|
#include <QPainter>
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
void ppm2PwgEnv(QStringList& env, bool urf, quint32 Quality, QString PaperSize,
|
2020-06-07 18:38:40 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, bool srgb, quint32 pages)
|
2020-05-01 16:02:29 +03:00
|
|
|
{
|
2020-05-09 21:39:50 +03:00
|
|
|
env.append("HWRES_X="+QString::number(HwResX));
|
|
|
|
env.append("HWRES_Y="+QString::number(HwResY));
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-05-09 21:39:50 +03:00
|
|
|
if(urf)
|
|
|
|
{
|
|
|
|
env.append("URF=true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Quality >= 3 && Quality <= 5)
|
|
|
|
{
|
|
|
|
env.append("QUALITY="+QString::number(Quality));
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
if(PaperSize != "")
|
|
|
|
{
|
|
|
|
env.append("PAGE_SIZE_NAME="+PaperSize);
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:39:50 +03:00
|
|
|
env.append("DUPLEX="+QString::number(TwoSided));
|
|
|
|
env.append("TUMBLE="+QString::number(Tumble));
|
2020-06-07 18:38:40 +03:00
|
|
|
env.append("SRGB="+QString::number(srgb));
|
2020-05-09 21:39:50 +03:00
|
|
|
|
2020-06-06 15:59:44 +03:00
|
|
|
if(pages != 0)
|
|
|
|
{
|
|
|
|
env.append("PAGES="+QString::number(pages));
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
|
2020-06-04 22:31:46 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2020-05-09 21:39:50 +03:00
|
|
|
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble)
|
|
|
|
{
|
2020-06-06 15:59:44 +03:00
|
|
|
|
|
|
|
QProcess* pdfinfo = new QProcess(this);
|
|
|
|
pdfinfo->setProgram("pdfinfo");
|
|
|
|
pdfinfo->setArguments({filename});
|
|
|
|
pdfinfo->start();
|
|
|
|
|
|
|
|
if(!pdfinfo->waitForStarted() || !pdfinfo->waitForFinished())
|
|
|
|
{
|
|
|
|
qDebug() << "pdfinfo died";
|
|
|
|
pdfinfo->deleteLater();
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Failed to get info about PDF file"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QByteArray pdfInfoOutput = pdfinfo->readAllStandardOutput();
|
|
|
|
pdfinfo->deleteLater();
|
|
|
|
qDebug() << pdfInfoOutput;
|
|
|
|
QList<QByteArray> pdfInfoOutputLines = pdfInfoOutput.split('\n');
|
|
|
|
quint32 pages = 0;
|
|
|
|
for(QList<QByteArray>::iterator it = pdfInfoOutputLines.begin(); it != pdfInfoOutputLines.end(); it++)
|
|
|
|
{
|
|
|
|
if(it->startsWith("Pages"))
|
|
|
|
{
|
|
|
|
QList<QByteArray> pagesTokens = it->split(' ');
|
|
|
|
pages = pagesTokens.last().toInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
bool urf = false;
|
2020-06-06 16:47:43 +03:00
|
|
|
bool ps = false;
|
2020-06-04 22:31:46 +03:00
|
|
|
|
|
|
|
if(targetFormat == "image/urf")
|
|
|
|
{
|
|
|
|
urf = true;
|
|
|
|
}
|
|
|
|
else if(targetFormat == "image/pwg-raster")
|
|
|
|
{
|
|
|
|
//ok
|
|
|
|
}
|
2020-06-06 16:47:43 +03:00
|
|
|
else if(targetFormat == "application/postscript")
|
|
|
|
{
|
|
|
|
ps = true;
|
|
|
|
}
|
2020-06-04 22:31:46 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
emit failed(tr("Unsupported target format"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:56:47 +03:00
|
|
|
if(urf && (HwResX != HwResY))
|
2020-05-09 21:39:50 +03:00
|
|
|
{ // URF only supports symmetric resolutions
|
2020-06-01 22:56:47 +03:00
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Unsupported resolution (dpi)"));
|
|
|
|
return;
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
2020-05-05 21:36:01 +03:00
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
QString ShortPaperSize;
|
|
|
|
if(PaperSize == "iso_a4_210x297mm")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "A4";
|
|
|
|
}
|
|
|
|
else if (PaperSize == "iso_a3_297x420mm")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "A3";
|
|
|
|
}
|
|
|
|
else if (PaperSize == "na_letter_8.5x11in")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "letter";
|
|
|
|
}
|
|
|
|
else if (PaperSize == "na_legal_8.5x14in")
|
|
|
|
{
|
|
|
|
ShortPaperSize = "legal";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported PDF paper size" << PaperSize;
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Unsupported PDF paper size"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
if(ps)
|
2020-05-09 21:39:50 +03:00
|
|
|
{
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* pdftops = new QProcess(this);
|
|
|
|
pdftops->setProgram("pdftops");
|
2020-06-07 16:27:27 +03:00
|
|
|
QStringList PdfToPsArgs = {"-paper", ShortPaperSize, filename, "-"};
|
|
|
|
if(TwoSided)
|
|
|
|
{
|
|
|
|
PdfToPsArgs.prepend("-duplex");
|
|
|
|
}
|
|
|
|
pdftops->setArguments(PdfToPsArgs);
|
2020-06-06 16:47:43 +03:00
|
|
|
|
|
|
|
pdftops->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
|
|
|
connect(pdftops, SIGNAL(finished(int, QProcess::ExitStatus)), pdftops, SLOT(deleteLater()));
|
|
|
|
|
|
|
|
pdftops->start();
|
|
|
|
|
|
|
|
qDebug() << "Starting";
|
|
|
|
|
|
|
|
if(!pdftops->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftops died";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Started";
|
|
|
|
|
|
|
|
if(!pdftops->waitForFinished())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftops failed";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
2020-05-09 21:39:50 +03:00
|
|
|
}
|
2020-06-06 16:47:43 +03:00
|
|
|
else
|
|
|
|
{
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* pdftocairo = new QProcess(this);
|
|
|
|
pdftocairo->setProgram("pdftocairo");
|
|
|
|
pdftocairo->setArguments({"-pdf", "-paper", ShortPaperSize, filename, "-"});
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* pdftoppm = new QProcess(this);
|
|
|
|
pdftoppm->setProgram("pdftoppm");
|
|
|
|
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
|
|
|
|
if(Colors != 3)
|
|
|
|
{
|
|
|
|
Pdf2PpmArgs.append("-gray");
|
|
|
|
}
|
|
|
|
pdftoppm->setArguments(Pdf2PpmArgs);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QProcess* ppm2pwg = new QProcess(this);
|
|
|
|
// Yo dawg, I heard you like programs...
|
|
|
|
ppm2pwg->setProgram("harbour-seaprint");
|
|
|
|
ppm2pwg->setArguments({"ppm2pwg"});
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
QStringList env;
|
2020-06-07 18:38:40 +03:00
|
|
|
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, false, pages);
|
2020-06-06 16:47:43 +03:00
|
|
|
qDebug() << "ppm2pwg env is " << env;
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
ppm2pwg->setEnvironment(env);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
pdftocairo->setStandardOutputProcess(pdftoppm);
|
|
|
|
pdftoppm->setStandardOutputProcess(ppm2pwg);
|
|
|
|
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
connect(pdftocairo, SIGNAL(finished(int, QProcess::ExitStatus)), pdftocairo, SLOT(deleteLater()));
|
|
|
|
connect(pdftoppm, SIGNAL(finished(int, QProcess::ExitStatus)), pdftoppm, SLOT(deleteLater()));
|
|
|
|
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
qDebug() << "All connected";
|
2020-05-01 16:02:29 +03:00
|
|
|
|
2020-06-06 16:47:43 +03:00
|
|
|
pdftocairo->start();
|
|
|
|
pdftoppm->start();
|
|
|
|
ppm2pwg->start();
|
|
|
|
|
|
|
|
qDebug() << "Starting";
|
|
|
|
|
|
|
|
if(!pdftocairo->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftocairo died";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!pdftoppm->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "pdftoppm died";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(!ppm2pwg->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "ppm2pwg died";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug() << "All started";
|
|
|
|
|
|
|
|
|
|
|
|
if(!ppm2pwg->waitForFinished())
|
|
|
|
{
|
|
|
|
qDebug() << "ppm2pwg failed";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
2020-05-01 16:02:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
qDebug() << "Finished";
|
|
|
|
|
|
|
|
emit done(request, tempfile);
|
|
|
|
qDebug() << "posted";
|
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
|
2020-06-04 22:31:46 +03:00
|
|
|
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
|
2020-05-16 16:17:42 +03:00
|
|
|
quint32 HwResX, quint32 HwResY)
|
2020-05-13 20:53:44 +03:00
|
|
|
{
|
2020-06-04 22:31:46 +03:00
|
|
|
bool urf = false;
|
|
|
|
QString imageFormat = "";
|
|
|
|
QStringList supportedImageFormats = {"image/jpeg", "image/png", "image/gif"};
|
|
|
|
|
|
|
|
if(targetFormat == "image/urf")
|
|
|
|
{
|
|
|
|
urf = true;
|
|
|
|
}
|
|
|
|
else if(targetFormat == "image/pwg-raster")
|
|
|
|
{
|
|
|
|
//ok
|
|
|
|
}
|
|
|
|
else if(supportedImageFormats.contains(targetFormat))
|
|
|
|
{
|
|
|
|
imageFormat = targetFormat.split("/")[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit failed(tr("Unsupported target format"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-01 22:56:47 +03:00
|
|
|
if(urf && (HwResX != HwResY))
|
|
|
|
{ // URF only supports symmetric resolutions
|
|
|
|
qDebug() << "Unsupported URF resolution" << PaperSize;
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Unsupported resolution (dpi)"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-16 16:17:42 +03:00
|
|
|
if(!PaperSizes.contains(PaperSize))
|
|
|
|
{
|
|
|
|
qDebug() << "Unsupported paper size" << PaperSize;
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Unsupported paper size"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPair<float,float> wh = PaperSizes[PaperSize];
|
|
|
|
quint32 Width = qRound(wh.first/25.4*HwResX);
|
|
|
|
quint32 Height = qRound(wh.second/25.4*HwResY);
|
|
|
|
|
|
|
|
qDebug() << "Size is" << Width << "x" << Height;
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
QImage inImage;
|
|
|
|
if(!inImage.load(filename))
|
|
|
|
{
|
|
|
|
qDebug() << "failed to load";
|
2020-05-14 22:00:37 +03:00
|
|
|
emit failed(tr("Failed to load image"));
|
2020-05-13 20:53:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(inImage.width() > inImage.height())
|
|
|
|
{
|
|
|
|
inImage = inImage.transformed(QMatrix().rotate(90.0));
|
|
|
|
}
|
|
|
|
inImage = inImage.scaled(Width, Height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
QImage outImage = QImage(Width, Height, inImage.format());
|
|
|
|
outImage.fill(Qt::white);
|
|
|
|
QPainter painter(&outImage);
|
2020-05-13 21:18:20 +03:00
|
|
|
painter.drawImage(0, (outImage.height()-inImage.height())/2, inImage);
|
2020-05-13 20:53:44 +03:00
|
|
|
painter.end();
|
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
if(imageFormat != "")
|
|
|
|
{ // We are converting to a supported image format
|
|
|
|
|
|
|
|
QFile tempfileAsFile(tempfile->fileName());
|
|
|
|
tempfileAsFile.open(QIODevice::Append);
|
2020-06-08 19:24:40 +03:00
|
|
|
outImage.save(&tempfileAsFile, imageFormat.toStdString().c_str());
|
2020-06-04 22:31:46 +03:00
|
|
|
tempfileAsFile.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // We are converting to a raster format
|
|
|
|
QTemporaryFile tmpImage;
|
|
|
|
tmpImage.open();
|
|
|
|
qDebug() << "Raw image: " << tmpImage.fileName();
|
|
|
|
|
|
|
|
outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm");
|
|
|
|
tmpImage.close();
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
QProcess* ppm2pwg = new QProcess(this);
|
|
|
|
// Yo dawg, I heard you like programs...
|
|
|
|
ppm2pwg->setProgram("harbour-seaprint");
|
|
|
|
ppm2pwg->setArguments({"ppm2pwg"});
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
QStringList env;
|
2020-06-07 18:38:40 +03:00
|
|
|
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, true, 0);
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "ppm2pwg env is " << env;
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
ppm2pwg->setEnvironment(env);
|
|
|
|
ppm2pwg->setStandardInputFile(tmpImage.fileName());
|
|
|
|
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "All connected";
|
|
|
|
ppm2pwg->start();
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "Starting";
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
if(!ppm2pwg->waitForStarted())
|
|
|
|
{
|
|
|
|
qDebug() << "ppm2pwg died";
|
|
|
|
tempfile->deleteLater();
|
|
|
|
emit failed(tr("Conversion error"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug() << "All started";
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
ppm2pwg->waitForFinished();
|
2020-05-13 20:53:44 +03:00
|
|
|
|
2020-06-04 22:31:46 +03:00
|
|
|
qDebug() << "Finished";
|
|
|
|
}
|
2020-05-13 20:53:44 +03:00
|
|
|
|
|
|
|
emit done(request, tempfile);
|
|
|
|
qDebug() << "posted";
|
|
|
|
}
|