Creating small BufferedImages from large BufferedImage

Hello Java Gaming Community.

I heard from Sun that this is the place to post about Java2D performance questions.

I am working on the Java Instructional Gaming Engine. We are trying to speed up the performance on our spritesheet loading code. Basically we have an XML file and Image pair that are loaded at the beginning of games. Each animation has to copy out all of the frames out of the main image.

From my testing I have found that the larger the main image is, the longer it takes to load. While the obvious solution might be just to not use big images, it is bound to happen when students are using the engine.

I have extracted essentially what we are doing into the code below. We are doing a little more than this with setting frame properties etc. but this is basically it.


import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class LoadTest {
	
	Frame frame;
	URL sheetURL;
	BufferedImage sheetImage;
	
	public static void main(String[] args) {
		//System.setProperty("sun.java2d.opengl", "True");
				
		new LoadTest();
	}
	
	public LoadTest()
	{
		long st = System.nanoTime();
		frame = new Frame();
		
		sheetURL = ClassLoader.getSystemResource("bugs.png");
		
		try {
			sheetImage = ImageIO.read(new BufferedInputStream(sheetURL
					.openStream()));

		} catch (IOException e) {
			sheetImage = null;
			System.err.println("IO Exception while loading image '"
					+ sheetURL.toString() + "'");
			return;
		}
		
		for(int i=0; i<10; ++i)
			load(i);		
		
		long t = ((System.nanoTime()-st)/(1000000));
		System.out.println("Total Load Test took " + t + " ms");
	}
	
	public boolean load(int i)
	{		
		int width = 20;
		int height = 20;
		int top = i * height;
		int left = 0;
		int rows = 1;
		int columns = 10;
		
		long st = System.nanoTime();
				
		BufferedImage[] bframes = new BufferedImage[rows * columns];
		for (int y = 0; y < rows; ++y) {
			for (int x = 0; x < columns; ++x) {
				bframes[y * (columns) + x] = createImageResource(
						sheetImage, Transparency.BITMASK, width,
						height, left + width * x, top + height * y);
			}
		}		
		long t = ((System.nanoTime()-st)/(1000000));
		System.out.println("Created Image List with " + (rows*columns) + " elements in " + 
					t + " ms for resource ");
		
		return false;
	}

	protected BufferedImage createImageResource(final BufferedImage originalImg,
			final int transparency, final int w, final int h, 
			final int xoffset, final int yoffset) {

		BufferedImage r = drawImage(originalImg, transparency, w, h, -xoffset,
				-yoffset);
		return r;
	}
	
	protected BufferedImage drawImage(final Image i, final int transparency, final int w, final int h,
			final int xoffset, final int yoffset) 
	{
		GraphicsConfiguration gc = frame.getGraphicsConfiguration();
		BufferedImage image = gc.createCompatibleImage(w, h, transparency);
		image.getGraphics().drawImage(i, xoffset, yoffset, null);
		
		return image;
	}
}

I have attached the images to this post. Here are my run times for each.
bugs.png - Total Load Test took 1206 ms
bugs_large.png Total Load Test took 17638 ms

Do you guys have any suggestions on how we could speed this up?

Thanks,

I think the main problem is that the large image isn’t getting hardware accelerated. It can’t be stored in video memory because it’s too large. Try changing it to something no larger than 1024x768. Assuming you can view that resolution on your screen and have an adequate video card, it will probably be much faster if I’m right.

There is probably some way to load the image in chunks without storing it in a larger image. javax.imageio.ImageReader has something involving tiles, which makes me think it supports this capability. I’m not sure how it works though.

If you figure out how to use javax.imageio.ImageReader in this manner, I would appreciate it if you posted the code. I’m a bit curious about it.

There are a couple minor things I noticed. First, your drawImage method should call dispose() on the Graphics context after you finished drawing the image. I doubt that is causing the problem, but dispose should free up some kind of resources.

Second, you could optimize the loops that split the image up into tiles by reducing the multiplications to additions. This probably won’t make enough of a difference, but it something I would always do with multiplications in an inner loop. Here’s some code to explain what I mean:


int iFrame = 0;
int yOffsetInSourceImage = top;
for (int y = 0; y < rows; ++y) {
   int xOffsetInSourceImage = left;

   for (int x = 0; x < columns; ++x) {
      bframes[iFrame] = createImageResource(sheetImage, Transparency.BITMASK, width,
         height, xOffsetInSourceImage, yOffsetInSourceImage);

      //update index and x-offset
      iFrame++;
      xOffsetInSourceImage += width;
   }

   //update y-offset
   yOffsetInSourceImage += height;
}

I believe that is correct unless I’m interpreting some of your variables wrong.

Addition is a bit faster than multiplication, though this optimization would probably have a neglible effect. It might even slow things down if it prevents some other memory from being cached by the CPU.

Thanks for the reply Fletcher.

I was experimenting with it more and trying Volatile Images and I discovered the problem.

When you load in a png with ImageIO it can sometimes be read in as a translucent image. If I copied that to a BITMASK buffered image it was then able to be accelerated.

I am still looking for a way to force initially loading it as a BITMASK transparency, but for now this works.

While the addition optimization might help, I am going to hold off since this is only done once during loading, not in a game loop.

Thanks

Ah thanks fletchergames for that tip, I knew addition was faster but didn’t think of doing my loops like so.