-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsiCamera.cpp
1902 lines (1666 loc) · 48 KB
/
AsiCamera.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
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
///////////////////////////////////////////////////////////////////////////////
// FILE: ASICamera.cpp
// PROJECT: Micro-Manager
// SUBSYSTEM: DeviceAdapters
//-----------------------------------------------------------------------------
// DESCRIPTION: A camera implementation that is backed by the file system
// Can access stage positions to choose image to display
//
// AUTHOR: Mikhail Latyshov
//
// COPYRIGHT: 2024 Mikhail Latyshov
// LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ASICamera.h"
using namespace std;
const char* cameraName = "ASICamera";
const char* g_StateDeviceName = "MM_EFW";
const char* g_PixelType_RAW8 = "RAW8";
const char* g_PixelType_RAW12 = "RAW12";
const char* g_PixelType_RAW16 = "RAW16";
const char* g_PixelType_Y8 = "Y8";
const char* g_PixelType_RGB24 = "RGB24";
const char* g_PixelType_RGB48 = "RGB48";
const char* g_DeviceIndex = "Selected Device";
const char* g_Keyword_USBTraffic = "USBTraffic";
const char* g_Keyword_USBTraffic_Auto = "USBTraffic Auto";
const char* g_Keyword_IsHeaterOn = "Anti-dew Switch";
const char* g_Keyword_IsCoolerOn = "Cooler Switch";
const char* g_Keyword_TargetTemp = "Target Temperature";
const char* g_Keyword_CoolPowerPerc = "Cooler Power Percentage";
const char* g_Keyword_WB_R = "White Balance Red";
const char* g_Keyword_WB_B = "White Balance Blue";
const char* g_Keyword_AutoWB = "White Balance Auto";
const char* g_Keyword_on = "on";
const char* g_Keyword_off = "off";
const char* g_Keyword_Gamma = "Gamma";
const char* g_Keyword_AutoExp = "Exp Auto";
const char* g_Keyword_AutoGain = "Gain Auto";
const char* g_Keyword_Flip = "Flip";
const char* g_Keyword_HighSpeedMode = "High Speed Mode";
const char* g_Keyword_HardwareBin = "Hardware Bin";
const char* g_Keyword_USBHost = "USB Host";
#include <fstream>
#include <iostream>
#include <cstdarg>
#include <string>
#include <ctime>
void LogToFile(const char* format, ...) {
std::ofstream logFile;
logFile.open("ASICamera_mg_log.txt", std::ios_base::app); // Открываем файл для добавления
if (!logFile.is_open()) return;
// Получаем текущее время
std::time_t now = std::time(nullptr);
logFile << std::asctime(std::localtime(&now)) << " - ";
// Форматируем строку
char buffer[256];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
// Записываем строку в файл
logFile << buffer << std::endl;
logFile.close();
}
inline static void OutputDbgPrint(const char* strOutPutString, ...)
{
return; // лог только для отладки..
char strBuf[128] = { 0 };
sprintf(strBuf, "<%s> ", "MM_ASI");
va_list vlArgs;
va_start(vlArgs, strOutPutString);
vsnprintf((char*)(strBuf + strlen(strBuf)), sizeof(strBuf) - strlen(strBuf), strOutPutString, vlArgs);
va_end(vlArgs);
#ifdef _DEBUG
#ifdef _WINDOWS
OutputDebugStringA(strBuf);
#elif defined _LIN
printf("%s", strBuf);
#endif
#endif
// Добавляем запись в файл
LogToFile("%s", strBuf);
}
ASICamera::ASICamera() :
iBin(1),
initialized_(false),
iROIWidth(0),
iROIHeight(0),
Status(closed),
ImgType(ASI_IMG_RAW8),
uc_pImg(0),
imageCounter_(0),
thd_(0),
pControlCaps(0),
pRGB32(0),
pRGB64(0),
b12RAW(false),
bRGB48(false),
ImgFlip(ASI_FLIP_NONE)
{
// call the base class method to set-up default error codes/messages
InitializeDefaultErrorMessages();
ASICameraInfo.CameraID = -1;
// Description property
int ret = CreateProperty(MM::g_Keyword_Description, "ASICamera Camera Device Adapter", MM::String, true);
assert(ret == DEVICE_OK);
iConnectedCamNum = ASIGetNumOfConnectedCameras();
vector<string> CamIndexValues;
for (int i = 0; i < iConnectedCamNum; i++)
{
ASIGetCameraProperty(&ASICameraInfo, i);
strcpy(ConnectedCamName[i], ASICameraInfo.Name);
CamIndexValues.push_back(ConnectedCamName[i]);
}
CPropertyAction* pAct = new CPropertyAction(this, &ASICamera::OnSelectCamIndex);
if (iConnectedCamNum > 0)
{
strcpy(sz_ModelIndex, ConnectedCamName[0]);//Ĭ�ϴ�һ��camera
//iCamIndex = 0;
ASIGetCameraProperty(&ASICameraInfo, 0);
}
else
{
strcpy(sz_ModelIndex, "no ASI camera connected");
}
// strcpy(sz_ModelIndex, "DropDown");
ret = CreateProperty(g_DeviceIndex, sz_ModelIndex, MM::String, false, pAct, true); //ѡ������ͷ���
SetAllowedValues(g_DeviceIndex, CamIndexValues);
assert(ret == DEVICE_OK);
strcpy(FlipArr[ASI_FLIP_BOTH], "both");
strcpy(FlipArr[ASI_FLIP_HORIZ], "horz");
strcpy(FlipArr[ASI_FLIP_VERT], "vert");
strcpy(FlipArr[ASI_FLIP_NONE], "none");
// camera type pre-initialization property
// create live video thread
thd_ = new SequenceThread(this);
}
ASICamera::~ASICamera()
{
if (Status != closed)
Shutdown();
DeleteImgBuf();
if (thd_)
delete thd_;
}
int ASICamera::Initialize()
{
OutputDbgPrint("open camera ID: %d\n", ASICameraInfo.CameraID);
if (ASICameraInfo.CameraID < 0)
return DEVICE_NOT_CONNECTED;
if (ASICameraInfo.CameraID >= 0)
{
if (ASIOpenCamera(ASICameraInfo.CameraID) != ASI_SUCCESS)
return DEVICE_NOT_CONNECTED;
if (ASIInitCamera(ASICameraInfo.CameraID) != ASI_SUCCESS)
return DEVICE_NOT_CONNECTED;
// CameraName
// ASIGetCameraProperty(&ASICameraInfo, iCamIndex);
char* sz_Name = ASICameraInfo.Name;
int nRet = CreateStringProperty(MM::g_Keyword_CameraName, sz_Name, true);
assert(nRet == DEVICE_OK);
iROIWidth = ASICameraInfo.MaxWidth / iBin / 8 * 8;// 2->1, *2
iROIHeight = ASICameraInfo.MaxHeight / iBin / 2 * 2;//1->2. *0.5
ASISetROIFormat(ASICameraInfo.CameraID, iROIWidth, iROIHeight, iBin, ImgType);
iSetWid = iROIWidth;
iSetHei = iROIHeight;
iSetBin = iBin;
iSetX = 0;
iSetY = 0;
long lVal;
ASI_BOOL bAuto;
lExpMs = GetExposure();
Status = opened;
ASIGetNumOfControls(ASICameraInfo.CameraID, &iCtrlNum);
DeletepControlCaps(ASICameraInfo.CameraID);
MallocControlCaps(ASICameraInfo.CameraID);
}
if (initialized_)
return DEVICE_OK;
OutputDbgPrint("Init property\n");
// set property list
// -----------------
vector<string> boolValues;
boolValues.push_back(g_Keyword_off);
boolValues.push_back(g_Keyword_on);
// binning
CPropertyAction* pAct = new CPropertyAction(this, &ASICamera::OnBinning);
int ret = CreateProperty(MM::g_Keyword_Binning, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
vector<string> binningValues;
int i = 0;
char cBin[2];
while (ASICameraInfo.SupportedBins[i] > 0)
{
sprintf(cBin, "%d", ASICameraInfo.SupportedBins[i]);
binningValues.push_back(cBin);
i++;
}
ret = SetAllowedValues(MM::g_Keyword_Binning, binningValues);
assert(ret == DEVICE_OK);
// pixel type
pAct = new CPropertyAction(this, &ASICamera::OnPixelType);
ret = CreateProperty(MM::g_Keyword_PixelType, g_PixelType_Y8, MM::String, false, pAct);
assert(ret == DEVICE_OK);
vector<string> pixelTypeValues;
if (isImgTypeSupported(ASI_IMG_RAW8))
pixelTypeValues.push_back(g_PixelType_RAW8);
if (isImgTypeSupported(ASI_IMG_RAW16))
{
pixelTypeValues.push_back(g_PixelType_RAW16);
pixelTypeValues.push_back(g_PixelType_RAW12);
}
if (isImgTypeSupported(ASI_IMG_Y8))
pixelTypeValues.push_back(g_PixelType_Y8);
if (isImgTypeSupported(ASI_IMG_RGB24))
{
pixelTypeValues.push_back(g_PixelType_RGB24);
pixelTypeValues.push_back(g_PixelType_RGB48);
}
ret = SetAllowedValues(MM::g_Keyword_PixelType, pixelTypeValues);
assert(ret == DEVICE_OK);
//gain
int iMin, iMax;
ASI_CONTROL_CAPS* pOneCtrlCap = GetOneCtrlCap(ASI_GAIN);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnGain);
ret = CreateProperty(MM::g_Keyword_Gain, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(MM::g_Keyword_Gain, iMin, iMax);
}
//brightness
pOneCtrlCap = GetOneCtrlCap(ASI_BRIGHTNESS);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnBrightness);
ret = CreateProperty(MM::g_Keyword_Offset, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(MM::g_Keyword_Offset, iMin, iMax);
}
//USBTraffic
pOneCtrlCap = GetOneCtrlCap(ASI_BANDWIDTHOVERLOAD);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnUSBTraffic);
ret = CreateProperty(g_Keyword_USBTraffic, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(g_Keyword_USBTraffic, iMin, iMax);
pAct = new CPropertyAction(this, &ASICamera::OnUSB_Auto);
ret = CreateProperty(g_Keyword_USBTraffic_Auto, g_Keyword_off, MM::String, false, pAct);
assert(ret == DEVICE_OK);
ret = SetAllowedValues(g_Keyword_USBTraffic_Auto, boolValues);
assert(ret == DEVICE_OK);
}
//Temperature
if (GetOneCtrlCap(ASI_TEMPERATURE))
{
pAct = new CPropertyAction(this, &ASICamera::OnTemperature);
ret = CreateProperty(MM::g_Keyword_CCDTemperature, "0", MM::Float, true, pAct);
assert(ret == DEVICE_OK);
}
// white balance red
if (ASICameraInfo.IsColorCam)
{
pOneCtrlCap = GetOneCtrlCap(ASI_WB_R);
pAct = new CPropertyAction(this, &ASICamera::OnWB_R);
ret = CreateProperty(g_Keyword_WB_R, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(g_Keyword_WB_R, iMin, iMax);
// white balance blue
pOneCtrlCap = GetOneCtrlCap(ASI_WB_B);
pAct = new CPropertyAction(this, &ASICamera::OnWB_B);
ret = CreateProperty(g_Keyword_WB_B, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(g_Keyword_WB_B, iMin, iMax);
//auto white balance blue
pAct = new CPropertyAction(this, &ASICamera::OnAutoWB);
ret = CreateProperty(g_Keyword_AutoWB, g_Keyword_off, MM::String, false, pAct);
assert(ret == DEVICE_OK);
ret = SetAllowedValues(g_Keyword_AutoWB, boolValues);
}
//cool
if (ASICameraInfo.IsCoolerCam)
{
//Cooler Switch
pAct = new CPropertyAction(this, &ASICamera::OnCoolerOn);
ret = CreateProperty(g_Keyword_IsCoolerOn, g_Keyword_off, MM::String, false, pAct);
assert(ret == DEVICE_OK);
vector<string> coolerValues;
coolerValues.push_back(g_Keyword_off);
coolerValues.push_back(g_Keyword_on);
ret = SetAllowedValues(g_Keyword_IsCoolerOn, coolerValues);
assert(ret == DEVICE_OK);
//Target Temperature
pOneCtrlCap = GetOneCtrlCap(ASI_TARGET_TEMP);
pAct = new CPropertyAction(this, &ASICamera::OnTargetTemp);
ret = CreateProperty(g_Keyword_TargetTemp, "0", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(g_Keyword_TargetTemp, iMin, iMax);
assert(ret == DEVICE_OK);
//power percentage
pOneCtrlCap = GetOneCtrlCap(ASI_COOLER_POWER_PERC);
pAct = new CPropertyAction(this, &ASICamera::OnCoolerPowerPerc);
ret = CreateProperty(g_Keyword_CoolPowerPerc, "0", MM::Integer, true, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(g_Keyword_CoolPowerPerc, iMin, iMax);
assert(ret == DEVICE_OK);
//Anti dew
pAct = new CPropertyAction(this, &ASICamera::OnHeater);
ret = CreateProperty(g_Keyword_IsHeaterOn, g_Keyword_off, MM::String, false, pAct);
assert(ret == DEVICE_OK);
ret = SetAllowedValues(g_Keyword_IsHeaterOn, coolerValues);
assert(ret == DEVICE_OK);
}
//gamma
pOneCtrlCap = GetOneCtrlCap(ASI_GAMMA);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnGamma);
ret = CreateProperty(g_Keyword_Gamma, "1", MM::Integer, false, pAct);
assert(ret == DEVICE_OK);
iMin = pOneCtrlCap->MinValue;
iMax = pOneCtrlCap->MaxValue;
SetPropertyLimits(g_Keyword_Gamma, iMin, iMax);
}
//auto exposure
pOneCtrlCap = GetOneCtrlCap(ASI_EXPOSURE);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnAutoExp);
ret = CreateProperty(g_Keyword_AutoExp, "1", MM::String, false, pAct);
assert(ret == DEVICE_OK);
SetAllowedValues(g_Keyword_AutoExp, boolValues);
}
//auto gain
pOneCtrlCap = GetOneCtrlCap(ASI_GAIN);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnAutoGain);
ret = CreateProperty(g_Keyword_AutoGain, "1", MM::String, false, pAct);
assert(ret == DEVICE_OK);
SetAllowedValues(g_Keyword_AutoGain, boolValues);
}
//flip
pOneCtrlCap = GetOneCtrlCap(ASI_FLIP);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnFlip);
ret = CreateProperty(g_Keyword_Flip, "1", MM::String, false, pAct);
assert(ret == DEVICE_OK);
boolValues.clear();
for (int i = 0; i < 4; i++)
boolValues.push_back(FlipArr[i]);
SetAllowedValues(g_Keyword_Flip, boolValues);
}
//high speed mode
pOneCtrlCap = GetOneCtrlCap(ASI_HIGH_SPEED_MODE);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnHighSpeedMod);
ret = CreateProperty(g_Keyword_HighSpeedMode, "1", MM::String, false, pAct);
assert(ret == DEVICE_OK);
boolValues.clear();
boolValues.push_back(g_Keyword_off);
boolValues.push_back(g_Keyword_on);
SetAllowedValues(g_Keyword_HighSpeedMode, boolValues);
}
//hardware bin
pOneCtrlCap = GetOneCtrlCap(ASI_HARDWARE_BIN);
if (pOneCtrlCap)
{
pAct = new CPropertyAction(this, &ASICamera::OnHardwareBin);
ret = CreateProperty(g_Keyword_HardwareBin, "1", MM::String, false, pAct);
assert(ret == DEVICE_OK);
boolValues.clear();
boolValues.push_back(g_Keyword_off);
boolValues.push_back(g_Keyword_on);
SetAllowedValues(g_Keyword_HardwareBin, boolValues);
}
//USB3 host
char USBHost[16] = { 0 };
if (ASICameraInfo.IsUSB3Host)
strcpy(USBHost, "USB3");
else
strcpy(USBHost, "USB2");
ret = CreateProperty(g_Keyword_USBHost, USBHost, MM::String, true);
assert(ret == DEVICE_OK);
// synchronize all properties
// --------------------------
ret = UpdateStatus();
if (ret != DEVICE_OK)
return ret;
OutputDbgPrint("Init initialized_ true\n");
initialized_ = true;
return DEVICE_OK;
}
int ASICamera::Shutdown()
{
initialized_ = false;
OutputDbgPrint("Shutdown initialized_ false\n");
return DEVICE_OK;
}
void ASICamera::GetName(char * name) const
{
CDeviceUtils::CopyLimitedString(name, cameraName);
}
long ASICamera::GetImageBufferSize() const
{
OutputDbgPrint("GetImageBufferSize\n");
return iROIWidth * iROIHeight * iPixBytes;
}
unsigned ASICamera::GetBitDepth() const//��ɫ�ķ�Χ 8bit �� 16bit
{
if (ImgType == ASI_IMG_RAW16)
{
if (b12RAW)
return 12;
else
return 16;
}
else
{
if (ImgType == ASI_IMG_RGB24 && bRGB48)
return 16;
else
return 8;
}
}
int ASICamera::GetBinning() const
{
return 1;
}
int ASICamera::SetBinning(int)
{
return DEVICE_OK;
}
void ASICamera::SetExposure(double exp)
{
lExpMs = exp;
ASISetControlValue(ASICameraInfo.CameraID, ASI_EXPOSURE, exp * 1000, ASI_FALSE);
}
double ASICamera::GetExposure() const
{
// return lExpMs;
long lVal;
ASI_BOOL bAuto;
ASIGetControlValue(ASICameraInfo.CameraID, ASI_EXPOSURE, &lVal, &bAuto);
return lVal / 1000;
}
/**
* Returns the name for each component
*/
int ASICamera::GetComponentName(unsigned component, char* name)
{
if (iComponents != 1)
{
switch (component)
{
case 1:
strcpy(name, "red");
break;
case 2:
strcpy(name, "green");
break;
case 3:
strcpy(name, "blue");
break;
case 4:
strcpy(name, "0");
break;
default:
strcpy(name, "error");
break;
}
}
else
strcpy(name, "grey");
return DEVICE_OK;
}
/**
* Sets the camera Region Of Interest.
* Required by the MM::Camera API.
* This command will change the dimensions of the image.
* Depending on the hardware capabilities the camera may not be able to configure the
* exact dimensions requested - but should try do as close as possible.
* If the hardware does not have this capability the software should simulate the ROI by
* appropriately cropping each frame.
* This demo implementation ignores the position coordinates and just crops the buffer.
* @param x - top-left corner coordinate
* @param y - top-left corner coordinate
* @param xSize - width
* @param ySize - height
*/
void ASICamera::DeleteImgBuf()
{
if (uc_pImg)
{
delete[] uc_pImg;
uc_pImg = 0;
iBufSize = 0;
OutputDbgPrint("clr\n");
}
if (pRGB32)
{
delete[] pRGB32;
pRGB32 = 0;
}
if (pRGB64)
{
delete[] pRGB64;
pRGB64 = 0;
}
}
int ASICamera::SetROI(unsigned x, unsigned y, unsigned xSize, unsigned ySize)
{
if (xSize == 0 && ySize == 0)
;
else
{
/*20160107
����ROI������ʾͼƬΪ���յ�,����������ʼ��(x, y)��������ʾͼƬ����ʼ��(����GetROI()�õ�)�������ѡ���ƫ�ƣ�
�ߴ����ʼ�㶼��ImgBin/iSetBin����, iSetBin��Ҫ���õ�binֵ
����з�ת, ��Ҫ������������������
*/
switch (ImgFlip)
{
case ASI_FLIP_NONE:
break;
case ASI_FLIP_HORIZ:
x = ASICameraInfo.MaxWidth / ImgBin - x - xSize;
break;
case ASI_FLIP_VERT:
y = ASICameraInfo.MaxHeight / ImgBin - y - ySize;
break;
case ASI_FLIP_BOTH:
x = ASICameraInfo.MaxWidth / ImgBin - x - xSize;
y = ASICameraInfo.MaxHeight / ImgBin - y - ySize;
break;
}
iSetWid = xSize * ImgBin / iSetBin;// 2->1, *2
iSetHei = ySize * ImgBin / iSetBin;//1->2. *0.5
iSetWid = iSetWid / 8 * 8;
iSetHei = iSetHei / 2 * 2;
iSetX = x * ImgBin / iSetBin;//bin�ı��, startpos�������bin��Ļ���ģ�ҲҪ���ձ����ı�
iSetY = y * ImgBin / iSetBin;
iSetX = iSetX / 4 * 4;
iSetY = iSetY / 2 * 2;
if (ASISetROIFormat(ASICameraInfo.CameraID, iSetWid, iSetHei, iSetBin, ImgType) == ASI_SUCCESS)//������óɹ�
{
OutputDbgPrint("wid:%d hei:%d bin:%d\n", xSize, ySize, iBin);
DeleteImgBuf();//buff��С�ı�
ASISetStartPos(ASICameraInfo.CameraID, iSetX, iSetY);
}
ASIGetROIFormat(ASICameraInfo.CameraID, &iROIWidth, &iROIHeight, &iBin, &ImgType);
}
return DEVICE_OK;
}
int ASICamera::GetROI(unsigned & x, unsigned & y, unsigned & xSize, unsigned & ySize)
{
/* 20160107
�õ���ʾͼ���ROI��Ϣ
����з�ת, Ҫ����ɷ�����ߵ�����, ���������ӵõ���ROI,�ٻ�����������������*/
x = ImgStartX;
y = ImgStartY;
switch (ImgFlip)
{
case ASI_FLIP_NONE:
break;
case ASI_FLIP_HORIZ:
x = ASICameraInfo.MaxWidth / ImgBin - ImgStartX - ImgWid;
break;
case ASI_FLIP_VERT:
y = ASICameraInfo.MaxHeight / ImgBin - ImgStartY - ImgHei;
break;
case ASI_FLIP_BOTH:
x = ASICameraInfo.MaxWidth / ImgBin - ImgStartX - ImgWid;
y = ASICameraInfo.MaxHeight / ImgBin - ImgStartY - ImgHei;
break;
}
xSize = ImgWid;
ySize = ImgHei;
return DEVICE_OK;
}
int ASICamera::ClearROI()
{
// ResizeImageBuffer();
iSetWid = iROIWidth = ASICameraInfo.MaxWidth / iBin / 8 * 8;
iSetHei = iROIHeight = ASICameraInfo.MaxHeight / iBin / 2 * 2;
if (ASISetROIFormat(ASICameraInfo.CameraID, iROIWidth, iROIHeight, iBin, ImgType) == ASI_SUCCESS)
{
ASISetStartPos(ASICameraInfo.CameraID, 0, 0);
iSetX = iSetY = 0;
DeleteImgBuf();
}
return DEVICE_OK;
}
int ASICamera::IsExposureSequenceable(bool & isSequenceable) const
{
isSequenceable = false;
return DEVICE_OK;
}
/*
* Inserts Image and MetaData into MMCore circular Buffer
*/
int ASICamera::InsertImage()
{
//OutputDbgPrint("InsertImage\n");
MM::MMTime timeStamp = this->GetCurrentMMTime();
char label[MM::MaxStrLength];
this->GetLabel(label);
// Important: metadata about the image are generated here:
Metadata md;
md.put("Camera", label);
char buf[MM::MaxStrLength];
GetProperty(MM::g_Keyword_Binning, buf);
md.put(MM::g_Keyword_Binning, buf);
// MMThreadGuard g(imgPixelsLock_);
const unsigned char* pI;
pI = GetImageBuffer();
int ret = 0;
ret = GetCoreCallback()->InsertImage(this, pI, iROIWidth, iROIHeight, iPixBytes, md.Serialize().c_str());
if (ret == DEVICE_BUFFER_OVERFLOW)//����������Ҫ���, �����ܼ�������ͼ�����ס
{
// do not stop on overflow - just reset the buffer
GetCoreCallback()->ClearImageBuffer(this);
// don't process this same image again...
return GetCoreCallback()->InsertImage(this, pI, iROIWidth, iROIHeight, iPixBytes, md.Serialize().c_str(), false);
}
else
return ret;
}
unsigned ASICamera::GetImageWidth() const
{
return iROIWidth;
}
unsigned ASICamera::GetImageHeight() const
{
return iROIHeight;
}
unsigned ASICamera::GetImageBytesPerPixel() const
{
return iPixBytes;
}
int ASICamera::SnapImage()
{
// GenerateImage();
// ASIGetStartPos(iCamIndex, &iStartXImg, &iStartYImg);
ASIStartExposure(ASICameraInfo.CameraID, ASI_FALSE);
Status = snaping;
unsigned long time = GetTickCount(), deltaTime = 0;
ASI_EXPOSURE_STATUS exp_status;
do
{
ASIGetExpStatus(ASICameraInfo.CameraID, &exp_status);
//OutputDbgPrint("SnapImage do exp_status %d\n", (int)exp_status);
deltaTime = GetTickCount() - time;
if (deltaTime > 10000 && GetTickCount() - time > 3 * lExpMs)
{
OutputDbgPrint("SnapImage delta %d ms, stop snap\n", deltaTime);
ASIStopExposure(ASICameraInfo.CameraID);
break;
}
Sleep(1);
} while (exp_status == ASI_EXP_WORKING);
Status = opened;
if (uc_pImg == 0)
{
iBufSize = GetImageBufferSize();
uc_pImg = new unsigned char[iBufSize];
}
if (exp_status == ASI_EXP_SUCCESS)
{
OutputDbgPrint("ASI_EXP_SUCCESS exp_status %d\n", (int)exp_status);
ASIGetDataAfterExp(ASICameraInfo.CameraID, uc_pImg, iBufSize);
ASI_BOOL bAuto;
long lVal;
ASIGetControlValue(ASICameraInfo.CameraID, ASI_FLIP, &lVal, &bAuto);
ImgFlip = (ASI_FLIP_STATUS)lVal;
ASI_IMG_TYPE imgType;
ASIGetROIFormat(ASICameraInfo.CameraID, &ImgWid, &ImgHei, &ImgBin, &imgType);
ASIGetStartPos(ASICameraInfo.CameraID, &ImgStartX, &ImgStartY);
}
OutputDbgPrint("exp_status %d\n", (int)exp_status);
if (exp_status == ASI_EXP_SUCCESS)
return DEVICE_OK;
else
return DEVICE_SNAP_IMAGE_FAILED;
}
int ASICamera::PrepareSequenceAcqusition()
{
if (IsCapturing())
return DEVICE_CAMERA_BUSY_ACQUIRING;
/* int ret = GetCoreCallback()->PrepareForAcq(this);
if (ret != DEVICE_OK)
return ret;*/
return DEVICE_OK;
}
/*
* Do actual capturing
* Called from inside the thread
*/
int ASICamera::RunSequenceOnThread(MM::MMTime startTime)
{
int ret = DEVICE_ERR;
if (ASIGetVideoData(ASICameraInfo.CameraID, uc_pImg, iBufSize, 2 * lExpMs) == ASI_SUCCESS)
{
ret = InsertImage();
ASI_BOOL bAuto;
long lVal;
ASIGetControlValue(ASICameraInfo.CameraID, ASI_FLIP, &lVal, &bAuto);
ImgFlip = (ASI_FLIP_STATUS)lVal;
ASI_IMG_TYPE imgType;
ASIGetROIFormat(ASICameraInfo.CameraID, &ImgWid, &ImgHei, &ImgBin, &imgType);
ASIGetStartPos(ASICameraInfo.CameraID, &ImgStartX, &ImgStartY);
}
return ret;
}
/**
* Required by the MM::Camera API
* Please implement this yourself and do not rely on the base class implementation
* The Base class implementation is deprecated and will be removed shortly
*/
int ASICamera::StartSequenceAcquisition(double interval) {
return StartSequenceAcquisition(LONG_MAX, interval, false);
}
/**
* Simple implementation of Sequence Acquisition
* A sequence acquisition should run on its own thread and transport new images
* coming of the camera into the MMCore circular buffer.
*/
int ASICamera::StartSequenceAcquisition(long numImages, double interval_ms, bool stopOnOverflow)
{
if (IsCapturing())
return DEVICE_CAMERA_BUSY_ACQUIRING;
OutputDbgPrint("StartCap\n");
ASIStartVideoCapture(ASICameraInfo.CameraID);
Status = capturing;
OutputDbgPrint("StartSeqAcq\n");
thd_->Start(numImages, interval_ms);//��ʼ�߳�
return DEVICE_OK;
}
/**
* Stop and wait for the Sequence thread finished
*/
int ASICamera::StopSequenceAcquisition()
{
if (!thd_->IsStopped())
{
thd_->Stop();//ֹͣ�߳�
OutputDbgPrint("StopSeqAcq bf wait\n");
// if(!thd_->IsStopped())
thd_->wait();//�ȴ��߳��˳�
OutputDbgPrint("StopSeqAcq af wait\n");
}
// if(Status == capturing)
// {
ASIStopVideoCapture(ASICameraInfo.CameraID);
Status = opened;
// }
return DEVICE_OK;
}
void ASICamera::Conv16RAWTo12RAW()
{
unsigned long line0;
// unsigned int *pBuf16 = (unsigned int*)uc_pImg;
#ifdef _WINDOWS
UINT16* pBuf16 = (UINT16*)uc_pImg;//unsigned short
#else
uint16_t* pBuf16 = (uint16_t*)uc_pImg;//unsigned short
#endif
for (int y = 0; y < iROIHeight; y++)
{
line0 = iROIWidth * y;
for (int x = 0; x < iROIWidth; x++)
{
pBuf16[line0 + x] /= 16;
}
}
}
void ASICamera::ConvRGB2RGBA32()
{
if (!pRGB32)
{
pRGB32 = new unsigned char[iROIWidth * iROIHeight * 4];
}
unsigned long index32, index24, line0;
for (int y = 0; y < iROIHeight; y++)
{
line0 = iROIWidth * y;
for (int x = 0; x < iROIWidth; x++)
{
index32 = (line0 + x) * 4;
index24 = (line0 + x) * 3;
pRGB32[index32 + 0] = uc_pImg[index24 + 0];
pRGB32[index32 + 1] = uc_pImg[index24 + 1];
pRGB32[index32 + 2] = uc_pImg[index24 + 2];
pRGB32[index32 + 3] = 0;
}
}
}
void ASICamera::ConvRGB2RGBA64()
{
if (!pRGB64)
{
pRGB64 = new unsigned char[iROIWidth * iROIHeight * 4 * 2];
memset(pRGB64, 0, iROIWidth * iROIHeight * 4 * 2);
}
unsigned long index64, index24, line0;
for (int y = 0; y < iROIHeight; y++)
{
line0 = iROIWidth * y;
for (int x = 0; x < iROIWidth; x++)
{
index64 = (line0 + x) * 8;
index24 = (line0 + x) * 3;
pRGB64[index64 + 1] = uc_pImg[index24 + 0];
pRGB64[index64 + 3] = uc_pImg[index24 + 1];
pRGB64[index64 + 5] = uc_pImg[index24 + 2];
// pRGB64[index64 + 6] = 0;
}
}
}
/**
* Returns pixel data.