-
Notifications
You must be signed in to change notification settings - Fork 1
/
lok_split.cpp
133 lines (116 loc) · 3.92 KB
/
lok_split.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* lok_split - extractor for Impress pages
*
* Written in 2016 by Mitsuya Shibata <[email protected]>
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <png++/png.hpp>
#define LOK_USE_UNSTABLE_API
#include <LibreOfficeKit/LibreOfficeKit.hxx>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
namespace lok_tools {
const std::string LO_PATH = "/usr/lib/libreoffice/program";
}
void usage(const char *name)
{
std::cerr << "Usage: " << name;
std::cerr << " [-p path-of-libreoffice] [-d dpi] ImpressFile BaseName";
std::cerr << std::endl;
}
int splitImpress(lok::Document *doc, std::string base, int dpi)
{
if (!doc || base.empty() || dpi == 0) return -1;
if (doc->getDocumentType() != LOK_DOCTYPE_PRESENTATION) {
std::cerr << "Error: Is not Impress file (type = ";
std::cerr << doc->getDocumentType() << ")" << std::endl;
return -1;
}
std::string extname = ".png";
for (int i = 0; i < doc->getParts(); ++i) {
long pageWidth, pageHeight;
doc->setPart(i);
doc->getDocumentSize(&pageWidth, &pageHeight);
/* 1 Twips = 1/1440 inch */
int canvasWidth = pageWidth * dpi / 1440;
int canvasHeight = pageHeight * dpi / 1440;
std::vector<unsigned char> pixmap(canvasWidth * canvasHeight * 4);
doc->paintTile(pixmap.data(), canvasWidth, canvasHeight, 0, 0,
pageWidth, pageHeight);
png::image <png::rgba_pixel> image(canvasWidth, canvasHeight);
for (int x = 0; x < canvasWidth; ++x) {
for (int y = 0; y < canvasHeight; ++y) {
image[y][x].red = pixmap[(canvasWidth * y + x) * 4];
image[y][x].green = pixmap[(canvasWidth * y + x) * 4 + 1];
image[y][x].blue = pixmap[(canvasWidth * y + x) * 4 + 2];
image[y][x].alpha = pixmap[(canvasWidth * y + x) * 4 + 3];
}
}
std::string filename = base + std::to_string(i) + extname;
image.write(filename);
}
return 0;
}
int main(int argc, char **argv)
{
int opt;
std::string lo_path = lok_tools::LO_PATH;
int dpi = 96;
while ((opt = getopt(argc, argv, "p:d:")) != -1) {
switch (opt) {
case 'p':
lo_path = std::string(optarg);
break;
case 'd':
dpi = atoi(optarg);
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (argc - optind < 2) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
const char *from_file = argv[optind++];
const char *base_name = argv[optind];
try {
lok::Office *lo = lok::lok_cpp_init(lo_path.c_str());
if (!lo) {
std::cerr << "Error: Failed to initialise LibreOfficeKit";
std::cerr << std::endl;
exit(EXIT_FAILURE);
}
lok::Document *doc = lo->documentLoad(from_file);
if (!doc) {
std::cerr << "Error: Failed to load document: ";
std::cerr << lo->getError() << std::endl;
delete lo;
exit(EXIT_FAILURE);
}
if (splitImpress(doc, base_name, dpi)) {
std::cerr << "Error: Failed to split document" << std::endl;
delete doc;
delete lo;
exit(EXIT_FAILURE);
}
delete doc;
delete lo;
} catch (const std::exception & e) {
std::cerr << "Error: " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
return 0;
}