Search code examples
javaeclipseswteclipse-pde

SWT Image concatenation or tiling / mosaic


I have an Eclipse RCP application that displays a lot (10k+) of small images next to each other, like a film strip. For each image, I am using a SWT Image object. This uses an excessive amount of memory and resources. I am looking for a more efficient way. I thought of taking all of these images and concatenating them by creating an ImageData object of the proper total, concatenated width (with a constant height) and using setPixel() for the rest of the pixels. However, the Palette used in the ImageData constructor I can't figure out.

I also searched for SWT tiling or mosaic functionality to create one image from a group of images, but found nothing.

Any ideas how I can display thousands of small images next to each other efficiently? Please note that once the images are displayed, they are not manipulated, so this is a one-time cost.


Solution

  • You can draw directly on the GC (graphics context) of a new (big) image. Having one big Image should result in much less resource usage than thousands of smaller images (each image in SWT keeps some OS graphics object handle)

    What you can try is something like this:

            final List<Image> images;
            final Image bigImage = new Image(Display.getCurrent(), combinedWidth, height);
            final GC gc = new GC(bigImage);
            //loop thru all the images while increasing x as necessary:
            int x = 0;
            int y = 0;
            for (Image curImage : images) {
                gc.drawImage(curImage, x, y);
                x += curImage.getBounds().width;
            }
            //very important to dispose GC!!!
                        gc.dispose();
            //now you can use bigImage