filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / demos / c_simple / v4l2_show.c
blob6c5b0163ff49d1f8cbebbb9850f15ae1d3ed810e
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-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple V4L2 grabber interactive example.
29 #include <errno.h>
30 #include <string.h>
31 #include <stdio.h>
33 #include <gfxprim.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 = 640, h = 480;
41 int mode = 0;
42 int opt;
44 while ((opt = getopt(argc, argv, "d:hH:W:l:")) != -1) {
45 switch (opt) {
46 case 'd':
47 v4l2_device = optarg;
48 break;
49 case 'W':
50 w = atoi(optarg);
51 break;
52 case 'H':
53 h = atoi(optarg);
54 break;
55 case 'l':
56 gp_set_debug_level(atoi(optarg));
57 break;
58 case 'h':
59 printf("Usage; %s opts\n", argv[0]);
60 printf("-d v4l2 device name (default is '/dev/video0'\n"
61 "-W output image width, default is 640\n"
62 "-H output image height, default is 480\n"
63 "-l sets GFXprim debug level (default is 0)\n"
64 "-h prints this help\n");
65 return 0;
66 break;
67 default:
68 fprintf(stderr, "Invalid paramter '%c'\n", opt);
69 return 1;
73 grabber = gp_grabber_v4l2_init(v4l2_device, w, h);
75 if (grabber == NULL) {
76 fprintf(stderr, "Failed to initalize grabber '%s': %s\n",
77 v4l2_device, strerror(errno));
78 return 1;
81 backend = gp_x11_init(NULL, 0, 0, grabber->frame->w,
82 grabber->frame->h, "V4L2", 0);
84 if (backend == NULL) {
85 gp_grabber_exit(grabber);
86 return 1;
89 if (gp_grabber_start(grabber)) {
90 fprintf(stderr, "Failed to start grabber\n");
91 gp_backend_exit(backend);
92 gp_grabber_exit(grabber);
93 return 1;
96 printf("Press SPACE to change mode and Q to exit.\n");
98 for (;;) {
99 if (gp_grabber_poll(grabber) > 0) {
100 gp_pixmap *res, *img = grabber->frame;
102 switch (mode) {
103 case 0:
104 res = img;
105 break;
106 case 1:
107 // gp_filter_edge_prewitt(img, &res, NULL, NULL);
108 gp_filter_edge_sobel(img, &res, NULL, NULL);
109 break;
110 case 2:
111 gp_filter_gaussian_blur(img, img, 1, 1, NULL);
112 res = gp_filter_floyd_steinberg_alloc(img, GP_PIXEL_G2, NULL);
113 break;
116 unsigned int c_x = (backend->pixmap->w - res->w) / 2;
117 unsigned int c_y = (backend->pixmap->h - res->h) / 2;
119 gp_blit_clipped(res, 0, 0, res->w, res->h, backend->pixmap, c_x, c_y);
120 gp_backend_flip(backend);
122 if (mode)
123 gp_pixmap_free(res);
126 usleep(1000);
128 gp_backend_poll(backend);
130 /* Read and parse events */
131 gp_event ev;
133 while (gp_backend_get_event(backend, &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_backend_exit(backend);
145 gp_grabber_exit(grabber);
146 return 0;
147 break;
148 case GP_KEY_SPACE:
150 mode++;
152 if (mode > 2)
153 mode = 0;
154 break;
156 break;
157 case GP_EV_SYS:
158 if (ev.code == GP_EV_SYS_RESIZE) {
159 gp_backend_resize_ack(backend);
160 gp_fill(backend->pixmap, 0);
162 break;
167 gp_backend_exit(backend);
169 return 0;