Search in sources :

Example 21 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class Opener method getFileType.

/**
 *	Attempts to determine the image file type by looking for
 *	'magic numbers' and the file name extension.
 */
@AstroImageJ(reason = "Add more file types; extend how fits file is checked; " + "skip read check for files within a zip; add option to ignore unopenable file; " + "add extension check for pngs", modified = true)
private int getFileType(String path, boolean checkOpenable) {
    if (openUsingPlugins && !path.endsWith(".txt") && !path.endsWith(".java"))
        return UNKNOWN;
    File file = new File(path);
    String name = file.getName();
    InputStream is;
    byte[] buf = new byte[132];
    try {
        if (checkOpenable) {
            is = new FileInputStream(file);
            is.read(buf, 0, 132);
            is.close();
        }
    } catch (IOException e) {
        return UNKNOWN;
    }
    int b0 = buf[0] & 255, b1 = buf[1] & 255, b2 = buf[2] & 255, b3 = buf[3] & 255;
    // Combined TIFF and DICOM created by GE Senographe scanners
    if (buf[128] == 68 && buf[129] == 73 && buf[130] == 67 && buf[131] == 77 && ((b0 == 73 && b1 == 73) || (b0 == 77 && b1 == 77)))
        return TIFF_AND_DICOM;
    // Big-endian TIFF ("MM")
    if (name.endsWith(".lsm"))
        // The LSM	Reader plugin opens these files
        return UNKNOWN;
    if (b0 == 73 && b1 == 73 && b2 == 42 && b3 == 0 && !(bioformats && name.endsWith(".flex")))
        return TIFF;
    // Little-endian TIFF ("II")
    if (b0 == 77 && b1 == 77 && b2 == 0 && b3 == 42)
        return TIFF;
    // JPEG
    if (b0 == 255 && b1 == 216 && b2 == 255)
        return JPEG;
    // GIF ("GIF8")
    if (b0 == 71 && b1 == 73 && b2 == 70 && b3 == 56)
        return GIF;
    name = name.toLowerCase(Locale.US);
    // DICOM ("DICM" at offset 128)
    if (buf[128] == 68 && buf[129] == 73 && buf[130] == 67 && buf[131] == 77 || name.endsWith(".dcm")) {
        return DICOM;
    }
    // ACR/NEMA with first tag = 00002,00xx or 00008,00xx
    if ((b0 == 8 || b0 == 2) && b1 == 0 && b3 == 0 && !name.endsWith(".spe") && !name.equals("fid"))
        return DICOM;
    // PGM ("P1", "P4", "P2", "P5", "P3" or "P6")
    if (b0 == 80 && (b1 == 49 || b1 == 52 || b1 == 50 || b1 == 53 || b1 == 51 || b1 == 54) && (b2 == 10 || b2 == 13 || b2 == 32 || b2 == 9))
        return PGM;
    // Lookup table
    if (name.endsWith(".lut"))
        return LUT;
    // PNG
    if ((b0 == 137 && b1 == 80 && b2 == 78 && b3 == 71) || name.endsWith(".png"))
        return PNG;
    // ZIP containing a TIFF
    if (name.endsWith(".zip"))
        return ZIP;
    // FITS ("SIMP")
    if ((b0 == 83 && b1 == 73 && b2 == 77 && b3 == 80) || name.endsWith(".fts.gz") || name.endsWith(".fits.gz") || name.endsWith(".fit.gz") || name.endsWith(".fts.fz") || name.endsWith(".fits.fz") || name.endsWith(".fit.fz") || name.endsWith(".fits"))
        return FITS;
    // Java source file, text file or macro
    if (name.endsWith(".java") || name.endsWith(".txt") || name.endsWith(".ijm") || name.endsWith(".js") || name.endsWith(".bsh") || name.endsWith(".py") || name.endsWith(".html"))
        return JAVA_OR_TEXT;
    // ImageJ, NIH Image, Scion Image for Windows ROI
    if (// "Iout"
    b0 == 73 && b1 == 111)
        return ROI;
    // ObjectJ project
    if ((b0 == 'o' && b1 == 'j' && b2 == 'j' && b3 == 0) || name.endsWith(".ojj"))
        return OJJ;
    // Results table (tab-delimited or comma-separated tabular text)
    if (name.endsWith(".xls") || name.endsWith(".csv") || name.endsWith(".dat") || name.endsWith(".tbl") || name.endsWith(".prn") || name.endsWith(".spc"))
        return TABLE;
    // AVI
    if (name.endsWith(".avi"))
        return AVI;
    // Text file
    boolean isText = true;
    for (int i = 0; i < 10; i++) {
        int c = buf[i] & 255;
        if ((c < 32 && c != 9 && c != 10 && c != 13) || c > 126) {
            isText = false;
            break;
        }
    }
    if (isText)
        return TEXT;
    // BMP ("BM")
    if ((b0 == 66 && b1 == 77) || name.endsWith(".dib"))
        return BMP;
    // RAW
    if (name.endsWith(".raw") && !Prefs.skipRawDialog)
        return RAW;
    return UNKNOWN;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipFile(java.util.zip.ZipFile) AstroImageJ(ij.astro.AstroImageJ)

Example 22 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class Opener method getImageFromPath.

/**
 * Get image from the path. If it is in a zip file, it will open the zip
 */
@AstroImageJ(reason = "Open zip of pngs as folder")
private BufferedImage getImageFromPath(String path) throws IOException {
    if (path.contains(".zip")) {
        var s = path.split("\\.zip");
        var zip = new ZipFile(s[0] + ".zip");
        var img = ImageIO.read(zip.getInputStream(zip.getEntry(s[1].substring(1))));
        zip.close();
        return img;
    }
    return ImageIO.read(new File(path));
}
Also used : ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile) AstroImageJ(ij.astro.AstroImageJ)

