Improve javaFx processing performance -
i'm working on image processing javafx. think code not enouth efficient (with hd images, refresh slow). because for
on each pixel of image everytime have refresh it. don't know how differently.
so need improve performance of processing.
this code :
import javafx.application.application; import javafx.beans.invalidationlistener; import javafx.beans.observable; import javafx.beans.property.doubleproperty; import javafx.scene.scene; import javafx.scene.control.scrollpane; import javafx.scene.control.slider; import javafx.scene.image.image; import javafx.scene.image.imageview; import javafx.scene.image.pixelreader; import javafx.scene.image.pixelwriter; import javafx.scene.image.writableimage; import javafx.scene.layout.anchorpane; import javafx.scene.paint.color; import javafx.stage.stage; public class example extends application { private image src; private writableimage dest; private int width; private int height; int value = 0; @override public void start(stage stage) { anchorpane root = new anchorpane(); initimage(root); scene scene = new scene(root); stage.settitle("demo processing"); stage.setresizable(false); stage.setscene(scene); stage.show(); } private void initimage(anchorpane root) { src = new image( "http://mikecann.co.uk/wp-content/uploads/2009/12/screenhunter_02-dec.-10-19.41-1024x484.jpg"); width = (int) src.getwidth(); height = (int) src.getheight(); root.setprefsize(800, 800 + 50); scrollpane scrollpane = new scrollpane(); scrollpane.setprefheight(600); scrollpane.setprefwidth(1000); dest = new writableimage(width, height); imageview destview = new imageview(dest); scrollpane.setcontent(destview); root.getchildren().add(scrollpane); anchorpane.settopanchor(scrollpane, 0.0); slider slider = new slider(0, 255, 1); slider.setprefsize(800, 50); slider.setshowticklabels(true); slider.setshowtickmarks(true); slider.setsnaptoticks(true); slider.setmajortickunit(1.0); slider.setminortickcount(0); slider.setlayouty(700); slider.valueproperty().addlistener(new invalidationlistener() { @override public void invalidated(observable o) { value = (int) ((doubleproperty) o).get(); color(); } }); root.getchildren().add(slider); color(); } private void color() { pixelreader reader = src.getpixelreader(); pixelwriter writer = dest.getpixelwriter(); (int x = 0; x < width; x++) { (int y = 0; y < height; y++) { color color = reader.getcolor(x, y); double red = (double) value * x * y / (width * height) / 255; double green = color.getgreen(); double blue = (double) value * ((width * height) - x * y) / (width * height) / 255; writer.setcolor(x, y, color.color(red, green, blue)); } } } public static void main(string[] args) { launch(args); } }
and full hd image :
src = new image( "http://www.freedomwallpaper.com//nature-wallpaper-hd/hd_sunshine_hd.jpg");
getitng color of each pixel in loop slow. so, entire pixels first, , change colors, wirte changed colors pixelwriter. this
private void color() { pixelreader reader = src.getpixelreader(); writablepixelformat<intbuffer> format = writablepixelformat.getintargbinstance(); int[] pixels = new int[width * height]; // buffer pixels reader.getpixels(0, 0, width, height, format, pixels, 0, width); // pixels argb format int alpha = 0xff << 24; (int x = 0; x < width; x++) { (int y = 0; y < height; y++) { int index = x + y * width; int argb = pixels[index]; int red = value * x * y / (width * height); int green = (argb >> 8) & 0xff; int blue = value * ((width * height) - x * y) / (width * height); int newargb = alpha | (red << 16) | (green << 8) | blue; pixels[index] = newargb; } } pixelwriter writer = dest.getpixelwriter(); writer.setpixels(0, 0, width, height, format, pixels, 0, width); // write entire image }
Comments
Post a Comment