Created
December 11, 2011 12:09
-
-
Save ctrueden/1460260 to your computer and use it in GitHub Desktop.
How to load and invoke ImageJ2 plugins
This file contains 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
// | |
// KnipImageJ.java | |
// | |
/* | |
ImageJ software for multidimensional image processing and analysis. | |
Copyright (c) 2010, ImageJDev.org. | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* 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. | |
* Neither the names of the ImageJDev.org developers nor the | |
names of its contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
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. | |
*/ | |
package imagej; | |
import imagej.data.Dataset; | |
import imagej.data.display.DatasetView; | |
import imagej.data.display.ImageDisplay; | |
import imagej.data.display.OverlayView; | |
import imagej.data.overlay.Overlay; | |
import imagej.event.EventHandler; | |
import imagej.event.EventService; | |
import imagej.event.StatusEvent; | |
import imagej.ext.module.Module; | |
import imagej.ext.module.ModuleException; | |
import imagej.ext.module.ModuleInfo; | |
import imagej.ext.module.ModuleItem; | |
import imagej.ext.module.ModuleRunner; | |
import imagej.ext.module.ModuleService; | |
import imagej.ext.module.process.ModulePostprocessor; | |
import imagej.ext.module.process.ModulePreprocessor; | |
import imagej.ext.plugin.DynamicPlugin; | |
import imagej.ext.plugin.InitPreprocessor; | |
import imagej.ext.plugin.PluginService; | |
import imagej.ext.plugin.ServicePreprocessor; | |
import imagej.util.ClassUtils; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Exploration of ImageJ plugin integration from other tools | |
* (e.g., KNIP, OMERO, ICY). | |
* | |
* @author Curtis Rueden | |
*/ | |
public final class KnipImageJ { | |
private KnipImageJ() { | |
// prevent instantiation of utility class | |
} | |
private static Object subscribers; | |
public static void main(final String[] args) { | |
// final ImageJ context = ImageJ.createContext(); | |
// context.getService(UIService.class).processArgs(args); | |
@SuppressWarnings("unchecked") | |
final ImageJ context = | |
ImageJ.createContext(EventService.class, ModuleService.class, | |
PluginService.class); | |
// Note: the above context will contain only the minimal services needed | |
// to discover plugins. But the plugins themselves may depend on | |
// additional services. So it may be a good idea to instead use | |
// ImageJ.createContext() which will load all available services, rather | |
// than only the minimal ones. | |
// listen to status events | |
// need to keep a reference, so the subscribers aren't GCed | |
final EventService eventService = context.getService(EventService.class); | |
subscribers = eventService.subscribe(new KnipImageJ()); | |
// get list of modules, and filter them to those acceptable to KNIME/KNIP | |
final ModuleService moduleService = | |
context.getService(ModuleService.class); | |
final List<ModuleInfo> modules = moduleService.getModules(); | |
final List<ModuleInfo> legalModules = findLegalModules(modules); | |
final SettingsModel settingsModel = null; | |
final ModuleInfo sampleInfo = legalModules.get(0); | |
final Module module; | |
try { | |
module = sampleInfo.createModule(); | |
} | |
catch (final ModuleException e) { | |
return; | |
} | |
final List<ModulePreprocessor> pre = new ArrayList<ModulePreprocessor>(); | |
pre.add(new InitPreprocessor()); | |
pre.add(new ServicePreprocessor(context)); | |
pre.add(new KnipPreprocessor(settingsModel)); | |
final List<ModulePostprocessor> post = | |
new ArrayList<ModulePostprocessor>(); | |
final Object something = new Object(); | |
post.add(new KnipPostprocessor(something)); | |
final ModuleRunner runner = new ModuleRunner(module, pre, post); | |
runner.run(); | |
} | |
public static class SettingsModel { | |
public Object getValue() { | |
return null; | |
} | |
} | |
public static class KnipPreprocessor implements ModulePreprocessor { | |
private final SettingsModel settingsModel; | |
public KnipPreprocessor(final SettingsModel settingsModel) { | |
this.settingsModel = settingsModel; | |
} | |
@Override | |
public boolean canceled() { | |
return false; | |
} | |
@Override | |
public String getMessage() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
@Override | |
public void process(final Module module) { | |
// TODO Auto-generated method stub | |
for (final ModuleItem<?> item : module.getInfo().inputs()) { | |
processItem(item, module); | |
} | |
} | |
public <T> void processItem(final ModuleItem<T> item, final Module module) | |
{ | |
if (module.isResolved(item.getName())) return; | |
module.setInput(item.getName(), settingsModel.getValue()); | |
} | |
} | |
public static class KnipPostprocessor implements ModulePostprocessor { | |
public KnipPostprocessor(final Object something) {} | |
@Override | |
public void process(final Module module) { | |
// TODO Auto-generated method stub | |
for (final ModuleItem<?> item : module.getInfo().outputs()) { | |
processItem(item, module); | |
} | |
} | |
private <T> void | |
processItem(final ModuleItem<T> item, final Module module) | |
{ | |
// TODO: do something with this output | |
} | |
} | |
@EventHandler | |
protected void onEvent(final StatusEvent event) { | |
// do something | |
} | |
private static List<ModuleInfo> findLegalModules( | |
final List<ModuleInfo> modules) | |
{ | |
final ArrayList<ModuleInfo> legal = new ArrayList<ModuleInfo>(); | |
for (final ModuleInfo info : modules) { | |
if (isEvilPlugin(info)) continue; | |
for (final ModuleItem<?> item : info.inputs()) { | |
final Class<?> type = item.getType(); | |
if (!isLegalType(type)) continue; | |
legal.add(info); | |
} | |
} | |
return legal; | |
} | |
private static boolean isEvilPlugin(final ModuleInfo info) { | |
// SO UGLY! TODO: add API to ModuleInfo to query whether the module has | |
// dynamically generated inputs and outputs (isStatic? isDynamic?) | |
final String className = info.getDelegateClassName(); | |
final Class<?> c = ClassUtils.loadClass(className); | |
return !DynamicPlugin.class.isAssignableFrom(c); | |
} | |
private static final Class<?>[] LEGAL_TYPES = { String.class, Number.class, | |
Dataset.class, Overlay.class, DatasetView.class, OverlayView.class, | |
ImageDisplay.class, | |
// TODO: primitive types? | |
}; | |
private static boolean isLegalType(final Class<?> type) { | |
for (final Class<?> candidate : LEGAL_TYPES) { | |
if (candidate.isAssignableFrom(type)) return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment