-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWindowsThreadedStaticBackgroundCompressor.cpp
More file actions
237 lines (198 loc) · 9.69 KB
/
WindowsThreadedStaticBackgroundCompressor.cpp
File metadata and controls
237 lines (198 loc) · 9.69 KB
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
/*
* File: WindowsThreadedStaticBackgroundCompressor.cpp
* Author: Marc
*
* Created on April 17, 2012, 11:18 AM
*/
#ifdef WIN32
#include "WindowsThreadedStaticBackgroundCompressor.h"
#include "WindowsThreadStackCompressor.h"
#include <windows.h>
#include <process.h>
struct processingInfo {
public:
int imageNumber;
IplImage *im;
ImageMetaData *metadata;
WindowsThreadedStaticBackgroundCompressor *sbc;
processingInfo (int ImageNumber, IplImage *Im, ImageMetaData *Metadata, WindowsThreadedStaticBackgroundCompressor *Sbc) : imageNumber(ImageNumber), im(Im), metadata(Metadata), sbc(Sbc) {
}
};
WindowsThreadedStaticBackgroundCompressor::WindowsThreadedStaticBackgroundCompressor(int maxThreads) : maxCompressionThreads(maxThreads > 0 ? maxThreads : 1) {
InitializeCriticalSection(&backgroundImCS);
InitializeCriticalSection(&backgroundRemovedImageStackCS);
InitializeCriticalSection(&imsToProcessCS);
compressionThreadSemaphore = CreateSemaphore(NULL, maxCompressionThreads, maxCompressionThreads, NULL);
}
WindowsThreadedStaticBackgroundCompressor::WindowsThreadedStaticBackgroundCompressor(const WindowsThreadedStaticBackgroundCompressor& orig) : maxCompressionThreads(orig.maxCompressionThreads) {
}
WindowsThreadedStaticBackgroundCompressor::~WindowsThreadedStaticBackgroundCompressor() {
CloseHandle(compressionThreadSemaphore);
DeleteCriticalSection(&backgroundImCS);
DeleteCriticalSection(&backgroundRemovedImageStackCS);
DeleteCriticalSection(&imsToProcessCS);
}
int WindowsThreadedStaticBackgroundCompressor::numProcessed() {
/***start backgroundRemovedImageStackCS ****/
EnterCriticalSection(&backgroundRemovedImageStackCS);
int sz = bri.size();
LeaveCriticalSection(&backgroundRemovedImageStackCS);
/****end backgroundRemovedImageStackCS ****/
return sz;
}
int WindowsThreadedStaticBackgroundCompressor::numToProccess() {
/***start imsToProcessCS ****/
EnterCriticalSection(&imsToProcessCS);
int sz = imsToProcess.size();
LeaveCriticalSection(&imsToProcessCS);
/*****end imsToProcessCS****/
DWORD timeout_ms = 10L;
DWORD dwWaitResult = WaitForSingleObject(compressionThreadSemaphore, timeout_ms);
long numSemaphoresLeft = 0;
if (dwWaitResult == WAIT_OBJECT_0) {
ReleaseSemaphore(compressionThreadSemaphore, 1, &numSemaphoresLeft);
++numSemaphoresLeft;
}
// std::cout << "sz = " << sz << "; numSemaphoresLeft = " << numSemaphoresLeft << "; maxCompressionThreads = " << maxCompressionThreads << "numleft = " << sz + maxCompressionThreads - numSemaphoresLeft << std::endl;
return sz + maxCompressionThreads - numSemaphoresLeft;
}
void WindowsThreadedStaticBackgroundCompressor::frameCompressionFunction(void *ptr) {
processingInfo *pi = (processingInfo *) ptr;
//background is read-only so we don't need to protect it with a mutex
BackgroundRemovedImage *brim = new BackgroundRemovedImage(pi->im, pi->sbc->background, pi->sbc->threshBelowBackground, pi->sbc->threshAboveBackground, pi->sbc->smallDimMinSize, pi->sbc->lgDimMinSize, pi->metadata);
/***start backgroundRemovedImageStackCS ****/
EnterCriticalSection(&(pi->sbc->backgroundRemovedImageStackCS));
// std::cout << "inserting image number " << pi->imageNumber << std::endl;
pi->sbc->bri[pi->imageNumber] = brim;
// std::cout << "bri size is " << pi->sbc->bri.size() << std::endl;
LeaveCriticalSection(&(pi->sbc->backgroundRemovedImageStackCS));
/****end backgroundRemovedImageStackCS ****/
cvReleaseImage(&(pi->im));
ReleaseSemaphore(pi->sbc->compressionThreadSemaphore,1,NULL);
delete(pi);
}
//you do not need to clean up after the thread (it cleans itself up automatically)
/*
The _beginthread function creates a thread that begins execution of a routine at start_address.
The routine at start_address must use the __cdecl calling convention and should have no return value.
When the thread returns from that routine, it is terminated automatically.
_
You can call _endthread or _endthreadex explicitly to terminate a thread;
however, _endthread or _endthreadex is called automatically when the thread returns from the routine passed as a parameter.
Terminating a thread with a call to endthread or _endthreadex helps to ensure proper recovery of resources allocated for the thread.
_endthread automatically closes the thread handle (whereas _endthreadex does not).
Therefore, when using _beginthread and _endthread, do not explicitly close the thread handle by calling the Win32
http://msdn.microsoft.com/en-us/library/cc429605.aspx API.
This behavior differs from the Win32 http://msdn.microsoft.com/en-us/library/cc429100.aspx API.
*/
bool WindowsThreadedStaticBackgroundCompressor::readyToProcess() {
//wait for space to open for another thread
DWORD timeout_ms = 100L;
DWORD dwWaitResult = WaitForSingleObject(compressionThreadSemaphore, timeout_ms);
//if there is space for another thread, pop an image off the stack and start a thread to process it
if (dwWaitResult == WAIT_OBJECT_0) {
ReleaseSemaphore(compressionThreadSemaphore,1,NULL);
return framesWaitingToProcess();
}
return false;
}
bool WindowsThreadedStaticBackgroundCompressor::framesWaitingToProcess() {
EnterCriticalSection(&imsToProcessCS);
bool rv = !imsToProcess.empty();
LeaveCriticalSection(&imsToProcessCS);
return rv;
}
int WindowsThreadedStaticBackgroundCompressor::processFrame() {
/***start imsToProcessCS ****/
EnterCriticalSection(&imsToProcessCS);
if (imsToProcess.empty()) {
LeaveCriticalSection(&imsToProcessCS);
/*****end imsToProcessCS****/
Sleep(5); // prevent constant querying while last frames process
return numToProccess();
}
LeaveCriticalSection(&imsToProcessCS);
/*****end imsToProcessCS****/
/*****start backgroundImCS****/
EnterCriticalSection(&backgroundImCS);
if (background == NULL) {
/****end backgroundImCS ****/
LeaveCriticalSection(&backgroundImCS);
return -1;
}
LeaveCriticalSection(&backgroundImCS);
/****end backgroundImCS ****/
//wait for space to open for another thread
DWORD timeout_ms = 100L;
DWORD dwWaitResult = WaitForSingleObject(compressionThreadSemaphore, timeout_ms);
//if there is space for another thread, pop an image off the stack and start a thread to process it
if (dwWaitResult == WAIT_OBJECT_0) {
/***start imsToProcessCS ****/
EnterCriticalSection(&imsToProcessCS);
/***start backgroundRemovedImageStackCS ****/
EnterCriticalSection(&backgroundRemovedImageStackCS);
bri.resize(MAX(imsToProcess.size(), bri.size()), NULL);
// bri.reserve(imsToProcess.size());
LeaveCriticalSection(&backgroundRemovedImageStackCS);
/***end backgroundRemovedImageStackCS ****/
InputImT nextim = imsToProcess.back();
imsToProcess.pop_back();
int imnum = imsToProcess.size();
LeaveCriticalSection(&imsToProcessCS);
/***end imsToProcessCS ****/
processingInfo *pi = new processingInfo(imnum, nextim.first, nextim.second, this);
_beginthread(WindowsThreadedStaticBackgroundCompressor::frameCompressionFunction, 0, (void *) pi);
} else {
Sleep(5); // should not be necessary, because we timed out above, but prevent hammering with repeated requests for a handle
}
return numToProccess();
}
void WindowsThreadedStaticBackgroundCompressor::calculateBackground() {
EnterCriticalSection(&imsToProcessCS);
EnterCriticalSection(&backgroundImCS);
StaticBackgroundCompressor::calculateBackground();
LeaveCriticalSection(&backgroundImCS);
LeaveCriticalSection(&imsToProcessCS);
}
void WindowsThreadedStaticBackgroundCompressor::updateBackground(const IplImage* im) {
EnterCriticalSection(&backgroundImCS);
StaticBackgroundCompressor::updateBackground(im);
LeaveCriticalSection(&backgroundImCS);
}
void WindowsThreadedStaticBackgroundCompressor::addFrame(IplImage** im, ImageMetaData* metadata) {
EnterCriticalSection(&imsToProcessCS);
EnterCriticalSection(&backgroundImCS);
imsToProcess.push_back(InputImT(*im,metadata));
if (updateBackgroundFrameInterval > 0 && updateCount == 0) {
updateBackground(*im);
updateCount = updateBackgroundFrameInterval;
}
--updateCount;
*im = NULL;
LeaveCriticalSection(&backgroundImCS);
LeaveCriticalSection(&imsToProcessCS);
}
void WindowsThreadedStaticBackgroundCompressor::toDisk(std::ofstream& os) {
// EnterCriticalSection(&backgroundImCS);
// EnterCriticalSection(&backgroundRemovedImageStackCS);
StaticBackgroundCompressor::toDisk(os);
// LeaveCriticalSection(&backgroundImCS);
// LeaveCriticalSection(&backgroundRemovedImageStackCS);
}
std::string WindowsThreadedStaticBackgroundCompressor::saveDescription() {
std::string rv;
EnterCriticalSection(&backgroundRemovedImageStackCS);
rv = StaticBackgroundCompressor::saveDescription();
LeaveCriticalSection(&backgroundRemovedImageStackCS);
return rv;
}
int WindowsThreadedStaticBackgroundCompressor::sizeOnDisk() {
int rv;
EnterCriticalSection(&backgroundImCS);
EnterCriticalSection(&backgroundRemovedImageStackCS);
rv = StaticBackgroundCompressor::sizeOnDisk();
LeaveCriticalSection(&backgroundRemovedImageStackCS);
LeaveCriticalSection(&backgroundImCS);
return rv;
}
#endif