Search in sources :

Example 6 with ReflectedUniverse

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

the class TiffJAIReader 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 JAI");
    try {
        r = new ReflectedUniverse();
        r.exec("import javax.media.jai.NullOpImage");
        r.exec("import javax.media.jai.OpImage");
        r.exec("import com.sun.media.jai.codec.FileSeekableStream");
        r.exec("import com.sun.media.jai.codec.ImageDecoder");
        r.exec("import com.sun.media.jai.codec.ImageCodec");
    } catch (ReflectException exc) {
        throw new MissingLibraryException(NO_JAI_MSG, exc);
    }
    super.initFile(id);
    LOGGER.info("Reading movie dimensions");
    // map Location to File or RandomAccessFile, if possible
    IRandomAccess ira = Location.getMappedFile(id);
    if (ira != null) {
        if (ira instanceof FileHandle) {
            FileHandle fh = (FileHandle) ira;
            r.setVar("file", fh.getRandomAccessFile());
        } else {
            throw new FormatException("Unsupported handle type" + ira.getClass().getName());
        }
    } else {
        String mapId = Location.getMappedId(id);
        File file = new File(mapId);
        if (file.exists()) {
            r.setVar("file", file);
        } else
            throw new FileNotFoundException(id);
    }
    r.setVar("tiff", "tiff");
    r.setVar("param", null);
    // create TIFF decoder
    int numPages;
    try {
        r.exec("s = new FileSeekableStream(file)");
        r.exec("dec = ImageCodec.createImageDecoder(tiff, s, param)");
        numPages = ((Integer) r.exec("dec.getNumPages()")).intValue();
    } catch (ReflectException exc) {
        throw new FormatException(exc);
    }
    if (numPages < 0) {
        throw new FormatException("Invalid page count: " + numPages);
    }
    // decode first image plane
    BufferedImage img = openBufferedImage(0);
    if (img == null)
        throw new FormatException("Invalid image stream");
    LOGGER.info("Populating metadata");
    CoreMetadata m = core.get(0);
    m.imageCount = numPages;
    m.sizeX = img.getWidth();
    m.sizeY = img.getHeight();
    m.sizeZ = 1;
    m.sizeC = img.getSampleModel().getNumBands();
    m.sizeT = numPages;
    m.rgb = m.sizeC > 1;
    m.dimensionOrder = "XYCZT";
    m.pixelType = AWTImageTools.getPixelType(img);
    m.interleaved = true;
    m.littleEndian = false;
    m.metadataComplete = true;
    m.indexed = false;
    m.falseColor = false;
    // populate the metadata store
    MetadataStore store = makeFilterMetadata();
    MetadataTools.populatePixels(store, this);
}
Also used : FileHandle(loci.common.FileHandle) FileNotFoundException(java.io.FileNotFoundException) ReflectException(loci.common.ReflectException) CoreMetadata(loci.formats.CoreMetadata) FormatException(loci.formats.FormatException) BufferedImage(java.awt.image.BufferedImage) MetadataStore(loci.formats.meta.MetadataStore) MissingLibraryException(loci.formats.MissingLibraryException) ReflectedUniverse(loci.common.ReflectedUniverse) IRandomAccess(loci.common.IRandomAccess) File(java.io.File)

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