~ubuntu-branches/ubuntu/precise/v4l-utils/precise

« back to all changes in this revision

Viewing changes to contrib/test/v4lgrab.c

  • Committer: Bazaar Package Importer
  • Author(s): Gregor Jasny
  • Date: 2010-02-28 19:44:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100228194415-067hdj8rvawj91zw
Tags: upstream-0.7.90
ImportĀ upstreamĀ versionĀ 0.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Simple Video4Linux image grabber. */
 
2
/*
 
3
 *      Video4Linux Driver Test/Example Framegrabbing Program
 
4
 *
 
5
 *      Compile with:
 
6
 *              gcc -s -Wall -Wstrict-prototypes v4lgrab.c -o v4lgrab
 
7
 *      Use as:
 
8
 *              v4lgrab >image.ppm
 
9
 *
 
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).
 
13
 *
 
14
 */
 
15
 
 
16
#include <unistd.h>
 
17
#include <sys/types.h>
 
18
#include <sys/stat.h>
 
19
#include <fcntl.h>
 
20
#include <stdio.h>
 
21
#include <sys/ioctl.h>
 
22
#include <stdlib.h>
 
23
 
 
24
#include <linux/types.h>
 
25
#include <linux/videodev.h>
 
26
 
 
27
#define FILE "/dev/video0"
 
28
 
 
29
/* Stole this from tvset.c */
 
30
 
 
31
#define READ_VIDEO_PIXEL(buf, format, depth, r, g, b)                   \
 
32
{                                                                       \
 
33
        switch (format)                                                 \
 
34
        {                                                               \
 
35
                case VIDEO_PALETTE_GREY:                                \
 
36
                        switch (depth)                                  \
 
37
                        {                                               \
 
38
                                case 4:                                 \
 
39
                                case 6:                                 \
 
40
                                case 8:                                 \
 
41
                                        (r) = (g) = (b) = (*buf++ << 8);\
 
42
                                        break;                          \
 
43
                                                                        \
 
44
                                case 16:                                \
 
45
                                        (r) = (g) = (b) =               \
 
46
                                        *((unsigned short *) buf);      \
 
47
                                        buf += 2;                       \
 
48
                                        break;                          \
 
49
                        }                                               \
 
50
                        break;                                          \
 
51
                                                                        \
 
52
                                                                        \
 
53
                case VIDEO_PALETTE_RGB565:                              \
 
54
                {                                                       \
 
55
                        unsigned short tmp = *(unsigned short *)buf;    \
 
56
                        (r) = tmp&0xF800;                               \
 
57
                        (g) = (tmp<<5)&0xFC00;                          \
 
58
                        (b) = (tmp<<11)&0xF800;                         \
 
59
                        buf += 2;                                       \
 
60
                }                                                       \
 
61
                break;                                                  \
 
62
                                                                        \
 
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;               \
 
67
                        buf += 2;                                       \
 
68
                        break;                                          \
 
69
                                                                        \
 
70
                case VIDEO_PALETTE_RGB24:                               \
 
71
                        (r) = buf[0] << 8; (g) = buf[1] << 8;           \
 
72
                        (b) = buf[2] << 8;                              \
 
73
                        buf += 3;                                       \
 
74
                        break;                                          \
 
75
                                                                        \
 
76
                default:                                                \
 
77
                        fprintf(stderr,                                 \
 
78
                                "Format %d not yet supported\n",        \
 
79
                                format);                                \
 
80
        }                                                               \
 
81
}
 
82
 
 
83
static int get_brightness_adj(unsigned char *image, long size, int *brightness)
 
84
{
 
85
        long i, tot = 0;
 
86
        for (i=0;i<size*3;i++)
 
87
                tot += image[i];
 
88
        *brightness = (128 - tot/(size*3))/3;
 
89
        return !((tot/(size*3)) >= 126 && (tot/(size*3)) <= 130);
 
90
}
 
91
 
 
92
int main(int argc, char **argv)
 
93
{
 
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;
 
99
 
 
100
        unsigned char *buffer, *src;
 
101
        int bpp = 24, r, g, b;
 
102
        unsigned int i, src_depth;
 
103
 
 
104
        if (fd < 0) {
 
105
                perror(FILE);
 
106
                exit(1);
 
107
        }
 
108
 
 
109
        if (ioctl(fd, VIDIOCGCAP, &cap) < 0) {
 
110
                perror("VIDIOGCAP");
 
111
                fprintf(stderr, "(" FILE " not a video4linux device?)\n");
 
112
                close(fd);
 
113
                exit(1);
 
114
        }
 
115
 
 
116
        if (ioctl(fd, VIDIOCGWIN, &win) < 0) {
 
117
                perror("VIDIOCGWIN");
 
118
                close(fd);
 
119
                exit(1);
 
120
        }
 
121
 
 
122
        if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) {
 
123
                perror("VIDIOCGPICT");
 
124
                close(fd);
 
125
                exit(1);
 
126
        }
 
127
 
 
128
        if (cap.type & VID_TYPE_MONOCHROME) {
 
129
                vpic.depth=8;
 
130
                vpic.palette=VIDEO_PALETTE_GREY;    /* 8bit grey */
 
131
                if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
 
132
                        vpic.depth=6;
 
133
                        if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
 
134
                                vpic.depth=4;
 
135
                                if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
 
136
                                        fprintf(stderr, "Unable to find a supported capture format.\n");
 
137
                                        close(fd);
 
138
                                        exit(1);
 
139
                                }
 
140
                        }
 
141
                }
 
142
        }
 
143
        else {
 
144
                vpic.depth=24;
 
145
                vpic.palette=VIDEO_PALETTE_RGB24;
 
146
 
 
147
                if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
 
148
                        vpic.palette=VIDEO_PALETTE_RGB565;
 
149
                        vpic.depth=16;
 
150
 
 
151
                        if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
 
152
                                vpic.palette=VIDEO_PALETTE_RGB555;
 
153
                                vpic.depth=15;
 
154
 
 
155
                                if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
 
156
                                        fprintf(stderr, "Unable to find a supported capture format.\n");
 
157
                                        return -1;
 
158
                                }
 
159
                        }
 
160
                }
 
161
        }
 
162
 
 
163
        buffer = malloc(win.width * win.height * bpp);
 
164
        if (!buffer) {
 
165
                fprintf(stderr, "Out of memory.\n");
 
166
                exit(1);
 
167
        }
 
168
 
 
169
        do {
 
170
                int newbright;
 
171
                read(fd, buffer, win.width * win.height * bpp);
 
172
                f = get_brightness_adj(buffer, win.width * win.height, &newbright);
 
173
                if (f) {
 
174
                        vpic.brightness += (newbright << 8);
 
175
                        if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
 
176
                                perror("VIDIOSPICT");
 
177
                                break;
 
178
                        }
 
179
                }
 
180
        } while (f);
 
181
 
 
182
        fprintf(stdout, "P6\n%d %d 255\n", win.width, win.height);
 
183
 
 
184
        src = buffer;
 
185
 
 
186
        for (i = 0; i < win.width * win.height; i++) {
 
187
                READ_VIDEO_PIXEL(src, vpic.palette, src_depth, r, g, b);
 
188
                fputc(r>>8, stdout);
 
189
                fputc(g>>8, stdout);
 
190
                fputc(b>>8, stdout);
 
191
        }
 
192
 
 
193
        close(fd);
 
194
#else
 
195
        fprintf(stderr, "V4L1 API is not configured!\n");
 
196
#endif
 
197
        return 0;
 
198
}