Search in sources :

Example 1 with CaseInsensitiveLocation

use of loci.common.CaseInsensitiveLocation in project bioformats by openmicroscopy.

the class ZeissTIFFReader method evalFile.

// -- Internal FormatReader API methods --
/**
 * Evaluate the metadata of a TIFF/XML file collection, and return this to the user.  This is done by filling a TIFFInfo object containing all of the file and directory paths, plus the parsed metadata.
 * @param id the filename to evaluate.  This may be the top-level or per-plane TIFF, or the XML metadata.
 * @return an TIFFInfo object.
 * @throws FormatException
 * @throws IOException
 */
protected TIFFInfo evalFile(String id) throws FormatException, IOException {
    // If this is an XML file, or one of the per-plane TIFFs, we must find the real basename now.
    // If it's a tiff, check for foo_meta.xml or foo_files/_meta.xml or _meta.xml.  For the latter, work out the basename from the XML itself.  Note that it might be missing, so should be optional.
    // The initial file is either:
    // Single plane
    // · A top-level TIFF (plus ${file}_meta.xml)
    // · A top-level ${file}_meta.xml (plus ${file} TIFF)
    // Multiple planes
    // · A top-level TIFF thumbnail (plus subdir with _meta.xml and per-plane TIFFs)
    // · A subdirectory _meta.xml (plus per-plane TIFFs and possible top-level TIFF thumbnail)
    // · A subdirectory per-plane TIFF (plus additional per-plane TIFFs, _meta.xml and possible top-level TIFF thumbnail)
    // 
    // Start by trying to find the XML.  Then get the Filename from the XML, and use that to try to find the other files.  If there's no Filename tag, try to use the original filename.
    TIFFInfo info = new TIFFInfo();
    CaseInsensitiveLocation l;
    CaseInsensitiveLocation lxml;
    l = new CaseInsensitiveLocation(id);
    String name = l.getAbsolutePath();
    // This "original" name is only tentative; it might be set to to eiher the top-level image or thumbnail (if it's the XML file, and the top-level file exists, or the XML file if it does not exist)
    if (name.endsWith(".tif")) {
        // Now iterate through the various XML locations
        info.xmlname = name + XML_NAME;
        // If the XML file isn't present, check we're not in a subdirectory.
        lxml = new CaseInsensitiveLocation(info.xmlname);
        if (lxml.exists()) {
            // Simple single-plane case
            info.origname = name;
            // Always null for single files.
            info.basedir = null;
            info.multifile = false;
        } else {
            // Multiple planes
            CaseInsensitiveLocation lb = new CaseInsensitiveLocation(name + "_files");
            lxml = new CaseInsensitiveLocation(lb, XML_NAME);
            if (lb.exists() && lxml.exists()) {
                // Planes in subdirectory
                info.xmlname = lxml.getAbsolutePath();
                info.origname = name;
                // Multifile
                info.basedir = lb.getAbsolutePath();
                info.multifile = true;
            } else {
                // Planes in this directory
                lb = new CaseInsensitiveLocation(l.getParent());
                lxml = new CaseInsensitiveLocation(lb, XML_NAME);
                if (lb.exists() && lxml.exists()) {
                    info.xmlname = lxml.getAbsolutePath();
                    // May be updated later
                    info.origname = info.xmlname;
                    // Multifile
                    info.basedir = lb.getAbsolutePath();
                    info.multifile = true;
                } else {
                    throw new FormatException("XML metadata not found");
                }
            }
        }
    } else if (name.endsWith(XML_NAME)) {
        info.xmlname = name;
        lxml = new CaseInsensitiveLocation(info.xmlname);
        if (!lxml.exists())
            throw new FormatException("XML metadata not found");
        if (lxml.getName().equals(XML_NAME)) {
            // Multiple files
            CaseInsensitiveLocation lb = new CaseInsensitiveLocation(lxml.getParent());
            info.xmlname = lxml.getAbsolutePath();
            info.origname = info.xmlname;
            info.basedir = lb.getAbsolutePath();
            info.multifile = true;
        } else {
            info.xmlname = lxml.getAbsolutePath();
            info.origname = info.xmlname.substring(0, info.xmlname.length() - XML_NAME.length());
            // Single file
            info.basedir = null;
            info.multifile = false;
            l = new CaseInsensitiveLocation(info.origname);
            if (!l.exists())
                throw new FormatException("TIFF image data not found");
            info.origname = l.getAbsolutePath();
        }
    } else {
        throw new FormatException("Invalid AxioVision TIFF XML");
    }
    String xml = DataTools.readFile(info.xmlname);
    info.handler = new ZeissTIFFHandler(this);
    XMLTools.parseXML(xml, info.handler);
    boolean found = false;
    for (Tag t : info.handler.main_tagset.tags) {
        if (t.getKey().equals("Filename")) {
            CaseInsensitiveLocation n = new CaseInsensitiveLocation(info.basedir, t.getValue());
            info.origname = n.getName();
            found = true;
            break;
        }
    }
    if (!found) {
        // It's possible we could guess this by looking for the common prefix in the contents of the directory.
        if (info.origname.endsWith(XML_NAME)) {
            throw new FormatException("Image name not found in XML metadata");
        }
    }
    info.prefix = getPrefix(info.origname);
    if (info.basedir != null) {
        CaseInsensitiveLocation b = new CaseInsensitiveLocation(info.basedir);
        info.basedir = b.getAbsolutePath();
        l = b;
    }
    l = new CaseInsensitiveLocation(l.getParent(), info.prefix + ".tif");
    info.origname = l.getAbsolutePath();
    return info;
}
Also used : CaseInsensitiveLocation(loci.common.CaseInsensitiveLocation) FormatException(loci.formats.FormatException)

