1 /*
2  * Grab partial images from camera. Camera must be format 7
3  *    (scalable image size) compatible. The images are written
4  *    in a PVN file
5  *
6  * Written by Damien Douxchamps <ddouxchamps@users.sf.net>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <dc1394/dc1394.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <inttypes.h>
29 #include <unistd.h>
30 
31 #ifdef _WIN32
32 #define times 0**(int*)
33 struct tms {int a;};
34 #else
35 #include <sys/times.h>
36 #endif
37 
38 #define WIDTH  1280
39 #define HEIGHT 1024
40 
main(int argn,char * argv[])41 int main(int argn, char *argv[])
42 {
43     FILE* imagefile;
44     dc1394camera_t *camera;
45     int i;
46     unsigned int width, height;
47     dc1394video_frame_t *frame=NULL;
48     dc1394_t * d;
49     dc1394camera_list_t * list;
50     dc1394error_t err;
51 
52     int nimages;
53 
54     if (argn<2) {
55         fprintf(stderr,"Usage:\n grab_partial_pvn <number of images>\n");
56         exit(1);
57     }
58 
59     nimages=atoi(argv[1]);
60 
61     d = dc1394_new ();
62     if (!d)
63         return 1;
64     err=dc1394_camera_enumerate (d, &list);
65     DC1394_ERR_RTN(err,"Failed to enumerate cameras");
66 
67     if (list->num == 0) {
68         dc1394_log_error("No cameras found");
69         return 1;
70     }
71 
72     camera = dc1394_camera_new (d, list->ids[0].guid);
73     if (!camera) {
74         dc1394_log_error("Failed to initialize camera with guid %"PRIx64,
75                 list->ids[0].guid);
76         return 1;
77     }
78     dc1394_camera_free_list (list);
79 
80     printf("Using camera with GUID %"PRIx64"\n", camera->guid);
81 
82     /*-----------------------------------------------------------------------
83      *  setup capture for format 7
84      *-----------------------------------------------------------------------*/
85 
86     dc1394_video_set_iso_speed(camera, DC1394_ISO_SPEED_400);
87     dc1394_video_set_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0);
88     err = dc1394_format7_set_roi(camera, DC1394_VIDEO_MODE_FORMAT7_0,
89                                  DC1394_COLOR_CODING_MONO8,
90                                  DC1394_USE_MAX_AVAIL, // use max packet size
91                                  0, 0, // left, top
92                                  WIDTH, HEIGHT);  // width, height
93     DC1394_ERR_RTN(err,"Unable to set Format7 mode 0.\nEdit the example file manually to fit your camera capabilities");
94 
95     err=dc1394_capture_setup(camera, 4, DC1394_CAPTURE_FLAGS_DEFAULT);
96     DC1394_ERR_CLN_RTN(err, dc1394_camera_free(camera), "Error capturing");
97 
98     /*-----------------------------------------------------------------------
99      *  have the camera start sending us data
100      *-----------------------------------------------------------------------*/
101     err=dc1394_video_set_transmission(camera,DC1394_ON);
102     if (err!=DC1394_SUCCESS) {
103         dc1394_log_error("unable to start camera iso transmission");
104         dc1394_capture_stop(camera);
105         dc1394_camera_free(camera);
106         exit(1);
107     }
108 
109     dc1394_get_image_size_from_video_mode(camera, DC1394_VIDEO_MODE_FORMAT7_0, &width, &height);
110 
111     //sleep(10);
112 
113     imagefile=fopen(argv[1],"wb");
114 
115     fprintf(imagefile,"PV5a\nWIDTH: %d\nHEIGHT: %d\nMAXVAL: 8\nFRAMERATE: 10\n",WIDTH,HEIGHT);
116 
117     for(i=0; i<nimages; ++i) {
118       //int k;
119       //for (k=0;k<4;k++) { // empty buffer
120       //  err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
121       //  dc1394_capture_enqueue(camera,frame);
122       //}
123       /*-----------------------------------------------------------------------
124        *  capture one frame
125        *-----------------------------------------------------------------------*/
126       err=dc1394_capture_dequeue(camera, DC1394_CAPTURE_POLICY_WAIT, &frame);
127       if (err!=DC1394_SUCCESS) {
128 	dc1394_log_error("unable to capture");
129 	dc1394_capture_stop(camera);
130 	dc1394_camera_free(camera);
131 	exit(1);
132       }
133 
134       fwrite(frame->image, 1, height * width, imagefile);
135 
136       // release buffer
137       dc1394_capture_enqueue(camera,frame);
138       fprintf(stderr,"Got frame %d\n",i);
139     }
140 
141       fclose(imagefile);
142     /*-----------------------------------------------------------------------
143      *  stop data transmission
144      *-----------------------------------------------------------------------*/
145     err=dc1394_video_set_transmission(camera,DC1394_OFF);
146     DC1394_ERR_RTN(err,"couldn't stop the camera?");
147 
148     /*-----------------------------------------------------------------------
149      *  close camera, cleanup
150      *-----------------------------------------------------------------------*/
151     dc1394_capture_stop(camera);
152     dc1394_video_set_transmission(camera, DC1394_OFF);
153     dc1394_camera_free(camera);
154     dc1394_free (d);
155     return 0;
156 }
157