Java API generator improvements:

- fixed return of complex types (Scalar, Point, etc)
- partial implementation of vector<> support (empty stubs for CPP-side converters)
pull/13383/head
Andrey Pavlenko 14 years ago
parent 30f265a16a
commit ecba099754
  1. 32
      modules/java/gen_java.py
  2. 101
      modules/java/src/cpp/utils.cpp
  3. 39
      modules/java/src/cpp/utils.h
  4. 4
      modules/java/src/java/Mat.java
  5. 78
      modules/java/src/java/Scalar.java

@ -149,8 +149,8 @@ type_dict = {
"RotatedRect": { "j_type" : "RotatedRect", "jn_args" : (("double", ".center.x"), ("double", ".center.y"), ("double", ".size.width"), ("double", ".size.height"), ("double", ".angle")), "RotatedRect": { "j_type" : "RotatedRect", "jn_args" : (("double", ".center.x"), ("double", ".center.y"), ("double", ".size.width"), ("double", ".size.height"), ("double", ".angle")),
"jni_var" : "RotatedRect %(n)s(cv::Point2f(%(n)s_center_x, %(n)s_center_y), cv::Size2f(%(n)s_size_width, %(n)s_size_height), %(n)s_angle)", "jni_var" : "RotatedRect %(n)s(cv::Point2f(%(n)s_center_x, %(n)s_center_y), cv::Size2f(%(n)s_size_width, %(n)s_size_height), %(n)s_angle)",
"jni_type" : "jdoubleArray", "suffix" : "DDDDD"}, "jni_type" : "jdoubleArray", "suffix" : "DDDDD"},
"Scalar" : { "j_type" : "Scalar", "jn_args" : (("double", ".v0"), ("double", ".v1"), ("double", ".v2"), ("double", ".v3")), "Scalar" : { "j_type" : "Scalar", "jn_args" : (("double", ".val[0]"), ("double", ".val[1]"), ("double", ".val[2]"), ("double", ".val[3]")),
"jni_var" : "Scalar %(n)s(%(n)s_v0, %(n)s_v1, %(n)s_v2, %(n)s_v3)", "jni_type" : "jdoubleArray", "jni_var" : "Scalar %(n)s(%(n)s_val0, %(n)s_val1, %(n)s_val2, %(n)s_val3)", "jni_type" : "jdoubleArray",
"suffix" : "DDDD"}, "suffix" : "DDDD"},
"Range" : { "j_type" : "Range", "jn_args" : (("int", ".start"), ("int", ".end")), "Range" : { "j_type" : "Range", "jn_args" : (("int", ".start"), ("int", ".end")),
"jni_var" : "Range %(n)s(%(n)s_start, %(n)s_end)", "jni_type" : "jdoubleArray", "jni_var" : "Range %(n)s(%(n)s_start, %(n)s_end)", "jni_type" : "jdoubleArray",
@ -489,6 +489,7 @@ class JavaWrapperGenerator(object):
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__)) #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
#endif // DEBUG #endif // DEBUG
#include "utils.h"
""" % module) """ % module)
self.cpp_code.write( "\n".join(['#include "opencv2/%s/%s"' % (module, os.path.basename(f)) \ self.cpp_code.write( "\n".join(['#include "opencv2/%s/%s"' % (module, os.path.basename(f)) \
for f in srcfiles]) ) for f in srcfiles]) )
@ -649,8 +650,14 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
c_prologue = [] c_prologue = []
c_epilogue = [] c_epilogue = []
if type_dict[fi.ctype]["jni_type"] == "jdoubleArray": if type_dict[fi.ctype]["jni_type"] == "jdoubleArray":
c_epilogue.append( "jdoubleArray _da_ = env->NewDoubleArray(6); /* assuming '6' is enough*/ //" + fields = type_dict[fi.ctype].get("jn_args")
fi.ctype + "_to_doubles(_retval_, _da_);" ) if fields:
c_epilogue.append( \
"jdoubleArray _da_retval_ = env->NewDoubleArray(6); /* assuming '6' is enough*/ " +
"jdouble _tmp_retval_[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(_da_retval_, 0, %(cnt)i, _tmp_retval_);" %
{ "cnt" : len(fields), "args" : ", ".join(["_retval_" + f[1] for f in fields]) } )
else:
c_epilogue.append( "/* TODO: NYI !!! */" )
if fi.classname and fi.ctype and not fi.static: # non-static class method except c-tor if fi.classname and fi.ctype and not fi.static: # non-static class method except c-tor
# adding 'self' # adding 'self'
jn_args.append ( ArgInfo([ "__int64", "nativeObj", "", [], "" ]) ) jn_args.append ( ArgInfo([ "__int64", "nativeObj", "", [], "" ]) )
@ -663,21 +670,22 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
jn_args.append ( ArgInfo([ "__int64", "%s_mat.nativeObj" % a.name, "", [], "" ]) ) jn_args.append ( ArgInfo([ "__int64", "%s_mat.nativeObj" % a.name, "", [], "" ]) )
jni_args.append ( ArgInfo([ "__int64", "%s_mat_nativeObj" % a.name, "", [], "" ]) ) jni_args.append ( ArgInfo([ "__int64", "%s_mat_nativeObj" % a.name, "", [], "" ]) )
c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" ) c_prologue.append( type_dict[a.ctype]["jni_var"] % {"n" : a.name} + ";" )
c_prologue.append( "Mat& %(n)s_mat = *((Mat*)%(n)s_mat_nativeObj)" % {"n" : a.name} + ";" )
if "I" in a.out or not a.out: if "I" in a.out or not a.out:
j_prologue.append( "Mat %s_mat = utils.%s_to_Mat(%s);" % (a.name, a.ctype, a.name) ) j_prologue.append( "Mat %(n)s_mat = utils.%(t)s_to_Mat(%(n)s);" % {"n" : a.name, "t" : a.ctype} )
c_prologue.append( "// %s_out -> %s" % (a.name, a.name) ) c_prologue.append( "Mat_to_%(t)s( %(n)s_mat, %(n)s );" % {"n" : a.name, "t" : a.ctype} )
else: else:
j_prologue.append( "Mat %s_mat = new Mat();" % a.name ) j_prologue.append( "Mat %s_mat = new Mat();" % a.name )
if "O" in a.out: if "O" in a.out:
j_epilogue.append("utils.Mat_to_%s(%s_mat, %s);" % (a.ctype, a.name, a.name)) j_epilogue.append("utils.Mat_to_%(t)s(%(n)s_mat, %(n)s);" % {"t" : a.ctype, "n" : a.name})
c_epilogue.append( "// %s -> %s_out" % (a.name, a.name) ) c_prologue.append( "%(t)s_to_Mat( %(n)s, %(n)s_mat );" % {"n" : a.name, "t" : a.ctype} )
else: else:
fields = type_dict[a.ctype].get("jn_args") fields = type_dict[a.ctype].get("jn_args")
if fields: # complex type if fields: # complex type
for f in fields: for f in fields:
jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) ) jn_args.append ( ArgInfo([ f[0], a.name + f[1], "", [], "" ]) )
jni_args.append( ArgInfo([ f[0], a.name + f[1].replace(".","_"), "", [], "" ]) ) jni_args.append( ArgInfo([ f[0], a.name + f[1].replace(".","_").replace("[","").replace("]",""), "", [], "" ]) )
else: else:
jn_args.append(a) jn_args.append(a)
jni_args.append(a) jni_args.append(a)
@ -695,7 +703,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
"jdouble tmp_%(n)s[%(cnt)i] = {%(args)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, %(cnt)i, tmp_%(n)s);" % "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]) } ) { "n" : a.name, "cnt" : len(fields), "args" : ", ".join([a.name + f[1] for f in fields]) } )
else: else:
j_epilogue.append("/* NYI: %s.set(%s_out); */" % (a.name, a.name)) j_epilogue.append("/* TODO: NYI: %s.set(%s_out); */" % (a.name, a.name))
c_epilogue.append( \ c_epilogue.append( \
"jdouble tmp_%(n)s[1] = {%(n)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, 1, tmp_%(n)s);" % "jdouble tmp_%(n)s[1] = {%(n)s}; env->SetDoubleArrayRegion(%(n)s_out, 0, 1, tmp_%(n)s);" %
{ "n" : a.name } ) { "n" : a.name } )
@ -710,7 +718,7 @@ JNIEXPORT jdoubleArray JNICALL Java_org_opencv_core_n_1minMaxLocManual
indent = indent, \ indent = indent, \
jn_type = type_dict[fi.ctype].get("jn_type", "double[]"), \ jn_type = type_dict[fi.ctype].get("jn_type", "double[]"), \
jn_name = fi.jn_name, \ jn_name = fi.jn_name, \
jn_args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], a.name.replace(".","_")) for a in jn_args]) jn_args = ", ".join(["%s %s" % (type_dict[a.ctype]["jn_type"], a.name.replace(".","_").replace("[","").replace("]","")) for a in jn_args])
) ); ) );
# java part: # java part:
@ -788,7 +796,7 @@ $indent}
elif fi.ctype in self.classes: # wrapped class: elif fi.ctype in self.classes: # wrapped class:
ret = "return (jlong) new %s(_retval_);" % fi.ctype ret = "return (jlong) new %s(_retval_);" % fi.ctype
elif type_dict[fi.ctype]["jni_type"] == "jdoubleArray": elif type_dict[fi.ctype]["jni_type"] == "jdoubleArray":
ret = "return _da_;" ret = "return _da_retval_;"
cvname = "cv::" + fi.name cvname = "cv::" + fi.name
#j2cvargs = [] #j2cvargs = []

