Skip to content

Commit

Permalink
Don't allow non-previewable values to be previewed (#821)
Browse files Browse the repository at this point in the history
Fixes #768
  • Loading branch information
SamCarlberg authored Feb 13, 2017
1 parent 65ab349 commit 0eb6ce4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package edu.wpi.grip.ui.pipeline;

import edu.wpi.grip.core.events.BenchmarkEvent;
import edu.wpi.grip.core.events.SocketChangedEvent;
import edu.wpi.grip.core.events.SocketPreviewChangedEvent;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.core.sockets.Socket;
import edu.wpi.grip.core.sockets.SocketHint;
import edu.wpi.grip.ui.Controller;
import edu.wpi.grip.ui.annotations.ParametrizedController;
import edu.wpi.grip.ui.preview.ImageBasedPreviewView;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.eventbus.Subscribe;
Expand All @@ -23,6 +25,7 @@
import javafx.scene.layout.StackPane;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.bytedeco.javacpp.opencv_core.Mat;

/**
* A JavaFX control that renders an {@link OutputSocket} that is the output of a step. It shows a
Expand All @@ -34,7 +37,7 @@ public class OutputSocketController implements Controller {

private final SocketHandleView.Factory socketHandleFactory;
private final InvalidationListener previewListener;
private final OutputSocket socket;
private final OutputSocket<?> socket;
@FXML
private HBox root;
@FXML
Expand Down Expand Up @@ -79,14 +82,43 @@ protected void initialize() {
this.type.setText(this.socket.getSocketHint().getTypeLabel());
}

public Socket getSocket() {
public Socket<?> getSocket() {
return this.socket;
}

public SocketHandleView getHandle() {
return this.handle;
}

@Subscribe
public void onSocketChanged(SocketChangedEvent event) {
if (event.isRegarding(this.socket)) {
if (!this.socket.getValue().isPresent()) {
// No value
handlePreview(false);
} else if (!(this.socket.getValue().get() instanceof Mat)) {
// There is a non-image value, which can always be previewed
handlePreview(true);
} else {
// Only allow the image to be previewed if it's previewable
boolean previewable = this.socket.getValue()
.map(Mat.class::cast)
.map(ImageBasedPreviewView::isPreviewable)
.get();
handlePreview(previewable);
}
}
}

/**
* Disables the preview button and hides the preview if the socket value isn't able to be
* previewed.
*/
private void handlePreview(boolean previewable) {
this.preview.setDisable(!previewable);
this.socket.setPreviewed(this.socket.isPreviewed() && previewable);
}

@Subscribe
public void onSocketPreviewChangedEvent(SocketPreviewChangedEvent event) {
// Only try to update the button if the two aren't the same
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import edu.wpi.grip.core.operations.composite.ContoursReport;
import edu.wpi.grip.core.sockets.OutputSocket;
import edu.wpi.grip.ui.util.GripPlatform;
import edu.wpi.grip.ui.util.ImageConverter;

import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

import static org.bytedeco.javacpp.opencv_core.CV_8UC3;
Expand All @@ -32,8 +30,6 @@ public final class ContoursSocketPreviewView extends ImageBasedPreviewView<Conto
Scalar.BLUE,
Scalar.MAGENTA,
};
private final ImageConverter imageConverter = new ImageConverter();
private final ImageView imageView = new ImageView();
private final Label infoLabel = new Label();
private final CheckBox colorContours;
private final Mat tmp = new Mat();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import javafx.application.Platform;
import javafx.scene.image.ImageView;

import static org.bytedeco.javacpp.opencv_core.CV_8S;
import static org.bytedeco.javacpp.opencv_core.CV_8U;
import static org.bytedeco.javacpp.opencv_core.Mat;

/**
* Base class for image previews.
*/
Expand Down Expand Up @@ -47,6 +51,18 @@ protected final int getImageHeight() {
*/
protected abstract void convertImage();

/**
* Checks if an image is able to be previewed.
*
* @param image the image to check
*
* @return true if the image can be previewed, false if it can't
*/
public static boolean isPreviewable(Mat image) {
return (image.channels() == 1) || (image.channels() == 3)
&& (image.depth() == CV_8U || image.depth() == CV_8S);
}

/**
* Updates the image preview when the pipeline runs.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public class ImageSocketPreviewView extends ImageBasedPreviewView<Mat> {
@Override
protected void convertImage() {
synchronized (this) {
this.getSocket().getValue().ifPresent(mat -> {
platform.runAsSoonAsPossible(() -> {
Image image = imageConverter.convert(mat, getImageHeight());
imageView.setImage(image);
});
});
this.getSocket().getValue()
.filter(ImageBasedPreviewView::isPreviewable)
.ifPresent(mat -> {
platform.runAsSoonAsPossible(() -> {
Image image = imageConverter.convert(mat, getImageHeight());
imageView.setImage(image);
});
});
}
}
}

0 comments on commit 0eb6ce4

Please sign in to comment.