Search in sources :

Example 51 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class IMetadataBasedOMEModelMock method makePlate.

private void makePlate() {
    store.setPlateID(InOutCurrentTest.PLATE_ID, 0);
    store.setPlateRows(InOutCurrentTest.WELL_ROWS, 0);
    store.setPlateColumns(InOutCurrentTest.WELL_COLS, 0);
    store.setPlateRowNamingConvention(InOutCurrentTest.WELL_ROW, 0);
    store.setPlateColumnNamingConvention(InOutCurrentTest.WELL_COL, 0);
    store.setPlateAnnotationRef(InOutCurrentTest.PLATE_ANNOTATION_ID, 0, 0);
    store.setTimestampAnnotationID(InOutCurrentTest.PLATE_ANNOTATION_ID, 0);
    store.setTimestampAnnotationNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE, 0);
    store.setTimestampAnnotationValue(new Timestamp(InOutCurrentTest.PLATE_ANNOTATION_VALUE), 0);
    int wellSampleIndex = 0;
    int wellCount = 0;
    for (int row = 0; row < InOutCurrentTest.WELL_ROWS.getValue(); row++) {
        for (int col = 0; col < InOutCurrentTest.WELL_COLS.getValue(); col++) {
            store.setWellID(String.format("Well:%d_%d", row, col), 0, wellCount);
            store.setWellRow(new NonNegativeInteger(row), 0, wellCount);
            store.setWellColumn(new NonNegativeInteger(col), 0, wellCount);
            if (row == 0 && col == 0) {
                store.setLongAnnotationID(InOutCurrentTest.WELL_ANNOTATION_ID, 0);
                store.setLongAnnotationNamespace(InOutCurrentTest.GENERAL_ANNOTATION_NAMESPACE, 0);
                store.setLongAnnotationValue(InOutCurrentTest.WELL_ANNOTATION_VALUE, 0);
                store.setWellAnnotationRef(InOutCurrentTest.WELL_ANNOTATION_ID, 0, wellCount, 0);
            }
            store.setWellSampleID(String.format("WellSample:%d_%d", row, col), 0, wellCount, 0);
            store.setWellSampleIndex(new NonNegativeInteger(wellSampleIndex), 0, wellCount, 0);
            store.setWellSampleImageRef(InOutCurrentTest.IMAGE_ID, 0, wellCount, 0);
            wellSampleIndex++;
            wellCount++;
        }
    }
}
Also used : NonNegativeInteger(ome.xml.model.primitives.NonNegativeInteger) Timestamp(ome.xml.model.primitives.Timestamp)

Example 52 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class TillVisionReader method populateMetadataStore.

