开发者

BufferedImage INT / 4BYTE / USHORT

开发者 https://www.devze.com 2023-03-05 07:57 出处:网络
I know the difference in memory usage between byte, unsigned short, and integer, but when it comes to a BufferedImage, is there a \'speed\' difference between them?

I know the difference in memory usage between byte, unsigned short, and integer, but when it comes to a BufferedImage, is there a 'speed' difference between them?

I have been using the Image type in my code to store images, but I require an alpha layer. Using BufferedImage provides me with ARGB, but my code is /considerably开发者_运维知识库/ slower after making the change from the Image type (and it was only changed for a few objects), so I'm looking for all the performance improvement I can get.

I'm not sure how stupid of a question this may be, so I thank you in response for any replies.


Tanaki,

I have found that, when in need of using an alpha channel in a BufferedImage, the best is to premultiply the alpha channel. For example:

// Create an ARGB image
BufferedImage bi = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
// Fill the background (for illustration)
g.setColor(Color.black);
g.fill(new Rectangle(0, 0, 512, 512));

AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f));
// Keep the original composite
Composite original = g.getComposite();
g.setComposite(alpha);

// Paint with transparency
Rectangle r = new Rectangle(100, 200, 50, 50);
g.setColor(Color.magenta);
g.fillRect(r);
g.setComposite(original);
// ... paint further shapes or images as necessary
// ...
g.dispose();

// Convert to a premultiplied alpha image for fast painting to a Canvas
BufferedImage biPre = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics2D gPre = biPre.createGraphics();
gPre.drawImage(bi, 0, 0, null);
gPre.dispose();

// clean up:
bi.flush();


// Now use biPre for painting to a Canvas, or a Component.
// ...

// Remember to flush it when done!
biPre.flush();

The reason for painting first to a TYPE_INT_ARGB is to ensure that all alpha gets painted as you expected (not pre-multiplied every time!). Then, when done, paint the whole image onto a TYPE_INT_ARGB_PRE, which is then able to bring the data to the screen with good speed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号