Search in sources :

Example 26 with OMEXMLService

use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.

the class FileExport method initializeMetadata.

/**
 * Populate the minimum amount of metadata required to export an image.
 *
 * @param width the width (in pixels) of the image
 * @param height the height (in pixels) of the image
 * @param pixelType the pixel type of the image; @see loci.formats.FormatTools
 */
private IMetadata initializeMetadata(int width, int height, int pixelType) {
    Exception exception = null;
    try {
        // create the OME-XML metadata storage object
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        IMetadata meta = service.createOMEXMLMetadata();
        meta.createRoot();
        // define each stack of images - this defines a single stack of images
        meta.setImageID("Image:0", 0);
        meta.setPixelsID("Pixels:0", 0);
        // specify that the pixel data is stored in big-endian format
        // change 'TRUE' to 'FALSE' to specify little-endian format
        meta.setPixelsBinDataBigEndian(Boolean.TRUE, 0, 0);
        // specify that the images are stored in ZCT order
        meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, 0);
        // specify that the pixel type of the images
        meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), 0);
        // specify the dimensions of the images
        meta.setPixelsSizeX(new PositiveInteger(width), 0);
        meta.setPixelsSizeY(new PositiveInteger(height), 0);
        meta.setPixelsSizeZ(new PositiveInteger(1), 0);
        meta.setPixelsSizeC(new PositiveInteger(1), 0);
        meta.setPixelsSizeT(new PositiveInteger(1), 0);
        // define each channel and specify the number of samples in the channel
        // the number of samples is 3 for RGB images and 1 otherwise
        meta.setChannelID("Channel:0:0", 0, 0);
        meta.setChannelSamplesPerPixel(new PositiveInteger(1), 0, 0);
        return meta;
    } catch (DependencyException e) {
        exception = e;
    } catch (ServiceException e) {
        exception = e;
    } catch (EnumerationException e) {
        exception = e;
    }
    System.err.println("Failed to populate OME-XML metadata object.");
    exception.printStackTrace();
    return null;
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) FormatException(loci.formats.FormatException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) IOException(java.io.IOException) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService)

Example 27 with OMEXMLService

use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.

the class OrthogonalReader method initialiseWriter.

private OMETiffWriter initialiseWriter(String fileName, ImageReader reader) throws Exception {
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata metadata = service.createOMEXMLMetadata();
    MetadataRetrieve mr = (MetadataRetrieve) reader.getMetadataStore();
    Length originalSizeX = mr.getPixelsPhysicalSizeX(0);
    Length originalSizeY = mr.getPixelsPhysicalSizeY(0);
    Length originalSizeZ = mr.getPixelsPhysicalSizeZ(0);
    // Original XY planes
    // XZ planes
    MetadataTools.populateMetadata(metadata, 0, "XZ", reader.isLittleEndian(), reader.getDimensionOrder(), FormatTools.getPixelTypeString(reader.getPixelType()), reader.getSizeX(), reader.getSizeZ(), reader.getSizeY(), 1, 1, 1);
    metadata.setPixelsPhysicalSizeX(originalSizeX, 0);
    metadata.setPixelsPhysicalSizeY(originalSizeZ, 0);
    metadata.setPixelsPhysicalSizeZ(originalSizeY, 0);
    // YZ planes
    MetadataTools.populateMetadata(metadata, 1, "YZ", reader.isLittleEndian(), reader.getDimensionOrder(), FormatTools.getPixelTypeString(reader.getPixelType()), reader.getSizeY(), reader.getSizeZ(), reader.getSizeX(), 1, 1, 1);
    metadata.setPixelsPhysicalSizeX(originalSizeY, 1);
    metadata.setPixelsPhysicalSizeY(originalSizeZ, 1);
    metadata.setPixelsPhysicalSizeZ(originalSizeX, 1);
    OMETiffWriter writer = new OMETiffWriter();
    writer.setMetadataRetrieve(metadata);
    writer.setId(fileName);
    return writer;
}
Also used : OMETiffWriter(loci.formats.out.OMETiffWriter) IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) Length(ome.units.quantity.Length) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Example 28 with OMEXMLService

use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.

the class SimpleTiledWriter method initialize.

/**
 * Set up the file reader and writer, ensuring that the input file is
 * associated with the reader and the output file is associated with the
 * writer.
 *
 * @return true if the reader and writer were successfully set up, or false
 *   if an error occurred
 * @throws DependencyException thrown if failed to create an OMEXMLService
 * @throws IOException thrown if unable to setup input or output stream for reader or writer
 * @throws FormatException thrown if invalid ID set for reader or writer or invalid tile size set
 * @throws ServiceException thrown if unable to create OME-XML meta data
 */
