简体   繁体   English

如何使用BitSet和BufferedImage快速设置像素?

[英]How would I use BitSet and a BufferedImage to quickly set pixels?

The Problem I'm trying to make a more efficient rendering system from scratch and I've come to the conclusion that using for loops are very inefficient for larger windows. 问题我试图从头开始创建一个更有效的渲染系统,并且得出的结论是,对于较大的窗口,使用for循环效率很低。

Question So my question is, can I use BitSet to manipulate the pixels much quicker than using a for loop to set each pixel individually? 问题所以我的问题是,与使用for循环分别设置每个像素相比,我可以使用BitSet更快地操纵像素吗?

Things You May Need The rendering system is very small, as it's more a test than anything. 您可能需要的东西渲染系统非常小,因为它比任何东西都要测试。 The only thing it currently does is draw rectangles to the screen so I can test the FPS I get. 它当前唯一要做的就是在屏幕上绘制矩形,以便我可以测试得到的FPS。

public void drawRect(int offX, int offY, int width, int height, int color)
{
    for (int y = 0; y < height; y++)
        for (int x = 0; x < width; x++)
            pixels[x + offX + this.width * (y + offY)] = color;
}

The pixel array is just the data from a BufferedImage using pixels = ((DataBufferInt) engine.getWindow().getImage().getRaster().getDataBuffer()).getData(); 像素数组只是使用pixels = ((DataBufferInt) engine.getWindow().getImage().getRaster().getDataBuffer()).getData();的BufferedImage中的数据pixels = ((DataBufferInt) engine.getWindow().getImage().getRaster().getDataBuffer()).getData();

I'm not sure how I would go about using the BitSet class with the pixels, but I'm hoping it's faster than the drawRect method. 我不确定如何将BitSet类与像素一起使用,但是我希望它比drawRect方法更快。 The FPS counter tells me that drawing a single rectangle to the screen of 1980x1080 is roughly 100-130 fps with no consistent clearing to it yet. FPS计数器告诉我,在1980x1080的屏幕上绘制单个矩形的速度约为100-130 fps,尚无一致的清除方法。

Any help is appreciated, please tell me if the way I have in mind is just plain stupid 感谢您的帮助,请告诉我我的想法是否只是愚蠢的

BitSet isn't really going to be useful for this purpose. BitSet并不会真正用于此目的。 BitSet is like a fast, compact boolean[] . BitSet就像一个快速,紧凑的boolean[] You might make some progress by replacing the inner loop with Arrays.fill(pixels, offX + this.width * (y + offY), offX + width + this.width * (y + offY)) . 您可以通过将内部循环替换为Arrays.fill(pixels, offX + this.width * (y + offY), offX + width + this.width * (y + offY))来取得一些进展。

All that said, if you're aiming for efficiency, you should be using the tools built into Java, which are going to be better than anything you might write. 综上所述,如果您追求效率,则应该使用Java内置的工具,这些工具将比您编写的任何工具都要好。

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

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