Getting images into vtk from Java quickly (was vtkImageImport in java?)

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

Getting images into vtk from Java quickly (was vtkImageImport in java?)

David Marshburn

On Fri, 10 Feb 2006 [hidden email] wrote:

> ------------------------------
> Message: 3
> Date: Thu, 9 Feb 2006 10:35:49 -0800
> From: "Stewart, Terry" <[hidden email]>
>
> Steve,
>
>   I'm using Java and ran into a similar situation while trying to get my
> internally-generated RGB image into vtkImageData.  I'd tried the
> vtkImageImport path too only to run into the same roadblock with the
> missing [Copy|Set]ImportVoidPointer() methods for Java.  I've gotten my
> logic to a functional state via:
>
>    vtkImageData imageData = new vtkImageData();
>          :
>    for (x = 0; ...)
>       for (y = 0; ... )
<snip>
>
> However this is quite slow.  Did you use some other mechanism to get
> your image data inserted into vtkImageData that is perhaps more
> efficient?  

> ------------------------------
>
> Message: 6
> Date: Thu, 09 Feb 2006 18:08:23 -0500
> From: "Steve M. Robbins" <[hidden email]>
> Subject: Re: Ghosts and Extents (was [vtkusers] vtkImageImport in
> java?)
>
> > However this is quite slow.
>
> Yep.
>
> > Did you use some other mechanism to get
> > your image data inserted into vtkImageData that is perhaps more
> > efficient?  
>
> Yep.  We wrote JNI code that effectively does a memcpy() to
> vtkImage.GetVoidPointer().  I'd give you the code except that the JNI
> bit is very specific to our application so it really wouldn't help
> you.
>
> ------------------------------
>
> Message: 7
> Date: Thu, 9 Feb 2006 15:32:19 -0800
> From: "Stewart, Terry" <[hidden email]>
> Subject: RE: Ghosts and Extents (was [vtkusers] vtkImageImport in
> java?)
>
>   OK, thanks.   Updating the JNI logic was an idea that my team had been
> contemplating.  Thanks for the confirmation.  And right, the interface
> updates we'd like to make would likely be different than yours, so no
> problem there.  

Before anyone goes to the length of writing jni code, here is a fast way
to get image data from Java down into vtk.  As with (arguably) any vtk
issue, the solution lies in finding just the right class and function out
of a sea of thousands of candidates! :)  (for a year or more, our group
thought that there was no fast way to get bulk data from java into vtk)

in Java, vtkImageData has a SetScalars method that takes as an arguement a
vtkDataArray.  vtkDataArray has a bunch of subclasses specific to
array-element types (e.g., vtkUnsignedCharArray, vtkUnsignedShortArray,
vtkFloatArray, etc.).  In Java, these data-array classes have a
SetJavaArray method that does fast copying.

some code like the following should work (this is for a 3D image stack):

  protected vtkImageData imageData_vtk = new vtkImageData( );
  protected vtkDataArray dataArray_vtk = null;
  protected Object singleArray = null;
        ....

        // for 8-bit pixels
        imageData_vtk.SetScalarTypeToUnsignedChar();
        imageData_vtk.SetNumberOfScalarComponents( 1 );
        dataArray_vtk = new vtkUnsignedCharArray( );
       
        imageData_vtk.SetDimensions( imageWidth, imageHeight, numImages );
        singleArray = new byte[ imageWidth * imageHeight * numImages ];
        imageData_vtk.AllocateScalars();

        // fill in singleArray, possibly using System.arraycopy

        ( (vtkUnsignedCharArray) dataArray_vtk).SetJavaArray( (byte[]) singleArray );
        imageData_vtk.GetPointData().SetScalars( dataArray_vtk );

this was written to be used for images of possibly many types; the code
would be simpler if you will only ever care about 8-bit images.

on a not-terribly-fast computer, this code will stuff a 50 or so 512x512
images into vtk in an amount of time that isn't notable, like a second or
two?  i've not timed it, although it is no longer wearisome to load images
in our java/vtk application!

cheers,
-david


_______________________________________________
This is the private VTK discussion list.
Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
Reply | Threaded
Open this post in threaded view
|

RE: Getting images into vtk from Java quickly (was vtkImageImport in java?)

Stewart, Terry
David,

  Thanks for mentioning the existence of the SetJavaArray() method.  The
time to copy a 512x512 RGB image improved by two orders of magnitude ...
pretty nice.  This was a big help and greatly appreciated!

All,

  I couldn't find this SetJavaArray() method in the VTK book, user's
manual or on-line Doxygen class documentation.  It seems that it, as
well as GetJavaArray(), are created specifically for Java while Java
wrapping is occurring during a build (see ./Java/vtkWrapJava.c).   I'm
guessing that Doxygen works entirely off the VTK *.h files so these
Java-only methods won't be visible because of the special way they are
generated.  So as far as I can tell you won't find them documented
anywhere other than the mail archives.

  Regardless, it's great that they are there and a big thanks to
