Skip to content

Commit

Permalink
Update to pom-scijava 29.2.1
Browse files Browse the repository at this point in the history
And update code accordingly.

This commit addresses the following issues:

* Account for Location-related backwards incompatibilities.
* Make the GetMetadata example GUI-free.
* Clean up the SwingExample.
* Slightly improve the WorkingWithModules example (but see #88).
  • Loading branch information
ctrueden committed Aug 27, 2020
1 parent 256d84a commit 3fedf35
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 204 deletions.
2 changes: 1 addition & 1 deletion howtos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion howtos/src/main/java/howto/app/DisposeImageJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void run() {
// do something with ImageJ

// dispose ImageJ
ij.context().dispose();
ij.dispose();
}

public static void main(String...args) { run(); }
Expand Down
11 changes: 7 additions & 4 deletions howtos/src/main/java/howto/images/SaveImageCompressed.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
import io.scif.config.SCIFIOConfig;
import io.scif.formats.TIFFFormat;
import io.scif.services.DatasetIOService;

import java.io.IOException;
import java.nio.file.Files;

import net.imagej.Dataset;
import net.imagej.DefaultDataset;
import net.imagej.ImageJ;
import net.imagej.ImgPlus;
import net.imglib2.img.Img;

import java.io.IOException;
import java.nio.file.Files;
import org.scijava.io.location.FileLocation;

/**
* How to save an image as LZW compressed TIFF
Expand All @@ -35,7 +38,7 @@ public static void run() throws IOException {
Img img = (Img) ij.io().open(Object.class.getResource("/blobs.png").getPath());

// create temporary path to save image to
String dest = Files.createTempFile("img", ".tif").toString();
FileLocation dest = new FileLocation(Files.createTempFile("img", ".tif").toFile());
System.out.println("Saving image to " + dest);

// create SCIFIO config to set compression algorithm
Expand All @@ -48,7 +51,7 @@ public static void run() throws IOException {
ij.get(DatasetIOService.class).save(dataset, dest, config);

// load and show saved image
Object savedImg = ij.io().open(dest);
Object savedImg = ij.io().open(dest.getFile().getAbsolutePath());
ij.ui().show("saved image", savedImg);
}

Expand Down
89 changes: 32 additions & 57 deletions howtos/src/main/java/howto/metadata/GetMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@
*/
package howto.metadata;

import java.io.IOException;

import io.scif.services.FormatService;
import io.scif.FieldPrinter;
import io.scif.Format;
import io.scif.FormatException;
import io.scif.Metadata;
import io.scif.FieldPrinter;
import io.scif.services.FormatService;

import net.imagej.ImageJ;
import net.imagej.Dataset;
import net.imagej.ImageJ;

import org.scijava.ItemIO;
import org.scijava.command.Command;
import org.scijava.io.location.Location;
import org.scijava.io.location.LocationService;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
* This example illustrates how to access and display image metadata using the {@link FormatService} and
Expand All @@ -35,61 +32,62 @@
* An optional {@code formatMetadata} method is included to (hopefully) make the text more readable.
* </p>
*/
@Plugin(type = Command.class, menuPath = "Image>Show Metadata")
public class GetMetadata implements Command {
public class GetMetadata {

// -- Needed services --

// for determining the Format of the input image
@Parameter
private FormatService formatService;

@Parameter
private LocationService locationService;

// for logging errors
@Parameter
private LogService log;

// -- Inputs and outputs to the command --

// input image
@Parameter
private Dataset img;

// output metadata string
@Parameter(label = "Metadata", type = ItemIO.OUTPUT)
private String mString;

@Override
public void run() {
// we need the file path to determine the file format
final String filePath = img.getSource();
public static void run() throws Exception {
ImageJ ij = new ImageJ();

// catch any Format or IO exceptions
try {
// open a sample image
final Dataset img = ij.scifio().datasetIO().open("http://imagej.net/images/FluorescentCells.jpg");

// determine the Format based on the extension and, if necessary, the source data
Format format = formatService.getFormat(filePath);
// we need the file path to determine the file format
Location source = ij.get(LocationService.class).resolve(img.getSource());

// create an instance of the associated Parser and parse metadata from the image file
Metadata metadata = format.createParser().parse(filePath);
// catch any Format or IO exceptions
// determine the Format based on the extension and, if necessary, the source data
Format format = ij.scifio().format().getFormat(source);

// use FieldPrinter to traverse metadata tree and return as a String
String metadataTree = new FieldPrinter(metadata).toString();
// create an instance of the associated Parser and parse metadata from the image file
Metadata metadata = format.createParser().parse(source);

// (optional) remove some of the tree formatting to make the metadata easier to read
mString = formatMetadata(metadataTree);
}
catch (final FormatException | IOException e) {
log.error(e);
}
// use FieldPrinter to traverse metadata tree and return as a String
String metadataTree = new FieldPrinter(metadata).toString();

// (optional) remove some of the tree formatting to make the metadata easier to read
String mString = formatMetadata(metadataTree);

// print out our precious metadata!
ij.log().info(mString);

// close out our ImageJ
ij.dispose();
}

/**
* This function makes the metadata easier to read by removing some of the tree formatting from FieldPrinter.
* @param metadataTree raw metadata string returned by FieldPrinter().toString()
* @return formatted version of metadataTree
*/
private String formatMetadata(String metadataTree) {
private static String formatMetadata(String metadataTree) {

// remove ending braces | replace ", " between OME fields
String tmp = metadataTree.replaceAll("(\\t+}\\n)|(,\\s)", "\n");
Expand All @@ -98,28 +96,5 @@ private String formatMetadata(String metadataTree) {
return tmp.replaceAll("(\\t+\\{\\n)|(\\t+)", "");
}

/**
* This main function serves for development purposes.
* It allows you to run the plugin immediately out of
* your integrated development environment (IDE).
*
* @param args unused
* @throws Exception
*/
public static void main(final String... args) throws Exception {
// create the ImageJ application context with all available services
final ImageJ ij = new ImageJ();
ij.launch(args);

// open a sample image
final Dataset img = ij.scifio().datasetIO().open("http://imagej.net/images/FluorescentCells.jpg");

// show the image
ij.ui().show(img);

// invoke the plugin
ij.command().run(GetMetadata.class, true);

}

public static void main(String...args) throws Exception { run(); }
}
2 changes: 1 addition & 1 deletion maven-projects/add-two-datasets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion maven-projects/custom-preprocessor-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion maven-projects/dynamic-commands/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion maven-projects/execute-commands/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static Dataset invokeWithArgs(final ImageJ ij) {
*/
public static Dataset invokeWithMap(final ImageJ ij) {
// populate the map of input parameters
final Map<String, Object> inputMap = new HashMap<String, Object>();
final Map<String, Object> inputMap = new HashMap<>();
inputMap.put("inputFile", new File("sample-image.fake"));
// execute asynchronously using the command service
final Future<CommandModule> future =
Expand Down
2 changes: 1 addition & 1 deletion maven-projects/ij2-image-plus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion maven-projects/listen-to-events/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion maven-projects/simple-commands/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
2 changes: 1 addition & 1 deletion maven-projects/swing-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>29.2.1</version>
<relativePath />
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public void run() {
}

log.info("starting deconvolution");
deconvolved = ops.deconvolve().richardsonLucy(imgFloat, psf, numIterations);
final Img<FloatType> deconvolved = ops.create().img(imgFloat);
ops.deconvolve().richardsonLucy(deconvolved, imgFloat, psf, numIterations);
log.info("finished deconvolution");

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,66 +8,37 @@

import javax.swing.SwingUtilities;

import net.imagej.ops.OpService;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.numeric.real.FloatType;

import org.scijava.Context;
import org.scijava.ItemIO;
import org.scijava.app.StatusService;
import org.scijava.command.Command;
import org.scijava.command.CommandService;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.thread.ThreadService;
import org.scijava.ui.UIService;

@Plugin(type = Command.class, headless = true,
menuPath = "Deconvolution>Deconvolution Swing")
public class DeconvolutionCommandSwing implements Command {

@Parameter
OpService ops;
private Context ctx;

@Parameter
LogService log;

@Parameter
UIService ui;

@Parameter
CommandService cmd;

@Parameter
StatusService status;

@Parameter
ThreadService thread;
@Parameter(type = ItemIO.OUTPUT)
private RandomAccessibleInterval<FloatType> deconvolved;

private static DeconvolutionDialog dialog = null;

@Parameter(type = ItemIO.OUTPUT)
RandomAccessibleInterval<FloatType> deconvolved;

/**
* show a dialog and give the dialog access to required IJ2 Services
*/
@Override
public void run() {

SwingUtilities.invokeLater(() -> {
if (dialog == null) {
dialog = new DeconvolutionDialog();
dialog = new DeconvolutionDialog(ctx);
}
dialog.setVisible(true);

dialog.setOps(ops);
dialog.setLog(log);
dialog.setStatus(status);
dialog.setCommand(cmd);
dialog.setThread(thread);
dialog.setUi(ui);

});
}
}
Loading

0 comments on commit 3fedf35

Please sign in to comment.