Skip to main content

How can I generate huge image using java?

6 replies [Last post]
acrbb
Offline
Joined: 2010-03-31
Points: 0

Dear all,
In my project, i need draw a complicate image, we use bufferedImage to get Graphics. Below is the code.

BufferedImage ssi = new BufferedImage(width,height,java.awt.image.BufferedImage.TYPE_INT_ARGB);
graphics = ssi.createGraphics();

As the width and height is cacluate accroding to a xml. Sometimes, width and height is very big, such as 9000*9000, which need consume about 243M heap space. It will lead to out of memory issue.

I read some materials related to JAI from java.net. But I still have some questions. Would you please take a look?

1. As the image is huge, If I draw within memory, the oom will happen. So Can i draw the picture using some cahe, swap space?

2. As the items in the image is connected, such as a line connect to image. Is ther any easy way to draw the image?

3. I saw some one mention JDI + ImageIO. I am not very clear about it. Can they solve my issues?

Thanks in advance.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
acrbb
Offline
Joined: 2010-03-31
Points: 0

Nidel, Mike 's Anser is good. But I can't see the correct button for Nidel, Mike's.

Thank you. Nidel, Mike

mnidel
Offline
Joined: 2005-04-08
Points: 0

That's probably because I normally post by email. Don't worry about it.

Nidel, Mike

What is it you want to do with the huge image when you are finished
drawing on it? Do you want to save it to a file, or display it on the
screen, or something else?

The key to processing huge images like this is only to load small pieces
of the data at any one time -- and if you load more than you can fit in
memory, you have to throw some of it out (or save to disk). Just like you
said, caching and swap space.

The key to this is to use tiling. Tiles are the small pieces you CAN fit
in memory at one time. If you store your graphics in memory (lines, polygons,
text or whatever) and then only draw on the tiles that you need to load
at any given time, then you won't have any problems.

BufferedImage won't work, though, because it doesn't support tiling. So
you need to use a different implementation of RenderedImage. You can
try to use JAI but it doesn't have exactly what you need (as has already
been said). You can build your own implementation of RenderedImage, or
what I recommend is to use JAI and subclass PlanarImage. Then you can
implement getTile() and draw your graphics on it as needed. Here is some
pseudocode, the method signatures are definitely NOT right:

public Raster getTile(int x, int y)
{
Raster tile = readTile(x, y); // you implement this yourself by reading from a file or whatever
BufferedImage bi = new BufferedImage(tile); // this is NOT the actual constructor
Graphics2D g2d = bi.createGraphics();
g2d.translate(tileX, tileY);
g2d.draw(...);// do your drawing here like you wanted to do
g2d.dispose;
return tile;
}

Note that you need to actually create a WritableRaster to pass into the
BufferedImage constructor. You'll need a few more lines but most of it
is there.

It sounds like you have a pretty good understanding of the problem, if
you need some more explanation feel free to ask.

Mike

> -----Original Message-----
> From: java2d@javadesktop.org [mailto:java2d@javadesktop.org]
> Sent: Thursday, April 01, 2010 2:38 AM
> To: java2d-interest@java-mail-lists.kenai.com
> Subject: [JAVA2D] How can I generate huge image using java?
>
> Dear all,
> In my project, i need draw a complicate image, we use bufferedImage to
> get Graphics. Below is the code.
>
> [b]BufferedImage ssi = new
> BufferedImage(width,height,java.awt.image.BufferedImage.TYPE_INT_ARGB);
> graphics = ssi.createGraphics();[/b]
>
> As the width and height is cacluate accroding to a xml. Sometimes,
> width and height is very big, such as 9000*9000, which need consume
> about 243M heap space. It will lead to out of memory issue.
>
> I read some materials related to JAI from java.net. But I still have
> some questions. Would you please take a look?
>
> 1. As the image is huge, If I draw within memory, the oom will happen.
> So Can i draw the picture using some cahe, swap space?
>
> 2. As the items in the image is connected, such as a line connect to
> image. Is ther any easy way to draw the image?
>
> 3. I saw some one mention JDI + ImageIO. I am not very clear about it.
> Can they solve my issues?
>
> Thanks in advance.
> [Message sent by forum member 'acrbb']
>
> http://forums.java.net/jive/thread.jspa?messageID=394800
--
To unsubscribe from the java2d-interest list, send email to:
java2d-interest-unsubscribe@java-mail-lists.kenai.com
Instructions will be mailed back to you.

acrbb
Offline
Joined: 2010-03-31
Points: 0

Hi Mike,
Thanks for the detail answer.

1. So to solve such problem, the key is to divide the image to serveral smaller image. Take divide 9 image as below. As image is compilcated, such as a line will conect 2 items, the 2 items may lies in area 3,4,5 8. I have some logica to control it, is it right?

0 1 2
3 4 5
6 7 8

Nidel, Mike

What is it you want to do with these images? Display them on the
screen, or save them to a file, or what?

You have the basic idea right. The size of the subimages 0, 1, 2
etc. will be fixed and you can easily determine if the line you
are drawing touches a particular tile.

You don't actually need to create separate image objects for each
part though, you can simply use a logical division of the RenderedImage
into tiles or Rasters.

> -----Original Message-----
> From: java2d@javadesktop.org [mailto:java2d@javadesktop.org]
> Sent: Friday, April 02, 2010 6:30 AM
> To: java2d-interest@java-mail-lists.kenai.com
> Subject: [JAVA2D] Re: How can I generate huge image using java?
>
> Hi Mike,
> Thanks for the detail answer.
>
> 1. So to solve such problem, the key is to divide the image to serveral
> smaller image. Take divide 9 image as below. As image is compilcated,
> such as a line will conect 2 items, the 2 items may lies in area 3,4,5
> 8. I have some logica to control it, is it right?
>
> 0 1 2
> 3 4 5
> 6 7 8
> [Message sent by forum member 'acrbb']
>
> http://forums.java.net/jive/thread.jspa?messageID=395030
--
To unsubscribe from the java2d-interest list, send email to:
java2d-interest-unsubscribe@java-mail-lists.kenai.com
Instructions will be mailed back to you.

imagero
Offline
Joined: 2003-11-18
Points: 0

JAI would not help you because JAI's TiledImage has all its tiles loaded - in memory. It may use even more memory as BufferedImage.

You may try Imagero (free for non-commercial use) - it supports deferred Java2D Graphics operations.