gfx: Implement GP_FillCircleSeg()
[gfxprim.git] / demos / c_simple / v4l2_grab.c
blob6498fb82ae8f0155a96b7afa2ee9c1104e000e14
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Gets an image from v4l2 device.
29 #include <string.h>
30 #include <errno.h>
31 #include <stdio.h>
33 #include <GP.h>
35 static int get_image(const char *filename, GP_Grabber *grabber)
37 /* turn on grabber */
38 if (GP_GrabberStart(grabber)) {
39 fprintf(stderr, "Failed to start grabber\n");
40 return 1;
43 /* throw away first frame, it's usually wrong */
44 while (!GP_GrabberPoll(grabber))
45 usleep(100000);
47 while (!GP_GrabberPoll(grabber))
48 usleep(100000);
50 /* save image */
51 if (GP_SaveJPG(grabber->frame, filename, NULL)) {
52 fprintf(stderr, "Failed to save image '%s': %s",
53 filename, strerror(errno));
54 return 1;
57 /* turn off grabber */
58 if (GP_GrabberStop(grabber)) {
59 fprintf(stderr, "Failed to start grabber\n");
60 return 1;
63 return 0;
66 int main(int argc, char *argv[])
68 const char *v4l2_device = "/dev/video0";
69 const char *image_filename = "frame.jpg";
70 unsigned int w = 640, h = 480;
71 int secs = 0;
72 int opt;
74 while ((opt = getopt(argc, argv, "d:hH:o:W:l:s:")) != -1) {
75 switch (opt) {
76 case 'o':
77 image_filename = optarg;
78 break;
79 case 'd':
80 v4l2_device = optarg;
81 break;
82 case 'W':
83 w = atoi(optarg);
84 break;
85 case 'H':
86 h = atoi(optarg);
87 break;
88 case 's':
89 secs = atoi(optarg);
90 break;
91 case 'l':
92 GP_SetDebugLevel(atoi(optarg));
93 break;
94 case 'h':
95 printf("Usage; %s opts\n", argv[0]);
96 printf("-o output image file, default is 'frame.jpg'\n"
97 "-d v4l2 device name (default is '/dev/video0'\n"
98 "-W output image width, default is 640\n"
99 "-H output image height, default is 480\n"
100 "-l sets GFXprim debug level (default is 0)\n"
101 "-s take image every s seconds (the images are stored as frameX.jpg)\n"
102 "-h prints this help\n");
103 return 0;
104 break;
105 default:
106 fprintf(stderr, "Invalid paramter '%c'\n", opt);
107 return 1;
111 GP_Grabber *grabber = GP_GrabberV4L2Init(v4l2_device, w, h);
113 if (grabber == NULL) {
114 fprintf(stderr, "Failed to initalize grabber '%s': %s\n",
115 v4l2_device, strerror(errno));
116 return 1;
119 if (secs == 0) {
120 get_image(image_filename, grabber);
121 GP_GrabberExit(grabber);
122 return 0;
125 int i = 0;
127 for (;;) {
128 char buf[128];
130 snprintf(buf, sizeof(buf), "frame%03i.jpg", i++);
132 if (get_image(buf, grabber)) {
133 fprintf(stderr, "Failed to get image, exitting...\n");
134 return 1;
137 sleep(secs);
141 return 0;