Example 23 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class FolderOpener method getFileCount.

/**
 * @return first = file count, second = size in MB
 */
@AstroImageJ(reason = "Obtain filtered file count")
private Pair.IntFloatPair getFileCount(GenericDialog gd) {
    var filter = ((TextField) gd.getStringFields().get(1)).getText();
    if (legacyRegex != null)
        filter = "(" + legacyRegex + ")";
    var directory = this.directory;
    directory = ((TextField) gd.getStringFields().get(0)).getText();
    File file = new File(directory);
    String[] list = file.list();
    // Zip as folder
    ZipOpenerUtil.InternalZipFile[] zipEntries = null;
    if (list == null) {
        zipEntries = ZipOpenerUtil.getFilesInZip(directory);
        list = ZipOpenerUtil.InternalZipFile.getPaths(zipEntries);
        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 {
            return Pair.IntFloatPair.empty();
        }
    }
    // remove subdirectories from list
    ArrayList<String> fileList = new ArrayList<>();
    for (String s : list) {
        File f = (new File(directory + s));
        if (!f.isDirectory())
            fileList.add(s);
    }
    if (fileList.size() < list.length)
        list = fileList.toArray(new String[fileList.size()]);
    list = trimFileList(list);
    if (list == null)
        return Pair.IntFloatPair.empty();
    // Dynamic filter does not support legacy regex
    list = getFilteredList(list, filter, null);
    if (list == null)
        return Pair.IntFloatPair.empty();
    // Get file sizes by locating a prototypical image file manually calculating its byte size in memory
    var width = 0;
    var height = 0;
    var bitDepth = 0;
    long sizeInBytes = 0;
    var stackCountPerImage = 1;
    for (String sf : list) {
        Opener opener = new Opener();
        opener.setSilentMode(true);
        IJ.redirectErrorMessages(true);
        ImagePlus imp = opener.openImage(directory, sf);
        IJ.redirectErrorMessages(false);
        if (imp != null) {
            width = imp.getWidth();
            height = imp.getHeight();
            bitDepth = imp.getBytesPerPixel();
            stackCountPerImage = imp.getImageStackSize();
            break;
        }
    }
    var increment = safeParse(((TextField) gd.getNumericFields().get(1)).getText(), 1);
    var count = safeParse(((TextField) gd.getStringFields().get(2)).getText(), list.length);
    var start = safeParse(((TextField) gd.getNumericFields().get(0)).getText(), 1);
    sizeInBytes = (long) bitDepth * width * height * ((long) ((count - start + 1) / increment) * stackCountPerImage);
    return new Pair.IntFloatPair(list.length, sizeInBytes / 1_000_000F);
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) Opener(ij.io.Opener) AstroImageJ(ij.astro.AstroImageJ)

Example 24 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class TextPanel method copySelection.

/**
 *	Copies the current selection to the system clipboard.
 *	Returns the number of characters copied.
 */
