-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpdffigures.cpp
316 lines (288 loc) · 9.77 KB
/
pdffigures.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <PDFDocFactory.h>
#include <GlobalParams.h>
#include <getopt.h>
#include "ExtractCaptions.h"
#include "BuildCaptions.h"
#include "PDFUtils.h"
#include "ExtractRegions.h"
#include "ExtractFigures.h"
const std::string version = "1.0.6";
void printUsage() {
printf("Usage: figureextractor [flags] </path/to/pdf>\n");
printf("--version\n");
printf("-v, --verbose\n");
printf(
"-m, --save-mistakes: If combined with -f,-o,-a additionally save/show figures that were detected\
but could not be extracted successfully\n");
printf("-s, --show-steps: Display image processing steps\n");
printf("-f, --show-final: Display pages with captions and images marked\n");
printf("-a, --save-final <prefix: Save page images with captions and images "
"marked to the given prefix. Files will be saved to "
"prefix-<page#>.png\n");
printf("-o, --save-figures <prefix>: Save images of detected figures to "
"prefix. Files are save to prefix-<(Table|Figure)>-<Number>.png\n");
printf("-c, --save-color-images <prefix>: Save color images with a high resolution for figures and tables."
"Files are save to prefix-<(Table|Figure)>-c<Number>.png\n");
printf("-j, --save-json <prefix>: Save json encoding of detected figures to "
"prefix. Files are save to prefix.json\n");
printf("-r, --reverse: Go through pages in reverse order\n");
printf("-p, --page <page#>: Run only for the given page\n");
printf("-i, --text-as-image: Attempt to parse documents even if the "
"document's text is encoded as part of an embedded image (usually "
"caused by scanned documents that have been processed with OCR). "
"These documents are not handeled well at the moment so precision is "
"liable to be poor\n");
printf("-h, --help show usage\n");
}
int main(int argc, char **argv) {
int verbose = false;
int showSteps = false;
int showFinal = false;
int onlyPage = -1;
int reverse = false;
int saveMistakes = false;
int textAsImage = false;
std::string imagePrefix = "";
std::string colorImagePrefix = "";
std::string jsonPrefix = "";
std::string finalPrefix = "";
const double resolution = 100;
const int resMultiply = 4;
const struct option long_options[] = {
{"version", no_argument, NULL, 0},
{"verbose", no_argument, &verbose, true},
{"show-steps", no_argument, &showSteps, true},
{"show-final", no_argument, &showFinal, true},
{"save-final", required_argument, NULL, 'a'},
{"save-figures", required_argument, NULL, 'o'},
{"save-color-images", required_argument, NULL, 'c'},
{"save-json", required_argument, NULL, 'j'},
{"page", required_argument, NULL, 'p'},
{"reverse", no_argument, &reverse, 'r'},
{"text-as-image", no_argument, &textAsImage, true},
{"save-mistakes", no_argument, &saveMistakes, true},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}};
int opt;
int optionIndex;
while ((opt = getopt_long(argc, argv, "svfrmic:j:a:o:p:", long_options,
&optionIndex)) != -1) {
switch (opt) {
case 0:
if (optionIndex == 0) {
printf("pdffigures version %s\n", version.c_str());
return 0;
}
break; // flag was set
case 'm':
saveMistakes = true;
break;
case 'v':
verbose = true;
break;
case 's':
showSteps = true;
break;
case 'f':
showFinal = true;
break;
case 'p':
onlyPage = std::stoi(optarg);
break;
case 'o':
imagePrefix = optarg;
break;
case 'c':
colorImagePrefix = optarg;
break;
case 'a':
finalPrefix = optarg;
break;
case 'j':
jsonPrefix = optarg;
break;
case 'i':
textAsImage = true;
break;
case 'h':
printUsage();
return 0;
case '?':
printUsage();
return 1;
}
}
if (optind > argc - 1) {
printf("No PDF file given!\n");
printUsage();
return 1;
}
if (optind < argc - 1) {
printf("Extra argument given\n");
printUsage();
return 1;
}
if (not showFinal and not showSteps and finalPrefix.length() == 0 and
not verbose and imagePrefix.length() == 0 and jsonPrefix.length() == 0 and
colorImagePrefix.length() == 0) {
printf("No output requested\n");
printUsage();
return 1;
}
globalParams = new GlobalParams(); // Set up poppler
// Build a writable str to pass to setTextEncoding
std::string str = "UTF-8";
std::vector<char> writableStr(str.begin(), str.end());
writableStr.push_back('\0');
globalParams->setTextEncoding(&writableStr.at(0));
std::unique_ptr<PDFDoc> doc(
PDFDocFactory().createPDFDoc(GooString(argv[optind]), NULL, NULL));
if (not doc->isOk()) {
return 1;
}
std::vector<TextPage *> pages = getTextPages(doc.get(), resolution);
if (verbose)
printf("Scanned %d pages\n", (int)pages.size());
DocumentStatistics docStats = DocumentStatistics(pages, doc.get(), verbose);
if (docStats.isBodyTextGraphical() and not textAsImage) {
printf("Body text appears to be encoded as graphics, skipping (use -i to "
"parse these kinds of documents)\n");
return 0;
}
std::vector<Figure> errors = std::vector<Figure>();
std::map<int, std::vector<CaptionStart>> captionStarts =
extractCaptionsFromText(pages, verbose);
if (captionStarts.size() == 0) {
printf("No captions found!");
if (jsonPrefix.length() != 0) {
// To be consistent, output a JSON file anyway
std::ofstream output((jsonPrefix + ".json").c_str());
output << "[]";
output.close();
}
return 0;
}
if (verbose) {
for (auto &c : captionStarts) {
printf("\nPage %d:", c.first);
for (size_t i = 0; i < c.second.size(); ++i) {
printf(" %s%d", getFigureTypeString(c.second.at(i).type),
c.second.at(i).number);
}
}
printf("\n");
}
std::vector<Figure> allFigures;
std::map<int, std::pair<int, int>> pageSizes;
std::map<int, std::vector<CaptionStart>>::iterator start =
captionStarts.begin();
std::map<int, std::vector<CaptionStart>>::iterator end = captionStarts.end();
while (start != end) {
int onPage;
if (reverse) {
end--;
onPage = end->first;
} else {
onPage = start->first;
start++;
}
if (onlyPage >= 0 and onlyPage != onPage)
continue;
if (verbose)
printf("Working on page %d\n", onPage);
std::unique_ptr<PIX> fullRender =
getFullRenderPix(doc.get(), onPage + 1, resolution);
std::unique_ptr<PIX> fullRender1d(pixConvertTo1(fullRender.get(), 250));
std::unique_ptr<PIX> graphics1d;
if (not docStats.isBodyTextGraphical()) {
std::unique_ptr<PIX> graphics =
getGraphicOnlyPix(doc.get(), onPage + 1, resolution);
graphics1d = std::unique_ptr<PIX>(pixConvertTo1(graphics.get(), 250));
} else {
graphics1d = std::unique_ptr<PIX>(pixCreateTemplate(fullRender1d.get()));
}
// Remove graphical elements that did not show up in the original due
// to PDF shenanigans.
pixAnd(graphics1d.get(), graphics1d.get(), fullRender1d.get());
std::vector<Caption> captions =
buildCaptions(captionStarts.at(onPage), docStats, pages.at(onPage),
graphics1d.get(), verbose);
PageRegions regions =
getPageRegions(fullRender1d.get(), pages.at(onPage), graphics1d.get(),
captions, docStats, onPage, verbose, showSteps, errors);
std::vector<Figure> figures;
if (regions.captions.size() == 0) {
figures = std::vector<Figure>();
} else {
figures = extractFigures(fullRender1d.get(), regions, docStats, verbose,
showSteps, errors);
}
if (figures.size() == 0 and verbose) {
printf("Warning: No figures recovered");
}
if (saveMistakes) {
for (Figure &fig : errors) {
figures.push_back(fig);
}
}
if (jsonPrefix.length() != 0) {
for (Figure &fig : figures) {
allFigures.push_back(fig);
}
pageSizes[onPage] = std::pair<int, int>(fullRender->w, fullRender->h);
}
if (imagePrefix.length() != 0) {
saveFiguresImage(figures, fullRender.get(), imagePrefix);
}
std::unique_ptr<PIX> fullColorRender;
if (colorImagePrefix.length() != 0) {
fullColorRender = getFullColorRenderPix(doc.get(), onPage + 1, resolution * resMultiply);
saveFiguresFullColorImage(figures, fullColorRender.get(), colorImagePrefix, resMultiply);
}
if (showFinal or finalPrefix.length() != 0) {
std::unique_ptr<PIX> final(drawFigureRegions(fullRender.get(), figures));
if (showFinal)
pixDisplay(final.get(), 0, 0);
if (finalPrefix.length() > 0)
pixWriteImpliedFormat(
(finalPrefix + "-" + std::to_string(onPage) + ".png").c_str(),
final.get(), 0, 0);
}
errors.clear();
if (verbose)
printf("Done\n\n");
}
if (jsonPrefix.length() != 0) {
std::ofstream output((jsonPrefix + ".json").c_str());
output << "[\n";
for (size_t i = 0; i < allFigures.size(); ++i) {
Figure fig = allFigures[i];
int width, height;
if (fig.page != -1) {
width = pageSizes[fig.page].first;
height = pageSizes[fig.page].second;
} else {
width = -1;
height = -1;
}
writeFigureJSON(allFigures[i], width, height, resolution, pages, output);
if (i != allFigures.size() - 1) {
output << ",";
}
output << "\n";
}
output << "]\n";
output.close();
if (verbose) {
printf("Saved %d figures to %s\n", (int)allFigures.size(),
(jsonPrefix + ".json").c_str());
}
}
for (auto &textPage : pages) {
textPage->decRefCnt();
}
}