forked from 4lex4/scantailor-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleBatch.cpp
428 lines (368 loc) · 14.8 KB
/
ConsoleBatch.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
ConsoleBatch - Batch processing scanned pages from command line.
Copyright (C) 2011 Petr Kovar <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <iostream>
#include <vector>
#include "FileNameDisambiguator.h"
#include "LoadFileTask.h"
#include "OutputFileNameGenerator.h"
#include "PageSelectionAccessor.h"
#include "PageSequence.h"
#include "ProcessingTaskQueue.h"
#include "ProjectPages.h"
#include "ProjectReader.h"
#include "ProjectWriter.h"
#include "StageSequence.h"
#include "Utils.h"
#include "filters/deskew/CacheDrivenTask.h"
#include "filters/deskew/Task.h"
#include "filters/fix_orientation/CacheDrivenTask.h"
#include "filters/fix_orientation/Settings.h"
#include "filters/fix_orientation/Task.h"
#include "filters/output/CacheDrivenTask.h"
#include "filters/output/Settings.h"
#include "filters/output/Task.h"
#include "filters/page_layout/CacheDrivenTask.h"
#include "filters/page_layout/Task.h"
#include "filters/page_split/CacheDrivenTask.h"
#include "filters/page_split/Settings.h"
#include "filters/page_split/Task.h"
#include "filters/select_content/CacheDrivenTask.h"
#include "filters/select_content/Task.h"
#include "ConsoleBatch.h"
ConsoleBatch::ConsoleBatch(const std::vector<ImageFileInfo>& images,
const QString& output_directory,
const Qt::LayoutDirection layout)
: batch(true),
debug(true),
m_disambiguator(new FileNameDisambiguator),
m_pages(new ProjectPages(images, ProjectPages::AUTO_PAGES, layout)) {
const PageSelectionAccessor accessor(nullptr); // Won't really be used anyway.
m_stages = make_intrusive<StageSequence>(m_pages, accessor);
// m_thumbnailCache = make_intrusive<ThumbnailPixmapCache>(output_dir+"/cache/thumbs",
// QSize(200,200), 40, 5);
m_thumbnailCache = Utils::createThumbnailCache(output_directory);
m_outFileNameGen = OutputFileNameGenerator(m_disambiguator, output_directory, m_pages->layoutDirection());
}
ConsoleBatch::ConsoleBatch(const QString project_file) : batch(true), debug(true) {
QFile file(project_file);
if (!file.open(QIODevice::ReadOnly)) {
throw std::runtime_error("ConsoleBatch: Unable to open the project file.");
}
QDomDocument doc;
if (!doc.setContent(&file)) {
throw std::runtime_error("ConsoleBatch: The project file is broken.");
}
file.close();
m_reader = std::make_unique<ProjectReader>(doc);
m_pages = m_reader->pages();
const PageSelectionAccessor accessor(nullptr); // Won't be used anyway.
m_disambiguator = m_reader->namingDisambiguator();
m_stages = make_intrusive<StageSequence>(m_pages, accessor);
m_reader->readFilterSettings(m_stages->filters());
const CommandLine& cli = CommandLine::get();
QString output_directory = m_reader->outputDirectory();
if (!cli.outputDirectory().isEmpty()) {
output_directory = cli.outputDirectory();
}
// m_thumbnailCache = make_intrusive< // ThumbnailPixmapCache>(output_directory+"/cache/thumbs",
// QSize(200,200), 40, 5);
m_thumbnailCache = Utils::createThumbnailCache(output_directory);
m_outFileNameGen = OutputFileNameGenerator(m_disambiguator, output_directory, m_pages->layoutDirection());
}
BackgroundTaskPtr ConsoleBatch::createCompositeTask(const PageInfo& page, const int last_filter_idx) {
intrusive_ptr<fix_orientation::Task> fix_orientation_task;
intrusive_ptr<page_split::Task> page_split_task;
intrusive_ptr<deskew::Task> deskew_task;
intrusive_ptr<select_content::Task> select_content_task;
intrusive_ptr<page_layout::Task> page_layout_task;
intrusive_ptr<output::Task> output_task;
if (batch) {
debug = false;
}
if (last_filter_idx >= m_stages->outputFilterIdx()) {
output_task = m_stages->outputFilter()->createTask(page.id(), m_thumbnailCache, m_outFileNameGen, batch, debug);
debug = false;
}
if (last_filter_idx >= m_stages->pageLayoutFilterIdx()) {
page_layout_task = m_stages->pageLayoutFilter()->createTask(page.id(), output_task, batch, debug);
debug = false;
}
if (last_filter_idx >= m_stages->selectContentFilterIdx()) {
select_content_task = m_stages->selectContentFilter()->createTask(page.id(), page_layout_task, batch, debug);
debug = false;
}
if (last_filter_idx >= m_stages->deskewFilterIdx()) {
deskew_task = m_stages->deskewFilter()->createTask(page.id(), select_content_task, batch, debug);
debug = false;
}
if (last_filter_idx >= m_stages->pageSplitFilterIdx()) {
page_split_task = m_stages->pageSplitFilter()->createTask(page, deskew_task, batch, debug);
debug = false;
}
if (last_filter_idx >= m_stages->fixOrientationFilterIdx()) {
fix_orientation_task = m_stages->fixOrientationFilter()->createTask(page.id(), page_split_task, batch);
debug = false;
}
assert(fix_orientation_task);
return make_intrusive<LoadFileTask>(BackgroundTask::BATCH, page, m_thumbnailCache, m_pages, fix_orientation_task);
} // ConsoleBatch::createCompositeTask
// process the image vector **images** and save output to **output_dir**
void ConsoleBatch::process() {
const CommandLine& cli = CommandLine::get();
int startFilterIdx = m_stages->fixOrientationFilterIdx();
if (cli.hasStartFilterIdx()) {
unsigned int sf = cli.getStartFilterIdx();
if ((sf < 0) || (sf >= m_stages->filters().size())) {
throw std::runtime_error("ConsoleBatch: Start filter out of range");
}
startFilterIdx = sf;
}
int endFilterIdx = m_stages->outputFilterIdx();
if (cli.hasEndFilterIdx()) {
unsigned int ef = cli.getEndFilterIdx();
if ((ef < 0) || (ef >= m_stages->filters().size())) {
throw std::runtime_error("ConsoleBatch: End filter out of range");
}
endFilterIdx = ef;
}
for (int j = startFilterIdx; j <= endFilterIdx; j++) {
if (cli.isVerbose()) {
std::cout << "Filter: " << (j + 1) << "\n";
}
PageSequence page_sequence = m_pages->toPageSequence(PAGE_VIEW);
setupFilter(j, page_sequence.selectAll());
for (unsigned i = 0; i < page_sequence.numPages(); i++) {
PageInfo page = page_sequence.pageAt(i);
if (cli.isVerbose()) {
std::cout << "\tProcessing: " << page.imageId().filePath().toLatin1().constData() << "\n";
}
BackgroundTaskPtr bgTask = createCompositeTask(page, j);
(*bgTask)();
}
}
for (int j = endFilterIdx + 1; j <= m_stages->count(); j++) {
PageSequence page_sequence = m_pages->toPageSequence(PAGE_VIEW);
setupFilter(j, page_sequence.selectAll());
}
for (int j = 0; j <= endFilterIdx; j++) {
m_stages->filterAt(j)->updateStatistics();
}
} // ConsoleBatch::process
void ConsoleBatch::saveProject(const QString project_file) {
PageInfo fpage = m_pages->toPageSequence(PAGE_VIEW).pageAt(0);
SelectedPage sPage(fpage.id(), IMAGE_VIEW);
ProjectWriter writer(m_pages, sPage, m_outFileNameGen);
writer.write(project_file, m_stages->filters());
}
void ConsoleBatch::setupFilter(int idx, std::set<PageId> allPages) {
if (idx == m_stages->fixOrientationFilterIdx()) {
setupFixOrientation(allPages);
} else if (idx == m_stages->pageSplitFilterIdx()) {
setupPageSplit(allPages);
} else if (idx == m_stages->deskewFilterIdx()) {
setupDeskew(allPages);
} else if (idx == m_stages->selectContentFilterIdx()) {
setupSelectContent(allPages);
} else if (idx == m_stages->pageLayoutFilterIdx()) {
setupPageLayout(allPages);
} else if (idx == m_stages->outputFilterIdx()) {
setupOutput(allPages);
}
}
void ConsoleBatch::setupFixOrientation(std::set<PageId> allPages) {
intrusive_ptr<fix_orientation::Filter> fix_orientation = m_stages->fixOrientationFilter();
const CommandLine& cli = CommandLine::get();
for (std::set<PageId>::iterator i = allPages.begin(); i != allPages.end(); i++) {
PageId page = *i;
OrthogonalRotation rotation;
// FIX ORIENTATION FILTER
if (cli.hasOrientation()) {
switch (cli.getOrientation()) {
case CommandLine::LEFT:
rotation.prevClockwiseDirection();
break;
case CommandLine::RIGHT:
rotation.nextClockwiseDirection();
break;
case CommandLine::UPSIDEDOWN:
rotation.nextClockwiseDirection();
rotation.nextClockwiseDirection();
break;
default:
break;
}
fix_orientation->getSettings()->applyRotation(page.imageId(), rotation);
}
}
}
void ConsoleBatch::setupPageSplit(std::set<PageId> allPages) {
intrusive_ptr<page_split::Filter> page_split = m_stages->pageSplitFilter();
const CommandLine& cli = CommandLine::get();
// PAGE SPLIT
if (cli.hasLayout()) {
page_split->getSettings()->setLayoutTypeForAllPages(cli.getLayout());
}
}
void ConsoleBatch::setupDeskew(std::set<PageId> allPages) {
intrusive_ptr<deskew::Filter> deskew = m_stages->deskewFilter();
const CommandLine& cli = CommandLine::get();
for (std::set<PageId>::iterator i = allPages.begin(); i != allPages.end(); i++) {
PageId page = *i;
// DESKEW FILTER
OrthogonalRotation rotation;
if (cli.hasDeskewAngle() || cli.hasDeskew()) {
double angle = 0.0;
if (cli.hasDeskewAngle()) {
angle = cli.getDeskewAngle();
}
deskew::Dependencies deps(QPolygonF(), rotation);
deskew::Params params(angle, deps, MODE_MANUAL);
deskew->getSettings()->setPageParams(page, params);
}
}
if (cli.hasSkewDeviation()) {
deskew->getSettings()->setMaxDeviation(cli.getSkewDeviation());
}
}
void ConsoleBatch::setupSelectContent(std::set<PageId> allPages) {
intrusive_ptr<select_content::Filter> select_content = m_stages->selectContentFilter();
const CommandLine& cli = CommandLine::get();
for (std::set<PageId>::iterator i = allPages.begin(); i != allPages.end(); i++) {
PageId page = *i;
select_content::Dependencies deps;
select_content::Params params(deps);
std::unique_ptr<select_content::Params> old_params = select_content->getSettings()->getPageParams(page);
if (old_params) {
params = *old_params;
}
// SELECT CONTENT FILTER
if (cli.hasContentRect()) {
params.setContentRect(cli.getContentRect());
}
params.setContentDetect(cli.isContentDetectionEnabled());
params.setPageDetect(cli.isPageDetectionEnabled());
params.setFineTuneCorners(cli.isFineTuningEnabled());
if (cli.hasPageBorders()) {
params.setPageBorders(cli.getPageBorders());
}
select_content->getSettings()->setPageParams(page, params);
}
if (cli.hasContentDeviation()) {
select_content->getSettings()->setMaxDeviation(cli.getContentDeviation());
}
if (cli.hasPageDetectionBox()) {
select_content->getSettings()->setPageDetectionBox(cli.getPageDetectionBox());
}
if (cli.hasPageDetectionTolerance()) {
select_content->getSettings()->setPageDetectionTolerance(cli.getPageDetectionTolerance());
}
} // ConsoleBatch::setupSelectContent
void ConsoleBatch::setupPageLayout(std::set<PageId> allPages) {
intrusive_ptr<page_layout::Filter> page_layout = m_stages->pageLayoutFilter();
const CommandLine& cli = CommandLine::get();
QMap<QString, float> img_cache;
for (std::set<PageId>::iterator i = allPages.begin(); i != allPages.end(); i++) {
PageId page = *i;
// PAGE LAYOUT FILTER
page_layout::Alignment alignment = cli.getAlignment();
if (cli.hasMatchLayoutTolerance()) {
const QString path = page.imageId().filePath();
if (!img_cache.contains(path)) {
QImage img(path);
img_cache[path] = float(img.width()) / float(img.height());
}
float imgAspectRatio = img_cache[path];
float tolerance = cli.getMatchLayoutTolerance();
std::vector<float> diffs;
for (std::set<PageId>::iterator pi = allPages.begin(); pi != allPages.end(); pi++) {
ImageId pimageId = pi->imageId();
QString ppath = pimageId.filePath();
if (!img_cache.contains(ppath)) {
QImage img(ppath);
img_cache[ppath] = float(img.width()) / float(img.height());
}
float pimgAspectRatio = img_cache[ppath];
float diff = imgAspectRatio - pimgAspectRatio;
if (diff < 0.0) {
diff *= -1;
}
diffs.push_back(diff);
}
unsigned bad_diffs = 0;
for (unsigned j = 0; j < diffs.size(); j++) {
if (diffs[j] > tolerance) {
bad_diffs += 1;
}
}
if (bad_diffs > (diffs.size() / 2)) {
alignment.setNull(true);
}
}
if (cli.hasMargins()) {
page_layout->getSettings()->setHardMarginsMM(page, cli.getMargins());
}
if (cli.hasAlignment()) {
page_layout->getSettings()->setPageAlignment(page, alignment);
}
}
} // ConsoleBatch::setupPageLayout
void ConsoleBatch::setupOutput(std::set<PageId> allPages) {
intrusive_ptr<output::Filter> output = m_stages->outputFilter();
const CommandLine& cli = CommandLine::get();
for (std::set<PageId>::iterator i = allPages.begin(); i != allPages.end(); i++) {
PageId page = *i;
// OUTPUT FILTER
output::Params params(output->getSettings()->getParams(page));
if (cli.hasOutputDpi()) {
Dpi outputDpi = cli.getOutputDpi();
params.setOutputDpi(outputDpi);
}
if (cli.hasPictureShape()) {
params.setPictureShape(cli.getPictureShape());
}
output::ColorParams colorParams = params.colorParams();
if (cli.hasColorMode()) {
colorParams.setColorMode(cli.getColorMode());
}
if (cli.hasFillMargins() || cli.hasNormalizeIllumination()) {
output::ColorCommonOptions cgo;
if (cli.hasFillMargins()) {
cgo.setFillMargins(true);
}
if (cli.hasNormalizeIllumination()) {
cgo.setNormalizeIllumination(true);
}
colorParams.setColorCommonOptions(cgo);
}
if (cli.hasThreshold()) {
output::BlackWhiteOptions bwo;
bwo.setThresholdAdjustment(cli.getThreshold());
colorParams.setBlackWhiteOptions(bwo);
}
params.setColorParams(colorParams);
if (cli.hasDespeckle()) {
params.setDespeckleLevel(cli.getDespeckleLevel());
}
if (cli.hasDewarping()) {
params.setDewarpingMode(cli.getDewarpingMode());
}
if (cli.hasDepthPerception()) {
params.setDepthPerception(cli.getDepthPerception());
}
output->getSettings()->setParams(page, params);
}
} // ConsoleBatch::setupOutput