Search in sources :

Example 16 with ServiceException

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

the class FileExportSPW method initializeMetadata.

/**
 * Populate the minimum amount of metadata required to export a Plate.
 *
 * @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);
        OMEXMLMetadata meta = service.createOMEXMLMetadata();
        meta.createRoot();
        int plateIndex = 0;
        // count of images
        int series = 0;
        int well = 0;
        // Create Minimal 2x2 Plate
        meta.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
        meta.setPlateRowNamingConvention(NamingConvention.LETTER, 0);
        meta.setPlateColumnNamingConvention(NamingConvention.NUMBER, 0);
        meta.setPlateRows(new PositiveInteger(rows), 0);
        meta.setPlateColumns(new PositiveInteger(cols), 0);
        meta.setPlateName("First test Plate", 0);
        PositiveInteger pwidth = new PositiveInteger(width);
        PositiveInteger pheight = new PositiveInteger(height);
        char rowChar = 'A';
        for (int row = 0; row < rows; row++) {
            for (int column = 0; column < cols; column++) {
                // set up well
                String wellID = MetadataTools.createLSID("Well:", well);
                meta.setWellID(wellID, plateIndex, well);
                meta.setWellRow(new NonNegativeInteger(row), plateIndex, well);
                meta.setWellColumn(new NonNegativeInteger(column), plateIndex, well);
                for (int fov = 0; fov < fovPerWell; fov++) {
                    // Create Image
                    String imageName = rowChar + ":" + Integer.toString(column + 1) + ":FOV:" + Integer.toString(fov + 1);
                    String imageID = MetadataTools.createLSID("Image", well, fov);
                    meta.setImageID(imageID, series);
                    meta.setImageName(imageName, series);
                    String pixelsID = MetadataTools.createLSID("Pixels", row, well, fov);
                    meta.setPixelsID(pixelsID, series);
                    // specify that the pixel data is stored in big-endian format
                    // change 'TRUE' to 'FALSE' to specify little-endian format
                    meta.setPixelsBinDataBigEndian(Boolean.TRUE, series, 0);
                    // specify that the image is stored in ZCT order
                    meta.setPixelsDimensionOrder(DimensionOrder.XYZCT, series);
                    // specify the pixel type of the image
                    meta.setPixelsType(PixelType.fromString(FormatTools.getPixelTypeString(pixelType)), series);
                    // specify the dimensions of the image
                    meta.setPixelsSizeX(pwidth, series);
                    meta.setPixelsSizeY(pheight, series);
                    meta.setPixelsSizeZ(new PositiveInteger(1), series);
                    meta.setPixelsSizeC(new PositiveInteger(1), series);
                    meta.setPixelsSizeT(new PositiveInteger(sizeT), series);
                    // define each channel and specify the number of samples in the
                    // channel the number of samples is 3 for RGB images and 1 otherwise
                    String channelID = MetadataTools.createLSID("Channel", well, fov);
                    meta.setChannelID(channelID, series, 0);
                    meta.setChannelSamplesPerPixel(new PositiveInteger(1), series, 0);
                    // set sample
                    String wellSampleID = MetadataTools.createLSID("WellSample", well, fov);
                    meta.setWellSampleID(wellSampleID, 0, well, fov);
                    // NB sampleIndex here == series ie the image No
                    meta.setWellSampleIndex(new NonNegativeInteger(series), 0, well, fov);
                    meta.setWellSampleImageRef(imageID, 0, well, fov);
                    // add FLIM ModuloAlongT annotation if required
                    // CoreMetadata modlo = createModuloAnn();
                    // meta.addModuloAlong(meta, modlo, series);
                    series++;
                }
                // end of samples
                well++;
            }
            rowChar++;
        }
        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) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) 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 17 with ServiceException

use of loci.common.services.ServiceException 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 18 with ServiceException

use of loci.common.services.ServiceException 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 19 with ServiceException

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

the class OMETiffReader method isSingleFile.

// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isSingleFile(String) */
@Override
public boolean isSingleFile(String id) throws FormatException, IOException {
    // companion files in a binary-only dataset should always have additional files
    if (checkSuffix(id, "companion.ome")) {
        return false;
    }
    // parse and populate OME-XML metadata
    String fileName = new Location(id).getAbsoluteFile().getAbsolutePath();
    RandomAccessInputStream ras = new RandomAccessInputStream(fileName, 16);
    TiffParser tp = new TiffParser(ras);
    IFD ifd = tp.getFirstIFD();
    long[] ifdOffsets = tp.getIFDOffsets();
    ras.close();
    String xml = ifd.getComment();
    if (service == null)
        setupService();
    OMEXMLMetadata meta;
    try {
        meta = service.createOMEXMLMetadata(xml);
        metaFile = new Location(id).getAbsolutePath();
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    if (meta.getRoot() == null) {
        throw new FormatException("Could not parse OME-XML from TIFF comment");
    }
    int nImages = 0;
    for (int i = 0; i < meta.getImageCount(); i++) {
        int nChannels = meta.getChannelCount(i);
        if (nChannels == 0)
            nChannels = 1;
        int z = meta.getPixelsSizeZ(i).getValue().intValue();
        int t = meta.getPixelsSizeT(i).getValue().intValue();
        nImages += z * t * nChannels;
    }
    return nImages > 0 && nImages <= ifdOffsets.length;
}
Also used : ServiceException(loci.common.services.ServiceException) IFD(loci.formats.tiff.IFD) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) FormatException(loci.formats.FormatException) Location(loci.common.Location)

Example 20 with ServiceException

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

the class TileJPEGReader method reopenFile.

@Override
public void reopenFile() throws IOException {
    if (in != null) {
        in.close();
    }
    in = new RandomAccessInputStream(currentId);
    in.seek(0);
    service = new JPEGTurboServiceImpl();
    try {
        service.initialize(in, getSizeX(), getSizeY());
    } catch (ServiceException se) {
        service = null;
        throw new IOException("Could not initialize JPEG service", se);
    }
}
Also used : ServiceException(loci.common.services.ServiceException) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) JPEGTurboServiceImpl(loci.formats.services.JPEGTurboServiceImpl)

Aggregations

ServiceException (loci.common.services.ServiceException)54 FormatException (loci.formats.FormatException)40 DependencyException (loci.common.services.DependencyException)34 ServiceFactory (loci.common.services.ServiceFactory)30 OMEXMLService (loci.formats.services.OMEXMLService)29 IOException (java.io.IOException)21 IMetadata (loci.formats.meta.IMetadata)16 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)12 Location (loci.common.Location)11 PositiveInteger (ome.xml.model.primitives.PositiveInteger)11 File (java.io.File)10 ImageReader (loci.formats.ImageReader)10 MetadataStore (loci.formats.meta.MetadataStore)10 CoreMetadata (loci.formats.CoreMetadata)9 OMEXMLMetadataRoot (ome.xml.meta.OMEXMLMetadataRoot)9 EnumerationException (ome.xml.model.enums.EnumerationException)9 RandomAccessInputStream (loci.common.RandomAccessInputStream)7 MissingLibraryException (loci.formats.MissingLibraryException)7 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 SAXException (org.xml.sax.SAXException)6