forked from unpaper/unpaper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpaper.c
1924 lines (1710 loc) · 73.6 KB
/
unpaper.c
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright © 2005-2007 Jens Gulden
// Copyright © 2011-2011 Diego Elio Pettenò
// SPDX-FileCopyrightText: 2005 The unpaper authors
//
// SPDX-License-Identifier: GPL-2.0-only
/* --- The main program -------------------------------------------------- */
#include <assert.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <libavutil/avutil.h>
#include "imageprocess/blit.h"
#include "imageprocess/deskew.h"
#include "imageprocess/filters.h"
#include "imageprocess/image.h"
#include "imageprocess/interpolate.h"
#include "imageprocess/masks.h"
#include "imageprocess/pixel.h"
#include "lib/options.h"
#include "lib/physical.h"
#include "parse.h"
#include "unpaper.h"
#include "version.h"
#define WELCOME \
"unpaper " VERSION_STR "\n" \
"License GPLv2: GNU GPL version 2.\n" \
"This is free software: you are free to change and redistribute it.\n" \
"There is NO WARRANTY, to the extent permitted by law.\n"
#define USAGE \
WELCOME "\n" \
"Usage: unpaper [options] <input-file(s)> <output-file(s)>\n" \
"\n" \
"Filenames may contain a formatting placeholder starting with '%%' " \
"to insert a\n" \
"page counter for multi-page processing. E.g.: 'scan%%03d.pbm' to " \
"process files\n" \
"scan001.pbm, scan002.pbm, scan003.pbm etc.\n" \
"\n" \
"See 'man unpaper' for options details\n" \
"Report bugs at https://github.com/unpaper/unpaper/issues\n"
/* --- global variable ---------------------------------------------------- */
// We use these for the "val" field in struct option, for getopt_long_only().
// These are for the options that do not have single characters as short
// options.
//
// The values start at 0x7e because this is above all the values for the
// short-option characters (e.g. 0x7e is '~', but there is no '~" short option,
// so we start with that).
enum LONG_OPTION_VALUES {
OPT_START_SHEET = 0x7e,
OPT_END_SHEET,
OPT_START_INPUT,
OPT_START_OUTPUT,
OPT_SHEET_BACKGROUND,
OPT_PRE_ROTATE,
OPT_POST_ROTATE,
OPT_POST_MIRROR,
OPT_PRE_SHIFT,
OPT_POST_SHIFT,
OPT_PRE_MASK,
OPT_POST_SIZE,
OPT_STRETCH,
OPT_POST_STRETCH,
OPT_POST_ZOOM,
OPT_PRE_WIPE,
OPT_POST_WIPE,
OPT_MIDDLE_WIPE,
OPT_PRE_BORDER,
OPT_POST_BORDER,
OPT_NO_BLACK_FILTER,
OPT_BLACK_FILTER_SCAN_DIRECTION,
OPT_BLACK_FILTER_SCAN_SIZE,
OPT_BLACK_FILTER_SCAN_DEPTH,
OPT_BLACK_FILTER_SCAN_STEP,
OPT_BLACK_FILTER_SCAN_THRESHOLD,
OPT_BLACK_FILTER_SCAN_EXCLUDE,
OPT_BLACK_FILTER_INTENSITY,
OPT_NO_NOISE_FILTER,
OPT_NOISE_FILTER_INTENSITY,
OPT_NO_BLUR_FILTER,
OPT_BLUR_FILTER_SIZE,
OPT_BLUR_FILTER_STEP,
OPT_BLUR_FILTER_INTENSITY,
OPT_NO_GRAY_FILTER,
OPT_GRAY_FILTER_SIZE,
OPT_GRAY_FILTER_STEP,
OPT_GRAY_FILTER_THRESHOLD,
OPT_NO_MASK_SCAN,
OPT_MASK_SCAN_DIRECTION,
OPT_MASK_SCAN_SIZE,
OPT_MASK_SCAN_DEPTH,
OPT_MASK_SCAN_STEP,
OPT_MASK_SCAN_THRESHOLD,
OPT_MASK_SCAN_MINIMUM,
OPT_MASK_SCAN_MAXIMUM,
OPT_MASK_COLOR,
OPT_NO_MASK_CENTER,
OPT_NO_DESKEW,
OPT_DESKEW_SCAN_DIRECTION,
OPT_DESKEW_SCAN_SIZE,
OPT_DESKEW_SCAN_DEPTH,
OPT_DESKEW_SCAN_RANGE,
OPT_DESKEW_SCAN_STEP,
OPT_DESKEW_SCAN_DEVIATION,
OPT_NO_BORDER_SCAN,
OPT_BORDER_SCAN_DIRECTION,
OPT_BORDER_SCAN_SIZE,
OPT_BORDER_SCAN_STEP,
OPT_BORDER_SCAN_THRESHOLD,
OPT_BORDER_ALIGN,
OPT_BORDER_MARGIN,
OPT_NO_BORDER_ALIGN,
OPT_NO_WIPE,
OPT_NO_BORDER,
OPT_INPUT_PAGES,
OPT_OUTPUT_PAGES,
OPT_INPUT_FILE_SEQUENCE,
OPT_OUTPUT_FILE_SEQUENCE,
OPT_INSERT_BLANK,
OPT_REPLACE_BLANK,
OPT_NO_MULTI_PAGES,
OPT_PPI,
OPT_OVERWRITE,
OPT_VERBOSE_MORE,
OPT_DEBUG,
OPT_DEBUG_SAVE,
OPT_INTERPOLATE,
};
/****************************************************************************
* MAIN() *
****************************************************************************/
/**
* The main program.
*/
int main(int argc, char *argv[]) {
// --- local variables ---
Options options;
// The variables in the following block need to stay allocated becuse the
size_t pointCount = 0;
Point points[MAX_POINTS];
size_t maskCount = 0;
Rectangle masks[MAX_MASKS];
size_t preMaskCount = 0;
Rectangle preMasks[MAX_MASKS];
int32_t middleWipe[2] = {0, 0};
Rectangle outsideBorderscanMask[MAX_PAGES]; // set by --layout
size_t outsideBorderscanMaskCount = 0;
Rectangle blackfilterExclude[MAX_MASKS]; // Required to stay allocated!
// -------------------------------------------------------------------
// --- parse parameters ---
// -------------------------------------------------------------------
{
// The following variables are no longer visible after argument parsing
// is complete.
float whiteThreshold = 0.9;
float blackThreshold = 0.33;
Edges deskewScanEdges = {
.left = true, .top = false, .right = true, .bottom = false};
int deskewScanSize = 1500;
float deskewScanDepth = 0.5;
float deskewScanRange = 5.0;
float deskewScanStep = 0.1;
float deskewScanDeviation = 1.0;
Direction maskScanDirections = DIRECTION_HORIZONTAL;
RectangleSize maskScanSize = {50, 50};
int32_t maskScanDepth[DIRECTIONS_COUNT] = {-1, -1};
Delta maskScanStep = {5, 5};
float maskScanThreshold[DIRECTIONS_COUNT] = {0.1, 0.1};
int maskScanMinimum[DIMENSIONS_COUNT] = {100, 100};
int maskScanMaximum[DIMENSIONS_COUNT] = {-1, -1}; // set default later
Direction borderScanDirections = DIRECTION_VERTICAL;
RectangleSize borderScanSize = {5, 5};
Delta borderScanStep = {5, 5};
int32_t borderScanThreshold[DIRECTIONS_COUNT] = {5, 5};
Edges borderAlign = {
.left = false, .top = false, .right = false, .bottom = false}; // center
MilsDelta borderAlignMarginPhysical = {0, 0, false}; // center
int16_t ppi = 300;
MilsSize sheetSizePhysical = {-1, -1, false};
MilsDelta preShiftPhysical = {0, 0, false};
MilsDelta postShiftPhysical = {0, 0, false};
MilsSize sizePhysical = {-1, -1, false};
MilsSize postSizePhysical = {-1, -1, false};
MilsSize stretchSizePhysical = {-1, -1, false};
MilsSize postStretchSizePhysical = {-1, -1, false};
Direction blackfilterScanDirections = DIRECTION_BOTH;
RectangleSize blackfilterScanSize = {20, 20};
int32_t blackfilterScanDepth[DIRECTIONS_COUNT] = {500, 500};
Delta blackfilterScanStep = {5, 5};
float blackfilterScanThreshold = 0.95;
size_t blackfilterExcludeCount = 0;
int blackfilterIntensity = 20;
RectangleSize blurfilterScanSize = {100, 100};
Delta blurfilterScanStep = {50, 50};
float blurfilterIntensity = 0.01;
RectangleSize grayfilterScanSize = {50, 50};
Delta grayfilterScanStep = {20, 20};
float grayfilterThreshold = 0.5;
options_init(&options);
int option_index = 0;
while (true) {
int c;
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"?", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{"layout", required_argument, NULL, 'l'},
{"#", required_argument, NULL, '#'},
{"sheet", required_argument, NULL, '#'},
{"start", required_argument, NULL, OPT_START_SHEET},
{"start-sheet", required_argument, NULL, OPT_START_SHEET},
{"end", required_argument, NULL, OPT_END_SHEET},
{"end-sheet", required_argument, NULL, OPT_END_SHEET},
{"start-input", required_argument, NULL, OPT_START_INPUT},
{"si", required_argument, NULL, OPT_START_INPUT},
{"start-output", required_argument, NULL, OPT_START_OUTPUT},
{"so", required_argument, NULL, OPT_START_OUTPUT},
{"sheet-size", required_argument, NULL, 'S'},
{"sheet-background", required_argument, NULL, OPT_SHEET_BACKGROUND},
{"exclude", optional_argument, NULL, 'x'},
{"no-processing", required_argument, NULL, 'n'},
{"pre-rotate", required_argument, NULL, OPT_PRE_ROTATE},
{"post-rotate", required_argument, NULL, OPT_POST_ROTATE},
{"pre-mirror", required_argument, NULL, 'M'},
{"post-mirror", required_argument, NULL, OPT_POST_MIRROR},
{"pre-shift", required_argument, NULL, OPT_PRE_SHIFT},
{"post-shift", required_argument, NULL, OPT_POST_SHIFT},
{"pre-mask", required_argument, NULL, OPT_PRE_MASK},
{"size", required_argument, NULL, 's'},
{"post-size", required_argument, NULL, OPT_POST_SIZE},
{"stretch", required_argument, NULL, OPT_STRETCH},
{"post-stretch", required_argument, NULL, OPT_POST_STRETCH},
{"zoom", required_argument, NULL, 'z'},
{"post-zoom", required_argument, NULL, OPT_POST_ZOOM},
{"mask-scan-point", required_argument, NULL, 'p'},
{"mask", required_argument, NULL, 'm'},
{"wipe", required_argument, NULL, 'W'},
{"pre-wipe", required_argument, NULL, OPT_PRE_WIPE},
{"post-wipe", required_argument, NULL, OPT_POST_WIPE},
{"middle-wipe", required_argument, NULL, OPT_MIDDLE_WIPE},
{"mw", required_argument, NULL, OPT_MIDDLE_WIPE},
{"border", required_argument, NULL, 'B'},
{"pre-border", required_argument, NULL, OPT_PRE_BORDER},
{"post-border", required_argument, NULL, OPT_POST_BORDER},
{"no-blackfilter", optional_argument, NULL, OPT_NO_BLACK_FILTER},
{"blackfilter-scan-direction", required_argument, NULL,
OPT_BLACK_FILTER_SCAN_DIRECTION},
{"bn", required_argument, NULL, OPT_BLACK_FILTER_SCAN_DIRECTION},
{"blackfilter-scan-size", required_argument, NULL,
OPT_BLACK_FILTER_SCAN_SIZE},
{"bs", required_argument, NULL, OPT_BLACK_FILTER_SCAN_SIZE},
{"blackfilter-scan-depth", required_argument, NULL,
OPT_BLACK_FILTER_SCAN_DEPTH},
{"bd", required_argument, NULL, OPT_BLACK_FILTER_SCAN_DEPTH},
{"blackfilter-scan-step", required_argument, NULL,
OPT_BLACK_FILTER_SCAN_STEP},
{"bp", required_argument, NULL, OPT_BLACK_FILTER_SCAN_STEP},
{"blackfilter-scan-threshold", required_argument, NULL,
OPT_BLACK_FILTER_SCAN_THRESHOLD},
{"bt", required_argument, NULL, OPT_BLACK_FILTER_SCAN_THRESHOLD},
{"blackfilter-scan-exclude", required_argument, NULL,
OPT_BLACK_FILTER_SCAN_EXCLUDE},
{"bx", required_argument, NULL, OPT_BLACK_FILTER_SCAN_EXCLUDE},
{"blackfilter-intensity", required_argument, NULL,
OPT_BLACK_FILTER_INTENSITY},
{"bi", required_argument, NULL, OPT_BLACK_FILTER_INTENSITY},
{"no-noisefilter", optional_argument, NULL, OPT_NO_NOISE_FILTER},
{"noisefilter-intensity", required_argument, NULL,
OPT_NOISE_FILTER_INTENSITY},
{"ni", required_argument, NULL, OPT_NOISE_FILTER_INTENSITY},
{"no-blurfilter", optional_argument, NULL, OPT_NO_BLUR_FILTER},
{"blurfilter-size", required_argument, NULL, OPT_BLUR_FILTER_SIZE},
{"ls", required_argument, NULL, OPT_BLUR_FILTER_SIZE},
{"blurfilter-step", required_argument, NULL, OPT_BLUR_FILTER_STEP},
{"lp", required_argument, NULL, OPT_BLUR_FILTER_STEP},
{"blurfilter-intensity", required_argument, NULL,
OPT_BLUR_FILTER_INTENSITY},
{"li", required_argument, NULL, OPT_BLUR_FILTER_INTENSITY},
{"no-grayfilter", optional_argument, NULL, OPT_NO_GRAY_FILTER},
{"grayfilter-size", required_argument, NULL, OPT_GRAY_FILTER_SIZE},
{"gs", required_argument, NULL, OPT_GRAY_FILTER_SIZE},
{"grayfilter-step", required_argument, NULL, OPT_GRAY_FILTER_STEP},
{"gp", required_argument, NULL, OPT_GRAY_FILTER_STEP},
{"grayfilter-threshold", required_argument, NULL,
OPT_GRAY_FILTER_THRESHOLD},
{"gt", required_argument, NULL, OPT_GRAY_FILTER_THRESHOLD},
{"no-mask-scan", optional_argument, NULL, OPT_NO_MASK_SCAN},
{"mask-scan-direction", required_argument, NULL,
OPT_MASK_SCAN_DIRECTION},
{"mn", required_argument, NULL, OPT_MASK_SCAN_DIRECTION},
{"mask-scan-size", required_argument, NULL, OPT_MASK_SCAN_SIZE},
{"ms", required_argument, NULL, OPT_MASK_SCAN_SIZE},
{"mask-scan-depth", required_argument, NULL, OPT_MASK_SCAN_DEPTH},
{"md", required_argument, NULL, OPT_MASK_SCAN_DEPTH},
{"mask-scan-step", required_argument, NULL, OPT_MASK_SCAN_STEP},
{"mp", required_argument, NULL, OPT_MASK_SCAN_STEP},
{"mask-scan-threshold", required_argument, NULL,
OPT_MASK_SCAN_THRESHOLD},
{"mt", required_argument, NULL, OPT_MASK_SCAN_THRESHOLD},
{"mask-scan-minimum", required_argument, NULL, OPT_MASK_SCAN_MINIMUM},
{"mm", required_argument, NULL, OPT_MASK_SCAN_MINIMUM},
{"mask-scan-maximum", required_argument, NULL, OPT_MASK_SCAN_MAXIMUM},
{"mM", required_argument, NULL, OPT_MASK_SCAN_MAXIMUM},
{"mask-color", required_argument, NULL, OPT_MASK_COLOR},
{"mc", required_argument, NULL, OPT_MASK_COLOR},
{"no-mask-center", optional_argument, NULL, OPT_NO_MASK_CENTER},
{"no-deskew", optional_argument, NULL, OPT_NO_DESKEW},
{"deskew-scan-direction", required_argument, NULL,
OPT_DESKEW_SCAN_DIRECTION},
{"dn", required_argument, NULL, OPT_DESKEW_SCAN_DIRECTION},
{"deskew-scan-size", required_argument, NULL, OPT_DESKEW_SCAN_SIZE},
{"ds", required_argument, NULL, OPT_DESKEW_SCAN_SIZE},
{"deskew-scan-depth", required_argument, NULL, OPT_DESKEW_SCAN_DEPTH},
{"dd", required_argument, NULL, OPT_DESKEW_SCAN_DEPTH},
{"deskew-scan-range", required_argument, NULL, OPT_DESKEW_SCAN_RANGE},
{"dr", required_argument, NULL, OPT_DESKEW_SCAN_RANGE},
{"deskew-scan-step", required_argument, NULL, OPT_DESKEW_SCAN_STEP},
{"dp", required_argument, NULL, OPT_DESKEW_SCAN_STEP},
{"deskew-scan-deviation", required_argument, NULL,
OPT_DESKEW_SCAN_DEVIATION},
{"dv", required_argument, NULL, OPT_DESKEW_SCAN_DEVIATION},
{"no-border-scan", optional_argument, NULL, OPT_NO_BORDER_SCAN},
{"border-scan-direction", required_argument, NULL,
OPT_BORDER_SCAN_DIRECTION},
{"Bn", required_argument, NULL, OPT_BORDER_SCAN_DIRECTION},
{"border-scan-size", required_argument, NULL, OPT_BORDER_SCAN_SIZE},
{"Bs", required_argument, NULL, OPT_BORDER_SCAN_SIZE},
{"border-scan-step", required_argument, NULL, OPT_BORDER_SCAN_STEP},
{"Bp", required_argument, NULL, OPT_BORDER_SCAN_STEP},
{"border-scan-threshold", required_argument, NULL,
OPT_BORDER_SCAN_THRESHOLD},
{"Bt", required_argument, NULL, OPT_BORDER_SCAN_THRESHOLD},
{"border-align", required_argument, NULL, OPT_BORDER_ALIGN},
{"Ba", required_argument, NULL, OPT_BORDER_ALIGN},
{"border-margin", required_argument, NULL, OPT_BORDER_MARGIN},
{"Bm", required_argument, NULL, OPT_BORDER_MARGIN},
{"no-border-align", optional_argument, NULL, OPT_NO_BORDER_ALIGN},
{"no-wipe", optional_argument, NULL, OPT_NO_WIPE},
{"no-border", optional_argument, NULL, OPT_NO_BORDER},
{"white-threshold", required_argument, NULL, 'w'},
{"black-threshold", required_argument, NULL, 'b'},
{"input-pages", required_argument, NULL, OPT_INPUT_PAGES},
{"ip", required_argument, NULL, OPT_INPUT_PAGES},
{"output-pages", required_argument, NULL, OPT_OUTPUT_PAGES},
{"op", required_argument, NULL, OPT_OUTPUT_PAGES},
{"input-file-sequence", required_argument, NULL,
OPT_INPUT_FILE_SEQUENCE},
{"if", required_argument, NULL, OPT_INPUT_FILE_SEQUENCE},
{"output-file-sequence", required_argument, NULL,
OPT_OUTPUT_FILE_SEQUENCE},
{"of", required_argument, NULL, OPT_OUTPUT_FILE_SEQUENCE},
{"insert-blank", required_argument, NULL, OPT_INSERT_BLANK},
{"replace-blank", required_argument, NULL, OPT_REPLACE_BLANK},
{"test-only", no_argument, NULL, 'T'},
{"no-multi-pages", no_argument, NULL, OPT_NO_MULTI_PAGES},
{"dpi", required_argument, NULL, OPT_PPI},
{"ppi", required_argument, NULL, OPT_PPI},
{"type", required_argument, NULL, 't'},
{"quiet", no_argument, NULL, 'q'},
{"overwrite", no_argument, NULL, OPT_OVERWRITE},
{"verbose", no_argument, NULL, 'v'},
{"vv", no_argument, NULL, OPT_VERBOSE_MORE},
{"debug", no_argument, NULL, OPT_DEBUG},
{"vvv", no_argument, NULL, OPT_DEBUG},
{"debug-save", no_argument, NULL, OPT_DEBUG_SAVE},
{"vvvv", no_argument, NULL, OPT_DEBUG_SAVE},
{"interpolate", required_argument, NULL, OPT_INTERPOLATE},
{NULL, no_argument, NULL, 0}};
c = getopt_long_only(argc, argv, "hVl:S:x::n::M:s:z:p:m:W:B:w:b:Tt:qv",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'h':
case '?':
puts(USAGE);
return c == '?' ? 1 : 0;
case 'V':
puts(VERSION_STR);
return 0;
case 'l':
if (!parse_layout(optarg, &options.layout)) {
errOutput("unable to parse layout: '%s'", optarg);
}
break;
case '#':
parseMultiIndex(optarg, &options.sheet_multi_index);
// allow 0 as start sheet, might be overwritten by --start-sheet again
if (options.sheet_multi_index.count > 0 &&
options.start_sheet > options.sheet_multi_index.indexes[0])
options.start_sheet = options.sheet_multi_index.indexes[0];
break;
case OPT_START_SHEET:
sscanf(optarg, "%d", &options.start_sheet);
break;
case OPT_END_SHEET:
sscanf(optarg, "%d", &options.end_sheet);
break;
case OPT_START_INPUT:
sscanf(optarg, "%d", &options.start_input);
break;
case OPT_START_OUTPUT:
sscanf(optarg, "%d", &options.start_output);
break;
case 'S':
parse_physical_size(optarg, &sheetSizePhysical);
break;
case OPT_SHEET_BACKGROUND:
if (!parse_color(optarg, &options.sheet_background)) {
errOutput("invalid value for sheet-background: '%s'", optarg);
}
break;
case 'x':
parseMultiIndex(optarg, &options.exclude_multi_index);
if (options.exclude_multi_index.count == -1)
options.exclude_multi_index.count = 0; // 'exclude all' makes no sense
break;
case 'n':
parseMultiIndex(optarg, &options.ignore_multi_index);
break;
case OPT_PRE_ROTATE:
sscanf(optarg, "%hd", &options.pre_rotate);
if ((options.pre_rotate != 0) && (abs(options.pre_rotate) != 90)) {
fprintf(stderr, "cannot set --pre-rotate value other than -90 or 90, "
"ignoring.\n");
options.pre_rotate = 0;
}
break;
case OPT_POST_ROTATE:
sscanf(optarg, "%hd", &options.post_rotate);
if ((options.post_rotate != 0) && (abs(options.post_rotate) != 90)) {
fprintf(stderr, "cannot set --post-rotate value other than -90 or "
"90, ignoring.\n");
options.post_rotate = 0;
}
break;
case 'M':
if (!parse_direction(optarg, &options.pre_mirror)) {
errOutput("unable to parse pre-mirror directions: '%s'", optarg);
};
break;
case OPT_POST_MIRROR:
if (!parse_direction(optarg, &options.post_mirror)) {
errOutput("unable to parse post-mirror directions: '%s'", optarg);
}
break;
case OPT_PRE_SHIFT:
parse_physical_delta(optarg, &preShiftPhysical);
break;
case OPT_POST_SHIFT:
parse_physical_delta(optarg, &postShiftPhysical);
break;
case OPT_PRE_MASK:
if (preMaskCount < MAX_MASKS) {
if (parse_rectangle(optarg, &preMasks[preMaskCount])) {
preMaskCount++;
}
} else {
fprintf(stderr,
"maximum number of masks (%d) exceeded, ignoring mask %s\n",
MAX_MASKS, optarg);
}
break;
case 's':
parse_physical_size(optarg, &sizePhysical);
break;
case OPT_POST_SIZE:
parse_physical_size(optarg, &postSizePhysical);
break;
case OPT_STRETCH:
parse_physical_size(optarg, &stretchSizePhysical);
break;
case OPT_POST_STRETCH:
parse_physical_size(optarg, &postStretchSizePhysical);
break;
case 'z':
sscanf(optarg, "%f", &options.pre_zoom_factor);
break;
case OPT_POST_ZOOM:
sscanf(optarg, "%f", &options.post_zoom_factor);
break;
case 'p':
if (pointCount < MAX_POINTS) {
int x = -1;
int y = -1;
sscanf(optarg, "%d,%d", &x, &y);
points[pointCount++] = (Point){x, y};
} else {
fprintf(stderr,
"maximum number of scan points (%d) exceeded, ignoring scan "
"point %s\n",
MAX_POINTS, optarg);
}
break;
case 'm':
if (maskCount < MAX_MASKS) {
if (parse_rectangle(optarg, &masks[maskCount])) {
maskCount++;
}
} else {
fprintf(stderr,
"maximum number of masks (%d) exceeded, ignoring mask %s\n",
MAX_MASKS, optarg);
}
break;
case 'W':
parse_wipe("wipe", optarg, &options.wipes);
break;
case OPT_PRE_WIPE:
parse_wipe("pre-wipe", optarg, &options.pre_wipes);
break;
case OPT_POST_WIPE:
parse_wipe("post-wipe", optarg, &options.post_wipes);
break;
case OPT_MIDDLE_WIPE:
if (!parse_symmetric_integers(optarg, &middleWipe[0], &middleWipe[1])) {
errOutput("unable to parse middle-wipe: '%s'", optarg);
}
break;
case 'B':
if (!parse_border(optarg, &options.border)) {
errOutput("unable to parse border: '%s'", optarg);
}
break;
case OPT_PRE_BORDER:
if (!parse_border(optarg, &options.pre_border)) {
errOutput("unable to parse pre-border: '%s'", optarg);
}
break;
case OPT_POST_BORDER:
if (!parse_border(optarg, &options.post_border)) {
errOutput("unable to parse post-border: '%s'", optarg);
}
break;
case OPT_NO_BLACK_FILTER:
parseMultiIndex(optarg, &options.no_blackfilter_multi_index);
break;
case OPT_BLACK_FILTER_SCAN_DIRECTION:
if (!parse_direction(optarg, &blackfilterScanDirections)) {
errOutput("unable to parse blackfilter-scan-direction: '%s'", optarg);
}
break;
case OPT_BLACK_FILTER_SCAN_SIZE:
if (!parse_rectangle_size(optarg, &blackfilterScanSize)) {
errOutput("unable to parse blackfilter-scan-size: '%s'", optarg);
}
break;
case OPT_BLACK_FILTER_SCAN_DEPTH:
if (!parse_symmetric_integers(optarg, &blackfilterScanDepth[0],
&blackfilterScanDepth[1]) ||
blackfilterScanDepth[0] <= 0 || blackfilterScanDepth[1] <= 0) {
errOutput("unable to parse blackfilter-scan-depth: '%s'", optarg);
}
break;
case OPT_BLACK_FILTER_SCAN_STEP:
if (!parse_scan_step(optarg, &blackfilterScanStep)) {
errOutput("unable to parse blackfilter-scan-step: '%s'", optarg);
}
break;
case OPT_BLACK_FILTER_SCAN_THRESHOLD:
sscanf(optarg, "%f", &blackfilterScanThreshold);
break;
case OPT_BLACK_FILTER_SCAN_EXCLUDE:
if (blackfilterExcludeCount < MAX_MASKS) {
if (parse_rectangle(optarg,
&blackfilterExclude[blackfilterExcludeCount])) {
blackfilterExcludeCount++;
}
} else {
fprintf(stderr,
"maximum number of blackfilter exclusion (%d) exceeded, "
"ignoring mask %s\n",
MAX_MASKS, optarg);
}
break;
case OPT_BLACK_FILTER_INTENSITY:
sscanf(optarg, "%d", &blackfilterIntensity);
break;
case OPT_NO_NOISE_FILTER:
parseMultiIndex(optarg, &options.no_noisefilter_multi_index);
break;
case OPT_NOISE_FILTER_INTENSITY:
sscanf(optarg, "%" SCNu64, &options.noisefilter_intensity);
break;
case OPT_NO_BLUR_FILTER:
parseMultiIndex(optarg, &options.no_blurfilter_multi_index);
break;
case OPT_BLUR_FILTER_SIZE:
if (!parse_rectangle_size(optarg, &blurfilterScanSize)) {
errOutput("unable to parse blurfilter-scan-size: '%s'", optarg);
}
break;
case OPT_BLUR_FILTER_STEP:
if (!parse_scan_step(optarg, &blurfilterScanStep)) {
errOutput("unable to parse blurfilter-scan-step: '%s'", optarg);
}
break;
case OPT_BLUR_FILTER_INTENSITY:
sscanf(optarg, "%f", &blurfilterIntensity);
break;
case OPT_NO_GRAY_FILTER:
parseMultiIndex(optarg, &options.no_grayfilter_multi_index);
break;
case OPT_GRAY_FILTER_SIZE:
if (!parse_rectangle_size(optarg, &grayfilterScanSize)) {
errOutput("unable to parse grayfilter-scan-size: '%s'", optarg);
}
break;
case OPT_GRAY_FILTER_STEP:
if (!parse_scan_step(optarg, &grayfilterScanStep)) {
errOutput("unable to parse grayfilter-scan-step: '%s'", optarg);
}
break;
case OPT_GRAY_FILTER_THRESHOLD:
sscanf(optarg, "%f", &grayfilterThreshold);
break;
case OPT_NO_MASK_SCAN:
parseMultiIndex(optarg, &options.no_mask_scan_multi_index);
break;
case OPT_MASK_SCAN_DIRECTION:
if (!parse_direction(optarg, &maskScanDirections)) {
errOutput("unable to parse mask-scan-direction: '%s'", optarg);
}
break;
case OPT_MASK_SCAN_SIZE:
if (!parse_rectangle_size(optarg, &maskScanSize)) {
errOutput("unable to parse mask-scan-size: '%s'", optarg);
}
break;
case OPT_MASK_SCAN_DEPTH:
if (!parse_symmetric_integers(optarg, &maskScanDepth[0],
&maskScanDepth[1]) ||
maskScanDepth[0] <= 0 || maskScanDepth[1] <= 0) {
errOutput("unable to parse mask-scan-depth: '%s'", optarg);
}
break;
case OPT_MASK_SCAN_STEP:
if (!parse_scan_step(optarg, &maskScanStep)) {
errOutput("unable to parse mask-scan-step");
}
break;
case OPT_MASK_SCAN_THRESHOLD:
if (!parse_symmetric_floats(optarg, &maskScanThreshold[0],
&maskScanThreshold[1]) ||
maskScanThreshold[0] <= 0 || maskScanThreshold[1] <= 0) {
errOutput("unable to parse mask-scan-threshold: '%s'", optarg);
}
break;
case OPT_MASK_SCAN_MINIMUM:
sscanf(optarg, "%d,%d", &maskScanMinimum[WIDTH],
&maskScanMinimum[HEIGHT]);
break;
case OPT_MASK_SCAN_MAXIMUM:
sscanf(optarg, "%d,%d", &maskScanMaximum[WIDTH],
&maskScanMaximum[HEIGHT]);
break;
case OPT_MASK_COLOR:
if (!parse_color(optarg, &options.mask_color)) {
errOutput("invalid value for mask-color: '%s'", optarg);
}
break;
case OPT_NO_MASK_CENTER:
parseMultiIndex(optarg, &options.no_mask_center_multi_index);
break;
case OPT_NO_DESKEW:
parseMultiIndex(optarg, &options.no_deskew_multi_index);
break;
case OPT_DESKEW_SCAN_DIRECTION:
if (!parse_edges(optarg, &deskewScanEdges)) {
errOutput("uanble to parse deskew-scan-direction: '%s'", optarg);
}
break;
case OPT_DESKEW_SCAN_SIZE:
sscanf(optarg, "%d", &deskewScanSize);
break;
case OPT_DESKEW_SCAN_DEPTH:
sscanf(optarg, "%f", &deskewScanDepth);
break;
case OPT_DESKEW_SCAN_RANGE:
sscanf(optarg, "%f", &deskewScanRange);
break;
case OPT_DESKEW_SCAN_STEP:
sscanf(optarg, "%f", &deskewScanStep);
break;
case OPT_DESKEW_SCAN_DEVIATION:
sscanf(optarg, "%f", &deskewScanDeviation);
break;
case OPT_NO_BORDER_SCAN:
parseMultiIndex(optarg, &options.no_border_scan_multi_index);
break;
case OPT_BORDER_SCAN_DIRECTION:
if (!parse_direction(optarg, &borderScanDirections)) {
errOutput("unable to parse border-scan-direction: '%s'", optarg);
}
break;
case OPT_BORDER_SCAN_SIZE:
if (!parse_rectangle_size(optarg, &borderScanSize)) {
errOutput("unable to parse border-scan-size: '%s'", optarg);
}
break;
case OPT_BORDER_SCAN_STEP:
if (!parse_scan_step(optarg, &borderScanStep)) {
errOutput("unable to parse border-scan-step: '%s'", optarg);
}
break;
case OPT_BORDER_SCAN_THRESHOLD:
if (!parse_symmetric_integers(optarg, &borderScanThreshold[0],
&borderScanThreshold[1]) ||
borderScanThreshold[0] <= 0 || borderScanThreshold <= 0) {
errOutput("unable to parse border-scan-threshold: '%s'", optarg);
}
break;
case OPT_BORDER_ALIGN:
if (!parse_edges(optarg, &borderAlign)) {
errOutput("unable to parse border-align: '%s'", optarg);
}
break;
case OPT_BORDER_MARGIN:
parse_physical_delta(optarg, &borderAlignMarginPhysical);
break;
case OPT_NO_BORDER_ALIGN:
parseMultiIndex(optarg, &options.no_border_align_multi_index);
break;
case OPT_NO_WIPE:
parseMultiIndex(optarg, &options.no_wipe_multi_index);
break;
case OPT_NO_BORDER:
parseMultiIndex(optarg, &options.no_border_multi_index);
break;
case 'w':
sscanf(optarg, "%f", &whiteThreshold);
break;
case 'b':
sscanf(optarg, "%f", &blackThreshold);
break;
case OPT_INPUT_PAGES:
sscanf(optarg, "%d", &options.input_count);
if (!(options.input_count >= 1 && options.input_count <= 2)) {
fprintf(
stderr,
"cannot set --input-pages value other than 1 or 2, ignoring.\n");
options.input_count = 1;
}
break;
case OPT_OUTPUT_PAGES:
sscanf(optarg, "%d", &options.output_count);
if (!(options.output_count >= 1 && options.output_count <= 2)) {
fprintf(
stderr,
"cannot set --output-pages value other than 1 or 2, ignoring.\n");
options.output_count = 1;
}
break;
case OPT_INPUT_FILE_SEQUENCE:
case OPT_OUTPUT_FILE_SEQUENCE:
errOutput(
"--input-file-sequence and --output-file-sequence are deprecated "
"and "
"unimplemented.\n"
"Please pass input output pairs as arguments to unpaper instead.");
break;
case OPT_INSERT_BLANK:
parseMultiIndex(optarg, &options.insert_blank);
break;
case OPT_REPLACE_BLANK:
parseMultiIndex(optarg, &options.replace_blank);
break;
case 'T':
options.write_output = false;
break;
case OPT_NO_MULTI_PAGES:
options.multiple_sheets = false;
break;
case OPT_PPI:
sscanf(optarg, "%hd", &ppi);
break;
case 't':
if (strcmp(optarg, "pbm") == 0) {
options.output_pixel_format = AV_PIX_FMT_MONOWHITE;
} else if (strcmp(optarg, "pgm") == 0) {
options.output_pixel_format = AV_PIX_FMT_GRAY8;
} else if (strcmp(optarg, "ppm") == 0) {
options.output_pixel_format = AV_PIX_FMT_RGB24;
}
break;
case 'q':
verbose = VERBOSE_QUIET;
break;
case OPT_OVERWRITE:
options.overwrite_output = true;
break;
case 'v':
verbose = VERBOSE_NORMAL;
break;
case OPT_VERBOSE_MORE:
verbose = VERBOSE_MORE;
break;
case OPT_DEBUG:
verbose = VERBOSE_DEBUG;
break;
case OPT_DEBUG_SAVE:
verbose = VERBOSE_DEBUG_SAVE;
break;
case OPT_INTERPOLATE:
if (!parse_interpolate(optarg, &options.interpolate_type)) {
errOutput("unable to parse interpolate: '%s'", optarg);
}
break;
}
}
// Expand any physical size to their pixel equivalents.
options.pre_shift = mils_delta_to_pixels(preShiftPhysical, ppi);
options.post_shift = mils_delta_to_pixels(postShiftPhysical, ppi);
options.sheet_size = mils_size_to_pixels(sheetSizePhysical, ppi);
options.page_size = mils_size_to_pixels(sizePhysical, ppi);
options.post_page_size = mils_size_to_pixels(postSizePhysical, ppi);
options.stretch_size = mils_size_to_pixels(stretchSizePhysical, ppi);
options.post_stretch_size =
mils_size_to_pixels(postStretchSizePhysical, ppi);
// Calculate the constant absolute values based on the relative parameters.
options.abs_black_threshold = WHITE * (1.0 - blackThreshold);
options.abs_white_threshold = WHITE * (whiteThreshold);
if (!validate_deskew_parameters(&options.deskew_parameters, deskewScanRange,
deskewScanStep, deskewScanDeviation,
deskewScanSize, deskewScanDepth,
deskewScanEdges)) {
errOutput("deskew parameters are not valid.");
}
if (!validate_mask_detection_parameters(
&options.mask_detection_parameters, maskScanDirections,
maskScanSize, maskScanDepth, maskScanStep, maskScanThreshold,
maskScanMinimum, maskScanMaximum)) {
errOutput("mask detection parameters are not valid.");
}
if (!validate_mask_alignment_parameters(
&options.mask_alignment_parameters, borderAlign,
mils_delta_to_pixels(borderAlignMarginPhysical, ppi))) {
errOutput("mask alignment parameters are not valid.");
};
if (!validate_border_scan_parameters(&options.border_scan_parameters,
borderScanDirections, borderScanSize,
borderScanStep, borderScanThreshold)) {
errOutput("border scan parameters are not valid.");
};
if (!validate_grayfilter_parameters(&options.grayfilter_parameters,
grayfilterScanSize, grayfilterScanStep,
grayfilterThreshold)) {
errOutput("grayfilter parameters are not valid.");
}
if (!validate_blackfilter_parameters(
&options.blackfilter_parameters, blackfilterScanSize,
blackfilterScanStep, blackfilterScanDepth[HORIZONTAL],
blackfilterScanDepth[VERTICAL], blackfilterScanDirections,
blackfilterScanThreshold, blackfilterIntensity,
blackfilterExcludeCount, blackfilterExclude)) {
errOutput("blackfilter parameters are not valid.");
}
if (!validate_blurfilter_parameters(&options.blurfilter_parameters,
blurfilterScanSize, blurfilterScanStep,
blurfilterIntensity)) {
errOutput("blurfilter parameters are not valid.");
}
if (options.start_input == -1)
options.start_input = (options.start_sheet - 1) * options.input_count + 1;
if (options.start_output == -1)
options.start_output =
(options.start_sheet - 1) * options.output_count + 1;
if (!options.multiple_sheets && options.end_sheet == -1)
options.end_sheet = options.start_sheet;