Rectangle 27 15

There's nothing wrong with your code here. I could read your image using my JPEGImageReader plugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.

However, your JPEG file seems to have a number of problems so it can't be read 100%:

  • First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply ignore the ICC profile.
  • Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.

Hi Harald thanks a lot for answering. Now this error is clear to me, thanks for explaining. I tried to use your plugin, but due to some missing dependency libraries I couldn't compile it (I added some twelvemonkeys-imageio jars). So would you mind explaining the exact way to use the plugin, and it would be great if you could show the simple code sample to get a buffered for my image.

Details on how to use the plugin in java apps, and servlet containers can be found here: github.com/haraldk/TwelveMonkeys

Worked like a charm :)

@haraldK ok, I found out that package libimage-exiftool-perl contains exiftool. So, I have an image with a value of 3 for colour components and CMYK as colour space data. IIRC, CMYK has 4 colour components and that mismatch is what causes the exception. Is that right?

java - IllegalArgumentException: Numbers of source Raster bands and so...

java image-processing bufferedimage javax.imageio
Rectangle 27 15

There's nothing wrong with your code here. I could read your image using my JPEGImageReader plugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.

However, your JPEG file seems to have a number of problems so it can't be read 100%:

  • First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply ignore the ICC profile.
  • Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.

Hi Harald thanks a lot for answering. Now this error is clear to me, thanks for explaining. I tried to use your plugin, but due to some missing dependency libraries I couldn't compile it (I added some twelvemonkeys-imageio jars). So would you mind explaining the exact way to use the plugin, and it would be great if you could show the simple code sample to get a buffered for my image.

Details on how to use the plugin in java apps, and servlet containers can be found here: github.com/haraldk/TwelveMonkeys

Worked like a charm :)

@haraldK ok, I found out that package libimage-exiftool-perl contains exiftool. So, I have an image with a value of 3 for colour components and CMYK as colour space data. IIRC, CMYK has 4 colour components and that mismatch is what causes the exception. Is that right?

java - IllegalArgumentException: Numbers of source Raster bands and so...

java image-processing bufferedimage javax.imageio
Rectangle 27 15

There's nothing wrong with your code here. I could read your image using my JPEGImageReader plugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.

However, your JPEG file seems to have a number of problems so it can't be read 100%:

  • First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply ignore the ICC profile.
  • Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.

Hi Harald thanks a lot for answering. Now this error is clear to me, thanks for explaining. I tried to use your plugin, but due to some missing dependency libraries I couldn't compile it (I added some twelvemonkeys-imageio jars). So would you mind explaining the exact way to use the plugin, and it would be great if you could show the simple code sample to get a buffered for my image.

Details on how to use the plugin in java apps, and servlet containers can be found here: github.com/haraldk/TwelveMonkeys

Worked like a charm :)

@haraldK ok, I found out that package libimage-exiftool-perl contains exiftool. So, I have an image with a value of 3 for colour components and CMYK as colour space data. IIRC, CMYK has 4 colour components and that mismatch is what causes the exception. Is that right?

java - IllegalArgumentException: Numbers of source Raster bands and so...

java image-processing bufferedimage javax.imageio
Rectangle 27 15

There's nothing wrong with your code here. I could read your image using my JPEGImageReader plugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.

However, your JPEG file seems to have a number of problems so it can't be read 100%:

  • First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply ignore the ICC profile.
  • Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.

Hi Harald thanks a lot for answering. Now this error is clear to me, thanks for explaining. I tried to use your plugin, but due to some missing dependency libraries I couldn't compile it (I added some twelvemonkeys-imageio jars). So would you mind explaining the exact way to use the plugin, and it would be great if you could show the simple code sample to get a buffered for my image.

Details on how to use the plugin in java apps, and servlet containers can be found here: github.com/haraldk/TwelveMonkeys

Worked like a charm :)

@haraldK ok, I found out that package libimage-exiftool-perl contains exiftool. So, I have an image with a value of 3 for colour components and CMYK as colour space data. IIRC, CMYK has 4 colour components and that mismatch is what causes the exception. Is that right?

java - IllegalArgumentException: Numbers of source Raster bands and so...

java image-processing bufferedimage javax.imageio
Rectangle 27 4

An empty iterator usually means ImageIO hasn't found a good image reader for decoding your image. This may be because you'are missing the right decoder in your classpath, or you image has an unsupported color model.

javax.imageio - Detecting image encoding in Java - Stack Overflow

java javax.imageio
Rectangle 27 3

Because the ImageIO plugin registry (the IIORegistry) is "VM global", it doesn't by default work well with servlet contexts. This is especially evident if you load plugins from the WEB-INF/lib or classes folder, as the OP seems to do.

