RPG BufferedImage Unterschiede

Hi
I want to load .png-Images with BufferedImage.
What happens is, that if I load them with 2 different ways (as follows),
one makes my FPS go down to nearly 0 and
the other way gets at least 20 FPS, which is still not enaugh.

Here´s the code:

public static BufferedImage loadBuffImage(String absolutePath) {
/||Created on 08.03.2008 01:30:56||/
try {
BufferedImage bz = ImageIO.read(new File(absolutePath));
BufferedImage bi = new BufferedImage (bz.getWidth(),bz.getHeight(),BufferedImage.TYPE_INT_ARGB);
Graphics2D biG = bi.createGraphics();
biG.drawImage(bz,0,0,null);
return bi;
} catch (IOException e) {
}
return null;
}

public static BufferedImage loadBuffImage(String absolutePath) {
/||Created on 08.03.2008 01:30:56||/
try {
return ImageIO.read(new File(absolutePath));
} catch (IOException e) {
}
return null;
}

the ImageIO.read Method makes my FPS go down to 0, the other one not …
Why?

I got 1280*1024 with 8600GT and 3200+, so it seems to me something is wrong.
I already use MultiBuffering, so I hope someone can help me.

Do you read the images from a file during each frame? You should read the images only once and keep them in memory.

Why do you copy the image in the first code?

I only read them once from a file and keep them in a class ImageManagement.
Earlier I tried to load Images over AWT and needed to convert them from Image
to BufferedImage.
Since this worked well I tried this with BufferedImage, too.
I know it seems to make no sense, that i copy it from the loaded BI to another one.
But surprisingly when i draw these i got much better performance.
When I only load them with ImageIO, i can count every single frame.

What Java runtime version are you using?

Java 6.0 Update 3.
Is there any problem ?

Have you profiled your code to find out the bottlenecks?

Yes, but i´m not really used to interpret these things.
Here´s some of the output from the Profiler:

CPU SAMPLES BEGIN (total = 518) Sat Mar 08 19:33:29 2008
rank self accum count trace method
1 45.95% 45.95% 238 300254 sun.awt.windows.WToolkit.eventLoop
2 28.76% 74.71% 149 300487 sun.awt.windows.WToolkit.nativeSync
3 3.28% 77.99% 17 300508 sun.awt.image.ByteInterleavedRaster.getDataElements
4 2.32% 80.31% 12 300507 java.awt.image.ComponentColorModel.getRGBComponent
5 2.32% 82.63% 12 300491 java.awt.image.ComponentColorModel.getRGBComponent
6 1.93% 84.56% 10 300517 sun.awt.windows.WToolkit.shutdown
7 1.74% 86.29% 9 300430 sun.java2d.d3d.D3DContext.initNativeContext
8 1.54% 87.84% 8 300493 java.awt.image.ComponentColorModel.getRGBComponent
9 1.16% 89.00% 6 300492 java.awt.image.ComponentColorModel.getRGB
10 0.77% 89.77% 4 300521 java.lang.Shutdown.halt0
11 0.58% 90.35% 3 300443 sun.java2d.loops.MaskBlit.MaskBlit
12 0.58% 90.93% 3 300509 sun.java2d.loops.MaskBlit.MaskBlit
13 0.39% 91.31% 2 300416 sun.awt.windows.WInputMethod.setOpenStatus
14 0.39% 91.70% 2 300415 sun.java2d.windows.Win32SurfaceData.initOps
15 0.39% 92.08% 2 300196 sun.awt.Win32GraphicsEnvironment.initDisplay
16 0.39% 92.47% 2 300350 java.awt.image.DataBufferInt.
17 0.19% 92.66% 1 300486 sun.java2d.d3d.D3DBlitLoops.doTransform
18 0.19% 92.86% 1 300485 sun.font.FileFont.getGlyphImage
19 0.19% 93.05% 1 300460 java.io.WinNTFileSystem.getBooleanAttributes
20 0.19% 93.24% 1 300019 java.io.FilePermission.init
21 0.19% 93.44% 1 300510 sun.java2d.loops.OpaqueCopyAnyToArgb.Blit
22 0.19% 93.63% 1 300431 sun.java2d.d3d.D3DContext.setRenderTarget
23 0.19% 93.82% 1 300511 java.awt.image.ComponentColorModel.getRed
24 0.19% 94.02% 1 300512 sun.java2d.loops.OpaqueCopyAnyToArgb.Blit
25 0.19% 94.21% 1 300412 sun.awt.windows.WInputMethod.setConversionStatus
26 0.19% 94.40% 1 300411 sun.awt.Win32GraphicsDevice.configDisplayMode
27 0.19% 94.59% 1 300410 sun.awt.windows.WInputMethod.getOpenStatus
28 0.19% 94.79% 1 300409 sun.awt.Win32GraphicsDevice.enterFullScreenExclusive
29 0.19% 94.98% 1 300396 sun.awt.windows.WWindowPeer.setMinSize

I noticed the same on Java 5. Try to use Toolkit.getDefaultToolkit().createImage(path) instead of ImageIO.read(path). Not sure why, but images are accelerated using Toolkit but not ImageIO.

Take a look at these options and give a couple a try:
http://java.sun.com/javase/6/docs/technotes/guides/2d/flags.html#translaccel

Particularly look at translaccel=true and d3d=True.

Unfortunately the docs don’t say what’s happening with java 6, they mostly refer to java 5.

By the way, java 6 update N will drastically change what the best practice is in terms of options (hopefully we won’t need to specifiy any). It might be a good move to download the latest java 6 update N jdk and give it a try because direct3d (the d3d option) is on by default and that pipeline is much improved.

good luck!
Keith

This may be caused by this bug:
615272: D3D: Some images loaded via Toolkit or ImageIcon can’t be accelerated
It was fixed in 6u10 and I believe jdk7.

Could you please try 6u10 (http://jdk6.dev.java.net).

Dmitri

Hi again.
Thanks for you help.
To kingaschi: That method is only for Image and not BufferedImage, but it helps
making my code less in case I only need Image.
To CommanderKeith: I tried this but unfortunately it didn´t help either (same with the FPS).
At least to trembovetski: Thx for the link, but as some friends want to try playing my game, too
I guess I wait for jdk7, change it then and leave it my way until.
Hope jdk7 solves this problem.

But really thanks a lot for your help, this forum is amazing.
I´m surprised I couldn´t find it earlier, that would have saved me much trouble :wink:

Note that 6u10 will be available before jdk7.

Also, you friends can also install 6u10 and you can have
a wild testing party! =)

But in the meantime your workaround of copying image loaded
with ImageIO into another image will work.

Dmitri