Hi all, I know there are simpler ways to do this in Java but I’m writing a method to scale an image represented by pixel array. This is what I’ve managed to knock up so far with guess work:
public void scale(double factor) {
int newWidth = (int) Math.abs(width * factor);
int newHeight = (int) Math.abs(height * factor);
int[] temp = new int[newWidth * newHeight];
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
int newX = (int) (x * factor);
int newY = (int) (y * factor);
if(newX < 0) newX += newWidth;
if(newY < 0) newY += newHeight;
if(newX < 0 || newY < 0 || newX >= newWidth || newY >= newHeight) continue;
temp[(int) (newY * newWidth + newX)] = pixels[y * width + x];
}
}
width = newWidth;
height = newHeight;
pixels = temp.clone();
}
This is inside a bitmap class which has its own pixel array. This method creates a new pixel array of the correct size, then loops through every pixel and scales each pixel according to the scale factor. It works fine scaling down, but when scaling up it creates a grid-like effect where pixel aren’t being filled as it doesn’t scale up. I tried this the other way round by looping through each pixel in the new array and mapping pixels from the old array, which works fine when mapping up, but doesn’t work for scaling down.
Does anyone know how I could modify this algorithm to cater for scaling in both directions? Thanks
MODIFIED:
public void TESTscale(double factor) {
int newWidth = (int) Math.abs(width * factor);
int newHeight = (int) Math.abs(height * factor);
int[] temp = new int[newWidth * newHeight];
double xRatio = width / (double) newWidth;
double yRatio = height / (double) newHeight;
double newX, newY;
for(int y = 0; y < newHeight; y++) {
for(int x = 0; x < newWidth; x++) {
newX = Math.floor(x * xRatio);
newY = Math.floor(y * yRatio);
temp[y * newWidth + x] = pixels[(int) (newY * width + newX)];
}
}
width = newWidth;
height = newHeight;
pixels = temp.clone();
}
Paul