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