use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class ZeissLSMReader method parseOverlays.
/**
* Parses overlay-related fields.
*/
protected void parseOverlays(int series, long data, String suffix, MetadataStore store) throws IOException {
if (data == 0)
return;
String prefix = "Series " + series + " ";
in.seek(data);
int numberOfShapes = in.readInt();
int size = in.readInt();
if (size <= 194)
return;
in.skipBytes(20);
boolean valid = in.readInt() == 1;
in.skipBytes(164);
for (int i = totalROIs; i < totalROIs + numberOfShapes; i++) {
long offset = in.getFilePointer();
int type = in.readInt();
int blockLength = in.readInt();
double lineWidth = in.readInt();
int measurements = in.readInt();
double textOffsetX = in.readDouble();
double textOffsetY = in.readDouble();
int color = in.readInt();
boolean validShape = in.readInt() != 0;
int knotWidth = in.readInt();
int catchArea = in.readInt();
int fontHeight = in.readInt();
int fontWidth = in.readInt();
int fontEscapement = in.readInt();
int fontOrientation = in.readInt();
int fontWeight = in.readInt();
boolean fontItalic = in.readInt() != 0;
boolean fontUnderlined = in.readInt() != 0;
boolean fontStrikeout = in.readInt() != 0;
int fontCharSet = in.readInt();
int fontOutputPrecision = in.readInt();
int fontClipPrecision = in.readInt();
int fontQuality = in.readInt();
int fontPitchAndFamily = in.readInt();
String fontName = DataTools.stripString(in.readString(64));
boolean enabled = in.readShort() == 0;
boolean moveable = in.readInt() == 0;
in.skipBytes(34);
String roiID = MetadataTools.createLSID("ROI", i);
String shapeID = MetadataTools.createLSID("Shape", i, 0);
Length fontSize = FormatTools.getFontSize(fontHeight);
Length line = new Length(lineWidth, UNITS.PIXEL);
switch(type) {
case TEXT:
double x = in.readDouble();
double y = in.readDouble();
String text = DataTools.stripString(in.readCString());
store.setROIID(roiID, i);
store.setLabelID(shapeID, i, 0);
store.setLabelX(x, i, 0);
store.setLabelY(y, i, 0);
store.setLabelText(text, i, 0);
if (fontSize != null) {
store.setLabelFontSize(fontSize, i, 0);
}
store.setLabelStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case LINE:
in.skipBytes(4);
double startX = in.readDouble();
double startY = in.readDouble();
double endX = in.readDouble();
double endY = in.readDouble();
store.setROIID(roiID, i);
store.setLineID(shapeID, i, 0);
store.setLineX1(startX, i, 0);
store.setLineY1(startY, i, 0);
store.setLineX2(endX, i, 0);
store.setLineY2(endY, i, 0);
if (fontSize != null) {
store.setLineFontSize(fontSize, i, 0);
}
store.setLineStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case SCALE_BAR:
case OPEN_ARROW:
case CLOSED_ARROW:
case PALETTE:
in.skipBytes(36);
i--;
numberOfShapes--;
break;
case RECTANGLE:
in.skipBytes(4);
double topX = in.readDouble();
double topY = in.readDouble();
double bottomX = in.readDouble();
double bottomY = in.readDouble();
double width = Math.abs(bottomX - topX);
double height = Math.abs(bottomY - topY);
topX = Math.min(topX, bottomX);
topY = Math.min(topY, bottomY);
store.setROIID(roiID, i);
store.setRectangleID(shapeID, i, 0);
store.setRectangleX(topX, i, 0);
store.setRectangleY(topY, i, 0);
store.setRectangleWidth(width, i, 0);
store.setRectangleHeight(height, i, 0);
if (fontSize != null) {
store.setRectangleFontSize(fontSize, i, 0);
}
store.setRectangleStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case ELLIPSE:
int knots = in.readInt();
double[] xs = new double[knots];
double[] ys = new double[knots];
for (int j = 0; j < xs.length; j++) {
xs[j] = in.readDouble();
ys[j] = in.readDouble();
}
double rx = 0, ry = 0, centerX = 0, centerY = 0;
store.setROIID(roiID, i);
store.setEllipseID(shapeID, i, 0);
if (knots == 4) {
double r1x = Math.abs(xs[2] - xs[0]) / 2;
double r1y = Math.abs(ys[2] - ys[0]) / 2;
double r2x = Math.abs(xs[3] - xs[1]) / 2;
double r2y = Math.abs(ys[3] - ys[1]) / 2;
if (r1x > r2x) {
ry = r1y;
rx = r2x;
centerX = Math.min(xs[3], xs[1]) + rx;
centerY = Math.min(ys[2], ys[0]) + ry;
} else {
ry = r2y;
rx = r1x;
centerX = Math.min(xs[2], xs[0]) + rx;
centerY = Math.min(ys[3], ys[1]) + ry;
}
} else if (knots == 3) {
// we are given the center point and one cut point for each axis
centerX = xs[0];
centerY = ys[0];
rx = Math.sqrt(Math.pow(xs[1] - xs[0], 2) + Math.pow(ys[1] - ys[0], 2));
ry = Math.sqrt(Math.pow(xs[2] - xs[0], 2) + Math.pow(ys[2] - ys[0], 2));
// calculate rotation angle
double slope = (ys[2] - centerY) / (xs[2] - centerX);
double theta = Math.toDegrees(Math.atan(slope));
store.setEllipseTransform(getRotationTransform(theta), i, 0);
}
store.setEllipseX(centerX, i, 0);
store.setEllipseY(centerY, i, 0);
store.setEllipseRadiusX(rx, i, 0);
store.setEllipseRadiusY(ry, i, 0);
if (fontSize != null) {
store.setEllipseFontSize(fontSize, i, 0);
}
store.setEllipseStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case CIRCLE:
in.skipBytes(4);
centerX = in.readDouble();
centerY = in.readDouble();
double curveX = in.readDouble();
double curveY = in.readDouble();
double radius = Math.sqrt(Math.pow(curveX - centerX, 2) + Math.pow(curveY - centerY, 2));
store.setROIID(roiID, i);
store.setEllipseID(shapeID, i, 0);
store.setEllipseX(centerX, i, 0);
store.setEllipseY(centerY, i, 0);
store.setEllipseRadiusX(radius, i, 0);
store.setEllipseRadiusY(radius, i, 0);
if (fontSize != null) {
store.setEllipseFontSize(fontSize, i, 0);
}
store.setEllipseStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case CIRCLE_3POINT:
in.skipBytes(4);
// given 3 points on the perimeter of the circle, we need to
// calculate the center and radius
double[][] points = new double[3][2];
for (int j = 0; j < points.length; j++) {
for (int k = 0; k < points[j].length; k++) {
points[j][k] = in.readDouble();
}
}
double s = 0.5 * ((points[1][0] - points[2][0]) * (points[0][0] - points[2][0]) - (points[1][1] - points[2][1]) * (points[2][1] - points[0][1]));
double div = (points[0][0] - points[1][0]) * (points[2][1] - points[0][1]) - (points[1][1] - points[0][1]) * (points[0][0] - points[2][0]);
s /= div;
double cx = 0.5 * (points[0][0] + points[1][0]) + s * (points[1][1] - points[0][1]);
double cy = 0.5 * (points[0][1] + points[1][1]) + s * (points[0][0] - points[1][0]);
double r = Math.sqrt(Math.pow(points[0][0] - cx, 2) + Math.pow(points[0][1] - cy, 2));
store.setROIID(roiID, i);
store.setEllipseID(shapeID, i, 0);
store.setEllipseX(cx, i, 0);
store.setEllipseY(cy, i, 0);
store.setEllipseRadiusX(r, i, 0);
store.setEllipseRadiusY(r, i, 0);
if (fontSize != null) {
store.setEllipseFontSize(fontSize, i, 0);
}
store.setEllipseStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case ANGLE:
in.skipBytes(4);
points = new double[3][2];
for (int j = 0; j < points.length; j++) {
for (int k = 0; k < points[j].length; k++) {
points[j][k] = in.readDouble();
}
}
StringBuilder p = new StringBuilder();
for (int j = 0; j < points.length; j++) {
p.append(points[j][0]);
p.append(",");
p.append(points[j][1]);
if (j < points.length - 1)
p.append(" ");
}
store.setROIID(roiID, i);
store.setPolylineID(shapeID, i, 0);
store.setPolylinePoints(p.toString(), i, 0);
if (fontSize != null) {
store.setPolylineFontSize(fontSize, i, 0);
}
store.setPolylineStrokeWidth(line, i, 0);
store.setImageROIRef(roiID, series, i);
break;
case CLOSED_POLYLINE:
case OPEN_POLYLINE:
case POLYLINE_ARROW:
int nKnots = in.readInt();
points = new double[nKnots][2];
for (int j = 0; j < points.length; j++) {
for (int k = 0; k < points[j].length; k++) {
points[j][k] = in.readDouble();
}
}
p = new StringBuilder();
for (int j = 0; j < points.length; j++) {
p.append(points[j][0]);
p.append(",");
p.append(points[j][1]);
if (j < points.length - 1)
p.append(" ");
}
store.setROIID(roiID, i);
if (type != CLOSED_POLYLINE) {
store.setPolylinePoints(p.toString(), i, 0);
if (fontSize != null) {
store.setPolylineFontSize(fontSize, i, 0);
}
store.setPolylineStrokeWidth(line, i, 0);
store.setPolylineID(shapeID, i, 0);
} else {
store.setPolygonPoints(p.toString(), i, 0);
if (fontSize != null) {
store.setPolygonFontSize(fontSize, i, 0);
}
store.setPolygonStrokeWidth(line, i, 0);
store.setPolygonID(shapeID, i, 0);
}
store.setImageROIRef(roiID, series, i);
break;
case CLOSED_BEZIER:
case OPEN_BEZIER:
case BEZIER_WITH_ARROW:
nKnots = in.readInt();
points = new double[nKnots][2];
for (int j = 0; j < points.length; j++) {
for (int k = 0; k < points[j].length; k++) {
points[j][k] = in.readDouble();
}
}
p = new StringBuilder();
for (int j = 0; j < points.length; j++) {
p.append(points[j][0]);
p.append(",");
p.append(points[j][1]);
if (j < points.length - 1)
p.append(" ");
}
store.setROIID(roiID, i);
if (type == OPEN_BEZIER) {
store.setPolylineID(shapeID, i, 0);
store.setPolylinePoints(p.toString(), i, 0);
if (fontSize != null) {
store.setPolylineFontSize(fontSize, i, 0);
}
store.setPolylineStrokeWidth(line, i, 0);
} else {
store.setPolygonID(shapeID, i, 0);
store.setPolygonPoints(p.toString(), i, 0);
if (fontSize != null) {
store.setPolygonFontSize(fontSize, i, 0);
}
store.setPolygonStrokeWidth(line, i, 0);
}
store.setImageROIRef(roiID, series, i);
break;
default:
i--;
numberOfShapes--;
continue;
}
// populate shape attributes
in.seek(offset + blockLength);
}
totalROIs += numberOfShapes;
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class VarianFDFReader method parseFDF.
// -- Helper methods --
private void parseFDF(String file) throws FormatException, IOException {
in = new RandomAccessInputStream(file);
CoreMetadata m = core.get(0);
boolean storedFloats = false;
boolean multifile = false;
String data = in.readString(Character.toString((char) 0x0c));
String[] lines = data.split("\n");
for (String line : lines) {
line = line.trim();
if (line.length() == 0)
break;
if (line.startsWith("#"))
continue;
int space = line.indexOf(' ');
int eq = line.indexOf('=');
String type = line.substring(0, space).trim();
String var = line.substring(space, eq).trim();
String value = line.substring(eq + 1, line.indexOf(';')).trim();
if (var.equals("*storage")) {
storedFloats = value.equals("\"float\"");
}
if (var.equals("bits")) {
m.bitsPerPixel = Integer.parseInt(value);
if (value.equals("8")) {
m.pixelType = FormatTools.UINT8;
} else if (value.equals("16")) {
m.pixelType = FormatTools.UINT16;
} else if (value.equals("32")) {
if (storedFloats) {
m.pixelType = FormatTools.FLOAT;
} else
m.pixelType = FormatTools.UINT32;
} else
throw new FormatException("Unsupported bits: " + value);
} else if (var.equals("matrix[]")) {
String[] values = parseArray(value);
m.sizeX = (int) Double.parseDouble(values[0]);
m.sizeY = (int) Double.parseDouble(values[1]);
if (values.length > 2) {
m.sizeZ = (int) Double.parseDouble(values[2]);
}
} else if (var.equals("slices")) {
m.sizeZ = Integer.parseInt(value);
multifile = true;
} else if (var.equals("echoes")) {
m.sizeT = Integer.parseInt(value);
multifile = true;
} else if (var.equals("span[]")) {
String[] values = parseArray(value);
if (values.length > 0) {
pixelSizeX = computePhysicalSize(getSizeX(), values[0], units[0]);
}
if (values.length > 1) {
pixelSizeY = computePhysicalSize(getSizeY(), values[1], units[1]);
}
if (values.length > 2) {
pixelSizeZ = computePhysicalSize(getSizeZ(), values[2], units[2]);
}
} else if (var.equals("origin[]")) {
String[] values = parseArray(value);
if (values.length > 0) {
final double size = computePhysicalSize(1, values[0], units[0]);
originX = new Length(size, UNITS.REFERENCEFRAME);
addGlobalMeta("X position for position #1", originX);
}
if (values.length > 1) {
final double size = computePhysicalSize(1, values[1], units[1]);
originY = new Length(size, UNITS.REFERENCEFRAME);
addGlobalMeta("Y position for position #1", originY);
}
if (values.length > 2) {
final double size = computePhysicalSize(1, values[2], units[2]);
originZ = new Length(size, UNITS.REFERENCEFRAME);
addGlobalMeta("Z position for position #1", originZ);
}
} else if (var.equals("*abscissa[]")) {
units = parseArray(value);
} else if (var.equals("bigendian")) {
m.littleEndian = value.equals("0");
in.order(isLittleEndian());
}
addGlobalMeta(var, value);
}
if (multifile && files.isEmpty()) {
Location thisFile = new Location(file).getAbsoluteFile();
Location parent = thisFile.getParentFile();
String[] list = parent.list(true);
Arrays.sort(list);
for (String f : list) {
if (checkSuffix(f, "fdf") && f.length() == thisFile.getName().length()) {
files.add(new Location(parent, f).getAbsolutePath());
}
}
}
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class VarianFDFReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
CoreMetadata m = core.get(0);
m.sizeZ = 1;
m.sizeC = 1;
m.sizeT = 1;
parseFDF(id);
m.imageCount = getSizeZ() * getSizeC() * getSizeT();
m.dimensionOrder = "XYTZC";
if (files.size() > getImageCount()) {
int rem = files.size() / getImageCount();
m.sizeT *= rem;
m.imageCount = getSizeZ() * getSizeC() * getSizeT();
}
pixelOffsets = new long[getImageCount()];
int planeSize = FormatTools.getPlaneSize(this);
for (int i = 0; i < pixelOffsets.length; i++) {
if (files.size() > 1) {
in.close();
in = new RandomAccessInputStream(files.get(i));
pixelOffsets[i] = in.length() - planeSize;
} else {
pixelOffsets[i] = in.length() - (planeSize * (getImageCount() - i));
}
}
boolean minMetadata = getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, !minMetadata);
if (!minMetadata) {
Length sizeX = FormatTools.getPhysicalSizeX(pixelSizeX);
Length sizeY = FormatTools.getPhysicalSizeY(pixelSizeY);
Length sizeZ = FormatTools.getPhysicalSizeZ(pixelSizeZ);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, 0);
}
for (int i = 0; i < getImageCount(); i++) {
store.setPlanePositionX(originX, 0, i);
store.setPlanePositionY(originY, 0, i);
store.setPlanePositionZ(originZ, 0, i);
}
}
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class ScanrReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
if (metadataFiles.size() > 0) {
// this dataset has already been initialized
return;
}
// make sure we have the .xml file
if (!checkSuffix(id, "xml") && isGroupFiles()) {
Location parent = new Location(id).getAbsoluteFile().getParentFile();
if (checkSuffix(id, "tif") && parent.getName().equalsIgnoreCase("Data")) {
parent = parent.getParentFile();
}
String[] list = parent.list();
for (String file : list) {
if (file.equals(XML_FILE)) {
id = new Location(parent, file).getAbsolutePath();
super.initFile(id);
break;
}
}
if (!checkSuffix(id, "xml")) {
throw new FormatException("Could not find " + XML_FILE + " in " + parent.getAbsolutePath());
}
} else if (!isGroupFiles() && checkSuffix(id, "tif")) {
TiffReader r = new TiffReader();
r.setMetadataStore(getMetadataStore());
r.setId(id);
core = new ArrayList<CoreMetadata>(r.getCoreMetadataList());
metadataStore = r.getMetadataStore();
final Map<String, Object> globalMetadata = r.getGlobalMetadata();
for (final Map.Entry<String, Object> entry : globalMetadata.entrySet()) {
addGlobalMeta(entry.getKey(), entry.getValue());
}
r.close();
tiffs = new String[] { id };
reader = new MinimalTiffReader();
return;
}
Location dir = new Location(id).getAbsoluteFile().getParentFile();
String[] list = dir.list(true);
for (String file : list) {
Location f = new Location(dir, file);
if (checkSuffix(file, METADATA_SUFFIXES) && !f.isDirectory()) {
metadataFiles.add(f.getAbsolutePath());
}
}
// parse XML metadata
String xml = DataTools.readFile(id).trim();
if (xml.startsWith("<?")) {
xml = xml.substring(xml.indexOf("?>") + 2);
}
xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + xml;
XMLTools.parseXML(xml, new ScanrHandler());
final List<String> uniqueRows = new ArrayList<String>();
final List<String> uniqueColumns = new ArrayList<String>();
if (wellRows == 0 || wellColumns == 0) {
for (String well : wellLabels.keySet()) {
if (!Character.isLetter(well.charAt(0)))
continue;
String row = well.substring(0, 1).trim();
String column = well.substring(1).trim();
if (!uniqueRows.contains(row) && row.length() > 0)
uniqueRows.add(row);
if (!uniqueColumns.contains(column) && column.length() > 0) {
uniqueColumns.add(column);
}
}
wellRows = uniqueRows.size();
wellColumns = uniqueColumns.size();
if (wellRows * wellColumns != wellCount) {
adjustWellDimensions();
}
}
int nChannels = getSizeC() == 0 ? channelNames.size() : (int) Math.min(channelNames.size(), getSizeC());
if (nChannels == 0)
nChannels = 1;
int nSlices = getSizeZ() == 0 ? 1 : getSizeZ();
int nTimepoints = getSizeT();
int nWells = wellCount;
int nPos = 0;
if (foundPositions)
nPos = fieldPositionX.length;
else
nPos = fieldRows * fieldColumns;
if (nPos == 0)
nPos = 1;
// get list of TIFF files
Location dataDir = new Location(dir, "data");
list = dataDir.list(true);
if (list == null) {
// try to find the TIFFs in the current directory
list = dir.list(true);
} else
dir = dataDir;
if (nTimepoints == 0 || list.length < nTimepoints * nChannels * nSlices * nWells * nPos) {
nTimepoints = list.length / (nChannels * nWells * nPos * nSlices);
if (nTimepoints == 0)
nTimepoints = 1;
}
tiffs = new String[nChannels * nWells * nPos * nTimepoints * nSlices];
Arrays.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int lastSeparator1 = s1.lastIndexOf(File.separator) + 1;
int lastSeparator2 = s2.lastIndexOf(File.separator) + 1;
String dir1 = s1.substring(0, lastSeparator1);
String dir2 = s2.substring(0, lastSeparator2);
if (!dir1.equals(dir2)) {
return dir1.compareTo(dir2);
}
int dash1 = s1.indexOf("-", lastSeparator1);
int dash2 = s2.indexOf("-", lastSeparator2);
String label1 = dash1 < 0 ? "" : s1.substring(lastSeparator1, dash1);
String label2 = dash2 < 0 ? "" : s2.substring(lastSeparator2, dash2);
if (label1.equals(label2)) {
String remainder1 = dash1 < 0 ? s1 : s1.substring(dash1);
String remainder2 = dash2 < 0 ? s2 : s2.substring(dash2);
return remainder1.compareTo(remainder2);
}
Integer index1 = wellLabels.get(label1);
Integer index2 = wellLabels.get(label2);
if (index1 == null && index2 != null) {
return 1;
} else if (index1 != null && index2 == null) {
return -1;
}
return index1.compareTo(index2);
}
});
int lastListIndex = 0;
int next = 0;
String[] keys = wellLabels.keySet().toArray(new String[wellLabels.size()]);
Arrays.sort(keys, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
char row1 = s1.charAt(0);
char row2 = s2.charAt(0);
final Integer col1 = new Integer(s1.substring(1));
final Integer col2 = new Integer(s2.substring(1));
if (row1 < row2) {
return -1;
} else if (row1 > row2) {
return 1;
}
return col1.compareTo(col2);
}
});
int realPosCount = 0;
for (int well = 0; well < nWells; well++) {
int missingWellFiles = 0;
int wellIndex = wellNumbers.get(well);
String wellPos = getBlock(wellIndex, "W");
int originalIndex = next;
for (int pos = 0; pos < nPos; pos++) {
String posPos = getBlock(pos + 1, "P");
int posIndex = next;
for (int z = 0; z < nSlices; z++) {
String zPos = getBlock(z, "Z");
for (int t = 0; t < nTimepoints; t++) {
String tPos = getBlock(t, "T");
for (int c = 0; c < nChannels; c++) {
for (int i = lastListIndex; i < list.length; i++) {
String file = list[i];
if (file.indexOf(wellPos) != -1 && file.indexOf(zPos) != -1 && file.indexOf(posPos) != -1 && file.indexOf(tPos) != -1 && file.indexOf(channelNames.get(c)) != -1) {
tiffs[next++] = new Location(dir, file).getAbsolutePath();
if (c == nChannels - 1) {
lastListIndex = i;
}
break;
}
}
if (next == originalIndex) {
missingWellFiles++;
}
}
}
}
if (posIndex != next)
realPosCount++;
}
if (next == originalIndex && well < keys.length) {
wellLabels.remove(keys[well]);
}
if (next == originalIndex && missingWellFiles == nSlices * nTimepoints * nChannels * nPos) {
wellNumbers.remove(well);
}
}
nWells = wellNumbers.size();
if (wellLabels.size() > 0 && wellLabels.size() != nWells) {
uniqueRows.clear();
uniqueColumns.clear();
for (String well : wellLabels.keySet()) {
if (!Character.isLetter(well.charAt(0)))
continue;
String row = well.substring(0, 1).trim();
String column = well.substring(1).trim();
if (!uniqueRows.contains(row) && row.length() > 0)
uniqueRows.add(row);
if (!uniqueColumns.contains(column) && column.length() > 0) {
uniqueColumns.add(column);
}
}
nWells = uniqueRows.size() * uniqueColumns.size();
adjustWellDimensions();
}
if (realPosCount < nPos) {
nPos = realPosCount;
}
reader = new MinimalTiffReader();
reader.setId(tiffs[0]);
int sizeX = reader.getSizeX();
int sizeY = reader.getSizeY();
int pixelType = reader.getPixelType();
tileWidth = reader.getOptimalTileWidth();
tileHeight = reader.getOptimalTileHeight();
switch(pixelType) {
case FormatTools.INT8:
pixelType = FormatTools.UINT8;
break;
case FormatTools.INT16:
pixelType = FormatTools.UINT16;
break;
}
boolean rgb = reader.isRGB();
boolean interleaved = reader.isInterleaved();
boolean indexed = reader.isIndexed();
boolean littleEndian = reader.isLittleEndian();
reader.close();
int seriesCount = nWells * nPos;
core.clear();
for (int i = 0; i < seriesCount; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
ms.sizeC = nChannels;
ms.sizeZ = nSlices;
ms.sizeT = nTimepoints;
ms.sizeX = sizeX;
ms.sizeY = sizeY;
ms.pixelType = pixelType;
ms.rgb = rgb;
ms.interleaved = interleaved;
ms.indexed = indexed;
ms.littleEndian = littleEndian;
ms.dimensionOrder = "XYCTZ";
ms.imageCount = nSlices * nTimepoints * nChannels;
ms.bitsPerPixel = 12;
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
store.setPlateColumns(new PositiveInteger(wellColumns), 0);
store.setPlateRows(new PositiveInteger(wellRows), 0);
String plateAcqID = MetadataTools.createLSID("PlateAcquisition", 0, 0);
store.setPlateAcquisitionID(plateAcqID, 0, 0);
int nFields = 0;
if (foundPositions) {
nFields = fieldPositionX.length;
} else {
nFields = fieldRows * fieldColumns;
}
PositiveInteger fieldCount = FormatTools.getMaxFieldCount(nFields);
if (fieldCount != null) {
store.setPlateAcquisitionMaximumFieldCount(fieldCount, 0, 0);
}
for (int i = 0; i < getSeriesCount(); i++) {
int field = i % nFields;
int well = i / nFields;
int index = well;
while (wellNumbers.get(index) == null && index < wellNumbers.size()) {
index++;
}
int wellIndex = wellNumbers.get(index) == null ? index : wellNumbers.get(index) - 1;
int wellRow = wellIndex / wellColumns;
int wellCol = wellIndex % wellColumns;
if (field == 0) {
store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
store.setWellColumn(new NonNegativeInteger(wellCol), 0, well);
store.setWellRow(new NonNegativeInteger(wellRow), 0, well);
}
String wellSample = MetadataTools.createLSID("WellSample", 0, well, field);
store.setWellSampleID(wellSample, 0, well, field);
store.setWellSampleIndex(new NonNegativeInteger(i), 0, well, field);
String imageID = MetadataTools.createLSID("Image", i);
store.setWellSampleImageRef(imageID, 0, well, field);
store.setImageID(imageID, i);
String name = "Well " + (well + 1) + ", Field " + (field + 1) + " (Spot " + (i + 1) + ")";
store.setImageName(name, i);
store.setPlateAcquisitionWellSampleRef(wellSample, 0, 0, i);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
for (int i = 0; i < getSeriesCount(); i++) {
for (int c = 0; c < getSizeC(); c++) {
store.setChannelName(channelNames.get(c), i, c);
}
Length x = FormatTools.getPhysicalSizeX(pixelSize);
Length y = FormatTools.getPhysicalSizeY(pixelSize);
if (x != null) {
store.setPixelsPhysicalSizeX(x, i);
}
if (y != null) {
store.setPixelsPhysicalSizeY(y, i);
}
if (fieldPositionX != null && fieldPositionY != null) {
int field = i % nFields;
int well = i / nFields;
final Length posX = fieldPositionX[field];
final Length posY = fieldPositionY[field];
store.setWellSamplePositionX(posX, 0, well, field);
store.setWellSamplePositionY(posY, 0, well, field);
for (int c = 0; c < getSizeC(); c++) {
int image = getIndex(0, c, 0);
store.setPlaneTheZ(new NonNegativeInteger(0), i, image);
store.setPlaneTheC(new NonNegativeInteger(c), i, image);
store.setPlaneTheT(new NonNegativeInteger(0), i, image);
store.setPlanePositionX(fieldPositionX[field], i, image);
store.setPlanePositionY(fieldPositionY[field], i, image);
// exposure time is stored in milliseconds
// convert to seconds before populating MetadataStore
Double time = exposures.get(c);
if (time != null) {
time /= 1000;
store.setPlaneExposureTime(new Time(time, UNITS.SECOND), i, image);
}
if (deltaT != null) {
store.setPlaneDeltaT(new Time(deltaT, UNITS.SECOND), i, image);
}
}
}
}
String row = wellRows > 26 ? "Number" : "Letter";
String col = wellRows > 26 ? "Letter" : "Number";
store.setPlateRowNamingConvention(getNamingConvention(row), 0);
store.setPlateColumnNamingConvention(getNamingConvention(col), 0);
store.setPlateName(plateName, 0);
}
}
use of ome.units.quantity.Length in project bioformats by openmicroscopy.
the class ColumbusReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
protected void initFile(String id) throws FormatException, IOException {
Location xml = findXML(id);
if (null == xml) {
throw new FormatException("Could not find " + XML_FILE);
}
id = xml.getAbsolutePath();
super.initFile(id);
Location parent = new Location(currentId).getAbsoluteFile().getParentFile();
// parse plate layout and image dimensions from the XML files
String xmlData = DataTools.readFile(id);
MeasurementHandler handler = new MeasurementHandler();
XMLTools.parseXML(xmlData, handler);
String[] parentDirectories = parent.list(true);
Arrays.sort(parentDirectories);
ArrayList<String> timepointDirs = new ArrayList<String>();
for (String file : parentDirectories) {
Location absFile = new Location(parent, file);
if (absFile.isDirectory()) {
timepointDirs.add(absFile.getAbsolutePath());
for (String f : absFile.list(true)) {
if (!checkSuffix(f, "tif")) {
if (!metadataFiles.contains(file + File.separator + f)) {
metadataFiles.add(file + File.separator + f);
}
}
}
}
}
for (int i = 0; i < metadataFiles.size(); i++) {
String metadataFile = metadataFiles.get(i);
int end = metadataFile.indexOf(File.separator);
String timepointPath = end < 0 ? "" : parent + File.separator + metadataFile.substring(0, end);
Location f = new Location(parent + File.separator + metadataFile);
if (!f.exists()) {
metadataFile = metadataFile.substring(end + 1);
f = new Location(parent, metadataFile);
}
String path = f.getAbsolutePath();
metadataFiles.set(i, path);
if (checkSuffix(path, "columbusidx.xml")) {
int timepoint = timepointDirs.indexOf(timepointPath);
if (timepointDirs.size() == 0) {
timepoint = 0;
}
parseImageXML(path, timepoint);
}
}
// process plane list to determine plate size
Comparator<Plane> planeComp = new Comparator<Plane>() {
public int compare(Plane p1, Plane p2) {
if (p1.row != p2.row) {
return p1.row - p2.row;
}
if (p1.col != p2.col) {
return p1.col - p2.col;
}
if (p1.field != p2.field) {
return p1.field - p2.field;
}
if (p1.timepoint != p2.timepoint) {
return p1.timepoint - p2.timepoint;
}
if (p1.channel != p2.channel) {
return p1.channel - p2.channel;
}
return 0;
}
};
Plane[] tmpPlanes = planes.toArray(new Plane[planes.size()]);
Arrays.sort(tmpPlanes, planeComp);
planes.clear();
reader = new MinimalTiffReader();
reader.setId(tmpPlanes[0].file);
core = reader.getCoreMetadataList();
CoreMetadata m = core.get(0);
m.sizeC = 0;
m.sizeT = 0;
ArrayList<Integer> uniqueSamples = new ArrayList<Integer>();
ArrayList<Integer> uniqueRows = new ArrayList<Integer>();
ArrayList<Integer> uniqueCols = new ArrayList<Integer>();
for (Plane p : tmpPlanes) {
planes.add(p);
int sampleIndex = p.row * handler.getPlateColumns() + p.col;
if (!uniqueSamples.contains(sampleIndex)) {
uniqueSamples.add(sampleIndex);
}
if (!uniqueRows.contains(p.row)) {
uniqueRows.add(p.row);
}
if (!uniqueCols.contains(p.col)) {
uniqueCols.add(p.col);
}
// counts are assumed to be non-sparse
if (p.field >= nFields) {
nFields = p.field + 1;
}
if (p.channel >= getSizeC()) {
m.sizeC = p.channel + 1;
}
if (p.timepoint >= getSizeT()) {
m.sizeT = p.timepoint + 1;
}
}
m.sizeZ = 1;
m.imageCount = getSizeZ() * getSizeC() * getSizeT();
m.dimensionOrder = "XYCTZ";
m.rgb = false;
int seriesCount = uniqueSamples.size() * nFields;
for (int i = 1; i < seriesCount; i++) {
core.add(m);
}
// populate the MetadataStore
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
store.setScreenID(MetadataTools.createLSID("Screen", 0), 0);
store.setScreenName(handler.getScreenName(), 0);
store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
store.setPlateName(handler.getPlateName(), 0);
store.setPlateRows(new PositiveInteger(handler.getPlateRows()), 0);
store.setPlateColumns(new PositiveInteger(handler.getPlateColumns()), 0);
String imagePrefix = handler.getPlateName() + " Well ";
int wellSample = 0;
int nextWell = -1;
Timestamp date = new Timestamp(acquisitionDate);
long timestampSeconds = date.asInstant().getMillis() / 1000;
for (Integer row : uniqueRows) {
for (Integer col : uniqueCols) {
if (!uniqueSamples.contains(row * handler.getPlateColumns() + col)) {
continue;
}
nextWell++;
store.setWellID(MetadataTools.createLSID("Well", 0, nextWell), 0, nextWell);
store.setWellRow(new NonNegativeInteger(row), 0, nextWell);
store.setWellColumn(new NonNegativeInteger(col), 0, nextWell);
for (int field = 0; field < nFields; field++) {
Plane p = lookupPlane(row, col, field, 0, 0);
String wellSampleID = MetadataTools.createLSID("WellSample", 0, nextWell, field);
store.setWellSampleID(wellSampleID, 0, nextWell, field);
store.setWellSampleIndex(new NonNegativeInteger(wellSample), 0, nextWell, field);
if (p != null) {
store.setWellSamplePositionX(new Length(p.positionX, UNITS.REFERENCEFRAME), 0, nextWell, field);
store.setWellSamplePositionY(new Length(p.positionY, UNITS.REFERENCEFRAME), 0, nextWell, field);
}
String imageID = MetadataTools.createLSID("Image", wellSample);
store.setImageID(imageID, wellSample);
store.setWellSampleImageRef(imageID, 0, nextWell, field);
store.setImageName(imagePrefix + (char) (row + 'A') + (col + 1) + " Field #" + (field + 1), wellSample);
store.setImageAcquisitionDate(date, wellSample);
if (p != null) {
p.series = wellSample;
store.setPixelsPhysicalSizeX(FormatTools.getPhysicalSizeX(p.sizeX), p.series);
store.setPixelsPhysicalSizeY(FormatTools.getPhysicalSizeY(p.sizeY), p.series);
for (int c = 0; c < getSizeC(); c++) {
p = lookupPlane(row, col, field, 0, c);
if (p != null) {
p.series = wellSample;
store.setChannelName(p.channelName, p.series, p.channel);
if ((int) p.emWavelength > 0) {
store.setChannelEmissionWavelength(FormatTools.getEmissionWavelength(p.emWavelength), p.series, p.channel);
}
if ((int) p.exWavelength > 0) {
store.setChannelExcitationWavelength(FormatTools.getExcitationWavelength(p.exWavelength), p.series, p.channel);
}
store.setChannelColor(p.channelColor, p.series, p.channel);
}
for (int t = 0; t < getSizeT(); t++) {
p = lookupPlane(row, col, field, t, c);
if (p != null) {
p.series = wellSample;
store.setPlaneDeltaT(new Time(p.deltaT - timestampSeconds, UNITS.SECOND), p.series, getIndex(0, c, t));
}
}
}
}
wellSample++;
}
}
}
}
Aggregations