Skip to content

Commit 3b7d414

Browse files
author
Steve Edwards
committed
Added imdecode support
1 parent a1f33ef commit 3b7d414

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

opencv.cc

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ const zend_function_entry opencv_functions[] = {
250250
ZEND_NS_NAMED_FE(OPENCV_DNN_NS, readNetFromCaffe, ZEND_FN(opencv_dnn_read_net_from_caffe), NULL)
251251
ZEND_NS_NAMED_FE(OPENCV_DNN_NS, readNetFromTorch, ZEND_FN(opencv_dnn_read_net_from_torch), NULL)
252252
ZEND_NS_NAMED_FE(OPENCV_DNN_NS, readNetFromTensorflow, ZEND_FN(opencv_dnn_read_net_from_tensorflow), NULL)
253+
ZEND_NS_NAMED_FE(OPENCV_NS, imdecode, ZEND_FN(opencv_imdecode), NULL)
253254
PHP_FE_END /* Must be the last line in opencv_functions[] */
254255
};
255256
/* }}} */

source/opencv2/opencv_imgcodecs.cc

+34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,40 @@ PHP_FUNCTION(opencv_imwrite){
8080
RETURN_TRUE;
8181
}
8282

83+
/**
84+
* CV\imdecode
85+
* @param execute_data
86+
* @param return_value
87+
*/
88+
PHP_FUNCTION(opencv_imdecode)
89+
{
90+
long flags;
91+
char *buf;
92+
long buf_len;
93+
flags = IMREAD_COLOR;//flags default value
94+
95+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &buf,&buf_len, &flags) == FAILURE) {
96+
RETURN_NULL();
97+
}
98+
zval instance;
99+
object_init_ex(&instance,opencv_mat_ce);
100+
opencv_mat_object *obj = Z_PHP_MAT_OBJ_P(&instance);
101+
102+
Mat im = imdecode(Mat(1, buf_len, CV_8UC1, buf), CV_LOAD_IMAGE_UNCHANGED);
103+
if(im.empty() || !im.data){
104+
char *error_message = (char*)malloc(strlen("Can not load image") + 1);
105+
strcpy(error_message,"Can not load image");
106+
opencv_throw_exception(error_message);//throw exception
107+
free(error_message);
108+
}
109+
110+
obj->mat = new Mat(im);
111+
112+
//update php Mat object property
113+
opencv_mat_update_property_by_c_mat(&instance, obj->mat);
114+
115+
RETURN_ZVAL(&instance,0,0); //return php Mat object
116+
}
83117

84118
void opencv_imgcodecs_init(int module_number)
85119
{

source/opencv2/opencv_imgcodecs.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
extern void opencv_imgcodecs_init(int module_number);
2121
PHP_FUNCTION(opencv_imread);
2222
PHP_FUNCTION(opencv_imwrite);
23+
PHP_FUNCTION(opencv_imdecode);
2324

2425
#endif //OPENCV_OPENCV_IMCODECS_H

0 commit comments

Comments
 (0)