Link to home
Start Free TrialLog in
Avatar of tigress298
tigress298

asked on

java.lang.OutOfMemoryError with jai

I am loading an image into a renderedOP from a cgi call and udating it based on manipulation inputs (rotate, scale, etc).  After about 3 rotations, the system crashes with an outofmemory error.  This app wil eventually become an applet, so compiling with a larger vm memory size probably won't work as I've seen in other forums.  Those forums also suggest using JAI, but I already am and can't figure out how to avoid the error.
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

how big is the image?

can you post some code?
Avatar of tigress298
tigress298

ASKER

The images are pretty large (about 30,000 pixels length and height each, for the full res image), but I don't know the exact sizes.  The user selects the res.

The code is a bit obfuscated, and I can't give the address of the actual images, but basicall I set up a query string in chipQuery() which I access from showChip() and create the image.  

public void showChip() {
        if (canvas.isVisible() == true) {
            String query = chipQuery();
            java.net.URL url;
            try {
                canvas.getDesktop().getMsgCenter().getMessageArea().setText(
                canvas.getDesktop().getMsgCenter().getMessageArea().getText() +
                "\nAccessing: "+ query);
                url = new java.net.URL(query);
            }
            catch (Exception e) {
                url = null;
                e.printStackTrace();
            }            
            if (url != null) {
                //canvas.getChip().setIcon(new javax.swing.ImageIcon(url));
                currentImage = null;
                currentImage = JAI.create("url", url);
                canvas.getChip().setIcon(new javax.swing.ImageIcon(
                    currentImage.getAsBufferedImage().getScaledInstance(
                    currentImage.getWidth(), currentImage.getHeight(), 0)));
            }
        }
    }

on rotation, which ultimately spawns the error, I set the rotation value in rotate(), and setup the parameter block for rotation in rotateChip()

 public void rotate(int angle) {
        int newAngle = angle;
        if (newAngle != currentRotation) {
            int difference = newAngle - currentRotation;
            rotateChip(difference);
            currentRotation = newAngle;
        }
    }

private void rotateChip(int angle) {
        if (canvas.isVisible() == true) {
            RenderedOp image;
            ParameterBlock pb = new ParameterBlock();
            pb.addSource(currentImage);
            pb.add((float)referencePoint.x);
            pb.add((float)referencePoint.y);
            pb.add((float)Math.toRadians((double)angle));
            pb.add(new InterpolationBilinear());
            image = JAI.create("Rotate", pb, null);
            canvas.getChip().setIcon(new javax.swing.ImageIcon(
                image.getAsBufferedImage().getScaledInstance(
                image.getWidth(), image.getHeight(), 0)));
            currentImage = image;
        }
    }  


Thanks.
Could be simply too much to handle if it's too big. Why not try creating tiles of the image and rotating the tiles sequentially? That way you can reduce memory footprint and still get the result you need
I'm guessing it's the:

canvas.getChip().setIcon(new javax.swing.ImageIcon(
                image.getAsBufferedImage().getScaledInstance(
                image.getWidth(), image.getHeight(), 0)));

line which throws the exception?

Hmmm...  I *think* you will have to do this by overriding the paintComponent on a JPanel (instead of setting the image as an Icon to a JLabel), and only draw the area that you can see, by using AffineTransform on the Graphics2D object that you get in that function...  

That way, you won't be creating a new Image object each time you do a transformation...

That might help anyway...  I'm not at work at the moment, so I can't dig out much code, but here are some links that may be of some help:

http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html
http://javaalmanac.com/egs/java.awt/TransformImage.html

Remember, override paintComponent (for a JPanel), not paint...

Tim
So, as (I think) CEHJ is saying, you will end up with a "stack" of transformations, and your original image...  Before you draw the image, add this stack of AffineTransforms to the Graphics2D object sequentially, then simply draw the image as normal...

Then you only create the image once (when you load it)
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How would I go about tiling the image?  

Also, the cgi call is based off the current viewer veriosn (java 1.1) which I'm revamping.  The current call includes among other things, the resLevel, the currentMode(rotate, scale, etc), a reference point (distance from the center of the image for translation), the dimensions of the expected image at the current resLevel, the viewable size (512 or 1024), and the rotation angle.  The cgi handled the image manipulation server side and passed back the image, but is VERY slow, so I'm having a hard time knowing what info to handle inside the applet with JAI, and what to let the server handle (and how to keep the differences reconciled).  Otherwise, loading the image once and handling all manipulations with JAI would be optimal.
What i meant was rather than going straight from (using e.g. 180 degree rotation)

¡ ¡ ¡ ¡
¡ ¡ ¡ ¡

to

! ! ! !
! ! ! !

tile and go in steps, reassembling

1.

! ¡ ¡ ¡
¡ ¡ ¡ ¡

2.

! ! ¡ ¡
¡ ¡ ¡ ¡

etc.
I stil don't know how to go about doing that, though I understand what you're doing.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial