-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
117 lines (101 loc) · 2.81 KB
/
main.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
#include <boost/filesystem.hpp>
#include <getopt.h>
#include <iostream>
#include <thread>
#include "DirectoryWalker.hpp"
#include "FingerprintStore.hpp"
#include "Util.hpp"
void usage() {
std::cerr << "photo-fingerprint:" << std::endl << std::endl;
std::cerr << " Generate fingerprints:" << std::endl;
std::cerr << " -g -s <source image directory> -d <destination directory for "
"fingerprints>"
<< std::endl;
std::cerr << std::endl;
std::cerr << " Find duplicates:" << std::endl;
std::cerr << " -f -s <fingerprint source dir> -d <image dir to be searched> "
"-u <fuzz factor>"
<< std::endl;
exit(1);
}
// Directory sanity check
bool isDirectoryValid(std::string directory) {
boost::filesystem::path d(directory);
if (!boost::filesystem::is_directory(d)) {
std::cerr << d << " is not a directory" << std::endl;
return false;
}
return true;
}
int main(int argc, char **argv) {
// Option handling
int ch = 0;
std::string srcDirectory, dstDirectory;
bool generateMode = false;
bool findDuplicateMode = false;
bool metadataMode = false;
int numThreads = std::thread::hardware_concurrency();
int fuzzFactor = 0;
while ((ch = getopt(argc, argv, "mgfd:s:t:u:")) != -1) {
switch (ch) {
case 'm':
metadataMode = true;
break;
case 'g':
generateMode = true;
break;
case 'f':
findDuplicateMode = true;
break;
case 's':
srcDirectory = optarg;
break;
case 'd':
dstDirectory = optarg;
break;
case 't':
numThreads = atoi(optarg);
break;
case 'u':
fuzzFactor = atoi(optarg);
break;
default:
usage();
}
}
// Only one mode can be selected
if (generateMode + findDuplicateMode + metadataMode != 1)
usage();
// Generate and find duplicate modes require two directories
if ((generateMode || findDuplicateMode) &&
(srcDirectory == "" || dstDirectory == ""))
usage();
// Check for a sensible number of threads
if (numThreads < 1)
usage();
std::cerr << "Using " << numThreads << " threads of maximum "
<< std::thread::hardware_concurrency() << std::endl;
// All modes require at least a source directory. Check it first.
if (!isDirectoryValid(srcDirectory))
return 1;
FingerprintStore fs(srcDirectory);
WorkerOptions options = {numThreads, fuzzFactor, dstDirectory};
if (metadataMode) {
options.WType = MetadataWorker;
fs.RunWorkers(options);
return 0;
}
// Remaining modes require a destination directory
if (!isDirectoryValid(dstDirectory))
return 1;
if (generateMode) {
options.WType = GenerateWorker;
fs.RunWorkers(options);
}
if (findDuplicateMode) {
options.WType = FingerprintWorker;
fs.Load();
fs.RunWorkers(options);
}
return 0;
}