Search in sources :

Example 1 with LegacyQTTools

use of loci.formats.gui.LegacyQTTools 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();
    }
}
Also used : Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) ReflectException(loci.common.ReflectException) BufferedImage(java.awt.image.BufferedImage) FormatException(loci.formats.FormatException) LegacyQTTools(loci.formats.gui.LegacyQTTools)

Example 2 with LegacyQTTools

use of loci.formats.gui.LegacyQTTools in project bioformats by openmicroscopy.

the class LegacyQTReader method initFile.

// -- Internal FormatReader API methods --
/* @see loci.formats.FormatReader#initFile(String) */
@Override
protected void initFile(String id) throws FormatException, IOException {
    LOGGER.info("Checking for QuickTime Java");
    if (tools == null) {
        tools = new LegacyQTTools();
        r = tools.getUniverse();
    }
    tools.checkQTLibrary();
    super.initFile(id);
    LOGGER.info("Reading movie dimensions");
    try {
        r.exec("QTSession.open()");
        // open movie file
        Location file = new Location(id);
        r.setVar("path", file.getAbsolutePath());
        r.exec("qtf = new QTFile(path)");
        r.exec("openMovieFile = OpenMovieFile.asRead(qtf)");
        r.exec("m = Movie.fromFile(openMovieFile)");
        int numTracks = ((Integer) r.exec("m.getTrackCount()")).intValue();
        int trackMostLikely = 0;
        int trackNum = 0;
        while (++trackNum <= numTracks && trackMostLikely == 0) {
            r.setVar("trackNum", trackNum);
            r.exec("imageTrack = m.getTrack(trackNum)");
            r.exec("d = imageTrack.getSize()");
            Integer w = (Integer) r.exec("d.getWidth()");
            if (w.intValue() > 0)
                trackMostLikely = trackNum;
        }
        r.setVar("trackMostLikely", trackMostLikely);
        r.exec("imageTrack = m.getTrack(trackMostLikely)");
        r.exec("d = imageTrack.getSize()");
        Integer w = (Integer) r.exec("d.getWidth()");
        Integer h = (Integer) r.exec("d.getHeight()");
        r.exec("moviePlayer = new MoviePlayer(m)");
        r.setVar("dim", new Dimension(w.intValue(), h.intValue()));
        ImageProducer qtip = (ImageProducer) r.exec("qtip = new QTImageProducer(moviePlayer, dim)");
        image = Toolkit.getDefaultToolkit().createImage(qtip);
        r.setVar("zero", 0);
        r.setVar("one", 1f);
        r.exec("timeInfo = new TimeInfo(zero, zero)");
        r.exec("moviePlayer.setTime(zero)");
        Vector<Integer> v = new Vector<Integer>();
        int time = 0;
        Integer q = new Integer(time);
        do {
            v.add(q);
            r.exec("timeInfo = imageTrack.getNextInterestingTime(" + "StdQTConstants.nextTimeMediaSample, timeInfo.time, one)");
            q = (Integer) r.getVar("timeInfo.time");
            time = q.intValue();
        } while (time >= 0);
        CoreMetadata m = core.get(0);
        m.imageCount = v.size();
        times = new int[getImageCount()];
        for (int i = 0; i < times.length; i++) {
            q = v.elementAt(i);
            times[i] = q.intValue();
        }
        LOGGER.info("Populating metadata");
        BufferedImage img = AWTImageTools.makeBuffered(image);
        m.sizeX = img.getWidth();
        m.sizeY = img.getHeight();
        m.sizeZ = 1;
        m.sizeC = img.getRaster().getNumBands();
        m.sizeT = getImageCount();
        m.pixelType = AWTImageTools.getPixelType(img);
        m.dimensionOrder = "XYCTZ";
        m.rgb = true;
        m.interleaved = false;
        m.littleEndian = false;
        m.indexed = false;
        m.falseColor = false;
        MetadataStore store = makeFilterMetadata();
        MetadataTools.populatePixels(store, this);
    } catch (ReflectException e) {
        throw new FormatException("Open movie failed", e);
    }
}
Also used : Dimension(java.awt.Dimension) CoreMetadata(loci.formats.CoreMetadata) ReflectException(loci.common.ReflectException) BufferedImage(java.awt.image.BufferedImage) FormatException(loci.formats.FormatException) MetadataStore(loci.formats.meta.MetadataStore) LegacyQTTools(loci.formats.gui.LegacyQTTools) ImageProducer(java.awt.image.ImageProducer) Vector(java.util.Vector) Location(loci.common.Location)

Aggregations

BufferedImage (java.awt.image.BufferedImage)2 ReflectException (loci.common.ReflectException)2 FormatException (loci.formats.FormatException)2 LegacyQTTools (loci.formats.gui.LegacyQTTools)2 Dimension (java.awt.Dimension)1 Image (java.awt.Image)1 ImageProducer (java.awt.image.ImageProducer)1 Vector (java.util.Vector)1 Location (loci.common.Location)1 CoreMetadata (loci.formats.CoreMetadata)1 MetadataStore (loci.formats.meta.MetadataStore)1