/* pygame - Python Game Library This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "pygame.h"#include "doc/camera_doc.h"#if defined(__unix__)#include <structmember.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <fcntl.h> /* low-level i/o */#include <unistd.h>#include <errno.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/time.h>#include <sys/mman.h>#include <sys/ioctl.h>/* on freebsd there is no asm/types */#ifdef linux#include <asm/types.h> /* for videodev2.h */#endif#include <linux/videodev2.h>#elif defined(__APPLE__)#include <AvailabilityMacros.h>/* We support OSX 10.6 and below. */#if __MAC_OS_X_VERSION_MAX_ALLOWED <= 1060#define PYGAME_MAC_CAMERA_OLD 1#endif#endif#if defined(PYGAME_MAC_CAMERA_OLD)#include <QuickTime/QuickTime.h>#include <QuickTime/Movies.h>#include <QuickTime/ImageCompression.h>#endif/* some constants used which are not defined on non-v4l machines. */#ifndef V4L2_PIX_FMT_RGB24#define V4L2_PIX_FMT_RGB24 'RGB3'#endif#ifndef V4L2_PIX_FMT_RGB444#define V4L2_PIX_FMT_RGB444 'R444'#endif#ifndef V4L2_PIX_FMT_YUYV#define V4L2_PIX_FMT_YUYV 'YUYV'#endif#define CLEAR(x) memset (&(x), 0, sizeof (x))#define SAT(c) if (c & (~255)) { if (c < 0) c = 0; else c = 255; }#define SAT2(c) ((c) & (~255) ? ((c) < 0 ? 0 : 255) : (c))#define DEFAULT_WIDTH 640#define DEFAULT_HEIGHT 480#define RGB_OUT 1#define YUV_OUT 2#define HSV_OUT 4#define CAM_V4L 1 /* deprecated. the incomplete support in pygame was removed */#define CAM_V4L2 2structbuffer{void*start;size_tlength;};#if defined(__unix__)typedefstructPyCameraObject{PyObject_HEADchar*device_name;intcamera_type;unsignedlongpixelformat;unsignedintcolor_out;structbuffer*buffers;unsignedintn_buffers;intwidth;intheight;intsize;inthflip;intvflip;intbrightness;intfd;}PyCameraObject;#elif defined(PYGAME_MAC_CAMERA_OLD)typedefstructPyCameraObject{PyObject_HEADchar*device_name;/* unieke name of the device */OSTypepixelformat;unsignedintcolor_out;SeqGrabComponentcomponent;/* A type used by the Sequence Grabber API */SGChannelchannel;/* Channel of the Sequence Grabber */GWorldPtrgworld;/* Pointer to the struct that holds the data of the captured image */RectboundsRect;/* bounds of the image frame */longsize;/* size of the image in our buffer to draw */inthflip;intvflip;shortdepth;structbufferpixels;//struct buffer tmp_pixels /* place where the flipped image in temporarly stored if hflip or vflip is true.*/}PyCameraObject;#else/* generic definition.*/typedefstructPyCameraObject{PyObject_HEADchar*device_name;intcamera_type;unsignedlongpixelformat;unsignedintcolor_out;structbuffer*buffers;unsignedintn_buffers;intwidth;intheight;intsize;inthflip;intvflip;intbrightness;intfd;}PyCameraObject;#endif/* internal functions for colorspace conversion */voidcolorspace(SDL_Surface*src,SDL_Surface*dst,intcspace);voidrgb24_to_rgb(constvoid*src,void*dst,intlength,SDL_PixelFormat*format);voidrgb444_to_rgb(constvoid*src,void*dst,intlength,SDL_PixelFormat*format);voidrgb_to_yuv(constvoid*src,void*dst,intlength,unsignedlongsource,SDL_PixelFormat*format);voidrgb_to_hsv(constvoid*src,void*dst,intlength,unsignedlongsource,SDL_PixelFormat*format);voidyuyv_to_rgb(constvoid*src,void*dst,intlength,SDL_PixelFormat*format);voidyuyv_to_yuv(constvoid*src,void*dst,intlength,SDL_PixelFormat*format);voidsbggr8_to_rgb(constvoid*src,void*dst,intwidth,intheight,SDL_PixelFormat*format);voidyuv420_to_rgb(constvoid*src,void*dst,intwidth,intheight,SDL_PixelFormat*format);voidyuv420_to_yuv(constvoid*src,void*dst,intwidth,intheight,SDL_PixelFormat*format);#if defined(__unix__)/* internal functions specific to v4l2 */char**v4l2_list_cameras(int*num_devices);intv4l2_get_control(intfd,intid,int*value);intv4l2_set_control(intfd,intid,intvalue);PyObject*v4l2_read_raw(PyCameraObject*self);intv4l2_xioctl(intfd,intrequest,void*arg);intv4l2_process_image(PyCameraObject*self,constvoid*image,unsignedintbuffer_size,SDL_Surface*surf);intv4l2_query_buffer(PyCameraObject*self);intv4l2_read_frame(PyCameraObject*self,SDL_Surface*surf);intv4l2_stop_capturing(PyCameraObject*self);intv4l2_start_capturing(PyCameraObject*self);intv4l2_uninit_device(PyCameraObject*self);intv4l2_init_mmap(PyCameraObject*self);intv4l2_init_device(PyCameraObject*self);intv4l2_close_device(PyCameraObject*self);intv4l2_open_device(PyCameraObject*self);#elif defined(PYGAME_MAC_CAMERA_OLD)/* internal functions specific to mac */char**mac_list_cameras(int*num_devices);intmac_open_device(PyCameraObject*self);intmac_init_device(PyCameraObject*self);intmac_close_device(PyCameraObject*self);intmac_start_capturing(PyCameraObject*self);intmac_stop_capturing(PyCameraObject*self);intmac_get_control(PyCameraObject*self,intid,int*value);intmac_set_control(PyCameraObject*self,intid,intvalue);PyObject*mac_read_raw(PyCameraObject*self);intmac_read_frame(PyCameraObject*self,SDL_Surface*surf);intmac_camera_idle(PyCameraObject*self);intmac_copy_gworld_to_surface(PyCameraObject*self,SDL_Surface*surf);voidflip_image(constvoid*image,void*flipped_image,intwidth,intheight,shortdepth,inthflip,intvflip);#endif