Question

I am using a Java program for some image processing and currently I am working with some rather large images (on the order of a gigapixel).

Not surprisingly, I have been unable to do much, due to the following exception:

Exception:
java.lang.IllegalArgumentException: Array size > Integer.MAX_VALUE!
    at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1064)
    at javax.imageio.ImageReader.getDestination(ImageReader.java:2879)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:1046)
    at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:1014)
    at javax.imageio.ImageIO.read(ImageIO.java:1422)
    at javax.imageio.ImageIO.read(ImageIO.java:1282)
...

I am wondering if there is any way I might circumvent this limit by overloading the BufferedImage class with another compatible class that supports large images?

Was it helpful?

Solution

No, the array size limit is hard-wired in the JVM and java specification.

To solve your problem, you will need to abstract from simple images and rasters. A large image can be subdivided into multiple smaller images, each containing only a small rectangular section of the large image.

This will not help you with the exception you got there though. There is nothing you can do about the way ImageIO handles images internally (besides possibly registering the limitation as a bug). You will need to find an alternate library for reading images of that size, or code it yourself (a lot of work to be sure).

EDIT: Creating a subclass of BufferedImage won't help you, BufferedImage is only a thin facade to the underlying structures (Raster, DataBuffer) which are ultimately hard-wired to use arrays for storage. And you would still have the problem ImageIO creating instances of the original BufferedImage class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top