gpt4 book ai didi

java - 缓冲图像 INT/4BYTE/USHORT

转载 作者:行者123 更新时间:2023-11-30 09:49:06 27 4
gpt4 key购买 nike

我知道 byte、unsigned short 和 integer 在内存使用上的区别,但是当涉及到 BufferedImage 时,它​​们之间是否存在“速度”差异?

我一直在我的代码中使用 Image 类型来存储图像,但我需要一个 alpha 层。使用 BufferedImage 为我提供了 ARGB,但是在从 Image 类型进行更改后我的代码/显着/慢了(并且它只针对几个对象进行了更改),所以我正在寻找我可以获得的所有性能改进。< br/>
我不确定这是一个多么愚蠢的问题,所以我感谢你的任何回复。

最佳答案

田木,

我发现,当需要在 BufferedImage 中使用 alpha channel 时,最好的方法是预乘 alpha channel 。例如:

// Create an ARGB imageBufferedImage 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 compositeComposite original = g.getComposite();g.setComposite(alpha);// Paint with transparencyRectangle 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 CanvasBufferedImage 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();

首先绘制到 TYPE_INT_ARGB 的原因是为了确保所有 alpha 都按照您的预期绘制(不是每次都预乘!)。然后,完成后,将整个图像绘制到 TYPE_INT_ARGB_PRE 上,然后 TYPE_INT_ARGB_PRE 能够以良好的速度将数据显示到屏幕上。

关于java - 缓冲图像 INT/4BYTE/USHORT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5986343/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com