Search in sources :

Example 81 with TiffParser

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

the class MetamorphTiffReader 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 {
    if (getSeriesCount() == 1 && files.length == 1 && !dualCamera) {
        return super.openBytes(no, buf, x, y, w, h);
    }
    int[] lengths = new int[] { getSizeZ(), getEffectiveSizeC(), fieldColumnCount, fieldRowCount, wellCount, getSizeT() };
    int[] zct = getZCTCoords(no);
    Well well = getWell(getSeries());
    int[] position = new int[] { zct[0], zct[1], well.fieldCol, well.fieldRow, well.well, zct[2] };
    if (dualCamera) {
        position[1] = 0;
    }
    int fileIndex = FormatTools.positionToRaster(lengths, position);
    final RandomAccessInputStream s;
    if (fileIndex < files.length) {
        s = new RandomAccessInputStream(files[fileIndex]);
    } else {
        s = new RandomAccessInputStream(files[0]);
    }
    TiffParser parser = new TiffParser(s);
    int ndx = getSeries() * getSizeZ() * getSizeT() + no;
    int[] pos = getZCTCoords(no);
    if (dualCamera) {
        int channel = pos[1];
        pos[1] = 0;
        ndx = getSeries() * getSizeZ() * getSizeT() + FormatTools.positionToRaster(new int[] { getSizeZ(), 1, getSizeT() }, pos);
        pos[1] = channel;
    }
    IFD ifd = files.length == 1 ? ifds.get(ndx) : parser.getFirstIFD();
    int realX = dualCamera ? (pos[1] == 0 ? x : x + pos[1] * getSizeX()) : x;
    parser.getSamples(ifd, buf, realX, y, w, h);
    s.close();
    return buf;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 82 with TiffParser

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

the class MetamorphTiffReader method parseFile.

private void parseFile(String tiff, MetamorphHandler handler) throws IOException {
    RandomAccessInputStream s = new RandomAccessInputStream(tiff);
    TiffParser parser = new TiffParser(s);
    IFD firstIFD = parser.getFirstIFD();
    String xml = XMLTools.sanitizeXML(firstIFD.getComment());
    XMLTools.parseXML(xml, handler);
    s.close();
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 83 with TiffParser

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

the class MetamorphTiffReader method isThisType.

// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser tp = new TiffParser(stream);
    String comment = tp.getComment();
    if (comment == null)
        return false;
    comment = comment.trim();
    return comment.startsWith("<MetaData>") && comment.endsWith("</MetaData>");
}
Also used : TiffParser(loci.formats.tiff.TiffParser)

Example 84 with TiffParser

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

the class NDPIReader method isThisType.

// -- IFormatReader API methods --
/* (non-Javadoc)
   * @see loci.formats.FormatReader#isThisType(java.lang.String, boolean)
   */
@Override
public boolean isThisType(String name, boolean open) {
    boolean isThisType = super.isThisType(name, open);
    if (isThisType && open) {
        RandomAccessInputStream stream = null;
        TiffParser parser = null;
        try {
            stream = new RandomAccessInputStream(name);
            parser = new TiffParser(stream);
            parser.setDoCaching(false);
            parser.setUse64BitOffsets(stream.length() >= Math.pow(2, 32));
            if (!parser.isValidHeader()) {
                return false;
            }
            IFD ifd = parser.getFirstIFD();
            if (ifd == null) {
                return false;
            }
            return ifd.containsKey(MARKER_TAG);
        } catch (IOException e) {
            LOGGER.debug("I/O exception during isThisType() evaluation.", e);
            return false;
        } finally {
            try {
                if (stream != null) {
                    stream.close();
                }
                if (parser != null) {
                    parser.getStream().close();
                }
            } catch (IOException e) {
                LOGGER.debug("I/O exception during stream closure.", e);
            }
        }
    }
    return isThisType;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException)

Example 85 with TiffParser

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

the class NDPISReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    Location parent = new Location(id).getAbsoluteFile().getParentFile();
    String[] lines = DataTools.readFile(currentId).split("\r\n");
    for (String line : lines) {
        int eq = line.indexOf('=');
        if (eq < 0) {
            continue;
        }
        String key = line.substring(0, eq).trim();
        String value = line.substring(eq + 1).trim();
        if (key.equals("NoImages")) {
            ndpiFiles = new String[Integer.parseInt(value)];
            readers = new ChannelSeparator[ndpiFiles.length];
        } else if (key.startsWith("Image")) {
            int index = Integer.parseInt(key.replaceAll("Image", ""));
            ndpiFiles[index] = new Location(parent, value).getAbsolutePath();
            readers[index] = new ChannelSeparator(new NDPIReader());
            readers[index].setFlattenedResolutions(hasFlattenedResolutions());
        }
    }
    MetadataStore store = makeFilterMetadata();
    readers[0].getReader().setMetadataStore(store);
    readers[0].setId(ndpiFiles[0]);
    core = new ArrayList<CoreMetadata>(readers[0].getCoreMetadataList());
    for (int i = 0; i < core.size(); i++) {
        CoreMetadata ms = core.get(i);
        ms.sizeC = readers.length;
        ms.rgb = false;
        ms.imageCount = ms.sizeC * ms.sizeZ * ms.sizeT;
    }
    MetadataTools.populatePixels(store, this);
    bandUsed = new int[ndpiFiles.length];
    IFD ifd;
    for (int c = 0; c < readers.length; c++) {
        // populate channel names based on IFD entry
        try (RandomAccessInputStream in = new RandomAccessInputStream(ndpiFiles[c])) {
            TiffParser tp = new TiffParser(in);
            ifd = tp.getIFDs().get(0);
        }
        String channelName = ifd.getIFDStringValue(TAG_CHANNEL);
        Float wavelength = (Float) ifd.getIFDValue(TAG_EMISSION_WAVELENGTH);
        store.setChannelName(channelName, 0, c);
        store.setChannelEmissionWavelength(new Length(wavelength, UNITS.NANOMETER), 0, c);
        bandUsed[c] = 0;
        if (ifd.getSamplesPerPixel() >= 3) {
            // 580 < wavelength <= 780 Red
            if (380 < wavelength && wavelength <= 490)
                bandUsed[c] = 2;
            else if (490 < wavelength && wavelength <= 580)
                bandUsed[c] = 1;
            else if (580 < wavelength && wavelength <= 780)
                bandUsed[c] = 0;
        }
    }
}
Also used : IFD(loci.formats.tiff.IFD) CoreMetadata(loci.formats.CoreMetadata) ChannelSeparator(loci.formats.ChannelSeparator) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

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