Skip to main content

getting accelerated images from PNG file

3 replies [Last post]
anthonywarp
Offline
Joined: 2007-01-13
Points: 0

Hi!
I am developing a game. So I need fast drawing of images. I draw images to BufferStrategy. I have to use JDK5 and Windows XP!
If I read an image from a jpg file, drawing it takes about 0 milliseconds. I assume it is hardware accelerated. If I load the Image from a png file it takes about 10ms to draw it. I assume it is not hardware accelerated. So how do I get Images from png accelerated? I have read that translucent images cannot be accelerated. So at least I want to have transparent Images accelerated.

I have tried the following to create transparent Images:

Image image = ...
BufferedImage buffimg= gc.createCompatibleImage(100, 100,Transparency.BITMASK);
Graphics2D g = img.createGraphics();
g.setComposite(AlphaComposite.Src);
g.setColor(new Color(0, 0, 0, 0));
g.fillRect(0, 0, 100, 100);
g.drawImage(imgage,0,0,null);

But when I draw Buffimg, it still takes to much time so I assume that acceleration doesn't work. How do I get it right?

Thank you!

Regards

Message was edited by: anthonywarp

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Dmitri Trembovetski

Hi Anthony,

what OS is this?

java2d@javadesktop.org wrote:
> Hi!
> I am developing a game. So I need fast drawing of images. I draw images to BufferStrategy. I have to use JDK5!
> If I read an image from a jpg file, drawing it takes about 0 milliseconds. I assume it is hardware accelerated. If I load the Image from a png file it takes about 10ms to draw it. I assume it is not hardware accelerated. So how do I get Images from png accelerated? I have read that translucent images cannot be accelerated. So at least I want to have transparent Images accelerated.
>
> I have tried the following to create transparent Images:
>
> Image image = ...
> BufferedImage buffimg= gc.createCompatibleImage(100, 100,Transparency.BITMASK);
> Graphics2D g = img.createGraphics();
> g.setComposite(AlphaComposite.Src);
> g.setColor(new Color(0, 0, 0, 0));
> g.fillRect(0, 0, 100, 100);

Just FYI this fillRect is redundant - will be overwritten by
the next drawImage call.

> g.drawImage(imgage,0,0,null);
>
> But when I draw Buffimg, it still takes to much time so I assume that acceleration doesn't work. How do I get it right?
>

You seem to be doing the right thing here. You don't create
a buffimg on every frame I presume, or render to it on every frame
before copying to the back-buffer?

Another question: how large are your bitmask images? We have
a certain limitation on the size of bitmask images we could
accelerate.

If you don't copy your image to the compatible image, does
it get accelerated?

Use -Dsun.java2d.trace=log to see what loops are being used.

Also, a plug for the troubleshooting guide (the Java2D section):

http://java.sun.com/javase/6/webnotes/trouble/TSG-Desktop/html/java2d.html

Thanks,
Dmitri
Java2D Team

> Thank you!
>
> Regards
> [Message sent by forum member 'anthonywarp' (anthonywarp)]
>
> http://forums.java.net/jive/thread.jspa?messageID=194213
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

anthonywarp
Offline
Joined: 2007-01-13
Points: 0

> Hi Anthony,
>
> what OS is this?

I am using Windows XP

