Search in sources :

Example 16 with IFD

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

the class FlexReader method parseFlexFile.

/**
 * Parses XML metadata from the Flex file corresponding to the given well.
 * If the 'firstFile' flag is set, then the core metadata is also
 * populated.
 */
private void parseFlexFile(int currentWell, int wellRow, int wellCol, int field, boolean firstFile, MetadataStore store) throws FormatException, IOException {
    LOGGER.info("Parsing .flex file (well {}{}, field {})", (char) (wellRow + 'A'), wellCol + 1, field);
    FlexFile file = lookupFile(wellRow, wellCol, field < 0 ? 0 : field);
    if (file == null)
        return;
    int originalFieldCount = fieldCount;
    if (xPositions == null)
        xPositions = new ArrayList<Double>();
    if (yPositions == null)
        yPositions = new ArrayList<Double>();
    if (xSizes == null)
        xSizes = new ArrayList<Double>();
    if (ySizes == null)
        ySizes = new ArrayList<Double>();
    if (cameraIDs == null)
        cameraIDs = new ArrayList<String>();
    if (lightSourceIDs == null)
        lightSourceIDs = new ArrayList<String>();
    if (objectiveIDs == null)
        objectiveIDs = new ArrayList<String>();
    if (lightSourceCombinationIDs == null) {
        lightSourceCombinationIDs = new HashMap<String, List<String>>();
    }
    if (lightSourceCombinationRefs == null) {
        lightSourceCombinationRefs = new ArrayList<String>();
    }
    if (cameraRefs == null)
        cameraRefs = new ArrayList<String>();
    if (objectiveRefs == null)
        objectiveRefs = new ArrayList<String>();
    if (binnings == null)
        binnings = new ArrayList<String>();
    if (filterSets == null)
        filterSets = new ArrayList<String>();
    if (filterSetMap == null)
        filterSetMap = new HashMap<String, FilterGroup>();
    // parse factors from XML
    LOGGER.debug("Parsing XML from {}", file.file);
    int nOffsets = file.offsets != null ? file.offsets.length : 0;
    IFD ifd = null;
    if (nOffsets == 0) {
        ifd = file.ifds.get(0);
    } else {
        RandomAccessInputStream ras = new RandomAccessInputStream(file.file);
        try {
            TiffParser parser = new TiffParser(ras);
            ifd = parser.getFirstIFD();
        } finally {
            ras.close();
        }
    }
    String xml = XMLTools.sanitizeXML(ifd.getIFDStringValue(FLEX));
    final List<String> n = new ArrayList<String>();
    final List<String> f = new ArrayList<String>();
    DefaultHandler handler = new FlexHandler(n, f, store, firstFile, currentWell, field);
    LOGGER.info("Parsing XML in .flex file");
    xml = xml.trim();
    // some files have a trailing ">" or "%", which needs to be removed
    if (xml.endsWith(">>") || xml.endsWith("%")) {
        xml = xml.substring(0, xml.length() - 1);
    }
    XMLTools.parseXML(xml.getBytes(Constants.ENCODING), handler);
    channelNames = n.toArray(new String[n.size()]);
    if (firstFile) {
        populateCoreMetadata(wellRow, wellCol, field < 0 ? 0 : field, n);
    }
    int totalPlanes = getSeriesCount() * getImageCount();
    LOGGER.info("Populating pixel scaling factors");
    // verify factor count
    int nsize = n.size();
    int fsize = f.size();
    if (nsize != fsize || nsize != totalPlanes) {
        LOGGER.warn("mismatch between image count, names and factors " + "(count={}, names={}, factors={})", new Object[] { totalPlanes, nsize, fsize });
    }
    if (firstFile) {
        for (String ns : n) {
            addGlobalMetaList("Name", ns);
        }
        for (String fs : f) {
            addGlobalMetaList("Factor", fs);
        }
    }
    // parse factor values
    file.factors = new double[totalPlanes];
    int max = 0;
    boolean oneFactors = true;
    for (int i = 0; i < fsize; i++) {
        String factor = f.get(i);
        double q = 1;
        try {
            q = Double.parseDouble(factor);
        } catch (NumberFormatException exc) {
            LOGGER.warn("invalid factor #{}: {}", i, factor);
        }
        if (i < file.factors.length) {
            file.factors[i] = q;
            if (q > file.factors[max])
                max = i;
            if (oneFactors && q != 1d) {
                oneFactors = false;
            }
        }
    }
    if (fsize < file.factors.length) {
        Arrays.fill(file.factors, fsize, file.factors.length, 1);
    }
    // determine pixel type
    if (file.factors[max] > 256) {
        core.get(0).pixelType = FormatTools.UINT32;
    } else if (file.factors[max] > 1) {
        core.get(0).pixelType = FormatTools.UINT16;
    }
    for (int i = 1; i < core.size(); i++) {
        core.get(i).pixelType = getPixelType();
    }
    if (!firstFile) {
        fieldCount = originalFieldCount;
    }
    if (oneFactors) {
        file.factors = null;
    }
}
Also used : HashMap(java.util.HashMap) IFD(loci.formats.tiff.IFD) ArrayList(java.util.ArrayList) DefaultHandler(org.xml.sax.helpers.DefaultHandler) ArrayList(java.util.ArrayList) IFDList(loci.formats.tiff.IFDList) List(java.util.List) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 17 with IFD

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

