The purpose of this program, FlipReverseRotate.EXE, is to demonstrate how to flip (top-to-bottom) and/or reverse (left-to-right) a bitmap in memory and display the results on the screen. Three methods to flip/reverse are compared: Scanline, CopyRect, StretchBlt. In addition, a bitmap can be rotated any multiple of 90 degrees, namely 0, 90, 180, or 270 degrees counterclockwise, but only with the Scanline method.
Materials and Equipment
Delphi 3 (to recompile)
Sunflower.BMP, PepsiChallengeSmall.BMP, or other 24-bits/pixel BMP files
VGA display in high color or true color mode
The ModifyOriginalImage procedure is called when the BMP file is read, or when the Flip or Reverse checkboxes change, or when the Method changes. This procedure calls a BitmapFlipReverse function, which flips or reverses the original bitmap, based on which Method is selected.. Each BitmapFlipReverse function creates a new bitmap that must be freed when the modified bitmap is no longer needed.
According to a July 25, 1997 post by David Ullrich to comp.lang.pascal.delphi.misc in response to a question "How to flip a bitmap?" David suggests: "You can flip stuff using CopyRect (or BitBlt or StretchBlt or whatever) by using rects that have Left to the right of Right and Top below Bottom, as in say: Canvas.CopyRect(Rect(0,0,300,300), Canvas, Rect(300,0,0,300));" If you don't need to process the pixels while flipping or reversing them, then this may be a good alternative. This advice was quite accurate except for a minor "-1" adjustment was necessary (shown in the code) whenever a flip or reversal takes place.
The CopyRect method calls the StretchBlt API call. A separate BitmapFlipReverseStretchBlt routine was written while trying to find why there was an "off by 1" error (which was solved by the "-1" adjustment seen in the code). This separate routine can be used for quick comparison of how to use CopyRect versus StretchBlt.
Thomas Kowalski suggested a slightly different approach with routines that operate much more quickly. See his SpiegellnHorizontal (Horizontal Mirror), SpiefelLnVertikal (Vertical Mirror), Drehen90Grad (90-degree rotation), Drehen180Grad (180-degree rotation) and Drehen270Grad (270-degree rotation) routines in his Unit7.
Dave Shapiro's unit for rotating TBitmaps 90, 180, or 270 degrees. Works
in D1, D2, D3, and D4. Not entirely optimized, but quick enough. It rotates a 1600x1200
image in 800 msec on his P2 400.
See the RotateScanline Lab Report for a rotation of any number of degrees.
Flip and/or Reverse followed by Rotation
The following data were recorded with a 166 MHz Pentium using the Sunflower.BMP bitmap (before Rotate 0 degrees was implemented):
mean ± st. dev.
|Copy (No Flip, No Reverse)||45 ± 4||28 ± 4||26 ± 4|
|Flip||47 ± 4||77 ± 7||80 ± 10|
|Reverse||53 ± 6||83 ± 7||77 ± 7|
|Flip & Reverse||55 ± 5||73 ± 4||74 ± 3|
The BitmapFlipReverse functions demonstrate how to write a Delphi function that returns a newly created TBitmap. This technique may be very useful in a variety of image processing tasks.
The technique shown here with Scanline is only for a 24-bit color bitmap. Similar, but different, code is needed for other PixelFormats.
There is no statistical difference between CopyRect and StretchBlt (which isn't surprising since CopyRect is a StretchBlt with a little bit of overhead). CopyRect/StretchBlt is faster than Scanline for copying a 24-bit color bitmap. But Scanline is faster than CopyRect/StretchBlt for Flipping or reversing a bitmap.
After a 90 degree rotation, a "flip" becomes a "reverse" and vice versa.
See other "Bitmap Rotation" links on Delphi Graphics Image Processing page.
TBitmap, TImage, Scanline, CopyRect, StretchBlt, Reverse Bitmap, Flip Bitmap, Bitmap Function, TRGBArray, pRGBArray, PixelFormat, pf24bit
Delphi 3 Source and EXE (299 KB): FlipReverseRotate.ZIP
Updated 18 Feb 2002
since 1 Nov 1998