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;
g.dispose();
return bi;
}
+
/**
* Returns a <code>BufferedImage</code> from URL
*
* @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();
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);
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
*/
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;
* 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
*/
{
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));
}
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)}
*
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)}
*
ImageUtil.getImage(imgMock);
Mockito.verify(imgMock).createImage();
}
-
+
/**
* Test {@link ImageUtil#getImageData(ImageDescriptor)}
*
@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)}
*
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)}
*
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)}
*
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)}
*
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);
+ }
+
}