Search in sources :

Example 81 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class BioRadGelReader 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);
    String check = in.readString(48);
    if (check.indexOf("Intel Format") != -1) {
        in.order(true);
    }
    in.seek(START_OFFSET);
    boolean codeFound = false;
    int skip = 0;
    long baseFP = 0;
    while (!codeFound) {
        short code = in.readShort();
        if (code == 0x81)
            codeFound = true;
        short length = in.readShort();
        in.skipBytes(2 + 2 * length);
        if (codeFound) {
            baseFP = in.getFilePointer() + 2;
            if (length > 1) {
                in.seek(in.getFilePointer() - 2);
            }
            skip = in.readInt() - 32;
        } else {
            if (length == 1)
                in.skipBytes(12);
            else if (length == 2)
                in.skipBytes(10);
        }
    }
    diff = BASE_OFFSET - baseFP;
    skip += diff;
    double physicalWidth = 0d, physicalHeight = 0d;
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        if (baseFP + skip - 8187 > 0) {
            in.seek(baseFP + skip - 8187);
            String scannerName = in.readCString();
            in.skipBytes(8);
            in.readCString();
            in.skipBytes(8);
            String imageArea = in.readCString();
            imageArea = imageArea.substring(imageArea.indexOf(':') + 1).trim();
            int xIndex = imageArea.indexOf('x');
            if (xIndex > 0) {
                int space = imageArea.indexOf(' ');
                if (space >= 0) {
                    String width = imageArea.substring(1, space);
                    int nextSpace = imageArea.indexOf(" ", xIndex + 2);
                    if (nextSpace > xIndex) {
                        String height = imageArea.substring(xIndex + 1, nextSpace);
                        physicalWidth = Double.parseDouble(width.trim()) * 1000;
                        physicalHeight = Double.parseDouble(height.trim()) * 1000;
                    }
                }
            }
        }
    }
    in.seek(baseFP + skip - 298);
    String date = in.readString(17);
    date = DateTools.formatDate(date, "dd-MMM-yyyy HH:mm");
    in.skipBytes(73);
    String scannerName = in.readCString();
    addGlobalMeta("Scanner name", scannerName);
    in.seek(baseFP + skip);
    CoreMetadata m = core.get(0);
    m.sizeX = in.readShort() & 0xffff;
    m.sizeY = in.readShort() & 0xffff;
    if (getSizeX() * getSizeY() > in.length()) {
        in.order(true);
        in.seek(in.getFilePointer() - 4);
        m.sizeX = in.readShort();
        m.sizeY = in.readShort();
    }
    in.skipBytes(2);
    int bpp = in.readShort();
    m.pixelType = FormatTools.pixelTypeFromBytes(bpp, false, false);
    offset = in.getFilePointer();
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.dimensionOrder = "XYCZT";
    m.rgb = false;
    m.interleaved = false;
    m.indexed = false;
    m.littleEndian = in.isLittleEndian();
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (date != null) {
        store.setImageAcquisitionDate(new Timestamp(date), 0);
    }
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Length sizeX = FormatTools.getPhysicalSizeX(physicalWidth / getSizeX());
        Length sizeY = FormatTools.getPhysicalSizeY(physicalHeight / getSizeY());
        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) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp)

Example 82 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class CanonRawReader method openBytes.

/**
 * @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int)
 */
