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

William A. Hoffman billlist at nycap.rr.com
Wed, 03 Mar 2004 15:24:00 -0500


We are using SWIG as the wrapper generator.  Many of the questions you ask
can be answered here (such as why the strange names):

http://www.swig.org/Doc1.3/Java.html

We may be closer than you think to doing what you want.
If you look at  SwigExtras.java it gives you the ability to create and set arrays
that can be passed in to things.   If itkImportImageFilter was added
to the classes being wrapped, you might be able to use SwigExtras to create
the arrays that you need and pass them into the SetData method.

All of the wrap_XXX.cxx files are generated and should not be edited.
The best thing to do would be to figure out the API that we want and then
figure out how to have swig generate the correct mapping.   Swig typemaps
can most likely be used to do much of what you want.

I would like to get the API figured out first.   Basically, it sounds
like you want to go from Java images to itk images, and from itk images
to java images.  

-Bill

At 02:52 PM 3/3/2004, you wrote:


>Hi Bill
>
>I was thinking in your words. 
>I have the following proposal for the import issue: implement a java version of ImportImageFilter within the java wrappers, I mean a class with native methods that can access a “real” ImportImageFilter. I was looking how the wrappers works and I imagine something like this:
>
>public class itkImportImageFilterJNI {
>        protected native static long itkImportImageFilterUS2_New();
>        protected native static void setDataSI2(long ptr, short[] rawData);
>        protected native static void setOrigin(long ptr, double[] origin);
>        protected native static void setSpacing(long ptr,double[] spacing);
>        protected native static void setSize(long ptr, int[] size);
>        protected native static long getOutput(long ptr);
>}
>
>public class itkImportImageFilterUS2 {
>        public static itkImportImageFilterUS2_Pointer itkImportImageFilterUS2_New() {
>                return new itkImportImageFilterUS2_Pointer(itkImportImageFilterJNI.itkImportImageFilterUS2_New());
>        }
>}
>
>public class itkImportImageFilterUS2_Pointer {
>        protected long jniPointer;
>        
>        protected itkImportImageFilterUS2_Pointer(long pointer) {
>                jniPointer = pointer;
>        }
>        
>        public void setOrigin(double[] origin) {
>                itkImportImageFilterJNI.setOrigin(jniPointer, origin);
>        }
>        
>        public void setSpacing(double[] spacing) {
>                itkImportImageFilterJNI.setSpacing(jniPointer, spacing);
>        }
>        
>        public void setSize(int[] size) {
>                itkImportImageFilterJNI.setSize(jniPointer, size);
>        }
>        
>        public void setData(short[] rawData) {
>                itkImportImageFilterJNI.setDataSI2(jniPointer, rawData);
>        }
>        
>        public SWIGTYPE_p_itk__ImageTunsigned_short_2_t getOutput() {
>                long outPtr = itkImportImageFilterJNI.getOutput(jniPointer);
>                return (outPtr == 0) ? null : new SWIGTYPE_p_itk__ImageTunsigned_short_2_t(outPtr, false);
>        }
>}
>
>        The next code shows how to use import filter with a PlanarImage:
>
>
>public class itkImportImageFilterTest {
>        
>        public static void main(String[] args) throws Exception {
>                PlanarImage image = PlanarImage.wrapRenderedImage(ImageIO.read(new File(args[0])));
>                short[] rawBuffer = getRawData(image.getData().getDataBuffer());
>                itkImportImageFilterUS2_Pointer importFilter = itkImportImageFilterUS2.itkImportImageFilterUS2_New();
>                importFilter.setOrigin(new double[] { 0.0, 0.0 });
>                importFilter.setSpacing(new double[] { 0.0, 0.0 });
>                importFilter.setSize(new int[] { image.getWidth(), image.getHeight() } );
>                importFilter.setData(rawBuffer);
>                itkRescaleIntensityImageFilterUS2US2_Pointer filter = itkRescaleIntensityImageFilterUS2US2.itkRescaleIntensityImageFilterUS2US2_New();
>                filter.SetOutputMinimum(0);
>                filter.SetOutputMaximum(65536);
>                filter.SetInput(importFilter.getOutput());
>                itkImageFileWriterUS2_Pointer writer = itkImageFileWriterUS2.itkImageFileWriterUS2_New();
>                writer.SetFileName(args[1]);
>                writer.SetInput(filter.GetOutput());
>                writer.Update();
>        }
>        
>        public static short[] getRawData(DataBuffer buffer) {
>                short[] data = new short[buffer.getSize()];
>                for (int i = 0; i < data.length; i++) {
>                        data[i] = clampShort(buffer.getElem(i));
>                }
>                return data;
>        }
>
>        public static short clampShort(int x) {
>                if (x < 0) {
>                        return 0;
>                } 
>                return (x > 65535) ? (short)65535 : (short)x;
>        }       
>}
>
>
>Unfortunately I have not finished the proposal for the export, I am reading the wrapper’s source code and as you can imagine it’s not a quick reading. For the export I am thinking in an adapter, so you can set their input for example with an SWIGTYPE_p_itk__ImageTunsigned_short_2_t instance and get a short[] as output. Something like this:
>
>public class itkJavaImageAdapterUS2 {
>        protected long jniPointer; 
>        
>        protected itkJavaImageAdapterUS2(long ptr) {
>                jniPointer = ptr;
>        }
>        
>        public void setInput(SWIGTYPE_p_itk__ImageTunsigned_short_2_t input) {
>                // DO THE JNI STUFF
>        }
>        
>        public short[] getData() {
>                short[] data = null;
>                // OBTAIN THE DATA VIA AN ITERATOR (JNI CODE)
>                return data;
>        }       
>}
>
>
>I think I can try to do both import and export for my own. But I have some questions about compiling the wrappers (C++ code):
>
>-       Can I compile ONLY the Wrappers DLL’s (wrap_itkXXXXXJava.cxx). When I modify something in the twrappers, there are many things are recompiled along the wrappers. (I am using Visual C++). I am using a centrino laptop, so you can imagine recompile is not very fast.
>-       Why the function names are so strange?. I have used JNI and the function names usually are the prefix Java_ followed by the package + class + method names. But the wrappers do not follow this convention adding suffix like “1_1SWIG_10” to the methods name, also many intermediate l’s. I know that the code is generated by cswig but I am curious about the meaning of the l’s and the suffix.
>-       Do you think that I need to make the changes directly in these files (wrap_itkXXXXX)? It’s all right if I make another library?
>
>
>Another issue is that I think that this adapter needs to notify their peer when an update request was made.
>
>What do you think?
>
>
>Well I have to go. Thanks for your attention; I will check my email within the next hours.
>
>Thanks in advance
>Alejandro Canales
>
>
>
>
>
>
>
>
>
>El Tue, 02 Mar 2004 15:12:09 -0500, billlist at nycap.rr.com escribio:
>>
>>Can you summarize the changes you would like?
>>I have been out of the office for a week or so, and have
>>been looking at your postings.  You can certainly help by providing
>>a description of what you want, and possibly some implementation code.
>>I guess the big thing you want is a way to send image data from
>>java to ITK and from ITK to java.   What sort of API would you expect?
>>How would you implement it if you did it by hand?
>>
>>-Bill
>>
>>
>>
>>At 02:33 AM 3/2/2004, Alejandro Canales Ochoa wrote:
>>>In the case you at Kitware can implement my requirements, when can it will be done? Can I help in any way (except by no asking)? The case is that I need to deliver the app within two months, so I need to make some decisions here. 
>>
>>
>_______________________________________________
>Insight-users mailing list
>Insight-users at itk.org
>http://www.itk.org/mailman/listinfo/insight-users