>
> java2d@javadesktop.org wrote:
> > Hi!
> > I am developing a game. So I need fast drawing of
> images. I draw images to BufferStrategy. I have to
> use JDK5!
> > If I read an image from a jpg file, drawing it
> takes about 0 milliseconds. I assume it is hardware
> accelerated. If I load the Image from a png file it
> takes about 10ms to draw it. I assume it is not
> hardware accelerated. So how do I get Images from png
> accelerated? I have read that translucent images
> cannot be accelerated. So at least I want to have
> transparent Images accelerated.
> >
> > I have tried the following to create transparent
> Images:
> >
> > Image image = ...
> > BufferedImage buffimg=
> gc.createCompatibleImage(100,
> 100,Transparency.BITMASK);
> > Graphics2D g = img.createGraphics();
> > g.setComposite(AlphaComposite.Src);
> > g.setColor(new Color(0, 0, 0, 0));
> > g.fillRect(0, 0, 100, 100);
>
> Just FYI this fillRect is redundant - will be
> be overwritten by
> the next drawImage call.
>
> > g.drawImage(imgage,0,0,null);
> >
> > But when I draw Buffimg, it still takes to much
> time so I assume that acceleration doesn't work. How
> do I get it right?
> >
>
> You seem to be doing the right thing here. You
> You don't create
> a buffimg on every frame I presume, or render to
> to it on every frame
> before copying to the back-buffer?

My Images are loaded once at startup. I don't do any manipulation. I just draw them all one by one to the BufferStrategy's Graphics2D on every frame.

>
> Another question: how large are your bitmask
> ask images? We have
> a certain limitation on the size of bitmask images
> ges we could
> accelerate.

One of my images has 3000x2000 pixels, the others usually have about 100x100 pixels.

>
> If you don't copy your image to the compatible
> ble image, does
> it get accelerated?

no, it doesn't. it is even drawn slower.

>
> Use -Dsun.java2d.trace=log to see what loops are
> are being used.

This is the console output when drawing the large image to the Buffer:

sun.java2d.loops.Blit::Blit(IntArgbBm, SrcOverNoEa, IntRgb)

Another question has come up:
Why are Buffered images drawn much slower than Images loaded via ImageIcon(fileName).getImage()

The framerate of my game drops extremely when loading images via:

BufferedImage in = javax.imageio.ImageIO.read(new java.io.File(fileName));

Dmitri Trembovetski

Hi Anthony,

java2d@javadesktop.org wrote:
>> You seem to be doing the right thing here. You
>> You don't create
>> a buffimg on every frame I presume, or render to
>> to it on every frame
>> before copying to the back-buffer?
>
> My Images are loaded once at startup. I don't do any manipulation. I just draw them all one by one to the BufferStrategy's Graphics2D on every frame.
>

Hm. That should make them accelerated (given enough
etc). What does GraphicsDevice.getAvailableAcceleratedMemory()
return?

>> Another question: how large are your bitmask
>> ask images? We have
>> a certain limitation on the size of bitmask images
>> ges we could
>> accelerate.
>
> One of my images has 3000x2000 pixels, the others usually have about 100x100 pixels.

The 3000x2000 one won't be accelerated, it's too large,
the 100x100 certainly should be.

>
>
>> If you don't copy your image to the compatible
>> ble image, does
>> it get accelerated?
>
> no, it doesn't. it is even drawn slower.

Now that would be very strange - there should be no
difference at all.

>
>> Use -Dsun.java2d.trace=log to see what loops are
>> are being used.
>
> This is the console output when drawing the large image to the Buffer:
>
> sun.java2d.loops.Blit::Blit(IntArgbBm, SrcOverNoEa, IntRgb)
>

OK, that's expected - it's too large to be accelerated.
What about the 100x100 ones?

I have a suspicion that your large image may be affecting
the frame rate. How does it look if you don't copy it?

> Another question has come up:
> Why are Buffered images drawn much slower than Images loaded via ImageIcon(fileName).getImage()
>
> The framerate of my game drops extremely when loading images via:
>
> BufferedImage in = javax.imageio.ImageIO.read(new java.io.File(fileName));

There was a bug in 1.5 which prevented some of the images loaded via
ImageIO from being accelerated. I don't have the bug id handly, but I
believe it's been fixed in jdk6.

Other than this bug, there should be no difference in acceleration
capabilities of images loaded via toolkit mechanism and ImageIO.

Thank you,
Dmitri

> [Message sent by forum member 'anthonywarp' (anthonywarp)]
>
> http://forums.java.net/jive/thread.jspa?messageID=194331
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".