Search in sources :

Example 1 with TargaRLECodec

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

the class TargaReader 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);
    in.seek(offset);
    int planeSize = FormatTools.getPlaneSize(this);
    TargaRLECodec codec = new TargaRLECodec();
    CodecOptions options = new CodecOptions();
    options.maxBytes = planeSize;
    options.bitsPerSample = bits;
    RandomAccessInputStream s = in;
    if (compressed) {
        byte[] b = codec.decompress(in, options);
        s = new RandomAccessInputStream(b);
        s.order(isLittleEndian());
    }
    int bpp = bits;
    while (bpp % 8 != 0) bpp++;
    bpp /= 8;
    int rowSkip = orientation < 2 ? (getSizeY() - h - y) : y;
    int colSkip = (orientation % 2) == 1 ? (getSizeX() - w - x) : x;
    s.skipBytes(rowSkip * getSizeX() * bpp);
    for (int row = 0; row < h; row++) {
        if (s.getFilePointer() >= s.length())
            break;
        s.skipBytes(colSkip * bpp);
        for (int col = 0; col < w; col++) {
            if (s.getFilePointer() >= s.length())
                break;
            int rowIndex = orientation < 2 ? h - row - 1 : row;
            int colIndex = (orientation % 2) == 1 ? w - col - 1 : col;
            int index = getSizeC() * (rowIndex * w + colIndex);
            if (bpp == 2) {
                int v = s.readShort();
                buf[index] = (byte) ((v & 0x7c00) >> 10);
                buf[index + 1] = (byte) ((v & 0x3e0) >> 5);
                buf[index + 2] = (byte) (v & 0x1f);
            } else if (bpp == 4) {
                buf[index + 2] = s.readByte();
                buf[index + 1] = s.readByte();
                buf[index] = s.readByte();
                s.skipBytes(1);
            } else {
                for (int c = getSizeC() - 1; c >= 0; c--) {
                    buf[index + c] = s.readByte();
                }
            }
        }
        s.skipBytes(bpp * (getSizeX() - w - colSkip));
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) TargaRLECodec(loci.formats.codec.TargaRLECodec) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Aggregations

RandomAccessInputStream (loci.common.RandomAccessInputStream)1 CodecOptions (loci.formats.codec.CodecOptions)1 TargaRLECodec (loci.formats.codec.TargaRLECodec)1