Add image-to-raster conversions

This commit is contained in:
Anton Thomasson 2020-05-13 19:53:44 +02:00
parent 16e6d8d766
commit f7aba42af2
4 changed files with 101 additions and 19 deletions

View file

@ -97,3 +97,71 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
emit done(request, tempfile);
qDebug() << "posted";
}
void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, quint32 HwResX, quint32 HwResY)
{
quint32 Width = 210.0/25.4*HwResX;
quint32 Height = 297.0/25.4*HwResY;
QImage inImage;
if(!inImage.load(filename))
{
qDebug() << "failed to load";
emit failed();
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);
painter.drawImage(0,0, inImage);
painter.end();
QTemporaryFile tmpImage;
tmpImage.open();
qDebug() << "Raw image: " << tmpImage.fileName();
outImage.save(tmpImage.fileName(), Colors == 1 ? "pgm" : "ppm");
tmpImage.close();
QProcess* ppm2pwg = new QProcess(this);
// Yo dawg, I heard you like programs...
ppm2pwg->setProgram("harbour-seaprint");
ppm2pwg->setArguments({"ppm2pwg"});
QStringList env;
ppm2PwgEnv(env, urf, Quality, HwResX, HwResY, false, false);
qDebug() << "ppm2pwg env is " << env;
ppm2pwg->setEnvironment(env);
ppm2pwg->setStandardInputFile(tmpImage.fileName());
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected";
ppm2pwg->start();
qDebug() << "Starting";
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
tempfile->deleteLater();
emit failed();
return;
}
qDebug() << "All started";
ppm2pwg->waitForFinished();
qDebug() << "Finished";
emit done(request, tempfile);
qDebug() << "posted";
}

View file

@ -11,7 +11,9 @@ public slots:
void convertPdf(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble);
//convertImage();
void convertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile,
bool urf, quint32 Colors, quint32 Quality, quint32 HwResX, quint32 HwResY);
signals:
void done(QNetworkRequest request, QTemporaryFile* data);

View file

@ -30,6 +30,7 @@ IppPrinter::IppPrinter()
connect(&_workerThread, &QThread::finished, _worker, &QObject::deleteLater);
connect(this, &IppPrinter::doConvertPdf, _worker, &ConvertWorker::convertPdf);
connect(this, &IppPrinter::doConvertImage, _worker, &ConvertWorker::convertImage);
connect(_worker, &ConvertWorker::done, this, &IppPrinter::convertDone);
connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed);
@ -275,14 +276,14 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
from = Pdf;
}
else if (mimeType.contains("image")) {
// from = Image; TODO: handle image conversions
from = Image;
}
QJsonArray supportedMimeTypes = _attrs["document-format-supported"].toObject()["value"].toArray();
qDebug() << supportedMimeTypes << supportedMimeTypes.contains(mimeType);
if(from == Pdf /*&& !supportedMimeTypes.contains("application/pdf")*/)
if(from == Image || (from == Pdf /*&& !supportedMimeTypes.contains("application/pdf")*/))
{
if(supportedMimeTypes.contains("image/pwg-raster"))
{
@ -303,10 +304,21 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
QString PrintColorMode = getAttrOrDefault(attrs, "print-color-mode").toString();
quint32 Colors = PrintColorMode=="color" ? 3 : PrintColorMode=="monochrome" ? 1 : 0;
if(from == Pdf && target != NoConvert)
if(target != NoConvert)
{
file.close();
QTemporaryFile* tempfile = new QTemporaryFile();
tempfile->open();
tempfile->write(contents);
qDebug() << tempfile->fileName();
tempfile->close();
setBusyMessage("Converting");
if(from == Pdf )
{
QString Sides = getAttrOrDefault(attrs, "sides").toString();
bool TwoSided = false;
bool Tumble = false;
@ -320,17 +332,14 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
Tumble = true;
}
QTemporaryFile* tempfile = new QTemporaryFile();
tempfile->open();
tempfile->write(contents);
qDebug() << tempfile->fileName();
tempfile->close();
setBusyMessage("Converting");
emit doConvertPdf(request, filename, tempfile, target==UrfConvert, Colors, Quality,
HwResX, HwResY, TwoSided, Tumble);
}
else if (from == Image)
{
emit doConvertImage(request, filename, tempfile, target==UrfConvert, Colors, Quality, HwResX, HwResY);
}
}
else
{
QByteArray filedata = file.readAll();

View file

@ -56,6 +56,9 @@ signals:
bool urf, quint32 Colors, quint32 Quality,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble);
void doConvertImage(QNetworkRequest request, QString filename, QTemporaryFile* tempfile, bool urf,
quint32 Colors, quint32 Quality, quint32 HwResX, quint32 HwResY);
void busyMessageChanged();
public slots: