"__int64" : { "j_type" : "long", "jn_type" : "long", "jni_type" : "jlong", "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" : "cv::Mat& %(n)s = *((cv::Mat*)%(n)s_nativeObj); //cv::Mat& %(n)s = (%(n)s_nativeObj ? *((cv::Mat*)%(n)s_nativeObj) : cv::Mat())",\r
+ "jni_var" : "Mat& %(n)s = *((Mat*)%(n)s_nativeObj)",\r
"jni_type" : "jlong", #"jni_name" : "*%(n)s",\r
"suffix" : "J" },\r
"Point" : { "j_type" : "Point", "jn_args" : (("double", ".x"), ("double", ".y")),\r
"suffix" : "DDDD"},\r
"string" : { "j_type" : "java.lang.String", "jn_type" : "java.lang.String",\r
"jni_type" : "jstring", "jni_name" : "n_%(n)s",\r
- "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);',\r
+ "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)',\r
"suffix" : "Ljava_lang_String_2"},\r
\r
}\r
name = name[name.find(" ")+1:].strip()\r
self.cname = self.name = self.jname = re.sub(r"^cv\.", "", name)\r
self.cname =self.cname.replace(".", "::")\r
- self.jname = re.sub(r"^Cv", "", self.jname)\r
+ #self.jname = re.sub(r"^Cv", "", self.jname)\r
self.methods = {}\r
self.consts = [] # using a list to save the occurence order\r
for m in decl[2]:\r
self.consts = [] # using a list to save the occurence order\r
self.module = ""\r
self.java_code = StringIO()\r
+ self.jn_code = StringIO()\r
self.cpp_code = StringIO()\r
self.ported_func_counter = 0\r
self.func_counter = 0\r
sys.exit(-1)\r
type_dict[classinfo.name] = \\r
{ "j_type" : classinfo.name, "jn_args" : (("__int64", ".nativeObj"),),\r
- "jni_name" : "(*((cv::"+classinfo.name+"*)%(n)s_nativeObj))",\r
+ "jni_name" : "(*("+classinfo.name+"*)%(n)s_nativeObj)",\r
"suffix" : "J" }\r
\r
\r
pass\r
\r
# java module header\r
- self.java_code.write(\\r
-"""package org.opencv;\r
-\r
-public class %(module)s {\r
-\r
- //Load the native jni library\r
- static {\r
- System.loadLibrary("opencv_java");\r
- }\r
-""" % {"module" : module} )\r
+ self.java_code.write("package org.opencv;\n\npublic class %s {\n" % module)\r
\r
if module == "core":\r
self.java_code.write(\\r
IPL_BORDER_REFLECT_101 = 4,\r
IPL_BORDER_TRANSPARENT = 5;\r
""" )\r
+\r
+ # java native stuff\r
+ self.jn_code.write("""\r
+ //\r
+ // native stuff\r
+ //\r
+ static { System.loadLibrary("opencv_java"); }\r
+""")\r
+\r
# cpp module header\r
self.cpp_code.write(\\r
-"""// This file is auto-generated, please don't edit!\r
+"""//\r
+// This file is auto-generated, please don't edit!\r
+//\r
\r
#include <jni.h>\r
/*\r
#include <android/log.h>\r
-#define TEGRA_LOG_TAG "OpenCV_for_Android"\r
-#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, TEGRA_LOG_TAG, __VA_ARGS__))\r
+#define MODULE_LOG_TAG "OpenCV.%s"\r
+#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, MODULE_LOG_TAG, __VA_ARGS__))\r
*/\r
\r
-""" % {"module" : module})\r
+""" % module)\r
self.cpp_code.write( "\n".join(['#include "opencv2/%s/%s"' % (module, os.path.basename(f)) \\r
for f in srcfiles]) )\r
+ self.cpp_code.write('\nusing namespace cv;\n')\r
self.cpp_code.write('\n\nextern "C" {\n\n')\r
\r
# step 2: generate the code for global constants\r
self.gen_funcs()\r
\r
# step 4: generate code for the classes\r
- #self.gen_classes() # !!! tempory disabled !!!\r
+ self.gen_classes()\r
\r
# module tail\r
+ self.java_code.write("\n\n" + self.jn_code.getvalue() + "\n")\r
self.java_code.write("}\n")\r
self.cpp_code.write('} // extern "C"\n')\r
\r
";\n\n")\r
\r
\r
- def gen_func(self, fi, isoverload):\r
+ def gen_func(self, fi, isoverload, jn_code):\r
self.func_counter += 1\r
\r
- # java part & cpp part:\r
- # // c_decl\r
+ # // C++: c_decl\r
# e.g:\r
- # // void add(Mat src1, Mat src2, Mat dst, Mat mask = Mat(), int dtype = -1)\r
+ # // C++: void add(Mat src1, Mat src2, Mat dst, Mat mask = Mat(), int dtype = -1)\r
c_decl = "%s %s %s(%s)" % \\r
( fi.static, fi.ctype, fi.cname, \\r
", ".join(a.ctype + " " + a.name + [""," = "+a.defval][bool(a.defval)] for a in fi.args) )\r
indent = " " * 4\r
if fi.classname:\r
indent += " " * 4\r
- self.java_code.write( "\n%s// %s\n" % (indent, c_decl) )\r
- self.cpp_code.write( "\n//\n//%s\n//\n" % c_decl )\r
+ # java comment\r
+ self.java_code.write( "\n%s// C++: %s\n" % (indent, c_decl) )\r
# check if we 'know' all the types\r
type_info = type_dict.get(fi.ctype)\r
if not (type_info and type_info.get("jn_type")): # unsupported ret type\r
msg = "// Return type '%s' is not supported, skipping the function\n\n" % fi.ctype\r
self.java_code.write( indent + msg )\r
- self.cpp_code.write( msg )\r
+ #self.cpp_code.write( msg )\r
print "SKIP:", c_decl, "\n\tdue to RET type", fi.ctype\r
return\r
for a in fi.args:\r
if a.ctype not in type_dict:\r
msg = "// Unknown type '%s', skipping the function\n\n" % a.ctype\r
self.java_code.write( indent + msg )\r
- self.cpp_code.write( msg )\r
+ #self.cpp_code.write( msg )\r
print "SKIP:", c_decl, "\n\tdue to ARG type", a.ctype\r
return\r
if a.ctype != "Mat" and "jn_args" in type_dict[a.ctype] and a.out: # complex out args not yet supported\r
msg = "// Unsupported type '%s&', skipping the function\n\n" % a.ctype\r
self.java_code.write( indent + msg )\r
- self.cpp_code.write( msg )\r
+ #self.cpp_code.write( msg )\r
print "SKIP:", c_decl, "\n\tdue to OUT ARG of type", a.ctype\r
return\r
\r
self.ported_func_counter += 1\r
\r
+ # jn & cpp comment\r
+ jn_code.write( "\n%s// C++: %s\n" % (indent, c_decl) )\r
+ self.cpp_code.write( "\n//\n// %s\n//\n" % c_decl )\r
+\r
# java args\r
args = fi.args[:] # copy\r
if args and args[-1].defval:\r
# private java NATIVE method decl\r
# e.g.\r
# private static native void n_add(long src1, long src2, long dst, long mask, int dtype);\r
- self.java_code.write( Template(\\r
+ jn_code.write( Template(\\r
"${indent}private static native $jn_type $jn_name($jn_args);\n").substitute(\\r
indent = indent, \\r
jn_type = type_dict[fi.ctype]["jn_type"], \\r
# cpp part:\r
# jni_func(..) { return cv_func(..); }\r
ret = "return "\r
+ ext = ""\r
if fi.ctype == "void":\r
ret = ""\r
+ elif fi.ctype == "string":\r
+ ret = "return env->NewStringUTF"\r
+ ext = ".c_str()"\r
elif fi.ctype in self.classes: # wrapped class:\r
- ret = "return (jlong) new cv::" + self.classes[fi.ctype].jname\r
+ ret = "return (jlong) new " + self.classes[fi.ctype].jname\r
\r
cvname = "cv::" + fi.name\r
j2cvargs = []\r
if fi.classname:\r
if not fi.ctype: # c-tor\r
- cvname = "(jlong) new cv::" + fi.classname\r
+ cvname = "(jlong) new " + fi.classname\r
elif fi.static:\r
- cvname = "cv::%s::%s" % (fi.classname, fi.name)\r
+ cvname = "%s::%s" % (fi.classname, fi.name)\r
else:\r
cvname = "me->" + fi.name\r
j2cvargs.append(\\r
- "cv::%(cls)s* me = (cv::%(cls)s*) self; //TODO: check for NULL" \\r
+ "%(cls)s* me = (%(cls)s*) self; //TODO: check for NULL" \\r
% { "cls" : fi.classname} \\r
)\r
cvargs = []\r
JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname\r
($args)\r
{\r
+ //LOGD("$module::$fname()");\r
$j2cv\r
- //LOGD("$module :: $fname");\r
- $ret( $cvname( $cvargs ) );\r
+ $ret( $cvname( $cvargs )$ext );\r
}\r
\r
\r
ret = ret, \\r
cvname = cvname, \\r
cvargs = ", ".join([a for a in cvargs]), \\r
+ ext = ext, \\r
) )\r
\r
# processing args with default values\r
for name, ffi in fflist:\r
assert not ffi.funcs[0].classname, "Error: global func is a class member - "+name\r
for fi in ffi.funcs:\r
- self.gen_func(fi, len(ffi.funcs)>1)\r
+ self.gen_func(fi, len(ffi.funcs)>1, self.jn_code)\r
\r
\r
def gen_classes(self):\r
# generate code for the classes (their methods and consts)\r
- indent = "\t"\r
- indent_m = indent + "\t"\r
+ indent = " " * 4\r
+ indent_m = indent + " " * 4\r
classlist = self.classes.items()\r
classlist.sort()\r
for name, ci in classlist:\r
- self.java_code.write( "\n" + indent + "// class %s" % (ci.cname) + "\n" )\r
+ if name == "Mat":\r
+ continue\r
+ self.java_code.write( "\n\n" + indent + "// C++: class %s" % (ci.cname) + "\n" )\r
self.java_code.write( indent + "public static class %s {\n\n" % (ci.jname) )\r
\r
# self\r
% name );\r
# constants\r
if ci.consts:\r
- prefix = "\n" + indent_m + "\t\t"\r
+ prefix = "\n" + indent_m + "\t"\r
s = indent_m + "public static final int" + prefix +\\r
("," + prefix).join(["%s = %s" % (c.name, c.value) for c in ci.consts]) + ";\n\n"\r
self.java_code.write( s )\r
+ # methods\r
+ jn_code = StringIO()\r
# c-tors\r
fflist = ci.methods.items()\r
fflist.sort()\r
for n, ffi in fflist:\r
if ffi.isconstructor:\r
for fi in ffi.funcs:\r
- self.gen_func(fi, len(ffi.funcs)>1)\r
+ self.gen_func(fi, len(ffi.funcs)>1, jn_code)\r
self.java_code.write( "\n" )\r
for n, ffi in fflist:\r
if not ffi.isconstructor:\r
for fi in ffi.funcs:\r
- self.gen_func(fi, len(ffi.funcs)>1)\r
+ self.gen_func(fi, len(ffi.funcs)>1, jn_code)\r
\r
- self.java_code.write( "\n" + indent + "}\n\n" )\r
+ self.java_code.write("\n\n" + indent_m + "// native stuff\n")\r
+ self.java_code.write( indent_m + "//\n" + jn_code.getvalue() )\r
+ self.java_code.write( "\n\n" + indent + "}\n\n" )\r
\r
\r
if __name__ == "__main__":\r