From 7694d2d727121ad91b6620d94e552c202448481e Mon Sep 17 00:00:00 2001 From: Hamdi Sahloul Date: Tue, 11 Sep 2018 17:06:43 +0900 Subject: [PATCH] Refactor java module --- modules/java/generator/gen_java.py | 12 ++--- modules/java/generator/src/cpp/Mat.cpp | 62 ++++++++++++++------------ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/modules/java/generator/gen_java.py b/modules/java/generator/gen_java.py index c2f6e8514c5b..afbba2093a6d 100755 --- a/modules/java/generator/gen_java.py +++ b/modules/java/generator/gen_java.py @@ -64,7 +64,9 @@ def checkFileRemap(path): "size_t" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" }, "__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" }, "int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" }, - "double[]": { "j_type" : "double[]", "jn_type" : "double[]", "jni_type" : "jdoubleArray", "suffix" : "_3D" } + "double[]": { "j_type" : "double[]", "jn_type" : "double[]", "jni_type" : "jdoubleArray", "suffix" : "_3D" }, + "ElemType": { "j_type" : "int", "jn_type" : "int", "jni_type" : "jint", "suffix" : "I", "cast_from" : "int", "cast_to" : "ElemType" }, + "ElemDepth": { "j_type" : "int", "jn_type" : "int", "jni_type" : "jint", "suffix" : "I", "cast_from" : "int", "cast_to" : "ElemDepth" }, } # Defines a rule to add extra prefixes for names from specific namespaces. @@ -562,7 +564,7 @@ def gen_func(self, ci, fi, prop_name=''): cpp_code = ci.cpp_code # c_decl - # e.g: void add(Mat src1, Mat src2, Mat dst, Mat mask = Mat(), int dtype = -1) + # e.g: void add(Mat src1, Mat src2, Mat dst, Mat mask = Mat(), ElemDepth ddepth = CV_DEPTH_AUTO) if prop_name: c_decl = "%s %s::%s" % (fi.ctype, fi.classname, prop_name) else: @@ -720,7 +722,7 @@ def gen_func(self, ci, fi, prop_name=''): # java part: # private java NATIVE method decl # e.g. - # private static native void add_0(long src1, long src2, long dst, long mask, int dtype); + # private static native void add_0(long src1, long src2, long dst, long mask, ElemDepth ddepth); jn_code.write( Template(\ " private static native $type $name($args);\n").substitute(\ type = type_dict[fi.ctype].get("jn_type", "double[]"), \ @@ -746,8 +748,8 @@ def gen_func(self, ci, fi, prop_name=''): # public java wrapper method impl (calling native one above) # e.g. - # public static void add( Mat src1, Mat src2, Mat dst, Mat mask, int dtype ) - # { add_0( src1.nativeObj, src2.nativeObj, dst.nativeObj, mask.nativeObj, dtype ); } + # public static void add( Mat src1, Mat src2, Mat dst, Mat mask, ElemDepth ddepth ) + # { add_0( src1.nativeObj, src2.nativeObj, dst.nativeObj, mask.nativeObj, ddepth ); } ret_type = fi.ctype if fi.ctype.endswith('*'): ret_type = ret_type[:-1] diff --git a/modules/java/generator/src/cpp/Mat.cpp b/modules/java/generator/src/cpp/Mat.cpp index db9f1936da4b..fb416ba59621 100644 --- a/modules/java/generator/src/cpp/Mat.cpp +++ b/modules/java/generator/src/cpp/Mat.cpp @@ -65,7 +65,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIILjava_nio_ByteBuffer static const char method_name[] = "Mat::n_1Mat__IIILByteBuffer()"; try { LOGD("%s", method_name); - return (jlong) new Mat( rows, cols, type, (void*)env->GetDirectBufferAddress(data) ); + return (jlong) new Mat( rows, cols, static_cast(type), (void*)env->GetDirectBufferAddress(data) ); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -90,7 +90,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__III static const char method_name[] = "Mat::n_1Mat__III()"; try { LOGD("%s", method_name); - return (jlong) new Mat( rows, cols, type ); + return (jlong) new Mat(rows, cols, static_cast(type)); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -116,7 +116,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDI try { LOGD("%s", method_name); Size size((int)size_width, (int)size_height); - return (jlong) new Mat( size, type ); + return (jlong) new Mat(size, static_cast(type)); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -143,7 +143,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__IIIDDDD try { LOGD("%s", method_name); Scalar s(s_val0, s_val1, s_val2, s_val3); - return (jlong) new Mat( rows, cols, type, s ); + return (jlong) new Mat(rows, cols, static_cast(type), s); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -170,7 +170,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1Mat__DDIDDDD LOGD("%s", method_name); Size size((int)size_width, (int)size_height); Scalar s(s_val0, s_val1, s_val2, s_val3); - return (jlong) new Mat( size, type, s ); + return (jlong) new Mat( size, static_cast(type), s ); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -258,20 +258,20 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1adjustROI // -// void Mat::assignTo(Mat m, int type = -1) +// void Mat::assignTo(Mat m, ElemDepth depth = CV_DEPTH_AUTO) // JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJI (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint type); JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1assignTo__JJI - (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint type) + (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint depth) { static const char method_name[] = "Mat::n_1assignTo__JJI()"; try { LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL - me->assignTo( (*(Mat*)m_nativeObj), type ); + me->assignTo((*(Mat*)m_nativeObj), static_cast(depth)); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -327,7 +327,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1channels // -// int Mat::checkVector(int elemChannels, int depth = -1, bool requireContinuous = true) +// int Mat::checkVector(int elemChannels, ElemDepth depth = -1, bool requireContinuous = true) // JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JIIZ @@ -340,7 +340,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JIIZ try { LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL - return me->checkVector( elemChannels, depth, requireContinuous ); + return me->checkVector(elemChannels, static_cast(depth), requireContinuous); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -362,7 +362,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1checkVector__JII try { LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL - return me->checkVector( elemChannels, depth ); + return me->checkVector(elemChannels, static_cast(depth)); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -531,21 +531,21 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_n_1cols // -// void Mat::convertTo(Mat& m, int rtype, double alpha = 1, double beta = 0) +// void Mat::convertTo(Mat& m, ElemDepth ddepth, double alpha = 1, double beta = 0) // JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJIDD (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha, jdouble beta); JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJIDD - (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha, jdouble beta) + (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint depth, jdouble alpha, jdouble beta) { static const char method_name[] = "Mat::n_1convertTo__JJIDD()"; try { LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL Mat& m = *((Mat*)m_nativeObj); - me->convertTo( m, rtype, alpha, beta ); + me->convertTo(m, static_cast(depth), alpha, beta); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -558,14 +558,14 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJID (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha); JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJID - (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rtype, jdouble alpha) + (JNIEnv* env, jclass, jlong self, jlong m_nativeObj, jint rdepth, jdouble alpha) { static const char method_name[] = "Mat::n_1convertTo__JJID()"; try { LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL Mat& m = *((Mat*)m_nativeObj); - me->convertTo( m, rtype, alpha ); + me->convertTo(m, static_cast(rdepth), alpha); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -585,7 +585,7 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1convertTo__JJI LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL Mat& m = *((Mat*)m_nativeObj); - me->convertTo( m, rtype ); + me->convertTo( m, static_cast(rtype) ); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -660,7 +660,7 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JIII try { LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL - me->create( rows, cols, type ); + me->create(rows, cols, static_cast(type)); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -685,7 +685,7 @@ JNIEXPORT void JNICALL Java_org_opencv_core_Mat_n_1create__JDDI LOGD("%s", method_name); Mat* me = (Mat*) self; //TODO: check for NULL Size size((int)size_width, (int)size_height); - me->create( size, type ); + me->create(size, static_cast(type)); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); } catch (...) { @@ -938,7 +938,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__III static const char method_name[] = "Mat::n_1eye__III()"; try { LOGD("%s", method_name); - Mat _retval_ = Mat::eye( rows, cols, type ); + Mat _retval_ = Mat::eye(rows, cols, static_cast(type)); return (jlong) new Mat(_retval_); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -965,7 +965,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1eye__DDI try { LOGD("%s", method_name); Size size((int)size_width, (int)size_height); - Mat _retval_ = Mat::eye( size, type ); + Mat _retval_ = Mat::eye(size, static_cast(type)); return (jlong) new Mat(_retval_); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -1171,7 +1171,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__III static const char method_name[] = "Mat::n_1ones__III()"; try { LOGD("%s", method_name); - Mat _retval_ = Mat::ones( rows, cols, type ); + Mat _retval_ = Mat::ones(rows, cols, static_cast(type)); return (jlong) new Mat(_retval_); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -1198,7 +1198,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1ones__DDI try { LOGD("%s", method_name); Size size((int)size_width, (int)size_height); - Mat _retval_ = Mat::ones( size, type ); + Mat _retval_ = Mat::ones(size, static_cast(type)); return (jlong) new Mat(_retval_); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -1724,7 +1724,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__III static const char method_name[] = "Mat::n_1zeros__III()"; try { LOGD("%s", method_name); - Mat _retval_ = Mat::zeros( rows, cols, type ); + Mat _retval_ = Mat::zeros(rows, cols, static_cast(type)); return (jlong) new Mat(_retval_); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -1751,7 +1751,7 @@ JNIEXPORT jlong JNICALL Java_org_opencv_core_Mat_n_1zeros__DDI try { LOGD("%s", method_name); Size size((int)size_width, (int)size_height); - Mat _retval_ = Mat::zeros( size, type ); + Mat _retval_ = Mat::zeros(size, static_cast(type)); return (jlong) new Mat(_retval_); } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -1795,7 +1795,7 @@ namespace { const char JavaOpenCVTrait::get[] = "Mat::nGet" s "()"; \ const char JavaOpenCVTrait::put[] = "Mat::nPut" s "()" - JOCvT(jbyte, "B", CV_8U, CV_8S); + JOCvT(jbyte, "B", CV_8UC1, CV_8S); JOCvT(jshort, "S", CV_16U, CV_16S); JOCvT(jint, "I", CV_32S, CV_32S); JOCvT(jfloat, "F", CV_32F, CV_32F); @@ -1840,7 +1840,7 @@ template static jint java_mat_put(JNIEnv* env, jlong self, jint row LOGD("%s", method_name); cv::Mat* me = (cv::Mat*) self; if(! self) return 0; // no native object behind - if(me->depth() != JavaOpenCVTrait::cvtype_1 && me->depth() != JavaOpenCVTrait::cvtype_2) return 0; // incompatible type + if(static_cast(me->depth()) != JavaOpenCVTrait::cvtype_1 && static_cast(me->depth()) != JavaOpenCVTrait::cvtype_2) return 0; // incompatible type if(me->rows<=row || me->cols<=col) return 0; // indexes out of range char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); @@ -1935,6 +1935,8 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD case CV_32S: PUT_ITEM(int, row, c); break; case CV_32F: PUT_ITEM(float, row, c); break; case CV_64F: PUT_ITEM(double, row, c); break; + case CV_USRTYPE1: + break; //unhandled } } @@ -1949,6 +1951,8 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD case CV_32S: PUT_ITEM(int, r, c); break; case CV_32F: PUT_ITEM(float, r, c); break; case CV_64F: PUT_ITEM(double, r, c); break; + case CV_USRTYPE1: + break; //unhandled } } @@ -2001,7 +2005,7 @@ template static jint java_mat_get(JNIEnv* env, jlong self, jint row LOGD("%s", method_name); cv::Mat* me = (cv::Mat*) self; if(! self) return 0; // no native object behind - if(me->depth() != JavaOpenCVTrait::cvtype_1 && me->depth() != JavaOpenCVTrait::cvtype_2) return 0; // incompatible type + if(static_cast(me->depth()) != JavaOpenCVTrait::cvtype_1 && static_cast(me->depth()) != JavaOpenCVTrait::cvtype_2) return 0; // incompatible type if(me->rows<=row || me->cols<=col) return 0; // indexes out of range char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); @@ -2089,6 +2093,8 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet case CV_32S: for(i=0; ichannels(); i++) buff[i] = *((int*) me->ptr(row, col) + i); break; case CV_32F: for(i=0; ichannels(); i++) buff[i] = *((float*) me->ptr(row, col) + i); break; case CV_64F: for(i=0; ichannels(); i++) buff[i] = *((double*) me->ptr(row, col) + i); break; + case CV_USRTYPE1: + break; //unhandled } env->SetDoubleArrayRegion(res, 0, me->channels(), buff); }