Here you can find the source of rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage)
public static BufferedImage rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage)
//package com.java2s; import java.awt.Point; import java.awt.image.BufferedImage; import java.awt.image.ColorModel; import java.awt.Transparency; import java.awt.color.ColorSpace; import java.awt.image.ComponentColorModel; import java.awt.image.ComponentSampleModel; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; import java.awt.image.DataBufferUShort; import java.awt.image.Raster; import java.awt.image.SampleModel; import java.awt.image.WritableRaster; public class Main { public static BufferedImage rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage) { int srcWidth = srcImage.getWidth(); int srcHeight = srcImage.getHeight(); ColorModel srcColorModel = srcImage.getColorModel(); SampleModel srcSampleModel = srcImage.getSampleModel(); WritableRaster srcRaster = srcImage.getRaster(); DataBuffer srcDataBuffer = srcRaster.getDataBuffer(); int srcDataBufferOffset = srcDataBuffer.getOffset(); //System.err.println("BufferedImageUtilities.resample(): srcDataBuffer is "+srcDataBuffer.getClass().getName()); //System.err.println("BufferedImageUtilities.resample(): srcDataBuffer.getOffset() is "+srcDataBufferOffset); int srcNumBands = srcRaster.getNumBands(); if (srcNumBands != 1 || !(srcDataBuffer instanceof DataBufferUShort || srcDataBuffer instanceof DataBufferByte)) { System.err.println(/*ww w. ja va2 s.c om*/ "BufferedImageUtilities.rotateAndFlipSwappingRowsAndColumns(): cannot do our own rotating"); return srcImage; // Give up if we don't know how to get pixels } int dstWidth = srcHeight; int dstHeight = srcWidth; BufferedImage dstImage = null; if (srcDataBuffer instanceof DataBufferUShort) { //System.err.println("BufferedImageUtilities.rotateAndFlipSwappingRowsAndColumns(): DataBufferUShort"); ColorModel dstColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] { 16 }, false, // has alpha false, // alpha premultipled Transparency.OPAQUE, DataBuffer.TYPE_USHORT); SampleModel dstSampleModel = new ComponentSampleModel(DataBuffer.TYPE_USHORT, dstWidth, dstHeight, 1, dstWidth, new int[] { 0 }); short srcPixels[] = ((DataBufferUShort) srcDataBuffer).getData(); int srcPixelsLength = srcPixels.length; int dstPixelsLength = dstWidth * dstHeight; short dstPixels[] = new short[dstPixelsLength]; for (int srcYandDstX = 0; srcYandDstX < srcHeight; ++srcYandDstX) { int srcRowOffset = srcDataBufferOffset + srcYandDstX * srcWidth; for (int srcXandDstY = 0, dstPixelOffset = srcYandDstX; srcXandDstY < srcWidth; ++srcXandDstY, dstPixelOffset += dstWidth) { dstPixels[dstPixelOffset] = srcPixels[srcRowOffset + srcXandDstY]; } } DataBuffer dstDataBuffer = new DataBufferUShort(dstPixels, dstWidth, 0); WritableRaster dstRaster = Raster.createWritableRaster(dstSampleModel, dstDataBuffer, new Point(0, 0)); dstImage = new BufferedImage(dstColorModel, dstRaster, true, null); // no properties hash table } else if (srcDataBuffer instanceof DataBufferByte) { //System.err.println("BufferedImageUtilities.rotateAndFlipSwappingRowsAndColumns(): DataBufferByte"); ColorModel dstColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), new int[] { 8 }, false, // has alpha false, // alpha premultipled Transparency.OPAQUE, DataBuffer.TYPE_BYTE); SampleModel dstSampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, dstWidth, dstHeight, 1, dstWidth, new int[] { 0 }); byte srcPixels[] = ((DataBufferByte) srcDataBuffer).getData(); int srcPixelsLength = srcPixels.length; int dstPixelsLength = dstWidth * dstHeight; byte dstPixels[] = new byte[dstPixelsLength]; for (int srcYandDstX = 0; srcYandDstX < srcHeight; ++srcYandDstX) { int srcRowOffset = srcDataBufferOffset + srcYandDstX * srcWidth; for (int srcXandDstY = 0, dstPixelOffset = srcYandDstX; srcXandDstY < srcWidth; ++srcXandDstY, dstPixelOffset += dstWidth) { dstPixels[dstPixelOffset] = srcPixels[srcRowOffset + srcXandDstY]; } } DataBuffer dstDataBuffer = new DataBufferByte(dstPixels, dstWidth, 0); WritableRaster dstRaster = Raster.createWritableRaster(dstSampleModel, dstDataBuffer, new Point(0, 0)); dstImage = new BufferedImage(dstColorModel, dstRaster, true, null); // no properties hash table } return dstImage; } }