-
Notifications
You must be signed in to change notification settings - Fork 109
Add a resize operation #372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
core/src/main/java/edu/wpi/grip/core/operations/composite/ResizeOperation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package edu.wpi.grip.core.operations.composite; | ||
|
||
import com.google.common.eventbus.EventBus; | ||
import edu.wpi.grip.core.InputSocket; | ||
import edu.wpi.grip.core.Operation; | ||
import edu.wpi.grip.core.OutputSocket; | ||
import edu.wpi.grip.core.SocketHints; | ||
|
||
import java.io.InputStream; | ||
import java.util.Optional; | ||
|
||
import static org.bytedeco.javacpp.opencv_core.Mat; | ||
import static org.bytedeco.javacpp.opencv_core.Size; | ||
import static org.bytedeco.javacpp.opencv_imgproc.*; | ||
import static org.bytedeco.javacpp.opencv_imgproc.resize; | ||
|
||
/** | ||
* Scale an image to an exact width and height using one of several interpolation modes. Scaling images down can | ||
* be a useful optimization, and scaling them up might be necessary for combining multiple images that are different | ||
* sizes. | ||
*/ | ||
public class ResizeOperation implements Operation { | ||
|
||
private enum Interpolation { | ||
NEAREST("None", INTER_NEAREST), | ||
LINEAR("Linear", INTER_LINEAR), | ||
CUBIC("Cubic", INTER_CUBIC), | ||
LANCZOS("Lanczos", INTER_LANCZOS4), | ||
AREA("Area", INTER_AREA); | ||
|
||
final String label; | ||
final int value; | ||
|
||
Interpolation(String label, int value) { | ||
this.label = label; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return label; | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "Resize Image"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Scale an image to an exact size"; | ||
} | ||
|
||
@Override | ||
public Optional<InputStream> getIcon() { | ||
return Optional.of(getClass().getResourceAsStream("/edu/wpi/grip/ui/icons/resize.png")); | ||
} | ||
|
||
@Override | ||
public InputSocket<?>[] createInputSockets(EventBus eventBus) { | ||
return new InputSocket<?>[]{ | ||
new InputSocket<>(eventBus, SocketHints.Inputs.createMatSocketHint("Input", false)), | ||
new InputSocket<>(eventBus, SocketHints.Inputs.createNumberSpinnerSocketHint("Width", 640)), | ||
new InputSocket<>(eventBus, SocketHints.Inputs.createNumberSpinnerSocketHint("Height", 480)), | ||
new InputSocket<>(eventBus, SocketHints.createEnumSocketHint("Interpolation", Interpolation.CUBIC)), | ||
}; | ||
} | ||
|
||
@Override | ||
public OutputSocket<?>[] createOutputSockets(EventBus eventBus) { | ||
return new OutputSocket<?>[]{ | ||
new OutputSocket<>(eventBus, SocketHints.Outputs.createMatSocketHint("Output")), | ||
}; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs) { | ||
final Mat input = ((InputSocket<Mat>) inputs[0]).getValue().get(); | ||
final Number width = ((InputSocket<Number>) inputs[1]).getValue().get(); | ||
final Number height = ((InputSocket<Number>) inputs[2]).getValue().get(); | ||
final Interpolation interpolation = ((InputSocket<Interpolation>) inputs[3]).getValue().get(); | ||
|
||
final OutputSocket<Mat> outputSocket = (OutputSocket<Mat>) outputs[0]; | ||
final Mat output = outputSocket.getValue().get(); | ||
|
||
resize(input, output, new Size(width.intValue(), height.intValue()), 0.0, 0.0, interpolation.value); | ||
|
||
outputSocket.setValue(output); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the order of the operations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the sort order of the operations? It should have some set order so people can expect where the operations are in the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have a sort order the order is defined by the order that we list them in this file and the generated operations file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To rephrase @AustinShalit's question.
We should have some sort of policy for ordering and defining how we want the palette ordered.
Why we are putting specific operations in a specified order.
Do we have a policy like this? Can we come up with one?