Skip to content

Commit 0eb6ce4

Browse files
authored
Don't allow non-previewable values to be previewed (#821)
Fixes #768
1 parent 65ab349 commit 0eb6ce4

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

ui/src/main/java/edu/wpi/grip/ui/pipeline/OutputSocketController.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package edu.wpi.grip.ui.pipeline;
22

33
import edu.wpi.grip.core.events.BenchmarkEvent;
4+
import edu.wpi.grip.core.events.SocketChangedEvent;
45
import edu.wpi.grip.core.events.SocketPreviewChangedEvent;
56
import edu.wpi.grip.core.sockets.OutputSocket;
67
import edu.wpi.grip.core.sockets.Socket;
78
import edu.wpi.grip.core.sockets.SocketHint;
89
import edu.wpi.grip.ui.Controller;
910
import edu.wpi.grip.ui.annotations.ParametrizedController;
11+
import edu.wpi.grip.ui.preview.ImageBasedPreviewView;
1012

1113
import com.google.common.annotations.VisibleForTesting;
1214
import com.google.common.eventbus.Subscribe;
@@ -23,6 +25,7 @@
2325
import javafx.scene.layout.StackPane;
2426

2527
import static com.google.common.base.Preconditions.checkNotNull;
28+
import static org.bytedeco.javacpp.opencv_core.Mat;
2629

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

3538
private final SocketHandleView.Factory socketHandleFactory;
3639
private final InvalidationListener previewListener;
37-
private final OutputSocket socket;
40+
private final OutputSocket<?> socket;
3841
@FXML
3942
private HBox root;
4043
@FXML
@@ -79,14 +82,43 @@ protected void initialize() {
7982
this.type.setText(this.socket.getSocketHint().getTypeLabel());
8083
}
8184

82-
public Socket getSocket() {
85+
public Socket<?> getSocket() {
8386
return this.socket;
8487
}
8588

8689
public SocketHandleView getHandle() {
8790
return this.handle;
8891
}
8992

93+
@Subscribe
94+
public void onSocketChanged(SocketChangedEvent event) {
95+
if (event.isRegarding(this.socket)) {
96+
if (!this.socket.getValue().isPresent()) {
97+
// No value
98+
handlePreview(false);
99+
} else if (!(this.socket.getValue().get() instanceof Mat)) {
100+
// There is a non-image value, which can always be previewed
101+
handlePreview(true);
102+
} else {
103+
// Only allow the image to be previewed if it's previewable
104+
boolean previewable = this.socket.getValue()
105+
.map(Mat.class::cast)
106+
.map(ImageBasedPreviewView::isPreviewable)
107+
.get();
108+
handlePreview(previewable);
109+
}
110+
}
111+
}
112+
113+
/**
114+
* Disables the preview button and hides the preview if the socket value isn't able to be
115+
* previewed.
116+
*/
117+
private void handlePreview(boolean previewable) {
118+
this.preview.setDisable(!previewable);
119+
this.socket.setPreviewed(this.socket.isPreviewed() && previewable);
120+
}
121+
90122
@Subscribe
91123
public void onSocketPreviewChangedEvent(SocketPreviewChangedEvent event) {
92124
// Only try to update the button if the two aren't the same

ui/src/main/java/edu/wpi/grip/ui/preview/ContoursSocketPreviewView.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import edu.wpi.grip.core.operations.composite.ContoursReport;
44
import edu.wpi.grip.core.sockets.OutputSocket;
55
import edu.wpi.grip.ui.util.GripPlatform;
6-
import edu.wpi.grip.ui.util.ImageConverter;
76

87
import javafx.scene.control.CheckBox;
98
import javafx.scene.control.Label;
109
import javafx.scene.image.Image;
11-
import javafx.scene.image.ImageView;
1210
import javafx.scene.layout.VBox;
1311

1412
import static org.bytedeco.javacpp.opencv_core.CV_8UC3;
@@ -32,8 +30,6 @@ public final class ContoursSocketPreviewView extends ImageBasedPreviewView<Conto
3230
Scalar.BLUE,
3331
Scalar.MAGENTA,
3432
};
35-
private final ImageConverter imageConverter = new ImageConverter();
36-
private final ImageView imageView = new ImageView();
3733
private final Label infoLabel = new Label();
3834
private final CheckBox colorContours;
3935
private final Mat tmp = new Mat();

ui/src/main/java/edu/wpi/grip/ui/preview/ImageBasedPreviewView.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
import javafx.application.Platform;
1010
import javafx.scene.image.ImageView;
1111

12+
import static org.bytedeco.javacpp.opencv_core.CV_8S;
13+
import static org.bytedeco.javacpp.opencv_core.CV_8U;
14+
import static org.bytedeco.javacpp.opencv_core.Mat;
15+
1216
/**
1317
* Base class for image previews.
1418
*/
@@ -47,6 +51,18 @@ protected final int getImageHeight() {
4751
*/
4852
protected abstract void convertImage();
4953

54+
/**
55+
* Checks if an image is able to be previewed.
56+
*
57+
* @param image the image to check
58+
*
59+
* @return true if the image can be previewed, false if it can't
60+
*/
61+
public static boolean isPreviewable(Mat image) {
62+
return (image.channels() == 1) || (image.channels() == 3)
63+
&& (image.depth() == CV_8U || image.depth() == CV_8S);
64+
}
65+
5066
/**
5167
* Updates the image preview when the pipeline runs.
5268
*/

ui/src/main/java/edu/wpi/grip/ui/preview/ImageSocketPreviewView.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ public class ImageSocketPreviewView extends ImageBasedPreviewView<Mat> {
2626
@Override
2727
protected void convertImage() {
2828
synchronized (this) {
29-
this.getSocket().getValue().ifPresent(mat -> {
30-
platform.runAsSoonAsPossible(() -> {
31-
Image image = imageConverter.convert(mat, getImageHeight());
32-
imageView.setImage(image);
33-
});
34-
});
29+
this.getSocket().getValue()
30+
.filter(ImageBasedPreviewView::isPreviewable)
31+
.ifPresent(mat -> {
32+
platform.runAsSoonAsPossible(() -> {
33+
Image image = imageConverter.convert(mat, getImageHeight());
34+
imageView.setImage(image);
35+
});
36+
});
3537
}
3638
}
3739
}

0 commit comments

Comments
 (0)