[Insight-developers] Re: [Insight-users] Java wrappers import/export data

William A. Hoffman billlist at nycap.rr.com
Mon, 08 Mar 2004 14:33:47 -0500


At 05:01 AM 3/4/2004, Alejandro Canales Ochoa wrote:

>IMPORTING AN IMAGE
>If I understand you, the ImportFilter should not be a problem for you to implement. I see the SwingExtras.java and understand that it provides you with a proxy class to create/remove arrays of primitive types. The only thing I was concern is that it needs to make a JNI call for each member of the array you need to set/get, and the performance could be affected. Maybe a two step approach should be used, one for allocate the C-Native array and the other to set/get a range in the array, thus reducing the JNI calls to two. Another concern is that there is missing a support  for unsigned short in SwingExtras.java, but I suppose that should not be difficult to accomplish this since it’s a wrapper for C code.

Unfortunately, I am going to have to take a break on this stuff for a while. (month??)
However, if you need the faster array copy stuff sooner.  Here is what I have found out:

The native swig input file at the end of this message can be used to create a java
function that will convert from SWIGTYPE_p_int to int[] in java, and the loop will
be in "C" so it should be fast.   You would have to add this to SwigExtras.i and
add it for unsinged char and all the other types in SwigExtras.i.   Also, to go
the other way, you would have to figure out how to create the function that would
go from a java int[] to a SWIGTYPE_p_int.   

With my other fix, you should be able to get at the image class from the reader now.
If you get things working, I can add it to CVS.


swig.i file for fast conversion of SWIGTYPE_p_int to java int[]:

%typemap(in) int arraySize "int arraySize = $input; $1 = $input;"
%typemap(jni) int*      "jintArray"
%typemap(jtype) int*    "int[]"
%typemap(jstype) int*   "int[]"
%typemap(out) int* 
%{$result = SWIG_JavaArrayOutInt(jenv, $1, arraySize); %}
%typemap(javaout) int* {
    return $jnicall;
  }

%inline %{
int* getJavaArrayFromCArray(int in[], int arraySize) {
  return in;
}
%}


%include "arrays_java.i" // Just to get the SWIG_JavaArrayOutInt() method
// This %include is put after the method being wrapped as otherwise
// its typemaps will be used, which we don't want. It might be easier to copy the code from
// SWIG_JavaArrayOutInt()


-Bill