Search in sources :

Example 81 with ServiceFactory

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

the class OMEXMLReader 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.setEncoding("ASCII");
    binData = new ArrayList<BinData>();
    binDataOffsets = new ArrayList<Long>();
    compression = new ArrayList<String>();
    DefaultHandler handler = new OMEXMLHandler();
    try {
        RandomAccessInputStream s = new RandomAccessInputStream(id);
        XMLTools.parseXML(s, handler);
        s.close();
    } catch (IOException e) {
        throw new FormatException("Malformed OME-XML", e);
    }
    int lineNumber = 1;
    for (BinData bin : binData) {
        int line = bin.getRow();
        int col = bin.getColumn();
        while (lineNumber < line) {
            in.readLine();
            lineNumber++;
        }
        binDataOffsets.add(in.getFilePointer() + col - 1);
    }
    LOGGER.info("Populating metadata");
    OMEXMLMetadata omexmlMeta;
    OMEXMLService service;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
        omexmlMeta = service.createOMEXMLMetadata(omexml);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    hasSPW = omexmlMeta.getPlateCount() > 0;
    // TODO
    // Hashtable originalMetadata = omexmlMeta.getOriginalMetadata();
    // if (originalMetadata != null) metadata = originalMetadata;
    int numDatasets = omexmlMeta.getImageCount();
    int oldSeries = getSeries();
    core.clear();
    for (int i = 0; i < numDatasets; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        setSeries(i);
        Integer w = omexmlMeta.getPixelsSizeX(i).getValue();
        Integer h = omexmlMeta.getPixelsSizeY(i).getValue();
        Integer t = omexmlMeta.getPixelsSizeT(i).getValue();
        Integer z = omexmlMeta.getPixelsSizeZ(i).getValue();
        Integer c = omexmlMeta.getPixelsSizeC(i).getValue();
        if (w == null || h == null || t == null || z == null | c == null) {
            throw new FormatException("Image dimensions not found");
        }
        Boolean endian = null;
        if (binData.size() > 0) {
            endian = false;
            if (omexmlMeta.getPixelsBigEndian(i) != null) {
                endian = omexmlMeta.getPixelsBigEndian(i).booleanValue();
            } else if (omexmlMeta.getPixelsBinDataCount(i) != 0) {
                endian = omexmlMeta.getPixelsBinDataBigEndian(i, 0).booleanValue();
            }
        }
        String pixType = omexmlMeta.getPixelsType(i).toString();
        ms.dimensionOrder = omexmlMeta.getPixelsDimensionOrder(i).toString();
        ms.sizeX = w.intValue();
        ms.sizeY = h.intValue();
        ms.sizeT = t.intValue();
        ms.sizeZ = z.intValue();
        ms.sizeC = c.intValue();
        ms.imageCount = getSizeZ() * getSizeC() * getSizeT();
        ms.littleEndian = endian == null ? false : !endian.booleanValue();
        ms.rgb = false;
        ms.interleaved = false;
        ms.indexed = false;
        ms.falseColor = true;
        ms.pixelType = FormatTools.pixelTypeFromString(pixType);
        ms.orderCertain = true;
        if (omexmlMeta.getPixelsSignificantBits(i) != null) {
            ms.bitsPerPixel = omexmlMeta.getPixelsSignificantBits(i).getValue();
        }
    }
    setSeries(oldSeries);
    // populate assigned metadata store with the
    // contents of the internal OME-XML metadata object
    MetadataStore store = getMetadataStore();
    service.convertMetadata(omexmlMeta, store);
    MetadataTools.populatePixels(store, this, false, false);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) IOException(java.io.IOException) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) OMEXMLService(loci.formats.services.OMEXMLService) DefaultHandler(org.xml.sax.helpers.DefaultHandler) MetadataStore(loci.formats.meta.MetadataStore) ServiceException(loci.common.services.ServiceException) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) MissingLibraryException(loci.formats.MissingLibraryException) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 82 with ServiceFactory

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

the class OMEXMLWriter method setId.

