Reputation: 3
I'm trying to get this code to run, but I keep getting an error saying:
at main.Main.flipVertically(Main.java:403) which is the code below.
img[row][col] = img[height - row - 1][col];
I don't know what's wrong with the code or the error they are talking about.
Here's the rest of the code:
public static int[][] flipVertically(int[][] img) {
String dir = "image.jpg";
img = Util.readImageToMatrix(dir);
int height = img[0].length;
int width = img.length;
for(int row = 0; row < height/2; row++)
{
for(int col = 0; col < width; col++)
{
int p = img[row][col];
img[row][col] = img[height - row - 1][col];
img[height - row - 1][col] = p;
}
}
return img;
}
Upvotes: 0
Views: 714
Reputation: 943
height and width swapped
int height = img.length;
int width = img[0].length;
you souldnt read the matrix in the loop and use the parameter img from function input, or better create a new matrix.
you can swap entire rows like:
public static void flipVertically(int[][] img) {
int height = img.length;
int width = img[0].length;
for(int row = 0; row < height/2; row++)
{
int [] myrow = img[row];
img [row] = img[height - row - 1];
img[height - row - 1] = myrow;
}
}
Upvotes: 1