Wednesday, August 15, 2007

High performance graphics with SWT

When dealing with high performance graphics using a Graphics Context is not always the fastest way to render images.

We discovered this when trying to render a seismic variable density plot. On a typical two monitor system we were rendering an partial image of 2,000x800 pixels. Repeated calls to GC.drawPoint() or GC.fillRectangle() returned extremely slow rendering times. A whole seismic panel could be 10,000x4000 pixels. A large dataset could have 15,000 panels (about 500 Gigabytes) so you see we have to do things quickly.

The answer was to draw directly into an ImageData by setting each pixel using an indexed palette. In the image above text and grid lines are drawn using the Graphics Context while the colour pixels are set using ImageData.setPixel(). It sounds slow but ends up being extremely fast.

    PaletteData paletteData = new PaletteData(new RGB[] 
                   {new RGB(255,0,0), new RGB(0,255,0)});
    ImageData imageData = new ImageData(48,48,1,paletteData);
    for(int x=11;x<35;x++){
        for(int y=11;y<35;y++){
            imageData.setPixel(x,y,1);
        }
    }
    Image image = new Image(display,imageData); 

There is a great article on using SWT Image called Taking a look at SWT Images Summary which the code example above was taken from.

1 comment:

Mário Marinato said...

Hello,

I've been facing performance problems with some stock market charts and it seems the article you linked will help me a lot.

Thank you for sharing.

Regards from Brazil.