Search in sources :

Example 26 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class HISReader 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);
    in.skipBytes(14);
    int nSeries = in.readShort();
    pixelOffset = new long[nSeries];
    String[] date = new String[nSeries];
    String[] binning = new String[nSeries];
    double[] offset = new double[nSeries];
    double[] exposureTime = new double[nSeries];
    boolean adjustedBitDepth = false;
    in.seek(0);
    core.clear();
    for (int i = 0; i < nSeries; i++) {
        CoreMetadata ms = new CoreMetadata();
        core.add(ms);
        String checkString = in.readString(2);
        if (!checkString.equals("IM") && i > 0) {
            if (getBitsPerPixel() == 12) {
                core.get(i - 1).bitsPerPixel = 16;
                int prevSkip = (getSizeX() * getSizeY() * getSizeC() * 12) / 8;
                int totalBytes = FormatTools.getPlaneSize(this);
                in.skipBytes(totalBytes - prevSkip);
                adjustedBitDepth = true;
            }
        }
        setSeries(i);
        int commentBytes = in.readShort();
        ms.sizeX = in.readShort();
        ms.sizeY = in.readShort();
        in.skipBytes(4);
        int dataType = in.readShort();
        switch(dataType) {
            case 1:
                ms.pixelType = FormatTools.UINT8;
                break;
            case 2:
                ms.pixelType = FormatTools.UINT16;
                break;
            case 6:
                ms.pixelType = FormatTools.UINT16;
                ms.bitsPerPixel = adjustedBitDepth ? 16 : 12;
                break;
            case 11:
                ms.pixelType = FormatTools.UINT8;
                ms.sizeC = 3;
                break;
            case 12:
                ms.pixelType = FormatTools.UINT16;
                ms.sizeC = 3;
                break;
            case 14:
                ms.pixelType = FormatTools.UINT16;
                ms.sizeC = 3;
                ms.bitsPerPixel = adjustedBitDepth ? 16 : 12;
                break;
        }
        in.skipBytes(50);
        String comment = in.readString(commentBytes);
        if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
            String[] data = comment.split(";");
            for (String token : data) {
                int eq = token.indexOf('=');
                if (eq != -1) {
                    String key = token.substring(0, eq);
                    String value = token.substring(eq + 1);
                    addSeriesMeta(key, value);
                    if (key.equals("vDate")) {
                        date[i] = value;
                    } else if (key.equals("vTime")) {
                        date[i] += " " + value;
                        date[i] = DateTools.formatDate(date[i], "yyyy/MM/dd HH:mm:ss");
                    } else if (key.equals("vOffset")) {
                        offset[i] = Double.parseDouble(value);
                    } else if (key.equals("vBinX")) {
                        binning[i] = value;
                    } else if (key.equals("vBinY")) {
                        binning[i] += "x" + value;
                    } else if (key.equals("vExpTim1")) {
                        exposureTime[i] = Double.parseDouble(value) * 100;
                    }
                }
            }
        }
        pixelOffset[i] = in.getFilePointer();
        ms.littleEndian = true;
        if (ms.sizeC == 0)
            ms.sizeC = 1;
        ms.sizeT = 1;
        ms.sizeZ = 1;
        ms.imageCount = 1;
        ms.rgb = ms.sizeC > 1;
        ms.interleaved = isRGB();
        ms.dimensionOrder = "XYCZT";
        in.skipBytes((getSizeX() * getSizeY() * getSizeC() * getBitsPerPixel()) / 8);
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    String instrumentID = MetadataTools.createLSID("Instrument", 0);
    store.setInstrumentID(instrumentID, 0);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 0; i < nSeries; i++) {
            store.setImageInstrumentRef(instrumentID, i);
            if (date[i] != null) {
                store.setImageAcquisitionDate(new Timestamp(date[i]), i);
            }
            store.setPlaneExposureTime(new Time(exposureTime[i], UNITS.SECOND), i, 0);
            String detectorID = MetadataTools.createLSID("Detector", 0, i);
            store.setDetectorID(detectorID, 0, i);
            store.setDetectorOffset(offset[i], 0, i);
            store.setDetectorType(getDetectorType("Other"), 0, i);
            store.setDetectorSettingsID(detectorID, i, 0);
            store.setDetectorSettingsBinning(getBinning(binning[i]), i, 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Time(ome.units.quantity.Time) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp)

Example 27 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class HamamatsuVMSReader 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);
    IniParser parser = new IniParser();
    IniList layout = parser.parseINI(new BufferedReader(new InputStreamReader(in, Constants.ENCODING)));
    IniTable slideInfo = layout.getTable("Virtual Microscope Specimen");
    int nLayers = Integer.parseInt(slideInfo.get("NoLayers"));
    nRows = Integer.parseInt(slideInfo.get("NoJpegRows"));
    nCols = Integer.parseInt(slideInfo.get("NoJpegColumns"));
    String imageFile = slideInfo.get("ImageFile");
    mapFile = slideInfo.get("MapFile");
    String optimisationFile = slideInfo.get("OptimisationFile");
    macroFile = slideInfo.get("MacroImage");
    Double physicalWidth = new Double(slideInfo.get("PhysicalWidth"));
    Double physicalHeight = new Double(slideInfo.get("PhysicalHeight"));
    Double magnification = new Double(slideInfo.get("SourceLens"));
    Double macroWidth = new Double(slideInfo.get("PhysicalMacroWidth"));
    Double macroHeight = new Double(slideInfo.get("PhysicalMacroHeight"));
    for (String key : slideInfo.keySet()) {
        addGlobalMeta(key, slideInfo.get(key));
    }
    Location dir = new Location(id).getAbsoluteFile().getParentFile();
    if (imageFile != null) {
        imageFile = new Location(dir, imageFile).getAbsolutePath();
        files.add(imageFile);
    }
    tileFiles = new String[nLayers][nRows][nCols];
    tileFiles[0][0][0] = imageFile;
    for (int layer = 0; layer < nLayers; layer++) {
        for (int row = 0; row < nRows; row++) {
            for (int col = 0; col < nCols; col++) {
                String f = slideInfo.get("ImageFile(" + col + "," + row + ")");
                if (f != null) {
                    f = new Location(dir, f).getAbsolutePath();
                    tileFiles[layer][row][col] = f;
                    files.add(f);
                }
            }
        }
    }
    if (mapFile != null) {
        mapFile = new Location(dir, mapFile).getAbsolutePath();
        files.add(mapFile);
    }
    if (optimisationFile != null) {
        optimisationFile = new Location(dir, optimisationFile).getAbsolutePath();
        files.add(optimisationFile);
    }
    if (macroFile != null) {
        macroFile = new Location(dir, macroFile).getAbsolutePath();
        files.add(macroFile);
    }
    int seriesCount = 3;
    core.clear();
    for (int i = 0; i < seriesCount; i++) {
        String file = null;
        switch(i) {
            case 0:
                file = tileFiles[0][nRows - 1][nCols - 1];
                break;
            case 1:
                file = macroFile;
                break;
            case 2:
                file = mapFile;
                break;
        }
        int[] dims;
        try (RandomAccessInputStream s = new RandomAccessInputStream(file);
            JPEGTileDecoder decoder = new JPEGTileDecoder()) {
            dims = decoder.preprocess(s);
        }
        CoreMetadata m = new CoreMetadata();
        if (i == 0) {
            m.sizeX = (MAX_JPEG_SIZE * (nCols - 1)) + dims[0];
            m.sizeY = (MAX_JPEG_SIZE * (nRows - 1)) + dims[1];
        } else {
            m.sizeX = dims[0];
            m.sizeY = dims[1];
        }
        m.sizeZ = 1;
        m.sizeC = 3;
        m.sizeT = 1;
        m.rgb = true;
        m.imageCount = 1;
        m.dimensionOrder = "XYCZT";
        m.pixelType = FormatTools.UINT8;
        m.interleaved = m.sizeX > MAX_SIZE && m.sizeY > MAX_SIZE;
        m.thumbnail = i > 0;
        core.add(m);
    }
    CoreMetadata ms0 = core.get(0);
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    String path = new Location(currentId).getAbsoluteFile().getName();
    store.setImageName(path + " full resolution", 0);
    store.setImageName(path + " macro", 1);
    store.setImageName(path + " map", 2);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Length sizeX = FormatTools.getPhysicalSizeX(physicalWidth / ms0.sizeX);
        Length sizeY = FormatTools.getPhysicalSizeY(physicalHeight / ms0.sizeY);
        Length macroSizeX = FormatTools.getPhysicalSizeX(macroWidth / core.get(1).sizeX);
        Length macroSizeY = FormatTools.getPhysicalSizeY(macroHeight / core.get(1).sizeY);
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        if (macroSizeX != null) {
            store.setPixelsPhysicalSizeX(macroSizeX, 1);
        }
        if (macroSizeY != null) {
            store.setPixelsPhysicalSizeY(macroSizeY, 1);
        }
        String instrumentID = MetadataTools.createLSID("Instrument", 0);
        store.setInstrumentID(instrumentID, 0);
        store.setImageInstrumentRef(instrumentID, 0);
        String objectiveID = MetadataTools.createLSID("Objective", 0, 0);
        store.setObjectiveID(objectiveID, 0, 0);
        store.setObjectiveNominalMagnification(magnification, 0, 0);
        store.setObjectiveSettingsID(objectiveID, 0);
    }
}
Also used : IniParser(loci.common.IniParser) InputStreamReader(java.io.InputStreamReader) IniList(loci.common.IniList) CoreMetadata(loci.formats.CoreMetadata) MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) IniTable(loci.common.IniTable) JPEGTileDecoder(loci.formats.codec.JPEGTileDecoder) BufferedReader(java.io.BufferedReader) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Example 28 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class I2IReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
public void initFile(String id) throws FormatException, IOException {
    super.initFile(id);
    in = new RandomAccessInputStream(id);
    CoreMetadata m = core.get(0);
    char pixelType = (char) in.readByte();
    switch(pixelType) {
        case 'I':
            m.pixelType = FormatTools.INT16;
            break;
        case 'R':
            m.pixelType = FormatTools.FLOAT;
            break;
        case 'C':
            throw new FormatException("Complex pixel data not yet supported");
        default:
            throw new FormatException("Invalid pixel type: " + pixelType);
    }
    if ((char) in.readByte() != ' ') {
        throw new FormatException("Expected space after pixel type character");
    }
    m.sizeX = getDimension(in);
    m.sizeY = getDimension(in);
    m.sizeZ = getDimension(in);
    m.littleEndian = (char) in.readByte() != 'B';
    in.order(isLittleEndian());
    short minPixelValue = in.readShort();
    short maxPixelValue = in.readShort();
    short xCoordinate = in.readShort();
    short yCoordinate = in.readShort();
    int n = in.readShort();
    // reserved for future use
    in.skipBytes(33);
    for (int i = 0; i < 15; i++) {
        String history = in.readString(64);
        addGlobalMetaList("Image history", history.trim());
    }
    addGlobalMeta("Minimum intensity value", minPixelValue);
    addGlobalMeta("Maximum intensity value", maxPixelValue);
    addGlobalMeta("Image position X", xCoordinate);
    addGlobalMeta("Image position Y", yCoordinate);
    // needs to be adjusted to reflect the true Z count
    if (n > 0) {
        m.sizeZ /= n;
    }
    // the user defines what the N dimension means
    // in practice, it could be timepoints, channels, etc. but we have no
    // way of knowing based on the file metadata
    m.sizeT = n;
    m.imageCount = getSizeZ() * getSizeT();
    m.sizeC = 1;
    m.rgb = false;
    m.dimensionOrder = "XYZTC";
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 29 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class PCORAWReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    if (checkSuffix(id, "rec")) {
        paramFile = new Location(id).getAbsolutePath();
        String base = new Location(id).getAbsoluteFile().getAbsolutePath();
        base = base.substring(0, base.lastIndexOf("."));
        id = base + ".pcoraw";
        if (!new Location(id).exists()) {
            throw new FormatException("Could not find image file.");
        }
    }
    super.initFile(id);
    imageFile = new Location(id).getAbsolutePath();
    reader.close();
    reader.setMetadataStore(getMetadataStore());
    reader.setId(id);
    core = reader.getCoreMetadataList();
    metadata = reader.getGlobalMetadata();
    in = new RandomAccessInputStream(id);
    try {
        if (in.length() >= Math.pow(2, 32)) {
            // even though BigTIFF is likely being used, the offsets
            // are still recorded as though only 32 bits are available
            long add = 0;
            long prevOffset = 0;
            for (IFD ifd : reader.ifds) {
                long[] offsets = ifd.getStripOffsets();
                for (int i = 0; i < offsets.length; i++) {
                    offsets[i] += add;
                    if (offsets[i] < prevOffset) {
                        add += 0x100000000L;
                        offsets[i] += 0x100000000L;
                    }
                    prevOffset = offsets[i];
                }
                ifd.put(IFD.STRIP_OFFSETS, offsets);
            }
        }
    } finally {
        in.close();
    }
    if (paramFile == null) {
        String base = imageFile.substring(0, imageFile.lastIndexOf("."));
        base += ".rec";
        if (new Location(base).exists()) {
            paramFile = base;
        }
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, true);
    if (paramFile != null) {
        // parse extra metadata from the parameter file
        store.setInstrumentID(MetadataTools.createLSID("Instrument", 0), 0);
        String detector = MetadataTools.createLSID("Detector", 0, 0);
        store.setDetectorID(detector, 0, 0);
        String[] lines = DataTools.readFile(paramFile).split("\n");
        for (int i = 0; i < lines.length; i++) {
            String line = lines[i];
            int sep = line.indexOf(':');
            if (sep < 0) {
                continue;
            }
            String key = line.substring(0, sep).trim();
            String value = line.substring(sep + 1).trim();
            addGlobalMeta(key, value);
            if (key.equals("Exposure / Delay")) {
                // set the exposure time
                String exp = value.substring(0, value.indexOf(' '));
                Double parsedExp = new Double(exp);
                Time exposure = null;
                if (parsedExp != null) {
                    exposure = new Time(parsedExp / 1000, UNITS.SECOND);
                }
                for (int plane = 0; plane < getImageCount(); plane++) {
                    store.setPlaneExposureTime(exposure, 0, plane);
                }
            } else if (key.equals("Camera serial number")) {
                // set the serial number
                store.setDetectorSerialNumber(value, 0, 0);
            } else if (key.equals("Binning horz./vert.")) {
                store.setDetectorSettingsID(detector, 0, 0);
                value = value.charAt(1) + value;
                value = value.substring(0, 3);
                store.setDetectorSettingsBinning(getBinning(value), 0, 0);
            } else if (key.equals("Comment")) {
                final StringBuilder description = new StringBuilder();
                for (int j = i + 1; j < lines.length; j++) {
                    lines[j] = lines[j].trim();
                    if (lines[j].length() > 0) {
                        description.append(lines[j]);
                        description.append(" ");
                    }
                }
                store.setImageDescription(description.toString().trim(), 0);
                break;
            }
        }
    }
}
Also used : IFD(loci.formats.tiff.IFD) Time(ome.units.quantity.Time) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) Location(loci.common.Location)