private void initialize() throws DependencyException, FormatException, IOException, ServiceException {
    // construct the object that stores OME-XML metadata
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    IMetadata omexml = service.createOMEXMLMetadata();
    // set up the reader and associate it with the input file
    reader = new ImageReader();
    reader.setMetadataStore(omexml);
    reader.setId(inputFile);
    /* initialize-tiling-writer-example-start */
    // set up the writer and associate it with the output file
    writer = new OMETiffWriter();
    writer.setMetadataRetrieve(omexml);
    writer.setInterleaved(reader.isInterleaved());
    // set the tile size height and width for writing
    this.tileSizeX = writer.setTileSizeX(tileSizeX);
    this.tileSizeY = writer.setTileSizeY(tileSizeY);
    writer.setId(outputFile);
/* initialize-tiling-writer-example-end */
}
Also used : OMETiffWriter(loci.formats.out.OMETiffWriter) IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) ImageReader(loci.formats.ImageReader) OMEXMLService(loci.formats.services.OMEXMLService)

Example 29 with OMEXMLService

use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.

the class OBFReader method initFile.

@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    currentInflatedFrame.series = -1;
    currentInflatedFrame.number = -1;
    in = new RandomAccessInputStream(id);
    file_version = getFileVersion(in);
    long stackPosition = in.readLong();
    final int lengthOfDescription = in.readInt();
    final String description = in.readString(lengthOfDescription);
    metadata.put("Description", description);
    if (file_version >= 2) {
        final long meta_data_position = in.readLong();
        final long current_position = in.getFilePointer();
        in.seek(meta_data_position);
        for (String key = readString(); key.length() > 0; key = readString()) {
            if (key.equals("ome_xml")) {
                final String ome_xml = readString();
                try {
                    ServiceFactory factory = new ServiceFactory();
                    OMEXMLService service = factory.getInstance(OMEXMLService.class);
                    if (service.validateOMEXML(ome_xml)) {
                        ome_meta_data = service.createOMEXMLMetadata(ome_xml);
                        for (int image = 0; image != ome_meta_data.getImageCount(); ++image) {
                            if (ome_meta_data.getPixelsBigEndian(image) == null) {
                                ome_meta_data.setPixelsBigEndian(Boolean.FALSE, image);
                            }
                            int channels = ome_meta_data.getChannelCount(image);
                            for (int channel = 0; channel != channels; ++channel) {
                                if (ome_meta_data.getChannelSamplesPerPixel(image, channel) == null) {
                                    ome_meta_data.setChannelSamplesPerPixel(new PositiveInteger(1), image, channel);
                                }
                            }
                        }
                        service.convertMetadata(ome_meta_data, metadataStore);
                        OMEXMLMetadata reference = service.getOMEMetadata(service.asRetrieve(metadataStore));
                        for (int image = 0; image != ome_meta_data.getImageCount(); ++image) {
                            service.addMetadataOnly(reference, image);
                        }
                    }
                } catch (DependencyException exception) {
                    throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, exception);
                } catch (ServiceException exception) {
                    throw new FormatException(exception);
                } catch (Exception e) {
                    LOGGER.warn("Could not parse OME-XML metadata", e);
                }
                break;
            } else {
                addGlobalMeta(key, readString());
            }
        }
        in.seek(current_position);
    }
    if (stackPosition != 0) {
        core.clear();
        do {
            stackPosition = initStack(stackPosition);
        } while (stackPosition != 0);
    }
    if (ome_meta_data == null) {
        MetadataTools.populatePixels(metadataStore, this);
        for (int image = 0; image != core.size(); ++image) {
            CoreMetadata meta_data = core.get(image);
            final String name = meta_data.seriesMetadata.get("Name").toString();
            metadataStore.setImageName(name, image);
            @SuppressWarnings("unchecked") final List<Double> lengths = (List<Double>) meta_data.seriesMetadata.get("Lengths");
            if (lengths.size() > 0) {
                double lengthX = Math.abs(lengths.get(0));
                if (lengthX < 0.01) {
                    lengthX *= 1000000;
                }
                if (lengthX > 0) {
                    Length physicalSizeX = FormatTools.getPhysicalSizeX(lengthX / meta_data.sizeX, UNITS.MICROMETER);
                    if (physicalSizeX != null) {
                        metadataStore.setPixelsPhysicalSizeX(physicalSizeX, image);
                    }
                }
            }
            if (lengths.size() > 1) {
                double lengthY = Math.abs(lengths.get(1));
                if (lengthY < 0.01) {
                    lengthY *= 1000000;
                }
                if (lengthY > 0) {
                    Length physicalSizeY = FormatTools.getPhysicalSizeY(lengthY / meta_data.sizeY, UNITS.MICROMETER);
                    if (physicalSizeY != null) {
                        metadataStore.setPixelsPhysicalSizeY(physicalSizeY, image);
                    }
                }
            }
            if (lengths.size() > 2) {
                double lengthZ = Math.abs(lengths.get(2));
                if (lengthZ < 0.01) {
                    lengthZ *= 1000000;
                }
                if (lengthZ > 0) {
                    Length physicalSizeZ = FormatTools.getPhysicalSizeZ(lengthZ / meta_data.sizeZ, UNITS.MICROMETER);
                    if (physicalSizeZ != null) {
                        metadataStore.setPixelsPhysicalSizeZ(physicalSizeZ, image);
                    }
                }
            }
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) OMEXMLService(loci.formats.services.OMEXMLService) DataFormatException(java.util.zip.DataFormatException) FormatException(loci.formats.FormatException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) DataFormatException(java.util.zip.DataFormatException) MissingLibraryException(loci.formats.MissingLibraryException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ServiceException(loci.common.services.ServiceException) Length(ome.units.quantity.Length) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) MissingLibraryException(loci.formats.MissingLibraryException) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 30 with OMEXMLService

use of loci.formats.services.OMEXMLService in project bioformats by openmicroscopy.

the class MakeTestOmeTiff method createMetadata.

private IMetadata createMetadata(final String name, final CoreMetadata info) throws DependencyException, ServiceException, EnumerationException {
    final ServiceFactory serviceFactory = new ServiceFactory();
    final OMEXMLService omexmlService = serviceFactory.getInstance(OMEXMLService.class);
    final IMetadata meta = omexmlService.createOMEXMLMetadata();
    MetadataTools.populateMetadata(meta, 0, name, info);
    if (isModulo) {
        meta.setXMLAnnotationID("Annotation:Modulo:0", 0);
        meta.setXMLAnnotationNamespace("openmicroscopy.org/omero/dimension/modulo", 0);
        meta.setXMLAnnotationDescription("For a description of how 6D, 7D, and 8D data is stored using the Modulo extension see https://docs.openmicroscopy.org/latest/ome-model/developers/6d-7d-and-8d-storage.html", 0);
        StringBuilder moduloBlock = new StringBuilder();
        moduloBlock.append("<Modulo namespace=\"http://www.openmicroscopy.org/Schemas/Additions/2011-09\">");
        if (sizeZsub != 1) {
            moduloBlock.append("<ModuloAlongZ Type=\"other\" TypeDescription=\"Example Data Over Z-Plane\" Start=\"0\" Step=\"1\" End=\"");
            moduloBlock.append(sizeZsub);
            moduloBlock.append("\"/>");
        }
        if (sizeTsub != 1) {
            moduloBlock.append("<ModuloAlongT Type=\"other\" TypeDescription=\"Example Data Over Time \" Start=\"0\" Step=\"1\" End=\"");
            moduloBlock.append(sizeTsub);
            moduloBlock.append("\"/>");
        }
        if (sizeCsub != 1) {
            moduloBlock.append("<ModuloAlongC Type=\"other\" TypeDescription=\"Example Data Over Channel\" Start=\"0\" Step=\"1\" End=\"");
            moduloBlock.append(sizeCsub);
            moduloBlock.append("\"/>");
        }
        moduloBlock.append("</Modulo>");
        meta.setXMLAnnotationValue(moduloBlock.toString(), 0);
        meta.setImageAnnotationRef("Annotation:Modulo:0", 0, 0);
    }
    return meta;
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLService(loci.formats.services.OMEXMLService)

Aggregations

OMEXMLService (loci.formats.services.OMEXMLService)59 ServiceFactory (loci.common.services.ServiceFactory)57 IMetadata (loci.formats.meta.IMetadata)35 DependencyException (loci.common.services.DependencyException)28 ServiceException (loci.common.services.ServiceException)26 FormatException (loci.formats.FormatException)23 ImageReader (loci.formats.ImageReader)21 PositiveInteger (ome.xml.model.primitives.PositiveInteger)12 IOException (java.io.IOException)11 MetadataStore (loci.formats.meta.MetadataStore)7 File (java.io.File)6 Location (loci.common.Location)6 ImageWriter (loci.formats.ImageWriter)6 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)6 Length (ome.units.quantity.Length)6 BeforeClass (org.testng.annotations.BeforeClass)6 IFormatWriter (loci.formats.IFormatWriter)5 MissingLibraryException (loci.formats.MissingLibraryException)5 OMETiffWriter (loci.formats.out.OMETiffWriter)5 ArrayList (java.util.ArrayList)4