use of loci.formats.codec.JPEG2000Codec in project bioformats by openmicroscopy.
the class OMEXMLWriter method compress.
// -- Helper methods --
/**
* Compress the given byte array using the current codec.
* The compressed data is then base64-encoded.
*/
private byte[] compress(byte[] b) throws FormatException, IOException {
MetadataRetrieve r = getMetadataRetrieve();
String type = r.getPixelsType(series).toString();
int pixelType = FormatTools.pixelTypeFromString(type);
int bytes = FormatTools.getBytesPerPixel(pixelType);
CodecOptions options = new CodecOptions();
options.width = r.getPixelsSizeX(series).getValue().intValue();
options.height = r.getPixelsSizeY(series).getValue().intValue();
options.channels = 1;
options.interleaved = false;
options.signed = FormatTools.isSigned(pixelType);
boolean littleEndian = false;
if (r.getPixelsBigEndian(series) != null) {
littleEndian = !r.getPixelsBigEndian(series).booleanValue();
} else if (r.getPixelsBinDataCount(series) == 0) {
littleEndian = !r.getPixelsBinDataBigEndian(series, 0).booleanValue();
}
options.littleEndian = littleEndian;
options.bitsPerSample = bytes * 8;
if (compression.equals("J2K")) {
b = new JPEG2000Codec().compress(b, options);
} else if (compression.equals("JPEG")) {
b = new JPEGCodec().compress(b, options);
} else if (compression.equals("zlib")) {
b = new ZlibCodec().compress(b, options);
}
return new Base64Codec().compress(b, options);
}
use of loci.formats.codec.JPEG2000Codec in project bioformats by openmicroscopy.
the class JPEG2000Reader 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);
if (lastSeries == getCoreIndex() && lastSeriesPlane != null) {
RandomAccessInputStream s = new RandomAccessInputStream(lastSeriesPlane);
readPlane(s, x, y, w, h, buf);
s.close();
return buf;
}
JPEG2000CodecOptions options = JPEG2000CodecOptions.getDefaultOptions();
options.interleaved = isInterleaved();
options.littleEndian = isLittleEndian();
if (resolutionLevels != null) {
options.resolution = Math.abs(getCoreIndex() - resolutionLevels);
} else if (core.size() > 1) {
options.resolution = getCoreIndex();
}
in.seek(pixelsOffset);
lastSeriesPlane = new JPEG2000Codec().decompress(in, options);
RandomAccessInputStream s = new RandomAccessInputStream(lastSeriesPlane);
readPlane(s, x, y, w, h, buf);
s.close();
lastSeries = getCoreIndex();
return buf;
}
use of loci.formats.codec.JPEG2000Codec 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;
}
use of loci.formats.codec.JPEG2000Codec 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;
}
use of loci.formats.codec.JPEG2000Codec 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;
}
Aggregations