filters: Cleanup and preparation for changes.
[gfxprim.git] / demos / c_simple / v4l2_show.c
blob681f30aec6ecb75149c6d518f117e9d06063811d
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 // GP_FilterEdgeSobel(img, &res, NULL, NULL);
112 break;
113 case 2:
114 GP_FilterGaussianBlur(img, img, 1, 1, NULL);
115 res = GP_FilterFloydSteinberg_RGB888_Alloc(img, GP_PIXEL_G2, NULL);
116 break;
119 GP_Blit_Clipped(res, 0, 0, res->w, res->h, backend->context, 0, 0);
120 GP_BackendFlip(backend);
122 if (mode)
123 GP_ContextFree(res);
126 usleep(1000);
128 GP_BackendPoll(backend);
130 /* Read and parse events */
131 GP_Event ev;
133 while (GP_EventGet(&ev)) {
134 switch (ev.type) {
135 case GP_EV_KEY:
137 /* ignore key up events */
138 if (!ev.code)
139 continue;
141 switch (ev.val.key.key) {
142 case GP_KEY_ESC:
143 case GP_KEY_Q:
144 GP_BackendExit(backend);
145 GP_GrabberExit(grabber);
146 return 0;
147 break;
148 case GP_KEY_SPACE:
150 mode++;
152 if (mode > 2)
153 mode = 0;
154 break;
160 GP_BackendExit(backend);
162 return 0;