@ -1,3 +1,102 @@
#include "utils.h" #include "utils.h"
using namespace cv; using namespace cv;
// vector_int
void Mat_to_vector_int(Mat& mat, vector<int>& v_int)
{
return;
}
void vector_int_to_Mat(vector<int>& v_int, Mat& mat)
{
return;
}
//vector_double
void Mat_to_vector_double(Mat& mat, vector<double>& v_double)
{
return;
}
void vector_double_to_Mat(vector<double>& v_double, Mat& mat)
{
return;
}
// vector_float
void Mat_to_vector_float(Mat& mat, vector<float>& v_float)
{
return;
}
void vector_float_to_Mat(vector<float>& v_float, Mat& mat)
{
return;
}
//vector_uchar
void Mat_to_vector_uchar(cv::Mat& mat, std::vector<uchar>& v_uchar)
{
return;
}
//vector_Rect
void Mat_to_vector_Rect(Mat& mat, vector<Rect>& v_rect)
{
return;
}
void vector_Rect_to_Mat(vector<Rect>& v_rect, Mat& mat)
{
return;
}
//vector_Point
void Mat_to_vector_Point(Mat& mat, vector<Point>& v_point)
{
return;
}
void vector_Point_to_Mat(vector<Point>& v_point, Mat& mat)
{
return;
}
//vector_KeyPoint
void Mat_to_vector_KeyPoint(Mat& mat, vector<KeyPoint>& v_kp)
{
return;
}
void vector_KeyPoint_to_Mat(vector<KeyPoint>& v_kp, Mat& mat)
{
return;
}
//vector_Mat
void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat)
{
return;
}
void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat)
{
return;
}

