From f7aba42af2d0d89eb9f1d5c3d29d3d30cadbed04 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Wed, 13 May 2020 19:53:44 +0200 Subject: [PATCH] Add image-to-raster conversions --- src/convertworker.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++ src/convertworker.h | 4 ++- src/ippprinter.cpp | 45 ++++++++++++++++------------ src/ippprinter.h | 3 ++ 4 files changed, 101 insertions(+), 19 deletions(-) diff --git a/src/convertworker.cpp b/src/convertworker.cpp index b876454..c814b2d 100644 --- a/src/convertworker.cpp +++ b/src/convertworker.cpp @@ -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"; +} diff --git a/src/convertworker.h b/src/convertworker.h index a500237..0246efd 100644 --- a/src/convertworker.h +++ b/src/convertworker.h @@ -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); diff --git a/src/ippprinter.cpp b/src/ippprinter.cpp index 06b233e..d9ba839 100644 --- a/src/ippprinter.cpp +++ b/src/ippprinter.cpp @@ -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,23 +304,10 @@ 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(); - QString Sides = getAttrOrDefault(attrs, "sides").toString(); - bool TwoSided = false; - bool Tumble = false; - if(Sides=="two-sided-long-edge") - { - TwoSided = true; - } - else if(Sides=="two-sided-short-edge") - { - TwoSided = true; - Tumble = true; - } - QTemporaryFile* tempfile = new QTemporaryFile(); tempfile->open(); tempfile->write(contents); @@ -328,8 +316,29 @@ void IppPrinter::print(QJsonObject attrs, QString filename){ setBusyMessage("Converting"); - emit doConvertPdf(request, filename, tempfile, target==UrfConvert, Colors, Quality, - HwResX, HwResY, TwoSided, Tumble); + if(from == Pdf ) + { + + QString Sides = getAttrOrDefault(attrs, "sides").toString(); + bool TwoSided = false; + bool Tumble = false; + if(Sides=="two-sided-long-edge") + { + TwoSided = true; + } + else if(Sides=="two-sided-short-edge") + { + TwoSided = true; + Tumble = true; + } + + 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 { diff --git a/src/ippprinter.h b/src/ippprinter.h index 93b4e01..5b64d4a 100644 --- a/src/ippprinter.h +++ b/src/ippprinter.h @@ -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: