Search in sources :

Example 6 with TiffParser

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

the class NDPIReader 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 (x == 0 && y == 0 && w == 1 && h == 1) {
        return buf;
    } else if (getSizeX() <= MAX_SIZE || getSizeY() <= MAX_SIZE) {
        int ifdIndex = getIFDIndex(getCoreIndex(), no);
        in = new RandomAccessInputStream(currentId);
        tiffParser = new TiffParser(in);
        tiffParser.setUse64BitOffsets(true);
        tiffParser.setYCbCrCorrection(false);
        byte[] b = tiffParser.getSamples(ifds.get(ifdIndex), buf, x, y, w, h);
        in.close();
        tiffParser.getStream().close();
        return b;
    }
    if (initializedSeries != getCoreIndex() || initializedPlane != no) {
        IFD ifd = ifds.get(getIFDIndex(getCoreIndex(), no));
        long offset = ifd.getStripOffsets()[0];
        long byteCount = ifd.getStripByteCounts()[0];
        if (in != null) {
            in.close();
        }
        in = new RandomAccessInputStream(currentId);
        in.seek(offset);
        in.setLength(offset + byteCount);
        try {
            service.close();
            long[] markers = ifd.getIFDLongArray(MARKER_TAG);
            if (!use64Bit) {
                for (int i = 0; i < markers.length; i++) {
                    markers[i] = markers[i] & 0xffffffffL;
                }
            }
            if (markers != null) {
                service.setRestartMarkers(markers);
            }
            service.initialize(in, getSizeX(), getSizeY());
        } catch (ServiceException e) {
            throw new FormatException(e);
        }
        initializedSeries = getCoreIndex();
        initializedPlane = no;
    }
    service.getTile(buf, x, y, w, h);
    return buf;
}
Also used : ServiceException(loci.common.services.ServiceException) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) FormatException(loci.formats.FormatException)

Example 7 with TiffParser

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

the class NikonElementsTiffReader 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;
    return ifd.containsKey(NIKON_XML_TAG);
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Example 8 with TiffParser

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

the class NikonReader 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;
    if (ifd.containsKey(TIFF_EPS_STANDARD))
        return true;
    String make = ifd.getIFDTextValue(IFD.MAKE);
    return make != null && make.indexOf("Nikon") != -1;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Example 9 with TiffParser

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

the class NikonReader method initStandardMetadata.

// -- Internal BaseTiffReader API methods --
/* @see BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
    super.initStandardMetadata();
    // reset image dimensions
    // the actual image data is stored in IFDs referenced by the SubIFD tag
    // in the 'real' IFD
    CoreMetadata m = core.get(0);
    m.imageCount = ifds.size();
    IFD firstIFD = ifds.get(0);
    PhotoInterp photo = firstIFD.getPhotometricInterpretation();
    int samples = firstIFD.getSamplesPerPixel();
    m.rgb = samples > 1 || photo == PhotoInterp.RGB || photo == PhotoInterp.CFA_ARRAY;
    if (photo == PhotoInterp.CFA_ARRAY)
        samples = 3;
    m.sizeX = (int) firstIFD.getImageWidth();
    m.sizeY = (int) firstIFD.getImageLength();
    m.sizeZ = 1;
    m.sizeC = isRGB() ? samples : 1;
    m.sizeT = ifds.size();
    m.pixelType = firstIFD.getPixelType();
    m.indexed = false;
    // now look for the EXIF IFD pointer
    IFDList exifIFDs = tiffParser.getExifIFDs();
    if (exifIFDs.size() > 0) {
        IFD exifIFD = exifIFDs.get(0);
        tiffParser.fillInIFD(exifIFD);
        for (Integer key : exifIFD.keySet()) {
            int tag = key.intValue();
            String name = IFD.getIFDTagName(tag);
            if (tag == IFD.CFA_PATTERN) {
                byte[] cfa = (byte[]) exifIFD.get(key);
                int[] colorMap = new int[cfa.length];
                for (int i = 0; i < cfa.length; i++) colorMap[i] = (int) cfa[i];
                addGlobalMeta(name, colorMap);
                cfaPattern = colorMap;
            } else {
                addGlobalMeta(name, exifIFD.get(key));
                if (name.equals("MAKER_NOTE")) {
                    byte[] b = (byte[]) exifIFD.get(key);
                    int extra = new String(b, 0, 10, Constants.ENCODING).startsWith("Nikon") ? 10 : 0;
                    byte[] buf = new byte[b.length];
                    System.arraycopy(b, extra, buf, 0, buf.length - extra);
                    RandomAccessInputStream makerNote = new RandomAccessInputStream(buf);
                    TiffParser tp = new TiffParser(makerNote);
                    IFD note = null;
                    try {
                        note = tp.getFirstIFD();
                    } catch (Exception e) {
                        LOGGER.debug("Failed to parse first IFD", e);
                    }
                    if (note != null) {
                        for (Integer nextKey : note.keySet()) {
                            int nextTag = nextKey.intValue();
                            addGlobalMeta(name, note.get(nextKey));
                            if (nextTag == 150) {
                                b = (byte[]) note.get(nextKey);
                                RandomAccessInputStream s = new RandomAccessInputStream(b);
                                byte check1 = s.readByte();
                                byte check2 = s.readByte();
                                lossyCompression = check1 != 0x46;
                                vPredictor = new int[4];
                                for (int q = 0; q < vPredictor.length; q++) {
                                    vPredictor[q] = s.readShort();
                                }
                                curve = new int[16385];
                                int bps = ifds.get(0).getBitsPerSample()[0];
                                int max = 1 << bps & 0x7fff;
                                int step = 0;
                                int csize = s.readShort();
                                if (csize > 1) {
                                    step = max / (csize - 1);
                                }
                                if (check1 == 0x44 && check2 == 0x20 && step > 0) {
                                    for (int i = 0; i < csize; i++) {
                                        curve[i * step] = s.readShort();
                                    }
                                    for (int i = 0; i < max; i++) {
                                        int n = i % step;
                                        curve[i] = (curve[i - n] * (step - n) + curve[i - n + step] * n) / step;
                                    }
                                    s.seek(562);
                                    split = s.readShort();
                                } else {
                                    int maxValue = (int) Math.pow(2, bps) - 1;
                                    Arrays.fill(curve, maxValue);
                                    int nElements = (int) (s.length() - s.getFilePointer()) / 2;
                                    if (nElements < 100) {
                                        for (int i = 0; i < curve.length; i++) {
                                            curve[i] = (short) i;
                                        }
                                    } else {
                                        for (int q = 0; q < nElements; q++) {
                                            curve[q] = s.readShort();
                                        }
                                    }
                                }
                                s.close();
                            } else if (nextTag == WHITE_BALANCE_RGB_COEFFS) {
                                whiteBalance = (TiffRational[]) note.get(nextKey);
                            }
                        }
                    }
                    makerNote.close();
                }
            }
        }
    }
}
Also used : IFD(loci.formats.tiff.IFD) PhotoInterp(loci.formats.tiff.PhotoInterp) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) IOException(java.io.IOException) IFDList(loci.formats.tiff.IFDList) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 10 with TiffParser

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

the class OperettaReader method isThisType.

/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
    TiffParser p = new TiffParser(stream);
    IFD ifd = p.getFirstIFD();
    if (ifd == null)
        return false;
    Object s = ifd.getIFDValue(XML_TAG);
    if (s == null)
        return false;
    String xml = s instanceof String[] ? ((String[]) s)[0] : s.toString();
    return xml.indexOf("Operetta") < 1024;
}
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