Calling C/C++ code using the JNI when programming in Jython

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Calling C/C++ code using the JNI when programming in Jython

haider riaz
Hello everyone

I am transferring an image denoising software called “CANDLE” from MatLab to ImageJ. ImageJ is an open source program for image analysis and supports JVM languages. And I decided upon Jython to rewrite CANDLE in (Eclipse is the IDE). The MatLab version of CANDLE has a C program compiled as a MEX file that it uses. I have modified the C file so that in theory it should now be callable using the Java Native Interface. The next step would be to write a Java class that declares the native methods, loads and calls the native library. It would look something like this in Java :

public class CANDLEWrapper {
  native void adaptONLMCollaborative(float[] jima, int v, int f,
                                       float[] sigma, float beta,
                                       float[] ref, float[] mask,
                                       int rows, int cols, int numSlices); /* (1) */
   static {
       System.loadLibrary("adapt_onlm_collaborative"); /* (2) */
   }
   static public void main(String argv[]) {
       CANDLEWrapper candle = new CANDLEWrapper();
      // Do parameter set up here
       // Read in the images - convert to 1D arrays
       // Call method
       float[] fima = candle.adaptONLMCollaborative(jima, v, f, sigma, beta,
                                                                        ref, mask, rows, cols, numSlices);
      // Convert to a 3D stack after for fima

   }
}


Could this be written as a Jython module instead. And how would one go about doing that? I also have to make use of a built-in MatLab function called upfirdn, which is MEX wrapped. I found a C++ package with a python module that implements upfirdn efficiently. The description for the package says that it was built using SWIG. Is SWIG compatible with Jython? And can I use it to create my own package that will allow me to integrate the C++ code for upfirdn with my Jython program. If not, any suggestions for the best course of action?

Thank You for your help. I really need it.


Cheers
Haider Khan
------------------------------------------------------------------------------
Slashdot TV.  Videos for Nerds.  Stuff that Matters.
http://pubads.g.doubleclick.net/gampad/clk?id=160591471&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: Calling C/C++ code using the JNI when programming in Jython

Jim Baker-2
For now, your best option is to write a wrapper module in Java, then use Jython to access this Java wrapper much like any other Java module. This is sort of what you are doing with CANDLEWrapper above.

SWIG and other Python C extension API tooling is not at this time supported for Jython, although there is some active development to eventually support this path by JyNI (jyni.org).

Given the use of C++, you should probably just use JNI for now - there is some alternative tooling that Jython uses for C binding, JNR, but you would still mostly likely need C code to provide linkage to C++.

- Jim

On Sun, Oct 5, 2014 at 10:13 PM, haider riaz <[hidden email]> wrote:
Hello everyone

I am transferring an image denoising software called “CANDLE” from MatLab to ImageJ. ImageJ is an open source program for image analysis and supports JVM languages. And I decided upon Jython to rewrite CANDLE in (Eclipse is the IDE). The MatLab version of CANDLE has a C program compiled as a MEX file that it uses. I have modified the C file so that in theory it should now be callable using the Java Native Interface. The next step would be to write a Java class that declares the native methods, loads and calls the native library. It would look something like this in Java :

public class CANDLEWrapper {
  native void adaptONLMCollaborative(float[] jima, int v, int f,
                                       float[] sigma, float beta,
                                       float[] ref, float[] mask,
                                       int rows, int cols, int numSlices); /* (1) */
   static {
       System.loadLibrary("adapt_onlm_collaborative"); /* (2) */
   }
   static public void main(String argv[]) {
       CANDLEWrapper candle = new CANDLEWrapper();
      // Do parameter set up here
       // Read in the images - convert to 1D arrays
       // Call method
       float[] fima = candle.adaptONLMCollaborative(jima, v, f, sigma, beta,
                                                                        ref, mask, rows, cols, numSlices);
      // Convert to a 3D stack after for fima

   }
}


Could this be written as a Jython module instead. And how would one go about doing that? I also have to make use of a built-in MatLab function called upfirdn, which is MEX wrapped. I found a C++ package with a python module that implements upfirdn efficiently. The description for the package says that it was built using SWIG. Is SWIG compatible with Jython? And can I use it to create my own package that will allow me to integrate the C++ code for upfirdn with my Jython program. If not, any suggestions for the best course of action?

Thank You for your help. I really need it.


Cheers
Haider Khan
------------------------------------------------------------------------------
Slashdot TV.  Videos for Nerds.  Stuff that Matters.
http://pubads.g.doubleclick.net/gampad/clk?id=160591471&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users



