@@ -54,6 +54,107 @@ void opencv_mat_update_property_by_c_mat(zval *z,Mat *mat){
5454 zend_update_property_long (opencv_mat_ce, z, " cols" , sizeof (" cols" )-1 , mat->cols );
5555 zend_update_property_long (opencv_mat_ce, z, " dims" , sizeof (" dims" )-1 , mat->dims );
5656 zend_update_property_long (opencv_mat_ce, z, " type" , sizeof (" type" )-1 , mat->type ());
57+ // zend_update_property_long(opencv_mat_ce, z, "depth", sizeof("depth")-1, mat->depth());
58+
59+ if (mat->dims > 2 ) {
60+ zval shape_zval;
61+ array_init (&shape_zval);
62+ for (int i = 0 ; i < mat->dims ; i++)
63+ {
64+ add_next_index_long (&shape_zval, mat->size .p [i]);
65+ }
66+ zend_update_property (opencv_mat_ce, z, " shape" , sizeof (" shape" )-1 , &shape_zval);
67+ }
68+ }
69+
70+ void opencv_mat_at (Mat *mat, int *idx, long channel, zval *value_zval, long *return_long_val, double *return_double_val) {
71+ uchar *return_uchar_val;
72+ schar *return_schar_val;
73+ ushort *return_ushort_val;
74+ short *return_short_val;
75+ int *return_int_val;
76+ float *return_float_val;
77+
78+ switch (mat->type ()) {
79+ // CV_8U
80+ case CV_8UC1: return_uchar_val = &mat->at <uchar>(idx); break ;
81+ case CV_8UC2: return_uchar_val = &mat->at <Vec2b>(idx)[channel]; break ;
82+ case CV_8UC3: return_uchar_val = &mat->at <Vec3b>(idx)[channel]; break ;
83+ case CV_8UC4: return_uchar_val = &mat->at <Vec4b>(idx)[channel]; break ;
84+
85+ // CV_8S
86+ case CV_8SC1: return_schar_val = &mat->at <schar>(idx); break ;
87+ case CV_8SC2: return_schar_val = &mat->at <Vec<schar, 2 >>(idx)[channel]; break ;
88+ case CV_8SC3: return_schar_val = &mat->at <Vec<schar, 3 >>(idx)[channel]; break ;
89+ case CV_8SC4: return_schar_val = &mat->at <Vec<schar, 4 >>(idx)[channel]; break ;
90+
91+ // CV_16U
92+ case CV_16UC1: return_ushort_val = &mat->at <ushort>(idx); break ;
93+ case CV_16UC2: return_ushort_val = &mat->at <Vec2w>(idx)[channel]; break ;
94+ case CV_16UC3: return_ushort_val = &mat->at <Vec3w>(idx)[channel]; break ;
95+ case CV_16UC4: return_ushort_val = &mat->at <Vec4w>(idx)[channel]; break ;
96+
97+ // CV_16S
98+ case CV_16SC1: return_short_val = &mat->at <short >(idx); break ;
99+ case CV_16SC2: return_short_val = &mat->at <Vec2s>(idx)[channel]; break ;
100+ case CV_16SC3: return_short_val = &mat->at <Vec3s>(idx)[channel]; break ;
101+ case CV_16SC4: return_short_val = &mat->at <Vec4s>(idx)[channel]; break ;
102+
103+ // CV_32S
104+ case CV_32SC1: return_int_val = &mat->at <int >(idx); break ;
105+ case CV_32SC2: return_int_val = &mat->at <Vec2i>(idx)[channel]; break ;
106+ case CV_32SC3: return_int_val = &mat->at <Vec3i>(idx)[channel]; break ;
107+ case CV_32SC4: return_int_val = &mat->at <Vec4i>(idx)[channel]; break ;
108+
109+ // CV_32F
110+ case CV_32FC1: return_float_val = &mat->at <float >(idx); break ;
111+ case CV_32FC2: return_float_val = &mat->at <Vec2f>(idx)[channel]; break ;
112+ case CV_32FC3: return_float_val = &mat->at <Vec3f>(idx)[channel]; break ;
113+ case CV_32FC4: return_float_val = &mat->at <Vec4f>(idx)[channel]; break ;
114+
115+ // CV_64F
116+ case CV_64FC1: return_double_val = &mat->at <double >(idx); break ;
117+ case CV_64FC2: return_double_val = &mat->at <Vec2d>(idx)[channel]; break ;
118+ case CV_64FC3: return_double_val = &mat->at <Vec3d>(idx)[channel]; break ;
119+ case CV_64FC4: return_double_val = &mat->at <Vec4d>(idx)[channel]; break ;
120+
121+ default : opencv_throw_exception (" Wrong Mat type" ); break ;
122+ }
123+
124+ // get px value
125+ switch (mat->depth ()){
126+ case CV_8U: *return_long_val = *return_uchar_val; break ;
127+ case CV_8S: *return_long_val = *return_schar_val; break ;
128+ case CV_16U: *return_long_val = *return_ushort_val; break ;
129+ case CV_16S: *return_long_val = *return_short_val; break ;
130+ case CV_32S: *return_long_val = *return_int_val; break ;
131+ case CV_32F: *return_double_val = *return_float_val; break ;
132+ case CV_64F: break ;
133+
134+ default : opencv_throw_exception (" Wrong Mat type" ); break ;
135+ }
136+
137+ // set px value
138+ if (value_zval != NULL ) {
139+ switch (mat->depth ()){
140+ case CV_32F:
141+ case CV_64F: convert_to_double (value_zval); break ;
142+ default : convert_to_long (value_zval); break ;
143+ }
144+
145+ zend_long value = Z_LVAL_P (value_zval);
146+ switch (mat->depth ()){
147+ case CV_8U: *return_uchar_val = saturate_cast<uchar>(Z_LVAL_P (value_zval)); break ;
148+ case CV_8S: *return_schar_val = saturate_cast<schar>(Z_LVAL_P (value_zval)); break ;
149+ case CV_16U: *return_ushort_val = saturate_cast<ushort>(Z_LVAL_P (value_zval)); break ;
150+ case CV_16S: *return_short_val = saturate_cast<short >(Z_LVAL_P (value_zval)); break ;
151+ case CV_32S: *return_int_val = saturate_cast<int >(Z_LVAL_P (value_zval)); break ;
152+ case CV_32F: *return_float_val = saturate_cast<float >(Z_DVAL_P (value_zval)); break ;
153+ case CV_64F: *return_double_val = saturate_cast<double >(Z_DVAL_P (value_zval)); break ;
154+
155+ default : opencv_throw_exception (" Wrong Mat type" ); break ;
156+ }
157+ }
57158}
58159
59160/* *
@@ -363,7 +464,6 @@ PHP_METHOD(opencv_mat, copy_to)
363464}
364465
365466/* *
366- * //todo int,fload,double
367467 * CV\Mat->at
368468 * @param execute_data
369469 * @param return_value
@@ -376,78 +476,89 @@ PHP_METHOD(opencv_mat, at)
376476 if (zend_parse_parameters (ZEND_NUM_ARGS (), " lll|z" , &row, &col, &channel, &value_zval) == FAILURE) {
377477 RETURN_NULL ();
378478 }
479+
379480 opencv_mat_object *this_object = Z_PHP_MAT_OBJ_P (getThis ());
380- if (value_zval == NULL ){
381- // get px value
382- switch (this_object->mat ->channels ()){
383- case 1 :
384- this_object->mat ->at <uchar>((int )row,(int )col);
385- break ;
386- case 2 :
387- RETURN_LONG (this_object->mat ->at <Vec2b>((int )row,(int )col)[channel]);
388- break ;
389- case 3 :
390- RETURN_LONG (this_object->mat ->at <Vec3b>((int )row,(int )col)[channel]);
391- break ;
392- case 4 :
393- RETURN_LONG (this_object->mat ->at <Vec4b>((int )row,(int )col)[channel]);
394- break ;
395- default :
396- opencv_throw_exception (" Get Mat px only channel in 1,2,3,4." );
397- break ;
398- }
481+ zval *idx_zval;
482+ int *idx;
483+ long return_long_val;
484+ double return_double_val;
485+
486+ idx = new int (2 );
487+ idx[0 ] = row;
488+ idx[1 ] = col;
489+
490+ opencv_mat_at (this_object->mat , idx, channel, value_zval, &return_long_val, &return_double_val);
491+
492+ switch (this_object->mat ->depth ()){
493+ case CV_32F:
494+ case CV_64F:
495+ RETURN_DOUBLE (return_double_val);
496+ default :
497+ RETURN_LONG (return_long_val);
498+ }
499+ }
500+
501+ /* *
502+ * CV\Mat->atIdx
503+ * @param execute_data
504+ * @param return_value
505+ */
506+ PHP_METHOD (opencv_mat, atIdx) // multi dimensions support
507+ {
508+ long channel;
509+ zval *value_zval = NULL ;
510+ zval *idx_zval;
511+ int *idx = nullptr ;
399512
513+ if (zend_parse_parameters_ex (ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS (), " al|z" , &idx_zval, &channel, &value_zval) == FAILURE) {
514+ RETURN_NULL ();
515+ }
400516
401- }else {
402- // set px value
403- convert_to_long (value_zval);
404- zend_long value = Z_LVAL_P (value_zval);
405- switch (this_object->mat ->depth ()){
406- case CV_8U:
407- switch (this_object->mat ->channels ()){
408- case 1 :
409- this_object->mat ->at <uchar>((int )row,(int )col) = saturate_cast<uchar>(value);
410- break ;
411- case 2 :
412- this_object->mat ->at <Vec2b>((int )row,(int )col)[channel]=saturate_cast<uchar>(value);
413- break ;
414- case 3 :
415- this_object->mat ->at <Vec3b>((int )row,(int )col)[channel]=saturate_cast<uchar>(value);
416- break ;
417- case 4 :
418- this_object->mat ->at <Vec4b>((int )row,(int )col)[channel]=saturate_cast<uchar>(value);
419- break ;
420- default :
421- opencv_throw_exception (" Get Mat px only channel in 1,2,3,4." );
422- break ;
423- }
424- break ;
425- default :
426- switch (this_object->mat ->channels ()){
427- case 1 :
428- this_object->mat ->at <uchar>((int )row,(int )col) = saturate_cast<char >(value);
429- break ;
430- case 2 :
431- this_object->mat ->at <Vec2b>((int )row,(int )col)[channel]=saturate_cast<char >(value);
432- break ;
433- case 3 :
434- this_object->mat ->at <Vec3b>((int )row,(int )col)[channel]=saturate_cast<char >(value);
435- break ;
436- case 4 :
437- this_object->mat ->at <Vec4b>((int )row,(int )col)[channel]=saturate_cast<char >(value);
438- break ;
439- default :
440- opencv_throw_exception (" Get Mat px only channel in 1,2,3,4." );
441- break ;
517+ opencv_mat_object *this_object = Z_PHP_MAT_OBJ_P (getThis ());
518+
519+ unsigned long idx_count = zend_hash_num_elements (Z_ARRVAL_P (idx_zval));
520+
521+ if (idx_count == 0 ) {
522+ opencv_throw_exception (" array lenght must be >=1" );
523+ RETURN_NULL ();
524+ } else if (idx_count != this_object->mat ->dims ) {
525+ opencv_throw_exception (" array lenght must be = dims" );
526+ RETURN_NULL ();
527+ }
528+
529+ idx = new int (idx_count);
530+ zval *array_val_zval;
531+ zend_ulong _h;
532+
533+ ZEND_HASH_FOREACH_NUM_KEY_VAL (Z_ARRVAL_P (idx_zval),_h,array_val_zval){
534+ again:
535+ if (Z_TYPE_P (array_val_zval) == IS_LONG){
536+ // idx.push_back((int)zval_get_long(array_val_zval));
537+ idx[_h] = (int )zval_get_long (array_val_zval);
538+ }else if (Z_TYPE_P (array_val_zval) == IS_REFERENCE){
539+ array_val_zval = Z_REFVAL_P (array_val_zval);
540+ goto again;
541+ } else {
542+ opencv_throw_exception (" array value just number." );
543+ RETURN_NULL ();
442544 }
443- break ;
444- }
545+ }ZEND_HASH_FOREACH_END ();
445546
547+ long return_long_val;
548+ double return_double_val;
549+
550+ opencv_mat_at (this_object->mat , idx, channel, value_zval, &return_long_val, &return_double_val);
551+
552+
553+ switch (this_object->mat ->depth ()){
554+ case CV_32F:
555+ case CV_64F:
556+ RETURN_DOUBLE (return_double_val);
557+ default :
558+ RETURN_LONG (return_long_val);
446559 }
447- RETURN_NULL ();
448560}
449561
450-
451562ZEND_BEGIN_ARG_INFO_EX (opencv_mat_convert_to_arginfo, 0 , 0 , 4 )
452563 ZEND_ARG_INFO(1 , dst)
453564 ZEND_ARG_INFO(0 , rtype)
@@ -656,6 +767,7 @@ const zend_function_entry opencv_mat_methods[] = {
656767 PHP_ME (opencv_mat, row, NULL , ZEND_ACC_PUBLIC)
657768 PHP_ME (opencv_mat, col, NULL , ZEND_ACC_PUBLIC)
658769 PHP_ME (opencv_mat, at, NULL , ZEND_ACC_PUBLIC)
770+ PHP_ME (opencv_mat, atIdx, NULL , ZEND_ACC_PUBLIC)
659771 PHP_MALIAS (opencv_mat, getImageROI ,get_image_roi, NULL , ZEND_ACC_PUBLIC)
660772 PHP_MALIAS (opencv_mat, copyTo ,copy_to, opencv_mat_copy_to_arginfo, ZEND_ACC_PUBLIC)
661773 PHP_MALIAS (opencv_mat, convertTo ,convert_to, opencv_mat_convert_to_arginfo, ZEND_ACC_PUBLIC)
0 commit comments