1

This is what I have so far:

public static Photograph rotated(Photograph photo) {
    Photograph rotated_copy = new Photograph(photo.getHeight(), photo.getWidth());
    Pixel starting_pixel = photo.getPixel(0,0);

    for(int col = 0; col < photo.getWidth(); col++){
        for(int row = 0; row < photo.getHeight(); row++){
            starting_pixel = photo.getPixel(col,row);
            rotated_copy.setPixel(row, col, starting_pixel);
        }
    }
    return rotated_copy;
}

However, this method rotates any Photograph taken in 90 degrees counter-clockwise. How can I fix this?

  • 90* counter-clockwise = x* clockwise. Find x and rethink the method. – Tdorno Oct 23 '13 at 21:47
  • Are you sure this really rotates it by 90 degrees? It just switches all the row indexes with columns... For example pixel 0,0 stays on 0,0, right? – libik Oct 23 '13 at 21:48
  • In fact, I think it just switches the picture with the center being the x=-y function... – libik Oct 23 '13 at 21:50
  • 1
    Your code is not rotating the image; it is flipping the image (mirroring the image along the line where x=y). – Big Al Oct 23 '13 at 21:50
  • Ah crap Big Al. In that case, how would I make it rotate? – Pernicious Rage Oct 23 '13 at 21:50
  • 1
    Big Al is wrong, your code is indeed rotating. You've tested it, right? And the problem is counter-clockwise? So why are you listening to him saying its flipping? – SpacePrez Oct 23 '13 at 21:51
  • Yeah Zaphod, I tested it and it's rotating counter-clockwise. – Pernicious Rage Oct 23 '13 at 21:53
  • I think here is your response: http://stackoverflow.com/questions/12165977/java-image-rotation – promanski Oct 23 '13 at 21:54
  • 1
    @Zaphod42 No, it's not rotating. The element at `[0][0]` always remains the same and also the element at `[rowscount][colscount]`. Take a look at [this IDEOne example with a simple int array](http://ideone.com/unxMJ6) – BackSlash Oct 23 '13 at 21:55
  • Can you use BufferedImage instead? – Kakalokia Oct 23 '13 at 21:57
  • @AliAlamiri the actual object implementation does not matter. You can do getPixel and setPixel with BufferedImage too. BufferedImage may have a convenient rotate method, but his point still stands. – SpacePrez Oct 23 '13 at 21:59
  • "Ah crap Big Al. In that case, how would I make it rotate" ... you think its rotate because it changes the picture, but `it does not rotate` it switch the picture with x=-y function as center – libik Oct 23 '13 at 22:01
  • 1
    Yeah, its actually mirroring the image across both axis, which makes it seem almost like its rotating but it isn't. I've modified my answer to point him in the right direction. – SpacePrez Oct 23 '13 at 22:06

2 Answers2

1
public static Photograph rotated(Photograph photo) {
    Photograph rotated_copy = new Photograph(photo.getHeight(), photo.getWidth());
    Pixel starting_pixel = photo.getPixel(0,0);

    for(int col = 0; col < photo.getWidth(); col++){
        for(int row = 0; row = photo.getHeight(); row++){
            starting_pixel = photo.getPixel(col,row);
            rotated_copy.setPixel(photo.getHeight() - row - 1, col, starting_pixel);
        }
    }
    return rotated_copy;
}

I think.

Maciej Stachowski
  • 1,708
  • 10
  • 19
0

EDIT: Hm, unfortunately that's close but doesn't quite work. Its reflecting the image while it rotates it. You could fix that though:

Instead of placing the co-ordinates in their opposite (X in Y and Y in X), you have to store them in a different order.

Think about the problem. After a 90' clockwise rotation, the co-ordinates (0,0) (0,HEIGHT) should become (0, HEIGHT), (WIDTH, 0) right?

So instead of:

starting_pixel = photo.getPixel(col,row);
rotated_copy.setPixel(row, col, starting_pixel);

you need something like

starting_pixel = photo.getPixel(col,row);
rotated_copy.setPixel(photo.getWidth() - row, col, starting_pixel);
SpacePrez
  • 1,086
  • 7
  • 15
  • 1
    Sry man, if you do it as "X becomes Y, Y becomes X", can you explain me, how the hell pixel on left top corner at {0,0} rotate to the right top corner {max_width,0} or to the left bottom corner {0,max_height}? :) – libik Oct 23 '13 at 21:57
  • @libik ah okay you're right, its both rotating and mirroring the image, so that's not quite right. I've modified my answer. – SpacePrez Oct 23 '13 at 22:02