@ -1,3 +1,42 @@
#include <jni.h> #include <jni.h>
#include "opencv2/core/core.hpp" #include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
void Mat_to_vector_int(cv::Mat& mat, std::vector<int>& v_int);
void vector_int_to_Mat(std::vector<int>& v_int, cv::Mat& mat);
void Mat_to_vector_double(cv::Mat& mat, std::vector<double>& v_double);
void vector_double_to_Mat(std::vector<double>& v_double, cv::Mat& mat);
void Mat_to_vector_float(cv::Mat& mat, std::vector<float>& v_float);
void vector_float_to_Mat(std::vector<float>& v_float, cv::Mat& mat);
void Mat_to_vector_uchar(cv::Mat& mat, std::vector<uchar>& v_uchar);
void Mat_to_vector_Rect(cv::Mat& mat, std::vector<cv::Rect>& v_rect);
void vector_Rect_to_Mat(std::vector<cv::Rect>& v_rect, cv::Mat& mat);
void Mat_to_vector_Point(cv::Mat& mat, std::vector<cv::Point>& v_point);
void vector_Point_to_Mat(std::vector<cv::Point>& v_point, cv::Mat& mat);
void Mat_to_vector_KeyPoint(cv::Mat& mat, std::vector<cv::KeyPoint>& v_kp);
void vector_KeyPoint_to_Mat(std::vector<cv::KeyPoint>& v_kp, cv::Mat& mat);
void Mat_to_vector_Mat(cv::Mat& mat, std::vector<cv::Mat>& v_mat);
void vector_Mat_to_Mat(std::vector<cv::Mat>& v_mat, cv::Mat& mat);

