Search in sources :

Example 1 with CBZip2InputStream

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

the class OMEXMLReader 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 {
    if (binDataOffsets.size() == 0)
        return buf;
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    int index = no;
    int series = getSeries();
    for (int i = 0; i < series; i++) {
        index += core.get(i).imageCount;
    }
    if (index >= binDataOffsets.size()) {
        index = binDataOffsets.size() - 1;
    }
    long offset = binDataOffsets.get(index).longValue();
    String compress = compression.get(index);
    in.seek(offset);
    int depth = FormatTools.getBytesPerPixel(getPixelType());
    int planeSize = getSizeX() * getSizeY() * depth;
    CodecOptions options = new CodecOptions();
    options.width = getSizeX();
    options.height = getSizeY();
    options.bitsPerSample = depth * 8;
    options.channels = getRGBChannelCount();
    options.maxBytes = planeSize;
    options.littleEndian = isLittleEndian();
    options.interleaved = isInterleaved();
    String encoded = in.readString("<");
    encoded = encoded.trim();
    if (encoded.length() == 0 || encoded.equals("<")) {
        LOGGER.debug("No pixel data for plane #{}", no);
        return buf;
    }
    encoded = encoded.substring(0, encoded.length() - 1);
    byte[] pixels = BaseEncoding.base64().decode(encoded);
    // return a blank plane if no pixel data was stored
    if (pixels.length == 0) {
        LOGGER.debug("No pixel data for plane #{}", no);
        return buf;
    }
    // TODO: Create a method uncompress to handle all compression methods
    if (compress.equals("bzip2")) {
        byte[] tempPixels = pixels;
        pixels = new byte[tempPixels.length - 2];
        System.arraycopy(tempPixels, 2, pixels, 0, pixels.length);
        ByteArrayInputStream bais = new ByteArrayInputStream(pixels);
        CBZip2InputStream bzip = new CBZip2InputStream(bais);
        pixels = new byte[planeSize];
        bzip.read(pixels, 0, pixels.length);
        tempPixels = null;
        bais.close();
        bzip.close();
        bais = null;
        bzip = null;
    } else if (compress.equals("zlib")) {
        pixels = new ZlibCodec().decompress(pixels, options);
    } else if (compress.equals("J2K")) {
        pixels = new JPEG2000Codec().decompress(pixels, options);
    } else if (compress.equals("JPEG")) {
        pixels = new JPEGCodec().decompress(pixels, options);
    }
    for (int row = 0; row < h; row++) {
        int off = (row + y) * getSizeX() * depth + x * depth;
        System.arraycopy(pixels, off, buf, row * w * depth, w * depth);
    }
    pixels = null;
    return buf;
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) ByteArrayInputStream(java.io.ByteArrayInputStream) CBZip2InputStream(loci.common.CBZip2InputStream) JPEGCodec(loci.formats.codec.JPEGCodec)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 CBZip2InputStream (loci.common.CBZip2InputStream)1 CodecOptions (loci.formats.codec.CodecOptions)1 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)1 JPEGCodec (loci.formats.codec.JPEGCodec)1 ZlibCodec (loci.formats.codec.ZlibCodec)1