[vtkusers] creating vtkImageData from byte array in Java
Oliver Vidovic
ovidovic at medipattern.com
Mon Apr 8 16:04:33 EDT 2002
Hi vtkusers,
How can I create vtkImageData from a byte array? I know this works:
-- begin code
public void createImageDataFromByteArray( byte[] data )
{
vtkImageData image = new vtkImageData();
image.SetDimensions( xsize, ysize, zsize );
image.SetExtent( xmin, xmax, ymin, ymax, zmin, zmax );
image.SetNumberOfScalarComponents( 1 );
image.SetOrigin( xorigin, yorigin, zorigin );
image.SetSpacing( xspacing, yspacing, zspacing );
image.SetScalarType( 3 ); // VTK_UNSIGNED_CHAR
// set values
vtkScalars scalars = new vtkScalars();
scalars.SetNumberOfComponents( 1 );
scalars.SetDataTypeToUnsignedChar();
int index = 0;
for ( int x = 0; x < xsize; x++ )
{
for ( int y = 0; y < ysize; y++ )
{
for ( int z = 0; z < zsize; z++ )
{
scalars.InsertNextScalar( data[ index ] );
index++;
}
}
}
image.GetPointData().SetScalars( scalars );
return image;
} // end method
-- end code
However, with this approach I have to loop through all the data in the byte
array and assign it to vtkScalars. Since I am working with 25 - 50 MB byte
arrays, is there a faster/better/smarter way to do this?
In C++ there is a class vtkImageImport and methods that can take void* to
the data, but those methods are not exposed in Java. Any reason why they are
not exposed? Can I expose them using JNI? Will there be any known problems?
Has anyone done it before?
Note: I am using vtk 3.2 and Java 1.3.
thanks
oliver
More information about the vtkusers
mailing list