Java BufferedImage Flip flipImageVertically(BufferedImage theImage)

Here you can find the source of flipImageVertically(BufferedImage theImage)

Description

Flips the supplied BufferedImage vertically.

License

Open Source License

Parameter

Parameter Description
theImage the image to flip

Declaration

public static void flipImageVertically(BufferedImage theImage) 

Method Source Code

//package com.java2s;
/*//from  w ww  . j a v a  2  s .c om
 * Copyright (c) 2013 christianr.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0.html
 * 
 * Contributors:
 *     christianr - initial API and implementation
 */

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

public class Main {
    /**
     * Flips the supplied BufferedImage vertically. This is often a necessary
     * conversion step to display a Java2D image correctly with OpenGL and vice
     * versa.
     * 
     * @param theImage the image to flip
     */
    public static void flipImageVertically(BufferedImage theImage) {
        WritableRaster raster = theImage.getRaster();
        Object scanline1 = null;
        Object scanline2 = null;

        for (int i = 0; i < theImage.getHeight() / 2; i++) {
            scanline1 = raster.getDataElements(0, i, theImage.getWidth(),
                    1, scanline1);
            scanline2 = raster.getDataElements(0, theImage.getHeight() - i
                    - 1, theImage.getWidth(), 1, scanline2);
            raster.setDataElements(0, i, theImage.getWidth(), 1, scanline2);
            raster.setDataElements(0, theImage.getHeight() - i - 1,
                    theImage.getWidth(), 1, scanline1);
        }
    }
}

Related

  1. flipHorizontally(BufferedImage srcImage)
  2. flipImage(BufferedImage image)
  3. flipImage(final BufferedImage image, final boolean horizontal, final boolean vertical)
  4. flipImage(Object original, boolean flipX, boolean flipY)
  5. flipImageHorizontally(BufferedImage image)
  6. flipVertical(BufferedImage im)
  7. flipVertically(BufferedImage image)
  8. flipVertically(BufferedImage image)
  9. flipVertically(BufferedImage image, RenderingHints hints)