[vtkusers] vtkCanvas to Java image

Biele Biele at quantentunnel.de
Thu Jun 24 13:08:56 EDT 2004


Thanks for the code, Shyam. It didn't work but it was a good starting point.
I developed the following code, which works fine for me. I tried to optimize
performance but because of the many Java/C++ crossings it's still slow.


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 + 1, height + 1,

BufferedImage.TYPE_INT_RGB);

int[] rgbArray = new int[(width + 1) * (height + 1)];

int index, r, g, b;
double[] rgbFloat;

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 - y) * (width + 1)) + x] =
                                                  ((r << 16) + (g << 8) +
b);
    }
}

bufImage.setRGB(0, 0, width+1, height+1, rgbArray, 0, width+1);





More information about the vtkusers mailing list