Introduction
(Written by Mark Gordon.) Aarot (Anti Aliased Image Rotation Algorithm) can rotate images by any angle. Unlike most image rotation algorithms, Aarot uses geometry to decide how much representation each pixel deserves. It does this by overlapping the source bitmap onto the destination bitmap at an angle and calculating for every source pixel the area of overlap over each destination pixel. With this value, Aarot can weigh how much of each source pixel should be represented in each destination pixel, thus creating the best rotation possible.
The following image demonstrates how Aarot works. On the left is the un-rotated source image. In the middle, the rotated source image has been overlaid on top of the destination image at a 40 degree angle. Then Aarot finds the area of overlap of each of the source pixel in each destination pixel. It uses these values to weigh how much color information of each source pixel belongs in each destination pixel.
Unfortunately, quality comes at a price. On my computer (1.81 GHz, AMD Athlon 64 Processor) it took just under 15 seconds to rotate a 1152 x 864 image by 61 degrees.
Implementation
HBITMAP aarot::rotate(HBITMAP src, double rotation,
aar_callback callbackfunc, int bgcolor,
bool autoblend)
Src
: The handle to the Bitmap
to be rotated.
Rotation
: The degree by which the image should be rotated counter-clockwise.
callbackfunc
: A pointer to the callback function. This function will be notified about the percentage of rotation completed as well as gives the opportunity to stop the rotation. This parameter may be NULL
. See Using the Callback Function for more information.
BgColor
: The color of the background where the rotated bitmap does not overlap the destination bitmap.
AutoBlend
: Decides if the edges of the rotated image should be blended with the background color defined by BgColor
. If false
, the rgbReserved
byte of each pixel will be set to the appropriate alpha values so that the rotated image can be blended onto another image later without a harsh edge.
Return
: A handle to the rotated Bitmap
object. Returns NULL
if there are errors or if the function was told to quit by the callback function.
Using the Callback Function
The callback function should share the following signature:
bool AarotCallbackFunc(double percentdone)
{
...
}
The double
passed to the callback function is a number ranging from 0 to 1, representing how close the main algorithm is to completion. (0 = 0%, .5 = 50%, 1 = 100%) If the callback function returns true
, the main algorithm will exit and return a NULL
bitmap. In this way, the user is given the opportunity to stop the rotation.