use of loci.common.ReflectException in project bioformats by openmicroscopy.
the class LegacyQTWriter method savePlane.
/**
* @see loci.formats.IFormatWriter#savePlane(int, Object, int, int, int, int)
*/
@Override
public void savePlane(int no, Object plane, int x, int y, int w, int h) throws FormatException, IOException {
if (!(plane instanceof Image)) {
throw new IllegalArgumentException("Object to save must be a java.awt.Image");
}
if (tools == null || r == null) {
tools = new LegacyQTTools();
r = tools.getUniverse();
}
tools.checkQTLibrary();
BufferedImage img = AWTImageTools.makeBuffered((Image) plane);
if (!initialized[series][no]) {
initialized[series][no] = true;
try {
r.exec("QTSession.open()");
width = img.getWidth();
height = img.getHeight();
r.setVar("path", currentId);
r.setVar("width", (float) width);
r.setVar("height", (float) height);
r.exec("movFile = new QTFile(path)");
r.exec("kMoviePlayer = StdQTConstants.kMoviePlayer");
int resFlag = ((Integer) r.exec("StdQTConstants.createMovieFileDontCreateResFile")).intValue();
r.setVar("flags", resFlag);
r.exec("movie = Movie.createMovieFile(movFile, kMoviePlayer, flags)");
r.setVar("timeScale", TIME_SCALE);
r.setVar("zero", 0);
r.setVar("zeroFloat", (float) 0);
r.exec("videoTrack = movie.addTrack(width, height, zeroFloat)");
r.exec("videoMedia = new VideoMedia(videoTrack, timeScale)");
r.exec("videoMedia.beginEdits()");
r.setVar("width", width);
r.setVar("height", height);
r.exec("bounds = new QDRect(zero, zero, width, height)");
r.exec("gw = new QDGraphics(bounds)");
r.exec("pixMap = gw.getPixMap()");
r.exec("pixSize = pixMap.getPixelSize()");
r.setVar("codec", codec);
r.setVar("quality", quality);
int rawImageSize = width * height * 4;
r.setVar("rawImageSize", rawImageSize);
r.setVar("boolTrue", true);
r.exec("imageHandle = new QTHandle(rawImageSize, boolTrue)");
r.exec("imageHandle.lock()");
r.exec("compressedImage = RawEncodedImage.fromQTHandle(imageHandle)");
r.setVar("rate", 30);
r.exec("seq = new CSequence(gw, bounds, pixSize, codec, " + "CodecComponent.bestFidelityCodec, quality, quality, rate, null, " + "zero)");
r.exec("imgDesc = seq.getDescription()");
} catch (ReflectException e) {
LOGGER.debug("", e);
throw new FormatException("Legacy QuickTime writer failed", e);
}
}
numWritten++;
try {
r.exec("pixelData = pixMap.getPixelData()");
r.exec("intsPerRow = pixelData.getRowBytes()");
int intsPerRow = ((Integer) r.getVar("intsPerRow")).intValue() / 4;
byte[][] px = AWTImageTools.getBytes(img);
int[] pixels = new int[px[0].length];
for (int i = 0; i < pixels.length; i++) {
byte[] b = new byte[4];
for (int j = 0; j < px.length; j++) {
b[j] = px[j][i];
}
for (int j = px.length; j < 4; j++) {
b[j] = px[j % px.length][i];
}
pixels[i] = DataTools.bytesToInt(b, true);
}
if (pixels2 == null)
pixels2 = new int[intsPerRow * height];
r.exec("nativeLittle = EndianOrder.isNativeLittleEndian()");
boolean nativeLittle = ((Boolean) r.getVar("nativeLittle")).booleanValue();
if (nativeLittle) {
int offset1, offset2;
for (int row = 0; row < height; row++) {
offset1 = row * width;
offset2 = row * intsPerRow;
for (int col = 0; col < width; col++) {
r.setVar("thisByte", pixels[offset1++]);
r.exec("b = EndianOrder.flipBigEndianToNative32(thisByte)");
pixels2[offset2++] = ((Integer) r.getVar("b")).intValue();
}
}
} else {
for (int i = 0; i < height; i++) {
System.arraycopy(pixels, i * width, pixels2, i * intsPerRow, width);
}
}
r.setVar("pixels2", pixels2);
r.setVar("len", intsPerRow * height);
r.exec("pixelData.copyFromArray(zero, pixels2, zero, len)");
r.exec("flags = StdQTConstants.codecFlagUpdatePrevious");
r.exec("cfInfo = seq.compressFrame(gw, bounds, flags, compressedImage)");
// see developer.apple.com/qa/qtmcc/qtmcc20.html
r.exec("similarity = cfInfo.getSimilarity()");
int sim = ((Integer) r.getVar("similarity")).intValue();
boolean sync = sim == 0;
r.exec("dataSize = cfInfo.getDataSize()");
r.setVar("fps", fps);
r.setVar("frameRate", TIME_SCALE);
r.setVar("rate", TIME_SCALE / fps);
if (sync) {
r.setVar("sync", 0);
} else
r.exec("sync = StdQTConstants.mediaSampleNotSync");
r.setVar("one", 1);
r.exec("videoMedia.addSample(imageHandle, zero, dataSize, " + "rate, imgDesc, one, sync)");
} catch (ReflectException e) {
LOGGER.debug("", e);
throw new FormatException("Legacy QuickTime writer failed", e);
}
if (no == getPlaneCount() - 1) {
try {
r.exec("videoMedia.endEdits()");
r.exec("duration = videoMedia.getDuration()");
r.setVar("floatOne", (float) 1.0);
r.exec("videoTrack.insertMedia(zero, zero, duration, floatOne)");
r.exec("omf = OpenMovieFile.asWrite(movFile)");
r.exec("name = movFile.getName()");
r.exec("flags = StdQTConstants.movieInDataForkResID");
r.exec("movie.addResource(omf, flags, name)");
r.exec("QTSession.close()");
} catch (ReflectException e) {
LOGGER.debug("", e);
throw new FormatException("Legacy QuickTime writer failed", e);
}
close();
}
}
use of loci.common.ReflectException in project bioformats by openmicroscopy.
the class LegacyQTTools method pictToImage.
/**
* Converts the given byte array in PICT format to a Java image.
*/
public synchronized Image pictToImage(byte[] bytes) throws FormatException {
checkQTLibrary();
try {
r.exec("QTSession.open()");
// Code adapted from:
// http://www.onjava.com/pub/a/onjava/2002/12/23/jmf.html?page=2
r.setVar("bytes", bytes);
r.exec("pict = new Pict(bytes)");
r.exec("box = pict.getPictFrame()");
int width = ((Integer) r.exec("box.getWidth()")).intValue();
int height = ((Integer) r.exec("box.getHeight()")).intValue();
// note: could get a RawEncodedImage from the Pict, but
// apparently no way to get a PixMap from the REI
r.exec("g = new QDGraphics(box)");
r.exec("pict.draw(g, box)");
// get data from the QDGraphics
r.exec("pixMap = g.getPixMap()");
r.exec("rei = pixMap.getPixelData()");
// copy bytes to an array
int rowBytes = ((Integer) r.exec("pixMap.getRowBytes()")).intValue();
int intsPerRow = rowBytes / 4;
int pixLen = intsPerRow * height;
r.setVar("pixLen", pixLen);
int[] pixels = new int[pixLen];
r.setVar("pixels", pixels);
r.setVar("zero", new Integer(0));
r.exec("rei.copyToArray(zero, pixels, zero, pixLen)");
// now coax into image, ignoring alpha for speed
int bitsPerSample = 32;
int redMask = 0x00ff0000;
int greenMask = 0x0000ff00;
int blueMask = 0x000000ff;
int alphaMask = 0x00000000;
DirectColorModel colorModel = new DirectColorModel(bitsPerSample, redMask, greenMask, blueMask, alphaMask);
r.exec("QTSession.close()");
return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width, height, colorModel, pixels, 0, intsPerRow));
} catch (ReflectException e) {
try {
r.exec("QTSession.close()");
} catch (ReflectException exc) {
LOGGER.info("Could not close QuickTime session", exc);
}
throw new FormatException("PICT extraction failed", e);
}
}
use of loci.common.ReflectException in project bioformats by openmicroscopy.
the class LegacyQTTools method getPictDimensions.
/**
* Gets width and height for the given PICT bytes.
*/
public Dimension getPictDimensions(byte[] bytes) throws FormatException, ReflectException {
checkQTLibrary();
try {
r.exec("QTSession.open()");
r.setVar("bytes", bytes);
r.exec("pict = new Pict(bytes)");
r.exec("box = pict.getPictFrame()");
int width = ((Integer) r.exec("box.getWidth()")).intValue();
int height = ((Integer) r.exec("box.getHeight()")).intValue();
r.exec("QTSession.close()");
return new Dimension(width, height);
} catch (ReflectException e) {
r.exec("QTSession.close()");
throw new FormatException("PICT height determination failed", e);
}
}
use of loci.common.ReflectException in project bioformats by openmicroscopy.
the class LegacyQTReader method openPlane.
// -- IFormatReader API methods --
/* @see loci.formats.IFormatReader#openPlane(int, int, int, int, int int) */
@Override
public Object openPlane(int no, int x, int y, int w, int h) throws FormatException, IOException {
FormatTools.checkPlaneParameters(this, no, -1, x, y, w, h);
// paint frame into image
try {
r.setVar("time", times[no]);
r.exec("moviePlayer.setTime(time)");
r.exec("qtip.redraw(null)");
r.exec("qtip.updateConsumers(null)");
} catch (ReflectException re) {
throw new FormatException("Open movie failed", re);
}
return AWTImageTools.getSubimage(AWTImageTools.makeBuffered(image), isLittleEndian(), x, y, w, h);
}
use of loci.common.ReflectException in project bioformats by openmicroscopy.
the class FormatTools method openThumbBytes.
/**
* Default implementation for {@link IFormatReader#openThumbBytes}.
*
* At the moment, it uses {@link java.awt.image.BufferedImage} objects
* to resize thumbnails, so it is not safe for use in headless contexts.
* In the future, we may reimplement the image scaling logic purely with
* byte arrays, but handling every case would be substantial effort, so
* doing so is currently a low priority item.
*/
public static byte[] openThumbBytes(IFormatReader reader, int no) throws FormatException, IOException {
// NB: Dependency on AWT here is unfortunate, but very difficult to
// eliminate in general. We use reflection to limit class loading
// problems with AWT on Mac OS X.
ReflectedUniverse r = new ReflectedUniverse();
byte[][] bytes = null;
try {
r.exec("import loci.formats.gui.AWTImageTools");
int planeSize = getPlaneSize(reader);
byte[] plane = null;
if (planeSize < 0) {
int width = reader.getThumbSizeX() * 4;
int height = reader.getThumbSizeY() * 4;
int x = (reader.getSizeX() - width) / 2;
int y = (reader.getSizeY() - height) / 2;
plane = reader.openBytes(no, x, y, width, height);
} else {
plane = reader.openBytes(no);
}
r.setVar("plane", plane);
r.setVar("reader", reader);
r.setVar("sizeX", reader.getSizeX());
r.setVar("sizeY", reader.getSizeY());
r.setVar("thumbSizeX", reader.getThumbSizeX());
r.setVar("thumbSizeY", reader.getThumbSizeY());
r.setVar("little", reader.isLittleEndian());
// always normalize floating point images, otherwise scaling will fail
r.setVar("normal", true);
r.exec("img = AWTImageTools.openImage(plane, reader, sizeX, sizeY, normal)");
r.exec("img = AWTImageTools.makeUnsigned(img)");
r.exec("thumb = AWTImageTools.scale(img, thumbSizeX, thumbSizeY, false)");
bytes = (byte[][]) r.exec("AWTImageTools.getPixelBytes(thumb, little)");
} catch (ReflectException exc) {
throw new FormatException(exc);
}
if (bytes.length == 1)
return bytes[0];
int rgbChannelCount = reader.getRGBChannelCount();
byte[] rtn = new byte[rgbChannelCount * bytes[0].length];
if (!reader.isInterleaved()) {
for (int i = 0; i < rgbChannelCount; i++) {
System.arraycopy(bytes[i], 0, rtn, bytes[0].length * i, bytes[i].length);
}
} else {
int bpp = FormatTools.getBytesPerPixel(reader.getPixelType());
for (int i = 0; i < bytes[0].length; i += bpp) {
for (int j = 0; j < rgbChannelCount; j++) {
System.arraycopy(bytes[j], i, rtn, (i * rgbChannelCount) + j * bpp, bpp);
}
}
}
return rtn;
}
Aggregations