Skip to content

Commit b812480

Browse files
committed
Merge branch 'dev'
2 parents abdad14 + 3de28f1 commit b812480

File tree

11 files changed

+500
-13
lines changed

11 files changed

+500
-13
lines changed

config.m4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ if test "$PHP_OPENCV" != "no"; then
4242
source/opencv2/core/opencv_type.cc \
4343
source/opencv2/opencv_core.cc \
4444
source/opencv2/opencv_imgproc.cc \
45-
source/opencv2/core/opencv_base.cc"
45+
source/opencv2/core/opencv_base.cc \
46+
source/opencv2/core/opencv_persistence.cc \
47+
source/opencv2/opencv_objdetect.cc"
4648

4749

4850
PHP_NEW_EXTENSION(opencv, $opencv_source_file, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

opencv.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern "C" {
3737
#include "opencv_exception.h"
3838
#include "source/opencv2/opencv_core.h"
3939
#include "source/opencv2/core/opencv_base.h"
40+
#include "source/opencv2/core/opencv_persistence.h"
41+
#include "source/opencv2/opencv_objdetect.h"
4042

4143
/* If you declare any globals in php_opencv.h uncomment this:
4244
ZEND_DECLARE_MODULE_GLOBALS(opencv)
@@ -64,7 +66,7 @@ PHP_INI_END()
6466
Return a string to confirm that the module is compiled in */
6567
PHP_FUNCTION(confirm_opencv_compiled)
6668
{
67-
char *arg = NULL;
69+
char *arg = NULL;
6870
size_t arg_len, len;
6971
zend_string *strg;
7072

@@ -114,6 +116,8 @@ PHP_MINIT_FUNCTION(opencv)
114116
opencv_imgproc_init(module_number);
115117
opencv_core_init(module_number);
116118
opencv_border_types_init(module_number);
119+
opencv_file_storage_init(module_number);
120+
opencv_objdetect_init(module_number);
117121

118122
return SUCCESS;
119123
}

php_opencv.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ using namespace cv;
5050

5151
#define OPENCV_NS "CV"
5252

53+
#define OPENCV_CONNECT(text1,text2) text1##text2
54+
55+
56+
//#define OPENCV_BEGIN_ARG_INFO ZEND_BEGIN_ARG_INFO
57+
//#define OPENCV_BEGIN_ARG_INFO_EX ZEND_BEGIN_ARG_INFO_EX
58+
5359
/*
5460
Declare any global variables you may need between the BEGIN
5561
and END macros here:

source/opencv2/core/opencv_mat.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ zend_class_entry *opencv_mat_ce;
1515
* @param type
1616
* @return
1717
*/
18-
zend_object* mat_create_handler(zend_class_entry *type)
18+
zend_object* opencv_mat_create_handler(zend_class_entry *type)
1919
{
2020
int size = sizeof(opencv_mat_object);
2121
opencv_mat_object *obj = (opencv_mat_object *)ecalloc(1,size);
@@ -360,9 +360,9 @@ PHP_METHOD(opencv_mat, convert_to){
360360

361361

362362
/**
363-
* mat_methods[]
363+
* opencv_mat_methods[]
364364
*/
365-
const zend_function_entry mat_methods[] = {
365+
const zend_function_entry opencv_mat_methods[] = {
366366
PHP_ME(opencv_mat, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
367367
PHP_ME(opencv_mat, type, NULL, ZEND_ACC_PUBLIC)
368368
PHP_ME(opencv_mat, depth, NULL, ZEND_ACC_PUBLIC)
@@ -410,10 +410,10 @@ void opencv_mat_write_property(zval *object, zval *member, zval *value, void **c
410410
*/
411411
void opencv_mat_init(void){
412412
zend_class_entry ce;
413-
INIT_NS_CLASS_ENTRY(ce,OPENCV_NS, "Mat", mat_methods);
413+
INIT_NS_CLASS_ENTRY(ce,OPENCV_NS, "Mat", opencv_mat_methods);
414414
opencv_mat_ce = zend_register_internal_class(&ce);
415415

416-
opencv_mat_ce->create_object = mat_create_handler;
416+
opencv_mat_ce->create_object = opencv_mat_create_handler;
417417
memcpy(&opencv_mat_object_handlers,
418418
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
419419
opencv_mat_object_handlers.clone_obj = NULL;
Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
#include "../../../php_opencv.h"
2+
#include "opencv_persistence.h"
3+
#include "opencv_mat.h"
4+
#include "../../../opencv_exception.h"
5+
6+
zend_class_entry *opencv_file_storage_ce;
7+
8+
zend_object_handlers opencv_file_storage_object_handlers;
9+
10+
zend_object* opencv_file_storage_create_handler(zend_class_entry *type){
11+
size_t size = sizeof(opencv_file_storage_object);
12+
opencv_file_storage_object *obj = (opencv_file_storage_object *)ecalloc(1,size);
13+
memset(obj, 0, sizeof(opencv_file_storage_object));
14+
zend_object_std_init(&obj->std, type);
15+
object_properties_init(&obj->std, type);
16+
obj->std.ce = type;
17+
obj->std.handlers = &opencv_file_storage_object_handlers;
18+
return &obj->std;
19+
}
20+
21+
/**
22+
* //todo encoding param
23+
* CV\FileStorage __construct
24+
* @param execute_data
25+
* @param return_value
26+
*/
27+
PHP_METHOD(opencv_file_storage, __construct){
28+
char *source;
29+
long source_len = 0, flags;
30+
FileStorage *fs;
31+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &source, &source_len, &flags) == FAILURE) {
32+
RETURN_NULL();
33+
}
34+
if(source_len == 0){
35+
fs = new FileStorage();
36+
}else{
37+
fs = new FileStorage(source, (int)flags);
38+
}
39+
opencv_file_storage_object *obj = Z_PHP_FILE_STORAGE_OBJ_P(getThis());
40+
obj->fileStorage = fs;
41+
}
42+
43+
/**
44+
* //todo encoding param
45+
* CV\FileStorage open
46+
* @param execute_data
47+
* @param return_value
48+
*/
49+
PHP_METHOD(opencv_file_storage, open){
50+
char *source;
51+
long source_len = 0, flags;
52+
FileStorage *fs;
53+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &source, &source_len, &flags) == FAILURE) {
54+
RETURN_NULL();
55+
}
56+
opencv_file_storage_object *obj = Z_PHP_FILE_STORAGE_OBJ_P(getThis());
57+
obj->fileStorage->open(source, (int)flags);
58+
RETURN_NULL();
59+
}
60+
61+
PHP_METHOD(opencv_file_storage, write){
62+
char *name;
63+
long name_len = 0;
64+
char *error_message;
65+
zval *val_zval;
66+
FileStorage fs;
67+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name, &name_len, &val_zval) == FAILURE) {
68+
RETURN_NULL();
69+
}
70+
//todo check name len
71+
opencv_file_storage_object *obj = Z_PHP_FILE_STORAGE_OBJ_P(getThis());
72+
fs = *(obj->fileStorage);
73+
again:
74+
switch (Z_TYPE_P(val_zval)) {
75+
case IS_FALSE:
76+
try{
77+
fs<< name << false;
78+
}catch (Exception e){
79+
opencv_throw_exception(e.what());
80+
}
81+
case IS_TRUE:
82+
try{
83+
fs<< name << true;
84+
}catch (Exception e){
85+
opencv_throw_exception(e.what());
86+
}
87+
break;
88+
case IS_LONG:
89+
try{
90+
int long_val = (int)zval_get_long(val_zval);
91+
fs<< name << long_val;
92+
}catch (Exception e){
93+
opencv_throw_exception(e.what());
94+
}
95+
break;
96+
case IS_DOUBLE:
97+
try{
98+
double double_val = (double)zval_get_double(val_zval);
99+
fs<< name << double_val;
100+
}catch (Exception e){
101+
opencv_throw_exception(e.what());
102+
}
103+
break;
104+
case IS_STRING:
105+
//todo check name len and value len
106+
try{
107+
String string_val=(String)ZSTR_VAL(zval_get_string(val_zval));
108+
fs<< name << string_val;
109+
}catch (Exception e){
110+
opencv_throw_exception(e.what());
111+
}
112+
// if (Z_STRLEN_P(op) > 1 || (Z_STRLEN_P(op) && Z_STRVAL_P(op)[0] != '0')) {
113+
// result = 1;
114+
// }
115+
break;
116+
case IS_ARRAY:
117+
//todo only one type array
118+
error_message = (char*)malloc(strlen("Can't write file on array.")+ 1);
119+
strcpy(error_message,"Can't write file object on array.");
120+
opencv_throw_exception(error_message);
121+
free(error_message);
122+
// if (zend_hash_num_elements(Z_ARRVAL_P(op))) {
123+
// result = 1;
124+
// }
125+
break;
126+
case IS_OBJECT:
127+
//only Mat
128+
if(Z_OBJCE_P(val_zval) == opencv_mat_ce){
129+
opencv_mat_object *mat_object = Z_PHP_MAT_OBJ_P(val_zval);
130+
fs<< name << *(mat_object->mat);
131+
}else{
132+
error_message = (char*)malloc(strlen("Can't write file object only Mat.")+ 1);
133+
strcpy(error_message,"Can't write file object only Mat.");
134+
opencv_throw_exception(error_message);
135+
free(error_message);
136+
}
137+
break;
138+
case IS_RESOURCE:
139+
error_message = (char*)malloc(strlen("Can't write file on resource.")+ 1);
140+
strcpy(error_message,"Can't write file on resource.");
141+
opencv_throw_exception(error_message);
142+
free(error_message);
143+
case IS_REFERENCE:
144+
val_zval = Z_REFVAL_P(val_zval);
145+
goto again;
146+
break;
147+
default:
148+
error_message = (char*)malloc(strlen("Can't write file on unknow type.")+ 1);
149+
strcpy(error_message,"Can't write file on unknow type.");
150+
opencv_throw_exception(error_message);
151+
free(error_message);
152+
break;
153+
}
154+
RETURN_NULL();
155+
156+
}
157+
158+
PHP_METHOD(opencv_file_storage, read){
159+
char *name;
160+
long name_len = 0, value_type;
161+
char *error_message;
162+
FileStorage fs;
163+
Mat mat_val;
164+
opencv_mat_object *mat_object;
165+
char* char_val;
166+
String string_val;
167+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl", &name, &name_len, &value_type) == FAILURE) {
168+
RETURN_NULL();
169+
}
170+
opencv_file_storage_object *obj = Z_PHP_FILE_STORAGE_OBJ_P(getThis());
171+
fs = *(obj->fileStorage);
172+
switch (value_type) {
173+
case 1://int
174+
int int_val;
175+
fs[name]>>int_val;
176+
RETURN_LONG((long)int_val);
177+
break;
178+
case 2://double
179+
double double_val;
180+
fs[name]>>double_val;
181+
RETURN_DOUBLE(double_val);
182+
break;
183+
case 3://string
184+
fs[name] >> string_val;
185+
char_val = const_cast<char*>(string_val.c_str());
186+
RETURN_STRING(char_val);
187+
break;
188+
case 4://bool
189+
bool bool_val;
190+
fs[name] >> bool_val;
191+
RETURN_BOOL(bool_val);
192+
break;
193+
case 5://Mat
194+
fs[name] >> mat_val;
195+
zval mat_instance;
196+
object_init_ex(&mat_instance,opencv_mat_ce);
197+
mat_object = Z_PHP_MAT_OBJ_P(&mat_instance);
198+
mat_object->mat = new Mat(mat_val);
199+
opencv_mat_update_property_by_c_mat(&mat_instance, mat_object->mat);
200+
RETURN_ZVAL(&mat_instance,0,0);
201+
break;
202+
default:
203+
RETURN_NULL();
204+
break;
205+
}
206+
}
207+
208+
PHP_METHOD(opencv_file_storage, release){
209+
opencv_file_storage_object *obj = Z_PHP_FILE_STORAGE_OBJ_P(getThis());
210+
obj->fileStorage->release();
211+
RETURN_NULL();
212+
}
213+
214+
/**
215+
* opencv_file_storage_methods[]
216+
*/
217+
const zend_function_entry opencv_file_storage_methods[] = {
218+
PHP_ME(opencv_file_storage, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
219+
PHP_ME(opencv_file_storage, open, NULL, ZEND_ACC_PUBLIC)
220+
PHP_ME(opencv_file_storage, write, NULL, ZEND_ACC_PUBLIC)
221+
PHP_ME(opencv_file_storage, read, NULL, ZEND_ACC_PUBLIC)
222+
PHP_ME(opencv_file_storage, release, NULL, ZEND_ACC_PUBLIC)
223+
PHP_FE_END
224+
};
225+
/* }}} */
226+
227+
zval *opencv_file_storage_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv){
228+
// std::cout<<"opencv_file_storage_read_property"<<std::endl;
229+
return std_object_handlers.read_property(object, member, type, cache_slot, rv);
230+
231+
}
232+
233+
void opencv_file_storage_consts_init(int module_number){
234+
//! file storage mode
235+
//@see cv::FileStorage::Mode
236+
zend_declare_class_constant_long(opencv_file_storage_ce,"READ",sizeof("READ")-1,FileStorage::READ);
237+
zend_declare_class_constant_long(opencv_file_storage_ce,"WRITE",sizeof("WRITE")-1,FileStorage::WRITE);
238+
zend_declare_class_constant_long(opencv_file_storage_ce,"APPEND",sizeof("APPEND")-1,FileStorage::APPEND);
239+
zend_declare_class_constant_long(opencv_file_storage_ce,"MEMORY",sizeof("MEMORY")-1,FileStorage::MEMORY);
240+
241+
zend_declare_class_constant_long(opencv_file_storage_ce,"FORMAT_MASK",sizeof("FORMAT_MASK")-1,FileStorage::FORMAT_MASK);
242+
zend_declare_class_constant_long(opencv_file_storage_ce,"FORMAT_AUTO",sizeof("FORMAT_AUTO")-1,FileStorage::FORMAT_AUTO);
243+
zend_declare_class_constant_long(opencv_file_storage_ce,"FORMAT_XML",sizeof("FORMAT_XML")-1,FileStorage::FORMAT_XML);
244+
zend_declare_class_constant_long(opencv_file_storage_ce,"FORMAT_YAML",sizeof("FORMAT_YAML")-1,FileStorage::FORMAT_YAML);
245+
zend_declare_class_constant_long(opencv_file_storage_ce,"FORMAT_JSON",sizeof("FORMAT_JSON")-1,FileStorage::FORMAT_JSON);
246+
}
247+
248+
/**
249+
* FileStorage class Init
250+
*/
251+
void opencv_file_storage_init(int module_number){
252+
zend_class_entry ce;
253+
INIT_NS_CLASS_ENTRY(ce,OPENCV_NS, "FileStorage", opencv_file_storage_methods);
254+
opencv_file_storage_ce = zend_register_internal_class(&ce);
255+
256+
// zend_class_implements(&opencv_file_storage_ce,1,zend_ce_arrayaccess);
257+
258+
opencv_file_storage_ce->create_object = opencv_file_storage_create_handler;
259+
memcpy(&opencv_file_storage_object_handlers,
260+
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
261+
opencv_file_storage_object_handlers.clone_obj = NULL;
262+
// opencv_file_storage_object_handlers.write_property = opencv_mat_write_property;
263+
opencv_file_storage_object_handlers.read_property = opencv_file_storage_read_property;
264+
265+
// zend_declare_property_null(opencv_mat_ce,"type",sizeof("type") - 1,ZEND_ACC_PRIVATE);//private Mat->type
266+
// opencv_mat_object_handlers.free_obj = opencv_mat_free_obj;
267+
opencv_file_storage_consts_init(module_number);
268+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef OPENCV_OPENCV_PERSISTENCE_H
2+
#define OPENCV_OPENCV_PERSISTENCE_H
3+
4+
#include "../../../php_opencv.h"
5+
6+
extern zend_class_entry *opencv_file_storage_ce;
7+
8+
typedef struct _opencv_file_storage_object{
9+
zend_object std;
10+
FileStorage *fileStorage;
11+
}opencv_file_storage_object;
12+
13+
#define Z_PHP_FILE_STORAGE_OBJ_P(zv) get_file_storage_obj(Z_OBJ_P(zv))
14+
15+
void opencv_file_storage_init(int module_number);
16+
17+
/**
18+
* @param obj
19+
* @return
20+
*/
21+
static inline opencv_file_storage_object* get_file_storage_obj(zend_object *obj) {
22+
return (opencv_file_storage_object*)((char*)(obj) - XtOffsetOf(opencv_file_storage_object, std));
23+
}
24+
25+
#endif //OPENCV_OPENCV_PERSISTENCE_H

0 commit comments

Comments
 (0)