Search in sources :

Example 1 with LosslessJPEGCodec

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

Aggregations

ByteArrayHandle (loci.common.ByteArrayHandle)1 RandomAccessInputStream (loci.common.RandomAccessInputStream)1 IFormatReader (loci.formats.IFormatReader)1 Codec (loci.formats.codec.Codec)1 CodecOptions (loci.formats.codec.CodecOptions)1 JPEG2000Codec (loci.formats.codec.JPEG2000Codec)1 JPEGCodec (loci.formats.codec.JPEGCodec)1 LosslessJPEGCodec (loci.formats.codec.LosslessJPEGCodec)1