create a sparse BufferedImage in java

Posted by elgcom on Stack Overflow See other posts from Stack Overflow or by elgcom
Published on 2010-06-11T14:18:17Z Indexed on 2010/06/11 14:42 UTC
Read the original article Hit count: 251

Filed under:
|
|
|
|

I have to create an image with very large resolution, but the image is relatively "sparse", only some areas in the image need to draw.

For example with following code

/* this take 5GB memory */

    final BufferedImage img = new BufferedImage( 36000, 36000, BufferedImage.TYPE_INT_ARGB);

/* draw something */

    Graphics g = img.getGraphics();
    g.drawImage(....);

/* output as PNG */

    final File out = new File("out.png"); 
    ImageIO.write(img, "png", out); 

The PNG image on the end I created is ONLY about 200~300 MB.

The question is how can I avoid creating a 5GB BufferedImage at the beginning? I do need an image with large dimension, but with very sparse color information.

Is there any Stream for BufferedImage so that it will not take so much memory?

© Stack Overflow or respective owner

Related posts about java

Related posts about image