Debian Codegraph @

cpp/tests/poppler-render.cpp

raw
1 /*
2 * Copyright (C) 2010, Pino Toscano <[email protected]>
3 * Copyright (C) 2017, 2019, Albert Astals Cid <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include <poppler-document.h>
21 #include <poppler-image.h>
22 #include <poppler-page.h>
23 #include <poppler-page-renderer.h>
24
25 #include <cstdlib>
26 #include <iostream>
27 #include <memory>
28
29 #include "parseargs.h"
30
31 bool show_help = false;
32 bool show_formats = false;
33 char out_filename[4096];
34 int doc_page = 0;
35
36 static const ArgDesc the_args[] = { { "-f", argFlag, &show_formats, 0, "show supported output image formats" },
37 { "--page", argInt, &doc_page, 0, "select page to render" },
38 { "-o", argString, &out_filename, sizeof(out_filename), "output filename for the resulting PNG image" },
39 { "-h", argFlag, &show_help, 0, "print usage information" },
40 { "--help", argFlag, &show_help, 0, "print usage information" },
41 { nullptr, argFlag, nullptr, 0, nullptr } };
42
43 static void error(const std::string &msg)
44 {
45 std::cerr << "Error: " << msg << std::endl;
46 std::cerr << "Exiting..." << std::endl;
47 exit(1);
48 }
49
50 int main(int argc, char *argv[])
51 {
52 if (!parseArgs(the_args, &argc, argv) || (argc < 2 && !show_formats) || show_help) {
53 printUsage(argv[0], "DOCUMENT", the_args);
54 exit(1);
55 }
56
57 if (show_formats) {
58 const std::vector<std::string> formats = poppler::image::supported_image_formats();
59 std::cout << "Supported image formats:" << std::endl;
60 for (const std::string &format : formats) {
61 std::cout << " " << format << std::endl;
62 }
63 exit(0);
64 }
65
66 if (!out_filename[0]) {
67 error("missing output filename (-o)");
68 }
69
70 if (!poppler::page_renderer::can_render()) {
71 error("renderer compiled without Splash support");
72 }
73
74 const std::string file_name(argv[1]);
75
76 std::unique_ptr<poppler::document> doc(poppler::document::load_from_file(file_name));
77 if (!doc) {
78 error("loading error");
79 }
80 if (doc->is_locked()) {
81 error("encrypted document");
82 }
83
84 if (doc_page < 0 || doc_page >= doc->pages()) {
85 error("specified page number out of page count");
86 }
87 std::unique_ptr<poppler::page> p(doc->create_page(doc_page));
88 if (!p) {
89 error("NULL page");
90 }
91
92 poppler::page_renderer pr;
93 pr.set_render_hint(poppler::page_renderer::antialiasing, true);
94 pr.set_render_hint(poppler::page_renderer::text_antialiasing, true);
95
96 poppler::image img = pr.render_page(p.get());
97 if (!img.is_valid()) {
98 error("rendering failed");
99 }
100
101 if (!img.save(out_filename, "png")) {
102 error("saving to file failed");
103 }
104
105 return 0;
106 }