use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.
the class MNGReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
in = new RandomAccessInputStream(id);
in.order(false);
LOGGER.info("Verifying MNG format");
seriesInfo = new ArrayList<SeriesInfo>();
seriesInfo.add(new SeriesInfo());
in.skipBytes(12);
if (!"MHDR".equals(in.readString(4))) {
throw new FormatException("Invalid MNG file.");
}
LOGGER.info("Reading dimensions");
in.skipBytes(32);
final List<Long> stack = new ArrayList<Long>();
int maxIterations = 0;
int currentIteration = 0;
LOGGER.info("Finding image offsets");
while (in.getFilePointer() < in.length()) {
int len = in.readInt();
String code = in.readString(4);
long fp = in.getFilePointer();
if (code.equals("IHDR")) {
seriesInfo.get(0).offsets.add(fp - 8);
} else if (code.equals("JDAT")) {
isJNG = true;
seriesInfo.get(0).offsets.add(fp);
} else if (code.equals("IEND")) {
seriesInfo.get(0).lengths.add(fp + len + 4);
} else if (code.equals("LOOP")) {
stack.add(fp + len + 4);
in.skipBytes(1);
maxIterations = in.readInt();
} else if (code.equals("ENDL")) {
long seek = stack.get(stack.size() - 1).longValue();
if (currentIteration < maxIterations) {
in.seek(seek);
currentIteration++;
} else {
stack.remove(stack.size() - 1);
maxIterations = 0;
currentIteration = 0;
}
}
in.seek(fp + len + 4);
}
LOGGER.info("Populating metadata");
// easiest way to get image dimensions is by opening the first plane
Hashtable<String, List<Long>> seriesOffsets = new Hashtable<String, List<Long>>();
Hashtable<String, List<Long>> seriesLengths = new Hashtable<String, List<Long>>();
SeriesInfo info = seriesInfo.get(0);
addGlobalMeta("Number of frames", info.offsets.size());
for (int i = 0; i < info.offsets.size(); i++) {
long offset = info.offsets.get(i);
in.seek(offset);
long end = info.lengths.get(i);
if (end < offset)
continue;
BufferedImage img = readImage(end);
String data = img.getWidth() + "-" + img.getHeight() + "-" + img.getRaster().getNumBands() + "-" + AWTImageTools.getPixelType(img);
List<Long> v = seriesOffsets.get(data);
if (v == null) {
v = new ArrayList<Long>();
seriesOffsets.put(data, v);
}
v.add(offset);
v = seriesLengths.get(data);
if (v == null) {
v = new ArrayList<Long>();
seriesLengths.put(data, v);
}
v.add(end);
}
String[] keys = seriesOffsets.keySet().toArray(new String[0]);
if (keys.length == 0) {
throw new FormatException("Pixel data not found.");
}
int seriesCount = keys.length;
core.clear();
seriesInfo.clear();
for (int i = 0; i < seriesCount; i++) {
CoreMetadata ms = new CoreMetadata();
core.add(ms);
String[] tokens = keys[i].split("-");
ms.sizeX = Integer.parseInt(tokens[0]);
ms.sizeY = Integer.parseInt(tokens[1]);
ms.sizeC = Integer.parseInt(tokens[2]);
ms.pixelType = Integer.parseInt(tokens[3]);
ms.rgb = ms.sizeC > 1;
ms.sizeZ = 1;
ms.dimensionOrder = "XYCZT";
ms.interleaved = false;
ms.metadataComplete = true;
ms.indexed = false;
ms.littleEndian = false;
ms.falseColor = false;
SeriesInfo inf = new SeriesInfo();
inf.offsets = seriesOffsets.get(keys[i]);
inf.lengths = seriesLengths.get(keys[i]);
seriesInfo.add(inf);
ms.imageCount = inf.offsets.size();
ms.sizeT = ms.imageCount;
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
for (int i = 0; i < getSeriesCount(); i++) {
store.setImageName("Series " + (i + 1), i);
}
}
use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.
the class MinimalTiffReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
in = new RandomAccessInputStream(id, 16);
initTiffParser();
Boolean littleEndian = tiffParser.checkHeader();
if (littleEndian == null) {
throw new FormatException("Invalid TIFF file: " + id);
}
boolean little = littleEndian.booleanValue();
in.order(little);
LOGGER.info("Reading IFDs");
IFDList allIFDs = tiffParser.getIFDs();
if (allIFDs == null || allIFDs.size() == 0) {
throw new FormatException("No IFDs found");
}
ifds = new IFDList();
thumbnailIFDs = new IFDList();
subResolutionIFDs = new ArrayList<IFDList>();
for (IFD ifd : allIFDs) {
tiffParser.fillInIFD(ifd);
Number subfile = (Number) ifd.getIFDValue(IFD.NEW_SUBFILE_TYPE);
int subfileType = subfile == null ? 0 : subfile.intValue();
if (subfileType != 1 || allIFDs.size() <= 1) {
ifds.add(ifd);
} else if (subfileType == 1) {
thumbnailIFDs.add(ifd);
}
}
LOGGER.info("Populating metadata");
CoreMetadata ms0 = core.get(0);
ms0.imageCount = ifds.size();
tiffParser.setAssumeEqualStrips(equalStrips);
for (IFD ifd : ifds) {
if ((ifd.getCompression() == TiffCompression.JPEG_2000 || ifd.getCompression() == TiffCompression.JPEG_2000_LOSSY) && ifd.getImageWidth() == ifds.get(0).getImageWidth()) {
LOGGER.debug("Found IFD with JPEG 2000 compression");
long[] stripOffsets = ifd.getStripOffsets();
long[] stripByteCounts = ifd.getStripByteCounts();
if (stripOffsets.length > 0) {
long stripOffset = stripOffsets[0];
in.seek(stripOffset);
JPEG2000MetadataParser metadataParser = new JPEG2000MetadataParser(in, stripOffset + stripByteCounts[0]);
resolutionLevels = metadataParser.getResolutionLevels();
if (resolutionLevels != null && !noSubresolutions) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Original resolution IFD Levels %d %dx%d Tile %dx%d", resolutionLevels, ifd.getImageWidth(), ifd.getImageLength(), ifd.getTileWidth(), ifd.getTileLength()));
}
IFDList theseSubResolutionIFDs = new IFDList();
subResolutionIFDs.add(theseSubResolutionIFDs);
for (int level = 1; level <= resolutionLevels; level++) {
IFD newIFD = new IFD(ifd);
long imageWidth = ifd.getImageWidth();
long imageLength = ifd.getImageLength();
long tileWidth = ifd.getTileWidth();
long tileLength = ifd.getTileLength();
long factor = (long) Math.pow(2, level);
long newTileWidth = Math.round((double) tileWidth / factor);
newTileWidth = newTileWidth < 1 ? 1 : newTileWidth;
long newTileLength = Math.round((double) tileLength / factor);
newTileLength = newTileLength < 1 ? 1 : newTileLength;
long evenTilesPerRow = imageWidth / tileWidth;
long evenTilesPerColumn = imageLength / tileLength;
double remainingWidth = ((double) (imageWidth - (evenTilesPerRow * tileWidth))) / factor;
remainingWidth = remainingWidth < 1 ? Math.ceil(remainingWidth) : Math.round(remainingWidth);
double remainingLength = ((double) (imageLength - (evenTilesPerColumn * tileLength))) / factor;
remainingLength = remainingLength < 1 ? Math.ceil(remainingLength) : Math.round(remainingLength);
long newImageWidth = (long) ((evenTilesPerRow * newTileWidth) + remainingWidth);
long newImageLength = (long) ((evenTilesPerColumn * newTileLength) + remainingLength);
int resolutionLevel = Math.abs(level - resolutionLevels);
newIFD.put(IFD.IMAGE_WIDTH, newImageWidth);
newIFD.put(IFD.IMAGE_LENGTH, newImageLength);
newIFD.put(IFD.TILE_WIDTH, newTileWidth);
newIFD.put(IFD.TILE_LENGTH, newTileLength);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Added JPEG 2000 sub-resolution IFD Level %d %dx%d " + "Tile %dx%d", resolutionLevel, newImageWidth, newImageLength, newTileWidth, newTileLength));
}
theseSubResolutionIFDs.add(newIFD);
}
}
} else {
LOGGER.warn("IFD has no strip offsets!");
}
}
}
IFD firstIFD = ifds.get(0);
PhotoInterp photo = firstIFD.getPhotometricInterpretation();
int samples = firstIFD.getSamplesPerPixel();
ms0.rgb = samples > 1 || photo == PhotoInterp.RGB;
ms0.interleaved = false;
ms0.littleEndian = firstIFD.isLittleEndian();
ms0.sizeX = (int) firstIFD.getImageWidth();
ms0.sizeY = (int) firstIFD.getImageLength();
ms0.sizeZ = 1;
ms0.sizeC = isRGB() ? samples : 1;
ms0.sizeT = ifds.size();
ms0.pixelType = firstIFD.getPixelType();
ms0.metadataComplete = true;
ms0.indexed = photo == PhotoInterp.RGB_PALETTE && (get8BitLookupTable() != null || get16BitLookupTable() != null);
if (isIndexed()) {
ms0.sizeC = 1;
ms0.rgb = false;
for (IFD ifd : ifds) {
ifd.putIFDValue(IFD.PHOTOMETRIC_INTERPRETATION, PhotoInterp.RGB_PALETTE);
}
}
if (getSizeC() == 1 && !isIndexed())
ms0.rgb = false;
ms0.dimensionOrder = "XYCZT";
ms0.bitsPerPixel = firstIFD.getBitsPerSample()[0];
// New core metadata now that we know how many sub-resolutions we have.
if (resolutionLevels != null && subResolutionIFDs.size() > 0) {
IFDList ifds = subResolutionIFDs.get(0);
int seriesCount = ifds.size() + 1;
if (!hasFlattenedResolutions()) {
ms0.resolutionCount = seriesCount;
}
ms0.sizeT = subResolutionIFDs.size();
ms0.imageCount = ms0.sizeT;
if (ms0.sizeT <= 0) {
ms0.sizeT = 1;
}
if (ms0.imageCount <= 0) {
ms0.imageCount = 1;
}
for (IFD ifd : ifds) {
CoreMetadata ms = new CoreMetadata(this, 0);
core.add(ms);
ms.sizeX = (int) ifd.getImageWidth();
ms.sizeY = (int) ifd.getImageLength();
ms.sizeT = ms0.sizeT;
ms.imageCount = ms0.imageCount;
ms.thumbnail = true;
ms.resolutionCount = 1;
}
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
}
use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.
the class MicromanagerReader method populateMetadata.
private void populateMetadata() throws FormatException, IOException {
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this, true);
String instrumentID = MetadataTools.createLSID("Instrument", 0);
store.setInstrumentID(instrumentID, 0);
for (int i = 0; i < positions.size(); i++) {
Position p = positions.get(i);
if (p.time != null) {
String date = DateTools.formatDate(p.time, DATE_FORMAT);
if (date != null) {
store.setImageAcquisitionDate(new Timestamp(date), i);
}
}
if (positions.size() > 1) {
Location parent = new Location(p.metadataFile).getParentFile();
store.setImageName(parent.getName(), i);
}
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
store.setImageDescription(p.comment, i);
// link Instrument and Image
store.setImageInstrumentRef(instrumentID, i);
for (int c = 0; c < p.channels.length; c++) {
store.setChannelName(p.channels[c], i, c);
}
Length sizeX = FormatTools.getPhysicalSizeX(p.pixelSize);
Length sizeY = FormatTools.getPhysicalSizeY(p.pixelSize);
Length sizeZ = FormatTools.getPhysicalSizeZ(p.sliceThickness);
if (sizeX != null) {
store.setPixelsPhysicalSizeX(sizeX, i);
}
if (sizeY != null) {
store.setPixelsPhysicalSizeY(sizeY, i);
}
if (sizeZ != null) {
store.setPixelsPhysicalSizeZ(sizeZ, i);
}
int nextStamp = 0;
for (int q = 0; q < getImageCount(); q++) {
store.setPlaneExposureTime(p.exposureTime, i, q);
String tiff = positions.get(getSeries()).getFile(q);
if (tiff != null && new Location(tiff).exists() && nextStamp < p.timestamps.length && p.timestamps[nextStamp] != null) {
store.setPlaneDeltaT(new Time(p.timestamps[nextStamp++], UNITS.MILLISECOND), i, q);
}
if (p.positions != null && q < p.positions.length) {
if (p.positions[q][0] != null) {
store.setPlanePositionX(new Length(p.positions[q][0], UNITS.MICROMETER), i, q);
}
if (p.positions[q][1] != null) {
store.setPlanePositionY(new Length(p.positions[q][1], UNITS.MICROMETER), i, q);
}
if (p.positions[q][2] != null) {
store.setPlanePositionZ(new Length(p.positions[q][2], UNITS.MICROMETER), i, q);
}
}
}
String serialNumber = p.detectorID;
p.detectorID = MetadataTools.createLSID("Detector", 0, i);
for (int c = 0; c < p.channels.length; c++) {
store.setDetectorSettingsBinning(getBinning(p.binning), i, c);
store.setDetectorSettingsGain(new Double(p.gain), i, c);
if (c < p.voltage.size()) {
store.setDetectorSettingsVoltage(new ElectricPotential(p.voltage.get(c), UNITS.VOLT), i, c);
}
store.setDetectorSettingsID(p.detectorID, i, c);
}
store.setDetectorID(p.detectorID, 0, i);
if (p.detectorModel != null) {
store.setDetectorModel(p.detectorModel, 0, i);
}
if (serialNumber != null) {
store.setDetectorSerialNumber(serialNumber, 0, i);
}
if (p.detectorManufacturer != null) {
store.setDetectorManufacturer(p.detectorManufacturer, 0, i);
}
if (p.cameraMode == null)
p.cameraMode = "Other";
store.setDetectorType(getDetectorType(p.cameraMode), 0, i);
store.setImagingEnvironmentTemperature(new Temperature(p.temperature, UNITS.CELSIUS), i);
}
}
}
use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.
the class NRRDReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
// make sure we actually have the .nrrd/.nhdr file
if (!checkSuffix(id, "nhdr") && !checkSuffix(id, "nrrd")) {
id += ".nhdr";
if (!new Location(id).exists()) {
id = id.substring(0, id.lastIndexOf("."));
id = id.substring(0, id.lastIndexOf("."));
id += ".nhdr";
}
id = new Location(id).getAbsolutePath();
}
super.initFile(id);
in = new RandomAccessInputStream(id);
ClassList<IFormatReader> classes = ImageReader.getDefaultReaderClasses();
Class<? extends IFormatReader>[] classArray = classes.getClasses();
ClassList<IFormatReader> newClasses = new ClassList<IFormatReader>(IFormatReader.class);
for (Class<? extends IFormatReader> c : classArray) {
if (!c.equals(NRRDReader.class)) {
newClasses.addClass(c);
}
}
helper = new ImageReader(newClasses);
helper.setMetadataOptions(new DefaultMetadataOptions(MetadataLevel.MINIMUM));
String key, v;
String[] pixelSizeUnits = null;
int numDimensions = 0;
CoreMetadata m = core.get(0);
m.sizeX = 1;
m.sizeY = 1;
m.sizeZ = 1;
m.sizeC = 1;
m.sizeT = 1;
m.dimensionOrder = "XYCZT";
String line = in.readLine();
while (line != null && line.length() > 0) {
if (!line.startsWith("#") && !line.startsWith("NRRD")) {
// parse key/value pair
key = line.substring(0, line.indexOf(':')).trim();
v = line.substring(line.indexOf(':') + 1).trim();
addGlobalMeta(key, v);
if (key.equals("type")) {
if (v.indexOf("char") != -1 || v.indexOf('8') != -1) {
m.pixelType = FormatTools.UINT8;
} else if (v.indexOf("short") != -1 || v.indexOf("16") != -1) {
m.pixelType = FormatTools.UINT16;
} else if (v.equals("int") || v.equals("signed int") || v.equals("int32") || v.equals("int32_t") || v.equals("uint") || v.equals("unsigned int") || v.equals("uint32") || v.equals("uint32_t")) {
m.pixelType = FormatTools.UINT32;
} else if (v.equals("float"))
m.pixelType = FormatTools.FLOAT;
else if (v.equals("double"))
m.pixelType = FormatTools.DOUBLE;
else
throw new FormatException("Unsupported data type: " + v);
} else if (key.equals("dimension")) {
numDimensions = Integer.parseInt(v);
} else if (key.equals("sizes")) {
String[] tokens = v.split(" ");
for (int i = 0; i < numDimensions; i++) {
int size = Integer.parseInt(tokens[i]);
if (numDimensions >= 3 && i == 0 && size > 1 && size <= 16) {
m.sizeC = size;
} else if (i == 0 || (getSizeC() > 1 && i == 1)) {
m.sizeX = size;
} else if (i == 1 || (getSizeC() > 1 && i == 2)) {
m.sizeY = size;
} else if (i == 2 || (getSizeC() > 1 && i == 3)) {
m.sizeZ = size;
} else if (i == 3 || (getSizeC() > 1 && i == 4)) {
m.sizeT = size;
}
}
} else if (key.equals("data file") || key.equals("datafile")) {
dataFile = v;
} else if (key.equals("encoding"))
encoding = v;
else if (key.equals("endian")) {
m.littleEndian = v.equals("little");
} else if (key.equals("spacings") || key.equals("space directions")) {
pixelSizes = v.split(" ");
} else if (key.equals("space units")) {
pixelSizeUnits = v.split(" ");
} else if (key.equals("byte skip")) {
offset = Long.parseLong(v);
}
}
line = in.readLine();
if (line != null)
line = line.trim();
}
if (dataFile == null)
offset = in.getFilePointer();
else {
Location f = new Location(currentId).getAbsoluteFile();
Location parent = f.getParentFile();
if (f.exists() && parent != null) {
dataFile = dataFile.substring(dataFile.indexOf(File.separator) + 1);
dataFile = new Location(parent, dataFile).getAbsolutePath();
}
initializeHelper = !encoding.equals("raw");
}
m.rgb = getSizeC() > 1;
m.interleaved = true;
m.imageCount = getSizeZ() * getSizeT();
m.indexed = false;
m.falseColor = false;
m.metadataComplete = true;
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
if (pixelSizes != null) {
for (int i = 0; i < pixelSizes.length; i++) {
if (pixelSizes[i] == null)
continue;
try {
Double d = parsePixelSize(i);
String unit = pixelSizeUnits == null || i >= pixelSizeUnits.length ? null : pixelSizeUnits[i].replaceAll("\"", "");
if (i == 0) {
Length x = FormatTools.getPhysicalSizeX(d, unit);
if (x != null) {
store.setPixelsPhysicalSizeX(x, 0);
}
} else if (i == 1) {
Length y = FormatTools.getPhysicalSizeY(d, unit);
if (y != null) {
store.setPixelsPhysicalSizeY(y, 0);
}
} else if (i == 2) {
Length z = FormatTools.getPhysicalSizeZ(d, unit);
if (z != null) {
store.setPixelsPhysicalSizeZ(z, 0);
}
}
} catch (NumberFormatException e) {
}
}
}
}
}
use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.
the class ImageIOReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
CoreMetadata m = core.get(0);
LOGGER.info("Populating metadata");
m.imageCount = 1;
initImage();
m.sizeX = img.getWidth();
m.sizeY = img.getHeight();
m.rgb = img.getRaster().getNumBands() > 1;
m.sizeZ = 1;
m.sizeC = isRGB() ? 3 : 1;
m.sizeT = 1;
m.dimensionOrder = "XYCZT";
m.pixelType = AWTImageTools.getPixelType(img);
m.interleaved = false;
m.littleEndian = false;
m.metadataComplete = true;
m.indexed = false;
m.falseColor = false;
// populate the metadata store
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
}
Aggregations