Search in sources :

Example 16 with CodecOptions

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

the class NativeQTReader method uncompress.

/**
 * Uncompresses an image plane according to the the codec identifier.
 */
private byte[] uncompress(byte[] pixs, String code) throws FormatException, IOException {
    CodecOptions options = new MJPBCodecOptions();
    options.width = getSizeX();
    options.height = getSizeY();
    options.bitsPerSample = bitsPerPixel;
    options.channels = bitsPerPixel < 40 ? bitsPerPixel / 8 : (bitsPerPixel - 32) / 8;
    options.previousImage = canUsePrevious ? prevPixels : null;
    options.littleEndian = isLittleEndian();
    options.interleaved = isRGB();
    if (code.equals("raw "))
        return pixs;
    else if (code.equals("rle ")) {
        return new QTRLECodec().decompress(pixs, options);
    } else if (code.equals("rpza")) {
        return new RPZACodec().decompress(pixs, options);
    } else if (code.equals("mjpb")) {
        ((MJPBCodecOptions) options).interlaced = interlaced;
        return new MJPBCodec().decompress(pixs, options);
    } else if (code.equals("jpeg")) {
        return new JPEGCodec().decompress(pixs, options);
    } else {
        throw new UnsupportedCompressionException("Unsupported codec : " + code);
    }
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) MJPBCodecOptions(loci.formats.codec.MJPBCodecOptions) QTRLECodec(loci.formats.codec.QTRLECodec) MJPBCodec(loci.formats.codec.MJPBCodec) RPZACodec(loci.formats.codec.RPZACodec) UnsupportedCompressionException(loci.formats.UnsupportedCompressionException) MJPBCodecOptions(loci.formats.codec.MJPBCodecOptions) JPEGCodec(loci.formats.codec.JPEGCodec)

Example 17 with CodecOptions

use of loci.formats.codec.CodecOptions 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 18 with CodecOptions

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

the class TiffSaver method writeImage.

