Search in sources :

Example 16 with Length

use of ome.units.quantity.Length in project bioformats by openmicroscopy.

the class FEITiffReader method initMetadataStore.

/* @see BaseTiffReader#initMetadataStore() */
@Override
protected void initMetadataStore() throws FormatException {
    super.initMetadataStore();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (date != null) {
        date = DateTools.formatDate(date, DATE_FORMAT);
        if (date != null) {
            store.setImageAcquisitionDate(new Timestamp(date), 0);
        }
    }
    if (imageName != null) {
        store.setImageName(imageName, 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        if (imageDescription != null) {
            store.setImageDescription(imageDescription, 0);
        }
        if (userName != null) {
            store.setExperimenterID(MetadataTools.createLSID("Experimenter", 0), 0);
            store.setExperimenterLastName(userName, 0);
        }
        if (microscopeModel != null) {
            String instrument = MetadataTools.createLSID("Instrument", 0);
            store.setInstrumentID(instrument, 0);
            store.setImageInstrumentRef(instrument, 0);
            store.setMicroscopeModel(microscopeModel, 0);
        }
        if (detectors != null && detectors.size() > 0) {
            String instrument = MetadataTools.createLSID("Instrument", 0);
            store.setInstrumentID(instrument, 0);
            store.setImageInstrumentRef(instrument, 0);
            for (int i = 0; i < detectors.size(); i++) {
                String detectorID = MetadataTools.createLSID("Detector", 0, i);
                store.setDetectorID(detectorID, 0, i);
                store.setDetectorModel(detectors.get(i), 0, i);
                store.setDetectorType(getDetectorType("Other"), 0, i);
            }
        }
        if (magnification != null) {
            store.setObjectiveID(MetadataTools.createLSID("Objective", 0, 0), 0, 0);
            store.setObjectiveNominalMagnification(magnification, 0, 0);
            store.setObjectiveCorrection(getCorrection("Other"), 0, 0);
            store.setObjectiveImmersion(getImmersion("Other"), 0, 0);
        }
        store.setStageLabelX(stageX, 0);
        store.setStageLabelY(stageY, 0);
        store.setStageLabelZ(stageZ, 0);
        store.setStageLabelName("", 0);
        boolean helios = ifds.get(0).containsKey(HELIOS_TAG);
        Unit<Length> unit = UNITS.MICROMETER;
        if (helios) {
            unit = UNITS.METER;
        }
        Length physicalSizeX = FormatTools.getPhysicalSizeX(sizeX, unit);
        Length physicalSizeY = FormatTools.getPhysicalSizeY(sizeY, unit);
        if (physicalSizeX != null) {
            store.setPixelsPhysicalSizeX(physicalSizeX, 0);
        }
        if (physicalSizeY != null) {
            store.setPixelsPhysicalSizeY(physicalSizeY, 0);
        }
        if (timeIncrement != null) {
            store.setPixelsTimeIncrement(new Time(timeIncrement, UNITS.SECOND), 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp)

Example 17 with Length

use of ome.units.quantity.Length in project bioformats by openmicroscopy.

the class FlexReader method populateMetadataStore.

private void populateMetadataStore(MetadataStore store) throws FormatException {
    LOGGER.info("Populating MetadataStore");
    MetadataTools.populatePixels(store, this, true);
    Location currentFile = new Location(getCurrentFile()).getAbsoluteFile();
    int[] lengths = new int[] { fieldCount, wellCount, plateCount };
    store.setPlateID(MetadataTools.createLSID("Plate", 0), 0);
    String plateAcqID = MetadataTools.createLSID("PlateAcquisition", 0, 0);
    store.setPlateAcquisitionID(plateAcqID, 0, 0);
    PositiveInteger maxFieldCount = FormatTools.getMaxFieldCount(fieldCount);
    if (maxFieldCount != null) {
        store.setPlateAcquisitionMaximumFieldCount(maxFieldCount, 0, 0);
    }
    plateAcqStartTime = DateTools.formatDate(plateAcqStartTime, "dd.MM.yyyy  HH:mm:ss");
    if (plateAcqStartTime != null) {
        store.setPlateAcquisitionStartTime(new Timestamp(plateAcqStartTime), 0, 0);
    }
    for (int row = 0; row < wellRows; row++) {
        for (int col = 0; col < wellColumns; col++) {
            int well = row * wellColumns + col;
            store.setWellID(MetadataTools.createLSID("Well", 0, well), 0, well);
            store.setWellRow(new NonNegativeInteger(row), 0, well);
            store.setWellColumn(new NonNegativeInteger(col), 0, well);
        }
    }
    for (int i = 0; i < getSeriesCount(); i++) {
        int[] pos = FormatTools.rasterToPosition(lengths, i);
        String imageID = MetadataTools.createLSID("Image", i);
        store.setImageID(imageID, i);
        int well = wellNumber[pos[1]][0] * wellColumns + wellNumber[pos[1]][1];
        char wellRow = (char) ('A' + wellNumber[pos[1]][0]);
        store.setImageName("Well " + wellRow + "-" + (wellNumber[pos[1]][1] + 1) + "; Field #" + (pos[0] + 1), i);
        if (acquisitionDates.get(i) != null) {
            store.setImageAcquisitionDate(acquisitionDates.get(i), i);
        }
        if (wellRows == 0 && wellColumns == 0) {
            well = pos[1];
            NonNegativeInteger row = new NonNegativeInteger(wellNumber[pos[1]][0]);
            NonNegativeInteger col = new NonNegativeInteger(wellNumber[pos[1]][1]);
            String wellID = MetadataTools.createLSID("Well", pos[2], well);
            store.setWellID(wellID, pos[2], well);
            store.setWellRow(row, pos[2], pos[1]);
            store.setWellColumn(col, pos[2], pos[1]);
        }
        String wellSample = MetadataTools.createLSID("WellSample", pos[2], well, pos[0]);
        store.setWellSampleID(wellSample, pos[2], well, pos[0]);
        store.setWellSampleIndex(new NonNegativeInteger(i), pos[2], well, pos[0]);
        store.setWellSampleImageRef(imageID, pos[2], well, pos[0]);
        store.setPlateAcquisitionWellSampleRef(wellSample, 0, 0, i);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        String instrumentID = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrumentID, 0);
        if (plateName == null)
            plateName = currentFile.getParentFile().getName();
        if (plateBarcode != null)
            plateName = plateBarcode + " " + plateName;
        store.setPlateName(plateName, 0);
        store.setPlateRowNamingConvention(getNamingConvention("Letter"), 0);
        store.setPlateColumnNamingConvention(getNamingConvention("Number"), 0);
        for (int i = 0; i < getSeriesCount(); i++) {
            int[] pos = FormatTools.rasterToPosition(lengths, i);
            store.setImageInstrumentRef(instrumentID, i);
            int seriesIndex = i * getImageCount();
            if (seriesIndex < objectiveRefs.size()) {
                store.setObjectiveSettingsID(objectiveRefs.get(seriesIndex), i);
            }
            for (int c = 0; c < getEffectiveSizeC(); c++) {
                int channelIndex = seriesIndex + c;
                if (seriesIndex > 0 && channelNames.length == getEffectiveSizeC() * getSeriesCount()) {
                    channelIndex = i * getEffectiveSizeC() + c;
                }
                if (channelNames != null && channelIndex >= channelNames.length) {
                    channelIndex = c;
                }
                if (channelNames != null && channelIndex < channelNames.length) {
                    store.setChannelName(channelNames[channelIndex], i, c);
                }
            }
            if (seriesIndex < lightSourceCombinationRefs.size()) {
                String lightSourceCombo = lightSourceCombinationRefs.get(seriesIndex);
                List<String> lightSources = lightSourceCombinationIDs.get(lightSourceCombo);
                for (int c = 0; c < getEffectiveSizeC(); c++) {
                    int index = seriesIndex + c;
                    if (index < cameraRefs.size()) {
                        store.setDetectorSettingsID(cameraRefs.get(index), i, c);
                        if (index < binnings.size()) {
                            store.setDetectorSettingsBinning(getBinning(binnings.get(index)), i, c);
                        }
                    }
                    if (lightSources != null && c < lightSources.size()) {
                        store.setChannelLightSourceSettingsID(lightSources.get(c), i, c);
                    } else if (c > 0 && lightSources != null && lightSources.size() == 1) {
                        lightSourceCombo = lightSourceCombinationRefs.get(seriesIndex + c);
                        lightSources = lightSourceCombinationIDs.get(lightSourceCombo);
                        store.setChannelLightSourceSettingsID(lightSources.get(0), i, c);
                    }
                    if (index < filterSets.size()) {
                        FilterGroup group = filterSetMap.get(filterSets.get(index));
                        if (group != null) {
                            if (group.emission != null) {
                                store.setLightPathEmissionFilterRef(group.emission, i, c, 0);
                            }
                            if (group.excitation != null) {
                                store.setLightPathExcitationFilterRef(group.excitation, i, c, 0);
                            }
                            if (group.dichroic != null) {
                                store.setLightPathDichroicRef(group.dichroic, i, c);
                            }
                        }
                    }
                }
            }
            if (seriesIndex < xSizes.size()) {
                Length size = FormatTools.getPhysicalSizeX(xSizes.get(seriesIndex));
                if (size != null) {
                    store.setPixelsPhysicalSizeX(size, i);
                }
            }
            if (seriesIndex < ySizes.size()) {
                Length size = FormatTools.getPhysicalSizeY(ySizes.get(seriesIndex));
                if (size != null) {
                    store.setPixelsPhysicalSizeY(size, i);
                }
            }
            int well = wellNumber[pos[1]][0] * wellColumns + wellNumber[pos[1]][1];
            if (wellRows == 0 && wellColumns == 0) {
                well = pos[1];
            }
            if (pos[0] < xPositions.size()) {
                Length l = new Length(xPositions.get(pos[0]), UNITS.REFERENCEFRAME);
                store.setWellSamplePositionX(l, pos[2], well, pos[0]);
            }
            if (pos[0] < yPositions.size()) {
                Length l = new Length(yPositions.get(pos[0]), UNITS.REFERENCEFRAME);
                store.setWellSamplePositionY(l, pos[2], well, pos[0]);
            }
            for (int image = 0; image < getImageCount(); image++) {
                int plane = i * getImageCount() + image;
                int c = getZCTCoords(image)[1];
                if (plane < planePositionX.size()) {
                    store.setPlanePositionX(planePositionX.get(plane), i, image);
                }
                if (plane < planePositionY.size()) {
                    store.setPlanePositionY(planePositionY.get(plane), i, image);
                }
                if (plane < planePositionZ.size()) {
                    store.setPlanePositionZ(planePositionZ.get(plane), i, image);
                }
                if (plane - image + c < planeExposureTime.size()) {
                    if (planeExposureTime.get(plane - image + c) != null) {
                        store.setPlaneExposureTime(new Time(planeExposureTime.get(plane - image + c), UNITS.SECOND), i, image);
                    }
                }
                if (plane < planeDeltaT.size() && planeDeltaT.get(plane) != null) {
                    store.setPlaneDeltaT(new Time(planeDeltaT.get(plane), UNITS.SECOND), i, image);
                }
            }
        }
    }
}
Also used : PositiveInteger(ome.xml.model.primitives.PositiveInteger) Length(ome.units.quantity.Length) NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location)

Example 18 with Length

use of ome.units.quantity.Length in project bioformats by openmicroscopy.

the class ImarisHDFReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    try {
        ServiceFactory factory = new ServiceFactory();
        netcdf = factory.getInstance(NetCDFService.class);
        netcdf.setFile(id);
    } catch (DependencyException e) {
        throw new MissingLibraryException(NetCDFServiceImpl.NO_NETCDF_MSG, e);
    }
    pixelSizeX = pixelSizeY = pixelSizeZ = 1;
    emWave = new ArrayList<String>();
    exWave = new ArrayList<String>();
    channelMin = new ArrayList<String>();
    channelMax = new ArrayList<String>();
    gain = new ArrayList<String>();
    pinhole = new ArrayList<String>();
    channelName = new ArrayList<String>();
    microscopyMode = new ArrayList<String>();
    colors = new ArrayList<double[]>();
    seriesCount = 0;
    // read all of the metadata key/value pairs
    parseAttributes();
    CoreMetadata ms0 = core.get(0);
    if (seriesCount > 1) {
        for (int i = 1; i < seriesCount; i++) {
            core.add(new CoreMetadata());
        }
        for (int i = 1; i < seriesCount; i++) {
            CoreMetadata ms = core.get(i);
            String groupPath = "DataSet/ResolutionLevel_" + i + "/TimePoint_0/Channel_0";
            ms.sizeX = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeX"));
            ms.sizeY = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeY"));
            ms.sizeZ = Integer.parseInt(netcdf.getAttributeValue(groupPath + "/ImageSizeZ"));
            ms.imageCount = ms.sizeZ * getSizeC() * getSizeT();
            ms.sizeC = getSizeC();
            ms.sizeT = getSizeT();
            ms.thumbnail = true;
            if (ms.sizeZ == ms0.sizeZ && ms.sizeC == ms0.sizeC && ms.sizeT == ms0.sizeT) {
                // do not assume that all series will have the same dimensions
                // if the Z, C or T size is different, then it cannot
                // be a subresolution
                ms0.resolutionCount++;
            }
        }
    }
    ms0.imageCount = getSizeZ() * getSizeC() * getSizeT();
    ms0.thumbnail = false;
    ms0.dimensionOrder = "XYZCT";
    // determine pixel type - this isn't stored in the metadata, so we need
    // to check the pixels themselves
    int type = -1;
    Object pix = getImageData(0, 0, 0, 1, 1);
    if (pix instanceof byte[][])
        type = FormatTools.UINT8;
    else if (pix instanceof short[][])
        type = FormatTools.UINT16;
    else if (pix instanceof int[][])
        type = FormatTools.UINT32;
    else if (pix instanceof float[][])
        type = FormatTools.FLOAT;
    else if (pix instanceof double[][])
        type = FormatTools.DOUBLE;
    else {
        throw new FormatException("Unknown pixel type: " + pix);
    }
    for (int i = 0; i < core.size(); i++) {
        CoreMetadata ms = core.get(i);
        ms.pixelType = type;
        ms.dimensionOrder = "XYZCT";
        ms.rgb = false;
        ms.thumbSizeX = 128;
        ms.thumbSizeY = 128;
        ms.orderCertain = true;
        ms.littleEndian = true;
        ms.interleaved = false;
        ms.indexed = colors.size() >= getSizeC();
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String imageName = new Location(getCurrentFile()).getName();
    for (int s = 0; s < getSeriesCount(); s++) {
        store.setImageName(imageName + " Resolution Level " + (s + 1), s);
    }
    if (getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM) {
        return;
    }
    int cIndex = 0;
    for (int s = 0; s < getSeriesCount(); s++) {
        setSeries(s);
        double px = pixelSizeX, py = pixelSizeY, pz = pixelSizeZ;
        if (px == 1)
            px = (maxX - minX) / getSizeX();
        if (py == 1)
            py = (maxY - minY) / getSizeY();
        if (pz == 1)
            pz = (maxZ - minZ) / getSizeZ();
        Length sizeX = FormatTools.getPhysicalSizeX(px);
        Length sizeY = FormatTools.getPhysicalSizeY(py);
        Length sizeZ = FormatTools.getPhysicalSizeZ(pz);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, s);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, s);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, s);
        }
        for (int i = 0; i < getSizeC(); i++, cIndex++) {
            Float gainValue = null;
            Integer pinholeValue = null, emWaveValue = null, exWaveValue;
            if (cIndex < gain.size()) {
                try {
                    gainValue = new Float(gain.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < pinhole.size()) {
                try {
                    pinholeValue = new Integer(pinhole.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < emWave.size()) {
                try {
                    emWaveValue = new Integer(emWave.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < exWave.size()) {
                try {
                    exWaveValue = new Integer(exWave.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            Double minValue = null, maxValue = null;
            if (cIndex < channelMin.size()) {
                try {
                    minValue = new Double(channelMin.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (cIndex < channelMax.size()) {
                try {
                    maxValue = new Double(channelMax.get(cIndex));
                } catch (NumberFormatException e) {
                }
            }
            if (i < colors.size()) {
                double[] color = colors.get(i);
                Color realColor = new Color((int) (color[0] * 255), (int) (color[1] * 255), (int) (color[2] * 255), 255);
                store.setChannelColor(realColor, s, i);
            }
        }
    }
    setSeries(0);
}
Also used : ServiceFactory(loci.common.services.ServiceFactory) Color(ome.xml.model.primitives.Color) DependencyException(loci.common.services.DependencyException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) MissingLibraryException(loci.formats.MissingLibraryException) NetCDFService(loci.formats.services.NetCDFService) Location(loci.common.Location)

Example 19 with Length

use of ome.units.quantity.Length in project bioformats by openmicroscopy.

the class FujiReader 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, "inf")) {
        infFile = new Location(id).getAbsolutePath();
        pixelsFile = infFile.substring(0, infFile.lastIndexOf(".")) + ".img";
    } else {
        pixelsFile = new Location(id).getAbsolutePath();
        infFile = pixelsFile.substring(0, pixelsFile.lastIndexOf(".")) + ".inf";
    }
    String[] lines = DataTools.readFile(infFile).split("\r{0,1}\n");
    int bits = Integer.parseInt(lines[5]);
    CoreMetadata m = core.get(0);
    m.pixelType = FormatTools.pixelTypeFromBytes(bits / 8, false, false);
    m.sizeX = Integer.parseInt(lines[6]);
    m.sizeY = Integer.parseInt(lines[7]);
    m.sizeC = 1;
    m.sizeT = 1;
    m.sizeZ = 1;
    m.imageCount = getSizeZ() * getSizeC() * getSizeT();
    m.dimensionOrder = "XYCZT";
    for (String line : lines) {
        addGlobalMetaList("Line", line);
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String imageName = lines[1];
    String timestamp = lines[10];
    timestamp = DateTools.formatDate(timestamp, DATE_FORMAT);
    store.setImageName(imageName, 0);
    if (timestamp != null) {
        store.setImageAcquisitionDate(new Timestamp(timestamp), 0);
    }
    double physicalWidth = Double.parseDouble(lines[3]);
    double physicalHeight = Double.parseDouble(lines[4]);
    Length sizeX = FormatTools.getPhysicalSizeX(physicalWidth);
    Length sizeY = FormatTools.getPhysicalSizeY(physicalHeight);
    if (sizeX != null) {
        store.setPixelsPhysicalSizeX(sizeX, 0);
    }
    if (sizeY != null) {
        store.setPixelsPhysicalSizeY(sizeY, 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        String instrument = lines[13];
        String instrumentID = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrumentID, 0);
        store.setMicroscopeModel(instrument, 0);
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) Location(loci.common.Location)

Example 20 with Length

use of ome.units.quantity.Length in project bioformats by openmicroscopy.

the class GatanReader method parseTags.

// -- Helper methods --
/**
 * Parses Gatan DM3 tags.
 * Information on the DM3 structure found at:
 * http://rsb.info.nih.gov/ij/plugins/DM3Format.gj.html and
 * http://www-hrem.msm.cam.ac.uk/~cbb/info/dmformat/
 *
 * The basic structure is this: the file is comprised of a list of tags.
 * Each tag is either a data tag or a group tag.  Group tags are simply
 * containers for more group and data tags, where data tags contain actual
 * metadata.  Each data tag is comprised of a type (byte, short, etc.),
 * a label, and a value.
 */
private void parseTags(int numTags, String parent, String indent) throws FormatException, IOException, ParseException {
    for (int i = 0; i < numTags; i++) {
        if (in.getFilePointer() + 3 >= in.length())
            break;
        // can be 21 (data) or 20 (tag group)
        byte type = in.readByte();
        int length = in.readShort();
        // image data is in tag with type 21 and label 'Data'
        // image dimensions are in type 20 tag with 2 type 15 tags
        // bytes per pixel is in type 21 tag with label 'PixelDepth'
        String labelString = null;
        String value = null;
        if (type == VALUE) {
            labelString = in.readByteToString(length);
            skipPadding();
            skipPadding();
            // equal to '%%%%' / 623191333
            int skip = in.readInt();
            skipPadding();
            int n = in.readInt();
            skipPadding();
            int dataType = in.readInt();
            String sb = labelString;
            if (sb.length() > 32) {
                sb = sb.substring(0, 20) + "... (" + sb.length() + ")";
            }
            LOGGER.debug("{}{}: n={}, dataType={}, label={}", new Object[] { indent, i, n, dataType, sb });
            if (skip != 623191333)
                LOGGER.warn("Skip mismatch: {}", skip);
            if (n == 1) {
                if ("Dimensions".equals(parent) && labelString.length() == 0) {
                    if (adjustEndianness)
                        in.order(!in.isLittleEndian());
                    if (i == 0) {
                        core.get(0).sizeX = in.readInt();
                    } else if (i == 1)
                        core.get(0).sizeY = in.readInt();
                    else if (i == 2) {
                        core.get(0).sizeZ = in.readInt();
                    }
                    if (adjustEndianness)
                        in.order(!in.isLittleEndian());
                } else
                    value = String.valueOf(readValue(dataType));
            } else if (n == 2) {
                if (dataType == 18) {
                    // this should always be true
                    length = in.readInt();
                } else
                    LOGGER.warn("dataType mismatch: {}", dataType);
                value = in.readString(length);
            } else if (n == 3) {
                if (dataType == GROUP) {
                    // this should always be true
                    skipPadding();
                    dataType = in.readInt();
                    long dataLength = 0;
                    if (version == 4) {
                        dataLength = in.readLong();
                    } else {
                        dataLength = in.readInt();
                    }
                    length = (int) (dataLength & 0xffffffff);
                    if (labelString.equals("Data")) {
                        if (dataLength > 0) {
                            pixelOffset = in.getFilePointer();
                            in.seek(in.getFilePointer() + getNumBytes(dataType) * dataLength);
                            numPixelBytes = in.getFilePointer() - pixelOffset;
                        }
                    } else {
                        if (dataType == 10)
                            in.skipBytes(length);
                        else
                            value = in.readByteToString(length * 2);
                    }
                } else
                    LOGGER.warn("dataType mismatch: {}", dataType);
            } else {
                // this is a normal struct of simple types
                if (dataType == ARRAY) {
                    in.skipBytes(4);
                    skipPadding();
                    skipPadding();
                    int numFields = in.readInt();
                    long startFP = in.getFilePointer();
                    final StringBuilder s = new StringBuilder();
                    in.skipBytes(4);
                    skipPadding();
                    long baseFP = in.getFilePointer();
                    if (version == 4) {
                        baseFP += 4;
                    }
                    int width = version == 4 ? 16 : 8;
                    for (int j = 0; j < numFields; j++) {
                        in.seek(baseFP + j * width);
                        dataType = in.readInt();
                        in.seek(startFP + numFields * width + j * getNumBytes(dataType));
                        s.append(readValue(dataType));
                        if (j < numFields - 1)
                            s.append(", ");
                    }
                    value = s.toString();
                } else if (dataType == GROUP) {
                    // this is an array of structs
                    skipPadding();
                    dataType = in.readInt();
                    if (dataType == ARRAY) {
                        // should always be true
                        in.skipBytes(4);
                        skipPadding();
                        skipPadding();
                        int numFields = in.readInt();
                        int[] dataTypes = new int[numFields];
                        long baseFP = in.getFilePointer() + 12;
                        for (int j = 0; j < numFields; j++) {
                            in.skipBytes(4);
                            if (version == 4) {
                                in.seek(baseFP + j * 16);
                            }
                            dataTypes[j] = in.readInt();
                        }
                        skipPadding();
                        int len = in.readInt();
                        double[][] values = new double[numFields][len];
                        for (int k = 0; k < len; k++) {
                            for (int q = 0; q < numFields; q++) {
                                values[q][k] = readValue(dataTypes[q]);
                            }
                        }
                    } else
                        LOGGER.warn("dataType mismatch: {}", dataType);
                }
            }
        } else if (type == GROUP) {
            labelString = in.readByteToString(length);
            in.skipBytes(2);
            skipPadding();
            skipPadding();
            skipPadding();
            int num = in.readInt();
            LOGGER.debug("{}{}: group({}) {} {", new Object[] { indent, i, num, labelString });
            parseTags(num, labelString.isEmpty() ? parent : labelString, indent + "  ");
            LOGGER.debug("{}}", indent);
        } else {
            LOGGER.debug("{}{}: unknown type: {}", new Object[] { indent, i, type });
        }
        NumberFormat f = NumberFormat.getInstance(Locale.ENGLISH);
        if (value != null) {
            addGlobalMeta(labelString, value);
            if (parent != null && parent.equals("AnnotationGroupList")) {
                // ROI found
                ROIShape shape = new ROIShape();
                if (labelString.equals("AnnotationType")) {
                    shape.type = DataTools.parseDouble(value).intValue();
                    shapes.add(shape);
                } else if (shapes.size() > 0) {
                    shape = shapes.get(shapes.size() - 1);
                }
                if (labelString.equals("Rectangle")) {
                    String[] points = value.split(",");
                    shape.y1 = DataTools.parseDouble(points[0].trim());
                    shape.x1 = DataTools.parseDouble(points[1].trim());
                    shape.y2 = DataTools.parseDouble(points[2].trim());
                    shape.x2 = DataTools.parseDouble(points[3].trim());
                } else if (labelString.equals("Text")) {
                    shape.text = value;
                } else if (labelString.equals("ForegroundColor")) {
                    String[] colors = value.split(",");
                    int red = DataTools.parseDouble(colors[0].trim()).intValue() & 0xff;
                    int green = DataTools.parseDouble(colors[1].trim()).intValue() & 0xff;
                    int blue = DataTools.parseDouble(colors[2].trim()).intValue() & 0xff;
                    shape.strokeColor = new Color(red, green, blue, 255);
                }
            } else if (parent != null && parent.equals("TextFormat")) {
                if (labelString.equals("FontSize")) {
                    ROIShape shape = shapes.get(shapes.size() - 1);
                    shape.fontSize = FormatTools.getFontSize(DataTools.parseDouble(value).intValue());
                }
            }
            boolean validPhysicalSize = parent != null && (parent.equals("Dimension") || ((pixelSizes.size() == 4 || units.size() == 4) && parent.equals("2")));
            if (labelString.equals("Scale") && validPhysicalSize) {
                if (value.indexOf(',') == -1) {
                    pixelSizes.add(f.parse(value).doubleValue());
                }
            } else if (labelString.equals("Units") && validPhysicalSize) {
                // make sure that we don't add more units than sizes
                if (pixelSizes.size() == units.size() + 1) {
                    units.add(value);
                }
            } else if (labelString.equals("LowLimit")) {
                signed = f.parse(value).doubleValue() < 0;
            } else if (labelString.equals("Acquisition Start Time (epoch)")) {
                timestamp = f.parse(value).longValue();
            } else if (labelString.equals("Voltage")) {
                voltage = f.parse(value).doubleValue();
            } else if (labelString.equals("Microscope Info"))
                info = value;
            else if (labelString.equals("Indicated Magnification")) {
                mag = f.parse(value).doubleValue();
            } else if (labelString.equals("Gamma")) {
                gamma = f.parse(value).doubleValue();
            } else if (labelString.startsWith("xPos")) {
                final Double number = f.parse(value).doubleValue();
                posX = new Length(number, UNITS.REFERENCEFRAME);
            } else if (labelString.startsWith("yPos")) {
                final Double number = f.parse(value).doubleValue();
                posY = new Length(number, UNITS.REFERENCEFRAME);
            } else if (labelString.startsWith("Specimen position")) {
                final Double number = f.parse(value).doubleValue();
                posZ = new Length(number, UNITS.REFERENCEFRAME);
            } else if (labelString.equals("Sample Time")) {
                sampleTime = f.parse(value).doubleValue();
            } else if (labelString.equals("DataType")) {
                int pixelType = f.parse(value).intValue();
                switch(pixelType) {
                    case 1:
                        core.get(0).pixelType = FormatTools.INT16;
                        break;
                    case 10:
                        core.get(0).pixelType = FormatTools.UINT16;
                        break;
                    case 2:
                        core.get(0).pixelType = FormatTools.FLOAT;
                        break;
                    case 12:
                        core.get(0).pixelType = FormatTools.DOUBLE;
                        break;
                    case 9:
                        core.get(0).pixelType = FormatTools.INT8;
                        break;
                    case 6:
                        core.get(0).pixelType = FormatTools.UINT8;
                        break;
                    case 7:
                        core.get(0).pixelType = FormatTools.INT32;
                        break;
                    case 11:
                        core.get(0).pixelType = FormatTools.UINT32;
                }
            }
            value = null;
        }
    }
}
Also used : Color(ome.xml.model.primitives.Color) Length(ome.units.quantity.Length) NumberFormat(java.text.NumberFormat)

Aggregations

Length (ome.units.quantity.Length)154 MetadataStore (loci.formats.meta.MetadataStore)82 CoreMetadata (loci.formats.CoreMetadata)74 Timestamp (ome.xml.model.primitives.Timestamp)52 RandomAccessInputStream (loci.common.RandomAccessInputStream)48 Time (ome.units.quantity.Time)46 FormatException (loci.formats.FormatException)39 Location (loci.common.Location)34 ArrayList (java.util.ArrayList)29 IMetadata (loci.formats.meta.IMetadata)13 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)13 ServiceFactory (loci.common.services.ServiceFactory)12 IOException (java.io.IOException)11 DependencyException (loci.common.services.DependencyException)11 PositiveInteger (ome.xml.model.primitives.PositiveInteger)11 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)10 ElectricPotential (ome.units.quantity.ElectricPotential)9 Test (org.testng.annotations.Test)9 Element (org.w3c.dom.Element)9 NodeList (org.w3c.dom.NodeList)9