Anti-Aliased Image Rotation (Aarot)

written by Mark Gordon (msg555)

Image rotation isn't as straightforward as it may appear, especially when faced with the fact that, although small, a pixel is not a point. In fact, it's a 1 x 1 square, which leads to the question of how best to rotate it. Some algorithms will ingore this idea altogether and treat a pixel like a point. Others will do an artificial zooming on the image to make the smallest unit of color even smaller so the aliasing won't be as evident. However, it is possible to rotate a pixel as a square, and Aarot does it. By treating a pixel like a square, you get the best rotation possible.

To learn how to perform anti-aliased image rotation, please see the companion article.

Aarot works by rotating each of the 1 x 1 squares from the source bitmap and overlapping them on the destination bitmap. Because pixels can only hold one discrete color, it is necessary to find how much representation each source pixel has in each destination pixel. Aarot does this by finding the area of each source pixel over each destination pixel and adding representation proportional to that area.

See the following image. On the left, you see the unrotated image; the next step shows the rotated image overlapped on the destination bitmap. In the final step, the amount of representation each source pixel should receive in each destination pixel is calculated and added to the destination pixels

Unfortunatly, this algorithm does not complete in real time so it is not useful for games or things of the like. I wrote the algorithm for C++ and VB (C++ is faster) and provided a DLL for implementing the C++ algorithm's speed with VB. (There are sample projects as well.)

I included a comparison between Aarot and Photoshop (CS2) so you can see the difference between the two. Here is an example of Aarot's work:

Logo rotated 38.47 degrees

Implementation

C++ Prototype

HBITMAP aarot::rotate(HBITMAP src, double rotation,
                      aar_callback callbackfunc, int bgcolor,
                      bool autoblend)

VB Function

Public Function Aarot(Src As Long, Rotation As Double, _
                      CallBackPtr As Long, BgColor As Long, _
                      AutoBlend As Boolean) As Long

VB DLL Declare

Private Declare Function aarotate Lib "aarot.dll" _
   Alias "_Z17AntiAliasedRotateidiii" _
   (ByVal hSrcBmp As Long, ByVal Rotation As Double, _
    ByVal Callbackfunc As Long, ByVal BgColor As Long, _
    ByVal AutoBlend As Long) As Long
  • Src: A handle to the bitmap to be rotated.
  • Rotation: The degrees that the image should be rotated counter-clockwise.
  • callbackfunc (CallBackPtr): A pointer to the callback function. This function will be notified what percentage of the rotation has completed as well as given the opportunity to stop the rotation. This parameter may be NULL (or 0 in VB). See Using the Callback Function below for information about the Callback structure.
  • BgColor: The color of the background where the rotated bitmap does not overlap the destination bitmap.
  • AutoBlend: Should the edges of the rotated image be blended with the background color defined by BgColor? If false, the rgbReserved byte of each pixel will be set to the approriate alpha values so that the rotated image can be blended onto another image later without a harsh edge.

Using the Callback Function

In C++, the callback function should look like this:

bool AarotCallbackFunc(double percentdone)
{
   ...
}

where percentdone is the amount of the image that has processed. 0 is 0%, 1 is 100%.

If you are using the DLL in VB, your callback function should look like this:

Public Function AarotCallbackFunc(ByVal Percentage As Double) As Long
...
End Function

On the other hand, if you are using the VB module, your callback should look like this (due to a need to have 16 bytes' worth of parameters to use CallWindowProc):

Public Function AarotCallbackFunc(ByVal Percentage As Double, _
                                  ByVal Numerator As Long, _
                                  ByVal Denominator As Long) As Long
...
End Function

where Percentage means the same thing, and Numerator and Denominator make up the rationale used to calculate Percentage.

No matter what you're using, if your callback function returns true (anything but 0), the main algorithm will immediatly exit (after clearing the memory) and return a null bitmap.



Downloads

Comments

  • Use this function in VBA (Access)

    Posted by ymkx on 01/23/2007 03:18am

    This function is what I have been looking for, for over 3 months now. Thanks, it is great! The problem is that Access does not have a PictureBox object where I can assign the result of the rotation. How can I send the result to an Image Object? yaacov@rampal.com

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds