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 spa开发者_开发知识库rse color information.
Is there any Stream for BufferedImage so that it will not take so much memory?
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.
You example uses TYPE_INT_ARGB as the image type which is 4 bytes per pixel. If the image has a limited number of colours you might be able to use a different image type with fewer bytes per pixel.
This could reduce the number of bytes per pixel but the best case scenario is 1 byte per pixel which will still use over 1 GB.
Otherwise you could attempt to divide the rendering into multiple images and merge them together separatly.
精彩评论