grabbers: Introduce grabbers library and V4L2 grabber.
[gfxprim.git] / demos / c_simple / v4l2_show.c
blob077ba7b05d9852f115a5d2908ecff2226d28628e
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 Simple V4L2 grabber interactive example.
29 #include <errno.h>
30 #include <string.h>
32 #include <GP.h>
33 #include <backends/GP_X11.h>
35 int main(int argc, char *argv[])
37 GP_Backend *backend;
38 GP_Grabber *grabber;
39 const char *v4l2_device = "/dev/video0";
40 unsigned int w = 320, h = 240;
41 int mode = 0;
42 int opt;
44 /* Turn on debug messages */
45 //GP_SetDebugLevel(10);
47 while ((opt = getopt(argc, argv, "d:hH:W:l:")) != -1) {
48 switch (opt) {
49 case 'd':
50 v4l2_device = optarg;
51 break;
52 case 'W':
53 w = atoi(optarg);
54 break;
55 case 'H':
56 h = atoi(optarg);
57 break;
58 case 'l':
59 GP_SetDebugLevel(atoi(optarg));
60 break;
61 case 'h':
62 printf("Usage; %s opts\n", argv[0]);
63 printf("-d v4l2 device name (default is '/dev/video0'\n"
64 "-W output image width, default is 640\n"
65 "-H output image height, default is 480\n"
66 "-l sets GFXprim debug level (default is 0)\n"
67 "-h prints this help\n");
68 return 0;
69 break;
70 default:
71 fprintf(stderr, "Invalid paramter '%c'\n", opt);
72 return 1;
76 grabber = GP_GrabberV4L2Init(v4l2_device, w, h);
78 if (grabber == NULL) {
79 fprintf(stderr, "Failed to initalize grabber '%s': %s\n",
80 v4l2_device, strerror(errno));
81 return 1;
84 backend = GP_BackendX11Init(NULL, 0, 0, grabber->frame->w,
85 grabber->frame->h, "V4L2", 0);
87 if (backend == NULL) {
88 GP_GrabberExit(grabber);
89 return 1;
92 if (GP_GrabberStart(grabber)) {
93 fprintf(stderr, "Failed to start grabber\n");
94 GP_BackendExit(backend);
95 GP_GrabberExit(grabber);
96 return 1;
99 printf("Press SPACE to change mode and Q to exit.\n");
101 for (;;) {
102 if (GP_GrabberPoll(grabber) > 0) {
103 GP_Context *res, *img = grabber->frame;
105 switch (mode) {
106 case 0:
107 res = img;
108 break;
109 case 1:
110 GP_FilterEdgePrewitt(img, &res, NULL, NULL);
111 break;
112 case 2:
113 GP_FilterGaussianBlur(img, img, 1, 1, NULL);
114 res = GP_FilterFloydSteinberg_RGB888_Alloc(img, GP_PIXEL_G2, NULL);
115 break;
118 GP_Blit_Clipped(res, 0, 0, res->w, res->h, backend->context, 0, 0);
119 GP_BackendFlip(backend);
121 if (mode)
122 GP_ContextFree(res);
125 usleep(1000);
127 GP_BackendPoll(backend);
129 /* Read and parse events */
130 GP_Event ev;
132 while (GP_EventGet(&ev)) {
133 switch (ev.type) {
134 case GP_EV_KEY:
136 /* ignore key up events */
137 if (!ev.code)
138 continue;
140 switch (ev.val.key.key) {
141 case GP_KEY_ESC:
142 case GP_KEY_Q:
143 GP_BackendExit(backend);
144 GP_GrabberExit(grabber);
145 return 0;
146 break;
147 case GP_KEY_SPACE:
149 mode++;
151 if (mode > 2)
152 mode = 0;
153 break;
159 GP_BackendExit(backend);
161 return 0;