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);
}
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);
}
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();
}
}
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);
}
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);
}
Aggregations