Search in sources :

Example 26 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream 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 27 with RandomAccessInputStream

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

the class PCIReader method getOptimalTileWidth.

/* @see loci.formats.IFormatReader#getOptimalTileWidth() */
@Override
public int getOptimalTileWidth() {
    FormatTools.assertId(currentId, true, 1);
    String file = imageFiles.get(0);
    try {
        if (poi == null) {
            initPOIService();
        }
        RandomAccessInputStream s = poi.getDocumentStream(file);
        TiffParser tp = new TiffParser(s);
        if (tp.isValidHeader()) {
            IFD ifd = tp.getFirstIFD();
            s.close();
            return (int) ifd.getTileWidth();
        }
        s.close();
    } catch (FormatException e) {
        LOGGER.debug("Could not retrieve tile width", e);
    } catch (IOException e) {
        LOGGER.debug("Could not retrieve tile width", e);
    }
    return super.getOptimalTileWidth();
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) FormatException(loci.formats.FormatException)

Example 28 with RandomAccessInputStream

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

the class PCIReader 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 (poi == null) {
        initPOIService();
    }
    String file = imageFiles.get(no);
    RandomAccessInputStream s = poi.getDocumentStream(file);
    TiffParser tp = new TiffParser(s);
    if (tp.isValidHeader()) {
        IFD ifd = tp.getFirstIFD();
        tp.getSamples(ifd, buf, x, y, w, h);
    } else {
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
    }
    s.close();
    return buf;
}
Also used : IFD(loci.formats.tiff.IFD) TiffParser(loci.formats.tiff.TiffParser) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 29 with RandomAccessInputStream

use of loci.common.RandomAccessInputStream 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 RandomAccessInputStream

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

the class PSDReader 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 = false;
    if (!in.readString(4).equals("8BPS")) {
        throw new FormatException("Not a valid Photoshop file.");
    }
    addGlobalMeta("Version", in.readShort());
    // reserved, set to 0
    in.skipBytes(6);
    m.sizeC = in.readShort();
    m.sizeY = in.readInt();
    m.sizeX = in.readInt();
    int bits = in.readShort();
    addGlobalMeta("Bits per pixel", bits);
    m.pixelType = FormatTools.pixelTypeFromBytes(bits / 8, false, false);
    int colorMode = in.readShort();
    String modeString = null;
    switch(colorMode) {
        case 0:
            modeString = "monochrome";
            break;
        case 1:
            modeString = "gray-scale";
            break;
        case 2:
            modeString = "palette color";
            break;
        case 3:
            modeString = "RGB";
            break;
        case 4:
            modeString = "CMYK";
            break;
        case 6:
            modeString = "Duotone";
            break;
        case 7:
            modeString = "Multichannel color";
            break;
        case 8:
            modeString = "Duotone";
            break;
        case 9:
            modeString = "LAB color";
            break;
    }
    addGlobalMeta("Color mode", modeString);
    // read color mode block, if present
    int modeDataLength = in.readInt();
    long fp = in.getFilePointer();
    if (modeDataLength != 0) {
        if (colorMode == 2) {
            lut = new byte[3][256];
            for (int i = 0; i < lut.length; i++) {
                in.read(lut[i]);
            }
        }
        in.seek(fp + modeDataLength);
    }
    // read image resources block
    in.skipBytes(4);
    while (in.readString(4).equals("8BIM")) {
        int tag = in.readShort();
        int read = 1;
        while (in.read() != 0) read++;
        if (read % 2 == 1)
            in.skipBytes(1);
        int size = in.readInt();
        if (size % 2 == 1)
            size++;
        in.skipBytes(size);
    }
    in.seek(in.getFilePointer() - 4);
    int blockLen = in.readInt();
    if (blockLen == 0) {
        offset = in.getFilePointer();
    } else {
        int layerLen = in.readInt();
        int layerCount = in.readShort();
        if (layerCount < 0) {
            throw new FormatException("Vector data is not supported.");
        }
        if (layerLen == 0 && layerCount == 0) {
            in.skipBytes(2);
            int check = in.readShort();
            in.seek(in.getFilePointer() - (check == 0 ? 4 : 2));
        }
        int[] w = new int[layerCount];
        int[] h = new int[layerCount];
        int[] c = new int[layerCount];
        for (int i = 0; i < layerCount; i++) {
            int top = in.readInt();
            int left = in.readInt();
            int bottom = in.readInt();
            int right = in.readInt();
            w[i] = right - left;
            h[i] = bottom - top;
            c[i] = in.readShort();
            in.skipBytes(c[i] * 6 + 12);
            int len = in.readInt();
            if (len % 2 == 1)
                len++;
            in.skipBytes(len);
        }
        // skip over pixel data for each layer
        for (int i = 0; i < layerCount; i++) {
            if (h[i] < 0) {
                continue;
            }
            int[] lens = new int[h[i]];
            for (int cc = 0; cc < c[i]; cc++) {
                boolean compressed = in.readShort() == 1;
                if (!compressed)
                    in.skipBytes(w[i] * h[i]);
                else {
                    for (int y = 0; y < h[i]; y++) {
                        lens[y] = in.readShort();
                    }
                    for (int y = 0; y < h[i]; y++) {
                        in.skipBytes(lens[y]);
                    }
                }
            }
        }
        long start = in.getFilePointer();
        while (in.read() != '8') ;
        in.skipBytes(7);
        if (in.getFilePointer() - start > 1024) {
            in.seek(start);
        }
        int len = in.readInt();
        if ((len % 4) != 0)
            len += 4 - (len % 4);
        if (len > in.length() - in.getFilePointer() || (len & 0xff0000) >> 16 == 1) {
            in.seek(start);
            len = 0;
        }
        in.skipBytes(len);
        String s = in.readString(4);
        while (s.equals("8BIM")) {
            in.skipBytes(4);
            len = in.readInt();
            if ((len % 4) != 0)
                len += 4 - (len % 4);
            in.skipBytes(len);
            s = in.readString(4);
        }
        offset = in.getFilePointer() - 4;
    }
    m.sizeZ = 1;
    m.sizeT = 1;
    m.rgb = modeString.equals("RGB") || modeString.equals("CMYK");
    m.imageCount = getSizeC() / (isRGB() ? 3 : 1);
    m.indexed = modeString.equals("palette color");
    m.falseColor = false;
    m.dimensionOrder = "XYCZT";
    m.interleaved = false;
    m.metadataComplete = true;
    in.seek(offset);
    compressed = in.readShort() == 1;
    lens = new int[getSizeC()][getSizeY()];
    if (compressed) {
        for (int c = 0; c < getSizeC(); c++) {
            for (int row = 0; row < getSizeY(); row++) {
                lens[c][row] = in.readShort();
            }
        }
    }
    offset = in.getFilePointer();
    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)

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