Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Fastest way to write pixels to PixelWriter

I'm looking for the fastest way to write pixels on javafx.scene.image.Image. Writing to BufferedImage's backing array is much faster. At least on the test image I made it took only ~20ms for BufferedImage, WritableImage on the other hand took ~100ms. I already tried SwingFXUtils but no luck.

Code for BufferedImage (faster):

BufferedImage bi = createCompatibleImage( width, height );
WritableRaster raster = bi.getRaster();
DataBufferInt dataBuffer = (DataBufferInt) raster.getDataBuffer();

System.arraycopy( pixels, 0, dataBuffer.getData(), 0, pixels.length );

Code for WritableImage (slower):

WritableImage wi = new WritableImage( width, height );
PixelWriter pw = wi.getPixelWriter();
WritablePixelFormat<IntBuffer> pf = WritablePixelFormat.getIntArgbInstance();

pw.setPixels( 0, 0, width, height, pf, pixels, 0, width );

Maybe there's a way to write to WritableImage's backing array too?

like image 463
Ezekiel Baniaga Avatar asked Nov 25 '15 07:11

Ezekiel Baniaga


Video Answer


1 Answers

For the performance of the pixel writer it is absolutely crucial that you pick the right pixel format. You can check what the native pixel format is via

pw.getPixelFormat().getType()

On my Mac this is PixelFormat.Type.BYTE_BGRA_PRE. If your raw data conforms to this pixel format, then the transfer to the image should be pretty fast. Otherwise the pixel data has to be converted and that takes some time.

like image 124
mipa Avatar answered Oct 23 '22 03:10

mipa