/* Simple Video4Linux image grabber. */ /* * Video4Linux Driver Test/Example Framegrabbing Program * * Compile with: * gcc -s -Wall -Wstrict-prototypes v4lgrab.c -o v4lgrab * Use as: * v4lgrab >image.ppm * * Copyright (C) 1998-05-03, Phil Blundell * Copied from http://www.tazenda.demon.co.uk/phil/vgrabber.c * with minor modifications (Dave Forrest, drf5n@virginia.edu). * */ #include "frgrab_ioctl.h" #include #include #include #include #include #include #include #include #include #ifndef __FRGRAB__ #define __FRGRAB__ #include #endif #define FILE "/dev/video0" /* Stole this from tvset.c */ #define READ_VIDEO_PIXEL(buf, format, depth, r, g, b) \ { \ switch (format) \ { \ case VIDEO_PALETTE_GREY: \ switch (depth) \ { \ case 4: \ case 6: \ case 8: \ (r) = (g) = (b) = (*buf++ << 8);\ break; \ \ case 16: \ (r) = (g) = (b) = \ *((unsigned short *) buf); \ buf += 2; \ break; \ } \ break; \ \ \ case VIDEO_PALETTE_RGB565: \ { \ unsigned short tmp = *(unsigned short *)buf; \ (r) = tmp&0xF800; \ (g) = (tmp<<5)&0xFC00; \ (b) = (tmp<<11)&0xF800; \ buf += 2; \ } \ break; \ \ case VIDEO_PALETTE_RGB555: \ (r) = (buf[0]&0xF8)<<8; \ (g) = ((buf[0] << 5 | buf[1] >> 3)&0xF8)<<8; \ (b) = ((buf[1] << 2 ) & 0xF8)<<8; \ buf += 2; \ break; \ \ case VIDEO_PALETTE_RGB24: \ (r) = buf[0] << 8; (g) = buf[1] << 8; \ (b) = buf[2] << 8; \ buf += 3; \ break; \ \ default: \ fprintf(stderr, \ "Format %d not yet supported\n", \ format); \ } \ } int get_brightness_adj(unsigned char *image, long size, int *brightness) { long i, tot = 0; for (i=0;i= 126 && (tot/(size*3)) <= 130); } int main(int argc, char ** argv) { int fd = open(FILE, O_RDONLY), f; struct video_capability cap; struct video_window win; struct video_picture vpic; caddr_t ptr; unsigned char *buffer, *src; int bpp = 24, r, g, b; unsigned int i, j,src_depth, arg=10, con=10, hue=10; if (fd < 0) { perror(FILE); exit(1); } if (ioctl(fd, VIDIOCGCAP, &cap) < 0) { perror("VIDIOGCAP"); fprintf(stderr, "(" FILE " not a video4linux device?)\n"); close(fd); exit(1); } /* if (ioctl(fd, VIDIOCGWIN, &win) < 0) { perror("VIDIOCGWIN"); close(fd); exit(1); } if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) { perror("VIDIOCGPICT"); close(fd); exit(1); } */ /**set brightness**/ /* if (ioctl(fd, SETBRIGHTNESS, &arg) < 0) { perror("SETBRIGHTNESS"); close(fd); exit(1); } */ /**get brightness**/ /* if (ioctl(fd, GETBRIGHTNESS, &i) < 0) { perror("GETBRIGHTNESS"); close(fd); exit(1); } */ /**set contrast**/ /* if (ioctl(fd, SETCONTRAST, &con) < 0) { perror("SETCONTRAST"); close(fd); exit(1); } */ /**get contrast**/ /* if (ioctl(fd, GETCONTRAST, &j) < 0) { perror("GETCONTRAST"); close(fd); exit(1); } */ /**set hue**/ if (ioctl(fd, SETHUE, &hue) < 0) { perror("SETHUE"); close(fd); exit(1); } /**get hue**/ /* if (ioctl(fd, GETHUE, &j) < 0) { perror("GETHUE"); close(fd); exit(1); } */ /***mmap****/ /* if((ptr=mmap(0,16000,PROT_READ,MAP_SHARED,fd,0))==(caddr_t)-1) { printf("mmap doesn't work\n"); return 0; } */ win.width = 512; win.height = 480; buffer = malloc(win.width * win.height); // fprintf(stdout, "bufsize=%ld",win.width*win.height*bpp); if (!buffer) { fprintf(stderr, "Out of memory.\n"); exit(1); } fprintf(stdout, "P5\n%d %d 255\n", win.width, win.height); /*testing mmap for (i = 0; i < win.width * win.height; i++) { fputc(*ptr, stdout); ptr++; } */ read(fd, buffer, win.width * win.height); for (i = 0; i < win.width * win.height; i++) { fputc(buffer[i], stdout); } close(fd); return 0; }