1 /* Simple Video4Linux image grabber. */
3 * Video4Linux Driver Test/Example Framegrabbing Program
6 * gcc -s -Wall -Wstrict-prototypes v4lgrab.c -o v4lgrab
10 * Copyright (C) 1998-05-03, Phil Blundell <philb@gnu.org>
11 * Copied from http://www.tazenda.demon.co.uk/phil/vgrabber.c
12 * with minor modifications (Dave Forrest, drf5n@virginia.edu).
17 #include <sys/types.h>
21 #include <sys/ioctl.h>
24 #include <linux/types.h>
25 #include <linux/videodev.h>
27 #define FILE "/dev/video0"
29 /* Stole this from tvset.c */
31 #define READ_VIDEO_PIXEL(buf, format, depth, r, g, b) \
35 case VIDEO_PALETTE_GREY: \
41 (r) = (g) = (b) = (*buf++ << 8);\
46 *((unsigned short *) buf); \
53 case VIDEO_PALETTE_RGB565: \
55 unsigned short tmp = *(unsigned short *)buf; \
57 (g) = (tmp<<5)&0xFC00; \
58 (b) = (tmp<<11)&0xF800; \
63 case VIDEO_PALETTE_RGB555: \
64 (r) = (buf[0]&0xF8)<<8; \
65 (g) = ((buf[0] << 5 | buf[1] >> 3)&0xF8)<<8; \
66 (b) = ((buf[1] << 2 ) & 0xF8)<<8; \
70 case VIDEO_PALETTE_RGB24: \
71 (r) = buf[0] << 8; (g) = buf[1] << 8; \
78 "Format %d not yet supported\n", \
83 int get_brightness_adj(unsigned char *image
, long size
, int *brightness
) {
85 for (i
=0;i
<size
*3;i
++)
87 *brightness
= (128 - tot
/(size
*3))/3;
88 return !((tot
/(size
*3)) >= 126 && (tot
/(size
*3)) <= 130);
91 int main(int argc
, char ** argv
)
93 int fd
= open(FILE, O_RDONLY
), f
;
94 struct video_capability cap
;
95 struct video_window win
;
96 struct video_picture vpic
;
98 unsigned char *buffer
, *src
;
99 int bpp
= 24, r
, g
, b
;
100 unsigned int i
, src_depth
;
107 if (ioctl(fd
, VIDIOCGCAP
, &cap
) < 0) {
109 fprintf(stderr
, "(" FILE " not a video4linux device?)\n");
114 if (ioctl(fd
, VIDIOCGWIN
, &win
) < 0) {
115 perror("VIDIOCGWIN");
120 if (ioctl(fd
, VIDIOCGPICT
, &vpic
) < 0) {
121 perror("VIDIOCGPICT");
126 if (cap
.type
& VID_TYPE_MONOCHROME
) {
128 vpic
.palette
=VIDEO_PALETTE_GREY
; /* 8bit grey */
129 if(ioctl(fd
, VIDIOCSPICT
, &vpic
) < 0) {
131 if(ioctl(fd
, VIDIOCSPICT
, &vpic
) < 0) {
133 if(ioctl(fd
, VIDIOCSPICT
, &vpic
) < 0) {
134 fprintf(stderr
, "Unable to find a supported capture format.\n");
142 vpic
.palette
=VIDEO_PALETTE_RGB24
;
144 if(ioctl(fd
, VIDIOCSPICT
, &vpic
) < 0) {
145 vpic
.palette
=VIDEO_PALETTE_RGB565
;
148 if(ioctl(fd
, VIDIOCSPICT
, &vpic
)==-1) {
149 vpic
.palette
=VIDEO_PALETTE_RGB555
;
152 if(ioctl(fd
, VIDIOCSPICT
, &vpic
)==-1) {
153 fprintf(stderr
, "Unable to find a supported capture format.\n");
160 buffer
= malloc(win
.width
* win
.height
* bpp
);
162 fprintf(stderr
, "Out of memory.\n");
168 read(fd
, buffer
, win
.width
* win
.height
* bpp
);
169 f
= get_brightness_adj(buffer
, win
.width
* win
.height
, &newbright
);
171 vpic
.brightness
+= (newbright
<< 8);
172 if(ioctl(fd
, VIDIOCSPICT
, &vpic
)==-1) {
173 perror("VIDIOSPICT");
179 fprintf(stdout
, "P6\n%d %d 255\n", win
.width
, win
.height
);
183 for (i
= 0; i
< win
.width
* win
.height
; i
++) {
184 READ_VIDEO_PIXEL(src
, vpic
.palette
, src_depth
, r
, g
, b
);