Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a sparse BufferedImage in java

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?

like image 776
rnd_nr_gen Avatar asked Dec 08 '25 10:12

rnd_nr_gen


1 Answers

Is there a reason for not using a lazy-initialized Map or similar structure full of smaller BufferedImages?

Edit: That's a pretty specialized data structure. If you want to draw across images, tile them in a some reasonable fashion and apply a corresponding AffineTransform to each one. Then you can just iterate over the Collection and draw to each one and null it out or Flyweight it if it's blank right afterward. I don't know of non-painful way to combine them all into a PNG inside Java though.

like image 150
j flemm Avatar answered Dec 09 '25 22:12

j flemm