use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.
the class OMEXMLReader 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 (binDataOffsets.size() == 0)
return buf;
FormatTools.checkPlaneParameters(this, no, buf.length, x, y, w, h);
int index = no;
int series = getSeries();
for (int i = 0; i < series; i++) {
index += core.get(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();
String encoded = in.readString("<");
encoded = encoded.trim();
if (encoded.length() == 0 || encoded.equals("<")) {
LOGGER.debug("No pixel data for plane #{}", no);
return buf;
}
encoded = encoded.substring(0, encoded.length() - 1);
byte[] pixels = BaseEncoding.base64().decode(encoded);
// 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();
bzip.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;
}
use of loci.formats.codec.ZlibCodec in project bioformats by openmicroscopy.
the class PhotoshopTiffReader method initFile.
// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
super.initFile(id);
Object sourceData = ifds.get(0).getIFDValue(IMAGE_SOURCE_DATA);
byte[] b = null;
if (sourceData instanceof byte[]) {
b = (byte[]) sourceData;
} else if (sourceData instanceof TiffIFDEntry) {
b = (byte[]) tiffParser.getIFDValue((TiffIFDEntry) sourceData);
}
if (b == null)
return;
tag = new RandomAccessInputStream(b);
tag.order(isLittleEndian());
String checkString = tag.readCString();
String signature, type;
int length;
while (tag.getFilePointer() < tag.length() - 12 && tag.getFilePointer() > 0) {
signature = tag.readString(4);
type = tag.readString(4);
length = tag.readInt();
int skip = length % 4;
if (skip != 0)
skip = 4 - skip;
if (type.equals("ryaL")) {
int nLayers = (int) Math.abs(tag.readShort());
compression = new int[nLayers];
layerNames = new String[nLayers];
channelOrder = new int[nLayers][];
int[][] dataSize = new int[nLayers][];
int offsetCount = 0;
for (int layer = 0; layer < nLayers; layer++) {
int top = tag.readInt();
int left = tag.readInt();
int bottom = tag.readInt();
int right = tag.readInt();
CoreMetadata layerCore = new CoreMetadata();
layerCore = new CoreMetadata();
layerCore.sizeX = right - left;
layerCore.sizeY = bottom - top;
layerCore.pixelType = getPixelType();
layerCore.sizeC = tag.readShort();
layerCore.sizeZ = 1;
layerCore.sizeT = 1;
layerCore.imageCount = 1;
layerCore.rgb = isRGB();
layerCore.interleaved = isInterleaved();
layerCore.littleEndian = isLittleEndian();
layerCore.dimensionOrder = getDimensionOrder();
if (layerCore.sizeX == 0 || layerCore.sizeY == 0 || (layerCore.sizeC > 1 && !isRGB())) {
// Set size to 1
CoreMetadata ms0 = core.get(0);
core.clear();
core.add(ms0);
break;
}
offsetCount += layerCore.sizeC;
channelOrder[layer] = new int[layerCore.sizeC];
dataSize[layer] = new int[layerCore.sizeC];
for (int c = 0; c < layerCore.sizeC; c++) {
int channelID = tag.readShort();
if (channelID < 0)
channelID = layerCore.sizeC - 1;
channelOrder[layer][channelID] = c;
dataSize[layer][c] = tag.readInt();
}
tag.skipBytes(12);
int len = tag.readInt();
long fp = tag.getFilePointer();
int mask = tag.readInt();
if (mask != 0)
tag.skipBytes(mask);
int blending = tag.readInt();
tag.skipBytes(blending);
int nameLength = tag.read();
int pad = nameLength % 4;
if (pad != 0)
pad = 4 - pad;
layerNames[layer] = tag.readString(nameLength + pad);
layerNames[layer] = layerNames[layer].replaceAll("[^\\p{ASCII}]", "").trim();
if (layerNames[layer].length() == nameLength + pad && !layerNames[layer].equalsIgnoreCase("Layer " + layer + "M")) {
addGlobalMetaList("Layer name", layerNames[layer]);
core.add(layerCore);
}
tag.skipBytes((int) (fp + len - tag.getFilePointer()));
}
nLayers = core.size() - 1;
layerOffset = new long[offsetCount];
int nextOffset = 0;
for (int layer = 0; layer < nLayers; layer++) {
for (int c = 0; c < core.get(layer + 1).sizeC; c++) {
long startFP = tag.getFilePointer();
compression[layer] = tag.readShort();
layerOffset[nextOffset] = tag.getFilePointer();
if (compression[layer] == ZIP) {
layerOffset[nextOffset] = tag.getFilePointer();
ZlibCodec codec = new ZlibCodec();
codec.decompress(tag, null);
} else if (compression[layer] == PACKBITS) {
if (layer == 0) {
tag.skipBytes(256 * 6 + 36);
} else
tag.skipBytes(192);
layerOffset[nextOffset] = tag.getFilePointer();
PackbitsCodec codec = new PackbitsCodec();
CodecOptions options = new CodecOptions();
options.maxBytes = core.get(layer + 1).sizeX * core.get(layer + 1).sizeY;
codec.decompress(tag, options);
}
tag.seek(startFP + dataSize[layer][c]);
nextOffset++;
}
}
} else
tag.skipBytes(length + skip);
}
MetadataStore store = makeFilterMetadata();
MetadataTools.populatePixels(store, this);
store.setImageName("Merged", 0);
if (layerNames != null) {
int end = (int) Math.min(getSeriesCount() - 1, layerNames.length);
for (int layer = 0; layer < end; layer++) {
store.setImageName(layerNames[layer], layer + 1);
}
}
}
Aggregations