@ -27,7 +27,7 @@ public class Mat {
//javadoc:Mat::Mat(rows,cols,type,s) //javadoc:Mat::Mat(rows,cols,type,s)
public Mat(int rows, int cols, CvType type, Scalar s) { public Mat(int rows, int cols, CvType type, Scalar s) {
this( nCreateMat(rows, cols, type.toInt(), s.v0, s.v1, s.v2, s.v3) ); this( nCreateMat(rows, cols, type.toInt(), s.val[0], s.val[1], s.val[2], s.val[3]) );
} }
//javadoc:Mat::Mat(rows,cols,depth,s) //javadoc:Mat::Mat(rows,cols,depth,s)
@ -280,7 +280,7 @@ public class Mat {
//javadoc:Mat::setTo(s) //javadoc:Mat::setTo(s)
public void setTo(Scalar s) { public void setTo(Scalar s) {
checkNull(); checkNull();
nSetTo(nativeObj, s.v0, s.v1, s.v2, s.v3); nSetTo(nativeObj, s.val[0], s.val[1], s.val[2], s.val[3]);
} }
//javadoc:Mat::copyTo(m) //javadoc:Mat::copyTo(m)

@ -3,41 +3,34 @@ package org.opencv;
//javadoc:Scalar_ //javadoc:Scalar_
public class Scalar { public class Scalar {
public double v0, v1, v2, v3; public double val[];
public Scalar(double v0, double v1, double v2, double v3) { public Scalar(double v0, double v1, double v2, double v3) {
this.v0 = v0; this.val = new double[] {v0, v1, v2, v3};
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
} }
public Scalar(double v0, double v1, double v2) { public Scalar(double v0, double v1, double v2) {
this(v0, v1, v2, 0); this.val = new double[] {v0, v1, v2, 0};
} }
public Scalar(double v0, double v1) { public Scalar(double v0, double v1) {
this(v0, v1, 0, 0); this.val = new double[] {v0, v1, 0, 0};
} }
public Scalar(double v0) { public Scalar(double v0) {
this(v0, 0, 0, 0); this.val = new double[] {v0, 0, 0, 0};
} }
public Scalar(double[] vals) { public Scalar(double[] vals) {
set(vals); this.val = new double[4];
set(vals);
} }
public void set(double[] vals) { public void set(double[] vals) {
if(vals!=null) { if(vals!=null) {
v0 = vals.length>0 ? (int)vals[0] : 0; this.val[0] = vals.length>0 ? vals[0] : 0;
v1 = vals.length>1 ? (int)vals[1] : 0; this.val[1] = vals.length>1 ? vals[1] : 0;
v2 = vals.length>2 ? (int)vals[2] : 0; this.val[2] = vals.length>2 ? vals[2] : 0;
v3 = vals.length>3 ? (int)vals[3] : 0; this.val[3] = vals.length>3 ? vals[3] : 0;
} else {
v0 = 0;
v1 = 0;
v2 = 0;
v3 = 0;
} }
} }
@ -46,46 +39,41 @@ public class Scalar {
} }
public Scalar clone() { public Scalar clone() {
return new Scalar(v0, v1, v2, v3); return new Scalar(val);
} }
public Scalar mul(Scalar it, double scale) { public Scalar mul(Scalar it, double scale) {
return new Scalar( v0 * it.v0 * scale, v1 * it.v1 * scale, return new Scalar( val[0] * it.val[0] * scale, val[1] * it.val[1] * scale,
v2 * it.v2 * scale, v3 * it.v3 * scale ); val[2] * it.val[2] * scale, val[3] * it.val[3] * scale );
} }
public Scalar mul(Scalar it) { public Scalar mul(Scalar it) {
return mul(it, 1); return mul(it, 1);
} }
public Scalar conj() { public Scalar conj() {
return new Scalar(v0, -v1, -v2, -v3); return new Scalar(val[0], -val[1], -val[2], -val[3]);
} }
public boolean isReal() { public boolean isReal() {
return v1 == 0 && v2 == 0 && v3 == 0; return val[1] == 0 && val[2] == 0 && val[3] == 0;
} }
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
long temp; result = prime * result + java.util.Arrays.hashCode(val);
temp = Double.doubleToLongBits(v0); return result;
result = prime * result + (int) (temp ^ (temp >>> 32)); }
temp = Double.doubleToLongBits(v1);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v2);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(v3);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) return true;
if (!(obj instanceof Scalar)) return false; if (!(obj instanceof Scalar)) return false;
Scalar it = (Scalar) obj; Scalar it = (Scalar) obj;
return v0 == it.v0 && v1 == it.v1 && v2 == it.v2 && v3 == it.v3; if (!java.util.Arrays.equals(val, it.val)) return false;
} return true;
}
} }

Loading…
Cancel
Save