Image Processing
FlipReverseRotate  Lab Report

Chinese Translation by Hector Xiang

Flip, Reverse, Rotate (by 90 degrees) a Bitmap

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

Software Requirements
Windows 95
Delphi 3 (to recompile)
Sunflower.BMP, PepsiChallengeSmall.BMP, or other 24-bits/pixel BMP files

Hardware Requirements
VGA display in high color or true color mode


  1. Double click on the FlipReverseRotate.EXE icon to start the program.
  2. Press the Load Image button and select a 24-bit color BMP file such as Sunflower.BMP, or PepsiChallengeSmall.BMP. Select the Open button. The time in milliseconds to complete the operation is shown in the upper-right corner.
  3. If desired, select or deselect the Stretch checkbox. (If checked, the whole TBitmap is shown in the TImage). For example, PepsiChallengeSmall.BMP looks best when not stretched.
  4. Check the Flip and/or Reverse checkboxes to flip the image top-to-bottom, or reverse the image left-to-right. Check one of the rotate radiogroup options to rotate the image 0, 90, 180 or 270 degrees.
  5. With a given setting for the Flip and Reverse checkboxes, experiment with the various methods. Compare the times for the various operations. The Rotation radiogroup is only present when the Scanline method is selected.
  6. Experiment with an 8-bit color image. (Only the CopyRect method can be used if the PixelFormat is not pf24bit-- this is out of convenience -- I didn't want to program for all possible PixelFormats or deal with palettes. This is not a limitation of ScanLine.)

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

Rotate = 0 degrees Flip=No Flip=Yes
Rotate = 180 degrees Flip=No Flip=Yes


Rotate = 90 degrees Flip=No Flip=Yes
Rotate = 270 degrees Flip=No Flip=Yes


The following data were recorded with a 166 MHz Pentium using the Sunflower.BMP bitmap (before Rotate 0 degrees was implemented):

mean st. dev.
5 trials
  Scanline CopyRect StretchBlt
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