forked from mgdm/OpenCV-for-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv_capture.cpp
335 lines (275 loc) · 11.4 KB
/
opencv_capture.cpp
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Michael Maclean <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_opencv.h"
zend_class_entry *opencv_ce_capture;
PHP_OPENCV_API opencv_capture_object* opencv_capture_object_get(zval *zobj TSRMLS_DC) {
opencv_capture_object *pobj = (opencv_capture_object *) zend_object_store_get_object(zobj TSRMLS_CC);
if (pobj->cvptr == NULL) {
php_error(E_ERROR, "Internal surface object missing in %s wrapper, you must call parent::__construct in extended classes", Z_OBJCE_P(zobj)->name);
}
return pobj;
}
void opencv_capture_object_destroy(void *object TSRMLS_DC)
{
opencv_capture_object *capture = (opencv_capture_object *)object;
zend_hash_destroy(capture->std.properties);
FREE_HASHTABLE(capture->std.properties);
if(capture->cvptr != NULL){
cvReleaseCapture(&capture->cvptr);
}
efree(capture);
}
PHP_OPENCV_API zend_object_value opencv_capture_object_new(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
opencv_capture_object *capture;
zval *temp;
capture = (opencv_capture_object *) ecalloc(1, sizeof(opencv_capture_object));
capture->std.ce = ce;
capture->cvptr = NULL;
ALLOC_HASHTABLE(capture->std.properties);
zend_hash_init(capture->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(capture->std.properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &temp, sizeof(zval *));
#else
object_properties_init(&capture->std, ce);
#endif
retval.handle = zend_objects_store_put(capture, NULL, (zend_objects_free_object_storage_t)opencv_capture_object_destroy, NULL TSRMLS_CC);
retval.handlers = zend_get_std_object_handlers();
return retval;
}
PHP_METHOD(OpenCV_Capture, createCameraCapture)
{
long camera;
opencv_capture_object *capture_object;
CvCapture *temp;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &camera) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_RESTORE_ERRORS();
object_init_ex(return_value, opencv_ce_capture);
temp = (CvCapture *) cvCaptureFromCAM(camera);
capture_object = (opencv_capture_object *) zend_object_store_get_object(return_value TSRMLS_CC);
capture_object->cvptr = temp;
php_opencv_throw_exception(TSRMLS_C);
}
/* }}} */
PHP_METHOD(OpenCV_Capture, createFileCapture)
{
const char *filename;
int filename_len;
opencv_capture_object *capture_object;
CvCapture *temp;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_RESTORE_ERRORS();
php_opencv_basedir_check(filename TSRMLS_CC);
object_init_ex(return_value, opencv_ce_capture);
capture_object = (opencv_capture_object *) zend_object_store_get_object(return_value TSRMLS_CC);
temp = (CvCapture *) cvCreateFileCapture(filename);
if (temp == NULL) {
char *error_message = estrdup("Could not open the video file - check it exists and the codec is available");
zend_throw_exception(opencv_ce_cvexception, error_message, 0 TSRMLS_CC);
efree(error_message);
return;
}
capture_object->cvptr = temp;
php_opencv_throw_exception(TSRMLS_C);
}
/* }}} */
PHP_METHOD(OpenCV_Capture, grabFrame)
{
zval *capture_zval;
opencv_capture_object *capture_object;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &capture_zval, opencv_ce_capture) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_ERROR_HANDLING();
capture_object = opencv_capture_object_get(getThis() TSRMLS_CC);
long result = cvGrabFrame(capture_object->cvptr);
php_opencv_throw_exception(TSRMLS_C);
RETURN_LONG(result);
}
PHP_METHOD(OpenCV_Capture, retrieveFrame)
{
zval *capture_zval;
opencv_capture_object *capture_object;
IplImage *temp;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &capture_zval, opencv_ce_capture) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_ERROR_HANDLING();
capture_object = opencv_capture_object_get(getThis() TSRMLS_CC);
temp = cvCloneImage(cvRetrieveFrame(capture_object->cvptr, 0));
php_opencv_make_image_zval(temp, return_value TSRMLS_CC);
php_opencv_throw_exception(TSRMLS_C);
}
PHP_METHOD(OpenCV_Capture, queryFrame)
{
zval *capture_zval;
opencv_capture_object *capture_object;
IplImage *temp;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &capture_zval, opencv_ce_capture) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_ERROR_HANDLING();
capture_object = opencv_capture_object_get(getThis() TSRMLS_CC);
temp = cvCloneImage(cvQueryFrame(capture_object->cvptr));
php_opencv_make_image_zval(temp, return_value TSRMLS_CC);
php_opencv_throw_exception(TSRMLS_C);
}
PHP_METHOD(OpenCV_Capture, getProperty)
{
zval *capture_zval;
opencv_capture_object *capture_object;
IplImage *temp;
double val;
long property;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &capture_zval, opencv_ce_capture, &property) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_ERROR_HANDLING();
capture_object = opencv_capture_object_get(getThis() TSRMLS_CC);
val = cvGetCaptureProperty(capture_object->cvptr, property);
php_opencv_throw_exception(TSRMLS_C);
/* FourCC is special */
if (property == CV_CAP_PROP_FOURCC) {
char *unpacked_val = (char *) &val;
RETURN_STRING(unpacked_val, 1);
}
RETURN_LONG(val);
}
PHP_METHOD(OpenCV_Capture, setProperty)
{
zval *capture_zval;
opencv_capture_object *capture_object;
IplImage *temp;
double val;
long property;
PHP_OPENCV_ERROR_HANDLING();
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Old", &capture_zval, opencv_ce_capture, &property, &val) == FAILURE)
{
PHP_OPENCV_RESTORE_ERRORS();
return;
}
PHP_OPENCV_ERROR_HANDLING();
capture_object = opencv_capture_object_get(getThis() TSRMLS_CC);
val = cvSetCaptureProperty(capture_object->cvptr, property, val);
php_opencv_throw_exception(TSRMLS_C);
}
/* {{{ opencv_capture_methods[] */
const zend_function_entry opencv_capture_methods[] = {
PHP_ME(OpenCV_Capture, createCameraCapture, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(OpenCV_Capture, createFileCapture, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(OpenCV_Capture, grabFrame, NULL, ZEND_ACC_PUBLIC)
PHP_ME(OpenCV_Capture, retrieveFrame, NULL, ZEND_ACC_PUBLIC)
PHP_ME(OpenCV_Capture, queryFrame, NULL, ZEND_ACC_PUBLIC)
PHP_ME(OpenCV_Capture, getProperty, NULL, ZEND_ACC_PUBLIC)
PHP_ME(OpenCV_Capture, setProperty, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(opencv_capture)
{
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, "OpenCV", "Capture", opencv_capture_methods);
opencv_ce_capture = zend_register_internal_class(&ce TSRMLS_CC);
#define REGISTER_CAPTURE_LONG_CONST(const_name, value) \
zend_declare_class_constant_long(opencv_ce_capture, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC); \
REGISTER_LONG_CONSTANT(#value, value, CONST_CS | CONST_PERSISTENT);
REGISTER_CAPTURE_LONG_CONST("PROP_POS_MSEC", CV_CAP_PROP_POS_MSEC);
REGISTER_CAPTURE_LONG_CONST("PROP_POS_FRAMES", CV_CAP_PROP_POS_FRAMES);
REGISTER_CAPTURE_LONG_CONST("PROP_POS_AVI_RATIO", CV_CAP_PROP_POS_AVI_RATIO);
REGISTER_CAPTURE_LONG_CONST("PROP_FRAME_WIDTH", CV_CAP_PROP_FRAME_WIDTH);
REGISTER_CAPTURE_LONG_CONST("PROP_FRAME_HEIGHT", CV_CAP_PROP_FRAME_HEIGHT);
REGISTER_CAPTURE_LONG_CONST("PROP_FPS", CV_CAP_PROP_FPS);
REGISTER_CAPTURE_LONG_CONST("PROP_FOURCC", CV_CAP_PROP_FOURCC);
REGISTER_CAPTURE_LONG_CONST("PROP_FRAME_COUNT", CV_CAP_PROP_FRAME_COUNT);
REGISTER_CAPTURE_LONG_CONST("PROP_FORMAT", CV_CAP_PROP_FORMAT);
REGISTER_CAPTURE_LONG_CONST("PROP_MODE", CV_CAP_PROP_MODE);
REGISTER_CAPTURE_LONG_CONST("PROP_BRIGHTNESS", CV_CAP_PROP_BRIGHTNESS);
REGISTER_CAPTURE_LONG_CONST("PROP_CONTRAST", CV_CAP_PROP_CONTRAST);
REGISTER_CAPTURE_LONG_CONST("PROP_SATURATION", CV_CAP_PROP_SATURATION);
REGISTER_CAPTURE_LONG_CONST("PROP_HUE", CV_CAP_PROP_HUE);
REGISTER_CAPTURE_LONG_CONST("PROP_GAIN", CV_CAP_PROP_GAIN);
REGISTER_CAPTURE_LONG_CONST("PROP_EXPOSURE", CV_CAP_PROP_EXPOSURE);
REGISTER_CAPTURE_LONG_CONST("PROP_CONVERT_RGB", CV_CAP_PROP_CONVERT_RGB);
#ifdef CV_CAP_PROP_WHITE_BALANCE
REGISTER_CAPTURE_LONG_CONST("PROP_WHITE_BALANCE", CV_CAP_PROP_WHITE_BALANCE);
#endif
#ifdef CV_CAP_PROP_WHITE_BALANCE_BLUE_U
REGISTER_CAPTURE_LONG_CONST("PROP_WHITE_BALANCE_BLUE_U", CV_CAP_PROP_WHITE_BALANCE_BLUE_U);
#endif
REGISTER_CAPTURE_LONG_CONST("PROP_RECTIFICATION", CV_CAP_PROP_RECTIFICATION);
#ifdef CV_CAP_PROP_MONOCROME
REGISTER_CAPTURE_LONG_CONST("PROP_MONOCHROME", CV_CAP_PROP_MONOCROME);
#endif
#ifdef CV_CAP_PROP_SHARPNESS
REGISTER_CAPTURE_LONG_CONST("PROP_SHARPNESS", CV_CAP_PROP_SHARPNESS);
#endif
#ifdef CV_CAP_PROP_AUTO_EXPOSURE
REGISTER_CAPTURE_LONG_CONST("PROP_AUTO_EXPOSURE", CV_CAP_PROP_AUTO_EXPOSURE);
#endif
#ifdef CV_CAP_PROP_GAMMA
REGISTER_CAPTURE_LONG_CONST("PROP_GAMMA", CV_CAP_PROP_GAMMA);
#endif
#ifdef CV_CAP_PROP_TEMPERATURE
REGISTER_CAPTURE_LONG_CONST("PROP_TEMPERATURE", CV_CAP_PROP_TEMPERATURE);
#endif
#ifdef CV_CAP_PROP_GAMMA
REGISTER_CAPTURE_LONG_CONST("PROP_GAMMA", CV_CAP_PROP_GAMMA);
#endif
#ifdef CV_CAP_PROP_TRIGGER
REGISTER_CAPTURE_LONG_CONST("PROP_TRIGGER", CV_CAP_PROP_TRIGGER);
#endif
#ifdef CV_CAP_PROP_TRIGGER_DELAY
REGISTER_CAPTURE_LONG_CONST("PROP_TRIGGER_DELAY", CV_CAP_PROP_TRIGGER_DELAY);
#endif
#ifdef CV_CAP_PROP_WHITE_BALANCE_RED_V
REGISTER_CAPTURE_LONG_CONST("PROP_WHITE_BALANCE_RED_V", CV_CAP_PROP_WHITE_BALANCE_RED_V);
#endif
#ifdef CV_CAP_PROP_MAX_DC1394
REGISTER_CAPTURE_LONG_CONST("PROP_MAX_DC1394", CV_CAP_PROP_MAX_DC1394);
#endif
return SUCCESS;
}
/* }}} */