// -- Helper methods --
private void populateMetadataStore() throws FormatException {
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    for (int i = 0; i < getSeriesCount(); i++) {
        // populate Image data
        if (i < imageNames.size()) {
            store.setImageName(imageNames.get(i), i);
        }
        String date = i < dates.size() ? dates.get(i) : "";
        if (date != null && !date.equals("")) {
            store.setImageAcquisitionDate(new Timestamp(date), i);
        }
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 0; i < getSeriesCount(); i++) {
            for (int q = 0; q < core.get(i).imageCount; q++) {
                if (exposureTimes.get(i) != null) {
                    store.setPlaneExposureTime(new Time(exposureTimes.get(i), UNITS.SECOND), i, q);
                }
            }
            if (i < types.size()) {
                store.setExperimentID(MetadataTools.createLSID("Experiment", i), i);
                store.setExperimentType(getExperimentType(types.get(i)), i);
            }
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Time(ome.units.quantity.Time) Timestamp(ome.xml.model.primitives.Timestamp)

Example 53 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class UnisokuReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    id = new Location(id).getAbsolutePath();
    if (checkSuffix(id, "dat")) {
        id = id.substring(0, id.lastIndexOf(".")) + ".HDR";
    }
    super.initFile(id);
    CoreMetadata m = core.get(0);
    datFile = id.substring(0, id.lastIndexOf(".")) + ".DAT";
    String header = DataTools.readFile(id);
    String[] lines = header.split("\r");
    Length sizeX = null;
    Length sizeY = null;
    String imageName = null, remark = null, date = null;
    double pixelSizeX = 0d, pixelSizeY = 0d;
    for (int i = 0; i < lines.length; ) {
        lines[i] = lines[i].trim();
        if (lines[i].startsWith(":")) {
            String key = lines[i++];
            final StringBuilder data = new StringBuilder();
            while (i < lines.length && !lines[i].trim().startsWith(":")) {
                data.append(" ");
                data.append(lines[i++].trim());
            }
            String value = data.toString().trim();
            addGlobalMeta(key, value);
            String[] v = value.split(" ");
            if (key.equals(":data volume(x*y)")) {
                m.sizeX = Integer.parseInt(v[0]);
                m.sizeY = Integer.parseInt(v[1]);
            } else if (key.equals(":date; time")) {
                date = DateTools.formatDate(value, "MM/dd/yy HH:mm:ss");
            } else if (key.startsWith(":ascii flag; data type")) {
                value = value.substring(value.indexOf(' ') + 1);
                int type = Integer.parseInt(value);
                boolean signed = type % 2 == 1;
                int bytes = type / 2;
                m.pixelType = FormatTools.pixelTypeFromBytes(bytes, signed, bytes == 4);
            } else if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
                if (key.equals(":sample name")) {
                    imageName = value;
                } else if (key.equals(":remark")) {
                    remark = value;
                } else if (key.startsWith(":x_data ->")) {
                    String unit = v[0];
                    pixelSizeX = Double.parseDouble(v[2]) - Double.parseDouble(v[1]);
                    pixelSizeX /= getSizeX();
                    sizeX = FormatTools.getPhysicalSizeX(pixelSizeX, unit);
                } else if (key.startsWith(":y_data ->")) {
                    String unit = v[0];
                    pixelSizeY = Double.parseDouble(v[2]) - Double.parseDouble(v[1]);
                    pixelSizeY /= getSizeY();
                    sizeY = FormatTools.getPhysicalSizeY(pixelSizeY, unit);
                }
            }
        }
    }
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.rgb = false;
    m.interleaved = false;
    m.indexed = false;
    m.dimensionOrder = "XYZCT";
    m.littleEndian = true;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    store.setImageName(imageName, 0);
    if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        store.setImageDescription(remark, 0);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 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 54 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class DicomReader 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(true);
    CoreMetadata m = core.get(0);
    // look for companion files
    attachCompanionFiles();
    helper = new DicomReader();
    helper.setGroupFiles(false);
    m.littleEndian = true;
    location = 0;
    isJPEG = false;
    isRLE = false;
    bigEndianTransferSyntax = false;
    oddLocations = false;
    inSequence = false;
    bitsPerPixel = 0;
    elementLength = 0;
    vr = 0;
    lut = null;
    offsets = null;
    inverted = false;
    // some DICOM files have a 128 byte header followed by a 4 byte identifier
    LOGGER.info("Verifying DICOM format");
    MetadataLevel level = getMetadataOptions().getMetadataLevel();
    in.seek(128);
    if (in.readString(4).equals("DICM")) {
        if (level != MetadataLevel.MINIMUM) {
            // header exists, so we'll read it
            in.seek(0);
            addSeriesMeta("Header information", in.readString(128));
            in.skipBytes(4);
        }
        location = 128;
    } else
        in.seek(0);
    LOGGER.info("Reading tags");
    long baseOffset = 0;
    boolean decodingTags = true;
    boolean signed = false;
    String currentType = "";
    while (decodingTags) {
        if (in.getFilePointer() + 4 >= in.length()) {
            break;
        }
        LOGGER.debug("Reading tag from {}", in.getFilePointer());
        int tag = getNextTag(in);
        if (elementLength <= 0)
            continue;
        oddLocations = (location & 1) != 0;
        LOGGER.debug("  tag={} len={} fp=", new Object[] { tag, elementLength, in.getFilePointer() });
        String s = null;
        switch(tag) {
            case TRANSFER_SYNTAX_UID:
                // this tag can indicate which compression scheme is used
                s = in.readString(elementLength);
                addInfo(tag, s);
                if (s.startsWith("1.2.840.10008.1.2.4.9"))
                    isJP2K = true;
                else if (s.startsWith("1.2.840.10008.1.2.4"))
                    isJPEG = true;
                else if (s.startsWith("1.2.840.10008.1.2.5"))
                    isRLE = true;
                else if (s.equals("1.2.8.10008.1.2.1.99"))
                    isDeflate = true;
                else if (s.indexOf("1.2.4") > -1 || s.indexOf("1.2.5") > -1) {
                    throw new UnsupportedCompressionException("Sorry, compression type " + s + " not supported");
                }
                if (s.indexOf("1.2.840.10008.1.2.2") >= 0) {
                    bigEndianTransferSyntax = true;
                }
                break;
            case NUMBER_OF_FRAMES:
                s = in.readString(elementLength);
                addInfo(tag, s);
                double frames = Double.parseDouble(s);
                if (frames > 1.0)
                    imagesPerFile = (int) frames;
                break;
            case SAMPLES_PER_PIXEL:
                addInfo(tag, in.readShort());
                break;
            case PLANAR_CONFIGURATION:
                int config = in.readShort();
                m.interleaved = config == 0;
                addInfo(tag, config);
                break;
            case ROWS:
                int y = in.readShort();
                if (y > getSizeY()) {
                    m.sizeY = y;
                }
                addInfo(tag, getSizeY());
                break;
            case COLUMNS:
                int x = in.readShort();
                if (x > getSizeX()) {
                    m.sizeX = x;
                }
                addInfo(tag, getSizeX());
                break;
            case PHOTOMETRIC_INTERPRETATION:
            case PIXEL_SPACING:
            case SLICE_SPACING:
            case RESCALE_INTERCEPT:
            case WINDOW_CENTER:
                String winCenter = in.readString(elementLength);
                if (winCenter.trim().length() == 0)
                    centerPixelValue = -1;
                else {
                    try {
                        centerPixelValue = new Double(winCenter).intValue();
                    } catch (NumberFormatException e) {
                        centerPixelValue = -1;
                    }
                }
                addInfo(tag, winCenter);
                break;
            case RESCALE_SLOPE:
                addInfo(tag, in.readString(elementLength));
                break;
            case BITS_ALLOCATED:
                if (bitsPerPixel == 0)
                    bitsPerPixel = in.readShort();
                else
                    in.skipBytes(2);
                addInfo(tag, bitsPerPixel);
                break;
            case PIXEL_REPRESENTATION:
            case PIXEL_SIGN:
                short ss = in.readShort();
                signed = ss == 1;
                addInfo(tag, ss);
                break;
            case 537262910:
            case WINDOW_WIDTH:
                String t = in.readString(elementLength);
                if (t.trim().length() == 0)
                    maxPixelRange = -1;
                else {
                    try {
                        maxPixelRange = new Double(t.trim()).intValue();
                    } catch (NumberFormatException e) {
                        maxPixelRange = -1;
                    }
                }
                addInfo(tag, t);
                break;
            case PIXEL_DATA:
            case ITEM:
            case 0xffee000:
                if (elementLength != 0) {
                    baseOffset = in.getFilePointer();
                    addInfo(tag, location);
                    decodingTags = false;
                } else
                    addInfo(tag, null);
                break;
            case 0x7f880010:
                if (elementLength != 0) {
                    baseOffset = location + 4;
                    decodingTags = false;
                }
                break;
            case 0x7fe00000:
                in.skipBytes(elementLength);
                break;
            case 0:
                in.seek(in.getFilePointer() - 4);
                break;
            case 0x41430:
                currentType = getHeaderInfo(tag, s).trim();
                break;
            case 0x41500:
                if (currentType.equals("IMAGE")) {
                    if (fileList == null) {
                        fileList = new HashMap<Integer, List<String>>();
                    }
                    int seriesIndex = 0;
                    if (originalInstance != null) {
                        try {
                            seriesIndex = Integer.parseInt(originalInstance);
                        } catch (NumberFormatException e) {
                            LOGGER.debug("Could not parse instance number: {}", originalInstance);
                        }
                    }
                    if (fileList.get(seriesIndex) == null) {
                        fileList.put(seriesIndex, new ArrayList<String>());
                    }
                    fileList.get(seriesIndex).add(getHeaderInfo(tag, s).trim());
                } else {
                    companionFiles.add(getHeaderInfo(tag, s).trim());
                }
                currentType = "";
                break;
            default:
                long oldfp = in.getFilePointer();
                addInfo(tag, s);
                in.seek(oldfp + elementLength);
        }
        if (in.getFilePointer() >= (in.length() - 4)) {
            decodingTags = false;
        }
    }
    if (imagesPerFile == 0)
        imagesPerFile = 1;
    if (id.endsWith("DICOMDIR")) {
        String parent = new Location(currentId).getAbsoluteFile().getParent();
        for (int q = 0; q < fileList.size(); q++) {
            Integer[] fileKeys = fileList.keySet().toArray(new Integer[0]);
            for (int i = 0; i < fileList.get(fileKeys[q]).size(); i++) {
                String file = fileList.get(fileKeys[q]).get(i);
                file = file.replace('\\', File.separatorChar);
                file = file.replaceAll("/", File.separator);
                fileList.get(fileKeys[q]).set(i, parent + File.separator + file);
            }
        }
        for (int i = 0; i < companionFiles.size(); i++) {
            String file = companionFiles.get(i);
            file = file.replace('\\', File.separatorChar);
            file = file.replaceAll("/", File.separator);
            companionFiles.set(i, parent + File.separator + file);
        }
        companionFiles.add(new Location(currentId).getAbsolutePath());
        initFile(fileList.get(0).get(0));
        return;
    }
    m.bitsPerPixel = bitsPerPixel;
    while (bitsPerPixel % 8 != 0) bitsPerPixel++;
    if (bitsPerPixel == 24 || bitsPerPixel == 48) {
        bitsPerPixel /= 3;
        m.bitsPerPixel /= 3;
    }
    m.pixelType = FormatTools.pixelTypeFromBytes(bitsPerPixel / 8, signed, false);
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    int plane = getSizeX() * getSizeY() * (lut == null ? getSizeC() : 1) * bpp;
    LOGGER.info("Calculating image offsets");
    // calculate the offset to each plane
    in.seek(baseOffset - 12);
    int len = in.readInt();
    if (len >= 0 && len + in.getFilePointer() < in.length()) {
        in.skipBytes(len);
        int check = in.readShort() & 0xffff;
        if (check == 0xfffe) {
            baseOffset = in.getFilePointer() + 2;
        }
    }
    offsets = new long[imagesPerFile];
    for (int i = 0; i < imagesPerFile; i++) {
        if (isRLE) {
            if (i == 0)
                in.seek(baseOffset);
            else {
                in.seek(offsets[i - 1]);
                CodecOptions options = new CodecOptions();
                options.maxBytes = plane / bpp;
                for (int q = 0; q < bpp; q++) {
                    new PackbitsCodec().decompress(in, options);
                    while (in.read() == 0) ;
                    in.seek(in.getFilePointer() - 1);
                }
            }
            in.skipBytes(i == 0 ? 64 : 53);
            while (in.read() == 0) ;
            offsets[i] = in.getFilePointer() - 1;
        } else if (isJPEG || isJP2K) {
            // scan for next JPEG magic byte sequence
            if (i == 0)
                offsets[i] = baseOffset;
            else
                offsets[i] = offsets[i - 1] + 3;
            byte secondCheck = isJPEG ? (byte) 0xd8 : (byte) 0x4f;
            in.seek(offsets[i]);
            byte[] buf = new byte[8192];
            int n = in.read(buf);
            boolean found = false;
            while (!found) {
                for (int q = 0; q < n - 2; q++) {
                    if (buf[q] == (byte) 0xff && buf[q + 1] == secondCheck && buf[q + 2] == (byte) 0xff) {
                        if (isJPEG || (isJP2K && buf[q + 3] == 0x51)) {
                            found = true;
                            offsets[i] = in.getFilePointer() + q - n;
                            break;
                        }
                    }
                }
                if (!found) {
                    for (int q = 0; q < 4; q++) {
                        buf[q] = buf[buf.length + q - 4];
                    }
                    n = in.read(buf, 4, buf.length - 4) + 4;
                }
            }
        } else
            offsets[i] = baseOffset + plane * i;
    }
    makeFileList();
    LOGGER.info("Populating metadata");
    int seriesCount = fileList.size();
    Integer[] keys = fileList.keySet().toArray(new Integer[0]);
    Arrays.sort(keys);
    if (seriesCount > 1) {
        core.clear();
    }
    for (int i = 0; i < seriesCount; i++) {
        if (seriesCount == 1) {
            CoreMetadata ms = core.get(i);
            ms.sizeZ = imagesPerFile * fileList.get(keys[i]).size();
            if (ms.sizeC == 0)
                ms.sizeC = 1;
            ms.rgb = ms.sizeC > 1;
            ms.sizeT = 1;
            ms.dimensionOrder = "XYCZT";
            ms.metadataComplete = true;
            ms.falseColor = false;
            if (isRLE)
                core.get(i).interleaved = false;
            ms.imageCount = ms.sizeZ;
        } else {
            helper.close();
            helper.setId(fileList.get(keys[i]).get(0));
            CoreMetadata ms = helper.getCoreMetadataList().get(0);
            ms.sizeZ *= fileList.get(keys[i]).size();
            ms.imageCount = ms.sizeZ;
            core.add(ms);
        }
    }
    // The metadata store we're working with.
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    String stamp = null;
    if (date != null && time != null) {
        stamp = date + " " + time;
        stamp = DateTools.formatDate(stamp, "yyyy.MM.dd HH:mm:ss", ".");
    }
    if (stamp == null || stamp.trim().equals(""))
        stamp = null;
    for (int i = 0; i < core.size(); i++) {
        if (stamp != null)
            store.setImageAcquisitionDate(new Timestamp(stamp), i);
        store.setImageName("Series " + i, i);
    }
    if (level != MetadataLevel.MINIMUM) {
        for (int i = 0; i < core.size(); i++) {
            store.setImageDescription(imageType, i);
            // all physical sizes were stored in mm, so must be converted to um
            if (pixelSizeX != null) {
                Length x = FormatTools.getPhysicalSizeX(new Double(pixelSizeX), UNITS.MILLIMETER);
                if (x != null) {
                    store.setPixelsPhysicalSizeX(x, i);
                }
            }
            if (pixelSizeY != null) {
                Length y = FormatTools.getPhysicalSizeY(new Double(pixelSizeY), UNITS.MILLIMETER);
                if (y != null) {
                    store.setPixelsPhysicalSizeY(y, i);
                }
            }
            if (pixelSizeZ != null) {
                Length z = FormatTools.getPhysicalSizeZ(new Double(pixelSizeZ), UNITS.MILLIMETER);
                if (z != null) {
                    store.setPixelsPhysicalSizeZ(z, i);
                }
            }
            for (int p = 0; p < getImageCount(); p++) {
                if (p < positionX.size()) {
                    if (positionX.get(p) != null) {
                        Length x = new Length(positionX.get(p), UNITS.MM);
                        if (x != null) {
                            store.setPlanePositionX(x, 0, p);
                        }
                    }
                }
                if (p < positionY.size()) {
                    if (positionY.get(p) != null) {
                        Length y = new Length(positionY.get(p), UNITS.MM);
                        if (y != null) {
                            store.setPlanePositionY(y, 0, p);
                        }
                    }
                }
                if (p < positionZ.size()) {
                    if (positionZ.get(p) != null) {
                        Length z = new Length(positionZ.get(p), UNITS.MM);
                        if (z != null) {
                            store.setPlanePositionZ(z, 0, p);
                        }
                    }
                }
            }
        }
    }
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) UnsupportedCompressionException(loci.formats.UnsupportedCompressionException) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp) PackbitsCodec(loci.formats.codec.PackbitsCodec) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) ArrayList(java.util.ArrayList) List(java.util.List) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Example 55 with Timestamp

