Search in sources :

Example 1 with ZlibCodec

use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.

the class PhotoshopTiffReader 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 (getSeries() == 0)
        return super.openBytes(no, buf, x, y, w, h);
    FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
    int offsetIndex = 0;
    for (int i = 1; i < getSeries(); i++) {
        offsetIndex += core.get(i).sizeC;
    }
    tag.seek(layerOffset[offsetIndex]);
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    if (compression[getSeries() - 1] == PACKBITS || compression[getSeries() - 1] == ZIP) {
        Codec codec = compression[getSeries() - 1] == ZIP ? new ZlibCodec() : new PackbitsCodec();
        CodecOptions options = new CodecOptions();
        options.maxBytes = FormatTools.getPlaneSize(this) / getSizeC();
        ByteArrayHandle pix = new ByteArrayHandle();
        for (int c = 0; c < getSizeC(); c++) {
            int index = channelOrder[getSeries() - 1][c];
            tag.seek(layerOffset[offsetIndex + index]);
            pix.write(codec.decompress(tag, options));
        }
        RandomAccessInputStream plane = new RandomAccessInputStream(pix);
        plane.seek(0);
        readPlane(plane, x, y, w, h, buf);
        plane.close();
        pix = null;
    } else
        readPlane(tag, x, y, w, h, buf);
    return buf;
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) PackbitsCodec(loci.formats.codec.PackbitsCodec) Codec(loci.formats.codec.Codec) PackbitsCodec(loci.formats.codec.PackbitsCodec) ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 2 with ZlibCodec

use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.

the class CellomicsReader method getDecompressedStream.

