use of ij.VirtualStack in project TrakEM2 by trakem2.
the class FSLoader method importStackAsPatches.
/**
* Returns the last Patch.
*/
protected Patch importStackAsPatches(final Project project, final Layer first_layer, final double x, final double y, final ImagePlus imp_stack, final boolean as_copy, final String filepath) {
Utils.log2("FSLoader.importStackAsPatches filepath=" + filepath);
String target_dir = null;
if (as_copy) {
DirectoryChooser dc = new DirectoryChooser("Folder to save images");
target_dir = dc.getDirectory();
// user canceled dialog
if (null == target_dir)
return null;
if (IJ.isWindows())
target_dir = target_dir.replace('\\', '/');
if (target_dir.length() - 1 != target_dir.lastIndexOf('/')) {
target_dir += "/";
}
}
// Double.MAX_VALUE is a flag to indicate "add centered"
double pos_x = Double.MAX_VALUE != x ? x : first_layer.getLayerWidth() / 2 - imp_stack.getWidth() / 2;
double pos_y = Double.MAX_VALUE != y ? y : first_layer.getLayerHeight() / 2 - imp_stack.getHeight() / 2;
final double thickness = first_layer.getThickness();
final String title = Utils.removeExtension(imp_stack.getTitle()).replace(' ', '_');
Utils.showProgress(0);
Patch previous_patch = null;
final int n = imp_stack.getStackSize();
final ImageStack stack = imp_stack.getStack();
final boolean virtual = stack.isVirtual();
final VirtualStack vs = virtual ? (VirtualStack) stack : null;
for (int i = 1; i <= n; i++) {
Layer layer = first_layer;
double z = first_layer.getZ() + (i - 1) * thickness;
// will create new layer if not found
if (i > 1)
layer = first_layer.getParent().getLayer(z, thickness, true);
if (null == layer) {
Utils.log("Display.importStack: could not create new layers.");
return null;
}
String patch_path = null;
ImagePlus imp_patch_i = null;
if (virtual) {
// because we love inefficiency, every time all this is done again
// VirtualStack vs = (VirtualStack)imp_stack.getStack();
String vs_dir = vs.getDirectory().replace('\\', '/');
if (!vs_dir.endsWith("/"))
vs_dir += "/";
String iname = vs.getFileName(i);
patch_path = vs_dir + iname;
Utils.log2("virtual stack: patch path is " + patch_path);
releaseToFit(new File(patch_path).length() * 3);
Utils.log2(i + " : " + patch_path);
imp_patch_i = openImage(patch_path);
} else {
ImageProcessor ip = stack.getProcessor(i);
if (as_copy)
ip = ip.duplicate();
imp_patch_i = new ImagePlus(title + "__slice=" + i, ip);
}
String label = stack.getSliceLabel(i);
if (null == label)
label = "";
Patch patch = null;
if (as_copy) {
patch_path = target_dir + cleanSlashes(imp_patch_i.getTitle()) + ".zip";
ini.trakem2.io.ImageSaver.saveAsZip(imp_patch_i, patch_path);
patch = new Patch(project, label + " " + title + " " + i, pos_x, pos_y, imp_patch_i);
} else if (virtual) {
patch = new Patch(project, label, pos_x, pos_y, imp_patch_i);
} else {
patch_path = filepath + "-----#slice=" + i;
// Utils.log2("path is "+ patch_path);
final AffineTransform atp = new AffineTransform();
atp.translate(pos_x, pos_y);
patch = new Patch(project, getNextId(), label + " " + title + " " + i, imp_stack.getWidth(), imp_stack.getHeight(), imp_stack.getWidth(), imp_stack.getHeight(), imp_stack.getType(), false, imp_stack.getProcessor().getMin(), imp_stack.getProcessor().getMax(), atp);
patch.addToDatabase();
// Utils.log2("type is " + imp_stack.getType());
}
Utils.log2("B: " + i + " : " + patch_path);
addedPatchFrom(patch_path, patch);
if (!as_copy && !virtual) {
if (// each slice separately
virtual)
// each slice separately
cache(patch, imp_patch_i);
else
// uses the entire stack, shared among all Patch instances
cache(patch, imp_stack);
}
// submit for regeneration
if (isMipMapsRegenerationEnabled())
regenerateMipMaps(patch);
if (null != previous_patch)
patch.link(previous_patch);
layer.add(patch);
previous_patch = patch;
Utils.showProgress(i * (1.0 / n));
}
Utils.showProgress(1.0);
// return the last patch
return previous_patch;
}
use of ij.VirtualStack in project TrakEM2 by trakem2.
the class DNDInsertImage method importImageFile.
private boolean importImageFile(File f, String path, Point point) throws Exception {
if (f.exists()) {
final Layer layer = display.getLayer();
Bureaucrat burro = null;
if (f.isDirectory()) {
// ask:
GenericDialog gd = new GenericDialog("Import directory");
String[] choice = new String[] { "Stack", "Grid", "Sequence as grid" };
gd.addChoice("Directory as: ", choice, choice[0]);
gd.showDialog();
if (gd.wasCanceled()) {
// the user cancel it, so all is ok.
return true;
}
display.getLayerSet().addLayerContentStep(layer);
switch(gd.getNextChoiceIndex()) {
case // as stack
0:
// if importing image sequence as a stack:
// don't filter by name "^[^\\.].*[\\.][a-zA-Z1-9_]{3,4}$"
String[] names = f.list(new ImageFileFilter());
Utils.log2("stack size: " + names.length);
for (int i = 0; i < names.length; i++) {
Utils.log2(names[i]);
}
Arrays.sort(names);
// I don't care about the dimensions
VirtualStack stack = new VirtualStack(10, 10, null, f.getAbsolutePath().replace('\\', '/'));
for (int k = 0; k < names.length; k++) {
IJ.redirectErrorMessages();
// ignore trakem2 files
if (names[k].toLowerCase().endsWith(".xml"))
continue;
stack.addSlice(names[k]);
}
if (stack.getSize() > 0) {
burro = display.getProject().getLoader().importStack(layer, point.x, point.y, new ImagePlus("stack", stack), true, path, false);
}
break;
case // as grid
1:
burro = display.getProject().getLoader().importGrid(layer, path);
break;
case // sequence as grid
2:
burro = display.getProject().getLoader().importSequenceAsGrid(layer, path);
break;
}
} else {
layer.getParent().addLayerContentStep(layer);
// single image file (single image or a stack)
burro = display.getProject().getLoader().importImage(layer, point.x, point.y, path, false);
}
if (null != burro) {
burro.addPostTask(new Runnable() {
public void run() {
// The current state
layer.getParent().addLayerContentStep(layer);
}
});
}
return true;
} else {
Utils.log("File not found: " + path);
return false;
}
}
use of ij.VirtualStack in project TrakEM2 by trakem2.
the class Loader method createFlyThrough.
/**
* Returns an ImageStack, one slice per region.
*/
public <I> ImagePlus createFlyThrough(final List<? extends Region<I>> regions, final double magnification, final int type, final String dir) {
final ExecutorService ex = Utils.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), "fly-through");
final List<Future<ImagePlus>> fus = new ArrayList<Future<ImagePlus>>();
for (final Region<I> r : regions) {
fus.add(ex.submit(new Callable<ImagePlus>() {
@Override
public ImagePlus call() {
return getFlatImage(r.layer, r.r, magnification, 0xffffffff, type, Displayable.class, null, true, Color.black);
}
}));
}
final Region<I> r = regions.get(0);
final int w = (int) (r.r.width * magnification), h = (int) (r.r.height * magnification), size = regions.size();
final ImageStack stack = null == dir ? new ImageStack(w, h) : new VirtualStack(w, h, null, dir);
final int digits = Integer.toString(size).length();
for (int i = 0; i < size; i++) {
try {
if (Thread.currentThread().isInterrupted()) {
ex.shutdownNow();
return null;
}
if (null == dir) {
stack.addSlice(regions.get(i).layer.toString(), fus.get(i).get().getProcessor());
} else {
final StringBuilder name = new StringBuilder().append(i);
while (name.length() < digits) name.insert(0, '0');
name.append(".png");
final String filename = name.toString();
new FileSaver(fus.get(i).get()).saveAsPng(dir + filename);
((VirtualStack) stack).addSlice(filename);
}
} catch (final Throwable t) {
IJError.print(t);
}
}
ex.shutdown();
return new ImagePlus("Fly-Through", stack);
}
Aggregations