Debian Codegraph @

qt5/tests/test-render-to-file.cpp

raw
1 #include <QtCore/QDebug>
2 #include <QtCore/QFile>
3 #include <QGuiApplication>
4 #include <QImage>
5
6 #include <poppler-qt5.h>
7
8 int main(int argc, char **argv)
9 {
10 QGuiApplication a(argc, argv); // QApplication required!
11
12 if (argc < 2 || (argc == 3 && strcmp(argv[2], "-qpainter") != 0) || argc > 3) {
13 // use argument as file name
14 qWarning() << "usage: test-render-to-file-qt5 filename [-qpainter]";
15 exit(1);
16 }
17
18 Poppler::Document *doc = Poppler::Document::load(QFile::decodeName(argv[1]));
19 if (!doc) {
20 qWarning() << "doc not loaded";
21 exit(1);
22 }
23
24 if (doc->isLocked()) {
25 qWarning() << "document locked (needs password)";
26 exit(0);
27 }
28
29 if (doc->numPages() <= 0) {
30 delete doc;
31 qDebug() << "Doc has no pages";
32 return 0;
33 }
34
35 QString backendString;
36 if (argc == 3 && strcmp(argv[2], "-qpainter") == 0) {
37 backendString = QStringLiteral("QPainter");
38 doc->setRenderBackend(Poppler::Document::QPainterBackend);
39 } else {
40 backendString = QStringLiteral("Splash");
41 doc->setRenderBackend(Poppler::Document::SplashBackend);
42 }
43 doc->setRenderHint(Poppler::Document::Antialiasing, true);
44 doc->setRenderHint(Poppler::Document::TextAntialiasing, true);
45
46 for (int i = 0; i < doc->numPages(); ++i) {
47 Poppler::Page *page = doc->page(i);
48 if (page) {
49 qDebug() << "Rendering page using" << backendString << "backend: " << i;
50 QTime t = QTime::currentTime();
51 QImage image = page->renderToImage();
52 qDebug() << "Rendering took" << t.msecsTo(QTime::currentTime()) << "msecs";
53 image.save(QStringLiteral("test-render-to-file%1.png").arg(i));
54 delete page;
55 }
56 }
57
58 return 0;
59 }