-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoolSNAP.c
357 lines (260 loc) · 7.18 KB
/
CoolSNAP.c
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
//system inclusion//
#include <windows.h>
#include "ansi_c.h"
//SDK inclusion//
#include "master.h"
#include "pvcam.h"
//CVI interface GUI stuff//
//#include "dataskt.h"
//#include <userint.h>
//#include <ansi_c.h>
//#include <utility.h>
//other codes//
#include "CoolSNAP.h"
//definitions//
#define DATETIME_FORMATSTRING "%Y%d%j_%H%M"
#define DATETIME_BUFFER 16
extern float LaserDiodeTemp;
//extern float PiezoVoltage;
//extern float DiodeCurrent;
//extern double STEP;
int16 Camera;
uns16 *ImageBuffer,
*BackgroundBuffer;
uns32 BufferSize;
uns16 CCDpixelser,
CCDpixelpar;
CCDgainindex;
CCDpixtime;
Spd_Tab_Index;
ExpTimeUnits;
rgn_type Region;
int xSize,
ySize;
unsigned char *BitMap;
int ColorTable[256];
//int I_TOL;
//int HoldOn;
int BackgroundSubtractFlag = 0;
uns32 ExpTime;
unsigned long int HistogramData[4096];
double HistogramData_Log[4096];
double HistogramPlotData[4096];
FILE *ImageFile;
char filename[260];//char filename[MAX_PATHNAME_LEN];uses utility.h
void SetDefault(void)
{
ExpTime = 1000;
BackgroundSubtractFlag = FALSE;
}
void InitializeCamera(void)
{
char CameraName[CAM_NAME_LEN];
SetDefault();
pl_pvcam_init();
pl_cam_get_name(0, CameraName);
pl_cam_open(CameraName, &Camera, OPEN_EXCLUSIVE);
printf("CameraName = %s\n", CameraName);
printf("Camera = %d\n", Camera);
Spd_Tab_Index = 2;
pl_set_param(Camera, PARAM_SPDTAB_INDEX, &Spd_Tab_Index); //10Mhz readout mode Readout Port 2
pl_get_param(Camera, PARAM_SER_SIZE, ATTR_CURRENT, &CCDpixelser); //serial dimension of active area
pl_get_param(Camera, PARAM_PAR_SIZE, ATTR_CURRENT, &CCDpixelpar); //parallel dimension of active area
pl_get_param(Camera, PARAM_GAIN_INDEX, ATTR_CURRENT, &CCDgainindex); //gain setting
pl_get_param(Camera, PARAM_PIX_TIME, ATTR_CURRENT, &CCDpixtime); //actual speed in ns
printf("\n");
printf("CCDpixelser = %d\n", CCDpixelser);
printf("CCDpixelpar = %d\n", CCDpixelpar);
printf("CCDgainindex = %d\n", CCDgainindex);
printf("CCDpixtime = %d\n", CCDpixtime);
Region.s1 = 0;
Region.s2 = CCDpixelser - 1;
Region.sbin = 1;
Region.p1 = 0;
Region.p2 = CCDpixelpar - 1;
Region.pbin = 1;
pl_get_param(Camera, PARAM_EXP_RES, ATTR_CURRENT, &ExpTimeUnits); //resolution
printf("ExptimeUnits = %d\n", ExpTimeUnits);
if (ExpTimeUnits == EXP_RES_ONE_MILLISEC)
printf("Milliseconds!!\n");
if (ExpTimeUnits == EXP_RES_ONE_MICROSEC)
printf("Microseconds!!\n");
pl_exp_init_seq(); // prepare to acquire and readout
pl_exp_setup_seq(Camera, 1, 1, &Region, TIMED_MODE, ExpTime, &BufferSize); // acquire in sequential mode
printf("BufferSize = %ld\n", BufferSize);
ImageBuffer = (uns16*)malloc(BufferSize);
BackgroundBuffer = (uns16*)malloc(BufferSize);
//HistogramData = malloc(4096*sizeof(unsigned long int));
//HistogramPlotData = malloc(4096*sizeof(double));
//HistogramData_Log = malloc(4096*sizeof(double));
AllocateBitMap();
// Acquire noise, don't use the flowing in case of normal operation
/* ExpTime = 10000;
();
sprintf(filename,"C:\\jason\\data\\speckle\\030625\\mode1\\bg10s2.spk");
ImageFile = fopen(filename,"wb");
fwrite(ImageBuffer,sizeof(uns16),CCDpixelser*CCDpixelpar,ImageFile);
fclose(ImageFile);
*/
}
void AllocateBitMap(void)
{
long BitMapSize;
int i;
//int CVIerr;
xSize = CCDpixelser / 2; // Displayed images only have 1/4 of the pixels
ySize = CCDpixelpar / 2; // of the CCD due to screen resolution limitation
printf("xSize = %d, ySize = %d\n", xSize, ySize);
BitMapSize = xSize*ySize;
BitMap = malloc(BitMapSize*sizeof(unsigned char));
for (i = 0; i <256; i++) {
ColorTable[i] = i + (i << 8) + (i << 16);
}
}
void CleanUpCamera(void)
{
pl_cam_close(Camera);
pl_pvcam_uninit();
free(ImageBuffer);
free(BitMap);
}
int GetExposureTime(void) {
return ExpTime;
}
void AcquireImage(void)
{
int16 Status;
uns32 NotUsed;
pl_exp_init_seq();
pl_exp_setup_seq(Camera, 1, 1, &Region, TIMED_MODE, ExpTime, &BufferSize);
printf(".");
pl_exp_start_seq(Camera, ImageBuffer);
while (pl_exp_check_status(Camera, &Status, &NotUsed) &&
(Status != READOUT_COMPLETE && Status != READOUT_FAILED)) {
Sleep(50);
}
// monitor status
// pl_exp_finish_seq(Camera,Buffer,0) // un-needed? p66
pl_exp_uninit_seq();
if (BackgroundSubtractFlag) {
SubtractBackground();
}
}
void SubtractBackground(void)
{
unsigned long int i;
for (i = 0; i < CCDpixelser*CCDpixelpar; i++) {
if (ImageBuffer[i] > BackgroundBuffer[i])
ImageBuffer[i] -= BackgroundBuffer[i];
else
ImageBuffer[i] = 0;
}
}
void SaveBackgroundImage(char * filename) {
SaveImageFile(filename, 0);
}
void SaveImage(char * filename) {
SaveImageFile(filename, 1);
}
void SaveImageFile(char *filename, int image)
{
FILE *ImageFile;
uns16* img;
if (image) {
img = ImageBuffer;
}
else {
img = BackgroundBuffer;
}
ImageFile = fopen(filename, "wb");
fwrite(img, sizeof(uns16), CCDpixelser*CCDpixelpar, ImageFile);
fwrite(&LaserDiodeTemp, sizeof(float), 1, ImageFile);
fclose(ImageFile);
}
void LoadImageFile(char *filename)
{
FILE *ImageFile;
ImageFile = fopen(filename, "rb");
fread(ImageBuffer, sizeof(uns16), CCDpixelser*CCDpixelpar, ImageFile);
fclose(ImageFile);
}
void AcquireBackground(void)
{
unsigned long int i;
//SetLaserOutput(0);
//Sleep(5*1000);
AcquireImage();
for (i = 0; i<CCDpixelser*CCDpixelpar; i++) {
BackgroundBuffer[i] = ImageBuffer[i];
}
AcquireImage();
for (i = 0; i<CCDpixelser*CCDpixelpar; i++) {
BackgroundBuffer[i] += ImageBuffer[i];
}
for (i = 0; i<CCDpixelser*CCDpixelpar; i++) {
BackgroundBuffer[i] /= 2;
}
CalcHistogram();
//SetLaserOutput(1);
//Sleep(5*1000);
}
void CalcHistogram(void)
{
unsigned long int Limit,
i;
unsigned int mu = 0;
double mu_2;
// if (ADCResolution == 12)
Limit = 4096;
// else
// Limit = 65536;
for (i = 0; i < Limit; i++) {
HistogramData[i] = 0;
}
for (i = 0; i < CCDpixelser*CCDpixelpar; i++) {
HistogramData[ImageBuffer[i]]++;
mu += ImageBuffer[i];
}
mu_2 = ((double)mu) / (CCDpixelser*CCDpixelpar);
}
double CalcMean(void) {
unsigned long int i;
double mu;
mu = 0;
for (i = 0; i < CCDpixelser*CCDpixelpar; i++) {
mu += ImageBuffer[i];
}
return mu / (CCDpixelser * CCDpixelpar);
}
int GetCCDCurrentTemp(void){
int16 CCDCurrentTemperature;
pl_get_param(Camera, PARAM_TEMP, ATTR_CURRENT, &CCDCurrentTemperature);
return CCDCurrentTemperature;
}
void EstimateNoise(void)
{
char DIR[256], Filename[256], ext[16];
int i;
clock_t t0, t1;
sprintf(DIR, "D:\\jason\\data\\temp\\bg.");
sprintf(ext, "0a");
ExpTime = 1;
t0 = clock();
for (i = 1; i <= 5; i++)
{
sprintf(Filename, DIR);
ext[0]++;
strcat(Filename, ext);
AcquireImage();
SaveImageFile(Filename, 1);
ext[1]++;
sprintf(Filename, DIR);
strcat(Filename, ext);
AcquireImage();
SaveImageFile(Filename, 1);
ext[1]--;
ExpTime = ExpTime * 10;
}
t1 = clock();
printf("%d secs elipses.", (int)difftime(t1, t0) / 1000);
}