use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class NikonTiffReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
TiffParser tp = new TiffParser(stream);
IFD ifd = tp.getFirstIFD();
if (ifd == null)
return false;
String software = ifd.getIFDTextValue(IFD.SOFTWARE);
return software != null && software.toString().indexOf("EZ-C1") != -1;
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class OperettaReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
if (!checkSuffix(id, "xml")) {
Location parent = new Location(id).getAbsoluteFile().getParentFile();
Location xml = new Location(parent, XML_FILE);
if (!xml.exists()) {
throw new FormatException("Could not find XML file " + xml.getAbsolutePath());
}
initFile(xml.getAbsolutePath());
return;
} else {
super.initFile(id);
}
// parse plate layout and image dimensions from the XML file
String xmlData = DataTools.readFile(id);
OperettaHandler handler = new OperettaHandler();
XMLTools.parseXML(xmlData, handler);
// sort the list of images by well and field indices
ArrayList<Plane> planeList = handler.getPlanes();
ArrayList<Integer> uniqueRows = new ArrayList<Integer>();
ArrayList<Integer> uniqueCols = new ArrayList<Integer>();
ArrayList<Integer> uniqueFields = new ArrayList<Integer>();
ArrayList<Integer> uniqueZs = new ArrayList<Integer>();
ArrayList<Integer> uniqueTs = new ArrayList<Integer>();
ArrayList<Integer> uniqueCs = new ArrayList<Integer>();
for (Plane p : planeList) {
if (!uniqueRows.contains(p.row)) {
uniqueRows.add(p.row);
}
if (!uniqueCols.contains(p.col)) {
uniqueCols.add(p.col);
}
if (!uniqueFields.contains(p.field)) {
uniqueFields.add(p.field);
}
if (!uniqueZs.contains(p.z)) {
uniqueZs.add(p.z);
}
if (!uniqueCs.contains(p.c)) {
uniqueCs.add(p.c);
}
if (!uniqueTs.contains(p.t)) {
uniqueTs.add(p.t);
}
}
Integer[] rows = uniqueRows.toArray(new Integer[uniqueRows.size()]);
Integer[] cols = uniqueCols.toArray(new Integer[uniqueCols.size()]);
Integer[] fields = uniqueFields.toArray(new Integer[uniqueFields.size()]);
Integer[] zs = uniqueZs.toArray(new Integer[uniqueZs.size()]);
Integer[] cs = uniqueCs.toArray(new Integer[uniqueCs.size()]);
Integer[] ts = uniqueTs.toArray(new Integer[uniqueTs.size()]);
Arrays.sort(rows);
Arrays.sort(cols);
Arrays.sort(fields);
Arrays.sort(zs);
Arrays.sort(ts);
Arrays.sort(cs);
int seriesCount = rows.length * cols.length * fields.length;
core.clear();
planes = new Plane[seriesCount][zs.length * cs.length * ts.length];
int nextSeries = 0;
for (int row = 0; row < rows.length; row++) {
for (int col = 0; col < cols.length; col++) {
for (int field = 0; field < fields.length; field++) {
int nextPlane = 0;
for (int t = 0; t < ts.length; t++) {
for (int z = 0; z < zs.length; z++) {
for (int c = 0; c < cs.length; c++) {
for (Plane p : planeList) {
if (p.row == rows[row] && p.col == cols[col] && p.field == fields[field] && p.t == ts[t] && p.z == zs[z] && p.c == cs[c]) {
planes[nextSeries][nextPlane] = p;
break;
}
}
nextPlane++;
}
}
}
nextSeries++;
}
}
}
reader = new MinimalTiffReader();
for (int i = 0; i < seriesCount; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
ms.sizeZ = uniqueZs.size();
ms.sizeC = uniqueCs.size();
ms.sizeT = uniqueTs.size();
ms.dimensionOrder = "XYCZT";
ms.rgb = false;
ms.imageCount = getSizeZ() * getSizeC() * getSizeT();
int planeIndex = 0;
while (planeIndex < planes[i].length && planes[i][planeIndex] == null) {
LOGGER.debug("skipping null plane series = {}, plane = {}", i, planeIndex);
planeIndex++;
}
if (planeIndex >= planes[i].length) {
if (i > 0) {
ms.sizeX = core.get(i - 1).sizeX;
ms.sizeY = core.get(i - 1).sizeY;
ms.pixelType = core.get(i - 1).pixelType;
ms.littleEndian = core.get(i - 1).littleEndian;
} else {
LOGGER.warn("Could not find valid plane for series 0");
}
} else {
ms.sizeX = planes[i][planeIndex].x;
ms.sizeY = planes[i][planeIndex].y;
String filename = planes[i][planeIndex].filename;
while ((filename == null || !new Location(filename).exists()) && planeIndex < planes[i].length - 1) {
LOGGER.debug("Missing TIFF file: {}", filename);
planeIndex++;
filename = planes[i][planeIndex].filename;
}
if (filename != null && new Location(filename).exists()) {
RandomAccessInputStream s = new RandomAccessInputStream(filename, 16);
TiffParser parser = new TiffParser(s);
parser.setDoCaching(false);
IFD firstIFD = parser.getFirstIFD();
ms.littleEndian = firstIFD.isLittleEndian();
ms.pixelType = firstIFD.getPixelType();
s.close();
} else if (i > 0) {
LOGGER.warn("Could not find valid TIFF file for series {}", i);
ms.littleEndian = core.get(0).littleEndian;
ms.pixelType = core.get(0).pixelType;
} else {
LOGGER.warn("Could not find valid TIFF file for series 0; pixel type may be wrong");
}
}
}
addGlobalMeta("Plate name", handler.getPlateName());
addGlobalMeta("Plate description", handler.getPlateDescription());
addGlobalMeta("Plate ID", handler.getPlateIdentifier());
addGlobalMeta("Measurement ID", handler.getMeasurementID());
// populate the MetadataStore
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
String instrument = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrument, 0);
String objective = MetadataTools.createLSID("Objective", 0, 0);
store.setObjectiveID(objective, 0, 0);
if (planes[0][0] != null) {
store.setObjectiveNominalMagnification(planes[0][0].magnification, 0, 0);
store.setObjectiveLensNA(planes[0][0].lensNA, 0, 0);
}
store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
store.setPlateRows(new PositiveInteger(handler.getPlateRows()), 0);
store.setPlateColumns(new PositiveInteger(handler.getPlateColumns()), 0);
String plateAcqID = MetadataTools.createLSID("PlateAcquisition", 0, 0);
store.setPlateAcquisitionID(plateAcqID, 0, 0);
PositiveInteger fieldCount = FormatTools.getMaxFieldCount(fields.length);
if (fieldCount != null) {
store.setPlateAcquisitionMaximumFieldCount(fieldCount, 0, 0);
}
for (int row = 0; row < rows.length; row++) {
for (int col = 0; col < cols.length; col++) {
int well = row * cols.length + col;
store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
store.setWellRow(new NonNegativeInteger(rows[row]), 0, well);
store.setWellColumn(new NonNegativeInteger(cols[col]), 0, well);
for (int field = 0; field < fields.length; field++) {
int imageIndex = well * fields.length + field;
String wellSampleID = MetadataTools.createLSID("WellSample", 0, well, field);
store.setWellSampleID(wellSampleID, 0, well, field);
store.setWellSampleIndex(new NonNegativeInteger(imageIndex), 0, well, field);
String imageID = MetadataTools.createLSID("Image", imageIndex);
store.setImageID(imageID, imageIndex);
store.setWellSampleImageRef(imageID, 0, well, field);
store.setImageInstrumentRef(instrument, imageIndex);
store.setObjectiveSettingsID(objective, imageIndex);
String name = "Well " + (well + 1) + ", Field " + (field + 1);
store.setImageName(name, imageIndex);
store.setPlateAcquisitionWellSampleRef(wellSampleID, 0, 0, imageIndex);
if (planes[imageIndex][0] != null && planes[imageIndex][0].absoluteTime != null) {
store.setImageAcquisitionDate(planes[imageIndex][0].absoluteTime, imageIndex);
}
}
}
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
store.setPlateName(handler.getPlateName(), 0);
store.setPlateDescription(handler.getPlateDescription(), 0);
store.setPlateExternalIdentifier(handler.getPlateIdentifier(), 0);
String experimenterID = MetadataTools.createLSID("Experimenter", 0);
store.setExperimenterID(experimenterID, 0);
store.setExperimenterLastName(handler.getExperimenterName(), 0);
for (int i = 0; i < getSeriesCount(); i++) {
store.setImageExperimenterRef(experimenterID, i);
for (int c = 0; c < getSizeC(); c++) {
if (planes[i][c] != null && planes[i][c].channelName != null) {
store.setChannelName(planes[i][c].channelName, i, c);
}
if (planes[i][c] != null) {
store.setChannelEmissionWavelength(FormatTools.getEmissionWavelength(planes[i][c].emWavelength), i, c);
store.setChannelExcitationWavelength(FormatTools.getExcitationWavelength(planes[i][c].exWavelength), i, c);
}
}
if (planes[i][0] != null) {
store.setPixelsPhysicalSizeX(FormatTools.getPhysicalSizeX(planes[i][0].resolutionX), i);
store.setPixelsPhysicalSizeY(FormatTools.getPhysicalSizeY(planes[i][0].resolutionY), i);
}
for (int p = 0; p < getImageCount(); p++) {
if (planes[i][p] != null) {
store.setPlanePositionX(planes[i][p].positionX, i, p);
store.setPlanePositionY(planes[i][p].positionY, i, p);
store.setPlanePositionZ(planes[i][p].positionZ, i, p);
store.setPlaneExposureTime(planes[i][p].exposureTime, i, p);
store.setPlaneDeltaT(planes[i][p].deltaT, i, p);
}
}
}
}
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class PCIReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
imageFiles = new HashMap<Integer, String>();
timestamps = new HashMap<Integer, Double>();
uniqueZ = new ArrayList<Double>();
CoreMetadata m = core.get(0);
initPOIService();
double scaleFactor = 1;
final List<String> allFiles = poi.getDocumentList();
if (allFiles.isEmpty()) {
throw new FormatException("No files were found - the .cxd may be corrupt.");
}
double firstZ = 0d, secondZ = 0d;
int mode = 0;
for (String name : allFiles) {
int separator = name.lastIndexOf(File.separator);
String parent = name.substring(0, separator);
String relativePath = name.substring(separator + 1);
RandomAccessInputStream stream = null;
if (!(relativePath.startsWith("Bitmap") || (relativePath.equals("Data") && parent.indexOf("Image") != -1))) {
stream = poi.getDocumentStream(name);
stream.order(true);
}
if (stream != null && stream.length() == 8) {
double value = stream.readDouble();
stream.seek(0);
String key = name.replace(File.separatorChar, ' ');
key = key.replaceAll("Root Entry ", "");
key = key.replaceAll("Field Data ", "");
key = key.replaceAll("Details ", "");
addGlobalMeta(key, value);
}
if (relativePath.equals("Field Count")) {
m.imageCount = stream.readInt();
} else if (relativePath.equals("File Has Image")) {
if (stream.readShort() == 0) {
throw new FormatException("This file does not contain image data.");
}
} else if (relativePath.startsWith("Bitmap") || (relativePath.equals("Data") && parent.indexOf("Image") != -1)) {
imageFiles.put(imageFiles.size(), name);
if (getSizeX() != 0 && getSizeY() != 0) {
int bpp = FormatTools.getBytesPerPixel(getPixelType());
int plane = getSizeX() * getSizeY() * bpp;
if (getSizeC() == 0 || getSizeC() * plane > poi.getFileSize(name)) {
m.sizeC = poi.getFileSize(name) / plane;
}
}
} else if (relativePath.indexOf("Image_Depth") != -1) {
boolean firstBits = m.bitsPerPixel == 0;
int bits = (int) stream.readDouble();
m.bitsPerPixel = bits;
while (bits % 8 != 0 || bits == 0) bits++;
if (bits % 3 == 0) {
m.sizeC = 3;
bits /= 3;
m.bitsPerPixel /= 3;
}
bits /= 8;
m.pixelType = FormatTools.pixelTypeFromBytes(bits, false, false);
if (getSizeC() > 1 && firstBits) {
m.sizeC /= bits;
}
} else if (relativePath.indexOf("Image_Height") != -1 && getSizeY() == 0) {
m.sizeY = (int) stream.readDouble();
} else if (relativePath.indexOf("Image_Width") != -1 && getSizeX() == 0) {
m.sizeX = (int) stream.readDouble();
} else if (relativePath.indexOf("Time_From_Start") != -1) {
timestamps.put(getTimestampIndex(parent), stream.readDouble());
} else if (relativePath.indexOf("Position_Z") != -1) {
double zPos = stream.readDouble();
if (!uniqueZ.contains(zPos) && getSizeZ() <= 1) {
uniqueZ.add(zPos);
}
if (name.indexOf("Field 1" + File.separator) != -1)
firstZ = zPos;
else if (name.indexOf("Field 2" + File.separator) != -1)
secondZ = zPos;
} else if (relativePath.equals("First Field Date & Time")) {
long date = (long) stream.readDouble() * 1000;
creationDate = DateTools.convertDate(date, DateTools.COBOL);
} else if (relativePath.equals("GroupMode")) {
mode = stream.readInt();
} else if (relativePath.equals("GroupSelectedFields")) {
m.sizeZ = (int) (stream.length() / 8);
} else if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
if (relativePath.equals("Binning")) {
binning = (int) stream.readDouble();
} else if (relativePath.equals("Comments")) {
String comments = stream.readString((int) stream.length());
String[] lines = comments.split("\n");
for (String line : lines) {
int eq = line.indexOf('=');
if (eq != -1) {
String key = line.substring(0, eq).trim();
String value = line.substring(eq + 1).trim();
addGlobalMeta(key, value);
if (key.equals("factor")) {
if (value.indexOf(';') != -1) {
value = value.substring(0, value.indexOf(';'));
}
scaleFactor = Double.parseDouble(value.trim());
}
}
}
}
}
if (stream != null) {
stream.close();
}
}
boolean zFirst = Math.abs(firstZ - secondZ) > Constants.EPSILON;
if (getSizeC() == 0)
m.sizeC = 1;
if (mode == 0) {
m.sizeZ = 0;
}
if (getSizeZ() <= 1 || (getImageCount() % getSizeZ()) != 0) {
m.sizeZ = uniqueZ.isEmpty() ? 1 : uniqueZ.size();
}
m.sizeT = getImageCount() / getSizeZ();
while (getSizeZ() * getSizeT() < getImageCount()) {
m.sizeZ++;
m.sizeT = getImageCount() / getSizeZ();
}
m.rgb = getSizeC() > 1;
if (imageFiles.size() > getImageCount() && getSizeC() == 1) {
m.sizeC = imageFiles.size() / getImageCount();
m.imageCount *= getSizeC();
} else {
m.imageCount = getSizeZ() * getSizeT();
}
m.interleaved = false;
m.dimensionOrder = zFirst ? "XYCZT" : "XYCTZ";
m.littleEndian = true;
m.indexed = false;
m.falseColor = false;
m.metadataComplete = true;
// re-index image files
String[] files = imageFiles.values().toArray(new String[imageFiles.size()]);
for (String file : files) {
int separator = file.lastIndexOf(File.separator);
String parent = file.substring(0, separator);
imageFiles.put(getImageIndex(parent), file);
}
int bpp = FormatTools.getBytesPerPixel(m.pixelType);
int expectedPlaneSize = m.sizeX * m.sizeY * bpp * m.sizeC;
String file = imageFiles.get(0);
RandomAccessInputStream s = poi.getDocumentStream(file);
TiffParser tp = new TiffParser(s);
// don't correct the image width if it's stored as a TIFF
if (!tp.isValidHeader() && s.length() > expectedPlaneSize) {
m.sizeX += (s.length() - expectedPlaneSize) / (m.sizeY * bpp * m.sizeC);
}
s.close();
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
if (creationDate != null) {
store.setImageAcquisitionDate(new Timestamp(creationDate), 0);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
Length sizeX = FormatTools.getPhysicalSizeX(scaleFactor);
Length sizeY = FormatTools.getPhysicalSizeY(scaleFactor);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, 0);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, 0);
}
for (int i = 0; i < timestamps.size(); i++) {
if (i >= getImageCount()) {
break;
}
Double timestamp = timestamps.get(i);
if (timestamp != null) {
store.setPlaneDeltaT(new Time(timestamp, UNITS.SECOND), 0, i);
}
if (i == 2) {
Double first = timestamps.get(1);
Double increment = timestamp - first;
if (increment != null) {
store.setPixelsTimeIncrement(new Time(increment, UNITS.SECOND), 0);
}
}
}
if (binning > 0) {
String instrumentID = MetadataTools.createLSID("Instrument", 0);
String detectorID = MetadataTools.createLSID("Detector", 0);
store.setInstrumentID(instrumentID, 0);
store.setDetectorID(detectorID, 0, 0);
store.setDetectorType(getDetectorType("Other"), 0, 0);
store.setImageInstrumentRef(instrumentID, 0);
Binning binningEnum = getBinning(binning + "x" + binning);
for (int c = 0; c < getEffectiveSizeC(); c++) {
store.setDetectorSettingsID(detectorID, 0, c);
store.setDetectorSettingsBinning(binningEnum, 0, c);
}
}
}
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class PyramidTiffReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
TiffParser parser = new TiffParser(stream);
parser.setAssumeEqualStrips(equalStrips);
IFD ifd = parser.getFirstIFD();
if (ifd == null)
return false;
String software = ifd.getIFDTextValue(IFD.SOFTWARE);
if (software == null)
return false;
return software.indexOf("Faas") >= 0;
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class LeicaSCNReader method isThisType.
// -- IFormatReader API methods --
/* (non-Javadoc)
* @see loci.formats.FormatReader#isThisType(java.lang.String, boolean)
*/
@Override
public boolean isThisType(String name, boolean open) {
if (super.isThisType(name, open) && open) {
RandomAccessInputStream stream = null;
try {
stream = new RandomAccessInputStream(name);
TiffParser tiffParser = new TiffParser(stream);
if (!tiffParser.isValidHeader()) {
return false;
}
String imageDescription = tiffParser.getComment();
if (imageDescription != null) {
try {
// Test if XML is valid SCN metadata
LeicaSCNHandler handler = new LeicaSCNHandler();
XMLTools.parseXML(imageDescription, handler);
return true;
} catch (Exception se) {
LOGGER.debug("XML parsing failed", se);
}
}
} catch (IOException e) {
LOGGER.debug("I/O exception during isThisType() evaluation.", e);
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
LOGGER.debug("I/O exception during stream closure.", e);
}
}
}
return false;
}
Aggregations