Java BufferedImage Flip flipVertical(BufferedImage im)

Here you can find the source of flipVertical(BufferedImage im)

Description

flip Vertical

License

LGPL

Declaration

public static BufferedImage flipVertical(BufferedImage im) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.awt.image.*;

public class Main {
    public static BufferedImage flipVertical(BufferedImage im) {
        int height = im.getHeight();
        int width = im.getWidth();

        DataBuffer db = im.getRaster().getDataBuffer();
        if (db instanceof DataBufferInt) {
            int imdata[] = ((DataBufferInt) db).getData();

            BufferedImage im2 = new BufferedImage(width, height, im.getType());
            int imdata2[] = ((DataBufferInt) (im2.getRaster().getDataBuffer())).getData();

            int stride = width;
            for (int y = 0; y < height; y++)
                System.arraycopy(imdata, y * stride, imdata2, (height - 1 - y) * stride, stride);

            return im2;
        }//w w w  .j a  v  a2s . c om

        if (db instanceof DataBufferByte) {
            byte imdata[] = ((DataBufferByte) db).getData();

            BufferedImage im2 = new BufferedImage(width, height, im.getType());
            byte imdata2[] = ((DataBufferByte) (im2.getRaster().getDataBuffer())).getData();

            int bytes_per_pixel = 0;
            switch (im.getType()) {
            case BufferedImage.TYPE_4BYTE_ABGR:
            case BufferedImage.TYPE_4BYTE_ABGR_PRE:
                bytes_per_pixel = 4;
                break;
            case BufferedImage.TYPE_3BYTE_BGR:
                bytes_per_pixel = 3;
                break;
            case BufferedImage.TYPE_BYTE_GRAY:
                bytes_per_pixel = 1;
                break;
            case BufferedImage.TYPE_BYTE_BINARY:
            case BufferedImage.TYPE_BYTE_INDEXED:
                return null; //XXX These types are not supported by this method
            }
            int stride = bytes_per_pixel * width;
            for (int y = 0; y < height; y++)
                System.arraycopy(imdata, y * stride, imdata2, (height - 1 - y) * stride, stride);

            return im2;
        }

        assert (false);
        return null;
    }
}

Related

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