get/put: more type-safety and code unification using templates

pull/6876/head
Jan Starzynski 8 years ago
parent b4112a5878
commit 479f933970
  1. 220
      modules/java/generator/src/cpp/Mat.cpp

@ -1815,6 +1815,29 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutD
} // extern "C" } // extern "C"
namespace {
/// map java-array-types to assigned data
template<class T> struct JavaOpenCVTrait;
/// less typing for specialisations
#define JOCvT(t,s,c1,c2) \
template<> struct JavaOpenCVTrait<t##Array> { \
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<t##Array>::get[] = "Mat::nGet" s "()"; \
const char JavaOpenCVTrait<t##Array>::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<typename T> static int mat_put(cv::Mat* m, int row, int col, int count, char* buff) template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count, char* buff)
{ {
if(! m) return 0; if(! m) return 0;
@ -1845,26 +1868,19 @@ template<typename T> static int mat_put(cv::Mat* m, int row, int col, int count,
return res; return res;
} }
template<class ARRAY> static jint java_mat_put(JNIEnv* env, jlong self, jint row, jint col, jint count, ARRAY vals)
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)
{ {
static const char method_name[] = "Mat::nPutB()"; static const char *method_name = JavaOpenCVTrait<ARRAY>::put;
try { try {
LOGD("%s", method_name); LOGD("%s", method_name);
cv::Mat* me = (cv::Mat*) self; cv::Mat* me = (cv::Mat*) self;
if(! self) return 0; // no native object behind 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<ARRAY>::cvtype_1 && me->depth() != JavaOpenCVTrait<ARRAY>::cvtype_2) return 0; // incompatible type
if(me->rows<=row || me->cols<=col) return 0; // indexes out of range if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
int res = mat_put<char>(me, row, col, count, values); int res = mat_put<typename JavaOpenCVTrait<ARRAY>::value_type>(me, row, col, count, values);
env->ReleasePrimitiveArrayCritical(vals, values, 0); env->ReleasePrimitiveArrayCritical(vals, values, JNI_ABORT);
return res; return res;
} catch(const std::exception &e) { } catch(const std::exception &e) {
throwJavaException(env, &e, method_name); throwJavaException(env, &e, method_name);
@ -1875,31 +1891,24 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutB
return 0; 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals); (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals);
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutS
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals)
{ {
static const char method_name[] = "Mat::nPutS()"; return java_mat_put(env, self, row, col, count, vals);
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<short>(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;
} }
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutI
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals)
{ {
static const char method_name[] = "Mat::nPutI()"; return java_mat_put(env, self, row, col, count, vals);
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<int>(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;
} }
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nPutF
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals)
{ {
static const char method_name[] = "Mat::nPutF()"; return java_mat_put(env, self, row, col, count, vals);
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<float>(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;
} }
} // extern "C" } // extern "C"
template<typename T> int mat_get(cv::Mat* m, int row, int col, int count, char* buff) template<typename T> static int mat_get(cv::Mat* m, int row, int col, int count, char* buff)
{ {
if(! m) return 0; if(! m) return 0;
if(! buff) return 0; if(! buff) return 0;
@ -1989,24 +1961,17 @@ template<typename T> int mat_get(cv::Mat* m, int row, int col, int count, char*
return res; return res;
} }
extern "C" { template<class ARRAY> static jint java_mat_get(JNIEnv* env, jlong self, jint row, jint col, jint count, ARRAY vals) {
static const char *method_name = JavaOpenCVTrait<ARRAY>::get;
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()";
try { try {
LOGD("%s", method_name); LOGD("%s", method_name);
cv::Mat* me = (cv::Mat*) self; cv::Mat* me = (cv::Mat*) self;
if(! self) return 0; // no native object behind 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<ARRAY>::cvtype_1 && me->depth() != JavaOpenCVTrait<ARRAY>::cvtype_2) return 0; // incompatible type
if(me->rows<=row || me->cols<=col) return 0; // indexes out of range if(me->rows<=row || me->cols<=col) return 0; // indexes out of range
char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0); char* values = (char*)env->GetPrimitiveArrayCritical(vals, 0);
int res = mat_get<char>(me, row, col, count, values); int res = mat_get<typename JavaOpenCVTrait<ARRAY>::value_type>(me, row, col, count, values);
env->ReleasePrimitiveArrayCritical(vals, values, 0); env->ReleasePrimitiveArrayCritical(vals, values, 0);
return res; return res;
} catch(const std::exception &e) { } catch(const std::exception &e) {
@ -2018,31 +1983,24 @@ JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetB
return 0; 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals); (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals);
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetS
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jshortArray vals)
{ {
static const char method_name[] = "Mat::nGetS()"; return java_mat_get(env, self, row, col, count, vals);
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<short>(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;
} }
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetI
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jintArray vals)
{ {
static const char method_name[] = "Mat::nGetI()"; return java_mat_get(env, self, row, col, count, vals);
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<int>(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;
} }
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetF
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jfloatArray vals)
{ {
static const char method_name[] = "Mat::nGetF()"; return java_mat_get(env, self, row, col, count, vals);
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<float>(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;
} }
JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD 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 JNIEXPORT jint JNICALL Java_org_opencv_core_Mat_nGetD
(JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals) (JNIEnv* env, jclass, jlong self, jint row, jint col, jint count, jdoubleArray vals)
{ {
static const char method_name[] = "Mat::nGetD()"; return java_mat_get(env, self, row, col, count, vals);
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<double>(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;
} }
JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_Mat_nGet

Loading…
Cancel
Save