Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Anti Aliased Image Rotation

0.00/5 (No votes)
11 Nov 2005 1  
Perfect image rotation using geometry.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here