@Override
public byte[] openBytes(int no, byte[] buf, int x, int y, int w, int h) throws FormatException, IOException {
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    if (plane == null) {
        plane = new byte[FormatTools.getPlaneSize(this)];
        ImageTools.interpolate(pix, plane, COLOR_MAP, getSizeX(), getSizeY(), isLittleEndian());
    }
    RandomAccessInputStream s = new RandomAccessInputStream(plane);
    readPlane(s, x, y, w, h, buf);
    s.close();
    return buf;
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 83 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class BioRadSCNReader 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);
    CoreMetadata m = core.get(0);
    String line = in.readLine();
    String currentBoundary = "";
    String currentType = "";
    int currentLength = 0;
    ArrayList<String> xml = new ArrayList<String>();
    while (in.getFilePointer() < in.length() && line != null) {
        line = line.trim();
        if (line.startsWith("Content-Type")) {
            currentType = line.substring(line.indexOf(' ') + 1);
            int boundary = currentType.indexOf("boundary");
            if (boundary > 0) {
                currentBoundary = currentType.substring(boundary + 10, currentType.length() - 1);
            }
            if (currentType.indexOf(';') > 0) {
                currentType = currentType.substring(0, currentType.indexOf(';'));
            }
        } else if (line.equals("--" + currentBoundary)) {
            currentLength = 0;
        } else if (line.startsWith("Content-Length")) {
            currentLength = Integer.parseInt(line.substring(line.indexOf(' ') + 1));
        } else if (line.length() == 0) {
            if (currentType.equals("application/octet-stream")) {
                pixelsOffset = in.getFilePointer();
                in.skipBytes(currentLength);
            } else if (currentType.equals("text/xml")) {
                String xmlBlock = in.readString(currentLength);
                xml.add(xmlBlock);
            }
        }
        line = in.readLine();
    }
    SCNHandler handler = new SCNHandler();
    for (String block : xml) {
        XMLTools.parseXML(block, handler);
    }
    m.sizeZ = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.dimensionOrder = "XYCZT";
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, exposureTime != null);
    store.setInstrumentID(MetadataTools.createLSID("Instrument", 0), 0);
    if (serialNumber != null) {
        store.setMicroscopeSerialNumber(serialNumber, 0);
    }
    if (model != null) {
        store.setMicroscopeModel(model, 0);
    }
    if (imageName != null) {
        store.setImageName(imageName, 0);
    }
    if (acquisitionDate != null) {
        store.setImageAcquisitionDate(new Timestamp(acquisitionDate), 0);
    }
    if (gain != null || binning != null) {
        String detector = MetadataTools.createLSID("Detector", 0, 0);
        store.setDetectorID(detector, 0, 0);
        store.setDetectorSettingsID(detector, 0, 0);
    }
    if (gain != null) {
        store.setDetectorSettingsGain(gain, 0, 0);
    }
    if (binning != null) {
        store.setDetectorSettingsBinning(getBinning(binning), 0, 0);
    }
    if (exposureTime != null) {
        store.setPlaneExposureTime(new Time(exposureTime, UNITS.SECOND), 0, 0);
    }
    if (physicalSizeX != null) {
        store.setPixelsPhysicalSizeX(FormatTools.createLength(physicalSizeX, UNITS.MICROMETER), 0);
    }
    if (physicalSizeY != null) {
        store.setPixelsPhysicalSizeY(FormatTools.createLength(physicalSizeY, UNITS.MICROMETER), 0);
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) ArrayList(java.util.ArrayList) Time(ome.units.quantity.Time) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) Timestamp(ome.xml.model.primitives.Timestamp)

Example 84 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class BurleighReader 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);
    CoreMetadata m = core.get(0);
    m.littleEndian = true;
    in.order(isLittleEndian());
    int version = (int) in.readFloat() - 1;
    m.sizeX = in.readShort();
    m.sizeY = in.readShort();
    double xSize = 0d, ySize = 0d, zSize = 0d;
    pixelsOffset = version == 1 ? 8 : 260;
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        double timePerPixel = 0d;
        int mode = 0, gain = 0, mag = 0;
        double sampleVolts = 0d, tunnelCurrent = 0d;
        if (version == 1) {
            in.seek(in.length() - 40);
            in.skipBytes(12);
            xSize = in.readInt();
            ySize = in.readInt();
            zSize = in.readInt();
            timePerPixel = in.readShort() * 50;
            mag = in.readShort();
            switch(mag) {
                case 3:
                    mag = 10;
                    break;
                case 4:
                    mag = 50;
                    break;
                case 5:
                    mag = 250;
                    break;
            }
            xSize /= mag;
            ySize /= mag;
            zSize /= mag;
            mode = in.readShort();
            gain = in.readShort();
            sampleVolts = in.readFloat() / 1000;
            tunnelCurrent = in.readFloat();
        } else if (version == 2) {
            in.skipBytes(14);
            xSize = in.readInt();
            ySize = in.readInt();
            zSize = in.readInt();
            mode = in.readShort();
            in.skipBytes(4);
            gain = in.readShort();
            timePerPixel = in.readShort() * 50;
            in.skipBytes(12);
            sampleVolts = in.readFloat();
            tunnelCurrent = in.readFloat();
            addGlobalMeta("Force", in.readFloat());
        }
        addGlobalMeta("Version", version);
        addGlobalMeta("Image mode", mode);
        addGlobalMeta("Z gain", gain);
        addGlobalMeta("Time per pixel (s)", timePerPixel);
        addGlobalMeta("Sample volts", sampleVolts);
        addGlobalMeta("Tunnel current", tunnelCurrent);
        addGlobalMeta("Magnification", mag);
    }
    m.pixelType = FormatTools.UINT16;
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.dimensionOrder = "XYZCT";
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Length sizeX = FormatTools.getPhysicalSizeX(xSize / getSizeX());
        Length sizeY = FormatTools.getPhysicalSizeY(ySize / getSizeY());
        Length sizeZ = FormatTools.getPhysicalSizeZ(zSize / getSizeZ());
        if (sizeX != null) {
            store.setPixelsPhysicalSizeX(sizeX, 0);
        }
        if (sizeY != null) {
            store.setPixelsPhysicalSizeY(sizeY, 0);
        }
        if (sizeZ != null) {
            store.setPixelsPhysicalSizeZ(sizeZ, 0);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Length(ome.units.quantity.Length) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata)