// -- FormatWriter API methods --
/* @see loci.formats.FormatWriter#setId(String) */
@Override
public void setId(String id) throws FormatException, IOException {
    if (id.equals(currentId)) {
        return;
    }
    super.setId(id);
    MetadataRetrieve retrieve = getMetadataRetrieve();
    String xml;
    try {
        ServiceFactory factory = new ServiceFactory();
        service = factory.getInstance(OMEXMLService.class);
        xml = service.getOMEXML(retrieve);
        OMEXMLMetadata noBin = service.createOMEXMLMetadata(xml);
        service.removeBinData(noBin);
        OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) noBin.getRoot();
        root.setCreator(FormatTools.CREATOR);
        xml = service.getOMEXML(noBin);
    } catch (DependencyException de) {
        throw new MissingLibraryException(OMEXMLServiceImpl.NO_OME_XML_MSG, de);
    } catch (ServiceException se) {
        throw new FormatException(se);
    }
    xmlFragments = new ArrayList<String>();
    currentFragment = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    XMLTools.parseXML(xml, new OMEHandler());
    xmlFragments.add(currentFragment);
}
Also used : ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) MissingLibraryException(loci.formats.MissingLibraryException) DependencyException(loci.common.services.DependencyException) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 83 with ServiceFactory

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

the class EightBitLosslessJPEG2000Test method setUp.

@BeforeMethod
public void setUp() throws Exception {
    for (byte v = Byte.MIN_VALUE; v < Byte.MAX_VALUE; v++) {
        int index = v + Byte.MAX_VALUE + 1;
        pixels[index][0] = v;
        String file = index + ".jp2";
        File tempFile = File.createTempFile("test", ".jp2");
        tempFile.deleteOnExit();
        Location.mapId(file, tempFile.getAbsolutePath());
        files.add(file);
        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);
        }
        MetadataTools.populateMetadata(metadata, 0, "foo", false, "XYCZT", "uint8", 1, 1, 1, 1, 1, 1);
        IFormatWriter writer = new JPEG2000Writer();
        writer.setMetadataRetrieve(metadata);
        writer.setId(file);
        writer.saveBytes(0, pixels[index]);
        writer.close();
    }
}
Also used : IFormatWriter(loci.formats.IFormatWriter) JPEG2000Writer(loci.formats.out.JPEG2000Writer) IMetadata(loci.formats.meta.IMetadata) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) DependencyException(loci.common.services.DependencyException) File(java.io.File) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 84 with ServiceFactory

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

the class OMETiffWriter method getBinaryOnlyOMEXML.

private String getBinaryOnlyOMEXML(String file, String companion, String companionUUID) throws FormatException, IOException, DependencyException, ServiceException {
    ServiceFactory factory = new ServiceFactory();
    OMEXMLService service = factory.getInstance(OMEXMLService.class);
    OMEXMLMetadata meta = service.createOMEXMLMetadata();
    String uuid = "urn:uuid:" + getUUID(new Location(file).getName());
    meta.setUUID(uuid);
    meta.setBinaryOnlyMetadataFile(new Location(companion).getName());
    meta.setBinaryOnlyUUID(companionUUID);
    OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) meta.getRoot();
    root.setCreator(FormatTools.CREATOR);
    return service.getOMEXML(meta);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) OMEXMLMetadataRoot(ome.xml.meta.OMEXMLMetadataRoot) OMEXMLService(loci.formats.services.OMEXMLService) Location(loci.common.Location)

Example 85 with ServiceFactory

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

the class OMETiffWriter method setupServiceAndMetadata.

private void setupServiceAndMetadata() throws DependencyException, ServiceException {
    // extract OME-XML string from metadata object
    MetadataRetrieve retrieve = getMetadataRetrieve();
    ServiceFactory factory = new ServiceFactory();
    service = factory.getInstance(OMEXMLService.class);
    OMEXMLMetadata originalOMEMeta = service.getOMEMetadata(retrieve);
    originalOMEMeta.resolveReferences();
    String omexml = service.getOMEXML(originalOMEMeta);
    omeMeta = service.createOMEXMLMetadata(omexml);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) OMEXMLMetadata(loci.formats.ome.OMEXMLMetadata) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) OMEXMLService(loci.formats.services.OMEXMLService)

Aggregations

ServiceFactory (loci.common.services.ServiceFactory)94 OMEXMLService (loci.formats.services.OMEXMLService)73 DependencyException (loci.common.services.DependencyException)49 FormatException (loci.formats.FormatException)38 IMetadata (loci.formats.meta.IMetadata)35 ServiceException (loci.common.services.ServiceException)30 ImageReader (loci.formats.ImageReader)23 MissingLibraryException (loci.formats.MissingLibraryException)15 BeforeMethod (org.testng.annotations.BeforeMethod)15 MetadataStore (loci.formats.meta.MetadataStore)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)14 IOException (java.io.IOException)13 CoreMetadata (loci.formats.CoreMetadata)13 Length (ome.units.quantity.Length)12 Location (loci.common.Location)11 OMEXMLMetadata (loci.formats.ome.OMEXMLMetadata)10 InputStream (java.io.InputStream)9 ArrayList (java.util.ArrayList)8 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)8 BeforeClass (org.testng.annotations.BeforeClass)8