COMMON: UTIL: Fixed exception for "numbers of source Raster bands and source color... 37/11837/6
authorchanghyun1.lee <changhyun1.lee@samsung.com>
Wed, 6 Nov 2013 06:46:00 +0000 (15:46 +0900)
committerchanghyun1.lee <changhyun1.lee@samsung.com>
Thu, 7 Nov 2013 07:16:42 +0000 (16:16 +0900)
 ImageIO.read() is not implementation for gray-scale of image.
 work around that retries the load as 'BufferedImage.TYPE_BYTE_GRAY' if it fails the main load.

Change-Id: I6e07087102cbc7821af7b9180c5e78d7039de902
Signed-off-by: changhyun1.lee <changhyun1.lee@samsung.com>
org.tizen.common/src/org/tizen/common/util/ImageUtil.java
org.tizen.common/test/src/org/tizen/common/util/ImageUtilTest.java
org.tizen.common/test/src/org/tizen/common/util/ImageUtilTest.jpg [new file with mode: 0644]

index eb30507..f0da7ed 100644 (file)
@@ -36,8 +36,13 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.Iterator;
 
 import javax.imageio.ImageIO;
+import javax.imageio.ImageReadParam;
+import javax.imageio.ImageReader;
+import javax.imageio.ImageTypeSpecifier;
+import javax.imageio.stream.ImageInputStream;
 
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Plugin;
@@ -165,6 +170,7 @@ public class ImageUtil {
         g.dispose();
         return bi;
     }
+
     /**
      * Returns a <code>BufferedImage</code> from URL
      * 
@@ -173,12 +179,13 @@ public class ImageUtil {
      * @return a <code>BufferedImage</code> containing the decoded
      * contents of the URL, or <code>null</code>.
      */
