camera.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. pygame - Python Game Library
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; if not, write to the Free
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include "pygame.h"
  16. #include "doc/camera_doc.h"
  17. #if defined(__unix__)
  18. #include <structmember.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <assert.h>
  23. #include <fcntl.h> /* low-level i/o */
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/time.h>
  29. #include <sys/mman.h>
  30. #include <sys/ioctl.h>
  31. /* on freebsd there is no asm/types */
  32. #ifdef linux
  33. #include <asm/types.h> /* for videodev2.h */
  34. #endif
  35. #include <linux/videodev2.h>
  36. #elif defined(__APPLE__)
  37. #include <AvailabilityMacros.h>
  38. /* We support OSX 10.6 and below. */
  39. #if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060
  40. #define PYGAME_MAC_CAMERA_OLD 1
  41. #endif
  42. #endif
  43. #if defined(PYGAME_MAC_CAMERA_OLD)
  44. #include <QuickTime/QuickTime.h>
  45. #include <QuickTime/Movies.h>
  46. #include <QuickTime/ImageCompression.h>
  47. #endif
  48. /* some constants used which are not defined on non-v4l machines. */
  49. #ifndef V4L2_PIX_FMT_RGB24
  50. #define V4L2_PIX_FMT_RGB24 'RGB3'
  51. #endif
  52. #ifndef V4L2_PIX_FMT_RGB444
  53. #define V4L2_PIX_FMT_RGB444 'R444'
  54. #endif
  55. #ifndef V4L2_PIX_FMT_YUYV
  56. #define V4L2_PIX_FMT_YUYV 'YUYV'
  57. #endif
  58. #define CLEAR(x) memset (&(x), 0, sizeof (x))
  59. #define SAT(c) if (c & (~255)) { if (c < 0) c = 0; else c = 255; }
  60. #define SAT2(c) ((c) & (~255) ? ((c) < 0 ? 0 : 255) : (c))
  61. #define DEFAULT_WIDTH 640
  62. #define DEFAULT_HEIGHT 480
  63. #define RGB_OUT 1
  64. #define YUV_OUT 2
  65. #define HSV_OUT 4
  66. #define CAM_V4L 1 /* deprecated. the incomplete support in pygame was removed */
  67. #define CAM_V4L2 2
  68. struct buffer {
  69. void * start;
  70. size_t length;
  71. };
  72. #if defined(__unix__)
  73. typedef struct pgCameraObject {
  74. PyObject_HEAD
  75. char* device_name;
  76. int camera_type;
  77. unsigned long pixelformat;
  78. unsigned int color_out;
  79. struct buffer* buffers;
  80. unsigned int n_buffers;
  81. int width;
  82. int height;
  83. int size;
  84. int hflip;
  85. int vflip;
  86. int brightness;
  87. int fd;
  88. } pgCameraObject;
  89. #elif defined(PYGAME_MAC_CAMERA_OLD)
  90. typedef struct pgCameraObject {
  91. PyObject_HEAD
  92. char* device_name; /* unieke name of the device */
  93. OSType pixelformat;
  94. unsigned int color_out;
  95. SeqGrabComponent component; /* A type used by the Sequence Grabber API */
  96. SGChannel channel; /* Channel of the Sequence Grabber */
  97. GWorldPtr gworld; /* Pointer to the struct that holds the data of the captured image */
  98. Rect boundsRect; /* bounds of the image frame */
  99. long size; /* size of the image in our buffer to draw */
  100. int hflip;
  101. int vflip;
  102. short depth;
  103. struct buffer pixels;
  104. //struct buffer tmp_pixels /* place where the flipped image in temporarly stored if hflip or vflip is true.*/
  105. } pgCameraObject;
  106. #else
  107. /* generic definition.
  108. */
  109. typedef struct pgCameraObject {
  110. PyObject_HEAD
  111. char* device_name;
  112. int camera_type;
  113. unsigned long pixelformat;
  114. unsigned int color_out;
  115. struct buffer* buffers;
  116. unsigned int n_buffers;
  117. int width;
  118. int height;
  119. int size;
  120. int hflip;
  121. int vflip;
  122. int brightness;
  123. int fd;
  124. } pgCameraObject;
  125. #endif
  126. /* internal functions for colorspace conversion */
  127. void colorspace (SDL_Surface *src, SDL_Surface *dst, int cspace);
  128. void rgb24_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
  129. void rgb444_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
  130. void rgb_to_yuv (const void* src, void* dst, int length,
  131. unsigned long source, SDL_PixelFormat* format);
  132. void rgb_to_hsv (const void* src, void* dst, int length,
  133. unsigned long source, SDL_PixelFormat* format);
  134. void yuyv_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
  135. void yuyv_to_yuv (const void* src, void* dst, int length, SDL_PixelFormat* format);
  136. void uyvy_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
  137. void uyvy_to_yuv (const void* src, void* dst, int length, SDL_PixelFormat* format);
  138. void sbggr8_to_rgb (const void* src, void* dst, int width, int height,
  139. SDL_PixelFormat* format);
  140. void yuv420_to_rgb (const void* src, void* dst, int width, int height,
  141. SDL_PixelFormat* format);
  142. void yuv420_to_yuv (const void* src, void* dst, int width, int height,
  143. SDL_PixelFormat* format);
  144. #if defined(__unix__)
  145. /* internal functions specific to v4l2 */
  146. char** v4l2_list_cameras (int* num_devices);
  147. int v4l2_get_control (int fd, int id, int *value);
  148. int v4l2_set_control (int fd, int id, int value);
  149. PyObject* v4l2_read_raw (pgCameraObject* self);
  150. int v4l2_xioctl (int fd, int request, void *arg);
  151. int v4l2_process_image (pgCameraObject* self, const void *image,
  152. unsigned int buffer_size, SDL_Surface* surf);
  153. int v4l2_query_buffer (pgCameraObject* self);
  154. int v4l2_read_frame (pgCameraObject* self, SDL_Surface* surf);
  155. int v4l2_stop_capturing (pgCameraObject* self);
  156. int v4l2_start_capturing (pgCameraObject* self);
  157. int v4l2_uninit_device (pgCameraObject* self);
  158. int v4l2_init_mmap (pgCameraObject* self);
  159. int v4l2_init_device (pgCameraObject* self);
  160. int v4l2_close_device (pgCameraObject* self);
  161. int v4l2_open_device (pgCameraObject* self);
  162. #elif defined(PYGAME_MAC_CAMERA_OLD)
  163. /* internal functions specific to mac */
  164. char** mac_list_cameras(int* num_devices);
  165. int mac_open_device (pgCameraObject* self);
  166. int mac_init_device(pgCameraObject* self);
  167. int mac_close_device (pgCameraObject* self);
  168. int mac_start_capturing(pgCameraObject* self);
  169. int mac_stop_capturing (pgCameraObject* self);
  170. int mac_get_control(pgCameraObject* self, int id, int* value);
  171. int mac_set_control(pgCameraObject* self, int id, int value);
  172. PyObject* mac_read_raw(pgCameraObject *self);
  173. int mac_read_frame(pgCameraObject* self, SDL_Surface* surf);
  174. int mac_camera_idle(pgCameraObject* self);
  175. int mac_copy_gworld_to_surface(pgCameraObject* self, SDL_Surface* surf);
  176. void flip_image(const void* image, void* flipped_image, int width, int height,
  177. short depth, int hflip, int vflip);
  178. #endif