use of loci.formats.codec.MSVideoCodec in project bioformats by openmicroscopy.
the class AVIReader method uncompress.
// -- Helper methods --
private byte[] uncompress(int no, byte[] buf) throws FormatException, IOException {
if (lastImageNo == no) {
buf = lastImage;
return buf;
}
CodecOptions options = new CodecOptions();
options.width = getSizeX();
options.height = getSizeY();
options.previousImage = (lastImageNo == no - 1) ? lastImage : null;
if (options.previousImage == null && bmpCompression != JPEG) {
while (lastImageNo < no - 1) {
openBytes(lastImageNo + 1, buf);
}
options.previousImage = lastImage;
}
long fileOff = offsets.get(no).longValue();
in.seek(fileOff);
options.bitsPerSample = bmpBitsPerPixel;
options.interleaved = isInterleaved();
options.littleEndian = isLittleEndian();
if (bmpCompression == MSRLE) {
byte[] b = new byte[(int) lengths.get(no).longValue()];
in.read(b);
MSRLECodec codec = new MSRLECodec();
buf = codec.decompress(b, options);
} else if (bmpCompression == MS_VIDEO) {
MSVideoCodec codec = new MSVideoCodec();
buf = codec.decompress(in, options);
} else if (bmpCompression == JPEG) {
JPEGCodec codec = new JPEGCodec();
byte[] plane = new byte[(int) lengths.get(no).longValue()];
in.read(plane);
boolean motionJPEG = plane.length >= 10 && new String(plane, 6, 4, Constants.ENCODING).equals("AVI1");
if (motionJPEG) {
// this is Motion JPEG data
// we must manually insert the Huffman table, as Motion JPEG
// uses a fixed (but not stored) Huffman table for all planes
byte[] fixedPlane = new byte[plane.length + MJPEG_HUFFMAN_TABLE.length];
System.arraycopy(plane, 0, fixedPlane, 0, 20);
System.arraycopy(MJPEG_HUFFMAN_TABLE, 0, fixedPlane, 20, MJPEG_HUFFMAN_TABLE.length);
System.arraycopy(plane, 20, fixedPlane, 20 + MJPEG_HUFFMAN_TABLE.length, plane.length - 20);
plane = fixedPlane;
}
if (plane.length > 0) {
buf = codec.decompress(plane, options);
} else {
buf = lastImage;
}
if (!lengths.contains(0L)) {
motionJPEG = false;
}
if (motionJPEG) {
for (int i = 0; i < buf.length; i += 3) {
int y = buf[i] & 0xff;
int cb = (buf[i + 1] & 0xff) - 128;
int cr = (buf[i + 2] & 0xff) - 128;
int red = (int) (y + 1.402 * cr);
int green = (int) (y - 0.34414 * cb - 0.71414 * cr);
int blue = (int) (y + 1.772 * cb);
if (red < 0) {
red = 0;
} else if (red > 255) {
red = 255;
}
if (green < 0) {
green = 0;
} else if (green > 255) {
green = 255;
}
if (blue < 0) {
blue = 0;
} else if (blue > 255) {
blue = 255;
}
buf[i] = (byte) (red & 0xff);
buf[i + 1] = (byte) (green & 0xff);
buf[i + 2] = (byte) (blue & 0xff);
}
}
} else /*
else if (bmpCompression == CINEPAK) {
Object[] options = new Object[2];
options[0] = new Integer(bmpBitsPerPixel);
options[1] = lastImage;
CinepakCodec codec = new CinepakCodec();
buf = codec.decompress(b, options);
lastImage = buf;
if (no == m.imageCount - 1) lastImage = null;
return buf;
}
*/
{
throw new UnsupportedCompressionException(bmpCompression + " not supported");
}
lastImage = buf;
lastImageNo = no;
return buf;
}
Aggregations