Create sparse BufferedImage in java

I need to create an image with a very high resolution, but the image is relatively "sparse", I need to draw only some areas of the image.

For example, using the 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 at the end that I created is ONLY about 200 ~ 300 MB.

Question: How can I avoid creating a 5GB BufferedImage at the beginning? I need an image with a large size, but with very rare color information.

Is there any thread for BufferedImage so it doesn't take up so much memory?

+3
source share
2 answers

- , BufferedImages?

: . , AffineTransform. , , . , PNG Java, .

+1

TYPE_INT_ARGB , 4 . , .

, - 1 , - 1 .

.

0

Source: https://habr.com/ru/post/1749602/


All Articles