Changeset 5921 for branches/cleanup


Ignore:
Timestamp:
02/16/10 14:12:19 (10 years ago)
Author:
melissa
Message:

Added service for reading JPEG-2000 data using JAI Image I/O. See #464.

Location:
branches/cleanup/components
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/cleanup/components/bio-formats/build.properties

    r5919 r5921  
    1010component.jar            = bio-formats.jar 
    1111component.version        = 4.1.0 
    12 component.classpath      = ${artifact.dir}/loci-common.jar:\ 
     12 
     13component.classpath      = ${artifact.dir}/jai_imageio.jar:\ 
     14                           ${artifact.dir}/loci-common.jar:\ 
    1315                           ${artifact.dir}/mdbtools-java.jar:\ 
    1416                           ${artifact.dir}/ome-xml.jar:\ 
     
    3032component.runtime-cp     = ${component.classpath}:\ 
    3133                           ${artifact.dir}/ome-io.jar:\ 
    32                            ${artifact.dir}/ome-notes.jar:\ 
    33                            ${artifact.dir}/jai_imageio.jar 
     34                           ${artifact.dir}/ome-notes.jar 
  • branches/cleanup/components/bio-formats/src/loci/formats/codec/JPEG2000Codec.java

    r5852 r5921  
    3131import java.io.ByteArrayOutputStream; 
    3232import java.io.IOException; 
    33 import java.util.Iterator; 
    34  
    35 import javax.imageio.IIOImage; 
    36 import javax.imageio.ImageIO; 
    37 import javax.imageio.spi.IIORegistry; 
    38 import javax.imageio.spi.ServiceRegistry; 
    39 import javax.imageio.stream.ImageOutputStream; 
    40 import javax.imageio.stream.MemoryCacheImageInputStream; 
    4133 
    4234import loci.common.DataTools; 
    4335import loci.common.RandomAccessInputStream; 
    44 import loci.common.ReflectException; 
    45 import loci.common.ReflectedUniverse; 
     36import loci.common.dependency.DependencyException; 
     37import loci.common.dependency.ServiceException; 
     38import loci.common.dependency.ServiceFactory; 
    4639import loci.formats.FormatException; 
    4740import loci.formats.MissingLibraryException; 
     
    6659    "obtain jai_imageio.jar from http://loci.wisc.edu/ome/formats-library.html"; 
    6760 
    68   private static final String J2K_READER = 
    69     "com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader"; 
    70  
    71   private static final String J2K_WRITER = 
    72     "com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageWriter"; 
    73  
    74   private static final String J2K_WRITER_PARAM = 
    75     "com.sun.media.imageio.plugins.jpeg2000.J2KImageWriteParam"; 
    76  
    7761  private static final double QUALITY = 6; 
    7862  private static int[] CODE_BLOCK_SIZE = { 64, 64 }; 
    79  
    80   // -- Static fields -- 
    81  
    82   private static boolean noJ2k = false; 
    83   private static ReflectedUniverse r = createReflectedUniverse(); 
    84  
    85   private static void registerClass(String className) { 
    86     String spi = className + "Spi"; 
    87     Class spiClass = null; 
    88     try { 
    89       spiClass = Class.forName(spi); 
    90     } 
    91     catch (ClassNotFoundException exc) { 
    92       LOGGER.info("Could not find {}", spi, exc); 
    93       noJ2k = true; 
    94       return; 
    95     } 
    96     catch (NoClassDefFoundError err) { 
    97       LOGGER.info("Could not find {}", spi, err); 
    98       noJ2k = true; 
    99       return; 
    100     } 
    101     catch (RuntimeException exc) { 
    102       // HACK: workaround for bug in Apache Axis2 
    103       String msg = exc.getMessage(); 
    104       if (msg != null && msg.indexOf("ClassNotFound") < 0) throw exc; 
    105       LOGGER.info("", exc); 
    106       noJ2k = true; 
    107       return; 
    108     } 
    109     IIORegistry registry = IIORegistry.getDefaultInstance(); 
    110     if (spiClass != null) { 
    111       Iterator providers = ServiceRegistry.lookupProviders(spiClass); 
    112       registry.registerServiceProviders(providers); 
    113     } 
    114   } 
    115  
    116   private static ReflectedUniverse createReflectedUniverse() { 
    117     // NB: ImageJ does not access the jai_imageio classes with the normal 
    118     // class loading scheme, and thus the necessary service provider stuff is 
    119     // not automatically registered. Instead, we register the J2KImageReader 
    120     // with the IIORegistry manually, merely so that we can obtain a 
    121     // J2KImageReaderSpi object from the IIORegistry's service provider lookup 
    122     // function, then use it to construct a J2KImageReader object directly. 
    123  
    124     ReflectedUniverse ru = null; 
    125     try { 
    126       // NB: the following comment facilitates dependency detection: 
    127       // import com.sun.media.imageioimpl.plugins.jpeg2000 
    128  
    129       // register J2KImageReader with IIORegistry 
    130       registerClass(J2K_READER); 
    131  
    132       if (noJ2k) { 
    133         throw new MissingLibraryException( 
    134           "Could not compress JPEG-2000 data." + NO_J2K_MSG); 
    135       } 
    136  
    137       IIORegistry registry = IIORegistry.getDefaultInstance(); 
    138  
    139       // obtain J2KImageReaderSpi instance from IIORegistry 
    140       Class j2kSpiClass = Class.forName(J2K_READER + "Spi"); 
    141       Object j2kSpi = registry.getServiceProviderByClass(j2kSpiClass); 
    142       ru = new ReflectedUniverse(); 
    143  
    144       ru.exec("import " + J2K_READER); 
    145       ru.setVar("j2kSpi", j2kSpi); 
    146       ru.exec("j2kReader = new J2KImageReader(j2kSpi)"); 
    147  
    148       // register J2KImageWriter with IIORegistry 
    149       registerClass(J2K_WRITER); 
    150       j2kSpiClass = Class.forName(J2K_WRITER + "Spi"); 
    151       j2kSpi = registry.getServiceProviderByClass(j2kSpiClass); 
    152       ru.exec("import " + J2K_WRITER); 
    153       ru.exec("import " + J2K_WRITER_PARAM); 
    154       ru.exec("import javax.imageio.ImageWriteParam"); 
    155       ru.setVar("j2kSpi", j2kSpi); 
    156       ru.exec("j2kWriter = new J2KImageWriter(j2kSpi)"); 
    157     } 
    158     catch (Throwable t) { 
    159       noJ2k = true; 
    160       LOGGER.info("", t); 
    161     } 
    162     return ru; 
    163   } 
    16463 
    16564  // -- Codec API methods -- 
     
    18079    throws FormatException 
    18180  { 
    182     if (r == null) { 
    183       throw new MissingLibraryException( 
    184         "Could not compress JPEG-2000 data." + NO_J2K_MSG); 
    185     } 
    186  
    18781    JPEG2000CodecOptions j2kOptions = 
    18882      JPEG2000CodecOptions.getDefaultOptions(options); 
     
    245139    } 
    246140 
    247     try { 
    248       ImageOutputStream ios = ImageIO.createImageOutputStream(out); 
    249  
    250       r.setVar("out", ios); 
    251       r.exec("j2kWriter.setOutput(out)"); 
    252  
    253       r.setVar("iioImage", new IIOImage(img, null, null)); 
    254       r.setVar("lossless", j2kOptions.lossless); 
    255       r.setVar("compressionType", "JPEG2000"); 
    256       r.setVar("codeBlockSize", j2kOptions.codeBlockSize); 
    257       r.setVar("quality", j2kOptions.quality); 
    258       String filter = j2kOptions.lossless ? 
    259         "J2KImageWriteParam.FILTER_53" : "J2KImageWriteParam.FILTER_97"; 
    260       //r.setVar("compressionFilter",options.filter); 
    261  
    262       r.exec("param = j2kWriter.getDefaultWriteParam()"); 
    263       r.exec("param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)"); 
    264       r.exec("param.setCompressionType(compressionType)"); 
    265       r.exec("param.setLossless(lossless)"); 
    266       r.exec("param.setFilter(" + filter + ")"); 
    267       r.exec("param.setCodeBlockSize(codeBlockSize)"); 
    268       r.exec("param.setEncodingRate(quality)"); 
    269       r.exec("j2kWriter.write(null, iioImage, param)"); 
    270       ios.close(); 
    271     } 
    272     catch (ReflectException e) { 
     141    JAIIIOService service = null; 
     142    try { 
     143      ServiceFactory factory = new ServiceFactory(); 
     144      service = factory.getInstance(JAIIIOService.class); 
     145    } 
     146    catch (DependencyException de) { 
     147    } 
     148 
     149    try { 
     150      service.writeImage(out, img, j2kOptions.lossless, 
     151        j2kOptions.codeBlockSize, j2kOptions.quality); 
     152    } 
     153    catch (IOException e) { 
    273154      throw new FormatException("Could not compress JPEG-2000 data.", e); 
    274155    } 
    275     catch (IOException e) { 
     156    catch (ServiceException e) { 
    276157      throw new FormatException("Could not compress JPEG-2000 data.", e); 
    277158    } 
     
    290171    throws FormatException, IOException 
    291172  { 
    292     if (r == null) { 
    293       throw new MissingLibraryException( 
    294         "Could not compress JPEG-2000 data." + NO_J2K_MSG); 
    295     } 
    296  
    297173    if (options == null) { 
    298174      options = CodecOptions.getDefaultOptions(); 
     
    303179    Exception exception = null; 
    304180    long fp = in.getFilePointer(); 
    305     try { 
    306       byte[] buf = null; 
    307       if (options.maxBytes == 0) { 
    308         buf = new byte[(int) (in.length() - fp)]; 
    309       } 
    310       else { 
    311         buf = new byte[(int) (options.maxBytes - fp)]; 
    312       } 
    313       in.read(buf); 
    314  
     181    byte[] buf = null; 
     182    if (options.maxBytes == 0) { 
     183      buf = new byte[(int) (in.length() - fp)]; 
     184    } 
     185    else { 
     186      buf = new byte[(int) (options.maxBytes - fp)]; 
     187    } 
     188    in.read(buf); 
     189 
     190    JAIIIOService service = null; 
     191    try { 
     192      ServiceFactory factory = new ServiceFactory(); 
     193      service = factory.getInstance(JAIIIOService.class); 
     194    } 
     195    catch (DependencyException de) { 
     196 
     197    } 
     198 
     199    try { 
    315200      ByteArrayInputStream bis = new ByteArrayInputStream(buf); 
    316       MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(bis); 
    317  
    318       r.setVar("mciis", mciis); 
    319       r.exec("j2kReader.setInput(mciis)"); 
    320       r.setVar("zero", 0); 
    321       b = (BufferedImage) r.exec("j2kReader.read(zero)"); 
     201      b = service.readImage(bis); 
    322202      single = AWTImageTools.getPixelBytes(b, options.littleEndian); 
    323203 
    324204      bis.close(); 
    325       mciis.close(); 
    326       buf = null; 
    327205      b = null; 
    328206    } 
    329     catch (ReflectException exc) { 
    330       exception = exc; 
    331     } 
    332     catch (IOException exc) { 
    333       exception = exc; 
    334     } 
    335  
    336     if (exception != null) { 
     207    catch (IOException e) { 
    337208      throw new FormatException("Could not decompress JPEG2000 image. Please " + 
    338         "make sure that jai_imageio.jar is installed.", exception); 
     209        "make sure that jai_imageio.jar is installed.", e); 
     210    } 
     211    catch (ServiceException e) { 
     212      throw new FormatException("Could not decompress JPEG2000 image. Please " + 
     213        "make sure that jai_imageio.jar is installed.", e); 
    339214    } 
    340215 
  • branches/cleanup/components/common/src/loci/common/dependency/services.properties

    r5919 r5921  
    1313# MDB Tools service (interface and implementation in bio-formats component) 
    1414loci.formats.MDBService=loci.formats.MDBServiceImpl 
     15# JAI Image I/O service (interface and implementation in bio-formats component) 
     16loci.formats.codec.JAIIIOService=loci.formats.codec.JAIIIOServiceImpl 
  • branches/cleanup/components/forks/jai/src/jj2000/j2k/codestream/reader/HeaderDecoder.java

    r5896 r5921  
    352352     * */ 
    353353    public final int getNumComps() { 
    354       /* debug */ System.out.println("*** FORK ***"); 
    355354        return nComp; 
    356355    } 
Note: See TracChangeset for help on using the changeset viewer.