use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.
the class WlzReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
try {
ServiceFactory factory = new ServiceFactory();
wlz = factory.getInstance(WlzService.class);
} catch (DependencyException e) {
throw new FormatException(NO_WLZ_MSG, e);
}
if (wlz != null) {
wlz.open(id, "r");
CoreMetadata md = core.get(0);
MetadataStore store = makeFilterMetadata();
md.rgb = wlz.isRGB();
md.interleaved = false;
md.indexed = false;
md.sizeX = wlz.getSizeX();
md.sizeY = wlz.getSizeY();
md.sizeZ = wlz.getSizeZ();
md.sizeC = wlz.getSizeC();
md.sizeT = wlz.getSizeT();
md.dimensionOrder = "XYZCT";
md.imageCount = wlz.getSizeZ();
md.pixelType = wlz.getPixelType();
PositiveFloat x = new PositiveFloat(Math.abs(wlz.getVoxSzX()));
PositiveFloat y = new PositiveFloat(Math.abs(wlz.getVoxSzY()));
PositiveFloat z = new PositiveFloat(Math.abs(wlz.getVoxSzZ()));
store.setPixelsPhysicalSizeX(FormatTools.createLength(x, UNITS.MICROMETER), 0);
store.setPixelsPhysicalSizeY(FormatTools.createLength(y, UNITS.MICROMETER), 0);
store.setPixelsPhysicalSizeZ(FormatTools.createLength(z, UNITS.MICROMETER), 0);
store.setStageLabelName(wlz.getWlzOrgLabelName(), 0);
store.setStageLabelX(new Length(wlz.getOrgX(), UNITS.REFERENCEFRAME), 0);
store.setStageLabelY(new Length(wlz.getOrgY(), UNITS.REFERENCEFRAME), 0);
store.setStageLabelZ(new Length(wlz.getOrgZ(), UNITS.REFERENCEFRAME), 0);
MetadataTools.populatePixels(store, this);
}
}
use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.
the class ZeissCZIReader method calculateDimensions.
private void calculateDimensions(int coreIndex, boolean xyOnly) {
// calculate the dimensions
CoreMetadata ms0 = core.get(coreIndex);
int previousCoreIndex = getCoreIndex();
setCoreIndex(coreIndex);
ArrayList<Integer> uniqueT = new ArrayList<Integer>();
for (SubBlock plane : planes) {
if (xyOnly && plane.coreIndex != coreIndex) {
continue;
}
for (DimensionEntry dimension : plane.directoryEntry.dimensionEntries) {
if (dimension == null) {
continue;
}
if (xyOnly && dimension.dimension.charAt(0) != 'X' && dimension.dimension.charAt(0) != 'Y') {
continue;
}
switch(dimension.dimension.charAt(0)) {
case 'X':
plane.x = dimension.size;
plane.col = dimension.start;
if ((prestitched == null || prestitched) && getSizeX() > 0 && dimension.size != getSizeX() && allowAutostitching()) {
prestitched = true;
continue;
}
if (allowAutostitching() || ms0.sizeX == 0 || dimension.size == dimension.storedSize) {
ms0.sizeX = dimension.size;
}
break;
case 'Y':
plane.y = dimension.size;
plane.row = dimension.start;
if ((prestitched == null || prestitched) && getSizeY() > 0 && dimension.size != getSizeY() && allowAutostitching()) {
prestitched = true;
continue;
}
if (allowAutostitching() || ms0.sizeY == 0 || dimension.size == dimension.storedSize) {
ms0.sizeY = dimension.size;
}
break;
case 'C':
if (dimension.start >= getSizeC()) {
ms0.sizeC = dimension.start + 1;
}
break;
case 'Z':
if (dimension.start > 0 && dimension.start >= getSizeZ()) {
ms0.sizeZ = dimension.start + 1;
} else if (dimension.size > getSizeZ()) {
ms0.sizeZ = dimension.size;
}
break;
case 'T':
if (!uniqueT.contains(dimension.start)) {
uniqueT.add(dimension.start);
ms0.sizeT = uniqueT.size();
}
if (dimension.size > getSizeT()) {
ms0.sizeT = dimension.size;
}
break;
case 'R':
if (dimension.start >= rotations) {
rotations = dimension.start + 1;
}
break;
case 'S':
if (dimension.start >= positions) {
positions = dimension.start + 1;
}
break;
case 'I':
if (dimension.start >= illuminations) {
illuminations = dimension.start + 1;
}
break;
case 'B':
if (dimension.start >= acquisitions) {
acquisitions = dimension.start + 1;
}
break;
case 'M':
if (dimension.start >= mosaics) {
mosaics = dimension.start + 1;
}
break;
case 'H':
if (dimension.start >= phases) {
phases = dimension.start + 1;
}
break;
case 'V':
if (dimension.start >= angles) {
angles = dimension.start + 1;
}
break;
default:
LOGGER.warn("Unknown dimension '{}'", dimension.dimension);
}
}
}
setCoreIndex(previousCoreIndex);
}
use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.
the class ZeissLMSReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
public void initFile(String id) throws FormatException, IOException {
super.initFile(id);
in = new RandomAccessInputStream(id);
CoreMetadata m = core.get(0);
CoreMetadata thumb = new CoreMetadata();
m.littleEndian = true;
thumb.littleEndian = true;
in.order(m.littleEndian);
in.seek(18);
double magnification = in.readInt();
// all assumed to be constant based upon the CSM 700 data sheet
thumb.sizeX = 1280;
thumb.sizeY = 1024;
thumb.pixelType = FormatTools.UINT8;
thumb.sizeC = 3;
thumb.rgb = true;
thumb.interleaved = true;
thumb.dimensionOrder = "XYCZT";
m.sizeX = 1280;
m.sizeY = 1024;
m.pixelType = FormatTools.UINT16;
m.sizeC = 1;
m.rgb = false;
m.dimensionOrder = "XYCZT";
m.indexed = true;
// each image can be found using the "BM6" marker
seekToNextMarker();
in.skipBytes(50);
offsets.add(in.getFilePointer());
in.skipBytes(thumb.sizeX * thumb.sizeY * thumb.sizeC);
seekToNextMarker();
in.skipBytes(50);
lut = new byte[3][256];
for (int i = 0; i < lut[0].length; i++) {
for (int j = 0; j < lut.length; j++) {
lut[j][i] = in.readByte();
}
// skip alpha channel
in.skipBytes(1);
}
offsets.add(in.getFilePointer());
// again, Z stack is assumed based upon the CSM 700 data sheet
thumb.sizeZ = 1;
thumb.sizeT = 1;
thumb.imageCount = thumb.sizeZ * thumb.sizeT;
long availableBytes = in.length() - offsets.get(1);
int planeSize = m.sizeX * m.sizeY * FormatTools.getBytesPerPixel(m.pixelType);
m.sizeZ = (int) (availableBytes / planeSize);
m.sizeT = 1;
m.imageCount = m.sizeZ * m.sizeT;
core.add(thumb);
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setInstrumentID(MetadataTools.createLSID("Instrument", 0), 0);
String objective = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objective, 0, 0);
store.setObjectiveNominalMagnification(magnification, 0, 0);
store.setObjectiveSettingsID(objective, 0);
store.setObjectiveSettingsID(objective, 1);
}
use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.
the class TillVisionReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "vws")) {
Location pst = new Location(id).getAbsoluteFile();
String name = pst.getParentFile().getName();
Location parent = pst.getParentFile().getParentFile();
Location vwsFile = new Location(parent, name.replaceAll(".pst", ".vws"));
if (vwsFile.exists() && !vwsFile.isDirectory()) {
id = vwsFile.getAbsolutePath();
} else if (vwsFile.isDirectory()) {
parent = pst.getParentFile();
String[] list = parent.list(true);
boolean foundVWS = false;
for (String f : list) {
if (checkSuffix(f, "vws")) {
id = new Location(parent, f).getAbsolutePath();
foundVWS = true;
break;
}
}
if (!foundVWS) {
throw new FormatException("Could not find .vws file.");
}
} else
throw new FormatException("Could not find .vws file.");
}
super.initFile(id);
exposureTimes = new HashMap<Integer, Double>();
POIService poi = null;
try {
ServiceFactory factory = new ServiceFactory();
poi = factory.getInstance(POIService.class);
} catch (DependencyException de) {
throw new FormatException("POI library not found", de);
}
poi.initialize(id);
Vector<String> documents = poi.getDocumentList();
int nImages = 0;
final Hashtable<String, Object> tmpSeriesMetadata = new Hashtable<String, Object>();
for (String name : documents) {
LOGGER.debug("Reading {}", name);
if (name.equals("Root Entry" + File.separator + "Contents")) {
RandomAccessInputStream s = poi.getDocumentStream(name);
s.order(true);
boolean specialCImage = false;
int nFound = 0;
Long[] cimages = null;
Location dir = new Location(id).getAbsoluteFile().getParentFile();
String[] list = dir.list(true);
boolean hasPST = false;
for (String f : list) {
if (checkSuffix(f, "pst")) {
hasPST = true;
break;
}
}
if (!hasPST) {
cimages = findImages(s);
nFound = cimages.length;
if (nFound == 0) {
s.seek(13);
int len = s.readShort();
String type = s.readString(len);
if (type.equals("CImage")) {
nFound = 1;
cimages = new Long[] { s.getFilePointer() + 6 };
specialCImage = true;
}
}
embeddedImages = nFound > 0;
}
LOGGER.debug("Images are {}embedded", embeddedImages ? "" : "not ");
if (embeddedImages) {
core.clear();
embeddedOffset = new long[nFound];
for (int i = 0; i < nFound; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
s.seek(cimages[i]);
int len = s.read();
String imageName = s.readString(len);
imageNames.add(imageName);
if (specialCImage) {
s.seek(1280);
} else {
while (true) {
if (s.readString(2).equals("sB")) {
break;
} else
s.seek(s.getFilePointer() - 1);
}
}
s.skipBytes(20);
ms.sizeX = s.readInt();
ms.sizeY = s.readInt();
ms.sizeZ = s.readInt();
ms.sizeC = s.readInt();
ms.sizeT = s.readInt();
ms.pixelType = convertPixelType(s.readInt());
if (specialCImage) {
embeddedOffset[i] = s.getFilePointer() + 27;
} else {
embeddedOffset[i] = s.getFilePointer() + 31;
}
}
if (in != null)
in.close();
in = poi.getDocumentStream(name);
s.close();
break;
}
s.seek(0);
int lowerBound = 0;
int upperBound = 0x1000;
while (s.getFilePointer() < s.length() - 2) {
LOGGER.debug(" Looking for image at {}", s.getFilePointer());
s.order(false);
int nextOffset = findNextOffset(s);
if (nextOffset < 0 || nextOffset >= s.length())
break;
s.seek(nextOffset);
s.skipBytes(3);
int len = s.readShort();
if (len <= 0)
continue;
imageNames.add(s.readString(len));
if (s.getFilePointer() + 8 >= s.length())
break;
s.skipBytes(6);
s.order(true);
len = s.readShort();
if (nImages == 0 && len > upperBound * 2 && len < upperBound * 4) {
lowerBound = 512;
upperBound = 0x4000;
}
if (len < lowerBound || len > upperBound)
continue;
String description = s.readString(len);
LOGGER.debug("Description: {}", description);
// parse key/value pairs from description
String dateTime = "";
String[] lines = description.split("[\r\n]");
for (String line : lines) {
line = line.trim();
int colon = line.indexOf(':');
if (colon != -1 && !line.startsWith(";")) {
String key = line.substring(0, colon).trim();
String value = line.substring(colon + 1).trim();
String metaKey = "Series " + nImages + " " + key;
addMeta(metaKey, value, tmpSeriesMetadata);
if (key.equals("Start time of experiment")) {
// HH:mm:ss aa OR HH:mm:ss.sss aa
dateTime += " " + value;
} else if (key.equals("Date")) {
// mm/dd/yy ?
dateTime = value + " " + dateTime;
} else if (key.equals("Exposure time [ms]")) {
double exp = Double.parseDouble(value) / 1000;
exposureTimes.put(nImages, exp);
} else if (key.equals("Image type")) {
types.add(value);
}
}
}
dateTime = dateTime.trim();
if (!dateTime.equals("")) {
boolean success = false;
for (String format : DATE_FORMATS) {
try {
dateTime = DateTools.formatDate(dateTime, format, ".");
success = true;
} catch (NullPointerException e) {
}
}
dates.add(success ? dateTime : "");
}
nImages++;
}
s.close();
}
}
Location directory = new Location(currentId).getAbsoluteFile().getParentFile();
String[] pixelsFile = new String[nImages];
if (!embeddedImages) {
if (nImages == 0) {
throw new FormatException("No images found.");
}
// look for appropriate pixels files
String[] files = directory.list(true);
String name = currentId.substring(currentId.lastIndexOf(File.separator) + 1, currentId.lastIndexOf("."));
int nextFile = 0;
for (String f : files) {
if (checkSuffix(f, "pst")) {
Location pst = new Location(directory, f);
if (pst.isDirectory() && f.startsWith(name)) {
String[] subfiles = pst.list(true);
Arrays.sort(subfiles);
for (String q : subfiles) {
if (checkSuffix(q, "pst") && nextFile < nImages) {
pixelsFile[nextFile++] = f + File.separator + q;
}
}
}
}
}
if (nextFile == 0) {
for (String f : files) {
if (checkSuffix(f, "pst")) {
pixelsFile[nextFile++] = new Location(directory, f).getAbsolutePath();
}
}
if (nextFile == 0)
throw new FormatException("No image files found.");
}
}
Arrays.sort(pixelsFile);
int nSeries = core.size();
if (!embeddedImages) {
core.clear();
nSeries = nImages;
}
pixelsFiles = new String[nSeries];
infFiles = new String[nSeries];
Object[] metadataKeys = tmpSeriesMetadata.keySet().toArray();
IniParser parser = new IniParser();
for (int i = 0; i < nSeries; i++) {
CoreMetadata ms;
if (!embeddedImages) {
ms = new CoreMetadata();
core.add(ms);
setSeries(i);
// make sure that pixels file exists
String file = pixelsFile[i];
file = file.replace('/', File.separatorChar);
file = file.replace('\\', File.separatorChar);
String oldFile = file;
Location f = new Location(directory, oldFile);
if (!f.exists()) {
oldFile = oldFile.substring(oldFile.lastIndexOf(File.separator) + 1);
f = new Location(directory, oldFile);
if (!f.exists()) {
throw new FormatException("Could not find pixels file '" + file);
}
}
file = f.getAbsolutePath();
pixelsFiles[i] = file;
// read key/value pairs from .inf files
int dot = file.lastIndexOf(".");
String inf = file.substring(0, dot) + ".inf";
infFiles[i] = inf;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inf), Constants.ENCODING));
IniList data = parser.parseINI(reader);
reader.close();
IniTable infoTable = data.getTable("Info");
ms.sizeX = Integer.parseInt(infoTable.get("Width"));
ms.sizeY = Integer.parseInt(infoTable.get("Height"));
ms.sizeC = Integer.parseInt(infoTable.get("Bands"));
ms.sizeZ = Integer.parseInt(infoTable.get("Slices"));
ms.sizeT = Integer.parseInt(infoTable.get("Frames"));
int dataType = Integer.parseInt(infoTable.get("Datatype"));
ms.pixelType = convertPixelType(dataType);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
HashMap<String, String> iniMap = data.flattenIntoHashMap();
ms.seriesMetadata.putAll(iniMap);
}
} else {
ms = core.get(i);
setSeries(i);
}
ms.imageCount = ms.sizeZ * ms.sizeC * ms.sizeT;
ms.rgb = false;
ms.littleEndian = true;
ms.dimensionOrder = "XYCZT";
ms.seriesMetadata = new Hashtable<String, Object>();
for (Object key : metadataKeys) {
String keyName = key.toString();
if (keyName.startsWith("Series " + i + " ")) {
keyName = keyName.replaceAll("Series " + i + " ", "");
ms.seriesMetadata.put(keyName, tmpSeriesMetadata.get(key));
}
}
}
setSeries(0);
populateMetadataStore();
poi.close();
poi = null;
}
use of loci.formats.CoreMetadata in project bioformats by openmicroscopy.
the class TrestleReader method initStandardMetadata.
// -- Internal BaseTiffReader API methods --
/* @see loci.formats.in.BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
ifds = tiffParser.getIFDs();
for (IFD ifd : ifds) {
tiffParser.fillInIFD(ifd);
}
String comment = ifds.get(0).getComment();
String[] values = comment.split(";");
for (String v : values) {
int eq = v.indexOf('=');
if (eq < 0)
continue;
String key = v.substring(0, eq).trim();
String value = v.substring(eq + 1).trim();
addGlobalMeta(key, value);
if (key.equals("OverlapsXY")) {
String[] overlapValues = value.split(" ");
overlaps = new int[ifds.size() * 2];
for (int i = 0; i < overlapValues.length; i++) {
overlaps[i] = Integer.parseInt(overlapValues[i]);
}
}
}
int seriesCount = ifds.size();
core.clear();
for (int i = 0; i < seriesCount; i++) {
CoreMetadata c = new CoreMetadata();
if (i == 0 && !hasFlattenedResolutions()) {
c.resolutionCount = seriesCount;
}
core.add(c);
}
for (int s = 0; s < core.size(); s++) {
CoreMetadata ms = core.get(s);
IFD ifd = ifds.get(s);
PhotoInterp p = ifd.getPhotometricInterpretation();
int samples = ifd.getSamplesPerPixel();
ms.rgb = samples > 1 || p == PhotoInterp.RGB;
long numTileRows = ifd.getTilesPerColumn() - 1;
long numTileCols = ifd.getTilesPerRow() - 1;
int overlapX = overlaps[s * 2];
int overlapY = overlaps[s * 2 + 1];
ms.sizeX = (int) (ifd.getImageWidth() - (numTileCols * overlapX));
ms.sizeY = (int) (ifd.getImageLength() - (numTileRows * overlapY));
ms.sizeZ = 1;
ms.sizeT = 1;
ms.sizeC = ms.rgb ? samples : 1;
ms.littleEndian = ifd.isLittleEndian();
ms.indexed = p == PhotoInterp.RGB_PALETTE && (get8BitLookupTable() != null || get16BitLookupTable() != null);
ms.imageCount = 1;
ms.pixelType = ifd.getPixelType();
ms.metadataComplete = true;
ms.interleaved = false;
ms.falseColor = false;
ms.dimensionOrder = "XYCZT";
ms.thumbnail = s > 0;
}
// look for all of the other associated metadata files
files = new ArrayList<String>();
Location baseFile = new Location(currentId).getAbsoluteFile();
Location parent = baseFile.getParentFile();
String name = baseFile.getName();
if (name.indexOf('.') >= 0) {
name = name.substring(0, name.indexOf('.') + 1);
}
roiFile = new Location(parent, name + "ROI").getAbsolutePath();
roiDrawFile = new Location(parent, name + "ROI-draw").getAbsolutePath();
String[] list = parent.list(true);
for (String f : list) {
if (!f.equals(baseFile.getName())) {
files.add(new Location(parent, f).getAbsolutePath());
}
}
}
Aggregations