Servlet contexts dynamically loads and unloads classes (using a new class loader per context). If you restart your application, old classes will by default remain in memory forever (because the next time scanForPlugins is called, it's another ClassLoader that scans/loads classes, and thus they will be new instances in the registry. In the test code loop, the same ClassLoader is used all the time, thus instances are replaced, and the real problem never manifests).

To work around this resource leak, I recommend using a ContextListener to make sure these "context local" plugins are explicitly removed. Here's an example IIOProviderContextListener I've used in a couple of projects, that implements dynamic loading and unloading of ImageIO plugins.

java - How to resolve OutOfMemoryError with ImageIO plugins as the cau...

java out-of-memory javax.imageio
Rectangle 27 11

So I was having this same issue and found that the image was gray-scale and that the default ImageIO.read implementation was not figuring that out because the image metadata wasn't quite as expected. I wrote a work around that retries the load as 'BufferedImage.TYPE_BYTE_GRAY' if it fails the main load.

Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

        Exception lastException = null;
        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;
                    }
                }
                bufferedImage = reader.read(0, param);
                if (null != bufferedImage) break;
            } catch (Exception e) {
                lastException = e;
            } finally {
                if (null != reader) reader.dispose();               
            }
        }
        // If you don't have an image at the end of all readers
        if (null == bufferedImage) {
            if (null != lastException) {
                throw lastException;
            }
        }

The beauty of SO: A problem that would have taken me two hours to figure out now takes 10 minutes. Thank you, Tate Moore.

Wonderful stuff! Never thought anyone would come back and knock this one down but you did! Lovely stuff.

when I use this solution after it fails with ImageIO.read(InputStream) it does not execute the rest of the code (while (iter.hasNext()) is false), but if I omit ImageIO.read(InputStream) part it does the job. What is wrong with my code?

+1 because it works on some gray-scale cases, not working with ImageIO.read(...). But note, this code forces image load as gray-scale, even if it's not. Yout have to make first a ImageIO.read(...) & if it fails, use this method.

stream
ImageIO.createImageInputStream(InputStream)
FileInputStream
ByteArrayInputStream

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 11

So I was having this same issue and found that the image was gray-scale and that the default ImageIO.read implementation was not figuring that out because the image metadata wasn't quite as expected. I wrote a work around that retries the load as 'BufferedImage.TYPE_BYTE_GRAY' if it fails the main load.

Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

        Exception lastException = null;
        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;
                    }
                }
                bufferedImage = reader.read(0, param);
                if (null != bufferedImage) break;
            } catch (Exception e) {
                lastException = e;
            } finally {
                if (null != reader) reader.dispose();               
            }
        }
        // If you don't have an image at the end of all readers
        if (null == bufferedImage) {
            if (null != lastException) {
                throw lastException;
            }
        }

The beauty of SO: A problem that would have taken me two hours to figure out now takes 10 minutes. Thank you, Tate Moore.

Wonderful stuff! Never thought anyone would come back and knock this one down but you did! Lovely stuff.

when I use this solution after it fails with ImageIO.read(InputStream) it does not execute the rest of the code (while (iter.hasNext()) is false), but if I omit ImageIO.read(InputStream) part it does the job. What is wrong with my code?

+1 because it works on some gray-scale cases, not working with ImageIO.read(...). But note, this code forces image load as gray-scale, even if it's not. Yout have to make first a ImageIO.read(...) & if it fails, use this method.

stream
ImageIO.createImageInputStream(InputStream)
FileInputStream
ByteArrayInputStream

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 11

So I was having this same issue and found that the image was gray-scale and that the default ImageIO.read implementation was not figuring that out because the image metadata wasn't quite as expected. I wrote a work around that retries the load as 'BufferedImage.TYPE_BYTE_GRAY' if it fails the main load.

Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

        Exception lastException = null;
        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;
                    }
                }
                bufferedImage = reader.read(0, param);
                if (null != bufferedImage) break;
            } catch (Exception e) {
                lastException = e;
            } finally {
                if (null != reader) reader.dispose();               
            }
        }
        // If you don't have an image at the end of all readers
        if (null == bufferedImage) {
            if (null != lastException) {
                throw lastException;
            }
        }

The beauty of SO: A problem that would have taken me two hours to figure out now takes 10 minutes. Thank you, Tate Moore.

Wonderful stuff! Never thought anyone would come back and knock this one down but you did! Lovely stuff.

when I use this solution after it fails with ImageIO.read(InputStream) it does not execute the rest of the code (while (iter.hasNext()) is false), but if I omit ImageIO.read(InputStream) part it does the job. What is wrong with my code?

+1 because it works on some gray-scale cases, not working with ImageIO.read(...). But note, this code forces image load as gray-scale, even if it's not. Yout have to make first a ImageIO.read(...) & if it fails, use this method.

stream
ImageIO.createImageInputStream(InputStream)
FileInputStream
ByteArrayInputStream

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 11

So I was having this same issue and found that the image was gray-scale and that the default ImageIO.read implementation was not figuring that out because the image metadata wasn't quite as expected. I wrote a work around that retries the load as 'BufferedImage.TYPE_BYTE_GRAY' if it fails the main load.

Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

        Exception lastException = null;
        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;
                    }
                }
                bufferedImage = reader.read(0, param);
                if (null != bufferedImage) break;
            } catch (Exception e) {
                lastException = e;
            } finally {
                if (null != reader) reader.dispose();               
            }
        }
        // If you don't have an image at the end of all readers
        if (null == bufferedImage) {
            if (null != lastException) {
                throw lastException;
            }
        }