--

------------------------------------------------------------------------------
Slashdot TV.  Videos for Nerds.  Stuff that Matters.
http://pubads.g.doubleclick.net/gampad/clk?id=160591471&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: Calling C/C++ code using the JNI when programming in Jython

Stefan Richthofer
>>The description for the package says that it was built using SWIG. Is SWIG compatible with Jython?
 
I suppose you can still use SWIG to create this wrapper module Jim mentions ("For now, your best option is to write a wrapper module in Java"). At this stage swig/jython compatibility would not matter because it's only between swig and Java.
 
(I never directly worked with swig, so please apologize if there is something I overlook, for instance technical pitfalls in switching the destination language of the swig script...)
 
- Stefan
 
Gesendet: Montag, 06. Oktober 2014 um 15:37 Uhr
Von: "Jim Baker" <[hidden email]>
An: "haider riaz" <[hidden email]>
Cc: [hidden email]
Betreff: Re: [Jython-users] Calling C/C++ code using the JNI when programming in Jython
For now, your best option is to write a wrapper module in Java, then use Jython to access this Java wrapper much like any other Java module. This is sort of what you are doing with CANDLEWrapper above.
 
SWIG and other Python C extension API tooling is not at this time supported for Jython, although there is some active development to eventually support this path by JyNI (jyni.org).
 
Given the use of C++, you should probably just use JNI for now - there is some alternative tooling that Jython uses for C binding, JNR, but you would still mostly likely need C code to provide linkage to C++.
 
- Jim
 
On Sun, Oct 5, 2014 at 10:13 PM, haider riaz <haiderriazkhan@...> wrote:
Hello everyone

I am transferring an image denoising software called “CANDLE” from MatLab to ImageJ. ImageJ is an open source program for image analysis and supports JVM languages. And I decided upon Jython to rewrite CANDLE in (Eclipse is the IDE). The MatLab version of CANDLE has a C program compiled as a MEX file that it uses. I have modified the C file so that in theory it should now be callable using the Java Native Interface. The next step would be to write a Java class that declares the native methods, loads and calls the native library. It would look something like this in Java :

public class CANDLEWrapper {
  native void adaptONLMCollaborative(float[] jima, int v, int f,
                                       float[] sigma, float beta,
                                       float[] ref, float[] mask,
                                       int rows, int cols, int numSlices); /* (1) */
   static {
       System.loadLibrary("adapt_onlm_collaborative"); /* (2) */
   }
   static public void main(String argv[]) {
       CANDLEWrapper candle = new CANDLEWrapper();
      // Do parameter set up here
       // Read in the images - convert to 1D arrays
       // Call method
       float[] fima = candle.adaptONLMCollaborative(jima, v, f, sigma, beta,
                                                                        ref, mask, rows, cols, numSlices);
      // Convert to a 3D stack after for fima

   }
}


Could this be written as a Jython module instead. And how would one go about doing that? I also have to make use of a built-in MatLab function called upfirdn, which is MEX wrapped. I found a C++ package with a python module that implements upfirdn efficiently. The description for the package says that it was built using SWIG. Is SWIG compatible with Jython? And can I use it to create my own package that will allow me to integrate the C++ code for upfirdn with my Jython program. If not, any suggestions for the best course of action?

Thank You for your help. I really need it.


Cheers
Haider Khan
------------------------------------------------------------------------------
Slashdot TV.  Videos for Nerds.  Stuff that Matters.
http://pubads.g.doubleclick.net/gampad/clk?id=160591471&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users
 
 
--
------------------------------------------------------------------------------ Slashdot TV. Videos for Nerds. Stuff that Matters. http://pubads.g.doubleclick.net/gampad/clk?id=160591471&iu=/4140/ostg.clktrk_______________________________________________ Jython-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/jython-users

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users
Reply | Threaded
Open this post in threaded view
|

Re: Calling C/C++ code using the JNI when programming in Jython

Dan Stromberg-2
In reply to this post by Jim Baker-2
On Mon, Oct 6, 2014 at 6:37 AM, Jim Baker <[hidden email]> wrote:

> Given the use of C++, you should probably just use JNI for now - there is
> some alternative tooling that Jython uses for C binding, JNR, but you would
> still mostly likely need C code to provide linkage to C++.

I don't know much Java, but I believe I heard that JNA is easier to
use than JNI - more like CPython's ctypes.

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Jython-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jython-users