Example 85 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream in project bioformats by openmicroscopy.

the class CellSensReader method parseETSFile.

private void parseETSFile(String file, int s) throws FormatException, IOException {
    fileMap.put(core.size() - 1, file);
    RandomAccessInputStream etsFile = new RandomAccessInputStream(file);
    etsFile.order(true);
    CoreMetadata ms = core.get(getCoreIndex());
    // read the volume header
    String magic = etsFile.readString(4).trim();
    if (!magic.equals("SIS")) {
        throw new FormatException("Unknown magic bytes: " + magic);
    }
    int headerSize = etsFile.readInt();
    int version = etsFile.readInt();
    nDimensions.add(etsFile.readInt());
    long additionalHeaderOffset = etsFile.readLong();
    int additionalHeaderSize = etsFile.readInt();
    // reserved
    etsFile.skipBytes(4);
    long usedChunkOffset = etsFile.readLong();
    int nUsedChunks = etsFile.readInt();
    // reserved
    etsFile.skipBytes(4);
    // read the additional header
    etsFile.seek(additionalHeaderOffset);
    String moreMagic = etsFile.readString(4).trim();
    if (!moreMagic.equals("ETS")) {
        throw new FormatException("Unknown magic bytes: " + moreMagic);
    }
    // extra version number
    etsFile.skipBytes(4);
    int pixelType = etsFile.readInt();
    ms.sizeC = etsFile.readInt();
    int colorspace = etsFile.readInt();
    compressionType.add(etsFile.readInt());
    int compressionQuality = etsFile.readInt();
    tileX.add(etsFile.readInt());
    tileY.add(etsFile.readInt());
    int tileZ = etsFile.readInt();
    // pixel info hints
    etsFile.skipBytes(4 * 17);
    byte[] color = new byte[ms.sizeC * FormatTools.getBytesPerPixel(convertPixelType(pixelType))];
    etsFile.read(color);
    backgroundColor.put(getCoreIndex(), color);
    // background color
    etsFile.skipBytes(4 * 10 - color.length);
    // component order
    etsFile.skipBytes(4);
    boolean usePyramid = etsFile.readInt() != 0;
    ms.rgb = ms.sizeC > 1;
    // read the used chunks
    etsFile.seek(usedChunkOffset);
    tileOffsets.add(new Long[nUsedChunks]);
    ArrayList<TileCoordinate> tmpTiles = new ArrayList<TileCoordinate>();
    for (int chunk = 0; chunk < nUsedChunks; chunk++) {
        etsFile.skipBytes(4);
        int dimensions = nDimensions.get(nDimensions.size() - 1);
        TileCoordinate t = new TileCoordinate(dimensions);
        for (int i = 0; i < dimensions; i++) {
            t.coordinate[i] = etsFile.readInt();
        }
        tileOffsets.get(tileOffsets.size() - 1)[chunk] = etsFile.readLong();
        int nBytes = etsFile.readInt();
        etsFile.skipBytes(4);
        tmpTiles.add(t);
    }
    int maxResolution = 0;
    if (usePyramid) {
        for (TileCoordinate t : tmpTiles) {
            if (t.coordinate[t.coordinate.length - 1] > maxResolution) {
                maxResolution = t.coordinate[t.coordinate.length - 1];
            }
        }
    }
    maxResolution++;
    int[] maxX = new int[maxResolution];
    int[] maxY = new int[maxResolution];
    int[] maxZ = new int[maxResolution];
    int[] maxC = new int[maxResolution];
    int[] maxT = new int[maxResolution];
    HashMap<String, Integer> dimOrder = pyramids.get(s).dimensionOrdering;
    for (TileCoordinate t : tmpTiles) {
        int resolution = usePyramid ? t.coordinate[t.coordinate.length - 1] : 0;
        Integer tv = dimOrder.get("T");
        Integer zv = dimOrder.get("Z");
        Integer cv = dimOrder.get("C");
        int tIndex = tv == null ? -1 : tv + 2;
        int zIndex = zv == null ? -1 : zv + 2;
        int cIndex = cv == null ? -1 : cv + 2;
        if (usePyramid && tIndex == t.coordinate.length - 1) {
            tv = null;
            tIndex = -1;
        }
        if (usePyramid && zIndex == t.coordinate.length - 1) {
            zv = null;
            zIndex = -1;
        }
        int upperLimit = usePyramid ? t.coordinate.length - 1 : t.coordinate.length;
        if ((tIndex < 0 || tIndex >= upperLimit) && (zIndex < 0 || zIndex >= upperLimit) && (cIndex < 0 || cIndex >= upperLimit)) {
            tIndex--;
            zIndex--;
            cIndex--;
            if (dimOrder.containsKey("T")) {
                dimOrder.put("T", tIndex - 2);
            }
            if (dimOrder.containsKey("Z")) {
                dimOrder.put("Z", zIndex - 2);
            }
            if (dimOrder.containsKey("C")) {
                dimOrder.put("C", cIndex - 2);
            }
        }
        if (tv == null && zv == null) {
            if (t.coordinate.length > 4 && cv == null) {
                cIndex = 2;
                dimOrder.put("C", cIndex - 2);
            }
            if (t.coordinate.length > 4) {
                if (cv == null) {
                    tIndex = 3;
                } else {
                    tIndex = cIndex + 2;
                }
                if (tIndex < t.coordinate.length) {
                    dimOrder.put("T", tIndex - 2);
                } else {
                    tIndex = -1;
                }
            }
            if (t.coordinate.length > 5) {
                if (cv == null) {
                    zIndex = 4;
                } else {
                    zIndex = cIndex + 1;
                }
                if (zIndex < t.coordinate.length) {
                    dimOrder.put("Z", zIndex - 2);
                } else {
                    zIndex = -1;
                }
            }
        }
        if (t.coordinate[0] > maxX[resolution]) {
            maxX[resolution] = t.coordinate[0];
        }
        if (t.coordinate[1] > maxY[resolution]) {
            maxY[resolution] = t.coordinate[1];
        }
        if (tIndex >= 0 && t.coordinate[tIndex] > maxT[resolution]) {
            maxT[resolution] = t.coordinate[tIndex];
        }
        if (zIndex >= 0 && t.coordinate[zIndex] > maxZ[resolution]) {
            maxZ[resolution] = t.coordinate[zIndex];
        }
        if (cIndex >= 0 && t.coordinate[cIndex] > maxC[resolution]) {
            maxC[resolution] = t.coordinate[cIndex];
        }
    }
    if (pyramids.get(s).width != null) {
        ms.sizeX = pyramids.get(s).width;
    }
    if (pyramids.get(s).height != null) {
        ms.sizeY = pyramids.get(s).height;
    }
    ms.sizeZ = maxZ[0] + 1;
    if (maxC[0] > 0) {
        ms.sizeC *= (maxC[0] + 1);
    }
    ms.sizeT = maxT[0] + 1;
    if (ms.sizeZ == 0) {
        ms.sizeZ = 1;
    }
    ms.imageCount = ms.sizeZ * ms.sizeT;
    if (maxC[0] > 0) {
        ms.imageCount *= (maxC[0] + 1);
    }
    if (maxY[0] >= 1) {
        rows.add(maxY[0] + 1);
    } else {
        rows.add(1);
    }
    if (maxX[0] >= 1) {
        cols.add(maxX[0] + 1);
    } else {
        cols.add(1);
    }
    ArrayList<TileCoordinate> map = new ArrayList<TileCoordinate>();
    for (int i = 0; i < tmpTiles.size(); i++) {
        map.add(tmpTiles.get(i));
    }
    tileMap.add(map);
    ms.pixelType = convertPixelType(pixelType);
    if (usePyramid) {
        int finalResolution = 1;
        int initialCoreSize = core.size();
        for (int i = 1; i < maxResolution; i++) {
            CoreMetadata newResolution = new CoreMetadata(ms);
            int previousX = core.get(core.size() - 1).sizeX;
            int previousY = core.get(core.size() - 1).sizeY;
            int maxSizeX = tileX.get(tileX.size() - 1) * (maxX[i] < 1 ? 1 : maxX[i] + 1);
            int maxSizeY = tileY.get(tileY.size() - 1) * (maxY[i] < 1 ? 1 : maxY[i] + 1);
            newResolution.sizeX = previousX / 2;
            if (previousX % 2 == 1 && newResolution.sizeX < maxSizeX) {
                newResolution.sizeX++;
            } else if (newResolution.sizeX > maxSizeX) {
                newResolution.sizeX = maxSizeX;
            }
            newResolution.sizeY = previousY / 2;
            if (previousY % 2 == 1 && newResolution.sizeY < maxSizeY) {
                newResolution.sizeY++;
            } else if (newResolution.sizeY > maxSizeY) {
                newResolution.sizeY = maxSizeY;
            }
            newResolution.sizeZ = maxZ[i] + 1;
            if (maxC[i] > 0 && newResolution.sizeC != (maxC[i] + 1)) {
                newResolution.sizeC *= (maxC[i] + 1);
            }
            newResolution.sizeT = maxT[i] + 1;
            if (newResolution.sizeZ == 0) {
                newResolution.sizeZ = 1;
            }
            newResolution.imageCount = newResolution.sizeZ * newResolution.sizeT;
            if (maxC[i] > 0) {
                newResolution.imageCount *= (maxC[i] + 1);
            }
            newResolution.metadataComplete = true;
            newResolution.dimensionOrder = "XYCZT";
            core.add(newResolution);
            rows.add(maxY[i] >= 1 ? maxY[i] + 1 : 1);
            cols.add(maxX[i] >= 1 ? maxX[i] + 1 : 1);
            fileMap.put(core.size() - 1, file);
            finalResolution = core.size() - initialCoreSize + 1;
            tileX.add(tileX.get(tileX.size() - 1));
            tileY.add(tileY.get(tileY.size() - 1));
            compressionType.add(compressionType.get(compressionType.size() - 1));
            tileMap.add(map);
            nDimensions.add(nDimensions.get(nDimensions.size() - 1));
            tileOffsets.add(tileOffsets.get(tileOffsets.size() - 1));
            backgroundColor.put(core.size() - 1, color);
        }
        ms.resolutionCount = finalResolution;
    }
    etsFile.close();
}
Also used : ArrayList(java.util.ArrayList) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

RandomAccessInputStream (loci.common.RandomAccessInputStream)246 CoreMetadata (loci.formats.CoreMetadata)108 MetadataStore (loci.formats.meta.MetadataStore)97 FormatException (loci.formats.FormatException)75 TiffParser (loci.formats.tiff.TiffParser)56 IFD (loci.formats.tiff.IFD)51 Length (ome.units.quantity.Length)48 Location (loci.common.Location)47 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)30 Timestamp (ome.xml.model.primitives.Timestamp)28 Time (ome.units.quantity.Time)21 ByteArrayHandle (loci.common.ByteArrayHandle)18 IFDList (loci.formats.tiff.IFDList)16 CodecOptions (loci.formats.codec.CodecOptions)9 RandomAccessOutputStream (loci.common.RandomAccessOutputStream)8 ServiceException (loci.common.services.ServiceException)7 PhotoInterp (loci.formats.tiff.PhotoInterp)7 TiffSaver (loci.formats.tiff.TiffSaver)7 BufferedReader (java.io.BufferedReader)6