Problems scaling down images and to set them to power of 2

Hi, i have written some code to take care of scaling of images for use with opengl.
My first problem is that i get outOfMemoryError everytime, i have tried different aproaches but every one ends up with that error.

The thing is that if i have images larger than 2048x1024 i get the error, so i need to first scale down the image, and then i scale it down again so i get it to the powerOf2.

Here is the code for the scaling. And if i try this code for an image that is 2048x2048 i get the error, does someone know a solution?

try
	{
		String temp[] = resourceName.split(":");
		BufferedImage img = null;
		if(temp.length == 0)
  	img = ImageIO.read(new BufferedInputStream(getClass().getClassLoader().getResourceAsStream(resourceName)));
		else
			img = ImageIO.read(new BufferedInputStream(new java.io.FileInputStream(resourceName)));
  if(img == null)
    return null;
		
		int imgSize = img.getHeight()+img.getWidth();
		
  AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
		if(imgSize > 3072)
		{
			double imgScale = (double)3072/imgSize;
			tx.scale(imgScale,imgScale);
		}	
		tx.translate(0, -img.getHeight(null));
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		img = op.filter(img, null);
		//if(img.getWidth() != img.getHeight())
		tx.scale((double)((double)powerOf2(img.getWidth())/(double)img.getWidth()),(double)((double)powerOf2(img.getHeight())/(double)img.getHeight()));
		tx.translate(0, -img.getHeight(null));
		op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		img = op.filter(img, null);
  if(img == null)
    System.out.println("Error reading image");
  System.out.println("Reading Texture: "+resourceName+", width="+img.getWidth()+", height="+img.getHeight());
			//System.out.println("Bildet skalert:"+img.getWidth()+"x"+img.getHeight());
		return img;
	}
	catch (IOException e)
	{
	  return null;
	}

After enough googling i found out that -Xmxm works, but is it some other way so i can use images of any size?

Not really. You can use gluScaleImage, which allocates its storage outside the Java heap using New I/O, but honestly for best portability and robustness I would recommend you keep doing what you are doing. We are considering changing our gluScaleImage implementation in a future release to do basically what you’re doing.

Thx for reply:) ok, well, i then think i just will have to pass the Xmx parameter until then:)