Search in sources :

Example 41 with RandomAccessInputStream

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

the class SBIGReader 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);
    Double sizeX = null, sizeY = null;
    String date = null, description = null;
    String[] lines = DataTools.readFile(currentId).split("\n");
    for (String line : lines) {
        line = line.trim();
        int eq = line.indexOf('=');
        if (eq != -1) {
            String key = line.substring(0, eq).trim();
            String value = line.substring(eq + 1).trim();
            addGlobalMeta(key, value);
            if (key.equals("Width")) {
                m.sizeX = Integer.parseInt(value);
            } else if (key.equals("Height")) {
                m.sizeY = Integer.parseInt(value);
            } else if (key.equals("Note")) {
                description = value;
            } else if (key.equals("X_pixel_size")) {
                sizeX = new Double(value) * 1000;
            } else if (key.equals("Y_pixel_size")) {
                sizeY = new Double(value) * 1000;
            } else if (key.equals("Date")) {
                date = value;
            } else if (key.equals("Time")) {
                date += " " + value;
            }
        } else if (line.indexOf("Compressed") != -1) {
            compressed = true;
        } else if (line.equals("End"))
            break;
    }
    m.pixelType = FormatTools.UINT16;
    m.littleEndian = true;
    m.rgb = false;
    m.sizeZ = 1;
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = 1;
    m.dimensionOrder = "XYZCT";
    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 (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        Length x = FormatTools.getPhysicalSizeX(sizeX);
        Length y = FormatTools.getPhysicalSizeY(sizeY);
        if (x != null) {
            store.setPixelsPhysicalSizeX(x, 0);
        }
        if (y != null) {
            store.setPixelsPhysicalSizeY(y, 0);
        }
        store.setImageDescription(description, 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 42 with RandomAccessInputStream

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

the class SDTReader 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);
    LOGGER.info("Reading header");
    // read file header information
    info = new SDTInfo(in, metadata);
    timeBins = info.timeBins;
    channels = info.channels;
    addGlobalMeta("time bins", timeBins);
    addGlobalMeta("channels", channels);
    double timeBase = 1e9 * info.tacR / info.tacG;
    addGlobalMeta("time base", timeBase);
    LOGGER.info("Populating metadata");
    CoreMetadata m = core.get(0);
    int timepoints = info.timepoints;
    if (timepoints == 0) {
        timepoints = 1;
    }
    m.sizeX = info.width;
    m.sizeY = info.height;
    m.sizeZ = 1;
    m.sizeT = intensity ? timepoints : timeBins * timepoints;
    m.sizeC = channels;
    m.dimensionOrder = "XYZTC";
    m.pixelType = FormatTools.UINT16;
    m.rgb = false;
    m.littleEndian = true;
    m.imageCount = m.sizeZ * m.sizeC * m.sizeT;
    m.indexed = false;
    m.falseColor = false;
    m.metadataComplete = true;
    if (intensity) {
        m.moduloT.parentType = FormatTools.SPECTRA;
    } else {
        m.moduloT.type = FormatTools.LIFETIME;
        m.moduloT.parentType = FormatTools.SPECTRA;
        m.moduloT.typeDescription = "TCSPC";
        m.moduloT.start = 0;
        timeBase *= 1000;
        m.moduloT.step = timeBase / timeBins;
        m.moduloT.end = m.moduloT.step * (m.sizeT - 1);
        m.moduloT.unit = "ps";
    }
    for (int i = 1; i < info.allBlockOffsets.length; i++) {
        CoreMetadata p = new CoreMetadata(m);
        core.add(p);
        int planeSize = m.sizeX * m.sizeY * FormatTools.getBytesPerPixel(m.pixelType);
        if (info.allBlockLengths[i] != planeSize * m.imageCount) {
            if (info.mcstaPoints * planeSize == info.allBlockLengths[i]) {
                p.sizeT = info.mcstaPoints;
                p.moduloT.end = p.moduloT.step * (p.sizeT - 1);
                p.imageCount = p.sizeZ * p.sizeC * p.sizeT;
            }
        }
    }
    // Arbitrarily chosen size limit for buffer
    int sizeThreshold = 512 * 512 * 512;
    // default No of bins in buffer
    blockLength = 2048;
    while (blockLength * m.sizeX * m.sizeY > sizeThreshold) {
        blockLength = blockLength / 2;
    }
    if (blockLength > timeBins) {
        blockLength = timeBins;
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata)

Example 43 with RandomAccessInputStream

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

the class SIFReader 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);
    double[] timestamp = null;
    int lineNumber = 1;
    String line = in.readLine();
    int endLine = -1;
    while (endLine < 0 || lineNumber < endLine) {
        lineNumber++;
        if (line.startsWith("Pixel number")) {
            String[] tokens = line.split(" ");
            if (tokens.length > 2) {
                m.sizeC = Integer.parseInt(tokens[2]);
                m.sizeX = Integer.parseInt(tokens[3]);
                m.sizeY = Integer.parseInt(tokens[4]);
                m.sizeZ = Integer.parseInt(tokens[5]);
                m.sizeT = Integer.parseInt(tokens[6]);
                m.imageCount = getSizeZ() * getSizeT() * getSizeC();
                timestamp = new double[getImageCount()];
                endLine = lineNumber;
            }
        } else if (lineNumber < endLine) {
            int index = lineNumber - (endLine - getImageCount()) - 1;
            if (index >= 0) {
                try {
                    timestamp[index] = Double.parseDouble(line.trim());
                } catch (NumberFormatException e) {
                    LOGGER.debug("Could not parse timestamp #" + index, e);
                }
            }
        } else {
            addGlobalMetaList("Line", line.trim());
        }
        line = in.readLine();
        if (endLine > 0 && endLine == lineNumber) {
            String[] tokens = line.split(" ");
            int x1 = Integer.parseInt(tokens[1]);
            int y1 = Integer.parseInt(tokens[2]);
            int x2 = Integer.parseInt(tokens[3]);
            int y2 = Integer.parseInt(tokens[4]);
            int x3 = Integer.parseInt(tokens[5]);
            int y3 = Integer.parseInt(tokens[6]);
            m.sizeX = (int) (Math.abs(x1 - x2) + x3);
            m.sizeY = (int) (Math.abs(y1 - y2) + y3);
            pixelOffset = in.length() - FOOTER_SIZE - (getImageCount() * getSizeX() * getSizeY() * 4);
        }
    }
    m.pixelType = FormatTools.FLOAT;
    m.dimensionOrder = "XYCZT";
    m.littleEndian = true;
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this, getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM);
    if (getMetadataOptions().getMetadataLevel() != MetadataLevel.MINIMUM) {
        for (int i = 0; i < getImageCount(); i++) {
            store.setPlaneDeltaT(new Time(timestamp[i], UNITS.SECOND), 0, i);
        }
    }
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) Time(ome.units.quantity.Time) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata)

