use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class IonpathMIBITiffReader method initStandardMetadata.
// -- Internal BaseTiffReader API methods --
/* @see loci.formats.BaseTiffReader#initStandardMetadata() */
@Override
protected void initStandardMetadata() throws FormatException, IOException {
super.initStandardMetadata();
ifds = tiffParser.getIFDs();
int seriesIndex;
String imageType = null;
core.clear();
for (int i = 0; i < ifds.size(); i++) {
IFD ifd = ifds.get(i);
Object description = ifd.get(IFD.IMAGE_DESCRIPTION);
if (description == null) {
throw new FormatException("Image description is mandatory.");
}
String imageDescription = null;
if (description instanceof TiffIFDEntry) {
Object value = tiffParser.getIFDValue((TiffIFDEntry) description);
if (value != null) {
imageDescription = value.toString();
}
} else if (description instanceof String) {
imageDescription = (String) description;
}
JSONObject jsonDescription;
try {
jsonDescription = new JSONObject(imageDescription);
imageType = jsonDescription.getString("image.type");
if (imageType.equals("SIMS")) {
String mass = jsonDescription.getString("channel.mass");
if (mass == null) {
throw new FormatException("Channel masses are mandatory.");
}
String target = jsonDescription.getString("channel.target");
channelIDs.add(mass);
channelNames.add(target != null && target != "null" ? target : mass);
}
} catch (JSONException e) {
throw new FormatException("Unexpected format in SIMS description JSON.");
}
if (seriesTypes.containsKey(imageType)) {
if (!imageType.equals("SIMS")) {
throw new FormatException("Only type 'SIMS' can have >1 image per file.");
}
seriesIndex = seriesTypes.get(imageType);
CoreMetadata ms = core.get(seriesIndex);
ms.sizeC += 1;
ms.imageCount += 1;
} else {
seriesIndex = seriesTypes.size();
seriesTypes.put(imageType, seriesIndex);
seriesIFDs.add(i);
core.add(new CoreMetadata());
setSeries(seriesIndex);
tiffParser.setDoCaching(true);
tiffParser.fillInIFD(ifd);
if (imageType.equals("SIMS")) {
try {
Iterator<?> keySet = jsonDescription.keys();
while (keySet.hasNext()) {
String key = (String) keySet.next();
if (key.startsWith("mibi.")) {
simsDescription.put(key, jsonDescription.getString(key));
}
}
} catch (JSONException e) {
throw new FormatException("Unexpected format in SIMS description JSON.");
}
}
CoreMetadata ms = core.get(seriesIndex);
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 = false;
ms.imageCount = 1;
if (imageType.equals("SIMS")) {
ms.seriesMetadata = simsDescription;
}
}
}
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class IonpathMIBITiffReader method isThisType.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#isThisType(RandomAccessInputStream) */
@Override
public boolean isThisType(RandomAccessInputStream stream) throws IOException {
TiffParser tiffParser = new TiffParser(stream);
IFD ifd = tiffParser.getFirstIFD();
if (ifd == null)
return false;
String software = ifd.getIFDTextValue(IFD.SOFTWARE);
if (software == null)
return false;
return software.startsWith(IONPATH_MIBI_SOFTWARE_PREFIX);
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class FlexReader method getOptimalTileWidth.
/* @see loci.formats.IFormatReader#getOptimalTileWidth() */
@Override
public int getOptimalTileWidth() {
FormatTools.assertId(currentId, true, 1);
FlexFile file = lookupFile(0);
IFD ifd = file.ifds.get(0);
try {
return (int) ifd.getTileWidth();
} catch (FormatException e) {
LOGGER.debug("Could not retrieve tile width", e);
}
return super.getOptimalTileWidth();
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class FlexReader method openBytes.
/**
* @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
*/
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
FlexFile firstFile = lookupFile(0);
FlexFile file = lookupFile(getSeries());
int[] lengths = new int[] { fieldCount / effectiveFieldCount, wellCount, plateCount };
int[] pos = FormatTools.rasterToPosition(lengths, getSeries());
int imageNumber = file.offsets == null ? getImageCount() * pos[0] + no : 0;
RandomAccessInputStream s = new RandomAccessInputStream(getFileHandle(file.file));
IFD ifd;
double factor;
if (file.offsets == null) {
ifd = file.ifds.get(imageNumber);
factor = 1d;
} else {
// Only the first IFD was read. Hack the IFD to adjust the offset.
final IFD firstIFD = firstFile.ifds.get(0);
ifd = new IFD(firstIFD);
int tag = IFD.STRIP_OFFSETS;
if (firstIFD.isTiled() && firstIFD.getIFDLongArray(IFD.TILE_OFFSETS) != null) {
tag = IFD.TILE_OFFSETS;
}
long[] offsets = ifd.getIFDLongArray(tag);
final int planeSize = getSizeX() * getSizeY() * getRGBChannelCount() * ifd.getBitsPerSample()[0] / 8;
final int index = getImageCount() * pos[0] + no;
long offset = (index == file.offsets.length - 1 ? s.length() : file.offsets[index + 1]) - offsets[0] - planeSize;
for (int i = 0; i < offsets.length; i++) {
offsets[i] += offset;
}
ifd.putIFDValue(tag, offsets);
}
int nBytes = ifd.getBitsPerSample()[0] / 8;
int bpp = FormatTools.getBytesPerPixel(getPixelType());
// read pixels from the file
TiffParser tp = new TiffParser(s);
tp.fillInIFD(ifd);
// log the first offset used
LOGGER.trace("first offset for series={} no={}: {}", getCoreIndex(), no, ifd.getStripOffsets()[0]);
tp.getSamples(ifd, buf, x, y, w, h);
factor = file.factors == null ? 1d : file.factors[imageNumber];
LOGGER.trace(" using factor = {}", factor);
tp.getStream().close();
// expand pixel values with multiplication by factor[no]
int num = buf.length / bpp;
if (factor != 1d || nBytes != bpp) {
for (int i = num - 1; i >= 0; i--) {
int q = nBytes == 1 ? buf[i] & 0xff : DataTools.bytesToInt(buf, i * nBytes, nBytes, isLittleEndian());
q = (int) (q * factor);
DataTools.unpackBytes(q, buf, i * bpp, bpp, isLittleEndian());
}
}
s.close();
return buf;
}
use of loci.formats.tiff.IFD in project bioformats by openmicroscopy.
the class FlexReader method populateCoreMetadata.
/**
* Populate core metadata using the given list of image names.
*/
private void populateCoreMetadata(int wellRow, int wellCol, int field, List<String> imageNames) throws FormatException {
LOGGER.info("Populating core metadata for well row " + wellRow + ", column " + wellCol);
CoreMetadata ms0 = core.get(0);
if (getSizeC() == 0 && getSizeT() == 0) {
if (fieldCount == 0 || (imageNames.size() % fieldCount) != 0) {
fieldCount = 1;
}
final List<String> uniqueChannels = new ArrayList<String>();
for (int i = 0; i < imageNames.size(); i++) {
String name = imageNames.get(i);
String[] tokens = name.split("_");
if (tokens.length > 1) {
// fields are indexed from 1
int fieldIndex = Integer.parseInt(tokens[0]);
if (fieldIndex > fieldCount)
fieldCount = fieldIndex;
} else
tokens = name.split(":");
String channel = tokens[tokens.length - 1];
if (!uniqueChannels.contains(channel))
uniqueChannels.add(channel);
}
if (fieldCount == 0)
fieldCount = 1;
ms0.sizeC = (int) Math.max(uniqueChannels.size(), 1);
if (getSizeZ() == 0)
ms0.sizeZ = 1;
ms0.sizeT = imageNames.size() / (fieldCount * getSizeC() * getSizeZ());
}
if (getSizeC() == 0) {
ms0.sizeC = (int) Math.max(channelNames.length, 1);
}
if (getSizeZ() == 0)
ms0.sizeZ = 1;
if (getSizeT() == 0)
ms0.sizeT = 1;
if (plateCount == 0)
plateCount = 1;
if (wellCount == 0)
wellCount = 1;
if (fieldCount == 0)
fieldCount = 1;
// adjust dimensions if the number of IFDs doesn't match the number
// of reported images
FlexFile file = lookupFile(wellRow, wellCol, field);
IFD ifd = file.ifds.get(0);
int nPlanes = file.ifds.size();
if (file.offsets != null) {
nPlanes = file.offsets.length;
}
ms0.imageCount = getSizeZ() * getSizeC() * getSizeT();
if (getImageCount() == imageNames.size()) {
fieldCount = 1;
}
// otherwise assume that fields are stored within the files
if (getImageCount() * fieldCount != nPlanes && ((getImageCount() != nPlanes && nFiles > 1) || nFiles == 1)) {
ms0.imageCount = nPlanes / fieldCount;
ms0.sizeZ = 1;
ms0.sizeT = nPlanes / fieldCount;
if (getSizeT() % getSizeC() == 0) {
ms0.sizeT /= getSizeC();
} else {
ms0.sizeC = 1;
}
}
ms0.sizeX = (int) ifd.getImageWidth();
ms0.sizeY = (int) ifd.getImageLength();
ms0.dimensionOrder = "XYCZT";
ms0.rgb = false;
ms0.interleaved = false;
ms0.indexed = false;
ms0.littleEndian = ifd.isLittleEndian();
ms0.pixelType = ifd.getPixelType();
if (fieldCount == 1) {
fieldCount *= nFiles;
}
int seriesCount = plateCount * wellCount * fieldCount;
if (seriesCount > 1) {
core.clear();
for (int i = 0; i < seriesCount; i++) {
core.add(ms0);
}
}
}
Aggregations