Arbitrary scaling is really needed. I created one method in my MicroGo game to scale source images (mutable or immutable) into an immutable image.
Comments? 8)
/**
* Changes the image to a new size
* @author Yu You
* Copyright 2005.
* PM me at: yuyou_@hotmail.com
*
* @param originalImage,
* or null
* @param width
* @param height
* @return Image the new Image. Never be null.
*/
public static Image resize(Image originalImage, int width, int height) {
//if (height <= 0 || width <= 0)
//throw new Exception(“ERROR_INVALID_ARGUMENT”);
if (originalImage == null)
return Image.createImage(height, width);
int orig_h = originalImage.getHeight();
int orig_w = originalImage.getWidth();
if (orig_w == width && orig_h == height)
return originalImage;
int[] orig = new int[orig_h * orig_w];
originalImage.getRGB(orig, 0, orig_w, 0, 0, orig_w, orig_h);
int index;
int[] newbuffer = new int[height * width];
int loc, oldloc = 0;
//long before = System.currentTimeMillis();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
loc = i * width + j;
index = (int) (i * orig_h / height) * orig_w
+ (int) (j * orig_w / width);
//System.arraycopy(orig,index,newbuffer,loc,1);
newbuffer[loc] = orig[index];
}
}
//System.out.println(“time:”+(System.currentTimeMillis()-before));
Image newImage = Image.createRGBImage(newbuffer, width, height, true);
orig = null;
newbuffer = null;
System.gc(); //
return newImage;
}