Example 30 with MetadataStore

use of loci.formats.meta.MetadataStore in project bioformats by openmicroscopy.

the class PDSReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    if (!checkSuffix(id, "hdr")) {
        String headerFile = id.substring(0, id.lastIndexOf(".")) + ".hdr";
        if (!new Location(headerFile).exists()) {
            headerFile = id.substring(0, id.lastIndexOf(".")) + ".HDR";
            if (!new Location(headerFile).exists()) {
                throw new FormatException("Could not find matching .hdr file.");
            }
        }
        initFile(headerFile);
        return;
    }
    super.initFile(id);
    String[] headerData = DataTools.readFile(id).split("\r\n");
    if (headerData.length == 1) {
        headerData = headerData[0].split("\r");
    }
    Length xPos = null, yPos = null;
    Double deltaX = null, deltaY = null;
    String date = null;
    CoreMetadata m = core.get(0);
    for (String line : headerData) {
        int eq = line.indexOf('=');
        if (eq < 0)
            continue;
        int end = line.indexOf('/');
        if (end < 0)
            end = line.length();
        String key = line.substring(0, eq).trim();
        String value = line.substring(eq + 1, end).trim();
        if (key.equals("NXP")) {
            m.sizeX = Integer.parseInt(value);
        } else if (key.equals("NYP")) {
            m.sizeY = Integer.parseInt(value);
        } else if (key.equals("XPOS")) {
            final Double number = Double.valueOf(value);
            xPos = new Length(number, UNITS.REFERENCEFRAME);
            addGlobalMeta("X position for position #1", xPos);
        } else if (key.equals("YPOS")) {
            final Double number = Double.valueOf(value);
            yPos = new Length(number, UNITS.REFERENCEFRAME);
            addGlobalMeta("Y position for position #1", yPos);
        } else if (key.equals("SIGNX")) {
            reverseX = value.replaceAll("'", "").trim().equals("-");
        } else if (key.equals("SIGNY")) {
            reverseY = value.replaceAll("'", "").trim().equals("-");
        } else if (key.equals("DELTAX")) {
            deltaX = new Double(value);
        } else if (key.equals("DELTAY")) {
            deltaY = new Double(value);
        } else if (key.equals("COLOR")) {
            int color = Integer.parseInt(value);
            if (color == 4) {
                m.sizeC = 3;
                m.rgb = true;
            } else {
                m.sizeC = 1;
                m.rgb = false;
                lutIndex = color - 1;
                m.indexed = lutIndex >= 0;
            }
        } else if (key.equals("SCAN TIME")) {
            long modTime = new Location(currentId).getAbsoluteFile().lastModified();
            String year = DateTools.convertDate(modTime, DateTools.UNIX, "yyyy");
            date = value.replaceAll("'", "") + " " + year;
            date = DateTools.formatDate(date, DATE_FORMAT);
        } else if (key.equals("FILE REC LEN")) {
            recordWidth = Integer.parseInt(value) / 2;
        }
        addGlobalMeta(key, value);
    }
    m.sizeZ = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.dimensionOrder = "XYCZT";
    m.pixelType = FormatTools.UINT16;
    m.littleEndian = true;
    String base = currentId.substring(0, currentId.lastIndexOf("."));
    pixelsFile = base + ".IMG";
    if (!new Location(pixelsFile).exists()) {
        pixelsFile = base + ".img";
    }
    boolean minimumMetadata = getMetadataOptions().getMetadataLevel() == MetadataLevel.MINIMUM;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, !minimumMetadata);
    if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
    }
    if (!minimumMetadata) {
        store.setPlanePositionX(xPos, 0, 0);
        store.setPlanePositionY(yPos, 0, 0);
        Length sizeX = FormatTools.getPhysicalSizeX(deltaX);
        Length sizeY = FormatTools.getPhysicalSizeY(deltaY);
        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) FormatException(loci.formats.FormatException) Location(loci.common.Location)

Aggregations

MetadataStore (loci.formats.meta.MetadataStore)180 CoreMetadata (loci.formats.CoreMetadata)130 RandomAccessInputStream (loci.common.RandomAccessInputStream)97 Length (ome.units.quantity.Length)82 FormatException (loci.formats.FormatException)66 Timestamp (ome.xml.model.primitives.Timestamp)54 Location (loci.common.Location)51 Time (ome.units.quantity.Time)41 ArrayList (java.util.ArrayList)34 IFD (loci.formats.tiff.IFD)21 DependencyException (loci.common.services.DependencyException)16 IOException (java.io.IOException)15 TiffParser (loci.formats.tiff.TiffParser)15 NonNegativeInteger (ome.xml.model.primitives.NonNegativeInteger)15 ServiceFactory (loci.common.services.ServiceFactory)14 PositiveInteger (ome.xml.model.primitives.PositiveInteger)13 IFDList (loci.formats.tiff.IFDList)12 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)11 ServiceException (loci.common.services.ServiceException)10 Map (java.util.Map)9