Search in sources :

Example 1 with LZOCodec

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

the class VolocityReader 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);
    int[] zct = getZCTCoords(no);
    Stack stack = stacks.get(getSeries());
    if (!new Location(stack.pixelsFiles[zct[1]]).exists()) {
        Arrays.fill(buf, (byte) 0);
        return buf;
    }
    RandomAccessInputStream pix = new RandomAccessInputStream(stack.pixelsFiles[zct[1]]);
    int padding = zct[2] * stack.planePadding;
    long planeSize = FormatTools.getPlaneSize(this);
    int planesInFile = (int) (pix.length() / planeSize);
    int planeIndex = no / getEffectiveSizeC();
    if (planesInFile == getSizeT()) {
        planeIndex = zct[2];
        int block = stack.blockSize;
        padding = block - (int) (planeSize % block);
        if (padding == block) {
            padding = 0;
        }
        padding *= zct[2];
    }
    long offset = (long) stack.blockSize + planeIndex * planeSize + padding;
    if (offset >= pix.length()) {
        pix.close();
        return buf;
    }
    pix.seek(offset);
    if (stack.clippingData) {
        pix.seek(offset - 3);
        ByteArrayHandle v = new ByteArrayHandle();
        while (v.length() < FormatTools.getPlaneSize(this) && pix.getFilePointer() < pix.length()) {
            try {
                byte[] b = new LZOCodec().decompress(pix, null);
                pix.skipBytes(4);
                v.write(b);
            } catch (IOException e) {
            }
        }
        RandomAccessInputStream s = new RandomAccessInputStream(v);
        s.seek(0);
        readPlane(s, x, y, w, h, buf);
        s.close();
    } else {
        if (pix.getFilePointer() + planeSize > pix.length()) {
            pix.close();
            return buf;
        }
        readPlane(pix, x, y, w, h, buf);
    }
    pix.close();
    if (getRGBChannelCount() == 4) {
        // stored as ARGB, need to swap to RGBA
        for (int i = 0; i < buf.length / 4; i++) {
            byte a = buf[i * 4];
            buf[i * 4] = buf[i * 4 + 1];
            buf[i * 4 + 1] = buf[i * 4 + 2];
            buf[i * 4 + 2] = buf[i * 4 + 3];
            buf[i * 4 + 3] = a;
        }
    }
    return buf;
}
Also used : LZOCodec(loci.formats.codec.LZOCodec) RandomAccessInputStream(loci.common.RandomAccessInputStream) IOException(java.io.IOException) ByteArrayHandle(loci.common.ByteArrayHandle) Location(loci.common.Location)

Example 2 with LZOCodec

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

the class VolocityClippingReader 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(pixelOffset);
    if (FormatTools.getPlaneSize(this) * 2 + in.getFilePointer() < in.length()) {
        readPlane(in, x, y, w, h, buf);
        return buf;
    }
    byte[] b = new LZOCodec().decompress(in, null);
    RandomAccessInputStream s = new RandomAccessInputStream(b);
    s.seek(0);
    readPlane(s, x, y, w, h, buf);
    s.close();
    return buf;
}
Also used : LZOCodec(loci.formats.codec.LZOCodec) RandomAccessInputStream(loci.common.RandomAccessInputStream)

Example 3 with LZOCodec

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

the class VolocityClippingReader 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 = in.read() == 'I';
    in.order(isLittleEndian());
    in.skipBytes(4);
    String magicString = in.readString(4);
    if (!magicString.equals(CLIPPING_MAGIC_STRING)) {
        throw new FormatException("Found invalid magic string: " + magicString);
    }
    int check = in.readInt();
    while (check != 0x208 && check != AISF) {
        in.seek(in.getFilePointer() - 3);
        check = in.readInt();
    }
    if (check == AISF) {
        m.littleEndian = false;
        in.order(isLittleEndian());
        in.skipBytes(28);
    }
    m.sizeX = in.readInt();
    m.sizeY = in.readInt();
    m.sizeZ = in.readInt();
    m.sizeC = 1;
    m.sizeT = 1;
    m.imageCount = getSizeZ() * getSizeT();
    m.dimensionOrder = "XYCZT";
    m.pixelType = FormatTools.UINT8;
    pixelOffset = in.getFilePointer() + 65;
    if (getSizeX() * getSizeY() * 100 >= in.length()) {
        while (in.getFilePointer() < in.length()) {
            try {
                byte[] b = new LZOCodec().decompress(in, null);
                if (b.length > 0 && (b.length % (getSizeX() * getSizeY())) == 0) {
                    int bytes = b.length / (getSizeX() * getSizeY());
                    m.pixelType = FormatTools.pixelTypeFromBytes(bytes, false, false);
                    break;
                }
            } catch (EOFException e) {
            }
            pixelOffset++;
            in.seek(pixelOffset);
        }
    }
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : MetadataStore(loci.formats.meta.MetadataStore) LZOCodec(loci.formats.codec.LZOCodec) EOFException(java.io.EOFException) RandomAccessInputStream(loci.common.RandomAccessInputStream) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException)

Example 4 with LZOCodec

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

