I have a problem saving large (f.e. 12 000 x 9 000 ) images.
I’m developing a graphical editing software ( something like simple Photoshop ) and
The user obviously has to have to ability to save the image.
Lets say I would like to save the image as .png.
Does JAVA always need to use the BufferedImage for saving drawn stuff ?
I know the equation for size of the image is:
Xsize * Ysize * 4 ( red, green, blue, alpha )
So in this case we get over 400 MB.
I know I could save the image in parts ( tiles ) but the user would have to merge them somehow anyway.
Is there any other way to save such a large image without using the BufferedImage ?
Code for saving the image:
public static void SavePanel() {
BufferedImage image = null;
image = new BufferedImage(
(int) (Main.scale * sizeX ),
(int) (Main.scale * sizeY ),
BufferedImage.TYPE_INT_RGB);
g2 = image.createGraphics();
panel.paint(g2);
try {
ImageIO.write(image, "png", new File(FactoryDialog.ProjectNameTxt.getText() + ".png"));
} catch (IOException e) {
}
}
Thank you in advance !
The
ImageIO.write(..)methods accept anRenderedImage, not just aBufferedImage. I successfully exploited this fact some time ago to write out really large images. Generally, the writer implementations write out the image sequentially, and ask theRenderedImageonly for the pieces they currently need.From looking at your code, I think it should be possible to hack a
RenderedImageimplementation which takes yourpanelin it’s constructor and can be passed toImageIOfor writing. During the process,ImageIOwill request data from your image. You can then use the panel to create the requested pieces (Rastercontents) on the fly. This way, the whole image does not have to be stored in memory at any point. A starting point for this approach isObviously, you should also check if your
paneldoesn’t suffer from the same problem as theBufferedImage. Depending on the nature of you application, you’ll have to hold the image in memory at least once anyway (modulo using tiles). But this way you can at least avoid the duplication.