OpenJDK + ImageUtil

2 messages Options
Embed this post
Permalink
Reijer Copier

OpenJDK + ImageUtil

Reply Threaded More More options
Print post
Permalink
Dear list,

While running the raster tree builder on OpenJDK 1.6.0_0-b11  (default
jvm on OpenSUSE) I discovered that org.deegree.framework.util.ImageUtil
uses com.sun.* classes to encode images. This caused the exception:
java.lang.IncompatibleClassChangeError: Found class
com.sun.image.codec.jpeg.JPEGImageEncoder, but interface was expected

The com.sun.* packages are apparently (slightly) different on OpenJDK.
Therefore are modified ImageUtil to use ImageIO instead.

Regards,

Reijer

Index: C:/Users/copierrj/geoide-workspace/deegree/src/org/deegree/framework/util/ImageUtils.java
===================================================================
--- C:/Users/copierrj/geoide-workspace/deegree/src/org/deegree/framework/util/ImageUtils.java (revision 20083)
+++ C:/Users/copierrj/geoide-workspace/deegree/src/org/deegree/framework/util/ImageUtils.java (working copy)
@@ -44,8 +44,13 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
+import java.util.Iterator;
 
+import javax.imageio.IIOImage;
 import javax.imageio.ImageIO;
+import javax.imageio.ImageWriteParam;
+import javax.imageio.ImageWriter;
+import javax.imageio.stream.MemoryCacheImageOutputStream;
 import javax.media.jai.JAI;
 import javax.media.jai.PlanarImage;
 import javax.media.jai.RenderedOp;
@@ -60,14 +65,8 @@
 
 import Acme.JPM.Encoders.GifEncoder;
 
-import com.sun.image.codec.jpeg.JPEGCodec;
-import com.sun.image.codec.jpeg.JPEGImageEncoder;
-import com.sun.media.jai.codec.BMPEncodeParam;
-import com.sun.media.jai.codec.ImageCodec;
 import com.sun.media.jai.codec.MemoryCacheSeekableStream;
-import com.sun.media.jai.codec.PNGEncodeParam;
 import com.sun.media.jai.codec.SeekableStream;
-import com.sun.media.jai.codec.TIFFEncodeParam;
 
 /**
  * Some utility methods for reading standard images
@@ -216,21 +215,27 @@
      */
     public static void saveImage( BufferedImage image, OutputStream os, String format, float quality )
                             throws IOException {
-        try {
-
-            if ( "jpeg".equalsIgnoreCase( format ) || "jpg".equalsIgnoreCase( format ) ) {
-                encodeJpeg( os, image, quality );
-            } else if ( "tif".equalsIgnoreCase( format ) || "tiff".equalsIgnoreCase( format ) ) {
-                encodeTiff( os, image );
-            } else if ( "png".equalsIgnoreCase( format ) ) {
-                encodePng( os, image );
-            } else if ( "gif".equalsIgnoreCase( format ) ) {
-                encodeGif( os, image );
-            } else if ( "bmp".equalsIgnoreCase( format ) ) {
-                encodeBmp( os, image );
-            } else {
-                throw new IOException( "invalid image format: " + format );
-            }
+        try {    
+         Iterator<ImageWriter> itr = ImageIO.getImageWritersByFormatName( format );
+         if( itr.hasNext() ) {
+         ImageWriter iw = itr.next();
+         ImageWriteParam iwp = iw.getDefaultWriteParam();
+        
+         try {
+         iwp.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
+         iwp.setCompressionQuality( quality );
+         } catch( UnsupportedOperationException e ) {
+        
+         } catch( IllegalArgumentException e ) {
+        
+         }
+        
+         iw.setOutput( new MemoryCacheImageOutputStream( os ) );
+         iw.write( null, new IIOImage( image, null, null ), iwp );
+         iw.dispose();
+         } else {
+         throw new IOException( "invalid image format: " + format );
+         }
         } catch ( IOException e ) {
             throw e;
         } finally {
@@ -253,82 +258,4 @@
         GifEncoder encoder = new GifEncoder( img, out );
         encoder.encode();
     }
-
-    /**
-     *
-     *
-     * @param out
-     * @param img
-     *
-     * @throws IOException
-     */
-    private static void encodeBmp( OutputStream out, BufferedImage img )
-                            throws IOException {
-        BMPEncodeParam encodeParam = new BMPEncodeParam();
-
-        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "BMP", out, encodeParam );
-
-        encoder.encode( img );
-    }
-
-    /**
-     *
-     *
-     * @param out
-     * @param img
-     *
-     * @throws IOException
-     */
-    private static void encodePng( OutputStream out, BufferedImage img )
-                            throws IOException {
-        PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam( img );
-
-        if ( encodeParam instanceof PNGEncodeParam.Palette ) {
-            PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
-            byte[] b = new byte[] { -127 };
-            p.setPaletteTransparency( b );
-        }
-
-        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "PNG", out, encodeParam );
-        encoder.encode( img.getData(), img.getColorModel() );
-    }
-
-    /**
-     *
-     *
-     * @param out
-     * @param img
-     *
-     * @throws IOException
-     */
-    private static void encodeTiff( OutputStream out, BufferedImage img )
-                            throws IOException {
-        TIFFEncodeParam encodeParam = new TIFFEncodeParam();
-
-        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "TIFF", out, encodeParam );
-
-        encoder.encode( img );
-    }
-
-    /**
-     *
-     *
-     * @param out
-     * @param img
-     * @param quality
-     *
-     * @throws IOException
-     */
-    private static void encodeJpeg( OutputStream out, BufferedImage img, float quality )
-                            throws IOException {
-
-        // encode JPEG
-        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
-        com.sun.image.codec.jpeg.JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam( img );
-        jpegParams.setQuality( quality, false );
-        encoder.setJPEGEncodeParam( jpegParams );
-
-        encoder.encode( img );
-    }
-
 }

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
deegree-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/deegree-devel
Rutger Bezema

