/** Opens the nth image of the specified TIFF stack. */ public ImagePlus openTiff(String path, int n) { TiffDecoder td = new TiffDecoder(getDir(path), getName(path)); if (IJ.debugMode) td.enableDebugging(); FileInfo[] info = null; try { info = td.getTiffInfo(); } catch (IOException e) { String msg = e.getMessage(); if (msg == null || msg.equals("")) msg = "" + e; IJ.error("Open TIFF", msg); return null; } if (info == null) return null; FileInfo fi = info[0]; if (info.length == 1 && fi.nImages > 1) { if (n < 1 || n > fi.nImages) throw new IllegalArgumentException("N out of 1-" + fi.nImages + " range"); long size = fi.width * fi.height * fi.getBytesPerPixel(); fi.longOffset = fi.getOffset() + (n - 1) * (size + fi.gapBetweenImages); fi.offset = 0; fi.nImages = 1; } else { if (n < 1 || n > info.length) throw new IllegalArgumentException("N out of 1-" + info.length + " range"); fi.longOffset = info[n - 1].getOffset(); fi.offset = 0; fi.stripOffsets = info[n - 1].stripOffsets; fi.stripLengths = info[n - 1].stripLengths; } FileOpener fo = new FileOpener(fi); return fo.open(false); }
/** Deserialize a byte array that was serialized using the FileSaver.serialize(). */ public ImagePlus deserialize(byte[] bytes) { ByteArrayInputStream stream = new ByteArrayInputStream(bytes); TiffDecoder decoder = new TiffDecoder(stream, "Untitled"); if (IJ.debugMode) decoder.enableDebugging(); FileInfo[] info = null; try { info = decoder.getTiffInfo(); } catch (IOException e) { return null; } FileOpener opener = new FileOpener(info[0]); ImagePlus imp = opener.open(false); if (imp == null) return null; imp.setTitle(info[0].fileName); imp = makeComposite(imp, info[0]); return imp; }
ImagePlus openTiff2(FileInfo[] info) { if (info == null) return null; ImagePlus imp = null; if (IJ.debugMode) // dump tiff tags IJ.log(info[0].debugInfo); if (info.length > 1) { // try to open as stack imp = openTiffStack(info); if (imp != null) return imp; } FileOpener fo = new FileOpener(info[0]); imp = fo.open(false); if (imp == null) return null; int[] offsets = info[0].stripOffsets; if (offsets != null && offsets.length > 1 && offsets[offsets.length - 1] < offsets[0]) ij.IJ.run(imp, "Flip Vertically", "stack"); imp = makeComposite(imp, info[0]); if (imp.getBitDepth() == 32 && imp.getTitle().startsWith("FFT of")) return openFFT(imp); else return imp; }
/** * Attempts to open the specified file as a tiff, bmp, dicom, fits, pgm, gif or jpeg image. * Returns an ImagePlus object if successful. Modified by Gregory Jefferis to call * HandleExtraFileTypes plugin if the file type is unrecognised. * * @see ij.IJ#openImage(String) */ public ImagePlus openImage(String directory, String name) { ImagePlus imp; FileOpener.setSilentMode(silentMode); if (directory.length() > 0 && !(directory.endsWith("/") || directory.endsWith("\\"))) directory += Prefs.separator; String path = directory + name; fileType = getFileType(path); if (IJ.debugMode) IJ.log("openImage: \"" + types[fileType] + "\", " + path); switch (fileType) { case TIFF: imp = openTiff(directory, name); return imp; case DICOM: imp = (ImagePlus) IJ.runPlugIn("ij.plugin.DICOM", path); if (imp.getWidth() != 0) return imp; else return null; case TIFF_AND_DICOM: // "hybrid" files created by GE-Senographe 2000 D */ imp = openTiff(directory, name); ImagePlus imp2 = (ImagePlus) IJ.runPlugIn("ij.plugin.DICOM", path); if (imp != null && imp2 != null) { imp.setProperty("Info", imp2.getProperty("Info")); imp.setCalibration(imp2.getCalibration()); } if (imp == null) imp = imp2; return imp; case FITS: imp = (ImagePlus) IJ.runPlugIn("ij.plugin.FITS_Reader", path); if (imp.getWidth() != 0) return imp; else return null; case PGM: imp = (ImagePlus) IJ.runPlugIn("ij.plugin.PGM_Reader", path); if (imp.getWidth() != 0) { if (imp.getStackSize() == 3 && imp.getBitDepth() == 16) imp = new CompositeImage(imp, IJ.COMPOSITE); return imp; } else return null; case JPEG: imp = openJpegOrGif(directory, name); if (imp != null && imp.getWidth() != 0) return imp; else return null; case GIF: imp = (ImagePlus) IJ.runPlugIn("ij.plugin.GIF_Reader", path); if (imp != null && imp.getWidth() != 0) return imp; else return null; case PNG: imp = openUsingImageIO(directory + name); if (imp != null && imp.getWidth() != 0) return imp; else return null; case BMP: imp = (ImagePlus) IJ.runPlugIn("ij.plugin.BMP_Reader", path); if (imp.getWidth() != 0) return imp; else return null; case ZIP: return openZip(path); case AVI: AVI_Reader reader = new AVI_Reader(); reader.displayDialog(!IJ.macroRunning()); reader.run(path); return reader.getImagePlus(); case UNKNOWN: case TEXT: // Call HandleExtraFileTypes plugin to see if it can handle unknown format int[] wrap = new int[] {fileType}; imp = openWithHandleExtraFileTypes(path, wrap); if (imp != null && imp.getNChannels() > 1) imp = new CompositeImage(imp, IJ.COLOR); fileType = wrap[0]; if (imp == null && fileType == UNKNOWN && IJ.getInstance() == null) IJ.error("Opener", "Unsupported format or not found"); return imp; default: return null; } }