4 pygame - Python Game Library
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the Free
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "doc/camera_doc.h"
26 #include <structmember.h>
32 #include <fcntl.h> /* low-level i/o */
36 #include <sys/types.h>
39 #include <sys/ioctl.h>
41 /* on freebsd there is no asm/types */
43 #include <asm/types.h> /* for videodev2.h */
46 #include <linux/videodev2.h>
47 #elif defined(__APPLE__)
48 #include <AvailabilityMacros.h>
49 /* We support OSX 10.6 and below. */
50 #if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060
51 #define PYGAME_MAC_CAMERA_OLD 1
55 #if defined(PYGAME_MAC_CAMERA_OLD)
56 #include <QuickTime/QuickTime.h>
57 #include <QuickTime/Movies.h>
58 #include <QuickTime/ImageCompression.h>
61 /* some constants used which are not defined on non-v4l machines. */
62 #ifndef V4L2_PIX_FMT_RGB24
63 #define V4L2_PIX_FMT_RGB24 'RGB3'
65 #ifndef V4L2_PIX_FMT_RGB444
66 #define V4L2_PIX_FMT_RGB444 'R444'
68 #ifndef V4L2_PIX_FMT_YUYV
69 #define V4L2_PIX_FMT_YUYV 'YUYV'
72 #define CLEAR(x) memset (&(x), 0, sizeof (x))
73 #define SAT(c) if (c & (~255)) { if (c < 0) c = 0; else c = 255; }
74 #define SAT2(c) ((c) & (~255) ? ((c) < 0 ? 0 : 255) : (c))
75 #define DEFAULT_WIDTH 640
76 #define DEFAULT_HEIGHT 480
80 #define CAM_V4L 1 /* deprecated. the incomplete support in pygame was removed */
89 typedef struct pgCameraObject {
93 unsigned long pixelformat;
94 unsigned int color_out;
95 struct buffer* buffers;
96 unsigned int n_buffers;
105 #elif defined(PYGAME_MAC_CAMERA_OLD)
106 typedef struct pgCameraObject {
108 char* device_name; /* unique name of the device */
110 unsigned int color_out;
111 SeqGrabComponent component; /* A type used by the Sequence Grabber API */
112 SGChannel channel; /* Channel of the Sequence Grabber */
113 GWorldPtr gworld; /* Pointer to the struct that holds the data of the captured image */
114 Rect boundsRect; /* bounds of the image frame */
115 long size; /* size of the image in our buffer to draw */
119 struct buffer pixels;
120 //struct buffer tmp_pixels /* place where the flipped image in temporarily stored if hflip or vflip is true.*/
124 /* generic definition.
127 typedef struct pgCameraObject {
131 unsigned long pixelformat;
132 unsigned int color_out;
133 struct buffer* buffers;
134 unsigned int n_buffers;
145 /* internal functions for colorspace conversion */
146 void colorspace (SDL_Surface *src, SDL_Surface *dst, int cspace);
147 void rgb24_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
148 void rgb444_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
149 void rgb_to_yuv (const void* src, void* dst, int length,
150 unsigned long source, SDL_PixelFormat* format);
151 void rgb_to_hsv (const void* src, void* dst, int length,
152 unsigned long source, SDL_PixelFormat* format);
153 void yuyv_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
154 void yuyv_to_yuv (const void* src, void* dst, int length, SDL_PixelFormat* format);
155 void uyvy_to_rgb (const void* src, void* dst, int length, SDL_PixelFormat* format);
156 void uyvy_to_yuv (const void* src, void* dst, int length, SDL_PixelFormat* format);
157 void sbggr8_to_rgb (const void* src, void* dst, int width, int height,
158 SDL_PixelFormat* format);
159 void yuv420_to_rgb (const void* src, void* dst, int width, int height,
160 SDL_PixelFormat* format);
161 void yuv420_to_yuv (const void* src, void* dst, int width, int height,
162 SDL_PixelFormat* format);
164 #if defined(__unix__)
165 /* internal functions specific to v4l2 */
166 char** v4l2_list_cameras (int* num_devices);
167 int v4l2_get_control (int fd, int id, int *value);
168 int v4l2_set_control (int fd, int id, int value);
169 PyObject* v4l2_read_raw (pgCameraObject* self);
170 int v4l2_xioctl (int fd, int request, void *arg);
171 int v4l2_process_image (pgCameraObject* self, const void *image,
172 unsigned int buffer_size, SDL_Surface* surf);
173 int v4l2_query_buffer (pgCameraObject* self);
174 int v4l2_read_frame (pgCameraObject* self, SDL_Surface* surf);
175 int v4l2_stop_capturing (pgCameraObject* self);
176 int v4l2_start_capturing (pgCameraObject* self);
177 int v4l2_uninit_device (pgCameraObject* self);
178 int v4l2_init_mmap (pgCameraObject* self);
179 int v4l2_init_device (pgCameraObject* self);
180 int v4l2_close_device (pgCameraObject* self);
181 int v4l2_open_device (pgCameraObject* self);
183 #elif defined(PYGAME_MAC_CAMERA_OLD)
184 /* internal functions specific to mac */
185 char** mac_list_cameras(int* num_devices);
186 int mac_open_device (pgCameraObject* self);
187 int mac_init_device(pgCameraObject* self);
188 int mac_close_device (pgCameraObject* self);
189 int mac_start_capturing(pgCameraObject* self);
190 int mac_stop_capturing (pgCameraObject* self);
192 int mac_get_control(pgCameraObject* self, int id, int* value);
193 int mac_set_control(pgCameraObject* self, int id, int value);
195 PyObject* mac_read_raw(pgCameraObject *self);
196 int mac_read_frame(pgCameraObject* self, SDL_Surface* surf);
197 int mac_camera_idle(pgCameraObject* self);
198 int mac_copy_gworld_to_surface(pgCameraObject* self, SDL_Surface* surf);
200 void flip_image(const void* image, void* flipped_image, int width, int height,
201 short depth, int hflip, int vflip);
205 #endif /* !CAMERA_H */