-    public static BufferedImage getBufferedImageFromURL(URL url) {
+    public static BufferedImage getBufferedImage(URL url) {
         if (url == null) {
             return null;
         }
-        BufferedImage bImage = null;
+
         InputStream input = null;
+        BufferedImage bImage = null;
         int contentLength = 0;
         try {
             URLConnection con = url.openConnection();
@@ -187,9 +194,9 @@ public class ImageUtil {
             if ( contentLength == 0 || contentLength == -1 ) {
                 return null;
             }
-            input  = url.openStream();
-            bImage = ImageIO.read( input );
-        } catch (Exception e) {
+            input = url.openStream();
+            bImage = getBufferedImage(input);
+        } catch (IOException e) {
             logger.error(e.getMessage(), e);
         } finally {
             IOUtil.tryClose(input);
@@ -197,6 +204,59 @@ public class ImageUtil {
         return bImage;
     }
 
+    /**
+     * Returns a <code>BufferedImage</code> from InputStream
+     * 
+     * @param stream an <code>ImageInputStream</code>
+     *
+     * @return a <code>BufferedImage</code>
+     */
+    public static BufferedImage getBufferedImage(InputStream input) {
+        if (input == null) {
+            return null;
+        }
+
+        BufferedImage bImage = null;
+        ImageInputStream stream = null;
+        try {
+            // NOTE: Fixed "numbers of source Raster bands and source color space components do not match" bug.
+            // see: http://stackoverflow.com/questions/10416378/imageio-read-illegal-argument-exception-raster-bands-colour-space-components
+            // bImage = ImageIO.read( input );
+            stream = ImageIO.createImageInputStream(input);
+            Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
+            while (iter.hasNext()) {
+                ImageReader reader = null;
+                try {
+                    reader = (ImageReader)iter.next();
+                    ImageReadParam param = reader.getDefaultReadParam();
+                    reader.setInput(stream, true, true);
+                    Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
+                    while (imageTypes.hasNext()) {
+                        ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
+                        int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
+                        if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
+                            param.setDestinationType(imageTypeSpecifier);
+                            break;
+                        }
+                    }
+                    bImage = reader.read(0, param);
+                    if (bImage != null) {
+                        break;
+                    }
+                } catch (Exception e) {
+                    throw e;
+                } finally {
+                    IOUtil.tryClose(reader);
+                }
+            }
+        } catch (Exception e) {
+            logger.error(e.getMessage(), e);
+        } finally {
+            IOUtil.tryClose(stream);
+        }
+        return bImage;
+    }
+
     /*
      * The original source is org.tizen.nativecpp.misc.utils.ImageUtil
      */
index ceb5eff..0a11650 100755 (executable)
@@ -27,6 +27,8 @@ package org.tizen.common.util;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.awt.image.BufferedImage;
+import java.io.InputStream;
 import java.net.URL;
 
 import org.eclipse.core.runtime.Plugin;
@@ -59,30 +61,32 @@ public class ImageUtilTest
      * Property key for URL handler
      */
     protected static final String PROP_PKG = "java.protocol.handler.pkgs";
-    
+
     protected static final String TEST_PATH = ImageUtilTest.class.getSimpleName() + ".png";
     protected static final String TEST_URL = "cp:///" + TEST_PATH;
-    
+    protected static final String JPG_TEST_PATH = ImageUtilTest.class.getSimpleName() + ".jpg";
+    protected static final String JPG_TEST_URL = "cp:///" + JPG_TEST_PATH;
+
     /**
      * Old property value for URL handler
      */
     protected String oldConfig;
-    
+
     /**
      * Set up URL handler before test
      */
     @SuppressWarnings("unchecked")
-       @Before
+    @Before
     public void setUp()
     {
         oldConfig = System.getProperty( PROP_PKG );
         System.setProperty( PROP_PKG, "org.tizen.common.util.url" );
-        
+
         ImageUtil.platformSurrogate = mock(SurrogateWithArgument.class);
         ImageUtil.pluginSurrogate = mock(SurrogateWithArgument.class);
 
     }
-    
+
     /**
      * Restore URL handler after test
      */
@@ -117,11 +121,11 @@ public class ImageUtilTest
     {
         final Bundle bundleMockup = mock( Bundle.class );
         final URL virtualPathUrl = new URL( TEST_URL );
-        
+
         when( bundleMockup.getEntry( TEST_PATH ) ).thenReturn( virtualPathUrl );
         final ImageDescriptor imgDes =
             ImageUtil.getImageDescriptor( bundleMockup, TEST_PATH );
-        
+
         Assert.assertNotNull(imgDes.createImage(false));
     }
 
@@ -140,12 +144,12 @@ public class ImageUtilTest
         final String pluginID = "testID";
         when(ImageUtil.platformSurrogate.getAdapter(pluginID)).thenReturn(bundleMockup);
         final ImageDescriptor imgDes = ImageUtil.getImageDescriptor(pluginID, TEST_PATH);
-        
+
         final Image img = imgDes.createImage(false);
-        
+
         Assert.assertNotNull(img);
     }
-    
+
     /**
      * Test {@link ImageUtil#getImageDescriptor(Plugin, String)}
      * 
@@ -162,10 +166,10 @@ public class ImageUtilTest
         when(ImageUtil.pluginSurrogate.getAdapter(pluginMockup)).thenReturn(bundleMockup);
         final ImageDescriptor imgDes = ImageUtil.getImageDescriptor(pluginMockup, TEST_PATH);
         final Image img = imgDes.createImage(false);
-        
+
         Assert.assertNotNull(img);
     }
-    
+
     /**
      * Test {@link ImageUtil#getImage(ImageDescriptor)}
      * 
@@ -180,7 +184,7 @@ public class ImageUtilTest
         ImageUtil.getImage(imgMock);
         Mockito.verify(imgMock).createImage();
     }
-    
+
     /**
      * Test {@link ImageUtil#getImageData(ImageDescriptor)}
      * 
@@ -191,11 +195,11 @@ public class ImageUtilTest
     @Test
     public void test_getImageData1() throws Exception {
         final ImageDescriptor imgMock = mock(ImageDescriptor.class);
-        
+
         ImageUtil.getImageData(imgMock);
         Mockito.verify(imgMock).getImageData();
     }
-    
+
     /**
      * Test {@link ImageUtil#getImage(Plugin, String)}
      * 
@@ -210,12 +214,12 @@ public class ImageUtilTest
         when( bundleMockup.getEntry( TEST_PATH ) ).thenReturn( virtualPathUrl );
         final Plugin pluginMockup = mock(Plugin.class);
         when(ImageUtil.pluginSurrogate.getAdapter(pluginMockup)).thenReturn(bundleMockup);
-        
+
         Image img = ImageUtil.getImage(pluginMockup, TEST_PATH);
-        
+
         Assert.assertNotNull(img);
     }
-    
+
     /**
      * Test {@link ImageUtil#getImageData(Plugin, String)}
      * 
@@ -230,12 +234,12 @@ public class ImageUtilTest
         when( bundleMockup.getEntry( TEST_PATH ) ).thenReturn( virtualPathUrl );
         final Plugin pluginMockup = mock(Plugin.class);
         when(ImageUtil.pluginSurrogate.getAdapter(pluginMockup)).thenReturn(bundleMockup);
-        
+
         ImageData imgData = ImageUtil.getImageData(pluginMockup, TEST_PATH);
 
         Assert.assertNotNull( imgData );
     }
-    
+
     /**
      * Test {@link ImageUtil#getImage(String, String)}
      * 
@@ -250,12 +254,12 @@ public class ImageUtilTest
         final URL virtualPathUrl = new URL( TEST_URL );
         when( bundleMockup.getEntry( TEST_PATH ) ).thenReturn( virtualPathUrl );
         when(ImageUtil.platformSurrogate.getAdapter(pluginID)).thenReturn(bundleMockup);
-        
+
         Image img = ImageUtil.getImage(pluginID, TEST_PATH);
-        
+
         Assert.assertNotNull(img);
     }
-    
+
     /**
      * Test {@link ImageUtil#getImageData(String, String)}
      * 
@@ -270,9 +274,33 @@ public class ImageUtilTest
         final URL virtualPathUrl = new URL( TEST_URL );
         when( bundleMockup.getEntry( TEST_PATH ) ).thenReturn( virtualPathUrl );
         when(ImageUtil.platformSurrogate.getAdapter(pluginID)).thenReturn(bundleMockup);
-        
+
         ImageData imgData = ImageUtil.getImageData(pluginID, TEST_PATH);
         Assert.assertNotNull( imgData );
     }
 
+    /**
+     * Test {@link ImageUtil#getBufferedImage(InputStream)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see ImageUtil#getBufferedImage(InputStream)
+     */
+    @Test
+    public void test_getBufferedImage() throws Exception {
+        final URL virtualJPGPathUrl = new URL(JPG_TEST_URL);
+        InputStream input = virtualJPGPathUrl.openStream();
+        BufferedImage image = ImageUtil.getBufferedImage(input);
+        Assert.assertNotNull(image);
+        image.flush();
+        IOUtil.tryClose(input);
+
+        final URL virtualPNGPathUrl = new URL(TEST_URL);
+        input = virtualPNGPathUrl.openStream();
+        image = ImageUtil.getBufferedImage(input);
+        Assert.assertNotNull(image);
+        image.flush();
+        IOUtil.tryClose(input);
+    }
+
 }
diff --git a/org.tizen.common/test/src/org/tizen/common/util/ImageUtilTest.jpg b/org.tizen.common/test/src/org/tizen/common/util/ImageUtilTest.jpg
new file mode 100644 (file)
index 0000000..5160315
Binary files /dev/null and b/org.tizen.common/test/src/org/tizen/common/util/ImageUtilTest.jpg differ