the class OpenlabReader 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;
    PlaneInfo planeInfo = null;
    if (specialPlateNames) {
        planeInfo = getPlane(getZCTCoords(no));
    } else if (no < planeOffsets[getSeries()].length) {
        int index = planeOffsets[getSeries()][no];
        planeInfo = planes[index];
    }
    if (planeInfo == null)
        return buf;
    long first = planeInfo.planeOffset;
    long last = first + FormatTools.getPlaneSize(this) * 2;
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    if (!planeInfo.pict) {
        if (version == 2) {
            in.seek(first);
            readPlane(in, x, y, w, h, buf);
        } else {
            in.seek(first + 16);
            int bytes = bpp * getRGBChannelCount();
            byte[] b = new byte[(int) (last - first)];
            in.read(b);
            CodecOptions options = new CodecOptions();
            options.width = getSizeX();
            options.height = getSizeY();
            options.bitsPerSample = bytes * 8;
            options.maxBytes = getSizeX() * getSizeY() * bytes;
            b = new LZOCodec().decompress(b, options);
            if (getSizeX() * getSizeY() * 4 <= b.length) {
                for (int yy = y; yy < h + y; yy++) {
                    for (int xx = x; xx < w + x; xx++) {
                        System.arraycopy(b, (yy * (getSizeX() + 4) + xx) * 4 + 1, buf, ((yy - y) * w + xx - x) * 3, 3);
                    }
                }
            } else {
                int src = b.length / getSizeY();
                if (src - (getSizeX() * bytes) != 16)
                    src = getSizeX() * bytes;
                int dest = w * bytes;
                for (int row = 0; row < h; row++) {
                    System.arraycopy(b, (row + y) * src + x * bytes, buf, row * dest, dest);
                }
            }
            b = null;
        }
        if (planeInfo.volumeType == MAC_256_GREYS || planeInfo.volumeType == MAC_256_COLORS) {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (~buf[i] & 0xff);
            }
        }
    } else {
        // PICT plane
        Exception exc = null;
        if (getPixelType() == FormatTools.UINT8) {
            in.seek(first);
            byte[] b = new byte[(int) (last - first) + 512];
            in.read(b, 512, b.length - 512);
            IFormatReader r = getRGBChannelCount() == 1 ? new ChannelSeparator(pict) : pict;
            try {
                Location.mapFile("OPENLAB_PICT", new ByteArrayHandle(b));
                r.setId("OPENLAB_PICT");
                if (getPixelType() != r.getPixelType()) {
                    throw new FormatException("Pixel type of inner PICT does not " + "match pixel type of Openlab file");
                }
                if (isIndexed()) {
                    int index = no;
                    if (getSeries() < planeOffsets.length) {
                        index = planeOffsets[getSeries()][lastPlane];
                    }
                    luts.set(index, pict.get8BitLookupTable());
                }
                r.openBytes(0, buf, x, y, w, h);
            } catch (FormatException e) {
                exc = e;
            } catch (IOException e) {
                exc = e;
            } finally {
                r.close();
                // remove file from map
                Location.mapFile("OPENLAB_PICT", null);
            }
            b = null;
        }
        if (exc != null || getPixelType() != FormatTools.UINT8) {
            LOGGER.debug("", exc);
            in.seek(planeInfo.planeOffset - 298);
            if (in.readByte() == 1)
                in.skipBytes(297);
            else
                in.skipBytes(169);
            int size = 0, expectedBlock = 0, totalBlocks = -1, pixPos = 0;
            byte[] plane = new byte[FormatTools.getPlaneSize(this)];
            while (expectedBlock != totalBlocks && pixPos < plane.length && in.getFilePointer() + 32 < last) {
                findNextBlock();
                if (in.getFilePointer() + 4 >= in.length())
                    break;
                int num = in.readInt();
                if (num != expectedBlock) {
                    throw new FormatException("Expected iPic block not found");
                }
                expectedBlock++;
                if (totalBlocks == -1) {
                    totalBlocks = in.readInt();
                    in.skipBytes(8);
                } else
                    in.skipBytes(12);
                size = in.readInt();
                in.skipBytes(4);
                if (size + pixPos > plane.length)
                    size = plane.length - pixPos;
                in.read(plane, pixPos, size);
                pixPos += size;
            }
            RandomAccessInputStream pix = new RandomAccessInputStream(plane);
            readPlane(pix, x, y, w, h, buf);
            pix.close();
            plane = null;
        }
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFormatReader(loci.formats.IFormatReader) LZOCodec(loci.formats.codec.LZOCodec) IOException(java.io.IOException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) ChannelSeparator(loci.formats.ChannelSeparator) FormatException(loci.formats.FormatException) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Aggregations

RandomAccessInputStream (loci.common.RandomAccessInputStream)4 LZOCodec (loci.formats.codec.LZOCodec)4 IOException (java.io.IOException)2 ByteArrayHandle (loci.common.ByteArrayHandle)2 FormatException (loci.formats.FormatException)2 EOFException (java.io.EOFException)1 Location (loci.common.Location)1 ChannelSeparator (loci.formats.ChannelSeparator)1 CoreMetadata (loci.formats.CoreMetadata)1 IFormatReader (loci.formats.IFormatReader)1 CodecOptions (loci.formats.codec.CodecOptions)1 MetadataStore (loci.formats.meta.MetadataStore)1