From 479f93397004d95da141b81b56e7c75187d142df Mon Sep 17 00:00:00 2001 From: Jan Starzynski Date: Mon, 11 Jul 2016 09:27:59 +0200 Subject: [PATCH] get/put: more type-safety and code unification using templates --- modules/java/generator/src/cpp/Mat.cpp | 220 +++++++------------------ 1 file changed, 62 insertions(+), 158 deletions(-) diff --git a/modules/java/generator/src/cpp/Mat.cpp b/modules/java/generator/src/cpp/Mat.cpp index d8483cd02f1f..c85a3d74007a 100644 --- a/modules/java/generator/src/cpp/Mat.cpp +++ b/modules/java/generator/src/cpp/Mat.cpp @@ -1815,6 +1815,29 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD } // extern "C" +namespace { + /// map java-array-types to assigned data + template struct JavaOpenCVTrait; + +/// less typing for specialisations +#define JOCvT(t,s,c1,c2) \ + template<> struct JavaOpenCVTrait { \ + typedef t value_type; /* type of array element */ \ + static const char get[]; /* name of getter */ \ + static const char put[]; /* name of putter */ \ + enum {cvtype_1 = c1, cvtype_2 = c2 }; /* allowed OpenCV-types */ \ + }; \ + const char JavaOpenCVTrait::get[] = "Mat::nGet" s "()"; \ + const char JavaOpenCVTrait::put[] = "Mat::nPut" s "()" + + JOCvT(jbyte, "B", CV_8U, CV_8S); + JOCvT(jshort, "S", CV_16U, CV_16S); + JOCvT(jint, "I", CV_32S, CV_32S); + JOCvT(jfloat, "F", CV_32F, CV_32F); + JOCvT(jdouble, "D", CV_64F, CV_64F); +#undef JOCvT +} + template static int mat_put(cv::Mat* m, int row, int col, int count, char* buff) { if(! m) return 0; @@ -1845,26 +1868,19 @@ template static int mat_put(cv::Mat* m, int row, int col, int count, return res; } - -extern "C" { - -JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB - (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals); - -JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB - (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals) +template static jint java_mat_put(JNIEnv* env, jlong self, jint row, jint col, jint count, ARRAY vals) { - static const char method_name[] = "Mat::nPutB()"; + static const char *method_name = JavaOpenCVTrait::put; try { LOGD("%s", method_name); cv::Mat* me = (cv::Mat*) self; if(! self) return 0; // no native object behind - if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type + if(me->depth() != JavaOpenCVTrait::cvtype_1 && 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); - int res = mat_put(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); + int res = mat_put::value_type>(me, row, col, count, values); + env->ReleasePrimitiveArrayCritical(vals, values, JNI_ABORT); return res; } catch(const std::exception &e) { throwJavaException(env, &e, method_name); @@ -1875,31 +1891,24 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB return 0; } +extern "C" { + +JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB + (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals); + +JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB + (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals) +{ + return java_mat_put(env, self, row, col, count, vals); +} + JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals); JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals) { - static const char method_name[] = "Mat::nPutS()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_put(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_put(env, self, row, col, count, vals); } JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI @@ -1908,25 +1917,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals) { - static const char method_name[] = "Mat::nPutI()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_32S) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_put(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_put(env, self, row, col, count, vals); } JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF @@ -1935,31 +1926,12 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals) { - static const char method_name[] = "Mat::nPutF()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_32F) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_put(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_put(env, self, row, col, count, vals); } - } // extern "C" -template int mat_get(cv::Mat* m, int row, int col, int count, char* buff) +template static int mat_get(cv::Mat* m, int row, int col, int count, char* buff) { if(! m) return 0; if(! buff) return 0; @@ -1989,24 +1961,17 @@ template int mat_get(cv::Mat* m, int row, int col, int count, char* return res; } -extern "C" { - -JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB - (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals); - -JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB - (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals) -{ - static const char method_name[] = "Mat::nGetB()"; +template static jint java_mat_get(JNIEnv* env, jlong self, jint row, jint col, jint count, ARRAY vals) { + static const char *method_name = JavaOpenCVTrait::get; try { LOGD("%s", method_name); cv::Mat* me = (cv::Mat*) self; if(! self) return 0; // no native object behind - if(me->depth() != CV_8U && me->depth() != CV_8S) return 0; // incompatible type + if(me->depth() != JavaOpenCVTrait::cvtype_1 && 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); - int res = mat_get(me, row, col, count, values); + int res = mat_get::value_type>(me, row, col, count, values); env->ReleasePrimitiveArrayCritical(vals, values, 0); return res; } catch(const std::exception &e) { @@ -2018,31 +1983,24 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB return 0; } +extern "C" { + +JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB + (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals); + +JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB + (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jbyteArray vals) +{ + return java_mat_get(env, self, row, col, count, vals); +} + JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals); JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals) { - static const char method_name[] = "Mat::nGetS()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_16U && me->depth() != CV_16S) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_get(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_get(env, self, row, col, count, vals); } JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI @@ -2051,25 +2009,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals) { - static const char method_name[] = "Mat::nGetI()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_32S) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_get(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_get(env, self, row, col, count, vals); } JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF @@ -2078,25 +2018,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals) { - static const char method_name[] = "Mat::nGetF()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_32F) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_get(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_get(env, self, row, col, count, vals); } JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD @@ -2105,25 +2027,7 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals) { - static const char method_name[] = "Mat::nGetD()"; - try { - LOGD("%s", method_name); - cv::Mat* me = (cv::Mat*) self; - if(! self) return 0; // no native object behind - if(me->depth() != CV_64F) return 0; // incompatible type - if(me->rows<=row || me->cols<=col) return 0; // indexes out of range - - char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); - int res = mat_get(me, row, col, count, values); - env->ReleasePrimitiveArrayCritical(vals, values, 0); - return res; - } catch(const std::exception &e) { - throwJavaException(env, &e, method_name); - } catch (...) { - throwJavaException(env, 0, method_name); - } - - return 0; + return java_mat_get(env, self, row, col, count, vals); } JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet