From 4deec6f0f079096214f462d0dac3bca107f44345 Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Fri, 15 Jul 2011 15:01:25 +0000 Subject: [PATCH] partial vector<> support in Java API generator --- modules/java/gen_java.py | 40 +++++++---- modules/java/src/cpp/utils.cpp | 3 + modules/java/src/cpp/utils.h | 3 + modules/java/src/java/utils.java | 118 +++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 12 deletions(-) create mode 100644 modules/java/src/cpp/utils.cpp create mode 100644 modules/java/src/cpp/utils.h create mode 100644 modules/java/src/java/utils.java diff --git a/modules/java/gen_java.py b/modules/java/gen_java.py index 63a828f8a8..5d6a57052f 100644 --- a/modules/java/gen_java.py +++ b/modules/java/gen_java.py @@ -106,7 +106,14 @@ type_dict = { "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" }, "double[]": { "j_type" : "double[]", "jn_type" : "double[]", "jni_type" : "jdoubleArray", "suffix" : "_3D" }, - "vector_Point": { "j_type" : "java.util.ArrayList", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Point": { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Mat" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_KeyPoint" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_Rect" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_uchar" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_int" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_float" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, + "vector_double" : { "j_type" : "java.util.List", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector %(n)s", "suffix" : "J" }, # "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" }, "Mat" : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),), "jni_var" : "Mat& %(n)s = *((Mat*)%(n)s_nativeObj)", @@ -320,7 +327,7 @@ class JavaWrapperGenerator(object): def add_func(self, decl): ffi = FuncFamilyInfo(decl) if ffi.jname in setManualFunctions : - print "Found function, which is ported manually: " + ffi.jname + print "Found function, which is ported manually: " + ffi.jname return None func_map = self.funcs classname = ffi.funcs[0].classname @@ -380,7 +387,7 @@ class JavaWrapperGenerator(object): //Manual ported functions - // C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray()) + // C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray()) //javadoc: minMaxLoc public static class MinMaxLocResult { public double minVal; @@ -513,9 +520,9 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual if (result == NULL) { return NULL; /* out of memory error thrown */ } - + Mat& src = *((Mat*)src_nativeObj); - + double minVal, maxVal; Point minLoc, maxLoc; if (mask_nativeObj != 0) { @@ -524,7 +531,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual } else { minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc); } - + jdouble fill[6]; fill[0]=minVal; fill[1]=maxVal; @@ -532,7 +539,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual fill[3]=minLoc.y; fill[4]=maxLoc.x; fill[5]=maxLoc.y; - + env->SetDoubleArrayRegion(result, 0, 6, fill); return result; @@ -657,16 +664,16 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual jni_args.append ( ArgInfo([ "__int64", "%s_mat_nativeObj" % a.name, "", [], "" ]) ) c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" ) if "I" in a.out or not a.out: - j_prologue.append( "Mat %s_mat = null;/*%s_to_Mat(%s);*/" % (a.name, a.ctype, a.name) ) + j_prologue.append( "Mat %s_mat = utils.%s_to_Mat(%s);" % (a.name, a.ctype, a.name) ) c_prologue.append( "// %s_out -> %s" % (a.name, a.name) ) else: j_prologue.append( "Mat %s_mat = new Mat();" % a.name ) if "O" in a.out: - j_epilogue.append("/*Mat_to_%s(%s_mat, %s);*/" % (a.ctype, a.name, a.name)) + j_epilogue.append("utils.Mat_to_%s(%s_mat, %s);" % (a.ctype, a.name, a.name)) c_epilogue.append( "// %s -> %s_out" % (a.name, a.name) ) else: - fields = type_dict[a.ctype].get("jn_args") or [] + fields = type_dict[a.ctype].get("jn_args") if fields: # complex type for f in fields: jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) ) @@ -682,8 +689,17 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) ) #jni_args.append ( ArgInfo([ "int", "%s_out_length" % a.name, "", [], "" ]) ) j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(type_dict[a.ctype].get("jn_args", [1]))) ) - j_epilogue.append("/*%s.set(%s_out);*/" % (a.name, a.name)) - c_epilogue.append( "/* %s_to_doubles(%s, %s_out); */" % (a.ctype, a.name, a.name) ) + if fields: + j_epilogue.append("%s.set(%s_out);" % (a.name, a.name)) + c_epilogue.append( \ + "jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" % + { "n" : a.name, "cnt" : len(fields), "args" : ", ".join([a.name + f[1] for f in fields]) } ) + else: + j_epilogue.append("/* NYI: %s.set(%s_out); */" % (a.name, a.name)) + c_epilogue.append( \ + "jdouble tmp_%(n)s[1] = {%(n)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, 1, tmp_%(n)s);" % + { "n" : a.name } ) + # java part: # private java NATIVE method decl diff --git a/modules/java/src/cpp/utils.cpp b/modules/java/src/cpp/utils.cpp new file mode 100644 index 0000000000..594c6d2daa --- /dev/null +++ b/modules/java/src/cpp/utils.cpp @@ -0,0 +1,3 @@ +#include "utils.h" + +using namespace cv; \ No newline at end of file diff --git a/modules/java/src/cpp/utils.h b/modules/java/src/cpp/utils.h new file mode 100644 index 0000000000..30e66c8f59 --- /dev/null +++ b/modules/java/src/cpp/utils.h @@ -0,0 +1,3 @@ +#include + +#include "opencv2/core/core.hpp" diff --git a/modules/java/src/java/utils.java b/modules/java/src/java/utils.java new file mode 100644 index 0000000000..2c73d1fab5 --- /dev/null +++ b/modules/java/src/java/utils.java @@ -0,0 +1,118 @@ +package org.opencv; + +import java.util.List; + + +public class utils { + + public static Mat vector_Point_to_Mat(List pts) { + Mat res; + int count = (pts!=null) ? pts.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_64FC2); //Point can be saved into double[2] + double[] buff = new double[count*2]; + for(int i=0; i pts) { + if(pts == null) + return; + int cols = m.cols(); + if(!CvType.CV_64FC2.equals(m.type()) || m.rows()!=1 || cols%2!=0) + return; + + pts.clear(); + double[] buff = new double[cols]; + m.get(0, 0, buff); + for(int i=0; i mats) { + Mat res; + int count = (mats!=null) ? mats.size() : 0; + if(count>0){ + res = new Mat(1, count, CvType.CV_32SC2); + int[] buff = new int[count*2]; + for(int i=0; i> 32); + buff[i*2+1] = (int)(addr & 0xffffffff); + } + res.put(0, 0, buff); + } else { + res = new Mat(); + } + return res; + } + + public static void Mat_to_vector_Mat(Mat m, List mats) { + if(mats == null) + return; + int cols = m.cols(); + if(!CvType.CV_32SC2.equals(m.type()) || m.rows()!=1 || cols%2!=0) + return; + + mats.clear(); + int[] buff = new int[cols]; + m.get(0, 0, buff); + for(int i=0; i kps) { + // TODO Auto-generated method stub + } + + public static Mat vector_float_to_Mat(List fs) { + // TODO Auto-generated method stub + return null; + } + + public static void Mat_to_vector_float(Mat m, List fs) { + // TODO Auto-generated method stub + } + + public static Mat vector_uchar_to_Mat(List bs) { + // TODO Auto-generated method stub + return null; + } + + public static Mat vector_int_to_Mat(List is) { + // TODO Auto-generated method stub + return null; + } + + public static void Mat_to_vector_int(Mat m, List is) { + // TODO Auto-generated method stub + + } + + public static Mat vector_Rect_to_Mat(List rs) { + // TODO Auto-generated method stub + return null; + } + + public static void Mat_to_vector_Rect(Mat m, List rs) { + // TODO Auto-generated method stub + + } + + public static Mat vector_double_to_Mat(List ds) { + // TODO Auto-generated method stub + return null; + } + +}