use of loci.formats.tiff.PhotoInterp in project bioformats by openmicroscopy.
the class CellSensReader 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 (!checkSuffix(id, "vsi")) {
Location current = new Location(id).getAbsoluteFile();
Location parent = current.getParentFile();
parent = parent.getParentFile();
Location grandparent = parent.getParentFile();
String vsi = parent.getName();
vsi = vsi.substring(1, vsi.length() - 1) + ".vsi";
Location vsiFile = new Location(grandparent, vsi);
if (!vsiFile.exists()) {
throw new FormatException("Could not find .vsi file.");
} else {
id = vsiFile.getAbsolutePath();
}
}
parser = new TiffParser(id);
ifds = parser.getIFDs();
RandomAccessInputStream vsi = new RandomAccessInputStream(id);
vsi.order(parser.getStream().isLittleEndian());
vsi.seek(8);
readTags(vsi, false, "");
vsi.close();
ArrayList<String> files = new ArrayList<String>();
Location file = new Location(id).getAbsoluteFile();
Location dir = file.getParentFile();
String name = file.getName();
name = name.substring(0, name.lastIndexOf("."));
Location pixelsDir = new Location(dir, "_" + name + "_");
String[] stackDirs = pixelsDir.list(true);
if (stackDirs != null) {
Arrays.sort(stackDirs);
for (String f : stackDirs) {
Location stackDir = new Location(pixelsDir, f);
String[] pixelsFiles = stackDir.list(true);
if (pixelsFiles != null) {
Arrays.sort(pixelsFiles);
for (String pixelsFile : pixelsFiles) {
if (checkSuffix(pixelsFile, "ets")) {
files.add(new Location(stackDir, pixelsFile).getAbsolutePath());
}
}
}
}
}
files.add(file.getAbsolutePath());
usedFiles = files.toArray(new String[files.size()]);
if (expectETS && files.size() == 1) {
String message = "Missing expected .ets files in " + pixelsDir.getAbsolutePath();
if (failOnMissingETS()) {
throw new FormatException(message);
} else {
LOGGER.warn(message);
}
} else if (!expectETS && files.size() > 1) {
LOGGER.warn(".ets files present but not expected");
}
int seriesCount = files.size();
core.clear();
IFDList exifs = parser.getExifIFDs();
int index = 0;
for (int s = 0; s < seriesCount; s++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
if (s < files.size() - 1) {
setCoreIndex(index);
parseETSFile(files.get(s), s);
ms.littleEndian = compressionType.get(index) == RAW;
ms.interleaved = ms.rgb;
for (int q = 1; q < ms.resolutionCount; q++) {
int res = core.size() - ms.resolutionCount + q;
core.get(res).littleEndian = ms.littleEndian;
core.get(res).interleaved = ms.interleaved;
}
if (s == 0 && exifs.size() > 0) {
IFD exif = exifs.get(0);
int newX = exif.getIFDIntValue(IFD.PIXEL_X_DIMENSION);
int newY = exif.getIFDIntValue(IFD.PIXEL_Y_DIMENSION);
if (getSizeX() > newX || getSizeY() > newY) {
ms.sizeX = newX;
ms.sizeY = newY;
}
}
index += ms.resolutionCount;
if (s < pyramids.size()) {
ms.seriesMetadata = pyramids.get(s).originalMetadata;
}
setCoreIndex(0);
} else {
IFD ifd = ifds.get(s - files.size() + 1);
PhotoInterp p = ifd.getPhotometricInterpretation();
int samples = ifd.getSamplesPerPixel();
ms.rgb = samples > 1 || p == PhotoInterp.RGB;
ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
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.interleaved = false;
ms.falseColor = false;
ms.thumbnail = s != 0;
index++;
}
ms.metadataComplete = true;
ms.dimensionOrder = "XYCZT";
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
String instrument = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrument, 0);
for (int i = 0; i < pyramids.size(); i++) {
Pyramid pyramid = pyramids.get(i);
store.setObjectiveID(MetadataTools.createLSID("Objective", 0, i), 0, i);
store.setObjectiveNominalMagnification(pyramid.magnification, 0, i);
store.setObjectiveWorkingDistance(FormatTools.createLength(pyramid.workingDistance, UNITS.MICROMETER), 0, i);
for (int q = 0; q < pyramid.objectiveTypes.size(); q++) {
if (pyramid.objectiveTypes.get(q) == 1) {
store.setObjectiveModel(pyramid.objectiveNames.get(q), 0, i);
break;
}
}
store.setObjectiveLensNA(pyramid.numericalAperture, 0, i);
store.setDetectorID(MetadataTools.createLSID("Detector", 0, i), 0, i);
store.setDetectorOffset(pyramid.offset, 0, i);
store.setDetectorGain(pyramid.gain, 0, i);
for (int q = 0; q < pyramid.deviceTypes.size(); q++) {
if (pyramid.deviceTypes.get(q).equals("Camera")) {
if (q < pyramid.deviceNames.size()) {
store.setDetectorModel(pyramid.deviceNames.get(q), 0, i);
}
if (q < pyramid.deviceIDs.size()) {
store.setDetectorSerialNumber(pyramid.deviceIDs.get(q), 0, i);
}
if (q < pyramid.deviceManufacturers.size()) {
store.setDetectorManufacturer(pyramid.deviceManufacturers.get(q), 0, i);
}
store.setDetectorType(getDetectorType("CCD"), 0, i);
break;
}
}
}
int nextPyramid = 0;
for (int i = 0; i < core.size(); ) {
setCoreIndex(i);
Pyramid pyramid = null;
if (nextPyramid < pyramids.size()) {
pyramid = pyramids.get(nextPyramid++);
}
int ii = coreIndexToSeries(i);
if (pyramid != null) {
int nextPlane = 0;
int effectiveSizeC = core.get(i).rgb ? 1 : core.get(i).sizeC;
for (int c = 0; c < effectiveSizeC; c++) {
store.setDetectorSettingsID(MetadataTools.createLSID("Detector", 0, nextPyramid - 1), ii, c);
store.setDetectorSettingsBinning(getBinning(pyramid.binningX + "x" + pyramid.binningY), ii, c);
if (c == 0) {
store.setDetectorSettingsGain(pyramid.redGain, ii, c);
store.setDetectorSettingsOffset(pyramid.redOffset, ii, c);
} else if (c == 1) {
store.setDetectorSettingsGain(pyramid.greenGain, ii, c);
store.setDetectorSettingsOffset(pyramid.greenOffset, ii, c);
} else if (c == 2) {
store.setDetectorSettingsGain(pyramid.blueGain, ii, c);
store.setDetectorSettingsOffset(pyramid.blueOffset, ii, c);
}
if (c < pyramid.channelNames.size()) {
store.setChannelName(pyramid.channelNames.get(c), ii, c);
}
if (c < pyramid.channelWavelengths.size()) {
int wave = pyramid.channelWavelengths.get(c).intValue();
if (wave > 0) {
store.setChannelEmissionWavelength(FormatTools.getEmissionWavelength((double) wave), ii, c);
}
}
for (int z = 0; z < core.get(i).sizeZ; z++) {
for (int t = 0; t < core.get(i).sizeT; t++) {
nextPlane = getIndex(z, c, t);
Long exp = pyramid.defaultExposureTime;
if (c < pyramid.exposureTimes.size()) {
exp = pyramid.exposureTimes.get(c);
}
if (exp != null) {
store.setPlaneExposureTime(FormatTools.createTime(exp / 1000000.0, UNITS.SECOND), ii, nextPlane);
}
store.setPlanePositionX(FormatTools.createLength(pyramid.originX, UNITS.MICROMETER), ii, nextPlane);
store.setPlanePositionY(FormatTools.createLength(pyramid.originY, UNITS.MICROMETER), ii, nextPlane);
}
}
}
}
store.setImageInstrumentRef(instrument, ii);
if (pyramid != null) {
String imageName = pyramid.name;
boolean duplicate = false;
for (int q = 0; q < pyramids.size(); q++) {
if (q != (nextPyramid - 1) && imageName.equals(pyramids.get(q).name)) {
duplicate = true;
break;
}
}
if (!imageName.equals("Overview") && !imageName.equals("Label") && duplicate) {
imageName += " #" + ii;
}
if (imageName.equals("Overview") || imageName.equals("Label")) {
imageName = imageName.toLowerCase();
}
store.setImageName(imageName, ii);
store.setObjectiveSettingsID(MetadataTools.createLSID("Objective", 0, nextPyramid - 1), ii);
store.setObjectiveSettingsRefractiveIndex(pyramid.refractiveIndex, ii);
if (pyramid.physicalSizeX > 0) {
store.setPixelsPhysicalSizeX(FormatTools.getPhysicalSizeX(pyramid.physicalSizeX), ii);
}
if (pyramid.physicalSizeY > 0) {
store.setPixelsPhysicalSizeY(FormatTools.getPhysicalSizeY(pyramid.physicalSizeY), ii);
}
if (pyramid.acquisitionTime != null) {
// acquisition time is stored in seconds
store.setImageAcquisitionDate(new Timestamp(DateTools.convertDate(pyramid.acquisitionTime * 1000, DateTools.UNIX)), ii);
}
} else {
store.setImageName("macro image", ii);
}
i += core.get(i).resolutionCount;
}
setCoreIndex(0);
}
use of loci.formats.tiff.PhotoInterp in project bioformats by openmicroscopy.
the class APLReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
LOGGER.debug("Initializing {}", id);
// find the corresponding .mtb file
if (!checkSuffix(id, "mtb")) {
if (checkSuffix(id, METADATA_SUFFIXES)) {
int separator = id.lastIndexOf(File.separator);
if (separator < 0)
separator = 0;
int underscore = id.lastIndexOf("_");
if (underscore < separator || checkSuffix(id, "apl")) {
underscore = id.lastIndexOf(".");
}
String mtbFile = id.substring(0, underscore) + "_d.mtb";
if (!new Location(mtbFile).exists()) {
throw new FormatException(".mtb file not found");
}
currentId = new Location(mtbFile).getAbsolutePath();
} else {
Location parent = new Location(id).getAbsoluteFile().getParentFile();
parent = parent.getParentFile();
String[] list = parent.list(true);
for (String f : list) {
if (checkSuffix(f, "mtb")) {
currentId = new Location(parent, f).getAbsolutePath();
break;
}
}
if (!checkSuffix(currentId, "mtb")) {
throw new FormatException(".mtb file not found");
}
}
}
String mtb = new Location(currentId).getAbsolutePath();
LOGGER.debug("Reading .mtb file '{}'", mtb);
MDBService mdb = null;
try {
ServiceFactory factory = new ServiceFactory();
mdb = factory.getInstance(MDBService.class);
} catch (DependencyException de) {
throw new FormatException("MDB Tools Java library not found", de);
}
String[] columnNames = null;
List<String[]> rows = null;
try {
mdb.initialize(mtb);
rows = mdb.parseDatabase().get(0);
columnNames = rows.get(0);
String[] tmpNames = columnNames;
columnNames = new String[tmpNames.length - 1];
System.arraycopy(tmpNames, 1, columnNames, 0, columnNames.length);
} finally {
mdb.close();
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
for (int i = 1; i < rows.size(); i++) {
String[] row = rows.get(i);
for (int q = 0; q < row.length; q++) {
addGlobalMetaList(columnNames[q], row[q]);
}
}
}
used = new ArrayList<String>();
used.add(mtb);
String tnb = mtb.substring(0, mtb.lastIndexOf("."));
if (tnb.lastIndexOf("_") > tnb.lastIndexOf(File.separator)) {
tnb = tnb.substring(0, tnb.lastIndexOf("_"));
}
used.add(tnb + "_1.tnb");
used.add(tnb + ".apl");
String idPath = new Location(id).getAbsolutePath();
if (!used.contains(idPath) && checkSuffix(idPath, METADATA_SUFFIXES)) {
used.add(idPath);
}
// calculate indexes to relevant metadata
int calibrationUnit = DataTools.indexOf(columnNames, "Calibration Unit");
int colorChannels = DataTools.indexOf(columnNames, "Color Channels");
int frames = DataTools.indexOf(columnNames, "Frames");
int calibratedHeight = DataTools.indexOf(columnNames, "Height");
int calibratedWidth = DataTools.indexOf(columnNames, "Width");
int path = DataTools.indexOf(columnNames, "Image Path");
if (path == -1) {
path = DataTools.indexOf(columnNames, "Path");
}
int filename = DataTools.indexOf(columnNames, "File Name");
int magnification = DataTools.indexOf(columnNames, "Magnification");
int width = DataTools.indexOf(columnNames, "X-Resolution");
int height = DataTools.indexOf(columnNames, "Y-Resolution");
int imageType = DataTools.indexOf(columnNames, "Image Type");
int imageName = DataTools.indexOf(columnNames, "Image Name");
int zLayers = DataTools.indexOf(columnNames, "Z-Layers");
String parentDirectory = mtb.substring(0, mtb.lastIndexOf(File.separator));
// look for the directory that contains TIFF and XML files
LOGGER.debug("Searching {} for a directory with TIFFs", parentDirectory);
Location dir = new Location(parentDirectory);
String topDirectory = null;
int index = 2;
String pathName = rows.get(index++)[path].trim();
while (pathName.equals("") && index < rows.size()) {
pathName = rows.get(index++)[path].trim();
}
pathName = pathName.replace('\\', File.separatorChar);
pathName = pathName.replaceAll("/", File.separator);
String[] dirs = pathName.split(File.separatorChar == '\\' ? "\\\\" : File.separator);
for (int i = dirs.length - 1; i >= 0; i--) {
if (dirs[i].indexOf("_DocumentFiles") > 0) {
Location file = new Location(dir, dirs[i]);
if (file.exists()) {
topDirectory = file.getAbsolutePath();
break;
}
}
}
if (topDirectory == null) {
String[] list = dir.list();
for (String f : list) {
LOGGER.debug(" '{}'", f);
Location file = new Location(dir, f);
if (file.isDirectory() && f.indexOf("_DocumentFiles") > 0) {
topDirectory = file.getAbsolutePath();
LOGGER.debug("Found {}", topDirectory);
break;
}
}
}
if (topDirectory == null) {
throw new FormatException("Could not find a directory with TIFF files.");
}
final List<Integer> seriesIndexes = new ArrayList<Integer>();
for (int i = 1; i < rows.size(); i++) {
String file = parseFilename(rows.get(i), filename, path);
if (file.equals(""))
continue;
file = topDirectory + File.separator + file;
if (new Location(file).exists() && checkSuffix(file, "tif")) {
seriesIndexes.add(i);
}
}
int seriesCount = seriesIndexes.size();
core.clear();
tiffFiles = new String[seriesCount];
xmlFiles = new String[seriesCount];
parser = new TiffParser[seriesCount];
ifds = new IFDList[seriesCount];
for (int i = 0; i < seriesCount; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
int secondRow = seriesIndexes.get(i);
int firstRow = secondRow - 1;
String[] row2 = rows.get(firstRow);
String[] row3 = rows.get(secondRow);
if (frames > -1) {
ms.sizeT = parseDimension(row3[frames]);
}
if (zLayers > -1) {
ms.sizeZ = parseDimension(row3[zLayers]);
}
if (colorChannels > -1) {
ms.sizeC = parseDimension(row3[colorChannels]);
} else if (imageType > -1) {
if (row3[imageType] != null && row3[imageType].equals("RGB")) {
ms.sizeC = 3;
}
}
ms.dimensionOrder = "XYCZT";
if (ms.sizeZ == 0)
ms.sizeZ = 1;
if (ms.sizeC == 0)
ms.sizeC = 1;
if (ms.sizeT == 0)
ms.sizeT = 1;
xmlFiles[i] = topDirectory + File.separator + parseFilename(row2, filename, path);
tiffFiles[i] = topDirectory + File.separator + parseFilename(row3, filename, path);
parser[i] = new TiffParser(tiffFiles[i]);
parser[i].setDoCaching(false);
ifds[i] = parser[i].getIFDs();
for (IFD ifd : ifds[i]) {
parser[i].fillInIFD(ifd);
}
// get core metadata from TIFF file
IFD ifd = ifds[i].get(0);
PhotoInterp photo = ifd.getPhotometricInterpretation();
int samples = ifd.getSamplesPerPixel();
ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
ms.rgb = samples > 1 || photo == PhotoInterp.RGB;
ms.pixelType = ifd.getPixelType();
ms.littleEndian = ifd.isLittleEndian();
ms.indexed = photo == PhotoInterp.RGB_PALETTE && ifd.containsKey(IFD.COLOR_MAP);
ms.imageCount = ifds[i].size();
if (ms.sizeZ * ms.sizeT * (ms.rgb ? 1 : ms.sizeC) != ms.imageCount) {
ms.sizeT = ms.imageCount / (ms.rgb ? 1 : ms.sizeC);
ms.sizeZ = 1;
}
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
for (int i = 0; i < seriesCount; i++) {
String[] row = rows.get(seriesIndexes.get(i));
// populate Image data
MetadataTools.setDefaultCreationDate(store, mtb, i);
store.setImageName(row[imageName].trim(), i);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
// populate Dimensions data
// calculate physical X and Y sizes
double realWidth = Double.parseDouble(row[calibratedWidth]);
double realHeight = Double.parseDouble(row[calibratedHeight]);
String units = row[calibrationUnit];
CoreMetadata ms = core.get(i);
double px = realWidth / ms.sizeX;
double py = realHeight / ms.sizeY;
Length physicalSizeX = FormatTools.getPhysicalSizeX(px, units);
Length physicalSizeY = FormatTools.getPhysicalSizeY(py, units);
if (physicalSizeX != null) {
store.setPixelsPhysicalSizeX(physicalSizeX, i);
}
if (physicalSizeY != null) {
store.setPixelsPhysicalSizeY(physicalSizeY, i);
}
}
}
setSeries(0);
}
use of loci.formats.tiff.PhotoInterp in project bioformats by openmicroscopy.
the class ZeissLSMReader method initMetadata.
protected void initMetadata(int series) throws FormatException, IOException {
setSeries(series);
IFDList ifds = ifdsList.get(series);
IFD ifd = ifds.get(0);
in.close();
in = new RandomAccessInputStream(getLSMFileFromSeries(series), 16);
in.order(isLittleEndian());
tiffParser = new TiffParser(in);
PhotoInterp photo = ifd.getPhotometricInterpretation();
int samples = ifd.getSamplesPerPixel();
CoreMetadata ms = core.get(series);
ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
ms.rgb = samples > 1 || photo == PhotoInterp.RGB;
ms.interleaved = false;
ms.sizeC = isRGB() ? samples : 1;
ms.pixelType = ifd.getPixelType();
ms.imageCount = ifds.size();
ms.sizeZ = getImageCount();
ms.sizeT = 1;
LOGGER.info("Reading LSM metadata for series #{}", series);
MetadataStore store = makeFilterMetadata();
int instrument = getEffectiveSeries(series);
String imageName = getLSMFileFromSeries(series);
if (imageName.indexOf('.') != -1) {
imageName = imageName.substring(0, imageName.lastIndexOf("."));
}
if (imageName.indexOf(File.separator) != -1) {
imageName = imageName.substring(imageName.lastIndexOf(File.separator) + 1);
}
if (lsmFilenames.length != getSeriesCount()) {
imageName += " #" + (getPosition(series) + 1);
}
// link Instrument and Image
store.setImageID(MetadataTools.createLSID("Image", series), series);
String instrumentID = MetadataTools.createLSID("Instrument", instrument);
store.setInstrumentID(instrumentID, instrument);
store.setImageInstrumentRef(instrumentID, series);
RandomAccessInputStream ras = getCZTag(ifd);
if (ras == null) {
imageNames.add(imageName);
return;
}
ras.seek(16);
ms.sizeZ = ras.readInt();
ras.skipBytes(4);
ms.sizeT = ras.readInt();
int dataType = ras.readInt();
switch(dataType) {
case 2:
addSeriesMeta("DataType", "12 bit unsigned integer");
break;
case 5:
addSeriesMeta("DataType", "32 bit float");
break;
case 0:
addSeriesMeta("DataType", "varying data types");
break;
default:
addSeriesMeta("DataType", "8 bit unsigned integer");
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
ras.seek(0);
addSeriesMeta("MagicNumber ", ras.readInt());
addSeriesMeta("StructureSize", ras.readInt());
addSeriesMeta("DimensionX", ras.readInt());
addSeriesMeta("DimensionY", ras.readInt());
ras.seek(32);
addSeriesMeta("ThumbnailX", ras.readInt());
addSeriesMeta("ThumbnailY", ras.readInt());
// pixel sizes are stored in meters, we need them in microns
pixelSizeX = ras.readDouble() * 1000000;
pixelSizeY = ras.readDouble() * 1000000;
pixelSizeZ = ras.readDouble() * 1000000;
addSeriesMeta("VoxelSizeX", pixelSizeX);
addSeriesMeta("VoxelSizeY", pixelSizeY);
addSeriesMeta("VoxelSizeZ", pixelSizeZ);
originX = ras.readDouble() * 1000000;
originY = ras.readDouble() * 1000000;
originZ = ras.readDouble() * 1000000;
addSeriesMeta("OriginX", originX);
addSeriesMeta("OriginY", originY);
addSeriesMeta("OriginZ", originZ);
} else
ras.seek(88);
int scanType = ras.readShort();
switch(scanType) {
case 0:
addSeriesMeta("ScanType", "x-y-z scan");
ms.dimensionOrder = "XYZCT";
break;
case 1:
addSeriesMeta("ScanType", "z scan (x-z plane)");
ms.dimensionOrder = "XYZCT";
break;
case 2:
addSeriesMeta("ScanType", "line scan");
ms.dimensionOrder = "XYZCT";
break;
case 3:
addSeriesMeta("ScanType", "time series x-y");
ms.dimensionOrder = "XYTCZ";
break;
case 4:
addSeriesMeta("ScanType", "time series x-z");
ms.dimensionOrder = "XYZTC";
break;
case 5:
addSeriesMeta("ScanType", "time series 'Mean of ROIs'");
ms.dimensionOrder = "XYTCZ";
break;
case 6:
addSeriesMeta("ScanType", "time series x-y-z");
ms.dimensionOrder = "XYZTC";
break;
case 7:
addSeriesMeta("ScanType", "spline scan");
ms.dimensionOrder = "XYCTZ";
break;
case 8:
addSeriesMeta("ScanType", "spline scan x-z");
ms.dimensionOrder = "XYCZT";
break;
case 9:
addSeriesMeta("ScanType", "time series spline plane x-z");
ms.dimensionOrder = "XYTCZ";
break;
case 10:
addSeriesMeta("ScanType", "point mode");
ms.dimensionOrder = "XYZCT";
break;
default:
addSeriesMeta("ScanType", "x-y-z scan");
ms.dimensionOrder = "XYZCT";
}
ms.indexed = lut != null && lut[series] != null;
if (isIndexed()) {
ms.rgb = false;
}
if (getSizeC() == 0)
ms.sizeC = 1;
if (isRGB()) {
// shuffle C to front of order string
ms.dimensionOrder = getDimensionOrder().replaceAll("C", "");
ms.dimensionOrder = getDimensionOrder().replaceAll("XY", "XYC");
}
if (getEffectiveSizeC() == 0) {
ms.imageCount = getSizeZ() * getSizeT();
} else {
ms.imageCount = getSizeZ() * getSizeT() * getEffectiveSizeC();
}
if (getImageCount() != ifds.size()) {
int diff = getImageCount() - ifds.size();
ms.imageCount = ifds.size();
if (diff % getSizeZ() == 0) {
ms.sizeT -= (diff / getSizeZ());
} else if (diff % getSizeT() == 0) {
ms.sizeZ -= (diff / getSizeT());
} else if (getSizeZ() > 1) {
ms.sizeZ = ifds.size();
ms.sizeT = 1;
} else if (getSizeT() > 1) {
ms.sizeT = ifds.size();
ms.sizeZ = 1;
}
}
if (getSizeZ() == 0)
ms.sizeZ = getImageCount();
if (getSizeT() == 0)
ms.sizeT = getImageCount() / getSizeZ();
long channelColorsOffset = 0;
long timeStampOffset = 0;
long eventListOffset = 0;
long scanInformationOffset = 0;
long channelWavelengthOffset = 0;
long applicationTagOffset = 0;
channelColor = new Color[getSizeC()];
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
int spectralScan = ras.readShort();
if (spectralScan != 1) {
addSeriesMeta("SpectralScan", "no spectral scan");
} else
addSeriesMeta("SpectralScan", "acquired with spectral scan");
int type = ras.readInt();
switch(type) {
case 1:
addSeriesMeta("DataType2", "calculated data");
break;
case 2:
addSeriesMeta("DataType2", "animation");
break;
default:
addSeriesMeta("DataType2", "original scan data");
}
long[] overlayOffsets = new long[9];
String[] overlayKeys = new String[] { "VectorOverlay", "InputLut", "OutputLut", "ROI", "BleachROI", "MeanOfRoisOverlay", "TopoIsolineOverlay", "TopoProfileOverlay", "LinescanOverlay" };
overlayOffsets[0] = ras.readInt();
overlayOffsets[1] = ras.readInt();
overlayOffsets[2] = ras.readInt();
channelColorsOffset = ras.readInt();
addSeriesMeta("TimeInterval", ras.readDouble());
ras.skipBytes(4);
scanInformationOffset = ras.readInt();
applicationTagOffset = ras.readInt();
timeStampOffset = ras.readInt();
eventListOffset = ras.readInt();
overlayOffsets[3] = ras.readInt();
overlayOffsets[4] = ras.readInt();
ras.skipBytes(4);
addSeriesMeta("DisplayAspectX", ras.readDouble());
addSeriesMeta("DisplayAspectY", ras.readDouble());
addSeriesMeta("DisplayAspectZ", ras.readDouble());
addSeriesMeta("DisplayAspectTime", ras.readDouble());
overlayOffsets[5] = ras.readInt();
overlayOffsets[6] = ras.readInt();
overlayOffsets[7] = ras.readInt();
overlayOffsets[8] = ras.readInt();
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.NO_OVERLAYS) {
for (int i = 0; i < overlayOffsets.length; i++) {
parseOverlays(series, overlayOffsets[i], overlayKeys[i], store);
}
}
addSeriesMeta("ToolbarFlags", ras.readInt());
channelWavelengthOffset = ras.readInt();
ras.skipBytes(64);
} else
ras.skipBytes(182);
if (getSizeC() > 1) {
if (!splitPlanes)
splitPlanes = isRGB();
ms.rgb = false;
if (splitPlanes)
ms.imageCount *= getSizeC();
}
// NB: the Zeiss LSM 5.5 specification indicates that there should be
// 15 32-bit integers here; however, there are actually 16 32-bit
// integers before the tile position offset.
// We have confirmed with Zeiss that this is correct, and the 6.0
// specification was updated to contain the correct information.
// rotations and phases may be reversed
// we only have examples where both values are equal
rotations = ras.readInt();
phases = ras.readInt();
illuminations = ras.readInt();
if (rotations > 1) {
ms.moduloZ.step = ms.sizeZ;
ms.moduloZ.end = ms.sizeZ * (rotations - 1);
ms.moduloZ.type = FormatTools.ROTATION;
ms.sizeZ *= rotations;
}
if (illuminations > 1) {
ms.moduloC.step = ms.sizeC;
ms.moduloC.end = ms.sizeC * (illuminations - 1);
ms.moduloC.type = FormatTools.ILLUMINATION;
ms.moduloC.parentType = FormatTools.CHANNEL;
ms.sizeC *= illuminations;
}
if (phases > 1) {
ms.moduloT.step = ms.sizeT;
ms.moduloT.end = ms.sizeT * (phases - 1);
ms.moduloT.type = FormatTools.PHASE;
ms.sizeT *= phases;
}
for (int c = 0; c < getEffectiveSizeC(); c++) {
String lsid = MetadataTools.createLSID("Channel", series, c);
store.setChannelID(lsid, series, c);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
ras.skipBytes(52);
int tilePositionOffset = ras.readInt();
ras.skipBytes(36);
int positionOffset = ras.readInt();
// read referenced structures
addSeriesMeta("DimensionZ", getSizeZ());
addSeriesMeta("DimensionChannels", getSizeC());
addSeriesMeta("DimensionM", dimensionM);
addSeriesMeta("DimensionP", dimensionP);
if (lsmFilenames.length == 1) {
xCoordinates.clear();
yCoordinates.clear();
zCoordinates.clear();
}
if (positionOffset != 0) {
in.seek(positionOffset);
int nPositions = in.readInt();
for (int i = 0; i < nPositions; i++) {
double xPos = originX + in.readDouble() * 1000000;
double yPos = originY + in.readDouble() * 1000000;
double zPos = originZ + in.readDouble() * 1000000;
xCoordinates.add(xPos);
yCoordinates.add(yPos);
zCoordinates.add(zPos);
addGlobalMetaList("X position for position", xPos);
addGlobalMetaList("Y position for position", yPos);
addGlobalMetaList("Z position for position", zPos);
}
}
if (tilePositionOffset != 0) {
in.seek(tilePositionOffset);
int nTiles = in.readInt();
for (int i = 0; i < nTiles; i++) {
double xPos = originX + in.readDouble() * 1000000;
double yPos = originY + in.readDouble() * 1000000;
double zPos = originZ + in.readDouble() * 1000000;
if (xCoordinates.size() > i) {
xPos += xCoordinates.get(i);
xCoordinates.set(i, xPos);
} else if (xCoordinates.size() == i) {
xCoordinates.add(xPos);
}
if (yCoordinates.size() > i) {
yPos += yCoordinates.get(i);
yCoordinates.set(i, yPos);
} else if (yCoordinates.size() == i) {
yCoordinates.add(yPos);
}
if (zCoordinates.size() > i) {
zPos += zCoordinates.get(i);
zCoordinates.set(i, zPos);
} else if (zCoordinates.size() == i) {
zCoordinates.add(zPos);
}
addGlobalMetaList("X position for position", xPos);
addGlobalMetaList("Y position for position", yPos);
addGlobalMetaList("Z position for position", zPos);
}
}
if (channelColorsOffset != 0) {
in.seek(channelColorsOffset + 12);
int colorsOffset = in.readInt();
int namesOffset = in.readInt();
if (colorsOffset > 0) {
in.seek(channelColorsOffset + colorsOffset);
lut[getSeries()] = new byte[getSizeC() * 3][256];
core.get(getSeries()).indexed = true;
for (int i = 0; i < getSizeC(); i++) {
if (i >= channelColor.length) {
continue;
}
int color = in.readInt();
int red = color & 0xff;
int green = (color & 0xff00) >> 8;
int blue = (color & 0xff0000) >> 16;
// otherwise set the color to white, as this will display better
if (red == 0 && green == 0 & blue == 0) {
if (i > 0) {
red = channelColor[i - 1].getRed();
green = channelColor[i - 1].getGreen();
blue = channelColor[i - 1].getBlue();
} else {
red = 255;
green = 255;
blue = 255;
}
}
channelColor[i] = new Color(red, green, blue, 255);
for (int j = 0; j < 256; j++) {
lut[getSeries()][i * 3][j] = (byte) ((red / 255.0) * j);
lut[getSeries()][i * 3 + 1][j] = (byte) ((green / 255.0) * j);
lut[getSeries()][i * 3 + 2][j] = (byte) ((blue / 255.0) * j);
}
}
}
if (namesOffset > 0) {
in.seek(channelColorsOffset + namesOffset);
channelNames[series] = new String[getSizeC()];
for (int i = 0; i < getSizeC(); i++) {
if (in.getFilePointer() >= in.length() - 1)
break;
// we want to read until we find a null char
int length = in.readInt();
String name = in.readString(length);
while ((name.length() > 0) && (name.codePointAt(name.length() - 1) == 0)) {
name = name.substring(0, name.length() - 1);
}
if (name.length() <= 128) {
addSeriesMetaList("ChannelName", name);
}
channelNames[series][i] = name;
}
}
}
if (timeStampOffset != 0) {
in.seek(timeStampOffset + 4);
int nStamps = in.readInt();
for (int i = 0; i < nStamps; i++) {
double stamp = in.readDouble();
addSeriesMetaList("TimeStamp", stamp);
timestamps.add(stamp);
}
}
if (eventListOffset != 0) {
in.seek(eventListOffset + 4);
int numEvents = in.readInt();
in.seek(in.getFilePointer() - 4);
in.order(!in.isLittleEndian());
int tmpEvents = in.readInt();
if (numEvents < 0)
numEvents = tmpEvents;
else
numEvents = (int) Math.min(numEvents, tmpEvents);
in.order(!in.isLittleEndian());
if (numEvents > 65535)
numEvents = 0;
for (int i = 0; i < numEvents; i++) {
if (in.getFilePointer() + 16 <= in.length()) {
int size = in.readInt();
double eventTime = in.readDouble();
int eventType = in.readInt();
addSeriesMetaList("Event Time", eventTime);
addSeriesMetaList("Event Type", eventType);
long fp = in.getFilePointer();
int len = size - 16;
if (len > 65536)
len = 65536;
if (len < 0)
len = 0;
addSeriesMetaList("Event Description", in.readString(len));
in.seek(fp + size - 16);
if (in.getFilePointer() < 0)
break;
}
}
}
if (scanInformationOffset != 0) {
in.seek(scanInformationOffset);
nextLaser = nextDetector = 0;
nextFilter = nextDichroicChannel = nextDichroic = 0;
nextDetectChannel = nextIllumChannel = 0;
final List<SubBlock> blocks = new ArrayList<SubBlock>();
while (in.getFilePointer() < in.length() - 12) {
if (in.getFilePointer() < 0)
break;
int entry = in.readInt();
int blockType = in.readInt();
int dataSize = in.readInt();
if (blockType == TYPE_SUBBLOCK) {
SubBlock block = null;
switch(entry) {
case SUBBLOCK_RECORDING:
block = new Recording();
break;
case SUBBLOCK_LASER:
block = new Laser();
break;
case SUBBLOCK_TRACK:
block = new Track();
break;
case SUBBLOCK_DETECTION_CHANNEL:
block = new DetectionChannel();
break;
case SUBBLOCK_ILLUMINATION_CHANNEL:
block = new IlluminationChannel();
break;
case SUBBLOCK_BEAM_SPLITTER:
block = new BeamSplitter();
break;
case SUBBLOCK_DATA_CHANNEL:
block = new DataChannel();
break;
case SUBBLOCK_TIMER:
block = new Timer();
break;
case SUBBLOCK_MARKER:
block = new Marker();
break;
}
if (block != null) {
blocks.add(block);
}
} else if (dataSize + in.getFilePointer() <= in.length() && dataSize > 0) {
in.skipBytes(dataSize);
} else
break;
}
final List<SubBlock> nonAcquiredBlocks = new ArrayList<SubBlock>();
SubBlock[] metadataBlocks = blocks.toArray(new SubBlock[0]);
for (SubBlock block : metadataBlocks) {
block.addToHashtable();
if (!block.acquire) {
nonAcquiredBlocks.add(block);
blocks.remove(block);
}
}
for (int i = 0; i < blocks.size(); i++) {
SubBlock block = blocks.get(i);
// a valid DataChannel or IlluminationChannel
if ((block instanceof IlluminationChannel) && i < blocks.size() - 1) {
SubBlock nextBlock = blocks.get(i + 1);
if (!(nextBlock instanceof DataChannel) && !(nextBlock instanceof IlluminationChannel)) {
((IlluminationChannel) block).wavelength = null;
}
} else // a valid Track or DetectionChannel
if ((block instanceof DetectionChannel) && i > 0) {
SubBlock prevBlock = blocks.get(i - 1);
if (!(prevBlock instanceof Track) && !(prevBlock instanceof DetectionChannel)) {
block.acquire = false;
nonAcquiredBlocks.add(block);
}
}
if (block.acquire)
populateMetadataStore(block, store, series);
}
for (SubBlock block : nonAcquiredBlocks) {
populateMetadataStore(block, store, series);
}
}
if (applicationTagOffset != 0) {
in.seek(applicationTagOffset);
parseApplicationTags();
}
}
imageNames.add(imageName);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
if (userName != null) {
String experimenterID = MetadataTools.createLSID("Experimenter", 0);
store.setExperimenterID(experimenterID, 0);
store.setExperimenterUserName(userName, 0);
}
Length pixX = FormatTools.getPhysicalSizeX(pixelSizeX);
Length pixY = FormatTools.getPhysicalSizeY(pixelSizeY);
Length pixZ = FormatTools.getPhysicalSizeZ(pixelSizeZ);
if (pixX != null) {
store.setPixelsPhysicalSizeX(pixX, series);
}
if (pixY != null) {
store.setPixelsPhysicalSizeY(pixY, series);
}
if (pixZ != null) {
store.setPixelsPhysicalSizeZ(pixZ, series);
}
for (int i = 0; i < getSizeC(); i++) {
store.setChannelColor(channelColor[i], series, i);
if (channelNames[series] != null) {
store.setChannelName(channelNames[series][i], series, i);
}
}
int stampIndex = 0;
for (int i = 0; i < series; i++) {
stampIndex += core.get(i).sizeT;
}
double firstStamp = 0;
if (timestamps.size() > 0 && stampIndex < timestamps.size()) {
firstStamp = timestamps.get(stampIndex).doubleValue();
}
for (int i = 0; i < getImageCount(); i++) {
int[] zct = FormatTools.getZCTCoords(this, i);
if (getSizeT() > 1 && zct[2] < timestamps.size() - stampIndex) {
double thisStamp = timestamps.get(stampIndex + zct[2]).doubleValue();
store.setPlaneDeltaT(new Time(thisStamp - firstStamp, UNITS.SECOND), series, i);
}
if (xCoordinates.size() > series) {
final Double xCoord = xCoordinates.get(series);
final Double yCoord = yCoordinates.get(series);
final Double zCoord = zCoordinates.get(series);
if (xCoord == null) {
store.setPlanePositionX(null, series, i);
} else {
final Length x = new Length(xCoord, UNITS.REFERENCEFRAME);
store.setPlanePositionX(x, series, i);
}
if (yCoord == null) {
store.setPlanePositionY(null, series, i);
} else {
final Length y = new Length(yCoord, UNITS.REFERENCEFRAME);
store.setPlanePositionY(y, series, i);
}
if (zCoord == null) {
store.setPlanePositionZ(null, series, i);
} else {
final Length z = new Length(zCoord, UNITS.REFERENCEFRAME);
store.setPlanePositionZ(z, series, i);
}
}
}
}
ras.close();
}
use of loci.formats.tiff.PhotoInterp in project bioformats by openmicroscopy.
the class SVSReader method initStandardMetadata.
// -- Internal BaseTiffReader API methods --
/* @see loci.formats.BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
ifds = tiffParser.getIFDs();
int seriesCount = ifds.size();
pixelSize = new Length[seriesCount];
comments = new String[seriesCount];
core.clear();
for (int i = 0; i < seriesCount; i++) {
core.add(new CoreMetadata());
}
for (int i = 0; i < seriesCount; i++) {
setSeries(i);
int index = getIFDIndex(i);
tiffParser.fillInIFD(ifds.get(index));
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
String comment = ifds.get(index).getComment();
if (comment == null) {
continue;
}
String[] lines = comment.split("\n");
String[] tokens;
String key, value;
for (String line : lines) {
tokens = line.split("[|]");
for (String t : tokens) {
if (t.indexOf('=') == -1) {
addGlobalMeta("Comment", t);
comments[i] = t;
} else {
key = t.substring(0, t.indexOf('=')).trim();
value = t.substring(t.indexOf('=') + 1).trim();
addSeriesMeta(key, value);
if (key.equals("MPP")) {
pixelSize[i] = FormatTools.getPhysicalSizeX(DataTools.parseDouble(value));
} else if (key.equals("Date")) {
date = value;
} else if (key.equals("Time")) {
time = value;
} else if (key.equals("Emission Wavelength")) {
emissionWavelength = DataTools.parseDouble(value);
} else if (key.equals("Excitation Wavelength")) {
excitationWavelength = DataTools.parseDouble(value);
} else if (key.equals("Exposure Time")) {
exposureTime = DataTools.parseDouble(value);
} else if (key.equals("Exposure Scale")) {
exposureScale = DataTools.parseDouble(value);
} else if (key.equals("AppMag")) {
magnification = DataTools.parseDouble(value);
}
}
}
}
}
}
setSeries(0);
for (int s = 0; s < seriesCount; s++) {
CoreMetadata ms = core.get(s);
if (s == 0 && getSeriesCount() > 2) {
ms.resolutionCount = getSeriesCount() - 2;
}
IFD ifd = ifds.get(getIFDIndex(s));
PhotoInterp p = ifd.getPhotometricInterpretation();
int samples = ifd.getSamplesPerPixel();
ms.rgb = samples > 1 || p == PhotoInterp.RGB;
ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
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;
}
reorderResolutions();
}
use of loci.formats.tiff.PhotoInterp in project bioformats by openmicroscopy.
the class OMETiffReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
// normalize file name
super.initFile(normalizeFilename(null, id));
id = currentId;
String dir = new File(id).getParent();
// parse and populate OME-XML metadata
String fileName = new Location(id).getAbsoluteFile().getAbsolutePath();
if (!new File(fileName).exists()) {
fileName = currentId;
}
String xml;
IFD firstIFD = null;
boolean companion = false;
if (checkSuffix(fileName, "companion.ome")) {
xml = DataTools.readFile(fileName);
companion = true;
} else {
RandomAccessInputStream ras = new RandomAccessInputStream(fileName, 16);
try {
TiffParser tp = new TiffParser(ras);
firstIFD = tp.getFirstIFD();
xml = firstIFD.getComment();
} finally {
ras.close();
}
}
if (service == null)
setupService();
try {
if (meta == null || !metaFile.equals(currentId)) {
meta = service.createOMEXMLMetadata(xml);
metaFile = currentId;
}
if (companion) {
String firstTIFF = meta.getUUIDFileName(0, 0);
initFile(new Location(dir, firstTIFF).getAbsolutePath());
return;
}
} catch (ServiceException se) {
throw new FormatException(se);
}
String metadataPath = null;
try {
metadataPath = meta.getBinaryOnlyMetadataFile();
} catch (NullPointerException e) {
}
if (metadataPath != null) {
// this is a binary-only file
// overwrite XML with what is in the companion OME-XML file
Location path = new Location(dir, metadataPath);
if (path.exists()) {
metadataFile = path.getAbsolutePath();
xml = readMetadataFile();
try {
meta = service.createOMEXMLMetadata(xml);
} catch (ServiceException se) {
throw new FormatException(se);
} catch (NullPointerException e) {
metadataFile = null;
metadataPath = null;
}
}
}
hasSPW = meta.getPlateCount() > 0;
for (int i = 0; i < meta.getImageCount(); i++) {
int sizeC = meta.getPixelsSizeC(i).getValue().intValue();
service.removeChannels(meta, i, sizeC);
}
Hashtable originalMetadata = service.getOriginalMetadata(meta);
if (originalMetadata != null)
metadata = originalMetadata;
LOGGER.trace(xml);
if (meta.getRoot() == null) {
throw new FormatException("Could not parse OME-XML from TIFF comment");
}
String[] acquiredDates = new String[meta.getImageCount()];
for (int i = 0; i < acquiredDates.length; i++) {
Timestamp acquisitionDate = meta.getImageAcquisitionDate(i);
if (acquisitionDate != null) {
acquiredDates[i] = acquisitionDate.getValue();
}
}
String currentUUID = meta.getUUID();
if (!isGroupFiles() && !isSingleFile(currentId)) {
IFormatReader reader = new MinimalTiffReader();
reader.setId(currentId);
core.set(0, reader.getCoreMetadataList().get(0));
int ifdCount = reader.getImageCount();
reader.close();
int maxSeries = 0;
info = new OMETiffPlane[meta.getImageCount()][];
ArrayList<Integer> imagesToRemove = new ArrayList<Integer>();
ArrayList<int[]> cBounds = new ArrayList<int[]>();
for (int i = 0; i < meta.getImageCount(); i++) {
int maxZ = 0;
int maxC = 0;
int maxT = 0;
int minZ = Integer.MAX_VALUE;
int minC = Integer.MAX_VALUE;
int minT = Integer.MAX_VALUE;
int sizeZ = meta.getPixelsSizeZ(i).getValue();
int sizeC = meta.getChannelCount(i);
int sizeT = meta.getPixelsSizeT(i).getValue();
String order = meta.getPixelsDimensionOrder(i).getValue();
int num = sizeZ * sizeC * sizeT;
CoreMetadata m = i < core.size() ? core.get(i) : new CoreMetadata(core.get(0));
m.dimensionOrder = order;
info[i] = new OMETiffPlane[meta.getTiffDataCount(i)];
int next = 0;
for (int td = 0; td < meta.getTiffDataCount(i); td++) {
String uuid = null;
try {
uuid = meta.getUUIDValue(i, td);
} catch (NullPointerException e) {
}
String filename = null;
try {
filename = meta.getUUIDFileName(i, td);
} catch (NullPointerException e) {
}
if ((uuid == null || !uuid.equals(currentUUID)) && (filename == null || !currentId.endsWith(filename))) {
// this plane doesn't appear to be in the current file
continue;
}
if (i > maxSeries) {
maxSeries = i;
}
NonNegativeInteger ifd = meta.getTiffDataIFD(i, td);
NonNegativeInteger count = meta.getTiffDataPlaneCount(i, td);
NonNegativeInteger firstZ = meta.getTiffDataFirstZ(i, td);
NonNegativeInteger firstC = meta.getTiffDataFirstC(i, td);
NonNegativeInteger firstT = meta.getTiffDataFirstT(i, td);
int realCount = count == null ? 1 : count.getValue();
if (ifd == null && count == null) {
realCount = ifdCount;
}
for (int q = 0; q < realCount; q++) {
OMETiffPlane p = new OMETiffPlane();
p.id = currentId;
p.ifd = q;
if (ifd != null) {
p.ifd += ifd.getValue();
}
p.reader = reader;
info[i][next++] = p;
int z = firstZ == null ? 0 : firstZ.getValue();
int c = firstC == null ? 0 : firstC.getValue();
int t = firstT == null ? 0 : firstT.getValue();
if (q > 0) {
int index = FormatTools.getIndex(order, sizeZ, sizeC, sizeT, num, z, c, t);
int[] add = FormatTools.getZCTCoords(order, sizeZ, sizeC, sizeT, num, q);
z += add[0];
c += add[1];
t += add[2];
}
if (z > maxZ) {
maxZ = z;
}
if (c > maxC) {
maxC = c;
}
if (t > maxT) {
maxT = t;
}
if (z < minZ) {
minZ = z;
}
if (c < minC) {
minC = c;
}
if (t < minT) {
minT = t;
}
}
}
if (i <= maxSeries) {
m.sizeZ = (maxZ - minZ) + 1;
m.sizeC = (maxC - minC) + 1;
m.sizeT = (maxT - minT) + 1;
m.imageCount = m.sizeZ * m.sizeC * m.sizeT;
m.sizeC *= meta.getChannelSamplesPerPixel(i, 0).getValue();
if (i >= core.size()) {
core.add(m);
}
cBounds.add(new int[] { minC, maxC });
} else {
imagesToRemove.add(i);
}
}
// remove extra Images, Channels, and Planes
meta.resolveReferences();
OMEXMLMetadataRoot root = (OMEXMLMetadataRoot) meta.getRoot();
List<Image> images = root.copyImageList();
for (int i = imagesToRemove.size() - 1; i >= 0; i--) {
images.remove(imagesToRemove.get(i));
}
for (int i = 0; i < images.size(); i++) {
Image img = images.get(i);
Pixels pix = img.getPixels();
List<Plane> planes = pix.copyPlaneList();
for (int p = 0; p < planes.size(); p++) {
Plane plane = planes.get(p);
if (plane.getTheZ().getValue() >= core.get(i).sizeZ || plane.getTheC().getValue() >= core.get(i).sizeC || plane.getTheT().getValue() >= core.get(i).sizeT) {
pix.removePlane(planes.get(p));
}
}
pix.setMetadataOnly(null);
List<Channel> channels = pix.copyChannelList();
for (int c = 0; c < channels.size(); c++) {
if (c < cBounds.get(i)[0] || c > cBounds.get(i)[1]) {
pix.removeChannel(channels.get(c));
}
}
}
meta.setRoot(root);
service.convertMetadata(meta, metadataStore);
MetadataTools.populatePixels(metadataStore, this);
return;
}
service.convertMetadata(meta, metadataStore);
// determine series count from Image and Pixels elements
int seriesCount = meta.getImageCount();
core.clear();
for (int i = 0; i < seriesCount; i++) {
core.add(new CoreMetadata());
}
info = new OMETiffPlane[seriesCount][];
tileWidth = new int[seriesCount];
tileHeight = new int[seriesCount];
// compile list of file/UUID mappings
Hashtable<String, String> files = new Hashtable<String, String>();
boolean needSearch = false;
for (int i = 0; i < seriesCount; i++) {
int tiffDataCount = meta.getTiffDataCount(i);
for (int td = 0; td < tiffDataCount; td++) {
String uuid = null;
try {
uuid = meta.getUUIDValue(i, td);
} catch (NullPointerException e) {
}
String filename = null;
if (uuid == null) {
// no UUID means that TiffData element refers to this file
uuid = "";
filename = id;
} else {
filename = meta.getUUIDFileName(i, td);
if (!new Location(dir, filename).exists())
filename = null;
if (filename == null) {
if (uuid.equals(currentUUID) || currentUUID == null) {
// UUID references this file
filename = id;
} else {
// will need to search for this UUID
filename = "";
needSearch = true;
}
} else
filename = normalizeFilename(dir, filename);
}
String existing = files.get(uuid);
if (existing == null)
files.put(uuid, filename);
else if (!existing.equals(filename)) {
throw new FormatException("Inconsistent UUID filenames");
}
}
}
// search for missing filenames
if (needSearch) {
Enumeration en = files.keys();
while (en.hasMoreElements()) {
String uuid = (String) en.nextElement();
String filename = files.get(uuid);
if (filename.equals("")) {
// to make this work with OME server may be a little tricky?
throw new FormatException("Unmatched UUID: " + uuid);
}
}
}
// build list of used files
Enumeration en = files.keys();
int numUUIDs = files.size();
// ensure no duplicate filenames
HashSet fileSet = new HashSet();
for (int i = 0; i < numUUIDs; i++) {
String uuid = (String) en.nextElement();
String filename = files.get(uuid);
fileSet.add(filename);
}
used = new String[fileSet.size()];
Iterator iter = fileSet.iterator();
for (int i = 0; i < used.length; i++) used[i] = (String) iter.next();
// process TiffData elements
Hashtable<String, IFormatReader> readers = new Hashtable<String, IFormatReader>();
boolean adjustedSamples = false;
for (int i = 0; i < seriesCount; i++) {
int s = i;
LOGGER.debug("Image[{}] {", i);
LOGGER.debug(" id = {}", meta.getImageID(i));
String order = meta.getPixelsDimensionOrder(i).toString();
PositiveInteger samplesPerPixel = null;
if (meta.getChannelCount(i) > 0) {
samplesPerPixel = meta.getChannelSamplesPerPixel(i, 0);
}
int samples = samplesPerPixel == null ? -1 : samplesPerPixel.getValue();
int tiffSamples = firstIFD.getSamplesPerPixel();
if (adjustedSamples || (samples != tiffSamples && (i == 0 || samples < 0))) {
LOGGER.warn("SamplesPerPixel mismatch: OME={}, TIFF={}", samples, tiffSamples);
samples = tiffSamples;
adjustedSamples = true;
} else {
adjustedSamples = false;
}
if (adjustedSamples && meta.getChannelCount(i) <= 1) {
adjustedSamples = false;
}
int effSizeC = meta.getPixelsSizeC(i).getValue().intValue();
if (!adjustedSamples) {
effSizeC /= samples;
}
if (effSizeC == 0)
effSizeC = 1;
if (effSizeC * samples != meta.getPixelsSizeC(i).getValue().intValue()) {
effSizeC = meta.getPixelsSizeC(i).getValue().intValue();
}
int sizeT = meta.getPixelsSizeT(i).getValue().intValue();
int sizeZ = meta.getPixelsSizeZ(i).getValue().intValue();
int num = effSizeC * sizeT * sizeZ;
OMETiffPlane[] planes = new OMETiffPlane[num];
for (int no = 0; no < num; no++) planes[no] = new OMETiffPlane();
int tiffDataCount = meta.getTiffDataCount(i);
Boolean zOneIndexed = null;
Boolean cOneIndexed = null;
Boolean tOneIndexed = null;
for (int td = 0; td < tiffDataCount; td++) {
NonNegativeInteger firstC = meta.getTiffDataFirstC(i, td);
NonNegativeInteger firstT = meta.getTiffDataFirstT(i, td);
NonNegativeInteger firstZ = meta.getTiffDataFirstZ(i, td);
int c = firstC == null ? 0 : firstC.getValue();
int t = firstT == null ? 0 : firstT.getValue();
int z = firstZ == null ? 0 : firstZ.getValue();
if (c >= effSizeC && cOneIndexed == null) {
cOneIndexed = true;
} else if (c == 0) {
cOneIndexed = false;
}
if (z >= sizeZ && zOneIndexed == null) {
zOneIndexed = true;
} else if (z == 0) {
zOneIndexed = false;
}
if (t >= sizeT && tOneIndexed == null) {
tOneIndexed = true;
} else if (t == 0) {
tOneIndexed = false;
}
}
for (int td = 0; td < tiffDataCount; td++) {
LOGGER.debug(" TiffData[{}] {", td);
// extract TiffData parameters
String filename = null;
String uuid = null;
try {
filename = meta.getUUIDFileName(i, td);
} catch (NullPointerException e) {
LOGGER.debug("Ignoring null UUID object when retrieving filename.");
}
try {
uuid = meta.getUUIDValue(i, td);
} catch (NullPointerException e) {
LOGGER.debug("Ignoring null UUID object when retrieving value.");
}
NonNegativeInteger tdIFD = meta.getTiffDataIFD(i, td);
int ifd = tdIFD == null ? 0 : tdIFD.getValue();
NonNegativeInteger numPlanes = meta.getTiffDataPlaneCount(i, td);
NonNegativeInteger firstC = meta.getTiffDataFirstC(i, td);
NonNegativeInteger firstT = meta.getTiffDataFirstT(i, td);
NonNegativeInteger firstZ = meta.getTiffDataFirstZ(i, td);
int c = firstC == null ? 0 : firstC.getValue();
int t = firstT == null ? 0 : firstT.getValue();
int z = firstZ == null ? 0 : firstZ.getValue();
// NB: some writers index FirstC, FirstZ and FirstT from 1
if (cOneIndexed != null && cOneIndexed)
c--;
if (zOneIndexed != null && zOneIndexed)
z--;
if (tOneIndexed != null && tOneIndexed)
t--;
if (z >= sizeZ || c >= effSizeC || t >= sizeT) {
LOGGER.warn("Found invalid TiffData: Z={}, C={}, T={}", new Object[] { z, c, t });
break;
}
int index = FormatTools.getIndex(order, sizeZ, effSizeC, sizeT, num, z, c, t);
int count = numPlanes == null ? 1 : numPlanes.getValue();
if (count == 0) {
core.set(s, null);
break;
}
// get reader object for this filename
if (filename == null) {
if (uuid == null)
filename = id;
else
filename = files.get(uuid);
} else
filename = normalizeFilename(dir, filename);
IFormatReader r = readers.get(filename);
if (r == null) {
r = new MinimalTiffReader();
readers.put(filename, r);
}
Location file = new Location(filename);
boolean exists = true;
if (!file.exists()) {
// if this is an absolute file name, try using a relative name
// old versions of OMETiffWriter wrote an absolute path to
// UUID.FileName, which causes problems if the file is moved to
// a different directory
filename = filename.substring(filename.lastIndexOf(File.separator) + 1);
filename = dir + File.separator + filename;
if (!new Location(filename).exists()) {
filename = currentId;
// if only one file is defined, we have to assume that it
// corresponds to the current file
exists = fileSet.size() == 1;
}
}
// populate plane index -> IFD mapping
for (int q = 0; q < count; q++) {
int no = index + q;
planes[no].reader = r;
planes[no].id = filename;
planes[no].ifd = ifd + q;
planes[no].certain = true;
planes[no].exists = exists;
LOGGER.debug(" Plane[{}]: file={}, IFD={}", new Object[] { no, planes[no].id, planes[no].ifd });
}
if (numPlanes == null) {
// unknown number of planes; fill down
for (int no = index + 1; no < num; no++) {
if (planes[no].certain)
break;
planes[no].reader = r;
planes[no].id = filename;
planes[no].ifd = planes[no - 1].ifd + 1;
planes[no].exists = exists;
LOGGER.debug(" Plane[{}]: FILLED", no);
}
} else {
// known number of planes; clear anything subsequently filled
for (int no = index + count; no < num; no++) {
if (planes[no].certain)
break;
planes[no].reader = null;
planes[no].id = null;
planes[no].ifd = -1;
LOGGER.debug(" Plane[{}]: CLEARED", no);
}
}
LOGGER.debug(" }");
}
if (core.get(s) == null)
continue;
// verify that all planes are available
LOGGER.debug(" --------------------------------");
for (int no = 0; no < num; no++) {
LOGGER.debug(" Plane[{}]: file={}, IFD={}", new Object[] { no, planes[no].id, planes[no].ifd });
if (planes[no].reader == null) {
LOGGER.warn("Image ID '{}': missing plane #{}. " + "Using TiffReader to determine the number of planes.", meta.getImageID(i), no);
TiffReader r = new TiffReader();
r.setId(currentId);
try {
planes = new OMETiffPlane[r.getImageCount()];
for (int plane = 0; plane < planes.length; plane++) {
planes[plane] = new OMETiffPlane();
planes[plane].id = currentId;
planes[plane].reader = r;
planes[plane].ifd = plane;
}
num = planes.length;
} finally {
r.close();
}
}
}
LOGGER.debug(" }");
// populate core metadata
CoreMetadata m = core.get(s);
info[s] = planes;
try {
RandomAccessInputStream testFile = new RandomAccessInputStream(info[s][0].id, 16);
String firstFile = info[s][0].id;
if (!info[s][0].reader.isThisType(testFile)) {
LOGGER.warn("{} is not a valid OME-TIFF", info[s][0].id);
info[s][0].id = currentId;
info[s][0].exists = false;
}
testFile.close();
for (int plane = 1; plane < info[s].length; plane++) {
if (info[s][plane].id.equals(firstFile)) {
// don't repeat slow type checking if the files are the same
if (!info[s][0].exists) {
info[s][plane].id = info[s][0].id;
info[s][plane].exists = false;
}
continue;
}
testFile = new RandomAccessInputStream(info[s][plane].id, 16);
if (!info[s][plane].reader.isThisType(testFile)) {
LOGGER.warn("{} is not a valid OME-TIFF", info[s][plane].id);
info[s][plane].id = info[s][0].id;
info[s][plane].exists = false;
}
testFile.close();
}
info[s][0].reader.setId(info[s][0].id);
tileWidth[s] = info[s][0].reader.getOptimalTileWidth();
tileHeight[s] = info[s][0].reader.getOptimalTileHeight();
m.sizeX = meta.getPixelsSizeX(i).getValue().intValue();
int tiffWidth = (int) firstIFD.getImageWidth();
if (m.sizeX != tiffWidth && s == 0) {
LOGGER.warn("SizeX mismatch: OME={}, TIFF={}", m.sizeX, tiffWidth);
}
m.sizeY = meta.getPixelsSizeY(i).getValue().intValue();
int tiffHeight = (int) firstIFD.getImageLength();
if (m.sizeY != tiffHeight && s == 0) {
LOGGER.warn("SizeY mismatch: OME={}, TIFF={}", m.sizeY, tiffHeight);
}
m.sizeZ = meta.getPixelsSizeZ(i).getValue().intValue();
m.sizeC = meta.getPixelsSizeC(i).getValue().intValue();
m.sizeT = meta.getPixelsSizeT(i).getValue().intValue();
m.pixelType = FormatTools.pixelTypeFromString(meta.getPixelsType(i).toString());
int tiffPixelType = firstIFD.getPixelType();
if (m.pixelType != tiffPixelType && (s == 0 || adjustedSamples)) {
LOGGER.warn("PixelType mismatch: OME={}, TIFF={}", m.pixelType, tiffPixelType);
m.pixelType = tiffPixelType;
}
m.imageCount = num;
m.dimensionOrder = meta.getPixelsDimensionOrder(i).toString();
// hackish workaround for files exported by OMERO that have an
// incorrect dimension order
String uuidFileName = "";
try {
if (meta.getTiffDataCount(i) > 0) {
uuidFileName = meta.getUUIDFileName(i, 0);
}
} catch (NullPointerException e) {
}
if (meta.getChannelCount(i) > 0 && meta.getChannelName(i, 0) == null && meta.getTiffDataCount(i) > 0 && uuidFileName.indexOf("__omero_export") != -1) {
m.dimensionOrder = "XYZCT";
}
m.orderCertain = true;
PhotoInterp photo = firstIFD.getPhotometricInterpretation();
m.rgb = samples > 1 || photo == PhotoInterp.RGB;
if ((samples != m.sizeC && (samples % m.sizeC) != 0 && (m.sizeC % samples) != 0) || m.sizeC == 1 || adjustedSamples) {
m.sizeC *= samples;
}
if (m.sizeZ * m.sizeT * m.sizeC > m.imageCount && !m.rgb) {
if (m.sizeZ == m.imageCount) {
m.sizeT = 1;
m.sizeC = 1;
} else if (m.sizeT == m.imageCount) {
m.sizeZ = 1;
m.sizeC = 1;
} else if (m.sizeC == m.imageCount) {
m.sizeT = 1;
m.sizeZ = 1;
}
}
if (meta.getPixelsBinDataCount(i) > 1) {
LOGGER.warn("OME-TIFF Pixels element contains BinData elements! " + "Ignoring.");
}
m.littleEndian = firstIFD.isLittleEndian();
m.interleaved = false;
m.indexed = photo == PhotoInterp.RGB_PALETTE && firstIFD.getIFDValue(IFD.COLOR_MAP) != null;
if (m.indexed) {
m.rgb = false;
}
m.falseColor = true;
m.metadataComplete = true;
if (meta.getPixelsSignificantBits(i) != null) {
m.bitsPerPixel = meta.getPixelsSignificantBits(i).getValue();
}
} catch (NullPointerException exc) {
throw new FormatException("Incomplete Pixels metadata", exc);
}
}
// remove null CoreMetadata entries
ArrayList<CoreMetadata> series = new ArrayList<CoreMetadata>();
final List<OMETiffPlane[]> planeInfo = new ArrayList<OMETiffPlane[]>();
for (int i = 0; i < core.size(); i++) {
if (core.get(i) != null) {
series.add(core.get(i));
planeInfo.add(info[i]);
}
}
core = series;
info = planeInfo.toArray(new OMETiffPlane[0][0]);
if (getImageCount() == 1) {
CoreMetadata ms0 = core.get(0);
ms0.sizeZ = 1;
if (!ms0.rgb) {
ms0.sizeC = 1;
}
ms0.sizeT = 1;
}
for (int i = 0; i < core.size(); i++) {
CoreMetadata m = core.get(i);
Modulo z = service.getModuloAlongZ(meta, i);
if (z != null) {
m.moduloZ = z;
}
Modulo c = service.getModuloAlongC(meta, i);
if (c != null) {
m.moduloC = c;
}
Modulo t = service.getModuloAlongT(meta, i);
if (t != null) {
m.moduloT = t;
}
}
MetadataTools.populatePixels(metadataStore, this, false, false);
for (int i = 0; i < meta.getImageCount(); i++) {
// TheT values are not changed
for (int p = 0; p < meta.getPlaneCount(i); p++) {
NonNegativeInteger z = meta.getPlaneTheZ(i, p);
NonNegativeInteger c = meta.getPlaneTheC(i, p);
NonNegativeInteger t = meta.getPlaneTheT(i, p);
if (z == null) {
z = new NonNegativeInteger(0);
metadataStore.setPlaneTheZ(z, i, p);
}
if (c == null) {
c = new NonNegativeInteger(0);
metadataStore.setPlaneTheC(c, i, p);
}
if (t == null) {
t = new NonNegativeInteger(0);
metadataStore.setPlaneTheT(t, i, p);
}
}
}
for (int i = 0; i < acquiredDates.length; i++) {
if (acquiredDates[i] != null) {
metadataStore.setImageAcquisitionDate(new Timestamp(acquiredDates[i]), i);
}
}
}
Aggregations