forked from saltastro/timDIMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrab_cube_8bpp_binned.c
280 lines (239 loc) · 10.4 KB
/
grab_cube_8bpp_binned.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
/*
* Grab partial image from camera. Camera must be format 7
* (scalable image size) compatible.
*
* Written by Damien Douxchamps <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdint.h>
#include <dc1394/dc1394.h>
#include <stdlib.h>
#include <time.h>
#include <inttypes.h>
#include <string.h>
#include <fitsio.h>
#include <xpa.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
fitsfile *fptr;
long fpixel=1, naxes[3];
dc1394camera_t *camera;
int grab_n_frames;
struct timeval start_time, end_time;
time_t start_sec, end_sec;
suseconds_t start_usec, end_usec;
float elapsed_time, fps;
int i, status = 0;
unsigned int min_bytes, max_bytes, max_height, max_width;
unsigned int actual_bytes;
uint64_t total_bytes = 0;
unsigned int width, height;
dc1394video_frame_t *frame=NULL;
dc1394_t * d;
dc1394camera_list_t * list;
dc1394error_t err;
double exptime;
char *filename;
if (argc != 4) {
printf("Usage: grab_cube_8bpp_binned <nframes> <FITS filename> <exp time (ms)>\n");
exit(0);
}
grab_n_frames = atoi(argv[1]);
filename = argv[2];
exptime = 1.0e-3*atof(argv[3]);
fps = 1.0/exptime;
if (fps > 330.0) {
fps = 330.0;
}
width = 320;
height = 240;
naxes[0] = width;
naxes[1] = height;
naxes[2] = grab_n_frames;
stderr = freopen("grab_cube.log", "w", stderr);
d = dc1394_new ();
if (!d)
return 1;
err = dc1394_camera_enumerate(d, &list);
DC1394_ERR_RTN(err,"Failed to enumerate cameras");
if (list->num == 0) {
dc1394_log_error("No cameras found");
return 1;
}
camera = dc1394_camera_new(d, list->ids[0].guid);
if (!camera) {
dc1394_log_error("Failed to initialize camera with guid %"PRIx64,
list->ids[0].guid);
return 1;
}
dc1394_camera_free_list(list);
printf("Using camera with GUID %"PRIx64"\n", camera->guid);
/*-----------------------------------------------------------------------
* setup capture for format 7
*-----------------------------------------------------------------------*/
// libdc1394 doesn't work well with firewire800 yet so set to legacy 400 mode
dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
// configure camera for format7
err = dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot choose format7_0");
err = dc1394_format7_get_max_image_size(camera, DC1394_VIDEO_MODE_FORMAT7_1,
&max_width, &max_height);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera),
"cannot get max image size for format7_0");
printf ("I: current image size is: height = %d, width = %d\n", height, width);
err = dc1394_format7_set_roi(camera,
DC1394_VIDEO_MODE_FORMAT7_1,
DC1394_COLOR_CODING_MONO8,
DC1394_USE_MAX_AVAIL,
0, 0, // left, top
width, height); // width, height
DC1394_ERR_CLN_RTN(err,dc1394_camera_free (camera),"cannot set roi");
// set the frame rate to absolute value in frames/sec
err = dc1394_feature_set_mode(camera, DC1394_FEATURE_FRAME_RATE,
DC1394_FEATURE_MODE_MANUAL);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set framerate to manual");
err = dc1394_feature_set_absolute_control(camera,
DC1394_FEATURE_FRAME_RATE,
DC1394_TRUE);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera),
"cannot set framerate to absolute mode");
err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_FRAME_RATE, fps);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set framerate");
printf("I: framerate is %f fps\n", fps);
// set the shutter speed to absolute value in seconds
err = dc1394_feature_set_mode(camera, DC1394_FEATURE_SHUTTER,
DC1394_FEATURE_MODE_MANUAL);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set shutter to manual");
err = dc1394_feature_set_absolute_control(camera,
DC1394_FEATURE_SHUTTER, DC1394_TRUE);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera),
"cannot set shutter to absolute mode");
err = dc1394_feature_set_absolute_value(camera, DC1394_FEATURE_SHUTTER, exptime);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set shutter");
printf("I: exptime is %f s\n", exptime);
// set gain manually. use relative value here in range 48 to 730.
err = dc1394_feature_set_mode(camera,
DC1394_FEATURE_GAIN, DC1394_FEATURE_MODE_MANUAL);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set gain to manual");
err = dc1394_feature_set_value(camera, DC1394_FEATURE_GAIN, 400);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set gain");
// set brightness manually. use relative value in range 0 to 1023.
err = dc1394_feature_set_mode(camera, DC1394_FEATURE_BRIGHTNESS,
DC1394_FEATURE_MODE_MANUAL);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera),
"cannot set brightness to manual");
err = dc1394_feature_set_value(camera, DC1394_FEATURE_BRIGHTNESS, 100);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot set brightness");
err = dc1394_format7_get_total_bytes (camera, DC1394_VIDEO_MODE_FORMAT7_1,
&total_bytes);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "cannot get total bytes");
err = dc1394_capture_setup(camera, 16, DC1394_CAPTURE_FLAGS_DEFAULT);
DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing");
/*-----------------------------------------------------------------------
* print allowed and used packet size
*-----------------------------------------------------------------------*/
err = dc1394_format7_get_packet_parameters(camera,
DC1394_VIDEO_MODE_FORMAT7_1,
&min_bytes, &max_bytes);
DC1394_ERR_RTN(err,"Packet para inq error");
err = dc1394_format7_get_packet_size(camera,
DC1394_VIDEO_MODE_FORMAT7_1, &actual_bytes);
DC1394_ERR_RTN(err,"dc1394_format7_get_packet_size error");
err = dc1394_format7_get_total_bytes(camera,
DC1394_VIDEO_MODE_FORMAT7_1, &total_bytes);
DC1394_ERR_RTN(err,"dc1394_query_format7_total_bytes error");
/*-----------------------------------------------------------------------
* have the camera start sending us data
*-----------------------------------------------------------------------*/
err = dc1394_video_set_transmission(camera, DC1394_ON);
if (err != DC1394_SUCCESS) {
dc1394_log_error("unable to start camera iso transmission");
dc1394_capture_stop(camera);
dc1394_camera_free(camera);
exit(1);
}
// set up FITS image and capture
fits_create_file(&fptr, filename, &status);
dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_1,
&width, &height);
fits_create_img(fptr, BYTE_IMG, 3, naxes, &status);
/*-----------------------------------------------------------------------
* capture frames and measure the time for this operation
*-----------------------------------------------------------------------*/
gettimeofday(&start_time, NULL);
printf("Start capture:\n");
for( i = 0; i < grab_n_frames; ++i) {
/*-----------------------------------------------------------------------
* capture one frame
*-----------------------------------------------------------------------*/
err = dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
if (err != DC1394_SUCCESS) {
dc1394_log_error("unable to capture");
dc1394_capture_stop(camera);
dc1394_camera_free(camera);
exit(1);
}
// attempt to preallocate image array and write to memory before dumping to disk.
// turns out to be slow due to large size of images. cfitsio buffering is far
// more efficient.
//memcpy(im_buffer+2*i*naxes[0]*naxes[1],
//frame->image-1,
//naxes[0]*naxes[1]*sizeof(short));
// just writing each frame to the FITS file goes pretty fast
fits_write_img(fptr,
TBYTE,
fpixel+i*naxes[0]*naxes[1],
naxes[0]*naxes[1],
frame->image-1,
&status);
// release buffer
dc1394_capture_enqueue(camera,frame);
}
gettimeofday(&end_time, NULL);
printf("End capture.\n");
/*-----------------------------------------------------------------------
* stop data transmission
*-----------------------------------------------------------------------*/
start_sec = start_time.tv_sec;
start_usec = start_time.tv_usec;
end_sec = end_time.tv_sec;
end_usec = end_time.tv_usec;
elapsed_time = (float)((end_sec + 1.0e-6*end_usec) - (start_sec + 1.0e-6*start_usec));
fps = grab_n_frames/elapsed_time;
printf("Elapsed time = %g seconds.\n", elapsed_time);
printf("Framerate = %g fps.\n", fps);
err = dc1394_video_set_transmission(camera, DC1394_OFF);
DC1394_ERR_RTN(err,"couldn't stop the camera?");
/*-----------------------------------------------------------------------
* save FITS image to disk
*-----------------------------------------------------------------------*/
//fits_write_img(fptr, TBYTE, fpixel, naxes[0]*naxes[1]*naxes[2], im_buffer, &status);
fits_close_file(fptr, &status);
fits_report_error(stderr, status);
//free(im_buffer);
printf("wrote: %s\n", filename);
printf("Image is %d bits/pixel.\n", frame->data_depth);
/*-----------------------------------------------------------------------
* close camera, cleanup
*-----------------------------------------------------------------------*/
dc1394_capture_stop(camera);
dc1394_video_set_transmission(camera, DC1394_OFF);
dc1394_camera_free(camera);
dc1394_free(d);
return 0;
}