private RandomAccessInputStream getDecompressedStream(String filename) throws FormatException, IOException {
    RandomAccessInputStream s = new RandomAccessInputStream(filename);
    if (checkSuffix(filename, "c01")) {
        LOGGER.info("Decompressing file");
        s.seek(4);
        ZlibCodec codec = new ZlibCodec();
        byte[] file = codec.decompress(s, null);
        s.close();
        return new RandomAccessInputStream(file);
    }
    return s;
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 3 with ZlibCodec

use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.

the class NativeQTReader method parse.

// -- Helper methods --
/**
 * Parse all of the atoms in the file.
 */
private void parse(int depth, long offset, long length) throws FormatException, IOException {
    while (offset < length) {
        in.seek(offset);
        // first 4 bytes are the atom size
        long atomSize = in.readInt() & 0xffffffffL;
        // read the atom type
        String atomType = in.readString(4);
        // if atomSize is 1, then there is an 8 byte extended size
        if (atomSize == 1) {
            atomSize = in.readLong();
        }
        if (atomSize < 0) {
            LOGGER.warn("QTReader: invalid atom size: {}", atomSize);
        } else if (atomSize > in.length()) {
            offset += 4;
            continue;
        }
        LOGGER.debug("Seeking to {}; atomType={}; atomSize={}", new Object[] { offset, atomType, atomSize });
        // if this is a container atom, parse the children
        if (isContainer(atomType)) {
            parse(depth++, in.getFilePointer(), offset + atomSize);
        } else {
            if (atomSize == 0)
                atomSize = in.length();
            long oldpos = in.getFilePointer();
            if (atomType.equals("mdat")) {
                // we've found the pixel data
                pixelOffset = in.getFilePointer();
                pixelBytes = atomSize;
                if (pixelBytes > (in.length() - pixelOffset)) {
                    pixelBytes = in.length() - pixelOffset;
                }
            } else if (atomType.equals("tkhd")) {
                // we've found the dimensions
                in.skipBytes(38);
                int[][] matrix = new int[3][3];
                for (int i = 0; i < matrix.length; i++) {
                    for (int j = 0; j < matrix[0].length; j++) {
                        matrix[i][j] = in.readInt();
                    }
                }
                // The contents of the matrix we just read determine whether or not
                // we should flip the width and height.  We can check the first two
                // rows of the matrix - they should correspond to the first two rows
                // of an identity matrix.
                // TODO : adapt to use the value of flip
                flip = matrix[0][0] == 0 && matrix[1][0] != 0;
                if (getSizeX() == 0)
                    core.get(0).sizeX = in.readInt();
                if (getSizeY() == 0)
                    core.get(0).sizeY = in.readInt();
            } else if (atomType.equals("cmov")) {
                in.skipBytes(8);
                if ("zlib".equals(in.readString(4))) {
                    atomSize = in.readInt();
                    in.skipBytes(4);
                    int uncompressedSize = in.readInt();
                    byte[] b = new byte[(int) (atomSize - 12)];
                    in.read(b);
                    byte[] output = new ZlibCodec().decompress(b, null);
                    RandomAccessInputStream oldIn = in;
                    in = new RandomAccessInputStream(output);
                    parse(0, 0, output.length);
                    in.close();
                    in = oldIn;
                } else {
                    throw new UnsupportedCompressionException("Compressed header not supported.");
                }
            } else if (atomType.equals("stco")) {
                if (offsets.size() > 0)
                    break;
                separatedFork = false;
                in.skipBytes(4);
                int numPlanes = in.readInt();
                if (numPlanes != getImageCount()) {
                    in.seek(in.getFilePointer() - 4);
                    int off = in.readInt();
                    offsets.add(new Integer(off));
                    for (int i = 1; i < getImageCount(); i++) {
                        if ((chunkSizes.size() > 0) && (i < chunkSizes.size())) {
                            rawSize = chunkSizes.get(i).intValue();
                        } else
                            i = getImageCount();
                        off += rawSize;
                        offsets.add(new Integer(off));
                    }
                } else {
                    for (int i = 0; i < numPlanes; i++) {
                        offsets.add(new Integer(in.readInt()));
                    }
                }
            } else if (atomType.equals("stsd")) {
                // found video codec and pixel depth information
                in.skipBytes(4);
                int numEntries = in.readInt();
                in.skipBytes(4);
                for (int i = 0; i < numEntries; i++) {
                    if (i == 0) {
                        codec = in.readString(4);
                        if (!codec.equals("raw ") && !codec.equals("rle ") && !codec.equals("rpza") && !codec.equals("mjpb") && !codec.equals("jpeg")) {
                            throw new UnsupportedCompressionException("Unsupported codec: " + codec);
                        }
                        in.skipBytes(16);
                        if (in.readShort() == 0) {
                            in.skipBytes(56);
                            bitsPerPixel = in.readShort();
                            if (codec.equals("rpza"))
                                bitsPerPixel = 8;
                            in.skipBytes(10);
                            interlaced = in.read() == 2;
                            addGlobalMeta("Codec", codec);
                            addGlobalMeta("Bits per pixel", bitsPerPixel);
                            in.skipBytes(9);
                        }
                    } else {
                        altCodec = in.readString(4);
                        addGlobalMeta("Second codec", altCodec);
                    }
                }
            } else if (atomType.equals("stsz")) {
                // found the number of planes
                in.skipBytes(4);
                rawSize = in.readInt();
                core.get(0).imageCount = in.readInt();
                if (rawSize == 0) {
                    in.seek(in.getFilePointer() - 4);
                    for (int b = 0; b < getImageCount(); b++) {
                        chunkSizes.add(new Integer(in.readInt()));
                    }
                }
            } else if (atomType.equals("stsc")) {
                in.skipBytes(4);
                int numChunks = in.readInt();
                if (altCodec != null) {
                    int prevChunk = 0;
                    for (int i = 0; i < numChunks; i++) {
                        int chunk = in.readInt();
                        int planesPerChunk = in.readInt();
                        int id = in.readInt();
                        if (id == 2)
                            altPlanes += planesPerChunk * (chunk - prevChunk);
                        prevChunk = chunk;
                    }
                }
            } else if (atomType.equals("stts")) {
                in.skipBytes(12);
                int fps = in.readInt();
                addGlobalMeta("Frames per second", fps);
            }
            if (oldpos + atomSize < in.length()) {
                in.seek(oldpos + atomSize);
            } else
                break;
        }
        if (atomSize == 0)
            offset = in.length();
        else
            offset += atomSize;
        // if a 'udta' atom, skip ahead 4 bytes
        if (atomType.equals("udta"))
            offset += 4;
        print(depth, atomSize, atomType);
    }
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) UnsupportedCompressionException(loci.formats.UnsupportedCompressionException) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 4 with ZlibCodec

use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.

the class OMEXMLWriter method compress.

// -- Helper methods --
/**
 * Compress the given byte array using the current codec.
 * The compressed data is then base64-encoded.
 */
private byte[] compress(byte[] b) throws FormatException, IOException {
    MetadataRetrieve r = getMetadataRetrieve();
    String type = r.getPixelsType(series).toString();
    int pixelType = FormatTools.pixelTypeFromString(type);
    int bytes = FormatTools.getBytesPerPixel(pixelType);
    CodecOptions options = new CodecOptions();
    options.width = r.getPixelsSizeX(series).getValue().intValue();
    options.height = r.getPixelsSizeY(series).getValue().intValue();
    options.channels = 1;
    options.interleaved = false;
    options.signed = FormatTools.isSigned(pixelType);
    boolean littleEndian = false;
    if (r.getPixelsBigEndian(series) != null) {
        littleEndian = !r.getPixelsBigEndian(series).booleanValue();
    } else if (r.getPixelsBinDataCount(series) == 0) {
        littleEndian = !r.getPixelsBinDataBigEndian(series, 0).booleanValue();
    }
    options.littleEndian = littleEndian;
    options.bitsPerSample = bytes * 8;
    if (compression.equals("J2K")) {
        b = new JPEG2000Codec().compress(b, options);
    } else if (compression.equals("JPEG")) {
        b = new JPEGCodec().compress(b, options);
    } else if (compression.equals("zlib")) {
        b = new ZlibCodec().compress(b, options);
    }
    return new Base64Codec().compress(b, options);
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) Base64Codec(loci.formats.codec.Base64Codec) MetadataRetrieve(loci.formats.meta.MetadataRetrieve) JPEGCodec(loci.formats.codec.JPEGCodec)

Example 5 with ZlibCodec

use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.

the class ZeissZVIReader 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);
    lastPlane = no;
    if (poi == null) {
        initPOIService();
    }
    int bytes = FormatTools.getBytesPerPixel(getPixelType());
    int pixel = bytes * getRGBChannelCount();
    CodecOptions options = new CodecOptions();
    options.littleEndian = isLittleEndian();
    options.interleaved = isInterleaved();
    int index = -1;
    int[] coords = getZCTCoords(no);
    for (int q = 0; q < coordinates.length; q++) {
        if (coordinates[q][0] == coords[0] && coordinates[q][1] == coords[1] && coordinates[q][2] == coords[2] && coordinates[q][3] == getSeries()) {
            index = q;
            break;
        }
    }
    LOGGER.trace("no = " + no + ", index = " + index);
    if (index < 0 || index >= imageFiles.length) {
        return buf;
    }
    RandomAccessInputStream s = poi.getDocumentStream(imageFiles[index]);
    s.seek(offsets[index]);
    int len = w * pixel;
    int row = getSizeX() * pixel;
    if (isJPEG) {
        byte[] t = new JPEGCodec().decompress(s, options);
        for (int yy = 0; yy < h; yy++) {
            System.arraycopy(t, (yy + y) * row + x * pixel, buf, yy * len, len);
        }
    } else if (isZlib) {
        byte[] t = new ZlibCodec().decompress(s, options);
        for (int yy = 0; yy < h; yy++) {
            int src = (yy + y) * row + x * pixel;
            int dest = yy * len;
            if (src + len <= t.length && dest + len <= buf.length) {
                System.arraycopy(t, src, buf, dest, len);
            } else
                break;
        }
    } else {
        readPlane(s, x, y, w, h, buf);
    }
    s.close();
    if (isRGB() && !isJPEG) {
        // reverse bytes in groups of 3 to account for BGR storage
        byte[] bb = new byte[bytes];
        for (int i = 0; i < buf.length; i += bpp) {
            System.arraycopy(buf, i + 2 * bytes, bb, 0, bytes);
            System.arraycopy(buf, i, buf, i + 2 * bytes, bytes);
            System.arraycopy(bb, 0, buf, i, bytes);
        }
    }
    return buf;
}
Also used : ZlibCodec(loci.formats.codec.ZlibCodec) CodecOptions(loci.formats.codec.CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream) JPEGCodec(loci.formats.codec.JPEGCodec)

Aggregations

ZlibCodec (loci.formats.codec.ZlibCodec)7 RandomAccessInputStream (loci.common.RandomAccessInputStream)5 CodecOptions (loci.formats.codec.CodecOptions)5 JPEGCodec (loci.formats.codec.JPEGCodec)3 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)2 PackbitsCodec (loci.formats.codec.PackbitsCodec)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayHandle (loci.common.ByteArrayHandle)1 CBZip2InputStream (loci.common.CBZip2InputStream)1 CoreMetadata (loci.formats.CoreMetadata)1 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)1 Base64Codec (loci.formats.codec.Base64Codec)1 Codec (loci.formats.codec.Codec)1 MetadataRetrieve (loci.formats.meta.MetadataRetrieve)1 MetadataStore (loci.formats.meta.MetadataStore)1 TiffIFDEntry (loci.formats.tiff.TiffIFDEntry)1