Search in sources :

Example 71 with TiffParser

use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.

the class EditTiffG method openFile.

public void openFile(File f) {
    try {
        String id = f.getAbsolutePath();
        String xml = new TiffParser(id).getComment();
        setXML(xml);
        file = f;
        setTitle(TITLE + " - " + id);
    } catch (IOException exc) {
        showError(exc);
    }
}
Also used : TiffParser(loci.formats.tiff.TiffParser) IOException(java.io.IOException)

Example 72 with TiffParser

use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.

the class MinimalTiffReader method initTiffParser.

/**
 * Reinitialize the underlying TiffParser.
 */
protected void initTiffParser() {
    if (in == null) {
        try {
            in = new RandomAccessInputStream(getCurrentFile(), 16);
        } catch (IOException e) {
            LOGGER.error("Could not initialize stream", e);
        }
    }
    tiffParser = new TiffParser(in);
    tiffParser.setDoCaching(false);
    tiffParser.setUse64BitOffsets(use64Bit);
}
Also used : TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException)

Example 73 with TiffParser

use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.

the class TiffWriter method saveBytes.

// -- IFormatWriter API methods --
/**
 * @see loci.formats.IFormatWriter#saveBytes(int, byte[], int, int, int, int)
 */
@Override
public void saveBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    IFD ifd = new IFD();
    if (!sequential) {
        TiffParser parser = new TiffParser(currentId);
        try {
            long[] ifdOffsets = parser.getIFDOffsets();
            if (no < ifdOffsets.length) {
                ifd = parser.getIFD(ifdOffsets[no]);
            }
            saveBytes(no, buf, ifd, x, y, w, h);
        } finally {
            RandomAccessInputStream tiffParserStream = parser.getStream();
            if (tiffParserStream != null) {
                tiffParserStream.close();
            }
        }
    } else {
        saveBytes(no, buf, ifd, x, y, w, h);
    }
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 74 with TiffParser

use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.

the class LeicaReader method findLEIFile.

// -- Helper methods --
/**
 * Find the .lei file that belongs to the same dataset as the given file.
 */
private String findLEIFile(String baseFile) throws FormatException, IOException {
    if (checkSuffix(baseFile, LEI_SUFFIX)) {
        return baseFile;
    } else if (checkSuffix(baseFile, TiffReader.TIFF_SUFFIXES) && isGroupFiles()) {
        // need to find the associated .lei file
        if (ifds == null)
            super.initFile(baseFile);
        in = new RandomAccessInputStream(baseFile, 16);
        TiffParser tp = new TiffParser(in);
        in.order(tp.checkHeader().booleanValue());
        in.seek(0);
        LOGGER.info("Finding companion file name");
        // open the TIFF file and look for the "Image Description" field
        ifds = tp.getIFDs();
        if (ifds == null)
            throw new FormatException("No IFDs found");
        String descr = ifds.get(0).getComment();
        // remove anything of the form "[blah]"
        descr = descr.replaceAll("\\[.*.\\]\n", "");
        // each remaining line in descr is a (key, value) pair,
        // where '=' separates the key from the value
        String lei = baseFile.substring(0, baseFile.lastIndexOf(File.separator) + 1);
        StringBuilder suffix = new StringBuilder();
        StringTokenizer lines = new StringTokenizer(descr, "\n");
        String line = null, key = null, value = null;
        while (lines.hasMoreTokens()) {
            line = lines.nextToken();
            if (line.indexOf('=') == -1)
                continue;
            key = line.substring(0, line.indexOf('=')).trim();
            value = line.substring(line.indexOf('=') + 1).trim();
            addGlobalMeta(key, value);
            if (key.startsWith("Series Name")) {
                suffix.append(value);
            }
        }
        lei += suffix.toString();
        // now open the LEI file
        Location l = new Location(lei).getAbsoluteFile();
        if (l.exists()) {
            return lei;
        } else {
            if (!lei.endsWith("lei") && !lei.endsWith("LEI")) {
                lei = lei.substring(0, lei.lastIndexOf(".") + 1);
                String test = lei + "lei";
                if (new Location(test).exists()) {
                    return test;
                }
                test = lei + "LEI";
                if (new Location(test).exists()) {
                    return test;
                }
            }
            l = l.getParentFile();
            String[] list = l.list();
            for (int i = 0; i < list.length; i++) {
                if (checkSuffix(list[i], LEI_SUFFIX)) {
                    return new Location(l.getAbsolutePath(), list[i]).getAbsolutePath();
                }
            }
        }
    } else if (checkSuffix(baseFile, "raw") && isGroupFiles()) {
        // check for that there is an .lei file in the same directory
        String prefix = baseFile;
        if (prefix.indexOf('.') != -1) {
            prefix = prefix.substring(0, prefix.lastIndexOf("."));
        }
        Location lei = new Location(prefix + ".lei");
        if (!lei.exists()) {
            lei = new Location(prefix + ".LEI");
            while (!lei.exists() && prefix.indexOf('_') != -1) {
                prefix = prefix.substring(0, prefix.lastIndexOf("_"));
                lei = new Location(prefix + ".lei");
                if (!lei.exists())
                    lei = new Location(prefix + ".LEI");
            }
        }
        if (lei.exists())
            return lei.getAbsolutePath();
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) FormatException(loci.formats.FormatException) Location(loci.common.Location)

Example 75 with TiffParser

use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.

the class LeicaReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser tp = new TiffParser(stream);
    IFD ifd = tp.getFirstIFD();
    if (ifd == null)
        return false;
    return ifd.containsKey(new Integer(LEICA_MAGIC_TAG));
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Aggregations

TiffParser (loci.formats.tiff.TiffParser)96 IFD (loci.formats.tiff.IFD)74 RandomAccessInputStream (loci.common.RandomAccessInputStream)56 FormatException (loci.formats.FormatException)26 CoreMetadata (loci.formats.CoreMetadata)19 IOException (java.io.IOException)18 IFDList (loci.formats.tiff.IFDList)16 Location (loci.common.Location)15 MetadataStore (loci.formats.meta.MetadataStore)15 ArrayList (java.util.ArrayList)12 Timestamp (ome.xml.model.primitives.Timestamp)9 Length (ome.units.quantity.Length)8 PhotoInterp (loci.formats.tiff.PhotoInterp)6 File (java.io.File)5 HashMap (java.util.HashMap)5 ServiceException (loci.common.services.ServiceException)4 Time (ome.units.quantity.Time)4 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)4 List (java.util.List)3 ByteArrayHandle (loci.common.ByteArrayHandle)3