Work in the BufferedImage array int []

When working with BufferedImage using the setRGB and getRGB methods, I noticed two things:

  • The setRGB and getRGB methods can be incredibly slow on some systems (two orders of magnitude slower than the modifiyng int [] array).

  • there is no guarantee that getRGB after setRGB will return the same pixel that you passed

This last paragraph is basically pretty clear from the JavaDoc setRGB, which states:

... For images with IndexColorModel, the index with the closest color is selected.

It can be seen that I can work directly in BufferedImage int [] pixels, which I can access, for example:

int[] a = ((DataBufferInt) tmp.getRaster().getDataBuffer()).getData(); 

I was wondering: are there any known / gotchas flaws when directly manipulating pixels in int[] ?

+6
source share
2 answers

The whole purpose of getData (), giving you access to the backing int array, is designed specifically for this optimization, so the advantages are likely to outweigh the disadvantages.

The disadvantages depend on how you use the buffer image. If you draw it on the screen during editing, you may encounter some artifacts on the screen (for example, pixels that are not colored over time), in which case you should consider double buffering (which includes copying the entire image for each update )

+4
source

Not sure if this is relevant for your question, but you ran into problems when the BufferedImage was created using the getSubimage(int x, int y, int w, int h) method getSubimage(int x, int y, int w, int h) .

Returns the subimage defined by the specified rectangular region. return BufferedImage uses the same data array as the original image.

The getTileGridXOffset() and getTileGridYOffset() methods return the offset, but despite the fact that they are described as

Returns the x offset of the tile grid relative to the origin, for example, the x coordinate of the tile location (0, 0). It is always zero.

but since you cannot (as far as I know) access the scanlineStride field of the raster, you cannot get the correct index for the array.

0
source

All Articles