Example 2 with CaseInsensitiveLocation

use of loci.common.CaseInsensitiveLocation in project bioformats by openmicroscopy.

the class ZeissTIFFReader method getSeriesUsedFiles.

/* @see loci.formats.IFormatReader#getSeriesUsedFiles(boolean) */
@Override
public String[] getSeriesUsedFiles(boolean noPixels) {
    FormatTools.assertId(currentId, true, 1);
    ArrayList<String> files = new ArrayList<String>();
    try {
        CaseInsensitiveLocation xml = new CaseInsensitiveLocation(tiffInfo.xmlname);
        if (xml.exists()) {
            files.add(xml.getAbsolutePath());
        }
    } catch (IOException e) {
        LOGGER.debug("Error checking existence of " + tiffInfo.xmlname, e);
    }
    try {
        CaseInsensitiveLocation origname = new CaseInsensitiveLocation(tiffInfo.origname);
        if (!noPixels && tiffInfo.origname != null && origname.exists()) {
            files.add(origname.getAbsolutePath());
        }
    } catch (IOException e) {
        LOGGER.debug("Error checking existence of " + tiffInfo.origname, e);
    }
    if (!noPixels) {
        for (String tiff : imageFiles) {
            try {
                CaseInsensitiveLocation tiffLocation = new CaseInsensitiveLocation(tiff);
                if (tiffLocation.exists()) {
                    files.add(tiffLocation.getAbsolutePath());
                }
            } catch (IOException e) {
                LOGGER.debug("Error checking existence of " + tiff, e);
            }
        }
    }
    return files.toArray(new String[files.size()]);
}
Also used : ArrayList(java.util.ArrayList) CaseInsensitiveLocation(loci.common.CaseInsensitiveLocation) IOException(java.io.IOException)

Example 3 with CaseInsensitiveLocation

use of loci.common.CaseInsensitiveLocation in project bioformats by openmicroscopy.

the class ZeissTIFFReader method fillMetadataPass1.