The beauty of SO: A problem that would have taken me two hours to figure out now takes 10 minutes. Thank you, Tate Moore.

Wonderful stuff! Never thought anyone would come back and knock this one down but you did! Lovely stuff.

when I use this solution after it fails with ImageIO.read(InputStream) it does not execute the rest of the code (while (iter.hasNext()) is false), but if I omit ImageIO.read(InputStream) part it does the job. What is wrong with my code?

+1 because it works on some gray-scale cases, not working with ImageIO.read(...). But note, this code forces image load as gray-scale, even if it's not. Yout have to make first a ImageIO.read(...) & if it fails, use this method.

stream
ImageIO.createImageInputStream(InputStream)
FileInputStream
ByteArrayInputStream

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 4

You don't need to use JAI. I would recommend you use the normal ImageIO API and a TIFF plugin.

There are several TIFF plugins for ImageIO available, and depending on your needs, you could use the one from jai-imageio, my TIFF plugin or some other third-party plugin.

My plugin supports most common types of TIFFs. In contrast to jai-imageio, no native libs are required. You can either add Maven dependencies to your project or download directly from this link.

The good thing about using the ImageIO API, is that after you have the plugins properly installed, all you need to do is:

BufferedImage img = ImageIO.read("pics/img"+i+".tif");

Worked instantely after simply adding to the pom.xml (from Maven Central). Even with 16 bit TIFF images. Good work, thank you!

Java Handling TIF Images - Stack Overflow

java image tiff javax.imageio
Rectangle 27 4

ImageIO reads the image within the same thread context it was called from, Toolkit.getDefaultToolkit().getImage() uses a separate background Thread to load the image in and returns (almost) immediately.

This is why you have MediaTracker#waitForAll and MediaTracker#waitForID and ImageObserver. This all monitor the state of a image loading process which was created by Toolkit.getDefaultToolkit().getImage() so they known when the image loading has completed...

Hm... are you sure? If yes, how could I do the same with ImageIO.read() ?

Wrap the ImageIO.read into a separate thread. This will mean that until ImageIO.read, you won't have any image data...

Yeah, you are right. The image is not completely loaded if I try to check its size exactly after the call to getImage().

java - Why is ImageIO.read() so slow compared to Toolkit.getDefaultToo...

java image javax.imageio
Rectangle 27 4

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.

It did! Thanks. I definitely think the message is informative, it's just above my level of understanding, not their fault. Thanks for commenting :)

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 4

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.

It did! Thanks. I definitely think the message is informative, it's just above my level of understanding, not their fault. Thanks for commenting :)

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 4

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.

It did! Thanks. I definitely think the message is informative, it's just above my level of understanding, not their fault. Thanks for commenting :)

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 4

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.

It did! Thanks. I definitely think the message is informative, it's just above my level of understanding, not their fault. Thanks for commenting :)

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 3

As @Bozho points out, the ImageIO uses the default URL.openConnection which (since the address starts with "http") returns a HttpURLConnection which, per default has setFollowRedirects(true).

URL url = new URL("http://graph.facebook.com/804672289/picture");
BufferedImage bi = ImageIO.read(url);

System.out.println(bi);
// Prints: BufferedImage@43b09468: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@7ddf5a8f transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 50 height = 50 #numDataElements 3 dataOff[0] = 2

The problem is that the URL I was using was https:// and not http://. The question is why all I see is that bi becomes null, and not some error - and how can I overcome the issue without replacing https with http.

java - Using ImageIO.read with a redirecting URL - Stack Overflow

java http javax.imageio jai
Rectangle 27 1

It is possible to read this image using twelvemonkeys ImageIO, which is a more robust and forgiving replacement for the original ImageIO provided by the JRE.

In order to use twelvemonkeys, it is sufficient to add it as a maven dependency. It then registers itself before the default image processor.

<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-jpeg</artifactId>
    <version>3.3.2</version> <!-- Alternatively, build your own version -->
</dependency>

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 1

It is possible to read this image using twelvemonkeys ImageIO, which is a more robust and forgiving replacement for the original ImageIO provided by the JRE.

In order to use twelvemonkeys, it is sufficient to add it as a maven dependency. It then registers itself before the default image processor.

<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-jpeg</artifactId>
    <version>3.3.2</version> <!-- Alternatively, build your own version -->
</dependency>

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio
Rectangle 27 1

It is possible to read this image using twelvemonkeys ImageIO, which is a more robust and forgiving replacement for the original ImageIO provided by the JRE.

In order to use twelvemonkeys, it is sufficient to add it as a maven dependency. It then registers itself before the default image processor.

<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-jpeg</artifactId>
    <version>3.3.2</version> <!-- Alternatively, build your own version -->
</dependency>

java - ImageIO.read illegal argument exception - raster bands/colour s...

java image javax.imageio