Search in sources :

Example 96 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class WrapperTest method testCoreMetadata.

@Test(dataProvider = "wrappers")
public void testCoreMetadata(IFormatReader reader) {
    assertNotNull(reader.getCurrentFile());
    List<CoreMetadata> coreList = reader.getCoreMetadataList();
    assertEquals(coreList.size(), reader.getSeriesCount());
    for (int i = 0; i < reader.getSeriesCount(); i++) {
        CoreMetadata core = coreList.get(i);
        reader.setSeries(i);
        assertEquals(core.sizeX, reader.getSizeX());
        assertEquals(core.sizeY, reader.getSizeY());
        assertEquals(core.sizeZ, reader.getSizeZ());
        assertEquals(core.sizeC, reader.getSizeC());
        assertEquals(core.sizeT, reader.getSizeT());
        assertEquals(core.pixelType, reader.getPixelType());
        assertEquals(core.imageCount, reader.getImageCount());
        assertEquals(core.dimensionOrder, reader.getDimensionOrder());
        assertEquals(core.littleEndian, reader.isLittleEndian());
        assertEquals(core.rgb, reader.isRGB());
        assertEquals(core.interleaved, reader.isInterleaved());
        assertEquals(core.indexed, reader.isIndexed());
    }
}
Also used : CoreMetadata(loci.formats.CoreMetadata) Test(org.testng.annotations.Test)

Example 97 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class FileExportSPW method createModuloAnn.

/**
 * Add ModuloAlong annotation.
 *
 * @param meta
 *          OMEXMLMetadata Object to which Modulo need to be added
 */
private CoreMetadata createModuloAnn() {
    CoreMetadata modlo = new CoreMetadata();
    modlo.moduloT.type = loci.formats.FormatTools.LIFETIME;
    modlo.moduloT.unit = "ps";
    modlo.moduloT.typeDescription = "Gated";
    modlo.moduloT.labels = new String[sizeT];
    for (int i = 0; i < sizeT; i++) {
        modlo.moduloT.labels[i] = Integer.toString(i * 1000);
    }
    return modlo;
}
Also used : CoreMetadata(loci.formats.CoreMetadata)

Example 98 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class FitsReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    CoreMetadata m = core.get(0);
    String line = in.readString(LINE_LENGTH);
    if (!line.startsWith("SIMPLE")) {
        throw new FormatException("Unsupported FITS file.");
    }
    String key = "", value = "";
    while (true) {
        line = in.readString(LINE_LENGTH);
        // parse key/value pair
        int ndx = line.indexOf('=');
        int comment = line.indexOf("/", ndx);
        if (comment < 0)
            comment = line.length();
        if (ndx >= 0) {
            key = line.substring(0, ndx).trim();
            value = line.substring(ndx + 1, comment).trim();
        } else
            key = line.trim();
        // image dimensions are only populated by the standard header
        if (key.equals("END") && getSizeX() > 0)
            break;
        if (key.equals("BITPIX")) {
            int bits = Integer.parseInt(value);
            boolean fp = bits < 0;
            boolean signed = bits != 8;
            bits = Math.abs(bits) / 8;
            m.pixelType = FormatTools.pixelTypeFromBytes(bits, signed, fp);
        } else if (key.equals("NAXIS1"))
            m.sizeX = Integer.parseInt(value);
        else if (key.equals("NAXIS2"))
            m.sizeY = Integer.parseInt(value);
        else if (key.equals("NAXIS3"))
            m.sizeZ = Integer.parseInt(value);
        addGlobalMeta(key, value);
    }
    while (in.read() == 0x20) ;
    pixelOffset = in.getFilePointer() - 1;
    m.sizeC = 1;
    m.sizeT = 1;
    if (getSizeZ() == 0)
        m.sizeZ = 1;
    // correct for truncated files
    int planeSize = getSizeX() * getSizeY() * FormatTools.getBytesPerPixel(getPixelType());
    if (DataTools.safeMultiply64(planeSize, getSizeZ()) > (in.length() - pixelOffset)) {
        m.sizeZ = (int) ((in.length() - pixelOffset) / planeSize);
    }
    m.imageCount = m.sizeZ;
    m.rgb = false;
    m.littleEndian = false;
    m.interleaved = false;
    m.dimensionOrder = "XYZCT";
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 99 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class IM3Reader method initFile.

