Search in sources :

Example 1 with ReflectedUniverse

use of loci.common.ReflectedUniverse 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;
}
Also used : ReflectedUniverse(loci.common.ReflectedUniverse) ReflectException(loci.common.ReflectException)

Example 2 with ReflectedUniverse

use of loci.common.ReflectedUniverse in project bioformats by openmicroscopy.

the class LegacyQTTools method initClass.

// -- LegacyQTTools API methods --
/**
 * Initializes the class.
 */
protected void initClass() {
    if (initialized)
        return;
    String arch = System.getProperty("os.arch");
    if (arch != null && arch.indexOf("64") >= 0) {
        // QTJava is not supported on 64-bit Java; don't even try
        noQT = true;
        jvm64Bit = true;
        initialized = true;
        return;
    }
    boolean needClose = false;
    r = new ReflectedUniverse(LOADER);
    try {
        r.exec("import quicktime.QTSession");
        r.exec("QTSession.open()");
        needClose = true;
        // for LegacyQTReader and LegacyQTWriter
        r.exec("import quicktime.io.QTFile");
        r.exec("import quicktime.std.movies.Movie");
        // for LegacyQTReader
        r.exec("import quicktime.app.view.MoviePlayer");
        r.exec("import quicktime.app.view.QTImageProducer");
        r.exec("import quicktime.io.OpenMovieFile");
        r.exec("import quicktime.qd.QDDimension");
        r.exec("import quicktime.std.StdQTConstants");
        r.exec("import quicktime.std.movies.TimeInfo");
        r.exec("import quicktime.std.movies.Track");
        // for LegacyQTWriter
        r.exec("import quicktime.qd.QDGraphics");
        r.exec("import quicktime.qd.QDRect");
        r.exec("import quicktime.std.image.CSequence");
        r.exec("import quicktime.std.image.CodecComponent");
        r.exec("import quicktime.std.image.ImageDescription");
        r.exec("import quicktime.std.movies.media.VideoMedia");
        r.exec("import quicktime.util.QTHandle");
        r.exec("import quicktime.util.RawEncodedImage");
        r.exec("import quicktime.util.EndianOrder");
    } catch (ExceptionInInitializerError err) {
        noQT = true;
        Throwable t = err.getException();
        if (t instanceof SecurityException) {
            SecurityException exc = (SecurityException) t;
            if (exc.getMessage().indexOf("expired") >= 0)
                expiredQT = true;
        }
    } catch (Throwable t) {
        noQT = true;
        LOGGER.debug("Could not find QuickTime for Java", t);
    } finally {
        if (needClose) {
            try {
                r.exec("QTSession.close()");
            } catch (Throwable t) {
                LOGGER.debug("Could not close QuickTime session", t);
            }
        }
        initialized = true;
    }
}
Also used : ReflectedUniverse(loci.common.ReflectedUniverse)

Example 3 with ReflectedUniverse

use of loci.common.ReflectedUniverse in project bioformats by openmicroscopy.

the class DisplayHandler method displayImage5D.

public void displayImage5D(ImagePlus imp) {
    WindowManager.setTempCurrentImage(imp);
    IFormatReader r = process.getReader();
    ReflectedUniverse ru = new ReflectedUniverse();
    try {
        ru.exec("import i5d.Image5D");
        ru.setVar("title", imp.getTitle());
        ru.setVar("stack", imp.getStack());
        ru.setVar("sizeC", imp.getNChannels());
        ru.setVar("sizeZ", imp.getNSlices());
        ru.setVar("sizeT", imp.getNFrames());
        ru.exec("i5d = new Image5D(title, stack, sizeC, sizeZ, sizeT)");
        ru.setVar("cal", imp.getCalibration());
        ru.setVar("fi", imp.getOriginalFileInfo());
        ru.exec("i5d.setCalibration(cal)");
        ru.exec("i5d.setFileInfo(fi)");
        // ru.exec("i5d.setDimensions(sizeC, sizeZ, sizeT)");
        ru.exec("i5d.show()");
    } catch (ReflectException exc) {
        WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem interfacing with Image5D");
    }
}
Also used : IFormatReader(loci.formats.IFormatReader) ReflectedUniverse(loci.common.ReflectedUniverse) ReflectException(loci.common.ReflectException)

Example 4 with ReflectedUniverse

use of loci.common.ReflectedUniverse in project bioformats by openmicroscopy.

the class ImportProcess method createBaseReader.

/**
 * Initializes an {@link loci.formats.IFormatReader}
 * according to the current configuration.
 */
