Search in sources :

Example 1 with Codec

use of loci.formats.codec.Codec 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 Codec

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

the class CellSensReader method decodeTile.

private byte[] decodeTile(int no, int row, int col) throws FormatException, IOException {
    if (tileMap.get(getCoreIndex()) == null) {
        return new byte[getTileSize()];
    }
    int[] zct = getZCTCoords(no);
    TileCoordinate t = new TileCoordinate(nDimensions.get(getCoreIndex()));
    t.coordinate[0] = col;
    t.coordinate[1] = row;
    int resIndex = getResolution();
    int pyramidIndex = getSeries();
    if (hasFlattenedResolutions()) {
        int index = 0;
        pyramidIndex = 0;
        for (int i = 0; i < core.size(); ) {
            if (index + core.get(i).resolutionCount <= getSeries()) {
                index += core.get(i).resolutionCount;
                i += core.get(i).resolutionCount;
                pyramidIndex++;
            } else {
                resIndex = getSeries() - index;
                break;
            }
        }
    }
    Pyramid pyramid = pyramids.get(pyramidIndex);
    for (String dim : pyramid.dimensionOrdering.keySet()) {
        int index = pyramid.dimensionOrdering.get(dim) + 2;
        if (dim.equals("Z")) {
            t.coordinate[index] = zct[0];
        } else if (dim.equals("C")) {
            t.coordinate[index] = zct[1];
        } else if (dim.equals("T")) {
            t.coordinate[index] = zct[2];
        }
    }
    if (resIndex > 0) {
        t.coordinate[t.coordinate.length - 1] = resIndex;
    }
    ArrayList<TileCoordinate> map = tileMap.get(getCoreIndex());
    Integer index = map.indexOf(t);
    if (index == null || index < 0) {
        // fill in the tile with the stored background color
        // usually this is either black or white
        byte[] tile = new byte[getTileSize()];
        byte[] color = backgroundColor.get(getCoreIndex());
        if (color != null) {
            for (int q = 0; q < getTileSize(); q += color.length) {
                for (int i = 0; i < color.length; i++) {
                    tile[q + i] = color[i];
                }
            }
        }
        return tile;
    }
    Long offset = tileOffsets.get(getCoreIndex())[index];
    RandomAccessInputStream ets = new RandomAccessInputStream(fileMap.get(getCoreIndex()));
    ets.seek(offset);
    CodecOptions options = new CodecOptions();
    options.interleaved = isInterleaved();
    options.littleEndian = isLittleEndian();
    int tileSize = getTileSize();
    if (tileSize == 0) {
        tileSize = tileX.get(getCoreIndex()) * tileY.get(getCoreIndex()) * 10;
    }
    options.maxBytes = (int) (offset + tileSize);
    byte[] buf = null;
    long end = index < tileOffsets.get(getCoreIndex()).length - 1 ? tileOffsets.get(getCoreIndex())[index + 1] : ets.length();
    IFormatReader reader = null;
    String file = null;
    switch(compressionType.get(getCoreIndex())) {
        case RAW:
            buf = new byte[tileSize];
            ets.read(buf);
            break;
        case JPEG:
            Codec codec = new JPEGCodec();
            buf = codec.decompress(ets, options);
            break;
        case JPEG_2000:
            codec = new JPEG2000Codec();
            buf = codec.decompress(ets, options);
            break;
        case JPEG_LOSSLESS:
            codec = new LosslessJPEGCodec();
            buf = codec.decompress(ets, options);
            break;
        case PNG:
            file = "tile.png";
            reader = new APNGReader();
        case BMP:
            if (reader == null) {
                file = "tile.bmp";
                reader = new BMPReader();
            }
            byte[] b = new byte[(int) (end - offset)];
            ets.read(b);
            Location.mapFile(file, new ByteArrayHandle(b));
            reader.setId(file);
            buf = reader.openBytes(0);
            Location.mapFile(file, null);
            break;
    }
    if (reader != null) {
        reader.close();
    }
    ets.close();
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) IFormatReader(loci.formats.IFormatReader) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) JPEGCodec(loci.formats.codec.JPEGCodec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) Codec(loci.formats.codec.Codec) JPEGCodec(loci.formats.codec.JPEGCodec) LosslessJPEGCodec(loci.formats.codec.LosslessJPEGCodec) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) RandomAccessInputStream(loci.common.RandomAccessInputStream) ByteArrayHandle(loci.common.ByteArrayHandle)

Example 3 with Codec

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

