Manipulating the pixels within a BufferedImage through an Array

一曲冷凌霜 提交于 2020-01-16 13:03:19

问题


I'm currently following a series on Java game development from scratch. I understand most java and oop concepts but have very little experience when dealing with graphics and hardware acceleration.

The lines of code that I am questioning are:

 private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
 private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

The BufferedImage "image" variable is always the variable being drawn to the screen through a render method. Usually something like:

 public void render() {
    BufferStrategy bs = this.getBufferStrategy;
    if(bs == null) { this.createBufferStrategy(3); return; }
    Graphics g = bs.getDrawGraphics();
    g.drawImage(image, 0, 0, WIDTH, HEIGHT, null);
    g.dispose();
    bs.show();
 }

I understand the array of pixels contains every pixel within the BufferedImage, however, it seems as though everytime that array is filled with values it directly effects the contents of the "image" variable. There is never a method used to copy the pixels array values into the image.

Are these varibales actually linked in such a way? Does the use of:

private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

create an automated link between the image and the array being created in the code above? Maybe I am going cray and just missed something but I have reviewed the code several times and not once is the "image" varibale manipulated after it's initial creation. (besides being rendered to the screen of course.) It's always the array "pixels" just being filled with different values that causes the change in the rendered image.

Some insight on this would be wonderful. Thank you in advance!


回答1:


Why don't you call

image.getData()

instead of

private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

image.getData() returns a copy of the Raster. getRaster() returns a WriteableRaster with the ability to modify pixels. I'm guessing but getRaster() probably returns a child of the image Raster and therefore is writeable if you modify the array. Try image.getData() to see if it works. If not, post back here and I'll take a closer look.

I looked into this further. The source code that comes with the JDK shows that image.getRaster().getDataBuffer().getData() returns the source data array. image.getData() indeed returns a copy. If the image is modified, the data in getData() will not be modified.

You can call getPixels on the returned Raster:

public int[] getPixels(int x,
              int y,
              int w,
              int h,
              int[] iArray)

Returns an int array containing all samples for a rectangle of pixels, one sample per array element. An ArrayIndexOutOfBoundsException may be thrown if the coordinates are not in bounds. However, explicit bounds checking is not guaranteed.




回答2:


Use int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); instead of image.getData(). image.getData() just returns a copy of the image data where image.getRaster() returns the original pixel array allowing to actually write pixels to it.



来源:https://stackoverflow.com/questions/20292418/manipulating-the-pixels-within-a-bufferedimage-through-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!