Search in sources :

Example 76 with TiffParser

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

the class IonpathMIBITiffReader method isThisType.

// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser tiffParser = new TiffParser(stream);
    IFD ifd = tiffParser.getFirstIFD();
    if (ifd == null)
        return false;
    String software = ifd.getIFDTextValue(IFD.SOFTWARE);
    if (software == null)
        return false;
    return software.startsWith(IONPATH_MIBI_SOFTWARE_PREFIX);
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Example 77 with TiffParser

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

the class FlexReader 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);
    FlexFile firstFile = lookupFile(0);
    FlexFile file = lookupFile(getSeries());
    int[] lengths = new int[] { fieldCount / effectiveFieldCount, wellCount, plateCount };
    int[] pos = FormatTools.rasterToPosition(lengths, getSeries());
    int imageNumber = file.offsets == null ? getImageCount() * pos[0] + no : 0;
    RandomAccessInputStream s = new RandomAccessInputStream(getFileHandle(file.file));
    IFD ifd;
    double factor;
    if (file.offsets == null) {
        ifd = file.ifds.get(imageNumber);
        factor = 1d;
    } else {
        // Only the first IFD was read. Hack the IFD to adjust the offset.
        final IFD firstIFD = firstFile.ifds.get(0);
        ifd = new IFD(firstIFD);
        int tag = IFD.STRIP_OFFSETS;
        if (firstIFD.isTiled() && firstIFD.getIFDLongArray(IFD.TILE_OFFSETS) != null) {
            tag = IFD.TILE_OFFSETS;
        }
        long[] offsets = ifd.getIFDLongArray(tag);
        final int planeSize = getSizeX() * getSizeY() * getRGBChannelCount() * ifd.getBitsPerSample()[0] / 8;
        final int index = getImageCount() * pos[0] + no;
        long offset = (index == file.offsets.length - 1 ? s.length() : file.offsets[index + 1]) - offsets[0] - planeSize;
        for (int i = 0; i < offsets.length; i++) {
            offsets[i] += offset;
        }
        ifd.putIFDValue(tag, offsets);
    }
    int nBytes = ifd.getBitsPerSample()[0] / 8;
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    // read pixels from the file
    TiffParser tp = new TiffParser(s);
    tp.fillInIFD(ifd);
    // log the first offset used
    LOGGER.trace("first offset for series={} no={}: {}", getCoreIndex(), no, ifd.getStripOffsets()[0]);
    tp.getSamples(ifd, buf, x, y, w, h);
    factor = file.factors == null ? 1d : file.factors[imageNumber];
    LOGGER.trace("  using factor = {}", factor);
    tp.getStream().close();
    // expand pixel values with multiplication by factor[no]
    int num = buf.length / bpp;
    if (factor != 1d || nBytes != bpp) {
        for (int i = num - 1; i >= 0; i--) {
            int q = nBytes == 1 ? buf[i] & 0xff : DataTools.bytesToInt(buf, i * nBytes, nBytes, isLittleEndian());
            q = (int) (q * factor);
            DataTools.unpackBytes(q, buf, i * bpp, bpp, isLittleEndian());
        }
    }
    s.close();
    return buf;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 78 with TiffParser

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

the class FlexReader method groupFiles.

