Skip to content

Commit

Permalink
Move ApplyLookupTable from imagej-plugins-commands
Browse files Browse the repository at this point in the history
It's a core piece of the LUTService logic here, and does not belong
in a downstream component which is intended to be optional.

This avoids "Could not load icon for class" errors in downstream
projects such as sciview who depend on imagej-common but not
imagej-plugins-commands.
  • Loading branch information
ctrueden committed Apr 16, 2024
1 parent 8117b71 commit 4bd1e57
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>net.imagej</groupId>
<artifactId>imagej-common</artifactId>
<version>2.0.6-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>

<name>ImageJ Common</name>
<description>This library contains ImageJ's core image data model, based on ImgLib2. It is the primary mechanism by which image data is managed internally by ImageJ2. This component also provides the corresponding core image display logic for user interfaces.</description>
Expand Down
171 changes: 171 additions & 0 deletions src/main/java/net/imagej/lut/ApplyLookupTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* #%L
* ImageJ2 software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2009 - 2024 ImageJ2 developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imagej.lut;

import java.io.IOException;
import java.net.URL;

import net.imagej.Dataset;
import net.imagej.DatasetService;
import net.imagej.axis.Axes;
import net.imagej.axis.AxisType;
import net.imagej.display.DatasetView;
import net.imagej.display.ImageDisplay;
import net.imagej.display.ImageDisplayService;
import net.imglib2.RandomAccess;
import net.imglib2.display.ColorTable;
import net.imglib2.type.numeric.RealType;

import org.scijava.ItemIO;
import org.scijava.command.Command;
import org.scijava.display.DisplayService;
import org.scijava.log.LogService;
import org.scijava.plugin.Attr;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
* <p>
* Reads a lookup table from a URL. If there is an active {@link ImageDisplay}
* the lookup table is applied to its current view. Otherwise it creates a new
* {@link ImageDisplay} in which in can display the lookup table.
* <p>
* Designed to be used by menu code. Lookup tables are discovered and menu
* entries are constructed using their URLs with instances of this class.
*
* @author Barry DeZonia
*/
@Plugin(type = Command.class, attrs = { @Attr(name = "no-legacy") })
public class ApplyLookupTable implements Command {

// -- Parameters --

@Parameter
private LogService logService;

@Parameter
private LUTService lutService;

@Parameter
private DisplayService displayService;

@Parameter
private ImageDisplayService imageDisplayService;

@Parameter
private DatasetService datasetService;

@Parameter(required = false)
private ImageDisplay display;

@Parameter(required = false)
private DatasetView view;

@Parameter
private URL tableURL;
// = "file:///Users/bdezonia/Desktop/ImageJ/luts/6_shades.lut";

@Parameter(type = ItemIO.OUTPUT)
private ImageDisplay output;

// -- constants --

private static final int WIDTH = 256;
private static final int HEIGHT = 32;

// -- Command methods --

@Override
public void run() {
if (tableURL == null) {
logService.warn("ApplyLookupTable: no URL string provided.");
return;
}
final ColorTable colorTable;
try {
colorTable = lutService.loadLUT(tableURL);
}
catch (IOException exc) {
logService.error("ApplyLookupTable: error loading color table at URL: " +
tableURL, exc);
return;
}
if (colorTable == null) {
logService.error("ApplyLookupTable: no color table at URL: " +
tableURL);
return;
}
if (display == null) {
Dataset ds = makeData();
display = (ImageDisplay) displayService.createDisplay(ds);
}
if (view == null) {
view = imageDisplayService.getActiveDatasetView(display);
}
int channel = (int) view.getLongPosition(Axes.CHANNEL);
view.setColorTable(colorTable, channel);
output = display;
}

// -- private helpers --

private Dataset makeData() {
String urlString;
try {
urlString = tableURL.toURI().getPath();
}
catch (Exception e) {
urlString = tableURL.getPath();
}
String name =
urlString.substring(urlString.lastIndexOf("/") + 1, urlString.length());
long[] dims = new long[] { WIDTH, HEIGHT };
AxisType[] axes = new AxisType[] { Axes.X, Axes.Y };
int bitsPerPixel = 8;
boolean signed = false;
boolean floating = false;
Dataset ds =
datasetService.create(dims, name, axes, bitsPerPixel, signed, floating);
ramp(ds);
return ds;
}

private void ramp(Dataset ds) {
RandomAccess<? extends RealType<?>> accessor =
ds.getImgPlus().randomAccess();
for (int x = 0; x < WIDTH; x++) {
accessor.setPosition(x, 0);
for (int y = 0; y < HEIGHT; y++) {
accessor.setPosition(y, 1);
accessor.get().setReal(x);
}
}
}
}
4 changes: 1 addition & 3 deletions src/main/java/net/imagej/lut/DefaultLUTService.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ private ModuleInfo createInfo(final String key, final URL url) {
final HashMap<String, Object> presets = new HashMap<>();
presets.put("tableURL", url);
// and create the command info
// CTR FIXME: Avoid circular dependency.
final CommandInfo info =
new CommandInfo("net.imagej.plugins.commands.misc.ApplyLookupTable");
final CommandInfo info = new CommandInfo(ApplyLookupTable.class);
info.setPresets(presets);
info.setMenuPath(menuPath);
// use the default icon
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/imagej/lut/LUTSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public class LUTSelector extends DynamicCommand {
public void run() {
// set LUT for current channel of current ImageDisplay
if (display != null) {
// CTR FIXME: Avoid circular dependency and convert to service method.
cmdService.run("net.imagej.plugins.commands.misc.ApplyLookupTable", true,
// TODO: Convert to service method.
cmdService.run(ApplyLookupTable.class, true,
"display", display, "tableURL", luts.get(choice));
}
}
Expand Down

0 comments on commit 4bd1e57

Please sign in to comment.