JavaFX: Fastest way to write pixels to PixelWriter

空扰寡人 提交于 2020-01-02 05:15:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/33911010/javafx-fastest-way-to-write-pixels-to-pixelwriter

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