|
| 1 | +package edu.wpi.grip.core.operations.composite; |
| 2 | + |
| 3 | +import com.google.common.eventbus.EventBus; |
| 4 | +import edu.wpi.grip.core.InputSocket; |
| 5 | +import edu.wpi.grip.core.Operation; |
| 6 | +import edu.wpi.grip.core.OutputSocket; |
| 7 | +import edu.wpi.grip.core.SocketHints; |
| 8 | + |
| 9 | +import java.io.InputStream; |
| 10 | +import java.util.Optional; |
| 11 | + |
| 12 | +import static org.bytedeco.javacpp.opencv_core.Mat; |
| 13 | +import static org.bytedeco.javacpp.opencv_core.Size; |
| 14 | +import static org.bytedeco.javacpp.opencv_imgproc.*; |
| 15 | +import static org.bytedeco.javacpp.opencv_imgproc.resize; |
| 16 | + |
| 17 | +/** |
| 18 | + * Scale an image to an exact width and height using one of several interpolation modes. Scaling images down can |
| 19 | + * be a useful optimization, and scaling them up might be necessary for combining multiple images that are different |
| 20 | + * sizes. |
| 21 | + */ |
| 22 | +public class ResizeOperation implements Operation { |
| 23 | + |
| 24 | + private enum Interpolation { |
| 25 | + NEAREST("None", INTER_NEAREST), |
| 26 | + LINEAR("Linear", INTER_LINEAR), |
| 27 | + CUBIC("Cubic", INTER_CUBIC), |
| 28 | + LANCZOS("Lanczos", INTER_LANCZOS4), |
| 29 | + AREA("Area", INTER_AREA); |
| 30 | + |
| 31 | + final String label; |
| 32 | + final int value; |
| 33 | + |
| 34 | + Interpolation(String label, int value) { |
| 35 | + this.label = label; |
| 36 | + this.value = value; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public String toString() { |
| 41 | + return label; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public String getName() { |
| 47 | + return "Resize Image"; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String getDescription() { |
| 52 | + return "Scale an image to an exact size"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Optional<InputStream> getIcon() { |
| 57 | + return Optional.of(getClass().getResourceAsStream("/edu/wpi/grip/ui/icons/resize.png")); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public InputSocket<?>[] createInputSockets(EventBus eventBus) { |
| 62 | + return new InputSocket<?>[]{ |
| 63 | + new InputSocket<>(eventBus, SocketHints.Inputs.createMatSocketHint("Input", false)), |
| 64 | + new InputSocket<>(eventBus, SocketHints.Inputs.createNumberSpinnerSocketHint("Width", 640)), |
| 65 | + new InputSocket<>(eventBus, SocketHints.Inputs.createNumberSpinnerSocketHint("Height", 480)), |
| 66 | + new InputSocket<>(eventBus, SocketHints.createEnumSocketHint("Interpolation", Interpolation.CUBIC)), |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public OutputSocket<?>[] createOutputSockets(EventBus eventBus) { |
| 72 | + return new OutputSocket<?>[]{ |
| 73 | + new OutputSocket<>(eventBus, SocketHints.Outputs.createMatSocketHint("Output")), |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) { |
| 80 | + final Mat input = ((InputSocket<Mat>) inputs[0]).getValue().get(); |
| 81 | + final Number width = ((InputSocket<Number>) inputs[1]).getValue().get(); |
| 82 | + final Number height = ((InputSocket<Number>) inputs[2]).getValue().get(); |
| 83 | + final Interpolation interpolation = ((InputSocket<Interpolation>) inputs[3]).getValue().get(); |
| 84 | + |
| 85 | + final OutputSocket<Mat> outputSocket = (OutputSocket<Mat>) outputs[0]; |
| 86 | + final Mat output = outputSocket.getValue().get(); |
| 87 | + |
| 88 | + resize(input, output, new Size(width.intValue(), height.intValue()), 0.0, 0.0, interpolation.value); |
| 89 | + |
| 90 | + outputSocket.setValue(output); |
| 91 | + } |
| 92 | +} |
0 commit comments