Example 44 with RandomAccessInputStream

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

the class APNGReader 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 (no == lastImageIndex && lastImage != null && y + h <= lastImageRow) {
        RandomAccessInputStream s = new RandomAccessInputStream(lastImage);
        readPlane(s, x, y, w, h, buf);
        s.close();
        s = null;
        return buf;
    }
    if (no == 0) {
        lastImage = null;
        PNGInputStream stream = new PNGInputStream("IDAT");
        int decodeHeight = y + h;
        if (decodeHeight < getSizeY() && decodeHeight % 8 != 0) {
            decodeHeight += 8 - (decodeHeight % 8);
        }
        lastImage = decode(stream, getSizeX(), decodeHeight);
        stream.close();
        lastImageIndex = 0;
        lastImageRow = y + h;
        RandomAccessInputStream pix = new RandomAccessInputStream(lastImage);
        readPlane(pix, x, y, w, h, buf);
        pix.close();
        pix = null;
        if (y + h < getSizeY()) {
            lastImage = null;
        }
        return buf;
    }
    int[] coords = frameCoordinates.get(no);
    lastImage = openBytes(0);
    lastImageRow = getSizeY();
    PNGInputStream stream = new PNGInputStream("fdAT", no);
    byte[] newImage = decode(stream, coords[2], coords[3]);
    stream.close();
    // paste current image onto first image
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    int len = coords[2] * bpp;
    int plane = getSizeX() * getSizeY() * bpp;
    int newPlane = len * coords[3];
    if (!isInterleaved()) {
        for (int c = 0; c < getRGBChannelCount(); c++) {
            for (int row = 0; row < coords[3]; row++) {
                System.arraycopy(newImage, c * newPlane + row * len, lastImage, c * plane + (coords[1] + row) * getSizeX() * bpp + coords[0] * bpp, len);
            }
        }
    } else {
        len *= getRGBChannelCount();
        for (int row = 0; row < coords[3]; row++) {
            System.arraycopy(newImage, row * len, lastImage, (coords[1] + row) * getSizeX() * bpp * getRGBChannelCount() + coords[0] * bpp * getRGBChannelCount(), len);
        }
    }
    lastImageIndex = no;
    RandomAccessInputStream pix = new RandomAccessInputStream(lastImage);
    readPlane(pix, x, y, w, h, buf);
    pix.close();
    return buf;
}
Also used : RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 45 with RandomAccessInputStream

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

