Java BufferedImage Rotate rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage)

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

Description

rotate And Flip Swapping Rows And Columns

License

Open Source License

Declaration

public static BufferedImage rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage) 

Method Source Code

//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;
    }
}

Related

  1. rotate(final BufferedImage image, final int angle)
  2. rotate(Graphics2D g, BufferedImage newImg, int x, int y, int width, int height, int angle, int originX, int originY)
  3. rotate90(BufferedImage bi)
  4. rotate90(BufferedImage img, boolean left)
  5. rotate90ToRight(BufferedImage inputImage)
  6. rotateBufferedImage(BufferedImage bufferedImage, double angle)
  7. rotateByRightAngle(BufferedImage source, int degrees)
  8. rotateFortyFiveClockwise(BufferedImage img)
  9. rotateImage(BufferedImage image, double angle)