use of ini.trakem2.display.Display in project TrakEM2 by trakem2.
the class Loader method getJobsPopup.
public JPopupMenu getJobsPopup(final Display display) {
synchronized (jobs) {
this.popup_jobs = new JPopupMenu("Cancel jobs:");
int i = 1;
for (final Bureaucrat burro : jobs) {
final JMenuItem item = new JMenuItem("Job " + i + ": " + burro.getTaskName());
item.addActionListener(display);
popup_jobs.add(item);
i++;
}
}
return popup_jobs;
}
use of ini.trakem2.display.Display in project TrakEM2 by trakem2.
the class Loader method importGrid.
/**
* Import a grid of images and put them in the layer. If the directory (@param dir) is null, it'll be asked for.
*/
public Bureaucrat importGrid(final Layer layer, String dir) {
try {
String file = null;
if (null == dir) {
final String[] dn = Utils.selectFile("Select first image");
if (null == dn)
return null;
dir = dn[0];
file = dn[1];
}
// char digit digit
String convention = "cdd";
boolean chars_are_columns = true;
// examine file name
/*
if (file.matches("\\A[a-zA-Z]\\d\\d.*")) { // one letter, 2 numbers
//means:
// \A - beggining of input
// [a-zA-Z] - any letter upper or lower case
// \d\d - two consecutive digits
// .* - any row of chars
ini_grid_convention = true;
}
*/
// ask for chars->rows, numbers->columns or viceversa
final GenericDialog gd = new GenericDialog("Conventions");
gd.addStringField("file_name_contains:", "");
gd.addNumericField("base_x: ", 0, 3);
gd.addNumericField("base_y: ", 0, 3);
gd.addMessage("Use: x(any), c(haracter), d(igit)");
gd.addStringField("convention: ", convention);
final String[] cr = new String[] { "columns", "rows" };
gd.addChoice("characters are: ", cr, cr[0]);
gd.addMessage("[File extension ignored]");
// as asked by Joachim Walter
gd.addNumericField("bottom-top overlap: ", 0, 3);
gd.addNumericField("left-right overlap: ", 0, 3);
gd.addCheckbox("link_images", false);
gd.addCheckbox("montage with phase correlation", false);
gd.addCheckbox("homogenize_contrast", true);
final Component[] c = { (Component) gd.getSliders().get(gd.getSliders().size() - 2), (Component) gd.getNumericFields().get(gd.getNumericFields().size() - 2), (Component) gd.getSliders().get(gd.getSliders().size() - 1), (Component) gd.getNumericFields().get(gd.getNumericFields().size() - 1), (Component) gd.getChoices().get(gd.getChoices().size() - 1) };
// enable the checkbox to control the slider and its associated numeric field:
Utils.addEnablerListener((Checkbox) gd.getCheckboxes().get(gd.getCheckboxes().size() - 1), c, null);
// gd.addCheckbox("Apply non-linear deformation", false);
gd.showDialog();
if (gd.wasCanceled()) {
return null;
}
// collect data
// filter away files not containing this tag
final String regex = gd.getNextString();
// the base x,y of the whole grid
final double bx = gd.getNextNumber();
final double by = gd.getNextNumber();
// if (!ini_grid_convention) {
convention = gd.getNextString().toLowerCase();
// }
if (/*!ini_grid_convention && */
(null == convention || convention.equals("") || -1 == convention.indexOf('c') || -1 == convention.indexOf('d'))) {
// TODO check that the convention has only 'cdx' chars and also that there is an island of 'c's and of 'd's only.
Utils.showMessage("Convention '" + convention + "' needs both c(haracters) and d(igits), optionally 'x', and nothing else!");
return null;
}
chars_are_columns = (0 == gd.getNextChoiceIndex());
final double bt_overlap = gd.getNextNumber();
final double lr_overlap = gd.getNextNumber();
final boolean link_images = gd.getNextBoolean();
final boolean stitch_tiles = gd.getNextBoolean();
final boolean homogenize_contrast = gd.getNextBoolean();
// start magic
// get ImageJ-openable files that comply with the convention
final File images_dir = new File(dir);
if (!(images_dir.exists() && images_dir.isDirectory())) {
Utils.showMessage("Something went wrong:\n\tCan't find directory " + dir);
return null;
}
final String[] file_names = images_dir.list(new ImageFileFilter(regex, convention));
if (null == file && file_names.length > 0) {
// the 'selected' file
file = file_names[0];
}
Utils.showStatus("Adding " + file_names.length + " patches.", false);
if (0 == file_names.length) {
Utils.log("Zero files match the convention '" + convention + "'");
return null;
}
// How to: select all files, and order their names in a double array as they should be placed in the Display. Then place them, displacing by offset, and resizing if necessary.
// gather image files:
final Montage montage = new Montage(convention, chars_are_columns);
montage.addAll(file_names);
// an array of Object[] arrays, of unequal length maybe, each containing a column of image file names
final ArrayList<String[]> cols = montage.getCols();
// !@#$%^&*
final String dir_ = dir;
final double bt_overlap_ = bt_overlap;
final double lr_overlap_ = lr_overlap;
final String file_ = file;
return Bureaucrat.createAndStart(new Worker.Task("Insert grid", true) {
@Override
public void exec() {
StitchingTEM.PhaseCorrelationParam pc_param = null;
if (stitch_tiles) {
pc_param = new StitchingTEM.PhaseCorrelationParam();
pc_param.setup(layer);
}
insertGrid(layer, dir_, file_, file_names.length, cols, bx, by, bt_overlap_, lr_overlap_, link_images, stitch_tiles, homogenize_contrast, pc_param, this);
}
}, layer.getProject());
} catch (final Exception e) {
IJError.print(e);
}
return null;
}
use of ini.trakem2.display.Display in project TrakEM2 by trakem2.
the class ProjectTiler method createRetiledSibling.
/**
* Take a {@link Project}, a size for the image tiles, and a target directory,
* and create a new copy of the current project in that folder but with the underlying
* images converted to tiles with a translation-only transform (saved as zipped TIFFs,
* with extension ".tif.zip").
* The new, returned {@link Project} represents the given project but with much
* simpler transformations (just translation) for the images and a defined size for
* the latter, which helps a lot regarding storage space of the XML (and parsing and
* saving time) and performance when browsing layers (keep in mind that, for a 32k x 32k image,
* at 100% zoom one would have to load a 32k x 32k image and render just a tiny bit
* of it). The copied Project preserves the ID of the {@link Layer}s of the original
* {@link Project}, as well as the dimensions; this means the copy is a sibling of
* the original, and it is possible to send segmentations from one to the other "as is"
* (directly, without having to transform along with the images which would not be possible).
*
* Image files are stored as
*
* The non-image objects of the given project are copied into the new project as well.
*
* @param srcProject The project to create a sibling of.
* @param targetDirectory The directory in which to create all the necessary data and mipmap folders for the new Project.
* @param tileWidth The width of the tiles to create for the data of the new project.
* @param tileHeight The height of the tiles.
* @param exportImageType Any of {@link ImagePlus#GRAY8}, {@link ImagePlus#GRAY16} or {@link ImagePlus#COLOR_RGB}, otherwise an {@link IllegalArgumentException} is thrown.
* @param onlyVisibleImages Whether to consider visible images only.
* @param nExportThreads Number of layers to export in parallel. Use a small number when original images are huge (such as larger than 4096 x 4096 pixels).
* @param createMipMaps Whether to generate the mipmaps when done or not.
*
* @throws Exception IllegalArgumentException When {@code exportImageType} is not {@link ImagePlus#GRAY16} or {@link ImagePlus#COLOR_RGB}, or when the directory exists and cannot be written to.
*/
public static final Project createRetiledSibling(final Project srcProject, final String targetDirectory, final int tileWidth, final int tileHeight, final int exportImageType, final boolean onlyVisibleImages, final int nExportThreads, final boolean createMipMaps) throws Exception {
// Validate exportImageType
switch(exportImageType) {
case ImagePlus.GRAY8:
case ImagePlus.GRAY16:
case ImagePlus.COLOR_RGB:
break;
default:
throw new IllegalArgumentException("Can only accept GRAY8, GRAY16 or COLOR_RGB as values for 'exportImageType'!");
}
// Validate targetDirectory
final File fdir = new File(targetDirectory);
if (fdir.exists()) {
if (!fdir.isDirectory() || !fdir.canWrite())
throw new IllegalArgumentException("Invalid directory: not a directory or cannot write to: " + targetDirectory);
} else {
if (!fdir.mkdirs()) {
throw new IllegalArgumentException("Cannot create directory at: " + targetDirectory);
}
}
final String targetDir = Utils.fixDir(targetDirectory);
// Create "data" directory
final String dataDir = new StringBuilder(targetDir).append("data/").toString();
final File fDataDir = new File(dataDir);
if (fDataDir.exists() && (!fDataDir.isDirectory() || !fDataDir.canWrite())) {
throw new IllegalArgumentException("Cannot create or write to 'data' directory in the targetDirectory at: " + targetDir);
} else {
fDataDir.mkdir();
}
// Create new Project, plain, without any automatic creation of a Layer or a Display
final Project newProject = Project.newFSProject("blank", null, targetDir, false);
final LayerSet newLayerSet = newProject.getRootLayerSet();
newLayerSet.setCalibration(srcProject.getRootLayerSet().getCalibrationCopy());
if (!createMipMaps) {
Utils.log("MipMaps are DISABLED:\n --> When done, right-click and choose 'Display - Properties...' and enable mipmaps,\n and then run 'Project - Regenerate all mipmaps'\n");
newProject.getLoader().setMipMapsRegeneration(false);
Utils.log("mipmaps enabled? " + newProject.getLoader().isMipMapsRegenerationEnabled());
}
// Copy the Template Tree of types
newProject.resetRootTemplateThing(srcProject.getRootTemplateThing().clone(newProject, true), null);
for (final TemplateThing tt : newProject.getRootTemplateThing().getUniqueTypes(new HashMap<String, TemplateThing>()).values()) {
newProject.addUniqueType(tt);
}
// Clone layers with the exact same IDs, so that the two projects are siblings at the layer-level:
// (Being siblings allows for treelines, arealists, etc. to be transferred from one to another "as is").
final List<Layer> srcLayers = srcProject.getRootLayerSet().getLayers();
final List<Layer> newLayers = new ArrayList<Layer>();
for (final Layer srcLayer : srcLayers) {
final Layer newLayer = new Layer(newProject, srcLayer.getId(), srcLayer.getZ(), srcLayer.getThickness());
// to update the ID generator in FSLoader
newLayer.addToDatabase();
newLayerSet.add(newLayer);
newLayers.add(newLayer);
newProject.getRootLayerThing().addChild(new LayerThing(newProject.getRootLayerThing().getChildTemplate("layer"), newProject, newLayer));
}
newProject.getLayerTree().rebuild();
// Update the LayerSet
newLayerSet.setDimensions(srcProject.getRootLayerSet().getLayerWidth(), srcProject.getRootLayerSet().getLayerHeight(), LayerSet.NORTHWEST);
Display.updateLayerScroller(newLayerSet);
Display.update(newLayerSet);
// Copy template from the src Project
// (It's done after creating layers so the IDs will not collide with those of the Layers)
newProject.resetRootTemplateThing(srcProject.getRootTemplateThing().clone(newProject, false), null);
// Export tiles as new Patch instances, creating new image files in disk
final int numThreads = Math.max(1, Math.min(nExportThreads, Runtime.getRuntime().availableProcessors()));
int i = 0;
for (final Layer srcLayer : srcLayers) {
Utils.log("Processing layer " + (i + 1) + "/" + srcLayers.size() + " -- " + new Date());
final int layerIndex = i++;
// Create subDirectory
final String dir = dataDir + "/" + layerIndex + "/";
new File(dir).mkdir();
// Create a new Layer with the same Z and thickness
final Layer newLayer = newLayers.get(layerIndex);
// Export layer tiles
final ArrayList<Patch> patches = new ArrayList<Patch>();
if (ImagePlus.GRAY16 == exportImageType) {
Process.progressive(ExportUnsignedShort.exportTiles(srcLayer, tileWidth, tileHeight, onlyVisibleImages), new CountingTaskFactory<Callable<ExportedTile>, Patch>() {
public Patch process(final Callable<ExportedTile> c, final int index) {
try {
// Create the tile
final ExportedTile t = c.call();
// Store the file
final String title = layerIndex + "-" + index;
final String path = dir + title + ".tif.zip";
final ImagePlus imp = new ImagePlus(title, t.sp);
if (!new FileSaver(imp).saveAsZip(path)) {
throw new Exception("Could not save tile: " + path);
}
// Create a Patch
final Patch patch = new Patch(newProject, title, t.x, t.y, imp);
patch.setLocked(true);
newProject.getLoader().addedPatchFrom(path, patch);
return patch;
} catch (Exception e) {
IJError.print(e);
return null;
}
}
}, patches, numThreads);
} else {
// GRAY8 or COLOR_RGB: created from mipmaps
Process.progressive(tileSequence(srcLayer, tileWidth, tileHeight, onlyVisibleImages), new CountingTaskFactory<Rectangle, Patch>() {
@Override
public Patch process(final Rectangle bounds, final int index) {
try {
// Create the tile
final ImagePlus imp = srcLayer.getProject().getLoader().getFlatImage(srcLayer, bounds, 1.0, -1, exportImageType, Patch.class, null, false, Color.black);
final String title = layerIndex + "-" + index;
imp.setTitle(title);
final String path = dir + title + ".tif.zip";
if (!new FileSaver(imp).saveAsZip(path)) {
throw new Exception("Could not save tile: " + path);
}
// Create a Patch
final Patch patch = new Patch(newProject, title, bounds.x, bounds.y, imp);
patch.setLocked(true);
newProject.getLoader().addedPatchFrom(path, patch);
return patch;
} catch (Exception e) {
IJError.print(e);
return null;
}
}
}, patches, numThreads);
}
// Add all Patches to the new Layer
for (final Patch p : patches) {
newLayer.add(p);
}
}
// Copy all segmentations "As is"
final ProjectThing root = srcProject.getRootProjectThing();
if (null != root.getChildren() && !root.getChildren().isEmpty()) {
final ProjectThing source_pt = srcProject.getRootProjectThing().getChildren().get(0);
// "As is"
final int transfer_mode = 0;
final ProjectThing landing_parent = newProject.getRootProjectThing();
srcProject.getProjectTree().rawSendToSiblingProject(source_pt, transfer_mode, newProject, landing_parent);
}
// Copy all floating text labels
i = 0;
for (final Layer srcLayer : srcLayers) {
for (final DLabel srcLabel : srcLayer.getAll(DLabel.class)) {
newLayers.get(i++).add(srcLabel.clone(newProject, false));
}
}
if (createMipMaps) {
final LinkedList<Future<?>> fus = new LinkedList<Future<?>>();
final int batch = Runtime.getRuntime().availableProcessors();
for (final Layer newLayer : newLayers) {
for (final Patch p : newLayer.getAll(Patch.class)) {
fus.add(p.updateMipMaps());
// Don't build-up too much
if (fus.size() > batch * 3) {
while (fus.size() > batch) {
try {
fus.removeFirst().get();
} catch (Exception e) {
IJError.print(e);
}
}
}
}
}
Utils.wait(fus);
}
// Save:
newProject.saveAs(targetDir + "exported.xml", false);
return newProject;
}
use of ini.trakem2.display.Display in project TrakEM2 by trakem2.
the class LayerThing method getPopupItems.
public JMenuItem[] getPopupItems(ActionListener listener) {
JMenuItem item;
ArrayList<JMenuItem> al_items = new ArrayList<JMenuItem>();
JMenu menu = new JMenu("Add...");
ArrayList<TemplateThing> tc = template.getChildren();
if (null != tc) {
for (Iterator<TemplateThing> it = tc.iterator(); it.hasNext(); ) {
// changing underscores for spaces, for the 'layer_set' type to read nice
item = new JMenuItem("new " + it.next().getType().replace('_', ' '));
item.addActionListener(listener);
menu.add(item);
}
if (template.getType().replaceAll("_", " ").equals("layer set")) {
item = new JMenuItem("many new layers...");
item.addActionListener(listener);
menu.add(item);
}
}
if (0 != menu.getItemCount()) {
al_items.add(menu);
}
// Add a "Show" for all except the root LayerSet
if (null != parent) {
item = new JMenuItem("Show");
item.addActionListener(listener);
al_items.add(item);
}
if (template.getType().equals("layer")) {
// adjust z and thickness
item = new JMenuItem("Adjust...");
item.addActionListener(listener);
al_items.add(item);
}
item = new JMenuItem("Rename...");
item.addActionListener(listener);
al_items.add(item);
if (template.getType().replaceAll("_", " ").equals("layer set")) {
if (null != parent) {
item = new JMenuItem("Show centered in Display");
item.addActionListener(listener);
al_items.add(item);
}
item = new JMenuItem("Resize LayerSet...");
item.addActionListener(listener);
al_items.add(item);
LayerSet layer_set = (LayerSet) object;
final boolean empty = 0 == layer_set.getLayers().size();
item = new JMenuItem("Autoresize LayerSet");
item.addActionListener(listener);
al_items.add(item);
if (empty)
item.setEnabled(false);
item = new JMenuItem("Translate layers in Z...");
item.addActionListener(listener);
al_items.add(item);
if (empty)
item.setEnabled(false);
item = new JMenuItem("Reverse layer Z coords...");
item.addActionListener(listener);
al_items.add(item);
if (empty)
item.setEnabled(false);
item = new JMenuItem("Reset layer Z and thickness");
item.addActionListener(listener);
al_items.add(item);
if (empty)
item.setEnabled(false);
item = new JMenuItem("Search...");
item.addActionListener(listener);
al_items.add(item);
if (empty)
item.setEnabled(false);
}
item = new JMenuItem("Import stack...");
// contains layers already, wouldn't know where to put it!
if (template.getType().replaceAll("_", " ").equals("layer set") && 0 != ((LayerSet) object).getLayers().size())
item.setEnabled(false);
item.addActionListener(listener);
al_items.add(item);
if (template.getType().equals("layer")) {
item = new JMenuItem("Import grid...");
item.addActionListener(listener);
al_items.add(item);
item = new JMenuItem("Import sequence as grid...");
item.addActionListener(listener);
al_items.add(item);
item = new JMenuItem("Import from text file...");
item.addActionListener(listener);
al_items.add(item);
}
// add a delete to all except the root LayerSet
if (null != parent) {
al_items.add(new JMenuItem(""));
item = new JMenuItem("Delete...");
item.addActionListener(listener);
al_items.add(item);
}
JMenuItem[] items = new JMenuItem[al_items.size()];
al_items.toArray(items);
return items;
}
use of ini.trakem2.display.Display in project TrakEM2 by trakem2.
the class LayerTree method mousePressed.
public void mousePressed(MouseEvent me) {
Object source = me.getSource();
if (!source.equals(this) || !project.isInputEnabled()) {
return;
}
// ignore if doing multiple selection
if (!Utils.isPopupTrigger(me) && (me.isShiftDown() || (!ij.IJ.isMacOSX() && me.isControlDown()))) {
return;
}
int x = me.getX();
int y = me.getY();
// check if there is a multiple selection
TreePath[] paths = this.getSelectionPaths();
if (null != paths && paths.length > 1) {
if (Utils.isPopupTrigger(me)) {
// check that all items are of the same type
String type_first = ((LayerThing) ((DefaultMutableTreeNode) paths[0].getLastPathComponent()).getUserObject()).getType();
for (int i = 1; i < paths.length; i++) {
String type = ((LayerThing) ((DefaultMutableTreeNode) paths[i].getLastPathComponent()).getUserObject()).getType();
if (!type.equals(type_first)) {
Utils.showMessage("All selected items must be of the same type for operations on multiple items.");
return;
}
}
// prepare popup menu
JPopupMenu popup = new JPopupMenu();
JMenuItem item = null;
if (type_first.equals("layer")) {
item = new JMenuItem("Reverse layer Z coords");
item.addActionListener(this);
popup.add(item);
item = new JMenuItem("Translate layers in Z...");
item.addActionListener(this);
popup.add(item);
item = new JMenuItem("Scale Z and thickness...");
item.addActionListener(this);
popup.add(item);
item = new JMenuItem("Delete...");
item.addActionListener(this);
popup.add(item);
}
if (popup.getSubElements().length > 0) {
popup.show(this, x, y);
}
}
// disable commands depending upon a single node being selected
selected_node = null;
return;
}
// find the node and set it selected
TreePath path = getPathForLocation(x, y);
if (null == path) {
return;
}
setSelectionPath(path);
selected_node = (DefaultMutableTreeNode) path.getLastPathComponent();
if (2 == me.getClickCount() && !Utils.isPopupTrigger(me) && MouseEvent.BUTTON1 == me.getButton()) {
// create a new Display
LayerThing thing = (LayerThing) selected_node.getUserObject();
DBObject ob = (DBObject) thing.getObject();
if (thing.getType().toLowerCase().replace('_', ' ').equals("layer set") && null == ((LayerSet) ob).getParent()) {
// the top level LayerSet
return;
}
// new Display(ob.getProject(), thing.getType().toLowerCase().equals("layer") ? (Layer)ob : ((LayerSet)ob).getParent());
Display.createDisplay(ob.getProject(), thing.getType().toLowerCase().equals("layer") ? (Layer) ob : ((LayerSet) ob).getParent());
return;
} else if (Utils.isPopupTrigger(me)) {
JPopupMenu popup = getPopupMenu(selected_node);
if (null == popup)
return;
popup.show(this, x, y);
return;
}
}
Aggregations