lexi_lu_22
lexi_lu_22

Reputation: 51

How do I flip an image upside-down?

I was wondering if I could find some help on this problem. I was asked to use an image ("corn.jpg"), and flip it entirely upside down. I know I need to write a program which will switch pixels from the top left corner with the bottom left, and so on, but I wasn't able to get my program to work properly before time ran out. Could anyone provide a few tips or suggestions to solve this problem? I'd like to be able to write my code out myself, so suggestions only please. Please note that my knowledge of APImage and Pixel is very limited. I am programming in Java. Here is what I managed to get done.

import images.APImage; 
import images.Pixel; 
public class Test2 
{ 
  public static void main(String [] args) 
  { 
    APImage image = new APImage("corn.jpg"); 
    int width = image.getImageWidth(); 
    int height = image.getImageHeight(); 
    int middle = height / 2; 
    //need to switch pixels in bottom half with the pixels in the top half 

    //top half of image 
    for(int y = 0; y < middle; y++) 
    { 
      for (int x = 0; x < width; x++) 
      { 
        //bottom half of image 
        for (int h = height; h > middle; h++) 
        { 
          for(int w = 0; w < width; w++) 
          { 
            Pixel bottomHalf = image.getPixel(h, w); 
            Pixel topHalf = image.getPixel(x, y); 
            //set bottom half pixels to corresponding top ones? 
            bottomHalf.setRed(topHalf.getRed()); 
            bottomHalf.setGreen(topHalf.getGreen()); 
            bottomHalf.setBlue(topHalf.getBlue()); 
            //set top half pixels to corresponding bottom ones? 
            topHalf.setRed(bottomHalf.getRed()); 
            topHalf.setGreen(bottomHalf.getGreen()); 
            topHalf.setBlue(bottomHalf.getBlue()); 
          }
        }
      }
    }
    image.draw(); 
  }
}

Thank you for your help!

Upvotes: 5

Views: 4406

Answers (5)

Andrew Thompson
Andrew Thompson

Reputation: 168825

See Transforming Shapes, Text, and Images.

enter image description here

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class FlipVertical {

    public static BufferedImage getFlippedImage(BufferedImage bi) {
        BufferedImage flipped = new BufferedImage(
                bi.getWidth(),
                bi.getHeight(),
                bi.getType());
        AffineTransform tran = AffineTransform.getTranslateInstance(0, bi.getHeight());
        AffineTransform flip = AffineTransform.getScaleInstance(1d, -1d);
        tran.concatenate(flip);

        Graphics2D g = flipped.createGraphics();
        g.setTransform(tran);
        g.drawImage(bi, 0, 0, null);
        g.dispose();

        return flipped;
    }

    FlipVertical(BufferedImage bi) {
        JPanel gui = new JPanel(new GridLayout(1,2,2,2));

        gui.add(new JLabel(new ImageIcon(bi)));
        gui.add(new JLabel(new ImageIcon(getFlippedImage(bi))));

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) throws AWTException {
        final Robot robot = new Robot();
        Runnable r = new Runnable() {

            @Override
            public void run() {
                final BufferedImage bi = robot.createScreenCapture(
                        new Rectangle(0, 660, 200, 100));
                new FlipVertical(bi);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Upvotes: 4

Tesseract
Tesseract

Reputation: 8139

You want to flip the image upside down, not swap the top and bottom half. The loop could look like this.

int topRow = 0;
int bottomRow = height-1;
while(topRow < bottomRow) {
    for(int x = 0; x < width; x++) {
        Pixel t = image.getPixel(x, topRow);
        image.setPixel(x, topRow, image.getPixel(x, bottomRow));
        image.setPixel(x, bottomRow, t);
    }
    topRow++;
    bottomRow--;
}

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201439

Instead of using

Pixel bottomHalf = image.getPixel(h, w); 
Pixel topHalf = image.getPixel(x, y);
//set bottom half pixels to corresponding top ones? 
bottomHalf.setRed(topHalf.getRed()); 
bottomHalf.setGreen(topHalf.getGreen()); 
bottomHalf.setBlue(topHalf.getBlue()); 
//set top half pixels to corresponding bottom ones? 
topHalf.setRed(bottomHalf.getRed()); 
topHalf.setGreen(bottomHalf.getGreen()); 
topHalf.setBlue(bottomHalf.getBlue()); 

You should have stored the bottomHalf's RGB into a temporary Pixel and used that to set topHalf after replacing bottomHalf's values (if you follow). You could have also really used something like this.... assuming your pixel operates on integer rgb values (which would have improved your main method).

 private static final Pixel updateRGB(Pixel in, int red, int green, int blue) {
   in.setRed(red); in.setGreen(green); in.setBlue(blue);
 }

Upvotes: 0

Aaron Gotreaux
Aaron Gotreaux

Reputation: 273

If you are able to use the Graphics class, the following may be of use: http://www.javaworld.com/javatips/jw-javatip32.html

And the Graphics class documentation: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

Upvotes: 0

Amadan
Amadan

Reputation: 198324

Whenever you're swapping variables, if your language doesn't allow for simultaneous assignment (and Java doesn't), you need to use a temporary variable.

Consider this:

a = 1;
b = 2;

a = b; // a is now 2, just like b
b = a; // b now uselessly becomes 2 again

Rather than that, do this:

t = a; // t is now 1
a = b; // a is now 2
b = t; // b is now 1

EDIT: And also what @vandale says in comments :P

Upvotes: 1

Related Questions