partial vector<> support in Java API generator
authorAndrey Pavlenko <no@email>
Fri, 15 Jul 2011 15:01:25 +0000 (15:01 +0000)
committerAndrey Pavlenko <no@email>
Fri, 15 Jul 2011 15:01:25 +0000 (15:01 +0000)
modules/java/gen_java.py
modules/java/src/cpp/utils.cpp [new file with mode: 0644]
modules/java/src/cpp/utils.h [new file with mode: 0644]
modules/java/src/java/utils.java [new file with mode: 0644]

index 63a828f..5d6a570 100644 (file)
@@ -106,7 +106,14 @@ type_dict = {
     "size_t"  : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },\r
     "__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },\r
     "double[]": { "j_type" : "double[]", "jn_type" : "double[]", "jni_type" : "jdoubleArray", "suffix" : "_3D" },\r
-    "vector_Point": { "j_type" : "java.util.ArrayList<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },\r
+    "vector_Point": { "j_type" : "java.util.List<Point>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Point> %(n)s", "suffix" : "J" },\r
+    "vector_Mat" :  { "j_type" : "java.util.List<Mat>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Mat> %(n)s", "suffix" : "J" },\r
+    "vector_KeyPoint" : { "j_type" : "java.util.List<KeyPoint>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<KeyPoint> %(n)s", "suffix" : "J" },\r
+    "vector_Rect" : { "j_type" : "java.util.List<Rect>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<Rect> %(n)s", "suffix" : "J" },\r
+    "vector_uchar" : { "j_type" : "java.util.List<Byte>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<uchar> %(n)s", "suffix" : "J" },\r
+    "vector_int" : { "j_type" : "java.util.List<Integer>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<int> %(n)s", "suffix" : "J" },\r
+    "vector_float" : { "j_type" : "java.util.List<Float>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<float> %(n)s", "suffix" : "J" },\r
+    "vector_double" : { "j_type" : "java.util.List<Double>", "jn_type" : "long", "jni_type" : "jlong", "jni_var" : "vector<double> %(n)s", "suffix" : "J" },\r
 # "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" },\r
     "Mat"     : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),\r
                   "jni_var" : "Mat& %(n)s = *((Mat*)%(n)s_nativeObj)",\r
@@ -320,7 +327,7 @@ class JavaWrapperGenerator(object):
     def add_func(self, decl):\r
         ffi = FuncFamilyInfo(decl)\r
        if ffi.jname in setManualFunctions :\r
-               print "Found function, which is ported manually: " + ffi.jname \r
+               print "Found function, which is ported manually: " + ffi.jname\r
                return None\r
         func_map = self.funcs\r
         classname = ffi.funcs[0].classname\r
@@ -380,7 +387,7 @@ class JavaWrapperGenerator(object):
 \r
     //Manual ported functions\r
 \r
-    // C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray()) \r
+    // C++: minMaxLoc(Mat src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray())\r
     //javadoc: minMaxLoc\r
     public static class MinMaxLocResult {\r
         public double minVal;\r
@@ -513,9 +520,9 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
         if (result == NULL) {\r
             return NULL; /* out of memory error thrown */\r
         }\r
-        \r
+\r
         Mat& src = *((Mat*)src_nativeObj);\r
-    \r
+\r
         double minVal, maxVal;\r
         Point minLoc, maxLoc;\r
         if (mask_nativeObj != 0) {\r
@@ -524,7 +531,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
         } else {\r
             minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc);\r
         }\r
-            \r
+\r
         jdouble fill[6];\r
         fill[0]=minVal;\r
         fill[1]=maxVal;\r
@@ -532,7 +539,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
         fill[3]=minLoc.y;\r
         fill[4]=maxLoc.x;\r
         fill[5]=maxLoc.y;\r
-    \r
+\r
         env->SetDoubleArrayRegion(result, 0, 6, fill);\r
 \r
        return result;\r
@@ -657,16 +664,16 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
                     jni_args.append ( ArgInfo([ "__int64", "%s_mat_nativeObj" % a.name, "", [], "" ]) )\r
                     c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" )\r
                     if "I" in a.out or not a.out:\r
-                        j_prologue.append( "Mat %s_mat = null;/*%s_to_Mat(%s);*/" % (a.name, a.ctype, a.name) )\r
+                        j_prologue.append( "Mat %s_mat = utils.%s_to_Mat(%s);" % (a.name, a.ctype, a.name) )\r
                         c_prologue.append( "// %s_out -> %s" % (a.name, a.name) )\r
                     else:\r
                         j_prologue.append( "Mat %s_mat = new Mat();" % a.name )\r
                     if "O" in a.out:\r
-                        j_epilogue.append("/*Mat_to_%s(%s_mat, %s);*/" % (a.ctype, a.name, a.name))\r
+                        j_epilogue.append("utils.Mat_to_%s(%s_mat, %s);" % (a.ctype, a.name, a.name))\r
                         c_epilogue.append( "// %s -> %s_out" % (a.name, a.name) )\r
                 else:\r
 \r
-                    fields = type_dict[a.ctype].get("jn_args") or []\r
+                    fields = type_dict[a.ctype].get("jn_args")\r
                     if fields: # complex type\r
                         for f in fields:\r
                             jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) )\r
@@ -682,8 +689,17 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
                         jni_args.append ( ArgInfo([ "double[]", "%s_out" % a.name, "", [], "" ]) )\r
                         #jni_args.append ( ArgInfo([ "int", "%s_out_length" % a.name, "", [], "" ]) )\r
                         j_prologue.append( "double[] %s_out = new double[%i];" % (a.name, len(type_dict[a.ctype].get("jn_args", [1]))) )\r
