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
static int get_brightness_adj(unsigned char *image, long size, int *brightness)
86
for (i=0;i<size*3;i++)
88
*brightness = (128 - tot/(size*3))/3;
89
return !((tot/(size*3)) >= 126 && (tot/(size*3)) <= 130);
92
int main(int argc, char **argv)
94
#ifdef CONFIG_VIDEO_V4L1_COMPAT
95
int fd = open(FILE, O_RDONLY), f;
96
struct video_capability cap;
97
struct video_window win;
98
struct video_picture vpic;
100
unsigned char *buffer, *src;
101
int bpp = 24, r, g, b;
102
unsigned int i, src_depth;
109
if (ioctl(fd, VIDIOCGCAP, &cap) < 0) {
111
fprintf(stderr, "(" FILE " not a video4linux device?)\n");
116
if (ioctl(fd, VIDIOCGWIN, &win) < 0) {
117
perror("VIDIOCGWIN");
122
if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) {
123
perror("VIDIOCGPICT");
128
if (cap.type & VID_TYPE_MONOCHROME) {
130
vpic.palette=VIDEO_PALETTE_GREY; /* 8bit grey */
131
if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
133
if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
135
if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
136
fprintf(stderr, "Unable to find a supported capture format.\n");
145
vpic.palette=VIDEO_PALETTE_RGB24;
147
if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
148
vpic.palette=VIDEO_PALETTE_RGB565;
151
if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
152
vpic.palette=VIDEO_PALETTE_RGB555;
155
if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
156
fprintf(stderr, "Unable to find a supported capture format.\n");
163
buffer = malloc(win.width * win.height * bpp);
165
fprintf(stderr, "Out of memory.\n");
171
read(fd, buffer, win.width * win.height * bpp);
172
f = get_brightness_adj(buffer, win.width * win.height, &newbright);
174
vpic.brightness += (newbright << 8);
175
if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
176
perror("VIDIOSPICT");
182
fprintf(stdout, "P6\n%d %d 255\n", win.width, win.height);
186
for (i = 0; i < win.width * win.height; i++) {
187
READ_VIDEO_PIXEL(src, vpic.palette, src_depth, r, g, b);
195
fprintf(stderr, "V4L1 API is not configured!\n");