Fast Drawing in Delphi (part 1) part-2

Introduction
This articles is about drawing in the Delphi programming language.

In this part - 1, the very basic principles are covered : single pixels.
The described methods may be used as the basis for more complicated geometry.
And, by having full control of the drawing process, the programmer may add enhancements
that Delphi does not offer, such as: Part-2, to be published 1st quarter 2009, will cover lines and ellipses,
using clipping rectangles, large penwidth dash-dot lines and 4 level drawing planes.

In Delphi, several ways exist to draw lines or fill shapes.
This article focusses on the comparison between these methods.

Ultimately, all drawing amounts to setting a single pixel on a bitmap canvas.
Therefore, single pixels are written and the time required is measured.
Each drawing method is used in two type of actions About the program
The program can be downloaded by clicking on the image at the left top of this page.

On the Form, several BitButtons are placed.
Pressing a button triggers a specific action and the time needed per pixel is indicated
in the Statictext.

All drawing is done in a 100 * 100 bitmap, named bm.
The pixelformat is 32 bit.
A paintbox is added to show the contents of the bitmap after drawing.

The time is obtained from the system clock, which counts milliseconds,
by the GetTickCount function.
For accurate measurement, the operation has to be repeated many times.
Procedure ShowTime(n) presents the elapsed time after writing n pixels.

Bitmap Layout
Figure below shows the way pixels are organised in the bitmap. Pixels are represented by 32-bit unsigned words.
Pixels are accessable by their horizontal- and vertical indexes.
Note the difference between the way the picture is stored in the bitmap
and the internal windows format.
The statement reads the pixel from column 2, row 3 , converts the format to windows internal
and stores this value in variable pixvalue, which must be of type LongInt or DWORD.

Another way to acces the pixels is by using a pointer to the first element of each row.
The ScanLine property of the bitmap provides this pointer.
Now, we can acces a selected row in the same way as an array.
The ScanLine property provides a much faster access of the bitmap,
especially when many operations on the same row take place.
Note: by directly accessing the bitmap, the 32-bit format is used, no conversion
to the internal windows format takes place.

As we will see from the time measurements later, the scanline statement itself
is rather time consuming.
Therefore, more time can be saved by minimizing its use.
See figure below, showing pointers to the memory storing the bitmap When pointer Pbase points to the memory location of pixel [0,0],
Pbase + 4 points to [1,0] and Pbase - 16 points to [0,1]

Using ScanLine only once to obtain Pbase, we can access pixels in the following way
Drawing Lines
The "pixels[ ]" method
The "LinoTo" method The "ScanLine" method The "Xdot" method Filling Rectangles
The "pixels[ ]" method The "FillRect" method The "ScanLine" method The "XfillRect" method Time measurements
Times are in nanoseconds per pixel. The remaining part of the test program