Search in sources :

Example 71 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class ImageInfo method printOMEXML.

public void printOMEXML() throws MissingLibraryException, ServiceException {
    LOGGER.info("");
    MetadataStore ms = reader.getMetadataStore();
    if (baseReader instanceof ImageReader) {
        baseReader = ((ImageReader) baseReader).getReader();
    }
    OMEXMLService service;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    }
    String version = service.getOMEXMLVersion(ms);
    if (version == null)
        LOGGER.info("Generating OME-XML");
    else {
        LOGGER.info("Generating OME-XML (schema version {})", version);
    }
    if (ms instanceof MetadataRetrieve) {
        if (omexmlOnly) {
            DebugTools.setRootLevel("INFO");
        }
        String xml = service.getOMEXML((MetadataRetrieve) ms);
        LOGGER.info("{}", XMLTools.indentXML(xml, xmlSpaces, true));
        if (omexmlOnly) {
            DebugTools.setRootLevel("OFF");
        }
    } else {
        LOGGER.info("The metadata could not be converted to OME-XML.");
        if (omexmlVersion == null) {
            LOGGER.info("The OME-XML Java library is probably not available.");
        } else {
            LOGGER.info("{} is probably not a legal schema version.", omexmlVersion);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) ServiceFactory(loci.common.services.ServiceFactory) MissingLibraryException(loci.formats.MissingLibraryException) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 72 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class FormatTools method convert.

/**
 * Convenience method for writing all of the images and metadata obtained
 * from the specified IFormatReader into the specified IFormatWriter.
 *
 * It is required that setId(String) be called on the IFormatReader
 * object before it is passed to convert(...).  setMetadataStore(...)
 * should also have been called with an appropriate instance of IMetadata.
 *
 * The setId(String) method must not be called on the IFormatWriter
 * object; this is taken care of internally.  Additionally, the
 * setMetadataRetrieve(...) method in IFormatWriter should not be called.
 *
 * @param input the pre-initialized IFormatReader used for reading data.
 * @param output the uninitialized IFormatWriter used for writing data.
 * @param outputFile the full path name of the output file to be created.
 * @throws FormatException if there is a general problem reading from or
 * writing to one of the files.
 * @throws IOException if there is an I/O-related error.
 */
public static void convert(IFormatReader input, IFormatWriter output, String outputFile) throws FormatException, IOException {
    MetadataStore store = input.getMetadataStore();
    MetadataRetrieve meta = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.asRetrieve(store);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    }
    output.setMetadataRetrieve(meta);
    output.setId(outputFile);
    for (int series = 0; series < input.getSeriesCount(); series++) {
        input.setSeries(series);
        output.setSeries(series);
        byte[] buf = new byte[getPlaneSize(input)];
        for (int image = 0; image < input.getImageCount(); image++) {
            input.openBytes(image, buf);
            output.saveBytes(image, buf);
        }
    }
    input.close();
    output.close();
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 73 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class FormatReaderTest method setupReader.

// -- Helper methods --
/**
 * Sets up the current IFormatReader.
 */
private void setupReader() {
    // Remove external SlideBook6Reader class for testing purposes
    ImageReader ir = new ImageReader();
    reader = new BufferedImageReader(new FileStitcher(new Memoizer(ir, Memoizer.DEFAULT_MINIMUM_ELAPSED, new File(""))));
    reader.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.NO_OVERLAYS));
    reader.setNormalized(true);
    reader.setOriginalMetadataPopulated(false);
    reader.setMetadataFiltered(true);
    MetadataStore store = null;
    try {
        store = omexmlService.createOMEXMLMetadata();
    } catch (ServiceException e) {
        LOGGER.warn("Could not parse OME-XML", e);
    }
    reader.setMetadataStore(store);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) FileStitcher(loci.formats.FileStitcher) ServiceException(loci.common.services.ServiceException) Memoizer(loci.formats.Memoizer) BufferedImageReader(loci.formats.gui.BufferedImageReader) ImageReader(loci.formats.ImageReader) BufferedImageReader(loci.formats.gui.BufferedImageReader) File(java.io.File)

Example 74 with MetadataStore

use of loci.formats.meta.MetadataStore 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 75 with MetadataStore

use of loci.formats.meta.MetadataStore 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)

Aggregations

MetadataStore (loci.formats.meta.MetadataStore)180 CoreMetadata (loci.formats.CoreMetadata)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)97 Length (ome.units.quantity.Length)82 FormatException (loci.formats.FormatException)66 Timestamp (ome.xml.model.primitives.Timestamp)54 Location (loci.common.Location)51 Time (ome.units.quantity.Time)41 ArrayList (java.util.ArrayList)34 IFD (loci.formats.tiff.IFD)21 DependencyException (loci.common.services.DependencyException)16 IOException (java.io.IOException)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 ServiceFactory (loci.common.services.ServiceFactory)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IFDList (loci.formats.tiff.IFDList)12 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)11 ServiceException (loci.common.services.ServiceException)10 Map (java.util.Map)9