Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / MFC
Article

Anti Aliased Image Rotation

Rate me:
Please Sign up or sign in to vote.
4.57/5 (18 votes)
11 Nov 20052 min read 104.3K   4K   53   18
Perfect image rotation using geometry.

Image 1

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.

Image 2

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAaform Pin
msg5558-Mar-06 5:36
msg5558-Mar-06 5:36 
Questionaarot fills more and more memory and finally crashes Pin
mephistho7-Mar-06 2:19
mephistho7-Mar-06 2:19 
AnswerRe: aarot fills more and more memory and finally crashes Pin
msg5557-Mar-06 10:45
msg5557-Mar-06 10:45 
QuestionRe: aarot fills more and more memory and finally crashes Pin
mephistho7-Mar-06 20:35
mephistho7-Mar-06 20:35 
AnswerRe: aarot fills more and more memory and finally crashes Pin
msg5558-Mar-06 5:31
msg5558-Mar-06 5:31 
You're not deleting your original un-rotated image.

After you call rotate you should delete it like this.

DeleteObject(hsrcbmp)
GeneralRe: aarot fills more and more memory and finally crashes Pin
mephistho8-Mar-06 20:47
mephistho8-Mar-06 20:47 
GeneralNew Aarot Pin
msg55516-Nov-05 15:35
msg55516-Nov-05 15:35 
GeneralHard work but useful. Pin
Iain Clarke, Warrior Programmer14-Nov-05 9:20
Iain Clarke, Warrior Programmer14-Nov-05 9:20 
GeneralInteresting. Pin
jkoorr114-Nov-05 3:52
jkoorr114-Nov-05 3:52 
GeneralSorry but I don't see it the same Pin
Teashirt213-Nov-05 15:14
Teashirt213-Nov-05 15:14 
GeneralRe: Sorry but I don't see it the same Pin
msg55513-Nov-05 16:35
msg55513-Nov-05 16:35 
GeneralPerhaps too complicate! Pin
Luca Piccarreta12-Nov-05 3:20
Luca Piccarreta12-Nov-05 3:20 
GeneralRe: Perhaps too complicate! Pin
msg55512-Nov-05 5:25
msg55512-Nov-05 5:25 
GeneralRe: Perhaps too complicate! Pin
Luca Piccarreta12-Nov-05 6:44
Luca Piccarreta12-Nov-05 6:44 
GeneralRe: Perhaps too complicate! Pin
maihem13-Nov-05 0:21
maihem13-Nov-05 0:21 
GeneralRe: Perhaps too complicate! Pin
Luca Piccarreta13-Nov-05 0:43
Luca Piccarreta13-Nov-05 0:43 
GeneralRe: Perhaps too complicate! Pin
maihem13-Nov-05 1:17
maihem13-Nov-05 1:17 
GeneralRe: Perhaps too complicate! Pin
Craig Muller21-Nov-05 11:28
Craig Muller21-Nov-05 11:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.