JDK-4884308 : IllegalArgExptn thrown when reading image using TYPE_BYTE_GRAY after TYPE_CUSTOM
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 1.8,1.4.0,11,12
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2003-06-26
  • Updated: 2019-03-25
Related Reports
Relates :  
I am trying to read a jpeg image from a file using ImageIO using all the type specifiers returned by reader.getImageTypes(). The JPEG reader returns 3 type specifiers - TYPE_3BYTE_BGR, TYPE_BYTE_GRAY and TYPE_CUSTOM. First I am setting the destination in the image read param to the buffered image created out of TYPE_CUSTOM and reading the image. I am able to read the image successfully. When I set the destination to the buffered image created out of TYPE_BYTE_GRAY right after this and try to read the same image using the same reader I am getting an IllegalArgumentException which is specified at the end of this bug report. But this does not occur when i read the image first using TYPE_BYTE_GRAY. This is reproducible on Solaris as well as Windows right from JDK1.4+. 

I have attached a sample code. Execute it. You would see the list of type specifiers returned by JPEG Reader. You will also see this IllegalArgumentException. When you edit and comment out the portion of the code which sets the destination to TYPE_CUSTOM the gray scale image will be read properly. But I am not quite sure why this is failing especially when doing it after TYPE_CUSTOM. 

Here is the exception:
java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match
at java.awt.image.ColorConvertOp.filter(ColorConvertOp.java:437)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.acceptPixels(JPEGImageReader.java:1092)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1053)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:853)
at SetDestinationTest.<init>(SetDestinationTest.java:47)
at SetDestinationTest.main(SetDestinationTest.java:99)

Here is a JPEG file that reproduces the error. Just call ImageIO.read(new File("JDK-4884308.jpg")); and the exception is thrown.