/** Called from io/Opener.java. */ public void run(String path) { if (path.equals("")) return; File theFile = new File(path); String directory = theFile.getParent(); String fileName = theFile.getName(); if (directory == null) directory = ""; // Try and recognise file type and load the file if recognised ImagePlus imp = openImage(directory, fileName, path); if (imp == null) { IJ.showStatus(""); return; // failed to load file or plugin has opened and displayed it } ImageStack stack = imp.getStack(); // get the title from the stack (falling back to the fileName) String title = imp.getTitle().equals("") ? fileName : imp.getTitle(); // set the stack of this HandleExtraFileTypes object // to that attached to the ImagePlus object returned by openImage() setStack(title, stack); // copy over the calibration info since it doesn't come with the ImageProcessor setCalibration(imp.getCalibration()); // also copy the Show Info field over if it exists if (imp.getProperty("Info") != null) setProperty("Info", imp.getProperty("Info")); // copy the FileInfo setFileInfo(imp.getOriginalFileInfo()); // copy dimensions if (IJ.getVersion().compareTo("1.38s") >= 0) setDimensions(imp.getNChannels(), imp.getNSlices(), imp.getNFrames()); if (IJ.getVersion().compareTo("1.41o") >= 0) setOpenAsHyperStack(imp.getOpenAsHyperStack()); }
private void openDirectory(File f, String path) { if (path == null) return; if (!(path.endsWith(File.separator) || path.endsWith("/"))) path += File.separator; String[] names = f.list(); names = (new FolderOpener()).trimFileList(names); if (names == null) return; String msg = "Open all " + names.length + " images in \"" + f.getName() + "\" as a stack?"; GenericDialog gd = new GenericDialog("Open Folder"); gd.setInsets(10, 5, 0); gd.addMessage(msg); gd.setInsets(15, 35, 0); gd.addCheckbox("Convert to RGB", convertToRGB); gd.setInsets(0, 35, 0); gd.addCheckbox("Use Virtual Stack", virtualStack); gd.enableYesNoCancel(); gd.showDialog(); if (gd.wasCanceled()) return; if (gd.wasOKed()) { convertToRGB = gd.getNextBoolean(); virtualStack = gd.getNextBoolean(); String options = " sort"; if (convertToRGB) options += " convert_to_rgb"; if (virtualStack) options += " use"; IJ.run("Image Sequence...", "open=[" + path + "]" + options); DirectoryChooser.setDefaultDirectory(path); } else { for (int k = 0; k < names.length; k++) { IJ.redirectErrorMessages(); if (!names[k].startsWith(".")) (new Opener()).open(path + names[k]); } } IJ.register(DragAndDrop.class); }
byte[] download(File f) { if (!f.exists()) { IJ.error("Plugin Installer", "File not found: " + f); return null; } byte[] data = null; try { int len = (int) f.length(); InputStream in = new BufferedInputStream(new FileInputStream(f)); DataInputStream dis = new DataInputStream(in); data = new byte[len]; dis.readFully(data); dis.close(); } catch (Exception e) { IJ.error("Plugin Installer", "" + e); data = null; } return data; }
/** Returns an InputStream for the image described by this FileInfo. */ public InputStream createInputStream(FileInfo fi) throws IOException, MalformedURLException { InputStream is = null; boolean gzip = fi.fileName != null && (fi.fileName.endsWith(".gz") || fi.fileName.endsWith(".GZ")); if (fi.inputStream != null) is = fi.inputStream; else if (fi.url != null && !fi.url.equals("")) is = new URL(fi.url + fi.fileName).openStream(); else { if (fi.directory.length() > 0 && !fi.directory.endsWith(Prefs.separator)) fi.directory += Prefs.separator; File f = new File(fi.directory + fi.fileName); if (gzip) fi.compression = FileInfo.COMPRESSION_UNKNOWN; if (f == null || f.isDirectory() || !validateFileInfo(f, fi)) is = null; else is = new FileInputStream(f); } if (is != null) { if (fi.compression >= FileInfo.LZW) is = new RandomAccessStream(is); else if (gzip) is = new GZIPInputStream(is, 50000); } return is; }
/** * Open a file. If it's a directory, ask to open all images as a sequence in a stack or * individually. */ public void openFile(File f) { if (IJ.debugMode) IJ.log("DragAndDrop.openFile: " + f); try { if (null == f) return; String path = f.getCanonicalPath(); if (f.exists()) { if (f.isDirectory()) openDirectory(f, path); else { if (openAsVirtualStack && (path.endsWith(".tif") || path.endsWith(".TIF"))) (new FileInfoVirtualStack()).run(path); else (new Opener()).openAndAddToRecent(path); OpenDialog.setLastDirectory(f.getParent() + File.separator); OpenDialog.setLastName(f.getName()); } } else { IJ.log("File not found: " + path); } } catch (Throwable e) { if (!Macro.MACRO_CANCELED.equals(e.getMessage())) IJ.handleException(e); } }
static boolean validateFileInfo(File f, FileInfo fi) { long offset = fi.getOffset(); long length = 0; if (fi.width <= 0 || fi.height < 0) { error("Width or height <= 0.", fi, offset, length); return false; } if (offset >= 0 && offset < 1000L) return true; if (offset < 0L) { error("Offset is negative.", fi, offset, length); return false; } if (fi.fileType == FileInfo.BITMAP || fi.compression != FileInfo.COMPRESSION_NONE) return true; length = f.length(); long size = fi.width * fi.height * fi.getBytesPerPixel(); size = fi.nImages > 1 ? size : size / 4; if (fi.height == 1) size = 0; // allows plugins to read info of unknown length at end of file if (offset + size > length) { error("Offset + image size > file length.", fi, offset, length); return false; } return true; }
public void run(String arg) { if (arg.equals("menus")) { updateMenus(); return; } if (IJ.getApplet() != null) return; URL url = getClass().getResource("/ij/IJ.class"); String ij_jar = url == null ? null : url.toString().replaceAll("%20", " "); if (ij_jar == null || !ij_jar.startsWith("jar:file:")) { error("Could not determine location of ij.jar"); return; } int exclamation = ij_jar.indexOf('!'); ij_jar = ij_jar.substring(9, exclamation); if (IJ.debugMode) IJ.log("Updater (jar loc): " + ij_jar); File file = new File(ij_jar); if (!file.exists()) { error("File not found: " + file.getPath()); return; } if (!file.canWrite()) { String msg = "No write access: " + file.getPath(); error(msg); return; } String[] list = openUrlAsList(IJ.URL + "/download/jars/list.txt"); int count = list.length + 3; String[] versions = new String[count]; String[] urls = new String[count]; String uv = getUpgradeVersion(); if (uv == null) return; versions[0] = "v" + uv; urls[0] = IJ.URL + "/upgrade/ij.jar"; if (versions[0] == null) return; for (int i = 1; i < count - 2; i++) { String version = list[i - 1]; versions[i] = version.substring(0, version.length() - 1); // remove letter urls[i] = IJ.URL + "/download/jars/ij" + version.substring(1, 2) + version.substring(3, 6) + ".jar"; } versions[count - 2] = "daily build"; urls[count - 2] = IJ.URL + "/ij.jar"; versions[count - 1] = "previous"; urls[count - 1] = IJ.URL + "/upgrade/ij2.jar"; int choice = showDialog(versions); if (choice == -1 || !Commands.closeAll()) return; // System.out.println("choice: "+choice); // for (int i=0; i<urls.length; i++) System.out.println(" "+i+" "+urls[i]); byte[] jar = null; if ("daily build".equals(versions[choice]) && notes != null && notes.contains(" </title>")) jar = getJar("http://wsr.imagej.net/download/daily-build/ij.jar"); if (jar == null) jar = getJar(urls[choice]); if (jar == null) { error("Unable to download ij.jar from " + urls[choice]); return; } Prefs.savePreferences(); // System.out.println("saveJar: "+file); saveJar(file, jar); if (choice < count - 2) // force macro Function Finder to download fresh list new File(IJ.getDirectory("macros") + "functions.html").delete(); System.exit(0); }
public void run(String arg) { if (arg.equals("menus")) { updateMenus(); return; } if (IJ.getApplet() != null) return; // File file = new File(Prefs.getHomeDir() + File.separator + "ij.jar"); // if (isMac() && !file.exists()) // file = new File(Prefs.getHomeDir() + File.separator + // "ImageJ.app/Contents/Resources/Java/ij.jar"); URL url = getClass().getResource("/ij/IJ.class"); String ij_jar = url == null ? null : url.toString().replaceAll("%20", " "); if (ij_jar == null || !ij_jar.startsWith("jar:file:")) { error("Could not determine location of ij.jar"); return; } int exclamation = ij_jar.indexOf('!'); ij_jar = ij_jar.substring(9, exclamation); if (IJ.debugMode) IJ.log("Updater: " + ij_jar); File file = new File(ij_jar); if (!file.exists()) { error("File not found: " + file.getPath()); return; } if (!file.canWrite()) { String msg = "No write access: " + file.getPath(); if (IJ.isVista()) msg += Prefs.vistaHint; error(msg); return; } String[] list = openUrlAsList(IJ.URL + "/download/jars/list.txt"); int count = list.length + 2; String[] versions = new String[count]; String[] urls = new String[count]; String uv = getUpgradeVersion(); if (uv == null) return; versions[0] = "v" + uv; urls[0] = IJ.URL + "/upgrade/ij.jar"; if (versions[0] == null) return; for (int i = 1; i < count - 1; i++) { String version = list[i - 1]; versions[i] = version.substring(0, version.length() - 1); // remove letter urls[i] = IJ.URL + "/download/jars/ij" + version.substring(1, 2) + version.substring(3, 6) + ".jar"; } versions[count - 1] = "daily build"; urls[count - 1] = IJ.URL + "/ij.jar"; int choice = showDialog(versions); if (choice == -1) return; if (!versions[choice].startsWith("daily") && versions[choice].compareTo("v1.39") < 0 && Menus.getCommands().get("ImageJ Updater") == null) { String msg = "This command is not available in versions of ImageJ prior\n" + "to 1.39 so you will need to install the plugin version at\n" + "<" + IJ.URL + "/plugins/imagej-updater.html>."; if (!IJ.showMessageWithCancel("Update ImageJ", msg)) return; } byte[] jar = getJar(urls[choice]); // file.renameTo(new File(file.getParent()+File.separator+"ij.bak")); if (version().compareTo("1.37v") >= 0) Prefs.savePreferences(); // if (!renameJar(file)) return; // doesn't work on Vista saveJar(file, jar); if (choice < count - 1) // force macro Function Finder to download fresh list new File(IJ.getDirectory("macros") + "functions.html").delete(); System.exit(0); }
public boolean install(String path) { boolean isURL = path.contains("://"); String lcPath = path.toLowerCase(); if (isURL) path = Opener.updateUrl(path); boolean isTool = lcPath.endsWith("tool.ijm") || lcPath.endsWith("tool.txt") || lcPath.endsWith("tool.class") || lcPath.endsWith("tool.jar"); boolean isMacro = lcPath.endsWith(".txt") || lcPath.endsWith(".ijm"); byte[] data = null; String name = path; if (isURL) { int index = path.lastIndexOf("/"); if (index != -1 && index <= path.length() - 1) name = path.substring(index + 1); data = download(path, name); } else { File f = new File(path); name = f.getName(); data = download(f); } if (data == null) return false; if (name.endsWith(".txt") && !name.contains("_")) name = name.substring(0, name.length() - 4) + ".ijm"; if (name.endsWith(".zip")) { if (!name.contains("_")) { IJ.error("Plugin Installer", "No underscore in file name:\n \n " + name); return false; } name = name.substring(0, name.length() - 4) + ".jar"; } String dir = null; boolean isLibrary = name.endsWith(".jar") && !name.contains("_"); if (isLibrary) { dir = Menus.getPlugInsPath() + "jars"; File f = new File(dir); if (!f.exists()) { boolean ok = f.mkdir(); if (!ok) dir = Menus.getPlugInsPath(); } } if (isTool) { dir = Menus.getPlugInsPath() + "Tools" + File.separator; File f = new File(dir); if (!f.exists()) { boolean ok = f.mkdir(); if (!ok) dir = null; } if (dir != null && isMacro) { String name2 = getToolName(data); if (name2 != null) name = name2; } } if (dir == null) { SaveDialog sd = new SaveDialog("Save Plugin, Macro or Script...", Menus.getPlugInsPath(), name, null); String name2 = sd.getFileName(); if (name2 == null) return false; dir = sd.getDirectory(); } // IJ.log(dir+" "+Menus.getPlugInsPath()); if (!savePlugin(new File(dir, name), data)) return false; if (name.endsWith(".java")) IJ.runPlugIn("ij.plugin.Compiler", dir + name); Menus.updateImageJMenus(); if (isTool) { if (isMacro) IJ.runPlugIn("ij.plugin.Macro_Runner", "Tools/" + name); else if (name.endsWith(".class")) { name = name.replaceAll("_", " "); name = name.substring(0, name.length() - 6); IJ.run(name); } } return true; }