the class EPSReader 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);
    LOGGER.info("Verifying EPS format");
    String line = readLine();
    if (!line.trim().startsWith("%!PS")) {
        // read the TIFF preview
        isTiff = true;
        in.order(true);
        in.seek(20);
        int offset = in.readInt();
        int len = in.readInt();
        byte[] b = new byte[len];
        in.seek(offset);
        in.read(b);
        in = new RandomAccessInputStream(b);
        TiffParser tp = new TiffParser(in);
        ifds = tp.getIFDs();
        IFD firstIFD = ifds.get(0);
        map = tp.getColorMap(firstIFD);
        m.sizeX = (int) firstIFD.getImageWidth();
        m.sizeY = (int) firstIFD.getImageLength();
        m.sizeZ = 1;
        m.sizeT = 1;
        m.sizeC = firstIFD.getSamplesPerPixel();
        if (map != null && getSizeC() == 1) {
            m.sizeC = 3;
        }
        if (getSizeC() == 2)
            m.sizeC = 4;
        m.littleEndian = firstIFD.isLittleEndian();
        m.interleaved = true;
        m.rgb = getSizeC() > 1;
        m.pixelType = firstIFD.getPixelType();
        m.imageCount = 1;
        m.dimensionOrder = "XYCZT";
        m.metadataComplete = true;
        m.indexed = false;
        m.falseColor = false;
        MetadataStore store = makeFilterMetadata();
        MetadataTools.populatePixels(store, this);
        return;
    }
    LOGGER.info("Finding image data");
    binary = false;
    String image = "image";
    int lineNum = 1;
    line = readLine().trim();
    while (line != null && !line.equals("%%EOF")) {
        if (line.endsWith(image)) {
            if (!line.startsWith(image)) {
                if (line.indexOf("colorimage") != -1)
                    m.sizeC = 3;
                String[] t = line.split(" ");
                try {
                    int newX = Integer.parseInt(t[0]);
                    int newY = Integer.parseInt(t[1]);
                    if (t.length > 2 && Integer.parseInt(t[2]) >= 8) {
                        m.sizeX = newX;
                        m.sizeY = newY;
                        start = lineNum;
                    }
                } catch (NumberFormatException exc) {
                    LOGGER.debug("Could not parse image dimensions", exc);
                    if (t.length > 3) {
                        m.sizeC = Integer.parseInt(t[3]);
                    }
                }
            }
            break;
        } else if (line.startsWith("%%")) {
            if (line.startsWith("%%BoundingBox:")) {
                line = line.substring(14).trim();
                String[] t = line.split(" ");
                try {
                    int originX = Integer.parseInt(t[0].trim());
                    int originY = Integer.parseInt(t[1].trim());
                    m.sizeX = Integer.parseInt(t[2].trim()) - originX;
                    m.sizeY = Integer.parseInt(t[3].trim()) - originY;
                    addGlobalMeta("X-coordinate of origin", originX);
                    addGlobalMeta("Y-coordinate of origin", originY);
                } catch (NumberFormatException e) {
                    throw new FormatException("Files without image data are not supported.");
                }
            } else if (line.startsWith("%%BeginBinary")) {
                binary = true;
            } else {
                // parse key/value pairs
                int ndx = line.indexOf(':');
                if (ndx != -1) {
                    String key = line.substring(0, ndx);
                    String value = line.substring(ndx + 1);
                    addGlobalMeta(key, value);
                }
            }
        } else if (line.startsWith("%ImageData:")) {
            line = line.substring(11);
            String[] t = line.split(" ");
            m.sizeX = Integer.parseInt(t[0]);
            m.sizeY = Integer.parseInt(t[1]);
            m.sizeC = Integer.parseInt(t[3]);
            for (int i = 4; i < t.length; i++) {
                image = t[i].trim();
                if (image.length() > 1) {
                    image = image.substring(1, image.length() - 1);
                }
            }
        }
        lineNum++;
        line = readLine().trim();
    }
    LOGGER.info("Populating metadata");
    if (getSizeC() == 0)
        m.sizeC = 1;
    m.sizeZ = 1;
    m.sizeT = 1;
    m.dimensionOrder = "XYCZT";
    m.pixelType = FormatTools.UINT8;
    m.rgb = getSizeC() == 3;
    m.interleaved = true;
    m.littleEndian = true;
    m.imageCount = 1;
    // Populate metadata store
    // The metadata store we're working with.
    MetadataStore store = getMetadataStore();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

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