简体   繁体   English

检测BufferedImage是否包含透明像素

[英]Detecting if a BufferedImage contains transparent pixels

I'm trying to optimise a rendering engine in Java to not draw object's which are covered up by 'solid' child objects drawn in front of them, ie the parent is occluded by its children. 我正在尝试优化Java中的渲染引擎,以不绘制由在其前面绘制的“实体”子对象所遮盖的对象,即,父对象被其子对象遮挡。

I'm wanting to know if an arbitrary BufferedImage I load in from a file contains any transparent pixels - as this affects my occlusion testing. 我想知道我从文件加载的任意BufferedImage是否包含任何透明像素-因为这会影响我的遮挡测试。

I've found I can use BufferedImage.getColorModel().hasAlpha() to find if the image supports alpha, but in the case that it does, it doesn't tell me if it definitely contains non-opaque pixels. 我发现我可以使用BufferedImage.getColorModel()。hasAlpha()来查找图像是否支持Alpha,但是如果可以,它不会告诉我它是否绝对包含非透明像素。

I know I could loop over the pixel data & test each one's alpha value & return as soon as I discover a non-opaque pixel, but I was wondering if there's already something native I could use, a flag that is set internally perhaps? 我知道我可以遍历像素数据并测试每个像素的Alpha值并在发现不透明像素后立即返回,但是我想知道是否已经可以使用本机的某个东西,也许是在内部设置的标志? Or something a little less intensive than iterating through pixels. 或强度比遍历像素少一些的东西。

Any input appreciated, thanks. 任何输入表示赞赏,谢谢。

Unfortunately, you will have to loop through each pixel (until you find a transparent pixel) to be sure. 不幸的是,您必须遍历每个像素(直到找到透明像素)才能确定。

If you don't need to be 100% sure, you could of course test only some pixels, where you think transparency is most likely to occur. 如果不需要100%确定,则当然可以只测试一些像素,因为您认为这些像素最有可能发生透明度。

By looking at various images, I think you'll find that most images that has transparent parts contains transparency along the edges. 通过查看各种图像,我认为您会发现大多数具有透明部分的图像在边缘都包含透明性。 This optimization will help in many common cases. 这种优化将在许多常见情况下有所帮助。

Unfortunately, I don't think that there's an optimization that can be done in one of the most common cases, the one where the color model allows transparency, but there really are no transparent pixels... You really need to test every pixel in this case, to know for sure. 不幸的是,我不认为在一种最常见的情况下可以进行优化,即在颜色模型允许透明的情况下进行优化,但是实际上并没有透明像素……您确实需要测试其中的每个像素这种情况,要确定。

Accessing the alpha values in its "native representation" (through the Raster / DataBuffer / SampleModel classes) is going to be faster than using BufferedImage.getRGB(x, y) and mask out the alpha values. (通过Raster / DataBuffer / SampleModel类)访问其“本机表示形式”中的alpha值将比使用BufferedImage.getRGB(x, y)并屏蔽掉alpha值更快。

I'm pretty sure you'll need to loop through each pixel and check for an Alpha value. 我很确定您需要遍历每个像素并检查Alpha值。

The best alternative I can offer is to write a custom method for reading the pixel data - ie your own Raster . 我能提供的最佳替代方法是编写一个自定义方法来读取像素数据,即您自己的Raster Within this class, as you're reading the pixel data from the source file into the data buffer, you can check for the alpha values as you go. 在此类中,当您将像素数据从源文件读取到数据缓冲区时,可以随时检查Alpha值。 Of course, this isn't much help if you're using a built-in image reading class, and involves a lot more effort. 当然,如果您使用内置的图像阅读类,这并没有太大帮助,并且需要付出更多的努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM