Initial classes support, improved generated java & cpp code formatting.
This commit is contained in:
parent
89115f0fcc
commit
9f4df42e24
@ -22,7 +22,7 @@ type_dict = {
|
|||||||
"__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
|
"__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "suffix" : "J" },
|
||||||
# "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" },
|
# "complex" : { j_type : "?", jn_args : (("", ""),), jn_name : "", jni_var : "", jni_name : "", "suffix" : "?" },
|
||||||
"Mat" : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),
|
"Mat" : { "j_type" : "Mat", "jn_type" : "long", "jn_args" : (("__int64", ".nativeObj"),),
|
||||||
"jni_var" : "cv::Mat& %(n)s = *((cv::Mat*)%(n)s_nativeObj); //cv::Mat& %(n)s = (%(n)s_nativeObj ? *((cv::Mat*)%(n)s_nativeObj) : cv::Mat())",
|
"jni_var" : "Mat& %(n)s = *((Mat*)%(n)s_nativeObj)",
|
||||||
"jni_type" : "jlong", #"jni_name" : "*%(n)s",
|
"jni_type" : "jlong", #"jni_name" : "*%(n)s",
|
||||||
"suffix" : "J" },
|
"suffix" : "J" },
|
||||||
"Point" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")),
|
"Point" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")),
|
||||||
@ -54,7 +54,7 @@ type_dict = {
|
|||||||
"suffix" : "DDDD"},
|
"suffix" : "DDDD"},
|
||||||
"string" : { "j_type" : "java.lang.String", "jn_type" : "java.lang.String",
|
"string" : { "j_type" : "java.lang.String", "jn_type" : "java.lang.String",
|
||||||
"jni_type" : "jstring", "jni_name" : "n_%(n)s",
|
"jni_type" : "jstring", "jni_name" : "n_%(n)s",
|
||||||
"jni_var" : 'const char* utf_%(n)s = env->GetStringUTFChars(%(n)s, 0); std::string n_%(n)s( utf_%(n)s ? utf_%(n)s : "" ); env->ReleaseStringUTFChars(%(n)s, utf_%(n)s);',
|
"jni_var" : 'const char* utf_%(n)s = env->GetStringUTFChars(%(n)s, 0); std::string n_%(n)s( utf_%(n)s ? utf_%(n)s : "" ); env->ReleaseStringUTFChars(%(n)s, utf_%(n)s)',
|
||||||
"suffix" : "Ljava_lang_String_2"},
|
"suffix" : "Ljava_lang_String_2"},
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ class ClassInfo(object):
|
|||||||
name = name[name.find(" ")+1:].strip()
|
name = name[name.find(" ")+1:].strip()
|
||||||
self.cname = self.name = self.jname = re.sub(r"^cv\.", "", name)
|
self.cname = self.name = self.jname = re.sub(r"^cv\.", "", name)
|
||||||
self.cname =self.cname.replace(".", "::")
|
self.cname =self.cname.replace(".", "::")
|
||||||
self.jname = re.sub(r"^Cv", "", self.jname)
|
#self.jname = re.sub(r"^Cv", "", self.jname)
|
||||||
self.methods = {}
|
self.methods = {}
|
||||||
self.consts = [] # using a list to save the occurence order
|
self.consts = [] # using a list to save the occurence order
|
||||||
for m in decl[2]:
|
for m in decl[2]:
|
||||||
@ -150,6 +150,7 @@ class JavaWrapperGenerator(object):
|
|||||||
self.consts = [] # using a list to save the occurence order
|
self.consts = [] # using a list to save the occurence order
|
||||||
self.module = ""
|
self.module = ""
|
||||||
self.java_code = StringIO()
|
self.java_code = StringIO()
|
||||||
|
self.jn_code = StringIO()
|
||||||
self.cpp_code = StringIO()
|
self.cpp_code = StringIO()
|
||||||
self.ported_func_counter = 0
|
self.ported_func_counter = 0
|
||||||
self.func_counter = 0
|
self.func_counter = 0
|
||||||
@ -166,7 +167,7 @@ class JavaWrapperGenerator(object):
|
|||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
type_dict[classinfo.name] = \
|
type_dict[classinfo.name] = \
|
||||||
{ "j_type" : classinfo.name, "jn_args" : (("__int64", ".nativeObj"),),
|
{ "j_type" : classinfo.name, "jn_args" : (("__int64", ".nativeObj"),),
|
||||||
"jni_name" : "(*((cv::"+classinfo.name+"*)%(n)s_nativeObj))",
|
"jni_name" : "(*("+classinfo.name+"*)%(n)s_nativeObj)",
|
||||||
"suffix" : "J" }
|
"suffix" : "J" }
|
||||||
|
|
||||||
|
|
||||||
@ -236,16 +237,7 @@ class JavaWrapperGenerator(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# java module header
|
# java module header
|
||||||
self.java_code.write(\
|
self.java_code.write("package org.opencv;\n\npublic class %s {\n" % module)
|
||||||
"""package org.opencv;
|
|
||||||
|
|
||||||
public class %(module)s {
|
|
||||||
|
|
||||||
//Load the native jni library
|
|
||||||
static {
|
|
||||||
System.loadLibrary("opencv_java");
|
|
||||||
}
|
|
||||||
""" % {"module" : module} )
|
|
||||||
|
|
||||||
if module == "core":
|
if module == "core":
|
||||||
self.java_code.write(\
|
self.java_code.write(\
|
||||||
@ -273,20 +265,32 @@ public class %(module)s {
|
|||||||
IPL_BORDER_REFLECT_101 = 4,
|
IPL_BORDER_REFLECT_101 = 4,
|
||||||
IPL_BORDER_TRANSPARENT = 5;
|
IPL_BORDER_TRANSPARENT = 5;
|
||||||
""" )
|
""" )
|
||||||
|
|
||||||
|
# java native stuff
|
||||||
|
self.jn_code.write("""
|
||||||
|
//
|
||||||
|
// native stuff
|
||||||
|
//
|
||||||
|
static { System.loadLibrary("opencv_java"); }
|
||||||
|
""")
|
||||||
|
|
||||||
# cpp module header
|
# cpp module header
|
||||||
self.cpp_code.write(\
|
self.cpp_code.write(\
|
||||||
"""// This file is auto-generated, please don't edit!
|
"""//
|
||||||
|
// This file is auto-generated, please don't edit!
|
||||||
|
//
|
||||||
|
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
/*
|
/*
|
||||||
#include <android/log.h>
|
#include <android/log.h>
|
||||||
#define TEGRA_LOG_TAG "OpenCV_for_Android"
|
#define MODULE_LOG_TAG "OpenCV.%s"
|
||||||
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, TEGRA_LOG_TAG, __VA_ARGS__))
|
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))
|
||||||
*/
|
*/
|
||||||
|
|
||||||
""" % {"module" : 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]) )
|
||||||
|
self.cpp_code.write('\nusing namespace cv;\n')
|
||||||
self.cpp_code.write('\n\nextern "C" {\n\n')
|
self.cpp_code.write('\n\nextern "C" {\n\n')
|
||||||
|
|
||||||
# step 2: generate the code for global constants
|
# step 2: generate the code for global constants
|
||||||
@ -296,9 +300,10 @@ public class %(module)s {
|
|||||||
self.gen_funcs()
|
self.gen_funcs()
|
||||||
|
|
||||||
# step 4: generate code for the classes
|
# step 4: generate code for the classes
|
||||||
#self.gen_classes() # !!! tempory disabled !!!
|
self.gen_classes()
|
||||||
|
|
||||||
# module tail
|
# module tail
|
||||||
|
self.java_code.write("\n\n" + self.jn_code.getvalue() + "\n")
|
||||||
self.java_code.write("}\n")
|
self.java_code.write("}\n")
|
||||||
self.cpp_code.write('} // extern "C"\n')
|
self.cpp_code.write('} // extern "C"\n')
|
||||||
|
|
||||||
@ -318,45 +323,48 @@ public class %(module)s {
|
|||||||
";\n\n")
|
";\n\n")
|
||||||
|
|
||||||
|
|
||||||
def gen_func(self, fi, isoverload):
|
def gen_func(self, fi, isoverload, jn_code):
|
||||||
self.func_counter += 1
|
self.func_counter += 1
|
||||||
|
|
||||||
# java part & cpp part:
|
# // C++: c_decl
|
||||||
# // c_decl
|
|
||||||
# e.g:
|
# e.g:
|
||||||
# // void add(Mat src1, Mat src2, Mat dst, Mat mask = Mat(), int dtype = -1)
|
# // C++: void add(Mat src1, Mat src2, Mat dst, Mat mask = Mat(), int dtype = -1)
|
||||||
c_decl = "%s %s %s(%s)" % \
|
c_decl = "%s %s %s(%s)" % \
|
||||||
( fi.static, fi.ctype, fi.cname, \
|
( fi.static, fi.ctype, fi.cname, \
|
||||||
", ".join(a.ctype + " " + a.name + [""," = "+a.defval][bool(a.defval)] for a in fi.args) )
|
", ".join(a.ctype + " " + a.name + [""," = "+a.defval][bool(a.defval)] for a in fi.args) )
|
||||||
indent = " " * 4
|
indent = " " * 4
|
||||||
if fi.classname:
|
if fi.classname:
|
||||||
indent += " " * 4
|
indent += " " * 4
|
||||||
self.java_code.write( "\n%s// %s\n" % (indent, c_decl) )
|
# java comment
|
||||||
self.cpp_code.write( "\n//\n//%s\n//\n" % c_decl )
|
self.java_code.write( "\n%s// C++: %s\n" % (indent, c_decl) )
|
||||||
# check if we 'know' all the types
|
# check if we 'know' all the types
|
||||||
type_info = type_dict.get(fi.ctype)
|
type_info = type_dict.get(fi.ctype)
|
||||||
if not (type_info and type_info.get("jn_type")): # unsupported ret type
|
if not (type_info and type_info.get("jn_type")): # unsupported ret type
|
||||||
msg = "// Return type '%s' is not supported, skipping the function\n\n" % fi.ctype
|
msg = "// Return type '%s' is not supported, skipping the function\n\n" % fi.ctype
|
||||||
self.java_code.write( indent + msg )
|
self.java_code.write( indent + msg )
|
||||||
self.cpp_code.write( msg )
|
#self.cpp_code.write( msg )
|
||||||
print "SKIP:", c_decl, "\n\tdue to RET type", fi.ctype
|
print "SKIP:", c_decl, "\n\tdue to RET type", fi.ctype
|
||||||
return
|
return
|
||||||
for a in fi.args:
|
for a in fi.args:
|
||||||
if a.ctype not in type_dict:
|
if a.ctype not in type_dict:
|
||||||
msg = "// Unknown type '%s', skipping the function\n\n" % a.ctype
|
msg = "// Unknown type '%s', skipping the function\n\n" % a.ctype
|
||||||
self.java_code.write( indent + msg )
|
self.java_code.write( indent + msg )
|
||||||
self.cpp_code.write( msg )
|
#self.cpp_code.write( msg )
|
||||||
print "SKIP:", c_decl, "\n\tdue to ARG type", a.ctype
|
print "SKIP:", c_decl, "\n\tdue to ARG type", a.ctype
|
||||||
return
|
return
|
||||||
if a.ctype != "Mat" and "jn_args" in type_dict[a.ctype] and a.out: # complex out args not yet supported
|
if a.ctype != "Mat" and "jn_args" in type_dict[a.ctype] and a.out: # complex out args not yet supported
|
||||||
msg = "// Unsupported type '%s&', skipping the function\n\n" % a.ctype
|
msg = "// Unsupported type '%s&', skipping the function\n\n" % a.ctype
|
||||||
self.java_code.write( indent + msg )
|
self.java_code.write( indent + msg )
|
||||||
self.cpp_code.write( msg )
|
#self.cpp_code.write( msg )
|
||||||
print "SKIP:", c_decl, "\n\tdue to OUT ARG of type", a.ctype
|
print "SKIP:", c_decl, "\n\tdue to OUT ARG of type", a.ctype
|
||||||
return
|
return
|
||||||
|
|
||||||
self.ported_func_counter += 1
|
self.ported_func_counter += 1
|
||||||
|
|
||||||
|
# jn & cpp comment
|
||||||
|
jn_code.write( "\n%s// C++: %s\n" % (indent, c_decl) )
|
||||||
|
self.cpp_code.write( "\n//\n// %s\n//\n" % c_decl )
|
||||||
|
|
||||||
# java args
|
# java args
|
||||||
args = fi.args[:] # copy
|
args = fi.args[:] # copy
|
||||||
if args and args[-1].defval:
|
if args and args[-1].defval:
|
||||||
@ -389,7 +397,7 @@ public class %(module)s {
|
|||||||
# private java NATIVE method decl
|
# private java NATIVE method decl
|
||||||
# e.g.
|
# e.g.
|
||||||
# private static native void n_add(long src1, long src2, long dst, long mask, int dtype);
|
# private static native void n_add(long src1, long src2, long dst, long mask, int dtype);
|
||||||
self.java_code.write( Template(\
|
jn_code.write( Template(\
|
||||||
"${indent}private static native $jn_type $jn_name($jn_args);\n").substitute(\
|
"${indent}private static native $jn_type $jn_name($jn_args);\n").substitute(\
|
||||||
indent = indent, \
|
indent = indent, \
|
||||||
jn_type = type_dict[fi.ctype]["jn_type"], \
|
jn_type = type_dict[fi.ctype]["jn_type"], \
|
||||||
@ -433,22 +441,26 @@ public class %(module)s {
|
|||||||
# cpp part:
|
# cpp part:
|
||||||
# jni_func(..) { return cv_func(..); }
|
# jni_func(..) { return cv_func(..); }
|
||||||
ret = "return "
|
ret = "return "
|
||||||
|
ext = ""
|
||||||
if fi.ctype == "void":
|
if fi.ctype == "void":
|
||||||
ret = ""
|
ret = ""
|
||||||
|
elif fi.ctype == "string":
|
||||||
|
ret = "return env->NewStringUTF"
|
||||||
|
ext = ".c_str()"
|
||||||
elif fi.ctype in self.classes: # wrapped class:
|
elif fi.ctype in self.classes: # wrapped class:
|
||||||
ret = "return (jlong) new cv::" + self.classes[fi.ctype].jname
|
ret = "return (jlong) new " + self.classes[fi.ctype].jname
|
||||||
|
|
||||||
cvname = "cv::" + fi.name
|
cvname = "cv::" + fi.name
|
||||||
j2cvargs = []
|
j2cvargs = []
|
||||||
if fi.classname:
|
if fi.classname:
|
||||||
if not fi.ctype: # c-tor
|
if not fi.ctype: # c-tor
|
||||||
cvname = "(jlong) new cv::" + fi.classname
|
cvname = "(jlong) new " + fi.classname
|
||||||
elif fi.static:
|
elif fi.static:
|
||||||
cvname = "cv::%s::%s" % (fi.classname, fi.name)
|
cvname = "%s::%s" % (fi.classname, fi.name)
|
||||||
else:
|
else:
|
||||||
cvname = "me->" + fi.name
|
cvname = "me->" + fi.name
|
||||||
j2cvargs.append(\
|
j2cvargs.append(\
|
||||||
"cv::%(cls)s* me = (cv::%(cls)s*) self; //TODO: check for NULL" \
|
"%(cls)s* me = (%(cls)s*) self; //TODO: check for NULL" \
|
||||||
% { "cls" : fi.classname} \
|
% { "cls" : fi.classname} \
|
||||||
)
|
)
|
||||||
cvargs = []
|
cvargs = []
|
||||||
@ -464,9 +476,9 @@ public class %(module)s {
|
|||||||
JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
|
JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
|
||||||
($args)
|
($args)
|
||||||
{
|
{
|
||||||
|
//LOGD("$module::$fname()");
|
||||||
$j2cv
|
$j2cv
|
||||||
//LOGD("$module :: $fname");
|
$ret( $cvname( $cvargs )$ext );
|
||||||
$ret( $cvname( $cvargs ) );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -479,6 +491,7 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
|
|||||||
ret = ret, \
|
ret = ret, \
|
||||||
cvname = cvname, \
|
cvname = cvname, \
|
||||||
cvargs = ", ".join([a for a in cvargs]), \
|
cvargs = ", ".join([a for a in cvargs]), \
|
||||||
|
ext = ext, \
|
||||||
) )
|
) )
|
||||||
|
|
||||||
# processing args with default values
|
# processing args with default values
|
||||||
@ -497,17 +510,19 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
|
|||||||
for name, ffi in fflist:
|
for name, ffi in fflist:
|
||||||
assert not ffi.funcs[0].classname, "Error: global func is a class member - "+name
|
assert not ffi.funcs[0].classname, "Error: global func is a class member - "+name
|
||||||
for fi in ffi.funcs:
|
for fi in ffi.funcs:
|
||||||
self.gen_func(fi, len(ffi.funcs)>1)
|
self.gen_func(fi, len(ffi.funcs)>1, self.jn_code)
|
||||||
|
|
||||||
|
|
||||||
def gen_classes(self):
|
def gen_classes(self):
|
||||||
# generate code for the classes (their methods and consts)
|
# generate code for the classes (their methods and consts)
|
||||||
indent = "\t"
|
indent = " " * 4
|
||||||
indent_m = indent + "\t"
|
indent_m = indent + " " * 4
|
||||||
classlist = self.classes.items()
|
classlist = self.classes.items()
|
||||||
classlist.sort()
|
classlist.sort()
|
||||||
for name, ci in classlist:
|
for name, ci in classlist:
|
||||||
self.java_code.write( "\n" + indent + "// class %s" % (ci.cname) + "\n" )
|
if name == "Mat":
|
||||||
|
continue
|
||||||
|
self.java_code.write( "\n\n" + indent + "// C++: class %s" % (ci.cname) + "\n" )
|
||||||
self.java_code.write( indent + "public static class %s {\n\n" % (ci.jname) )
|
self.java_code.write( indent + "public static class %s {\n\n" % (ci.jname) )
|
||||||
|
|
||||||
# self
|
# self
|
||||||
@ -516,24 +531,28 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
|
|||||||
% name );
|
% name );
|
||||||
# constants
|
# constants
|
||||||
if ci.consts:
|
if ci.consts:
|
||||||
prefix = "\n" + indent_m + "\t\t"
|
prefix = "\n" + indent_m + "\t"
|
||||||
s = indent_m + "public static final int" + prefix +\
|
s = indent_m + "public static final int" + prefix +\
|
||||||
("," + prefix).join(["%s = %s" % (c.name, c.value) for c in ci.consts]) + ";\n\n"
|
("," + prefix).join(["%s = %s" % (c.name, c.value) for c in ci.consts]) + ";\n\n"
|
||||||
self.java_code.write( s )
|
self.java_code.write( s )
|
||||||
|
# methods
|
||||||
|
jn_code = StringIO()
|
||||||
# c-tors
|
# c-tors
|
||||||
fflist = ci.methods.items()
|
fflist = ci.methods.items()
|
||||||
fflist.sort()
|
fflist.sort()
|
||||||
for n, ffi in fflist:
|
for n, ffi in fflist:
|
||||||
if ffi.isconstructor:
|
if ffi.isconstructor:
|
||||||
for fi in ffi.funcs:
|
for fi in ffi.funcs:
|
||||||
self.gen_func(fi, len(ffi.funcs)>1)
|
self.gen_func(fi, len(ffi.funcs)>1, jn_code)
|
||||||
self.java_code.write( "\n" )
|
self.java_code.write( "\n" )
|
||||||
for n, ffi in fflist:
|
for n, ffi in fflist:
|
||||||
if not ffi.isconstructor:
|
if not ffi.isconstructor:
|
||||||
for fi in ffi.funcs:
|
for fi in ffi.funcs:
|
||||||
self.gen_func(fi, len(ffi.funcs)>1)
|
self.gen_func(fi, len(ffi.funcs)>1, jn_code)
|
||||||
|
|
||||||
self.java_code.write( "\n" + indent + "}\n\n" )
|
self.java_code.write("\n\n" + indent_m + "// native stuff\n")
|
||||||
|
self.java_code.write( indent_m + "//\n" + jn_code.getvalue() )
|
||||||
|
self.java_code.write( "\n\n" + indent + "}\n\n" )
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user