private void groupFiles(String[] fileList, MetadataStore store) throws FormatException, IOException {
    LOGGER.info("Grouping together files in the same dataset");
    HashMap<String, ArrayList<String>> v = new HashMap<String, ArrayList<String>>();
    Boolean firstCompressed = null;
    int firstIFDCount = 0;
    for (String file : fileList) {
        LOGGER.warn("parsing {}", file);
        RandomAccessInputStream s = new RandomAccessInputStream(file, 16);
        TiffParser parser = new TiffParser(s);
        IFD firstIFD = parser.getFirstIFD();
        int ifdCount = parser.getIFDOffsets().length;
        s.close();
        boolean compressed = firstIFD.getCompression() != TiffCompression.UNCOMPRESSED;
        if (firstCompressed == null) {
            firstCompressed = compressed;
            firstIFDCount = ifdCount;
        }
        if (compressed == firstCompressed && ifdCount == firstIFDCount) {
            int[] well = getWell(file);
            int field = getField(file);
            if (well[0] > nRows)
                nRows = well[0];
            if (well[1] > nCols)
                nCols = well[1];
            if (fileList.length == 1) {
                well[0] = 0;
                well[1] = 0;
            }
            ArrayList<String> files = v.get(well[0] + "," + well[1]);
            if (files == null) {
                files = new ArrayList<String>();
            }
            files.add(file);
            v.put(well[0] + "," + well[1], files);
        } else {
            v.clear();
            ArrayList<String> files = new ArrayList<String>();
            files.add(currentId);
            v.put("0,0", files);
            fileList = new String[] { currentId };
            break;
        }
    }
    nRows++;
    nCols++;
    if (fileList.length == 1) {
        nRows = 1;
        nCols = 1;
    }
    LOGGER.debug("Determined that there are {} rows and {} columns of wells.", nRows, nCols);
    flexFiles = new ArrayList<FlexFile>();
    wellCount = v.size();
    wellNumber = new int[wellCount][2];
    RandomAccessInputStream s = null;
    boolean firstFile = true;
    boolean compressed = false;
    int nOffsets = 1;
    int currentWell = 0;
    for (int row = 0; row < nRows; row++) {
        for (int col = 0; col < nCols; col++) {
            ArrayList<String> files = v.get(row + "," + col);
            if (files == null) {
                continue;
            }
            nFiles = files.size();
            String[] sortedFiles = files.toArray(new String[files.size()]);
            Arrays.sort(sortedFiles);
            files.clear();
            for (String f : sortedFiles) {
                files.add(f);
            }
            for (int field = 0; field < nFiles; field++) {
                FlexFile file = new FlexFile();
                file.row = row;
                file.column = col;
                file.field = field;
                file.file = files.get(field);
                if (file.file == null) {
                    continue;
                }
                wellNumber[currentWell][0] = row;
                wellNumber[currentWell][1] = col;
                s = new RandomAccessInputStream(getFileHandle(file.file));
                TiffParser tp = new TiffParser(s);
                if (compressed || firstFile) {
                    LOGGER.info("Parsing IFDs for well {}{}", (char) (row + 'A'), col + 1);
                    IFD firstIFD = tp.getFirstIFD();
                    compressed = firstIFD.getCompression() != TiffCompression.UNCOMPRESSED;
                    if (compressed || firstIFD.getStripOffsets()[0] == 16 || firstIFD.getStripOffsets().length == 1) {
                        tp.setDoCaching(false);
                        file.ifds = tp.getIFDs();
                        file.ifds.set(0, firstIFD);
                        if (firstIFD.getStripOffsets().length == 1) {
                            // used to ensure that image offsets are read, not calculated
                            compressed = true;
                        }
                    } else {
                        // if the pixel data is uncompressed and the IFD is stored
                        // before the image, we can assume that
                        // the pixel data for image #0 is located immediately before
                        // IFD #1; as a result, we only need to parse the first IFD
                        file.offsets = tp.getIFDOffsets();
                        nOffsets = file.offsets.length;
                        file.ifds = new IFDList();
                        file.ifds.add(firstIFD);
                    }
                } else {
                    // retrieve the offsets to each IFD, instead of parsing
                    // all of the IFDs
                    LOGGER.info("Retrieving IFD offsets for well {}{}", (char) (row + 'A'), col + 1);
                    file.offsets = new long[nOffsets];
                    // Assume that all IFDs after the first are evenly spaced.
                    // TiffParser.getIFDOffsets() could be used instead, but is
                    // substantially slower.
                    file.offsets[0] = tp.getFirstOffset();
                    if (file.offsets.length > 1) {
                        s.seek(file.offsets[0]);
                        s.skipBytes(s.readShort() * TiffConstants.BYTES_PER_ENTRY);
                        file.offsets[1] = s.readInt();
                        int size = FormatTools.getPlaneSize(this) + 174;
                        for (int i = 2; i < file.offsets.length; i++) {
                            file.offsets[i] = file.offsets[i - 1] + size;
                        }
                    }
                }
                flexFiles.add(file);
                // setting a negative field index indicates that the field count
                // should be taken from the XML
                parseFlexFile(currentWell, row, col, nFiles == 1 ? -1 : field, firstFile, store);
                s.close();
                if (firstFile)
                    firstFile = false;
            }
            currentWell++;
        }
    }
}
Also used : HashMap(java.util.HashMap) IFD(loci.formats.tiff.IFD) ArrayList(java.util.ArrayList) IFDList(loci.formats.tiff.IFDList) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 79 with TiffParser

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

the class FluoviewReader method isThisType.

// -- IFormatReader API methods --
/* @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;
    String com = ifd.getComment();
    if (com == null)
        com = "";
    return (com.indexOf(FLUOVIEW_MAGIC_STRING) != -1 && ifd.containsKey(new Integer(MMHEADER)) || ifd.containsKey(new Integer(MMSTAMP))) || com.startsWith(ANDOR_MAGIC_STRING);
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Example 80 with TiffParser

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

the class GelReader 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(MD_FILETAG);
}
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