0

I'm using ImageIO api to read byte array (using ByteArrayInputStream) to get back a BufferredImage. It works most of the time but fails for certain images. After doing extensive research - I found it's related to color conversion. But I'm not sure how I can quickly resolve this by not giving away ImageIO API. Below is the stacktrace:

java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match
   at java.awt.image.ColorConvertOp.filter(ColorConvertOp.java:460)
   at com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels(JPEGImageReader.java:1114)
   at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(JPEGImageReader.java:0)
   at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1082)
   at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:897)
   at javax.imageio.ImageIO.read(ImageIO.java:1422)
   at javax.imageio.ImageIO.read(ImageIO.java:1374)
   at org.netbeans.modules.form.editors.CustomIconEditor$IconFileItem.<init>(CustomIconEditor.java:516)
   at org.netbeans.modules.form.editors.CustomIconEditor.createFileComboModel(CustomIconEditor.java:479)
   at org.netbeans.modules.form.editors.CustomIconEditor.setPackage(CustomIconEditor.java:312)
   at org.netbeans.modules.form.editors.CustomIconEditor.setValue(CustomIconEditor.java:155)
   at org.netbeans.modules.form.editors.IconEditor.getCustomEditor(IconEditor.java:228)
   at org.netbeans.modules.form.ResourceWrapperEditor.createCustomEditorGUI(ResourceWrapperEditor.java:311)
   at org.netbeans.modules.form.ResourceWrapperEditor.getCustomEditor(ResourceWrapperEditor.java:203)
   at org.netbeans.modules.form.FormPropertyEditor.getCustomEditor(FormPropertyEditor.java:303)
   at org.openide.explorer.propertysheet.PropertyDialogManager.<init>(PropertyDialogManager.java:129)
   at org.openide.explorer.propertysheet.CustomEditorAction.actionPerformed(CustomEditorAction.java:217)
   at org.openide.explorer.propertysheet.SheetTable.editCellAt(SheetTable.java:998)
   at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1078)
   at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1008)
   at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
   at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262)
   at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:262)
   at java.awt.Component.processMouseEvent(Component.java:6260)
   at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
   at org.openide.explorer.propertysheet.SheetTable.processMouseEvent(SheetTable.java:731)
   at java.awt.Component.processEvent(Component.java:6028)
   at java.awt.Container.processEvent(Container.java:2041)
   at java.awt.Component.dispatchEventImpl(Component.java:4630)
   at java.awt.Container.dispatchEventImpl(Container.java:2099)
   at java.awt.Component.dispatchEvent(Component.java:4460)
   at java.awt.LightweightDispatcher.retargetMouseEvent(LightweightDispatcher.java:4574)
   at java.awt.LightweightDispatcher.processMouseEvent(LightweightDispatcher.java:4235)
   at java.awt.LightweightDispatcher.dispatchEvent(LightweightDispatcher.java:4168)
   at java.awt.Container.dispatchEventImpl(Container.java:2085)
   at java.awt.Window.dispatchEventImpl(Window.java:2475)
   at java.awt.Component.dispatchEvent(Component.java:4460)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
   at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jsf
  • 2,851
  • 9
  • 30
  • 33
  • skip this code one more times – Roman C Jan 17 '13 at 21:31
  • *sigh*... stacktraces in swing... – ApproachingDarknessFish Jan 17 '13 at 21:32
  • 2
    It looks as if you're trying to read a corrupted JPEG file, more specifically either a file with a colour space declaring it as a colour image, but with only one raster channel, or more likely a colour space declaring it as a grayscale image, but with three raster channels. The latter is often produced by broken JPEG encoders and cannot be read with the JPEG decoder in ImageIO. Other JPEG decoders are often more tolerant to incorrectly encoded JPEG files. – jarnbjo Jan 17 '13 at 21:41
  • @jarnbjo Yes, the issue was with the ColorSpace. The ColorSpace was TYPE_GRAY. And ImageIO currently doesn't work with TYPE_GRAY when asked to return a BufferedImage. The link to bug is provided in my answer below. Thanks for your time. – jsf Jan 18 '13 at 20:31
  • The bug you are linking to has absolutely nothing to do with reading broken JPEG files (as obviously is the cause for your stack trace). – jarnbjo Jan 18 '13 at 22:12

2 Answers2

1

So, it's a JDK bug in the javax.imageio API that prevents ImageIO.read(..) to return a BufferedImage. It was reported in year 2007 and still open : (

http://bugs.sun.com/view_bug.do;jsessionid=2e30e4710093bcffffffffa4c93719b2921d5?bug_id=6619667

To resolve my issue - I have implemented a workaround to deal with bytes directly. Life is good. Thanks for taking time though )

jsf
  • 2,851
  • 9
  • 30
  • 33
1

The error message is informative and indicates that the number of raster bands, as mentioned in the ICC color profile, seems to be incorrect. I used ImageMagick to strip the ICC profile from the image. ImageIO subsequently has no problems reading the images (~1k bad images). Hope that helps.

Suchet
  • 206
  • 1
  • 4