/* (non-Javadoc)
   * @see loci.formats.FormatReader#initFile(java.lang.String)
   */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    core.clear();
    IRandomAccess is = Location.getHandle(id, false);
    try {
        is.setOrder(ByteOrder.LITTLE_ENDIAN);
        final int cookie = is.readInt();
        if (cookie != COOKIE) {
            throw new FormatException(String.format("Expected file cookie of %d, but got %d.", COOKIE, cookie));
        }
        long fileLength = is.length();
        while (is.getFilePointer() < fileLength) {
            final IM3Record rec = parseRecord(is);
            if (rec == null) {
                if (is.getFilePointer() > fileLength - 16)
                    break;
                /*
           * # of bytes in chunk.
           */
                @SuppressWarnings("unused") final int chunkLength = is.readInt();
                /*
           * Is always zero? Chunk #?
           */
                @SuppressWarnings("unused") final int unknown = is.readInt();
                /*
           * Is always one? Chunk #?
           */
                @SuppressWarnings("unused") final int unknown1 = is.readInt();
                /*
           * # of records to follow
           */
                @SuppressWarnings("unused") final int nRecords = is.readInt();
            } else {
                if (rec instanceof ContainerRecord) {
                    final ContainerRecord bRec = (ContainerRecord) rec;
                    for (IM3Record subDS : bRec.parseChunks(is)) {
                        if ((subDS instanceof ContainerRecord) && (subDS.name.equals(FIELD_DATA_SET))) {
                            final ContainerRecord bSubDS = (ContainerRecord) subDS;
                            for (IM3Record subSubDS : bSubDS.parseChunks(is)) {
                                if (subSubDS instanceof ContainerRecord) {
                                    final ContainerRecord bDataSet = (ContainerRecord) subSubDS;
                                    dataSets.add(bDataSet);
                                    List<IM3Record> subRecs = bDataSet.parseChunks(is);
                                    final CoreMetadata cm = new CoreMetadata();
                                    cm.dimensionOrder = DimensionOrder.XYCZT.getValue();
                                    cm.littleEndian = true;
                                    // TODO: Detect pixel type
                                    cm.pixelType = FormatTools.UINT16;
                                    for (IM3Record subRec : subRecs) {
                                        if (subRec.name.equals(FIELD_SHAPE) && (subRec instanceof IntIM3Record)) {
                                            final IntIM3Record iRec = (IntIM3Record) subRec;
                                            cm.sizeX = iRec.getEntry(is, 0);
                                            cm.sizeY = iRec.getEntry(is, 1);
                                            cm.sizeC = iRec.getEntry(is, 2);
                                            cm.sizeZ = 1;
                                            cm.sizeT = 1;
                                            cm.imageCount = cm.sizeC;
                                            cm.metadataComplete = true;
                                        }
                                    }
                                    core.add(cm);
                                }
                            }
                        } else if ((subDS instanceof ContainerRecord) && subDS.name.equals(FIELD_SPECTRAL_LIBRARY)) {
                            /*
                 * SpectralLibrary
                 *   (unnamed container record)
                 *       Spectra
                 *           Keys (integers)
                 *           Values
                 *               (unnamed container record for spectrum #1)
                 *               (unnamed container record for spectrum #2)...
                 *
                 */
                            for (IM3Record slContainer : ((ContainerRecord) subDS).parseChunks(is)) {
                                /* unnamed container */
                                if (slContainer instanceof ContainerRecord) {
                                    for (IM3Record slSpectra : ((ContainerRecord) slContainer).parseChunks(is)) {
                                        if ((slSpectra instanceof ContainerRecord) && (slSpectra.name.equals(FIELD_SPECTRA))) {
                                            for (IM3Record slRec : ((ContainerRecord) slSpectra).parseChunks(is)) {
                                                if (slRec.name.equals(FIELD_VALUES) && (slRec instanceof ContainerRecord)) {
                                                    for (IM3Record spectrumRec : ((ContainerRecord) slRec).parseChunks(is)) {
                                                        if (spectrumRec instanceof ContainerRecord) {
                                                            spectra.add(new Spectrum(is, (ContainerRecord) spectrumRec));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                records.add(rec);
            }
        }
    } finally {
        is.close();
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IRandomAccess(loci.common.IRandomAccess) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 100 with CoreMetadata

use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.

the class MNGReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    in.order(false);
    LOGGER.info("Verifying MNG format");
    seriesInfo = new ArrayList<SeriesInfo>();
    seriesInfo.add(new SeriesInfo());
    in.skipBytes(12);
    if (!"MHDR".equals(in.readString(4))) {
        throw new FormatException("Invalid MNG file.");
    }
    LOGGER.info("Reading dimensions");
    in.skipBytes(32);
    final List<Long> stack = new ArrayList<Long>();
    int maxIterations = 0;
    int currentIteration = 0;
    LOGGER.info("Finding image offsets");
    while (in.getFilePointer() < in.length()) {
        int len = in.readInt();
        String code = in.readString(4);
        long fp = in.getFilePointer();
        if (code.equals("IHDR")) {
            seriesInfo.get(0).offsets.add(fp - 8);
        } else if (code.equals("JDAT")) {
            isJNG = true;
            seriesInfo.get(0).offsets.add(fp);
        } else if (code.equals("IEND")) {
            seriesInfo.get(0).lengths.add(fp + len + 4);
        } else if (code.equals("LOOP")) {
            stack.add(fp + len + 4);
            in.skipBytes(1);
            maxIterations = in.readInt();
        } else if (code.equals("ENDL")) {
            long seek = stack.get(stack.size() - 1).longValue();
            if (currentIteration < maxIterations) {
                in.seek(seek);
                currentIteration++;
            } else {
                stack.remove(stack.size() - 1);
                maxIterations = 0;
                currentIteration = 0;
            }
        }
        in.seek(fp + len + 4);
    }
    LOGGER.info("Populating metadata");
    // easiest way to get image dimensions is by opening the first plane
    Hashtable<String, List<Long>> seriesOffsets = new Hashtable<String, List<Long>>();
    Hashtable<String, List<Long>> seriesLengths = new Hashtable<String, List<Long>>();
    SeriesInfo info = seriesInfo.get(0);
    addGlobalMeta("Number of frames", info.offsets.size());
    for (int i = 0; i < info.offsets.size(); i++) {
        long offset = info.offsets.get(i);
        in.seek(offset);
        long end = info.lengths.get(i);
        if (end < offset)
            continue;
        BufferedImage img = readImage(end);
        String data = img.getWidth() + "-" + img.getHeight() + "-" + img.getRaster().getNumBands() + "-" + AWTImageTools.getPixelType(img);
        List<Long> v = seriesOffsets.get(data);
        if (v == null) {
            v = new ArrayList<Long>();
            seriesOffsets.put(data, v);
        }
        v.add(offset);
        v = seriesLengths.get(data);
        if (v == null) {
            v = new ArrayList<Long>();
            seriesLengths.put(data, v);
        }
        v.add(end);
    }
    String[] keys = seriesOffsets.keySet().toArray(new String[0]);
    if (keys.length == 0) {
        throw new FormatException("Pixel data not found.");
    }
    int seriesCount = keys.length;
    core.clear();
    seriesInfo.clear();
    for (int i = 0; i < seriesCount; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        String[] tokens = keys[i].split("-");
        ms.sizeX = Integer.parseInt(tokens[0]);
        ms.sizeY = Integer.parseInt(tokens[1]);
        ms.sizeC = Integer.parseInt(tokens[2]);
        ms.pixelType = Integer.parseInt(tokens[3]);
        ms.rgb = ms.sizeC > 1;
        ms.sizeZ = 1;
        ms.dimensionOrder = "XYCZT";
        ms.interleaved = false;
        ms.metadataComplete = true;
        ms.indexed = false;
        ms.littleEndian = false;
        ms.falseColor = false;
        SeriesInfo inf = new SeriesInfo();
        inf.offsets = seriesOffsets.get(keys[i]);
        inf.lengths = seriesLengths.get(keys[i]);
        seriesInfo.add(inf);
        ms.imageCount = inf.offsets.size();
        ms.sizeT = ms.imageCount;
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    for (int i = 0; i < getSeriesCount(); i++) {
        store.setImageName("Series " + (i + 1), i);
    }
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) BufferedImage(java.awt.image.BufferedImage) MetadataStore(loci.formats.meta.MetadataStore) ArrayList(java.util.ArrayList) List(java.util.List) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

CoreMetadata (loci.formats.CoreMetadata)211 MetadataStore (loci.formats.meta.MetadataStore)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)108 FormatException (loci.formats.FormatException)87 Length (ome.units.quantity.Length)74 Location (loci.common.Location)55 ArrayList (java.util.ArrayList)50 Timestamp (ome.xml.model.primitives.Timestamp)44 IFD (loci.formats.tiff.IFD)33 Time (ome.units.quantity.Time)30 IOException (java.io.IOException)21 TiffParser (loci.formats.tiff.TiffParser)19 PhotoInterp (loci.formats.tiff.PhotoInterp)17 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)16 IFDList (loci.formats.tiff.IFDList)15 DependencyException (loci.common.services.DependencyException)14 ServiceFactory (loci.common.services.ServiceFactory)13 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IniList (loci.common.IniList)9 HashMap (java.util.HashMap)8