title: How to add TIFF ImageReader to those registered in Grails
body: <p>I'm trying to create a Grails application that can display previews of TIFF files, and other images as well.</p>
  <h1>Background</h1>
  <p>The images are constructed from a SOAP service that gives me the bytes of the image. In a service method, I take the byte[], construct a ByteArrayInputStream from it, and then create a BufferedImage from that.</p>
  <pre><code>def inputStream = new ByteArrayInputStream(bytes)
  BufferedImage originalImage = ImageIO.read(inputStream)
  ImageIO.write(originalImage, 'png', response.outputStream)
  </code></pre>
  <p>For JPGs, I can easily stream the images to the browser this way as the src of an img tag. TIFFs, though, I'd need to convert the images into some other format (preferably JPG or PNG) to make them the src of an  tag.</p>
  <h1>The Problem</h1>
  <p>I know that <a href="https://stackoverflow.com/questions/2898311/reading-and-writing-out-tiff-image-in-java">I need JAI in order to read the TIFF files</a>. The jai_core.jar, jai_codec.jar files are in my classpath. In fact, because I'm on Mac OSX, they're installed automatically. However, when I run the grails application and it tries to construct a TIFF image from the bytes received from the SOAP service, I get this error:</p>
  <pre><code>| Error 2013-06-18 15:23:38,135 [http-bio-8080-exec-10] ERROR errors.GrailsExceptionResolver  - IllegalArgumentException occurred when processing request: [GET] /BDMPlugin/BDMPlugin/displayImageFromRef - parameters:
  pageRef: 28:22072FBCA0A8889D9C041D76A588BCF4DCB40376A23B5FD5C301378C8E66EB9F4933A5DFCA46365F927D9E91B337B6E1E980FB4406644801
  type: TIFF
  im == null!. Stacktrace follows:
  Message: im == null!
      Line | Method
  -&gt;&gt; 1457 | write                in javax.imageio.ImageIO
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |   1571 | write                in     ''
  |     28 | writeImageToResponse in edu.missouristate.bdmplugin.ImageService
  |     44 | bytesToPng           in     ''
  |     39 | displayImageFromRef  in edu.missouristate.bdmplugin.BDMPluginController
  |    895 | runTask              in java.util.concurrent.ThreadPoolExecutor$Worker
  |    918 | run . . . . . . . .  in     ''
  ^    680 | run                  in java.lang.Thread
  </code></pre>
  <p>I tried the following <a href="http://www.jroller.com/phidias/entry/groovy_what_imagereaders_are_installed" rel="nofollow noreferrer">script</a> to figure out which image readers were installed:</p>
  <pre><code>IIORegistry reg = IIORegistry.getDefaultInstance();
  Iterator spIt = reg.getServiceProviders(ImageReaderSpi.class, false);
  spIt.each(){
  println it.getVendorName() &lt;&lt; &quot; | &quot; &lt;&lt; it.getVersion() &lt;&lt; &quot; | &quot;&lt;&lt; it.getDescription() ;
  }
  </code></pre>
  <p>This outputs the following:</p>
  <pre><code>Sun Microsystems, Inc. | 1.0 | Standard BMP Image Reader
  Sun Microsystems, Inc. | 1.0 | Standard GIF image reader
  Sun Microsystems, Inc. | 1.0 | Standard WBMP Image Reader
  Sun Microsystems, Inc. | 1.0 | Standard PNG image reader
  Sun Microsystems, Inc. | 0.5 | Standard JPEG Image Reader
  </code></pre>
  <p>However, if I run that same Groovy script in the Groovy Console, I get this output:</p>
  <pre><code>Sun Microsystems, Inc. | 0.5 | Standard JPEG Image Reader
  Sun Microsystems, Inc. | 1.0 | Standard BMP Image Reader
  Sun Microsystems, Inc. | 1.0 | Standard WBMP Image Reader
  Sun Microsystems, Inc. | 1.0 | Standard PNG image reader
  Sun Microsystems, Inc. | 1.0 | Standard GIF image reader
  Apple computer Inc. | 1.0 | Standard TIFF image reader
  </code></pre>
  <p>Same set of readers, but it also includes Apple's TIFF reader. Why is the GroovyConsole able to find it and not my Grails environment, even though they're both using the same JRE? Is there a way that I can manually add the TIFF reader via some import from import <code>com.sun.media.jai</code> or <code>com.sun.media.imageio.plugins.tiff</code>?</p>
  <p>I tried adding a manual registration of the TIFFImageReaderSpi to my service method:</p>
  <pre><code>import com.sun.imageio.plugins.tiff.TIFFImageReaderSpi
  ...
  IIORegistry reg = IIORegistry.getDefaultInstance()
  reg.registerServiceProvider(new TIFFImageReaderSpi())
  </code></pre>
  <p>The originalImage variable still comes back null.</p>