public void writeImage(byte[] buf, IFD ifd, int no, int pixelType, int x, int y, int w, int h, boolean last, Integer nChannels, boolean copyDirectly) throws FormatException, IOException {
    LOGGER.debug("Attempting to write image.");
    // b/c method is public should check parameters again
    if (buf == null) {
        throw new FormatException("Image data cannot be null");
    }
    if (ifd == null) {
        throw new FormatException("IFD cannot be null");
    }
    // These operations are synchronized
    TiffCompression compression;
    int tileWidth, tileHeight, nStrips;
    boolean interleaved;
    ByteArrayOutputStream[] stripBuf;
    synchronized (this) {
        int bytesPerPixel = FormatTools.getBytesPerPixel(pixelType);
        int blockSize = w * h * bytesPerPixel;
        if (nChannels == null) {
            nChannels = buf.length / (w * h * bytesPerPixel);
        }
        interleaved = ifd.getPlanarConfiguration() == 1;
        makeValidIFD(ifd, pixelType, nChannels);
        // create pixel output buffers
        compression = ifd.getCompression();
        tileWidth = (int) ifd.getTileWidth();
        tileHeight = (int) ifd.getTileLength();
        int tilesPerRow = (int) ifd.getTilesPerRow();
        int rowsPerStrip = (int) ifd.getRowsPerStrip()[0];
        int stripSize = rowsPerStrip * tileWidth * bytesPerPixel;
        nStrips = ((w + tileWidth - 1) / tileWidth) * ((h + tileHeight - 1) / tileHeight);
        if (interleaved)
            stripSize *= nChannels;
        else
            nStrips *= nChannels;
        stripBuf = new ByteArrayOutputStream[nStrips];
        DataOutputStream[] stripOut = new DataOutputStream[nStrips];
        for (int strip = 0; strip < nStrips; strip++) {
            stripBuf[strip] = new ByteArrayOutputStream(stripSize);
            stripOut[strip] = new DataOutputStream(stripBuf[strip]);
        }
        int[] bps = ifd.getBitsPerSample();
        int off;
        // write pixel strips to output buffers
        int effectiveStrips = !interleaved ? nStrips / nChannels : nStrips;
        if (effectiveStrips == 1 && copyDirectly) {
            stripOut[0].write(buf);
        } else {
            for (int strip = 0; strip < effectiveStrips; strip++) {
                int xOffset = (strip % tilesPerRow) * tileWidth;
                int yOffset = (strip / tilesPerRow) * tileHeight;
                for (int row = 0; row < tileHeight; row++) {
                    for (int col = 0; col < tileWidth; col++) {
                        int ndx = ((row + yOffset) * w + col + xOffset) * bytesPerPixel;
                        for (int c = 0; c < nChannels; c++) {
                            for (int n = 0; n < bps[c] / 8; n++) {
                                if (interleaved) {
                                    off = ndx * nChannels + c * bytesPerPixel + n;
                                    if (row >= h || col >= w) {
                                        stripOut[strip].writeByte(0);
                                    } else if (off < buf.length) {
                                        stripOut[strip].writeByte(buf[off]);
                                    } else {
                                        stripOut[strip].writeByte(0);
                                    }
                                } else {
                                    off = c * blockSize + ndx + n;
                                    int realStrip = (c * (nStrips / nChannels)) + strip;
                                    if (row >= h || col >= w) {
                                        stripOut[realStrip].writeByte(0);
                                    } else if (off < buf.length) {
                                        stripOut[realStrip].writeByte(buf[off]);
                                    } else {
                                        stripOut[realStrip].writeByte(0);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // Compress strips according to given differencing and compression schemes,
    // this operation is NOT synchronized and is the ONLY portion of the
    // TiffWriter.saveBytes() --> TiffSaver.writeImage() stack that is NOT
    // synchronized.
    byte[][] strips = new byte[nStrips][];
    for (int strip = 0; strip < nStrips; strip++) {
        strips[strip] = stripBuf[strip].toByteArray();
        TiffCompression.difference(strips[strip], ifd);
        CodecOptions codecOptions = compression.getCompressionCodecOptions(ifd, options);
        codecOptions.height = tileHeight;
        codecOptions.width = tileWidth;
        codecOptions.channels = interleaved ? nChannels : 1;
        strips[strip] = compression.compress(strips[strip], codecOptions);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(String.format("Compressed strip %d/%d length %d", strip + 1, nStrips, strips[strip].length));
        }
    }
    // This operation is synchronized
    synchronized (this) {
        writeImageIFD(ifd, no, strips, nChannels, last, x, y);
    }
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FormatException(loci.formats.FormatException)

Example 19 with CodecOptions

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

the class PictReader 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 (jpegOffsets.size() > 0) {
        ByteArrayHandle v = new ByteArrayHandle();
        in.seek(jpegOffsets.get(0));
        byte[] b = new byte[(int) (in.length() - in.getFilePointer())];
        in.read(b);
        RandomAccessInputStream s = new RandomAccessInputStream(b);
        for (long jpegOffset : jpegOffsets) {
            s.seek(jpegOffset - jpegOffsets.get(0));
            CodecOptions options = new CodecOptions();
            options.interleaved = isInterleaved();
            options.littleEndian = isLittleEndian();
            v.write(new JPEGCodec().decompress(s, options));
        }
        s.close();
        s = new RandomAccessInputStream(v);
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
        s.close();
        return buf;
    }
    if (legacy || strips.size() == 0) {
        in.seek(512);
        byte[] pix = new byte[(int) (in.length() - in.getFilePointer())];
        in.read(pix);
        byte[][] b = AWTImageTools.getBytes(AWTImageTools.makeBuffered(qtTools.pictToImage(pix)));
        pix = null;
        for (int i = 0; i < b.length; i++) {
            System.arraycopy(b[i], 0, buf, i * b[i].length, b[i].length);
        }
        b = null;
        return buf;
    }
    if ((getSizeY() * 4 < strips.size()) && (((strips.size() / 3) % getSizeY()) != 0)) {
        core.get(0).sizeY = strips.size();
    }
    int plane = w * h;
    if (lookup != null) {
        // 8 bit data
        byte[] row;
        for (int i = y; i < y + h; i++) {
            row = (byte[]) strips.get(i);
            int len = (int) Math.min(row.length, w);
            System.arraycopy(row, x, buf, (i - y) * w, len);
        }
    } else if (getSizeY() * 3 == strips.size() || getSizeY() * 4 == strips.size()) {
        // 24 or 32 bit data
        int nc = strips.size() / getSizeY();
        byte[] c0 = null;
        byte[] c1 = null;
        byte[] c2 = null;
        for (int i = y; i < h + y; i++) {
            c0 = (byte[]) strips.get(i * nc + nc - 3);
            c1 = (byte[]) strips.get(i * nc + nc - 2);
            c2 = (byte[]) strips.get(i * nc + nc - 1);
            int baseOffset = (i - y) * w;
            System.arraycopy(c0, x, buf, baseOffset, w);
            System.arraycopy(c1, x, buf, plane + baseOffset, w);
            System.arraycopy(c2, x, buf, 2 * plane + baseOffset, w);
        }
    } else {
        // RGB value is packed into a single short: xRRR RRGG GGGB BBBB
        int[] row = null;
        for (int i = y; i < h + y; i++) {
            row = (int[]) strips.get(i);
            for (int j = x; j < w + x; j++) {
                int base = (i - y) * w + (j - x);
                buf[base] = (byte) ((row[j] & 0x7c00) >> 10);
                buf[plane + base] = (byte) ((row[j] & 0x3e0) >> 5);
                buf[2 * plane + base] = (byte) (row[j] & 0x1f);
            }
        }
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle) JPEGCodec(loci.formats.codec.JPEGCodec)

Example 20 with CodecOptions

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

the class PictReader method handlePixmap.

/**
 * Handles the unpacking of the image data.
 */
private void handlePixmap(int pixelSize, int compCount) throws FormatException, IOException {
    LOGGER.debug("handlePixmap({}, {}, {})", new Object[] { rowBytes, pixelSize, compCount });
    int rawLen;
    // row raw bytes
    byte[] buf;
    // row uncompressed data
    byte[] uBuf = null;
    // row uncompressed data - 16+ bit pixels
    int[] uBufI = null;
    int bufSize = rowBytes;
    int outBufSize = getSizeX();
    // used to expand pixel data
    byte[] outBuf = null;
    boolean compressed = (rowBytes >= 8) || (pixelSize == 32);
    switch(pixelSize) {
        case 32:
            if (!compressed)
                uBufI = new int[getSizeX()];
            else
                uBuf = new byte[bufSize];
            break;
        case 16:
            uBufI = new int[getSizeX()];
            break;
        case 8:
            uBuf = new byte[bufSize];
            break;
        default:
            outBuf = new byte[outBufSize];
            uBuf = new byte[bufSize];
            break;
    }
    if (!compressed) {
        LOGGER.debug("Pixel data is uncompressed (pixelSize={}).", pixelSize);
        buf = new byte[bufSize];
        for (int row = 0; row < getSizeY(); row++) {
            in.read(buf, 0, rowBytes);
            switch(pixelSize) {
                case 16:
                    for (int i = 0; i < getSizeX(); i++) {
                        uBufI[i] = DataTools.bytesToShort(buf, i * 2, 2, false);
                    }
                    strips.add(uBufI);
                    buf = null;
                    core.get(0).sizeC = 3;
                    break;
                case 8:
                    strips.add(buf);
                    break;
                default:
                    // pixel size < 8
                    expandPixels(pixelSize, buf, outBuf, outBuf.length);
                    strips.add(outBuf);
                    buf = null;
            }
        }
    } else {
        LOGGER.debug("Pixel data is compressed (pixelSize={}; compCount={}).", pixelSize, compCount);
        buf = new byte[bufSize + 1 + bufSize / 128];
        for (int row = 0; row < getSizeY(); row++) {
            if (rowBytes > 250)
                rawLen = in.readShort();
            else
                rawLen = in.read();
            if (rawLen > buf.length)
                rawLen = buf.length;
            if ((in.length() - in.getFilePointer()) <= rawLen) {
                rawLen = (int) (in.length() - in.getFilePointer() - 1);
            }
            if (rawLen < 0) {
                rawLen = 0;
                in.seek(in.length() - 1);
            }
            in.read(buf, 0, rawLen);
            if (pixelSize == 16) {
                uBufI = new int[getSizeX()];
                unpackBits(buf, uBufI);
                strips.add(uBufI);
                core.get(0).sizeC = 3;
            } else {
                PackbitsCodec c = new PackbitsCodec();
                CodecOptions options = new CodecOptions();
                options.maxBytes = getSizeX() * 4;
                uBuf = c.decompress(buf, options);
            }
            if (pixelSize < 8) {
                expandPixels(pixelSize, uBuf, outBuf, outBuf.length);
                strips.add(outBuf);
            } else if (pixelSize == 8) {
                strips.add(uBuf);
            } else if (pixelSize == 24 || pixelSize == 32) {
                byte[] newBuf = null;
                for (int q = 0; q < compCount; q++) {
                    int offset = q * getSizeX();
                    int len = (int) Math.min(getSizeX(), uBuf.length - offset);
                    newBuf = new byte[getSizeX()];
                    if (offset < uBuf.length) {
                        System.arraycopy(uBuf, offset, newBuf, 0, len);
                    }
                    strips.add(newBuf);
                }
                core.get(0).sizeC = 3;
            }
        }
    }
}
Also used : PackbitsCodec(loci.formats.codec.PackbitsCodec) CodecOptions(loci.formats.codec.CodecOptions)

Aggregations

CodecOptions (loci.formats.codec.CodecOptions)37 JPEG2000CodecOptions (loci.formats.codec.JPEG2000CodecOptions)18 TiffCompression (loci.formats.tiff.TiffCompression)18 Test (org.testng.annotations.Test)18 RandomAccessInputStream (loci.common.RandomAccessInputStream)9 JPEGCodec (loci.formats.codec.JPEGCodec)8 PackbitsCodec (loci.formats.codec.PackbitsCodec)6 ByteArrayHandle (loci.common.ByteArrayHandle)5 ZlibCodec (loci.formats.codec.ZlibCodec)5 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)4 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)4 IOException (java.io.IOException)3 CoreMetadata (loci.formats.CoreMetadata)3 Codec (loci.formats.codec.Codec)3 FormatException (loci.formats.FormatException)2 IFormatReader (loci.formats.IFormatReader)2 MetadataStore (loci.formats.meta.MetadataStore)2 Length (ome.units.quantity.Length)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1