Search in sources :

Example 41 with TiffParser

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

the class DNGReader 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 = FormatTools.UINT16;
    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 offset = DataTools.bytesToInt(b, b.length - 4, isLittleEndian());
                    byte[] buf = new byte[b.length + offset - 8];
                    System.arraycopy(b, b.length - 8, buf, 0, 8);
                    System.arraycopy(b, 0, buf, offset, b.length - 8);
                    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 == WHITE_BALANCE_RGB_COEFFS) {
                                if (note.get(nextTag) instanceof TiffRational[]) {
                                    TiffRational[] wb = (TiffRational[]) note.get(nextTag);
                                    whiteBalance = new double[wb.length];
                                    for (int i = 0; i < wb.length; i++) {
                                        whiteBalance[i] = wb[i].doubleValue();
                                    }
                                } else {
                                    // use a default white balance table
                                    whiteBalance = new double[3];
                                    whiteBalance[0] = 2.391381;
                                    whiteBalance[1] = 0.929156;
                                    whiteBalance[2] = 1.298254;
                                }
                            }
                        }
                    }
                    makerNote.close();
                }
            }
        }
    }
}
Also used : IFD(loci.formats.tiff.IFD) PhotoInterp(loci.formats.tiff.PhotoInterp) TiffRational(loci.formats.tiff.TiffRational) 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 42 with TiffParser

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

the class CellSensReader method reopenFile.

/* @see loci.formats.IFormatReader#reopenFile() */
public void reopenFile() throws IOException {
    super.reopenFile();
    parser = new TiffParser(currentId);
}
Also used : TiffParser(loci.formats.tiff.TiffParser)

Example 43 with TiffParser

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

the class APLReader 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 (parser == null) {
        parser = new TiffParser[getSeriesCount()];
    }
    if (parser[getSeries()] == null) {
        parser[getSeries()] = new TiffParser(tiffFiles[getSeries()]);
        parser[getSeries()].setDoCaching(false);
    }
    IFD ifd = ifds[getSeries()].get(no);
    return parser[getSeries()].getSamples(ifd, buf, x, y, w, h);
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser)

Example 44 with TiffParser

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

the class BDReader method getTimestamp.

private long getTimestamp(String file) throws FormatException, IOException {
    RandomAccessInputStream s = new RandomAccessInputStream(file, 16);
    TiffParser parser = new TiffParser(s);
    parser.setDoCaching(false);
    IFD firstIFD = parser.getFirstIFD();
    if (firstIFD != null) {
        TiffIFDEntry timestamp = (TiffIFDEntry) firstIFD.get(IFD.DATE_TIME);
        if (timestamp != null) {
            String stamp = parser.getIFDValue(timestamp).toString();
            s.close();
            stamp = DateTools.formatDate(stamp, BaseTiffReader.DATE_FORMATS, ".");
            Timestamp t = Timestamp.valueOf(stamp);
            // NPE if invalid input.
            return t.asInstant().getMillis();
        }
    }
    s.close();
    return new Location(file).lastModified();
}
Also used : TiffIFDEntry(loci.formats.tiff.TiffIFDEntry) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location)

Example 45 with TiffParser

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

the class TiffPixelsTest method readSavedPlane.

// -- Helper method --
private byte[] readSavedPlane() throws FormatException, IOException {
    ByteArrayHandle savedData = new ByteArrayHandle();
    RandomAccessOutputStream out = new RandomAccessOutputStream(savedData);
    RandomAccessInputStream in = new RandomAccessInputStream(savedData);
    TiffSaver saver = new TiffSaver(out, savedData);
    // saver.setInputStream(in);
    saver.writeImage(data, ifd, 0, FormatTools.UINT16, false);
    out.close();
    TiffParser parser = new TiffParser(in);
    byte[] plane = new byte[data.length];
    parser.getSamples(ifd, plane);
    in.close();
    return plane;
}
Also used : RandomAccessOutputStream(loci.common.RandomAccessOutputStream) TiffSaver(loci.formats.tiff.TiffSaver) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

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