use of loci.formats.tiff.TiffParser 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.TiffParser in project bioformats by openmicroscopy.
the class APLReader method close.
/* @see loci.formats.IFormatReader#close(boolean) */
@Override
public void close(boolean fileOnly) throws IOException {
super.close(fileOnly);
if (!fileOnly) {
tiffFiles = null;
xmlFiles = null;
used = null;
ifds = null;
if (parser != null) {
for (TiffParser p : parser) {
if (p != null) {
p.getStream().close();
}
}
}
parser = null;
}
}
use of loci.formats.tiff.TiffParser 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.TiffParser in project bioformats by openmicroscopy.
the class BDReader method isThisType.
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
TiffParser p = new TiffParser(stream);
IFD ifd = p.getFirstIFD();
if (ifd == null)
return false;
String software = ifd.getIFDTextValue(IFD.SOFTWARE);
if (software == null)
return false;
return software.trim().startsWith("MATROX Imaging Library");
}
use of loci.formats.tiff.TiffParser in project bioformats by openmicroscopy.
the class TiffSaverTest method setUp.
@BeforeMethod
public void setUp() throws IOException {
ByteArrayHandle handle = new ByteArrayHandle(INITIAL_CAPACITY);
out = new RandomAccessOutputStream(handle);
in = new RandomAccessInputStream(handle);
tiffSaver = new TiffSaver(out, handle);
tiffParser = new TiffParser(in);
ifd = new IFD();
ifd.putIFDValue(IFD.IMAGE_WIDTH, 512);
ifd.putIFDValue(IFD.IMAGE_DESCRIPTION, "comment");
}
Aggregations