forked from atsommer/Simplicio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraThread.cpp
378 lines (351 loc) · 10.5 KB
/
CameraThread.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
#include "stdafx.h"
#include "CameraThread.h"
#include "form1.h"
#include "CameraSettings.h"
#include "sencam.h"
#include "ImageData.h"
#include "SencamDriver.h"
#include "WinXDriver.h"
//#include "SC2Driver.h"
//#include "cam_types.h"
namespace forms2{
using namespace System;
using namespace System::IO;
using namespace System::Threading;
void CameraThread::init(Form1^ f, String^ path){
mainForm = f;
//camDriver = (CameraDriver ^)gcnew SensicamDriver();
setLayersReadMainWindow = gcnew DelegateInt(f,&Form1::setLayersRead);
loopFinishedMainWindow = gcnew DelegateVoid(f,&Form1::loopFinished);
setImgMainWindow = gcnew DelegateImg(f,&Form1::addImageData);
singleFrame = f->isSingleFrame();
camSet = new CameraSettings();
//driver = (Driver *) new SencamDriver();
driver = (Driver ^) gcnew SencamDriver();//Driver REF
//driver = nullptr;
continueLock = gcnew String("");
interruptLock = gcnew String("");
filePath = String::Copy(path);
continueImageLoop = false;
interruptImageLoop = false;
running = false;
cameraInited = false;
callBack = true;
save=false;
nextTime = NO_TIME;
seqVars = gcnew LinkedList<Variable^>();
}
String^ CameraThread::getCameraDriverName(){
return driver->getDriverName();
}
int CameraThread::changeCamera(String^ camname){
if (running)
return CAM_NOT_CHANGED;
Driver^ newdriver=nullptr;//Driver REF
if (camname->Equals(gcnew String(L"Sensicam"))){
newdriver = (Driver ^)gcnew SencamDriver();//Driver REF
}else if (camname->Equals(gcnew String(L"SC2 Cam"))){
//newdriver = (Driver ^)gcnew SC2Driver();//Driver REF
MessageBox::Show("Pixelfly not yet supported.","Simplicio",MessageBoxButtons::OK);
}else if (camname->Equals(gcnew String(L"Princeton Instruments (WinView)"))){
newdriver = (Driver ^)gcnew WinXDriver();
//MessageBox::Show("Princeton Instruments not yet supported.","Simplicio",MessageBoxButtons::OK);
}else{
MessageBox::Show("Unsupported camera type requested.","Simplicio",MessageBoxButtons::OK);
}
if (newdriver != nullptr){
//requested camera is one of the supported types
driver->closeCamera();
//Driver* oldDriver = driver;
//driver=newdriver;
int err = newdriver->initCamera();
if (err){
MessageBox::Show("Error initializing camera.","Simplicio",MessageBoxButtons::OK);
newdriver->closeCamera();
//delete newdriver;//Driver REF
driver->initCamera();
return CAM_NOT_CHANGED;
}else{
//delete driver;//Driver REF
driver = newdriver;
cameraInited=true;
return 0;
}
}
/*
if (!err){
delete driver;
driver = newdriver;
return 0;
}else{
//error making new driver, re-initialize old driver
driver->initCamera();
if (newdriver != nullptr) delete newdriver;
}
*/
return CAM_NOT_CHANGED;
}
void CameraThread::setPath(String^ path){
filePath = String::Copy(path);
}
void CameraThread::initCamera(){
if (running){
MessageBox::Show("Stop the image acquisition before re-initializing the camera.","Simplicio",MessageBoxButtons::OK);
return;
}
int err = driver->initCamera();
if (!err)
//no errors:
cameraInited = true;
else
MessageBox::Show("Error initializing camera.","Simplicio",MessageBoxButtons::OK);
}
bool CameraThread::isRunning(){
return running;
}
bool CameraThread::openCameraDialog()
{//opens the camera settings dialogue box
//returns false if no errors occurred
/*
CLOSE_DIALOG_CAM();
if(OPEN_DIALOG_CAM(NULL,0,NULL)){
MessageBox::Show("Error opening camera settings dialogue box.","Box",MessageBoxButtons::OK);
return true;
}
return false;
*/
if (driver->openCameraDialog())
return true;//errors occurred
else
return false;
}
bool CameraThread::acquire(int layers, bool runLoop){
//start a new thread to acquire images
//return true if something prevents the form from starting
if (running) return true;
if (!cameraInited){
MessageBox::Show("Initialize camera first","Box",MessageBoxButtons::OK);return true;}
setContinue(runLoop);
setInterrupt(false);
Thread^ acquireThread = gcnew Thread(gcnew ParameterizedThreadStart(this,&CameraThread::takeImages));
acquireThread->Start(layers);
return false;
}
void CameraThread::stop(){
setContinue(false);
}
void CameraThread::interrupt(bool callback){
callBack=callback;
setInterrupt(true);
setContinue(false);
}
void CameraThread::closeCamera(){
/*
STOP_COC(0);
LOCK_DIALOG_CAM(0);//neccessary?
CLOSE_DIALOG_CAM();
SET_INIT(0);
*/
driver->closeCamera();
}
bool CameraThread::getContinue(){
bool continueTemp=false;
Monitor::Enter(continueLock);
try{
continueTemp = continueImageLoop;
}
finally{
Monitor::Exit(continueLock);
}
return continueTemp;
}
void CameraThread::setContinue(bool c){
Monitor::Enter(continueLock);
try{
continueImageLoop = c;
}
finally{
Monitor::Exit(continueLock);
}
}
bool CameraThread::getInterrupt(){
bool interruptTemp=false;
Monitor::Enter(interruptLock);
try{
interruptTemp = interruptImageLoop;
}
finally{
Monitor::Exit(interruptLock);
}
return interruptTemp;
}
void CameraThread::setInterrupt(bool c){
Monitor::Enter(interruptLock);
try{
interruptImageLoop = c;
}
finally{
Monitor::Exit(interruptLock);
}
}
void CameraThread::finishedRunning(){
if (callBack)
mainForm->BeginInvoke(loopFinishedMainWindow);
driver->lockCameraDialog(false);
running = false;
}
void CameraThread::readLayers(int layersRead){
array<Object^>^ parameters = gcnew array<Object^>(1);
parameters[0] = layersRead;
if (callBack)
mainForm->BeginInvoke(setLayersReadMainWindow,parameters);
}
void CameraThread::gotImage(ImageData^ img){
/*
if (img!=nullptr)
MessageBox::Show("Got image.","Box",MessageBoxButtons::OK);
else
MessageBox::Show("Got image: null","Box",MessageBoxButtons::OK);
*/
array<Object^>^ parameters = gcnew array<Object^>(1);
parameters[0] = img;
if(callBack)
mainForm->BeginInvoke(setImgMainWindow,parameters);
}
void CameraThread::setSave(bool saveFiles){
save = saveFiles;
}
void CameraThread::takeImages(Object^ layersObj)
{
const int layers = (int)layersObj;
driver->lockCameraDialog(true);
driver->armCamera();
running=true;
const int rows = driver->getRows();
const int cols = driver->getCols();
//MessageBox::Show(String::Concat("rows, cols:",rows,", ",cols),"Box",MessageBoxButtons::OK);
const int dbl = (driver->isDouble())? 2:1;
//MessageBox::Show(String::Concat("Doubler = ",dbl),"Box",MessageBoxButtons::OK);
//allocate memory for the images
UInt16 *buf = (UInt16 *)malloc(2*dbl*cols*rows*layers);
if (buf==0){
MessageBox::Show("Buffer memory allocation failed.","Box",MessageBoxButtons::OK);
finishedRunning();
}
//driver->stop();//remove this
bool runErr=false;
do{//loop for several images
int layersRead = 0;
readLayers(layersRead);
//loop over each layer of the current image
for (int lay=0;lay<layers;lay++)
{
//expose the CCD once
driver->expose();
//int picstat;
//int errS;
int imgstatus;
do{
//errS=GET_IMAGE_STATUS(&picstat);
imgstatus = driver->getImageStatus();
Thread::Sleep(0);
//}while (errS==0 && ((picstat&0x02)!=0 || (dbl==4 && (picstat&16) == 0)) && !getInterrupt());
} while (imgstatus == Driver::CAMERA_BUSY && !getInterrupt());
if (getInterrupt()) break;
if (imgstatus == Driver::IMAGE_ERROR){
MessageBox::Show("Error while waiting for image","Box",MessageBoxButtons::OK); break;}
if(imgstatus== Driver::CAMERA_BUSY){
MessageBox::Show("No image","Box",MessageBoxButtons::OK); break;}
//if (dbl==2 && (picstat&16) == 0)
// MessageBox::Show("One buffer empty","Box",MessageBoxButtons::OK);
int width = driver->getImageWidth();
int height = driver->getImageHeight();
if (width != cols || height != dbl*rows){//or if width>cols etc
MessageBox::Show("Wrong dimensions in transfered image","Box",MessageBoxButtons::OK);break;}
else{
int bufInd = lay*width*height;
driver->readImage(buf+bufInd);
layersRead++;
readLayers(layersRead);
}
}//end of layer for-loop
if (layersRead==layers){//each layer read
DateTime datetime = nextTime;
if (datetime.Equals(NO_TIME))
datetime = DateTime::Now;
nextTime = NO_TIME;
//copy the image data
int templayers=layers;
int temprows=rows;
if (singleFrame){//hijacks existing code to implement this new feature
templayers = 8;
temprows = 128;
}
ImageData^ img = gcnew ImageData(buf,temprows,cols,templayers,(dbl==2),singleFrame,datetime);
img->setSeqVars(seqVars);
seqVars->Clear();
//save
if (save) //saveImage(img);
img->saveFile(filePath);
//send the image to the main window
gotImage(img);
//saveImage(img);//(UInt16)rows*dbl,(UInt16)cols,(UInt16)layers,buf);
}
runErr = (layersRead!=layers);
}while(!runErr && getContinue());
free(buf);
driver->stop();
///if (runErr) MessageBox::Show("Acquisition stopped ","Box",MessageBoxButtons::OK);
finishedRunning();
}
/*
void CameraThread::saveImage(ImageData^ img)//UInt16 rows, UInt16 cols, UInt16 layers, UInt16 *buf)
{ //saves a 16-bit AIA file in little endian
String^ extension =".aia";
String^ basename = String::Format("{0}\\{1}",filePath,img->getDateTimeString());
String^ filename = String::Copy(basename);
//prevent overwrite by renaming
for (int i=0; (i<1000) && File::Exists(String::Concat(filename,extension));i++){
filename =String::Format("{0} Copy {1}",basename,i);// String::Concat(basename,i);
}
filename = String::Concat(filename,extension);
if (File::Exists(filename)) return;
FileStream^ fs = File::Open(filename,FileMode::Create);
BinaryWriter^ bw = gcnew BinaryWriter(fs);
try{
Byte b0 = 0x41;
Byte b1 = 0x49;
Byte b2 = 0x41;
bw->Write(b0);
bw->Write(b1);
bw->Write(b2);
UInt16 intLength = 2;
bw->Write(intLength);
int rows = (img->getRows())*(img->getDoubler());
int cols = img->getCols();
int layers = img->getLayers();
bw->Write((UInt16)rows);
bw->Write((UInt16)cols);
bw->Write((UInt16)layers);
for (int i=0;i<rows*cols*layers;i++)
bw->Write(img->getValue(i));
}
finally{
bw->Close();
fs->Close();
}
}
*/
void CameraThread::setNextTime(DateTime nexttime){
nextTime=nexttime;
}
void CameraThread::setSeqVars(LinkedList<Variable^>^ listvars){
seqVars->Clear();
for each(Variable^ var in listvars){
Variable^ newvar = gcnew Variable();
newvar->VariableValue = var->VariableValue;
newvar->VariableName = var->VariableName;
seqVars->AddLast(newvar);
}
}
}