@AstroImageJ(reason = "Limit selection to tabs", modified = true)
public int copySelection() {
    if (Recorder.record && title.equals("Results"))
        Recorder.record("String.copyResults");
    if (selStart == -1 || selEnd == -1)
        return copyAll();
    StringBuffer sb = new StringBuffer();
    ResultsTable rt2 = getResultsTable();
    boolean hasRowNumers = rt2 != null && rt2.showRowNumbers();
    if (Prefs.copyColumnHeaders && labels != null && !labels.equals("") && selStart == 0 && selEnd == iRowCount - 1) {
        if (hasRowNumers && Prefs.noRowNumbers) {
            String s = labels;
            int index = s.indexOf("\t", s.indexOf("\t") + 1);
            if (index != -1)
                s = s.substring(index + 1, s.length());
            sb.append(s);
        } else
            sb.append(labels);
        sb.append('\n');
    }
    for (int i = selStart; i <= selEnd; i++) {
        char[] chars = (char[]) (vData.elementAt(i));
        String s = new String(chars);
        if (s.endsWith("\t"))
            s = s.substring(0, s.length() - 1);
        if (hasRowNumers && Prefs.noRowNumbers && labels != null && !labels.equals("")) {
            int index = s.indexOf("\t");
            if (index != -1)
                s = s.substring(index + 1, s.length());
            sb.append(s);
        } else
            sb.append(s);
        if (i < selEnd || selEnd > selStart)
            sb.append('\n');
    }
    String s = new String(sb);
    Clipboard clip = getToolkit().getSystemClipboard();
    if (clip == null)
        return 0;
    StringSelection cont = new StringSelection(s);
    clip.setContents(cont, this);
    if (s.length() > 0) {
        IJ.showStatus((selEnd - selStart + 1) + " lines copied to clipboard");
        if (this.getParent() instanceof ImageJ)
            Analyzer.setUnsavedMeasurements(false);
    }
    return s.length();
}
Also used : AstroImageJ(ij.astro.AstroImageJ) ResultsTable(ij.measure.ResultsTable) Clipboard(java.awt.datatransfer.Clipboard) StringSelection(java.awt.datatransfer.StringSelection) AstroImageJ(ij.astro.AstroImageJ)

Example 25 with AstroImageJ

use of ij.astro.AstroImageJ in project astroimagej by keastrid.

the class TextPanel method rename.

@AstroImageJ(reason = "Add check for AIJ windows", modified = true)
void rename(String title2) {
    ResultsTable rt2 = getOrCreateResultsTable();
    if (rt2 == null)
        return;
    if (title2 != null && title2.equals(""))
        title2 = null;
    TextWindow tw = getTextWindow();
    if (tw == null)
        return;
    if (title2 == null) {
        GenericDialog gd = new GenericDialog("Rename", tw);
        gd.addStringField("Title:", tw.getTitle().contains("Measure") ? tw.getTitle() + "_2" : "Results2", 40);
        gd.showDialog();
        if (gd.wasCanceled())
            return;
        title2 = gd.getNextString();
    }
    String title1 = title;
    if (title != null && title.equals("Results")) {
        IJ.setTextPanel(null);
        Analyzer.setUnsavedMeasurements(false);
        Analyzer.setResultsTable(null);
        Analyzer.resetCounter();
    }
    if (title2.equals("Results")) {
        // tw.setVisible(false);
        tw.dispose();
        WindowManager.removeWindow(tw);
        flush();
        rt2.show("Results");
    } else {
        tw.setTitle(title2);
        int mbSize = tw.mb != null ? tw.mb.getMenuCount() : 0;
        if (mbSize > 0 && tw.mb.getMenu(mbSize - 1).getLabel().equals("Results"))
            tw.mb.remove(mbSize - 1);
        title = title2;
        rt2.show(title);
    }
    Menus.updateWindowMenuItem(title1, title2);
    if (Recorder.record) {
        if (Recorder.scriptMode())
            Recorder.recordString("IJ.renameResults(\"" + title1 + "\", \"" + title2 + "\");\n");
        else
            Recorder.record("Table.rename", title1, title2);
    }
}
Also used : ResultsTable(ij.measure.ResultsTable) AstroImageJ(ij.astro.AstroImageJ)

Aggregations

AstroImageJ (ij.astro.AstroImageJ)28 File (java.io.File)4 ZipFile (java.util.zip.ZipFile)4 SaveDialog (ij.io.SaveDialog)3 ResultsTable (ij.measure.ResultsTable)3 ImageProcessor (ij.process.ImageProcessor)3 URL (java.net.URL)3 GenericDialog (ij.gui.GenericDialog)2 Opener (ij.io.Opener)2 TextWindow (ij.text.TextWindow)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 ZipInputStream (java.util.zip.ZipInputStream)2 ZipOpenerUtil (ij.astro.util.ZipOpenerUtil)1 ImageWindow (ij.gui.ImageWindow)1 Overlay (ij.gui.Overlay)1 Roi (ij.gui.Roi)1 FileInfo (ij.io.FileInfo)1