@Override
protected void fillMetadataPass1(MetadataStore store) throws FormatException, IOException {
    super.fillMetadataPass1(store);
    int nplanes = tiffInfo.handler.planes.size();
    if (rawCount == 0)
        rawCount = nplanes;
    else if (rawCount != nplanes)
        LOGGER.warn("Problem determining correct number of image planes.  Document reported {}, while {} were found", rawCount, nplanes);
    if (rawCount == 0)
        LOGGER.warn("No image planes found");
    // Determine number of separate timepoints, channels, and z slices.
    for (ZeissTIFFHandler.Plane p : tiffInfo.handler.planes) {
        Plane np = new Plane();
        boolean tag_1047_present = false;
        for (Tag t : p.tagset.tags) {
            if (t.getKeyID() == 1047)
                tag_1047_present = true;
        }
        for (Tag t : p.tagset.tags) {
            // better to use the latter.
            if (t.getKeyID() != 1025 || tag_1047_present == false)
                np.tags.put(t.getKey(), t.getValue());
        }
        np.taglist = p.tagset.tags;
        // Other planes: _files/_meta
        if (nplanes == 1 && tiffInfo.multifile == false)
            np.filename = tiffInfo.origname;
        else
            np.filename = new CaseInsensitiveLocation(tiffInfo.basedir + "/" + tiffInfo.prefix + "_" + p.basename + ".tif").getAbsolutePath();
        int tileid = parseInt(np.tags.get("ImageTile Index"));
        int channelid = parseInt(np.tags.get("Image Channel Index"));
        int sliceid = parseInt(np.tags.get("Image Index Z"));
        int timepointid = parseInt(np.tags.get("Image Index T"));
        int xsize = parseInt(np.tags.get("Camera Frame Width"));
        int ysize = parseInt(np.tags.get("Camera Frame Height"));
        np.site = tileid;
        tileIndices.add(tileid);
        channelIndices.add(channelid);
        zIndices.add(sliceid);
        timepointIndices.add(timepointid);
        if (getSizeX() == 0) {
            core.get(0).sizeX = xsize;
            core.get(0).sizeY = ysize;
        }
        planes.add(np);
        if (bpp == 0) {
            tiffReader.setId(np.filename);
            IFDList ifds = tiffReader.getIFDs();
            tiffReader.close();
            IFD firstIFD = ifds.get(0);
            int bits = firstIFD.getBitsPerSample()[0];
            int samples = firstIFD.getSamplesPerPixel();
            bpp = (bits / 8) * samples;
        }
    }
    // Filter out images with an incomplete number of z slices.
    // This is to work around a probable bug in AxioVision.
    int full = Collections.max(tileIndices);
    int[] indexCount = new int[full + 1];
    int max = 0;
    for (Plane plane : planes) {
        indexCount[plane.site]++;
        if (indexCount[plane.site] > max)
            max = indexCount[plane.site];
    }
    for (int i = 0; i < full + 1; i++) {
        if (indexCount[i] != max)
            tileIndices.remove(i);
    }
    for (Iterator<Plane> i = planes.iterator(); i.hasNext(); ) {
        Plane plane = i.next();
        if (!tileIndices.contains(plane.site)) {
            i.remove();
        }
    }
    // Allocates memory for arrays below
    countImages();
    for (int i = 0; i < planes.size(); i++) {
        Plane plane = planes.get(i);
        int channelid = parseInt(plane.tags.get("Image Channel Index"));
        int sliceid = parseInt(plane.tags.get("Image Index Z"));
        int timepointid = parseInt(plane.tags.get("Image Index T"));
        coordinates[i][0] = sliceid;
        coordinates[i][1] = channelid;
        coordinates[i][2] = timepointid;
        imageFiles[i] = plane.filename;
    }
    int total = tileIndices.size() * channelIndices.size() * zIndices.size() * timepointIndices.size();
    if (total != planes.size())
        LOGGER.warn("Number of image planes not detected correctly: total={} planes.size={}", total, planes.size());
}
Also used : IFD(loci.formats.tiff.IFD) CaseInsensitiveLocation(loci.common.CaseInsensitiveLocation) IFDList(loci.formats.tiff.IFDList)

Example 4 with CaseInsensitiveLocation

use of loci.common.CaseInsensitiveLocation in project bioformats by openmicroscopy.

the class ZeissTIFFReader method openBytes.

/**
 * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
 */
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    if (new CaseInsensitiveLocation(imageFiles[no]).exists()) {
        Plane p = planes.get(no);
        tiffReader.setId(p.filename);
        tiffReader.openBytes(0, buf, x, y, w, h);
        tiffReader.close();
    } else {
        LOGGER.warn("File for image #{} ({}) is missing.", no, imageFiles[no]);
    }
    return buf;
}
Also used : CaseInsensitiveLocation(loci.common.CaseInsensitiveLocation)

Aggregations

CaseInsensitiveLocation (loci.common.CaseInsensitiveLocation)4 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 FormatException (loci.formats.FormatException)1 IFD (loci.formats.tiff.IFD)1 IFDList (loci.formats.tiff.IFDList)1