private byte[] decodeTile(int no, int row, int col) throws FormatException, IOException { if (tileMap[getCoreIndex()] == null) { return new byte[getTileSize()]; } int[] zct = getZCTCoords(no); TileCoordinate t = new TileCoordinate(nDimensions[getCoreIndex()]); t.coordinate[0] = col; t.coordinate[1] = row; for (String dim : dimensionOrdering.keySet()) { int index = 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]; } } Integer index = (Integer) tileMap[getCoreIndex()].get(t); if (index == null) { return new byte[getTileSize()]; } Long offset = tileOffsets[getCoreIndex()][index]; RandomAccessInputStream ets = new RandomAccessInputStream(usedFiles[getCoreIndex()]); ets.seek(offset); CodecOptions options = new CodecOptions(); options.interleaved = isInterleaved(); options.littleEndian = isLittleEndian(); int tileSize = getTileSize(); if (tileSize == 0) { tileSize = tileX[getCoreIndex()] * tileY[getCoreIndex()] * 10; } options.maxBytes = (int) (offset + tileSize); byte[] buf = null; long end = index < tileOffsets[getCoreIndex()].length - 1 ? tileOffsets[getCoreIndex()][index + 1] : ets.length(); IFormatReader reader = null; String file = null; switch (compressionType[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 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; }
/** @see loci.formats.IFormatReader#openBytes(int, byte[], int, int, int, int) */ 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 index = no; for (int i = 0; i < series; i++) { index += core[i].imageCount; } if (index >= binDataOffsets.size()) { index = binDataOffsets.size() - 1; } long offset = binDataOffsets.get(index).longValue(); String compress = compression.get(index); in.seek(offset); int depth = FormatTools.getBytesPerPixel(getPixelType()); int planeSize = getSizeX() * getSizeY() * depth; CodecOptions options = new CodecOptions(); options.width = getSizeX(); options.height = getSizeY(); options.bitsPerSample = depth * 8; options.channels = getRGBChannelCount(); options.maxBytes = planeSize; options.littleEndian = isLittleEndian(); options.interleaved = isInterleaved(); byte[] pixels = new Base64Codec().decompress(in, options); // return a blank plane if no pixel data was stored if (pixels.length == 0) { LOGGER.debug("No pixel data for plane #{}", no); return buf; } // TODO: Create a method uncompress to handle all compression methods if (compress.equals("bzip2")) { byte[] tempPixels = pixels; pixels = new byte[tempPixels.length - 2]; System.arraycopy(tempPixels, 2, pixels, 0, pixels.length); ByteArrayInputStream bais = new ByteArrayInputStream(pixels); CBZip2InputStream bzip = new CBZip2InputStream(bais); pixels = new byte[planeSize]; bzip.read(pixels, 0, pixels.length); tempPixels = null; bais.close(); bais = null; bzip = null; } else if (compress.equals("zlib")) { pixels = new ZlibCodec().decompress(pixels, options); } else if (compress.equals("J2K")) { pixels = new JPEG2000Codec().decompress(pixels, options); } else if (compress.equals("JPEG")) { pixels = new JPEGCodec().decompress(pixels, options); } for (int row = 0; row < h; row++) { int off = (row + y) * getSizeX() * depth + x * depth; System.arraycopy(pixels, off, buf, row * w * depth, w * depth); } pixels = null; return buf; }