Re: OpenJDK + ImageUtil

Reply Threaded More More options
Print post
Permalink
Hi Reijer,

Thanks for the notification and your effort of supplying a patch.

We are aware of the Sun specific implementation usage in the ImageUtils
class, and yes it is not very nice, but to my knowledge only the jpeg
classes are specific to a sun jvm, the others are part of the
JAI-codec.jar. Deploying your application with this jar should not be
a problem.

Maybe you could send a patch removing only the JPEG classes. The
other encoders should still be used.

I'm sorry to say, that because of the need to use the specific jai
encoders, we cannot apply your supplied patch.

With kind regards,
Rutger Bezema

Reijer Copier wrote:

> Dear list,
>
> While running the raster tree builder on OpenJDK 1.6.0_0-b11  (default
> jvm on OpenSUSE) I discovered that org.deegree.framework.util.ImageUtil
> uses com.sun.* classes to encode images. This caused the exception:
> java.lang.IncompatibleClassChangeError: Found class
> com.sun.image.codec.jpeg.JPEGImageEncoder, but interface was expected
>
> The com.sun.* packages are apparently (slightly) different on OpenJDK.
> Therefore are modified ImageUtil to use ImageIO instead.
>
> Regards,
>
> Reijer

> Index: C:/Users/copierrj/geoide-workspace/deegree/src/org/deegree/framework/util/ImageUtils.java
> ===================================================================
> --- C:/Users/copierrj/geoide-workspace/deegree/src/org/deegree/framework/util/ImageUtils.java (revision 20083)
> +++ C:/Users/copierrj/geoide-workspace/deegree/src/org/deegree/framework/util/ImageUtils.java (working copy)
> @@ -44,8 +44,13 @@
>  import java.io.InputStream;
>  import java.io.OutputStream;
>  import java.net.URL;
> +import java.util.Iterator;
>  
> +import javax.imageio.IIOImage;
>  import javax.imageio.ImageIO;
> +import javax.imageio.ImageWriteParam;
> +import javax.imageio.ImageWriter;
> +import javax.imageio.stream.MemoryCacheImageOutputStream;
>  import javax.media.jai.JAI;
>  import javax.media.jai.PlanarImage;
>  import javax.media.jai.RenderedOp;
> @@ -60,14 +65,8 @@
>  
>  import Acme.JPM.Encoders.GifEncoder;
>  
> -import com.sun.image.codec.jpeg.JPEGCodec;
> -import com.sun.image.codec.jpeg.JPEGImageEncoder;
> -import com.sun.media.jai.codec.BMPEncodeParam;
> -import com.sun.media.jai.codec.ImageCodec;
>  import com.sun.media.jai.codec.MemoryCacheSeekableStream;
> -import com.sun.media.jai.codec.PNGEncodeParam;
>  import com.sun.media.jai.codec.SeekableStream;
> -import com.sun.media.jai.codec.TIFFEncodeParam;
>  
>  /**
>   * Some utility methods for reading standard images
> @@ -216,21 +215,27 @@
>       */
>      public static void saveImage( BufferedImage image, OutputStream os, String format, float quality )
>                              throws IOException {
> -        try {
> -
> -            if ( "jpeg".equalsIgnoreCase( format ) || "jpg".equalsIgnoreCase( format ) ) {
> -                encodeJpeg( os, image, quality );
> -            } else if ( "tif".equalsIgnoreCase( format ) || "tiff".equalsIgnoreCase( format ) ) {
> -                encodeTiff( os, image );
> -            } else if ( "png".equalsIgnoreCase( format ) ) {
> -                encodePng( os, image );
> -            } else if ( "gif".equalsIgnoreCase( format ) ) {
> -                encodeGif( os, image );
> -            } else if ( "bmp".equalsIgnoreCase( format ) ) {
> -                encodeBmp( os, image );
> -            } else {
> -                throw new IOException( "invalid image format: " + format );
> -            }
> +        try {    
> +         Iterator<ImageWriter> itr = ImageIO.getImageWritersByFormatName( format );
> +         if( itr.hasNext() ) {
> +         ImageWriter iw = itr.next();
> +         ImageWriteParam iwp = iw.getDefaultWriteParam();
> +        
> +         try {
> +         iwp.setCompressionMode( ImageWriteParam.MODE_EXPLICIT );
> +         iwp.setCompressionQuality( quality );
> +         } catch( UnsupportedOperationException e ) {
> +        
> +         } catch( IllegalArgumentException e ) {
> +        
> +         }
> +        
> +         iw.setOutput( new MemoryCacheImageOutputStream( os ) );
> +         iw.write( null, new IIOImage( image, null, null ), iwp );
> +         iw.dispose();
> +         } else {
> +         throw new IOException( "invalid image format: " + format );
> +         }
>          } catch ( IOException e ) {
>              throw e;
>          } finally {
> @@ -253,82 +258,4 @@
>          GifEncoder encoder = new GifEncoder( img, out );
>          encoder.encode();
>      }
> -
> -    /**
> -     *
> -     *
> -     * @param out
> -     * @param img
> -     *
> -     * @throws IOException
> -     */
> -    private static void encodeBmp( OutputStream out, BufferedImage img )
> -                            throws IOException {
> -        BMPEncodeParam encodeParam = new BMPEncodeParam();
> -
> -        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "BMP", out, encodeParam );
> -
> -        encoder.encode( img );
> -    }
> -
> -    /**
> -     *
> -     *
> -     * @param out
> -     * @param img
> -     *
> -     * @throws IOException
> -     */
> -    private static void encodePng( OutputStream out, BufferedImage img )
> -                            throws IOException {
> -        PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam( img );
> -
> -        if ( encodeParam instanceof PNGEncodeParam.Palette ) {
> -            PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
> -            byte[] b = new byte[] { -127 };
> -            p.setPaletteTransparency( b );
> -        }
> -
> -        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "PNG", out, encodeParam );
> -        encoder.encode( img.getData(), img.getColorModel() );
> -    }
> -
> -    /**
> -     *
> -     *
> -     * @param out
> -     * @param img
> -     *
> -     * @throws IOException
> -     */
> -    private static void encodeTiff( OutputStream out, BufferedImage img )
> -                            throws IOException {
> -        TIFFEncodeParam encodeParam = new TIFFEncodeParam();
> -
> -        com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder( "TIFF", out, encodeParam );
> -
> -        encoder.encode( img );
> -    }
> -
> -    /**
> -     *
> -     *
> -     * @param out
> -     * @param img
> -     * @param quality
> -     *
> -     * @throws IOException
> -     */
> -    private static void encodeJpeg( OutputStream out, BufferedImage img, float quality )
> -                            throws IOException {
> -
> -        // encode JPEG
> -        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( out );
> -        com.sun.image.codec.jpeg.JPEGEncodeParam jpegParams = encoder.getDefaultJPEGEncodeParam( img );
> -        jpegParams.setQuality( quality, false );
> -        encoder.setJPEGEncodeParam( jpegParams );
> -
> -        encoder.encode( img );
> -    }
> -
>  }

> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> deegree-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/deegree-devel
--
Rutger Bezema

l a t / l o n  GmbH
Aennchenstrasse 19           53177 Bonn, Germany
phone ++49 +228 184960       fax ++49 +228 1849629
http://www.lat-lon.de        http://www.deegree.org


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
deegree-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/deegree-devel

signature.asc (204 bytes) Download Attachment