Mercurial > pygame
annotate src/camera_mac.m @ 3194:9fcb7abf3185 default tip
Remove some gcc 4.7.2 compiler warnings
| author | Lenard Lindstrom <len-l@telus.net> |
|---|---|
| date | Tue, 23 Apr 2013 19:53:36 -0700 |
| parents | 1c265998cf59 |
| children |
| rev | line source |
|---|---|
| 1975 | 1 // |
| 2 // camera_mac.m | |
| 3 // | |
| 4 // Created by Werner Laurensse on 2009-05-28. | |
| 5 // Copyright (c) 2009 . All rights reserved. | |
| 6 // | |
| 7 | |
| 8 #import "camera.h" | |
|
2143
99afc91b219a
apply camera module patch for Python 3; thanks Christoph Gohlke
Lenard Lindstrom <len-l@telus.net>
parents:
1989
diff
changeset
|
9 #import "pgcompat.h" |
| 1975 | 10 |
|
3073
1c265998cf59
Disable camera_mac if OS X is above version 10.6.
Rene Dudfield <renesd@gmail.com>
parents:
2143
diff
changeset
|
11 #if defined(PYGAME_MAC_CAMERA_OLD) |
|
1c265998cf59
Disable camera_mac if OS X is above version 10.6.
Rene Dudfield <renesd@gmail.com>
parents:
2143
diff
changeset
|
12 |
|
1989
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
13 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
14 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
15 /* Flips the image array horizontally and/or vertically by reverse copying |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
16 * a 'depth' number of bytes to flipped_image.*/ |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
17 /* todo speed up... */ |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
18 void flip_image(const void* image, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
19 void* flipped_image, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
20 int width, int height, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
21 short depth, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
22 int hflip, int vflip) |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
23 { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
24 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
25 if (!hflip && vflip) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
26 int i, j; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
27 int width_size = width*depth; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
28 const void* tmp_image = image; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
29 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
30 for(i=0; i<=height-1; i++) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
31 for(j=0; j<=width; j++) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
32 memcpy(flipped_image+width_size-j*depth-3, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
33 tmp_image+j*depth, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
34 depth); |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
35 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
36 tmp_image += width_size; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
37 flipped_image += width_size; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
38 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
39 } else if (hflip && !vflip) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
40 int i; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
41 int width_size = width*depth; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
42 void* tmp_image = flipped_image+width_size*height; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
43 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
44 for(i=0; i<height; i++) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
45 tmp_image -= width_size; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
46 memcpy(tmp_image, image+i*width_size, width_size); |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
47 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
48 } else if (hflip && vflip) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
49 int i, j; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
50 int width_size = width*depth; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
51 void* tmp_image = flipped_image + width_size*height; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
52 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
53 for(i=0; i<=height-1; i++) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
54 for(j=0; j<=width; j++) { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
55 memcpy(tmp_image-j*depth-3, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
56 image+j*depth, |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
57 depth); |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
58 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
59 tmp_image -= width_size; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
60 image += width_size; |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
61 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
62 } else { |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
63 memcpy(flipped_image, image, height*width*depth); |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
64 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
65 } |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
66 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
67 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
68 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
69 |
|
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
70 |
| 1975 | 71 /* |
| 72 * return: an array of the available cameras ids. | |
| 73 * num_cameras: number of cameras in array. | |
| 74 */ | |
| 75 char** mac_list_cameras(int* num_cameras) { | |
| 76 char** cameras; | |
| 77 char* camera; | |
| 78 | |
| 79 // The ComponentDescription allows you to search for Components that can be used to capture | |
| 80 // images, and thus can be used as camera. | |
| 81 ComponentDescription cameraDescription; | |
| 82 memset(&cameraDescription, 0, sizeof(ComponentDescription)); | |
| 83 cameraDescription.componentType = SeqGrabComponentType; | |
| 84 | |
| 85 // Count the number of cameras on the system, and allocate an array for the cameras | |
| 86 *num_cameras = (int) CountComponents(&cameraDescription); | |
| 87 cameras = (char **) malloc(sizeof(char*) * *num_cameras); | |
| 88 | |
| 89 // Try to find a camera, and add the camera's 'number' as a string to the array of cameras | |
| 90 Component cameraComponent = FindNextComponent(0, &cameraDescription); | |
| 91 short num = 0; | |
| 92 while(cameraComponent != NULL) { | |
| 93 camera = malloc(sizeof(char) * 50); //TODO: find a better way to do this... | |
| 94 sprintf(camera, "%d", cameraComponent); | |
| 95 cameras[num] = camera; | |
| 96 cameraComponent = FindNextComponent(cameraComponent, &cameraDescription); | |
| 97 num++; | |
| 98 } | |
| 99 return cameras; | |
| 100 } | |
| 101 | |
| 102 /* Open a Camera component. */ | |
| 103 int mac_open_device (PyCameraObject* self) { | |
| 104 OSErr theErr; | |
| 105 | |
| 106 // Initialize movie toolbox | |
| 107 theErr = EnterMovies(); | |
| 108 if (theErr != noErr) { | |
| 109 PyErr_Format(PyExc_SystemError, | |
| 110 "Cannot initializes the Movie Toolbox"); | |
| 111 return 0; | |
| 112 } | |
| 113 | |
| 114 // Open camera component | |
| 115 SeqGrabComponent component = OpenComponent((Component) atoi(self->device_name)); | |
| 116 if (component == NULL) { | |
| 117 PyErr_Format(PyExc_SystemError, | |
| 118 "Cannot open '%s'", self->device_name); | |
| 119 return 0; | |
| 120 } | |
| 121 self->component = component; | |
| 122 | |
| 123 return 1; | |
| 124 } | |
| 125 | |
| 126 /* Make the Camera object ready for capturing images. */ | |
| 127 int mac_init_device(PyCameraObject* self) { | |
| 128 OSErr theErr; | |
| 129 | |
| 130 if (self->color_out == YUV_OUT) { | |
| 131 self->pixelformat = kYUVSPixelFormat; | |
| 132 self->depth = 2; | |
| 133 } else { | |
| 134 self->pixelformat = k24RGBPixelFormat; | |
| 135 self->depth = 3; | |
| 136 } | |
| 137 | |
| 138 int rowlength = self->boundsRect.right * self->depth; | |
| 139 | |
| 140 theErr = SGInitialize(self->component); | |
| 141 if (theErr != noErr) { | |
| 142 PyErr_Format(PyExc_SystemError, | |
| 143 "Cannot initialize sequence grabber component"); | |
| 144 return 0; | |
| 145 } | |
| 146 | |
| 147 | |
| 148 theErr = SGSetDataRef(self->component, 0, 0, seqGrabDontMakeMovie); | |
| 149 if (theErr != noErr) { | |
| 150 PyErr_Format(PyExc_SystemError, | |
| 151 "Cannot set the sequence grabber destination data reference for a record operation"); | |
| 152 return 0; | |
| 153 } | |
| 154 | |
| 155 theErr = SGNewChannel(self->component, VideoMediaType, &self->channel); | |
| 156 if (theErr != noErr) { | |
| 157 PyErr_Format(PyExc_SystemError, | |
| 158 "Cannot creates a sequence grabber channel and assigns a channel component to the channel"); | |
| 159 return 0; | |
| 160 } | |
| 161 | |
| 162 //theErr = SGSettingsDialog (self->component, self->channel, 0, NULL, 0, NULL, 0); | |
| 163 | |
| 164 theErr = SGSetChannelBounds(self->channel, &self->boundsRect); | |
| 165 if (theErr != noErr) { | |
| 166 PyErr_Format(PyExc_SystemError, | |
| 167 "Cannot specifie a channel's display boundary rectangle"); | |
| 168 return 0; | |
| 169 } | |
| 170 | |
| 171 /* | |
| 172 theErr = SGSetFrameRate (vc, fps); | |
| 173 if(theErr != noErr){ | |
| 174 PyErr_Format(PyExc_SystemError, | |
| 175 "Cannot set the frame rate of the sequence grabber"); | |
| 176 return 0; | |
| 177 } | |
| 178 */ | |
| 179 | |
| 180 theErr = SGSetChannelUsage(self->channel, seqGrabPreview); | |
| 181 if (theErr != noErr) { | |
| 182 PyErr_Format(PyExc_SystemError, | |
| 183 "Cannot specifie how a channel is to be used by the sequence grabber componen"); | |
| 184 return 0; | |
| 185 } | |
| 186 | |
| 187 theErr = SGSetChannelPlayFlags(self->channel, channelPlayAllData); | |
| 188 if (theErr != noErr) { | |
| 189 PyErr_Format(PyExc_SystemError, | |
| 190 "Cannot adjust the speed and quality with which the sequence grabber displays data from a channel"); | |
| 191 return 0; | |
| 192 } | |
| 193 | |
| 194 self->pixels.length = self->boundsRect.right * self->boundsRect.bottom * self->depth; | |
| 195 self->pixels.start = (unsigned char*) malloc(self->pixels.length); | |
| 196 | |
| 197 theErr = QTNewGWorldFromPtr(&self->gworld, | |
| 198 self->pixelformat, | |
| 199 &self->boundsRect, | |
| 200 NULL, | |
| 201 NULL, | |
| 202 0, | |
| 203 self->pixels.start, | |
| 204 rowlength); | |
| 205 | |
| 206 if (theErr != noErr) { | |
| 207 PyErr_Format(PyExc_SystemError, | |
| 208 "Cannot wrap a graphics world and pixel map structure around an existing block of memory containing an image, " | |
| 209 "failed to run QTNewGWorldFromPtr"); | |
| 210 free(self->pixels.start); | |
| 211 self->pixels.start = NULL; | |
| 212 self->pixels.length = 0; | |
| 213 return 0; | |
| 214 } | |
| 215 | |
| 216 if (self->gworld == NULL) { | |
| 217 PyErr_Format(PyExc_SystemError, | |
| 218 "Cannot wrap a graphics world and pixel map structure around an existing block of memory containing an image, " | |
| 219 "gworld is NULL"); | |
| 220 free(self->pixels.start); | |
| 221 self->pixels.start = NULL; | |
| 222 self->pixels.length = 0; | |
| 223 return 0; | |
| 224 } | |
| 225 | |
| 226 theErr = SGSetGWorld(self->component, (CGrafPtr)self->gworld, NULL); | |
| 227 if (theErr != noErr) { | |
| 228 PyErr_Format(PyExc_SystemError, | |
| 229 "Cannot establishe the graphics port and device for a sequence grabber component"); | |
| 230 free(self->pixels.start); | |
| 231 self->pixels.start = NULL; | |
| 232 self->pixels.length = 0; | |
| 233 return 0; | |
| 234 } | |
| 235 | |
| 236 return 1; | |
| 237 } | |
| 238 | |
| 239 /* Start Capturing */ | |
| 240 int mac_start_capturing(PyCameraObject* self) { | |
| 241 OSErr theErr; | |
| 242 | |
| 243 theErr = SGPrepare(self->component, true, false); | |
| 244 if (theErr != noErr) { | |
| 245 PyErr_Format(PyExc_SystemError, | |
| 246 "Cannot istruct a sequence grabber to get ready to begin a preview or record operation"); | |
| 247 free(self->pixels.start); | |
| 248 self->pixels.start = NULL; | |
| 249 self->pixels.length = 0; | |
| 250 return 0; | |
| 251 } | |
| 252 | |
| 253 theErr = SGStartPreview(self->component); | |
| 254 if (theErr != noErr) { | |
| 255 PyErr_Format(PyExc_SystemError, | |
| 256 "Cannot instruct the sequence grabber to begin processing data from its channels"); | |
| 257 free(self->pixels.start); | |
| 258 self->pixels.start = NULL; | |
| 259 self->pixels.length = 0; | |
| 260 return 0; | |
| 261 } | |
| 262 | |
| 263 return 1; | |
| 264 } | |
| 265 | |
| 266 /* Close the camera component, and stop the image capturing if necessary. */ | |
| 267 int mac_close_device (PyCameraObject* self) { | |
| 268 ComponentResult theErr; | |
| 269 | |
| 270 // Stop recording | |
| 271 if (self->component) | |
| 272 SGStop(self->component); | |
| 273 | |
| 274 // Close sequence grabber component | |
| 275 if (self->component) { | |
| 276 theErr = CloseComponent(self->component); | |
| 277 if (theErr != noErr) { | |
| 278 PyErr_Format(PyExc_SystemError, | |
| 279 "Cannot close sequence grabber component"); | |
| 280 return 0; | |
| 281 } | |
| 282 self->component = NULL; | |
| 283 } | |
| 284 | |
| 285 // Dispose of GWorld | |
| 286 if (self->gworld) { | |
| 287 DisposeGWorld(self->gworld); | |
| 288 self->gworld = NULL; | |
| 289 } | |
| 290 // Dispose of pixels buffer | |
| 291 free(self->pixels.start); | |
| 292 self->pixels.start = NULL; | |
| 293 self->pixels.length = 0; | |
| 294 return 1; | |
| 295 } | |
| 296 | |
| 297 /* Stop capturing. */ | |
| 298 int mac_stop_capturing (PyCameraObject* self) { | |
| 299 OSErr theErr = SGStop(self->component); | |
| 300 if (theErr != noErr) { | |
| 301 PyErr_Format(PyExc_SystemError, | |
| 302 "Could not stop the sequence grabber with previewing"); | |
| 303 return 0; | |
| 304 } | |
| 305 return 1; | |
| 306 } | |
| 307 | |
| 308 /* Read a frame, and put the raw data into a python string. */ | |
| 309 PyObject *mac_read_raw(PyCameraObject *self) { | |
| 310 if (self->gworld == NULL) { | |
| 311 PyErr_Format(PyExc_SystemError, | |
| 312 "Cannot set convert gworld to surface because gworls is 0"); | |
| 313 return 0; | |
| 314 } | |
| 315 | |
| 316 if (mac_camera_idle(self) == 0) { | |
| 317 return 0; | |
| 318 } | |
| 319 | |
| 320 PyObject *raw; | |
| 321 PixMapHandle pixmap_handle = GetGWorldPixMap(self->gworld); | |
| 322 LockPixels(pixmap_handle); | |
|
2143
99afc91b219a
apply camera module patch for Python 3; thanks Christoph Gohlke
Lenard Lindstrom <len-l@telus.net>
parents:
1989
diff
changeset
|
323 raw = Bytes_FromStringAndSize(self->pixels.start, self->pixels.length); |
| 1975 | 324 UnlockPixels(pixmap_handle); |
| 325 return raw; | |
| 326 } | |
| 327 | |
| 328 /* Read a frame from the camera and copy it to a surface. */ | |
| 329 int mac_read_frame(PyCameraObject* self, SDL_Surface* surf) { | |
| 330 if (mac_camera_idle(self) != 0) { | |
| 331 return mac_process_image(self, self->pixels.start, self->pixels.length, surf); | |
| 332 } else { | |
| 333 return 0; | |
| 334 } | |
| 335 } | |
| 336 | |
| 337 // TODO sometimes it is posible to directly grab the image in the desired pixel format, | |
| 338 // but this format needs to be known at the beginning of the initiation of the camera. | |
| 339 int mac_process_image(PyCameraObject* self, const void *image, unsigned int buffer_size, SDL_Surface* surf) { | |
| 340 if (!surf) | |
| 341 return 0; | |
| 342 | |
| 343 void* new_pixels; | |
| 344 if (self->hflip || self->vflip) { | |
| 345 new_pixels = malloc(self->pixels.length); | |
| 346 flip_image(self->pixels.start, | |
| 347 new_pixels, | |
| 348 self->boundsRect.right, | |
| 349 self->boundsRect.bottom, | |
| 350 self->depth, | |
| 351 self->hflip, | |
| 352 self->vflip); | |
| 353 } else { | |
| 354 new_pixels = image; | |
| 355 } | |
| 356 | |
| 357 SDL_LockSurface(surf); | |
| 358 | |
| 359 switch (self->pixelformat) { | |
| 360 case k24RGBPixelFormat: | |
| 361 if (buffer_size >= self->size * 3) { | |
| 362 switch (self->color_out) { | |
| 363 case RGB_OUT: | |
| 364 rgb24_to_rgb(new_pixels, surf->pixels, self->size, surf->format); | |
| 365 break; | |
| 366 case HSV_OUT: | |
| 367 rgb_to_hsv(new_pixels, surf->pixels, self->size, V4L2_PIX_FMT_RGB24, surf->format); | |
| 368 break; | |
| 369 case YUV_OUT: | |
| 370 rgb_to_yuv(new_pixels, surf->pixels, self->size, V4L2_PIX_FMT_RGB24, surf->format); | |
| 371 break; | |
| 372 } | |
| 373 } else { | |
| 374 SDL_UnlockSurface(surf); | |
| 375 free(new_pixels); | |
| 376 return 0; | |
| 377 } | |
| 378 break; | |
| 379 | |
| 380 case kYUVSPixelFormat: | |
| 381 if (buffer_size >= self->size * 2) { | |
| 382 switch (self->color_out) { | |
| 383 case YUV_OUT: | |
| 384 yuyv_to_yuv(new_pixels, surf->pixels, self->size, surf->format); | |
| 385 break; | |
| 386 case RGB_OUT: | |
| 387 yuyv_to_rgb(new_pixels, surf->pixels, self->size, surf->format); | |
| 388 break; | |
| 389 case HSV_OUT: | |
| 390 yuyv_to_rgb(new_pixels, surf->pixels, self->size, surf->format); | |
| 391 rgb_to_hsv(surf->pixels, surf->pixels, self->size, V4L2_PIX_FMT_YUYV, surf->format); | |
| 392 break; | |
| 393 } | |
| 394 } else { | |
| 395 SDL_UnlockSurface(surf); | |
| 396 free(new_pixels); | |
| 397 return 0; | |
| 398 } | |
| 399 break; | |
| 400 } | |
| 401 SDL_UnlockSurface(surf); | |
| 402 if (self->hflip || self->vflip) | |
| 403 free(new_pixels); | |
| 404 | |
| 405 return 1; | |
| 406 } | |
| 407 | |
| 408 /* Put the camera in idle mode. */ | |
| 409 int mac_camera_idle(PyCameraObject* self) { | |
| 410 OSErr theErr = SGIdle(self->component); | |
| 411 if (theErr != noErr) { | |
| 412 PyErr_Format(PyExc_SystemError, "SGIdle failed"); | |
| 413 return 0; | |
| 414 } | |
| 415 | |
| 416 return 1; | |
|
1989
b8611a14022c
moved flip_image into camera_mac since it is only used there and fails to build x platform.
Rene Dudfield <renesd@gmail.com>
parents:
1975
diff
changeset
|
417 } |
|
3073
1c265998cf59
Disable camera_mac if OS X is above version 10.6.
Rene Dudfield <renesd@gmail.com>
parents:
2143
diff
changeset
|
418 |
|
1c265998cf59
Disable camera_mac if OS X is above version 10.6.
Rene Dudfield <renesd@gmail.com>
parents:
2143
diff
changeset
|
419 |
|
1c265998cf59
Disable camera_mac if OS X is above version 10.6.
Rene Dudfield <renesd@gmail.com>
parents:
2143
diff
changeset
|
420 #endif |
