Changeset 2187


Ignore:
Timestamp:
01/30/07 13:33:41 (13 years ago)
Author:
curtis
Message:

First cut at making ND2 work without JAI Image I/O Tools in classpath.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/formats/in/ND2Reader.java

    r2182 r2187  
    2828import java.io.*; 
    2929import java.util.*; 
    30 import javax.imageio.*; 
     30import javax.imageio.spi.IIORegistry; 
     31import javax.imageio.spi.ServiceRegistry; 
     32import javax.imageio.stream.MemoryCacheImageInputStream; 
    3133import loci.formats.*; 
    3234 
     
    4345  // -- Constants -- 
    4446 
    45   private static final String NO_JAI_MSG = 
    46     "Please install JAI from http://jai-imageio.dev.java.net"; 
     47  private static final String NO_J2K_MSG = 
     48    "Please install JAI Image I/O Tools from http://jai-imageio.dev.java.net"; 
     49 
     50  private static final String J2K_READER = 
     51    "com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader"; 
    4752 
    4853  // -- Static fields -- 
    4954 
    50   private static boolean noJAI = false; 
     55  private static boolean noJ2k = false; 
    5156  private static ReflectedUniverse r = createReflectedUniverse(); 
    5257 
    5358  private static ReflectedUniverse createReflectedUniverse() { 
     59    // NB: ImageJ does not access the jai_imageio classes with the normal 
     60    // class loading scheme, and thus the necessary service provider stuff is 
     61    // not automatically registered. Instead, we register the J2KImageReader 
     62    // with the IIORegistry manually, merely so that we can obtain a 
     63    // J2KImageReaderSpi object from the IIORegistry's service provider 
     64    // lookup function, then use it to construct a J2KImageReader object 
     65    // directly, which we can use to process ND2 files one plane at a time. 
     66 
     67    // register J2KImageReader with IIORegistry 
     68    String j2kReaderSpi = J2K_READER + "Spi"; 
     69    Class j2kSpiClass = null; 
     70    try { 
     71      j2kSpiClass = Class.forName(j2kReaderSpi); 
     72    } 
     73    catch (ClassNotFoundException exc) { 
     74      if (debug) exc.printStackTrace(); 
     75      noJ2k = true; 
     76    } 
     77    catch (NoClassDefFoundError err) { 
     78      if (debug) err.printStackTrace(); 
     79      noJ2k = true; 
     80    } 
     81    IIORegistry registry = IIORegistry.getDefaultInstance(); 
     82    if (j2kSpiClass != null) { 
     83      Iterator providers = ServiceRegistry.lookupProviders(j2kSpiClass); 
     84      registry.registerServiceProviders(providers); 
     85    } 
     86 
     87    // obtain J2KImageReaderSpi instance from IIORegistry 
     88    Object j2kSpi = registry.getServiceProviderByClass(j2kSpiClass); 
     89 
    5490    r = null; 
    5591    try { 
    5692      r = new ReflectedUniverse(); 
     93 
     94      // for computing offsets in initFile 
    5795      r.exec("import jj2000.j2k.fileformat.reader.FileFormatReader"); 
    5896      r.exec("import jj2000.j2k.io.BEBufferedRandomAccessFile"); 
    5997      r.exec("import jj2000.j2k.util.ISRandomAccessIO"); 
    6098 
    61       // make sure that a JPEG-2000 reader is available 
    62       String[] fnames = ImageIO.getReaderFormatNames(); 
    63       boolean foundReader = false; 
    64       for (int i=0; i<fnames.length; i++) { 
    65         if (fnames[i].equals("JPEG 2000")) { 
    66           foundReader = true; 
    67           break; 
    68         } 
    69       } 
    70       if (!foundReader) { 
    71         if (debug) System.err.println("No JPEG-2000 reader found."); 
    72         noJAI = true; 
    73       } 
     99      // for reading pixel data in openImage 
     100      r.exec("import " + J2K_READER); 
     101      r.setVar("j2kSpi", j2kSpi); 
     102      r.exec("j2kReader = new J2KImageReader(j2kSpi)"); 
    74103    } 
    75104    catch (Throwable exc) { 
    76105      if (debug) exc.printStackTrace(); 
    77       noJAI = true; 
     106      noJ2k = true; 
    78107    } 
    79108    return r; 
     
    177206 
    178207    ByteArrayInputStream bis = new ByteArrayInputStream(b); 
    179     BufferedImage img = ImageIO.read(bis); 
     208    // NB: Even after registering J2KImageReader with 
     209    // IIORegistry manually, the following still does not work: 
     210    //BufferedImage img = ImageIO.read(bis); 
     211    MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(bis); 
     212    BufferedImage img = null; 
     213    try { 
     214      r.setVar("mciis", mciis); 
     215      r.exec("j2kReader.setInput(mciis)"); 
     216      r.setVar("zero", 0); 
     217      r.setVar("param", null); 
     218      img = (BufferedImage) r.exec("j2kReader.read(zero, param)"); 
     219    } 
     220    catch (ReflectException exc) { 
     221      throw new FormatException(exc); 
     222    } 
    180223    bis.close(); 
    181224    b = null; 
     
    223266  protected void initFile(String id) throws FormatException, IOException { 
    224267    if (debug) debug("ND2Reader.initFile(" + id + ")"); 
    225     if (noJAI) throw new FormatException(NO_JAI_MSG); 
     268    if (noJ2k) throw new FormatException(NO_J2K_MSG); 
    226269    super.initFile(id); 
    227270 
Note: See TracChangeset for help on using the changeset viewer.