whomever implemented them.

Terry

>-----Original Message-----
>From: vtkusers-bounces+terry.stewart=[hidden email]
>[mailto:vtkusers-bounces+terry.stewart=[hidden email]]
>On Behalf Of David Marshburn
>Sent: Friday, February 10, 2006 3:35 PM
>To: [hidden email]
>Subject: Getting images into vtk from Java quickly (was
>[vtkusers]vtkImageImport in java?)
>
>
>On Fri, 10 Feb 2006 [hidden email] wrote:
>
>> ------------------------------
>> Message: 3
>> Date: Thu, 9 Feb 2006 10:35:49 -0800
>> From: "Stewart, Terry" <[hidden email]>
>>
>> Steve,
>>
>>   I'm using Java and ran into a similar situation while
>trying to get
>> my internally-generated RGB image into vtkImageData.  I'd tried the
>> vtkImageImport path too only to run into the same roadblock with the
>> missing [Copy|Set]ImportVoidPointer() methods for Java.  I've gotten
>> my logic to a functional state via:
>>
>>    vtkImageData imageData = new vtkImageData();
>>          :
>>    for (x = 0; ...)
>>       for (y = 0; ... )
><snip>
>>
>> However this is quite slow.  Did you use some other mechanism to get
>> your image data inserted into vtkImageData that is perhaps more
>> efficient?
>
>> ------------------------------
>>
>> Message: 6
>> Date: Thu, 09 Feb 2006 18:08:23 -0500
>> From: "Steve M. Robbins" <[hidden email]>
>> Subject: Re: Ghosts and Extents (was [vtkusers] vtkImageImport in
>> java?)
>>
>> > However this is quite slow.
>>
>> Yep.
>>
>> > Did you use some other mechanism to get your image data inserted
>> > into vtkImageData that is perhaps more efficient?
>>
>> Yep.  We wrote JNI code that effectively does a memcpy() to
>> vtkImage.GetVoidPointer().  I'd give you the code except
>that the JNI
>> bit is very specific to our application so it really wouldn't help
>> you.
>>
>> ------------------------------
>>
>> Message: 7
>> Date: Thu, 9 Feb 2006 15:32:19 -0800
>> From: "Stewart, Terry" <[hidden email]>
>> Subject: RE: Ghosts and Extents (was [vtkusers] vtkImageImport in
>> java?)
>>
>>   OK, thanks.   Updating the JNI logic was an idea that my
>team had been
>> contemplating.  Thanks for the confirmation.  And right, the
>interface
>> updates we'd like to make would likely be different than
>yours, so no
>> problem there.
>
>Before anyone goes to the length of writing jni code, here is
>a fast way to get image data from Java down into vtk.  As with
>(arguably) any vtk issue, the solution lies in finding just
>the right class and function out of a sea of thousands of
>candidates! :)  (for a year or more, our group thought that
>there was no fast way to get bulk data from java into vtk)
>
>in Java, vtkImageData has a SetScalars method that takes as an
>arguement a vtkDataArray.  vtkDataArray has a bunch of
>subclasses specific to array-element types (e.g.,
>vtkUnsignedCharArray, vtkUnsignedShortArray, vtkFloatArray,
>etc.).  In Java, these data-array classes have a SetJavaArray
>method that does fast copying.
>
>some code like the following should work (this is for a 3D
>image stack):
>
>  protected vtkImageData imageData_vtk = new vtkImageData( );
>  protected vtkDataArray dataArray_vtk = null;
>  protected Object singleArray = null;
> ....
>
> // for 8-bit pixels
> imageData_vtk.SetScalarTypeToUnsignedChar();
> imageData_vtk.SetNumberOfScalarComponents( 1 );
> dataArray_vtk = new vtkUnsignedCharArray( );
>
> imageData_vtk.SetDimensions( imageWidth, imageHeight,
>numImages );
> singleArray = new byte[ imageWidth * imageHeight * numImages ];
> imageData_vtk.AllocateScalars();
>
> // fill in singleArray, possibly using System.arraycopy
>
> ( (vtkUnsignedCharArray) dataArray_vtk).SetJavaArray(
>(byte[]) singleArray );
> imageData_vtk.GetPointData().SetScalars( dataArray_vtk );
>
>this was written to be used for images of possibly many types;
>the code would be simpler if you will only ever care about
>8-bit images.
>
>on a not-terribly-fast computer, this code will stuff a 50 or
>so 512x512 images into vtk in an amount of time that isn't
>notable, like a second or two?  i've not timed it, although it
>is no longer wearisome to load images in our java/vtk application!
>
>cheers,
>-david
>
>
>_______________________________________________
>This is the private VTK discussion list.
>Please keep messages on-topic. Check the FAQ at:
>http://www.vtk.org/Wiki/VTK_FAQ Follow this link to
>subscribe/unsubscribe:
>http://www.vtk.org/mailman/listinfo/vtkusers
>
_______________________________________________
This is the private VTK discussion list.
Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers