use of ini.trakem2.display.ZDisplayable in project TrakEM2 by trakem2.
the class Loader method importStack.
/**
* Imports an image stack from a multitiff file and places each slice in the proper layer, creating new layers as it goes. If the given stack is null, popup a file dialog to choose one
*/
public Bureaucrat importStack(final Layer first_layer, final double x, final double y, final ImagePlus imp_stack_, final boolean ask_for_data, final String filepath_, final boolean one_patch_per_layer_) {
Utils.log2("Loader.importStack filepath: " + filepath_);
if (null == first_layer)
return null;
final Worker worker = new Worker("import stack") {
@Override
public void run() {
startedWorking();
try {
String filepath = filepath_;
boolean one_patch_per_layer = one_patch_per_layer_;
/* On drag and drop the stack is not null! */
// Utils.log2("imp_stack_ is " + imp_stack_);
ImagePlus[] stks = null;
boolean choose = false;
if (null == imp_stack_) {
stks = Utils.findOpenStacks();
choose = null == stks || stks.length > 0;
} else {
stks = new ImagePlus[] { imp_stack_ };
}
ImagePlus imp_stack = null;
// ask to open a stack if it's null
if (null == stks) {
// choose one
imp_stack = openStack();
} else if (choose) {
// choose one from the list
final GenericDialog gd = new GenericDialog("Choose one");
gd.addMessage("Choose a stack from the list or 'open...' to bring up a file chooser dialog:");
final String[] list = new String[stks.length + 1];
for (int i = 0; i < list.length - 1; i++) {
list[i] = stks[i].getTitle();
}
list[list.length - 1] = "[ Open stack... ]";
gd.addChoice("choose stack: ", list, list[0]);
gd.showDialog();
if (gd.wasCanceled()) {
finishedWorking();
return;
}
final int i_choice = gd.getNextChoiceIndex();
if (list.length - 1 == i_choice) {
// the open... command
imp_stack = first_layer.getProject().getLoader().openStack();
} else {
imp_stack = stks[i_choice];
}
} else {
imp_stack = imp_stack_;
}
// check:
if (null == imp_stack) {
finishedWorking();
return;
}
final String props = (String) imp_stack.getProperty("Info");
// check if it's amira labels stack to prevent missimports
if (null != props && -1 != props.indexOf("Materials {")) {
final YesNoDialog yn = new YesNoDialog(IJ.getInstance(), "Warning", "You are importing a stack of Amira labels as a regular image stack. Continue anyway?");
if (!yn.yesPressed()) {
finishedWorking();
return;
}
}
// String dir = imp_stack.getFileInfo().directory;
final double layer_width = first_layer.getLayerWidth();
final double layer_height = first_layer.getLayerHeight();
final double current_thickness = first_layer.getThickness();
double thickness = current_thickness;
boolean expand_layer_set = false;
boolean lock_stack = false;
// int anchor = LayerSet.NORTHWEST; //default
if (ask_for_data) {
// ask for slice separation in pixels
final GenericDialog gd = new GenericDialog("Slice separation?");
gd.addMessage("Please enter the slice thickness, in pixels");
// assuming pixelWidth == pixelHeight
gd.addNumericField("slice_thickness: ", Math.abs(imp_stack.getCalibration().pixelDepth / imp_stack.getCalibration().pixelHeight), 3);
if (layer_width != imp_stack.getWidth() || layer_height != imp_stack.getHeight()) {
gd.addCheckbox("Resize canvas to fit stack", true);
// gd.addChoice("Anchor: ", LayerSet.ANCHORS, LayerSet.ANCHORS[0]);
}
gd.addCheckbox("Lock stack", false);
final String[] importStackTypes = { "One slice per layer (Patches)", "Image volume (Stack)" };
if (imp_stack.getStack().isVirtual()) {
one_patch_per_layer = true;
}
gd.addChoice("Import stack as:", importStackTypes, importStackTypes[0]);
((Component) gd.getChoices().get(0)).setEnabled(!imp_stack.getStack().isVirtual());
gd.showDialog();
if (gd.wasCanceled()) {
if (null == stks) {
// flush only if it was not open before
flush(imp_stack);
}
finishedWorking();
return;
}
if (layer_width != imp_stack.getWidth() || layer_height != imp_stack.getHeight()) {
expand_layer_set = gd.getNextBoolean();
// anchor = gd.getNextChoiceIndex();
}
lock_stack = gd.getNextBoolean();
thickness = gd.getNextNumber();
// check provided thickness with that of the first layer:
if (thickness != current_thickness) {
if (1 == first_layer.getParent().size() && first_layer.isEmpty()) {
final YesNoCancelDialog yn = new YesNoCancelDialog(IJ.getInstance(), "Mismatch!", "The current layer's thickness is " + current_thickness + "\nwhich is " + (thickness < current_thickness ? "larger" : "smaller") + " than\nthe desired " + thickness + " for each stack slice.\nAdjust current layer's thickness to " + thickness + " ?");
if (yn.cancelPressed()) {
// was opened new
if (null != imp_stack_)
flush(imp_stack);
finishedWorking();
return;
} else if (yn.yesPressed()) {
first_layer.setThickness(thickness);
// The rest of layers, created new, will inherit the same thickness
}
} else {
final YesNoDialog yn = new YesNoDialog(IJ.getInstance(), "WARNING", "There's more than one layer or the current layer is not empty\nso the thickness cannot be adjusted. Proceed anyway?");
if (!yn.yesPressed()) {
finishedWorking();
return;
}
}
}
one_patch_per_layer = imp_stack.getStack().isVirtual() || 0 == gd.getNextChoiceIndex();
}
if (null == imp_stack.getStack()) {
Utils.showMessage("Not a stack.");
finishedWorking();
return;
}
// WARNING: there are fundamental issues with calibration, because the Layer thickness is disconnected from the Calibration pixelDepth
// set LayerSet calibration if there is no calibration
boolean calibrate = true;
if (ask_for_data && first_layer.getParent().isCalibrated()) {
if (!ControlWindow.isGUIEnabled()) {
Utils.log2("Loader.importStack: overriding LayerSet calibration with that of the imported stack.");
} else {
final YesNoDialog yn = new YesNoDialog("Calibration", "The layer set is already calibrated. Override with the stack calibration values?");
if (!yn.yesPressed()) {
calibrate = false;
}
}
}
if (calibrate) {
first_layer.getParent().setCalibration(imp_stack.getCalibration());
}
if (layer_width < imp_stack.getWidth() || layer_height < imp_stack.getHeight()) {
expand_layer_set = true;
}
if (imp_stack.getStack().isVirtual()) {
// do nothing
} else if (null == filepath) {
// try to get it from the original FileInfo
final FileInfo fi = imp_stack.getOriginalFileInfo();
if (null != fi && null != fi.directory && null != fi.fileName) {
filepath = fi.directory.replace('\\', '/');
if (!filepath.endsWith("/"))
filepath += '/';
filepath += fi.fileName;
}
Utils.log2("Getting filepath from FileInfo: " + filepath);
// check that file exists, otherwise save a copy in the storage folder
if (null == filepath || (!filepath.startsWith("http://") && !new File(filepath).exists())) {
filepath = handlePathlessImage(imp_stack);
}
} else {
filepath = filepath.replace('\\', '/');
}
// Import as Stack ZDisplayable object:
if (!one_patch_per_layer) {
final Stack st = new Stack(first_layer.getProject(), new File(filepath).getName(), x, y, first_layer, filepath);
first_layer.getParent().add(st);
finishedWorking();
return;
}
// Place the first slice in the current layer, and then query the parent LayerSet for subsequent layers, and create them if not present.
final Patch last_patch = Loader.this.importStackAsPatches(first_layer.getProject(), first_layer, x, y, imp_stack, null != imp_stack_ && null != imp_stack_.getCanvas(), filepath);
if (null != last_patch) {
last_patch.setLocked(lock_stack);
Display.updateCheckboxes(last_patch.getLinkedGroup(null), DisplayablePanel.LOCK_STATE, true);
}
if (expand_layer_set) {
last_patch.getLayer().getParent().setMinimumDimensions();
}
Utils.log2("props: " + props);
// check if it's an amira stack, then ask to import labels
if (null != props && -1 == props.indexOf("Materials {") && -1 != props.indexOf("CoordType")) {
final YesNoDialog yn = new YesNoDialog(IJ.getInstance(), "Amira Importer", "Import labels as well?");
if (yn.yesPressed()) {
// select labels
final Collection<AreaList> alis = AmiraImporter.importAmiraLabels(first_layer, last_patch.getX(), last_patch.getY(), imp_stack.getOriginalFileInfo().directory);
if (null != alis) {
// import all created AreaList as nodes in the ProjectTree under a new imported_segmentations node
first_layer.getProject().getProjectTree().insertSegmentations(alis);
// link them to the images
for (final AreaList ali : alis) {
ali.linkPatches();
}
}
}
}
// it is safe not to flush the imp_stack, because all its resources are being used anyway (all the ImageProcessor), and it has no awt.Image. Unless it's being shown in ImageJ, and then it will be flushed on its own when the user closes its window.
} catch (final Exception e) {
IJError.print(e);
}
finishedWorking();
}
};
return Bureaucrat.createAndStart(worker, first_layer.getProject());
}
use of ini.trakem2.display.ZDisplayable in project TrakEM2 by trakem2.
the class TMLHandler method getProjectData.
/**
* returns 4 objects packed in an array:
* <pre>
* [0] = root TemplateThing
* [1] = root ProjectThing (contains Project instance)
* [2] = root LayerThing (contains the top-level LayerSet)
* [3] = expanded states of all ProjectThing objects
* </pre>
* <p>
* Also, triggers the reconstruction of links and assignment of Displayable objects to their layer.
* </p>
*/
public Object[] getProjectData(final boolean open_displays) {
if (null == project)
return null;
this.open_displays = open_displays;
// Links exist between Displayable objects.
for (final Displayable d : ht_displayables.values()) {
String olinks = ht_links.get(d);
// not linked
if (null == olinks)
continue;
String[] links = olinks.split(",");
Long lid = null;
for (int i = 0; i < links.length; i++) {
try {
lid = new Long(links[i]);
} catch (NumberFormatException nfe) {
Utils.log2("Ignoring incorrectly formated link '" + links[i] + "' for ob " + d);
continue;
}
Displayable partner = ht_displayables.get(lid);
if (null != partner)
d.link(partner, false);
else
Utils.log("TMLHandler: can't find partner with id=" + links[i] + " for Displayable with id=" + d.getId());
}
}
// 1.2 - Reconstruct linked properties
for (final Map.Entry<Displayable, Map<Long, Map<String, String>>> lpe : all_linked_props.entrySet()) {
final Displayable origin = lpe.getKey();
for (final Map.Entry<Long, Map<String, String>> e : lpe.getValue().entrySet()) {
final Displayable target = ht_displayables.get(e.getKey());
if (null == target) {
Utils.log("Setting linked properties for origin " + origin.getId() + ":\n\t* Could not find target displayable #" + e.getKey());
continue;
}
origin.setLinkedProperties(target, e.getValue());
}
}
// 2 - Add Displayable objects to ProjectThing that can contain them
for (final Map.Entry<Long, ProjectThing> entry : ht_oid_pt.entrySet()) {
ProjectThing pt = entry.getValue();
Object od = ht_displayables.remove(entry.getKey());
// Utils.log("==== processing: Displayable [" + od + "] vs. ProjectThing [" + pt + "]");
if (null != od) {
pt.setObject(od);
} else {
Utils.log("#### Failed to find a Displayable for ProjectThing " + pt + " #####");
}
}
// 3 - Assign a layer pointer to ZDisplayable objects
for (final ZDisplayable zd : ht_zdispl.values()) {
// zd.setLayer((Layer)zd.getLayerSet().getLayers().get(0));
zd.setLayer(zd.getLayerSet().getLayer(0));
}
// 4 - Assign layers to Treeline nodes
for (final Layer la : al_layers) {
final List<Node<?>> list = node_layer_table.remove(la.getId());
if (null == list)
continue;
for (final Node<?> nd : list) nd.setLayer(la);
}
if (!node_layer_table.isEmpty()) {
Utils.log("ERROR: node_layer_table is not empty!");
}
// 5 - Assign root nodes to Treelines, now that all nodes have a layer
for (final Map.Entry<Tree<?>, Node<?>> e : tree_root_nodes.entrySet()) {
if (null == e.getValue()) {
// Utils.log2("Ignoring, applies to new Treeline format only.");
continue;
}
// Can't compile with <?>
// will generate node caches of each Treeline
e.getKey().setRoot((Node) e.getValue());
}
tree_root_nodes.clear();
// Assign colors to nodes
for (final Map.Entry<Color, Collection<Node<?>>> e : node_colors.entrySet()) {
for (final Node<?> nd : e.getValue()) {
nd.setColor(e.getKey());
}
}
node_colors.clear();
// 6 - Run legacy operations
for (final Runnable r : legacy) {
r.run();
}
try {
// Create a table with all layer ids vs layer instances:
final HashMap<Long, Layer> ht_lids = new HashMap<Long, Layer>();
for (final Layer layer : al_layers) {
ht_lids.put(new Long(layer.getId()), layer);
}
// Spawn threads to recreate buckets, starting from the subset of displays to open
int n = Runtime.getRuntime().availableProcessors();
switch(n) {
case 1:
break;
case 2:
case 3:
case 4:
n--;
break;
default:
n -= 2;
break;
}
final ExecutorService exec = Utils.newFixedThreadPool(n, "TMLHandler-recreateBuckets");
final Set<Long> dlids = new HashSet<Long>();
final LayerSet layer_set = (LayerSet) root_lt.getObject();
final List<Future<?>> fus = new ArrayList<Future<?>>();
final List<Future<?>> fus2 = new ArrayList<Future<?>>();
for (final HashMap<String, String> ht_attributes : al_displays) {
String ob = ht_attributes.get("layer_id");
if (null == ob)
continue;
final Long lid = new Long(ob);
dlids.add(lid);
final Layer la = ht_lids.get(lid);
if (null == la) {
ht_lids.remove(lid);
continue;
}
// to open later:
new Display(project, Long.parseLong(ht_attributes.get("id")), la, ht_attributes);
fus.add(exec.submit(new Runnable() {
public void run() {
la.recreateBuckets();
}
}));
}
fus.add(exec.submit(new Runnable() {
public void run() {
// only for ZDisplayable
layer_set.recreateBuckets(false);
}
}));
// Ensure launching:
if (dlids.isEmpty() && layer_set.size() > 0) {
dlids.add(layer_set.getLayer(0).getId());
}
final List<Layer> layers = layer_set.getLayers();
for (final Long lid : new HashSet<Long>(dlids)) {
fus.add(exec.submit(new Runnable() {
public void run() {
int start = layer_set.indexOf(layer_set.getLayer(lid.longValue()));
int next = start + 1;
int prev = start - 1;
while (next < layer_set.size() || prev > -1) {
if (prev > -1) {
final Layer lprev = layers.get(prev);
synchronized (dlids) {
if (dlids.add(lprev.getId())) {
// returns true if not there already
fus2.add(exec.submit(new Runnable() {
public void run() {
lprev.recreateBuckets();
}
}));
}
}
prev--;
}
if (next < layers.size()) {
final Layer lnext = layers.get(next);
synchronized (dlids) {
if (dlids.add(lnext.getId())) {
// returns true if not there already
fus2.add(exec.submit(new Runnable() {
public void run() {
lnext.recreateBuckets();
}
}));
}
}
next++;
}
}
Utils.log2("done recreateBuckets chunk");
}
}));
}
Utils.wait(fus);
exec.submit(new Runnable() {
public void run() {
Utils.log2("waiting for TMLHandler fus...");
Utils.wait(fus2);
Utils.log2("done waiting TMLHandler fus.");
exec.shutdown();
}
});
} catch (Throwable t) {
IJError.print(t);
}
return new Object[] { root_tt, root_pt, root_lt, ht_pt_expanded };
}
use of ini.trakem2.display.ZDisplayable in project TrakEM2 by trakem2.
the class ProjectThing method createChild.
public ProjectThing createChild(String type) {
// create the Displayable
TemplateThing tt = template.getChildTemplate(type);
if (null == tt) {
Utils.log2("Can't create a child of type " + type);
return null;
}
Object ob = project.makeObject(tt);
Layer layer = null;
if (ob instanceof Displayable) {
// which layer to add it to? Get it from the front Display
layer = Display.getFrontLayer(this.project);
if (null == layer) {
Utils.showMessage("Open a display first!");
((DBObject) ob).removeFromDatabase();
return null;
}
}
// wrap it in a new ProjectThing
ProjectThing pt = null;
try {
pt = new ProjectThing(tt, project, ob);
} catch (Exception e) {
IJError.print(e);
return null;
}
// add it here as child
addChild(pt);
// finally, add it to the layer if appropriate
if (null != layer) {
if (ob instanceof ZDisplayable) {
layer.getParent().add((ZDisplayable) ob);
} else {
layer.add((Displayable) ob);
}
}
// finally, return it to be added to the ProjectTree as a new node
return pt;
}
use of ini.trakem2.display.ZDisplayable in project TrakEM2 by trakem2.
the class ProjectThing method createClonedChild.
/**
* At the moment only for basic types, which by definition have no children.
*/
public ProjectThing createClonedChild(final ProjectThing child) {
// must be a child and a basic type
if (null == child || null == child.object || null == al_children || !al_children.contains(child) || !Project.isBasicType(child.getType())) {
return null;
}
final Displayable displ = (Displayable) ((Displayable) child.object).clone();
ProjectThing pt = null;
try {
pt = new ProjectThing(child.template, project, displ);
addChild(pt);
// add to the proper container
if (displ instanceof ZDisplayable) {
ZDisplayable original = (ZDisplayable) child.object;
original.getLayerSet().add((ZDisplayable) displ);
Display.repaint(original.getLayerSet(), (ZDisplayable) displ, 5);
} else {
Displayable original = (Displayable) child.object;
original.getLayer().add(displ);
Display.repaint(original.getLayer(), displ, 5);
}
// user-friendly copy:
displ.setLocked(false);
displ.setVisible(true);
// set the copy as selected in the front Display, if any
if (null != Display.getFront())
Display.getFront().select(displ);
} catch (Exception e) {
IJError.print(e);
return null;
}
return pt;
}
use of ini.trakem2.display.ZDisplayable in project TrakEM2 by trakem2.
the class Merger method createTable.
private static JTable createTable(final HashSet<ZDisplayable> hs, final String column_title, final Project p1, final Project p2) {
final TwoColumnModel tcm = new TwoColumnModel(hs, column_title);
final JTable table = new JTable(tcm);
table.setDefaultRenderer(table.getColumnClass(0), new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (1 == column && tcm.sent[row]) {
c.setBackground(Color.green);
c.setForeground(Color.white);
} else if (isSelected) {
c.setForeground(table.getSelectionForeground());
c.setBackground(table.getSelectionBackground());
} else {
c.setBackground(Color.white);
c.setForeground(Color.black);
}
return c;
}
});
table.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
final JTable src = (JTable) me.getSource();
final TwoColumnModel model = (TwoColumnModel) src.getModel();
final int row = src.rowAtPoint(me.getPoint()), col = src.columnAtPoint(me.getPoint());
if (2 == me.getClickCount()) {
Object ob = model.getValueAt(row, col);
if (ob instanceof ZDisplayable) {
ZDisplayable zd = (ZDisplayable) ob;
Display df = Display.getOrCreateFront(zd.getProject());
// also select
df.show(zd.getFirstLayer(), zd, true, false);
}
} else if (me.isPopupTrigger()) {
JPopupMenu popup = new JPopupMenu();
final JMenuItem send = new JMenuItem("Send selection");
popup.add(send);
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
ArrayList<ZDisplayable> col = new ArrayList<ZDisplayable>();
for (final int i : src.getSelectedRows()) {
col.add((ZDisplayable) model.getValueAt(i, 0));
}
if (col.isEmpty())
return;
// the other
Project target = col.get(0).getProject() == p1 ? p2 : p1;
LayerSet ls = target.getRootLayerSet();
ArrayList<ZDisplayable> copies = new ArrayList<ZDisplayable>();
for (ZDisplayable zd : col) {
copies.add((ZDisplayable) zd.clone(target, false));
model.sent[row] = true;
}
// 1. To the LayerSet:
ls.addAll(copies);
// 2. To the ProjectTree:
target.getProjectTree().insertSegmentations(copies);
// Update:
model.fireTableDataChanged();
}
});
popup.show(table, me.getX(), me.getY());
}
}
});
return table;
}
Aggregations