use of loci.formats.ome.OMEXMLMetadata 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);
}
}
}
}
}
}
use of loci.formats.ome.OMEXMLMetadata 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;
}
use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.
the class ROIHandler method openROIs.
/**
* Opens the rois and converts them into ImageJ Rois.
*
* @param retrieve The OMEXML store.
* @param images The imageJ object.
* @param isOMERO <code>true</code> if data stored in OMERO,
* <code>false</code> otherwise.
* @param roisMode Determines whether to import Rois to overlay or RoiManager
*/
public static void openROIs(IMetadata retrieve, ImagePlus[] images, boolean isOMERO, String roisMode) {
if (!(retrieve instanceof OMEXMLMetadata))
return;
int nextRoi = 0;
RoiManager manager = RoiManager.getInstance();
OME root = (OME) retrieve.getRoot();
Roi roi;
Float sw;
Color sc;
Color fc;
int imageCount = images.length;
for (int imageNum = 0; imageNum < imageCount; imageNum++) {
int roiCount = root.sizeOfROIList();
if (roiCount > 0 && manager == null && roisMode.equals(ImporterOptions.ROIS_MODE_MANAGER)) {
manager = new RoiManager();
}
for (int roiNum = 0; roiNum < roiCount; roiNum++) {
Union shapeSet = root.getROI(roiNum).getUnion();
int shapeCount = shapeSet.sizeOfShapeList();
for (int shape = 0; shape < shapeCount; shape++) {
Shape shapeObject = shapeSet.getShape(shape);
roi = null;
sw = null;
sc = null;
fc = null;
int c = 0;
int z = 0;
int t = 0;
if (shapeObject instanceof Ellipse) {
Ellipse ellipse = (Ellipse) shapeObject;
int cx = ellipse.getX().intValue();
int cy = ellipse.getY().intValue();
int rx = ellipse.getRadiusX().intValue();
int ry = ellipse.getRadiusY().intValue();
roi = new OvalRoi(cx - rx, cy - ry, rx * 2, ry * 2);
if (ellipse.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = ellipse.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (ellipse.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = ellipse.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (ellipse.getStrokeWidth() != null) {
sw = ellipse.getStrokeWidth().value().floatValue();
}
} else if (shapeObject instanceof ome.xml.model.Line) {
ome.xml.model.Line line = (ome.xml.model.Line) shapeObject;
int x1 = line.getX1().intValue();
int x2 = line.getX2().intValue();
int y1 = line.getY1().intValue();
int y2 = line.getY2().intValue();
roi = new Line(x1, y1, x2, y2);
if (line.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = line.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (line.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = line.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (line.getStrokeWidth() != null) {
sw = line.getStrokeWidth().value().floatValue();
}
} else if (shapeObject instanceof Point) {
Point point = (Point) shapeObject;
int x = point.getX().intValue();
int y = point.getY().intValue();
roi = new PointRoi(x, y);
if (point.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = point.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (point.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = point.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (point.getStrokeWidth() != null) {
sw = point.getStrokeWidth().value().floatValue();
}
} else if (shapeObject instanceof Polyline) {
Polyline polyline = (Polyline) shapeObject;
String points = polyline.getPoints();
int[][] coordinates = parsePoints(points);
roi = new PolygonRoi(coordinates[0], coordinates[1], coordinates[0].length, Roi.POLYLINE);
if (polyline.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = polyline.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (polyline.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = polyline.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (polyline.getStrokeWidth() != null) {
sw = polyline.getStrokeWidth().value().floatValue();
}
} else if (shapeObject instanceof Polygon) {
Polygon polygon = (Polygon) shapeObject;
String points = polygon.getPoints();
int[][] coordinates = parsePoints(points);
roi = new PolygonRoi(coordinates[0], coordinates[1], coordinates[0].length, Roi.POLYGON);
if (polygon.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = polygon.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (polygon.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = polygon.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (polygon.getStrokeWidth() != null) {
sw = polygon.getStrokeWidth().value().floatValue();
}
} else if (shapeObject instanceof ome.xml.model.Label) {
// add support for TextROI's
ome.xml.model.Label label = (ome.xml.model.Label) shapeObject;
double x = label.getX().doubleValue();
double y = label.getY().doubleValue();
String labelText = label.getText();
int size = label.getFontSize().value().intValue();
Font font = new Font(labelText, Font.PLAIN, size);
roi = new TextRoi((int) x, (int) y, labelText, font);
if (label.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = label.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (label.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = label.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (label.getStrokeWidth() != null) {
sw = label.getStrokeWidth().value().floatValue();
}
} else if (shapeObject instanceof ome.xml.model.Rectangle) {
ome.xml.model.Rectangle rectangle = (ome.xml.model.Rectangle) shapeObject;
int x = rectangle.getX().intValue();
int y = rectangle.getY().intValue();
int w = rectangle.getWidth().intValue();
int h = rectangle.getHeight().intValue();
roi = new Roi(x, y, w, h);
if (rectangle.getStrokeColor() != null) {
ome.xml.model.primitives.Color StrokeColor = rectangle.getStrokeColor();
sc = new Color(StrokeColor.getRed(), StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha());
if (isOMERO) {
sc = new Color(StrokeColor.getGreen(), StrokeColor.getBlue(), StrokeColor.getAlpha(), StrokeColor.getRed());
}
}
if (rectangle.getFillColor() != null) {
ome.xml.model.primitives.Color FillColor = rectangle.getFillColor();
fc = new Color(FillColor.getRed(), FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha());
if (isOMERO) {
fc = new Color(FillColor.getGreen(), FillColor.getBlue(), FillColor.getAlpha(), FillColor.getRed());
}
}
if (rectangle.getStrokeWidth() != null) {
sw = rectangle.getStrokeWidth().value().floatValue();
}
}
if (roi != null) {
String roiLabel = shapeObject.getText();
if (roiLabel == null) {
roiLabel = shapeObject.getID();
}
roi.setName(roiLabel);
if (Prefs.showAllSliceOnly) {
if (shapeObject.getTheC() != null) {
c = shapeObject.getTheC().getValue();
}
if (shapeObject.getTheZ() != null) {
z = shapeObject.getTheZ().getValue();
}
if (shapeObject.getTheT() != null) {
t = shapeObject.getTheT().getValue();
}
// ImageJ expects 1-based indexing, opposed to
// 0-based indexing in OME
// Roi positions differ between hyperstacks and normal stacks
ImagePlus imp = images[imageNum];
if (imp.getNChannels() > 1) {
c++;
}
if (imp.getNSlices() > 1) {
z++;
}
if (imp.getNFrames() > 1) {
t++;
}
if (c == 0)
c = 1;
if (t == 0)
t = 1;
if (z == 0)
z = 1;
if (imp.getNChannels() == 1 && imp.getNSlices() == 1) {
roi.setPosition(t);
} else if (imp.getNChannels() == 1 && imp.getNFrames() == 1) {
roi.setPosition(z);
} else if (imp.getNSlices() == 1 && imp.getNFrames() == 1) {
roi.setPosition(c);
} else if (imp.isHyperStack()) {
roi.setPosition(c, z, t);
}
}
if (sw == null) {
roi.setStrokeWidth((float) 1);
}
if (sw != null) {
if (sw == 0) {
sw = (float) 1;
}
roi.setStrokeWidth(sw);
}
if (sc != null) {
roi.setStrokeColor(sc);
}
if (roisMode.equals(ImporterOptions.ROIS_MODE_MANAGER)) {
manager.add(images[imageNum], roi, nextRoi++);
} else if (roisMode.equals(ImporterOptions.ROIS_MODE_OVERLAY)) {
Overlay overlay = images[imageNum].getOverlay();
if (overlay == null) {
overlay = new Overlay(roi);
images[imageNum].setOverlay(overlay);
} else {
overlay.add(roi);
}
}
}
}
}
if (roiCount > 0 && manager != null) {
manager.setAlwaysOnTop(true);
manager.runCommand("show all with labels");
}
}
}
use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.
the class FormatReader method setId.
/**
* Initializes a reader from the input file name.
*
* Calls {@link #initFile(String id)} to initializes the input file, reads
* all of the metadata and sets the reader up for reading planes.
* The performance of this method depends on the format and can be up to
* several minutes for large file sets.
*
* @param id a {@link String} specifying the path to the file
*/
@Override
public void setId(String id) throws FormatException, IOException {
LOGGER.debug("{} initializing {}", this.getClass().getSimpleName(), id);
if (currentId == null || !new Location(id).getAbsolutePath().equals(new Location(currentId).getAbsolutePath())) {
initFile(id);
MetadataStore store = getMetadataStore();
if (saveOriginalMetadata) {
if (store instanceof OMEXMLMetadata) {
setupService();
Hashtable<String, Object> allMetadata = new Hashtable<String, Object>();
allMetadata.putAll(metadata);
for (int series = 0; series < getSeriesCount(); series++) {
String name = "Series " + series;
try {
String realName = ((IMetadata) store).getImageName(series);
if (realName != null && realName.trim().length() != 0) {
name = realName;
}
} catch (Exception e) {
}
setSeries(series);
MetadataTools.merge(getSeriesMetadata(), allMetadata, name + " ");
}
setSeries(0);
service.populateOriginalMetadata((OMEXMLMetadata) store, allMetadata);
}
}
if (store instanceof OMEXMLMetadata) {
((OMEXMLMetadata) store).resolveReferences();
setupService();
if (getMetadataOptions().isValidate()) {
try {
String omexml = service.getOMEXML((MetadataRetrieve) store);
service.validateOMEXML(omexml);
} catch (ServiceException | NullPointerException e) {
LOGGER.warn("OMEXMLService unable to create OME-XML metadata object.", e);
}
}
for (int series = 0; series < getSeriesCount(); series++) {
setSeries(series);
if (getModuloZ().length() > 1 || getModuloC().length() > 1 || getModuloT().length() > 1) {
service.addModuloAlong((OMEXMLMetadata) store, core.get(series), series);
}
}
setSeries(0);
}
}
}
use of loci.formats.ome.OMEXMLMetadata in project bioformats by openmicroscopy.
the class MetadataTools method populatePixels.
/**
* Populates the Pixels element of the given metadata store, using core
* metadata from the given reader. If the {@code doPlane} flag is set,
* then the Plane elements will be populated as well. If the
* {@code doImageName} flag is set, then the image name will be populated as
* well.
*
* @param store The metadata store whose Pixels should be populated
* @param r The format reader whose core metadata should be used
* @param doPlane Specifies whether Plane elements should be populated
* @param doImageName Specifies whether the Image name should be populated
*/
public static void populatePixels(MetadataStore store, IFormatReader r, boolean doPlane, boolean doImageName) {
if (store == null || r == null)
return;
int oldSeries = r.getSeries();
for (int i = 0; i < r.getSeriesCount(); i++) {
r.setSeries(i);
String imageName = null;
if (doImageName) {
Location f = new Location(r.getCurrentFile());
imageName = f.getName();
if (r.getSeriesCount() > 1) {
imageName += " #" + (i + 1);
}
}
String pixelType = FormatTools.getPixelTypeString(r.getPixelType());
populateMetadata(store, r.getCurrentFile(), i, imageName, r.isLittleEndian(), r.getDimensionOrder(), pixelType, r.getSizeX(), r.getSizeY(), r.getSizeZ(), r.getSizeC(), r.getSizeT(), r.getRGBChannelCount());
store.setPixelsInterleaved(r.isInterleaved(), i);
store.setPixelsSignificantBits(new PositiveInteger(r.getBitsPerPixel()), i);
try {
OMEXMLService service = new ServiceFactory().getInstance(OMEXMLService.class);
if (service.isOMEXMLRoot(store.getRoot())) {
MetadataStore baseStore = r.getMetadataStore();
if (service.isOMEXMLMetadata(baseStore)) {
OMEXMLMetadata omeMeta;
try {
omeMeta = service.getOMEMetadata(service.asRetrieve(baseStore));
if (omeMeta.getTiffDataCount(i) == 0 && omeMeta.getPixelsBinDataCount(i) == 0) {
service.addMetadataOnly(omeMeta, i, i == 0);
}
} catch (ServiceException e) {
LOGGER.warn("Failed to add MetadataOnly", e);
}
}
}
} catch (DependencyException exc) {
LOGGER.warn("Failed to add MetadataOnly", exc);
}
if (doPlane) {
for (int q = 0; q < r.getImageCount(); q++) {
int[] coords = r.getZCTCoords(q);
store.setPlaneTheZ(new NonNegativeInteger(coords[0]), i, q);
store.setPlaneTheC(new NonNegativeInteger(coords[1]), i, q);
store.setPlaneTheT(new NonNegativeInteger(coords[2]), i, q);
}
}
}
r.setSeries(oldSeries);
}
Aggregations