[vtkusers] vtkCanvas to Java image

Biele Biele at quantentunnel.de
Wed Jun 30 13:33:40 EDT 2004


just for the archive:
With this code you can get the rendered image of a 3D scene correctly into a
java.awt.image.BufferedImage. (In my last post I messed a little with the
image size). Anyway this method is dead slow and to create a .mov-file it
works much faster to use the vtk image writer classes to save files to the
harddrive which are then read by Java Media Framework classes to create the
movie.


int[] renderSize = renderer.GetRenderWindow().GetSize();
int width  = renderSize[0];
int height = renderSize[1];
vtkUnsignedCharArray vtkPixelData = new vtkUnsignedCharArray();
renderer.GetRenderWindow().GetPixelData(0, 0, width, height,
                1, vtkPixelData);
BufferedImage bufImage = new BufferedImage(width, height,
                   BufferedImage.TYPE_INT_RGB);
int[] rgbArray = new int[(width) * (height)];
int index, r, g, b;
double[] rgbFloat;
// bad performance because one has to get the values out of the vtk
// data structure tuple by tuple (instead of one "copyToArray") ...
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        index = ((y * (width + 1)) + x);
        rgbFloat = vtkPixelData.GetTuple3(index);
        r = (int) rgbFloat[0];
        g = (int) rgbFloat[1];
        b = (int) rgbFloat[2];
        // vtk window origin: bottom left, Java image origin: top left
        rgbArray[((height -1 - y) * (width)) + x] =
                   ((r << 16) + (g << 8) + b);
    }
}
bufImage.setRGB(0, 0, width, height, rgbArray, 0, width);





More information about the vtkusers mailing list