the class FlexReader method getOptimalTileHeight.

/* @see loci.formats.IFormatReader#getOptimalTileHeight() */
@Override
public int getOptimalTileHeight() {
    FormatTools.assertId(currentId, true, 1);
    FlexFile file = lookupFile(0);
    IFD ifd = file.ifds.get(0);
    try {
        return (int) ifd.getTileLength();
    } catch (FormatException e) {
        LOGGER.debug("Could not retrieve tile height", e);
    }
    return super.getOptimalTileHeight();
}
Also used : IFD(loci.formats.tiff.IFD) FormatException(loci.formats.FormatException)

Example 18 with IFD

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

the class ImaconReader method isThisType.

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

Example 19 with IFD

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

the class PCIReader method getOptimalTileWidth.

/* @see loci.formats.IFormatReader#getOptimalTileWidth() */
@Override
public int getOptimalTileWidth() {
    FormatTools.assertId(currentId, true, 1);
    String file = imageFiles.get(0);
    try {
        if (poi == null) {
            initPOIService();
        }
        RandomAccessInputStream s = poi.getDocumentStream(file);
        TiffParser tp = new TiffParser(s);
        if (tp.isValidHeader()) {
            IFD ifd = tp.getFirstIFD();
            s.close();
            return (int) ifd.getTileWidth();
        }
        s.close();
    } catch (FormatException e) {
        LOGGER.debug("Could not retrieve tile width", e);
    } catch (IOException e) {
        LOGGER.debug("Could not retrieve tile width", e);
    }
    return super.getOptimalTileWidth();
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) FormatException(loci.formats.FormatException)

Example 20 with IFD

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

the class PCIReader 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 (poi == null) {
        initPOIService();
    }
    String file = imageFiles.get(no);
    RandomAccessInputStream s = poi.getDocumentStream(file);
    TiffParser tp = new TiffParser(s);
    if (tp.isValidHeader()) {
        IFD ifd = tp.getFirstIFD();
        tp.getSamples(ifd, buf, x, y, w, h);
    } else {
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
    }
    s.close();
    return buf;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

IFD (loci.formats.tiff.IFD)121 TiffParser (loci.formats.tiff.TiffParser)74 RandomAccessInputStream (loci.common.RandomAccessInputStream)51 CoreMetadata (loci.formats.CoreMetadata)33 FormatException (loci.formats.FormatException)32 MetadataStore (loci.formats.meta.MetadataStore)21 IFDList (loci.formats.tiff.IFDList)21 PhotoInterp (loci.formats.tiff.PhotoInterp)18 IOException (java.io.IOException)17 Location (loci.common.Location)15 ArrayList (java.util.ArrayList)14 Timestamp (ome.xml.model.primitives.Timestamp)11 Length (ome.units.quantity.Length)10 Time (ome.units.quantity.Time)8 TiffIFDEntry (loci.formats.tiff.TiffIFDEntry)7 TiffRational (loci.formats.tiff.TiffRational)6 File (java.io.File)5 TiffReader (loci.formats.in.TiffReader)5 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)5 PositiveInteger (ome.xml.model.primitives.PositiveInteger)5