Here you can find the source of flipHorizontally(BufferedImage srcImage)
public static void flipHorizontally(BufferedImage srcImage)
//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 } }