JavaFX PixelWriter 低性能

JavaFX PixelWriter low performance

我创建了一个简单的 JavaFX 图像编辑器。有两个 ImageView 实例(打开用于原始图像,一个用于编辑后的图像)。 一种方法将 WritableImage 转换为灰度。 问题是,转换花费的时间大约是 Swing 完成相同任务所需时间的两倍。知道为什么吗?

@FXML
void loadImage(ActionEvent event) {
    FileChooser fileChooser = new FileChooser();
    File file = fileChooser.showOpenDialog(Main.primaryStage);
    if (file != null) {
        Image image = new Image("file:" + file.getAbsolutePath());
        imViewOrig.setImage(image);
        PixelReader pr = imViewOrig.getImage().getPixelReader();
        WritableImage wi = new WritableImage(
            pr, 
            (int)imViewOrig.getImage().getWidth(), 
            (int)imViewOrig.getImage().getHeight());
        imViewEdit.setImage(wi);
    }
}

@FXML
void greyscale(ActionEvent event) {
    PixelReader pr = imViewOrig.getImage().getPixelReader();
    PixelWriter pw = ((WritableImage)imViewEdit.getImage()).getPixelWriter();
    long beginTime = System.currentTimeMillis();
    for (int x = 0; x < imViewEdit.getImage().getWidth(); x++) {
        for (int y = 0; y < imViewEdit.getImage().getHeight(); y++) {
            Color c = pr.getColor(x, y);
            double avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3.;
            Color grey = new Color(avg, avg, avg, c.getOpacity());
            pw.setColor(x, y, grey);                
        }
    }
    System.out.println(System.currentTimeMillis() - beginTime + "ms");
}

转码:

public void greyscale() {
    long beginTime = System.currentTimeMillis();
    for (int x = 0; x < image.getWidth(); x++) {
        for (int y = 0; y < image.getHeight(); y++) {
            Color c = new Color(image.getRGB(x, y));
            int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
            Color grey = new Color(avg, avg, avg);
            image.setRGB(x, y, grey.getRGB());                
        }
    }
    repaint();
    System.out.println(System.currentTimeMillis() - beginTime + "ms");
}

在快速测试中,如果您只进行 int 计算而不是操作 Color 对象,它的运行速度会提高 10 倍。 JavaFX Color class 是一个比 AWT Color class 更丰富的实现;我想这是以额外开销为代价的。

    int width = (int)image.getWidth();
    int height = (int)image.getHeight();

    int[] pixels = new int[width * height];
    WritablePixelFormat<IntBuffer> pixelFormat = PixelFormat.getIntArgbPreInstance();
    pr.getPixels(0, 0, width, height, pixelFormat, pixels, 0, width);

    int[] newPixels = new int[pixels.length];
    for (int i = 0 ; i < pixels.length; i++) {
        int c = pixels[i];
        int a = c & 0xFF000000 ;
        int r = (c >> 16) & 0xFF ;
        int g = (c >> 8) & 0xFF ;
        int b = c & 0xFF ;
        int gray = (r+g+b) / 3 ;
        newPixels[i] = a | (gray << 16) | (gray << 8) | gray ;
    }

    pw.setPixels(0, 0, width, height, pixelFormat, newPixels, 0, width);

ColorAdjust 基于样本,当您 mouse-over 图像时,通过去饱和将彩色图像转换为单色。

给定合适的硬件(大多数运行 JavaFX 的系统都可用),JavaFX 运行时可以在硬件中执行一些颜色调整的实现,这可能比使用 PixelWriter.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.*;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Shadow extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Image image = new Image(
                "http://icons.iconarchive.com/icons/designbolts/smurfs-movie/512/smurfette-icon.png"
        );

        ImageView imageView = new ImageView(image);

        ColorAdjust monochrome = new ColorAdjust();
        monochrome.setSaturation(-1.0);

        imageView.effectProperty().bind(
                Bindings
                    .when(imageView.hoverProperty())
                        .then(monochrome)
                        .otherwise((ColorAdjust) null)
        );

        stage.setScene(new Scene(new Group(imageView), Color.AQUA));
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch();
    }
}