Class ArrayRotation
The rotation by 90 and 270 degrees in-place (i.e., without using a second array to copy to) is based on ideas and code developed by others. See Rotation of arrays by Thomas W. Christopher.
I also got very useful advice from Hans-Bernhard Broeker and others in comp.graphics.algorithms. There is a thread titled In-place rotation of pixel images starting Oct 11, 2000.
Note: This class should be adjusted if Java ever supports genericity. Then rotation functionality could be provided for all kinds of arrays.
- Author:
- Hans-Bernhard Broeker, Thomas W. Christopher, Marco Schmidt
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckPixelArray(int[] pixels, int width, int height) This method checks several properties of the arguments.static final int[]flip(boolean inPlace, int[] pixels, int width, int height) Flips the image given by the arguments.private static final int[]flip(int[] pixels, int width, int height) private static final voidflipInPlace(int[] pixels, int width, int height) Flips the argument image, i.e., the top line becomes the bottom line and vice versa, etc.static int[]rotate180(boolean inPlace, int[] pixels, int width, int height) Rotates the argument image by 180 degrees.static voidrotate180(int width, int height, byte[] src, int srcOffset, byte[] dest, int destOffset) static voidrotate90Left(int width, int height, byte[] src, int srcOffset, byte[] dest, int destOffset) static int[]rotate90Right(boolean inPlace, int[] pixels, int width, int height) private static int[]rotate90Right(int[] pixels, int width, int height) static voidrotate90Right(int width, int height, byte[] src, int srcOffset, byte[] dest, int destOffset) private static voidrotateInPlace180(int[] pixels, int width, int height) private static voidrotateInPlace90Right(int[] pixels, int width, int height)
-
Constructor Details
-
ArrayRotation
private ArrayRotation()
-
-
Method Details
-
checkPixelArray
public static void checkPixelArray(int[] pixels, int width, int height) This method checks several properties of the arguments. If any of the properties is not fulfilled, an explainingIllegalArgumentExceptionis thrown. Otherwise, nothing happens. This method is supposed to be called at the beginning of several other methods in this class. Properties checked:pixelsis non-nullwidthandheightare larger than zero- number of elements in
pixelsis at leastwidthtimesheight
-
flipInPlace
private static final void flipInPlace(int[] pixels, int width, int height) Flips the argument image, i.e., the top line becomes the bottom line and vice versa, etc. This method first checks the validity of the arguments that define the image by a call tocheckPixelArray(int[],int,int). Then the image data is flipped in place, no additional memory is required. Note that after applying this operation twice you will get the original image back.- Parameters:
pixels- the array of pixels that form the image to be flippedwidth- the horizontal resolution of the image; must be larger than 0height- the vertical resolution of the image; must be larger than 0- Throws:
IllegalArgumentException- if the arguments are invalid
-
flip
private static final int[] flip(int[] pixels, int width, int height) -
flip
public static final int[] flip(boolean inPlace, int[] pixels, int width, int height) Flips the image given by the arguments. The inPlace argument determines if the pixels array is modified or not. If inPlace is true, no additional array is allocated. Otherwise, an array of width times height items is allocated and the flipped image will be stored in this array.- Parameters:
inPlace- iftrueall work is done on thepixelsarray; otherwise, a second array is allocated and thepixelsarray remains unmodifiedpixels- the array of pixels that form the image to be flippedwidth- the horizontal resolution of the image; must be larger than 0height- the vertical resolution of the image; must be larger than 0- Returns:
- the flipped image as int array; equals
pixelsifinPlaceis true - Throws:
IllegalArgumentException- if the pixel resolution is invalid or the pixels array is not initialized or its length smaller thanwidthtimesheight
-
rotate180
public static int[] rotate180(boolean inPlace, int[] pixels, int width, int height) Rotates the argument image by 180 degrees. The resulting image will have exactly the same pixel resolution. Note that this operation is the same as two consecutive 90 degree rotations in the same direction. Another way of implementing a 180 degree rotation is first flipping and then mirroring the original image (or vice versa).If
inPlaceis true, the rotation is done on the argumentpixelsarray. Otherwise a new array of sufficient length is allocated and the rotated image will be stored in this new array, not modifying the content of thepixelsarray.- Parameters:
inPlace- determines whether the rotated image is written to the argument arraypixels- the array of pixels that form the image to be rotatedwidth- the horizontal resolution of the image; must be larger than 0height- the vertical resolution of the image; must be larger than 0- Returns:
- the flipped image as int array; equals
pixelsifinPlaceis true - Throws:
IllegalArgumentException- if the pixel resolution is invalid or the pixels array is not initialized or its length smaller thanwidthtimesheight
-
rotateInPlace180
private static void rotateInPlace180(int[] pixels, int width, int height) -
rotate90Left
public static void rotate90Left(int width, int height, byte[] src, int srcOffset, byte[] dest, int destOffset) -
rotate90Right
public static void rotate90Right(int width, int height, byte[] src, int srcOffset, byte[] dest, int destOffset) -
rotate180
public static void rotate180(int width, int height, byte[] src, int srcOffset, byte[] dest, int destOffset) -
rotateInPlace90Right
private static void rotateInPlace90Right(int[] pixels, int width, int height) -
rotate90Right
private static int[] rotate90Right(int[] pixels, int width, int height) -
rotate90Right
public static int[] rotate90Right(boolean inPlace, int[] pixels, int width, int height)
-