forked from 4lex4/scantailor-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLine.cpp
781 lines (673 loc) · 25.3 KB
/
CommandLine.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/* Scan Tailor - Interactive post-processing tool for scanned pages.
CommandLine - Interface for ScanTailor's parameters provided on CL.
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 <tiff.h>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <QDir>
#include <QMap>
#include "CommandLine.h"
#include "Dpi.h"
#include "ImageId.h"
#include "version.h"
CommandLine CommandLine::m_globalInstance;
void CommandLine::set(const CommandLine& cl) {
assert(!m_globalInstance.isGlobal());
m_globalInstance = cl;
m_globalInstance.setGlobal();
}
bool CommandLine::parseCli(const QStringList& argv) {
QRegExp rx("^--([^=]+)=(.*)$");
QRegExp rx_switch("^--([^=]+)$");
QRegExp rx_short("^-([^=]+)=(.*)$");
QRegExp rx_short_switch("^-([^=]+)$");
QRegExp rx_project(".*\\.ScanTailor$", Qt::CaseInsensitive);
QList<QString> opts;
opts << "help";
opts << "verbose";
opts << "layout";
opts << "layout-direction";
opts << "orientation";
opts << "rotate";
opts << "deskew";
opts << "skew-deviation";
opts << "disable-content-detection";
opts << "enable-page-detection";
opts << "enable-fine-tuning";
opts << "force-disable-page-detection";
opts << "content-detection";
opts << "content-box";
opts << "content-deviation";
opts << "enable-auto-margins";
opts << "margins";
opts << "margins-left";
opts << "margins-right";
opts << "margins-top";
opts << "margins-bottom";
opts << "default-margins";
opts << "default-margins-left";
opts << "default-margins-right";
opts << "default-margins-top";
opts << "default-margins-bottom";
opts << "match-layout";
opts << "match-layout-tolerance";
opts << "match-layout-default";
opts << "alignment";
opts << "alignment-vertical";
opts << "alignment-horizontal";
opts << "alignment-tolerance";
opts << "dpi";
opts << "output-dpi";
opts << "dpi-x";
opts << "dpi-y";
opts << "output-dpi-x";
opts << "output-dpi-y";
opts << "color-mode";
opts << "fill-margins";
opts << "normalize-illumination";
opts << "threshold";
opts << "despeckle";
opts << "dewarping";
opts << "depth-perception";
opts << "start-filter";
opts << "end-filter";
opts << "output-project";
opts << "picture-shape";
opts << "language";
opts << "disable-content-text-mask";
opts << "window-title";
opts << "page-detection-box";
opts << "page-detection-tolerance";
opts << "page-borders";
opts << "page-borders-left";
opts << "page-borders-top";
opts << "page-borders-right";
opts << "page-borders-bottom";
opts << "disable-check-output";
opts << "default-output-dpi";
opts << "default-color-mode";
opts << "tiff-force-rgb";
opts << "tiff-force-grayscale";
opts << "tiff-force-keep-color-space";
QMap<QString, QString> shortMap;
shortMap["h"] = "help";
shortMap["help"] = "help";
shortMap["v"] = "verbose";
shortMap["l"] = "layout";
shortMap["ld"] = "layout-direction";
shortMap["o"] = "output-project";
// skip first argument (scantailor)
for (int i = 1; i < argv.size(); i++) {
#ifdef DEBUG
std::cout << "arg[" << i << "]=" << argv[i].toLatin1().constData() << std::endl;
#endif
if (rx.exactMatch(argv[i])) {
QString key = rx.cap(1);
if (!opts.contains(key)) {
m_error = true;
std::cout << "Unknown option '" << key.toStdString() << "'" << std::endl;
continue;
}
m_options[key] = rx.cap(2);
} else if (rx_switch.exactMatch(argv[i])) {
QString key = rx_switch.cap(1);
if (!opts.contains(key)) {
m_error = true;
std::cout << "Unknown switch '" << key.toStdString() << "'" << std::endl;
continue;
}
m_options[key] = "true";
} else if (rx_short.exactMatch(argv[i])) {
QString key = shortMap[rx_short.cap(1)];
if (key == "") {
std::cout << "Unknown option: '" << rx_short.cap(1).toStdString() << "'" << std::endl;
m_error = true;
continue;
}
m_options[key] = rx_short.cap(2);
} else if (rx_short_switch.exactMatch(argv[i])) {
QString key = shortMap[rx_short_switch.cap(1)];
if (key == "") {
std::cout << "Unknown switch: '" << rx_short_switch.cap(1).toStdString() << "'" << std::endl;
m_error = true;
continue;
}
m_options[key] = "true";
} else if (rx_project.exactMatch(argv[i])) {
// project file
CommandLine::m_projectFile = argv[i];
} else {
// handle input images and output directory
QFileInfo file(argv[i]);
if (i == (argv.size() - 1)) {
// output directory
if (file.isDir()) {
CommandLine::m_outputDirectory = file.filePath();
} else {
std::cout << "Error: Last argument must be an existing directory" << std::endl;
exit(1);
}
} else if (file.filePath() == "-") {
// file names from stdin
std::string fname;
while (!std::cin.eof()) {
std::cin >> fname;
addImage(fname.c_str());
}
} else if (file.isDir()) {
// add all files from given directory as images
QDir dir(argv[i]);
QStringList files = dir.entryList(QDir::Files, QDir::Name);
for (int f = 0; f < files.count(); f++) {
addImage(dir.filePath(files[f]));
}
} else {
// argument is image
addImage(file.filePath());
}
}
}
setup();
#ifdef DEBUG
QStringList params = m_options.keys();
for (int i = 0; i < params.size(); i++) {
std::cout << params[i].toLatin1().constData() << "=" << m_options[params[i]].toLatin1().constData() << std::endl;
}
std::cout << "Images: " << CommandLine::m_images.size() << std::endl;
#endif
return m_error;
} // CommandLine::parseCli
void CommandLine::addImage(const QString& path) {
QFileInfo file(path);
m_files.push_back(file);
}
void CommandLine::setup() {
m_outputProjectFile = fetchOutputProjectFile();
m_layoutType = fetchLayoutType();
m_layoutDirection = fetchLayoutDirection();
m_colorMode = fetchColorMode();
m_defaultColorMode = fetchDefaultColorMode();
m_pictureShape = fetchPictureShape();
m_dpi = fetchDpi();
m_outputDpi = fetchDpi("output-dpi");
m_defaultOutputDpi = fetchDpi("default-output-dpi");
m_margins = fetchMargins();
m_defaultMargins = fetchMargins("default-margins");
m_pageBorders = fetchPageBorders();
m_alignment = fetchAlignment();
m_contentDetection = fetchContentDetection();
m_contentRect = fetchContentRect();
m_contentDeviation = fetchContentDeviation();
m_orientation = fetchOrientation();
m_threshold = fetchThreshold();
m_deskewAngle = fetchDeskewAngle();
m_deskewMode = fetchDeskewMode();
m_skewDeviation = fetchSkewDeviation();
m_startFilterIdx = fetchStartFilterIdx();
m_endFilterIdx = fetchEndFilterIdx();
m_matchLayoutTolerance = fetchMatchLayoutTolerance();
m_dewarpingOptions = output::DewarpingOptions(fetchDewarpingMode());
m_language = fetchLanguage();
m_windowTitle = fetchWindowTitle();
m_pageDetectionBox = fetchPageDetectionBox();
m_pageDetectionTolerance = fetchPageDetectionTolerance();
m_defaultNull = fetchDefaultNull();
QRegExp exp(".*(tif|tiff|jpg|jpeg|bmp|gif|png|pbm|pgm|ppm|xbm|xpm)$", Qt::CaseInsensitive);
for (auto& m_file : m_files) {
if (!exp.exactMatch(m_file.filePath())) {
#ifdef DEBUG
std::cout << "Skipping file: " << m_files[i].filePath().toStdString() << std::endl;
#endif
continue;
}
const ImageId image_id(m_file.filePath());
ImageMetadata metadata;
metadata.setDpi(m_dpi);
std::vector<ImageMetadata> vMetadata;
vMetadata.push_back(metadata);
ImageFileInfo image_info(m_file, vMetadata);
m_images.push_back(image_info);
}
} // CommandLine::setup
void CommandLine::printHelp() {
std::cout << std::endl;
std::cout << "Scan Tailor is a post-processing tool for scanned pages." << std::endl;
std::cout << "Version: " << VERSION << std::endl;
std::cout << std::endl;
std::cout << "ScanTailor usage: " << std::endl;
std::cout << "\t1) scantailor" << std::endl;
std::cout << "\t2) scantailor <project_file>" << std::endl;
std::cout << "\t3) scantailor-cli [options] <images|directory|-> <output_directory>" << std::endl;
std::cout << "\t4) scantailor-cli [options] <project_file> [output_directory]" << std::endl;
std::cout << std::endl;
std::cout << "1)" << std::endl;
std::cout << "\tstart ScanTailor's GUI interface" << std::endl;
std::cout << "2)" << std::endl;
std::cout << "\tstart ScanTailor's GUI interface and load project file" << std::endl;
std::cout << "3)" << std::endl;
std::cout << "\tbatch processing images from command line; no GUI" << std::endl;
std::cout << "\tfile names are collected from arguments, input directory or stdin (-)" << std::endl;
std::cout << "4)" << std::endl;
std::cout << "\tbatch processing project from command line; no GUI" << std::endl;
std::cout << "\tif output_directory is specified as last argument, it overwrites the one in project file"
<< std::endl;
std::cout << std::endl;
std::cout << "Options:" << std::endl;
std::cout << "\t--help, -h" << std::endl;
std::cout << "\t--verbose, -v" << std::endl;
std::cout << "\t--languge=<cs|de|...>\t-- default: system language" << std::endl;
std::cout << "\t--layout=, -l=<0|1|1.5|2>\t\t-- default: 0" << std::endl;
std::cout << "\t\t\t 0: auto detect" << std::endl;
std::cout << "\t\t\t 1: one page layout" << std::endl;
std::cout << "\t\t\t1.5: one page layout but cutting is needed" << std::endl;
std::cout << "\t\t\t 2: two page layout" << std::endl;
std::cout << "\t--layout-direction=, -ld=<lr|rl>\t-- default: lr" << std::endl;
std::cout << "\t--orientation=<left|right|upsidedown|none>\n\t\t\t\t\t\t-- default: none" << std::endl;
std::cout << "\t--rotate=<0.0...360.0>\t\t\t-- it also sets deskew to manual mode" << std::endl;
std::cout << "\t--deskew=<auto|manual>\t\t\t-- default: auto" << std::endl;
std::cout << "\t--skew-deviation=<0.0...)\t\t-- default: 5.0; pages with bigger skew deviation will be painted in red"
<< std::endl;
std::cout << "\t--disable-content-detection\t\t-- default: enabled" << std::endl;
std::cout << "\t--enable-page-detection\t\t\t-- default: disabled" << std::endl;
std::cout << "\t--enable-fine-tuning\t\t\t-- default: disabled; if page detection enabled it moves edges while "
"corners are in black"
<< std::endl;
std::cout << "\t--force-disable-page-detection\t\t-- switch page detection from page project off if enabled and "
"set content detection to manual mode"
<< std::endl;
std::cout << "\t--disable-content-text-mask\n\t\t\t\t\t\t-- disable using text mask to estimate a content box"
<< std::endl;
std::cout << "\t--content-detection=<cautious|normal|aggressive>\n\t\t\t\t\t\t-- default: normal" << std::endl;
std::cout << "\t--content-deviation=<0.0...)\t\t-- default: 1.0; pages with bigger content deviation will be "
"painted in red"
<< std::endl;
std::cout << "\t--content-box=<<left_offset>x<top_offset>:<width>x<height>>" << std::endl;
std::cout << "\t\t\t\t\t\t-- if set the content detection is se to manual mode" << std::endl;
std::cout << "\t\t\t\t\t\t example: --content-box=100x100:1500x2500" << std::endl;
std::cout << "\t--enable-auto-margins\t\t\t-- sets the margins to original ones (based on detected page or image "
"size)"
<< std::endl;
std::cout << "\t--margins=<number>\t\t\t-- sets left, top, right and bottom margins to same number." << std::endl;
std::cout << "\t\t--margins-left=<number>" << std::endl;
std::cout << "\t\t--margins-right=<number>" << std::endl;
std::cout << "\t\t--margins-top=<number>" << std::endl;
std::cout << "\t\t--margins-bottom=<number>" << std::endl;
std::cout << "\t--default-margins=<number>\t\t\t-- sets left, top, right and bottom margins, for new pages, to "
"same number."
<< std::endl;
std::cout << "\t\t--default-margins-left=<number>" << std::endl;
std::cout << "\t\t--default-margins-right=<number>" << std::endl;
std::cout << "\t\t--default-margins-top=<number>" << std::endl;
std::cout << "\t\t--default-margins-bottom=<number>" << std::endl;
std::cout << "\t--match-layout=<true|false>\t\t-- default: true" << std::endl;
std::cout << "\t--match-layout-tolerance=<0.0...)\t-- default: off" << std::endl;
std::cout << "\t--match-layout-default=<true|false>\t-- default: true" << std::endl;
std::cout << "\t--alignment=<center|original|auto>\t-- sets vertical to original and horizontal to center"
<< std::endl;
std::cout << "\t\t--alignment-vertical=<top|center|bottom|original>" << std::endl;
std::cout << "\t\t--alignment-horizontal=<left|center|right|original>" << std::endl;
std::cout << "\t--alignment-tolerance=<float>\t\t-- sets tolerance for auto alignment" << std::endl;
std::cout << "\t--dpi=<number>\t\t\t\t-- sets x and y dpi. default: 600" << std::endl;
std::cout << "\t\t--dpi-x=<number>" << std::endl;
std::cout << "\t\t--dpi-y=<number>" << std::endl;
std::cout << "\t--output-dpi=<number>\t\t\t-- sets x and y output dpi. default: 600" << std::endl;
std::cout << "\t\t--output-dpi-x=<number>" << std::endl;
std::cout << "\t\t--output-dpi-y=<number>" << std::endl;
std::cout << "\t--default-output-dpi=<number>\t\t-- default output dpi for pages created by split filter in gui"
<< std::endl;
std::cout << "\t--color-mode=<black_and_white|color_grayscale|mixed>\n\t\t\t\t\t\t-- default: black_and_white"
<< std::endl;
std::cout << "\t--default-color-mode=<...>\t\t-- sets default value for new images created by split filter"
<< std::endl;
std::cout << "\t--picture-shape=<free|rectangular>\n\t\t\t\t\t\t-- default: free" << std::endl;
std::cout << "\t--fill-margins\t\t\t\t-- default: false" << std::endl;
std::cout << "\t--normalize-illumination\t\t-- default: false" << std::endl;
std::cout << "\t--threshold=<n>\t\t\t\t-- n<0 thinner, n>0 thicker; default: 0" << std::endl;
std::cout << "\t--despeckle=<1.0...3.0>\n\t\t\t\t\t\t-- default: normal" << std::endl;
std::cout << "\t--dewarping=<off|auto>\t\t\t-- default: off" << std::endl;
std::cout << "\t--depth-perception=<1.0...3.0>\t\t-- default: 2.0" << std::endl;
std::cout << "\t--start-filter=<1...6>\t\t\t-- default: 4" << std::endl;
std::cout << "\t--end-filter=<1...6>\t\t\t-- default: 6" << std::endl;
std::cout << "\t--output-project=, -o=<project_name>" << std::endl;
std::cout << "\t--tiff-force-rgb\t\t\t-- all output tiffs will be rgb" << std::endl;
std::cout << "\t--tiff-force-grayscale\t\t\t-- all output tiffs will be grayscale" << std::endl;
std::cout << "\t--tiff-force-keep-color-space\t\t-- output tiffs will be in original color space" << std::endl;
std::cout << "\t--window-title=WindowTitle\t\t-- default: project name" << std::endl;
std::cout << "\t--page-detection-box=<widthxheight>\t\t-- in mm" << std::endl;
std::cout << "\t\t--page-detection-tolerance=<0.0..1.0>\t-- default: 0.1" << std::endl;
std::cout << "\t--disable-check-output\t\t\t-- don't check if page is valid when switching to step 6";
std::cout << std::endl;
} // CommandLine::printHelp
page_split::LayoutType CommandLine::fetchLayoutType() {
page_split::LayoutType lt = page_split::AUTO_LAYOUT_TYPE;
if (!hasLayout()) {
return lt;
}
if (m_options["layout"] == "1") {
lt = page_split::SINGLE_PAGE_UNCUT;
} else if (m_options["layout"] == "1.5") {
lt = page_split::PAGE_PLUS_OFFCUT;
} else if (m_options["layout"] == "2") {
lt = page_split::TWO_PAGES;
}
return lt;
}
Qt::LayoutDirection CommandLine::fetchLayoutDirection() {
Qt::LayoutDirection l = Qt::LeftToRight;
if (!hasLayoutDirection()) {
return l;
}
QString ld = m_options["layout-direction"].toLower();
if (ld == "rl") {
l = Qt::RightToLeft;
}
return l;
}
Dpi CommandLine::fetchDpi(QString oname) {
int xdpi = 600;
int ydpi = 600;
if (m_options.contains(oname + "-x")) {
xdpi = m_options[oname + "-x"].toInt();
}
if (m_options.contains(oname + "-y")) {
ydpi = m_options[oname + "-y"].toInt();
}
if (m_options.contains(oname)) {
xdpi = m_options[oname].toInt();
ydpi = m_options[oname].toInt();
}
return Dpi(xdpi, ydpi);
}
output::ColorMode CommandLine::fetchColorMode() {
if (!hasColorMode()) {
return output::BLACK_AND_WHITE;
}
QString cm = m_options["color-mode"].toLower();
if (cm == "color_grayscale") {
return output::COLOR_GRAYSCALE;
} else if (cm == "mixed") {
return output::MIXED;
}
return output::BLACK_AND_WHITE;
}
output::ColorMode CommandLine::fetchDefaultColorMode() {
if (!hasDefaultColorMode()) {
return output::BLACK_AND_WHITE;
}
QString cm = m_options["default-color-mode"].toLower();
if (cm == "color_grayscale") {
return output::COLOR_GRAYSCALE;
} else if (cm == "mixed") {
return output::MIXED;
}
return output::BLACK_AND_WHITE;
}
output::PictureShape CommandLine::fetchPictureShape() {
if (!hasPictureShape()) {
return output::FREE_SHAPE;
}
QString ps = m_options["picture-shape"].toLower();
if (ps == "rectangular") {
return output::RECTANGULAR_SHAPE;
}
return output::FREE_SHAPE;
}
Margins CommandLine::fetchMargins(QString base, Margins def) {
Margins margins(def);
if (m_options.contains(base)) {
double m = m_options[base].toDouble();
margins.setTop(m);
margins.setBottom(m);
margins.setLeft(m);
margins.setRight(m);
}
QString lstr = base + "-left", tstr = base + "-top", rstr = base + "-right", bstr = base + "-bottom";
if (m_options.contains(lstr)) {
margins.setLeft(m_options[lstr].toFloat());
}
if (m_options.contains(rstr)) {
margins.setRight(m_options[rstr].toFloat());
}
if (m_options.contains(tstr)) {
margins.setTop(m_options[tstr].toFloat());
}
if (m_options.contains(bstr)) {
margins.setBottom(m_options[bstr].toFloat());
}
return margins;
} // CommandLine::fetchMargins
page_layout::Alignment CommandLine::fetchAlignment() {
page_layout::Alignment alignment(page_layout::Alignment::TOP, page_layout::Alignment::HCENTER);
if (m_options.contains("match-layout")) {
m_defaultNull = false;
if (m_options["match-layout"] == "false") {
alignment.setNull(true);
}
if (m_options["match-layout"] == "true") {
alignment.setNull(false);
m_defaultNull = true;
}
}
if (m_options.contains("alignment")) {
if (m_options["alignment"] == "original") {
alignment.setVertical(page_layout::Alignment::VORIGINAL);
} else if (m_options["alignment"] == "auto") {
alignment.setVertical(page_layout::Alignment::VAUTO);
} else {
alignment.setVertical(page_layout::Alignment::VCENTER);
}
alignment.setHorizontal(page_layout::Alignment::HCENTER);
}
if (m_options.contains("alignment-vertical")) {
QString a = m_options["alignment-vertical"].toLower();
if (a == "top") {
alignment.setVertical(page_layout::Alignment::TOP);
}
if (a == "center") {
alignment.setVertical(page_layout::Alignment::VCENTER);
}
if (a == "bottom") {
alignment.setVertical(page_layout::Alignment::BOTTOM);
}
if (a == "original") {
alignment.setVertical(page_layout::Alignment::VORIGINAL);
}
if (a == "auto") {
alignment.setVertical(page_layout::Alignment::VAUTO);
}
}
if (m_options.contains("alignment-horizontal")) {
QString a = m_options["alignment-horizontal"].toLower();
if (a == "left") {
alignment.setHorizontal(page_layout::Alignment::LEFT);
}
if (a == "center") {
alignment.setHorizontal(page_layout::Alignment::HCENTER);
}
if (a == "right") {
alignment.setHorizontal(page_layout::Alignment::RIGHT);
}
if (a == "original") {
alignment.setHorizontal(page_layout::Alignment::HORIGINAL);
}
if (a == "auto") {
alignment.setHorizontal(page_layout::Alignment::HAUTO);
}
}
return alignment;
} // CommandLine::fetchAlignment
Despeckle::Level CommandLine::fetchContentDetection() {
Despeckle::Level level = Despeckle::NORMAL;
if (hasContentDetection()) {
QString cm = m_options["content-detection"].toLower();
if (cm == "cautious") {
level = Despeckle::CAUTIOUS;
} else if (cm == "aggressive") {
level = Despeckle::AGGRESSIVE;
}
}
return level;
}
QRectF CommandLine::fetchContentRect() {
if (!hasContentRect()) {
return QRectF();
}
QRegExp rx(R"(([\d\.]+)x([\d\.]+):([\d\.]+)x([\d\.]+))");
if (rx.exactMatch(m_options["content-box"])) {
return QRectF(rx.cap(1).toFloat(), rx.cap(2).toFloat(), rx.cap(3).toFloat(), rx.cap(4).toFloat());
}
std::cout << "invalid --content-box=" << m_options["content-box"].toLatin1().constData() << std::endl;
exit(1);
}
double CommandLine::fetchContentDeviation() {
if (!hasContentDeviation()) {
return 1.0;
}
return m_options["content-deviation"].toDouble();
}
CommandLine::Orientation CommandLine::fetchOrientation() {
if (!hasOrientation()) {
return TOP;
}
Orientation orient;
QString cli_orient = m_options["orientation"];
if (cli_orient == "left") {
orient = LEFT;
} else if (cli_orient == "right") {
orient = RIGHT;
} else if (cli_orient == "upsidedown") {
orient = UPSIDEDOWN;
} else {
std::cout << "Wrong orientation " << m_options["orientation"].toLatin1().constData() << std::endl;
exit(1);
}
return orient;
}
QString CommandLine::fetchOutputProjectFile() {
if (!hasOutputProject()) {
return QString();
}
return m_options["output-project"];
}
int CommandLine::fetchThreshold() {
if (!hasThreshold()) {
return 0;
}
return m_options["threshold"].toInt();
}
double CommandLine::fetchDeskewAngle() {
if (!hasDeskewAngle()) {
return 0.0;
}
return m_options["rotate"].toDouble();
}
AutoManualMode CommandLine::fetchDeskewMode() {
if (!hasDeskew()) {
return MODE_AUTO;
}
return (m_options["deskew"].toLower() == "manual") ? MODE_MANUAL : MODE_AUTO;
}
double CommandLine::fetchSkewDeviation() {
if (!hasSkewDeviation()) {
return 5.0;
}
return m_options["skew-deviation"].toDouble();
}
int CommandLine::fetchStartFilterIdx() {
if (!hasStartFilterIdx()) {
return 0;
}
return m_options["start-filter"].toInt() - 1;
}
int CommandLine::fetchEndFilterIdx() {
if (!hasEndFilterIdx()) {
return 5;
}
return m_options["end-filter"].toInt() - 1;
}
output::DewarpingMode CommandLine::fetchDewarpingMode() {
if (!hasDewarping()) {
return output::OFF;
}
return output::DewarpingOptions::parseDewarpingMode(m_options["dewarping"].toLower());
}
double CommandLine::fetchDespeckleLevel() {
if (!hasDespeckle()) {
return 2.0;
}
return m_options["despeckle"].toDouble();
}
output::DepthPerception CommandLine::fetchDepthPerception() {
if (!hasDepthPerception()) {
return output::DepthPerception();
}
return output::DepthPerception(m_options["depth-perception"]);
}
float CommandLine::fetchMatchLayoutTolerance() {
if (!hasMatchLayoutTolerance()) {
return 0.2f;
}
return m_options["match-layout-tolerance"].toFloat();
}
bool CommandLine::hasMargins(QString base) const {
return m_options.contains(base) || m_options.contains(base + "-left") || m_options.contains(base + "-right")
|| m_options.contains(base + "-top") || m_options.contains(base + "-bottom");
}
bool CommandLine::hasAlignment() const {
return hasMatchLayoutTolerance() || m_options.contains("alignment") || m_options.contains("alignment-vertical")
|| m_options.contains("alignment-horizontal") || isAutoMarginsEnabled();
}
bool CommandLine::hasOutputDpi() const {
return m_options.contains("output-dpi") || m_options.contains("output-dpi-x") || m_options.contains("output-dpi-y");
}
bool CommandLine::hasLanguage() const {
return m_options.contains("language");
}
QString CommandLine::fetchLanguage() const {
if (hasLanguage()) {
return m_options["language"];
}
return "untranslated";
}
QString CommandLine::fetchWindowTitle() const {
if (hasWindowTitle()) {
return m_options["window-title"];
}
return "";
}
QSizeF CommandLine::fetchPageDetectionBox() const {
if (!hasPageDetectionBox()) {
return QSizeF();
}
QRegExp rx(R"(([\d\.]+)x([\d\.]+))");
if (rx.exactMatch(m_options["page-detection-box"])) {
return QSizeF(rx.cap(1).toFloat(), rx.cap(2).toFloat());
}
std::cout << "invalid --page-detection-box=" << m_options["page-detection-box"].toLatin1().constData() << std::endl;
exit(1);
}
double CommandLine::fetchPageDetectionTolerance() const {
if (hasPageDetectionTolerance()) {
return m_options["page-detection-tolerance"].toFloat();
}
return 0.1;
}
bool CommandLine::fetchDefaultNull() {
m_defaultNull = false;
if (contains("match-layout-default") && (m_options["match-layout-default"] == "false")) {
m_defaultNull = true;
}
return m_defaultNull;
}