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+ }
0 commit comments