Getting images into vtk from Java quickly (was [vtkusers] vtkImageImport in java?)
David Marshburn
marshbur at cs.unc.edu
Fri Feb 10 18:34:43 EST 2006
On Fri, 10 Feb 2006 vtkusers-request at vtk.org wrote:
> ------------------------------
> Message: 3
> Date: Thu, 9 Feb 2006 10:35:49 -0800
> From: "Stewart, Terry" <Terry.Stewart at xeroxlabs.com>
>
> 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" <steven.robbins at videotron.ca>
> 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" <Terry.Stewart at xeroxlabs.com>
> 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
More information about the vtkusers
mailing list