the class DicomReader 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);
    Integer[] keys = fileList.keySet().toArray(new Integer[0]);
    Arrays.sort(keys);
    if (fileList.size() > 1) {
        int fileNumber = 0;
        if (fileList.get(keys[getSeries()]).size() > 1) {
            fileNumber = no / imagesPerFile;
            no = no % imagesPerFile;
        }
        String file = fileList.get(keys[getSeries()]).get(fileNumber);
        helper.setId(file);
        return helper.openBytes(no, buf, x, y, w, h);
    }
    int ec = isIndexed() ? 1 : getSizeC();
    int bpp = FormatTools.getBytesPerPixel(getPixelType());
    int bytes = getSizeX() * getSizeY() * bpp * ec;
    in.seek(offsets[no]);
    if (isRLE) {
        // plane is compressed using run-length encoding
        CodecOptions options = new CodecOptions();
        options.maxBytes = getSizeX() * getSizeY();
        for (int c = 0; c < ec; c++) {
            PackbitsCodec codec = new PackbitsCodec();
            byte[] t = null;
            if (bpp > 1) {
                int plane = bytes / (bpp * ec);
                byte[][] tmp = new byte[bpp][];
                long start = in.getFilePointer();
                for (int i = 0; i < bpp; i++) {
                    // one or more extra 0 bytes can be inserted between
                    // the planes, but there isn't a good way to know in advance
                    // only way to know is to see if decompressing produces the
                    // correct number of bytes
                    tmp[i] = codec.decompress(in, options);
                    if (i > 0 && tmp[i].length > options.maxBytes) {
                        in.seek(start);
                        tmp[i] = codec.decompress(in, options);
                    }
                    if (no < imagesPerFile - 1 || i < bpp - 1) {
                        start = in.getFilePointer();
                        while (in.read() == 0) ;
                        long end = in.getFilePointer();
                        in.seek(end - 1);
                    }
                }
                t = new byte[bytes / ec];
                for (int i = 0; i < plane; i++) {
                    for (int j = 0; j < bpp; j++) {
                        int byteIndex = isLittleEndian() ? bpp - j - 1 : j;
                        if (i < tmp[byteIndex].length) {
                            t[i * bpp + j] = tmp[byteIndex][i];
                        }
                    }
                }
            } else {
                t = codec.decompress(in, options);
                if (t.length < (bytes / ec)) {
                    byte[] tmp = t;
                    t = new byte[bytes / ec];
                    System.arraycopy(tmp, 0, t, 0, tmp.length);
                }
                if (no < imagesPerFile - 1 || c < ec - 1) {
                    while (in.read() == 0) ;
                    in.seek(in.getFilePointer() - 1);
                }
            }
            int rowLen = w * bpp;
            int srcRowLen = getSizeX() * bpp;
            for (int row = 0; row < h; row++) {
                int src = (row + y) * srcRowLen + x * bpp;
                int dest = (h * c + row) * rowLen;
                int len = (int) Math.min(rowLen, t.length - src - 1);
                if (len < 0)
                    break;
                System.arraycopy(t, src, buf, dest, len);
            }
        }
    } else if (isJPEG || isJP2K) {
        // plane is compressed using JPEG or JPEG-2000
        long end = no < offsets.length - 1 ? offsets[no + 1] : in.length();
        byte[] b = new byte[(int) (end - in.getFilePointer())];
        in.read(b);
        if (b[2] != (byte) 0xff) {
            byte[] tmp = new byte[b.length + 1];
            tmp[0] = b[0];
            tmp[1] = b[1];
            tmp[2] = (byte) 0xff;
            System.arraycopy(b, 2, tmp, 3, b.length - 2);
            b = tmp;
        }
        if ((b[3] & 0xff) >= 0xf0) {
            b[3] -= (byte) 0x30;
        }
        int pt = b.length - 2;
        while (pt >= 0 && b[pt] != (byte) 0xff || b[pt + 1] != (byte) 0xd9) {
            pt--;
        }
        if (pt < b.length - 2) {
            byte[] tmp = b;
            b = new byte[pt + 2];
            System.arraycopy(tmp, 0, b, 0, b.length);
        }
        Codec codec = null;
        CodecOptions options = new CodecOptions();
        options.littleEndian = isLittleEndian();
        options.interleaved = isInterleaved();
        if (isJPEG)
            codec = new JPEGCodec();
        else
            codec = new JPEG2000Codec();
        b = codec.decompress(b, options);
        int rowLen = w * bpp;
        int srcRowLen = getSizeX() * bpp;
        int srcPlane = getSizeY() * srcRowLen;
        for (int c = 0; c < ec; c++) {
            for (int row = 0; row < h; row++) {
                System.arraycopy(b, c * srcPlane + (row + y) * srcRowLen + x * bpp, buf, h * rowLen * c + row * rowLen, rowLen);
            }
        }
    } else if (isDeflate) {
        // TODO
        throw new UnsupportedCompressionException("Deflate data is not supported.");
    } else {
        // plane is not compressed
        readPlane(in, x, y, w, h, buf);
    }
    if (inverted) {
        // white -> 255 (or 65535)
        if (bpp == 1) {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (255 - buf[i]);
            }
        } else if (bpp == 2) {
            long maxPixelValue = maxPixelRange + (centerPixelValue / 2);
            if (maxPixelRange == -1 || centerPixelValue < (maxPixelRange / 2)) {
                maxPixelValue = FormatTools.defaultMinMax(getPixelType())[1];
            }
            boolean little = isLittleEndian();
            for (int i = 0; i < buf.length; i += 2) {
                short s = DataTools.bytesToShort(buf, i, 2, little);
                DataTools.unpackBytes(maxPixelValue - s, buf, i, 2, little);
            }
        }
    }
    return buf;
}
Also used : CodecOptions(loci.formats.codec.CodecOptions) JPEG2000Codec(loci.formats.codec.JPEG2000Codec) UnsupportedCompressionException(loci.formats.UnsupportedCompressionException) JPEGCodec(loci.formats.codec.JPEGCodec) PackbitsCodec(loci.formats.codec.PackbitsCodec) Codec(loci.formats.codec.Codec) PackbitsCodec(loci.formats.codec.PackbitsCodec) JPEGCodec(loci.formats.codec.JPEGCodec) JPEG2000Codec(loci.formats.codec.JPEG2000Codec)

Aggregations

Codec (loci.formats.codec.Codec)3 CodecOptions (loci.formats.codec.CodecOptions)3 ByteArrayHandle (loci.common.ByteArrayHandle)2 RandomAccessInputStream (loci.common.RandomAccessInputStream)2 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)2 JPEGCodec (loci.formats.codec.JPEGCodec)2 PackbitsCodec (loci.formats.codec.PackbitsCodec)2 IFormatReader (loci.formats.IFormatReader)1 UnsupportedCompressionException (loci.formats.UnsupportedCompressionException)1 LosslessJPEGCodec (loci.formats.codec.LosslessJPEGCodec)1 ZlibCodec (loci.formats.codec.ZlibCodec)1