use of ome.xml.model.primitives.Timestamp in project bioformats by openmicroscopy.

the class FakeReader method fillAcquisitionDate.

private void fillAcquisitionDate(MetadataStore store, String date, int imageIndex) {
    if (date == null)
        return;
    if (DateTools.getTime(date, DateTools.FILENAME_FORMAT) != -1) {
        Timestamp stamp = new Timestamp(DateTools.formatDate(date, DateTools.FILENAME_FORMAT));
        store.setImageAcquisitionDate(stamp, imageIndex);
    }
}
Also used : Timestamp(ome.xml.model.primitives.Timestamp)

Aggregations

Timestamp (ome.xml.model.primitives.Timestamp)76 MetadataStore (loci.formats.meta.MetadataStore)54 Length (ome.units.quantity.Length)52 CoreMetadata (loci.formats.CoreMetadata)44 Time (ome.units.quantity.Time)33 Location (loci.common.Location)28 RandomAccessInputStream (loci.common.RandomAccessInputStream)28 FormatException (loci.formats.FormatException)23 ArrayList (java.util.ArrayList)20 IFD (loci.formats.tiff.IFD)11 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)10 TiffParser (loci.formats.tiff.TiffParser)9 IOException (java.io.IOException)8 IFDList (loci.formats.tiff.IFDList)8 PositiveInteger (ome.xml.model.primitives.PositiveInteger)8 Temperature (ome.units.quantity.Temperature)7 Hashtable (java.util.Hashtable)4 StringTokenizer (java.util.StringTokenizer)4 Frequency (ome.units.quantity.Frequency)4 File (java.io.File)3