v4lgrab.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * For some cameras you may need to pre-load libv4l to perform
  16. * the necessary decompression, e.g.:
  17. *
  18. * export LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so
  19. * ./v4lgrab >image.ppm
  20. *
  21. * see http://hansdegoede.livejournal.com/3636.html for details.
  22. *
  23. */
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #include <sys/ioctl.h>
  30. #include <stdlib.h>
  31. #include <linux/types.h>
  32. #include <linux/videodev.h>
  33. #define VIDEO_DEV "/dev/video0"
  34. /* Stole this from tvset.c */
  35. #define READ_VIDEO_PIXEL(buf, format, depth, r, g, b) \
  36. { \
  37. switch (format) \
  38. { \
  39. case VIDEO_PALETTE_GREY: \
  40. switch (depth) \
  41. { \
  42. case 4: \
  43. case 6: \
  44. case 8: \
  45. (r) = (g) = (b) = (*buf++ << 8);\
  46. break; \
  47. \
  48. case 16: \
  49. (r) = (g) = (b) = \
  50. *((unsigned short *) buf); \
  51. buf += 2; \
  52. break; \
  53. } \
  54. break; \
  55. \
  56. \
  57. case VIDEO_PALETTE_RGB565: \
  58. { \
  59. unsigned short tmp = *(unsigned short *)buf; \
  60. (r) = tmp&0xF800; \
  61. (g) = (tmp<<5)&0xFC00; \
  62. (b) = (tmp<<11)&0xF800; \
  63. buf += 2; \
  64. } \
  65. break; \
  66. \
  67. case VIDEO_PALETTE_RGB555: \
  68. (r) = (buf[0]&0xF8)<<8; \
  69. (g) = ((buf[0] << 5 | buf[1] >> 3)&0xF8)<<8; \
  70. (b) = ((buf[1] << 2 ) & 0xF8)<<8; \
  71. buf += 2; \
  72. break; \
  73. \
  74. case VIDEO_PALETTE_RGB24: \
  75. (r) = buf[0] << 8; (g) = buf[1] << 8; \
  76. (b) = buf[2] << 8; \
  77. buf += 3; \
  78. break; \
  79. \
  80. default: \
  81. fprintf(stderr, \
  82. "Format %d not yet supported\n", \
  83. format); \
  84. } \
  85. }
  86. int get_brightness_adj(unsigned char *image, long size, int *brightness) {
  87. long i, tot = 0;
  88. for (i=0;i<size*3;i++)
  89. tot += image[i];
  90. *brightness = (128 - tot/(size*3))/3;
  91. return !((tot/(size*3)) >= 126 && (tot/(size*3)) <= 130);
  92. }
  93. int main(int argc, char ** argv)
  94. {
  95. int fd = open(VIDEO_DEV, O_RDONLY), f;
  96. struct video_capability cap;
  97. struct video_window win;
  98. struct video_picture vpic;
  99. unsigned char *buffer, *src;
  100. int bpp = 24, r = 0, g = 0, b = 0;
  101. unsigned int i, src_depth = 16;
  102. if (fd < 0) {
  103. perror(VIDEO_DEV);
  104. exit(1);
  105. }
  106. if (ioctl(fd, VIDIOCGCAP, &cap) < 0) {
  107. perror("VIDIOGCAP");
  108. fprintf(stderr, "(" VIDEO_DEV " not a video4linux device?)\n");
  109. close(fd);
  110. exit(1);
  111. }
  112. if (ioctl(fd, VIDIOCGWIN, &win) < 0) {
  113. perror("VIDIOCGWIN");
  114. close(fd);
  115. exit(1);
  116. }
  117. if (ioctl(fd, VIDIOCGPICT, &vpic) < 0) {
  118. perror("VIDIOCGPICT");
  119. close(fd);
  120. exit(1);
  121. }
  122. if (cap.type & VID_TYPE_MONOCHROME) {
  123. vpic.depth=8;
  124. vpic.palette=VIDEO_PALETTE_GREY; /* 8bit grey */
  125. if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  126. vpic.depth=6;
  127. if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  128. vpic.depth=4;
  129. if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  130. fprintf(stderr, "Unable to find a supported capture format.\n");
  131. close(fd);
  132. exit(1);
  133. }
  134. }
  135. }
  136. } else {
  137. vpic.depth=24;
  138. vpic.palette=VIDEO_PALETTE_RGB24;
  139. if(ioctl(fd, VIDIOCSPICT, &vpic) < 0) {
  140. vpic.palette=VIDEO_PALETTE_RGB565;
  141. vpic.depth=16;
  142. if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
  143. vpic.palette=VIDEO_PALETTE_RGB555;
  144. vpic.depth=15;
  145. if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
  146. fprintf(stderr, "Unable to find a supported capture format.\n");
  147. return -1;
  148. }
  149. }
  150. }
  151. }
  152. buffer = malloc(win.width * win.height * bpp);
  153. if (!buffer) {
  154. fprintf(stderr, "Out of memory.\n");
  155. exit(1);
  156. }
  157. do {
  158. int newbright;
  159. read(fd, buffer, win.width * win.height * bpp);
  160. f = get_brightness_adj(buffer, win.width * win.height, &newbright);
  161. if (f) {
  162. vpic.brightness += (newbright << 8);
  163. if(ioctl(fd, VIDIOCSPICT, &vpic)==-1) {
  164. perror("VIDIOSPICT");
  165. break;
  166. }
  167. }
  168. } while (f);
  169. fprintf(stdout, "P6\n%d %d 255\n", win.width, win.height);
  170. src = buffer;
  171. for (i = 0; i < win.width * win.height; i++) {
  172. READ_VIDEO_PIXEL(src, vpic.palette, src_depth, r, g, b);
  173. fputc(r>>8, stdout);
  174. fputc(g>>8, stdout);
  175. fputc(b>>8, stdout);
  176. }
  177. close(fd);
  178. return 0;
  179. }