use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class ImageJ method loadCursors.
@AstroImageJ(reason = "Use astronomy_icon", modified = true)
private void loadCursors() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
String path = Prefs.getImageJDir() + "/astronomy_icon.gif";
File f = new File(path);
if (!f.exists())
return;
// Image image = toolkit.getImage(path);
ImageIcon icon = new ImageIcon(path);
Image image = icon.getImage();
if (image == null)
return;
int width = icon.getIconWidth();
int height = icon.getIconHeight();
Point hotSpot = new Point(width / 2, height / 2);
Cursor crosshairCursor = toolkit.createCustomCursor(image, hotSpot, "crosshair-cursor.gif");
ImageCanvas.setCursor(crosshairCursor, 0);
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class FolderOpener method showDialog.
@AstroImageJ(reason = "Save preference option to open as virtual stack; widen access; support zip files as folder;" + "Add filter count", modified = true)
public boolean showDialog() {
String options = Macro.getOptions();
if (options != null) {
// macro
String optionsOrig = options;
options = options.replace("open=", "dir=");
options = options.replace("file=", "filter=");
options = options.replace("starting=", "start=");
options = options.replace("number=", "count=");
options = options.replace("increment=", "step=");
options = options.replace("inc=", "step=");
if (!options.equals(optionsOrig))
Macro.setOptions(options);
if (options.contains("convert_to_rgb"))
this.bitDepth = 24;
}
String countStr = "---";
if (directorySet) {
File f = new File(directory);
String[] names = f.list();
// Zip as folder
if (names == null || names.length == 0) {
names = ZipOpenerUtil.getFilePathsInZip(directory);
}
// End zip as folder
names = (new FolderOpener()).trimFileList(names);
countStr = "" + names.length;
} else
directory = Prefs.get(DIR_KEY, IJ.getDir("downloads") + "stack/");
GenericDialog gd = new GenericDialog("Import Image Sequence");
gd.setInsets(5, 0, 0);
gd.addDirectoryField("Dir:", directory);
gd.setInsets(2, 55, 10);
gd.addMessage("drag and drop target", IJ.font10, Color.darkGray);
gd.addChoice("Type:", types, bitDepthToType(bitDepth));
gd.addStringField("Filter:", "", 10);
gd.setInsets(0, 55, 0);
gd.addMessage("enclose regex in parens", IJ.font10, Color.darkGray);
gd.addNumericField("Start:", this.start, 0, 6, "");
gd.addStringField("Count:", countStr, 6);
gd.addNumericField("Step:", this.step, 0, 6, "");
gd.addNumericField("Scale:", this.scale, 0, 6, "%");
gd.addCheckbox("Sort names numerically", sortFileNames);
gd.addCheckbox("Use virtual stack", Prefs.get("folderopener.openAsVirtualStack", openAsVirtualStack));
gd.addCheckbox("Open as separate images", false);
gd.addHelp(IJ.URL + "/docs/menus/file.html#seq1");
// Add display of stack size
var initialSizes = getFileCount(gd);
gd.addMessage("Matched files: " + initialSizes.first());
var filterCountDisplay = (Label) gd.getComponent(gd.getComponentCount() - 1);
gd.addMessage("Estimated stack size: " + initialSizes.second() + " MB");
var filterSizeDisplay = (Label) gd.getComponent(gd.getComponentCount() - 1);
for (Object stringField : gd.getStringFields()) {
((TextField) stringField).addTextListener($ -> {
filterCountDisplay.setText("Matched files: " + 0);
filterSizeDisplay.setText("Estimated stack size: " + 0 + " MB");
var x = getFileCount(gd);
filterCountDisplay.setText("Matched files: " + x.first());
filterSizeDisplay.setText("Estimated stack size: " + IJ.d2s(x.second(), 1) + " MB");
});
}
// End display stack size
gd.showDialog();
if (gd.wasCanceled())
return false;
directory = gd.getNextString();
Prefs.set(DIR_KEY, directory);
gd.setSmartRecording(true);
int index = gd.getNextChoiceIndex();
bitDepth = typeToBitDepth(types[index]);
filter = gd.getNextString();
if (legacyRegex != null)
filter = "(" + legacyRegex + ")";
gd.setSmartRecording(true);
this.start = (int) gd.getNextNumber();
countStr = gd.getNextString();
double count = Tools.parseDouble(countStr);
if (!Double.isNaN(count))
nFiles = (int) count;
this.step = (int) gd.getNextNumber();
if (this.step < 1)
this.step = 1;
this.scale = gd.getNextNumber();
if (this.scale < 5.0)
this.scale = 5.0;
if (this.scale > 100.0)
this.scale = 100.0;
sortFileNames = gd.getNextBoolean();
if (!sortFileNames)
sortByMetaData = false;
openAsVirtualStack = gd.getNextBoolean();
Prefs.set("folderopener.openAsVirtualStack", openAsVirtualStack);
if (openAsVirtualStack)
scale = 100.0;
openAsSeparateImages = gd.getNextBoolean();
if (openAsSeparateImages)
openAsVirtualStack = true;
if (!IJ.macroRunning()) {
staticSortFileNames = sortFileNames;
if (!openAsSeparateImages)
staticOpenAsVirtualStack = openAsVirtualStack;
}
virtualIntended = openAsVirtualStack;
return true;
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class FolderOpener method run.
@AstroImageJ(reason = "When opening images that individually go to a stack, preserve stack title. This allows" + " MultiAperture to run on a folder of 3D fits images, otherwise WCS and other information is lost;" + " If filter fails to match any files, after closing the error reopen dialog.", modified = true)
public void run(String arg) {
boolean isMacro = Macro.getOptions() != null;
if (!directorySet)
directory = null;
if (arg != null && !arg.equals("")) {
directory = arg;
} else {
if (!isMacro) {
sortFileNames = staticSortFileNames;
openAsVirtualStack = staticOpenAsVirtualStack;
}
arg = null;
String title = "Open Image Sequence...";
String macroOptions = Macro.getOptions();
if (macroOptions != null) {
directory = Macro.getValue(macroOptions, title, null);
if (directory != null) {
directory = OpenDialog.lookupPathVariable(directory);
File f = new File(directory);
if (!f.isDirectory() && (f.exists() || directory.lastIndexOf(".") > directory.length() - 5))
directory = f.getParent();
}
legacyRegex = Macro.getValue(macroOptions, "or", "");
if (legacyRegex.equals(""))
legacyRegex = null;
}
}
if (arg == null) {
if (!showDialog())
return;
}
File file = new File(directory);
String[] list = file.list();
// Zip as folder
if (list == null) {
list = ZipOpenerUtil.getFilePathsInZip(directory);
if (list.length == 0)
list = null;
}
if (list == null) {
String parent = file.getParent();
file = new File(parent);
list = file.list();
if (list != null)
directory = parent;
else {
IJ.error("File>Import>Image Sequence", "Directory not found: " + directory);
return;
}
}
// remove subdirectories from list
ArrayList fileList = new ArrayList();
for (int i = 0; i < list.length; i++) {
File f = (new File(directory + list[i]));
if (!f.isDirectory())
fileList.add(list[i]);
}
if (fileList.size() < list.length)
list = (String[]) fileList.toArray(new String[fileList.size()]);
String title = directory;
if (title.endsWith(File.separator) || title.endsWith("/"))
title = title.substring(0, title.length() - 1);
int index = title.lastIndexOf(File.separatorChar);
if (index != -1)
title = title.substring(index + 1);
else {
index = title.lastIndexOf("/");
if (index != -1)
title = title.substring(index + 1);
}
if (title.endsWith(":"))
title = title.substring(0, title.length() - 1);
list = trimFileList(list);
if (list == null)
return;
if (IJ.debugMode)
IJ.log("FolderOpener: " + directory + " (" + list.length + " files)");
int width = 0, height = 0, stackSize = 1;
ImageStack stack = null;
double min = Double.MAX_VALUE;
double max = -Double.MAX_VALUE;
Calibration cal = null;
boolean allSameCalibration = true;
IJ.resetEscape();
Overlay overlay = null;
if (this.nFiles == 0)
this.nFiles = list.length;
boolean dicomImages = false;
try {
for (int i = 0; i < list.length; i++) {
Opener opener = new Opener();
opener.setSilentMode(true);
IJ.redirectErrorMessages(true);
ImagePlus imp = opener.openImage(directory, list[i]);
IJ.redirectErrorMessages(false);
if (imp != null) {
width = imp.getWidth();
height = imp.getHeight();
if (this.bitDepth == 0) {
this.bitDepth = imp.getBitDepth();
this.defaultBitDepth = bitDepth;
}
String info = (String) imp.getProperty("Info");
if (info != null && info.contains("7FE0,0010"))
dicomImages = true;
break;
}
}
if (width == 0) {
IJ.error("Sequence Reader", "This folder does not appear to contain\n" + "any TIFF, JPEG, BMP, DICOM, GIF, FITS or PGM files.\n \n" + " \"" + directory + "\"");
return;
}
String pluginName = "Sequence Reader";
if (legacyRegex != null)
pluginName += "(legacy)";
list = getFilteredList(list, filter, pluginName);
if (list == null) {
// Reopen dialog if filter fails
run(arg);
return;
}
IJ.showStatus("");
t0 = System.currentTimeMillis();
if (sortFileNames || dicomImages || IJ.isMacOSX())
list = StringSorter.sortNumerically(list);
if (this.nFiles < 1)
this.nFiles = list.length;
if (this.start < 1 || this.start > list.length)
this.start = 1;
if (this.start + this.nFiles - 1 > list.length)
this.nFiles = list.length - this.start + 1;
int count = 0;
int counter = 0;
ImagePlus imp = null;
boolean firstMessage = true;
boolean fileInfoStack = false;
// open images as stack
for (int i = this.start - 1; i < list.length; i++) {
if ((counter++ % this.step) != 0)
continue;
Opener opener = new Opener();
opener.setSilentMode(true);
IJ.redirectErrorMessages(true);
if ("RoiSet.zip".equals(list[i])) {
IJ.open(directory + list[i]);
imp = null;
} else if (!openAsVirtualStack || stack == null) {
imp = opener.openImage(directory, list[i]);
stackSize = imp != null ? imp.getStackSize() : 1;
}
IJ.redirectErrorMessages(false);
if (imp != null && stack == null) {
width = imp.getWidth();
height = imp.getHeight();
if (stackWidth > 0 && stackHeight > 0) {
width = stackWidth;
height = stackHeight;
}
if (bitDepth == 0)
bitDepth = imp.getBitDepth();
fi = imp.getOriginalFileInfo();
ImageProcessor ip = imp.getProcessor();
min = ip.getMin();
max = ip.getMax();
cal = imp.getCalibration();
if (convertToRGB)
bitDepth = 24;
ColorModel cm = imp.getProcessor().getColorModel();
if (openAsVirtualStack) {
if (stackSize > 1) {
stack = new FileInfoVirtualStack();
fileInfoStack = true;
} else {
if (stackWidth > 0 && stackHeight > 0)
stack = new VirtualStack(stackWidth, stackHeight, cm, directory);
else
stack = new VirtualStack(width, height, cm, directory);
}
} else if (this.scale < 100.0)
stack = new ImageStack((int) (width * this.scale / 100.0), (int) (height * this.scale / 100.0), cm);
else
stack = new ImageStack(width, height, cm);
if (bitDepth != 0)
stack.setBitDepth(bitDepth);
info1 = (String) imp.getProperty("Info");
}
if (imp == null)
continue;
if (imp.getWidth() != width || imp.getHeight() != height) {
if (stackWidth > 0 && stackHeight > 0) {
ImagePlus imp2 = imp.createImagePlus();
ImageProcessor ip = imp.getProcessor();
ImageProcessor ip2 = ip.createProcessor(width, height);
ip2.insert(ip, 0, 0);
imp2.setProcessor(ip2);
imp = imp2;
} else {
IJ.log(list[i] + ": wrong size; " + width + "x" + height + " expected, " + imp.getWidth() + "x" + imp.getHeight() + " found");
continue;
}
}
String label = imp.getTitle();
if (stackSize == 1) {
String info = (String) imp.getProperty("Info");
if (info != null) {
if (useInfo(info))
label += "\n" + info;
} else if (imp.getStackSize() > 0) {
String sliceLabel = imp.getStack().getSliceLabel(1);
if (useInfo(sliceLabel))
label = sliceLabel;
}
}
if (Math.abs(imp.getCalibration().pixelWidth - cal.pixelWidth) > 0.0000000001)
allSameCalibration = false;
ImageStack inputStack = imp.getStack();
Overlay overlay2 = imp.getOverlay();
if (overlay2 != null && !openAsVirtualStack) {
if (overlay == null)
overlay = new Overlay();
for (int j = 0; j < overlay2.size(); j++) {
Roi roi = overlay2.get(j);
int position = roi.getPosition();
if (position == 0)
roi.setPosition(count + 1);
overlay.add(roi);
}
}
if (openAsVirtualStack) {
if (fileInfoStack)
openAsFileInfoStack((FileInfoVirtualStack) stack, directory + list[i]);
else
((VirtualStack) stack).addSlice(list[i]);
} else {
for (int slice = 1; slice <= stackSize; slice++) {
int bitDepth2 = imp.getBitDepth();
String label2 = label;
ImageProcessor ip = null;
if (stackSize > 1) {
String sliceLabel = inputStack.getSliceLabel(slice);
label2 = "Image " + (i + 1) + " : " + sliceLabel;
}
ip = inputStack.getProcessor(slice);
if (convertToRGB) {
ip = ip.convertToRGB();
bitDepth2 = 24;
}
if (bitDepth2 != bitDepth) {
if (dicomImages && bitDepth == 16 && bitDepth2 == 32 && this.scale == 100) {
ip = ip.convertToFloat();
bitDepth = 32;
ImageStack stack2 = new ImageStack(width, height, stack.getColorModel());
for (int n = 1; n <= stack.size(); n++) {
ImageProcessor ip2 = stack.getProcessor(n);
ip2 = ip2.convertToFloat();
ip2.subtract(32768);
String sliceLabel = stack.getSliceLabel(n);
stack2.addSlice(sliceLabel, ip2.convertToFloat());
}
stack = stack2;
}
}
if (this.scale < 100.0)
ip = ip.resize((int) (width * this.scale / 100.0), (int) (height * this.scale / 100.0));
if (ip.getMin() < min)
min = ip.getMin();
if (ip.getMax() > max)
max = ip.getMax();
stack.addSlice(label2, ip);
}
}
count++;
IJ.showStatus("!" + count + "/" + this.nFiles);
IJ.showProgress(count, this.nFiles);
if (count >= this.nFiles)
break;
if (IJ.escapePressed()) {
IJ.beep();
break;
}
}
// open images as stack
} catch (OutOfMemoryError e) {
IJ.outOfMemory("FolderOpener");
if (stack != null)
stack.trim();
}
if (stack != null && stack.size() > 0) {
ImagePlus imp2 = new ImagePlus(title, stack);
if (imp2.getType() == ImagePlus.GRAY16 || imp2.getType() == ImagePlus.GRAY32)
imp2.getProcessor().setMinAndMax(min, max);
if (fi == null)
fi = new FileInfo();
fi.fileFormat = FileInfo.UNKNOWN;
fi.fileName = "";
fi.directory = directory;
// saves FileInfo of the first image
imp2.setFileInfo(fi);
imp2.setOverlay(overlay);
if (stack instanceof VirtualStack) {
Properties props = ((VirtualStack) stack).getProperties();
if (props != null)
imp2.setProperty("FHT", props.get("FHT"));
}
if (allSameCalibration) {
// use calibration from first image
if (this.scale != 100.0 && cal.scaled()) {
cal.pixelWidth /= this.scale / 100.0;
cal.pixelHeight /= this.scale / 100.0;
}
if (cal.pixelWidth != 1.0 && cal.pixelDepth == 1.0)
cal.pixelDepth = cal.pixelWidth;
imp2.setCalibration(cal);
}
if (info1 != null && info1.lastIndexOf("7FE0,0010") > 0) {
// DICOM
if (sortByMetaData)
stack = DicomTools.sort(stack);
imp2.setStack(stack);
double voxelDepth = DicomTools.getVoxelDepth(stack);
if (voxelDepth > 0.0) {
if (IJ.debugMode)
IJ.log("DICOM voxel depth set to " + voxelDepth + " (" + cal.pixelDepth + ")");
cal.pixelDepth = voxelDepth;
imp2.setCalibration(cal);
}
if (imp2.getType() == ImagePlus.GRAY16 || imp2.getType() == ImagePlus.GRAY32) {
imp2.getProcessor().setMinAndMax(min, max);
imp2.updateAndDraw();
}
}
if (imp2.getStackSize() == 1) {
imp2.setProperty("Label", list[0]);
if (info1 != null)
imp2.setProperty("Info", info1);
}
if (arg == null && !saveImage) {
String time = (System.currentTimeMillis() - t0) / 1000.0 + " seconds";
if (openAsSeparateImages && imp2.getStackSize() <= MAX_SEPARATE)
openAsSeparateImages(imp2);
else
imp2.show(time);
if (stack.isVirtual()) {
overlay = stack.getProcessor(1).getOverlay();
if (overlay != null)
imp2.setOverlay(overlay);
}
}
if (saveImage)
image = imp2;
if (openAsSeparateImages && imp2.getStackSize() > MAX_SEPARATE)
IJ.error("Import>Image Sequence", "A maximum of " + MAX_SEPARATE + " images can be opened separately.");
}
IJ.showProgress(1.0);
if (Recorder.record) {
String options = openAsVirtualStack ? "virtual" : "";
if (bitDepth != defaultBitDepth)
options = options + " bitdepth=" + bitDepth;
if (filter != null && filter.length() > 0) {
if (filter.contains(" "))
filter = "[" + filter + "]";
options = options + " filter=" + filter;
}
if (start != 1)
options = options + " start=" + start;
if (step != 1)
options = options + " step=" + step;
if (scale != 100)
options = options + " scale=" + scale;
if (!sortByMetaData)
options = options + " noMetaSort";
String dir = Recorder.fixPath(directory);
Recorder.recordCall("imp = FolderOpener.open(\"" + dir + "\", \"" + options + "\");");
}
virtualIntended = false;
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class CompositeConverter method run.
@AstroImageJ(reason = "Run Channels plugin", modified = true)
public void run(String arg) {
String[] modes = { "Composite", "Color", "Grayscale" };
ImagePlus imp = IJ.getImage();
if (imp.isComposite()) {
CompositeImage ci = (CompositeImage) imp;
if (ci.getMode() != IJ.COMPOSITE) {
ci.setMode(IJ.COMPOSITE);
ci.updateAndDraw();
}
return;
}
String mode = modes[0];
int z = imp.getStackSize();
int c = imp.getNChannels();
if (c == 1) {
c = z;
imp.setDimensions(c, 1, 1);
if (c > 7)
mode = modes[2];
}
if (imp.getBitDepth() == 24) {
ImageWindow win = imp.getWindow();
Point loc = win != null ? win.getLocation() : null;
int slice = imp.getCurrentSlice();
ImagePlus imp2 = makeComposite(imp);
if (loc != null)
ImageWindow.setNextLocation(loc);
imp2.show();
imp.changes = false;
if (z == 1) {
imp.hide();
WindowManager.setCurrentWindow(imp2.getWindow());
} else {
if (arg != null && arg.equals("color"))
((CompositeImage) imp2).setMode(IJ.COLOR);
imp2.setZ(slice);
imp.close();
}
if (!IJ.isMacro())
((Channels) IJ.runPlugIn("ij.plugin.frame.Channels", arg)).requestFocus();
if (IJ.isMacro() && !Interpreter.isBatchMode())
IJ.wait(500);
} else if (c >= 2 || (IJ.macroRunning() && c >= 1)) {
GenericDialog gd = new GenericDialog("Make Composite");
gd.addChoice("Display Mode:", modes, mode);
gd.showDialog();
if (gd.wasCanceled())
return;
int index = gd.getNextChoiceIndex();
CompositeImage ci = new CompositeImage(imp, index + 1);
if (imp.getBitDepth() != 8) {
ci.reset();
ci.resetDisplayRanges();
}
ImageWindow win = imp.getWindow();
Point location = win != null ? win.getLocation() : null;
imp.hide();
if (location != null)
ImageWindow.setNextLocation(location);
if (IJ.isMacro())
IJ.wait(250);
ci.show();
ci.getWindow().toBack();
IJ.runPlugIn("ij.plugin.frame.Channels", arg);
} else
IJ.error("To create a composite, the current image must be\n a stack with at least 2 channels or be in RGB format.");
}
use of ij.astro.AstroImageJ in project astroimagej by keastrid.
the class AboutBox method run.
@AstroImageJ(modified = true, reason = "Adding AIJ version and authors to about page; fixing class loading issue.")
public void run(String arg) {
System.gc();
int lines = 13;
String[] text = new String[lines];
text[0] = "ImageJ " + ImageJ.VERSION + ImageJ.BUILD;
text[1] = "Wayne Rasband and contributors";
text[2] = "National Institutes of Health, USA";
text[3] = IJ.URL;
text[4] = "Java " + System.getProperty("java.version") + (IJ.is64Bit() ? " (64-bit)" : " (32-bit)");
text[5] = IJ.freeMemory();
text[6] = "ImageJ is in the public domain";
text[7] = "════════════════════════════════";
text[8] = "AstroImageJ " + IJ.getAstroVersion();
text[9] = "Karen Collins (Smithsonian Astrophysical Observatory)";
text[10] = "Kevin Eastridge (George Mason University)";
text[11] = "John Kielkopf (University of Louisville)";
text[12] = "AstroImageJ is Licensed under GPL 3.0";
ImageProcessor ip = null;
ImageJ ij = IJ.getInstance();
URL url = ij.getClass().getClassLoader().getResource("about.jpg");
if (url != null) {
Image img = null;
try {
img = ij.createImage((ImageProducer) url.getContent());
} catch (Exception e) {
}
if (img != null) {
ImagePlus sImp = new ImagePlus("", img);
ip = sImp.getProcessor();
}
}
if (ip == null)
ip = new ColorProcessor(55, 45);
ip = ip.resize(ip.getWidth() * 6, ip.getHeight() * 6);
ImagePlus imp = new ImagePlus("About AstroImageJ", ip);
int width = imp.getWidth();
Overlay overlay = new Overlay();
Font font = new Font("SansSerif", Font.PLAIN, LARGE_FONT);
int y = 50;
int xcenter = 350;
add(text[0], xcenter, y, font, TextRoi.CENTER, overlay);
font = new Font("SansSerif", Font.PLAIN, SMALL_FONT);
y += 37;
add(text[1], xcenter, y, font, TextRoi.CENTER, overlay);
y += 27;
add(text[2], xcenter, y, font, TextRoi.CENTER, overlay);
y += 27;
add(text[3], xcenter, y, font, TextRoi.CENTER, overlay);
y += 27;
add(text[4], xcenter, y, font, TextRoi.CENTER, overlay);
y += 27;
add(text[5], xcenter, y, font, TextRoi.CENTER, overlay);
y += 27;
add(text[6], xcenter, y, font, TextRoi.CENTER, overlay);
y += 27;
add(text[7], xcenter, y, font, TextRoi.CENTER, overlay);
font = new Font("SansSerif", Font.PLAIN, LARGE_FONT);
y += 40;
add(text[8], xcenter, y, font, TextRoi.CENTER, overlay);
font = new Font("SansSerif", Font.PLAIN, SMALL_FONT);
y += 37;
add(text[9], xcenter, y, font, TextRoi.CENTER, overlay);
if (IJ.maxMemory() > 0L) {
y += 27;
add(text[10], xcenter, y, font, TextRoi.CENTER, overlay);
}
y += 27;
add(text[11], xcenter, y, font, TextRoi.CENTER, overlay);
y += 33;
add(text[12], xcenter, y, font, TextRoi.CENTER, overlay);
imp.setOverlay(overlay);
ImageWindow.centerNextImage();
imp.show();
}
Aggregations