Search in sources :

Example 21 with DependencyException

use of loci.common.services.DependencyException 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 22 with DependencyException

use of loci.common.services.DependencyException 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 23 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class OMETiffReader method setupService.

private void setupService() throws FormatException {
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    }
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) MissingLibraryException(loci.formats.MissingLibraryException) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService)

Example 24 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class OMETiffWriter method close.

// -- IFormatHandler API methods --
/* @see loci.formats.IFormatHandler#close() */
@Override
public void close() throws IOException {
    try {
        if (currentId != null) {
            setupServiceAndMetadata();
            // remove any BinData and old TiffData elements from the OME-XML
            service.removeBinData(omeMeta);
            service.removeTiffData(omeMeta);
            for (int series = 0; series < omeMeta.getImageCount(); series++) {
                setSeries(series);
                populateImage(omeMeta, series);
            }
            String companion = getCompanion();
            String companionUUID = null;
            if (null != companion) {
                String companionXML = getOMEXML(companion);
                PrintWriter out = new PrintWriter(companion, Constants.ENCODING);
                out.println(XMLTools.indentXML(companionXML, true));
                out.close();
                companionUUID = "urn:uuid:" + getUUID(new Location(companion).getName());
            }
            List<String> files = new ArrayList<String>();
            for (String[] s : imageLocations) {
                for (String f : s) {
                    if (!files.contains(f) && f != null) {
                        files.add(f);
                        String xml = null;
                        if (null != companion) {
                            xml = getBinaryOnlyOMEXML(f, companion, companionUUID);
                        } else {
                            xml = getOMEXML(f);
                        }
                        xml = insertWarningComment(xml);
                        if (getMetadataOptions().isValidate()) {
                            service.validateOMEXML(xml);
                        }
                        // write OME-XML to the first IFD's comment
                        saveComment(f, xml);
                    }
                }
            }
        }
    } catch (DependencyException de) {
        throw new RuntimeException(de);
    } catch (ServiceException se) {
        throw new RuntimeException(se);
    } catch (FormatException fe) {
        throw new RuntimeException(fe);
    } catch (IllegalArgumentException iae) {
        throw new RuntimeException(iae);
    } finally {
        super.close();
        boolean canReallyClose = omeMeta == null || ifdCounts.size() == omeMeta.getImageCount();
        if (omeMeta != null && canReallyClose) {
            int omePlaneCount = 0;
            for (int i = 0; i < omeMeta.getImageCount(); i++) {
                int sizeZ = omeMeta.getPixelsSizeZ(i).getValue();
                int sizeC = omeMeta.getPixelsSizeC(i).getValue();
                int sizeT = omeMeta.getPixelsSizeT(i).getValue();
                omePlaneCount += sizeZ * sizeC * sizeT;
            }
            int ifdCount = 0;
            for (String key : ifdCounts.keySet()) {
                ifdCount += ifdCounts.get(key);
            }
            canReallyClose = omePlaneCount == ifdCount;
        }
        if (canReallyClose) {
            imageLocations = null;
            omeMeta = null;
            service = null;
            ifdCounts.clear();
        } else {
            for (String k : ifdCounts.keySet()) ifdCounts.put(k, 0);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) ServiceException(loci.common.services.ServiceException) PrintWriter(java.io.PrintWriter) Location(loci.common.Location)

Example 25 with DependencyException

use of loci.common.services.DependencyException in project bioformats by openmicroscopy.

the class ConversionTest method createMetadata.

private IMetadata createMetadata(String pixelType, int rgbChannels, int seriesCount, boolean littleEndian) throws Exception {
    IMetadata metadata;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        metadata = service.createOMEXMLMetadata();
    } catch (DependencyException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    } catch (ServiceException exc) {
        throw new FormatException("Could not create OME-XML store.", exc);
    }
    for (int i = 0; i < seriesCount; i++) {
        MetadataTools.populateMetadata(metadata, i, "image #" + i, littleEndian, "XYCZT", pixelType, WIDTH, HEIGHT, 1, rgbChannels, 1, rgbChannels);
    }
    return metadata;
}
Also used : IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Aggregations

DependencyException (loci.common.services.DependencyException)51 ServiceFactory (loci.common.services.ServiceFactory)49 FormatException (loci.formats.FormatException)39 ServiceException (loci.common.services.ServiceException)32 OMEXMLService (loci.formats.services.OMEXMLService)32 MissingLibraryException (loci.formats.MissingLibraryException)15 MetadataStore (loci.formats.meta.MetadataStore)14 CoreMetadata (loci.formats.CoreMetadata)13 IMetadata (loci.formats.meta.IMetadata)13 IOException (java.io.IOException)12 Location (loci.common.Location)10 ImageReader (loci.formats.ImageReader)10 Length (ome.units.quantity.Length)10 PositiveInteger (ome.xml.model.primitives.PositiveInteger)9 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)8 ArrayList (java.util.ArrayList)7 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)6 EnumerationException (ome.xml.model.enums.EnumerationException)6 File (java.io.File)5 IFormatWriter (loci.formats.IFormatWriter)5