-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCandidateFinder.cpp
153 lines (130 loc) · 5.48 KB
/
CandidateFinder.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
#include "CandidateFinder.h"
#include "Correlation.h"
void analyzeTask(CandidateFinder& candidateFinder) {
int bytePerPixel = 1;
switch (candidateFinder.candidateSettings.bitDepth) {
case Bitdepth::bpp16:
bytePerPixel = 2;
break;
case Bitdepth::bpp24:
bytePerPixel = 3;
break;
case Bitdepth::bpp32:
bytePerPixel = 4;
break;
}
// estimate progress max (how many pixels will be used to calculate the correlation coefficients)
uint64_t progress_max = 0;
auto settings = candidateFinder.candidateSettings;
for (uint64_t width = settings.widthMin; width <= settings.widthMax; width++) {
uint64_t bytes_to_process = candidateFinder.dataLength;
if (settings.limitHeight) {
bytes_to_process = std::min(bytes_to_process, (uint64_t)width * bytePerPixel * settings.heightMax);
}
progress_max += width * ((bytes_to_process - width * bytePerPixel) / (width * bytePerPixel));
}
// calculate correlation coefficients for each width
uint64_t progress = 0;
unsigned char* dataptr;
for (uint64_t width = settings.widthMin; width <= settings.widthMax; width++) {
uint64_t widthInBytes = width * bytePerPixel;
auto correlationCoefficientsForWidth = std::vector<float>();
uint64_t offset = 0;
uint64_t height = 0;
uint64_t maxHeight = UINT64_MAX;
if (settings.limitHeight) {
maxHeight = settings.heightMax;
}
Candidate candidate;
bool processingCandidate = false;
float correlationCoefficientSum;
while ((offset + 2 * widthInBytes < candidateFinder.dataLength) && (height < maxHeight)) {
dataptr = candidateFinder.dataToAnalyze + offset;
float correlationCoefficient = std::abs(CorrelationCoefficient(dataptr, dataptr + widthInBytes, widthInBytes));
correlationCoefficientsForWidth.push_back(correlationCoefficient);
if (!processingCandidate) {
if (correlationCoefficient > settings.hysteresisMax) {
processingCandidate = true;
candidate.bytePerPixel = bytePerPixel;
candidate.width = width;
candidate.startOffset = offset;
candidate.startLine = height;
correlationCoefficientSum = correlationCoefficient;
}
}
else {
correlationCoefficientSum += correlationCoefficient;
if (correlationCoefficient < settings.hysteresisMin) {
processingCandidate = false;
candidate.endLine = height;
candidate.height = candidate.endLine - candidate.startLine;
candidate.pixelCount = candidate.width * candidate.height;
candidate.meanCorrelationCoefficient = correlationCoefficientSum / (candidate.height + 1);
candidate.score = candidate.pixelCount * candidate.meanCorrelationCoefficient;
if ((!settings.limitHeight) || ((candidate.height <= settings.heightMax) && (candidate.height >= settings.heightMin))) {
std::lock_guard<std::mutex> candidatesGuard(candidateFinder.candidatesMutex);
candidateFinder.candidates.insert(candidate);
}
}
}
offset += widthInBytes;
progress += width;
height++;
{
std::lock_guard<std::mutex> guard(candidateFinder.analysisProgressMutex);
candidateFinder.analysisProgress = progress / (float)progress_max;
}
{
std::lock_guard<std::mutex> guard(candidateFinder.finderStateMutex);
if (candidateFinder.finderState == FinderState::cancellationRequested) {
candidateFinder.finderState = FinderState::cancelled;
return;
}
}
}
// if a candidate is still in processing, add it now
if (processingCandidate) {
processingCandidate = false;
candidate.endLine = height;
candidate.height = candidate.endLine - candidate.startLine;
candidate.pixelCount = candidate.width * candidate.height;
candidate.meanCorrelationCoefficient = correlationCoefficientSum / (candidate.height + 1);
candidate.score = candidate.pixelCount * candidate.meanCorrelationCoefficient;
if ((!settings.limitHeight) || ((candidate.height <= settings.heightMax) && (candidate.height >= settings.heightMin))) {
std::lock_guard<std::mutex> candidatesGuard(candidateFinder.candidatesMutex);
candidateFinder.candidates.insert(candidate);
}
}
candidateFinder.correlationCoefficientsForLines.push_back(correlationCoefficientsForWidth);
}
std::lock_guard<std::mutex> progressGuard(candidateFinder.analysisProgressMutex);
candidateFinder.analysisProgress = 1.0f;
std::lock_guard<std::mutex> finderGuard(candidateFinder.finderStateMutex);
candidateFinder.finderState = FinderState::ready;
}
CandidateFinder::CandidateFinder(CandidateSettings settings, unsigned char* dataToAnalyze,
uint64_t dataLength) {
candidateSettings = settings;
finderState = FinderState::analyzing;
this->dataToAnalyze = dataToAnalyze;
this->dataLength = dataLength;
analysisProgress = 0.0f;
// start analyze task in background
std::thread analyzeThread(analyzeTask, std::ref(*this));
analyzeThread.detach();
}
CandidateFinder::~CandidateFinder() {
if (finderState == FinderState::analyzing) {
finderState = FinderState::cancellationRequested;
for (;;) {
{
std::lock_guard<std::mutex> finderGuard(finderStateMutex);
if (finderState != FinderState::cancellationRequested) {
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
free(dataToAnalyze);
}