Bitmap Rotation

Fastgraph includes three functions for 2D bitmap rotation. The fg_rotate() function rotates a 256-color bitmap about its center by an arbitrary angle, and fg_rotdcb() does the same thing for direct color bitmaps. Unless you're rotating a bitmap by a multiple of 90 degrees, the size of the rotated bitmap may be larger than the original bitmap. In such cases, fg_rotsize() can determine the size of a rotated bitmap before actually performing the rotation. You can use fg_rotsize() with either 256-color or direct color bitmaps.

The fg_rotsize() function's first two parameters are the width and height of the original bitmap in pixels. Its third parameter is the rotation angle, multiplied by 10, expressed counterclockwise from the positive x axis. The last two fg_rotsize() parameters receive the width and height of the rotated bitmap, again in pixels. For example, to determine the size of a 40x20 bitmap rotated 45 degrees, you would call

C/C++:

fg_rotsize(40,20,45*10,&NewWidth,&NewHeight);

Delphi:

fg_rotsize(40,20,45*10,NewWidth,NewHeight);

Visual Basic:

Call fg_rotsize(40, 20, 45 * 10, NewWidth, NewHeight)

Again, note how we multiply the rotation angle by 10. To determine the number of bytes required for the rotated bitmap, just pass the rotated width and height values to fg_imagesiz():

C/C++:

nBytes = fg_imagesiz(NewWidth,NewHeight);

Delphi:

nBytes := fg_imagesiz(NewWidth,NewHeight);

Visual Basic:

nBytes = fg_imagesiz(NewWidth, NewHeight)

The above calls work for 256-color bitmaps and direct color bitmaps. We can then pass nBytes to a function that allocates dynamic memory and use that memory to hold the rotated bitmap.

The actual bitmap rotation takes place in fg_rotate() or fg_rotdcb(). Each function's first parameter is the name of the array that holds the original (unrotated) bitmap, and the second parameter is the name of the array that receives the rotated bitmap. The only difference is that fg_rotate() expects and creates 256-color bitmaps, while fg_rotdcb() works with direct color bitmaps. The third and fourth parameters define the width and height of the unrotated bitmap in pixels. The fifth parameter defines the rotation angle, which must be multiplied by 10 just as with fg_rotsize(). Thus, to perform the 45-degree rotation of our 40x20 256-color bitmap stored in the array Source, we would use

C/C++ and Delphi:

fg_rotate(Source,Dest,40,20,45*10);

Visual Basic:

Call fg_rotate(Source(0), Dest(0), 40, 20, 45 * 10)

Upon return, the Dest array will contain the rotated 256-color bitmap. If the bitmap were a direct color bitmap, we would instead pass the same parameters to fg_rotdcb().

Note that fg_rotate() and fg_rotdcb() do not actually display the rotated bitmap, but merely rotate it. We can display it with any of the bitmap display functions, passing the bitmap width and height returned by fg_rotsize(). Assuming the Dest array is large enough to hold the rotated bitmap, we can summarize the bitmap rotation and display process for 256-color bitmaps as shown here:

C/C++:

fg_rotsize(40,20,45*10,&NewWidth,&NewHeight);
fg_rotate(Source,Dest,40,20,45*10);
fg_drwimage(Dest,NewWidth,NewHeight);

Delphi:

fg_rotsize(40,20,45*10,NewWidth,NewHeight);
fg_rotate(Source,Dest,40,20,45*10);
fg_drwimage(Dest,NewWidth,NewHeight);

Visual Basic:

Call fg_rotsize(40, 20, 45 * 10, NewWidth, NewHeight)
Call fg_rotate(Source(0), Dest(0), 40, 20, 45 * 10)
Call fg_drwimage(Dest(0), NewWidth, NewHeight)

Because the bitmap rotation functions rotate the bitmap around its center, it's easy to display a spinning bitmap. If we're displaying a bitmap with its lower left corner at (x,y), we only need to calculate the new (x,y) position such that displaying the rotated bitmap at that position leaves the bitmap center at its original position. This sounds harder than it really is:

C/C++:

fg_rotsize(OldWidth,OldHeight,Angle,&NewWidth,&NewHeight);
fg_rotate(Source,Dest,OldWidth,OldHeight,Angle);
fg_move(X-NewWidth/2,Y+NewHeight/2);
fg_drwimage(Dest,NewWidth,NewHeight);

Delphi:

fg_rotsize(OldWidth,OldHeight,Angle,NewWidth,NewHeight);
fg_rotate(Source,Dest,OldWidth,OldHeight,Angle);
fg_move(X - NewWidth div 2,y + NewHeight div 2);
fg_drwimage(Dest,NewWidth,NewHeight);

Visual Basic:

Call fg_rotsize(OldWidth, OldHeight, Angle, NewWidth, NewHeight)
Call fg_rotate(Source(0), Dest(0), OldWidth, OldHeight, Angle)
Call fg_move(X - NewWidth / 2,Y + NewHeight / 2)
Call fg_drwimage(Dest(0), NewWidth, NewHeight)

The above code is for 256-color bitmaps. For direct color bitmaps, use fg_rotdcb() instead of fg_rotate(), and fg_drawdcb() instead of fg_drwimage().

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.