-                        j_epilogue.append("/*%s.set(%s_out);*/" % (a.name, a.name))\r
-                        c_epilogue.append( "/* %s_to_doubles(%s, %s_out); */" % (a.ctype, a.name, a.name) )\r
+                        if fields:\r
+                            j_epilogue.append("%s.set(%s_out);" % (a.name, a.name))\r
+                            c_epilogue.append( \\r
+                                "jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" %\r
+                                { "n" : a.name, "cnt" : len(fields), "args" : ", ".join([a.name + f[1] for f in fields]) } )\r
+                        else:\r
+                            j_epilogue.append("/* NYI: %s.set(%s_out); */" % (a.name, a.name))\r
+                            c_epilogue.append( \\r
+                                "jdouble tmp_%(n)s[1] = {%(n)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, 1, tmp_%(n)s);" %\r
+                                { "n" : a.name } )\r
+\r
 \r
             # java part:\r
             # private java NATIVE method decl\r
diff --git a/modules/java/src/cpp/utils.cpp b/modules/java/src/cpp/utils.cpp
new file mode 100644 (file)
index 0000000..594c6d2
--- /dev/null
@@ -0,0 +1,3 @@
+#include "utils.h"\r
+\r
+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 (file)
index 0000000..30e66c8
--- /dev/null
@@ -0,0 +1,3 @@
+#include <jni.h>\r
+\r
+#include "opencv2/core/core.hpp"\r
diff --git a/modules/java/src/java/utils.java b/modules/java/src/java/utils.java
new file mode 100644 (file)
index 0000000..2c73d1f
--- /dev/null
@@ -0,0 +1,118 @@
+package org.opencv;\r
+\r
+import java.util.List;\r
+\r
+\r
+public class utils {\r
+       \r
+       public static Mat vector_Point_to_Mat(List<Point> pts) {\r
+               Mat res;\r
+               int count = (pts!=null) ? pts.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_64FC2); //Point can be saved into double[2]\r
+                       double[] buff = new double[count*2];\r
+                       for(int i=0; i<count; i++) {\r
+                               Point p = pts.get(i);\r
+                               buff[i*2]   = p.x;\r
+                               buff[i*2+1] = p.y;\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
+       }\r
+\r
+       public static void Mat_to_vector_Point(Mat m, List<Point> pts) {\r
+               if(pts == null)\r
+                       return;\r
+               int cols = m.cols();\r
+               if(!CvType.CV_64FC2.equals(m.type()) ||  m.rows()!=1 || cols%2!=0)\r
+                       return;\r
+               \r
+               pts.clear();\r
+               double[] buff = new double[cols];\r
+               m.get(0, 0, buff);\r
+               for(int i=0; i<cols/2; i++) {\r
+                       pts.add( new Point(buff[i*2], buff[i*2+1]) );\r
+               }\r
+       }\r
+\r
+       public static Mat vector_Mat_to_Mat(List<Mat> mats) {\r
+               Mat res;\r
+               int count = (mats!=null) ? mats.size() : 0;\r
+               if(count>0){\r
+                       res = new Mat(1, count, CvType.CV_32SC2);\r
+                       int[] buff = new int[count*2];\r
+                       for(int i=0; i<count; i++) {\r
+                               long addr = mats.get(i).nativeObj;\r
+                               buff[i*2]   = (int)(addr >> 32);\r
+                               buff[i*2+1] = (int)(addr & 0xffffffff);\r
+                       }\r
+                       res.put(0, 0, buff);\r
+               } else {\r
+                       res = new Mat();\r
+               }\r
+               return res;\r
+       }\r
+\r
+       public static void Mat_to_vector_Mat(Mat m, List<Mat> mats) {\r
+               if(mats == null)\r
+                       return;\r
+               int cols = m.cols();\r
+               if(!CvType.CV_32SC2.equals(m.type()) ||  m.rows()!=1 || cols%2!=0)\r
+                       return;\r
+               \r
+               mats.clear();\r
+               int[] buff = new int[cols];\r
+               m.get(0, 0, buff);\r
+               for(int i=0; i<cols/2; i++) {\r
+                       long addr = (((long)buff[i*2])<<32) | ((long)buff[i*2+1]);\r
+                       mats.add( new Mat(addr) );\r
+               }\r
+       }\r
+\r
+       public static void Mat_to_vector_KeyPoint(Mat kp_mat, List<features2d.KeyPoint> kps) {\r
+               // TODO Auto-generated method stub\r
+       }\r
+\r
+       public static Mat vector_float_to_Mat(List<Float> fs) {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+       public static void Mat_to_vector_float(Mat m, List<Float> fs) {\r
+               // TODO Auto-generated method stub\r
+       }\r
+\r
+       public static Mat vector_uchar_to_Mat(List<Byte> bs) {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+       public static Mat vector_int_to_Mat(List<Integer> is) {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+       public static void Mat_to_vector_int(Mat m, List<Integer> is) {\r
+               // TODO Auto-generated method stub\r
+               \r
+       }\r
+\r
+       public static Mat vector_Rect_to_Mat(List<Rect> rs) {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+       public static void Mat_to_vector_Rect(Mat m, List<Rect> rs) {\r
+               // TODO Auto-generated method stub\r
+               \r
+       }\r
+\r
+       public static Mat vector_double_to_Mat(List<Double> ds) {\r
+               // TODO Auto-generated method stub\r
+               return null;\r
+       }\r
+\r
+}\r