private void createBaseReader() throws FormatException, IOException {
    if (options.isLocal() || options.isHTTP()) {
        BF.status(options.isQuiet(), "Identifying " + idName);
        imageReader = LociPrefs.makeImageReader();
        baseReader = imageReader.getReader(options.isUsingPatternIds() ? new FilePattern(options.getId()).getFiles()[0] : options.getId());
    } else if (options.isOMERO()) {
        BF.status(options.isQuiet(), "Establishing server connection");
        try {
            ReflectedUniverse r = new ReflectedUniverse();
            r.exec("import loci.ome.io.OmeroReader");
            r.exec("baseReader = new OmeroReader()");
            ClassList<IFormatReader> classes = new ClassList<IFormatReader>(IFormatReader.class);
            r.setVar("classes", classes);
            r.exec("class = baseReader.getClass()");
            r.exec("classes.addClass(class)");
            imageReader = new ImageReader(classes);
            baseReader = imageReader.getReader(options.getId());
        } catch (Exception exc) {
            WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem communicating with the server.");
            cancel();
            return;
        }
    } else {
        WindowTools.reportException(null, options.isQuiet(), "Sorry, there has been an internal error: unknown data source");
        cancel();
        return;
    }
    // attach OME-XML metadata store
    Exception exc = null;
    try {
        ServiceFactory factory = new ServiceFactory();
        OMEXMLService service = factory.getInstance(OMEXMLService.class);
        meta = service.createOMEXMLMetadata();
        omeXML = null;
    } catch (DependencyException de) {
        exc = de;
    } catch (ServiceException se) {
        exc = se;
    }
    if (exc != null) {
        WindowTools.reportException(exc, options.isQuiet(), "Sorry, there was a problem constructing the OME-XML metadata store");
        throw new FormatException(exc);
    }
    baseReader.setMetadataStore(meta);
    BF.status(options.isQuiet(), "");
    DebugTools.enableIJLogging(IJ.debugMode);
}
Also used : IFormatReader(loci.formats.IFormatReader) ServiceException(loci.common.services.ServiceException) ServiceFactory(loci.common.services.ServiceFactory) FilePattern(loci.formats.FilePattern) ReflectedUniverse(loci.common.ReflectedUniverse) ClassList(loci.formats.ClassList) ImageReader(loci.formats.ImageReader) DependencyException(loci.common.services.DependencyException) EnumerationException(ome.xml.model.enums.EnumerationException) ServiceException(loci.common.services.ServiceException) DependencyException(loci.common.services.DependencyException) FormatException(loci.formats.FormatException) IOException(java.io.IOException) OMEXMLService(loci.formats.services.OMEXMLService) FormatException(loci.formats.FormatException)

Example 5 with ReflectedUniverse

use of loci.common.ReflectedUniverse in project bioformats by openmicroscopy.

the class MetaSupportList method format.

/**
 * Gets the name of the format for the current handler.
 */
public String format() {
    ReflectedUniverse r = new ReflectedUniverse();
    IFormatHandler handler;
    try {
        r.exec("import loci.formats.in." + handlerName);
    } catch (ReflectException exc) {
    }
    try {
        r.exec("import loci.formats.out." + handlerName);
    } catch (ReflectException exc) {
    }
    try {
        handler = (IFormatHandler) r.exec("new " + handlerName + "()");
        return handler.getFormat();
    } catch (ReflectException exc) {
    }
    return null;
}
Also used : IFormatHandler(loci.formats.IFormatHandler) ReflectedUniverse(loci.common.ReflectedUniverse) ReflectException(loci.common.ReflectException)

Aggregations

ReflectedUniverse (loci.common.ReflectedUniverse)6 ReflectException (loci.common.ReflectException)4 FormatException (loci.formats.FormatException)2 IFormatReader (loci.formats.IFormatReader)2 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 FileHandle (loci.common.FileHandle)1 IRandomAccess (loci.common.IRandomAccess)1 DependencyException (loci.common.services.DependencyException)1 ServiceException (loci.common.services.ServiceException)1 ServiceFactory (loci.common.services.ServiceFactory)1 ClassList (loci.formats.ClassList)1 CoreMetadata (loci.formats.CoreMetadata)1 FilePattern (loci.formats.FilePattern)1 IFormatHandler (loci.formats.IFormatHandler)1 ImageReader (loci.formats.ImageReader)1 MissingLibraryException (loci.formats.MissingLibraryException)1 MetadataStore (loci.formats.meta.MetadataStore)1