9

I'm unable to create a huge BufferedImage (lack of memory is not the problem). Does anyone have any ideas?

1. new BufferedImage(10000, 1000000, BufferedImage.TYPE_3BYTE_BGR);

Exception in thread "main" java.lang.NegativeArraySizeException
    at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:42)
    at java.awt.image.Raster.createInterleavedRaster(Raster.java:253)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:368)

2. new BufferedImage(10000, 1000000, BufferedImage.TYPE_INT_RGB);

Exception in thread "main" java.lang.IllegalArgumentException: Dimensions (width=10000 height=1000000) are too large    
at java.awt.image.SampleModel.<init>(SampleModel.java:112)
    at java.awt.image.SinglePixelPackedSampleModel.<init>(SinglePixelPackedSampleModel.java:124)
    at java.awt.image.Raster.createPackedRaster(Raster.java:770)
    at java.awt.image.Raster.createPackedRaster(Raster.java:466)
    at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
    at java.awt.image.BufferedImage.<init>(BufferedImage.java:315)
Josh G
  • 91
  • 1
  • 2
  • 2
    What is it that you're trying to do? – Alastair Feb 01 '12 at 02:35
  • 4
    You realize such an image would require approximately 30Gb to hold in memory, right? Please answer Alastair's question, because it seems that whatever you are trying to achieve, you are going about it the wrong way. – Andrew Thompson Feb 01 '12 at 03:16
  • I actually have more than 30 gigs of memory. I'm trying to save an image representation of data to a png file. Thanks. – Josh G Feb 01 '12 at 12:20
  • 1
    Writing a BufferedImage to a png file is very straightforward (http://docs.oracle.com/javase/tutorial/2d/images/saveimage.html). My concern is more about why you have such a ginormous image. Is it meant to be a sprite sheet or some sort of databank by any chance? Because Java places a (reasonable) limit on the sizes of images, we need to know more information so we can suggest a different strategy. – Alastair Feb 01 '12 at 15:57
  • 1
    *"I actually have more than 30 gigs of memory."* What your PC has as memory is not the same thing as what the JRE has as memory. AFAIR with the 32 bit VM could assign a maximum of 2Gb to a Java app. – Andrew Thompson Feb 02 '12 at 01:29

1 Answers1

17

I believe this is a limitation of the Raster class. Width * Height needs to be less than Integer.MAX_VALUE

http://docs.oracle.com/javase/7/docs/api/java/awt/image/Raster.html

As a work around I'd probably split my BufferedImage into sections where width and height are both less than the square root of Integer.MAX_VALUE, so 46,340x46,340 max.

UPDATE: It looks like the PNGJ Library at http://code.google.com/p/pngj/ was created for this purpose.

The Real Baumann
  • 1,941
  • 1
  • 14
  • 20
  • 1
    yeah, there are a number of them. I believe they were written before Java added support for PNG in Java 5 through ImageIO (although ImageIOs is ghastly slow). – Alastair Feb 01 '12 at 17:24
  • Actually I wrote the PNGJ library for my own needs: sequential read/write of huge PNG images (including 48-bit) at low level, decoupled from BufferedImage/ImageIO – leonbloy Mar 27 '12 at 14:17