Java BufferedImage Flip flipHorizontally(BufferedImage srcImage)

Here you can find the source of flipHorizontally(BufferedImage srcImage)

Description

flip Horizontally

License

Open Source License

Declaration

public static void flipHorizontally(BufferedImage srcImage) 

Method Source Code

//package com.java2s;

import java.awt.image.BufferedImage;

import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;

import java.awt.image.DataBufferUShort;

import java.awt.image.WritableRaster;

public class Main {
    public static void flipHorizontally(BufferedImage srcImage) {
        int srcWidth = srcImage.getWidth();
        int srcHeight = srcImage.getHeight();
        WritableRaster srcRaster = srcImage.getRaster();
        DataBuffer srcDataBuffer = srcRaster.getDataBuffer();
        //System.err.println("BufferedImageUtilities.flipHorizontally(): srcDataBuffer is "+srcDataBuffer.getClass().getName());
        int srcDataBufferOffset = srcDataBuffer.getOffset();
        //System.err.println("BufferedImageUtilities.flipHorizontally(): srcDataBuffer.getOffset() is "+srcDataBufferOffset);
        int srcNumBands = srcRaster.getNumBands();

        if (srcNumBands == 1) {
            // DataBufferShort will not be encountered ... see comments in SourceImage.java
            if (srcDataBuffer instanceof DataBufferUShort) {
                short srcPixels[] = ((DataBufferUShort) srcDataBuffer).getData();
                short rowBuffer[] = new short[srcWidth];
                for (int srcY = 0; srcY < srcHeight; ++srcY) {
                    int srcPixelOffset = srcDataBufferOffset + srcY * srcWidth;
                    for (int dstX = srcWidth - 1; dstX >= 0; --dstX) {
                        rowBuffer[dstX] = srcPixels[srcPixelOffset++];
                    }//from www  . ja va2 s .co m
                    srcPixelOffset = srcDataBufferOffset + srcY * srcWidth;
                    for (int dstX = 0; dstX < srcWidth; ++dstX) {
                        srcPixels[srcPixelOffset++] = rowBuffer[dstX];
                    }
                }
            } else if (srcDataBuffer instanceof DataBufferByte) {
                byte srcPixels[] = ((DataBufferByte) srcDataBuffer).getData();
                byte rowBuffer[] = new byte[srcWidth];
                for (int srcY = 0; srcY < srcHeight; ++srcY) {
                    int srcPixelOffset = srcDataBufferOffset + srcY * srcWidth;
                    for (int dstX = srcWidth - 1; dstX >= 0; --dstX) {
                        rowBuffer[dstX] = srcPixels[srcPixelOffset++];
                    }
                    srcPixelOffset = srcDataBufferOffset + srcY * srcWidth;
                    for (int dstX = 0; dstX < srcWidth; ++dstX) {
                        srcPixels[srcPixelOffset++] = rowBuffer[dstX];
                    }
                }
            } else {
                System.err.println(
                        "BufferedImageUtilities.flipHorizontally(): cannot flip unsupported DataBuffer type of "
                                + srcDataBuffer.getClass().getName());
            }
        } else {
            System.err.println(
                    "BufferedImageUtilities.flipHorizontally(): cannot flip more than one band; number of bands is"
                            + srcNumBands);
        }
        // May or may not have been changed, but would have been changed in place
    }
}

Related

  1. flipAroundX(BufferedImage image)
  2. flipBothVerticallyAndHorizontallyWithAffineTransformOp(BufferedImage srcImage)
  3. flipBufferedImage(BufferedImage bufferedImage)
  4. flipBufferedImageVertically(BufferedImage bufferedImage)
  5. flipHorizontal(BufferedImage bufferedImage)
  6. flipImage(BufferedImage image)
  7. flipImage(final BufferedImage image, final boolean horizontal, final boolean vertical)
  8. flipImage(Object original, boolean flipX, boolean flipY)
  9. flipImageHorizontally(BufferedImage image)